ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/keyboard.C
(Generate patch)

Comparing rxvt-unicode/src/keyboard.C (file contents):
Revision 1.49 by sf-exg, Sun Nov 27 12:12:49 2011 UTC vs.
Revision 1.59 by root, Fri May 18 00:10:47 2012 UTC

24#include "../config.h" 24#include "../config.h"
25#include "rxvt.h" 25#include "rxvt.h"
26 26
27#ifdef KEYSYM_RESOURCE 27#ifdef KEYSYM_RESOURCE
28 28
29#include <cstring> 29#include <string.h>
30 30
31#include "rxvtperl.h" 31#include "rxvtperl.h"
32#include "keyboard.h" 32#include "keyboard.h"
33#include "command.h"
34 33
35/* an intro to the data structure: 34/* an intro to the data structure:
36 * 35 *
37 * vector keymap[] is grouped. 36 * vector keymap[] is grouped.
38 * 37 *
98 hash [0] = 1; // hash[0] != 0 indicates uninitialized data 97 hash [0] = 1; // hash[0] != 0 indicates uninitialized data
99} 98}
100 99
101keyboard_manager::~keyboard_manager () 100keyboard_manager::~keyboard_manager ()
102{ 101{
103 hash [0] = 2;
104
105 for (unsigned int i = 0; i < keymap.size (); ++i) 102 for (unsigned int i = 0; i < keymap.size (); ++i)
106 { 103 {
107 free (keymap [i]->str); 104 free (keymap [i]->str);
108 delete keymap [i]; 105 delete keymap [i];
109 keymap [i] = 0;
110 } 106 }
111
112 keymap.clear ();
113} 107}
114 108
115// a wrapper for register_translation that converts the input string
116// to utf-8 and expands 'list' syntax.
117void 109void
118keyboard_manager::register_user_translation (KeySym keysym, unsigned int state, const char *trans) 110keyboard_manager::register_user_translation (KeySym keysym, unsigned int state, const wchar_t *ws)
119{ 111{
120 wchar_t *wc = rxvt_mbstowcs (trans);
121 char *translation = rxvt_wcstoutf8 (wc); 112 char *translation = rxvt_wcstoutf8 (ws);
122 free (wc);
123 113
124 if (strncmp (translation, "list", 4) == 0 && translation [4]
125 && strlen (translation) < STRING_MAX)
126 {
127 char *prefix = translation + 4;
128 char *middle = strchr (prefix + 1, translation [4]);
129 char *suffix = strrchr (prefix + 1, translation [4]);
130
131 if (suffix && middle && suffix > middle + 1)
132 {
133 int range = suffix - middle - 1;
134 int prefix_len = middle - prefix - 1;
135 char buf[STRING_MAX];
136
137 memcpy (buf, prefix + 1, prefix_len);
138 strcpy (buf + prefix_len + 1, suffix + 1);
139
140 for (int i = 0; i < range; i++)
141 {
142 buf [prefix_len] = middle [i + 1];
143 register_translation (keysym + i, state, strdup (buf));
144 }
145
146 free (translation);
147 return;
148 }
149 else
150 rxvt_warn ("unable to parse list-type keysym '%s', processing as normal keysym.\n", translation);
151 }
152
153 register_translation (keysym, state, translation);
154}
155
156void
157keyboard_manager::register_translation (KeySym keysym, unsigned int state, char *translation)
158{
159 keysym_t *key = new keysym_t; 114 keysym_t *key = new keysym_t;
160 115
161 if (key && translation)
162 {
163 key->keysym = keysym; 116 key->keysym = keysym;
164 key->state = state; 117 key->state = state;
165 key->str = translation; 118 key->str = translation;
166 key->type = keysym_t::STRING; 119 key->type = keysym_t::STRING;
167 120
168 if (strncmp (translation, "builtin:", 8) == 0) 121 if (strncmp (translation, "builtin:", 8) == 0)
169 key->type = keysym_t::BUILTIN; 122 key->type = keysym_t::BUILTIN;
170 123
171 if (keymap.size () == keymap.capacity ()) 124 if (keymap.size () == keymap.capacity ())
172 keymap.reserve (keymap.size () * 2); 125 keymap.reserve (keymap.size () * 2);
173 126
174 keymap.push_back (key); 127 keymap.push_back (key);
175 hash[0] = 3; 128 hash[0] = 3;
176 }
177 else
178 {
179 delete key;
180 free (translation);
181 rxvt_fatal ("memory allocation failure. aborting.\n");
182 }
183} 129}
184 130
185bool 131bool
186keyboard_manager::dispatch (rxvt_term *term, KeySym keysym, unsigned int state) 132keyboard_manager::dispatch (rxvt_term *term, KeySym keysym, unsigned int state)
187{ 133{
202 { 148 {
203 keysym_t *key = keymap [index]; 149 keysym_t *key = keymap [index];
204 150
205 if (key->type != keysym_t::BUILTIN) 151 if (key->type != keysym_t::BUILTIN)
206 { 152 {
207 wchar_t *wc = rxvt_utf8towcs (key->str); 153 wchar_t *ws = rxvt_utf8towcs (key->str);
208 char *str = rxvt_wcstombs (wc); 154 char *str = rxvt_wcstombs (ws);
209 // TODO: do (some) translations, unescaping etc, here (allow \u escape etc.) 155 // TODO: do (some) translations, unescaping etc, here (allow \u escape etc.)
210 free (wc); 156 free (ws);
211 157
212 output_string (term, str); 158 output_string (term, str);
213 159
214 free (str); 160 free (str);
215 161
222 168
223void 169void
224keyboard_manager::register_done () 170keyboard_manager::register_done ()
225{ 171{
226 unsigned int i, index, hashkey; 172 unsigned int i, index, hashkey;
227 vector <keysym_t *> sorted_keymap;
228 uint16_t hash_bucket_size[KEYSYM_HASH_BUCKETS]; // size of each bucket 173 uint16_t hash_bucket_size[KEYSYM_HASH_BUCKETS]; // size of each bucket
229 174
230 memset (hash_bucket_size, 0, sizeof (hash_bucket_size)); 175 memset (hash_bucket_size, 0, sizeof (hash_bucket_size));
231 176
232 // determine hash bucket size 177 // determine hash bucket size
244 index += hash_bucket_size [i - 1]; 189 index += hash_bucket_size [i - 1];
245 hash [i] = index; 190 hash [i] = index;
246 } 191 }
247 192
248 // and allocate just enough space 193 // and allocate just enough space
249 sorted_keymap.insert (sorted_keymap.begin (), index + hash_bucket_size [i - 1], 0); 194 simplevec <keysym_t *> sorted_keymap (index + hash_bucket_size [i - 1], 0);
250 195
251 memset (hash_bucket_size, 0, sizeof (hash_bucket_size)); 196 memset (hash_bucket_size, 0, sizeof (hash_bucket_size));
252 197
253 // fill in sorted_keymap 198 // fill in sorted_keymap
254 // it is sorted in each bucket 199 // it is sorted in each bucket

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines