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

use std::ffi::CStr;

use glib::translate::*;

use crate::PadActionType;

glib::wrapper! {
    /// Struct defining a pad action entry.
    #[doc(alias = "GtkPadActionEntry")]
    pub struct PadActionEntry(BoxedInline<ffi::GtkPadActionEntry>);

    match fn {
        init => |ptr| init_action_entry(ptr),
        copy_into => |dest, src| copy_into_action_entry(dest, src),
        clear => |ptr| clear_action_entry(ptr),
    }
}

impl PadActionEntry {
    #[inline]
    pub fn new(
        type_: PadActionType,
        index: i32,
        mode: i32,
        label: &str,
        action_name: &str,
    ) -> Self {
        assert_initialized_main_thread!();
        unsafe {
            Self::unsafe_from(ffi::GtkPadActionEntry {
                type_: type_.into_glib(),
                index,
                mode,
                label: label.to_glib_full(),
                action_name: action_name.to_glib_full(),
            })
        }
    }

    #[inline]
    pub fn type_(&self) -> PadActionType {
        unsafe { from_glib(self.inner.type_) }
    }

    #[inline]
    pub fn index(&self) -> i32 {
        self.inner.index
    }

    #[inline]
    pub fn mode(&self) -> i32 {
        self.inner.mode
    }

    #[inline]
    pub fn label(&self) -> &str {
        unsafe { CStr::from_ptr(self.inner.label).to_str().unwrap() }
    }

    #[inline]
    pub fn action_name(&self) -> &str {
        unsafe { CStr::from_ptr(self.inner.action_name).to_str().unwrap() }
    }
}
unsafe fn init_action_entry(action_entry: *mut ffi::GtkPadActionEntry) {
    std::ptr::write(action_entry, std::mem::zeroed());
}

unsafe fn copy_into_action_entry(
    dest: *mut ffi::GtkPadActionEntry,
    src: *const ffi::GtkPadActionEntry,
) {
    init_action_entry(dest);
    (*dest).action_name = glib::ffi::g_strdup((*src).action_name);
    (*dest).label = glib::ffi::g_strdup((*src).label);
    (*dest).type_ = (*src).type_;
    (*dest).index = (*src).index;
    (*dest).mode = (*src).mode;
}

unsafe fn clear_action_entry(action_entry: *mut ffi::GtkPadActionEntry) {
    glib::ffi::g_free((*action_entry).label as *mut _);
    glib::ffi::g_free((*action_entry).action_name as *mut _);
}