input-linux: Allow to toggle grab from QMP
cover input-linux: Allow to toggle grab from QMP
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(-)
Signature · 1 line
2.25.1
PATCH 1/2 input-linux: Delay grab toggle if keys are pressed
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.
--- ui/input-linux.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
ui/input-linux.c +6 -1
@@ -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); + } } } }
Signature · 1 line
2.25.1
Re: [PATCH 1/2] input-linux: Delay grab toggle if keys are pressed
Quoted 4 lines · 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.
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
Re: [PATCH 1/2] input-linux: Delay grab toggle if keys are pressed
Quoted 9 lines · Gerd Hoffmann
Quoted 4 lines · 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.
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
Re: [PATCH 1/2] input-linux: Delay grab toggle if keys are pressed
Quoted 17 lines · Rainer Müller
Quoted 9 lines · Gerd Hoffmann
Quoted 4 lines · 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.
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...
Quoted 3 lines
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
PATCH 2/2 input-linux: Allow to toggle grab from QMP
In reply to Rainer MüllerThis 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.
--- qapi/qom.json | 3 +++ ui/input-linux.c | 39 +++++++++++++++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 4 deletions(-)
qapi/qom.json +3 -0
@@ -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' } } ##
ui/input-linux.c +35 -4
@@ -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,
Signature · 1 line
2.25.1
qapi/qom.json line 495
Quoted 9 lines
qapi/qom.json +2
-0@@ -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