1 | #include "../config.h" |
1 | #include "../config.h" |
2 | #include "rxvt.h" |
2 | #include "rxvt.h" |
|
|
3 | |
|
|
4 | #ifdef KEYSYM_RESOURCE |
|
|
5 | |
|
|
6 | #include <cstring> |
|
|
7 | |
3 | #include "keyboard.h" |
8 | #include "keyboard.h" |
4 | #include "command.h" |
9 | #include "command.h" |
5 | #include <string.h> |
|
|
6 | #include <X11/X.h> |
|
|
7 | |
|
|
8 | #ifdef KEYSYM_RESOURCE |
|
|
9 | |
10 | |
10 | //////////////////////////////////////////////////////////////////////////////// |
11 | //////////////////////////////////////////////////////////////////////////////// |
11 | // default keycode translation map and keyevent handlers |
12 | // default keycode translation map and keyevent handlers |
12 | |
13 | |
13 | keysym_t keyboard_manager::stock_keymap[] = { |
14 | keysym_t keyboard_manager::stock_keymap[] = { |
… | |
… | |
57 | *ch |= 0x80; |
58 | *ch |= 0x80; |
58 | } |
59 | } |
59 | else if (rt->meta_char == C0_ESC) /* escape prefix */ |
60 | else if (rt->meta_char == C0_ESC) /* escape prefix */ |
60 | #endif |
61 | #endif |
61 | { |
62 | { |
62 | const unsigned char |
63 | const unsigned char ch = C0_ESC; |
63 | ch = C0_ESC; |
|
|
64 | rt->tt_write (&ch, 1); |
64 | rt->tt_write (&ch, 1); |
65 | } |
65 | } |
66 | } |
66 | } |
67 | |
67 | |
68 | rt->tt_write ((unsigned char *) buf, buflen); |
68 | rt->tt_write ((unsigned char *) buf, buflen); |
69 | } |
69 | } |
70 | |
70 | |
71 | static int |
71 | static int |
72 | format_keyrange_string (const char *str, int keysym_offset, char *buf, int bufsize) |
72 | format_keyrange_string (const char *str, int keysym_offset, char *buf, int bufsize) |
73 | { |
73 | { |
74 | int len = snprintf (buf, bufsize, str + 1, keysym_offset + str [0]); |
74 | size_t len = snprintf (buf, bufsize, str + 1, keysym_offset + str [0]); |
75 | |
75 | |
76 | if (len >= bufsize) |
76 | if (len >= (size_t)bufsize) |
77 | { |
|
|
78 | fprintf (stderr, "buffer overflowed!\n"); |
|
|
79 | buf[bufsize - 1] = '\0'; |
|
|
80 | } |
77 | { |
81 | else if (len < 0) |
78 | rxvt_warn ("format_keyrange_string: formatting failed, ignoring key.\n"); |
82 | { |
79 | *buf = 0; |
83 | perror ("keyrange_translator()"); |
|
|
84 | } |
80 | } |
85 | |
81 | |
86 | return len; |
82 | return len; |
87 | } |
83 | } |
88 | |
84 | |
89 | //////////////////////////////////////////////////////////////////////////////// |
85 | //////////////////////////////////////////////////////////////////////////////// |
90 | // return: #bits of '1' |
86 | // return: #bits of '1' |
|
|
87 | #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 3) |
|
|
88 | # define bitcount(n) (__extension__ ({ uint32_t n__ = (n); __builtin_popcount (n); })) |
|
|
89 | #else |
91 | static int |
90 | static int |
92 | bitcount (unsigned int n) |
91 | bitcount (uint16_t n) |
93 | { |
92 | { |
94 | int i; |
93 | int i; |
95 | |
94 | |
96 | for (i = 0; n; ++i, n &= (n - 1)) |
95 | for (i = 0; n; ++i, n &= n - 1) |
97 | ; |
96 | ; |
98 | |
97 | |
99 | return i; |
98 | return i; |
100 | } |
99 | } |
|
|
100 | #endif |
101 | |
101 | |
102 | // return: priority_of_a - priority_of_b |
102 | // return: priority_of_a - priority_of_b |
103 | static int |
103 | static int |
104 | compare_priority (keysym_t *a, keysym_t *b) |
104 | compare_priority (keysym_t *a, keysym_t *b) |
105 | { |
105 | { |
… | |
… | |
161 | { |
161 | { |
162 | assert (trans); |
162 | assert (trans); |
163 | |
163 | |
164 | keysym_t *key = new keysym_t; |
164 | keysym_t *key = new keysym_t; |
165 | wchar_t *wc = rxvt_mbstowcs (trans); |
165 | wchar_t *wc = rxvt_mbstowcs (trans); |
|
|
166 | printf ("CONV <%s> %x %x %x %x\n", trans, (int)wc[0], (int)wc[1], (int)wc[2], (int)wc[3]); |
166 | const char *translation = rxvt_wcstoutf8 (wc); |
167 | const char *translation = rxvt_wcstoutf8 (wc); |
167 | free (wc); |
168 | free (wc); |
168 | |
169 | |
169 | if (key && translation) |
170 | if (key && translation) |
170 | { |
171 | { |
… | |
… | |
185 | key->range = suffix - middle - 1; |
186 | key->range = suffix - middle - 1; |
186 | |
187 | |
187 | strcpy (translation, translation + 4); |
188 | strcpy (translation, translation + 4); |
188 | } |
189 | } |
189 | else |
190 | else |
190 | { |
|
|
191 | key->range = 1; |
|
|
192 | rxvt_warn ("cannot parse list-type keysym '%s', treating as normal keysym.\n", translation); |
191 | rxvt_warn ("cannot parse list-type keysym '%s', treating as normal keysym.\n", translation); |
193 | } |
192 | } |
194 | } |
|
|
195 | else |
|
|
196 | |
193 | |
197 | user_keymap.push_back (key); |
194 | user_keymap.push_back (key); |
198 | user_translations.push_back (translation); |
195 | user_translations.push_back (translation); |
199 | register_keymap (key); |
196 | register_keymap (key); |
200 | } |
197 | } |
… | |
… | |
236 | bool |
233 | bool |
237 | keyboard_manager::dispatch (rxvt_term *term, KeySym keysym, unsigned int state) |
234 | keyboard_manager::dispatch (rxvt_term *term, KeySym keysym, unsigned int state) |
238 | { |
235 | { |
239 | assert (hash[0] == 0 && "register_done() need to be called"); |
236 | assert (hash[0] == 0 && "register_done() need to be called"); |
240 | |
237 | |
241 | if (state & term->ModMetaMask) |
238 | if (state & term->ModMetaMask) state |= MetaMask; |
242 | state |= MetaMask; |
|
|
243 | |
|
|
244 | if (state & term->ModNumLockMask) |
239 | if (state & term->ModNumLockMask) state |= NumLockMask; |
245 | state |= NumLockMask; |
240 | if (state & term->ModLevel3Mask) state |= Level3Mask; |
246 | |
241 | |
247 | if (!!(term->priv_modes & PrivMode_aplKP) != !!(state & ShiftMask)) |
242 | if (!!(term->priv_modes & PrivMode_aplKP) != !!(state & ShiftMask)) |
248 | state |= AppKeypadMask; |
243 | state |= AppKeypadMask; |
249 | |
244 | |
250 | int index = find_keysym (keysym, state); |
245 | int index = find_keysym (keysym, state); |
251 | |
246 | |
252 | if (index >= 0) |
247 | if (index >= 0) |
253 | { |
248 | { |
254 | assert (term && keymap [index]); |
|
|
255 | const keysym_t &key = *keymap [index]; |
249 | const keysym_t &key = *keymap [index]; |
256 | |
250 | |
257 | int keysym_offset = keysym - key.keysym; |
251 | int keysym_offset = keysym - key.keysym; |
258 | |
252 | |
259 | wchar_t *wc = rxvt_utf8towcs (key.str); |
253 | wchar_t *wc = rxvt_utf8towcs (key.str); |
|
|
254 | |
260 | char *str = rxvt_wcstombs (wc); |
255 | char *str = rxvt_wcstombs (wc); |
261 | // TODO: do translations, unescaping etc, here (allow \u escape etc.) |
256 | // TODO: do translations, unescaping etc, here (allow \u escape etc.) |
262 | free (wc); |
257 | free (wc); |
263 | |
258 | |
264 | switch (key.type) |
259 | switch (key.type) |
… | |
… | |
434 | for (i = 0; i < sorted_keymap.size (); ++i) |
429 | for (i = 0; i < sorted_keymap.size (); ++i) |
435 | { |
430 | { |
436 | keysym_t *a = sorted_keymap[i]; |
431 | keysym_t *a = sorted_keymap[i]; |
437 | for (int j = 0; j < a->range; ++j) |
432 | for (int j = 0; j < a->range; ++j) |
438 | { |
433 | { |
439 | int index = find_keysym (a->keysym + j, a->state & OtherModMask); |
434 | int index = find_keysym (a->keysym + j, a->state); |
|
|
435 | |
440 | assert (index >= 0); |
436 | assert (index >= 0); |
441 | keysym_t *b = keymap [index]; |
437 | keysym_t *b = keymap [index]; |
442 | assert (i == (signed) index || // the normally expected result |
438 | assert (i == (signed) index || // the normally expected result |
443 | (a->keysym + j) >= b->keysym && (a->keysym + j) <= (b->keysym + b->range) && compare_priority (a, b) <= 0); // is effectively the same |
439 | (a->keysym + j) >= b->keysym && (a->keysym + j) <= (b->keysym + b->range) && compare_priority (a, b) <= 0); // is effectively the same |
444 | } |
440 | } |
… | |
… | |
457 | keysym_t *key = keymap [index]; |
453 | keysym_t *key = keymap [index]; |
458 | assert (key); |
454 | assert (key); |
459 | |
455 | |
460 | if (key->keysym <= keysym && key->keysym + key->range > keysym |
456 | if (key->keysym <= keysym && key->keysym + key->range > keysym |
461 | // match only the specified bits in state and ignore others |
457 | // match only the specified bits in state and ignore others |
462 | && (key->state & OtherModMask) == (key->state & state)) |
458 | && (key->state & state) == key->state) |
463 | return index; |
459 | return index; |
464 | else if (key->keysym > keysym && key->range == 1) |
460 | else if (key->keysym > keysym && key->range == 1) |
465 | return -1; |
461 | return -1; |
466 | } |
462 | } |
467 | |
463 | |