rofi 1.7.9
mode.c
Go to the documentation of this file.
1/*
2 * rofi
3 *
4 * MIT/X11 License
5 * Copyright © 2013-2023 Qball Cow <qball@gmpclient.org>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 */
27
28#include "mode.h"
29#include "rofi.h"
30#include "xrmoptions.h"
31#include <glib.h>
32#include <stdio.h>
33#include <string.h>
34
35#include "rofi-icon-fetcher.h"
36// This one should only be in mode implementations.
37#include "helper.h"
38#include "mode-private.h"
43
44int mode_init(Mode *mode) {
45 g_return_val_if_fail(mode != NULL, FALSE);
46 g_return_val_if_fail(mode->_init != NULL, FALSE);
47 if (mode->type == MODE_TYPE_UNSET) {
48 g_warning("Mode '%s' does not have a type set. Please update mode/plugin.",
49 mode->name);
50 }
52 if (mode->_completer_result == NULL) {
53 g_error(
54 "Mode '%s' is incomplete and does not implement _completer_result.",
55 mode->name);
56 }
57 }
58 // to make sure this is initialized correctly.
60 mode->fallback_icon_not_found = FALSE;
61 return mode->_init(mode);
62}
63
64void mode_destroy(Mode *mode) {
65 g_assert(mode != NULL);
66 g_assert(mode->_destroy != NULL);
67 mode->_destroy(mode);
68}
69
70unsigned int mode_get_num_entries(const Mode *mode) {
71 g_assert(mode != NULL);
72 g_assert(mode->_get_num_entries != NULL);
73 return mode->_get_num_entries(mode);
74}
75
76char *mode_get_display_value(const Mode *mode, unsigned int selected_line,
77 int *state, GList **attribute_list,
78 int get_entry) {
79 g_assert(mode != NULL);
80 g_assert(state != NULL);
81 g_assert(mode->_get_display_value != NULL);
82
83 return mode->_get_display_value(mode, selected_line, state, attribute_list,
84 get_entry);
85}
86
87cairo_surface_t *mode_get_icon(Mode *mode, unsigned int selected_line,
88 unsigned int height) {
89 g_assert(mode != NULL);
90
91 if (mode->_get_icon != NULL) {
92 cairo_surface_t *icon = mode->_get_icon(mode, selected_line, height);
93 if (icon) {
94 return icon;
95 }
96 }
97
98 if (mode->fallback_icon_not_found == TRUE) {
99 return NULL;
100 }
101 if (mode->fallback_icon_fetch_uid > 0) {
102 cairo_surface_t *icon =
104 return icon;
105 }
106 ThemeWidget *wid = rofi_config_find_widget(mode->name, NULL, TRUE);
107 if (wid) {
109 Property *p =
110 rofi_theme_find_property(wid, P_STRING, "fallback-icon", TRUE);
111 if (p != NULL && (p->type == P_STRING && p->value.s)) {
113 rofi_icon_fetcher_query(p->value.s, height);
114 return NULL;
115 }
116 }
117 mode->fallback_icon_not_found = TRUE;
118 return NULL;
119}
120
121char *mode_get_completion(const Mode *mode, unsigned int selected_line) {
122 g_assert(mode != NULL);
123 if (mode->_get_completion != NULL) {
124 return mode->_get_completion(mode, selected_line);
125 }
126 int state;
127 g_assert(mode->_get_display_value != NULL);
128 return mode->_get_display_value(mode, selected_line, &state, NULL, TRUE);
129}
130
131ModeMode mode_result(Mode *mode, int menu_retv, char **input,
132 unsigned int selected_line) {
133 if (menu_retv & MENU_NEXT) {
134 return NEXT_DIALOG;
135 }
136 if (menu_retv & MENU_PREVIOUS) {
137 return PREVIOUS_DIALOG;
138 }
139 if (menu_retv & MENU_QUICK_SWITCH) {
140 return menu_retv & MENU_LOWER_MASK;
141 }
142
143 g_assert(mode != NULL);
144 g_assert(mode->_result != NULL);
145 g_assert(input != NULL);
146
147 return mode->_result(mode, menu_retv, input, selected_line);
148}
149
150int mode_token_match(const Mode *mode, rofi_int_matcher **tokens,
151 unsigned int selected_line) {
152 g_assert(mode != NULL);
153 g_assert(mode->_token_match != NULL);
154 return mode->_token_match(mode, tokens, selected_line);
155}
156
157const char *mode_get_name(const Mode *mode) {
158 g_assert(mode != NULL);
159 return mode->name;
160}
161
162int mode_get_abi_version(Mode *const mode) {
163 g_assert(mode != NULL);
164 return mode->abi_version;
165}
166
167void mode_free(Mode **mode) {
168 g_assert(mode != NULL);
169 g_assert((*mode) != NULL);
170 if ((*mode)->free != NULL) {
171 (*mode)->free(*mode);
172 }
173 (*mode) = NULL;
174}
175
176void *mode_get_private_data(const Mode *mode) {
177 g_assert(mode != NULL);
178 return mode->private_data;
179}
180
181void mode_set_private_data(Mode *mode, void *pd) {
182 g_assert(mode != NULL);
183 if (pd != NULL) {
184 g_assert(mode->private_data == NULL);
185 }
186 mode->private_data = pd;
187}
188
189const char *mode_get_display_name(const Mode *mode) {
191 ThemeWidget *wid = rofi_config_find_widget(mode->name, NULL, TRUE);
192 if (wid) {
194 Property *p = rofi_theme_find_property(wid, P_STRING, "display-name", TRUE);
195 if (p != NULL && p->type == P_STRING) {
196 return p->value.s;
197 }
198 }
199 if (mode->display_name != NULL) {
200 return mode->display_name;
201 }
202 return mode->name;
203}
204
206 snprintf(mode->cfg_name_key, 128, "display-%s", mode->name);
208 (void **)&(mode->display_name),
209 "The display name of this browser");
210}
211
212char *mode_preprocess_input(Mode *mode, const char *input) {
213 if (mode->_preprocess_input) {
214 return mode->_preprocess_input(mode, input);
215 }
216 return g_strdup(input);
217}
218char *mode_get_message(const Mode *mode) {
219 if (mode->_get_message) {
220 return mode->_get_message(mode);
221 }
222 return NULL;
223}
224
225Mode *mode_create(const Mode *mode) {
226 if (mode->_create) {
227 return mode->_create();
228 }
229 return NULL;
230}
231
232ModeMode mode_completer_result(Mode *mode, int menu_retv, char **input,
233 unsigned int selected_line, char **path) {
234 if ((mode->type & MODE_TYPE_COMPLETER) == 0) {
235 g_warning("Trying to call completer_result on non completion mode.");
236 return 0;
237 }
238 if (mode->_completer_result) {
239 return mode->_completer_result(mode, menu_retv, input, selected_line, path);
240 }
241 return 0;
242}
243
244gboolean mode_is_completer(const Mode *mode) {
245 if (mode) {
247 return TRUE;
248 }
249 }
250 return FALSE;
251}
252
253void mode_plugin_set_module(Mode *mode, GModule *mod){
254 mode->module = mod;
255}
257 return mode->module;
258}
259
void config_parser_add_option(XrmOptionType type, const char *key, void **value, const char *comment)
Definition xrmoptions.c:523
@ xrm_String
Definition xrmoptions.h:74
Property * rofi_theme_find_property(ThemeWidget *wid, PropertyType type, const char *property, gboolean exact)
Definition theme.c:743
ThemeWidget * rofi_config_find_widget(const char *name, const char *state, gboolean exact)
Definition theme.c:780
cairo_surface_t * rofi_icon_fetcher_get(const uint32_t uid)
uint32_t rofi_icon_fetcher_query(const char *name, const int size)
void mode_destroy(Mode *mode)
Definition mode.c:64
const char * mode_get_name(const Mode *mode)
Definition mode.c:157
char * mode_preprocess_input(Mode *mode, const char *input)
Definition mode.c:212
int mode_init(Mode *mode)
Definition mode.c:44
cairo_surface_t * mode_get_icon(Mode *mode, unsigned int selected_line, unsigned int height)
Definition mode.c:87
const char * mode_get_display_name(const Mode *mode)
Definition mode.c:189
struct rofi_mode Mode
Definition mode.h:49
unsigned int mode_get_num_entries(const Mode *mode)
Definition mode.c:70
void mode_free(Mode **mode)
Definition mode.c:167
int mode_get_abi_version(Mode *const mode)
Definition mode.c:162
ModeMode mode_result(Mode *mode, int menu_retv, char **input, unsigned int selected_line)
Definition mode.c:131
Mode * mode_create(const Mode *mode)
Definition mode.c:225
void mode_plugin_set_module(Mode *mode, GModule *mod)
Definition mode.c:253
gboolean mode_is_completer(const Mode *mode)
Definition mode.c:244
ModeMode mode_completer_result(Mode *mode, int menu_retv, char **input, unsigned int selected_line, char **path)
Definition mode.c:232
void * mode_get_private_data(const Mode *mode)
Definition mode.c:176
char * mode_get_message(const Mode *mode)
Definition mode.c:218
GModule * mode_plugin_get_module(Mode *mode)
Definition mode.c:256
void mode_set_private_data(Mode *mode, void *pd)
Definition mode.c:181
int mode_token_match(const Mode *mode, rofi_int_matcher **tokens, unsigned int selected_line)
Definition mode.c:150
ModeMode
Definition mode.h:54
char * mode_get_display_value(const Mode *mode, unsigned int selected_line, int *state, GList **attribute_list, int get_entry)
Definition mode.c:76
void mode_set_config(Mode *mode)
Definition mode.c:205
char * mode_get_completion(const Mode *mode, unsigned int selected_line)
Definition mode.c:121
@ MENU_LOWER_MASK
Definition mode.h:92
@ MENU_PREVIOUS
Definition mode.h:86
@ MENU_QUICK_SWITCH
Definition mode.h:82
@ MENU_NEXT
Definition mode.h:76
@ NEXT_DIALOG
Definition mode.h:58
@ PREVIOUS_DIALOG
Definition mode.h:62
struct _icon icon
Definition icon.h:44
@ MODE_TYPE_COMPLETER
@ MODE_TYPE_UNSET
@ P_STRING
Definition rofi-types.h:16
struct rofi_int_matcher_t rofi_int_matcher
PropertyValue value
Definition rofi-types.h:293
PropertyType type
Definition rofi-types.h:291
_mode_result _result
__mode_get_num_entries _get_num_entries
__mode_destroy _destroy
_mode_preprocess_input _preprocess_input
char * display_name
unsigned int abi_version
_mode_token_match _token_match
_mode_create _create
uint32_t fallback_icon_fetch_uid
_mode_get_display_value _get_display_value
_mode_get_icon _get_icon
_mode_get_completion _get_completion
char cfg_name_key[128]
GModule * module
uint32_t fallback_icon_not_found
__mode_init _init
_mode_completer_result _completer_result
ModeType type
char * name
_mode_get_message _get_message
void * private_data