Skip to contentSkip to conversation

ui/cocoa.m: prevent stuck key situation

PATCH Superseded Programmingkid 2 participants 6 messages Sep 18, 2015 – Sep 23, 2015

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ui/cocoa.m: prevent stuck key situation

When the user puts QEMU in the background while holding down a key, QEMU will not receive the keyup event when the user lets go of the key. When the user goes back to QEMU, QEMU will think the key is still down causing stuck key symptoms. This patch fixes this problem by releasing all keys when QEMU goes into the background.

---
 ui/cocoa.m |   17 ++++++++++++++++-
 1 files changed, 16 insertions(+), 1 deletions(-)
ui/cocoa.m +16 -1
index 334e6f6..d07b22d 100644
@@ -69,6 +69,7 @@ char **gArgv;
 bool stretch_video;
 NSTextField *pauseLabel;
 NSArray * supportedImageFileTypes;
+int modifiers_state[256];
 
 // keymap conversion
 int keymap[] =
@@ -274,7 +275,6 @@ static void handleAnyDeviceErrors(Error * err)
     NSWindow *fullScreenWindow;
     float cx,cy,cw,ch,cdx,cdy;
     CGDataProviderRef dataProviderRef;
-    int modifiers_state[256];
     BOOL isMouseGrabbed;
     BOOL isFullscreen;
     BOOL isAbsoluteEnabled;
@@ -933,6 +933,21 @@ QemuCocoaView *cocoaView;
     return YES;
 }
 
+/* Called when QEMU goes into the background */
+- (void) applicationWillResignActive: (NSNotification *)aNotification
+{
+    COCOA_DEBUG("QemuCocoaAppController: applicationWillResignActive\n");
+    int keycode, index;
+    const int max_index = 126; /* This is the size of the keymap array */
+
+    /* Release all the keys to prevent a stuck key situation */
+    for(index = 0; index <= max_index; index++) {
+        keycode = keymap[index];
+        modifiers_state[keycode] = 0;
+        qemu_input_event_send_key_number(dcl->con, keycode, false);
+    }
+}
+
 - (void)startEmulationWithArgc:(int)argc argv:(char**)argv
 {
     COCOA_DEBUG("QemuCocoaAppController: startEmulationWithArgc\n");
Signature · 1 line
1.7.5.4
#1 lore raw
Quoted 9 lines · Programmingkid

When the user puts QEMU in the background while holding down a key, QEMU will
not receive the keyup event when the user lets go of the key. When the user goes
back to QEMU, QEMU will think the key is still down causing stuck key symptoms.
This patch fixes this problem by releasing all keys when QEMU goes into the background.

Quoted 5 lines
---
 ui/cocoa.m |   17 ++++++++++++++++-
 1 files changed, 16 insertions(+), 1 deletions(-)

Can you please fix how you're sending patch emails not to send them as HTML? This really does mess up some of the tools we use to process patchmail, and it's a pain to have to handle them manually (also error-prone; last patch of yours I applied I messed up fixing the authorship on).

The git send-email tool is one way of doing this.

thanks
-- PMM

#2 lore raw
Quoted 10 lines · Peter Maydell
Quoted 9 lines · Programmingkid

When the user puts QEMU in the background while holding down a key, QEMU will
not receive the keyup event when the user lets go of the key. When the user goes
back to QEMU, QEMU will think the key is still down causing stuck key symptoms.
This patch fixes this problem by releasing all keys when QEMU goes into the background.

Quoted 8 lines
Quoted 5 lines

---
ui/cocoa.m | 17 ++++++++++++++++-

1 files changed, 16 insertions(+), 1 deletions(-)

Can you please fix how you're sending patch emails not to send them as HTML?

I just copy and paste them into Apple's Mail application. Didn't know it makes it into html.

Quoted 6 lines

This really does mess up some of the tools we
use to process patchmail, and it's a pain to have to handle them manually (also error-prone; last patch of yours I applied I messed up fixing the authorship on).

The git send-email tool is one way of doing this.

I will learn this send-email tool today.

#3 lore raw
In reply to Programmingkid
Quoted 8 lines · Programmingkid

When the user puts QEMU in the background while holding down a key, QEMU will
not receive the keyup event when the user lets go of the key. When the user goes
back to QEMU, QEMU will think the key is still down causing stuck key symptoms.
This patch fixes this problem by releasing all keys when QEMU goes into the background.

Looks like maybe you're not wrapping lines early enough in your commit messages, resulting in this ugly effect when they're quoted. It's best to not have lines longer than 75 chars or so.

Quoted 11 lines
---
 ui/cocoa.m |   17 ++++++++++++++++-
 1 files changed, 16 insertions(+), 1 deletions(-)
ui/cocoa.m +0 -0
index 334e6f6..d07b22d 100644

ui/cocoa.m char **gArgv; · line 72

Quoted 4 lines
ui/cocoa.m +1 -0
@@ -69,6 +69,7 @@ char **gArgv;
 bool stretch_video;
 NSTextField *pauseLabel;
 NSArray * supportedImageFileTypes;
+int modifiers_state[256];

Rather than making this global, could we have the applicationWillResignActive method call a new raiseAllKeys method on the NSView?

Quoted 15 lines
ui/cocoa.m +0 -1
 // keymap conversion
 int keymap[] =
@@ -274,7 +275,6 @@ static void handleAnyDeviceErrors(Error * err)
     NSWindow *fullScreenWindow;
     float cx,cy,cw,ch,cdx,cdy;
     CGDataProviderRef dataProviderRef;
-    int modifiers_state[256];
     BOOL isMouseGrabbed;
     BOOL isFullscreen;
     BOOL isAbsoluteEnabled;
@@ -933,6 +933,21 @@ QemuCocoaView *cocoaView;
     return YES;

ui/cocoa.m QemuCocoaView *cocoaView; · line 943

Quoted 10 lines
ui/cocoa.m +6 -0
 }
 
 
 
