Skip to main content

gdk/auto/
keymap.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{Display, ModifierIntent, ModifierType, ffi};
6use glib::{
7    object::ObjectType as _,
8    prelude::*,
9    signal::{SignalHandlerId, connect_raw},
10    translate::*,
11};
12use std::boxed::Box as Box_;
13
14glib::wrapper! {
15    /// A [`Keymap`][crate::Keymap] defines the translation from keyboard state
16    /// (including a hardware key, a modifier mask, and active keyboard group)
17    /// to a keyval. This translation has two phases. The first phase is
18    /// to determine the effective keyboard group and level for the keyboard
19    /// state; the second phase is to look up the keycode/group/level triplet
20    /// in the keymap and see what keyval it corresponds to.
21    ///
22    /// ## Signals
23    ///
24    ///
25    /// #### `direction-changed`
26    ///  The ::direction-changed signal gets emitted when the direction of
27    /// the keymap changes.
28    ///
29    ///
30    ///
31    ///
32    /// #### `keys-changed`
33    ///  The ::keys-changed signal is emitted when the mapping represented by
34    /// `keymap` changes.
35    ///
36    ///
37    ///
38    ///
39    /// #### `state-changed`
40    ///  The ::state-changed signal is emitted when the state of the
41    /// keyboard changes, e.g when Caps Lock is turned on or off.
42    /// See [`Keymap::is_caps_locked()`][crate::Keymap::is_caps_locked()].
43    ///
44    ///
45    #[doc(alias = "GdkKeymap")]
46    pub struct Keymap(Object<ffi::GdkKeymap>);
47
48    match fn {
49        type_ => || ffi::gdk_keymap_get_type(),
50    }
51}
52
53impl Keymap {
54    /// Returns whether the Caps Lock modifer is locked.
55    ///
56    /// # Returns
57    ///
58    /// [`true`] if Caps Lock is on
59    #[doc(alias = "gdk_keymap_get_caps_lock_state")]
60    #[doc(alias = "get_caps_lock_state")]
61    pub fn is_caps_locked(&self) -> bool {
62        unsafe { from_glib(ffi::gdk_keymap_get_caps_lock_state(self.to_glib_none().0)) }
63    }
64
65    /// Returns the direction of effective layout of the keymap.
66    ///
67    /// # Returns
68    ///
69    /// [`pango::Direction::Ltr`][crate::pango::Direction::Ltr] or [`pango::Direction::Rtl`][crate::pango::Direction::Rtl]
70    ///  if it can determine the direction. [`pango::Direction::Neutral`][crate::pango::Direction::Neutral]
71    ///  otherwise.
72    #[doc(alias = "gdk_keymap_get_direction")]
73    #[doc(alias = "get_direction")]
74    pub fn direction(&self) -> pango::Direction {
75        unsafe { from_glib(ffi::gdk_keymap_get_direction(self.to_glib_none().0)) }
76    }
77
78    /// Returns the modifier mask the `self`’s windowing system backend
79    /// uses for a particular purpose.
80    ///
81    /// Note that this function always returns real hardware modifiers, not
82    /// virtual ones (e.g. it will return [`ModifierType::MOD1_MASK`][crate::ModifierType::MOD1_MASK] rather than
83    /// [`ModifierType::META_MASK`][crate::ModifierType::META_MASK] if the backend maps MOD1 to META), so there are use
84    /// cases where the return value of this function has to be transformed
85    /// by [`add_virtual_modifiers()`][Self::add_virtual_modifiers()] in order to contain the
86    /// expected result.
87    /// ## `intent`
88    /// the use case for the modifier mask
89    ///
90    /// # Returns
91    ///
92    /// the modifier mask used for `intent`.
93    #[doc(alias = "gdk_keymap_get_modifier_mask")]
94    #[doc(alias = "get_modifier_mask")]
95    pub fn modifier_mask(&self, intent: ModifierIntent) -> ModifierType {
96        unsafe {
97            from_glib(ffi::gdk_keymap_get_modifier_mask(
98                self.to_glib_none().0,
99                intent.into_glib(),
100            ))
101        }
102    }
103
104    /// Returns the current modifier state.
105    ///
106    /// # Returns
107    ///
108    /// the current modifier state.
109    #[doc(alias = "gdk_keymap_get_modifier_state")]
110    #[doc(alias = "get_modifier_state")]
111    pub fn modifier_state(&self) -> u32 {
112        unsafe { ffi::gdk_keymap_get_modifier_state(self.to_glib_none().0) }
113    }
114
115    /// Returns whether the Num Lock modifer is locked.
116    ///
117    /// # Returns
118    ///
119    /// [`true`] if Num Lock is on
120    #[doc(alias = "gdk_keymap_get_num_lock_state")]
121    #[doc(alias = "get_num_lock_state")]
122    pub fn is_num_locked(&self) -> bool {
123        unsafe { from_glib(ffi::gdk_keymap_get_num_lock_state(self.to_glib_none().0)) }
124    }
125
126    /// Returns whether the Scroll Lock modifer is locked.
127    ///
128    /// # Returns
129    ///
130    /// [`true`] if Scroll Lock is on
131    #[doc(alias = "gdk_keymap_get_scroll_lock_state")]
132    #[doc(alias = "get_scroll_lock_state")]
133    pub fn is_scroll_locked(&self) -> bool {
134        unsafe { from_glib(ffi::gdk_keymap_get_scroll_lock_state(self.to_glib_none().0)) }
135    }
136
137    /// Determines if keyboard layouts for both right-to-left and left-to-right
138    /// languages are in use.
139    ///
140    /// # Returns
141    ///
142    /// [`true`] if there are layouts in both directions, [`false`] otherwise
143    #[doc(alias = "gdk_keymap_have_bidi_layouts")]
144    pub fn have_bidi_layouts(&self) -> bool {
145        unsafe { from_glib(ffi::gdk_keymap_have_bidi_layouts(self.to_glib_none().0)) }
146    }
147
148    /// plus`,
149    /// ## `hardware_keycode`
150    /// a keycode
151    /// ## `state`
152    /// a modifier state
153    /// ## `group`
154    /// active keyboard group
155    ///
156    /// # Returns
157    ///
158    /// [`true`] if there was a keyval bound to the keycode/state/group
159    ///
160    /// ## `keyval`
161    /// return location for keyval, or [`None`]
162    ///
163    /// ## `effective_group`
164    /// return location for effective
165    ///  group, or [`None`]
166    ///
167    /// ## `level`
168    /// return location for level, or [`None`]
169    ///
170    /// ## `consumed_modifiers`
171    /// return location for modifiers
172    ///  that were used to determine the group or level, or [`None`]
173    #[doc(alias = "gdk_keymap_translate_keyboard_state")]
174    pub fn translate_keyboard_state(
175        &self,
176        hardware_keycode: u32,
177        state: ModifierType,
178        group: i32,
179    ) -> Option<(u32, i32, i32, ModifierType)> {
180        unsafe {
181            let mut keyval = std::mem::MaybeUninit::uninit();
182            let mut effective_group = std::mem::MaybeUninit::uninit();
183            let mut level = std::mem::MaybeUninit::uninit();
184            let mut consumed_modifiers = std::mem::MaybeUninit::uninit();
185            let ret = from_glib(ffi::gdk_keymap_translate_keyboard_state(
186                self.to_glib_none().0,
187                hardware_keycode,
188                state.into_glib(),
189                group,
190                keyval.as_mut_ptr(),
191                effective_group.as_mut_ptr(),
192                level.as_mut_ptr(),
193                consumed_modifiers.as_mut_ptr(),
194            ));
195            if ret {
196                Some((
197                    keyval.assume_init(),
198                    effective_group.assume_init(),
199                    level.assume_init(),
200                    from_glib(consumed_modifiers.assume_init()),
201                ))
202            } else {
203                None
204            }
205        }
206    }
207
208    /// Returns the [`Keymap`][crate::Keymap] attached to `display`.
209    /// ## `display`
210    /// the [`Display`][crate::Display].
211    ///
212    /// # Returns
213    ///
214    /// the [`Keymap`][crate::Keymap] attached to `display`.
215    #[doc(alias = "gdk_keymap_get_for_display")]
216    #[doc(alias = "get_for_display")]
217    pub fn for_display(display: &Display) -> Option<Keymap> {
218        skip_assert_initialized!();
219        unsafe { from_glib_none(ffi::gdk_keymap_get_for_display(display.to_glib_none().0)) }
220    }
221
222    /// The ::direction-changed signal gets emitted when the direction of
223    /// the keymap changes.
224    #[doc(alias = "direction-changed")]
225    pub fn connect_direction_changed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
226        unsafe extern "C" fn direction_changed_trampoline<F: Fn(&Keymap) + 'static>(
227            this: *mut ffi::GdkKeymap,
228            f: glib::ffi::gpointer,
229        ) {
230            unsafe {
231                let f: &F = &*(f as *const F);
232                f(&from_glib_borrow(this))
233            }
234        }
235        unsafe {
236            let f: Box_<F> = Box_::new(f);
237            connect_raw(
238                self.as_ptr() as *mut _,
239                c"direction-changed".as_ptr(),
240                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
241                    direction_changed_trampoline::<F> as *const (),
242                )),
243                Box_::into_raw(f),
244            )
245        }
246    }
247
248    /// The ::keys-changed signal is emitted when the mapping represented by
249    /// `keymap` changes.
250    #[doc(alias = "keys-changed")]
251    pub fn connect_keys_changed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
252        unsafe extern "C" fn keys_changed_trampoline<F: Fn(&Keymap) + 'static>(
253            this: *mut ffi::GdkKeymap,
254            f: glib::ffi::gpointer,
255        ) {
256            unsafe {
257                let f: &F = &*(f as *const F);
258                f(&from_glib_borrow(this))
259            }
260        }
261        unsafe {
262            let f: Box_<F> = Box_::new(f);
263            connect_raw(
264                self.as_ptr() as *mut _,
265                c"keys-changed".as_ptr(),
266                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
267                    keys_changed_trampoline::<F> as *const (),
268                )),
269                Box_::into_raw(f),
270            )
271        }
272    }
273
274    /// The ::state-changed signal is emitted when the state of the
275    /// keyboard changes, e.g when Caps Lock is turned on or off.
276    /// See [`is_caps_locked()`][Self::is_caps_locked()].
277    #[doc(alias = "state-changed")]
278    pub fn connect_state_changed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
279        unsafe extern "C" fn state_changed_trampoline<F: Fn(&Keymap) + 'static>(
280            this: *mut ffi::GdkKeymap,
281            f: glib::ffi::gpointer,
282        ) {
283            unsafe {
284                let f: &F = &*(f as *const F);
285                f(&from_glib_borrow(this))
286            }
287        }
288        unsafe {
289            let f: Box_<F> = Box_::new(f);
290            connect_raw(
291                self.as_ptr() as *mut _,
292                c"state-changed".as_ptr(),
293                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
294                    state_changed_trampoline::<F> as *const (),
295                )),
296                Box_::into_raw(f),
297            )
298        }
299    }
300}