Diffstat (limited to 'noncore/applets/zkbapplet/keyzcfg/zkb.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/applets/zkbapplet/keyzcfg/zkb.cpp | 592 |
1 files changed, 592 insertions, 0 deletions
diff --git a/noncore/applets/zkbapplet/keyzcfg/zkb.cpp b/noncore/applets/zkbapplet/keyzcfg/zkb.cpp new file mode 100644 index 0000000..a357b88 --- a/dev/null +++ b/noncore/applets/zkbapplet/keyzcfg/zkb.cpp | |||
@@ -0,0 +1,592 @@ | |||
1 | #include "zkb.h" | ||
2 | |||
3 | /* OPIE */ | ||
4 | #include <opie2/odebug.h> | ||
5 | #include <opie2/okeyfilter.h> | ||
6 | |||
7 | #include <stdio.h> | ||
8 | |||
9 | // Implementation of Action class | ||
10 | Action::Action():state(0), keycode(0), unicode(0), flags(0) { | ||
11 | } | ||
12 | |||
13 | Action::Action(State* s, ushort kc, ushort uni, int f): | ||
14 | state(s), keycode(kc), unicode(uni), flags(f) { | ||
15 | } | ||
16 | |||
17 | Action::~Action() { | ||
18 | } | ||
19 | |||
20 | State* Action::getState() const { | ||
21 | return state; | ||
22 | } | ||
23 | |||
24 | void Action::setState(State* s) { | ||
25 | state = s; | ||
26 | setDefined(true); | ||
27 | } | ||
28 | |||
29 | bool Action::hasEvent() const { | ||
30 | return flags & Event; | ||
31 | } | ||
32 | |||
33 | void Action::setEvent(bool e) { | ||
34 | flags = (flags & ~Event) | ((e) ? Event : 0); | ||
35 | |||
36 | if (e) { | ||
37 | setDefined(true); | ||
38 | } else { | ||
39 | if (state == 0) { | ||
40 | setDefined(false); | ||
41 | } | ||
42 | } | ||
43 | } | ||
44 | |||
45 | bool Action::isDefined() const { | ||
46 | return flags & Defined; | ||
47 | } | ||
48 | |||
49 | void Action::setDefined(bool d) { | ||
50 | flags = (flags & ~Defined) | ((d) ? Defined : 0); | ||
51 | } | ||
52 | |||
53 | int Action::getKeycode() const { | ||
54 | return keycode; | ||
55 | } | ||
56 | |||
57 | void Action::setKeycode(int c) { | ||
58 | keycode = (ushort) c; | ||
59 | setEvent(true); | ||
60 | } | ||
61 | |||
62 | int Action::getUnicode() const { | ||
63 | return unicode; | ||
64 | } | ||
65 | |||
66 | void Action::setUnicode(int u) { | ||
67 | unicode = (ushort) u; | ||
68 | setEvent(true); | ||
69 | } | ||
70 | |||
71 | int Action::getModifiers() const { | ||
72 | int ret = 0; | ||
73 | if (flags & Shift_Mod) { | ||
74 | ret |= Qt::ShiftButton; | ||
75 | } | ||
76 | |||
77 | if (flags & Ctrl_Mod) { | ||
78 | ret |= Qt::ControlButton; | ||
79 | } | ||
80 | |||
81 | if (flags & Alt_Mod) { | ||
82 | ret |= Qt::AltButton; | ||
83 | } | ||
84 | |||
85 | if (flags & Keypad_Mod) { | ||
86 | ret |= Qt::Keypad; | ||
87 | } | ||
88 | |||
89 | return ret; | ||
90 | } | ||
91 | |||
92 | void Action::setModifiers(int m) { | ||
93 | int n = 0; | ||
94 | |||
95 | if (m & Qt::ShiftButton) { | ||
96 | n |= Shift_Mod; | ||
97 | } | ||
98 | |||
99 | if (m & Qt::ControlButton) { | ||
100 | n |= Ctrl_Mod; | ||
101 | } | ||
102 | |||
103 | if (m & Qt::AltButton) { | ||
104 | n |= Alt_Mod; | ||
105 | } | ||
106 | |||
107 | if (m & Qt::Keypad) { | ||
108 | n |= Keypad_Mod; | ||
109 | } | ||
110 | |||
111 | flags = flags & ~Mod_Bits | n; | ||
112 | setEvent(true); | ||
113 | } | ||
114 | |||
115 | bool Action::isPressed() const { | ||
116 | return (flags & Press) != 0; | ||
117 | } | ||
118 | |||
119 | void Action::setPressed(bool p) { | ||
120 | flags = (flags & ~Press) | ((p) ? Press : 0); | ||
121 | setEvent(true); | ||
122 | } | ||
123 | |||
124 | bool Action::isAutorepeat() const { | ||
125 | return (flags & Autorepeat) != 0; | ||
126 | } | ||
127 | |||
128 | void Action::setAutorepeat(bool p) { | ||
129 | flags = (flags & ~Autorepeat) | ((p) ? Autorepeat : 0); | ||
130 | setEvent(true); | ||
131 | } | ||
132 | |||
133 | // Implementation of State class | ||
134 | const short State::x1[] = { /* from 0x20 to 0x5f */ | ||
135 | 31, 0, 28, 3, 5, 6, 9, 28, /* 0x20 - 0x27 */ | ||
136 | 11, 26, 10, 13, 26, 1, 29, 27, /* 0x28 - 0x2f */ | ||
137 | 15, 16, 22, 4, 17, 19, 24, 20, /* 0x30 - 0x37 */ | ||
138 | 8, 14, 29, 26, 29, 12, 32, 27, /* 0x38 - 0x3f */ | ||
139 | 18, 0, 1, 2, 3, 4, 5, 6, /* 0x40 - 0x47 */ | ||
140 | 7, 8, 9, 10, 11, 12, 13, 14, /* 0x48 - 0x4f */ | ||
141 | 15, 16, 17, 18, 19, 20, 21, 22, /* 0x50 - 0x57 */ | ||
142 | 23, 24, 25, 30, -1, 26, 28, 7, /* 0x58 - 0x5f */ | ||
143 | 31, -1, -1, -1, -1, -1, -1, -1, /* 0x60 - 0x67 */ | ||
144 | -1, -1, -1, -1, -1, -1, -1, -1, /* 0x68 - 0x6f */ | ||
145 | -1, -1, -1, -1, -1, -1, -1, -1, /* 0x70 - 0x77 */ | ||
146 | -1, -1, -1, 29, 31, 32, 32, 28, /* 0x78 - 0x7f */ | ||
147 | }; | ||
148 | |||
149 | const short State::x2[] = { /* from 0x1000 to 0x1057*/ | ||
150 | 42, 36, -1, 30, 32, -1, -1, -1, /* 0x1000 - 0x1007 */ | ||
151 | -1, -1, -1, -1, -1, -1, -1, -1, /* 0x1008 - 0x100f */ | ||
152 | -1, -1, 44, 45, 46, 47, -1, -1, /* 0x1010 - 0x1017 */ | ||
153 | -1, -1, -1, -1, -1, -1, -1, -1, /* 0x1018 - 0x101f */ | ||
154 | 33, 35, 34, -1, 36, 27, -1, -1, /* 0x1020 - 0x1027 */ | ||
155 | -1, -1, -1, -1, -1, -1, -1, -1, /* 0x1028 - 0x102f */ | ||
156 | -1, -1, -1, -1, -1, -1, -1, -1, /* 0x1030 - 0x1037 */ | ||
157 | 37, 38, 40, 39, 41, -1, -1, -1, /* 0x1038 - 0x103f */ | ||
158 | -1, -1, -1, -1, -1, 35, -1, -1, /* 0x1040 - 0x1047 */ | ||
159 | -1, -1, -1, -1, -1, 48, -1, -1, /* 0x1048 - 0x104f */ | ||
160 | 43, 49, 50, -1, -1, -1, -1, -1, /* 0x1050 - 0x1057 */ | ||
161 | }; | ||
162 | |||
163 | State::State(State* p):parent(p), keys(0) { | ||
164 | keys = new Action[Key_Max * 2 + 1]; | ||
165 | } | ||
166 | |||
167 | State::State(const State& s) { | ||
168 | parent = s.parent; | ||
169 | keys = new Action[Key_Max * 2 + 1]; | ||
170 | memcpy(keys, s.keys, sizeof(Action) * (Key_Max * 2 + 1)); | ||
171 | } | ||
172 | |||
173 | State::~State() { | ||
174 | if (keys!=0) { | ||
175 | delete [] keys; | ||
176 | } | ||
177 | } | ||
178 | |||
179 | Action* State::get(int keycode, bool pressed, bool follow) const { | ||
180 | Action* ret = 0; | ||
181 | int n = translateKeycode(keycode); | ||
182 | |||
183 | if (n != -1 && keys != 0) { | ||
184 | if (pressed) { | ||
185 | n += Key_Max; | ||
186 | } | ||
187 | ret = &keys[n]; | ||
188 | } | ||
189 | |||
190 | if (ret==0 || !ret->isDefined()) { | ||
191 | if (follow && parent!=0) { | ||
192 | ret = parent->get(keycode, pressed, follow); | ||
193 | } | ||
194 | } | ||
195 | |||
196 | return ret; | ||
197 | } | ||
198 | |||
199 | bool State::set(int keycode, bool pressed, Action& action) { | ||
200 | int n = translateKeycode(keycode); | ||
201 | |||
202 | if (n==-1 || keys==0) { | ||
203 | return false; | ||
204 | } | ||
205 | |||
206 | if (pressed) { | ||
207 | n += Key_Max + 1; | ||
208 | } | ||
209 | |||
210 | keys[n] = action; | ||
211 | return true; | ||
212 | } | ||
213 | |||
214 | State* State::getParent() const { | ||
215 | return parent; | ||
216 | } | ||
217 | |||
218 | void State::setParent(State* s) { | ||
219 | parent = s; | ||
220 | } | ||
221 | |||
222 | int State::translateKeycode(int keycode) const { | ||
223 | if (keycode < 0x20) { | ||
224 | return -1; | ||
225 | } | ||
226 | |||
227 | if (keycode < 0x80) { | ||
228 | return x1[keycode - 0x20]; | ||
229 | } | ||
230 | |||
231 | if (keycode < 0x1000) { | ||
232 | return -1; | ||
233 | } | ||
234 | |||
235 | if (keycode < 0x1057) { | ||
236 | return x2[keycode - 0x1000]; | ||
237 | } | ||
238 | |||
239 | return -1; | ||
240 | } | ||
241 | |||
242 | // Implementation of Keymap class | ||
243 | Keymap::Keymap():enabled(true), currentState(0), autoRepeatAction(0), repeater(this) { | ||
244 | repeatDelay=400; | ||
245 | repeatPeriod=80; | ||
246 | connect(&repeater, SIGNAL(timeout()), this, SLOT(autoRepeat())); | ||
247 | } | ||
248 | |||
249 | Keymap::~Keymap() { | ||
250 | odebug << "removing keyboard filter for zkb"<<oendl; | ||
251 | Opie::Core::OKeyFilter::inst()->remHandler(this); | ||
252 | QMap<QString, State*>::Iterator it; | ||
253 | for(it = states.begin(); it != states.end(); ++it) { | ||
254 | delete it.data(); | ||
255 | } | ||
256 | states.clear(); | ||
257 | } | ||
258 | |||
259 | bool Keymap::filter(int unicode, int keycode, int modifiers, | ||
260 | bool isPress, bool autoRepeat) { | ||
261 | |||
262 | odebug << "filter: >>> unicode=" << unicode << ", keycode=" << keycode | ||
263 | << ", modifiers=" << modifiers << ", ispressed=" << isPress << oendl; | ||
264 | |||
265 | if (!enabled) { | ||
266 | return false; | ||
267 | } | ||
268 | |||
269 | // the second check is workaround to make suspend work if | ||
270 | // the user pressed it right after he did resume. for some | ||
271 | // reason the event sent by qt has autoRepeat true in this | ||
272 | // case | ||
273 | if (autoRepeat && keycode != 4177) { | ||
274 | return true; | ||
275 | } | ||
276 | |||
277 | (void) unicode; (void) modifiers; | ||
278 | |||
279 | Action* action = currentState->get(keycode, isPress, true); | ||
280 | if (action==0 || !action->isDefined()) { | ||
281 | return true; | ||
282 | } | ||
283 | |||
284 | if (action->hasEvent()) { | ||
285 | odebug << "filter:<<< unicode=" << action->getUnicode() << ", keycode=" << action->getKeycode() | ||
286 | << ", modifiers=" << action->getModifiers() << ", ispressed=" << action->isPressed() << oendl; | ||
287 | |||
288 | QWSServer::sendKeyEvent(action->getUnicode(), | ||
289 | action->getKeycode(), action->getModifiers(), | ||
290 | action->isPressed(), false); | ||
291 | } | ||
292 | |||
293 | if (action->isAutorepeat()) { | ||
294 | autoRepeatAction = action; | ||
295 | repeater.start(repeatDelay, TRUE); | ||
296 | } else { | ||
297 | autoRepeatAction = 0; | ||
298 | } | ||
299 | |||
300 | State* nstate = action->getState(); | ||
301 | if (nstate != 0) { | ||
302 | setCurrentState(nstate); | ||
303 | QString lbl = getCurrentLabel(); | ||
304 | if (!lbl.isEmpty()) { | ||
305 | emit stateChanged(lbl); | ||
306 | } | ||
307 | } | ||
308 | |||
309 | |||
310 | return true; | ||
311 | } | ||
312 | |||
313 | void Keymap::enable() { | ||
314 | enabled = true; | ||
315 | } | ||
316 | |||
317 | void Keymap::disable() { | ||
318 | enabled = false; | ||
319 | } | ||
320 | |||
321 | QStringList Keymap::listStates() { | ||
322 | QStringList ret; | ||
323 | |||
324 | QMap<QString, State*>::Iterator it; | ||
325 | for(it = states.begin(); it != states.end(); ++it) { | ||
326 | ret.append(it.key()); | ||
327 | } | ||
328 | |||
329 | return ret; | ||
330 | } | ||
331 | |||
332 | State* Keymap::getStateByName(const QString& name) { | ||
333 | QMap<QString, State*>::Iterator it = states.find(name); | ||
334 | |||
335 | if (it == states.end()) { | ||
336 | return 0; | ||
337 | } | ||
338 | |||
339 | return it.data(); | ||
340 | } | ||
341 | |||
342 | QStringList Keymap::listLabels() { | ||
343 | QStringList ret; | ||
344 | |||
345 | for(uint i = 0; i < labelList.count(); i++) { | ||
346 | ret.append(*labelList.at(i)); | ||
347 | } | ||
348 | |||
349 | return ret; | ||
350 | } | ||
351 | |||
352 | State* Keymap::getStateByLabel(const QString& label) { | ||
353 | QMap<QString, QString>::Iterator lit = labels.find(label); | ||
354 | State* state = 0; | ||
355 | |||
356 | if (lit == labels.end()) { | ||
357 | return 0; | ||
358 | } | ||
359 | |||
360 | QString name = lit.data(); | ||
361 | |||
362 | int n = name.find(":*"); | ||
363 | if (n>=0 && n==(int)(name.length()-2)) { | ||
364 | name=name.left(name.length() - 1); | ||
365 | |||
366 | n = currentStateName.findRev(":"); | ||
367 | if (n >= 0) { | ||
368 | name += currentStateName.mid(n+1); | ||
369 | } | ||
370 | } | ||
371 | |||
372 | // odebug << "look for: " << name.utf8() << "\n" << oendl; | ||
373 | QMap<QString, State*>::Iterator sit = states.find(name); | ||
374 | if (sit != states.end()) { | ||
375 | state = sit.data(); | ||
376 | } | ||
377 | |||
378 | return state; | ||
379 | } | ||
380 | |||
381 | bool Keymap::addState(const QString& name, State* state) { | ||
382 | if (states.find(name) != states.end()) { | ||
383 | return false; | ||
384 | } | ||
385 | |||
386 | states.insert(name, state); | ||
387 | lsmapInSync = false; | ||
388 | |||
389 | if (currentState == 0) { | ||
390 | setCurrentState(state); | ||
391 | } | ||
392 | |||
393 | return true; | ||
394 | } | ||
395 | |||
396 | State* Keymap::getCurrentState() const { | ||
397 | return currentState; | ||
398 | } | ||
399 | |||
400 | QString Keymap::getCurrentLabel() { | ||
401 | return currentLabel; | ||
402 | } | ||
403 | |||
404 | bool Keymap::setCurrentState(State* state) { | ||
405 | QMap<QString, State*>::Iterator it; | ||
406 | for(it = states.begin(); it != states.end(); ++it) { | ||
407 | State* s = it.data(); | ||
408 | if (s == state) { | ||
409 | currentState = s; | ||
410 | currentStateName = it.key(); | ||
411 | |||
412 | odebug << "state changed: " << (const char*)currentStateName.utf8() << oendl; | ||
413 | |||
414 | if (!lsmapInSync) { | ||
415 | generateLabelStateMaps(); | ||
416 | } | ||
417 | |||
418 | QMap<State*, QString>::Iterator tit; | ||
419 | tit = stateLabelMap.find(state); | ||
420 | if (tit != stateLabelMap.end()) { | ||
421 | currentLabel = tit.data(); | ||
422 | } else { | ||
423 | // odebug << "no label for: " + currentStateName + "\n" << oendl; | ||
424 | currentLabel = ""; | ||
425 | } | ||
426 | |||
427 | return true; | ||
428 | } | ||
429 | } | ||
430 | |||
431 | return false; | ||
432 | } | ||
433 | |||
434 | bool Keymap::removeState(const QString& name, bool force) { | ||
435 | QMap<QString, State*>::Iterator it = states.find(name); | ||
436 | |||
437 | if (it == states.end()) { | ||
438 | return false; | ||
439 | } | ||
440 | |||
441 | State* state = it.data(); | ||
442 | QList<Action> acts = findStateUsage(state); | ||
443 | |||
444 | if (!acts.isEmpty()) { | ||
445 | if (!force) { | ||
446 | return false; | ||
447 | } else { | ||
448 | for(Action* a = acts.first(); a != 0; a = acts.next()) { | ||
449 | a->setState(0); | ||
450 | } | ||
451 | } | ||
452 | } | ||
453 | |||
454 | if (state == currentState) { | ||
455 | if (states.begin() != states.end()) { | ||
456 | setCurrentState(states.begin().data()); | ||
457 | } | ||
458 | } | ||
459 | |||
460 | states.remove(it); | ||
461 | delete state; | ||
462 | |||
463 | lsmapInSync = false; | ||
464 | |||
465 | return true; | ||
466 | } | ||
467 | |||
468 | void Keymap::autoRepeat() { | ||
469 | if (autoRepeatAction != 0) { | ||
470 | odebug << "filter:<<< unicode=" << autoRepeatAction->getUnicode() | ||
471 | << ", keycode=" << autoRepeatAction->getKeycode() | ||
472 | << ", modifiers=" << autoRepeatAction->getModifiers() | ||
473 | << "ispressed=" << autoRepeatAction->isPressed() << oendl; | ||
474 | |||
475 | QWSServer::sendKeyEvent(autoRepeatAction->getUnicode(), | ||
476 | autoRepeatAction->getKeycode(), | ||
477 | autoRepeatAction->getModifiers(), | ||
478 | autoRepeatAction->isPressed(), true); | ||
479 | } | ||
480 | |||
481 | repeater.start(repeatPeriod, TRUE); | ||
482 | } | ||
483 | |||
484 | bool Keymap::addLabel(const QString& label, const QString& state, int index) { | ||
485 | if (labels.find(label) != labels.end()) { | ||
486 | return false; | ||
487 | } | ||
488 | |||
489 | labels.insert(label, state); | ||
490 | const QString& l = labels.find(label).key(); | ||
491 | if (index == -1) { | ||
492 | labelList.append(l); | ||
493 | } else { | ||
494 | labelList.insert(labelList.at(index), l); | ||
495 | } | ||
496 | |||
497 | lsmapInSync = false; | ||
498 | |||
499 | return true; | ||
500 | } | ||
501 | |||
502 | bool Keymap::removeLabel(const QString& label) { | ||
503 | |||
504 | if (labels.find(label) == labels.end()) { | ||
505 | return false; | ||
506 | } | ||
507 | |||
508 | labels.remove(label); | ||
509 | labelList.remove(label); | ||
510 | lsmapInSync = false; | ||
511 | |||
512 | if (label == currentLabel) { | ||
513 | currentLabel = ""; | ||
514 | } | ||
515 | |||
516 | return true; | ||
517 | } | ||
518 | |||
519 | int Keymap::getAutorepeatDelay() const { | ||
520 | return repeatDelay; | ||
521 | } | ||
522 | |||
523 | void Keymap::setAutorepeatDelay(int n) { | ||
524 | repeatDelay = n; | ||
525 | } | ||
526 | |||
527 | int Keymap::getAutorepeatPeriod() const { | ||
528 | return repeatPeriod; | ||
529 | } | ||
530 | |||
531 | void Keymap::setAutorepeatPeriod(int n) { | ||
532 | repeatPeriod = n; | ||
533 | } | ||
534 | |||
535 | QList<Action> Keymap::findStateUsage(State* s) { | ||
536 | QList<Action> ret; | ||
537 | |||
538 | QMap<QString, State*>::Iterator it; | ||
539 | for(it = states.begin(); it != states.end(); ++it) { | ||
540 | State* state = it.data(); | ||
541 | |||
542 | for(int i = 0; i < 0x1100; i++) { | ||
543 | Action* action = state->get(i, false); | ||
544 | if (action!=0 && action->getState()==s) { | ||
545 | ret.append(action); | ||
546 | } | ||
547 | |||
548 | action = state->get(i, true); | ||
549 | if (action!=0 && action->getState()==s) { | ||
550 | ret.append(action); | ||
551 | } | ||
552 | } | ||
553 | } | ||
554 | |||
555 | return ret; | ||
556 | } | ||
557 | |||
558 | void Keymap::generateLabelStateMaps() { | ||
559 | stateLabelMap.clear(); | ||
560 | |||
561 | QMap<QString, QString>::Iterator lit; | ||
562 | for(lit = labels.begin(); lit != labels.end(); ++lit) { | ||
563 | QString label = lit.key(); | ||
564 | QString name = lit.data(); | ||
565 | |||
566 | bool wc = false; | ||
567 | int n = name.find("*"); | ||
568 | if (n>=0 && n==(int)(name.length()-1)) { | ||
569 | name=name.left(name.length() - 1); | ||
570 | wc = true; | ||
571 | } | ||
572 | |||
573 | QMap<QString, State*>::Iterator sit; | ||
574 | for(sit = states.begin(); sit != states.end(); ++sit) { | ||
575 | QString sname = sit.key(); | ||
576 | State* state = sit.data(); | ||
577 | |||
578 | if (sname.length() < name.length()) { | ||
579 | continue; | ||
580 | } | ||
581 | |||
582 | if (sname.left(name.length()) == name) { | ||
583 | if (wc || sname.length()==name.length()) { | ||
584 | stateLabelMap.insert(state, label); | ||
585 | } | ||
586 | |||
587 | } | ||
588 | } | ||
589 | } | ||
590 | |||
591 | lsmapInSync = true; | ||
592 | } | ||