+/* Called when QEMU goes into the background */
+- (void) applicationWillResignActive: (NSNotification *)aNotification
+{
+    COCOA_DEBUG("QemuCocoaAppController: applicationWillResignActive\n");
+    int keycode, index;
+    const int max_index = 126; /* This is the size of the keymap array */

Hardcoding array sizes is never a good idea. We have an ARRAY_SIZE macro which automatically gets it right.

ui/cocoa.m QemuCocoaView *cocoaView; · line 948

Quoted 7 lines
ui/cocoa.m +7 -0
+
+    /* Release all the keys to prevent a stuck key situation */
+    for(index = 0; index <= max_index; index++) {
+        keycode = keymap[index];
+        modifiers_state[keycode] = 0;
+        qemu_input_event_send_key_number(dcl->con, keycode, false);
+    }

This will send key-up events even for keys which are already up. Instead you can just send events for only the keys which are down (and avoid the lookup in keymap[] too):

    for (i = 0; i < ARRAY_SIZE(modifiers_state); i++) {
        if (modifiers_state[i])) {
            modifiers_state[i] = 0;
            qemu_input_event_send_key_number(dcl->con, i, false);
        }
    }

thanks
-- PMM

#4 lore raw
Quoted 10 lines · Peter Maydell
Quoted 8 lines · Programmingkid

When the user puts QEMU in the background while holding down a key, QEMU will
not receive the keyup event when the user lets go of the key. When the user goes
back to QEMU, QEMU will think the key is still down causing stuck key symptoms.
This patch fixes this problem by releasing all keys when QEMU goes into the background.

Quoted 3 lines

Looks like maybe you're not wrapping lines early enough in your commit messages, resulting in this ugly effect when they're quoted. It's best to not have lines longer than 75 chars or so.

Sorry, will make the lines shorter in the future.

Quoted 7 lines
Quoted 7 lines

---
ui/cocoa.m | 17 ++++++++++++++++-

1 files changed, 16 insertions(+), 1 deletions(-)
Quoted 12 lines

ui/cocoa.m char **gArgv; · line 72

