qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] input-linux: Allow to toggle grab from QMP
@ 2021-05-01 19:06 Rainer Müller
  2021-05-01 19:06 ` [PATCH 1/2] input-linux: Delay grab toggle if keys are pressed Rainer Müller
  2021-05-01 19:06 ` [PATCH 2/2] input-linux: Allow to toggle grab from QMP Rainer Müller
  0 siblings, 2 replies; 7+ messages in thread
From: Rainer Müller @ 2021-05-01 19:06 UTC (permalink / raw)
  To: qemu-devel; +Cc: Gerd Hoffmann, Rainer Müller

This adds an grab-active bool option to input-linux objects to control
the grab state of evdev devices from QMP. The first patch fixes
a problem with multiple keyboards that was previously unlikely, as the
user will only use one device at a time. It could be merged
independently, but I am submitting them together as this becomes more
relevant when grab state can be controlled from QMP.

Rainer Müller (2):
  input-linux: Delay grab toggle if keys are pressed
  input-linux: Allow to toggle grab from QMP

 qapi/qom.json    |  3 +++
 ui/input-linux.c | 46 +++++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 44 insertions(+), 5 deletions(-)

-- 
2.25.1



^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 1/2] input-linux: Delay grab toggle if keys are pressed
  2021-05-01 19:06 [PATCH 0/2] input-linux: Allow to toggle grab from QMP Rainer Müller
@ 2021-05-01 19:06 ` Rainer Müller
  2021-05-04  9:14   ` Gerd Hoffmann
  2021-05-01 19:06 ` [PATCH 2/2] input-linux: Allow to toggle grab from QMP Rainer Müller
  1 sibling, 1 reply; 7+ messages in thread
From: Rainer Müller @ 2021-05-01 19:06 UTC (permalink / raw)
  To: qemu-devel; +Cc: Gerd Hoffmann, Rainer Müller

When multiple keyboards are passed to the guest with input-linux, there
could still be keys pressed on the other keyboard when toggling grab.
Delay toggling grab on the other keyboard until all keys are released,
otherwise keys could be stuck on host without a key up event.

Signed-off-by: Rainer Müller <raimue@codingfarm.de>
---
 ui/input-linux.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/ui/input-linux.c b/ui/input-linux.c
index 05c0c98819..47d489d738 100644
--- a/ui/input-linux.c
+++ b/ui/input-linux.c
@@ -89,7 +89,12 @@ static void input_linux_toggle_grab(InputLinux *il)
             continue;
         }
         if (item->grab_active != il->grab_active) {
-            input_linux_toggle_grab(item);
+            if (item->keycount) {
+                /* delay grab until all keys are released */
+                item->grab_request = true;
+            } else {
+                input_linux_toggle_grab(item);
+            }
         }
     }
 }
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 2/2] input-linux: Allow to toggle grab from QMP
  2021-05-01 19:06 [PATCH 0/2] input-linux: Allow to toggle grab from QMP Rainer Müller
  2021-05-01 19:06 ` [PATCH 1/2] input-linux: Delay grab toggle if keys are pressed Rainer Müller
@ 2021-05-01 19:06 ` Rainer Müller
  2021-05-04  9:17   ` Gerd Hoffmann
  1 sibling, 1 reply; 7+ messages in thread
From: Rainer Müller @ 2021-05-01 19:06 UTC (permalink / raw)
  To: qemu-devel
  Cc: Daniel P. Berrangé,
	Eduardo Habkost, Markus Armbruster, Rainer Müller,
	Gerd Hoffmann, Paolo Bonzini

This patch allows to boot a guest without the input-linux device being
grabbed immediately from the host. This is useful when the guest is
automatically started, but is supposed to stay in the background until
the user actively switches to it with a key combination.

In this usage example the host continues to own the keyboard until the
user explicitly toggles the grab state with both control keys:
  -object input-linux,id=kbd1,evdev=/dev/input/eventX,grab-active=off

When grab-active is not given, input-linux will behave as before and
devices are being grabbed immediately on initialization.

Note that even if grab_all=on is set, other devices will initially be
grabbed according to their own grab-active option. The first toggle
operation on a grab_all=on device will sync state to the other devices.

Furthermore, this new option allows to toggle the grab state from QMP
with the qom-set command. By setting grab-active at runtime, the device
will be grabbed or released as indicated by the passed value.

  $ ./scripts/qmp-shell /tmp/qmp.sock
  (QEMU) qom-set path=/objects/kbd1 property=grab-active value=true
  {"return": {}}
  (QEMU) qom-get path=/objects/kbd1 property=grab-active
  {"return": true}

For devices with grab_all=on, the action will propagate to other devices
as if the grab toggle hotkey was used.

Signed-off-by: Rainer Müller <raimue@codingfarm.de>
---
 qapi/qom.json    |  3 +++
 ui/input-linux.c | 39 +++++++++++++++++++++++++++++++++++----
 2 files changed, 38 insertions(+), 4 deletions(-)

diff --git a/qapi/qom.json b/qapi/qom.json
index cd0e76d564..51704465ec 100644
--- a/qapi/qom.json
+++ b/qapi/qom.json
@@ -488,6 +488,8 @@
 #
 # @repeat: enables auto-repeat events (default: false)
 #
+# @grab-active: if true, device is grabbed (default: true)
+#
 # @grab-toggle: the key or key combination that toggles device grab
 #               (default: ctrl-ctrl)
 #
@@ -497,6 +499,7 @@
   'data': { 'evdev': 'str',
             '*grab_all': 'bool',
             '*repeat': 'bool',
+            '*grab-active': 'bool',
             '*grab-toggle': 'GrabToggleKeys' } }
 
 ##
diff --git a/ui/input-linux.c b/ui/input-linux.c
index 47d489d738..64efb83e21 100644
--- a/ui/input-linux.c
+++ b/ui/input-linux.c
@@ -399,10 +399,9 @@ static void input_linux_complete(UserCreatable *uc, Error **errp)
     }
 
     qemu_set_fd_handler(il->fd, input_linux_event, NULL, il);
-    if (il->keycount) {
-        /* delay grab until all keys are released */
-        il->grab_request = true;
-    } else {
+    /* delay grab until all keys are released */
+    if (il->grab_request && !il->keycount) {
+        il->grab_request = false;
         input_linux_toggle_grab(il);
     }
     QTAILQ_INSERT_TAIL(&inputs, il, next);
@@ -493,8 +492,37 @@ static void input_linux_set_grab_toggle(Object *obj, int value,
     il->grab_toggle = value;
 }
 
+static bool input_linux_get_grab_active(Object *obj, Error **errp)
+{
+    InputLinux *il = INPUT_LINUX(obj);
+
+    return il->grab_active;
+}
+
+static void input_linux_set_grab_active(Object *obj, bool value,
+                                        Error **errp)
+{
+    InputLinux *il = INPUT_LINUX(obj);
+
+    if (!il->initialized) {
+        il->grab_request = value;
+        return;
+    }
+
+    if (il->grab_active != value) {
+        if (il->keycount) {
+            il->grab_request = true;
+        } else {
+            input_linux_toggle_grab(il);
+        }
+    }
+}
+
 static void input_linux_instance_init(Object *obj)
 {
+    InputLinux *il = INPUT_LINUX(obj);
+
+    il->grab_request = true;
 }
 
 static void input_linux_class_init(ObjectClass *oc, void *data)
@@ -512,6 +540,9 @@ static void input_linux_class_init(ObjectClass *oc, void *data)
     object_class_property_add_bool(oc, "repeat",
                                    input_linux_get_repeat,
                                    input_linux_set_repeat);
+    object_class_property_add_bool(oc, "grab-active",
+                                   input_linux_get_grab_active,
+                                   input_linux_set_grab_active);
     object_class_property_add_enum(oc, "grab-toggle", "GrabToggleKeys",
                                    &GrabToggleKeys_lookup,
                                    input_linux_get_grab_toggle,
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/2] input-linux: Delay grab toggle if keys are pressed
  2021-05-01 19:06 ` [PATCH 1/2] input-linux: Delay grab toggle if keys are pressed Rainer Müller
@ 2021-05-04  9:14   ` Gerd Hoffmann
  2021-05-09 11:43     ` Rainer Müller
  0 siblings, 1 reply; 7+ messages in thread
From: Gerd Hoffmann @ 2021-05-04  9:14 UTC (permalink / raw)
  To: Rainer Müller; +Cc: qemu-devel

On Sat, May 01, 2021 at 09:06:21PM +0200, Rainer Müller wrote:
> When multiple keyboards are passed to the guest with input-linux, there
> could still be keys pressed on the other keyboard when toggling grab.
> Delay toggling grab on the other keyboard until all keys are released,
> otherwise keys could be stuck on host without a key up event.

Hmm, if you have two keyboards plugged into your machine, why would you
assign both to a virtual machine?  Instead of simply using one for the
host and one for the guest?

take care,
  Gerd



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/2] input-linux: Allow to toggle grab from QMP
  2021-05-01 19:06 ` [PATCH 2/2] input-linux: Allow to toggle grab from QMP Rainer Müller
@ 2021-05-04  9:17   ` Gerd Hoffmann
  0 siblings, 0 replies; 7+ messages in thread
From: Gerd Hoffmann @ 2021-05-04  9:17 UTC (permalink / raw)
  To: Rainer Müller
  Cc: Daniel P. Berrangé,
	Eduardo Habkost, qemu-devel, Markus Armbruster, Paolo Bonzini

> @@ -488,6 +488,8 @@
>  #
>  # @repeat: enables auto-repeat events (default: false)
>  #
> +# @grab-active: if true, device is grabbed (default: true)
> +#
>  # @grab-toggle: the key or key combination that toggles device grab
>  #               (default: ctrl-ctrl)
>  #

Can grab-active be added at the end of the list please?

Also add a "(Since: 6.1)" note as this will most likely be available in
version 6.1 & newer.

Otherwise the patch looks sane.

thanks,
  Gerd



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/2] input-linux: Delay grab toggle if keys are pressed
  2021-05-04  9:14   ` Gerd Hoffmann
@ 2021-05-09 11:43     ` Rainer Müller
  2021-05-10  7:30       ` Gerd Hoffmann
  0 siblings, 1 reply; 7+ messages in thread
From: Rainer Müller @ 2021-05-09 11:43 UTC (permalink / raw)
  To: Gerd Hoffmann; +Cc: qemu-devel

On 04/05/2021 11.14, Gerd Hoffmann wrote:
> On Sat, May 01, 2021 at 09:06:21PM +0200, Rainer Müller wrote:
>> When multiple keyboards are passed to the guest with input-linux, there
>> could still be keys pressed on the other keyboard when toggling grab.
>> Delay toggling grab on the other keyboard until all keys are released,
>> otherwise keys could be stuck on host without a key up event.
> 
> Hmm, if you have two keyboards plugged into your machine, why would you
> assign both to a virtual machine?  Instead of simply using one for the
> host and one for the guest?

Fair enough. I only noticed the possibility during testing. I plugged in
a second keyboard for development to avoid locking myself out and passed
only one. Then I became confident to pass them both, but mostly because
I already had them connected. I agree it does not seem like a typical
setup...

This was the only code path that did not check !il->keycount before
calling input_linux_toggle_grab(), so I added it here as well. Maybe it
would make sense to move the condition into the function?

Rainer


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/2] input-linux: Delay grab toggle if keys are pressed
  2021-05-09 11:43     ` Rainer Müller
@ 2021-05-10  7:30       ` Gerd Hoffmann
  0 siblings, 0 replies; 7+ messages in thread
From: Gerd Hoffmann @ 2021-05-10  7:30 UTC (permalink / raw)
  To: Rainer Müller; +Cc: qemu-devel

On Sun, May 09, 2021 at 01:43:38PM +0200, Rainer Müller wrote:
> On 04/05/2021 11.14, Gerd Hoffmann wrote:
> > On Sat, May 01, 2021 at 09:06:21PM +0200, Rainer Müller wrote:
> >> When multiple keyboards are passed to the guest with input-linux, there
> >> could still be keys pressed on the other keyboard when toggling grab.
> >> Delay toggling grab on the other keyboard until all keys are released,
> >> otherwise keys could be stuck on host without a key up event.
> > 
> > Hmm, if you have two keyboards plugged into your machine, why would you
> > assign both to a virtual machine?  Instead of simply using one for the
> > host and one for the guest?
> 
> Fair enough. I only noticed the possibility during testing. I plugged in
> a second keyboard for development to avoid locking myself out and passed
> only one. Then I became confident to pass them both, but mostly because
> I already had them connected. I agree it does not seem like a typical
> setup...
> 
> This was the only code path that did not check !il->keycount before
> calling input_linux_toggle_grab(), so I added it here as well. Maybe it
> would make sense to move the condition into the function?

Should work, yes.  I think with that in place patch 2/2 can be
simplified a bit too.

take care,
  Gerd



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2021-05-10  7:32 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-01 19:06 [PATCH 0/2] input-linux: Allow to toggle grab from QMP Rainer Müller
2021-05-01 19:06 ` [PATCH 1/2] input-linux: Delay grab toggle if keys are pressed Rainer Müller
2021-05-04  9:14   ` Gerd Hoffmann
2021-05-09 11:43     ` Rainer Müller
2021-05-10  7:30       ` Gerd Hoffmann
2021-05-01 19:06 ` [PATCH 2/2] input-linux: Allow to toggle grab from QMP Rainer Müller
2021-05-04  9:17   ` Gerd Hoffmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).