rofi  1.7.0
mode.c
Go to the documentation of this file.
1 /*
2  * rofi
3  *
4  * MIT/X11 License
5  * Copyright © 2013-2021 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 // This one should only be in mode implementations.
36 #include "mode-private.h"
42 int mode_init(Mode *mode) {
43  g_return_val_if_fail(mode != NULL, FALSE);
44  g_return_val_if_fail(mode->_init != NULL, FALSE);
45  return mode->_init(mode);
46 }
47 
48 void mode_destroy(Mode *mode) {
49  g_assert(mode != NULL);
50  g_assert(mode->_destroy != NULL);
51  mode->_destroy(mode);
52 }
53 
54 unsigned int mode_get_num_entries(const Mode *mode) {
55  g_assert(mode != NULL);
56  g_assert(mode->_get_num_entries != NULL);
57  return mode->_get_num_entries(mode);
58 }
59 
60 char *mode_get_display_value(const Mode *mode, unsigned int selected_line,
61  int *state, GList **attribute_list,
62  int get_entry) {
63  g_assert(mode != NULL);
64  g_assert(state != NULL);
65  g_assert(mode->_get_display_value != NULL);
66 
67  return mode->_get_display_value(mode, selected_line, state, attribute_list,
68  get_entry);
69 }
70 
71 cairo_surface_t *mode_get_icon(const Mode *mode, unsigned int selected_line,
72  int height) {
73  g_assert(mode != NULL);
74 
75  if (mode->_get_icon != NULL) {
76  return mode->_get_icon(mode, selected_line, height);
77  }
78  return NULL;
79 }
80 
81 char *mode_get_completion(const Mode *mode, unsigned int selected_line) {
82  g_assert(mode != NULL);
83  if (mode->_get_completion != NULL) {
84  return mode->_get_completion(mode, selected_line);
85  }
86  int state;
87  g_assert(mode->_get_display_value != NULL);
88  return mode->_get_display_value(mode, selected_line, &state, NULL, TRUE);
89 }
90 
91 ModeMode mode_result(Mode *mode, int menu_retv, char **input,
92  unsigned int selected_line) {
93  if (menu_retv & MENU_NEXT) {
94  return NEXT_DIALOG;
95  }
96  if (menu_retv & MENU_PREVIOUS) {
97  return PREVIOUS_DIALOG;
98  }
99  if (menu_retv & MENU_QUICK_SWITCH) {
100  return menu_retv & MENU_LOWER_MASK;
101  }
102 
103  g_assert(mode != NULL);
104  g_assert(mode->_result != NULL);
105  g_assert(input != NULL);
106 
107  return mode->_result(mode, menu_retv, input, selected_line);
108 }
109 
110 int mode_token_match(const Mode *mode, rofi_int_matcher **tokens,
111  unsigned int selected_line) {
112  g_assert(mode != NULL);
113  g_assert(mode->_token_match != NULL);
114  return mode->_token_match(mode, tokens, selected_line);
115 }
116 
117 const char *mode_get_name(const Mode *mode) {
118  g_assert(mode != NULL);
119  return mode->name;
120 }
121 
122 void mode_free(Mode **mode) {
123  g_assert(mode != NULL);
124  g_assert((*mode) != NULL);
125  if ((*mode)->free != NULL) {
126  (*mode)->free(*mode);
127  }
128  (*mode) = NULL;
129 }
130 
131 void *mode_get_private_data(const Mode *mode) {
132  g_assert(mode != NULL);
133  return mode->private_data;
134 }
135 
136 void mode_set_private_data(Mode *mode, void *pd) {
137  g_assert(mode != NULL);
138  if (pd != NULL) {
139  g_assert(mode->private_data == NULL);
140  }
141  mode->private_data = pd;
142 }
143 
144 const char *mode_get_display_name(const Mode *mode) {
146  ThemeWidget *wid = rofi_config_find_widget(mode->name, NULL, TRUE);
147  if (wid) {
149  Property *p = rofi_theme_find_property(wid, P_STRING, "display-name", TRUE);
150  if (p != NULL && p->type == P_STRING) {
151  return p->value.s;
152  }
153  }
154  if (mode->display_name != NULL) {
155  return mode->display_name;
156  }
157  return mode->name;
158 }
159 
160 void mode_set_config(Mode *mode) {
161  snprintf(mode->cfg_name_key, 128, "display-%s", mode->name);
163  (void **)&(mode->display_name),
164  "The display name of this browser");
165 }
166 
167 char *mode_preprocess_input(Mode *mode, const char *input) {
168  if (mode->_preprocess_input) {
169  return mode->_preprocess_input(mode, input);
170  }
171  return g_strdup(input);
172 }
173 char *mode_get_message(const Mode *mode) {
174  if (mode->_get_message) {
175  return mode->_get_message(mode);
176  }
177  return NULL;
178 }
void config_parser_add_option(XrmOptionType type, const char *key, void **value, const char *comment)
Definition: xrmoptions.c:424
@ xrm_String
Definition: xrmoptions.h:74
char * mode_get_completion(const Mode *mode, unsigned int selected_line)
Definition: mode.c:81
void mode_destroy(Mode *mode)
Definition: mode.c:48
int mode_init(Mode *mode)
Definition: mode.c:42
unsigned int mode_get_num_entries(const Mode *mode)
Definition: mode.c:54
void mode_free(Mode **mode)
Definition: mode.c:122
const char * mode_get_display_name(const Mode *mode)
Definition: mode.c:144
ModeMode mode_result(Mode *mode, int menu_retv, char **input, unsigned int selected_line)
Definition: mode.c:91
char * mode_get_display_value(const Mode *mode, unsigned int selected_line, int *state, GList **attribute_list, int get_entry)
Definition: mode.c:60
char * mode_preprocess_input(Mode *mode, const char *input)
Definition: mode.c:167
void mode_set_private_data(Mode *mode, void *pd)
Definition: mode.c:136
int mode_token_match(const Mode *mode, rofi_int_matcher **tokens, unsigned int selected_line)
Definition: mode.c:110
char * mode_get_message(const Mode *mode)
Definition: mode.c:173
void * mode_get_private_data(const Mode *mode)
Definition: mode.c:131
ModeMode
Definition: mode.h:49
const char * mode_get_name(const Mode *mode)
Definition: mode.c:117
cairo_surface_t * mode_get_icon(const Mode *mode, unsigned int selected_line, int height)
Definition: mode.c:71
void mode_set_config(Mode *mode)
Definition: mode.c:160
@ 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
@ P_STRING
Definition: rofi-types.h:16
PropertyValue value
Definition: rofi-types.h:290
PropertyType type
Definition: rofi-types.h:288
_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
_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
__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:732
Property * rofi_theme_find_property(ThemeWidget *widget, PropertyType type, const char *property, gboolean exact)
Definition: theme.c:694