Skip to contentSkip to conversation

input-linux: Add option to not grab a device upon guest startup

PATCH In review Justinien Bouron 3 participants 6 messages Apr 3, 2024 – May 7, 2024

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 input-linux: Add option to not grab a device upon guest startup

Depending on your use-case, it might be inconvenient to have qemu grab the input device from the host immediately upon starting the guest.

Added a new bool option to input-linux: grab-on-startup. If true, the device is grabbed as soon as the guest is started, otherwise it is not grabbed until the toggle combination is entered. To avoid breaking existing setups, the default value of grab-on-startup is true, i.e. same behaviour as before this change.

---

Changes since v2:
- Added missing (since 9.1) to the new option in qapi/qom.json

 qapi/qom.json    | 14 +++++++++++++-
 ui/input-linux.c | 20 +++++++++++++++++++-
 2 files changed, 32 insertions(+), 2 deletions(-)
qapi/qom.json +13 -1
index 85e6b4f84a..2067e41991 100644
@@ -508,13 +508,25 @@
 # @grab-toggle: the key or key combination that toggles device grab
 #     (default: ctrl-ctrl)
 #
+# @grab-on-startup: if true, grab the device immediately upon starting
+#     the guest.  Otherwise, don't grab the device until the
+#     combination is entered.  This does not influence other devices
+#     even if grab_all is true, i.e. in the unlikely scenario where
+#     device1 has grab_all=true + grab-on-startup=true and device2 has
+#     grab-on-startup=false, only device1 is grabbed on startup, then,
+#     once the grab combination is entered, grabbing is toggled off
+#     for both devices (because device1 enforces the grab_all
+#     property) until the combination is entered again at which point
+#     both devices will be grabbed.  (default: true)  (since 9.1).
+#
 # Since: 2.6
 ##
 { 'struct': 'InputLinuxProperties',
   'data': { 'evdev': 'str',
             '*grab_all': 'bool',
             '*repeat': 'bool',
-            '*grab-toggle': 'GrabToggleKeys' } }
+            '*grab-toggle': 'GrabToggleKeys',
+            '*grab-on-startup': 'bool'} }
 
 ##
 # @EventLoopBaseProperties:
ui/input-linux.c +19 -1
index e572a2e905..68b5c6d485 100644
@@ -44,6 +44,7 @@ struct InputLinux {
     bool        grab_request;
     bool        grab_active;
     bool        grab_all;
+    bool        grab_on_startup;
     bool        keydown[KEY_CNT];
     int         keycount;
     int         wheel;
@@ -400,7 +401,7 @@ static void input_linux_complete(UserCreatable *uc, Error **errp)
     if (il->keycount) {
         /* delay grab until all keys are released */
         il->grab_request = true;
-    } else {
+    } else if (il->grab_on_startup) {
         input_linux_toggle_grab(il);
     }
     QTAILQ_INSERT_TAIL(&inputs, il, next);
@@ -491,6 +492,19 @@ static void input_linux_set_grab_toggle(Object *obj, int value,
     il->grab_toggle = value;
 }
 
+static bool input_linux_get_grab_on_startup(Object *obj, Error **errp)
+{
+    InputLinux *il = INPUT_LINUX(obj);
+    return il->grab_on_startup;
+}
+
+static void input_linux_set_grab_on_startup(Object *obj, bool value,
+                                            Error **errp)
+{
+    InputLinux *il = INPUT_LINUX(obj);
+    il->grab_on_startup = value;
+}
+
 static void input_linux_instance_init(Object *obj)
 {
 }
@@ -498,6 +512,7 @@ static void input_linux_instance_init(Object *obj)
 static void input_linux_class_init(ObjectClass *oc, void *data)
 {
     UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
+    ObjectProperty *grab_on_startup_prop;
 
     ucc->complete = input_linux_complete;
 
@@ -514,6 +529,9 @@ static void input_linux_class_init(ObjectClass *oc, void *data)
                                    &GrabToggleKeys_lookup,
                                    input_linux_get_grab_toggle,
                                    input_linux_set_grab_toggle);
+    grab_on_startup_prop = object_class_property_add_bool(oc, "grab-on-startup",
+        input_linux_get_grab_on_startup, input_linux_set_grab_on_startup);
+    object_property_set_default_bool(grab_on_startup_prop, true);
 }
 
 static const TypeInfo input_linux_info = {
Signature · 1 line
2.44.0
Acked-by on v3 1/1
#1 lore raw
Quoted 10 lines · Justinien Bouron

Depending on your use-case, it might be inconvenient to have qemu grab the input device from the host immediately upon starting the guest.

Added a new bool option to input-linux: grab-on-startup. If true, the device is grabbed as soon as the guest is started, otherwise it is not grabbed until the toggle combination is entered. To avoid breaking existing setups, the default value of grab-on-startup is true, i.e. same behaviour as before this change.

Again, QAPI schema

#2 lore raw
Quoted 1 line

Again, QAPI schema

Pardon my ignorance, but are you writing this because there is a problem with the QAPI schema changes that I would need to fix and re-submit? Or is it just here to indicate that you've reviewed the change made to the schema?

Regards,
Justinien Bouron

Reviewed-by means I reviewed the entire patch, and approve it.
#3 lore raw
Quoted 6 lines · Justinien Bouron
Quoted 1 line

Again, QAPI schema

Pardon my ignorance, but are you writing this because there is a problem with the QAPI schema changes that I would need to fix and re-submit? Or is it just here to indicate that you've reviewed the change made to the schema?

The latter.

Reviewed-by means I reviewed the entire patch, and approve it.

Acked-by: is weaker, and its meaning depends on context. In this case, it means I reviewed just the QAPI schema, and approve it.

Details (likely more than you ever wanted to know) at:
https://www.kernel.org/doc/html/latest/process/submitting-patches.html

#5 lore raw
In reply to Justinien Bouron

Hi

Quoted 64 lines · Justinien Bouron

Depending on your use-case, it might be inconvenient to have qemu grab the input device from the host immediately upon starting the guest.

Added a new bool option to input-linux: grab-on-startup. If true, the device is grabbed as soon as the guest is started, otherwise it is not grabbed until the toggle combination is entered. To avoid breaking existing setups, the default value of grab-on-startup is true, i.e. same behaviour as before this change.

---

Changes since v2:
- Added missing (since 9.1) to the new option in qapi/qom.json

 qapi/qom.json    | 14 +++++++++++++-
 ui/input-linux.c | 20 +++++++++++++++++++-
 2 files changed, 32 insertions(+), 2 deletions(-)
qapi/qom.json +13 -1
index 85e6b4f84a..2067e41991 100644
@@ -508,13 +508,25 @@
 # @grab-toggle: the key or key combination that toggles device grab
 #     (default: ctrl-ctrl)
 #
+# @grab-on-startup: if true, grab the device immediately upon starting
+#     the guest.  Otherwise, don't grab the device until the
+#     combination is entered.  This does not influence other devices
+#     even if grab_all is true, i.e. in the unlikely scenario where
+#     device1 has grab_all=true + grab-on-startup=true and device2 has
+#     grab-on-startup=false, only device1 is grabbed on startup, then,
+#     once the grab combination is entered, grabbing is toggled off
+#     for both devices (because device1 enforces the grab_all
+#     property) until the combination is entered again at which point
+#     both devices will be grabbed.  (default: true)  (since 9.1).
+#
 # Since: 2.6
 ##
 { 'struct': 'InputLinuxProperties',
   'data': { 'evdev': 'str',
             '*grab_all': 'bool',
             '*repeat': 'bool',
-            '*grab-toggle': 'GrabToggleKeys' } }
+            '*grab-toggle': 'GrabToggleKeys',
+            '*grab-on-startup': 'bool'} }
 
 ##
 # @EventLoopBaseProperties:
