comparison console/libgnt/gntstyle.c @ 15801:682022b8a129

Make sure unbound key-combinations are handled properly.
author Sadrul Habib Chowdhury <imadil@gmail.com>
date Fri, 16 Mar 2007 20:53:24 +0000
parents 78ff267f50e3
children 1c8f1dc50685
comparison
equal deleted inserted replaced
15796:1dee9065e336 15801:682022b8a129
82 } 82 }
83 83
84 static char * 84 static char *
85 parse_key(const char *key) 85 parse_key(const char *key)
86 { 86 {
87 char *ret = NULL; 87 return gnt_key_translate(key);
88 int ctrl = 0, alt = 0;
89 char k;
90
91 /* XXX: Need to do something about ctrl/alt+home, end etc. */
92
93 #define SPECIAL_KEY(k, code) do { \
94 if (strcasecmp(key, k) == 0) \
95 return g_strdup(code); \
96 } while (0)
97
98 SPECIAL_KEY("home", GNT_KEY_HOME);
99 SPECIAL_KEY("end", GNT_KEY_END);
100 SPECIAL_KEY("pageup", GNT_KEY_PGUP);
101 SPECIAL_KEY("pagedown", GNT_KEY_PGDOWN);
102 SPECIAL_KEY("insert", GNT_KEY_INS);
103 SPECIAL_KEY("delete", GNT_KEY_DEL);
104
105 SPECIAL_KEY("left", GNT_KEY_LEFT);
106 SPECIAL_KEY("right", GNT_KEY_RIGHT);
107 SPECIAL_KEY("up", GNT_KEY_UP);
108 SPECIAL_KEY("down", GNT_KEY_DOWN);
109
110 SPECIAL_KEY("tab", "\t");
111 SPECIAL_KEY("menu", GNT_KEY_POPUP);
112
113 SPECIAL_KEY("f1", GNT_KEY_F1);
114 SPECIAL_KEY("f2", GNT_KEY_F2);
115 SPECIAL_KEY("f3", GNT_KEY_F3);
116 SPECIAL_KEY("f4", GNT_KEY_F4);
117 SPECIAL_KEY("f5", GNT_KEY_F5);
118 SPECIAL_KEY("f6", GNT_KEY_F6);
119 SPECIAL_KEY("f7", GNT_KEY_F7);
120 SPECIAL_KEY("f8", GNT_KEY_F8);
121 SPECIAL_KEY("f9", GNT_KEY_F9);
122 SPECIAL_KEY("f10", GNT_KEY_F10);
123 SPECIAL_KEY("f11", GNT_KEY_F11);
124 SPECIAL_KEY("f12", GNT_KEY_F12);
125
126 #undef SPECIAL_KEY
127
128 #define MATCH(string, var) do { \
129 if (strncasecmp(key, string, sizeof(string) - 1) == 0) { \
130 key += sizeof(string) - 1; \
131 var = 1; \
132 } \
133 }while (0)
134
135 MATCH("c-", ctrl);
136 MATCH("ctl-", ctrl);
137 MATCH("ctr-", ctrl);
138 MATCH("ctrl-", ctrl);
139
140 MATCH("alt-", alt);
141 MATCH("a-", alt);
142 MATCH("m-", alt);
143 MATCH("meta-", alt);
144
145 if (strlen(key) != 1) /* We can only have stuff like "ctrl-alt-a" */
146 return NULL;
147
148 if (ctrl && !isalpha(*key)) {
149 /* These keys cannot be used with ctrl */
150 return NULL;
151 }
152
153 if (ctrl)
154 k = *key | 0x20;
155 else
156 k = *key;
157
158 ret = g_strdup_printf("%s%c", alt ? "\033" : "", ctrl ? k - 0x60 : k);
159
160 #undef MATCH
161
162 return ret;
163 } 88 }
164 89
165 void gnt_style_read_actions(GType type, GntBindableClass *klass) 90 void gnt_style_read_actions(GType type, GntBindableClass *klass)
166 { 91 {
167 #if GLIB_CHECK_VERSION(2,6,0) 92 #if GLIB_CHECK_VERSION(2,6,0)
197 g_error_free(error); 122 g_error_free(error);
198 error = NULL; 123 error = NULL;
199 } 124 }
200 else 125 else
201 { 126 {
202 char *keycode = parse_key(key); 127 const char *keycode = parse_key(key);
203 if (keycode == NULL) { 128 if (keycode == NULL) {
204 g_printerr("GntStyle: Invalid key-binding %s\n", key); 129 g_printerr("GntStyle: Invalid key-binding %s\n", key);
205 } else { 130 } else {
206 gnt_bindable_register_binding(klass, action, keycode, NULL); 131 gnt_bindable_register_binding(klass, action, keycode, NULL);
207 g_free(keycode);
208 } 132 }
209 } 133 }
210 g_free(key); 134 g_free(key);
211 g_free(action); 135 g_free(action);
212 } 136 }