Add missing AUTHORS and ChangeLog.
[gnt.git] / gntkeys.c
blobc93601ab3ab7a13fd1dcb63008b4615b64a550c8
1 /**
2 * GNT - The GLib Ncurses Toolkit
4 * GNT is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
6 * source distribution.
8 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
23 #include "gntkeys.h"
25 #include <glib.h>
26 #include <stdlib.h>
27 #include <string.h>
29 char *gnt_key_cup;
30 char *gnt_key_cdown;
31 char *gnt_key_cleft;
32 char *gnt_key_cright;
34 static const char *term;
35 static GHashTable *specials;
37 void gnt_init_keys()
39 const char *controls[] = {"", "c-", "ctrl-", "ctr-", "ctl-", NULL};
40 const char *alts[] = {"", "alt-", "a-", "m-", "meta-", NULL};
41 int c, a, ch;
42 char key[32];
44 if (term == NULL) {
45 term = getenv("TERM");
46 if (!term)
47 term = ""; /* Just in case */
50 if (strcmp(term, "xterm") == 0 || strcmp(term, "rxvt") == 0) {
51 gnt_key_cup = "\033" "[1;5A";
52 gnt_key_cdown = "\033" "[1;5B";
53 gnt_key_cright = "\033" "[1;5C";
54 gnt_key_cleft = "\033" "[1;5D";
55 } else if (strcmp(term, "screen") == 0 || strcmp(term, "rxvt-unicode") == 0) {
56 gnt_key_cup = "\033" "Oa";
57 gnt_key_cdown = "\033" "Ob";
58 gnt_key_cright = "\033" "Oc";
59 gnt_key_cleft = "\033" "Od";
62 specials = g_hash_table_new(g_str_hash, g_str_equal);
64 #define INSERT_KEY(k, code) do { \
65 g_hash_table_insert(specials, g_strdup(k), g_strdup(code)); \
66 gnt_keys_add_combination(code); \
67 } while (0)
69 INSERT_KEY("home", GNT_KEY_HOME);
70 INSERT_KEY("end", GNT_KEY_END);
71 INSERT_KEY("pageup", GNT_KEY_PGUP);
72 INSERT_KEY("pagedown", GNT_KEY_PGDOWN);
73 INSERT_KEY("insert", GNT_KEY_INS);
74 INSERT_KEY("delete", GNT_KEY_DEL);
75 INSERT_KEY("back_tab", GNT_KEY_BACK_TAB);
77 INSERT_KEY("left", GNT_KEY_LEFT);
78 INSERT_KEY("right", GNT_KEY_RIGHT);
79 INSERT_KEY("up", GNT_KEY_UP);
80 INSERT_KEY("down", GNT_KEY_DOWN);
82 INSERT_KEY("tab", "\t");
83 INSERT_KEY("menu", GNT_KEY_POPUP);
85 INSERT_KEY("f1", GNT_KEY_F1);
86 INSERT_KEY("f2", GNT_KEY_F2);
87 INSERT_KEY("f3", GNT_KEY_F3);
88 INSERT_KEY("f4", GNT_KEY_F4);
89 INSERT_KEY("f5", GNT_KEY_F5);
90 INSERT_KEY("f6", GNT_KEY_F6);
91 INSERT_KEY("f7", GNT_KEY_F7);
92 INSERT_KEY("f8", GNT_KEY_F8);
93 INSERT_KEY("f9", GNT_KEY_F9);
94 INSERT_KEY("f10", GNT_KEY_F10);
95 INSERT_KEY("f11", GNT_KEY_F11);
96 INSERT_KEY("f12", GNT_KEY_F12);
98 #define REM_LENGTH (sizeof(key) - (cur - key))
99 #define INSERT_COMB(k, code) do { \
100 snprintf(key, sizeof(key), "%s%s%s", controls[c], alts[a], k); \
101 INSERT_KEY(key, code); \
102 } while (0);
104 /* Lower-case alphabets */
105 for (a = 0, c = 0; controls[c]; c++, a = 0) {
106 if (c) {
107 INSERT_COMB("up", gnt_key_cup);
108 INSERT_COMB("down", gnt_key_cdown);
109 INSERT_COMB("left", gnt_key_cleft);
110 INSERT_COMB("right", gnt_key_cright);
113 for (a = 0; alts[a]; a++) {
114 for (ch = 0; ch < 26; ch++) {
115 char str[2] = {'a' + ch, 0}, code[4] = "\0\0\0\0";
116 int ind = 0;
117 if (a)
118 code[ind++] = '\033';
119 code[ind] = (c ? 1 : 'a') + ch;
120 INSERT_COMB(str, code);
124 c = 0;
125 for (a = 0; alts[a]; a++) {
126 /* Upper-case alphabets */
127 for (ch = 0; ch < 26; ch++) {
128 char str[2] = {'A' + ch, 0}, code[] = {'\033', 'A' + ch, 0};
129 INSERT_COMB(str, code);
131 /* Digits */
132 for (ch = 0; ch < 10; ch++) {
133 char str[2] = {'0' + ch, 0}, code[] = {'\033', '0' + ch, 0};
134 INSERT_COMB(str, code);
139 void gnt_keys_refine(char *text)
141 if (*text == 27 && *(text + 1) == '[' &&
142 (*(text + 2) >= 'A' && *(text + 2) <= 'D')) {
143 /* Apparently this is necessary for urxvt and screen and xterm */
144 if (strcmp(term, "screen") == 0 || strcmp(term, "rxvt-unicode") == 0 ||
145 strcmp(term, "xterm") == 0)
146 *(text + 1) = 'O';
147 } else if (*(unsigned char*)text == 195) {
148 if (*(text + 2) == 0 && strcmp(term, "xterm") == 0) {
149 *(text) = 27;
150 *(text + 1) -= 64; /* Say wha? */
155 const char *gnt_key_translate(const char *name)
157 return name ? g_hash_table_lookup(specials, name) : NULL;
160 typedef struct {
161 const char *name;
162 const char *key;
163 } gntkey;
165 static void
166 get_key_name(gpointer key, gpointer value, gpointer data)
168 gntkey *k = data;
169 if (k->name)
170 return;
171 if (g_utf8_collate(value, k->key) == 0)
172 k->name = key;
175 const char *gnt_key_lookup(const char *key)
177 gntkey k = {NULL, key};
178 g_hash_table_foreach(specials, get_key_name, &k);
179 return k.name;
183 * The key-bindings will be saved in a tree. When a keystroke happens, GNT will
184 * find the sequence that matches a binding and return the length.
185 * A sequence should not be a prefix of another sequence. If it is, then only
186 * the shortest one will be processed. If we want to change that, we will need
187 * to allow getting the k-th prefix that matches the input, and pay attention
188 * to the return value of gnt_wm_process_input in gntmain.c.
190 #define SIZE 256
192 #define IS_END 1 << 0
193 struct _node
195 struct _node *next[SIZE];
196 int ref;
197 int flags;
200 static struct _node root = {.ref = 1, .flags = 0};
202 static void add_path(struct _node *node, const char *path)
204 struct _node *n = NULL;
205 if (!path || !*path) {
206 node->flags |= IS_END;
207 return;
209 while (*path && node->next[(unsigned char)*path]) {
210 node = node->next[(unsigned char)*path];
211 node->ref++;
212 path++;
214 if (!*path)
215 return;
216 n = g_new0(struct _node, 1);
217 n->ref = 1;
218 node->next[(unsigned char)*path++] = n;
219 add_path(n, path);
222 void gnt_keys_add_combination(const char *path)
224 add_path(&root, path);
227 static void del_path(struct _node *node, const char *path)
229 struct _node *next = NULL;
231 if (!*path)
232 return;
233 next = node->next[(unsigned char)*path];
234 if (!next)
235 return;
236 del_path(next, path + 1);
237 next->ref--;
238 if (next->ref == 0) {
239 node->next[(unsigned char)*path] = NULL;
240 g_free(next);
244 void gnt_keys_del_combination(const char *path)
246 del_path(&root, path);
249 int gnt_keys_find_combination(const char *path)
251 int depth = 0;
252 struct _node *n = &root;
254 root.flags &= ~IS_END;
255 while (*path && n->next[(unsigned char)*path] && !(n->flags & IS_END)) {
256 if (!g_ascii_isspace(*path) &&
257 !g_ascii_iscntrl(*path) &&
258 !g_ascii_isgraph(*path))
259 return 0;
260 n = n->next[(unsigned char)*path++];
261 depth++;
264 if (!(n->flags & IS_END))
265 depth = 0;
266 return depth;
269 static void
270 print_path(struct _node *node, int depth)
272 int i;
273 for (i = 0; i < SIZE; i++) {
274 if (node->next[i]) {
275 g_printerr("%*c (%d:%d)\n", depth * 4, i, node->next[i]->ref,
276 node->next[i]->flags);
277 print_path(node->next[i], depth + 1);
282 /* this is purely for debugging purposes. */
283 void gnt_keys_print_combinations(void);
284 void gnt_keys_print_combinations()
286 g_printerr("--------\n");
287 print_path(&root, 1);
288 g_printerr("--------\n");