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