Skip to contentSkip to conversation

Make scrolling work again

PATCH Superseded John Arbuckle 2 participants 2 messages Oct 5, 2017 – Oct 31, 2017

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Make scrolling work again

Make scrolling in the monitor work.

---
 ui/cocoa.m | 88 +++++++++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 56 insertions(+), 32 deletions(-)
ui/cocoa.m +56 -32
index 93e56d0518..5545c42b9c 100644
@@ -281,6 +281,7 @@ - (void) switchSurface:(DisplaySurface *)surface;
 - (void) grabMouse;
 - (void) ungrabMouse;
 - (void) toggleFullScreen:(id)sender;
+- (void) handleMonitorInput:(NSEvent *)event;
 - (void) handleEvent:(NSEvent *)event;
 - (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled;
 /* The state surrounding mouse grabbing is potentially confusing.
@@ -554,6 +555,60 @@ - (void) toggleStatefulModifier: (int)keycode {
     qemu_input_event_send_key_qcode(dcl->con, keycode, false);
 }
 
+// Does the work of sending input to the monitor
+- (void) handleMonitorInput:(NSEvent *)event
+{
+    int keysym = 0;
+    int control_key = 0;
+
+    // if the control key is down
+    if ([event modifierFlags] & NSEventModifierFlagControl) {
+        control_key = 4; // shifts by one nibble (hex digit)
+    }
+
+    /* translates Macintosh keycodes to QEMU's keysym */
+    int translation_matrix[] = {
+        [0 ... 0xff] = 0,   // invalid key
+
+        [kVK_UpArrow]       = QEMU_KEY_UP,
+        [kVK_DownArrow]     = QEMU_KEY_DOWN,
+        [kVK_RightArrow]    = QEMU_KEY_RIGHT,
+        [kVK_LeftArrow]     = QEMU_KEY_LEFT,
+        [kVK_Home]          = QEMU_KEY_HOME,
+        [kVK_End]           = QEMU_KEY_END,
+        [kVK_PageUp]        = QEMU_KEY_PAGEUP,
+        [kVK_PageDown]      = QEMU_KEY_PAGEDOWN,
+        [kVK_ForwardDelete] = QEMU_KEY_DELETE,
+        [kVK_Delete]        = QEMU_KEY_BACKSPACE,
+
+        /*
+         * Shift value by one hex digit.
+         * Since no key has a 3 digit hex value there is no chance
+         * for overlap.
+         */
+        [kVK_UpArrow << 4]       = QEMU_KEY_CTRL_UP,
+        [kVK_DownArrow << 4]     = QEMU_KEY_CTRL_DOWN,
+        [kVK_RightArrow << 4]    = QEMU_KEY_CTRL_RIGHT,
+        [kVK_LeftArrow << 4]     = QEMU_KEY_CTRL_LEFT,
+        [kVK_Home << 4]          = QEMU_KEY_CTRL_HOME,
+        [kVK_End << 4]           = QEMU_KEY_CTRL_END,
+        [kVK_PageUp << 4]        = QEMU_KEY_CTRL_PAGEUP,
+        [kVK_PageDown << 4]      = QEMU_KEY_CTRL_PAGEDOWN,
+    };
+
+    keysym = translation_matrix[[event keyCode] << control_key];
+
+    // if not a key that needs translating
+    if (keysym == 0) {
+        NSString *ks = [event characters];
+        if ([ks length] > 0)
+            keysym = [ks characterAtIndex:0];
+    }
+
+    if(keysym)
+        kbd_put_keysym(keysym);
+}
+
 - (void) handleEvent:(NSEvent *)event
 {
     COCOA_DEBUG("QemuCocoaView: handleEvent\n");
@@ -641,38 +696,7 @@ - (void) handleEvent:(NSEvent *)event
 
             // handlekeys for Monitor
             } else {
-                int keysym = 0;
-                switch([event keyCode]) {
-                case 115:
-                    keysym = QEMU_KEY_HOME;
-                    break;
-                case 117:
-                    keysym = QEMU_KEY_DELETE;
-                    break;
-                case 119:
-                    keysym = QEMU_KEY_END;
-                    break;
-                case 123:
-                    keysym = QEMU_KEY_LEFT;
-                    break;
-                case 124:
-                    keysym = QEMU_KEY_RIGHT;
-                    break;
-                case 125:
-                    keysym = QEMU_KEY_DOWN;
-                    break;
-                case 126:
-                    keysym = QEMU_KEY_UP;
-                    break;
-                default:
-                    {
-                        NSString *ks = [event characters];
-                        if ([ks length] > 0)
-                            keysym = [ks characterAtIndex:0];
-                    }
-                }
-                if (keysym)
-                    kbd_put_keysym(keysym);
+                [self handleMonitorInput: event];
             }
             break;
         case NSEventTypeKeyUp:
Signature · 1 line
2.13.5 (Apple Git-94)
#1 lore raw
Quoted 3 lines · John Arbuckle

Make scrolling in the monitor work.

Sorry this took me a while to get to reviewing.

Quoted 20 lines
---
 ui/cocoa.m | 88 +++++++++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 56 insertions(+), 32 deletions(-)
ui/cocoa.m +1 -0
index 93e56d0518..5545c42b9c 100644
@@ -281,6 +281,7 @@ - (void) switchSurface:(DisplaySurface *)surface;
 - (void) grabMouse;
 - (void) ungrabMouse;
 - (void) toggleFullScreen:(id)sender;
+- (void) handleMonitorInput:(NSEvent *)event;
 - (void) handleEvent:(NSEvent *)event;
 - (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled;
 /* The state surrounding mouse grabbing is potentially confusing.
@@ -554,6 +555,60 @@ - (void) toggleStatefulModifier: (int)keycode {
     qemu_input_event_send_key_qcode(dcl->con, keycode, false);
 }
 

ui/cocoa.m - (void) toggleStatefulModifier: (int)keycode { · line 567

Quoted 10 lines
ui/cocoa.m +10 -0
+// Does the work of sending input to the monitor
+- (void) handleMonitorInput:(NSEvent *)event
+{
+    int keysym = 0;
+    int control_key = 0;
+
+    // if the control key is down
+    if ([event modifierFlags] & NSEventModifierFlagControl) {
+        control_key = 4; // shifts by one nibble (hex digit)
+    }

Rather than doing this, just have an array for "with control" and one for "without control".

Quoted 22 lines
ui/cocoa.m +22 -0
+
+    /* translates Macintosh keycodes to QEMU's keysym */
+    int translation_matrix[] = {
+        [0 ... 0xff] = 0,   // invalid key
+
+        [kVK_UpArrow]       = QEMU_KEY_UP,
+        [kVK_DownArrow]     = QEMU_KEY_DOWN,
+        [kVK_RightArrow]    = QEMU_KEY_RIGHT,
+        [kVK_LeftArrow]     = QEMU_KEY_LEFT,
+        [kVK_Home]          = QEMU_KEY_HOME,
+        [kVK_End]           = QEMU_KEY_END,
+        [kVK_PageUp]        = QEMU_KEY_PAGEUP,
+        [kVK_PageDown]      = QEMU_KEY_PAGEDOWN,
+        [kVK_ForwardDelete] = QEMU_KEY_DELETE,
+        [kVK_Delete]        = QEMU_KEY_BACKSPACE,
+
+        /*
+         * Shift value by one hex digit.
+         * Since no key has a 3 digit hex value there is no chance
+         * for overlap.
+         */
+        [kVK_UpArrow << 4]       = QEMU_KEY_CTRL_UP,

ui/cocoa.m - (void) toggleStatefulModifier: (int)keycode { · line 599

Quoted 10 lines
ui/cocoa.m +10 -0
+        [kVK_DownArrow << 4]     = QEMU_KEY_CTRL_DOWN,
+        [kVK_RightArrow << 4]    = QEMU_KEY_CTRL_RIGHT,
+        [kVK_LeftArrow << 4]     = QEMU_KEY_CTRL_LEFT,
+        [kVK_Home << 4]          = QEMU_KEY_CTRL_HOME,
+        [kVK_End << 4]           = QEMU_KEY_CTRL_END,
+        [kVK_PageUp << 4]        = QEMU_KEY_CTRL_PAGEUP,
+        [kVK_PageDown << 4]      = QEMU_KEY_CTRL_PAGEDOWN,
+    };
+
+    keysym = translation_matrix[[event keyCode] << control_key];

You need to check whether your array index value is within bounds for the array before you use it as an array index.

ui/cocoa.m - (void) toggleStatefulModifier: (int)keycode { · line 609

Quoted 10 lines
ui/cocoa.m +10 -0
+
+    // if not a key that needs translating
+    if (keysym == 0) {
+        NSString *ks = [event characters];
+        if ([ks length] > 0)
+            keysym = [ks characterAtIndex:0];
+    }
+
+    if(keysym)
+        kbd_put_keysym(keysym);

Coding style requires braces for all if() statements. (I know you're just moving these lines; that's a good point to bring them into line with the style.)

thanks
-- PMM