Diffstat (limited to 'noncore/applets/keyhelper/keyhelperapplet/applet/KeyHelperWidget.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/applets/keyhelper/keyhelperapplet/applet/KeyHelperWidget.cpp | 490 |
1 files changed, 490 insertions, 0 deletions
diff --git a/noncore/applets/keyhelper/keyhelperapplet/applet/KeyHelperWidget.cpp b/noncore/applets/keyhelper/keyhelperapplet/applet/KeyHelperWidget.cpp new file mode 100644 index 0000000..7beb511 --- a/dev/null +++ b/noncore/applets/keyhelper/keyhelperapplet/applet/KeyHelperWidget.cpp | |||
@@ -0,0 +1,490 @@ | |||
1 | #include "KeyHelperWidget.h" | ||
2 | #include "QSafeDataStream.h" | ||
3 | #include "KHUtil.h" | ||
4 | |||
5 | QWidget* g_Widget = NULL; | ||
6 | int g_level = 0; | ||
7 | |||
8 | static const char* _version_ = "1.2.2"; | ||
9 | |||
10 | KeyHelperWidget::KeyHelperWidget(QWidget* parent, const char* name) | ||
11 | : QLabel(parent, name),disabled(Resource::loadPixmap("zkb-disabled")) | ||
12 | { | ||
13 | qDebug("KeyHelperWidget::KeyHelperWidget()"); | ||
14 | g_Widget = this; | ||
15 | m_defHandler = NULL; | ||
16 | //m_reset = false; | ||
17 | m_reset = true; | ||
18 | m_useFilter = false; | ||
19 | m_pHelper = NULL; | ||
20 | m_status = false; | ||
21 | |||
22 | //unset(); | ||
23 | initDebugLevel(); | ||
24 | |||
25 | m_pHelper = new KeyHelper(); | ||
26 | |||
27 | //qApp->installEventFilter(this); | ||
28 | |||
29 | connect(qwsServer, | ||
30 | SIGNAL(windowEvent(QWSWindow*, QWSServer::WindowEvent)), | ||
31 | this, | ||
32 | SLOT(windowEvent(QWSWindow*, QWSServer::WindowEvent))); | ||
33 | |||
34 | m_pChannel = new QCopChannel("QPE/KeyHelper", this); | ||
35 | connect(m_pChannel, SIGNAL(received(const QCString&, const QByteArray&)), | ||
36 | this, SLOT(receiveMessage(const QCString&, const QByteArray&))); | ||
37 | m_pSysChannel = new QCopChannel("QPE/System", this); | ||
38 | connect(m_pSysChannel, SIGNAL(received(const QCString&, const QByteArray&)), | ||
39 | this, SLOT(sysMessage(const QCString&, const QByteArray&))); | ||
40 | |||
41 | //AppLnkManager::init(); | ||
42 | setFixedWidth ( AppLnk::smallIconSize() ); | ||
43 | setFixedHeight ( AppLnk::smallIconSize() ); | ||
44 | setPixmap(disabled); | ||
45 | init(); | ||
46 | } | ||
47 | |||
48 | KeyHelperWidget::~KeyHelperWidget() | ||
49 | { | ||
50 | qDebug("KeyHelperWidget::~KeyHelperWidget()"); | ||
51 | disconnect(qwsServer, | ||
52 | SIGNAL(windowEvent(QWSWindow*, QWSServer::WindowEvent)), | ||
53 | this, | ||
54 | SLOT(windowEvent(QWSWindow*, QWSServer::WindowEvent))); | ||
55 | disconnect(m_pChannel, SIGNAL(received(const QCString&, const QByteArray&)), | ||
56 | this, SLOT(receiveMessage(const QCString&, const QByteArray&))); | ||
57 | unset(); | ||
58 | if (m_pHelper) delete m_pHelper; | ||
59 | if (m_pChannel) delete m_pChannel; | ||
60 | if (m_pSysChannel) delete m_pSysChannel; | ||
61 | m_pHelper = NULL; | ||
62 | setDebugLevel(0); | ||
63 | } | ||
64 | |||
65 | void KeyHelperWidget::mouseReleaseEvent(QMouseEvent*) | ||
66 | { | ||
67 | ConfigEx::getInstance("keyhelper").setConfig("keyhelper"); | ||
68 | } | ||
69 | |||
70 | void KeyHelperWidget::receiveMessage( | ||
71 | const QCString& msg, const QByteArray& data) | ||
72 | { | ||
73 | if(m_pHelper == NULL){ | ||
74 | return; | ||
75 | } | ||
76 | QSafeDataStream stream(data, IO_ReadOnly); | ||
77 | if(msg == "event(int,int,int,int,int)"){ | ||
78 | int unicode; | ||
79 | int keycode; | ||
80 | int modifiers; | ||
81 | int isPress; | ||
82 | int autoRepeat; | ||
83 | stream >> unicode >> keycode >> modifiers >> isPress >> autoRepeat; | ||
84 | doEvent(unicode, keycode, modifiers, isPress, autoRepeat); | ||
85 | } else if(msg == "event(QString,int)"){ | ||
86 | QString key; | ||
87 | int isPress; | ||
88 | stream >> key >> isPress; | ||
89 | doEvent(key, isPress); | ||
90 | } else if(msg == "enable()"){ | ||
91 | enable(); | ||
92 | } else if(msg == "disable()"){ | ||
93 | disable(); | ||
94 | } else if(msg == "pause()"){ | ||
95 | pause(); | ||
96 | } else if(msg == "restart()"){ | ||
97 | restart(); | ||
98 | } else if(msg == "reload()"){ | ||
99 | m_xmlfile = QString::null; | ||
100 | doReload(); | ||
101 | } else if(msg == "reload(QString)"){ | ||
102 | stream >> m_xmlfile; | ||
103 | doReload(false); | ||
104 | } else if(msg == "version()"){ | ||
105 | version(); | ||
106 | } else if(msg == "repeater(int)"){ | ||
107 | int mode; | ||
108 | stream >> mode; | ||
109 | m_pHelper->m_oRepeater.setMode(mode); | ||
110 | } else if(msg == "hook(QString)"){ | ||
111 | QString s; | ||
112 | stream >> s; | ||
113 | m_pHelper->m_oAction.setHook(s.local8Bit()); | ||
114 | } else if(msg == "unhook()"){ | ||
115 | m_pHelper->m_oAction.setHook(""); | ||
116 | } else if(msg == "config()"){ | ||
117 | ConfigEx::getInstance("keyhelper").setConfig("keyhelper"); | ||
118 | } else if(msg == "config(QString)"){ | ||
119 | QString name; | ||
120 | stream >> name; | ||
121 | if(name == QString::null){ | ||
122 | ConfigEx::getInstance("keyhelper").setConfig("keyhelper"); | ||
123 | } else { | ||
124 | ConfigEx::getInstance("keyhelper").setConfig(name); | ||
125 | } | ||
126 | } else if(msg == "capture(int)"){ | ||
127 | int enable; | ||
128 | stream >> enable; | ||
129 | m_pHelper->m_oAction.setCapture(enable); | ||
130 | } else if(msg == "statistics()"){ | ||
131 | int level = g_level; | ||
132 | if(level == 0){ | ||
133 | setDebugLevel(1); | ||
134 | } | ||
135 | m_pHelper->statistics(); | ||
136 | if(level == 0){ | ||
137 | setDebugLevel(0); | ||
138 | } | ||
139 | } else if(msg == "dumpkeymap()"){ | ||
140 | int level = g_level; | ||
141 | if(level == 0){ | ||
142 | setDebugLevel(1); | ||
143 | } | ||
144 | m_pHelper->dumpkeymap(); | ||
145 | if(level == 0){ | ||
146 | setDebugLevel(0); | ||
147 | } | ||
148 | } else if(msg == "debug(int)"){ | ||
149 | int level; | ||
150 | stream >> level; | ||
151 | setDebugLevel(level); | ||
152 | } | ||
153 | } | ||
154 | |||
155 | void KeyHelperWidget::doReload(bool showstatus) | ||
156 | { | ||
157 | ConfigEx& cfg = ConfigEx::getInstance("keyhelper"); | ||
158 | QString oldgroup = cfg.getGroup(); | ||
159 | cfg.setGroup("Global"); | ||
160 | m_status = false; | ||
161 | if(showstatus && (cfg.readNumEntry("ShowStatusOnReload", 1) == 1)){ | ||
162 | m_status = true; | ||
163 | version(); | ||
164 | QCopEnvelope("QPE/System", "busy()"); | ||
165 | } | ||
166 | cfg.setGroup(oldgroup); | ||
167 | QTimer::singleShot(0, this, SLOT(reload())); | ||
168 | } | ||
169 | |||
170 | void KeyHelperWidget::doEvent(int unicode, int keycode, int modifiers, int isPress, int autoRepeat) | ||
171 | { | ||
172 | if(isPress == 0 || isPress == 1){ | ||
173 | m_pHelper->m_oAction.setAction(unicode, keycode, modifiers, isPress, autoRepeat); | ||
174 | m_pHelper->m_oAction.doAction(); | ||
175 | } else { | ||
176 | /* press & release */ | ||
177 | m_pHelper->m_oAction.setAction(unicode, keycode, modifiers, 1, autoRepeat); | ||
178 | m_pHelper->m_oAction.doAction(); | ||
179 | m_pHelper->m_oAction.setAction(unicode, keycode, modifiers, 0, autoRepeat); | ||
180 | m_pHelper->m_oAction.doAction(); | ||
181 | } | ||
182 | } | ||
183 | |||
184 | void KeyHelperWidget::doEvent(const QString& key, int isPress) | ||
185 | { | ||
186 | int unicode,keycode; | ||
187 | int modifiers = 0; | ||
188 | int pos; | ||
189 | QString keyname = key; | ||
190 | pos = keyname.find("+SHIFT", 0, FALSE); | ||
191 | if(pos > 0){ | ||
192 | modifiers |= Qt::ShiftButton; | ||
193 | keyname.remove(pos, 6); | ||
194 | } | ||
195 | pos = keyname.find("+CTRL", 0, FALSE); | ||
196 | if(pos > 0){ | ||
197 | modifiers |= Qt::ControlButton; | ||
198 | keyname.remove(pos, 5); | ||
199 | } | ||
200 | pos = keyname.find("+ALT", 0, FALSE); | ||
201 | if(pos > 0){ | ||
202 | modifiers |= Qt::AltButton; | ||
203 | keyname.remove(pos, 4); | ||
204 | } | ||
205 | if(keyname.length() > 1){ | ||
206 | unicode = 0xffff; | ||
207 | keycode = KeyNames::getCode(keyname); | ||
208 | /* get unicode */ | ||
209 | const QWSServer::KeyMap* m; | ||
210 | for(m=QWSServer::keyMap(); m->key_code != 0; m++){ | ||
211 | if(m->key_code == keycode){ | ||
212 | if(modifiers & Qt::ControlButton){ | ||
213 | unicode = m->ctrl_unicode; | ||
214 | } else if(modifiers & Qt::ShiftButton){ | ||
215 | unicode = m->shift_unicode; | ||
216 | } else { | ||
217 | unicode = m->unicode; | ||
218 | } | ||
219 | break; | ||
220 | } | ||
221 | } | ||
222 | } else { | ||
223 | const QWSServer::KeyMap* m; | ||
224 | keycode = 0; | ||
225 | unicode = keyname[0].unicode(); | ||
226 | /* check unicode */ | ||
227 | for(m=QWSServer::keyMap(); keycode == 0 && m->key_code != 0; m++){ | ||
228 | if(m->unicode == unicode){ | ||
229 | keycode = m->key_code; | ||
230 | break; | ||
231 | } | ||
232 | } | ||
233 | /* check shift_unicode */ | ||
234 | for(m=QWSServer::keyMap(); keycode == 0 && m->key_code != 0; m++){ | ||
235 | if(m->shift_unicode == unicode){ | ||
236 | keycode = m->key_code; | ||
237 | modifiers |= Qt::ShiftButton; | ||
238 | break; | ||
239 | } | ||
240 | } | ||
241 | /* check ctrl_unicode */ | ||
242 | for(m=QWSServer::keyMap(); keycode == 0 && m->key_code != 0; m++){ | ||
243 | if(m->ctrl_unicode == unicode){ | ||
244 | keycode = m->key_code; | ||
245 | modifiers |= Qt::ControlButton; | ||
246 | break; | ||
247 | } | ||
248 | } | ||
249 | } | ||
250 | doEvent(unicode, keycode, modifiers, isPress, 0); | ||
251 | } | ||
252 | |||
253 | void KeyHelperWidget::sysMessage( | ||
254 | const QCString& msg, const QByteArray& data) | ||
255 | { | ||
256 | QSafeDataStream stream(data, IO_ReadOnly); | ||
257 | if(msg == "linkChanged(QString)"){ | ||
258 | ConfigEx& cfg = ConfigEx::getInstance("keyhelper"); | ||
259 | QString oldgroup = cfg.getGroup(); | ||
260 | if(cfg.readNumEntry("DetectLinkChange", 1) == 1){ | ||
261 | AppLnkManager::init(true); | ||
262 | reload(); | ||
263 | } | ||
264 | cfg.setGroup(oldgroup); | ||
265 | } | ||
266 | } | ||
267 | |||
268 | void MsgHandler(QtMsgType type, const char* msg) | ||
269 | { | ||
270 | switch(type){ | ||
271 | case QtDebugMsg: | ||
272 | if(g_level >= 2){ | ||
273 | syslog(LOG_LOCAL5|LOG_DEBUG, | ||
274 | "<2>%s", msg); | ||
275 | } | ||
276 | break; | ||
277 | case QtWarningMsg: | ||
278 | if(g_level >= 1){ | ||
279 | syslog(LOG_LOCAL5|LOG_DEBUG, | ||
280 | "<1>%s", msg); | ||
281 | } | ||
282 | break; | ||
283 | default: | ||
284 | break; | ||
285 | } | ||
286 | } | ||
287 | |||
288 | void KeyHelperWidget::initDebugLevel() | ||
289 | { | ||
290 | ConfigEx& cfg = ConfigEx::getInstance("keyhelper"); | ||
291 | cfg.setGroup("Global"); | ||
292 | |||
293 | int level = cfg.readNumEntry("DebugLevel", 0); | ||
294 | setDebugLevel(level); | ||
295 | } | ||
296 | |||
297 | void KeyHelperWidget::setDebugLevel(int level) | ||
298 | { | ||
299 | #ifdef QT_QWS_EBX | ||
300 | static bool noDebug = true; | ||
301 | g_level = level; | ||
302 | if(g_level > 0){ | ||
303 | if(noDebug){ | ||
304 | m_defHandler = qInstallMsgHandler(MsgHandler); | ||
305 | noDebug = false; | ||
306 | } | ||
307 | } else { | ||
308 | qInstallMsgHandler(m_defHandler); | ||
309 | noDebug = true; | ||
310 | } | ||
311 | #endif | ||
312 | } | ||
313 | |||
314 | void KeyHelperWidget::enable() | ||
315 | { | ||
316 | m_enable = true; | ||
317 | m_pHelper->enable(); | ||
318 | //set(); | ||
319 | QTimer::singleShot(0, this, SLOT(set())); | ||
320 | } | ||
321 | |||
322 | void KeyHelperWidget::disable() | ||
323 | { | ||
324 | m_enable = false; | ||
325 | m_pHelper->disable(); | ||
326 | unset(); | ||
327 | } | ||
328 | |||
329 | void KeyHelperWidget::pause() | ||
330 | { | ||
331 | m_saved = m_enable; | ||
332 | disable(); | ||
333 | } | ||
334 | |||
335 | void KeyHelperWidget::restart() | ||
336 | { | ||
337 | if(m_saved){ | ||
338 | enable(); | ||
339 | } | ||
340 | } | ||
341 | |||
342 | void KeyHelperWidget::reload() | ||
343 | { | ||
344 | disable(); | ||
345 | ConfigEx& cfg = ConfigEx::getInstance("keyhelper"); | ||
346 | cfg.reload(); | ||
347 | if(m_pHelper->reload(m_xmlfile) == false){ | ||
348 | if(m_status){ | ||
349 | Global::statusMessage("KeyHelper: Load Error"); | ||
350 | } | ||
351 | } | ||
352 | init(); | ||
353 | if(m_status){ | ||
354 | QCopEnvelope e("QPE/System", "notBusy(QString)"); | ||
355 | const QString app = KHUtil::currentApp(); | ||
356 | e << app; | ||
357 | m_status = false; | ||
358 | } | ||
359 | } | ||
360 | |||
361 | void KeyHelperWidget::version() | ||
362 | { | ||
363 | QString ver = "KeyHelper "; | ||
364 | ver.append(_version_); | ||
365 | Global::statusMessage(ver); | ||
366 | } | ||
367 | |||
368 | void KeyHelperWidget::init() | ||
369 | { | ||
370 | AppLnkManager::init(); | ||
371 | #if 0 | ||
372 | if(m_pHelper == NULL){ | ||
373 | m_pHelper = new KeyHelper(); | ||
374 | } | ||
375 | #endif | ||
376 | loadUseFilterApps(); | ||
377 | enable(); | ||
378 | } | ||
379 | |||
380 | void KeyHelperWidget::set() | ||
381 | { | ||
382 | if(m_pHelper != NULL && m_enable == true && m_useFilter == false){ | ||
383 | qWarning("KeyHelperWidget::set()"); | ||
384 | m_pHelper->set(); | ||
385 | } | ||
386 | } | ||
387 | |||
388 | void KeyHelperWidget::unset() | ||
389 | { | ||
390 | m_pHelper->unset(); | ||
391 | } | ||
392 | |||
393 | void KeyHelperWidget::loadUseFilterApps() | ||
394 | { | ||
395 | ConfigEx& cfg = ConfigEx::getInstance("keyhelper"); | ||
396 | |||
397 | cfg.setGroup("Global"); | ||
398 | m_apps = cfg.readListEntry("UseFilterApps", ','); | ||
399 | |||
400 | if(m_apps.isEmpty()){ | ||
401 | /* default */ | ||
402 | m_apps.append("CRIM"); | ||
403 | m_apps.append("Jpn50Pad"); | ||
404 | m_apps.append("JpnKtnPad"); | ||
405 | m_apps.append("JpnNumPad"); | ||
406 | m_apps.append("JpnSymPad"); | ||
407 | m_apps.append("Keyboard"); | ||
408 | m_apps.append("IMWidget"); /* IMKit */ | ||
409 | m_apps.append("POBox"); /* QPOBox */ | ||
410 | } | ||
411 | } | ||
412 | |||
413 | #if 0 | ||
414 | void KeyHelperWidget::windowEvent(QWSWindow* w, QWSServer::WindowEvent e) | ||
415 | { | ||
416 | if(m_apps.contains(w->name())){ | ||
417 | switch(e){ | ||
418 | case QWSServer::Hide: | ||
419 | case QWSServer::Destroy: | ||
420 | m_useFilter = false; | ||
421 | //m_reset = true; | ||
422 | QTimer::singleShot(0, this, SLOT(set())); | ||
423 | break; | ||
424 | case QWSServer::Create: | ||
425 | case QWSServer::Raise: | ||
426 | case QWSServer::Show: | ||
427 | m_useFilter = true; | ||
428 | //m_reset = false; | ||
429 | m_reset = true; | ||
430 | break; | ||
431 | default: | ||
432 | break; | ||
433 | } | ||
434 | } else if(w->client()->identity() != NULL && w->name() != NULL){ | ||
435 | switch(e){ | ||
436 | #if 0 | ||
437 | case QWSServer::Create: | ||
438 | case QWSServer::Hide: | ||
439 | if(m_useFilter == false && m_reset){ | ||
440 | m_reset = false; | ||
441 | set(); | ||
442 | //QTimer::singleShot(0, this, SLOT(set())); | ||
443 | } | ||
444 | break; | ||
445 | #else | ||
446 | case QWSServer::Hide: | ||
447 | case QWSServer::Destroy: | ||
448 | //if(m_useFilter == false && m_reset){ | ||
449 | if(m_reset){ | ||
450 | m_reset = false; | ||
451 | set(); | ||
452 | //QTimer::singleShot(0, this, SLOT(set())); | ||
453 | } | ||
454 | break; | ||
455 | case QWSServer::Create: | ||
456 | case QWSServer::Raise: | ||
457 | case QWSServer::Show: | ||
458 | m_reset = true; | ||
459 | break; | ||
460 | #endif | ||
461 | default: | ||
462 | #if 0 | ||
463 | if(m_reset == true){ | ||
464 | m_reset = false; | ||
465 | set(); | ||
466 | } | ||
467 | #endif | ||
468 | break; | ||
469 | } | ||
470 | } | ||
471 | if(w->name() != NULL){ | ||
472 | qWarning("[%s][%s][%x][%s]", | ||
473 | w->name().latin1(), | ||
474 | w->caption().latin1(), | ||
475 | e, | ||
476 | w->client()->identity().latin1()); | ||
477 | } | ||
478 | } | ||
479 | |||
480 | #endif | ||
481 | |||
482 | bool KeyHelperWidget::eventFilter(QObject* o, QEvent* e) | ||
483 | { | ||
484 | return QWidget::eventFilter(o, e); | ||
485 | } | ||
486 | |||
487 | int KeyHelperWidget::position() | ||
488 | { | ||
489 | return 3; | ||
490 | } | ||