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 a list of all possible executable key bindings (not "key" keys).
42 Format is _always_ in key1-key2-key3 format and fully extracted, like
44 static GTree *key_states;
45 static int key_config_frozen;
47 struct _KEYBOARD_REC {
48 char *key_state; /* the ongoing key combo */
49 void *gui_data; /* GUI specific data sent in "key pressed" signal */
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"
55 KEYBOARD_REC *keyboard_create(void *data)
59 rec = g_new0(KEYBOARD_REC, 1);
62 signal_emit("keyboard created", 1, rec);
66 /* Destroys a keyboard */
67 void keyboard_destroy(KEYBOARD_REC *keyboard)
69 signal_emit("keyboard destroyed", 1, keyboard);
71 g_free_not_null(keyboard->key_state);
75 static void key_destroy(KEY_REC *rec, GHashTable *hash)
77 g_hash_table_remove(hash, rec->key);
79 g_free_not_null(rec->data);
84 static void key_default_add(const char *id, const char *key, const char *data)
89 info = key_info_find(id);
93 rec = g_hash_table_lookup(default_keys, key);
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);
101 rec = g_new0(KEY_REC, 1);
102 rec->key = g_strdup(key);
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);
109 static CONFIG_NODE *key_config_find(const char *key)
114 /* remove old keyboard settings */
115 node = iconfig_node_traverse("(keyboard", TRUE);
117 tmp = config_node_first(node->value);
118 for (; tmp != NULL; tmp = config_node_next(tmp)) {
121 if (strcmp(config_node_get_str(node, "key", ""), key) == 0)
128 static void keyconfig_save(const char *id, const char *key, const char *data)
132 g_return_if_fail(id != NULL);
133 g_return_if_fail(key != NULL);
135 node = key_config_find(key);
137 node = iconfig_node_traverse("(keyboard", TRUE);
138 node = config_node_section(node, NULL, NODE_TYPE_BLOCK);
141 iconfig_node_set_str(node, "key", key);
142 iconfig_node_set_str(node, "id", id);
143 iconfig_node_set_str(node, "data", data);
146 static void keyconfig_clear(const char *key)
150 g_return_if_fail(key != NULL);
152 /* remove old keyboard settings */
153 node = key_config_find(key);
155 iconfig_node_remove(iconfig_node_traverse("(keyboard", FALSE),
160 KEYINFO_REC *key_info_find(const char *id)
164 for (tmp = keyinfos; tmp != NULL; tmp = tmp->next) {
165 KEYINFO_REC *rec = tmp->data;
167 if (g_strcasecmp(rec->id, id) == 0)
174 static int expand_key(const char *key, GSList **out);
176 #define expand_out_char(out, c) \
179 for (tmp = out; tmp != NULL; tmp = tmp->next) \
180 g_string_append_c(tmp->data, c); \
183 #define expand_out_free(out) \
186 for (tmp = out; tmp != NULL; tmp = tmp->next) \
187 g_string_free(tmp->data, TRUE); \
188 g_slist_free(out); out = NULL; \
191 static int expand_combo(const char *start, const char *end, GSList **out)
195 GSList *tmp, *tmp2, *list, *copy, *newout;
200 expand_out_char(*out, *start);
204 info = key_info_find("key");
208 /* get list of all key combos that generate the named combo.. */
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;
214 if (strcmp(rec->data, str) == 0)
215 list = g_slist_append(list, rec);
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);
228 if (list->next == NULL) {
229 /* only one way to generate the combo, good */
231 return expand_key(rec->key, out);
234 /* multiple ways to generate the combo -
235 we'll need to include all of them in output */
237 for (tmp = list->next; tmp != NULL; tmp = tmp->next) {
238 KEY_REC *rec = tmp->data;
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));
246 if (!expand_key(rec->key, ©)) {
247 /* illegal key combo, remove from list */
248 expand_out_free(copy);
250 newout = g_slist_concat(newout, copy);
255 if (!expand_key(rec->key, out)) {
256 /* illegal key combo, remove from list */
257 expand_out_free(*out);
260 *out = g_slist_concat(*out, newout);
264 /* Expand key code - returns TRUE if successful. */
265 static int expand_key(const char *key, GSList **out)
271 /* meta-^W^Gf -> ^[-^W-^G-f */
272 start = NULL; last_hyphen = TRUE;
273 for (; *key != '\0'; key++) {
275 if (i_isalnum(*key) || *key == '_') {
276 /* key combo continues */
280 if (!expand_combo(start, key-1, out))
282 expand_out_char(*out, '-');
288 expand_out_char(*out, '-');
289 expand_out_char(*out, '-');
291 last_hyphen = !last_hyphen;
292 } else if (*key == '^') {
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 */
306 expand_out_char(*out, *key);
307 expand_out_char(*out, '-');
308 last_hyphen = FALSE; /* optional */
313 return expand_combo(start, key-1, out);
315 for (tmp = *out; tmp != NULL; tmp = tmp->next) {
316 GString *str = tmp->data;
318 g_string_truncate(str, str->len-1);
324 static void key_states_scan_key(const char *key, KEY_REC *rec)
328 if (strcmp(rec->info->id, "key") == 0)
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;
336 if (str->str[1] == '-' || str->str[1] == '\0')
337 used_keys[(int)(unsigned char)str->str[0]] = 1;
339 g_tree_insert(key_states, g_strdup(str->str), rec);
343 expand_out_free(out);
346 static int key_state_destroy(char *key)
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)
359 memset(used_keys, 0, sizeof(used_keys));
361 g_tree_traverse(key_states, (GTraverseFunc) key_state_destroy,
363 g_tree_destroy(key_states);
364 key_states = g_tree_new((GCompareFunc) strcmp);
366 temp = g_string_new(NULL);
367 g_hash_table_foreach(keys, (GHFunc) key_states_scan_key, temp);
368 g_string_free(temp, TRUE);
371 void key_configure_freeze(void)
376 void key_configure_thaw(void)
378 g_return_if_fail(key_config_frozen > 0);
380 if (--key_config_frozen == 0)
384 static void key_configure_destroy(KEY_REC *rec)
386 g_return_if_fail(rec != NULL);
388 rec->info->keys = g_slist_remove(rec->info->keys, rec);
389 g_hash_table_remove(keys, rec->key);
391 signal_emit("key destroyed", 1, rec);
393 if (!key_config_frozen)
396 g_free_not_null(rec->data);
401 /* Configure new key */
402 static void key_configure_create(const char *id, const char *key,
408 g_return_if_fail(id != NULL);
409 g_return_if_fail(key != NULL && *key != '\0');
411 info = key_info_find(id);
415 rec = g_hash_table_lookup(keys, key);
417 key_configure_destroy(rec);
419 rec = g_new0(KEY_REC, 1);
420 rec->key = g_strdup(key);
422 rec->data = g_strdup(data);
423 info->keys = g_slist_append(info->keys, rec);
424 g_hash_table_insert(keys, rec->key, rec);
426 signal_emit("key created", 1, rec);
428 if (!key_config_frozen)
432 /* Bind a key for function */
433 void key_bind(const char *id, const char *description,
434 const char *key_default, const char *data, SIGNAL_FUNC func)
439 g_return_if_fail(id != NULL);
441 /* create key info record */
442 info = key_info_find(id);
444 g_return_if_fail(func != NULL);
446 if (description == NULL)
447 g_warning("key_bind(%s) should have description!", id);
448 info = g_new0(KEYINFO_REC, 1);
449 info->id = g_strdup(id);
450 info->description = g_strdup(description);
451 keyinfos = g_slist_append(keyinfos, info);
454 key = g_strconcat("key ", id, NULL);
455 signal_add(key, func);
458 signal_emit("keyinfo created", 1, info);
461 if (key_default != NULL && *key_default != '\0') {
462 key_default_add(id, key_default, data);
463 key_configure_create(id, key_default, data);
467 static void keyinfo_remove(KEYINFO_REC *info)
469 g_return_if_fail(info != NULL);
471 keyinfos = g_slist_remove(keyinfos, info);
472 signal_emit("keyinfo destroyed", 1, info);
474 /* destroy all keys */
475 g_slist_foreach(info->keys, (GFunc) key_destroy, keys);
476 g_slist_foreach(info->default_keys, (GFunc) key_destroy, default_keys);
478 /* destroy key info */
479 g_slist_free(info->keys);
480 g_slist_free(info->default_keys);
481 g_free_not_null(info->description);
487 void key_unbind(const char *id, SIGNAL_FUNC func)
492 g_return_if_fail(id != NULL);
493 g_return_if_fail(func != NULL);
496 info = key_info_find(id);
498 keyinfo_remove(info);
501 key = g_strconcat("key ", id, NULL);
502 signal_remove(key, func);
506 /* Configure new key */
507 void key_configure_add(const char *id, const char *key, const char *data)
509 g_return_if_fail(id != NULL);
510 g_return_if_fail(key != NULL && *key != '\0');
512 key_configure_create(id, key, data);
513 keyconfig_save(id, key, data);
517 void key_configure_remove(const char *key)
521 g_return_if_fail(key != NULL);
523 rec = g_hash_table_lookup(keys, key);
524 if (rec == NULL) return;
526 keyconfig_clear(key);
527 key_configure_destroy(rec);
530 static int key_emit_signal(KEYBOARD_REC *keyboard, KEY_REC *key)
535 str = g_strconcat("key ", key->info->id, NULL);
536 consumed = signal_emit(str, 3, key->data, keyboard->gui_data, key->info);
542 static int key_states_search(const unsigned char *combo,
543 const unsigned char *search)
545 while (*search != '\0') {
546 if (*combo != *search)
547 return *search - *combo;
554 /* Returns TRUE if key press was consumed. Control characters should be sent
555 as "^@" .. "^_" instead of #0..#31 chars, #127 should be sent as ^? */
556 int key_pressed(KEYBOARD_REC *keyboard, const char *key)
560 int first_key, consumed;
562 g_return_val_if_fail(keyboard != NULL, FALSE);
563 g_return_val_if_fail(key != NULL && *key != '\0', FALSE);
565 if (keyboard->key_state == NULL && key[1] == '\0' &&
566 !used_keys[(int) (unsigned char) key[0]]) {
567 /* fast check - key not used */
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);
576 #if GLIB_MAJOR_VERSION == 2
577 # define GSearchFunc GCompareFunc
579 rec = g_tree_search(key_states,
580 (GSearchFunc) key_states_search,
583 /* unknown key combo, eat the invalid key
584 unless it was the first key pressed */
589 if (g_tree_lookup(key_states, combo) != rec) {
590 /* key combo continues.. */
591 keyboard->key_state = combo;
595 /* finished key combo, execute */
597 consumed = key_emit_signal(keyboard, rec);
599 /* never consume non-control characters */
603 void keyboard_entry_redirect(SIGNAL_FUNC func, const char *entry,
604 int flags, void *data)
606 signal_emit("gui entry redirect", 4, func, entry,
607 GINT_TO_POINTER(flags), data);
610 static void sig_command(const char *data)
612 const char *cmdchars;
615 cmdchars = settings_get_str("cmdchars");
616 str = strchr(cmdchars, *data) != NULL ? g_strdup(data) :
617 g_strdup_printf("%c%s", *cmdchars, data);
619 signal_emit("send command", 3, str, active_win->active_server, active_win->active);
624 static void sig_key(const char *data)
626 /* we should never get here */
629 static void sig_multi(const char *data, void *gui_data)
632 char **list, **tmp, *p, *str;
634 list = g_strsplit(data, ";", -1);
635 for (tmp = list; *tmp != NULL; tmp++) {
636 p = strchr(*tmp, ' ');
637 if (p != NULL) *p++ = '\0'; else p = "";
639 info = key_info_find(*tmp);
641 str = g_strconcat("key ", info->id, NULL);
642 signal_emit(str, 3, p, gui_data, info);
649 static void sig_nothing(const char *data)
653 static void cmd_show_keys(const char *searchkey, int full)
658 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_BIND_HEADER);
660 len = searchkey == NULL ? 0 : strlen(searchkey);
661 for (info = keyinfos; info != NULL; info = info->next) {
662 KEYINFO_REC *rec = info->data;
664 for (key = rec->keys; key != NULL; key = key->next) {
665 KEY_REC *rec = key->data;
667 if ((len == 0 || g_strncasecmp(rec->key, searchkey, len) == 0) &&
668 (!full || rec->key[len] == '\0')) {
669 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_BIND_LIST,
670 rec->key, rec->info->id, rec->data == NULL ? "" : rec->data);
675 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_BIND_FOOTER);
678 /* SYNTAX: BIND [-delete] [<key> [<command> [<data>]]] */
679 static void cmd_bind(const char *data)
682 char *key, *id, *keydata;
686 if (!cmd_get_params(data, &free_arg, 3 | PARAM_FLAG_GETREST | PARAM_FLAG_OPTIONS,
687 "bind", &optlist, &key, &id, &keydata))
690 if (*key != '\0' && g_hash_table_lookup(optlist, "delete")) {
692 key_configure_remove(key);
693 cmd_params_free(free_arg);
698 /* show some/all keys */
699 cmd_show_keys(key, FALSE);
700 cmd_params_free(free_arg);
704 command_id = strchr(settings_get_str("cmdchars"), *id) != NULL;
706 /* using shortcut to command id */
707 keydata = g_strconcat(id+1, " ", keydata, NULL);
711 if (key_info_find(id) == NULL)
712 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, TXT_BIND_UNKNOWN_ID, id);
714 key_configure_add(id, key, keydata);
715 cmd_show_keys(key, TRUE);
718 if (command_id) g_free(keydata);
719 cmd_params_free(free_arg);
722 static GList *completion_get_keyinfos(const char *info)
728 list = NULL; len = strlen(info);
729 for (tmp = keyinfos; tmp != NULL; tmp = tmp->next) {
730 KEYINFO_REC *rec = tmp->data;
732 if (g_strncasecmp(rec->id, info, len) == 0)
733 list = g_list_append(list, g_strdup(rec->id));
739 static void sig_complete_bind(GList **list, WINDOW_REC *window,
740 const char *word, const char *line,
743 g_return_if_fail(list != NULL);
744 g_return_if_fail(word != NULL);
745 g_return_if_fail(line != NULL);
747 if (*line == '\0' || strchr(line, ' ') != NULL)
750 *list = completion_get_keyinfos(word);
751 if (*list != NULL) signal_stop();
754 static int key_destroy_hash(const char *key, KEY_REC *rec)
756 rec->info->keys = g_slist_remove(rec->info->keys, rec);
758 g_free_not_null(rec->data);
764 static void key_copy_default(const char *key, KEY_REC *orig)
768 rec = g_new0(KEY_REC, 1);
769 rec->key = g_strdup(orig->key);
770 rec->info = orig->info;
771 rec->data = g_strdup(orig->data);
773 rec->info->keys = g_slist_append(rec->info->keys, rec);
774 g_hash_table_insert(keys, rec->key, rec);
777 static void keyboard_reset_defaults(void)
779 g_hash_table_foreach_remove(keys, (GHRFunc) key_destroy_hash, NULL);
780 g_hash_table_foreach(default_keys, (GHFunc) key_copy_default, NULL);
783 static void key_config_read(CONFIG_NODE *node)
785 char *key, *id, *data;
787 g_return_if_fail(node != NULL);
789 key = config_node_get_str(node, "key", NULL);
790 id = config_node_get_str(node, "id", NULL);
791 data = config_node_get_str(node, "data", NULL);
793 if (key != NULL && id != NULL)
794 key_configure_create(id, key, data);
797 static void read_keyboard_config(void)
802 key_configure_freeze();
804 keyboard_reset_defaults();
806 node = iconfig_node_traverse("keyboard", FALSE);
808 key_configure_thaw();
812 /* FIXME: backward "compatibility" - remove after irssi .99 */
813 if (node->type != NODE_TYPE_LIST) {
814 iconfig_node_remove(NULL, node);
815 key_configure_thaw();
819 tmp = config_node_first(node->value);
820 for (; tmp != NULL; tmp = config_node_next(tmp))
821 key_config_read(tmp->data);
823 key_configure_thaw();
826 void keyboard_init(void)
828 keys = g_hash_table_new((GHashFunc) g_str_hash,
829 (GCompareFunc) g_str_equal);
830 default_keys = g_hash_table_new((GHashFunc) g_str_hash,
831 (GCompareFunc) g_str_equal);
833 key_states = g_tree_new((GCompareFunc) strcmp);
834 key_config_frozen = 0;
835 memset(used_keys, 0, sizeof(used_keys));
837 key_bind("command", "Run any IRC command", NULL, NULL, (SIGNAL_FUNC) sig_command);
838 key_bind("key", "Specify name for key binding", NULL, NULL, (SIGNAL_FUNC) sig_key);
839 key_bind("multi", "Run multiple commands", NULL, NULL, (SIGNAL_FUNC) sig_multi);
840 key_bind("nothing", "Do nothing", NULL, NULL, (SIGNAL_FUNC) sig_nothing);
842 /* read the keyboard config when all key binds are known */
843 signal_add("irssi init read settings", (SIGNAL_FUNC) read_keyboard_config);
844 signal_add("setup reread", (SIGNAL_FUNC) read_keyboard_config);
845 signal_add("complete command bind", (SIGNAL_FUNC) sig_complete_bind);
847 command_bind("bind", NULL, (SIGNAL_FUNC) cmd_bind);
848 command_set_options("bind", "delete");
851 void keyboard_deinit(void)
853 key_unbind("command", (SIGNAL_FUNC) sig_command);
854 key_unbind("key", (SIGNAL_FUNC) sig_key);
855 key_unbind("multi", (SIGNAL_FUNC) sig_multi);
856 key_unbind("nothing", (SIGNAL_FUNC) sig_nothing);
858 while (keyinfos != NULL)
859 keyinfo_remove(keyinfos->data);
860 g_hash_table_destroy(keys);
861 g_hash_table_destroy(default_keys);
863 g_tree_traverse(key_states, (GTraverseFunc) key_state_destroy,
865 g_tree_destroy(key_states);
867 signal_remove("irssi init read settings", (SIGNAL_FUNC) read_keyboard_config);
868 signal_remove("setup reread", (SIGNAL_FUNC) read_keyboard_config);
869 signal_remove("complete command bind", (SIGNAL_FUNC) sig_complete_bind);
870 command_unbind("bind", (SIGNAL_FUNC) cmd_bind);