summaryrefslogtreecommitdiffstats
path: root/source/mode.c
blob: 8c0a9fc08266954d2969d2c2cfaccc7d1ee8b590 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * rofi
 *
 * MIT/X11 License
 * Copyright © 2013-2021 Qball Cow <qball@gmpclient.org>
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

#include <stdio.h>
#include <glib.h>
#include <string.h>
#include "rofi.h"
#include "xrmoptions.h"
#include "mode.h"

// This one should only be in mode implementations.
#include "mode-private.h"
/**
 * @ingroup MODE
 * @{
 */

int mode_init ( Mode *mode )
{
    g_return_val_if_fail ( mode != NULL, FALSE );
    g_return_val_if_fail ( mode->_init != NULL, FALSE );
    return mode->_init ( mode );
}

void mode_destroy ( Mode *mode )
{
    g_assert ( mode != NULL );
    g_assert ( mode->_destroy != NULL );
    mode->_destroy ( mode );
}

unsigned int mode_get_num_entries ( const Mode *mode )
{
    g_assert ( mode != NULL );
    g_assert ( mode->_get_num_entries != NULL );
    return mode->_get_num_entries ( mode );
}

char * mode_get_display_value ( const Mode *mode, unsigned int selected_line, int *state, GList **attribute_list, int get_entry )
{
    g_assert ( mode != NULL );
    g_assert ( state != NULL );
    g_assert ( mode->_get_display_value != NULL );

    return mode->_get_display_value ( mode, selected_line, state, attribute_list, get_entry );
}

cairo_surface_t * mode_get_icon ( const Mode *mode, unsigned int selected_line, int height )
{
    g_assert ( mode != NULL );

    if ( mode->_get_icon != NULL ) {
        return mode->_get_icon ( mode, selected_line, height );
    }
    else {
        return NULL;
    }
}

char * mode_get_completion ( const Mode *mode, unsigned int selected_line )
{
    g_assert ( mode != NULL );
    if ( mode->_get_completion != NULL ) {
        return mode->_get_completion ( mode, selected_line );
    }
    else {
        int state;
        g_assert ( mode->_get_display_value != NULL );
        return mode->_get_display_value ( mode, selected_line, &state, NULL, TRUE );
    }
}

ModeMode mode_result ( Mode *mode, int menu_retv, char **input, unsigned int selected_line )
{
    if ( menu_retv & MENU_NEXT ) {
        return NEXT_DIALOG;
    }
    else if ( menu_retv & MENU_PREVIOUS ) {
        return PREVIOUS_DIALOG;
    }
    else if ( menu_retv & MENU_QUICK_SWITCH ) {
        return menu_retv & MENU_LOWER_MASK;
    }

    g_assert ( mode != NULL );
    g_assert ( mode->_result != NULL );
    g_assert ( input != NULL );

    return mode->_result ( mode, menu_retv, input, selected_line );
}

int mode_token_match ( const Mode *mode, rofi_int_matcher **tokens, unsigned int selected_line )
{
    g_assert ( mode != NULL );
    g_assert ( mode->_token_match != NULL );
    return mode->_token_match ( mode, tokens, selected_line );
}

const char *mode_get_name ( const Mode *mode )
{
    g_assert ( mode != NULL );
    return mode->name;
}

void mode_free ( Mode **mode )
{
    g_assert ( mode != NULL );
    g_assert ( ( *mode ) != NULL );
    if ( ( *mode )->free != NULL ) {
        ( *mode )->free ( *mode );
    }
    ( *mode ) = NULL;
}

void *mode_get_private_data ( const Mode *mode )
{
    g_assert ( mode != NULL );
    return mode->private_data;
}

void mode_set_private_data ( Mode *mode, void *pd )
{
    g_assert ( mode != NULL );
    if ( pd != NULL ) {
        g_assert ( mode->private_data == NULL );
    }
    mode->private_data = pd;
}

const char *mode_get_display_name ( const Mode *mode )
{
  ThemeWidget *wid = rofi_config_find_widget ( mode->name, NULL, TRUE );
  if ( wid ) {
    Property    *p   = rofi_theme_find_property ( wid, P_STRING, "display-name", FALSE );
    if ( p != NULL ) {
      return p->value.s;
    }
  }
  if ( mode->display_name != NULL ) {
    return mode->display_name;
    }
    return mode->name;
}

void mode_set_config ( Mode *mode )
{
    snprintf ( mode->cfg_name_key, 128, "display-%s", mode->name );
    config_parser_add_option ( xrm_String, mode->cfg_name_key, (void * *) &( mode->display_name ), "The display name of this browser" );
}

char * mode_preprocess_input ( Mode *mode, const char *input )
{
    if ( mode->_preprocess_input ) {
        return mode->_preprocess_input ( mode, input );
    }
    return g_strdup ( input );
}
char *mode_get_message ( const Mode *mode )
{
    if ( mode->_get_message ) {
        return mode->_get_message ( mode );
    }
    return NULL;
}
/**@}*/