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