4 Copyright (C) 1999-2001 Timo Sirainen
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.
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.
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
22 #include "module-formats.h"
27 #include "lib-config/iconfig.h"
31 #include "fe-windows.h"
32 #include "printtext.h"
35 static GHashTable *keys, *default_keys;
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];
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,
45 static GTree *key_states;
46 /* List of all key combo names */
47 static GSList *key_combos;
48 static int key_config_frozen;
53 /BIND meta-O key meta2
54 /BIND meta-[ key meta2
56 /BIND meta2-C key right
57 /BIND ^W-meta-right /echo ^W Meta-right key pressed
59 When ^W Meta-Right is pressed, the full char combination
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
70 key_state is moved to key_prev_state if there's nothing else in
71 /BINDs matching for key_state-newkey.
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;
77 /* GUI specific data sent in "key pressed" signal */
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"
84 KEYBOARD_REC *keyboard_create(void *data)
88 rec = g_new0(KEYBOARD_REC, 1);
91 signal_emit("keyboard created", 1, rec);
95 /* Destroys a keyboard */
96 void keyboard_destroy(KEYBOARD_REC *keyboard)
98 signal_emit("keyboard destroyed", 1, keyboard);
100 g_free_not_null(keyboard->key_state);
101 g_free_not_null(keyboard->key_prev_state);
105 static void key_destroy(KEY_REC *rec, GHashTable *hash)
107 g_hash_table_remove(hash, rec->key);
109 g_free_not_null(rec->data);
114 static void key_default_add(const char *id, const char *key, const char *data)
119 info = key_info_find(id);
123 rec = g_hash_table_lookup(default_keys, key);
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);
131 rec = g_new0(KEY_REC, 1);
132 rec->key = g_strdup(key);
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);
139 static CONFIG_NODE *key_config_find(const char *key)
144 /* remove old keyboard settings */
145 node = iconfig_node_traverse("(keyboard", TRUE);
147 for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
150 if (strcmp(config_node_get_str(node, "key", ""), key) == 0)
157 static void keyconfig_save(const char *id, const char *key, const char *data)
161 g_return_if_fail(id != NULL);
162 g_return_if_fail(key != NULL);
164 node = key_config_find(key);
166 node = iconfig_node_traverse("(keyboard", TRUE);
167 node = config_node_section(node, NULL, NODE_TYPE_BLOCK);
170 iconfig_node_set_str(node, "key", key);
171 iconfig_node_set_str(node, "id", id);
172 iconfig_node_set_str(node, "data", data);
175 static void keyconfig_clear(const char *key)
179 g_return_if_fail(key != NULL);
181 /* remove old keyboard settings */
182 node = key_config_find(key);
184 iconfig_node_clear(node);
187 KEYINFO_REC *key_info_find(const char *id)
191 for (tmp = keyinfos; tmp != NULL; tmp = tmp->next) {
192 KEYINFO_REC *rec = tmp->data;
194 if (g_strcasecmp(rec->id, id) == 0)
201 static KEY_REC *key_combo_find(const char *key)
206 info = key_info_find("key");
210 for (tmp = info->keys; tmp != NULL; tmp = tmp->next) {
211 KEY_REC *rec = tmp->data;
213 if (strcmp(rec->data, key) == 0)
220 static void key_states_scan_key(const char *key, KEY_REC *rec, GString *temp)
222 char **keys, **tmp, *p;
224 g_string_truncate(temp, 0);
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)) {
231 g_string_append(temp, *tmp);
232 g_string_append_c(temp, '-');
238 g_string_append(temp, "--");
242 for (p = *tmp; *p != '\0'; p++) {
243 g_string_append_c(temp, *p);
249 g_string_append_c(temp, *p);
252 g_string_append_c(temp, '-');
258 g_string_truncate(temp, temp->len-1);
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);
266 static int key_state_destroy(char *key)
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)
279 memset(used_keys, 0, sizeof(used_keys));
281 g_tree_traverse(key_states, (GTraverseFunc) key_state_destroy,
283 g_tree_destroy(key_states);
284 key_states = g_tree_new((GCompareFunc) strcmp);
286 temp = g_string_new(NULL);
287 g_hash_table_foreach(keys, (GHFunc) key_states_scan_key, temp);
288 g_string_free(temp, TRUE);
291 void key_configure_freeze(void)
296 void key_configure_thaw(void)
298 g_return_if_fail(key_config_frozen > 0);
300 if (--key_config_frozen == 0)
304 static void key_configure_destroy(KEY_REC *rec)
306 g_return_if_fail(rec != NULL);
308 rec->info->keys = g_slist_remove(rec->info->keys, rec);
309 g_hash_table_remove(keys, rec->key);
311 if (!key_config_frozen)
314 g_free_not_null(rec->data);
319 /* Configure new key */
320 static void key_configure_create(const char *id, const char *key,
326 g_return_if_fail(id != NULL);
327 g_return_if_fail(key != NULL && *key != '\0');
329 info = key_info_find(id);
333 rec = g_hash_table_lookup(keys, key);
335 key_configure_destroy(rec);
337 rec = g_new0(KEY_REC, 1);
338 rec->key = g_strdup(key);
340 rec->data = g_strdup(data);
341 info->keys = g_slist_append(info->keys, rec);
342 g_hash_table_insert(keys, rec->key, rec);
344 if (!key_config_frozen)
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)
355 g_return_if_fail(id != NULL);
357 /* create key info record */
358 info = key_info_find(id);
360 g_return_if_fail(func != NULL);
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);
370 key = g_strconcat("key ", id, NULL);
371 signal_add(key, func);
374 signal_emit("keyinfo created", 1, info);
377 if (key_default != NULL && *key_default != '\0') {
378 key_default_add(id, key_default, data);
379 key_configure_create(id, key_default, data);
383 static void keyinfo_remove(KEYINFO_REC *info)
385 g_return_if_fail(info != NULL);
387 keyinfos = g_slist_remove(keyinfos, info);
388 signal_emit("keyinfo destroyed", 1, info);
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);
394 /* destroy key info */
395 g_slist_free(info->keys);
396 g_slist_free(info->default_keys);
397 g_free_not_null(info->description);
403 void key_unbind(const char *id, SIGNAL_FUNC func)
408 g_return_if_fail(id != NULL);
409 g_return_if_fail(func != NULL);
412 info = key_info_find(id);
414 keyinfo_remove(info);
417 key = g_strconcat("key ", id, NULL);
418 signal_remove(key, func);
422 /* Configure new key */
423 void key_configure_add(const char *id, const char *key, const char *data)
425 g_return_if_fail(id != NULL);
426 g_return_if_fail(key != NULL && *key != '\0');
428 key_configure_create(id, key, data);
429 keyconfig_save(id, key, data);
433 void key_configure_remove(const char *key)
437 g_return_if_fail(key != NULL);
439 rec = g_hash_table_lookup(keys, key);
440 if (rec == NULL) return;
442 keyconfig_clear(key);
443 key_configure_destroy(rec);
446 static int key_emit_signal(KEYBOARD_REC *keyboard, KEY_REC *key)
451 str = g_strconcat("key ", key->info->id, NULL);
452 consumed = signal_emit(str, 3, key->data, keyboard->gui_data, key->info);
458 int key_states_search(const char *combo, const char *search)
460 while (*search != '\0') {
461 if (*combo != *search)
462 return *search - *combo;
466 return *combo == '\0' || *combo == '-' ? 0 : -1;
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)
477 g_return_val_if_fail(keyboard != NULL, FALSE);
478 g_return_val_if_fail(key != NULL && *key != '\0', FALSE);
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 */
487 rec = g_tree_search(key_states,
488 (GSearchFunc) key_states_search,
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);
497 consumed = key_emit_signal(keyboard, rec);
499 /* never consume non-control characters */
500 return consumed && key[1] != '\0';
504 if (keyboard->key_state == NULL) {
505 /* first key in combo */
506 rec = g_tree_lookup(key_states, (void *) key);
508 /* continuing key combination */
509 str = g_strconcat(keyboard->key_state, "-", key, NULL);
510 rec = g_tree_lookup(key_states, str);
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);
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;
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;
531 keyboard->key_state = g_strdup(key);
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);
540 rec = g_tree_lookup(key_states, str);
542 if (strcmp(rec->info->id, "key") == 0)
543 rec = g_tree_lookup(key_states, rec->data);
547 key_emit_signal(keyboard, rec);
551 /* check that combo is possible */
552 rec = g_tree_search(key_states,
553 (GSearchFunc) key_states_search, str);
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);
566 void keyboard_entry_redirect(SIGNAL_FUNC func, const char *entry,
567 int flags, void *data)
569 signal_emit("gui entry redirect", 4, func, entry,
570 GINT_TO_POINTER(flags), data);
573 static void sig_command(const char *data)
575 const char *cmdchars;
578 cmdchars = settings_get_str("cmdchars");
579 str = strchr(cmdchars, *data) != NULL ? g_strdup(data) :
580 g_strdup_printf("%c%s", *cmdchars, data);
582 signal_emit("send command", 3, str, active_win->active_server, active_win->active);
587 static void sig_key(const char *data)
589 /* we should never get here */
592 static void sig_multi(const char *data, void *gui_data)
595 char **list, **tmp, *p, *str;
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 = "";
602 info = key_info_find(*tmp);
604 str = g_strconcat("key ", info->id, NULL);
605 signal_emit(str, 3, p, gui_data, info);
612 static void cmd_show_keys(const char *searchkey, int full)
617 len = searchkey == NULL ? 0 : strlen(searchkey);
618 for (info = keyinfos; info != NULL; info = info->next) {
619 KEYINFO_REC *rec = info->data;
621 for (key = rec->keys; key != NULL; key = key->next) {
622 KEY_REC *rec = key->data;
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);
633 /* SYNTAX: BIND [-delete] [<key> [<command> [<data>]]] */
634 static void cmd_bind(const char *data)
637 char *key, *id, *keydata;
641 if (!cmd_get_params(data, &free_arg, 3 | PARAM_FLAG_GETREST | PARAM_FLAG_OPTIONS,
642 "bind", &optlist, &key, &id, &keydata))
645 if (*key != '\0' && g_hash_table_lookup(optlist, "delete")) {
647 key_configure_remove(key);
648 cmd_params_free(free_arg);
653 /* show some/all keys */
654 cmd_show_keys(key, FALSE);
655 cmd_params_free(free_arg);
659 command_id = strchr(settings_get_str("cmdchars"), *id) != NULL;
661 /* using shortcut to command id */
662 keydata = g_strconcat(id, " ", keydata, NULL);
666 if (key_info_find(id) == NULL)
667 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, TXT_BIND_UNKNOWN_ID, id);
669 key_configure_add(id, key, keydata);
670 cmd_show_keys(key, TRUE);
673 if (command_id) g_free(keydata);
674 cmd_params_free(free_arg);
677 static GList *completion_get_keyinfos(const char *info)
683 list = NULL; len = strlen(info);
684 for (tmp = keyinfos; tmp != NULL; tmp = tmp->next) {
685 KEYINFO_REC *rec = tmp->data;
687 if (g_strncasecmp(rec->id, info, len) == 0)
688 list = g_list_append(list, g_strdup(rec->id));
694 static void sig_complete_bind(GList **list, WINDOW_REC *window,
695 const char *word, const char *line,
698 g_return_if_fail(list != NULL);
699 g_return_if_fail(word != NULL);
700 g_return_if_fail(line != NULL);
702 if (*line == '\0' || strchr(line, ' ') != NULL)
705 *list = completion_get_keyinfos(word);
706 if (*list != NULL) signal_stop();
709 static int key_destroy_hash(const char *key, KEY_REC *rec)
711 rec->info->keys = g_slist_remove(rec->info->keys, rec);
713 g_free_not_null(rec->data);
719 static void key_copy_default(const char *key, KEY_REC *orig)
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);
728 rec->info->keys = g_slist_append(rec->info->keys, rec);
729 g_hash_table_insert(keys, rec->key, rec);
732 static void keyboard_reset_defaults(void)
734 g_hash_table_foreach_remove(keys, (GHRFunc) key_destroy_hash, NULL);
735 g_hash_table_foreach(default_keys, (GHFunc) key_copy_default, NULL);
738 static void key_config_read(CONFIG_NODE *node)
740 char *key, *id, *data;
742 g_return_if_fail(node != NULL);
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);
748 if (key != NULL && id != NULL)
749 key_configure_create(id, key, data);
752 static void read_keyboard_config(void)
757 key_configure_freeze();
759 keyboard_reset_defaults();
761 node = iconfig_node_traverse("keyboard", FALSE);
763 key_configure_thaw();
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();
774 for (tmp = node->value; tmp != NULL; tmp = tmp->next)
775 key_config_read(tmp->data);
777 key_configure_thaw();
780 void keyboard_init(void)
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);
787 key_states = g_tree_new((GCompareFunc) strcmp);
789 key_config_frozen = 0;
790 memset(used_keys, 0, sizeof(used_keys));
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);
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);
801 command_bind("bind", NULL, (SIGNAL_FUNC) cmd_bind);
802 command_set_options("bind", "delete");
805 void keyboard_deinit(void)
807 while (keyinfos != NULL)
808 keyinfo_remove(keyinfos->data);
809 g_hash_table_destroy(keys);
810 g_hash_table_destroy(default_keys);
812 g_tree_traverse(key_states, (GTraverseFunc) key_state_destroy,
814 g_tree_destroy(key_states);
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);