ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/keyboard.C
Revision: 1.11
Committed: Sun Feb 6 15:18:01 2005 UTC (19 years, 3 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-5_0
Changes since 1.10: +31 -52 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     char *str = rxvt_wcstombs (wc);
245 root 1.11 // TODO: do (some) translations, unescaping etc, here (allow \u escape etc.)
246 root 1.2 free (wc);
247    
248     switch (key.type)
249     {
250     case keysym_t::NORMAL:
251     output_string (term, str);
252     break;
253    
254     case keysym_t::RANGE:
255     {
256     char buf[STRING_MAX];
257    
258     if (format_keyrange_string (str, keysym_offset, buf, sizeof (buf)) > 0)
259     output_string (term, buf);
260     }
261     break;
262    
263     case keysym_t::RANGE_META8:
264     {
265     int len;
266     char buf[STRING_MAX];
267    
268     len = format_keyrange_string (str, keysym_offset, buf, sizeof (buf));
269     if (len > 0)
270     output_string_meta8 (term, state, buf, len);
271     }
272     break;
273    
274     case keysym_t::LIST:
275     {
276     char buf[STRING_MAX];
277    
278     char *prefix, *middle, *suffix;
279    
280     prefix = str;
281     middle = strchr (prefix + 1, *prefix);
282     suffix = strrchr (middle + 1, *prefix);
283    
284     memcpy (buf, prefix + 1, middle - prefix - 1);
285     buf [middle - prefix - 1] = middle [keysym_offset + 1];
286     strcpy (buf + (middle - prefix), suffix + 1);
287    
288     output_string (term, buf);
289     }
290     break;
291     }
292    
293     free (str);
294    
295 root 1.1 return true;
296     }
297     else
298 root 1.11 return false;
299 root 1.1 }
300    
301 root 1.2 // purge duplicate keymap entries
302     void keyboard_manager::purge_duplicate_keymap ()
303 root 1.1 {
304 root 1.2 for (unsigned int i = 0; i < keymap.size (); ++i)
305 root 1.1 {
306     for (unsigned int j = 0; j < i; ++j)
307     {
308 root 1.4 if (keymap [i] == keymap [j])
309 root 1.1 {
310 root 1.4 while (keymap [i] == keymap.back ())
311 root 1.2 keymap.pop_back ();
312    
313     if (i < keymap.size ())
314 root 1.1 {
315 root 1.2 keymap[i] = keymap.back ();
316     keymap.pop_back ();
317 root 1.1 }
318 root 1.11
319 root 1.1 break;
320     }
321     }
322     }
323     }
324    
325     void
326     keyboard_manager::setup_hash ()
327     {
328     unsigned int i, index, hashkey;
329 root 1.2 vector <keysym_t *> sorted_keymap;
330     uint16_t hash_budget_size[KEYSYM_HASH_BUDGETS]; // size of each budget
331     uint16_t hash_budget_counter[KEYSYM_HASH_BUDGETS]; // #elements in each budget
332 root 1.1
333     memset (hash_budget_size, 0, sizeof (hash_budget_size));
334     memset (hash_budget_counter, 0, sizeof (hash_budget_counter));
335    
336 root 1.11 // determine hash bucket size
337 root 1.2 for (i = 0; i < keymap.size (); ++i)
338 root 1.11 for (int j = min (keymap [i]->range, KEYSYM_HASH_BUDGETS) - 1; j >= 0; --j)
339     {
340     hashkey = (keymap [i]->keysym + j) & KEYSYM_HASH_MASK;
341     ++hash_budget_size [hashkey];
342     }
343 root 1.1
344     // now we know the size of each budget
345     // compute the index of each budget
346 root 1.4 hash [0] = 0;
347 root 1.2 for (index = 0, i = 1; i < KEYSYM_HASH_BUDGETS; ++i)
348 root 1.1 {
349 root 1.4 index += hash_budget_size [i - 1];
350 root 1.11 hash [i] = index;
351 root 1.1 }
352 root 1.2
353 root 1.1 // and allocate just enough space
354 root 1.4 sorted_keymap.insert (sorted_keymap.begin (), index + hash_budget_size [i - 1], 0);
355 root 1.1
356     // fill in sorted_keymap
357     // it is sorted in each budget
358 root 1.2 for (i = 0; i < keymap.size (); ++i)
359 root 1.11 for (int j = min (keymap [i]->range, KEYSYM_HASH_BUDGETS) - 1; j >= 0; --j)
360     {
361     hashkey = (keymap [i]->keysym + j) & KEYSYM_HASH_MASK;
362    
363     index = hash [hashkey] + hash_budget_counter [hashkey];
364    
365     while (index > hash [hashkey]
366     && compare_priority (keymap [i], sorted_keymap [index - 1]) > 0)
367     {
368     sorted_keymap [index] = sorted_keymap [index - 1];
369     --index;
370     }
371    
372     sorted_keymap [index] = keymap [i];
373     ++hash_budget_counter [hashkey];
374     }
375 root 1.1
376 root 1.2 keymap.swap (sorted_keymap);
377 root 1.1
378     #if defined (DEBUG_STRICT) || defined (DEBUG_KEYBOARD)
379     // check for invariants
380     for (i = 0; i < KEYSYM_HASH_BUDGETS; ++i)
381     {
382 root 1.2 index = hash[i];
383 root 1.4 for (int j = 0; j < hash_budget_size [i]; ++j)
384 root 1.1 {
385 root 1.4 if (keymap [index + j]->range == 1)
386     assert (i == (keymap [index + j]->keysym & KEYSYM_HASH_MASK));
387 root 1.2
388 root 1.1 if (j)
389 root 1.4 assert (compare_priority (keymap [index + j - 1],
390     keymap [index + j]) >= 0);
391 root 1.1 }
392     }
393    
394     // this should be able to detect most possible bugs
395     for (i = 0; i < sorted_keymap.size (); ++i)
396     {
397     keysym_t *a = sorted_keymap[i];
398     for (int j = 0; j < a->range; ++j)
399     {
400 root 1.7 int index = find_keysym (a->keysym + j, a->state);
401 root 1.6
402 root 1.1 assert (index >= 0);
403 root 1.4 keysym_t *b = keymap [index];
404 root 1.2 assert (i == (signed) index || // the normally expected result
405 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
406 root 1.1 }
407     }
408     #endif
409     }
410    
411     int
412     keyboard_manager::find_keysym (KeySym keysym, unsigned int state)
413     {
414 root 1.2 int hashkey = keysym & KEYSYM_HASH_MASK;
415     unsigned int index = hash [hashkey];
416 root 1.11 unsigned int end = hashkey < KEYSYM_HASH_BUDGETS - 1
417     ? hash [hashkey + 1]
418     : keymap.size ();
419 root 1.1
420 root 1.11 for (; index < end; ++index)
421 root 1.1 {
422 root 1.4 keysym_t *key = keymap [index];
423 root 1.2
424 root 1.11 if (key->keysym <= keysym && keysym < key->keysym + key->range
425 root 1.1 // match only the specified bits in state and ignore others
426 root 1.7 && (key->state & state) == key->state)
427 root 1.2 return index;
428 root 1.1 }
429    
430     return -1;
431     }
432    
433     #endif /* KEYSYM_RESOURCE */
434     // vim:et:ts=2:sw=2