Merges from Irssi CVS.
[crypto.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, *p;
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
218         if (list == NULL) {
219                 /* unknown keycombo - add it as-is, maybe the GUI will
220                    feed it to us as such */
221                 for (p = str; *p != '\0'; p++)
222                         expand_out_char(*out, *p);
223                 g_free(str);
224                 return TRUE;
225         }
226         g_free(str);
227
228         if (list->next == NULL) {
229                 /* only one way to generate the combo, good */
230                 rec = list->data;
231                 return expand_key(rec->key, out);
232         }
233
234         /* multiple ways to generate the combo -
235            we'll need to include all of them in output */
236         newout = NULL;
237         for (tmp = list->next; tmp != NULL; tmp = tmp->next) {
238                 KEY_REC *rec = tmp->data;
239
240                 copy = NULL;
241                 for (tmp2 = *out; tmp2 != NULL; tmp2 = tmp2->next) {
242                         GString *str = tmp2->data;
243                         copy = g_slist_append(copy, g_string_new(str->str));
244                 }
245
246                 if (!expand_key(rec->key, &copy)) {
247                         /* illegal key combo, remove from list */
248                         expand_out_free(copy);
249                 } else {
250                         newout = g_slist_concat(newout, copy);
251                 }
252         }
253
254         rec = list->data;
255         if (!expand_key(rec->key, out)) {
256                 /* illegal key combo, remove from list */
257                 expand_out_free(*out);
258         }
259
260         *out = g_slist_concat(*out, newout);
261         return *out != NULL;
262 }
263
264 /* Expand key code - returns TRUE if successful. */
265 static int expand_key(const char *key, GSList **out)
266 {
267         GSList *tmp;
268         const char *start;
269         int last_hyphen;
270
271         /* meta-^W^Gf -> ^[-^W-^G-f */
272         start = NULL; last_hyphen = TRUE;
273         for (; *key != '\0'; key++) {
274                 if (start != NULL) {
275                         if (i_isalnum(*key) || *key == '_') {
276                                 /* key combo continues */
277                                 continue;
278                         }
279
280                         if (!expand_combo(start, key-1, out))
281                                 return FALSE;
282                         expand_out_char(*out, '-');
283                         start = NULL;
284                 }
285
286                 if (*key == '-') {
287                         if (last_hyphen) {
288                                 expand_out_char(*out, '-');
289                                 expand_out_char(*out, '-');
290                         }
291                         last_hyphen = !last_hyphen;
292                 } else if (*key == '^') {
293                         /* ctrl-code */
294                         if (key[1] != '\0')
295                                 key++;
296
297                         expand_out_char(*out, '^');
298                         expand_out_char(*out, *key);
299                         expand_out_char(*out, '-');
300                         last_hyphen = FALSE; /* optional */
301                 } else if (last_hyphen && i_isalnum(*key) && !i_isdigit(*key)) {
302                         /* possibly beginning of keycombo */
303                         start = key;
304                         last_hyphen = FALSE;
305                 } else {
306                         expand_out_char(*out, *key);
307                         expand_out_char(*out, '-');
308                         last_hyphen = FALSE; /* optional */
309                 }
310         }
311
312         if (start != NULL)
313                 return expand_combo(start, key-1, out);
314
315         for (tmp = *out; tmp != NULL; tmp = tmp->next) {
316                 GString *str = tmp->data;
317
318                 g_string_truncate(str, str->len-1);
319         }
320
321         return TRUE;
322 }
323
324 static void key_states_scan_key(const char *key, KEY_REC *rec)
325 {
326         GSList *tmp, *out;
327
328         if (strcmp(rec->info->id, "key") == 0)
329                 return;
330
331         out = g_slist_append(NULL, g_string_new(NULL));
332         if (expand_key(key, &out)) {
333                 for (tmp = out; tmp != NULL; tmp = tmp->next) {
334                         GString *str = tmp->data;
335
336                         if (str->str[1] == '-' || str->str[1] == '\0')
337                                 used_keys[(int)(unsigned char)str->str[0]] = 1;
338
339                         g_tree_insert(key_states, g_strdup(str->str), rec);
340                 }
341         }
342
343         expand_out_free(out);
344 }
345
346 static int key_state_destroy(char *key)
347 {
348         g_free(key);
349         return FALSE;
350 }
351
352 /* Rescan all the key combos and figure out which characters are supposed
353    to be treated as characters and which as key combos.
354    Yes, this is pretty slow function... */
355 static void key_states_rescan(void)
356 {
357         GString *temp;
358
359         memset(used_keys, 0, sizeof(used_keys));
360
361         g_tree_traverse(key_states, (GTraverseFunc) key_state_destroy,
362                         G_IN_ORDER, NULL);
363         g_tree_destroy(key_states);
364         key_states = g_tree_new((GCompareFunc) strcmp);
365
366         temp = g_string_new(NULL);
367         g_hash_table_foreach(keys, (GHFunc) key_states_scan_key, temp);
368         g_string_free(temp, TRUE);
369 }
370
371 void key_configure_freeze(void)
372 {
373         key_config_frozen++;
374 }
375
376 void key_configure_thaw(void)
377 {
378         g_return_if_fail(key_config_frozen > 0);
379
380         if (--key_config_frozen == 0)
381                 key_states_rescan();
382 }
383
384 static void key_configure_destroy(KEY_REC *rec)
385 {
386         g_return_if_fail(rec != NULL);
387
388         rec->info->keys = g_slist_remove(rec->info->keys, rec);
389         g_hash_table_remove(keys, rec->key);
390
391         if (!key_config_frozen)
392                 key_states_rescan();
393
394         g_free_not_null(rec->data);
395         g_free(rec->key);
396         g_free(rec);
397 }
398
399 /* Configure new key */
400 static void key_configure_create(const char *id, const char *key,
401                                  const char *data)
402 {
403         KEYINFO_REC *info;
404         KEY_REC *rec;
405
406         g_return_if_fail(id != NULL);
407         g_return_if_fail(key != NULL && *key != '\0');
408
409         info = key_info_find(id);
410         if (info == NULL)
411                 return;
412
413         rec = g_hash_table_lookup(keys, key);
414         if (rec != NULL)
415                 key_configure_destroy(rec);
416
417         rec = g_new0(KEY_REC, 1);
418         rec->key = g_strdup(key);
419         rec->info = info;
420         rec->data = g_strdup(data);
421         info->keys = g_slist_append(info->keys, rec);
422         g_hash_table_insert(keys, rec->key, rec);
423
424         if (!key_config_frozen)
425                 key_states_rescan();
426 }
427
428 /* Bind a key for function */
429 void key_bind(const char *id, const char *description,
430               const char *key_default, const char *data, SIGNAL_FUNC func)
431 {
432         KEYINFO_REC *info;
433         char *key;
434
435         g_return_if_fail(id != NULL);
436
437         /* create key info record */
438         info = key_info_find(id);
439         if (info == NULL) {
440                 g_return_if_fail(func != NULL);
441
442                 if (description == NULL)
443                         g_warning("key_bind(%s) should have description!", id);
444                 info = g_new0(KEYINFO_REC, 1);
445                 info->id = g_strdup(id);
446                 info->description = g_strdup(description);
447                 keyinfos = g_slist_append(keyinfos, info);
448
449                 /* add the signal */
450                 key = g_strconcat("key ", id, NULL);
451                 signal_add(key, func);
452                 g_free(key);
453
454                 signal_emit("keyinfo created", 1, info);
455         }
456
457         if (key_default != NULL && *key_default != '\0') {
458                 key_default_add(id, key_default, data);
459                 key_configure_create(id, key_default, data);
460         }
461 }
462
463 static void keyinfo_remove(KEYINFO_REC *info)
464 {
465         g_return_if_fail(info != NULL);
466
467         keyinfos = g_slist_remove(keyinfos, info);
468         signal_emit("keyinfo destroyed", 1, info);
469
470         /* destroy all keys */
471         g_slist_foreach(info->keys, (GFunc) key_destroy, keys);
472         g_slist_foreach(info->default_keys, (GFunc) key_destroy, default_keys);
473
474         /* destroy key info */
475         g_slist_free(info->keys);
476         g_slist_free(info->default_keys);
477         g_free_not_null(info->description);
478         g_free(info->id);
479         g_free(info);
480 }
481
482 /* Unbind key */
483 void key_unbind(const char *id, SIGNAL_FUNC func)
484 {
485         KEYINFO_REC *info;
486         char *key;
487
488         g_return_if_fail(id != NULL);
489         g_return_if_fail(func != NULL);
490
491         /* remove keys */
492         info = key_info_find(id);
493         if (info != NULL)
494                 keyinfo_remove(info);
495
496         /* remove signal */
497         key = g_strconcat("key ", id, NULL);
498         signal_remove(key, func);
499         g_free(key);
500 }
501
502 /* Configure new key */
503 void key_configure_add(const char *id, const char *key, const char *data)
504 {
505         g_return_if_fail(id != NULL);
506         g_return_if_fail(key != NULL && *key != '\0');
507
508         key_configure_create(id, key, data);
509         keyconfig_save(id, key, data);
510 }
511
512 /* Remove key */
513 void key_configure_remove(const char *key)
514 {
515         KEY_REC *rec;
516
517         g_return_if_fail(key != NULL);
518
519         rec = g_hash_table_lookup(keys, key);
520         if (rec == NULL) return;
521
522         keyconfig_clear(key);
523         key_configure_destroy(rec);
524 }
525
526 static int key_emit_signal(KEYBOARD_REC *keyboard, KEY_REC *key)
527 {
528         int consumed;
529         char *str;
530
531         str = g_strconcat("key ", key->info->id, NULL);
532         consumed = signal_emit(str, 3, key->data, keyboard->gui_data, key->info);
533         g_free(str);
534
535         return consumed;
536 }
537
538 static int key_states_search(const unsigned char *combo,
539                              const unsigned char *search)
540 {
541         while (*search != '\0') {
542                 if (*combo != *search)
543                         return *search - *combo;
544                 search++; combo++;
545         }
546
547         return 0;
548 }
549
550 /* Returns TRUE if key press was consumed. Control characters should be sent
551    as "^@" .. "^_" instead of #0..#31 chars, #127 should be sent as ^? */
552 int key_pressed(KEYBOARD_REC *keyboard, const char *key)
553 {
554         KEY_REC *rec;
555         char *combo;
556         int first_key, consumed;
557
558         g_return_val_if_fail(keyboard != NULL, FALSE);
559         g_return_val_if_fail(key != NULL && *key != '\0', FALSE);
560
561         if (keyboard->key_state == NULL && key[1] == '\0' &&
562             !used_keys[(int) (unsigned char) key[0]]) {
563                 /* fast check - key not used */
564                 return FALSE;
565         }
566
567         first_key = keyboard->key_state == NULL;
568         combo = keyboard->key_state == NULL ? g_strdup(key) :
569                 g_strconcat(keyboard->key_state, "-", key, NULL);
570         g_free_and_null(keyboard->key_state);
571
572 #if GLIB_MAJOR_VERSION == 2
573 #  define GSearchFunc GCompareFunc
574 #endif
575         rec = g_tree_search(key_states,
576                             (GSearchFunc) key_states_search,
577                             combo);
578         if (rec == NULL) {
579                 /* unknown key combo, eat the invalid key
580                    unless it was the first key pressed */
581                 g_free(combo);
582                 return !first_key;
583         }
584
585         if (g_tree_lookup(key_states, combo) != rec) {
586                 /* key combo continues.. */
587                 keyboard->key_state = combo;
588                 return TRUE;
589         }
590
591         /* finished key combo, execute */
592         g_free(combo);
593         consumed = key_emit_signal(keyboard, rec);
594
595         /* never consume non-control characters */
596         return consumed;
597 }
598
599 void keyboard_entry_redirect(SIGNAL_FUNC func, const char *entry,
600                              int flags, void *data)
601 {
602         signal_emit("gui entry redirect", 4, func, entry,
603                     GINT_TO_POINTER(flags), data);
604 }
605
606 static void sig_command(const char *data)
607 {
608         const char *cmdchars;
609         char *str;
610
611         cmdchars = settings_get_str("cmdchars");
612         str = strchr(cmdchars, *data) != NULL ? g_strdup(data) :
613                 g_strdup_printf("%c%s", *cmdchars, data);
614
615         signal_emit("send command", 3, str, active_win->active_server, active_win->active);
616
617         g_free(str);
618 }
619
620 static void sig_key(const char *data)
621 {
622         /* we should never get here */
623 }
624
625 static void sig_multi(const char *data, void *gui_data)
626 {
627         KEYINFO_REC *info;
628         char **list, **tmp, *p, *str;
629
630         list = g_strsplit(data, ";", -1);
631         for (tmp = list; *tmp != NULL; tmp++) {
632                 p = strchr(*tmp, ' ');
633                 if (p != NULL) *p++ = '\0'; else p = "";
634
635                 info = key_info_find(*tmp);
636                 if (info != NULL) {
637                         str = g_strconcat("key ", info->id, NULL);
638                         signal_emit(str, 3, p, gui_data, info);
639                         g_free(str);
640                 }
641         }
642         g_strfreev(list);
643 }
644
645 static void sig_nothing(const char *data)
646 {
647 }
648
649 static void cmd_show_keys(const char *searchkey, int full)
650 {
651         GSList *info, *key;
652         int len;
653
654         printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_BIND_HEADER);
655
656         len = searchkey == NULL ? 0 : strlen(searchkey);
657         for (info = keyinfos; info != NULL; info = info->next) {
658                 KEYINFO_REC *rec = info->data;
659
660                 for (key = rec->keys; key != NULL; key = key->next) {
661                         KEY_REC *rec = key->data;
662
663                         if ((len == 0 || g_strncasecmp(rec->key, searchkey, len) == 0) &&
664                             (!full || rec->key[len] == '\0')) {
665                                 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_BIND_LIST,
666                                             rec->key, rec->info->id, rec->data == NULL ? "" : rec->data);
667                         }
668                 }
669         }
670
671         printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_BIND_FOOTER);
672 }
673
674 /* SYNTAX: BIND [-delete] [<key> [<command> [<data>]]] */
675 static void cmd_bind(const char *data)
676 {
677         GHashTable *optlist;
678         char *key, *id, *keydata;
679         void *free_arg;
680         int command_id;
681
682         if (!cmd_get_params(data, &free_arg, 3 | PARAM_FLAG_GETREST | PARAM_FLAG_OPTIONS,
683                             "bind", &optlist, &key, &id, &keydata))
684                 return;
685
686         if (*key != '\0' && g_hash_table_lookup(optlist, "delete")) {
687                 /* delete key */
688                 key_configure_remove(key);
689                 cmd_params_free(free_arg);
690                 return;
691         }
692
693         if (*id == '\0') {
694                 /* show some/all keys */
695                 cmd_show_keys(key, FALSE);
696                 cmd_params_free(free_arg);
697                 return;
698         }
699
700         command_id = strchr(settings_get_str("cmdchars"), *id) != NULL;
701         if (command_id) {
702                 /* using shortcut to command id */
703                 keydata = g_strconcat(id+1, " ", keydata, NULL);
704                 id = "command";
705         }
706
707         if (key_info_find(id) == NULL)
708                 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, TXT_BIND_UNKNOWN_ID, id);
709         else {
710                 key_configure_add(id, key, keydata);
711                 cmd_show_keys(key, TRUE);
712         }
713
714         if (command_id) g_free(keydata);
715         cmd_params_free(free_arg);
716 }
717
718 static GList *completion_get_keyinfos(const char *info)
719 {
720         GList *list;
721         GSList *tmp;
722         int len;
723
724         list = NULL; len = strlen(info);
725         for (tmp = keyinfos; tmp != NULL; tmp = tmp->next) {
726                 KEYINFO_REC *rec = tmp->data;
727
728                 if (g_strncasecmp(rec->id, info, len) == 0)
729                         list = g_list_append(list, g_strdup(rec->id));
730         }
731
732         return list;
733 }
734
735 static void sig_complete_bind(GList **list, WINDOW_REC *window,
736                               const char *word, const char *line,
737                               int *want_space)
738 {
739         g_return_if_fail(list != NULL);
740         g_return_if_fail(word != NULL);
741         g_return_if_fail(line != NULL);
742
743         if (*line == '\0' || strchr(line, ' ') != NULL)
744                 return;
745
746         *list = completion_get_keyinfos(word);
747         if (*list != NULL) signal_stop();
748 }
749
750 static int key_destroy_hash(const char *key, KEY_REC *rec)
751 {
752         rec->info->keys = g_slist_remove(rec->info->keys, rec);
753
754         g_free_not_null(rec->data);
755         g_free(rec->key);
756         g_free(rec);
757         return TRUE;
758 }
759
760 static void key_copy_default(const char *key, KEY_REC *orig)
761 {
762         KEY_REC *rec;
763
764         rec = g_new0(KEY_REC, 1);
765         rec->key = g_strdup(orig->key);
766         rec->info = orig->info;
767         rec->data = g_strdup(orig->data);
768
769         rec->info->keys = g_slist_append(rec->info->keys, rec);
770         g_hash_table_insert(keys, rec->key, rec);
771 }
772
773 static void keyboard_reset_defaults(void)
774 {
775         g_hash_table_foreach_remove(keys, (GHRFunc) key_destroy_hash, NULL);
776         g_hash_table_foreach(default_keys, (GHFunc) key_copy_default, NULL);
777 }
778
779 static void key_config_read(CONFIG_NODE *node)
780 {
781         char *key, *id, *data;
782
783         g_return_if_fail(node != NULL);
784
785         key = config_node_get_str(node, "key", NULL);
786         id = config_node_get_str(node, "id", NULL);
787         data = config_node_get_str(node, "data", NULL);
788
789         if (key != NULL && id != NULL)
790                 key_configure_create(id, key, data);
791 }
792
793 static void read_keyboard_config(void)
794 {
795         CONFIG_NODE *node;
796         GSList *tmp;
797
798         key_configure_freeze();
799
800         keyboard_reset_defaults();
801
802         node = iconfig_node_traverse("keyboard", FALSE);
803         if (node == NULL) {
804                 key_configure_thaw();
805                 return;
806         }
807
808         /* FIXME: backward "compatibility" - remove after irssi .99 */
809         if (node->type != NODE_TYPE_LIST) {
810                 iconfig_node_remove(NULL, node);
811                 key_configure_thaw();
812                 return;
813         }
814
815         tmp = config_node_first(node->value);
816         for (; tmp != NULL; tmp = config_node_next(tmp))
817                 key_config_read(tmp->data);
818
819         key_configure_thaw();
820 }
821
822 void keyboard_init(void)
823 {
824         keys = g_hash_table_new((GHashFunc) g_str_hash,
825                                 (GCompareFunc) g_str_equal);
826         default_keys = g_hash_table_new((GHashFunc) g_str_hash,
827                                         (GCompareFunc) g_str_equal);
828         keyinfos = NULL;
829         key_states = g_tree_new((GCompareFunc) strcmp);
830         key_config_frozen = 0;
831         memset(used_keys, 0, sizeof(used_keys));
832
833         key_bind("command", "Run any IRC command", NULL, NULL, (SIGNAL_FUNC) sig_command);
834         key_bind("key", "Specify name for key binding", NULL, NULL, (SIGNAL_FUNC) sig_key);
835         key_bind("multi", "Run multiple commands", NULL, NULL, (SIGNAL_FUNC) sig_multi);
836         key_bind("nothing", "Do nothing", NULL, NULL, (SIGNAL_FUNC) sig_nothing);
837
838         /* read the keyboard config when all key binds are known */
839         signal_add("irssi init read settings", (SIGNAL_FUNC) read_keyboard_config);
840         signal_add("setup reread", (SIGNAL_FUNC) read_keyboard_config);
841         signal_add("complete command bind", (SIGNAL_FUNC) sig_complete_bind);
842
843         command_bind("bind", NULL, (SIGNAL_FUNC) cmd_bind);
844         command_set_options("bind", "delete");
845 }
846
847 void keyboard_deinit(void)
848 {
849         key_unbind("command", (SIGNAL_FUNC) sig_command);
850         key_unbind("key", (SIGNAL_FUNC) sig_key);
851         key_unbind("multi", (SIGNAL_FUNC) sig_multi);
852         key_unbind("nothing", (SIGNAL_FUNC) sig_nothing);
853
854         while (keyinfos != NULL)
855                 keyinfo_remove(keyinfos->data);
856         g_hash_table_destroy(keys);
857         g_hash_table_destroy(default_keys);
858
859         g_tree_traverse(key_states, (GTraverseFunc) key_state_destroy,
860                         G_IN_ORDER, NULL);
861         g_tree_destroy(key_states);
862
863         signal_remove("irssi init read settings", (SIGNAL_FUNC) read_keyboard_config);
864         signal_remove("setup reread", (SIGNAL_FUNC) read_keyboard_config);
865         signal_remove("complete command bind", (SIGNAL_FUNC) sig_complete_bind);
866         command_unbind("bind", (SIGNAL_FUNC) cmd_bind);
867 }