|
| 1 | +// Copy from https://github.com/pop-os/distinst/blob/8322c936a91ba5812ac9cbef79282a04bad738ea/crates/locales/src/keyboard_layout.rs#L82 |
| 2 | + |
| 3 | +use eyre::Result; |
| 4 | +use serde::{Deserialize, Serialize}; |
| 5 | +use serde_xml_rs as xml; |
| 6 | +use std::{fs::File, io::BufReader}; |
| 7 | + |
| 8 | +/// A list of keyboard layouts parsed from `/usr/share/X11/xkb/rules/base.xml`. |
| 9 | +#[derive(Debug, Deserialize, Serialize)] |
| 10 | +pub struct KeyboardLayouts { |
| 11 | + #[serde(rename = "layoutList")] |
| 12 | + pub layout_list: LayoutList, |
| 13 | +} |
| 14 | + |
| 15 | +impl KeyboardLayouts { |
| 16 | + /// Fetch the layouts from the layout list. |
| 17 | + pub fn get_layouts(&self) -> &[KeyboardLayout] { |
| 18 | + &self.layout_list.layout |
| 19 | + } |
| 20 | + |
| 21 | + /// Fetch the layouts from the layout list. |
| 22 | + pub fn get_layouts_mut(&mut self) -> &mut [KeyboardLayout] { |
| 23 | + &mut self.layout_list.layout |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +/// A list of keyboard layouts. |
| 28 | +#[derive(Debug, Deserialize, Serialize)] |
| 29 | +pub struct LayoutList { |
| 30 | + pub layout: Vec<KeyboardLayout>, |
| 31 | +} |
| 32 | + |
| 33 | +/// A keyboard layout, which contains an optional list of variants, a name, and a description. |
| 34 | +#[derive(Debug, Deserialize, Serialize)] |
| 35 | +pub struct KeyboardLayout { |
| 36 | + #[serde(rename = "configItem")] |
| 37 | + pub config_item: ConfigItem, |
| 38 | + #[serde(rename = "variantList")] |
| 39 | + pub variant_list: Option<VariantList>, |
| 40 | +} |
| 41 | + |
| 42 | +impl KeyboardLayout { |
| 43 | + /// Fetches the name of the keyboard layout. |
| 44 | + pub fn get_name(&self) -> &str { |
| 45 | + &self.config_item.name |
| 46 | + } |
| 47 | + |
| 48 | + /// Fetches a description of the layout. |
| 49 | + pub fn get_description(&self) -> &str { |
| 50 | + &self.config_item.description |
| 51 | + } |
| 52 | + |
| 53 | + /// Fetches a list of possible layout variants. |
| 54 | + pub fn get_variants(&self) -> Option<&Vec<KeyboardVariant>> { |
| 55 | + self.variant_list.as_ref().and_then(|x| x.variant.as_ref()) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/// Contains the name and description of a keyboard layout. |
| 60 | +#[derive(Debug, Deserialize, Serialize)] |
| 61 | +pub struct ConfigItem { |
| 62 | + pub name: String, |
| 63 | + #[serde(rename = "shortDescription")] |
| 64 | + pub short_description: Option<String>, |
| 65 | + pub description: String, |
| 66 | +} |
| 67 | + |
| 68 | +/// A list of possible variants of a keyboard layout. |
| 69 | +#[derive(Debug, Deserialize, Serialize)] |
| 70 | +pub struct VariantList { |
| 71 | + pub variant: Option<Vec<KeyboardVariant>>, |
| 72 | +} |
| 73 | + |
| 74 | +/// A variant of a keyboard layout. |
| 75 | +#[derive(Debug, Deserialize, Serialize)] |
| 76 | +pub struct KeyboardVariant { |
| 77 | + #[serde(rename = "configItem")] |
| 78 | + pub config_item: ConfigItem, |
| 79 | +} |
| 80 | + |
| 81 | +impl KeyboardVariant { |
| 82 | + /// The name of this variant of a keybaord layout. |
| 83 | + pub fn get_name(&self) -> &str { |
| 84 | + &self.config_item.name |
| 85 | + } |
| 86 | + |
| 87 | + /// A description of this variant of a keyboard layout. |
| 88 | + pub fn get_description(&self) -> &str { |
| 89 | + &self.config_item.description |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +const X11_BASE_RULES: &str = "/usr/share/X11/xkb/rules/base.xml"; |
| 94 | + |
| 95 | +/// Fetches a list of keyboard layouts from `/usr/share/X11/xkb/rules/base.xml`. |
| 96 | +pub fn get_keyboard_layouts() -> Result<KeyboardLayouts> { |
| 97 | + Ok(xml::from_reader(BufReader::new(File::open( |
| 98 | + X11_BASE_RULES, |
| 99 | + )?))?) |
| 100 | +} |
0 commit comments