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
//! Bindings to mode-private.h

use {
    crate::types::RofiIntMatcher,
    ::std::{
        ffi::c_void,
        os::raw::{c_char, c_int, c_uint},
        ptr,
    },
};

/// ABI version to check if loaded plugin is compatible.
pub const ABI_VERSION: c_uint = 6;

/// Free the switcher.
///
/// Only to be used when the switcher object itself is dynamic and has data in [`Mode::ed`].
pub type ModeFree = Option<unsafe extern "C" fn(data: *mut Mode)>;

/// Get the string to display for the entry.
///
/// Returns the string and state for displaying.
///
/// - `selected_line`: The selected line
/// - `state`: The state to display \[out\]
/// - `attribute_list`: List of extra (pango) attributes to apply when displaying. \[out\] \[null\]
/// - `get_entry`: If it should only return the state
pub type ModeGetDisplayValue = Option<
    unsafe extern "C" fn(
        sw: *const Mode,
        selected_line: c_uint,
        state: *mut c_int,
        attribute_list: *mut *mut glib_sys::GList,
        get_entry: c_int,
    ) -> *mut c_char,
>;

/// Obtains the icon of the entry if available.
///
/// - `selected_line`: The selected line.
pub type ModeGetIcon = Option<
    unsafe extern "C" fn(
        sw: *const Mode,
        selected_line: c_uint,
        height: c_int,
    ) -> *mut cairo_sys::cairo_surface_t,
>;

/// Obtains the string to complete.
///
/// - `selected_line`: The selected line
pub type ModeGetCompletion =
    Option<unsafe extern "C" fn(sw: *const Mode, selected_line: c_uint) -> *mut c_char>;

/// Token match for the matching algorithm.
///
/// Returns 1 when it matches, 0 if not.
///
/// - `tokens`: List of (input) tokens to match.
/// - `input`: The entry to match against.
/// - `case_sensitive`: Whether case is significant.
/// - `index`: The current selected index.
/// - `data`: User data.
pub type ModeTokenMatch = Option<
    unsafe extern "C" fn(sw: *const Mode, tokens: *mut *mut RofiIntMatcher, index: c_uint) -> c_int,
>;

/// Initialize the Mode.
///
/// Returns `true` if successful.
pub type ModeInit = Option<unsafe extern "C" fn(sw: *mut Mode) -> c_int>;

/// Get the number of entries to display (unfiltered).
pub type ModeGetNumEntries = Option<unsafe extern "C" fn(sw: *const Mode) -> c_uint>;

/// Destroy the current mode. Still ready to restart.
pub type ModeDestroy = Option<unsafe extern "C" fn(sw: *mut Mode)>;

/// Process the result of the user selection.
///
/// Returns the next action to take.
///
/// - `menu_retv`: The return value
/// - `input`: The input string
/// - `selected_line`: The selected line
pub type ModeResult = Option<
    unsafe extern "C" fn(
        sw: *mut Mode,
        menu_retv: c_int,
        input: *mut *mut c_char,
        selected_line: c_uint,
    ) -> c_int,
>;

/// Preprocess the input for sorting.
///
/// Returns the entry stripped from markup for sorting.
///
/// - `input`: The input string
pub type ModePreprocessInput =
    Option<unsafe extern "C" fn(sw: *mut Mode, input: *const c_char) -> *mut c_char>;

/// Message to show in the message bar.
///
/// Returns the (valid Pango markup) message to display.
pub type ModeGetMessage = Option<unsafe extern "C" fn(sw: *const Mode) -> *mut c_char>;

/// Structure defining a switcher.
///
/// Access should be done through `mode_*` functions,
/// not the function pointer fields on this type.
///
/// It consists of a name, callback and if enabled a textbox for the sidebar-mode.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(C)]
pub struct Mode {
    /// Used for external plugins.
    ///
    /// You should set this to [`ABI_VERSION`].
    pub abi_version: c_uint,

    /// Name (max 31 char long)
    pub name: *mut c_char,
    pub cfg_name_key: [c_char; 128],
    pub display_name: *mut c_char,

    /// Initialize the Mode.
    ///
    /// Returns `true` if successful.
    pub _init: ModeInit,

    /// Destroy the switcher, e.g. free all its memory. Still ready to restart.
    pub _destroy: ModeDestroy,

    /// Get the number of entries to display (unfiltered).
    pub _get_num_entries: ModeGetNumEntries,

    /// Process the result of the user selection.
    pub _result: ModeResult,

    /// Token match for the matching algorithm.
    pub _token_match: ModeTokenMatch,

    /// Get the string to display for the entry.
    pub _get_display_value: ModeGetDisplayValue,

    /// Obtains the icon of the entry if available.
    pub _get_icon: ModeGetIcon,

    /// Obtains the string to complete.
    pub _get_completion: ModeGetCompletion,

    /// Preprocess the input for sorting.
    pub _preprocess_input: ModePreprocessInput,

    /// Message to show in the message bar.
    pub _get_message: ModeGetMessage,

    /// Pointer to private data.
    pub private_data: *mut c_void,

    /// Free the switcher.
    pub free: ModeFree,

    /// Extra fields for the script.
    pub ed: *mut c_void,

    /// Module
    pub module: *mut GModule,
}

impl Mode {
    const DEFAULT: Self = Self {
        abi_version: ABI_VERSION,
        name: ptr::null_mut(),
        cfg_name_key: [0; 128],
        display_name: ptr::null_mut(),
        _init: None,
        _destroy: None,
        _get_num_entries: None,
        _result: None,
        _token_match: None,
        _get_display_value: None,
        _get_icon: None,
        _get_completion: None,
        _preprocess_input: None,
        _get_message: None,
        private_data: ptr::null_mut(),
        free: None,
        ed: ptr::null_mut(),
        module: ptr::null_mut(),
    };

    /// Create a [`Mode`] with all `None`/null fields.
    pub const fn default() -> Self {
        Self::DEFAULT
    }
}

impl Default for Mode {
    fn default() -> Self {
        Self::DEFAULT
    }
}

// Mode needs to be put in a static
unsafe impl Sync for Mode {}

/// An opaque C type from GLib.
#[derive(Debug, Copy, Clone)]
#[repr(C)]
pub struct GModule {
    _unused: [u8; 0],
}