tawasuyu

tawasuyu/mirada

Sovereign tiling Wayland compositor + window manager in Rust, on Llimphi.

Rust 50Markdown 30TOML 211282 Definiciones

⎇ main vendor/smithay/src/input/keyboard/modifiers_state.rs Rust · 144 líneas · 4.8 KB Historia Quién tocó qué crudo

chore: refresco desde el monorepo (pin tawasuyu.git v0.2.0) a91da3fea · Sergio ·

1use xkbcommon::xkb;
2
3/// Represents the current state of the keyboard modifiers
4///
5/// Each field of this struct represents a modifier and is `true` if this modifier is active.
6///
7/// For some modifiers, this means that the key is currently pressed, others are toggled
8/// (like caps lock).
9#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
10pub struct ModifiersState {
11    /// The "control" key
12    pub ctrl: bool,
13    /// The "alt" key
14    pub alt: bool,
15    /// The "shift" key
16    pub shift: bool,
17    /// The "Caps lock" key
18    pub caps_lock: bool,
19    /// The "logo" key
20    ///
21    /// Also known as the "windows" key on most keyboards
22    pub logo: bool,
23    /// The "Num lock" key
24    pub num_lock: bool,
25    /// The "ISO level 3 shift" key
26    ///
27    /// Also known as the "AltGr" key
28    pub iso_level3_shift: bool,
29
30    /// The "ISO level 5 shift" key
31    pub iso_level5_shift: bool,
32
33    /// Serialized modifier state, as send e.g. by the wl_keyboard protocol
34    pub serialized: SerializedMods,
35}
36
37impl ModifiersState {
38    /// Update the modifiers state from an xkb state
39    pub fn update_with(&mut self, state: &xkb::State) {
40        self.ctrl = state.mod_name_is_active(&xkb::MOD_NAME_CTRL, xkb::STATE_MODS_EFFECTIVE);
41        self.alt = state.mod_name_is_active(&xkb::MOD_NAME_ALT, xkb::STATE_MODS_EFFECTIVE);
42        self.shift = state.mod_name_is_active(&xkb::MOD_NAME_SHIFT, xkb::STATE_MODS_EFFECTIVE);
43        self.caps_lock = state.mod_name_is_active(&xkb::MOD_NAME_CAPS, xkb::STATE_MODS_EFFECTIVE);
44        self.logo = state.mod_name_is_active(&xkb::MOD_NAME_LOGO, xkb::STATE_MODS_EFFECTIVE);
45        self.num_lock = state.mod_name_is_active(&xkb::MOD_NAME_NUM, xkb::STATE_MODS_EFFECTIVE);
46        self.iso_level3_shift =
47            state.mod_name_is_active(&xkb::MOD_NAME_ISO_LEVEL3_SHIFT, xkb::STATE_MODS_EFFECTIVE);
48        self.iso_level5_shift = state.mod_name_is_active(&xkb::MOD_NAME_MOD3, xkb::STATE_MODS_EFFECTIVE);
49        self.serialized = serialize_modifiers(state);
50    }
51
52    /// Serialize modifier state back to be sent to xkb.
53    pub fn serialize_back(&self, state: &xkb::State) -> SerializedMods {
54        let keymap = state.get_keymap();
55
56        let mut locked: u32 = 0;
57        let mut depressed: u32 = 0;
58
59        if self.caps_lock {
60            let index = keymap.mod_get_index(&xkb::MOD_NAME_CAPS);
61            if index != xkb::MOD_INVALID {
62                locked |= 1 << index;
63            }
64        }
65        if self.num_lock {
66            let index = keymap.mod_get_index(&xkb::MOD_NAME_NUM);
67            if index != xkb::MOD_INVALID {
68                locked |= 1 << index;
69            }
70        }
71        if self.ctrl {
72            let index = keymap.mod_get_index(&xkb::MOD_NAME_CTRL);
73            if index != xkb::MOD_INVALID {
74                depressed |= 1 << index;
75            }
76        }
77        if self.alt {
78            let index = keymap.mod_get_index(&xkb::MOD_NAME_ALT);
79            if index != xkb::MOD_INVALID {
80                depressed |= 1 << index;
81            }
82        }
83        if self.shift {
84            let index = keymap.mod_get_index(&xkb::MOD_NAME_SHIFT);
85            if index != xkb::MOD_INVALID {
86                depressed |= 1 << index;
87            }
88        }
89        if self.logo {
90            let index = keymap.mod_get_index(&xkb::MOD_NAME_LOGO);
91            if index != xkb::MOD_INVALID {
92                depressed |= 1 << index;
93            }
94        }
95        if self.iso_level3_shift {
96            let index = keymap.mod_get_index(&xkb::MOD_NAME_ISO_LEVEL3_SHIFT);
97            if index != xkb::MOD_INVALID {
98                depressed |= 1 << index;
99            }
100        }
101        if self.iso_level5_shift {
102            let index = keymap.mod_get_index(&xkb::MOD_NAME_MOD3);
103            if index != xkb::MOD_INVALID {
104                depressed |= 1 << index;
105            }
106        }
107
108        let layout_effective = state.serialize_layout(xkb::STATE_LAYOUT_EFFECTIVE);
109
110        SerializedMods {
111            depressed,
112            latched: 0,
113            locked,
114            layout_effective,
115        }
116    }
117}
118
119/// Serialized modifier state
120#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
121pub struct SerializedMods {
122    /// Depressed modifiers
123    pub depressed: u32,
124    /// Latched modifiers
125    pub latched: u32,
126    /// Locked modifiers
127    pub locked: u32,
128    /// Effective keyboard layout
129    pub layout_effective: u32,
130}
131
132fn serialize_modifiers(state: &xkb::State) -> SerializedMods {
133    let depressed = state.serialize_mods(xkb::STATE_MODS_DEPRESSED);
134    let latched = state.serialize_mods(xkb::STATE_MODS_LATCHED);
135    let locked = state.serialize_mods(xkb::STATE_MODS_LOCKED);
136    let layout_effective = state.serialize_layout(xkb::STATE_LAYOUT_EFFECTIVE);
137
138    SerializedMods {
139        depressed,
140        latched,
141        locked,
142        layout_effective,
143    }
144}