… | |
… | |
3 | *----------------------------------------------------------------------* |
3 | *----------------------------------------------------------------------* |
4 | * |
4 | * |
5 | * All portions of code are copyright by their respective author/s. |
5 | * All portions of code are copyright by their respective author/s. |
6 | * Copyright (c) 2005 WU Fengguang |
6 | * Copyright (c) 2005 WU Fengguang |
7 | * Copyright (c) 2005-2006 Marc Lehmann <schmorp@schmorp.de> |
7 | * Copyright (c) 2005-2006 Marc Lehmann <schmorp@schmorp.de> |
|
|
8 | * Copyright (c) 2015 Emanuele Giaquinta <e.giaquinta@glauco.it> |
8 | * |
9 | * |
9 | * This program is free software; you can redistribute it and/or modify |
10 | * This program is free software; you can redistribute it and/or modify |
10 | * it under the terms of the GNU General Public License as published by |
11 | * it under the terms of the GNU General Public License as published by |
11 | * the Free Software Foundation; either version 2 of the License, or |
12 | * the Free Software Foundation; either version 3 of the License, or |
12 | * (at your option) any later version. |
13 | * (at your option) any later version. |
13 | * |
14 | * |
14 | * This program is distributed in the hope that it will be useful, |
15 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
… | |
… | |
61 | * where hashkey of keymap[i]->keysym = keymap[i]->keysym & KEYSYM_HASH_MASK |
62 | * where hashkey of keymap[i]->keysym = keymap[i]->keysym & KEYSYM_HASH_MASK |
62 | * n(the number of groups) = the number of non-zero member of hash_bucket_size[]; |
63 | * n(the number of groups) = the number of non-zero member of hash_bucket_size[]; |
63 | * Ni(the size of group i) = hash_bucket_size[Ii]. |
64 | * Ni(the size of group i) = hash_bucket_size[Ii]. |
64 | */ |
65 | */ |
65 | |
66 | |
66 | static void |
|
|
67 | output_string (rxvt_term *term, const char *str) |
|
|
68 | { |
|
|
69 | if (strncmp (str, "command:", 8) == 0) |
|
|
70 | term->cmdbuf_append (str + 8, strlen (str) - 8); |
|
|
71 | else if (strncmp (str, "perl:", 5) == 0) |
|
|
72 | HOOK_INVOKE((term, HOOK_USER_COMMAND, DT_STR, str + 5, DT_END)); |
|
|
73 | else |
|
|
74 | term->tt_write (str, strlen (str)); |
|
|
75 | } |
|
|
76 | |
|
|
77 | // return: priority_of_a - priority_of_b |
67 | // return: priority_of_a - priority_of_b |
78 | static int |
68 | static int |
79 | compare_priority (keysym_t *a, keysym_t *b) |
69 | compare_priority (keysym_t *a, keysym_t *b) |
80 | { |
70 | { |
81 | // (the more '1's in state; the less range): the greater priority |
71 | // (the more '1's in state; the less range): the greater priority |
82 | int ca = ecb_popcount32 (a->state /* & OtherModMask */); |
72 | int ca = ecb_popcount32 (a->state /* & OtherModMask */); |
83 | int cb = ecb_popcount32 (b->state /* & OtherModMask */); |
73 | int cb = ecb_popcount32 (b->state /* & OtherModMask */); |
84 | |
74 | |
85 | if (ca != cb) |
|
|
86 | return ca - cb; |
75 | return ca - cb; |
87 | //else if (a->state != b->state) // this behavior is to be discussed |
|
|
88 | // return b->state - a->state; |
|
|
89 | else |
|
|
90 | return 0; |
|
|
91 | } |
76 | } |
92 | |
77 | |
93 | //////////////////////////////////////////////////////////////////////////////// |
78 | //////////////////////////////////////////////////////////////////////////////// |
94 | keyboard_manager::keyboard_manager () |
79 | keyboard_manager::keyboard_manager () |
95 | { |
80 | { |
… | |
… | |
105 | delete keymap [i]; |
90 | delete keymap [i]; |
106 | } |
91 | } |
107 | } |
92 | } |
108 | |
93 | |
109 | void |
94 | void |
|
|
95 | keyboard_manager::unregister_action (KeySym keysym, unsigned int state) |
|
|
96 | { |
|
|
97 | for (unsigned int i = 0; i < keymap.size (); ++i) |
|
|
98 | if (keymap [i]->keysym == keysym |
|
|
99 | && keymap [i]->state == state) |
|
|
100 | { |
|
|
101 | free (keymap [i]->str); |
|
|
102 | delete keymap [i]; |
|
|
103 | |
|
|
104 | if (i < keymap.size () - 1) |
|
|
105 | keymap [i] = keymap [keymap.size () - 1]; |
|
|
106 | keymap.pop_back (); |
|
|
107 | |
|
|
108 | break; |
|
|
109 | } |
|
|
110 | } |
|
|
111 | |
|
|
112 | void |
110 | keyboard_manager::register_user_translation (KeySym keysym, unsigned int state, const wchar_t *ws) |
113 | keyboard_manager::register_action (KeySym keysym, unsigned int state, const wchar_t *ws) |
111 | { |
114 | { |
112 | char *translation = rxvt_wcstoutf8 (ws); |
115 | char *action = rxvt_wcstoutf8 (ws); |
113 | |
116 | |
114 | keysym_t *key = new keysym_t; |
117 | keysym_t *key = new keysym_t; |
115 | |
118 | |
116 | key->keysym = keysym; |
119 | key->keysym = keysym; |
117 | key->state = state; |
120 | key->state = state; |
118 | key->str = translation; |
121 | key->str = action; |
119 | key->type = keysym_t::STRING; |
122 | key->type = keysym_t::STRING; |
120 | |
123 | |
121 | if (strncmp (translation, "builtin:", 8) == 0) |
124 | if (strncmp (action, "builtin:", 8) == 0) |
122 | key->type = keysym_t::BUILTIN; |
125 | key->type = keysym_t::BUILTIN; |
|
|
126 | else if (strncmp (action, "builtin-string:", 15) == 0) |
|
|
127 | key->type = keysym_t::BUILTIN_STRING; |
|
|
128 | |
|
|
129 | unregister_action (keysym, state); |
123 | |
130 | |
124 | if (keymap.size () == keymap.capacity ()) |
131 | if (keymap.size () == keymap.capacity ()) |
125 | keymap.reserve (keymap.size () * 2); |
132 | keymap.reserve (keymap.size () * 2); |
126 | |
133 | |
127 | keymap.push_back (key); |
134 | keymap.push_back (key); |
128 | hash[0] = 3; |
135 | hash[0] = 3; |
129 | } |
136 | } |
130 | |
137 | |
131 | bool |
138 | keysym_t * |
132 | keyboard_manager::dispatch (rxvt_term *term, KeySym keysym, unsigned int state) |
139 | keyboard_manager::lookup_keysym (rxvt_term *term, KeySym keysym, unsigned int state) |
133 | { |
140 | { |
134 | assert (("register_done() need to be called", hash[0] == 0)); |
141 | assert (("register_done() need to be called", hash[0] == 0)); |
135 | |
142 | |
136 | state &= OtherModMask; // mask out uninteresting modifiers |
143 | state &= OtherModMask; // mask out uninteresting modifiers |
137 | |
144 | |
… | |
… | |
142 | if (!!(term->priv_modes & PrivMode_aplKP) != !!(state & ShiftMask)) |
149 | if (!!(term->priv_modes & PrivMode_aplKP) != !!(state & ShiftMask)) |
143 | state |= AppKeypadMask; |
150 | state |= AppKeypadMask; |
144 | |
151 | |
145 | int index = find_keysym (keysym, state); |
152 | int index = find_keysym (keysym, state); |
146 | |
153 | |
147 | if (index >= 0) |
154 | return index >= 0 ? keymap [index] : 0; |
148 | { |
155 | } |
149 | keysym_t *key = keymap [index]; |
|
|
150 | |
156 | |
|
|
157 | bool |
|
|
158 | keyboard_manager::dispatch (rxvt_term *term, KeySym keysym, unsigned int state, const char *kbuf, int len) |
|
|
159 | { |
|
|
160 | keysym_t *key = lookup_keysym (term, keysym, state); |
|
|
161 | |
|
|
162 | if (key) |
|
|
163 | { |
|
|
164 | if (key->type == keysym_t::BUILTIN_STRING) |
|
|
165 | { |
|
|
166 | term->tt_write_user_input (kbuf, len); |
|
|
167 | return true; |
|
|
168 | } |
151 | if (key->type != keysym_t::BUILTIN) |
169 | else if (key->type != keysym_t::BUILTIN) |
152 | { |
170 | { |
153 | wchar_t *ws = rxvt_utf8towcs (key->str); |
171 | wchar_t *ws = rxvt_utf8towcs (key->str); |
154 | char *str = rxvt_wcstombs (ws); |
172 | char *str = rxvt_wcstombs (ws); |
155 | // TODO: do (some) translations, unescaping etc, here (allow \u escape etc.) |
173 | // TODO: do (some) translations, unescaping etc, here (allow \u escape etc.) |
156 | free (ws); |
174 | free (ws); |
157 | |
175 | |
158 | output_string (term, str); |
176 | if (char *colon = strchr (str, ':')) |
|
|
177 | { |
|
|
178 | if (strncmp (str, "command:", 8) == 0) |
|
|
179 | term->cmdbuf_append (str + 8, strlen (str) - 8); |
|
|
180 | else if (strncmp (str, "string:", 7) == 0) |
|
|
181 | term->tt_write_user_input (colon + 1, strlen (colon + 1)); |
|
|
182 | else if (strncmp (str, "perl:", 5) == 0) |
|
|
183 | HOOK_INVOKE ((term, HOOK_USER_COMMAND, DT_STR, colon + 1, DT_END)); |
|
|
184 | else |
|
|
185 | HOOK_INVOKE ((term, HOOK_ACTION, DT_STR_LEN, str, colon - str, DT_STR, colon + 1, DT_INT, 0, DT_STR_LEN, kbuf, len, DT_END)); |
|
|
186 | } |
|
|
187 | else |
|
|
188 | term->tt_write_user_input (str, strlen (str)); |
159 | |
189 | |
160 | free (str); |
190 | free (str); |
161 | |
191 | |
162 | return true; |
192 | return true; |
163 | } |
193 | } |
… | |
… | |
168 | |
198 | |
169 | void |
199 | void |
170 | keyboard_manager::register_done () |
200 | keyboard_manager::register_done () |
171 | { |
201 | { |
172 | unsigned int i, index, hashkey; |
202 | unsigned int i, index, hashkey; |
173 | vector <keysym_t *> sorted_keymap; |
|
|
174 | uint16_t hash_bucket_size[KEYSYM_HASH_BUCKETS]; // size of each bucket |
203 | uint16_t hash_bucket_size[KEYSYM_HASH_BUCKETS]; // size of each bucket |
175 | |
204 | |
176 | memset (hash_bucket_size, 0, sizeof (hash_bucket_size)); |
205 | memset (hash_bucket_size, 0, sizeof (hash_bucket_size)); |
177 | |
206 | |
178 | // determine hash bucket size |
207 | // determine hash bucket size |
… | |
… | |
182 | ++hash_bucket_size [hashkey]; |
211 | ++hash_bucket_size [hashkey]; |
183 | } |
212 | } |
184 | |
213 | |
185 | // now we know the size of each bucket |
214 | // now we know the size of each bucket |
186 | // compute the index of each bucket |
215 | // compute the index of each bucket |
187 | hash [0] = 0; |
|
|
188 | for (index = 0, i = 1; i < KEYSYM_HASH_BUCKETS; ++i) |
216 | for (index = 0, i = 0; i < KEYSYM_HASH_BUCKETS; ++i) |
189 | { |
217 | { |
190 | index += hash_bucket_size [i - 1]; |
|
|
191 | hash [i] = index; |
218 | hash [i] = index; |
|
|
219 | index += hash_bucket_size [i]; |
192 | } |
220 | } |
193 | |
221 | |
194 | // and allocate just enough space |
222 | // and allocate just enough space |
195 | sorted_keymap.insert (sorted_keymap.begin (), index + hash_bucket_size [i - 1], 0); |
223 | simplevec <keysym_t *> sorted_keymap (index, 0); |
196 | |
224 | |
197 | memset (hash_bucket_size, 0, sizeof (hash_bucket_size)); |
225 | memset (hash_bucket_size, 0, sizeof (hash_bucket_size)); |
198 | |
226 | |
199 | // fill in sorted_keymap |
227 | // fill in sorted_keymap |
200 | // it is sorted in each bucket |
228 | // it is sorted in each bucket |