1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use crate::Display;
use crate::ModifierIntent;
use crate::ModifierType;
use glib::object::ObjectType as ObjectType_;
use glib::signal::connect_raw;
use glib::signal::SignalHandlerId;
use glib::translate::*;
use std::boxed::Box as Box_;
use std::fmt;
use std::mem;
use std::mem::transmute;

glib::wrapper! {
    /// A [`Keymap`][crate::Keymap] defines the translation from keyboard state
    /// (including a hardware key, a modifier mask, and active keyboard group)
    /// to a keyval. This translation has two phases. The first phase is
    /// to determine the effective keyboard group and level for the keyboard
    /// state; the second phase is to look up the keycode/group/level triplet
    /// in the keymap and see what keyval it corresponds to.
    #[doc(alias = "GdkKeymap")]
    pub struct Keymap(Object<ffi::GdkKeymap>);

    match fn {
        type_ => || ffi::gdk_keymap_get_type(),
    }
}

impl Keymap {
    /// Returns whether the Caps Lock modifer is locked.
    ///
    /// # Returns
    ///
    /// [`true`] if Caps Lock is on
    #[doc(alias = "gdk_keymap_get_caps_lock_state")]
    #[doc(alias = "get_caps_lock_state")]
    pub fn is_caps_locked(&self) -> bool {
        unsafe { from_glib(ffi::gdk_keymap_get_caps_lock_state(self.to_glib_none().0)) }
    }

    /// Returns the direction of effective layout of the keymap.
    ///
    /// # Returns
    ///
    /// [`pango::Direction::Ltr`][crate::pango::Direction::Ltr] or [`pango::Direction::Rtl`][crate::pango::Direction::Rtl]
    ///  if it can determine the direction. [`pango::Direction::Neutral`][crate::pango::Direction::Neutral]
    ///  otherwise.
    #[doc(alias = "gdk_keymap_get_direction")]
    #[doc(alias = "get_direction")]
    pub fn direction(&self) -> pango::Direction {
        unsafe { from_glib(ffi::gdk_keymap_get_direction(self.to_glib_none().0)) }
    }

    /// Returns the modifier mask the `self`’s windowing system backend
    /// uses for a particular purpose.
    ///
    /// Note that this function always returns real hardware modifiers, not
    /// virtual ones (e.g. it will return [`ModifierType::MOD1_MASK`][crate::ModifierType::MOD1_MASK] rather than
    /// [`ModifierType::META_MASK`][crate::ModifierType::META_MASK] if the backend maps MOD1 to META), so there are use
    /// cases where the return value of this function has to be transformed
    /// by [`add_virtual_modifiers()`][Self::add_virtual_modifiers()] in order to contain the
    /// expected result.
    /// ## `intent`
    /// the use case for the modifier mask
    ///
    /// # Returns
    ///
    /// the modifier mask used for `intent`.
    #[doc(alias = "gdk_keymap_get_modifier_mask")]
    #[doc(alias = "get_modifier_mask")]
    pub fn modifier_mask(&self, intent: ModifierIntent) -> ModifierType {
        unsafe {
            from_glib(ffi::gdk_keymap_get_modifier_mask(
                self.to_glib_none().0,
                intent.into_glib(),
            ))
        }
    }

    /// Returns the current modifier state.
    ///
    /// # Returns
    ///
    /// the current modifier state.
    #[doc(alias = "gdk_keymap_get_modifier_state")]
    #[doc(alias = "get_modifier_state")]
    pub fn modifier_state(&self) -> u32 {
        unsafe { ffi::gdk_keymap_get_modifier_state(self.to_glib_none().0) }
    }

    /// Returns whether the Num Lock modifer is locked.
    ///
    /// # Returns
    ///
    /// [`true`] if Num Lock is on
    #[doc(alias = "gdk_keymap_get_num_lock_state")]
    #[doc(alias = "get_num_lock_state")]
    pub fn is_num_locked(&self) -> bool {
        unsafe { from_glib(ffi::gdk_keymap_get_num_lock_state(self.to_glib_none().0)) }
    }

    /// Returns whether the Scroll Lock modifer is locked.
    ///
    /// # Returns
    ///
    /// [`true`] if Scroll Lock is on
    #[doc(alias = "gdk_keymap_get_scroll_lock_state")]
    #[doc(alias = "get_scroll_lock_state")]
    pub fn is_scroll_locked(&self) -> bool {
        unsafe { from_glib(ffi::gdk_keymap_get_scroll_lock_state(self.to_glib_none().0)) }
    }

    /// Determines if keyboard layouts for both right-to-left and left-to-right
    /// languages are in use.
    ///
    /// # Returns
    ///
    /// [`true`] if there are layouts in both directions, [`false`] otherwise
    #[doc(alias = "gdk_keymap_have_bidi_layouts")]
    pub fn have_bidi_layouts(&self) -> bool {
        unsafe { from_glib(ffi::gdk_keymap_have_bidi_layouts(self.to_glib_none().0)) }
    }

    /// Translates the contents of a [`EventKey`][crate::EventKey] into a keyval, effective
    /// group, and level. Modifiers that affected the translation and
    /// are thus unavailable for application use are returned in
    /// `consumed_modifiers`.
    /// See [Groups][key-group-explanation] for an explanation of
    /// groups and levels. The `effective_group` is the group that was
    /// actually used for the translation; some keys such as Enter are not
    /// affected by the active keyboard group. The `level` is derived from
    /// `state`. For convenience, [`EventKey`][crate::EventKey] already contains the translated
    /// keyval, so this function isn’t as useful as you might think.
    ///
    /// `consumed_modifiers` gives modifiers that should be masked outfrom `state`
    /// when comparing this key press to a hot key. For instance, on a US keyboard,
    /// the `plus` symbol is shifted, so when comparing a key press to a
    /// ``<Control>`plus` accelerator ``<Shift>`` should be masked out.
    ///
    ///
    ///
    /// **⚠️ The following code is in C ⚠️**
    ///
    /// ```C
    /// // We want to ignore irrelevant modifiers like ScrollLock
    /// #define ALL_ACCELS_MASK (GDK_CONTROL_MASK | GDK_SHIFT_MASK | GDK_MOD1_MASK)
    /// gdk_keymap_translate_keyboard_state (keymap, event->hardware_keycode,
    ///                                      event->state, event->group,
    ///                                      &keyval, NULL, NULL, &consumed);
    /// if (keyval == GDK_PLUS &&
    ///     (event->state & ~consumed & ALL_ACCELS_MASK) == GDK_CONTROL_MASK)
    ///   // Control was pressed
    /// ```
    ///
    /// An older interpretation `consumed_modifiers` was that it contained
    /// all modifiers that might affect the translation of the key;
    /// this allowed accelerators to be stored with irrelevant consumed
    /// modifiers, by doing:
    ///
    ///
    /// **⚠️ The following code is in C ⚠️**
    ///
    /// ```C
    /// // XXX Don’t do this XXX
    /// if (keyval == accel_keyval &&
    ///     (event->state & ~consumed & ALL_ACCELS_MASK) == (accel_mods & ~consumed))
    ///   // Accelerator was pressed
    /// ```
    ///
    /// However, this did not work if multi-modifier combinations were
    /// used in the keymap, since, for instance, ``<Control>`` would be
    /// masked out even if only ``<Control>``<Alt>`` was used in the keymap.
    /// To support this usage as well as well as possible, all single
    /// modifier combinations that could affect the key for any combination
    /// of modifiers will be returned in `consumed_modifiers`; multi-modifier
    /// combinations are returned only when actually found in `state`. When
    /// you store accelerators, you should always store them with consumed
    /// modifiers removed. Store ``<Control>`plus`, not ``<Control>``<Shift>`plus`,
    /// ## `hardware_keycode`
    /// a keycode
    /// ## `state`
    /// a modifier state
    /// ## `group`
    /// active keyboard group
    ///
    /// # Returns
    ///
    /// [`true`] if there was a keyval bound to the keycode/state/group
    ///
    /// ## `keyval`
    /// return location for keyval, or [`None`]
    ///
    /// ## `effective_group`
    /// return location for effective
    ///  group, or [`None`]
    ///
    /// ## `level`
    /// return location for level, or [`None`]
    ///
    /// ## `consumed_modifiers`
    /// return location for modifiers
    ///  that were used to determine the group or level, or [`None`]
    #[doc(alias = "gdk_keymap_translate_keyboard_state")]
    pub fn translate_keyboard_state(
        &self,
        hardware_keycode: u32,
        state: ModifierType,
        group: i32,
    ) -> Option<(u32, i32, i32, ModifierType)> {
        unsafe {
            let mut keyval = mem::MaybeUninit::uninit();
            let mut effective_group = mem::MaybeUninit::uninit();
            let mut level = mem::MaybeUninit::uninit();
            let mut consumed_modifiers = mem::MaybeUninit::uninit();
            let ret = from_glib(ffi::gdk_keymap_translate_keyboard_state(
                self.to_glib_none().0,
                hardware_keycode,
                state.into_glib(),
                group,
                keyval.as_mut_ptr(),
                effective_group.as_mut_ptr(),
                level.as_mut_ptr(),
                consumed_modifiers.as_mut_ptr(),
            ));
            let keyval = keyval.assume_init();
            let effective_group = effective_group.assume_init();
            let level = level.assume_init();
            let consumed_modifiers = consumed_modifiers.assume_init();
            if ret {
                Some((
                    keyval,
                    effective_group,
                    level,
                    from_glib(consumed_modifiers),
                ))
            } else {
                None
            }
        }
    }

