Skip to contentSkip to conversation

Add ability for user to specify mouse ungrab key

PATCH Superseded John Arbuckle 3 participants 3 messages Dec 14, 2017

v2 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Add ability for user to specify mouse ungrab key

Currently the ungrab keys for the Cocoa and GTK interface are Control-Alt-g. This combination may not be very fun for the user to have to enter, so we now enable the user to specify their own key as the ungrab key. Since the function keys are the keys that don't tend to be used that often and are usually available, they will be the ones the user can pick to be the ungrab key.

---
v2 changes:
- Removed the "int i" code from the for loops.

 qemu-options.hx |  2 ++
 ui/cocoa.m      | 20 ++++++++++++++--
 vl.c            | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 92 insertions(+), 3 deletions(-)
qemu-options.hx +2 -0
index f11c4ac960..0a727ea50a 100644
@@ -4472,6 +4472,8 @@ contents of @code{iv.b64} to the second secret
 
 ETEXI
 
+DEF("ungrab", HAS_ARG, QEMU_OPTION_ungrab, \
+    "-ungrab <key>", QEMU_ARCH_ALL)
 
 HXCOMM This is the last statement. Insert new options before this line!
 STEXI
ui/cocoa.m +18 -2
index 330ccebf90..8dc603adf0 100644
@@ -106,6 +106,9 @@
 NSTextField *pauseLabel;
 NSArray * supportedImageFileTypes;
 
+int get_ungrab_key_value(void);
+const char * get_ungrab_key_name(void);
+
 // Mac to QKeyCode conversion
 const int mac_to_qkeycode_map[] = {
     [kVK_ANSI_A] = Q_KEY_CODE_A,
@@ -678,6 +681,12 @@ - (void) handleEvent:(NSEvent *)event
         case NSEventTypeKeyDown:
             keycode = cocoa_keycode_to_qemu([event keyCode]);
 
+            // if the user wants to release the mouse grab
+            if (keycode == get_ungrab_key_value()) {
+                [self ungrabMouse];
+                return;
+            }
+
             // forward command key combos to the host UI unless the mouse is grabbed
             if (!isMouseGrabbed && ([event modifierFlags] & NSEventModifierFlagCommand)) {
                 [NSApp sendEvent:event];
@@ -842,10 +851,17 @@ - (void) grabMouse
     COCOA_DEBUG("QemuCocoaView: grabMouse\n");
 
     if (!isFullscreen) {
+        NSString * message_string;
+        if (get_ungrab_key_value() < 0) {
+            message_string = [NSString stringWithFormat: @"- (Press ctrl + alt + g to release Mouse"];
+        } else {
+            message_string = [NSString stringWithFormat: @"- (Press ctrl + alt + g or %s to release Mouse", get_ungrab_key_name()];
+        }
+
         if (qemu_name)
-            [normalWindow setTitle:[NSString stringWithFormat:@"QEMU %s - (Press ctrl + alt + g to release Mouse)", qemu_name]];
+            [normalWindow setTitle:[NSString stringWithFormat: @"QEMU %s %@", qemu_name, message_string]];
         else
-            [normalWindow setTitle:@"QEMU - (Press ctrl + alt + g to release Mouse)"];
+            [normalWindow setTitle:[NSString stringWithFormat: @"QEMU %@", message_string]];
     }
     [self hideCursor];
     if (!isAbsoluteEnabled) {
vl.c +72 -1

91 lines not on this page · raw message

Signature · 1 line
2.14.3 (Apple Git-98)
#1 lore raw
This series seems to have some coding style problems. See output below for more information:

Hi,

This series seems to have some coding style problems. See output below for more information:

Message-id: [email protected]
Subject: [Qemu-devel] [PATCH v2] Add ability for user to specify mouse ungrab key
Type: series

=== TEST SCRIPT BEGIN ===
#!/bin/bash

BASE=base
n=1
total=$(git log --oneline $BASE.. | wc -l)
failed=0

git config --local diff.renamelimit 0
git config --local diff.renames True

commits="$(git log --format=%H --reverse $BASE..)" for c in $commits; do

    echo "Checking PATCH $n/$total: $(git log -n 1 --format=%s $c)..."
    if ! git show $c --format=email | ./scripts/checkpatch.pl --mailback -; then
        failed=1
        echo
    fi
    n=$((n+1))

done

exit $failed
=== TEST SCRIPT END ===

Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384
From https://github.com/patchew-project/qemu

 t [tag update]            patchew/[email protected] -> patchew/[email protected]
 t [tag update]            patchew/[email protected] -> patchew/[email protected]
 t [tag update]            patchew/[email protected] -> patchew/[email protected]
 * [new tag]               patchew/[email protected] -> patchew/[email protected]

Switched to a new branch 'test'
829356df0b Add ability for user to specify mouse ungrab key

=== OUTPUT BEGIN ===
Checking PATCH 1/1: Add ability for user to specify mouse ungrab key...
ERROR: externs should be avoided in .c files
#145: FILE: vl.c:3143:
+int get_ungrab_key_value(void);

total: 1 errors, 0 warnings, 139 lines checked

Your patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.

=== OUTPUT END ===

Test command exited with code: 1

---
Email generated automatically by Patchew [http://patchew.org/].
Please send your feedback to [email protected]

#2 lore raw
In reply to John Arbuckle
Quoted 6 lines · John Arbuckle

Currently the ungrab keys for the Cocoa and GTK interface are Control-Alt-g. This combination may not be very fun for the user to have to enter, so we now enable the user to specify their own key as the ungrab key. Since the function keys are the keys that don't tend to be used that often and are usually available, they will be the ones the user can pick to be the ungrab key.

We shouldn't make such assumptions about the function keys being what the users wants to use. We should allow any arbitrary combination of keys to be used. Just generalize the way Control+Alt+g is handled such that we match an arbitrary sequence instead of the hardcoded sequence.

Quoted 16 lines

---
v2 changes:
- Removed the "int i" code from the for loops.

 qemu-options.hx |  2 ++
 ui/cocoa.m      | 20 ++++++++++++++--
 vl.c            | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 92 insertions(+), 3 deletions(-)
qemu-options.hx +0 -0
index f11c4ac960..0a727ea50a 100644

qemu-options.hx contents of @code{iv.b64} to the second secret · line 4476

Quoted 5 lines
qemu-options.hx +2 -0
@@ -4472,6 +4472,8 @@ contents of @code{iv.b64} to the second secret
 
 ETEXI
 
+DEF("ungrab", HAS_ARG, QEMU_OPTION_ungrab, \
+    "-ungrab <key>", QEMU_ARCH_ALL)

Should be '<key sequence>', as we must allow more than one key to be listed to get a full sequence. eg you should be able to set

-uncgrab ctrl+alt+g

to replicate the default behaviour via CLI. For sanity, it is reasonable to limit the number of keys to max of 3.

ui/cocoa.m line 110

Quoted 10 lines
ui/cocoa.m +2 -0
index 330ccebf90..8dc603adf0 100644
@@ -106,6 +106,9 @@
 NSTextField *pauseLabel;
 NSArray * supportedImageFileTypes;
 
+int get_ungrab_key_value(void);
+const char * get_ungrab_key_name(void);

This shouldn't be in cocoa.m - we should have something in common code. Perhaps declare them in include/ui/console.h. Also give them a 'console_' prefix in fnuction name.

Since we need a full sequence of keys we would need something more like

int *console_ungrab_key_sequence()
const char *console_ungrab_key_string()

The first returns a 0 terminated array of ints, eg ctrl+alt+g would be represented as a 4 element array

{ Q_KEY_CODE_CTRL, Q_KEY_CODE_ALT, Q_KEY_CODE_G, 0 }

ui/cocoa.m - (void) handleEvent:(NSEvent *)event · line 688

Quoted 11 lines
ui/cocoa.m +5 -0
 const int mac_to_qkeycode_map[] = {
     [kVK_ANSI_A] = Q_KEY_CODE_A,
@@ -678,6 +681,12 @@ - (void) handleEvent:(NSEvent *)event
         case NSEventTypeKeyDown:
             keycode = cocoa_keycode_to_qemu([event keyCode]);
 
+            // if the user wants to release the mouse grab
+            if (keycode == get_ungrab_key_value()) {
+                [self ungrabMouse];
+                return;
+            }

This will have to track the sequence of pressed keys across invokations of handleEvent.

This could be done by simple static variable that tracks the array index we've matched in the ungrab sequence. eg perhaps

    static size_t ungrab_index;

    int *ungrab_seq = get_ungrab_key_seq()
    if (keycode == ungrab_seq[ungrab_index]) {
        ungrab_index++;
	if (ungrab_seq[ungrab_seq] == 0) {
	     [self ungrabMouse];
	     return;
	}
    } else {
        ungrab_index = 0;
    }

this lets you remove the current code that deals with ctrl+alt+g ungrab.

Quoted 29 lines
vl.c +13 -1
index 1ad1c04637..c2d848fabc 100644
@@ -185,7 +185,8 @@ bool boot_strict;
 uint8_t *boot_splash_filedata;
 size_t boot_splash_filedata_size;
 uint8_t qemu_extra_params_fw[2];
-
+int ungrab_key_value = -1;
+char *ungrab_key_name;
 int icount_align_option;
 
 /* The bytes in qemu_uuid are in the order specified by RFC4122, _not_ in the
@@ -3088,6 +3089,73 @@ static void register_global_properties(MachineState *ms)
     user_register_global_props();
 }
 
+/* Sets the mouse ungrab key to what the user wants */
+static void set_ungrab_key(const char *new_key)
+{
+    const char *keys[] = {
+        [0 ... 0xff]    = "",
+        [Q_KEY_CODE_F1] = "f1",
+        [Q_KEY_CODE_F2] = "f2",
+        [Q_KEY_CODE_F3] = "f3",
+        [Q_KEY_CODE_F4] = "f4",
+        [Q_KEY_CODE_F5] = "f5",
+        [Q_KEY_CODE_F6] = "f6",

vl.c static void register_global_properties(MachineState *ms) · line 3112

Quoted 10 lines
vl.c +10 -0
+        [Q_KEY_CODE_F7] = "f7",
+        [Q_KEY_CODE_F8] = "f8",
+        [Q_KEY_CODE_F9] = "f9",
+        [Q_KEY_CODE_F10] = "f10",
+        [Q_KEY_CODE_F11] = "f11",
+        [Q_KEY_CODE_F12] = "f12",
+        [Q_KEY_CODE_PRINT] = "f13",
+        [Q_KEY_CODE_SCROLL_LOCK] = "f14",
+        [Q_KEY_CODE_PAUSE] = "f15",
+    };

We should just use the existing table generated by qapi.

vl.c static void register_global_properties(MachineState *ms) · line 3123

Quoted 11 lines
vl.c +11 -0
+
+    int key_value = -1;
+    int i;
+
+    /* see if the user's key is recognized */
+    for (i = 0; i < ARRAY_SIZE(keys); i++) {
+        if (strcmp(keys[i], new_key) == 0) {
+            key_value = i;
+            break;
+        }
+    }

And of course we need to cope with a full sequence

vl.c static void register_global_properties(MachineState *ms) · line 3136

Quoted 13 lines
vl.c +13 -0
+
+    /* if the user's key isn't recognized print the usable keys */
+    if (key_value == -1) {
+        printf("Unrecognized key: %s\n", new_key);
+        printf("Usable ungrab keys: ");
+        for (i = 0; i < ARRAY_SIZE(keys); i++) {
+            if (strlen(keys[i]) > 0) { /* filters out undefined values */
+                printf("%s ", keys[i]);
+            }
+        }
+        printf("\n\n");
+        exit(1);
+    }

Not sure this is really needed - any valid qemu keycode should be allowed.

Quoted 11 lines
vl.c +11 -0
+
+    ungrab_key_name = (char *) malloc(4 * sizeof(char));
+    strncpy(ungrab_key_name, new_key, 3);
+    ungrab_key_value = key_value;
+}
+
+int get_ungrab_key_value(void);
+
+/* Returns the user specified ungrab key's value or -1 if not specified */
+int get_ungrab_key_value(void)
+{

vl.c static void register_global_properties(MachineState *ms) · line 3157

Quoted 10 lines
vl.c +10 -0
+    return ungrab_key_value;
+}
+
+const char *get_ungrab_key_name(void);
+
+/* Returns the name of the user specified ungrab key */
+const char *get_ungrab_key_name(void)
+{
+    return ungrab_key_name;
+}

Should all be in ui/console.c really, not vl.c

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|