diff mbox

Make scrolling work again

Message ID 20171005184309.14521-1-programmingkidx@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Programmingkid Oct. 5, 2017, 6:43 p.m. UTC
Make scrolling in the monitor work.

Signed-off-by: John Arbuckle <programmingkidx@gmail.com>
---
 ui/cocoa.m | 88 +++++++++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 56 insertions(+), 32 deletions(-)

Comments

Peter Maydell Oct. 31, 2017, 4:23 p.m. UTC | #1
On 5 October 2017 at 19:43, John Arbuckle <programmingkidx@gmail.com> wrote:
> Make scrolling in the monitor work.
>
> Signed-off-by: John Arbuckle <programmingkidx@gmail.com>

Sorry this took me a while to get to reviewing.

> ---
>  ui/cocoa.m | 88 +++++++++++++++++++++++++++++++++++++++-----------------------
>  1 file changed, 56 insertions(+), 32 deletions(-)
>
> diff --git a/ui/cocoa.m b/ui/cocoa.m
> index 93e56d0518..5545c42b9c 100644
> --- a/ui/cocoa.m
> +++ b/ui/cocoa.m
> @@ -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)
> +    }

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

> +
> +    /* 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];

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

> +
> +    // 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
diff mbox

Patch

diff --git a/ui/cocoa.m b/ui/cocoa.m
index 93e56d0518..5545c42b9c 100644
--- a/ui/cocoa.m
+++ b/ui/cocoa.m
@@ -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: