author | alwin <alwin> | 2005-02-28 09:40:30 (UTC) |
---|---|---|
committer | alwin <alwin> | 2005-02-28 09:40:30 (UTC) |
commit | 2b64a84d39eeed5681d0ee5068c7d11a01527750 (patch) (unidiff) | |
tree | c8693340dbc5ef5e2f9afa90b690829ddff2c4bd /noncore/applets/keyhelper/keyhelperapplet/misc/KeyMappings.cpp | |
parent | 61fa699140c5efbb6ba0bf2a62f7e8fbf62976be (diff) | |
download | opie-2b64a84d39eeed5681d0ee5068c7d11a01527750.zip opie-2b64a84d39eeed5681d0ee5068c7d11a01527750.tar.gz opie-2b64a84d39eeed5681d0ee5068c7d11a01527750.tar.bz2 |
other keymapping tool - not working this moment, I have to check it out
- the reason is that the config file is somewhat easier to understand than
from zkbapplet and has a nice config tool.
Please don't put it into any repositories this moment.
Diffstat (limited to 'noncore/applets/keyhelper/keyhelperapplet/misc/KeyMappings.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/applets/keyhelper/keyhelperapplet/misc/KeyMappings.cpp | 340 |
1 files changed, 340 insertions, 0 deletions
diff --git a/noncore/applets/keyhelper/keyhelperapplet/misc/KeyMappings.cpp b/noncore/applets/keyhelper/keyhelperapplet/misc/KeyMappings.cpp new file mode 100644 index 0000000..0151d39 --- a/dev/null +++ b/noncore/applets/keyhelper/keyhelperapplet/misc/KeyMappings.cpp | |||
@@ -0,0 +1,340 @@ | |||
1 | #include "KeyMappings.h" | ||
2 | |||
3 | static QIntDict<QWSServer::KeyMap> g_mapCache(127); | ||
4 | |||
5 | MapInfo::MapInfo(int code, int mod, int uni, | ||
6 | int shift_uni, int ctrl_uni) | ||
7 | { | ||
8 | const QWSServer::KeyMap* map; | ||
9 | if(uni == 0 || shift_uni == 0 || ctrl_uni == 0){ | ||
10 | map = MapInfo::findKeyMap(code); | ||
11 | if(map != NULL){ | ||
12 | isDefined = true; | ||
13 | unicode = map->unicode; | ||
14 | shift_unicode = map->shift_unicode; | ||
15 | ctrl_unicode = map->ctrl_unicode; | ||
16 | } else { | ||
17 | isDefined = false; | ||
18 | unicode = 0; | ||
19 | shift_unicode = 0; | ||
20 | ctrl_unicode = 0; | ||
21 | } | ||
22 | } else { | ||
23 | isDefined = true; | ||
24 | } | ||
25 | keycode = code; | ||
26 | modifiers = mod; | ||
27 | if(uni != 0){ | ||
28 | unicode = uni; | ||
29 | } | ||
30 | if(shift_uni != 0){ | ||
31 | shift_unicode = shift_uni; | ||
32 | } | ||
33 | if(ctrl_uni != 0){ | ||
34 | ctrl_unicode = ctrl_uni; | ||
35 | } | ||
36 | } | ||
37 | |||
38 | const QWSServer::KeyMap* MapInfo::findKeyMap(int keycode) | ||
39 | { | ||
40 | const QWSServer::KeyMap* m = QWSServer::keyMap(); | ||
41 | |||
42 | while(m->key_code != 0){ | ||
43 | if(m->key_code == keycode){ | ||
44 | return(m); | ||
45 | } | ||
46 | m++; | ||
47 | } | ||
48 | return(NULL); | ||
49 | } | ||
50 | |||
51 | KeyMappings::KeyMappings() | ||
52 | { | ||
53 | qDebug("KeyMappings::KeyMappings()"); | ||
54 | init(); | ||
55 | } | ||
56 | |||
57 | KeyMappings::~KeyMappings() | ||
58 | { | ||
59 | qDebug("KeyMappings::~KeyMappings()"); | ||
60 | clear(); | ||
61 | } | ||
62 | |||
63 | void KeyMappings::init() | ||
64 | { | ||
65 | m_capslock = false; | ||
66 | } | ||
67 | |||
68 | void KeyMappings::reset() | ||
69 | { | ||
70 | clear(); | ||
71 | } | ||
72 | |||
73 | void KeyMappings::clear() | ||
74 | { | ||
75 | for(QMap<int, CodeMaps*>::Iterator it = m_keymaps.begin(); | ||
76 | it!=m_keymaps.end(); ++it){ | ||
77 | delete (*it); | ||
78 | } | ||
79 | m_keymaps.clear(); | ||
80 | g_mapCache.setAutoDelete(true); | ||
81 | g_mapCache.clear(); | ||
82 | } | ||
83 | |||
84 | void KeyMappings::assign(int keycode, int keymask, int mapcode, | ||
85 | int mapmodifiers, int unicode, int shift_unicode, int ctrl_unicode) | ||
86 | { | ||
87 | CodeMaps* map; | ||
88 | if(m_keymaps.contains(keycode)){ | ||
89 | map = m_keymaps[keycode]; | ||
90 | } else { | ||
91 | map = new CodeMaps(); | ||
92 | m_keymaps.insert(keycode, map); | ||
93 | } | ||
94 | |||
95 | MapInfo info(mapcode, mapmodifiers, unicode, shift_unicode, ctrl_unicode); | ||
96 | m_it = map->insert(keymask, info); | ||
97 | } | ||
98 | |||
99 | void KeyMappings::assignModifier(const QString& type, const QString& state) | ||
100 | { | ||
101 | int maskbit = 0; | ||
102 | QString str; | ||
103 | str = type.lower(); | ||
104 | if(str == "shift"){ | ||
105 | maskbit = Qt::ShiftButton; | ||
106 | } else if(str == "control"){ | ||
107 | maskbit = Qt::ControlButton; | ||
108 | } else if(str == "alt"){ | ||
109 | maskbit = Qt::AltButton; | ||
110 | } | ||
111 | str = state.lower(); | ||
112 | if(str == "off"){ | ||
113 | maskbit = (maskbit << 16) & 0xFFFF0000; | ||
114 | } | ||
115 | (*m_it).modifiers |= maskbit; | ||
116 | } | ||
117 | |||
118 | void KeyMappings::assignUnicode(const QString& kind, const QString& ch) | ||
119 | { | ||
120 | QString str; | ||
121 | int code = ch[0].unicode(); | ||
122 | str = kind.lower(); | ||
123 | if(str == "unicode"){ | ||
124 | (*m_it).unicode = code; | ||
125 | } else if(str == "shift_unicode"){ | ||
126 | (*m_it).shift_unicode = code; | ||
127 | } else if(str == "ctrl_unicode"){ | ||
128 | (*m_it).ctrl_unicode = code; | ||
129 | } | ||
130 | } | ||
131 | |||
132 | void KeyMappings::assignUnicode(int unicode) | ||
133 | { | ||
134 | (*m_it).unicode = (*m_it).shift_unicode = | ||
135 | (*m_it).ctrl_unicode = unicode; | ||
136 | } | ||
137 | |||
138 | void KeyMappings::statistics() | ||
139 | { | ||
140 | qWarning("KeyMappings::statistics()"); | ||
141 | for(QMap<int, CodeMaps*>::Iterator it=m_keymaps.begin(); | ||
142 | it!=m_keymaps.end(); ++it){ | ||
143 | qWarning(" code = %x", it.key()); | ||
144 | for(QMap<int, MapInfo>::Iterator it2=(*it)->begin(); | ||
145 | it2!=(*it)->end(); ++it2){ | ||
146 | qDebug(" [%x]-[%x][%x][%x][%x][%x]", it2.key(), | ||
147 | (*it2).keycode, | ||
148 | (*it2).modifiers, | ||
149 | (*it2).unicode, | ||
150 | (*it2).shift_unicode, | ||
151 | (*it2).ctrl_unicode); | ||
152 | } | ||
153 | } | ||
154 | } | ||
155 | |||
156 | bool KeyMappings::apply(int unicode, int keycode, int modifiers, | ||
157 | int keymask, bool isPress) | ||
158 | { | ||
159 | CodeMaps* map; | ||
160 | m_isMapped = false; | ||
161 | |||
162 | if(m_keymaps.contains(keycode)){ | ||
163 | map = m_keymaps[keycode]; | ||
164 | if(map->contains(keymask)){ | ||
165 | m_isMapped = true; | ||
166 | m_keyinfo = (*map)[keymask]; | ||
167 | } else { | ||
168 | int mask = -1; | ||
169 | for(CodeMaps::Iterator it=map->begin(); | ||
170 | it!=map->end(); ++it){ | ||
171 | if((keymask & it.key()) == it.key() | ||
172 | && it.key() > mask){ | ||
173 | mask = it.key(); | ||
174 | } | ||
175 | } | ||
176 | if(mask != -1){ | ||
177 | m_isMapped = true; | ||
178 | m_keyinfo = (*map)[mask]; | ||
179 | } | ||
180 | } | ||
181 | } | ||
182 | |||
183 | if(m_isMapped == false){ | ||
184 | QWSServer::KeyMap* cache = g_mapCache[keycode]; | ||
185 | if(cache == NULL){ | ||
186 | cache = new QWSServer::KeyMap(); | ||
187 | g_mapCache.insert(keycode, cache); | ||
188 | cache->unicode = cache->shift_unicode = cache->ctrl_unicode = 0; | ||
189 | } | ||
190 | if(cache->unicode == 0 || cache->shift_unicode == 0 || cache->ctrl_unicode == 0){ | ||
191 | QChar ch(unicode); | ||
192 | if(modifiers & Qt::ControlButton){ | ||
193 | cache->ctrl_unicode = unicode; | ||
194 | } else if(modifiers & Qt::ShiftButton){ | ||
195 | cache->shift_unicode = ch.upper().unicode(); | ||
196 | } else { | ||
197 | cache->unicode = ch.lower().unicode(); | ||
198 | } | ||
199 | } | ||
200 | m_keyinfo = MapInfo(keycode, 0, | ||
201 | cache->unicode, cache->shift_unicode, cache->ctrl_unicode); | ||
202 | if(m_keyinfo.isDefined){ | ||
203 | setOriginal(unicode, modifiers); | ||
204 | } else { | ||
205 | setUnicode(unicode); | ||
206 | } | ||
207 | } | ||
208 | |||
209 | #if 1 | ||
210 | if(isPress){ | ||
211 | if(m_keyinfo.keycode == Qt::Key_CapsLock){ | ||
212 | m_capslock = !m_capslock; | ||
213 | } | ||
214 | } | ||
215 | #endif | ||
216 | return(m_isMapped); | ||
217 | } | ||
218 | |||
219 | bool KeyMappings::apply(int keycode, int keymask, bool isPress) | ||
220 | { | ||
221 | CodeMaps* map; | ||
222 | m_isMapped = false; | ||
223 | |||
224 | if(m_keymaps.contains(keycode)){ | ||
225 | map = m_keymaps[keycode]; | ||
226 | if(map->contains(keymask)){ | ||
227 | m_isMapped = true; | ||
228 | m_keyinfo = (*map)[keymask]; | ||
229 | } else { | ||
230 | int mask = -1; | ||
231 | for(CodeMaps::Iterator it=map->begin(); | ||
232 | it!=map->end(); ++it){ | ||
233 | if((keymask & it.key()) == it.key() | ||
234 | && it.key() > mask){ | ||
235 | mask = it.key(); | ||
236 | } | ||
237 | } | ||
238 | if(mask != -1){ | ||
239 | m_isMapped = true; | ||
240 | m_keyinfo = (*map)[mask]; | ||
241 | } | ||
242 | } | ||
243 | } | ||
244 | if(m_isMapped == false){ | ||
245 | m_keyinfo = MapInfo(keycode); | ||
246 | } | ||
247 | #if 1 | ||
248 | if(isPress){ | ||
249 | if(m_keyinfo.keycode == Qt::Key_CapsLock){ | ||
250 | m_capslock = !m_capslock; | ||
251 | } | ||
252 | } | ||
253 | #endif | ||
254 | return(m_isMapped); | ||
255 | } | ||
256 | |||
257 | /** | ||
258 | * set original unicode | ||
259 | */ | ||
260 | void KeyMappings::setOriginal(int unicode, int modifiers) | ||
261 | { | ||
262 | if(modifiers & Qt::ControlButton){ | ||
263 | m_keyinfo.ctrl_unicode = unicode; | ||
264 | } else if(modifiers & Qt::ShiftButton){ | ||
265 | m_keyinfo.shift_unicode = unicode; | ||
266 | } else { | ||
267 | m_keyinfo.unicode = unicode; | ||
268 | } | ||
269 | } | ||
270 | |||
271 | void KeyMappings::setModifiers(int modifiers) | ||
272 | { | ||
273 | m_modifiers = modifiers; | ||
274 | m_modifiers |= (m_keyinfo.modifiers & 0xFFFF); | ||
275 | m_modifiers &= ~((m_keyinfo.modifiers >> 16) & 0xFFFF); | ||
276 | } | ||
277 | |||
278 | void KeyMappings::setUnicode(int unicode) | ||
279 | { | ||
280 | m_keyinfo.unicode = unicode; | ||
281 | m_keyinfo.shift_unicode = unicode; | ||
282 | m_keyinfo.ctrl_unicode = unicode; | ||
283 | } | ||
284 | |||
285 | void KeyMappings::setKeycode(int keycode) | ||
286 | { | ||
287 | m_keyinfo.keycode = keycode; | ||
288 | } | ||
289 | |||
290 | int KeyMappings::getUnicode() | ||
291 | { | ||
292 | int unicode; | ||
293 | if(m_modifiers & Qt::ControlButton){ | ||
294 | unicode = m_keyinfo.ctrl_unicode; | ||
295 | } else if(m_modifiers & Qt::ShiftButton){ | ||
296 | unicode = m_keyinfo.shift_unicode; | ||
297 | QChar ch(unicode); | ||
298 | if(m_capslock){ | ||
299 | unicode = ch.lower().unicode(); | ||
300 | } else { | ||
301 | unicode = ch.upper().unicode(); | ||
302 | } | ||
303 | } else { | ||
304 | unicode = m_keyinfo.unicode; | ||
305 | QChar ch(unicode); | ||
306 | if(m_capslock){ | ||
307 | unicode = ch.upper().unicode(); | ||
308 | } else { | ||
309 | unicode = ch.lower().unicode(); | ||
310 | } | ||
311 | } | ||
312 | qDebug("UNICODE[%d][%x][%x]", m_capslock, unicode, m_keyinfo.unicode); | ||
313 | #if 0 | ||
314 | if(m_isMapped && m_capslock){ | ||
315 | QChar ch(unicode); | ||
316 | QChar::Category category = ch.category(); | ||
317 | if(category == QChar::Letter_Uppercase){ | ||
318 | unicode = ch.lower().unicode(); | ||
319 | } else if(category == QChar::Letter_Lowercase){ | ||
320 | unicode = ch.upper().unicode(); | ||
321 | } | ||
322 | } | ||
323 | #endif | ||
324 | return(unicode); | ||
325 | } | ||
326 | |||
327 | int KeyMappings::getKeycode() | ||
328 | { | ||
329 | return(m_keyinfo.keycode); | ||
330 | } | ||
331 | |||
332 | int KeyMappings::getModifiers() | ||
333 | { | ||
334 | return(m_modifiers); | ||
335 | } | ||
336 | |||
337 | bool KeyMappings::isDefined() | ||
338 | { | ||
339 | return(m_keyinfo.isDefined); | ||
340 | } | ||