Coverage Report

Created: 2026-10-31 08:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/hw/input/hid.c
Line
Count
Source
1
/*
2
 * QEMU HID devices
3
 *
4
 * Copyright (c) 2005 Fabrice Bellard
5
 * Copyright (c) 2007 OpenMoko, Inc.  (andrew@openedhand.com)
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
 * THE SOFTWARE.
24
 */
25
26
#include "qemu/osdep.h"
27
#include "ui/console.h"
28
#include "ui/input.h"
29
#include "qemu/timer.h"
30
#include "hw/input/hid.h"
31
#include "migration/vmstate.h"
32
#include "trace.h"
33
34
0
#define HID_USAGE_ERROR_ROLLOVER        0x01
35
#define HID_USAGE_POSTFAIL              0x02
36
#define HID_USAGE_ERROR_UNDEFINED       0x03
37
38
/* Indices are QEMU keycodes, values are from HID Usage Table.  Indices
39
 * above 0x80 are for keys that come after 0xe0 or 0xe1+0x1d or 0xe1+0x9d.  */
40
static const uint8_t hid_usage_keys[0x100] = {
41
    0x00, 0x29, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
42
    0x24, 0x25, 0x26, 0x27, 0x2d, 0x2e, 0x2a, 0x2b,
43
    0x14, 0x1a, 0x08, 0x15, 0x17, 0x1c, 0x18, 0x0c,
44
    0x12, 0x13, 0x2f, 0x30, 0x28, 0xe0, 0x04, 0x16,
45
    0x07, 0x09, 0x0a, 0x0b, 0x0d, 0x0e, 0x0f, 0x33,
46
    0x34, 0x35, 0xe1, 0x31, 0x1d, 0x1b, 0x06, 0x19,
47
    0x05, 0x11, 0x10, 0x36, 0x37, 0x38, 0xe5, 0x55,
48
    0xe2, 0x2c, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e,
49
    0x3f, 0x40, 0x41, 0x42, 0x43, 0x53, 0x47, 0x5f,
50
    0x60, 0x61, 0x56, 0x5c, 0x5d, 0x5e, 0x57, 0x59,
51
    0x5a, 0x5b, 0x62, 0x63, 0x46, 0x00, 0x64, 0x44,
52
    0x45, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e,
53
    0xe8, 0xe9, 0x71, 0x72, 0x73, 0x00, 0x00, 0x00,
54
    0x00, 0x00, 0x00, 0x85, 0x00, 0x00, 0x00, 0x00,
55
    0x88, 0x00, 0x00, 0x87, 0x00, 0x00, 0x00, 0x00,
56
    0x00, 0x8a, 0x00, 0x8b, 0x00, 0x89, 0xe7, 0x65,
57
58
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
59
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
60
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61
    0x00, 0x00, 0x00, 0x00, 0x58, 0xe4, 0x00, 0x00,
62
    0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
63
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x00,
64
    0x80, 0x00, 0x00, 0x00, 0x00, 0x54, 0x00, 0x46,
65
    0xe6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
66
    0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x48, 0x4a,
67
    0x52, 0x4b, 0x00, 0x50, 0x00, 0x4f, 0x00, 0x4d,
68
    0x51, 0x4e, 0x49, 0x4c, 0x00, 0x00, 0x00, 0x00,
69
    0x00, 0x00, 0x00, 0xe3, 0xe7, 0x65, 0x66, 0x00,
70
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
71
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
72
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
73
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
74
};
75
76
bool hid_has_events(HIDState *hs)
77
3.95k
{
78
3.95k
    return hs->n > 0 || hs->idle_pending;
79
3.95k
}
80
81
static void hid_idle_timer(void *opaque)
82
9
{
83
9
    HIDState *hs = opaque;
84
85
9
    hs->idle_pending = true;
86
9
    hs->event(hs);
87
9
}
88
89
static void hid_del_idle_timer(HIDState *hs)
90
19.6k
{
91
19.6k
    if (hs->idle_timer) {
92
10
        timer_free(hs->idle_timer);
93
10
        hs->idle_timer = NULL;
94
10
    }
95
19.6k
}
96
97
void hid_set_next_idle(HIDState *hs)
98
58
{
99
58
    if (hs->idle) {
100
58
        uint64_t expire_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
101
58
                               NANOSECONDS_PER_SECOND * hs->idle * 4 / 1000;
102
58
        if (!hs->idle_timer) {
103
10
            hs->idle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, hid_idle_timer, hs);
104
10
        }
105
58
        timer_mod_ns(hs->idle_timer, expire_time);
106
58
    } else {
107
0
        hid_del_idle_timer(hs);
108
0
    }
