Release keys on exit to avoid leaving the keyboard in a strange state.
authorThibaut Girka <thib@sitedethib.com>
Thu, 21 Jul 2011 23:07:30 +0200
changeset 18 db9a4d812715
parent 17 2cb8af0a70ca
child 19 37cc422efb37
Release keys on exit to avoid leaving the keyboard in a strange state.
TODO
main.c
ui.c
ui.h
--- a/TODO	Thu Jul 21 19:03:19 2011 +0200
+++ b/TODO	Thu Jul 21 23:07:30 2011 +0200
@@ -1,14 +1,7 @@
-* handle things like ISO_Level3_Shift
-* Use Xutf8LookupString
 * use engravings for modifiers
 * handle portrait VS landscape
 * handle additional things:
     * Delete requests
 * fallback when no WM
-* save more space:
-    * don't trust sections left/top/width/height too much and compute it instead
-    * remove gaps
-* Remove the whole geometry thing and provide our own, simple, geometry:
-    <daniels> ThibG: you should know that geometry is deprecated and soon to be removed for a few reasons.
-    * Use symbolic names such as TLDE (see how layouts are defined:
-        /usr/share/X11/xkb/symbols/fr)
+* ability to start hidden
+* find out why modifier keys disappear when changing group, and put them back
--- a/main.c	Thu Jul 21 19:03:19 2011 +0200
+++ b/main.c	Thu Jul 21 23:07:30 2011 +0200
@@ -1,13 +1,24 @@
 #include "keyboard.h"
 #include "ui.h"
+#include <signal.h>
 #include <X11/Xlocale.h>
 
 
+VirtualKeyboardUI *ui;
+
+
+void signal_handler(int signal)
+{
+    ui_release_all_keys(ui);
+    exit(0);
+}
+
+
 int main(void)
 {
+    struct sigaction sa;
     Display *dpy;
     VirtualKeyboard *vkb;
-    VirtualKeyboardUI *ui;
     int xtest_event_base, xtest_error_base, xtest_major_opcode;
     int event_base, error_base, status;
 
@@ -56,6 +67,14 @@
         return 1;
     }
 
+
+    sigemptyset(&sa.sa_mask);
+    sa.sa_handler = signal_handler;
+    sa.sa_flags = 0;
+    sigaction(SIGTERM, &sa, NULL);
+    sigaction(SIGINT, &sa, NULL);
+    sigaction(SIGHUP, &sa, NULL);
+
     ui_create_window(ui);
     ui_show_window(ui);
 
--- a/ui.c	Thu Jul 21 19:03:19 2011 +0200
+++ b/ui.c	Thu Jul 21 23:07:30 2011 +0200
@@ -444,6 +444,36 @@
 }
 
 
+void ui_release_all_keys(VirtualKeyboardUI *ui)
+{
+    VirtualKey *vkey = ui->vkb->pressed_key;
+    if (vkey != NULL)
+    {
+        XTestFakeKeyEvent(ui->vkb->display, vkey->keycode, False, CurrentTime);
+        vkey->state = VirtualKeyReleased;
+
+        ui->vkb->pressed_key = NULL;
+    }
+
+    ui_release_latched_keys(ui);
+
+    XFlush(ui->vkb->display);
+}
+
+
+void ui_release_latched_keys(VirtualKeyboardUI *ui)
+{
+    unsigned int i;
+    for (i=0; i < ui->vkb->nb_keys; i++)
+    {
+        if (ui->vkb->keys[i].state == VirtualKeyLatched)
+        {
+            XTestFakeKeyEvent(ui->vkb->display, ui->vkb->keys[i].keycode, False, CurrentTime);
+            ui->vkb->keys[i].state = VirtualKeyReleased;
+        }
+    }
+}
+
 void ui_event_loop(VirtualKeyboardUI *ui)
 {
     VirtualKey *vkey = NULL;
@@ -474,16 +504,16 @@
                         {
                             vkey->state = VirtualKeyReleased;
                             if (action->any.type == XkbSA_LockMods)
-                                XTestFakeKeyEvent(ui->vkb->display, vkey->keycode, True, 0);
-                            XTestFakeKeyEvent(ui->vkb->display, vkey->keycode, False, 0);
+                                XTestFakeKeyEvent(ui->vkb->display, vkey->keycode, True, CurrentTime);
+                            XTestFakeKeyEvent(ui->vkb->display, vkey->keycode, False, CurrentTime);
                         }
                         else
                         {
-                            XTestFakeKeyEvent(ui->vkb->display, vkey->keycode, True, 0);
+                            XTestFakeKeyEvent(ui->vkb->display, vkey->keycode, True, CurrentTime);
                             if (action->any.type == XkbSA_LockMods)
                             {
                                 vkey->state = VirtualKeyToggled;
-                                XTestFakeKeyEvent(ui->vkb->display, vkey->keycode, False, 0);
+                                XTestFakeKeyEvent(ui->vkb->display, vkey->keycode, False, CurrentTime);
                             }
                             else
                                 vkey->state = VirtualKeyLatched;
@@ -495,7 +525,7 @@
                         {
                             vkey->state = VirtualKeyPressed;
                             ui->vkb->pressed_key = vkey;
-                            XTestFakeKeyEvent(ui->vkb->display, vkey->keycode, True, 0);
+                            XTestFakeKeyEvent(ui->vkb->display, vkey->keycode, True, CurrentTime);
                         }
                     }
 
@@ -506,20 +536,11 @@
                 vkey = ui->vkb->pressed_key;
                 if (vkey != NULL)
                 {
-                    XTestFakeKeyEvent(ui->vkb->display, vkey->keycode, False, 0);
+                    XTestFakeKeyEvent(ui->vkb->display, vkey->keycode, False, CurrentTime);
                     vkey->state = VirtualKeyReleased;
 
                     /* Release latched keys */
-                    /* TODO: since there will only be a small number of latched keys, this method is suboptimal */
-                    unsigned int i;
-                    for (i=0; i < ui->vkb->nb_keys; i++)
-                    {
-                        if (ui->vkb->keys[i].state == VirtualKeyLatched)
-                        {
-                            XTestFakeKeyEvent(ui->vkb->display, ui->vkb->keys[i].keycode, False, 0);
-                            ui->vkb->keys[i].state = VirtualKeyReleased;
-                        }
-                    }
+                    ui_release_latched_keys(ui);
 
                     ui->vkb->pressed_key = NULL;
                     ui_redraw(ui);
--- a/ui.h	Thu Jul 21 19:03:19 2011 +0200
+++ b/ui.h	Thu Jul 21 23:07:30 2011 +0200
@@ -21,4 +21,6 @@
 void ui_resize(VirtualKeyboardUI *ui, int width, int height);
 void ui_show_window(VirtualKeyboardUI *ui);
 void ui_hide_window(VirtualKeyboardUI *ui);
+void ui_release_latched_keys(VirtualKeyboardUI *ui);
+void ui_release_all_keys(VirtualKeyboardUI *ui);
 void ui_event_loop(VirtualKeyboardUI *ui);