diff mbox

[v3,2/2] ui/cocoa.m: send ctrl-alt key combinations to guest if not used by QEMU

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

Commit Message

Programmingkid Nov. 2, 2017, 9:39 p.m. UTC
Send control-alt key combinations to the guest if not used by the user interface.

Signed-off-by: John Arbuckle <programmingkidx@gmail.com>
---
v3 changes:
- Code is now keyboard layout aware

v2 changes:
- changed logic to use existing if case

 ui/cocoa.m | 41 ++++++++++++++++++++---------------------
 1 file changed, 20 insertions(+), 21 deletions(-)

Comments

Peter Maydell Nov. 7, 2017, 10:13 a.m. UTC | #1
On 2 November 2017 at 21:39, John Arbuckle <programmingkidx@gmail.com> wrote:
> Send control-alt key combinations to the guest if not used by the user interface.
>
> Signed-off-by: John Arbuckle <programmingkidx@gmail.com>
> ---
> v3 changes:
> - Code is now keyboard layout aware
>
> v2 changes:
> - changed logic to use existing if case
>
>  ui/cocoa.m | 41 ++++++++++++++++++++---------------------
>  1 file changed, 20 insertions(+), 21 deletions(-)
>
> diff --git a/ui/cocoa.m b/ui/cocoa.m
> index e06aa9c65f..030479f4cd 100644
> --- a/ui/cocoa.m
> +++ b/ui/cocoa.m
> @@ -619,29 +619,28 @@ - (void) handleEvent:(NSEvent *)event
>                  return;
>              }
>
> -            // default
> -
> -            // handle control + alt Key Combos (ctrl+alt+[1..9,g] is reserved for QEMU)
> -            if (([event modifierFlags] & NSEventModifierFlagControl) && ([event modifierFlags] & NSEventModifierFlagOption)) {
> +            // console selection
> +            if (([event modifierFlags] & NSEventModifierFlagControl) &&
> +                ([event modifierFlags] & NSEventModifierFlagOption) &&
> +                ([[event charactersIgnoringModifiers] length] == 1) &&
> +                (isdigit([[event charactersIgnoringModifiers] characterAtIndex: 0]))) {
>                  NSString *keychar = [event charactersIgnoringModifiers];
> -                if ([keychar length] == 1) {
> -                    char key = [keychar characterAtIndex:0];
> -                    switch (key) {
> -
> -                        // enable graphic console
> -                        case '1' ... '9':
> -                            console_select(key - '0' - 1); /* ascii math */
> -                            return;
> -
> -                        // release the mouse grab
> -                        case 'g':
> -                            [self ungrabMouse];
> -                            return;
> -                    }
> -                }
> +                char key = [keychar characterAtIndex:0];
> +                console_select(key - '0' - 1); /* ascii math */
> +                return;
> +            }
> +
> +            // mouse ungrab
> +            else if (([event modifierFlags] & NSEventModifierFlagControl) &&
> +                     ([event modifierFlags] & NSEventModifierFlagOption) &&
> +                     ([[event charactersIgnoringModifiers] length] == 1) &&
> +                     ([[event charactersIgnoringModifiers] characterAtIndex: 0] == 'g')) {
> +                [self ungrabMouse];
> +                return;
> +            }
>
> -            // handle keys for graphic console
> -            } else if (qemu_console_is_graphic(NULL)) {
> +            // send to guest
> +            else if (qemu_console_is_graphic(NULL)) {
>                  qemu_input_event_send_key_qcode(dcl->con, keycode, true);
>
>              // handlekeys for Monitor
> --
> 2.13.5 (Apple Git-94)

This is an awful lot of entirely unnecessary code change.

Any time you find yourself repeating a big long
conditional like
> +            if (([event modifierFlags] & NSEventModifierFlagControl) &&
> +                ([event modifierFlags] & NSEventModifierFlagOption) &&
> +                ([[event charactersIgnoringModifiers] length] == 1) &&

you should take a step back and say "how can I
avoid repeating this?".

In this case, all you need to do is delete the "else"
from the start of
} else if (qemu_console_is_graphic(NULL)) {

All the specially-handled ctrl-alt combos finish with
"return", so all the rest will then just fall through
as required.

I've put a patch which does that into cocoa.next.

thanks
-- PMM
diff mbox

Patch

diff --git a/ui/cocoa.m b/ui/cocoa.m
index e06aa9c65f..030479f4cd 100644
--- a/ui/cocoa.m
+++ b/ui/cocoa.m
@@ -619,29 +619,28 @@  - (void) handleEvent:(NSEvent *)event
                 return;
             }
 
-            // default
-
-            // handle control + alt Key Combos (ctrl+alt+[1..9,g] is reserved for QEMU)
-            if (([event modifierFlags] & NSEventModifierFlagControl) && ([event modifierFlags] & NSEventModifierFlagOption)) {
+            // console selection
+            if (([event modifierFlags] & NSEventModifierFlagControl) &&
+                ([event modifierFlags] & NSEventModifierFlagOption) &&
+                ([[event charactersIgnoringModifiers] length] == 1) &&
+                (isdigit([[event charactersIgnoringModifiers] characterAtIndex: 0]))) {
                 NSString *keychar = [event charactersIgnoringModifiers];
-                if ([keychar length] == 1) {
-                    char key = [keychar characterAtIndex:0];
-                    switch (key) {
-
-                        // enable graphic console
-                        case '1' ... '9':
-                            console_select(key - '0' - 1); /* ascii math */
-                            return;
-
-                        // release the mouse grab
-                        case 'g':
-                            [self ungrabMouse];
-                            return;
-                    }
-                }
+                char key = [keychar characterAtIndex:0];
+                console_select(key - '0' - 1); /* ascii math */
+                return;
+            }
+
+            // mouse ungrab
+            else if (([event modifierFlags] & NSEventModifierFlagControl) &&
+                     ([event modifierFlags] & NSEventModifierFlagOption) &&
+                     ([[event charactersIgnoringModifiers] length] == 1) &&
+                     ([[event charactersIgnoringModifiers] characterAtIndex: 0] == 'g')) {
+                [self ungrabMouse];
+                return;
+            }
 
-            // handle keys for graphic console
-            } else if (qemu_console_is_graphic(NULL)) {
+            // send to guest
+            else if (qemu_console_is_graphic(NULL)) {
                 qemu_input_event_send_key_qcode(dcl->con, keycode, true);
 
             // handlekeys for Monitor