109
58
}
110
111
static void hid_pointer_event(DeviceState *dev, QemuConsole *src,
112
                              QemuInputEvent *evt)
113
0
{
114
0
    static const int bmap[INPUT_BUTTON__MAX] = {
115
0
        [INPUT_BUTTON_LEFT]   = 0x01,
116
0
        [INPUT_BUTTON_RIGHT]  = 0x02,
117
0
        [INPUT_BUTTON_MIDDLE] = 0x04,
118
0
        [INPUT_BUTTON_SIDE] = 0x08,
119
0
        [INPUT_BUTTON_EXTRA] = 0x10,
120
0
    };
121
0
    HIDState *hs = (HIDState *)dev;
122
0
    HIDPointerEvent *e;
123
124
0
    assert(hs->n < QUEUE_LENGTH);
125
0
    e = &hs->ptr.queue[(hs->head + hs->n) & QUEUE_MASK];
126
127
0
    switch (evt->type) {
128
0
    case INPUT_EVENT_KIND_REL:
129
0
        if (evt->rel.axis == INPUT_AXIS_X) {
130
0
            e->xdx += evt->rel.value;
131
0
        } else if (evt->rel.axis == INPUT_AXIS_Y) {
132
0
            e->ydy += evt->rel.value;
133
0
        }
134
0
        break;
135
136
0
    case INPUT_EVENT_KIND_ABS:
137
0
        if (evt->abs.axis == INPUT_AXIS_X) {
138
0
            e->xdx = evt->abs.value;
139
0
        } else if (evt->abs.axis == INPUT_AXIS_Y) {
140
0
            e->ydy = evt->abs.value;
141
0
        }
142
0
        break;
143
144
0
    case INPUT_EVENT_KIND_BTN:
145
0
        if (evt->btn.down) {
146
0
            e->buttons_state |= bmap[evt->btn.button];
147
0
            if (evt->btn.button == INPUT_BUTTON_WHEEL_UP) {
148
0
                e->dz--;
149
0
            } else if (evt->btn.button == INPUT_BUTTON_WHEEL_DOWN) {
150
0
                e->dz++;
151
0
            }
152
0
        } else {
153
0
            e->buttons_state &= ~bmap[evt->btn.button];
154
0
        }
155
0
        break;
156
157
0
    default:
158
        /* keep gcc happy */
159
0
        break;
160
0
    }
161
162
0
}
163
164
static void hid_pointer_sync(DeviceState *dev)
165
0
{
166
0
    HIDState *hs = (HIDState *)dev;
167
0
    HIDPointerEvent *prev, *curr, *next;
168
0
    bool event_compression = false;
169
170
0
    if (hs->n == QUEUE_LENGTH-1) {
171
        /*
172
         * Queue full.  We are losing information, but we at least
173
         * keep track of most recent button state.
174
         */
175
0
        return;
176
0
    }
177
178
0
    prev = &hs->ptr.queue[(hs->head + hs->n - 1) & QUEUE_MASK];
179
0
    curr = &hs->ptr.queue[(hs->head + hs->n) & QUEUE_MASK];
180
0
    next = &hs->ptr.queue[(hs->head + hs->n + 1) & QUEUE_MASK];
181
182
0
    if (hs->n > 0) {
183
        /*
184
         * No button state change between previous and current event
185
         * (and previous wasn't seen by the guest yet), so there is
186
         * motion information only and we can combine the two event
187
         * into one.
188
         */
189
0
        if (curr->buttons_state == prev->buttons_state) {
190
0
            event_compression = true;
191
0
        }
192
0
    }
193
194
0
    if (event_compression) {
195
        /* add current motion to previous, clear current */
196
0
        if (hs->kind == HID_MOUSE) {
197
0
            prev->xdx += curr->xdx;
198
0
            curr->xdx = 0;
199
0
            prev->ydy += curr->ydy;
200
0
            curr->ydy = 0;
201
0
        } else {
202
0
            prev->xdx = curr->xdx;
203
0
            prev->ydy = curr->ydy;
204
0
        }
205
0
        prev->dz += curr->dz;
206
0
        curr->dz = 0;
207
0
    } else {
208
        /* prepare next (clear rel, copy abs + btns) */
209
0
        if (hs->kind == HID_MOUSE) {
210
0
            next->xdx = 0;
211
0
            next->ydy = 0;
212
0
        } else {
213
0
            next->xdx = curr->xdx;
214
0
            next->ydy = curr->ydy;
215
0
        }
216
0
        next->dz = 0;
217
0
        next->buttons_state = curr->buttons_state;
218
        /* make current guest visible, notify guest */
219
0
        hs->n++;
220
0
        hs->event(hs);
221
0
    }
222
0
}
223
224
static void hid_keyboard_event(DeviceState *dev, QemuConsole *src,
225
                               QemuInputEvent *evt)
