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