2000-04-26 04:03:38 -04:00
|
|
|
/*
|
|
|
|
keyboard.c : irssi
|
|
|
|
|
|
|
|
Copyright (C) 1999 Timo Sirainen
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "module.h"
|
2000-07-08 18:31:11 -04:00
|
|
|
#include "module-formats.h"
|
2000-04-26 04:03:38 -04:00
|
|
|
#include "signals.h"
|
2000-07-08 17:47:05 -04:00
|
|
|
#include "commands.h"
|
2000-07-08 18:31:11 -04:00
|
|
|
#include "levels.h"
|
2000-07-22 18:03:37 -04:00
|
|
|
#include "misc.h"
|
2000-04-26 04:03:38 -04:00
|
|
|
#include "lib-config/iconfig.h"
|
|
|
|
#include "settings.h"
|
|
|
|
|
|
|
|
#include "keyboard.h"
|
|
|
|
#include "windows.h"
|
|
|
|
|
|
|
|
GSList *keyinfos;
|
|
|
|
static GHashTable *keys;
|
|
|
|
|
2000-07-08 17:47:05 -04:00
|
|
|
static void keyconfig_save(const char *id, const char *key, const char *data)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2000-07-08 17:47:05 -04:00
|
|
|
CONFIG_NODE *node;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-08 17:47:05 -04:00
|
|
|
g_return_if_fail(id != NULL);
|
|
|
|
g_return_if_fail(key != NULL);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-08 17:47:05 -04:00
|
|
|
/* remove old keyboard settings */
|
|
|
|
node = iconfig_node_traverse("keyboard", TRUE);
|
|
|
|
node = config_node_section(node, id, NODE_TYPE_BLOCK);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-08 17:47:05 -04:00
|
|
|
iconfig_node_set_str(node, key, data == NULL ? "" : data);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-07-08 17:47:05 -04:00
|
|
|
static void keyconfig_clear(const char *id, const char *key)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2000-07-08 17:47:05 -04:00
|
|
|
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 {
|
2000-07-08 18:31:11 -04:00
|
|
|
node = config_node_section(node, id, -1);
|
2000-07-08 17:47:05 -04:00
|
|
|
if (node != NULL) iconfig_node_set_str(node, key, NULL);
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-07-08 17:47:05 -04:00
|
|
|
KEYINFO_REC *key_info_find(const char *id)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2000-07-08 17:47:05 -04:00
|
|
|
GSList *tmp;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-08 17:47:05 -04:00
|
|
|
for (tmp = keyinfos; tmp != NULL; tmp = tmp->next) {
|
|
|
|
KEYINFO_REC *rec = tmp->data;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-08 17:47:05 -04:00
|
|
|
if (g_strcasecmp(rec->id, id) == 0)
|
|
|
|
return rec;
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-08 17:47:05 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-08 17:47:05 -04:00
|
|
|
/* 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);
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-08 17:47:05 -04:00
|
|
|
if (key_default != NULL && *key_default != '\0')
|
|
|
|
key_configure_add(id, key_default, data);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-07-08 17:47:05 -04:00
|
|
|
static void keyinfo_remove(KEYINFO_REC *info)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2000-07-08 17:47:05 -04:00
|
|
|
GSList *tmp;
|
|
|
|
|
|
|
|
g_return_if_fail(info != NULL);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-08 17:47:05 -04:00
|
|
|
keyinfos = g_slist_remove(keyinfos, info);
|
|
|
|
signal_emit("keyinfo destroyed", 1, info);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-08 17:47:05 -04:00
|
|
|
/* destroy all keys */
|
|
|
|
for (tmp = info->keys; tmp != NULL; tmp = tmp->next) {
|
|
|
|
KEY_REC *rec = tmp->data;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-08 17:47:05 -04:00
|
|
|
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);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-07-08 17:47:05 -04:00
|
|
|
/* Unbind key */
|
|
|
|
void key_unbind(const char *id, SIGNAL_FUNC func)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2000-07-08 17:47:05 -04:00
|
|
|
KEYINFO_REC *info;
|
|
|
|
char *key;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-08 17:47:05 -04:00
|
|
|
g_return_if_fail(id != NULL);
|
|
|
|
g_return_if_fail(func != NULL);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-08 17:47:05 -04:00
|
|
|
/* remove keys */
|
|
|
|
info = key_info_find(id);
|
|
|
|
if (info != NULL)
|
|
|
|
keyinfo_remove(info);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-08 17:47:05 -04:00
|
|
|
/* remove signal */
|
|
|
|
key = g_strconcat("key ", id, NULL);
|
|
|
|
signal_remove(key, func);
|
|
|
|
g_free(key);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-07-08 17:47:05 -04:00
|
|
|
/* Configure new key */
|
2000-07-21 19:46:24 -04:00
|
|
|
static void key_configure_create(const char *id, const char *key,
|
|
|
|
const char *data)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2000-07-08 17:47:05 -04:00
|
|
|
KEYINFO_REC *info;
|
|
|
|
KEY_REC *rec;
|
|
|
|
|
|
|
|
g_return_if_fail(id != NULL);
|
|
|
|
g_return_if_fail(key != NULL && *key != '\0');
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-08 17:47:05 -04:00
|
|
|
info = key_info_find(id);
|
|
|
|
if (info == NULL)
|
|
|
|
return;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-08 17:47:05 -04:00
|
|
|
key_configure_remove(key);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-08 17:47:05 -04:00
|
|
|
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);
|
2000-07-21 19:46:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Configure new key */
|
|
|
|
void key_configure_add(const char *id, const char *key, const char *data)
|
|
|
|
{
|
|
|
|
g_return_if_fail(id != NULL);
|
|
|
|
g_return_if_fail(key != NULL && *key != '\0');
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-21 19:46:24 -04:00
|
|
|
key_configure_create(id, key, data);
|
|
|
|
keyconfig_save(id, key, data);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-07-16 12:35:37 -04:00
|
|
|
static void key_configure_destroy(KEY_REC *rec)
|
|
|
|
{
|
|
|
|
g_return_if_fail(rec != NULL);
|
|
|
|
|
|
|
|
rec->info->keys = g_slist_remove(rec->info->keys, rec);
|
|
|
|
g_hash_table_remove(keys, rec->key);
|
|
|
|
|
|
|
|
g_free_not_null(rec->data);
|
|
|
|
g_free(rec->key);
|
|
|
|
g_free(rec);
|
|
|
|
}
|
|
|
|
|
2000-07-08 17:47:05 -04:00
|
|
|
/* Remove key */
|
|
|
|
void key_configure_remove(const char *key)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2000-07-08 17:47:05 -04:00
|
|
|
KEY_REC *rec;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-08 17:47:05 -04:00
|
|
|
g_return_if_fail(key != NULL);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-08 17:47:05 -04:00
|
|
|
rec = g_hash_table_lookup(keys, key);
|
|
|
|
if (rec == NULL) return;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-08 17:47:05 -04:00
|
|
|
keyconfig_clear(rec->info->id, key);
|
2000-07-16 12:35:37 -04:00
|
|
|
key_configure_destroy(rec);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-07-08 17:47:05 -04:00
|
|
|
int key_pressed(const char *key, void *data)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2000-07-08 17:47:05 -04:00
|
|
|
KEY_REC *rec;
|
|
|
|
char *str;
|
|
|
|
int ret;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-08 17:47:05 -04:00
|
|
|
g_return_val_if_fail(key != NULL, FALSE);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-08 17:47:05 -04:00
|
|
|
rec = g_hash_table_lookup(keys, key);
|
|
|
|
if (rec == NULL) return FALSE;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-08 17:47:05 -04:00
|
|
|
str = g_strconcat("key ", rec->info->id, NULL);
|
|
|
|
ret = signal_emit(str, 3, rec->data, data, rec->info);
|
|
|
|
g_free(str);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-08 17:47:05 -04:00
|
|
|
return ret;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-07-08 17:47:05 -04:00
|
|
|
static void sig_command(const char *data)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2000-09-06 15:16:02 -04:00
|
|
|
const char *cmdchars;
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
cmdchars = settings_get_str("cmdchars");
|
|
|
|
str = strchr(cmdchars, *data) != NULL ? g_strdup(data) :
|
|
|
|
g_strdup_printf("%c%s", *cmdchars, data);
|
|
|
|
|
|
|
|
signal_emit("send command", 3, str, active_win->active_server, active_win->active);
|
|
|
|
|
|
|
|
g_free(str);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void read_keyinfo(KEYINFO_REC *info, CONFIG_NODE *node)
|
|
|
|
{
|
|
|
|
GSList *tmp;
|
|
|
|
|
|
|
|
g_return_if_fail(info != NULL);
|
|
|
|
g_return_if_fail(node != NULL);
|
|
|
|
g_return_if_fail(is_node_list(node));
|
|
|
|
|
|
|
|
/* remove all old keys */
|
|
|
|
while (info->keys != NULL)
|
2000-07-16 12:35:37 -04:00
|
|
|
key_configure_destroy(info->keys->data);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
/* add the new keys */
|
|
|
|
for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
|
|
|
|
node = tmp->data;
|
|
|
|
|
2000-07-08 17:47:05 -04:00
|
|
|
if (node->key != NULL)
|
2000-07-21 19:46:24 -04:00
|
|
|
key_configure_create(info->id, node->key, node->value);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void read_keyboard_config(void)
|
|
|
|
{
|
|
|
|
KEYINFO_REC *info;
|
|
|
|
CONFIG_NODE *node;
|
|
|
|
GSList *tmp;
|
|
|
|
|
|
|
|
node = iconfig_node_traverse("keyboard", FALSE);
|
|
|
|
if (node == NULL) return;
|
|
|
|
|
|
|
|
for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
|
|
|
|
node = tmp->data;
|
|
|
|
|
|
|
|
if (node->key == NULL || node->value == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
info = key_info_find(node->key);
|
2000-07-08 17:47:05 -04:00
|
|
|
if (info != NULL) read_keyinfo(info, node);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-07-22 20:38:46 -04:00
|
|
|
static void cmd_show_keys(const char *searchkey, int full)
|
2000-07-08 18:31:11 -04:00
|
|
|
{
|
|
|
|
GSList *info, *key;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = searchkey == NULL ? 0 : strlen(searchkey);
|
|
|
|
for (info = keyinfos; info != NULL; info = info->next) {
|
|
|
|
KEYINFO_REC *rec = info->data;
|
|
|
|
|
|
|
|
for (key = rec->keys; key != NULL; key = key->next) {
|
|
|
|
KEY_REC *rec = key->data;
|
|
|
|
|
2000-07-22 20:38:46 -04:00
|
|
|
if ((len == 0 || g_strncasecmp(rec->key, searchkey, len) == 0) &&
|
|
|
|
(!full || rec->key[len] == '\0')) {
|
2000-07-08 18:31:11 -04:00
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, IRCTXT_BIND_KEY,
|
|
|
|
rec->key, rec->info->id, rec->data == NULL ? "" : rec->data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-07-23 19:19:22 -04:00
|
|
|
/* SYNTAX: BIND [<key> [<command> [<data>]]] */
|
2000-07-08 18:31:11 -04:00
|
|
|
static void cmd_bind(const char *data)
|
|
|
|
{
|
|
|
|
GHashTable *optlist;
|
|
|
|
char *key, *id, *keydata;
|
|
|
|
void *free_arg;
|
|
|
|
|
|
|
|
if (!cmd_get_params(data, &free_arg, 3 | PARAM_FLAG_GETREST | PARAM_FLAG_OPTIONS,
|
|
|
|
"bind", &optlist, &key, &id, &keydata))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (*key != '\0' && g_hash_table_lookup(optlist, "delete")) {
|
|
|
|
key_configure_remove(key);
|
|
|
|
} else if (*id == '\0') {
|
|
|
|
/* show some/all keys */
|
2000-07-22 20:38:46 -04:00
|
|
|
cmd_show_keys(key, FALSE);
|
2000-07-08 18:31:11 -04:00
|
|
|
} else if (key_info_find(id) == NULL)
|
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, IRCTXT_BIND_UNKNOWN_ID, id);
|
|
|
|
else {
|
|
|
|
key_configure_add(id, key, keydata);
|
2000-07-22 20:38:46 -04:00
|
|
|
cmd_show_keys(key, TRUE);
|
2000-07-08 18:31:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd_params_free(free_arg);
|
|
|
|
}
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
void keyboard_init(void)
|
|
|
|
{
|
2000-07-22 18:03:37 -04:00
|
|
|
keys = g_hash_table_new((GHashFunc) g_istr_hash, (GCompareFunc) g_istr_equal);
|
2000-05-15 04:25:45 -04:00
|
|
|
keyinfos = NULL;
|
|
|
|
|
2000-07-08 17:47:05 -04:00
|
|
|
key_bind("command", "Run any IRC command", NULL, NULL, (SIGNAL_FUNC) sig_command);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
read_keyboard_config();
|
2000-07-08 17:47:05 -04:00
|
|
|
signal_add("setup reread", (SIGNAL_FUNC) read_keyboard_config);
|
2000-07-08 18:31:11 -04:00
|
|
|
|
|
|
|
command_bind("bind", NULL, (SIGNAL_FUNC) cmd_bind);
|
|
|
|
command_set_options("bind", "delete");
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void keyboard_deinit(void)
|
|
|
|
{
|
|
|
|
while (keyinfos != NULL)
|
|
|
|
keyinfo_remove(keyinfos->data);
|
|
|
|
g_hash_table_destroy(keys);
|
|
|
|
|
|
|
|
signal_remove("setup reread", (SIGNAL_FUNC) read_keyboard_config);
|
2000-07-08 18:31:11 -04:00
|
|
|
command_unbind("bind", (SIGNAL_FUNC) cmd_bind);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|