Skip to main content

rofi_plugin_sys/
mode_private.rs

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