stellaris_input: Fix vmstate description of buttons field
[qemu/ar7.git] / hw / input / stellaris_input.c
blob3a666d61d47eaa84d00aadbc67fdc81e4c7d1344
1 /*
2 * Gamepad style buttons connected to IRQ/GPIO lines
4 * Copyright (c) 2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the GPL.
8 */
9 #include "qemu/osdep.h"
10 #include "hw/hw.h"
11 #include "hw/input/gamepad.h"
12 #include "ui/console.h"
14 typedef struct {
15 qemu_irq irq;
16 int keycode;
17 uint8_t pressed;
18 } gamepad_button;
20 typedef struct {
21 gamepad_button *buttons;
22 int num_buttons;
23 int extension;
24 } gamepad_state;
26 static void stellaris_gamepad_put_key(void * opaque, int keycode)
28 gamepad_state *s = (gamepad_state *)opaque;
29 int i;
30 int down;
32 if (keycode == 0xe0 && !s->extension) {
33 s->extension = 0x80;
34 return;
37 down = (keycode & 0x80) == 0;
38 keycode = (keycode & 0x7f) | s->extension;
40 for (i = 0; i < s->num_buttons; i++) {
41 if (s->buttons[i].keycode == keycode
42 && s->buttons[i].pressed != down) {
43 s->buttons[i].pressed = down;
44 qemu_set_irq(s->buttons[i].irq, down);
48 s->extension = 0;
51 static const VMStateDescription vmstate_stellaris_button = {
52 .name = "stellaris_button",
53 .version_id = 0,
54 .minimum_version_id = 0,
55 .fields = (VMStateField[]) {
56 VMSTATE_UINT8(pressed, gamepad_button),
57 VMSTATE_END_OF_LIST()
61 static const VMStateDescription vmstate_stellaris_gamepad = {
62 .name = "stellaris_gamepad",
63 .version_id = 2,
64 .minimum_version_id = 2,
65 .fields = (VMStateField[]) {
66 VMSTATE_INT32(extension, gamepad_state),
67 VMSTATE_STRUCT_VARRAY_POINTER_INT32(buttons, gamepad_state,
68 num_buttons,
69 vmstate_stellaris_button,
70 gamepad_button),
71 VMSTATE_END_OF_LIST()
75 /* Returns an array of 5 output slots. */
76 void stellaris_gamepad_init(int n, qemu_irq *irq, const int *keycode)
78 gamepad_state *s;
79 int i;
81 s = g_new0(gamepad_state, 1);
82 s->buttons = g_new0(gamepad_button, n);
83 for (i = 0; i < n; i++) {
84 s->buttons[i].irq = irq[i];
85 s->buttons[i].keycode = keycode[i];
87 s->num_buttons = n;
88 qemu_add_kbd_event_handler(stellaris_gamepad_put_key, s);
89 vmstate_register(NULL, -1, &vmstate_stellaris_gamepad, s);