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
// Take a look at the license at the top of the repository in the LICENSE file.

use glib::translate::*;
use glib::Slice;
use std::ptr;

/// Parses a string representing an accelerator, similarly to
/// [`accelerator_parse()`][crate::accelerator_parse()] but handles keycodes as well. This is only
/// useful for system-level components, applications should use
/// [`accelerator_parse()`][crate::accelerator_parse()] instead.
///
/// If `accelerator_codes` is given and the result stored in it is non-[`None`],
/// the result must be freed with `g_free()`.
///
/// If a keycode is present in the accelerator and no `accelerator_codes`
/// is given, the parse will fail.
///
/// If the parse fails, `accelerator_key`, `accelerator_mods` and
/// `accelerator_codes` will be set to 0 (zero).
/// ## `accelerator`
/// string representing an accelerator
///
/// # Returns
///
///
/// ## `accelerator_key`
/// return location for accelerator
///  keyval, or [`None`]
///
/// ## `accelerator_codes`
///
///  return location for accelerator keycodes, or [`None`]
///
/// ## `accelerator_mods`
/// return location for accelerator
///  modifier mask, [`None`]
#[doc(alias = "gtk_accelerator_parse_with_keycode")]
pub fn accelerator_parse_with_keycode(
    accelerator: &str,
) -> Option<(u32, Slice<u32>, gdk::ModifierType)> {
    assert_initialized_main_thread!();
    unsafe {
        let mut accelerator_key = std::mem::MaybeUninit::uninit();
        let mut accelerator_codes_ptr = ptr::null_mut();
        let mut accelerator_mods = std::mem::MaybeUninit::uninit();
        ffi::gtk_accelerator_parse_with_keycode(
            accelerator.to_glib_none().0,
            accelerator_key.as_mut_ptr(),
            &mut accelerator_codes_ptr,
            accelerator_mods.as_mut_ptr(),
        );
        if !accelerator_codes_ptr.is_null() {
            let mut len = 0;
            if !accelerator_codes_ptr.is_null() {
                while ptr::read(accelerator_codes_ptr.add(len)) != 0 {
                    len += 1;
                }
            }
            let accelerator_codes = Slice::from_glib_full_num(accelerator_codes_ptr, len);
            Some((
                accelerator_key.assume_init(),
                accelerator_codes,
                from_glib(accelerator_mods.assume_init()),
            ))
        } else {
            None
        }
    }
}