ui/input-linux.c +1 -0
index e572a2e905..68b5c6d485 100644
@@ -44,6 +44,7 @@ struct InputLinux {
     bool        grab_request;
     bool        grab_active;
     bool        grab_all;
+    bool        grab_on_startup;
     bool        keydown[KEY_CNT];
     int         keycount;
     int         wheel;

ui/input-linux.c static void input_linux_complete(UserCreatable *uc, Error **errp) · line 406

Quoted 7 lines
ui/input-linux.c +1 -1
@@ -400,7 +401,7 @@ static void input_linux_complete(UserCreatable *uc, Error **errp)
     if (il->keycount) {
         /* delay grab until all keys are released */
         il->grab_request = true;
-    } else {
+    } else if (il->grab_on_startup) {
         input_linux_toggle_grab(il);
     }

The patch looks ok to me, but you don't handle the case where keys are pressed at startup and the grab is delayed. You could add a if (!il->grab_on_startup) goto skip_grab, probably instead.

Quoted 42 lines
ui/input-linux.c +17 -1
     QTAILQ_INSERT_TAIL(&inputs, il, next);
@@ -491,6 +492,19 @@ static void input_linux_set_grab_toggle(Object *obj, int value,
     il->grab_toggle = value;
 }
 
+static bool input_linux_get_grab_on_startup(Object *obj, Error **errp)
+{
+    InputLinux *il = INPUT_LINUX(obj);
+    return il->grab_on_startup;
+}
+
+static void input_linux_set_grab_on_startup(Object *obj, bool value,
+                                            Error **errp)
+{
+    InputLinux *il = INPUT_LINUX(obj);
+    il->grab_on_startup = value;
+}
+
 static void input_linux_instance_init(Object *obj)
 {
 }
@@ -498,6 +512,7 @@ static void input_linux_instance_init(Object *obj)
 static void input_linux_class_init(ObjectClass *oc, void *data)
 {
     UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
+    ObjectProperty *grab_on_startup_prop;
 
     ucc->complete = input_linux_complete;
 
@@ -514,6 +529,9 @@ static void input_linux_class_init(ObjectClass *oc, void *data)
                                    &GrabToggleKeys_lookup,
                                    input_linux_get_grab_toggle,
                                    input_linux_set_grab_toggle);
+    grab_on_startup_prop = object_class_property_add_bool(oc, "grab-on-startup",
+        input_linux_get_grab_on_startup, input_linux_set_grab_on_startup);
+    object_property_set_default_bool(grab_on_startup_prop, true);
 }
 
 static const TypeInfo input_linux_info = {
--

2.44.0