diff options
Diffstat (limited to 'src/fe-common/core/keyboard.c')
-rw-r--r-- | src/fe-common/core/keyboard.c | 295 |
1 files changed, 142 insertions, 153 deletions
diff --git a/src/fe-common/core/keyboard.c b/src/fe-common/core/keyboard.c index 0406c758..79f0fbe4 100644 --- a/src/fe-common/core/keyboard.c +++ b/src/fe-common/core/keyboard.c @@ -20,6 +20,7 @@ #include "module.h" #include "signals.h" +#include "commands.h" #include "lib-config/iconfig.h" #include "settings.h" @@ -29,201 +30,192 @@ GSList *keyinfos; static GHashTable *keys; -KEYINFO_REC *key_info_find(gchar *id) +static void keyconfig_save(const char *id, const char *key, const char *data) { - GSList *tmp; + CONFIG_NODE *node; - for (tmp = keyinfos; tmp != NULL; tmp = tmp->next) - { - KEYINFO_REC *rec = tmp->data; + g_return_if_fail(id != NULL); + g_return_if_fail(key != NULL); - if (g_strcasecmp(rec->id, id) == 0) - return rec; - } + /* remove old keyboard settings */ + node = iconfig_node_traverse("keyboard", TRUE); + node = config_node_section(node, id, NODE_TYPE_BLOCK); - return NULL; + iconfig_node_set_str(node, key, data == NULL ? "" : data); } -/* Bind a key for function */ -void key_bind(gchar *id, gchar *data, gchar *description, gchar *key_default, SIGNAL_FUNC func) +static void keyconfig_clear(const char *id, const char *key) { - KEYINFO_REC *info; - KEY_REC *rec; - - g_return_if_fail(id != NULL); - g_return_if_fail(func != NULL); - - /* create key info record */ - info = key_info_find(id); - if (info == NULL) - { - g_return_if_fail(description != NULL); - info = g_new0(KEYINFO_REC, 1); - info->id = g_strdup(id); - info->description = g_strdup(description); - keyinfos = g_slist_append(keyinfos, info); - - /* add the signal */ - id = g_strconcat("key ", id, NULL); - signal_add(id, func); - g_free(id); - - signal_emit("keyinfo created", 1, info); - } - - if (key_default == NULL || *key_default == '\0') - { - /* just create a possible key command, don't bind it to any key yet */ - return; - } - - /* create/replace key record */ - rec = g_hash_table_lookup(keys, key_default); - if (rec != NULL) - { - if (rec->data != NULL) - g_free(rec->data); - } - else - { - rec = g_new0(KEY_REC, 1); - info->keys = g_slist_append(info->keys, rec); - rec->key = g_strdup(key_default); - g_hash_table_insert(keys, rec->key, rec); - } - rec->info = info; - rec->data = data == NULL ? NULL : g_strdup(data); + CONFIG_NODE *node; + + g_return_if_fail(id != NULL); + + /* remove old keyboard settings */ + node = iconfig_node_traverse("keyboard", TRUE); + if (key == NULL) + iconfig_node_set_str(node, id, NULL); + else { + node = config_node_section(node, id, 0); + if (node != NULL) iconfig_node_set_str(node, key, NULL); + } } -static void keyinfo_remove(KEYINFO_REC *info) +KEYINFO_REC *key_info_find(const char *id) { - GSList *tmp; + GSList *tmp; - g_return_if_fail(info != NULL); + for (tmp = keyinfos; tmp != NULL; tmp = tmp->next) { + KEYINFO_REC *rec = tmp->data; - keyinfos = g_slist_remove(keyinfos, info); - signal_emit("keyinfo destroyed", 1, info); + if (g_strcasecmp(rec->id, id) == 0) + return rec; + } - /* destroy all keys */ - for (tmp = info->keys; tmp != NULL; tmp = tmp->next) - { - KEY_REC *rec = tmp->data; + return NULL; +} - g_hash_table_remove(keys, rec->key); - if (rec->data != NULL) g_free(rec->data); - g_free(rec->key); - g_free(rec); - } +/* Bind a key for function */ +void key_bind(const char *id, const char *description, + const char *key_default, const char *data, SIGNAL_FUNC func) +{ + KEYINFO_REC *info; + char *key; + + g_return_if_fail(id != NULL); + g_return_if_fail(func != NULL); + + /* create key info record */ + info = key_info_find(id); + if (info == NULL) { + if (description == NULL) + g_warning("key_bind(%s) should have description!", id); + info = g_new0(KEYINFO_REC, 1); + info->id = g_strdup(id); + info->description = g_strdup(description); + keyinfos = g_slist_append(keyinfos, info); + + /* add the signal */ + key = g_strconcat("key ", id, NULL); + signal_add(key, func); + g_free(key); + + signal_emit("keyinfo created", 1, info); + } - /* destroy key info */ - g_slist_free(info->keys); - g_free(info->description); - g_free(info->id); - g_free(info); + if (key_default != NULL && *key_default != '\0') + key_configure_add(id, key_default, data); } -/* Unbind key */ -void key_unbind(gchar *id, SIGNAL_FUNC func) +static void keyinfo_remove(KEYINFO_REC *info) { - KEYINFO_REC *info; + GSList *tmp; + + g_return_if_fail(info != NULL); - g_return_if_fail(id != NULL); - g_return_if_fail(func != NULL); + keyinfos = g_slist_remove(keyinfos, info); + signal_emit("keyinfo destroyed", 1, info); - /* remove keys */ - info = key_info_find(id); - if (info != NULL) - keyinfo_remove(info); + /* destroy all keys */ + for (tmp = info->keys; tmp != NULL; tmp = tmp->next) { + KEY_REC *rec = tmp->data; - /* remove signal */ - id = g_strconcat("key ", id, NULL); - signal_remove(id, func); - g_free(id); + g_hash_table_remove(keys, rec->key); + g_free_not_null(rec->data); + g_free(rec->key); + g_free(rec); + } + + /* destroy key info */ + g_slist_free(info->keys); + g_free_not_null(info->description); + g_free(info->id); + g_free(info); } -/* Configure new key */ -void key_configure_add(gchar *id, gchar *data, gchar *key) +/* Unbind key */ +void key_unbind(const char *id, SIGNAL_FUNC func) { - KEYINFO_REC *info; - KEY_REC *rec; - - g_return_if_fail(id != NULL); - g_return_if_fail(key != NULL && *key != '\0'); + KEYINFO_REC *info; + char *key; - info = key_info_find(id); - if (info == NULL) - return; + g_return_if_fail(id != NULL); + g_return_if_fail(func != NULL); - rec = g_new0(KEY_REC, 1); - info->keys = g_slist_append(info->keys, rec); + /* remove keys */ + info = key_info_find(id); + if (info != NULL) + keyinfo_remove(info); - rec->info = info; - rec->data = data == NULL ? NULL : g_strdup(data); - rec->key = g_strdup(key); - g_hash_table_insert(keys, rec->key, rec); + /* remove signal */ + key = g_strconcat("key ", id, NULL); + signal_remove(key, func); + g_free(key); } -/* Remove key */ -void key_configure_remove(gchar *key) +/* Configure new key */ +void key_configure_add(const char *id, const char *key, const char *data) { - KEY_REC *rec; + KEYINFO_REC *info; + KEY_REC *rec; + + g_return_if_fail(id != NULL); + g_return_if_fail(key != NULL && *key != '\0'); - g_return_if_fail(key != NULL); + info = key_info_find(id); + if (info == NULL) + return; - rec = g_hash_table_lookup(keys, key); - if (rec == NULL) return; + key_configure_remove(key); - rec->info->keys = g_slist_remove(rec->info->keys, rec); - g_hash_table_remove(keys, key); + rec = g_new0(KEY_REC, 1); + rec->key = g_strdup(key); + rec->info = info; + rec->data = g_strdup(data); + info->keys = g_slist_append(info->keys, rec); + g_hash_table_insert(keys, rec->key, rec); - if (rec->data != NULL) g_free(rec->data); - g_free(rec->key); - g_free(rec); + keyconfig_save(id, key, data); } -gboolean key_pressed(gchar *key, gpointer data) +/* Remove key */ +void key_configure_remove(const char *key) { - KEY_REC *rec; - gboolean ret; - gchar *str; + KEY_REC *rec; - g_return_val_if_fail(key != NULL, FALSE); + g_return_if_fail(key != NULL); - rec = g_hash_table_lookup(keys, key); - if (rec == NULL) return FALSE; + rec = g_hash_table_lookup(keys, key); + if (rec == NULL) return; - str = g_strconcat("key ", rec->info->id, NULL); - ret = signal_emit(str, 3, rec->data, data, rec->info); - g_free(str); + keyconfig_clear(rec->info->id, key); - return ret; + rec->info->keys = g_slist_remove(rec->info->keys, rec); + g_hash_table_remove(keys, key); + + g_free_not_null(rec->data); + g_free(rec->key); + g_free(rec); } -void keyboard_save(void) +int key_pressed(const char *key, void *data) { - CONFIG_NODE *keyboard, *node, *listnode; - GSList *tmp, *tmp2; + KEY_REC *rec; + char *str; + int ret; - /* remove old keyboard settings */ - iconfig_node_set_str(NULL, "(keyboard", NULL); - keyboard = iconfig_node_traverse("(keyboard", TRUE); + g_return_val_if_fail(key != NULL, FALSE); - for (tmp = keyinfos; tmp != NULL; tmp = tmp->next) { - KEYINFO_REC *info = tmp->data; + rec = g_hash_table_lookup(keys, key); + if (rec == NULL) return FALSE; - node = config_node_section(keyboard, info->id, TRUE); - for (tmp2 = info->keys; tmp2 != NULL; tmp2 = tmp2->next) { - KEY_REC *key = tmp2->data; + str = g_strconcat("key ", rec->info->id, NULL); + ret = signal_emit(str, 3, rec->data, data, rec->info); + g_free(str); - listnode = config_node_section(node, NULL, NODE_TYPE_BLOCK); - if (key->data != NULL) - iconfig_node_set_str(listnode, "data", key->data); - iconfig_node_set_str(listnode, "key", key->key); - } - } + return ret; } -static void sig_command(gchar *data) +static void sig_command(const char *data) { signal_emit("send command", 3, data, active_win->active_server, active_win->active); } @@ -231,7 +223,6 @@ static void sig_command(gchar *data) void read_keyinfo(KEYINFO_REC *info, CONFIG_NODE *node) { GSList *tmp; - char *data, *key; g_return_if_fail(info != NULL); g_return_if_fail(node != NULL); @@ -245,10 +236,8 @@ void read_keyinfo(KEYINFO_REC *info, CONFIG_NODE *node) for (tmp = node->value; tmp != NULL; tmp = tmp->next) { node = tmp->data; - data = config_node_get_str(node->value, "data", NULL); - key = config_node_get_str(node->value, "key", NULL); - - if (key != NULL) key_configure_add(info->id, data, key); + if (node->key != NULL) + key_configure_add(info->id, node->value, node->key); } } @@ -268,7 +257,7 @@ static void read_keyboard_config(void) continue; info = key_info_find(node->key); - if (info != NULL) read_keyinfo(info, node->value); + if (info != NULL) read_keyinfo(info, node); } } @@ -277,10 +266,10 @@ void keyboard_init(void) keys = g_hash_table_new((GHashFunc) g_str_hash, (GCompareFunc) g_str_equal); keyinfos = NULL; - key_bind("command", NULL, "Run any IRC command", NULL, (SIGNAL_FUNC) sig_command); + key_bind("command", "Run any IRC command", NULL, NULL, (SIGNAL_FUNC) sig_command); read_keyboard_config(); - signal_add("setup reread", (SIGNAL_FUNC) read_keyboard_config); + signal_add("setup reread", (SIGNAL_FUNC) read_keyboard_config); } void keyboard_deinit(void) |