@@ -4,8 +4,12 @@ use xkbcommon::xkb;
4
4
///
5
5
/// Each field of this struct represents a modifier and is `true` if this modifier is active.
6
6
///
7
- /// For some modifiers, this means that the key is currently pressed, others are toggled
7
+ /// For some modifiers, this means that the key is currently pressed, others are toggled/locked
8
8
/// (like caps lock).
9
+ ///
10
+ /// **Note:** The XKB state should usually be the single source of truth, and the
11
+ /// serialization is lossy and will not survive round trips. This is documented in
12
+ /// [`xkb::State::update_mask`].
9
13
#[ derive( Copy , Clone , Debug , Default , PartialEq , Eq , Hash ) ]
10
14
pub struct ModifiersState {
11
15
/// The "control" key
@@ -30,12 +34,19 @@ pub struct ModifiersState {
30
34
/// The "ISO level 5 shift" key
31
35
pub iso_level5_shift : bool ,
32
36
33
- /// Serialized modifier state, as send e.g. by the wl_keyboard protocol
37
+ /// Cached serialized modifier state, e.g. for sending in `wl_keyboard.modifiers`.
38
+ ///
39
+ /// Note that this may have outdated information compared to the other fields, and that
40
+ /// this is not updated in [`ModifiersState::serialize_back`].
34
41
pub serialized : SerializedMods ,
35
42
}
36
43
37
44
impl ModifiersState {
38
- /// Update the modifiers state from an xkb state
45
+ /// Updates the high-level modifiers state from an XKB state.
46
+ ///
47
+ /// **Note:** The XKB state should usually be the single source of truth, and the
48
+ /// serialization is lossy and will not survive round trips. This is documented in
49
+ /// [`xkb::State::update_mask`].
39
50
pub fn update_with ( & mut self , state : & xkb:: State ) {
40
51
self . ctrl = state. mod_name_is_active ( & xkb:: MOD_NAME_CTRL , xkb:: STATE_MODS_EFFECTIVE ) ;
41
52
self . alt = state. mod_name_is_active ( & xkb:: MOD_NAME_ALT , xkb:: STATE_MODS_EFFECTIVE ) ;
@@ -49,7 +60,33 @@ impl ModifiersState {
49
60
self . serialized = serialize_modifiers ( state) ;
50
61
}
51
62
52
- /// Serialize modifier state back to be sent to xkb.
63
+ /// Serializes the high-level modifiers state to be sent to XKB e.g. in
64
+ /// `wl_keyboard.modifiers`.
65
+ ///
66
+ /// **Note:** The XKB state should usually be the single source of truth, and the
67
+ /// serialization is lossy and will not survive round trips. This is documented in
68
+ /// [`xkb::State::update_mask`].
69
+ ///
70
+ /// Note that cached serialized state is stored in [`ModifiersState::serialized`], but it may
71
+ /// have outdated information. This function ignores that field. You should update the cached
72
+ /// serialized state after using this function, like so:
73
+ ///
74
+ /// ```no_run
75
+ /// use smithay::input::keyboard::ModifiersState;
76
+ ///
77
+ /// let mut mods_state: ModifiersState;
78
+ /// # mods_state = todo!();
79
+ /// # let xkb_state = todo!();
80
+ ///
81
+ /// // Update the information
82
+ /// mods_state.ctrl = true;
83
+ ///
84
+ /// // Serialize e.g. for sending in `wl_keyboard.modifiers`
85
+ /// let serialized = mods_state.serialize_back(&xkb_state);
86
+ ///
87
+ /// // Update the cached serialized state
88
+ /// mods_state.serialized = serialized;
89
+ /// ```
53
90
pub fn serialize_back ( & self , state : & xkb:: State ) -> SerializedMods {
54
91
let keymap = state. get_keymap ( ) ;
55
92
0 commit comments