ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/keyboard.C
Revision: 1.10
Committed: Thu Feb 3 10:24:10 2005 UTC (19 years, 3 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-4_9
Changes since 1.8: +24 -25 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #include "../config.h"
2     #include "rxvt.h"
3 root 1.7
4     #ifdef KEYSYM_RESOURCE
5    
6     #include <cstring>
7    
8 root 1.1 #include "keyboard.h"
9     #include "command.h"
10    
11     ////////////////////////////////////////////////////////////////////////////////
12     // default keycode translation map and keyevent handlers
13    
14 root 1.2 keysym_t keyboard_manager::stock_keymap[] = {
15 root 1.1 /* examples */
16 root 1.10 /* keysym, state, range, handler, str */
17     //{XK_ISO_Left_Tab, 0, 1, keysym_t::NORMAL, "\033[Z"},
18     //{ 'a', 0, 26, keysym_t::RANGE_META8, "a" "%c"},
19     //{ 'a', ControlMask, 26, keysym_t::RANGE_META8, "" "%c"},
20     //{ XK_Left, 0, 4, keysym_t::LIST, ".\033[.DACB."},
21     //{ XK_Left, ShiftMask, 4, keysym_t::LIST, ".\033[.dacb."},
22     //{ XK_Left, ControlMask, 4, keysym_t::LIST, ".\033O.dacb."},
23     //{ XK_Tab, ControlMask, 1, keysym_t::NORMAL, "\033<C-Tab>"},
24     //{ XK_apostrophe, ControlMask, 1, keysym_t::NORMAL, "\033<C-'>"},
25     //{ XK_slash, ControlMask, 1, keysym_t::NORMAL, "\033<C-/>"},
26     //{ XK_semicolon, ControlMask, 1, keysym_t::NORMAL, "\033<C-;>"},
27     //{ XK_grave, ControlMask, 1, keysym_t::NORMAL, "\033<C-`>"},
28     //{ XK_comma, ControlMask, 1, keysym_t::NORMAL, "\033<C-\054>"},
29     //{ XK_Return, ControlMask, 1, keysym_t::NORMAL, "\033<C-Return>"},
30     //{ XK_Return, ShiftMask, 1, keysym_t::NORMAL, "\033<S-Return>"},
31     //{ ' ', ShiftMask, 1, keysym_t::NORMAL, "\033<S-Space>"},
32     //{ '.', ControlMask, 1, keysym_t::NORMAL, "\033<C-.>"},
33     //{ '0', ControlMask, 10, keysym_t::RANGE, "0" "\033<C-%c>"},
34     //{ '0', MetaMask|ControlMask, 10, keysym_t::RANGE, "0" "\033<M-C-%c>"},
35     //{ 'a', MetaMask|ControlMask, 26, keysym_t::RANGE, "a" "\033<M-C-%c>"},
36 root 1.1 };
37    
38 root 1.2 static void
39     output_string (rxvt_term *rt, const char *str)
40 root 1.1 {
41 root 1.10 if (strncmp (str, "command:", 8) == 0)
42     rt->cmd_write ((unsigned char *)str + 8, strlen (str) - 8);
43 root 1.1 else
44 root 1.2 rt->tt_write ((unsigned char *)str, strlen (str));
45 root 1.1 }
46    
47 root 1.2 static void
48     output_string_meta8 (rxvt_term *rt, unsigned int state, char *buf, int buflen)
49 root 1.1 {
50     if (state & rt->ModMetaMask)
51     {
52     #ifdef META8_OPTION
53 root 1.2 if (rt->meta_char == 0x80) /* set 8-bit on */
54 root 1.1 {
55     for (char *ch = buf; ch < buf + buflen; ch++)
56     *ch |= 0x80;
57     }
58 root 1.2 else if (rt->meta_char == C0_ESC) /* escape prefix */
59 root 1.1 #endif
60     {
61 root 1.5 const unsigned char ch = C0_ESC;
62 root 1.1 rt->tt_write (&ch, 1);
63     }
64     }
65    
66 root 1.2 rt->tt_write ((unsigned char *) buf, buflen);
67 root 1.1 }
68    
69 root 1.2 static int
70     format_keyrange_string (const char *str, int keysym_offset, char *buf, int bufsize)
71 root 1.1 {
72 root 1.7 size_t len = snprintf (buf, bufsize, str + 1, keysym_offset + str [0]);
73 root 1.1
74 root 1.7 if (len >= (size_t)bufsize)
75 root 1.1 {
76 root 1.7 rxvt_warn ("format_keyrange_string: formatting failed, ignoring key.\n");
77 root 1.5 *buf = 0;
78 root 1.1 }
79    
80     return len;
81     }
82    
83     ////////////////////////////////////////////////////////////////////////////////
84     // return: #bits of '1'
85 root 1.7 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 3)
86     # define bitcount(n) (__extension__ ({ uint32_t n__ = (n); __builtin_popcount (n); }))
87     #else
88 root 1.2 static int
89 root 1.7 bitcount (uint16_t n)
90 root 1.1 {
91     int i;
92 root 1.2
93 root 1.7 for (i = 0; n; ++i, n &= n - 1)
94 root 1.2 ;
95    
96 root 1.1 return i;
97     }
98 root 1.7 #endif
99 root 1.1
100     // return: priority_of_a - priority_of_b
101 root 1.2 static int
102 root 1.1 compare_priority (keysym_t *a, keysym_t *b)
103     {
104     // (the more '1's in state; the less range): the greater priority
105 root 1.2 int ca = bitcount (a->state /* & OtherModMask */);
106     int cb = bitcount (b->state /* & OtherModMask */);
107    
108 root 1.1 if (ca != cb)
109     return ca - cb;
110     //else if (a->state != b->state) // this behavior is to be disscussed
111     // return b->state - a->state;
112     else
113     return b->range - a->range;
114     }
115    
116     ////////////////////////////////////////////////////////////////////////////////
117 root 1.2 keyboard_manager::keyboard_manager ()
118 root 1.1 {
119 root 1.2 keymap.reserve (256);
120 root 1.4 hash [0] = 1; // hash[0] != 0 indicates uninitialized data
121 root 1.1 }
122    
123     keyboard_manager::~keyboard_manager ()
124     {
125     clear ();
126     }
127    
128     void
129     keyboard_manager::clear ()
130     {
131 root 1.2 keymap.clear ();
132     hash [0] = 2;
133 root 1.1
134 root 1.2 for (unsigned int i = 0; i < user_translations.size (); ++i)
135 root 1.1 {
136 root 1.2 free ((void *)user_translations [i]);
137     user_translations [i] = 0;
138 root 1.1 }
139    
140 root 1.2 for (unsigned int i = 0; i < user_keymap.size (); ++i)
141 root 1.1 {
142 root 1.2 delete user_keymap [i];
143     user_keymap [i] = 0;
144 root 1.1 }
145    
146 root 1.2 user_keymap.clear ();
147     user_translations.clear ();
148 root 1.1 }
149    
150     // a wrapper for register_keymap,
151     // so that outside codes don't have to know so much details.
152     //
153     // the string 'trans' is copied to an internal managed buffer,
154     // so the caller can free memory of 'trans' at any time.
155     void
156 root 1.2 keyboard_manager::register_user_translation (KeySym keysym, unsigned int state, const char *trans)
157 root 1.1 {
158     keysym_t *key = new keysym_t;
159 root 1.2 wchar_t *wc = rxvt_mbstowcs (trans);
160     const char *translation = rxvt_wcstoutf8 (wc);
161     free (wc);
162 root 1.1
163 root 1.2 if (key && translation)
164 root 1.1 {
165     key->keysym = keysym;
166 root 1.2 key->state = state;
167     key->range = 1;
168     key->str = translation;
169     key->type = keysym_t::NORMAL;
170    
171     if (strncmp (translation, "list", 4) == 0 && translation [4])
172     {
173     char *middle = strchr (translation + 5, translation [4]);
174     char *suffix = strrchr (translation + 5, translation [4]);
175    
176     if (suffix && middle && suffix > middle + 1)
177     {
178     key->type = keysym_t::LIST;
179     key->range = suffix - middle - 1;
180 root 1.1
181 root 1.2 strcpy (translation, translation + 4);
182     }
183     else
184 root 1.7 rxvt_warn ("cannot parse list-type keysym '%s', treating as normal keysym.\n", translation);
185 root 1.1 }
186    
187 root 1.2 user_keymap.push_back (key);
188     user_translations.push_back (translation);
189 root 1.1 register_keymap (key);
190     }
191     else
192     {
193     delete key;
194 root 1.2 free ((void *)translation);
195 root 1.1 rxvt_fatal ("out of memory, aborting.\n");
196     }
197     }
198    
199     void
200     keyboard_manager::register_keymap (keysym_t *key)
201     {
202 root 1.2 if (keymap.size () == keymap.capacity ())
203     keymap.reserve (keymap.size () * 2);
204 root 1.1
205 root 1.2 keymap.push_back (key);
206     hash[0] = 3;
207 root 1.1 }
208    
209     void
210     keyboard_manager::register_done ()
211     {
212 root 1.2 unsigned int i, n = sizeof (stock_keymap) / sizeof (keysym_t);
213 root 1.1
214 root 1.2 if (keymap.back () != &stock_keymap[n - 1])
215     for (i = 0; i < n; ++i)
216     register_keymap (&stock_keymap[i]);
217 root 1.1
218     purge_duplicate_keymap ();
219    
220     setup_hash ();
221     }
222    
223 root 1.2 bool
224     keyboard_manager::dispatch (rxvt_term *term, KeySym keysym, unsigned int state)
225 root 1.1 {
226 root 1.2 assert (hash[0] == 0 && "register_done() need to be called");
227 root 1.1
228 root 1.6 if (state & term->ModMetaMask) state |= MetaMask;
229     if (state & term->ModNumLockMask) state |= NumLockMask;
230     if (state & term->ModLevel3Mask) state |= Level3Mask;
231 root 1.3
232     if (!!(term->priv_modes & PrivMode_aplKP) != !!(state & ShiftMask))
233     state |= AppKeypadMask;
234    
235 root 1.1 int index = find_keysym (keysym, state);
236    
237     if (index >= 0)
238     {
239 root 1.2 const keysym_t &key = *keymap [index];
240    
241     int keysym_offset = keysym - key.keysym;
242    
243     wchar_t *wc = rxvt_utf8towcs (key.str);
244 root 1.7
245 root 1.2 char *str = rxvt_wcstombs (wc);
246     // TODO: do translations, unescaping etc, here (allow \u escape etc.)
247     free (wc);
248    
249     switch (key.type)
250     {
251     case keysym_t::NORMAL:
252     output_string (term, str);
253     break;
254    
255     case keysym_t::RANGE:
256     {
257     char buf[STRING_MAX];
258    
259     if (format_keyrange_string (str, keysym_offset, buf, sizeof (buf)) > 0)
260     output_string (term, buf);
261     }
262     break;
263    
264     case keysym_t::RANGE_META8:
265     {
266     int len;
267     char buf[STRING_MAX];
268    
269     len = format_keyrange_string (str, keysym_offset, buf, sizeof (buf));
270     if (len > 0)
271     output_string_meta8 (term, state, buf, len);
272     }
273     break;
274    
275     case keysym_t::LIST:
276     {
277     char buf[STRING_MAX];
278    
279     char *prefix, *middle, *suffix;
280    
281     prefix = str;
282     middle = strchr (prefix + 1, *prefix);
283     suffix = strrchr (middle + 1, *prefix);
284    
285     memcpy (buf, prefix + 1, middle - prefix - 1);
286     buf [middle - prefix - 1] = middle [keysym_offset + 1];
287     strcpy (buf + (middle - prefix), suffix + 1);
288    
289     output_string (term, buf);
290     }
291     break;
292     }
293    
294     free (str);
295    
296 root 1.1 return true;
297     }
298     else
299 root 1.2 {
300     // fprintf(stderr,"[%x:%x]",state,keysym);
301     return false;
302     }
303 root 1.1 }
304    
305 root 1.2 // purge duplicate keymap entries
306     void keyboard_manager::purge_duplicate_keymap ()
307 root 1.1 {
308 root 1.2 for (unsigned int i = 0; i < keymap.size (); ++i)
309 root 1.1 {
310     for (unsigned int j = 0; j < i; ++j)
311     {
312 root 1.4 if (keymap [i] == keymap [j])
313 root 1.1 {
314 root 1.4 while (keymap [i] == keymap.back ())
315 root 1.2 keymap.pop_back ();
316    
317     if (i < keymap.size ())
318 root 1.1 {
319 root 1.2 keymap[i] = keymap.back ();
320     keymap.pop_back ();
321 root 1.1 }
322     break;
323     }
324     }
325     }
326     }
327    
328     void
329     keyboard_manager::setup_hash ()
330     {
331     unsigned int i, index, hashkey;
332 root 1.2 vector <keysym_t *> sorted_keymap;
333     uint16_t hash_budget_size[KEYSYM_HASH_BUDGETS]; // size of each budget
334     uint16_t hash_budget_counter[KEYSYM_HASH_BUDGETS]; // #elements in each budget
335 root 1.1
336     memset (hash_budget_size, 0, sizeof (hash_budget_size));
337     memset (hash_budget_counter, 0, sizeof (hash_budget_counter));
338    
339     // count keysyms for corresponding hash budgets
340 root 1.2 for (i = 0; i < keymap.size (); ++i)
341 root 1.1 {
342 root 1.8 hashkey = keymap [i]->keysym & KEYSYM_HASH_MASK;
343 root 1.4 ++hash_budget_size [hashkey];
344 root 1.1 }
345    
346 root 1.8 // a keysym_t with range>1 is counted one more time for every keysym that
347     // lies in its range
348 root 1.2 for (i = 0; i < keymap.size (); ++i)
349 root 1.1 {
350 root 1.2 if (keymap[i]->range > 1)
351 root 1.1 {
352 root 1.4 for (int j = min (keymap [i]->range, KEYSYM_HASH_BUDGETS) - 1; j > 0; --j)
353 root 1.1 {
354 root 1.4 hashkey = ((keymap [i]->keysym + j) & KEYSYM_HASH_MASK);
355     if (hash_budget_size [hashkey])
356     ++hash_budget_size [hashkey];
357 root 1.1 }
358     }
359     }
360    
361     // now we know the size of each budget
362     // compute the index of each budget
363 root 1.4 hash [0] = 0;
364 root 1.2 for (index = 0, i = 1; i < KEYSYM_HASH_BUDGETS; ++i)
365 root 1.1 {
366 root 1.4 index += hash_budget_size [i - 1];
367     hash[i] = (hash_budget_size [i] ? index : hash [i - 1]);
368 root 1.1 }
369 root 1.2
370 root 1.1 // and allocate just enough space
371 root 1.4 sorted_keymap.insert (sorted_keymap.begin (), index + hash_budget_size [i - 1], 0);
372 root 1.1
373     // fill in sorted_keymap
374     // it is sorted in each budget
375 root 1.2 for (i = 0; i < keymap.size (); ++i)
376 root 1.1 {
377 root 1.4 for (int j = min (keymap [i]->range, KEYSYM_HASH_BUDGETS) - 1; j >= 0; --j)
378 root 1.1 {
379 root 1.4 hashkey = ((keymap [i]->keysym + j) & KEYSYM_HASH_MASK);
380 root 1.2
381 root 1.4 if (hash_budget_size [hashkey])
382 root 1.1 {
383 root 1.4 index = hash [hashkey] + hash_budget_counter [hashkey];
384 root 1.2
385 root 1.4 while (index > hash [hashkey]
386     && compare_priority (keymap [i], sorted_keymap [index - 1]) > 0)
387 root 1.1 {
388 root 1.4 sorted_keymap [index] = sorted_keymap [index - 1];
389 root 1.1 --index;
390     }
391 root 1.2
392 root 1.4 sorted_keymap [index] = keymap [i];
393     ++hash_budget_counter [hashkey];
394 root 1.1 }
395     }
396     }
397    
398 root 1.2 keymap.swap (sorted_keymap);
399 root 1.1
400     #if defined (DEBUG_STRICT) || defined (DEBUG_KEYBOARD)
401     // check for invariants
402     for (i = 0; i < KEYSYM_HASH_BUDGETS; ++i)
403     {
404 root 1.2 index = hash[i];
405 root 1.4 for (int j = 0; j < hash_budget_size [i]; ++j)
406 root 1.1 {
407 root 1.4 if (keymap [index + j]->range == 1)
408     assert (i == (keymap [index + j]->keysym & KEYSYM_HASH_MASK));
409 root 1.2
410 root 1.1 if (j)
411 root 1.4 assert (compare_priority (keymap [index + j - 1],
412     keymap [index + j]) >= 0);
413 root 1.1 }
414     }
415    
416     // this should be able to detect most possible bugs
417     for (i = 0; i < sorted_keymap.size (); ++i)
418     {
419     keysym_t *a = sorted_keymap[i];
420     for (int j = 0; j < a->range; ++j)
421     {
422 root 1.7 int index = find_keysym (a->keysym + j, a->state);
423 root 1.6
424 root 1.1 assert (index >= 0);
425 root 1.4 keysym_t *b = keymap [index];
426 root 1.2 assert (i == (signed) index || // the normally expected result
427 root 1.10 (a->keysym + j) >= b->keysym && (a->keysym + j) <= (b->keysym + b->range) && compare_priority (a, b) <= 0); // is effectively the same or a closer match
428 root 1.1 }
429     }
430     #endif
431     }
432    
433     int
434     keyboard_manager::find_keysym (KeySym keysym, unsigned int state)
435     {
436 root 1.2 int hashkey = keysym & KEYSYM_HASH_MASK;
437     unsigned int index = hash [hashkey];
438 root 1.1
439 root 1.2 for (; index < keymap.size (); ++index)
440 root 1.1 {
441 root 1.4 keysym_t *key = keymap [index];
442 root 1.2
443     if (key->keysym <= keysym && key->keysym + key->range > keysym
444 root 1.1 // match only the specified bits in state and ignore others
445 root 1.7 && (key->state & state) == key->state)
446 root 1.2 return index;
447 root 1.10 else if ((key->keysym & KEYSYM_HASH_MASK) > hashkey && key->range == 1)
448 root 1.1 return -1;
449     }
450    
451     return -1;
452     }
453    
454     #endif /* KEYSYM_RESOURCE */
455     // vim:et:ts=2:sw=2