[mythtv] patch to add native lirc support

Anduin Withers awithers at anduin.com
Wed Aug 6 03:38:47 EDT 2003


> Using XSendEvent would be mostly OK, but it'd be nice to do it inside of
Qt
> for, say, the Qt/Embedded support =)

Attached is a patch with my best attempt at doing this without XSendEvent.

The reason I suggested going directly to X was that I couldn't find a clean
Qt approach. I still can't but the attached patch seems to work, (it should
also work with Qt 3.0, tested as much as one can without really reverting to
Qt 3.0). There is no "clean" interface to insert keyboard events from
another thread that I can find in Qt (Qt/Embedded seems to have something
though).

XSendEvent may seem all dirty and raw but after tromping through the way
keyboard handling is done in Qt, XSendEvent starts to look downright clean.

Note: The special escape key handling is necessary so that you don't get
stuck on some widgets (combo/edit boxes).

Known bug: The left/right keys behave poorly in edit boxes (at least the one
where you enter your system shutdown command).

Uneasiness: can't lock qApp. The modal event loop locks that sucker tight.

That said... it works rather nicely for me.

--
Anduin Withers

-------------- next part --------------
Index: lirc.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythtv/libs/libmyth/lirc.cpp,v
retrieving revision 1.2
diff -u -r1.2 lirc.cpp
--- lirc.cpp	5 Aug 2003 05:42:33 -0000	1.2
+++ lirc.cpp	6 Aug 2003 09:00:17 -0000
@@ -59,22 +59,61 @@
         {
             QKeySequence a(code);
 
-            if (!a.count()) 
+            int keycode = 0;
+#if (QT_VERSION > 0x030100)
+            for (unsigned int i = 0; i < a.count(); i++)
             {
-                cerr << "LircClient warning: attempt to convert '" << code 
-                     << "' to a key sequence failed. Fix your key mappings.\n";
-            }
+                keycode = a[i];
+#else
+                keycode = a;
+#endif
+                if (keycode)
+                {
+                    int mod = keycode & MODIFIER_MASK;
+                    int k = keycode & ~MODIFIER_MASK; /* trim off the mod */
+                    QString text(QChar(k >> 24));
+                    QKeyEvent *key_down = new QKeyEvent(QEvent::KeyPress, k,
+                                                        k >> 24, mod, text);
+                    QKeyEvent *key_up = new QKeyEvent(QEvent::KeyRelease, k,
+                                                      k >> 24, mod, text);
 
-            for (unsigned int i = 0; i < a.count(); i++)
+                    // Make the key events go to the widgets almost
+                    // the same way Qt would.
+                    //
+                    // Note: You might be tempted to lock the app mutex, don't.
+                    // If you are unfortunate enough to be in the modal
+                    // processing loop your lock will block until that loop
+                    // is done.
+                    QObject *key_target = NULL;
+                    if (!key_target) key_target = QWidget::keyboardGrabber();
+                    if (!key_target) {
+                        QWidget *focus_widget = qApp->focusWidget();
+                        if (focus_widget && focus_widget->isEnabled()) {
+                            key_target = focus_widget;
+
+                            // Yes this is special code for handling the
+                            // the escape key.
+                            if (key_down->key() == Key_Escape &&
+                                    focus_widget->topLevelWidget()) {
+                                key_target = focus_widget->topLevelWidget();
+                            }
+                        }
+                    }
+                    if (!key_target) key_target = mainWindow;
+                    QApplication::postEvent(key_target, key_down);
+                    QApplication::postEvent(key_target, key_up);
+                }
+#if (QT_VERSION > 0x030100)
+            }
+#endif
+            if (!keycode)
             {
-                int mod = a[i] & MODIFIER_MASK;
-                int k = a[i] & ~MODIFIER_MASK; /* trim off the mod */
-                QString text(QChar(k >> 24));
-                QKeyEvent *key = new QKeyEvent(QEvent::KeyPress, k, k >> 24, 
-                                               mod, text);
-                QApplication::postEvent(mainWindow, key);
+                cerr << "LircClient warning: attempt to convert '" <<
+                        code << "' to a key sequence failed. Fix your "
+                                "key mappings.\n";
             }
         }
+
         free(ir);
         if (ret == -1)
             break;


More information about the mythtv-dev mailing list