rofi_plugin_sys/
mode_private.rs

1//! Bindings to mode-private.h
2
3use {
4    crate::types::RofiIntMatcher,
5    ::std::{
6        ffi::c_void,
7        os::raw::{c_char, c_int, c_uint},
8        ptr,
9    },
10    bitflags::bitflags,
11    std::mem,
12};
13
14/// ABI version to check if loaded plugin is compatible.
15///
16/// The current value is 7.
17pub const ABI_VERSION: c_uint = 7;
18
19bitflags! {
20    /// Indicator what type of mode this is.
21    /// For now it can be the classic switcher, or also implement a completer.
22    #[repr(transparent)]
23    #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
24    pub struct ModeType: c_uint {
25        /// Mode type is not set.
26        const UNSET = 0x0;
27        /// A normal mode.
28        const SWITCHER = 0x1;
29        /// A mode that can be used to completer.
30        const COMPLETER = 0x2;
31        /// DMenu mode.
32        const DMENU = 0x4;
33    }
34}
35
36#[repr(C)]
37#[allow(dead_code)]
38enum ModeTypeSizeChecker {
39    Unset = 0,
40    Switcher = 1,
41    Completer = 2,
42}
43
44// Make sure the enum's sizes match
45const _: [(); mem::size_of::<ModeType>()] = [(); mem::size_of::<ModeTypeSizeChecker>()];
46
47/// Free the switcher.
48///
49/// Only to be used when the switcher object itself is dynamic and has data in [`Mode::ed`].
50pub type ModeFree = Option<unsafe extern "C" fn(data: *mut Mode)>;
51
52/// Get the string to display for the entry.
53///
54/// Returns the string and state for displaying.
55///
56/// - `selected_line`: The selected line
57/// - `state`: The state to display \[out\]
58/// - `attribute_list`: List of extra (pango) attributes to apply when displaying. \[out\] \[null\]
59/// - `get_entry`: If it should only return the state
60pub type ModeGetDisplayValue = Option<
61    unsafe extern "C" fn(
62        sw: *const Mode,
63        selected_line: c_uint,
64        state: *mut c_int,
65        attribute_list: *mut *mut glib_sys::GList,
66        get_entry: c_int,
67    ) -> *mut c_char,
68>;
69
70/// Obtains the icon of the entry if available.
71///
72/// - `selected_line`: The selected line.
73pub type ModeGetIcon = Option<
74    unsafe extern "C" fn(
75        sw: *const Mode,
76        selected_line: c_uint,
77        height: c_int,
78    ) -> *mut cairo_sys::cairo_surface_t,
79>;
80
81/// Obtains the string to complete.
82///
83/// - `selected_line`: The selected line
84pub type ModeGetCompletion =
85    Option<unsafe extern "C" fn(sw: *const Mode, selected_line: c_uint) -> *mut c_char>;
86
87/// Token match for the matching algorithm.
88///
89/// Returns 1 when it matches, 0 if not.
90///
91/// - `tokens`: List of (input) tokens to match.
92/// - `input`: The entry to match against.
93/// - `case_sensitive`: Whether case is significant.
94/// - `index`: The current selected index.
95/// - `data`: User data.
96pub type ModeTokenMatch = Option<
97    unsafe extern "C" fn(sw: *const Mode, tokens: *mut *mut RofiIntMatcher, index: c_uint) -> c_int,
98>;
99
100/// Initialize the Mode.
101///
102/// Returns `true` if successful.
103pub type ModeInit = Option<unsafe extern "C" fn(sw: *mut Mode) -> c_int>;
104
105/// Get the number of entries to display (unfiltered).
106pub type ModeGetNumEntries = Option<unsafe extern "C" fn(sw: *const Mode) -> c_uint>;
107
108/// Destroy the current mode. Still ready to restart.
109pub type ModeDestroy = Option<unsafe extern "C" fn(sw: *mut Mode)>;
110
111/// Process the result of the user selection.
112///
113/// Returns the next action to take.
114///
115/// - `menu_retv`: The return value
116/// - `input`: The input string
117/// - `selected_line`: The selected line
118pub type ModeResult = Option<
119    unsafe extern "C" fn(
120        sw: *mut Mode,
121        menu_retv: c_int,
122        input: *mut *mut c_char,
123        selected_line: c_uint,
124    ) -> c_int,
125>;
126
127/// Preprocess the input for sorting.
128///
129/// Returns the entry stripped from markup for sorting.
130///
131/// - `input`: The input string
132pub type ModePreprocessInput =
133    Option<unsafe extern "C" fn(sw: *mut Mode, input: *const c_char) -> *mut c_char>;
134
135/// Message to show in the message bar.
136///
137/// Returns the (valid Pango markup) message to display.
138pub type ModeGetMessage = Option<unsafe extern "C" fn(sw: *const Mode) -> *mut c_char>;
139
140/// Create a new instance of this mode.
141/// Free (free) result after use, after using `mode_destroy`.
142pub type ModeCreate = Option<unsafe extern "C" fn() -> *mut Mode>;
143
144/// Handle the user accepting an entry in completion mode.
145/// Returns the next action to take.
146/// - `sw`: The mode pointer
147/// - `menu_retv`: The return value
148/// - `input`: The input string
149/// - `selected_line`: The selected line
150/// - `path`: the path that was completed
151pub type ModeCompleterResult = Option<
152    unsafe extern "C" fn(*mut Mode, c_int, *mut *mut c_char, c_uint, *mut *mut c_char) -> c_uint,
153>;
154
155/// Structure defining a switcher.
156///
157/// Access should be done through `mode_*` functions,
158/// not the function pointer fields on this type.
159///
160/// It consists of a name, callback and if enabled a textbox for the sidebar-mode.
161#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
162#[repr(C)]
163pub struct Mode {
164    /// Used for external plugins.
165    ///
166    /// You should set this to [`ABI_VERSION`].
167    pub abi_version: c_uint,
168
169    /// Name (max 31 char long)
170    pub name: *mut c_char,
171    pub cfg_name_key: [c_char; 128],
172    pub display_name: *mut c_char,
173
174    /// Initialize the Mode.
175    ///
176    /// Returns `true` if successful.
177    pub _init: ModeInit,
178
179    /// Destroy the switcher, e.g. free all its memory. Still ready to restart.
180    pub _destroy: ModeDestroy,
181
182    /// Get the number of entries to display (unfiltered).
183    pub _get_num_entries: ModeGetNumEntries,
184
185    /// Process the result of the user selection.
186    pub _result: ModeResult,
187
188    /// Token match for the matching algorithm.
189    pub _token_match: ModeTokenMatch,
190
191    /// Get the string to display for the entry.
192    pub _get_display_value: ModeGetDisplayValue,
193
194    /// Obtains the icon of the entry if available.
195    pub _get_icon: ModeGetIcon,
196
197    /// Obtains the string to complete.
198    pub _get_completion: ModeGetCompletion,
199
200    /// Preprocess the input for sorting.
201    pub _preprocess_input: ModePreprocessInput,
202
203    /// Message to show in the message bar.
204    pub _get_message: ModeGetMessage,
205
206    /// Pointer to private data.
207    pub private_data: *mut c_void,
208
209    /// Free the switcher.
210    pub free: ModeFree,
211
212    /// Create mode.
213    pub _create: ModeCreate,
214
215    /// If this mode is used as a completer.
216    pub _completer_result: ModeCompleterResult,
217
218    /// Extra fields for the script.
219    pub ed: *mut c_void,
220
221    /// Module
222    pub module: *mut GModule,
223
224    /// Fallback icon
225    pub fallback_icon_fetch_uid: u32,
226
227    /// Fallback icon
228    pub fallback_icon_not_found: u32,
229
230    /// Type.
231    pub r#type: ModeType,
232}
233
234impl Mode {
235    const DEFAULT: Self = Self {
236        abi_version: ABI_VERSION,
237        name: ptr::null_mut(),
238        cfg_name_key: [0; 128],
239        display_name: ptr::null_mut(),
240        _init: None,
241        _destroy: None,
242        _get_num_entries: None,
243        _result: None,
244        _token_match: None,
245        _get_display_value: None,
246        _get_icon: None,
247        _get_completion: None,
248        _preprocess_input: None,
249        _get_message: None,
250        private_data: ptr::null_mut(),
251        free: None,
252        _create: None,
253        _completer_result: None,
254        ed: ptr::null_mut(),
255        module: ptr::null_mut(),
256        fallback_icon_fetch_uid: 0,
257        fallback_icon_not_found: 0,
258        r#type: ModeType::SWITCHER,
259    };
260
261    /// Create a [`Mode`] with all `None`/null fields.
262    pub const fn default() -> Self {
263        Self::DEFAULT
264    }
265}
266
267impl Default for Mode {
268    fn default() -> Self {
269        Self::DEFAULT
270    }
271}
272
273// Mode needs to be put in a static
274unsafe impl Sync for Mode {}
275
276/// An opaque C type from GLib.
277#[derive(Debug, Copy, Clone)]
278#[repr(C)]
279pub struct GModule {
280    _unused: [u8; 0],
281}