Quoted 9 lines
ui/cocoa.m +1 -0
index 334e6f6..d07b22d 100644
@@ -69,6 +69,7 @@ char **gArgv;
 bool stretch_video;
 NSTextField *pauseLabel;
 NSArray * supportedImageFileTypes;
+int modifiers_state[256];

Rather than making this global, could we have the applicationWillResignActive method call a new raiseAllKeys method on the NSView?

We could do that, but isn't this more of an app controller function?

Quoted 19 lines
Quoted 19 lines
ui/cocoa.m +0 -0
 // keymap conversion

int keymap[] =

ui/cocoa.m +0 -1
@@ -274,7 +275,6 @@ static void handleAnyDeviceErrors(Error * err)
    NSWindow *fullScreenWindow;
    float cx,cy,cw,ch,cdx,cdy;
    CGDataProviderRef dataProviderRef;
-    int modifiers_state[256];
    BOOL isMouseGrabbed;
    BOOL isFullscreen;
    BOOL isAbsoluteEnabled;
@@ -933,6 +933,21 @@ QemuCocoaView *cocoaView;
    return YES;
 }
 
 
 
Quoted 9 lines

ui/cocoa.m QemuCocoaView *cocoaView; · line 941

Quoted 6 lines
ui/cocoa.m +6 -0
+/* Called when QEMU goes into the background */
+- (void) applicationWillResignActive: (NSNotification *)aNotification
+{
+    COCOA_DEBUG("QemuCocoaAppController: applicationWillResignActive\n");
+    int keycode, index;
+    const int max_index = 126; /* This is the size of the keymap array */

Hardcoding array sizes is never a good idea. We have an ARRAY_SIZE macro which automatically gets it right.

Didn't know about this macro. Will use it in the next patch.

Quoted 15 lines

ui/cocoa.m QemuCocoaView *cocoaView; · line 948

Quoted 9 lines
ui/cocoa.m +7 -0
+
+    /* Release all the keys to prevent a stuck key situation */
+    for(index = 0; index <= max_index; index++) {
+        keycode = keymap[index];
+        modifiers_state[keycode] = 0;
+        qemu_input_event_send_key_number(dcl->con, keycode, false);
+    }

This will send key-up events even for keys which are already up. Instead you can just send events for only the keys which are down (and avoid the lookup in keymap[] too):

Quoted 6 lines
   for (i = 0; i < ARRAY_SIZE(modifiers_state); i++) {
       if (modifiers_state[i])) {
           modifiers_state[i] = 0;
           qemu_input_event_send_key_number(dcl->con, i, false);
       }
   }

Sounds good. Will use it.

#5 lore raw
Quoted 39 lines · Programmingkid
Quoted 10 lines · Peter Maydell
Quoted 8 lines · Programmingkid

When the user puts QEMU in the background while holding down a key, QEMU will
not receive the keyup event when the user lets go of the key. When the user goes
back to QEMU, QEMU will think the key is still down causing stuck key symptoms.
This patch fixes this problem by releasing all keys when QEMU goes into the background.

Quoted 3 lines

Looks like maybe you're not wrapping lines early enough in your commit messages, resulting in this ugly effect when they're quoted. It's best to not have lines longer than 75 chars or so.

Sorry, will make the lines shorter in the future.

Quoted 7 lines
Quoted 7 lines

---
ui/cocoa.m | 17 ++++++++++++++++-

1 files changed, 16 insertions(+), 1 deletions(-)
Quoted 12 lines

ui/cocoa.m char **gArgv; · line 72

Quoted 9 lines
ui/cocoa.m +1 -0
index 334e6f6..d07b22d 100644
@@ -69,6 +69,7 @@ char **gArgv;
 bool stretch_video;
 NSTextField *pauseLabel;
 NSArray * supportedImageFileTypes;
+int modifiers_state[256];

Rather than making this global, could we have the applicationWillResignActive method call a new raiseAllKeys method on the NSView?

Quoted 1 line

We could do that, but isn't this more of an app controller function?

I don't feel strongly about where things should live, globals just make me think we could arrange things better somehow.

-- PMM