0b7ac714c0572563ca79cb7f9558c515561b6476
[silc.git] / apps / irssi / src / fe-common / core / keyboard.c
1 /*
2  keyboard.c : irssi
3
4     Copyright (C) 1999-2001 Timo Sirainen
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "module.h"
22 #include "module-formats.h"
23 #include "signals.h"
24 #include "commands.h"
25 #include "levels.h"
26 #include "misc.h"
27 #include "lib-config/iconfig.h"
28 #include "settings.h"
29
30 #include "keyboard.h"
31 #include "fe-windows.h"
32 #include "printtext.h"
33
34 GSList *keyinfos;
35 static GHashTable *keys, *default_keys;
36
37 /* A cache of some sort for key presses that generate a single char only.
38    If the key isn't used, used_keys[key] is zero. */
39 static char used_keys[256];
40
41 /* contains list of all key bindings of which command is "key" -
42    this can be used to check fast if some command queue exists or not.
43    Format is _always_ in key1-key2-key3 format (like ^W-^N,
44    not ^W^N) */
45 static GTree *key_states;
46 /* List of all key combo names */
47 static GSList *key_combos;
48 static int key_config_frozen;
49
50 struct KEYBOARD_REC {
51         /* example:
52            /BIND ^[ key meta
53            /BIND meta-O key meta2
54            /BIND meta-[ key meta2
55
56            /BIND meta2-C key right
57            /BIND ^W-meta-right /echo ^W Meta-right key pressed
58
59            When ^W Meta-Right is pressed, the full char combination
60            is "^W^[^[[C".
61
62            We'll get there with key states:
63              ^W - key_prev_state = NULL, key_state = NULL -> ^W
64              ^[ - key_prev_state = NULL, key_state = ^W -> meta
65              ^[ - key_prev_state = ^W, key_state = meta -> meta
66              [ - key_prev_state = ^W-meta, key_state = meta -> meta2
67              C - key_prev_state = ^W-meta, key_state = meta2 -> right
68              key_prev_state = ^W-meta, key_state = right -> ^W-meta-right
69
70            key_state is moved to key_prev_state if there's nothing else in
71            /BINDs matching for key_state-newkey.
72
73            ^X^Y equals to ^X-^Y, ABC equals to A-B-C unless there's ABC
74            named key. ^ can be used with ^^ and - with -- */
75         char *key_state, *key_prev_state;
76
77         /* GUI specific data sent in "key pressed" signal */
78         void *gui_data;
79 };
80
81 /* Creates a new "keyboard" - this is used only for keeping track of
82    key combo states and sending the gui_data parameter in "key pressed"
83    signal */
84 KEYBOARD_REC *keyboard_create(void *data)
85 {
86         KEYBOARD_REC *rec;
87
88         rec = g_new0(KEYBOARD_REC, 1);
89         rec->gui_data = data;
90
91         signal_emit("keyboard created", 1, rec);
92         return rec;
93 }
94
95 /* Destroys a keyboard */
96 void keyboard_destroy(KEYBOARD_REC *keyboard)
97 {
98         signal_emit("keyboard destroyed", 1, keyboard);
99
100         g_free_not_null(keyboard->key_state);
101         g_free_not_null(keyboard->key_prev_state);
102         g_free(keyboard);
103 }
104
105 static void key_destroy(KEY_REC *rec, GHashTable *hash)
106 {
107         g_hash_table_remove(hash, rec->key);
108
109         g_free_not_null(rec->data);
110         g_free(rec->key);
111         g_free(rec);
112 }
113
114 static void key_default_add(const char *id, const char *key, const char *data)
115 {
116         KEYINFO_REC *info;
117         KEY_REC *rec;
118
119         info = key_info_find(id);
120         if (info == NULL)
121                 return;
122
123         rec = g_hash_table_lookup(default_keys, key);
124         if (rec != NULL) {
125                 /* key already exists, replace */
126                 rec->info->default_keys =
127                         g_slist_remove(rec->info->default_keys, rec);
128                 key_destroy(rec, default_keys);
129         }
130
131         rec = g_new0(KEY_REC, 1);
132         rec->key = g_strdup(key);
133         rec->info = info;
134         rec->data = g_strdup(data);
135         info->default_keys = g_slist_append(info->default_keys, rec);
136         g_hash_table_insert(default_keys, rec->key, rec);
137 }
138
139 static CONFIG_NODE *key_config_find(const char *key)
140 {
141         CONFIG_NODE *node;
142         GSList *tmp;
143
144         /* remove old keyboard settings */
145         node = iconfig_node_traverse("(keyboard", TRUE);
146
147         for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
148                 node = tmp->data;
149
150                 if (strcmp(config_node_get_str(node, "key", ""), key) == 0)
151                         return node;
152         }
153
154         return NULL;
155 }
156
157 static void keyconfig_save(const char *id, const char *key, const char *data)
158 {
159         CONFIG_NODE *node;
160
161         g_return_if_fail(id != NULL);
162         g_return_if_fail(key != NULL);
163
164         node = key_config_find(key);
165         if (node == NULL) {
166                 node = iconfig_node_traverse("(keyboard", TRUE);
167                 node = config_node_section(node, NULL, NODE_TYPE_BLOCK);
168         }
169
170         iconfig_node_set_str(node, "key", key);
171         iconfig_node_set_str(node, "id", id);
172         iconfig_node_set_str(node, "data", data);
173 }
174
175 static void keyconfig_clear(const char *key)
176 {
177         CONFIG_NODE *node;
178
179         g_return_if_fail(key != NULL);
180
181         /* remove old keyboard settings */
182         node = key_config_find(key);
183         if (node != NULL)
184                 iconfig_node_clear(node);
185 }
186
187 KEYINFO_REC *key_info_find(const char *id)
188 {
189         GSList *tmp;
190
191         for (tmp = keyinfos; tmp != NULL; tmp = tmp->next) {
192                 KEYINFO_REC *rec = tmp->data;
193
194                 if (g_strcasecmp(rec->id, id) == 0)
195                         return rec;
196         }
197
198         return NULL;
199 }
200
201 static KEY_REC *key_combo_find(const char *key)
202 {
203         KEYINFO_REC *info;
204         GSList *tmp;
205
206         info = key_info_find("key");
207         if (info == NULL)
208                 return NULL;
209
210         for (tmp = info->keys; tmp != NULL; tmp = tmp->next) {
211                 KEY_REC *rec = tmp->data;
212
213                 if (strcmp(rec->data, key) == 0)
214                         return rec;
215         }
216
217         return NULL;
218 }
219
220 static void key_states_scan_key(const char *key, KEY_REC *rec, GString *temp)
221 {
222         char **keys, **tmp, *p;
223
224         g_string_truncate(temp, 0);
225
226         /* meta-^W^Gfoo -> meta-^W-^G-f-o-o */
227         keys = g_strsplit(key, "-", -1);
228         for (tmp = keys; *tmp != NULL; tmp++) {
229                 if (key_combo_find(*tmp)) {
230                         /* key combo */
231                         g_string_append(temp, *tmp);
232                         g_string_append_c(temp, '-');
233                         continue;
234                 }
235
236                 if (**tmp == '\0') {
237                         /* '-' */
238                         g_string_append(temp, "--");
239                         continue;
240                 }
241
242                 for (p = *tmp; *p != '\0'; p++) {
243                         g_string_append_c(temp, *p);
244
245                         if (*p == '^') {
246                                 /* ctrl-code */
247                                 if (p[1] != '\0')
248                                         p++;
249                                 g_string_append_c(temp, *p);
250                         }
251
252                         g_string_append_c(temp, '-');
253                 }
254         }
255         g_strfreev(keys);
256
257         if (temp->len > 0) {
258                 g_string_truncate(temp, temp->len-1);
259
260                 if (temp->str[1] == '-' || temp->str[1] == '\0')
261                         used_keys[(int) (unsigned char) temp->str[0]] = 1;
262                 g_tree_insert(key_states, g_strdup(temp->str), rec);
263         }
264 }
265
266 static int key_state_destroy(char *key)
267 {
268         g_free(key);
269         return FALSE;
270 }
271
272 /* Rescan all the key combos and figure out which characters are supposed
273    to be treated as characters and which as key combos.
274    Yes, this is pretty slow function... */
275 static void key_states_rescan(void)
276 {
277         GString *temp;
278
279         memset(used_keys, 0, sizeof(used_keys));
280
281         g_tree_traverse(key_states, (GTraverseFunc) key_state_destroy,
282                         G_IN_ORDER, NULL);
283         g_tree_destroy(key_states);
284         key_states = g_tree_new((GCompareFunc) strcmp);
285
286         temp = g_string_new(NULL);
287         g_hash_table_foreach(keys, (GHFunc) key_states_scan_key, temp);
288         g_string_free(temp, TRUE);
289 }
290
291 void key_configure_freeze(void)
292 {
293         key_config_frozen++;
294 }
295
296 void key_configure_thaw(void)
297 {
298         g_return_if_fail(key_config_frozen > 0);
299
300         if (--key_config_frozen == 0)
301                 key_states_rescan();
302 }
303
304 static void key_configure_destroy(KEY_REC *rec)
305 {
306         g_return_if_fail(rec != NULL);
307
308         rec->info->keys = g_slist_remove(rec->info->keys, rec);
309         g_hash_table_remove(keys, rec->key);
310
311         if (!key_config_frozen)
312                 key_states_rescan();
313
314         g_free_not_null(rec->data);
315         g_free(rec->key);
316         g_free(rec);
317 }
318
319 /* Configure new key */
320 static void key_configure_create(const char *id, const char *key,
321                                  const char *data)
322 {
323         KEYINFO_REC *info;
324         KEY_REC *rec;
325
326         g_return_if_fail(id != NULL);
327         g_return_if_fail(key != NULL && *key != '\0');
328
329         info = key_info_find(id);
330         if (info == NULL)
331                 return;
332
333         rec = g_hash_table_lookup(keys, key);
334         if (rec != NULL)
335                 key_configure_destroy(rec);
336
337         rec = g_new0(KEY_REC, 1);
338         rec->key = g_strdup(key);
339         rec->info = info;
340         rec->data = g_strdup(data);
341         info->keys = g_slist_append(info->keys, rec);
342         g_hash_table_insert(keys, rec->key, rec);
343
344         if (!key_config_frozen)
345                 key_states_rescan();
346 }
347
348 /* Bind a key for function */
349 void key_bind(const char *id, const char *description,
350               const char *key_default, const char *data, SIGNAL_FUNC func)
351 {
352         KEYINFO_REC *info;
353         char *key;
354
355         g_return_if_fail(id != NULL);
356
357         /* create key info record */
358         info = key_info_find(id);
359         if (info == NULL) {
360                 g_return_if_fail(func != NULL);
361
362                 if (description == NULL)
363                         g_warning("key_bind(%s) should have description!", id);
364                 info = g_new0(KEYINFO_REC, 1);
365                 info->id = g_strdup(id);
366                 info->description = g_strdup(description);
367                 keyinfos = g_slist_append(keyinfos, info);
368
369                 /* add the signal */
370                 key = g_strconcat("key ", id, NULL);
371                 signal_add(key, func);
372                 g_free(key);
373
374                 signal_emit("keyinfo created", 1, info);
375         }
376
377         if (key_default != NULL && *key_default != '\0') {
378                 key_default_add(id, key_default, data);
379                 key_configure_create(id, key_default, data);
380         }
381 }
382
383 static void keyinfo_remove(KEYINFO_REC *info)
384 {
385         g_return_if_fail(info != NULL);
386
387         keyinfos = g_slist_remove(keyinfos, info);
388         signal_emit("keyinfo destroyed", 1, info);
389
390         /* destroy all keys */
391         g_slist_foreach(info->keys, (GFunc) key_destroy, keys);
392         g_slist_foreach(info->default_keys, (GFunc) key_destroy, default_keys);
393
394         /* destroy key info */
395         g_slist_free(info->keys);
396         g_slist_free(info->default_keys);
397         g_free_not_null(info->description);
398         g_free(info->id);
399         g_free(info);
400 }
401
402 /* Unbind key */
403 void key_unbind(const char *id, SIGNAL_FUNC func)
404 {
405         KEYINFO_REC *info;
406         char *key;
407
408         g_return_if_fail(id != NULL);
409         g_return_if_fail(func != NULL);
410
411         /* remove keys */
412         info = key_info_find(id);
413         if (info != NULL)
414                 keyinfo_remove(info);
415
416         /* remove signal */
417         key = g_strconcat("key ", id, NULL);
418         signal_remove(key, func);
419         g_free(key);
420 }
421
422 /* Configure new key */
423 void key_configure_add(const char *id, const char *key, const char *data)
424 {
425         g_return_if_fail(id != NULL);
426         g_return_if_fail(key != NULL && *key != '\0');
427
428         key_configure_create(id, key, data);
429         keyconfig_save(id, key, data);
430 }
431
432 /* Remove key */
433 void key_configure_remove(const char *key)
434 {
435         KEY_REC *rec;
436
437         g_return_if_fail(key != NULL);
438
439         rec = g_hash_table_lookup(keys, key);
440         if (rec == NULL) return;
441
442         keyconfig_clear(key);
443         key_configure_destroy(rec);
444 }
445
446 static int key_emit_signal(KEYBOARD_REC *keyboard, KEY_REC *key)
447 {
448         int consumed;
449         char *str;
450
451         str = g_strconcat("key ", key->info->id, NULL);
452         consumed = signal_emit(str, 3, key->data, keyboard->gui_data, key->info);
453         g_free(str);
454
455         return consumed;
456 }
457
458 int key_states_search(const char *combo, const char *search)
459 {
460         while (*search != '\0') {
461                 if (*combo != *search)
462                         return *search - *combo;
463                 search++; combo++;
464         }
465
466         return *combo == '\0' || *combo == '-' ? 0 : -1;
467 }
468
469 /* Returns TRUE if key press was consumed. Control characters should be sent
470    as "^@" .. "^_" instead of #0..#31 chars, #127 should be sent as ^? */
471 int key_pressed(KEYBOARD_REC *keyboard, const char *key)
472 {
473         KEY_REC *rec;
474         char *str;
475         int consumed;
476
477         g_return_val_if_fail(keyboard != NULL, FALSE);
478         g_return_val_if_fail(key != NULL && *key != '\0', FALSE);
479
480         if (keyboard->key_state == NULL) {
481                 if (key[1] == '\0' &&
482                     !used_keys[(int) (unsigned char) key[0]]) {
483                         /* fast check - key not used */
484                         return FALSE;
485                 }
486
487                 rec = g_tree_search(key_states,
488                                     (GSearchFunc) key_states_search,
489                                     (void *) key);
490                 if (rec == NULL ||
491                     (g_tree_lookup(key_states, (void *) key) != NULL &&
492                      strcmp(rec->info->id, "key") != 0)) {
493                         /* a single non-combo key was pressed */
494                         rec = g_hash_table_lookup(keys, key);
495                         if (rec == NULL)
496                                 return FALSE;
497                         consumed = key_emit_signal(keyboard, rec);
498
499                         /* never consume non-control characters */
500                         return consumed && key[1] != '\0';
501                 }
502         }
503
504         if (keyboard->key_state == NULL) {
505                 /* first key in combo */
506                 rec = g_tree_lookup(key_states, (void *) key);
507         } else {
508                 /* continuing key combination */
509                 str = g_strconcat(keyboard->key_state, "-", key, NULL);
510                 rec = g_tree_lookup(key_states, str);
511                 g_free(str);
512         }
513
514         if (rec != NULL && strcmp(rec->info->id, "key") == 0) {
515                 /* combo has a specified name, use it */
516                 g_free_not_null(keyboard->key_state);
517                 keyboard->key_state = g_strdup(rec->data);
518         } else {
519                 /* some unnamed key - move key_state after key_prev_state
520                    and replace key_state with this new key */
521                 if (keyboard->key_prev_state == NULL)
522                         keyboard->key_prev_state = keyboard->key_state;
523                 else {
524                         str = g_strconcat(keyboard->key_prev_state, "-",
525                                           keyboard->key_state, NULL);
526                         g_free(keyboard->key_prev_state);
527                         g_free(keyboard->key_state);
528                         keyboard->key_prev_state = str;
529                 }
530
531                 keyboard->key_state = g_strdup(key);
532         }
533
534         /* what to do with the key combo? */
535         str = keyboard->key_prev_state == NULL ?
536                 g_strdup(keyboard->key_state) :
537                 g_strconcat(keyboard->key_prev_state, "-",
538                             keyboard->key_state, NULL);
539
540         rec = g_tree_lookup(key_states, str);
541         if (rec != NULL) {
542                 if (strcmp(rec->info->id, "key") == 0)
543                         rec = g_tree_lookup(key_states, rec->data);
544
545                 if (rec != NULL) {
546                         /* full key combo */
547                         key_emit_signal(keyboard, rec);
548                         rec = NULL;
549                 }
550         } else {
551                 /* check that combo is possible */
552                 rec = g_tree_search(key_states,
553                                     (GSearchFunc) key_states_search, str);
554         }
555
556         if (rec == NULL) {
557                 /* a) key combo finished, b) unknown key combo, abort */
558                 g_free_and_null(keyboard->key_prev_state);
559                 g_free_and_null(keyboard->key_state);
560         }
561
562         g_free(str);
563         return TRUE;
564 }
565
566 void keyboard_entry_redirect(SIGNAL_FUNC func, const char *entry,
567                              int flags, void *data)
568 {
569         signal_emit("gui entry redirect", 4, func, entry,
570                     GINT_TO_POINTER(flags), data);
571 }
572
573 static void sig_command(const char *data)
574 {
575         const char *cmdchars;
576         char *str;
577
578         cmdchars = settings_get_str("cmdchars");
579         str = strchr(cmdchars, *data) != NULL ? g_strdup(data) :
580                 g_strdup_printf("%c%s", *cmdchars, data);
581
582         signal_emit("send command", 3, str, active_win->active_server, active_win->active);
583
584         g_free(str);
585 }
586
587 static void sig_key(const char *data)
588 {
589         /* we should never get here */
590 }
591
592 static void sig_multi(const char *data, void *gui_data)
593 {
594         KEYINFO_REC *info;
595         char **list, **tmp, *p, *str;
596
597         list = g_strsplit(data, ";", -1);
598         for (tmp = list; *tmp != NULL; tmp++) {
599                 p = strchr(*tmp, ' ');
600                 if (p != NULL) *p++ = '\0'; else p = "";
601
602                 info = key_info_find(*tmp);
603                 if (info != NULL) {
604                         str = g_strconcat("key ", info->id, NULL);
605                         signal_emit(str, 3, p, gui_data, info);
606                         g_free(str);
607                 }
608         }
609         g_strfreev(list);
610 }
611
612 static void cmd_show_keys(const char *searchkey, int full)
613 {
614         GSList *info, *key;
615         int len;
616
617         len = searchkey == NULL ? 0 : strlen(searchkey);
618         for (info = keyinfos; info != NULL; info = info->next) {
619                 KEYINFO_REC *rec = info->data;
620
621                 for (key = rec->keys; key != NULL; key = key->next) {
622                         KEY_REC *rec = key->data;
623
624                         if ((len == 0 || g_strncasecmp(rec->key, searchkey, len) == 0) &&
625                             (!full || rec->key[len] == '\0')) {
626                                 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_BIND_KEY,
627                                             rec->key, rec->info->id, rec->data == NULL ? "" : rec->data);
628                         }
629                 }
630         }
631 }
632
633 /* SYNTAX: BIND [-delete] [<key> [<command> [<data>]]] */
634 static void cmd_bind(const char *data)
635 {
636         GHashTable *optlist;
637         char *key, *id, *keydata;
638         void *free_arg;
639         int command_id;
640
641         if (!cmd_get_params(data, &free_arg, 3 | PARAM_FLAG_GETREST | PARAM_FLAG_OPTIONS,
642                             "bind", &optlist, &key, &id, &keydata))
643                 return;
644
645         if (*key != '\0' && g_hash_table_lookup(optlist, "delete")) {
646                 /* delete key */
647                 key_configure_remove(key);
648                 cmd_params_free(free_arg);
649                 return;
650         }
651
652         if (*id == '\0') {
653                 /* show some/all keys */
654                 cmd_show_keys(key, FALSE);
655                 cmd_params_free(free_arg);
656                 return;
657         }
658
659         command_id = strchr(settings_get_str("cmdchars"), *id) != NULL;
660         if (command_id) {
661                 /* using shortcut to command id */
662                 keydata = g_strconcat(id, " ", keydata, NULL);
663                 id = "command";
664         }
665
666         if (key_info_find(id) == NULL)
667                 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, TXT_BIND_UNKNOWN_ID, id);
668         else {
669                 key_configure_add(id, key, keydata);
670                 cmd_show_keys(key, TRUE);
671         }
672
673         if (command_id) g_free(keydata);
674         cmd_params_free(free_arg);
675 }
676
677 static GList *completion_get_keyinfos(const char *info)
678 {
679         GList *list;
680         GSList *tmp;
681         int len;
682
683         list = NULL; len = strlen(info);
684         for (tmp = keyinfos; tmp != NULL; tmp = tmp->next) {
685                 KEYINFO_REC *rec = tmp->data;
686
687                 if (g_strncasecmp(rec->id, info, len) == 0)
688                         list = g_list_append(list, g_strdup(rec->id));
689         }
690
691         return list;
692 }
693
694 static void sig_complete_bind(GList **list, WINDOW_REC *window,
695                               const char *word, const char *line,
696                               int *want_space)
697 {
698         g_return_if_fail(list != NULL);
699         g_return_if_fail(word != NULL);
700         g_return_if_fail(line != NULL);
701
702         if (*line == '\0' || strchr(line, ' ') != NULL)
703                 return;
704
705         *list = completion_get_keyinfos(word);
706         if (*list != NULL) signal_stop();
707 }
708
709 static int key_destroy_hash(const char *key, KEY_REC *rec)
710 {
711         rec->info->keys = g_slist_remove(rec->info->keys, rec);
712
713         g_free_not_null(rec->data);
714         g_free(rec->key);
715         g_free(rec);
716         return TRUE;
717 }
718
719 static void key_copy_default(const char *key, KEY_REC *orig)
720 {
721         KEY_REC *rec;
722
723         rec = g_new0(KEY_REC, 1);
724         rec->key = g_strdup(orig->key);
725         rec->info = orig->info;
726         rec->data = g_strdup(orig->data);
727
728         rec->info->keys = g_slist_append(rec->info->keys, rec);
729         g_hash_table_insert(keys, rec->key, rec);
730 }
731
732 static void keyboard_reset_defaults(void)
733 {
734         g_hash_table_foreach_remove(keys, (GHRFunc) key_destroy_hash, NULL);
735         g_hash_table_foreach(default_keys, (GHFunc) key_copy_default, NULL);
736 }
737
738 static void key_config_read(CONFIG_NODE *node)
739 {
740         char *key, *id, *data;
741
742         g_return_if_fail(node != NULL);
743
744         key = config_node_get_str(node, "key", NULL);
745         id = config_node_get_str(node, "id", NULL);
746         data = config_node_get_str(node, "data", NULL);
747
748         if (key != NULL && id != NULL)
749                 key_configure_create(id, key, data);
750 }
751
752 static void read_keyboard_config(void)
753 {
754         CONFIG_NODE *node;
755         GSList *tmp;
756
757         key_configure_freeze();
758
759         keyboard_reset_defaults();
760
761         node = iconfig_node_traverse("keyboard", FALSE);
762         if (node == NULL) {
763                 key_configure_thaw();
764                 return;
765         }
766
767         /* FIXME: backward "compatibility" - remove after irssi .99 */
768         if (node->type != NODE_TYPE_LIST) {
769                 iconfig_node_remove(NULL, node);
770                 key_configure_thaw();
771                 return;
772         }
773
774         for (tmp = node->value; tmp != NULL; tmp = tmp->next)
775                 key_config_read(tmp->data);
776
777         key_configure_thaw();
778 }
779
780 void keyboard_init(void)
781 {
782         keys = g_hash_table_new((GHashFunc) g_str_hash,
783                                 (GCompareFunc) g_str_equal);
784         default_keys = g_hash_table_new((GHashFunc) g_str_hash,
785                                         (GCompareFunc) g_str_equal);
786         keyinfos = NULL;
787         key_states = g_tree_new((GCompareFunc) strcmp);
788         key_combos = NULL;
789         key_config_frozen = 0;
790         memset(used_keys, 0, sizeof(used_keys));
791
792         key_bind("command", "Run any IRC command", NULL, NULL, (SIGNAL_FUNC) sig_command);
793         key_bind("key", "Specify name for key binding", NULL, NULL, (SIGNAL_FUNC) sig_key);
794         key_bind("multi", "Run multiple commands", NULL, NULL, (SIGNAL_FUNC) sig_multi);
795
796         /* read the keyboard config when all key binds are known */
797         signal_add("irssi init read settings", (SIGNAL_FUNC) read_keyboard_config);
798         signal_add("setup reread", (SIGNAL_FUNC) read_keyboard_config);
799         signal_add("complete command bind", (SIGNAL_FUNC) sig_complete_bind);
800
801         command_bind("bind", NULL, (SIGNAL_FUNC) cmd_bind);
802         command_set_options("bind", "delete");
803 }
804
805 void keyboard_deinit(void)
806 {
807         while (keyinfos != NULL)
808                 keyinfo_remove(keyinfos->data);
809         g_hash_table_destroy(keys);
810         g_hash_table_destroy(default_keys);
811
812         g_tree_traverse(key_states, (GTraverseFunc) key_state_destroy,
813                         G_IN_ORDER, NULL);
814         g_tree_destroy(key_states);
815
816         signal_remove("irssi init read settings", (SIGNAL_FUNC) read_keyboard_config);
817         signal_remove("setup reread", (SIGNAL_FUNC) read_keyboard_config);
818         signal_remove("complete command bind", (SIGNAL_FUNC) sig_complete_bind);
819         command_unbind("bind", (SIGNAL_FUNC) cmd_bind);
820 }