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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
|
/*
* Copyright (C) 2010 Intel, Inc
* Copyright (C) 2016 Endless, Inc
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
* Author: Thomas Wood <thomas.wood@intel.com>
* Georges Basile Stavracas Neto <georges.stavracas@gmail.com>
*
*/
#include <glib/gi18n.h>
#include "cc-keyboard-manager.h"
#include "keyboard-shortcuts.h"
#include "wm-common.h"
#ifdef GDK_WINDOWING_X11
#include <gdk/gdkx.h>
#endif
#define BINDINGS_SCHEMA "org.gnome.settings-daemon.plugins.media-keys"
#define CUSTOM_SHORTCUTS_ID "custom"
struct _CcKeyboardManager
{
GObject parent;
GtkListStore *sections_store;
GHashTable *kb_system_sections;
GHashTable *kb_apps_sections;
GHashTable *kb_user_sections;
GSettings *binding_settings;
gpointer wm_changed_id;
};
G_DEFINE_TYPE (CcKeyboardManager, cc_keyboard_manager, G_TYPE_OBJECT)
enum
{
SHORTCUT_ADDED,
SHORTCUT_CHANGED,
SHORTCUT_REMOVED,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0, };
/*
* Auxiliary methos
*/
static void
free_key_array (GPtrArray *keys)
{
if (keys != NULL)
{
gint i;
for (i = 0; i < keys->len; i++)
{
CcKeyboardItem *item;
item = g_ptr_array_index (keys, i);
g_object_unref (item);
}
g_ptr_array_free (keys, TRUE);
}
}
static gboolean
find_conflict (CcUniquenessData *data,
CcKeyboardItem *item)
{
GList *l;
gboolean is_conflict = FALSE;
if (data->orig_item && cc_keyboard_item_equal (data->orig_item, item))
return FALSE;
for (l = cc_keyboard_item_get_key_combos (item); l; l = l->next)
{
CcKeyCombo *combo = l->data;
if (data->new_mask != combo->mask)
continue;
if (data->new_keyval != 0)
is_conflict = data->new_keyval == combo->keyval;
else
is_conflict = combo->keyval == 0 && data->new_keycode == combo->keycode;
if (is_conflict)
break;
}
if (is_conflict)
data->conflict_item = item;
return is_conflict;
}
static gboolean
compare_keys_for_uniqueness (CcKeyboardItem *current_item,
CcUniquenessData *data)
{
CcKeyboardItem *reverse_item;
/* No conflict for: blanks or ourselves */
if (!current_item || data->orig_item == current_item)
return FALSE;
reverse_item = cc_keyboard_item_get_reverse_item (current_item);
/* When the current item is the reversed shortcut of a main item, simply ignore it */
if (reverse_item && cc_keyboard_item_is_hidden (current_item))
return FALSE;
if (find_conflict (data, current_item))
return TRUE;
/* Also check for the reverse item if any */
if (reverse_item && find_conflict (data, reverse_item))
return TRUE;
return FALSE;
}
static gboolean
check_for_uniqueness (gpointer key,
GPtrArray *keys_array,
CcUniquenessData *data)
{
guint i;
for (i = 0; i < keys_array->len; i++)
{
CcKeyboardItem *item;
item = keys_array->pdata[i];
if (compare_keys_for_uniqueness (item, data))
return TRUE;
}
return FALSE;
}
static GHashTable*
get_hash_for_group (CcKeyboardManager *self,
BindingGroupType group)
{
GHashTable *hash;
switch (group)
{
case BINDING_GROUP_SYSTEM:
hash = self->kb_system_sections;
break;
case BINDING_GROUP_APPS:
hash = self->kb_apps_sections;
break;
case BINDING_GROUP_USER:
hash = self->kb_user_sections;
break;
default:
hash = NULL;
}
return hash;
}
static gboolean
have_key_for_group (CcKeyboardManager *self,
int group,
const gchar *name)
{
GHashTableIter iter;
GPtrArray *keys;
gint i;
g_hash_table_iter_init (&iter, get_hash_for_group (self, group));
while (g_hash_table_iter_next (&iter, NULL, (gpointer*) &keys))
{
for (i = 0; i < keys->len; i++)
{
CcKeyboardItem *item = g_ptr_array_index (keys, i);
if (cc_keyboard_item_get_item_type (item) == CC_KEYBOARD_ITEM_TYPE_GSETTINGS &&
g_strcmp0 (name, cc_keyboard_item_get_key (item)) == 0)
{
return TRUE;
}
}
}
return FALSE;
}
static void
add_shortcuts (CcKeyboardManager *self)
{
GtkTreeModel *sections_model;
GtkTreeIter sections_iter;
gboolean can_continue;
sections_model = GTK_TREE_MODEL (self->sections_store);
can_continue = gtk_tree_model_get_iter_first (sections_model, §ions_iter);
while (can_continue)
{
BindingGroupType group;
GPtrArray *keys;
g_autofree gchar *id = NULL;
g_autofree gchar *title = NULL;
gint i;
gtk_tree_model_get (sections_model,
§ions_iter,
SECTION_DESCRIPTION_COLUMN, &title,
SECTION_GROUP_COLUMN, &group,
SECTION_ID_COLUMN, &id,
-1);
/* Ignore separators */
if (group == BINDING_GROUP_SEPARATOR)
{
can_continue = gtk_tree_model_iter_next (sections_model, §ions_iter);
continue;
}
keys = g_hash_table_lookup (get_hash_for_group (self, group), id);
for (i = 0; i < keys->len; i++)
{
CcKeyboardItem *item = g_ptr_array_index (keys, i);
if (!cc_keyboard_item_is_hidden (item))
{
g_signal_emit (self, signals[SHORTCUT_ADDED],
0,
item,
id,
title);
}
}
can_continue = gtk_tree_model_iter_next (sections_model, §ions_iter);
}
}
static void
append_section (CcKeyboardManager *self,
const gchar *title,
const gchar *id,
BindingGroupType group,
const KeyListEntry *keys_list)
{
GtkTreeIter iter;
GHashTable *reverse_items;
GHashTable *hash;
GPtrArray *keys_array;
gboolean is_new;
gint i;
hash = get_hash_for_group (self, group);
if (!hash)
return;
/* Add all CcKeyboardItems for this section */
is_new = FALSE;
keys_array = g_hash_table_lookup (hash, id);
if (keys_array == NULL)
{
keys_array = g_ptr_array_new ();
is_new = TRUE;
}
reverse_items = g_hash_table_new (g_str_hash, g_str_equal);
for (i = 0; keys_list != NULL && keys_list[i].name != NULL; i++)
{
CcKeyboardItem *item;
gboolean ret;
if (have_key_for_group (self, group, keys_list[i].name))
continue;
item = cc_keyboard_item_new (keys_list[i].type);
switch (keys_list[i].type)
{
case CC_KEYBOARD_ITEM_TYPE_GSETTINGS_PATH:
ret = cc_keyboard_item_load_from_gsettings_path (item, keys_list[i].name, FALSE);
break;
case CC_KEYBOARD_ITEM_TYPE_GSETTINGS:
ret = cc_keyboard_item_load_from_gsettings (item,
keys_list[i].description,
keys_list[i].schema,
keys_list[i].name);
if (ret && keys_list[i].reverse_entry != NULL)
{
CcKeyboardItem *reverse_item;
reverse_item = g_hash_table_lookup (reverse_items,
keys_list[i].reverse_entry);
if (reverse_item != NULL)
{
cc_keyboard_item_add_reverse_item (item,
reverse_item,
keys_list[i].is_reversed);
}
else
{
g_hash_table_insert (reverse_items,
keys_list[i].name,
item);
}
}
break;
default:
g_assert_not_reached ();
}
if (ret == FALSE)
{
/* We don't actually want to popup a dialog - just skip this one */
g_object_unref (item);
continue;
}
cc_keyboard_item_set_hidden (item, keys_list[i].hidden);
g_ptr_array_add (keys_array, item);
}
g_hash_table_destroy (reverse_items);
/* Add the keys to the hash table */
if (is_new)
{
g_hash_table_insert (hash, g_strdup (id), keys_array);
/* Append the section to the left tree view */
gtk_list_store_append (GTK_LIST_STORE (self->sections_store), &iter);
gtk_list_store_set (GTK_LIST_STORE (self->sections_store),
&iter,
SECTION_DESCRIPTION_COLUMN, title,
SECTION_ID_COLUMN, id,
SECTION_GROUP_COLUMN, group,
-1);
}
}
static void
append_sections_from_file (CcKeyboardManager *self,
const gchar *path,
const char *datadir,
gchar **wm_keybindings)
{
KeyList *keylist;
KeyListEntry *keys;
KeyListEntry key = { 0, 0, 0, 0, 0, 0, 0 };
const char *title;
int group;
guint i;
keylist = parse_keylist_from_file (path);
if (keylist == NULL)
return;
#define const_strv(s) ((const gchar* const*) s)
/* If there's no keys to add, or the settings apply to a window manager
* that's not the one we're running */
if (keylist->entries->len == 0 ||
(keylist->wm_name != NULL && !g_strv_contains (const_strv (wm_keybindings), keylist->wm_name)) ||
keylist->name == NULL)
{
g_free (keylist->name);
g_free (keylist->package);
g_free (keylist->wm_name);
g_array_free (keylist->entries, TRUE);
g_free (keylist);
return;
}
#undef const_strv
/* Empty KeyListEntry to end the array */
key.name = NULL;
g_array_append_val (keylist->entries, key);
keys = (KeyListEntry *) g_array_free (keylist->entries, FALSE);
if (keylist->package)
{
g_autofree gchar *localedir = NULL;
localedir = g_build_filename (datadir, "locale", NULL);
bindtextdomain (keylist->package, localedir);
title = dgettext (keylist->package, keylist->name);
} else {
title = _(keylist->name);
}
if (keylist->group && strcmp (keylist->group, "system") == 0)
group = BINDING_GROUP_SYSTEM;
else
group = BINDING_GROUP_APPS;
append_section (self, title, keylist->name, group, keys);
g_free (keylist->name);
g_free (keylist->package);
g_free (keylist->wm_name);
g_free (keylist->schema);
g_free (keylist->group);
for (i = 0; keys[i].name != NULL; i++)
{
KeyListEntry *entry = &keys[i];
g_free (entry->schema);
g_free (entry->description);
g_free (entry->name);
g_free (entry->reverse_entry);
}
g_free (keylist);
g_free (keys);
}
static void
append_sections_from_gsettings (CcKeyboardManager *self)
{
g_auto(GStrv) custom_paths = NULL;
GArray *entries;
KeyListEntry key = { 0, 0, 0, 0, 0, 0, 0 };
int i;
/* load custom shortcuts from GSettings */
entries = g_array_new (FALSE, TRUE, sizeof (KeyListEntry));
custom_paths = g_settings_get_strv (self->binding_settings, "custom-keybindings");
for (i = 0; custom_paths[i]; i++)
{
key.name = g_strdup (custom_paths[i]);
if (!have_key_for_group (self, BINDING_GROUP_USER, key.name))
{
key.type = CC_KEYBOARD_ITEM_TYPE_GSETTINGS_PATH;
g_array_append_val (entries, key);
}
else
g_free (key.name);
}
if (entries->len > 0)
{
KeyListEntry *keys;
int i;
/* Empty KeyListEntry to end the array */
key.name = NULL;
g_array_append_val (entries, key);
keys = (KeyListEntry *) entries->data;
append_section (self, _("Custom Shortcuts"), CUSTOM_SHORTCUTS_ID, BINDING_GROUP_USER, keys);
for (i = 0; i < entries->len; ++i)
{
g_free (keys[i].name);
}
}
else
{
append_section (self, _("Custom Shortcuts"), CUSTOM_SHORTCUTS_ID, BINDING_GROUP_USER, NULL);
}
g_array_free (entries, TRUE);
}
static void
reload_sections (CcKeyboardManager *self)
{
GHashTable *loaded_files;
GDir *dir;
gchar *default_wm_keybindings[] = { "Mutter", "GNOME Shell", NULL };
g_auto(GStrv) wm_keybindings = NULL;
const gchar * const * data_dirs;
guint i;
/* Clear previous models and hash tables */
gtk_list_store_clear (GTK_LIST_STORE (self->sections_store));
g_clear_pointer (&self->kb_system_sections, g_hash_table_destroy);
self->kb_system_sections = g_hash_table_new_full (g_str_hash,
g_str_equal,
g_free,
(GDestroyNotify) free_key_array);
g_clear_pointer (&self->kb_apps_sections, g_hash_table_destroy);
self->kb_apps_sections = g_hash_table_new_full (g_str_hash,
g_str_equal,
g_free,
(GDestroyNotify) free_key_array);
g_clear_pointer (&self->kb_user_sections, g_hash_table_destroy);
self->kb_user_sections = g_hash_table_new_full (g_str_hash,
g_str_equal,
g_free,
(GDestroyNotify) free_key_array);
/* Load WM keybindings */
#ifdef GDK_WINDOWING_X11
if (GDK_IS_X11_DISPLAY (gdk_display_get_default ()))
wm_keybindings = wm_common_get_current_keybindings ();
else
#endif
wm_keybindings = g_strdupv (default_wm_keybindings);
loaded_files = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
data_dirs = g_get_system_data_dirs ();
for (i = 0; data_dirs[i] != NULL; i++)
{
g_autofree gchar *dir_path = NULL;
const gchar *name;
dir_path = g_build_filename (data_dirs[i], "gnome-control-center", "keybindings", NULL);
dir = g_dir_open (dir_path, 0, NULL);
if (!dir)
continue;
for (name = g_dir_read_name (dir) ; name ; name = g_dir_read_name (dir))
{
g_autofree gchar *path = NULL;
if (g_str_has_suffix (name, ".xml") == FALSE)
continue;
if (g_hash_table_lookup (loaded_files, name) != NULL)
{
g_debug ("Not loading %s, it was already loaded from another directory", name);
continue;
}
g_hash_table_insert (loaded_files, g_strdup (name), GINT_TO_POINTER (1));
path = g_build_filename (dir_path, name, NULL);
append_sections_from_file (self, path, data_dirs[i], wm_keybindings);
}
g_dir_close (dir);
}
g_hash_table_destroy (loaded_files);
/* Load custom keybindings */
append_sections_from_gsettings (self);
}
/*
* Callbacks
*/
static void
on_window_manager_change (const char *wm_name,
CcKeyboardManager *self)
{
reload_sections (self);
}
static void
cc_keyboard_manager_finalize (GObject *object)
{
CcKeyboardManager *self = (CcKeyboardManager *)object;
g_clear_pointer (&self->kb_system_sections, g_hash_table_destroy);
g_clear_pointer (&self->kb_apps_sections, g_hash_table_destroy);
g_clear_pointer (&self->kb_user_sections, g_hash_table_destroy);
g_clear_object (&self->binding_settings);
g_clear_pointer (&self->wm_changed_id, wm_common_unregister_window_manager_change);
G_OBJECT_CLASS (cc_keyboard_manager_parent_class)->finalize (object);
}
static void
cc_keyboard_manager_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
static void
cc_keyboard_manager_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
static void
cc_keyboard_manager_class_init (CcKeyboardManagerClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = cc_keyboard_manager_finalize;
object_class->get_property = cc_keyboard_manager_get_property;
object_class->set_property = cc_keyboard_manager_set_property;
/**
* CcKeyboardManager:shortcut-added:
*
* Emitted when a shortcut is added.
*/
signals[SHORTCUT_ADDED] = g_signal_new ("shortcut-added",
CC_TYPE_KEYBOARD_MANAGER,
G_SIGNAL_RUN_FIRST,
0, NULL, NULL, NULL,
G_TYPE_NONE,
3,
CC_TYPE_KEYBOARD_ITEM,
G_TYPE_STRING,
G_TYPE_STRING);
/**
* CcKeyboardManager:shortcut-changed:
*
* Emitted when a shortcut is added.
*/
signals[SHORTCUT_CHANGED] = g_signal_new ("shortcut-changed",
CC_TYPE_KEYBOARD_MANAGER,
G_SIGNAL_RUN_FIRST,
0, NULL, NULL, NULL,
G_TYPE_NONE,
1,
CC_TYPE_KEYBOARD_ITEM);
/**
* CcKeyboardManager:shortcut-removed:
*
* Emitted when a shortcut is removed.
*/
signals[SHORTCUT_REMOVED] = g_signal_new ("shortcut-removed",
CC_TYPE_KEYBOARD_MANAGER,
G_SIGNAL_RUN_FIRST,
0, NULL, NULL, NULL,
G_TYPE_NONE,
1,
CC_TYPE_KEYBOARD_ITEM);
}
static void
cc_keyboard_manager_init (CcKeyboardManager *self)
{
/* Bindings */
self->binding_settings = g_settings_new (BINDINGS_SCHEMA);
/* Setup the section models */
self->sections_store = gtk_list_store_new (SECTION_N_COLUMNS,
G_TYPE_STRING,
G_TYPE_STRING,
G_TYPE_INT);
#ifdef GDK_WINDOWING_X11
if (GDK_IS_X11_DISPLAY (gdk_display_get_default ()))
self->wm_changed_id = wm_common_register_window_manager_change ((GFunc) on_window_manager_change,
self);
#endif
}
CcKeyboardManager *
cc_keyboard_manager_new (void)
{
return g_object_new (CC_TYPE_KEYBOARD_MANAGER, NULL);
}
void
cc_keyboard_manager_load_shortcuts (CcKeyboardManager *self)
{
g_return_if_fail (CC_IS_KEYBOARD_MANAGER (self));
reload_sections (self);
add_shortcuts (self);
}
/**
* cc_keyboard_manager_create_custom_shortcut:
* @self: a #CcKeyboardPanel
*
* Creates a new temporary keyboard shortcut.
*
* Returns: (transfer full): a #CcKeyboardItem
*/
CcKeyboardItem*
cc_keyboard_manager_create_custom_shortcut (CcKeyboardManager *self)
{
CcKeyboardItem *item;
g_autofree gchar *settings_path = NULL;
g_return_val_if_fail (CC_IS_KEYBOARD_MANAGER (self), NULL);
item = cc_keyboard_item_new (CC_KEYBOARD_ITEM_TYPE_GSETTINGS_PATH);
settings_path = find_free_settings_path (self->binding_settings);
cc_keyboard_item_load_from_gsettings_path (item, settings_path, TRUE);
return item;
}
/**
* cc_keyboard_manager_add_custom_shortcut:
* @self: a #CcKeyboardPanel
* @item: the #CcKeyboardItem to be added
*
* Effectively adds the custom shortcut.
*/
void
cc_keyboard_manager_add_custom_shortcut (CcKeyboardManager *self,
CcKeyboardItem *item)
{
GPtrArray *keys_array;
GHashTable *hash;
GVariantBuilder builder;
char **settings_paths;
int i;
g_return_if_fail (CC_IS_KEYBOARD_MANAGER (self));
hash = get_hash_for_group (self, BINDING_GROUP_USER);
keys_array = g_hash_table_lookup (hash, CUSTOM_SHORTCUTS_ID);
if (keys_array == NULL)
{
keys_array = g_ptr_array_new ();
g_hash_table_insert (hash, g_strdup (CUSTOM_SHORTCUTS_ID), keys_array);
}
g_ptr_array_add (keys_array, item);
settings_paths = g_settings_get_strv (self->binding_settings, "custom-keybindings");
g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
for (i = 0; settings_paths[i]; i++)
g_variant_builder_add (&builder, "s", settings_paths[i]);
g_variant_builder_add (&builder, "s", cc_keyboard_item_get_gsettings_path (item));
g_settings_set_value (self->binding_settings, "custom-keybindings", g_variant_builder_end (&builder));
g_signal_emit (self, signals[SHORTCUT_ADDED],
0,
item,
CUSTOM_SHORTCUTS_ID,
_("Custom Shortcuts"));
}
/**
* cc_keyboard_manager_remove_custom_shortcut:
* @self: a #CcKeyboardPanel
* @item: the #CcKeyboardItem to be added
*
* Removed the custom shortcut.
*/
void
cc_keyboard_manager_remove_custom_shortcut (CcKeyboardManager *self,
CcKeyboardItem *item)
{
GPtrArray *keys_array;
GVariantBuilder builder;
GSettings *settings;
char **settings_paths;
int i;
g_return_if_fail (CC_IS_KEYBOARD_MANAGER (self));
/* Shortcut not a custom shortcut */
g_assert (cc_keyboard_item_get_item_type (item) == CC_KEYBOARD_ITEM_TYPE_GSETTINGS_PATH);
settings = cc_keyboard_item_get_settings (item);
g_settings_delay (settings);
g_settings_reset (settings, "name");
g_settings_reset (settings, "command");
g_settings_reset (settings, "binding");
g_settings_apply (settings);
g_settings_sync ();
settings_paths = g_settings_get_strv (self->binding_settings, "custom-keybindings");
g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
for (i = 0; settings_paths[i]; i++)
if (strcmp (settings_paths[i], cc_keyboard_item_get_gsettings_path (item)) != 0)
g_variant_builder_add (&builder, "s", settings_paths[i]);
g_settings_set_value (self->binding_settings,
"custom-keybindings",
g_variant_builder_end (&builder));
g_strfreev (settings_paths);
keys_array = g_hash_table_lookup (get_hash_for_group (self, BINDING_GROUP_USER), CUSTOM_SHORTCUTS_ID);
g_ptr_array_remove (keys_array, item);
g_signal_emit (self, signals[SHORTCUT_REMOVED], 0, item);
}
/**
* cc_keyboard_manager_get_collision:
* @self: a #CcKeyboardManager
* @item: (nullable): a keyboard shortcut
* @combo: a #CcKeyCombo
*
* Retrieves the collision item for the given shortcut.
*
* Returns: (transfer none)(nullable): the collisioned shortcut
*/
CcKeyboardItem*
cc_keyboard_manager_get_collision (CcKeyboardManager *self,
CcKeyboardItem *item,
CcKeyCombo *combo)
{
CcUniquenessData data;
BindingGroupType i;
g_return_val_if_fail (CC_IS_KEYBOARD_MANAGER (self), NULL);
data.orig_item = item;
data.new_keyval = combo->keyval;
data.new_mask = combo->mask;
data.new_keycode = combo->keycode;
data.conflict_item = NULL;
if (combo->keyval == 0 && combo->keycode == 0)
return NULL;
/* Any number of shortcuts can be disabled */
for (i = BINDING_GROUP_SYSTEM; i <= BINDING_GROUP_USER && !data.conflict_item; i++)
{
GHashTable *table;
table = get_hash_for_group (self, i);
if (!table)
continue;
g_hash_table_find (table, (GHRFunc) check_for_uniqueness, &data);
}
return data.conflict_item;
}
/**
* cc_keyboard_manager_disable_shortcut:
* @self: a #CcKeyboardManager
* @item: a @CcKeyboardItem
*
* Disables the given keyboard shortcut.
*/
void
cc_keyboard_manager_disable_shortcut (CcKeyboardManager *self,
CcKeyboardItem *item)
{
g_return_if_fail (CC_IS_KEYBOARD_MANAGER (self));
g_object_set (item, "binding", NULL, NULL);
}
/**
* cc_keyboard_manager_reset_shortcut:
* @self: a #CcKeyboardManager
* @item: a #CcKeyboardItem
*
* Resets the keyboard shortcut managed by @item, and eventually
* disables any shortcut that conflicts with the new shortcut's
* value.
*/
void
cc_keyboard_manager_reset_shortcut (CcKeyboardManager *self,
CcKeyboardItem *item)
{
GList *l;
g_return_if_fail (CC_IS_KEYBOARD_MANAGER (self));
g_return_if_fail (CC_IS_KEYBOARD_ITEM (item));
/* Disables any shortcut that conflicts with the new shortcut's value */
for (l = cc_keyboard_item_get_default_combos (item); l; l = l->next)
{
CcKeyCombo *combo = l->data;
CcKeyboardItem *collision;
collision = cc_keyboard_manager_get_collision (self, NULL, combo);
if (collision)
cc_keyboard_manager_disable_shortcut (self, collision);
}
/* Resets the current item */
cc_keyboard_item_reset (item);
}
|