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"
37 /**
38  * @ingroup MODE
39  * @{
40  */
41 
mode_init(Mode * mode)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 
mode_destroy(Mode * mode)49 void mode_destroy ( Mode *mode )
50 {
51     g_assert ( mode != NULL );
52     g_assert ( mode->_destroy != NULL );
53     mode->_destroy ( mode );
54 }
55 
mode_get_num_entries(const Mode * mode)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 
mode_get_display_value(const Mode * mode,unsigned int selected_line,int * state,GList ** attribute_list,int get_entry)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 
mode_get_icon(const Mode * mode,unsigned int selected_line,int height)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 
mode_get_completion(const Mode * mode,unsigned int selected_line)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 
mode_result(Mode * mode,int menu_retv,char ** input,unsigned int selected_line)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 
mode_token_match(const Mode * mode,rofi_int_matcher ** tokens,unsigned int selected_line)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 
mode_get_name(const Mode * mode)123 const char *mode_get_name ( const Mode *mode )
124 {
125     g_assert ( mode != NULL );
126     return mode->name;
127 }
128 
mode_free(Mode ** mode)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 
mode_get_private_data(const Mode * mode)139 void *mode_get_private_data ( const Mode *mode )
140 {
141     g_assert ( mode != NULL );
142     return mode->private_data;
143 }
144 
mode_set_private_data(Mode * mode,void * pd)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 
mode_get_display_name(const Mode * mode)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 
mode_set_config(Mode * mode)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 
mode_preprocess_input(Mode * mode,const char * input)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 }
mode_get_message(const Mode * mode)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 }
182 /**@}*/
183