]> git.proxmox.com Git - pve-installer.git/commitdiff
tui: apply keyboard layout immediately after selecting it
authorChristoph Heiss <c.heiss@proxmox.com>
Thu, 15 Jun 2023 09:51:24 +0000 (11:51 +0200)
committerChristoph Heiss <c.heiss@proxmox.com>
Thu, 15 Jun 2023 09:51:24 +0000 (11:51 +0200)
Signed-off-by: Christoph Heiss <c.heiss@proxmox.com>
proxmox-tui-installer/src/system.rs
proxmox-tui-installer/src/views/timezone.rs

index 5e168e39c63044524751be1fed4bd457855fa6d2..e781ca289d45bdcfe69643f7c1e68d48f7b5eb82 100644 (file)
@@ -1,4 +1,6 @@
+use crate::setup::KeyboardMapping;
 use proxmox_sys::linux::procfs;
+use std::{fs::OpenOptions, io::Write, process::Command};
 
 pub fn has_min_requirements() -> Result<(), String> {
     let meminfo = procfs::read_meminfo().map_err(|err| err.to_string())?;
@@ -23,3 +25,32 @@ pub fn has_min_requirements() -> Result<(), String> {
 
     Ok(())
 }
+
+pub fn set_keyboard_layout(kmap: &KeyboardMapping) -> Result<(), String> {
+    Command::new("setxkbmap")
+        .args([&kmap.xkb_layout, &kmap.xkb_variant])
+        .output()
+        .map_err(|err| err.to_string())?;
+
+    let mut f = OpenOptions::new()
+        .write(true)
+        .truncate(true)
+        .open("/etc/default/keyboard")
+        .map_err(|err| err.to_string())?;
+
+    write!(
+        f,
+        r#"XKBLAYOUT="{}"
+XKBVARIANT="{}"
+BACKSPACE="guess"
+"#,
+        kmap.xkb_layout, kmap.xkb_variant
+    )
+    .map_err(|err| err.to_string())?;
+
+    Command::new("setupcon")
+        .output()
+        .map_err(|err| err.to_string())?;
+
+    Ok(())
+}
index 39043490d4a46d636b258bda8dd59551e6d64e98..e778ea0d777e3c07dea96c38f3db213c380abd96 100644 (file)
@@ -2,10 +2,11 @@ use super::FormView;
 use crate::{
     options::TimezoneOptions,
     setup::{KeyboardMapping, LocaleInfo},
+    system, InstallerState,
 };
 use cursive::{
     view::{Nameable, ViewWrapper},
-    views::{NamedView, SelectView},
+    views::{Dialog, NamedView, SelectView},
     Cursive,
 };
 
@@ -72,7 +73,23 @@ impl TimezoneOptionsView {
                 SelectView::new()
                     .popup()
                     .with_all(kb_layouts)
-                    .selected(kb_layout_selected_pos),
+                    .selected(kb_layout_selected_pos)
+                    .on_submit(|siv, selected| {
+                        if siv
+                            .user_data::<InstallerState>()
+                            .map(|state| state.in_test_mode)
+                            // In doubt, don't override the layout
+                            .unwrap_or(true)
+                        {
+                            return;
+                        }
+
+                        if let Err(err) = system::set_keyboard_layout(selected) {
+                            siv.add_layer(Dialog::info(format!(
+                                "Failed to apply keyboard layout: {err}"
+                            )));
+                        }
+                    }),
             );
 
         Self { view }