rofi 1.7.7
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"
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
162void mode_free(Mode **mode) {
163 g_assert(mode != NULL);
164 g_assert((*mode) != NULL);
165 if ((*mode)->free != NULL) {
166 (*mode)->free(*mode);
167 }
168 (*mode) = NULL;
169}
170
171void *mode_get_private_data(const Mode *mode) {
172 g_assert(mode != NULL);
173 return mode->private_data;
174}
175
176void mode_set_private_data(Mode *mode, void *pd) {
177 g_assert(mode != NULL);
178 if (pd != NULL) {
179 g_assert(mode->private_data == NULL);
180 }
181 mode->private_data = pd;
182}
183
184const char *mode_get_display_name(const Mode *mode) {
186 ThemeWidget *wid = rofi_config_find_widget(mode->name, NULL, TRUE);
187 if (wid) {
189 Property *p = rofi_theme_find_property(wid, P_STRING, "display-name", TRUE);
190 if (p != NULL && p->type == P_STRING) {
191 return p->value.s;
192 }
193 }
194 if (mode->display_name != NULL) {
195 return mode->display_name;
196 }
197 return mode->name;
198}
199
201 snprintf(mode->cfg_name_key, 128, "display-%s", mode->name);
203 (void **)&(mode->display_name),
204 "The display name of this browser");
205}
206
207char *mode_preprocess_input(Mode *mode, const char *input) {
208 if (mode->_preprocess_input) {
209 return mode->_preprocess_input(mode, input);
210 }
211 return g_strdup(input);
212}
213char *mode_get_message(const Mode *mode) {
214 if (mode->_get_message) {
215 return mode->_get_message(mode);
216 }
217 return NULL;
218}
219
220Mode *mode_create(const Mode *mode) {
221 if (mode->_create) {
222 return mode->_create();
223 }
224 return NULL;
225}
226
227ModeMode mode_completer_result(Mode *mode, int menu_retv, char **input,
228 unsigned int selected_line, char **path) {
229 if ((mode->type & MODE_TYPE_COMPLETER) == 0) {
230 g_warning("Trying to call completer_result on non completion mode.");
231 return 0;
232 }
233 if (mode->_completer_result) {
234 return mode->_completer_result(mode, menu_retv, input, selected_line, path);
235 }
236 return 0;
237}
238
239gboolean mode_is_completer(const Mode *mode) {
240 if (mode) {
242 return TRUE;
243 }
244 }
245 return FALSE;
246}
247
void config_parser_add_option(XrmOptionType type, const char *key, void **value, const char *comment)
Definition xrmoptions.c:468
@ 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:207
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:184
unsigned int mode_get_num_entries(const Mode *mode)
Definition mode.c:70
void mode_free(Mode **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:220
gboolean mode_is_completer(const Mode *mode)
Definition mode.c:239
ModeMode mode_completer_result(Mode *mode, int menu_retv, char **input, unsigned int selected_line, char **path)
Definition mode.c:227
void * mode_get_private_data(const Mode *mode)
Definition mode.c:171
char * mode_get_message(const Mode *mode)
Definition mode.c:213
void mode_set_private_data(Mode *mode, void *pd)
Definition mode.c:176
int mode_token_match(const Mode *mode, rofi_int_matcher **tokens, unsigned int selected_line)
Definition mode.c:150
ModeMode
Definition mode.h:49
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:200
char * mode_get_completion(const Mode *mode, unsigned int selected_line)
Definition mode.c:121
@ MENU_LOWER_MASK
Definition mode.h:87
@ MENU_PREVIOUS
Definition mode.h:81
@ MENU_QUICK_SWITCH
Definition mode.h:77
@ MENU_NEXT
Definition mode.h:71
@ NEXT_DIALOG
Definition mode.h:53
@ PREVIOUS_DIALOG
Definition mode.h:57
struct _icon icon
Definition icon.h:44
@ MODE_TYPE_COMPLETER
@ MODE_TYPE_UNSET
@ P_STRING
Definition rofi-types.h:16
PropertyValue value
Definition rofi-types.h:293
PropertyType type
Definition rofi-types.h:291
Definition icon.c:40
_mode_result _result
__mode_get_num_entries _get_num_entries
__mode_destroy _destroy
_mode_preprocess_input _preprocess_input
char * display_name
_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]
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