226
0
{
227
0
    HIDState *hs = (HIDState *)dev;
228
0
    int scancodes[3], i, count;
229
0
    int slot;
230
231
0
    count = qemu_input_linux_to_scancode(evt->key.key, evt->key.down,
232
0
                                         scancodes);
233
0
    if (hs->n + count > QUEUE_LENGTH) {
234
0
        trace_hid_kbd_queue_full();
235
0
        return;
236
0
    }
237
0
    for (i = 0; i < count; i++) {
238
0
        slot = (hs->head + hs->n) & QUEUE_MASK; hs->n++;
239
0
        hs->kbd.keycodes[slot] = scancodes[i];
240
0
    }
241
0
    hs->event(hs);
242
0
}
243
244
static void hid_keyboard_process_keycode(HIDState *hs)
245
0
{
246
0
    uint8_t hid_code, index, key;
247
0
    int i, keycode, slot;
248
249
0
    if (hs->n == 0) {
250
0
        return;
251
0
    }
252
0
    slot = hs->head & QUEUE_MASK; QUEUE_INCR(hs->head); hs->n--;
253
0
    keycode = hs->kbd.keycodes[slot];
254
255
0
    if (!hs->n) {
256
0
        trace_hid_kbd_queue_empty();
257
0
    }
258
259
0
    key = keycode & 0x7f;
260
0
    index = key | ((hs->kbd.modifiers & (1 << 8)) >> 1);
261
0
    hid_code = hid_usage_keys[index];
262
0
    hs->kbd.modifiers &= ~(1 << 8);
263
264
0
    switch (hid_code) {
265
0
    case 0x00:
266
0
        return;
267
268
0
    case 0xe0:
269
0
        assert(key == 0x1d);
270
0
        if (hs->kbd.modifiers & (1 << 9)) {
271
            /* The hid_codes for the 0xe1/0x1d scancode sequence are 0xe9/0xe0.
272
             * Here we're processing the second hid_code.  By dropping bit 9
273
             * and setting bit 8, the scancode after 0x1d will access the
274
             * second half of the table.
275
             */
276
0
            hs->kbd.modifiers ^= (1 << 8) | (1 << 9);
277
0
            return;
278
0
        }
279
        /* fall through to process Ctrl_L */
280
0
    case 0xe1 ... 0xe7:
281
        /* Ctrl_L/Ctrl_R, Shift_L/Shift_R, Alt_L/Alt_R, Win_L/Win_R.
282
         * Handle releases here, or fall through to process presses.
283
         */
284
0
        if (keycode & (1 << 7)) {
285
0
            hs->kbd.modifiers &= ~(1 << (hid_code & 0x0f));
286
0
            return;
287
0
        }
288
        /* fall through */
289
0
    case 0xe8 ... 0xe9:
290
        /* USB modifiers are just 1 byte long.  Bits 8 and 9 of
291
         * hs->kbd.modifiers implement a state machine that detects the
292
         * 0xe0 and 0xe1/0x1d sequences.  These bits do not follow the
293
         * usual rules where bit 7 marks released keys; they are cleared
294
         * elsewhere in the function as the state machine dictates.
295
         */
296
0
        hs->kbd.modifiers |= 1 << (hid_code & 0x0f);
297
0
        return;
298
299
0
    case 0xea ... 0xef:
300
0
        abort();
301
302
0
    default:
303
0
        break;
304
0
    }
305
306
0
    if (keycode & (1 << 7)) {
307
0
        for (i = hs->kbd.keys - 1; i >= 0; i--) {
308
0
            if (hs->kbd.key[i] == hid_code) {
309
0
                hs->kbd.key[i] = hs->kbd.key[-- hs->kbd.keys];
310
0
                hs->kbd.key[hs->kbd.keys] = 0x00;
311
0
                break;
312
0
            }
313
0
        }
314
0
        if (i < 0) {
315
0
            return;
316
0
        }
317
0
    } else {
318
0
        for (i = hs->kbd.keys - 1; i >= 0; i--) {
319
0
            if (hs->kbd.key[i] == hid_code) {
320
0
                break;
321
0
            }
322
0
        }
323
0
        if (i < 0) {
324
0
            if (hs->kbd.keys < sizeof(hs->kbd.key)) {
325
0
                hs->kbd.key[hs->kbd.keys++] = hid_code;
326
0
            }
327
0
        } else {
328
0
            return;
329
0
        }
330
0
    }
331
0
}
332
333
static inline int int_clamp(int val, int vmin, int vmax)
334
0
{
335
0
    if (val < vmin) {
336
0
        return vmin;
337
0
    } else if (val > vmax) {
338
0
        return vmax;
339
0
    } else {
340
0
        return val;
341
0
    }
342
0
}
343
344
void hid_pointer_activate(HIDState *hs)
345
4.01k
{
346
4.01k
    if (!hs->ptr.mouse_grabbed) {
347
1
        qemu_input_handler_activate(hs->s);
348
1
        hs->ptr.mouse_grabbed = 1;
349
1
    }
350
4.01k
}
351
352
int hid_pointer_poll(HIDState *hs, uint8_t *buf, int len)
353
0
{
354
0
    int dx, dy, dz, l;
355
0
    int index;
356
0
    HIDPointerEvent *e;
357
358
0
    hs->idle_pending = false;
359
360
0
    hid_pointer_activate(hs);
361
362
    /* When the buffer is empty, return the last event.  Relative
363
       movements will all be zero.  */
364
0
    index = (hs->n ? hs->head : hs->head - 1);
365
0
    e = &hs->ptr.queue[index & QUEUE_MASK];
366
367
0
    if (hs->kind == HID_MOUSE) {
368
0
        dx = int_clamp(e->xdx, -127, 127);
369
0
        dy = int_clamp(e->ydy, -127, 127);
370
0
        e->xdx -= dx;
371
0
        e->ydy -= dy;
372
0
    } else {
373
0
        dx = e->xdx;
374
0
        dy = e->ydy;
375
0
    }
376
0
    dz = int_clamp(e->dz, -127, 127);
377
0
    e->dz -= dz;
378
379
0
    if (hs->n &&
380
0
        !e->dz &&
381
0
        (hs->kind == HID_TABLET || (!e->xdx && !e->ydy))) {
382
        /* that deals with this event */
383
0
        QUEUE_INCR(hs->head);
384
0
        hs->n--;
385
0
    }
386
387
    /* Appears we have to invert the wheel direction */
388
0
    dz = 0 - dz;
389
0
    l = 0;
390
0
    switch (hs->kind) {
391
0
    case HID_MOUSE:
392
0
        if (len > l) {
393
0
            buf[l++] = e->buttons_state;
394
0
        }
395
0
        if (len > l) {
396
0
            buf[l++] = dx;
397
0
        }
398
0
        if (len > l) {
399
0
            buf[l++] = dy;
400
0
        }
401
0
        if (len > l) {
402
0
            buf[l++] = dz;
403
0
        }
404
0
        break;
405
406
0
    case HID_TABLET:
407
0
        if (len > l) {
408
0
            buf[l++] = e->buttons_state;
409
0
        }
410
0
        if (len > l) {
411
0
            buf[l++] = dx & 0xff;
412
0
        }
413
0
        if (len > l) {
414
0
            buf[l++] = dx >> 8;
415
0
        }
416
0
        if (len > l) {
417
0
            buf[l++] = dy & 0xff;
418
0
        }
419
0
        if (len > l) {
420
0
            buf[l++] = dy >> 8;
421
0
        }
422
0
        if (len > l) {
423
0
            buf[l++] = dz;
424
0
        }
425
0
        break;
426
427
0
    default:
428
0
        abort();
429
0
    }
430
431
0
    return l;
432
0
}
433
434
int hid_keyboard_poll(HIDState *hs, uint8_t *buf, int len)
435
0
{
436
0
    hs->idle_pending = false;
437
438
0
    if (len < 2) {
439
0
        return 0;
440
0
    }
441
442
0
    hid_keyboard_process_keycode(hs);
443
444
0
    buf[0] = hs->kbd.modifiers & 0xff;
445
0
    buf[1] = 0;
446
0
    if (hs->kbd.keys > 6) {
447
0
        memset(buf + 2, HID_USAGE_ERROR_ROLLOVER, MIN(8, len) - 2);
448
0
    } else {
449
0
        memcpy(buf + 2, hs->kbd.key, MIN(8, len) - 2);
450
0
    }
451
452
0
    return MIN(8, len);
453
0
}
454
455
int hid_keyboard_write(HIDState *hs, uint8_t *buf, int len)
456
0
{
457
0
    if (len > 0) {
458
0
        int ledstate = 0;
459
        /* 0x01: Num Lock LED
460
         * 0x02: Caps Lock LED
461
         * 0x04: Scroll Lock LED
462
         * 0x08: Compose LED
463
         * 0x10: Kana LED */
464
0
        hs->kbd.leds = buf[0];
465
0
        if (hs->kbd.leds & 0x04) {
466
0
            ledstate |= QEMU_SCROLL_LOCK_LED;
467
0
        }
468
0
        if (hs->kbd.leds & 0x01) {
469
0
            ledstate |= QEMU_NUM_LOCK_LED;
470
0
        }
471
0
        if (hs->kbd.leds & 0x02) {
472
0
            ledstate |= QEMU_CAPS_LOCK_LED;
473
0
        }
474
0
        qemu_input_handler_set_leds_mask(hs->s, ledstate);
475
0
        return 1;
476
0
    }
477
0
    return 0;
478
0
}
479
480
void hid_reset(HIDState *hs)
481
19.6k
{
482
19.6k
    switch (hs->kind) {
483
3.22k
    case HID_KEYBOARD:
484
3.22k
        memset(hs->kbd.keycodes, 0, sizeof(hs->kbd.keycodes));
485
3.22k
        memset(hs->kbd.key, 0, sizeof(hs->kbd.key));
486
3.22k
        hs->kbd.keys = 0;
487
3.22k
        hs->kbd.modifiers = 0;
488
3.22k
        break;
489
326
    case HID_MOUSE:
490
16.4k
    case HID_TABLET:
491
16.4k
        memset(hs->ptr.queue, 0, sizeof(hs->ptr.queue));
492
16.4k
        break;
493
19.6k
    }
494
19.6k
    hs->head = 0;
495
19.6k
    hs->n = 0;
496
19.6k
    hs->protocol = 1;
497
19.6k
    hs->idle = 0;
498
19.6k
    hs->idle_pending = false;
499
19.6k
    hid_del_idle_timer(hs);
500
19.6k
}
501
502
void hid_free(HIDState *hs)
503
0
{
504
0
    qemu_input_handler_unregister(hs->s);
505
0
    hid_del_idle_timer(hs);
506
0
}
507
508
static const QemuInputHandler hid_keyboard_handler = {
509
    .name  = "QEMU HID Keyboard",
510
    .mask  = INPUT_EVENT_MASK_KEY,
511
    .event = hid_keyboard_event,
512
};
513
514
static const QemuInputHandler hid_mouse_handler = {
515
    .name  = "QEMU HID Mouse",
516
    .mask  = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_REL,
517
    .event = hid_pointer_event,
518
    .sync  = hid_pointer_sync,
519
};
520
521
static const QemuInputHandler hid_tablet_handler = {
522
    .name  = "QEMU HID Tablet",
523
    .mask  = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_ABS,
524
    .event = hid_pointer_event,
525
    .sync  = hid_pointer_sync,
526
};
527
528
void hid_init(HIDState *hs, int kind, HIDEventFunc event)
529
11
{
530
11
    hs->kind = kind;
531
11
    hs->event = event;
532
533
11
    if (hs->kind == HID_KEYBOARD) {
534
3
        hs->s = qemu_input_handler_register((DeviceState *)hs,
535
3
                                            &hid_keyboard_handler);
536
3
        qemu_input_handler_activate(hs->s);
537
8
    } else if (hs->kind == HID_MOUSE) {
538
2
        hs->s = qemu_input_handler_register((DeviceState *)hs,
539
2
                                            &hid_mouse_handler);
540
6
    } else if (hs->kind == HID_TABLET) {
541
6
        hs->s = qemu_input_handler_register((DeviceState *)hs,
542
6
                                            &hid_tablet_handler);
543
6
    }
544
11
}
545
546
static int hid_post_load(void *opaque, int version_id)
547
0
{
548
0
    HIDState *s = opaque;
549
550
0
    hid_set_next_idle(s);
551
552
0
    if (s->n == QUEUE_LENGTH && (s->kind == HID_TABLET ||
553
0
                                 s->kind == HID_MOUSE)) {
554
        /*
555
         * Handle ptr device migration from old qemu with full queue.
556
         *
557
         * Throw away everything but the last event, so we propagate
558
         * at least the current button state to the guest.  Also keep
559
         * current position for the tablet, signal "no motion" for the
560
         * mouse.
561
         */
562
0
        HIDPointerEvent evt;
563
0
        evt = s->ptr.queue[(s->head+s->n) & QUEUE_MASK];
564
0
        if (s->kind == HID_MOUSE) {
565
0
            evt.xdx = 0;
566
0
            evt.ydy = 0;
567
0
        }
568
0
        s->ptr.queue[0] = evt;
569
0
        s->head = 0;
570
0
        s->n = 1;
571
0
    }
572
0
    return 0;
573
0
}
574
575
static const VMStateDescription vmstate_hid_ptr_queue = {
576
    .name = "HIDPointerEventQueue",
577
    .version_id = 1,
578
    .minimum_version_id = 1,
579
    .fields = (const VMStateField[]) {
580
        VMSTATE_INT32(xdx, HIDPointerEvent),
581
        VMSTATE_INT32(ydy, HIDPointerEvent),
582
        VMSTATE_INT32(dz, HIDPointerEvent),
583
        VMSTATE_INT32(buttons_state, HIDPointerEvent),
584
        VMSTATE_END_OF_LIST()
585
    }
586
};
587
588
const VMStateDescription vmstate_hid_ptr_device = {
589
    .name = "HIDPointerDevice",
590
    .version_id = 1,
591
    .minimum_version_id = 1,
592
    .post_load = hid_post_load,
593
    .fields = (const VMStateField[]) {
594
        VMSTATE_STRUCT_ARRAY(ptr.queue, HIDState, QUEUE_LENGTH, 0,
595
                             vmstate_hid_ptr_queue, HIDPointerEvent),
596
        VMSTATE_UINT32(head, HIDState),
597
        VMSTATE_UINT32(n, HIDState),
598
        VMSTATE_INT32(protocol, HIDState),
599
        VMSTATE_UINT8(idle, HIDState),
600
        VMSTATE_END_OF_LIST(),
601
    }
602
};
603
604
const VMStateDescription vmstate_hid_keyboard_device = {
605
    .name = "HIDKeyboardDevice",
606
    .version_id = 1,
607
    .minimum_version_id = 1,
608
    .post_load = hid_post_load,
609
    .fields = (const VMStateField[]) {
610
        VMSTATE_UINT32_ARRAY(kbd.keycodes, HIDState, QUEUE_LENGTH),
611
        VMSTATE_UINT32(head, HIDState),
612
        VMSTATE_UINT32(n, HIDState),
613
        VMSTATE_UINT16(kbd.modifiers, HIDState),
614
        VMSTATE_UINT8(kbd.leds, HIDState),
615
        VMSTATE_UINT8_ARRAY(kbd.key, HIDState, 16),
616
        VMSTATE_INT32(kbd.keys, HIDState),
617
        VMSTATE_INT32(protocol, HIDState),
618
        VMSTATE_UINT8(idle, HIDState),
619
        VMSTATE_END_OF_LIST(),
620
    }
621
};