diff --git a/Cargo.lock b/Cargo.lock index 7bff165..5466462 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1753,7 +1753,9 @@ dependencies = [ "rust-i18n", "serde", "serde_json", + "smol", "time-format", + "zbus 6.5.0", ] [[package]] @@ -1993,7 +1995,7 @@ dependencies = [ "wasm-bindgen-futures", "web-sys", "windows", - "zbus", + "zbus 5.19.0", ] [[package]] @@ -3335,6 +3337,16 @@ dependencies = [ "serde", ] +[[package]] +name = "socket2" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3d1e2c7f27f8d4cb10542a02c49005dbd6e93095799d6f3be745fae9f8fedd4" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + [[package]] name = "softbuffer" version = "0.4.8" @@ -4351,11 +4363,38 @@ dependencies = [ "uuid", "windows-sys 0.61.2", "winnow 1.0.5", - "zbus_macros", + "zbus_macros 5.19.0", "zbus_names", "zvariant", ] +[[package]] +name = "zbus" +version = "6.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a7f9f5dd97fa527c1202ba1cb35d48e07f77d4de8542317c854f7aef22b0668" +dependencies = [ + "async-trait", + "bitflags 2.13.2", + "endi", + "futures-core", + "hex", + "libc", + "ordered-stream", + "rustix 1.1.6", + "serde", + "serde_repr", + "socket2", + "uds_windows", + "uuid", + "windows-sys 0.61.2", + "winnow 1.0.5", + "zbus_macros 6.5.0", + "zbus_utils", + "zcheapstr", + "zruntime", +] + [[package]] name = "zbus_macros" version = "5.19.0" @@ -4371,6 +4410,18 @@ dependencies = [ "zvariant_utils", ] +[[package]] +name = "zbus_macros" +version = "6.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f5aa7f2c4ea81b7ccfb26bafefdb4d1a44dd9d4f82c8426173a3727311743e4" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.10", + "zbus_utils", +] + [[package]] name = "zbus_names" version = "4.3.5" @@ -4382,6 +4433,19 @@ dependencies = [ "zvariant", ] +[[package]] +name = "zbus_utils" +version = "6.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af55e52da295311b368c45bcae221f020e28e97a2420360170ce389218e76ec7" +dependencies = [ + "proc-macro2", + "quote", + "serde", + "syn 3.0.10", + "winnow 1.0.5", +] + [[package]] name = "zcheapstr" version = "1.3.0" @@ -4429,6 +4493,19 @@ version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b" +[[package]] +name = "zruntime" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac67096b97c00742108c4674db6bb6178adb78c4b66a506b0e4a7a9593aa5d9e" +dependencies = [ + "futures-core", + "futures-io", + "rustix 1.1.6", + "socket2", + "windows-sys 0.61.2", +] + [[package]] name = "zune-core" version = "0.5.3" diff --git a/Cargo.toml b/Cargo.toml index 88c19d0..07b0e51 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,8 +8,10 @@ edition = "2024" [dependencies] blocking = "1.6" +smol = "2" iced = { version = "0.14", default-features = false, features = ["crisp", "linux-theme-detection", "tiny-skia", "x11", "smol", "image"] } #iced = { git = "https://github.com/iced-rs/iced", branch = "vello_cpu", default-features = false, features = ["crisp", "linux-theme-detection", "vello-cpu", "x11", "smol", "image"]} +zbus = { version = "6", default-features = false, features = ["default-rt", "comms"] } iced_moving_picture = "0" log = "0.4" rust-i18n = "4" diff --git a/src/kira_system.rs b/src/kira_system.rs index 95c1bf8..60b1aa8 100644 --- a/src/kira_system.rs +++ b/src/kira_system.rs @@ -22,6 +22,7 @@ use crate::kira_disk_layout::*; use crate::kira_size::KiraSize; use std::fmt::format; use std::fs; +use std::io::Read; use std::path::Path; use std::process::Command; @@ -103,6 +104,51 @@ impl std::fmt::Display for ExecCommandError { // Implement std::error::Error impl std::error::Error for ExecCommandError {} +/// exec given sommand with arguments, capturing stderr in case or failure +pub fn exec_command_stdin( + cmd: &str, + args: &Vec<&str>, + stdin_data: &str, +) -> Result<(), ExecCommandError> { + use std::io::Write; + use std::process::{Command, Stdio}; + use std::thread; + + let mut child = Command::new(cmd) + .args(args) + .stdin(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn()?; + + let mut stdin = child + .stdin + .take() + .ok_or("Failed to capture stdin".to_string())?; + + let mut stderr = child + .stderr + .take() + .ok_or("Failed to capture stderr".to_string())?; + + let stderr_handle = thread::spawn(move || { + let mut buf = Vec::new(); + use std::io::Read; + let _ = stderr.read_to_end(&mut buf); + buf + }); + + stdin.write_all(stdin_data.as_bytes())?; + + let status = child.wait()?; + let stderr_output = stderr_handle.join().unwrap(); + + if status.success() { + Ok(()) + } else { + Err(CommandFailed::new(cmd, args, stderr_output).into()) + } +} + /// exec given sommand with arguments, capturing stderr in case or failure pub fn exec_command(cmd: &str, args: &Vec<&str>) -> Result<(), ExecCommandError> { let out = Command::new(cmd).args(args).output()?; @@ -466,3 +512,51 @@ pub fn fstabgen(mnt_dir: &str) -> Result<(), ExecCommandError> { fs::write(&fstab_file_name, fstab)?; Ok(()) } + +/// +/// Executes passwd --stdin --root chroot_dir root +/// To set new root password in specified chroot env +/// +pub fn set_root_passwd(chroot_dir: &str, root_passwd: &str) -> Result<(), ExecCommandError> { + exec_command_stdin( + "passwd", + &vec!["--stdin", "--root", chroot_dir, "root"], + root_passwd, + ) +} + +/// +/// Sends Dbus message locale1 to change input method +/// SetX11Keyboard(layout: s, model: s, variant: s, options: s, convert: b, interactive: b) +/// +pub async fn set_input_method( + layout: &str, + model: &str, + variant: &str, + options: &str, +) -> zbus::Result<()> { + use zbus::Connection; + + let conn = Connection::system().await?; + log::info!("Sending dbus message to change layout"); + log::info!( + "layout: {}, model: {}, variant: {}, options: {}", + layout, + model, + variant, + options + ); + let resp = conn + .call_method( + Some("org.freedesktop.locale1"), + "/org/freedesktop/locale1", + Some("org.freedesktop.locale1"), + "SetX11Keyboard", + &(layout, model, variant, options, true, false), + ) + .await?; + + log::info!("Dbus response: {}", resp.to_string()); + + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index b87b633..c92407c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,7 +20,7 @@ stages loading, switching between stages, and stuff. */ -use iced::widget; +use iced::{keyboard, widget}; use log; use std::process::ExitCode; @@ -33,14 +33,14 @@ use crate::stages::welcome; use crate::stages::welcome::WelcomeStage; rust_i18n::i18n!("src/locales", fallback = "en"); -mod kira_include; mod kira_color_bar; mod kira_disk_layout; -mod kira_system; +mod kira_include; mod kira_logger; mod kira_scroll_list; mod kira_size; mod kira_sysinfo; +mod kira_system; mod kira_theming; mod stage; mod stages; @@ -97,7 +97,7 @@ impl KiraState { ( Self { current_view: Views::Start, - k_config: KiraConfig ::default(), + k_config: KiraConfig::default(), stages_save: Vec::new(), }, Task::done(Message::Start), @@ -123,10 +123,10 @@ fn view(k_state: &KiraState) -> Element<'_, Message> { fn update(k_state: &mut KiraState, message: Message) -> Task { match message { Message::Start => { - log::info!("Starting!"); - k_state.current_view = Views::Welcome(WelcomeStage::new(k_state.k_config.clone())); - Task::none() - }, + log::info!("Starting!"); + k_state.current_view = Views::Welcome(WelcomeStage::new(k_state.k_config.clone())); + Task::none() + } Message::Welcome(wlc_msg) => { if let Views::Welcome(wlc_view) = &mut k_state.current_view { match wlc_view.update(wlc_msg) { @@ -164,7 +164,7 @@ fn update(k_state: &mut KiraState, message: Message) -> Task { k_state.current_view = k_state.stages_save.pop().unwrap(); Task::none() } - StageAction::None => Task::none(), + _ => Task::none(), } } else { Task::none() @@ -180,7 +180,9 @@ fn update(k_state: &mut KiraState, message: Message) -> Task { k_state.k_config = network_res; let s_state = std::mem::replace( &mut k_state.current_view, - Views::TimeZone(stages::timezone::TimeZoneStage::new(k_state.k_config.clone())), + Views::TimeZone(stages::timezone::TimeZoneStage::new( + k_state.k_config.clone(), + )), ); k_state.stages_save.push(s_state); Task::none() @@ -204,7 +206,9 @@ fn update(k_state: &mut KiraState, message: Message) -> Task { k_state.k_config = tz_res; let s_state = std::mem::replace( &mut k_state.current_view, - Views::Locale(stages::locale::LocaleStage::new(k_state.k_config.clone())), + Views::Locale(stages::locale::LocaleStage::new( + k_state.k_config.clone(), + )), ); k_state.stages_save.push(s_state); Task::none() @@ -214,7 +218,7 @@ fn update(k_state: &mut KiraState, message: Message) -> Task { k_state.current_view = k_state.stages_save.pop().unwrap(); Task::none() } - StageAction::None => Task::none(), + _ => Task::none(), } } else { Task::none() @@ -227,7 +231,9 @@ fn update(k_state: &mut KiraState, message: Message) -> Task { k_state.k_config = locale_res; let s_state = std::mem::replace( &mut k_state.current_view, - Views::Keyboard(stages::keyboard::KeyboardStage::new(k_state.k_config.clone())), + Views::Keyboard(stages::keyboard::KeyboardStage::new( + k_state.k_config.clone(), + )), ); k_state.stages_save.push(s_state); Task::none() @@ -245,27 +251,30 @@ fn update(k_state: &mut KiraState, message: Message) -> Task { Message::Keyboard(keyboard_msg) => { if let Views::Keyboard(keyboard_view) = &mut k_state.current_view { match keyboard_view.update(keyboard_msg) { - StageAction::Next(keyboard_res) => { - k_state.k_config = keyboard_res; - match stages::partition::PartitionStage::new(k_state.k_config.clone()) { - Ok(part_stage) => { - let s_state = std::mem::replace( - &mut k_state.current_view, - Views::Partition(part_stage), - ); - k_state.stages_save.push(s_state); - } - Err(ex) => { - log::error!("Error, cannot init partition stage: {}", ex); + stages::keyboard::Update::Task(t) => t.map(Message::Keyboard), + stages::keyboard::Update::StgAct(action) => match action { + StageAction::Next(keyboard_res) => { + k_state.k_config = keyboard_res; + match stages::partition::PartitionStage::new(k_state.k_config.clone()) { + Ok(part_stage) => { + let s_state = std::mem::replace( + &mut k_state.current_view, + Views::Partition(part_stage), + ); + k_state.stages_save.push(s_state); + } + Err(ex) => { + log::error!("Error, cannot init partition stage: {}", ex); + } } + Task::none() } - Task::none() - } - StageAction::Back => { - k_state.current_view = k_state.stages_save.pop().unwrap(); - Task::none() - } - _ => Task::none(), + StageAction::Back => { + k_state.current_view = k_state.stages_save.pop().unwrap(); + Task::none() + } + _ => Task::none(), + }, } } else { Task::none() @@ -278,7 +287,9 @@ fn update(k_state: &mut KiraState, message: Message) -> Task { k_state.k_config = partition_res; let s_state = std::mem::replace( &mut k_state.current_view, - Views::Security(stages::security::SecurityStage::new(k_state.k_config.clone())), + Views::Security(stages::security::SecurityStage::new( + k_state.k_config.clone(), + )), ); k_state.stages_save.push(s_state); Task::none() @@ -300,7 +311,9 @@ fn update(k_state: &mut KiraState, message: Message) -> Task { k_state.k_config = security_res; let s_state = std::mem::replace( &mut k_state.current_view, - Views::Install(stages::install::InstallStage::new(k_state.k_config.clone())), + Views::Install(stages::install::InstallStage::new( + k_state.k_config.clone(), + )), ); k_state.stages_save.push(s_state); Task::none() diff --git a/src/stage.rs b/src/stage.rs index c2d3e4a..cd72071 100644 --- a/src/stage.rs +++ b/src/stage.rs @@ -19,7 +19,7 @@ */ use crate::stages; - +use iced::Task; #[derive(Debug, Clone)] pub struct KiraConfig { @@ -50,8 +50,7 @@ impl Default for KiraConfig { } } - -#[derive(Debug, Clone)] +#[derive(Debug)] pub enum StageAction { Back, None, diff --git a/src/stages/install/mod.rs b/src/stages/install/mod.rs index e90c846..b9af502 100644 --- a/src/stages/install/mod.rs +++ b/src/stages/install/mod.rs @@ -371,6 +371,11 @@ fn config_stuff(k_config: KiraConfig) -> Result<(), ExecCommandError> { // hwclock --systohc kira_system::exec_cmd_chroot(MNT_DIR, "hwclock", &vec!["--systohc"])?; + // set root password + log::info!("Setting root password..."); + let sec_conf = k_config.res_security.as_ref().unwrap(); + kira_system::set_root_passwd(MNT_DIR, &sec_conf.root_password)?; + Ok(()) } diff --git a/src/stages/keyboard/mod.rs b/src/stages/keyboard/mod.rs index d611929..6bcb62c 100644 --- a/src/stages/keyboard/mod.rs +++ b/src/stages/keyboard/mod.rs @@ -19,8 +19,9 @@ */ use crate::kira_scroll_list::{self, ListViewMessage}; +use crate::kira_system; use crate::stage::{KiraConfig, StageAction}; -use iced::{Alignment, Length, widget}; +use iced::{Alignment, Length, Task, widget}; use log; use rust_i18n::t; use std::collections::HashMap; @@ -82,6 +83,19 @@ impl KeyboarLayout { pub fn to_result(&self) -> String { format!("{}||{}", self.layout.layout, self.variant.variant) } + pub fn xkb_layout(&self) -> String { + self.layout.layout.clone() + } + /// + /// Returns string variant or empty string if variant is "default" + /// + pub fn xkb_variant(&self) -> String { + if self.variant.variant == "default" { + "".into() + } else { + self.variant.variant.clone() + } + } } #[derive(Debug, Clone, PartialEq, Eq)] @@ -102,6 +116,9 @@ impl KeyboarModel { description: v.1.trim().to_string(), } } + pub fn xkb_model(&self) -> String { + self.model.clone() + } } impl std::fmt::Display for KeyboarLayout { @@ -122,6 +139,7 @@ pub struct KeyboardStage { keyboard_layout: Option, // selected_kbd_layout_id: Option, k_config: KiraConfig, + test_text: String, } #[derive(Debug, Clone, PartialEq)] @@ -157,22 +175,18 @@ impl KeyboardStageResult { } pub fn xkb_model(&self) -> String { - self.keyboard_model.model.clone() + self.keyboard_model.xkb_model() } pub fn xkb_layout(&self) -> String { - self.keyboard_layout.layout.layout.clone() + self.keyboard_layout.xkb_layout() } /// - /// Returns string variant or eppty string if variant is "default" - /// + /// Returns string variant or empty string if variant is "default" + /// pub fn xkb_variant(&self) -> String { - if self.keyboard_layout.variant.variant == "default" { - "".into() - } else { - self.keyboard_layout.variant.variant.clone() - } + self.keyboard_layout.xkb_variant() } } @@ -191,6 +205,8 @@ pub enum Message { SelectKeyboardModel(KeyboarModel), SelectLayout(kira_scroll_list::ListViewMessage), SelectVariant(kira_scroll_list::ListViewMessage), + TestTextChange(String), + SysLayoutChanged, // AddLayout(usize), // RemoveLayout(usize), @@ -199,6 +215,11 @@ pub enum Message { Back, } +pub enum Update { + StgAct(StageAction), + Task(Task), +} + const XKB_RULES_FILE_NAME: &str = "/usr/share/X11/xkb/rules/evdev.lst"; /// Getting list of awailable keyboard layouts from from xkb/rules/evdev.lst file. @@ -315,6 +336,7 @@ impl KeyboardStage { variant: Some(0), keyboard_layout: keyboard_layout, k_config: k_config, + test_text: String::new(), } } Err(ex) => { @@ -332,11 +354,34 @@ impl KeyboardStage { variant: None, keyboard_layout: None, k_config: k_config, + test_text: String::new(), } } } } + fn set_input_method(&self) -> Task { + log::info!("Preparing task!"); + let (layout, variant) = if let Some(kb_layout) = &self.keyboard_layout { + (kb_layout.xkb_layout(), kb_layout.xkb_variant()) + } else { + (String::new(), String::new()) + }; + let model = if let Some(kb_model) = &self.model { + kb_model.xkb_model() + } else { + String::new() + }; + log::info!("Task perform!"); + Task::perform( + async move { + let a = kira_system::set_input_method(&layout, &model, &variant, "").await; + log::info!("Result: {:?}", a); + }, + |_| Message::SysLayoutChanged, + ) + } + fn gen_result(&mut self) -> KiraConfig { self.k_config.res_keyboard = Some(KeyboardStageResult { keyboard_model: self.model.clone().expect("Keyboard model must be choosed!"), @@ -348,7 +393,7 @@ impl KeyboardStage { self.k_config.clone() } - pub fn update(&mut self, message: Message) -> StageAction { + pub fn update(&mut self, message: Message) -> Update { match message { Message::SelectLayout(ListViewMessage::Select(l_id)) => { if let Some(current_l_id) = self.layout @@ -358,12 +403,18 @@ impl KeyboardStage { self.variants = self.variants_map[self.layouts[l_id].layout.as_str()].clone(); self.variant = Some(0); + let layout = self.layouts[l_id].clone(); + let variant = self.variants[0].clone(); + self.keyboard_layout = Some(KeyboarLayout { - layout: self.layouts[l_id].clone(), - variant: self.variants[0].clone(), + layout: layout, + variant: variant, }); + + Update::Task(self.set_input_method()) + } else { + Update::StgAct(StageAction::None) } - StageAction::None } Message::SelectVariant(ListViewMessage::Select(v_id)) => { self.variant = Some(v_id); @@ -371,14 +422,21 @@ impl KeyboardStage { layout: self.layouts[self.layout.unwrap()].clone(), variant: self.variants[v_id].clone(), }); - StageAction::None + Update::Task(self.set_input_method()) } Message::SelectKeyboardModel(k_m) => { self.model = Some(k_m); - StageAction::None + Update::Task(self.set_input_method()) } - Message::Next => StageAction::Next(self.gen_result()), - Message::Back => StageAction::Back, + Message::TestTextChange(test_text) => { + let mut res: String = test_text; + res.retain(|c: char| !c.is_control()); + self.test_text = res; + Update::StgAct(StageAction::None) + } + Message::SysLayoutChanged => Update::StgAct(StageAction::None), + Message::Next => Update::StgAct(StageAction::Next(self.gen_result())), + Message::Back => Update::StgAct(StageAction::Back), } } @@ -406,7 +464,7 @@ impl KeyboardStage { crate::kira_scroll_list::list_view( &self.layouts, self.layout, - Length::Shrink + Length::Fill ) .map(Message::SelectLayout) ], @@ -415,15 +473,23 @@ impl KeyboardStage { crate::kira_scroll_list::list_view( &self.variants, self.variant, - Length::FillPortion(1) + Length::Fill ) .map(Message::SelectVariant) ], ] .spacing(10) - .height(Length::Shrink), + .height(Length::Fill), + widget::row![ + widget::text("Test Keyboard:"), + widget::text_input("", &self.test_text).on_input(Message::TestTextChange), + ] + .spacing(2) + .align_y(Alignment::Center) + .width(Length::Fill), ] .spacing(10) + .height(Length::Fill) .align_x(Alignment::Center), ) } else {