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[] = { |
… | |
… | |
68 | } |
69 | } |
69 | |
70 | |
70 | static int |
71 | static int |
71 | 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) |
72 | { |
73 | { |
73 | int len = snprintf (buf, bufsize, str + 1, keysym_offset + str [0]); |
74 | size_t len = snprintf (buf, bufsize, str + 1, keysym_offset + str [0]); |
74 | |
75 | |
75 | if (len >= bufsize) |
76 | if (len >= (size_t)bufsize) |
76 | { |
|
|
77 | rxvt_warn ("buffer overflowed!\n"); |
|
|
78 | *buf = 0; |
|
|
79 | } |
77 | { |
80 | else if (len < 0) |
78 | rxvt_warn ("format_keyrange_string: formatting failed, ignoring key.\n"); |
81 | { |
|
|
82 | rxvt_warn ("keyrange_translator(), snprintf error"); |
|
|
83 | *buf = 0; |
79 | *buf = 0; |
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 | } |
… | |
… | |
247 | |
244 | |
248 | int index = find_keysym (keysym, state); |
245 | int index = find_keysym (keysym, state); |
249 | |
246 | |
250 | if (index >= 0) |
247 | if (index >= 0) |
251 | { |
248 | { |
252 | assert (term && keymap [index]); |
|
|
253 | const keysym_t &key = *keymap [index]; |
249 | const keysym_t &key = *keymap [index]; |
254 | |
250 | |
255 | int keysym_offset = keysym - key.keysym; |
251 | int keysym_offset = keysym - key.keysym; |
256 | |
252 | |
257 | wchar_t *wc = rxvt_utf8towcs (key.str); |
253 | wchar_t *wc = rxvt_utf8towcs (key.str); |
|
|
254 | |
258 | char *str = rxvt_wcstombs (wc); |
255 | char *str = rxvt_wcstombs (wc); |
259 | // TODO: do translations, unescaping etc, here (allow \u escape etc.) |
256 | // TODO: do translations, unescaping etc, here (allow \u escape etc.) |
260 | free (wc); |
257 | free (wc); |
261 | |
258 | |
262 | switch (key.type) |
259 | switch (key.type) |
… | |
… | |
432 | for (i = 0; i < sorted_keymap.size (); ++i) |
429 | for (i = 0; i < sorted_keymap.size (); ++i) |
433 | { |
430 | { |
434 | keysym_t *a = sorted_keymap[i]; |
431 | keysym_t *a = sorted_keymap[i]; |
435 | for (int j = 0; j < a->range; ++j) |
432 | for (int j = 0; j < a->range; ++j) |
436 | { |
433 | { |
437 | int index = find_keysym (a->keysym + j, a->state & OtherModMask); |
434 | int index = find_keysym (a->keysym + j, a->state); |
438 | |
435 | |
439 | assert (index >= 0); |
436 | assert (index >= 0); |
440 | keysym_t *b = keymap [index]; |
437 | keysym_t *b = keymap [index]; |
441 | assert (i == (signed) index || // the normally expected result |
438 | assert (i == (signed) index || // the normally expected result |
442 | (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 |
… | |
… | |
456 | keysym_t *key = keymap [index]; |
453 | keysym_t *key = keymap [index]; |
457 | assert (key); |
454 | assert (key); |
458 | |
455 | |
459 | if (key->keysym <= keysym && key->keysym + key->range > keysym |
456 | if (key->keysym <= keysym && key->keysym + key->range > keysym |
460 | // match only the specified bits in state and ignore others |
457 | // match only the specified bits in state and ignore others |
461 | && (key->state & OtherModMask) == (key->state & state)) |
458 | && (key->state & state) == key->state) |
462 | return index; |
459 | return index; |
463 | else if (key->keysym > keysym && key->range == 1) |
460 | else if (key->keysym > keysym && key->range == 1) |
464 | return -1; |
461 | return -1; |
465 | } |
462 | } |
466 | |
463 | |