rofi  1.7.5
mode.c
Go to the documentation of this file.
1 /*
2  * rofi
3  *
4  * MIT/X11 License
5  * Copyright © 2013-2022 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 "mode-private.h"
43 int mode_init(Mode *mode) {
44  g_return_val_if_fail(mode != NULL, FALSE);
45  g_return_val_if_fail(mode->_init != NULL, FALSE);
46  // to make sure this is initialized correctly.
47  mode->fallback_icon_fetch_uid = 0;
48  mode->fallback_icon_not_found = FALSE;
49  return mode->_init(mode);
50 }
51 
52 void mode_destroy(Mode *mode) {
53  g_assert(mode != NULL);
54  g_assert(mode->_destroy != NULL);
55  mode->_destroy(mode);
56 }
57 
58 unsigned int mode_get_num_entries(const Mode *mode) {
59  g_assert(mode != NULL);
60  g_assert(mode->_get_num_entries != NULL);
61  return mode->_get_num_entries(mode);
62 }
63 
64 char *mode_get_display_value(const Mode *mode, unsigned int selected_line,
65  int *state, GList **attribute_list,
66  int get_entry) {
67  g_assert(mode != NULL);
68  g_assert(state != NULL);
69  g_assert(mode->_get_display_value != NULL);
70 
71  return mode->_get_display_value(mode, selected_line, state, attribute_list,
72  get_entry);
73 }
74 
75 cairo_surface_t *mode_get_icon(Mode *mode, unsigned int selected_line,
76  unsigned int height) {
77  g_assert(mode != NULL);
78 
79  if (mode->_get_icon != NULL) {
80  cairo_surface_t *icon = mode->_get_icon(mode, selected_line, height);
81  if (icon) {
82  return icon;
83  }
84  }
85 
86  if (mode->fallback_icon_not_found == TRUE) {
87  return NULL;
88  }
89  if (mode->fallback_icon_fetch_uid > 0) {
90  cairo_surface_t *icon =
92  return icon;
93  }
94  ThemeWidget *wid = rofi_config_find_widget(mode->name, NULL, TRUE);
95  if (wid) {
97  Property *p =
98  rofi_theme_find_property(wid, P_STRING, "fallback-icon", TRUE);
99  if (p != NULL && (p->type == P_STRING && p->value.s)) {
101  rofi_icon_fetcher_query(p->value.s, height);
102  return NULL;
103  }
104  }
105  mode->fallback_icon_not_found = TRUE;
106  return NULL;
107 }
108 
109 char *mode_get_completion(const Mode *mode, unsigned int selected_line) {
110  g_assert(mode != NULL);
111  if (mode->_get_completion != NULL) {
112  return mode->_get_completion(mode, selected_line);
113  }
114  int state;
115  g_assert(mode->_get_display_value != NULL);
116  return mode->_get_display_value(mode, selected_line, &state, NULL, TRUE);
117 }
118 
119 ModeMode mode_result(Mode *mode, int menu_retv, char **input,
120  unsigned int selected_line) {
121  if (menu_retv & MENU_NEXT) {
122  return NEXT_DIALOG;
123  }
124  if (menu_retv & MENU_PREVIOUS) {
125  return PREVIOUS_DIALOG;
126  }
127  if (menu_retv & MENU_QUICK_SWITCH) {
128  return menu_retv & MENU_LOWER_MASK;
129  }
130 
131  g_assert(mode != NULL);
132  g_assert(mode->_result != NULL);
133  g_assert(input != NULL);
134 
135  return mode->_result(mode, menu_retv, input, selected_line);
136 }
137 
138 int mode_token_match(const Mode *mode, rofi_int_matcher **tokens,
139  unsigned int selected_line) {
140  g_assert(mode != NULL);
141  g_assert(mode->_token_match != NULL);
142  return mode->_token_match(mode, tokens, selected_line);
143 }
144 
145 const char *mode_get_name(const Mode *mode) {
146  g_assert(mode != NULL);
147  return mode->name;
148 }
149 
150 void mode_free(Mode **mode) {
151  g_assert(mode != NULL);
152  g_assert((*mode) != NULL);
153  if ((*mode)->free != NULL) {
154  (*mode)->free(*mode);
155  }
156  (*mode) = NULL;
157 }
158 
159 void *mode_get_private_data(const Mode *mode) {
160  g_assert(mode != NULL);
161  return mode->private_data;
162 }
163 
164 void mode_set_private_data(Mode *mode, void *pd) {
165  g_assert(mode != NULL);
166  if (pd != NULL) {
167  g_assert(mode->private_data == NULL);
168  }
169  mode->private_data = pd;
170 }
171 
172 const char *mode_get_display_name(const Mode *mode) {
174  ThemeWidget *wid = rofi_config_find_widget(mode->name, NULL, TRUE);
175  if (wid) {
177  Property *p = rofi_theme_find_property(wid, P_STRING, "display-name", TRUE);
178  if (p != NULL && p->type == P_STRING) {
179  return p->value.s;
180  }
181  }
182  if (mode->display_name != NULL) {
183  return mode->display_name;
184  }
185  return mode->name;
186 }
187 
188 void mode_set_config(Mode *mode) {
189  snprintf(mode->cfg_name_key, 128, "display-%s", mode->name);
191  (void **)&(mode->display_name),
192  "The display name of this browser");
193 }
194 
195 char *mode_preprocess_input(Mode *mode, const char *input) {
196  if (mode->_preprocess_input) {
197  return mode->_preprocess_input(mode, input);
198  }
199  return g_strdup(input);
200 }
201 char *mode_get_message(const Mode *mode) {
202  if (mode->_get_message) {
203  return mode->_get_message(mode);
204  }
205  return NULL;
206 }
void config_parser_add_option(XrmOptionType type, const char *key, void **value, const char *comment)
Definition: xrmoptions.c:455
@ xrm_String
Definition: xrmoptions.h:74
cairo_surface_t * rofi_icon_fetcher_get(const uint32_t uid)
uint32_t rofi_icon_fetcher_query(const char *name, const int size)
char * mode_get_completion(const Mode *mode, unsigned int selected_line)
Definition: mode.c:109
void mode_destroy(Mode *mode)
Definition: mode.c:52
int mode_init(Mode *mode)
Definition: mode.c:43
unsigned int mode_get_num_entries(const Mode *mode)
Definition: mode.c:58
void mode_free(Mode **mode)
Definition: mode.c:150
const char * mode_get_display_name(const Mode *mode)
Definition: mode.c:172
ModeMode mode_result(Mode *mode, int menu_retv, char **input, unsigned int selected_line)
Definition: mode.c:119
char * mode_get_display_value(const Mode *mode, unsigned int selected_line, int *state, GList **attribute_list, int get_entry)
Definition: mode.c:64
char * mode_preprocess_input(Mode *mode, const char *input)
Definition: mode.c:195
void mode_set_private_data(Mode *mode, void *pd)
Definition: mode.c:164
cairo_surface_t * mode_get_icon(Mode *mode, unsigned int selected_line, unsigned int height)
Definition: mode.c:75
int mode_token_match(const Mode *mode, rofi_int_matcher **tokens, unsigned int selected_line)
Definition: mode.c:138
char * mode_get_message(const Mode *mode)
Definition: mode.c:201
void * mode_get_private_data(const Mode *mode)
Definition: mode.c:159
ModeMode
Definition: mode.h:49
const char * mode_get_name(const Mode *mode)
Definition: mode.c:145
void mode_set_config(Mode *mode)
Definition: mode.c:188
@ 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
@ P_STRING
Definition: rofi-types.h:16
PropertyValue value
Definition: rofi-types.h:297
PropertyType type
Definition: rofi-types.h:295
Definition: icon.c:39
_mode_result _result
Definition: mode-private.h:177
__mode_get_num_entries _get_num_entries
Definition: mode-private.h:175
__mode_destroy _destroy
Definition: mode-private.h:173
_mode_preprocess_input _preprocess_input
Definition: mode-private.h:187
char * display_name
Definition: mode-private.h:165
_mode_token_match _token_match
Definition: mode-private.h:179
uint32_t fallback_icon_fetch_uid
Definition: mode-private.h:207
_mode_get_display_value _get_display_value
Definition: mode-private.h:181
_mode_get_icon _get_icon
Definition: mode-private.h:183
_mode_get_completion _get_completion
Definition: mode-private.h:185
char cfg_name_key[128]
Definition: mode-private.h:164
uint32_t fallback_icon_not_found
Definition: mode-private.h:208
__mode_init _init
Definition: mode-private.h:171
char * name
Definition: mode-private.h:163
_mode_get_message _get_message
Definition: mode-private.h:189
void * private_data
Definition: mode-private.h:192
ThemeWidget * rofi_config_find_widget(const char *name, const char *state, gboolean exact)
Definition: theme.c:778
Property * rofi_theme_find_property(ThemeWidget *widget, PropertyType type, const char *property, gboolean exact)
Definition: theme.c:740