    /// Returns the [`Keymap`][crate::Keymap] attached to the default display.
    ///
    /// # Deprecated since 3.22
    ///
    /// Use [`for_display()`][Self::for_display()] instead
    ///
    /// # Returns
    ///
    /// the [`Keymap`][crate::Keymap] attached to the default display.
    #[cfg_attr(feature = "v3_22", deprecated = "Since 3.22")]
    #[doc(alias = "gdk_keymap_get_default")]
    #[doc(alias = "get_default")]
    pub fn default() -> Option<Keymap> {
        assert_initialized_main_thread!();
        unsafe { from_glib_none(ffi::gdk_keymap_get_default()) }
    }

    /// Returns the [`Keymap`][crate::Keymap] attached to `display`.
    /// ## `display`
    /// the [`Display`][crate::Display].
    ///
    /// # Returns
    ///
    /// the [`Keymap`][crate::Keymap] attached to `display`.
    #[doc(alias = "gdk_keymap_get_for_display")]
    #[doc(alias = "get_for_display")]
    pub fn for_display(display: &Display) -> Option<Keymap> {
        skip_assert_initialized!();
        unsafe { from_glib_none(ffi::gdk_keymap_get_for_display(display.to_glib_none().0)) }
    }

    /// The ::direction-changed signal gets emitted when the direction of
    /// the keymap changes.
    #[doc(alias = "direction-changed")]
    pub fn connect_direction_changed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn direction_changed_trampoline<F: Fn(&Keymap) + 'static>(
            this: *mut ffi::GdkKeymap,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(&from_glib_borrow(this))
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"direction-changed\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    direction_changed_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    /// The ::keys-changed signal is emitted when the mapping represented by
    /// `keymap` changes.
    #[doc(alias = "keys-changed")]
    pub fn connect_keys_changed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn keys_changed_trampoline<F: Fn(&Keymap) + 'static>(
            this: *mut ffi::GdkKeymap,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(&from_glib_borrow(this))
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"keys-changed\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    keys_changed_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    /// The ::state-changed signal is emitted when the state of the
    /// keyboard changes, e.g when Caps Lock is turned on or off.
    /// See [`is_caps_locked()`][Self::is_caps_locked()].
    #[doc(alias = "state-changed")]
    pub fn connect_state_changed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn state_changed_trampoline<F: Fn(&Keymap) + 'static>(
            this: *mut ffi::GdkKeymap,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(&from_glib_borrow(this))
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"state-changed\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    state_changed_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

impl fmt::Display for Keymap {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("Keymap")
    }
}