Skip to contentSkip to conversation

ui/cocoa.m: prevent stuck key situation

PATCH Superseded Programmingkid 2 participants 4 messages Sep 24, 2015

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 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 down keys when QEMU goes into the background.

---
Removed the modifiers_state global variable. Added a raiseAllKeys method.

 ui/cocoa.m |   26 ++++++++++++++++++++++++++
 1 files changed, 26 insertions(+), 0 deletions(-)
ui/cocoa.m +26 -0
index 334e6f6..4d15553 100644
@@ -304,6 +304,7 @@ static void handleAnyDeviceErrors(Error * err)
 - (float) cdx;
 - (float) cdy;
 - (QEMUScreen) gscreen;
+- (void) raiseAllKeys;
 @end
 
 QemuCocoaView *cocoaView;
@@ -798,6 +799,23 @@ QemuCocoaView *cocoaView;
 - (float) cdx {return cdx;}
 - (float) cdy {return cdy;}
 - (QEMUScreen) gscreen {return screen;}
+
+/*
+ * Makes the target think all down keys are being released.
+ * This prevents a stuck key problem.
+ */
+- (void) raiseAllKeys
+{
+    int index;
+    const int max_index = 220; /* This is the highest value key */
+
+   for (index = 0; index <= max_index; index++) {
+       if (modifiers_state[index]) {
+           modifiers_state[index] = 0;
+           qemu_input_event_send_key_number(dcl->con, index, false);
+       }
+   }
+}
 @end
 
 
@@ -933,6 +951,14 @@ QemuCocoaView *cocoaView;
     return YES;
 }
 
+/* Called when QEMU goes into the background */
+- (void) applicationWillResignActive: (NSNotification *)aNotification
+{
+    COCOA_DEBUG("QemuCocoaAppController: applicationWillResignActive\n");
+
+    [cocoaView raiseAllKeys];
+}
+
 - (void)startEmulationWithArgc:(int)argc argv:(char**)argv
 {
     COCOA_DEBUG("QemuCocoaAppController: startEmulationWithArgc\n");
Signature · 1 line
1.7.5.4
#1 lore raw
Quoted 30 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 down keys when QEMU goes into the background.

---
Removed the modifiers_state global variable. Added a raiseAllKeys method.

 ui/cocoa.m |   26 ++++++++++++++++++++++++++
 1 files changed, 26 insertions(+), 0 deletions(-)
ui/cocoa.m +1 -0
index 334e6f6..4d15553 100644
@@ -304,6 +304,7 @@ static void handleAnyDeviceErrors(Error * err)
 - (float) cdx;
 - (float) cdy;
 - (QEMUScreen) gscreen;
+- (void) raiseAllKeys;
 @end
 
 QemuCocoaView *cocoaView;

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

Quoted 7 lines
ui/cocoa.m +4 -0
@@ -798,6 +799,23 @@ QemuCocoaView *cocoaView;
 - (float) cdx {return cdx;}
 - (float) cdy {return cdy;}
 - (QEMUScreen) gscreen {return screen;}
+
+/*
+ * Makes the target think all down keys are being released.
+ * This prevents a stuck key problem.

", since we will not see key up events for those keys after we have lost focus."

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

Quoted 5 lines
ui/cocoa.m +5 -0
+ */
+- (void) raiseAllKeys
+{
+    int index;
+    const int max_index = 220; /* This is the highest value key */

No, you need to use ARRAY_SIZE.

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

Quoted 7 lines
ui/cocoa.m +7 -0
+
+   for (index = 0; index <= max_index; index++) {
+       if (modifiers_state[index]) {
+           modifiers_state[index] = 0;
+           qemu_input_event_send_key_number(dcl->con, index, false);
+       }
+   }

-- PMM

#2 lore raw
Quoted 29 lines · Peter Maydell
Quoted 28 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 down keys when QEMU goes into the background.

---
Removed the modifiers_state global variable. Added a raiseAllKeys method.

ui/cocoa.m | 26 ++++++++++++++++++++++++++

1 files changed, 26 insertions(+), 0 deletions(-)
ui/cocoa.m +1 -3
index 334e6f6..4d15553 100644
@@ -304,6 +304,7 @@ static void handleAnyDeviceErrors(Error * err)
- (float) cdx;
- (float) cdy;
- (QEMUScreen) gscreen;
+- (void) raiseAllKeys;

@end

Quoted 12 lines

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

Quoted 9 lines

QemuCocoaView *cocoaView;

ui/cocoa.m +4 -3
@@ -798,6 +799,23 @@ QemuCocoaView *cocoaView;
- (float) cdx {return cdx;}
- (float) cdy {return cdy;}
- (QEMUScreen) gscreen {return screen;}
+
+/*
+ * Makes the target think all down keys are being released.
+ * This prevents a stuck key problem.

", since we will not see key up events for those keys after we have lost focus."

If that is all the changes that are required, I wouldn't mind if you just added it to the patch.

Quoted 8 lines

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

Quoted 6 lines
ui/cocoa.m +5 -0
+ */
+- (void) raiseAllKeys
+{
+    int index;
+    const int max_index = 220; /* This is the highest value key */

No, you need to use ARRAY_SIZE.

I did use it, it didn't work. The command key would still stay down. This is because the command key has a value of 220. The keymap array size is only 126. I know it is confusing. I was thinking of using cocoa_keycode_to_qemu() to translate the index to the qemu (pc xt) values, but that would be expensive in terms of cpu usage.

Quoted 10 lines

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

Quoted 8 lines
ui/cocoa.m +7 -0
+
+   for (index = 0; index <= max_index; index++) {
+       if (modifiers_state[index]) {
+           modifiers_state[index] = 0;
+           qemu_input_event_send_key_number(dcl->con, index, false);
+       }
+   }

-- PMM

#3 lore raw
Quoted 9 lines · Programmingkid
Quoted 6 lines · Peter Maydell
Quoted 5 lines

+ */
+- (void) raiseAllKeys

ui/cocoa.m +3 -0
+{
+    int index;
+    const int max_index = 220; /* This is the highest value key */
Quoted 7 lines
Quoted 1 line

No, you need to use ARRAY_SIZE.

I did use it, it didn't work. The command key would still stay down. This is because the command key has a value of 220. The keymap array size is only 126. I know it is confusing. I was thinking of using cocoa_keycode_to_qemu() to translate the index to the qemu (pc xt) values, but that would be expensive in terms of cpu usage.

We don't care about the keymap array size. ARRAY_SIZE(modifiers_state) is 256.

If you're iterating through an array, as we are here, then the correct upper bound is always ARRAY_SIZE(array).

Quoted 8 lines
Quoted 8 lines

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

Quoted 8 lines
ui/cocoa.m +7 -0
+
+   for (index = 0; index <= max_index; index++) {
+       if (modifiers_state[index]) {
+           modifiers_state[index] = 0;
+           qemu_input_event_send_key_number(dcl->con, index, false);
+       }
+   }

-- PMM