Repository navigation
Avoid unchanged USB keyboard reports at idle #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
0xdaniell
wants to merge
1
commit into
main
Choose a base branch
from
fix/usb-keyboard-idle
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
src/patches/qemu/0015-usb-kbd-x-ignore-idle-an-idle-keyboard-reports-only-on.patch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 | ||
| From: macbox <macbox> | ||
| Date: Sun, 15 Nov 2026 01:20:04 +0200 | ||
| Subject: [PATCH 15/15] usb-kbd: x-ignore-idle, an idle keyboard reports only on key | ||
| changes | ||
|
|
||
| Allow usb-kbd to ignore the HID idle timer while preserving the idle value | ||
| reported to the guest. The property defaults to off and can be changed live. | ||
| The guest idle value is reset on device reset and is not migrated. | ||
|
|
||
| --- | ||
| hw/usb/dev-hid.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++- | ||
| 1 file changed, 54 insertions(+), 1 deletion(-) | ||
|
|
||
| diff --git a/hw/usb/dev-hid.c b/hw/usb/dev-hid.c | ||
| index ae19d60203..1d0e36d405 100644 | ||
| --- a/hw/usb/dev-hid.c | ||
| +++ b/hw/usb/dev-hid.c | ||
| @@ -43,6 +43,19 @@ struct USBHIDState { | ||
| uint32_t usb_version; | ||
| char *display; | ||
| uint32_t head; | ||
| + /* | ||
| + * x-ignore-idle (usb-kbd only, off by default). macOS sets | ||
| + * the keyboard's idle rate to 24 ms, so an idle keyboard sends a report | ||
| + * every 24 ms (41.6/s: 2026-11-15, QEMU's usb_xhci_xfer_success trace) | ||
| + * and the guest's HID driver wakes for each. With it on, SET_IDLE is | ||
| + * accepted and GET_IDLE answers it, but the keyboard reports only when | ||
| + * a key changes, as it does at idle rate 0. macOS repeats held keys | ||
| + * itself. Live: qom-set turns it on or off in a running VM (an A/B in | ||
| + * one boot); -global usb-kbd.x-ignore-idle=on sets it at start. Not | ||
| + * migrated. | ||
| + */ | ||
| + bool ignore_idle; | ||
| + uint8_t guest_idle; | ||
| }; | ||
|
|
||
| #define TYPE_USB_HID "usb-hid" | ||
| @@ -570,6 +583,7 @@ static void usb_hid_handle_reset(USBDevice *dev) | ||
| USBHIDState *us = USB_HID(dev); | ||
|
|
||
| hid_reset(&us->hid); | ||
| + us->guest_idle = 0; | ||
| } | ||
|
|
||
| static void usb_hid_handle_control(USBDevice *dev, USBPacket *p, | ||
| @@ -635,10 +649,14 @@ static void usb_hid_handle_control(USBDevice *dev, USBPacket *p, | ||
| hs->protocol = value; | ||
| break; | ||
| case HID_GET_IDLE: | ||
| - data[0] = hs->idle; | ||
| + data[0] = us->ignore_idle ? us->guest_idle : hs->idle; | ||
| p->actual_length = 1; | ||
| break; | ||
| case HID_SET_IDLE: | ||
| + if (us->ignore_idle) { | ||
| + us->guest_idle = (uint8_t) (value >> 8); | ||
| + break; | ||
| + } | ||
| hs->idle = (uint8_t) (value >> 8); | ||
| hid_set_next_idle(hs); | ||
| if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) { | ||
| @@ -844,6 +862,35 @@ static const Property usb_keyboard_properties[] = { | ||
| DEFINE_PROP_STRING("display", USBHIDState, display), | ||
| }; | ||
|
|
||
| +static bool usb_kbd_get_ignore_idle(Object *obj, Error **errp) | ||
| +{ | ||
| + return USB_HID(obj)->ignore_idle; | ||
| +} | ||
| + | ||
| +/* | ||
| + * On: the idle rate the guest set is kept aside and the keyboard's is 0, so | ||
| + * its timer goes. Off: the guest's rate is the keyboard's again. | ||
| + */ | ||
| +static void usb_kbd_set_ignore_idle(Object *obj, bool value, Error **errp) | ||
| +{ | ||
| + USBHIDState *us = USB_HID(obj); | ||
| + HIDState *hs = &us->hid; | ||
| + | ||
| + if (value == us->ignore_idle) { | ||
| + return; | ||
| + } | ||
| + if (value) { | ||
| + us->guest_idle = hs->idle; | ||
| + hs->idle = 0; | ||
| + } else { | ||
| + hs->idle = us->guest_idle; | ||
| + } | ||
| + us->ignore_idle = value; | ||
| + if (hs->event) { | ||
| + hid_set_next_idle(hs); | ||
| + } | ||
| +} | ||
| + | ||
| static void usb_keyboard_class_initfn(ObjectClass *klass, const void *data) | ||
| { | ||
| DeviceClass *dc = DEVICE_CLASS(klass); | ||
| @@ -853,6 +900,12 @@ static void usb_keyboard_class_initfn(ObjectClass *klass, const void *data) | ||
| uc->product_desc = "QEMU USB Keyboard"; | ||
| dc->vmsd = &vmstate_usb_kbd; | ||
| device_class_set_props(dc, usb_keyboard_properties); | ||
| + object_class_property_add_bool(klass, "x-ignore-idle", | ||
| + usb_kbd_get_ignore_idle, | ||
| + usb_kbd_set_ignore_idle); | ||
| + object_class_property_set_description(klass, "x-ignore-idle", | ||
| + "Report only on key changes, whatever idle rate the " | ||
| + "guest sets (live)"); | ||
| set_bit(DEVICE_CATEGORY_INPUT, dc->categories); | ||
| } | ||
|
|
||
| -- | ||
| 2.53.0 | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When using
macbox build --upstreamor switchingqemu/currentto a pre-0015 build, this unconditional argument prevents the VM from starting: as the addeddocs/setup.mdwarning states, “older builds cannot start with that flag.” The upstream branch ofsrc/container/build-reims.shexplicitly skips the patches, whilecmd_up()rewrites--extra-argsusingqemu_extra()before every stopped-VM start, so removing the argument fromvm.tomldoes not resolve it. This also breaks the documented rollback-by-symlink procedure unless users edit the launcher itself. Check the selected QEMU's capabilities before adding the property so upstream builds and rollback remain usable.AGENTS.md reference: AGENTS.md:L108-L110
Useful? React with 👍 / 👎.