-
Notifications
You must be signed in to change notification settings - Fork 10
/
switch.c
401 lines (332 loc) · 11.1 KB
/
switch.c
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
* =============================================================================
*
* Filename: switch.c
*
* Description: g3kb-switch implementation
*
* Version: 1.0
* Created: 13.12.2019 12:13:37
* Revision: none
* Compiler: gcc
*
* Author: Alexey Radkov (),
* Company:
*
* =============================================================================
*/
#include <errno.h>
#include <gio/gio.h>
#include <stdlib.h>
#include "switch.h"
#define G3KB_SWITCH_MAX_LAYOUTS 256
#define G3KB_SWITCH_DBUS_CALL_TIMEOUT 2000
G_DEFINE_QUARK( g3kb_switch_error_quark, g3kb_switch_error )
#define G3KB_SWITCH_ERROR g3kb_switch_error_quark()
enum g3kb_switch_error_type {
G3KB_SWITCH_ERROR_RUN_METHOD,
G3KB_SWITCH_ERROR_BUILD_LAYOUTS_MAP,
G3KB_SWITCH_ERROR_GET_LAYOUT,
G3KB_SWITCH_ERROR_SET_LAYOUT,
};
struct value_search_data {
const gchar *value;
guintptr idx;
};
/* compare two guintptr values */
static gint key_compare( gconstpointer k1, gconstpointer k2, gpointer unused )
{
guintptr a, b;
a = (guintptr)k1;
b = (guintptr)k2;
if ( a > b ) {
return 1;
}
if ( a == b ) {
return 0;
}
return -1; /* if a < b */
}
static gboolean value_search( gpointer k, gpointer v, gpointer data )
{
guintptr key;
gchar *value;
struct value_search_data *vs;
key = (guintptr)k;
value = (gchar *)v;
vs = (struct value_search_data *)data;
vs->idx = G3KB_SWITCH_MAX_LAYOUTS;
if ( g_strcmp0( vs->value, value ) == 0 ) {
vs->idx = key;
return TRUE;
}
return FALSE;
}
gboolean g3kb_print_layouts( gpointer k, gpointer v, gpointer unused )
{
g_print( "%s\n", (const char *)v );
return FALSE;
}
static gboolean run_method( const gchar *name, const gchar *method,
const GVariantType *vtype, gchar **value,
GError **err )
{
GDBusConnection *c = NULL;
GVariant *result = NULL;
GVariant *vmethod = NULL;
GVariant *param = NULL;
GVariantBuilder builder;
gboolean success;
c = g_bus_get_sync( G_BUS_TYPE_SESSION, NULL, err );
if ( c == NULL ) {
return FALSE;
}
if ( method != NULL ) {
g_variant_builder_init( &builder, G_VARIANT_TYPE_TUPLE );
vmethod = g_variant_parse( vtype, method, NULL, NULL, NULL );
if ( vmethod == NULL ) {
g_set_error( err, G3KB_SWITCH_ERROR, G3KB_SWITCH_ERROR_RUN_METHOD,
"Failed to parse input method" );
g_object_unref( c );
return FALSE;
}
g_variant_builder_add_value( &builder, vmethod );
param = g_variant_builder_end( &builder );
if ( param == NULL ) {
g_set_error( err, G3KB_SWITCH_ERROR, G3KB_SWITCH_ERROR_RUN_METHOD,
"Failed to build call parameter" );
g_object_unref( c );
return FALSE;
}
}
result = g_dbus_connection_call_sync(
c, "org.gnome.Shell",
#ifdef G3KBSWITCH_WITH_GNOME_SHELL_EXTENSION
"/org/g3kbswitch/G3kbSwitch", "org.g3kbswitch.G3kbSwitch",
#else
"/org/gnome/Shell", "org.gnome.Shell",
#endif
name, param, NULL, G_DBUS_CALL_FLAGS_NONE,
G3KB_SWITCH_DBUS_CALL_TIMEOUT, NULL, err );
if ( method != NULL ) {
g_variant_unref( vmethod );
g_object_unref( c );
}
if ( result == NULL ) {
return FALSE;
}
if ( !g_variant_is_of_type( result, G_VARIANT_TYPE( "(bs)" ) ) ) {
g_set_error( err, G3KB_SWITCH_ERROR, G3KB_SWITCH_ERROR_RUN_METHOD,
"Unexpected response type" );
g_variant_unref( result );
return FALSE;
}
g_variant_get( result, "(bs)", &success, value );
if ( !success ) {
g_set_error( err, G3KB_SWITCH_ERROR, G3KB_SWITCH_ERROR_RUN_METHOD,
"Bad response: %s",
value == NULL || *value == NULL ? "<empty>" : *value );
if ( value ) {
g_free( *value );
}
g_variant_unref( result );
return FALSE;
}
g_variant_unref( result );
return TRUE;
}
GTree *g3kb_build_layouts_map( GError **err )
{
GVariant *vdict = NULL;
GVariantIter iter1, *iter2;
GTree *layouts = NULL;
const gchar *name = NULL;
const gchar *method;
const GVariantType *vtype;
gchar *key, *value;
gchar *dict = NULL;
#ifdef G3KBSWITCH_WITH_GNOME_SHELL_EXTENSION
name = "List";
method = NULL;
vtype = NULL;
#else
name = "Eval";
/* BEWARE: g3kb_get_layout() takes currentSource.index while here we simply
* put counter i as the value of the key when iterating inputSources, this
* should be correct as soon as currentSource.index drives iteration
* order; to ensure correctness we could also put inputSources[i].index
* instead of i */
method = "\"var ids=[];"
"for(var i in imports.ui.status.keyboard.getInputSourceManager()"
".inputSources){"
"ids.push({key:i,value:"
"imports.ui.status.keyboard.getInputSourceManager()"
".inputSources[i].id})};"
"ids\"";
vtype = G_VARIANT_TYPE_STRING;
#endif
if ( !run_method( name, method, vtype, &dict, err ) ) {
return NULL;
}
vdict = g_variant_parse( NULL, dict, NULL, NULL, NULL );
if ( vdict == NULL ) {
g_set_error( err, G3KB_SWITCH_ERROR,
G3KB_SWITCH_ERROR_BUILD_LAYOUTS_MAP,
"Failed to parse response value" );
g_free( dict );
return NULL;
}
g_free( dict );
if ( !g_variant_is_of_type( vdict, G_VARIANT_TYPE( "aa{ss}" ) ) ) {
g_set_error( err, G3KB_SWITCH_ERROR,
G3KB_SWITCH_ERROR_BUILD_LAYOUTS_MAP,
"Unexpected type of response value" );
g_variant_unref( vdict );
return NULL;
}
layouts = g_tree_new_full( key_compare, NULL, NULL, g_free );
gsize size = g_variant_iter_init( &iter1, vdict );
for ( gsize i = 0; i < size; i++ ) {
g_variant_iter_loop( &iter1, "a{ss}", &iter2 );
while ( g_variant_iter_loop( iter2, "{ss}", &key, &value ) ) {
if ( g_strcmp0( key, "value" ) == 0 ) {
g_tree_insert(
layouts, (gpointer)i,
g_strdup( g_strcmp0( value, "" ) ? value : "(nil)" ) );
}
}
}
g_variant_unref( vdict );
return layouts;
}
guint g3kb_get_layout( GError **err )
{
const gchar *name = NULL;
const gchar *method;
gchar *value = NULL;
const GVariantType *vtype;
guintptr idx = G3KB_SWITCH_MAX_LAYOUTS;
#ifdef G3KBSWITCH_WITH_GNOME_SHELL_EXTENSION
name = "Get";
vtype = NULL;
method = NULL;
#else
name = "Eval";
vtype = G_VARIANT_TYPE_STRING;
method = "\"imports.ui.status.keyboard.getInputSourceManager()"
".currentSource.index\"";
#endif
if ( !run_method( name, method, vtype, &value, err ) ) {
/* BEWARE: return invalid value G3KB_SWITCH_MAX_LAYOUTS, but as far as
* the returned value is supposed to be later passed to
* g3kb_search_layout(), this should not be a problem */
return G3KB_SWITCH_MAX_LAYOUTS;
}
errno = 0;
/* see why we do not use g_ascii_strtoull() or g_ascii_string_to_unsigned()
* in a comment inside g3kb_build_layouts_map() */
idx = (guintptr)strtoull( value, NULL, 10 );
if ( errno != 0 || idx >= G3KB_SWITCH_MAX_LAYOUTS ) {
g_set_error( err, G3KB_SWITCH_ERROR, G3KB_SWITCH_ERROR_GET_LAYOUT,
"Key %s is not a valid index",
value == NULL ? "<empty>" : value );
idx = G3KB_SWITCH_MAX_LAYOUTS;
}
g_free( value );
return (guint)idx;
}
gboolean g3kb_set_layout( guint idx, GError **err )
{
#ifdef G3KBSWITCH_WITH_GNOME_SHELL_EXTENSION
static const gsize method_activate_len =
/* zero terminator */ /* max 3 digits for index */
1 + 3;
#else
#define METHOD_ACTIVATE_HEAD \
"\"imports.ui.status.keyboard.getInputSourceManager().inputSources["
#define METHOD_ACTIVATE_TAIL "].activate()\""
static const gsize method_activate_len =
/* head */ /* tail */ /* zero terminator */ /* max 3 digits for index */
sizeof( METHOD_ACTIVATE_HEAD ) - 1 + sizeof( METHOD_ACTIVATE_TAIL ) -
1 + 1 + 3;
#endif
const gchar *name = NULL;
const GVariantType *vtype;
gchar method[ method_activate_len ];
if ( idx >= G3KB_SWITCH_MAX_LAYOUTS ) {
g_set_error( err, G3KB_SWITCH_ERROR, G3KB_SWITCH_ERROR_SET_LAYOUT,
"Index %u is not valid", idx );
return FALSE;
}
#ifdef G3KBSWITCH_WITH_GNOME_SHELL_EXTENSION
name = "Set";
vtype = G_VARIANT_TYPE_UINT32;
g_snprintf( method, method_activate_len, "%u", idx );
#else
name = "Eval";
vtype = G_VARIANT_TYPE_STRING;
g_snprintf( method, method_activate_len,
METHOD_ACTIVATE_HEAD "%u" METHOD_ACTIVATE_TAIL, idx );
#endif
return run_method( name, method, vtype, NULL, err );
}
gconstpointer g3kb_search_layout( GTree *layouts, guint idx )
{
guintptr layout_idx = (guintptr)idx;
return g_tree_lookup( layouts, (gconstpointer)layout_idx );
}
guintptr g3kb_reverse_search_layout( GTree *layouts, const gchar *layout )
{
struct value_search_data vs;
vs.value = layout;
g_tree_foreach( layouts, value_search, &vs );
/* BEWARE: may return invalid value G3KB_SWITCH_MAX_LAYOUTS, but as far as
* the returned value is supposed to be later passed to g3kb_set_layout(),
* this should not be a problem */
return vs.idx;
}
gconstpointer g3kb_safe_get_layout( GTree *layouts, GError **err )
{
guint idx;
idx = g3kb_get_layout( err );
if ( idx >= G3KB_SWITCH_MAX_LAYOUTS ) {
return NULL;
}
return g3kb_search_layout( layouts, idx );
}
gboolean g3kb_safe_set_layout( GTree *layouts, const gchar *layout,
GError **err )
{
guint idx;
idx = (guint)g3kb_reverse_search_layout( layouts, layout );
if ( idx >= G3KB_SWITCH_MAX_LAYOUTS ) {
return FALSE;
}
return g3kb_set_layout( idx, err );
}
guintptr g3kb_get_next_layout( GTree *layouts, GError **err )
{
/* BEWARE: may return invalid value G3KB_SWIkCH_MAX_LAYOUTS, but as far as
* the returned value is supposed to be later passed to
* g3kb_set_next_layout(), this should not be a problem */
guintptr key = (guintptr)g3kb_get_layout( err );
if ( key >= G3KB_SWITCH_MAX_LAYOUTS ) {
return G3KB_SWITCH_MAX_LAYOUTS;
}
GTreeNode *node = g_tree_lookup_node( layouts, (gconstpointer)key );
/* if current layout is last layout, next layout will be not found
* just return 0 to use the first layout */
if ( node == NULL || ( node = g_tree_node_next( node ) ) == NULL ) {
return 0;
}
return (guintptr)g_tree_node_key( node );
}
gboolean g3kb_set_next_layout( GTree *layouts, GError **err )
{
guint idx;
idx = (guint)g3kb_get_next_layout( layouts, err );
if ( idx >= G3KB_SWITCH_MAX_LAYOUTS ) {
return FALSE;
}
return g3kb_set_layout( idx, err );
}