1
2
3
4
5
6
7
8
9#include "hw.h"
10#include "devices.h"
11#include "console.h"
12
13typedef struct {
14 qemu_irq irq;
15 int keycode;
16 uint8_t pressed;
17} gamepad_button;
18
19typedef struct {
20 gamepad_button *buttons;
21 int num_buttons;
22 int extension;
23} gamepad_state;
24
25static void stellaris_gamepad_put_key(void * opaque, int keycode)
26{
27 gamepad_state *s = (gamepad_state *)opaque;
28 int i;
29 int down;
30
31 if (keycode == 0xe0 && !s->extension) {
32 s->extension = 0x80;
33 return;
34 }
35
36 down = (keycode & 0x80) == 0;
37 keycode = (keycode & 0x7f) | s->extension;
38
39 for (i = 0; i < s->num_buttons; i++) {
40 if (s->buttons[i].keycode == keycode
41 && s->buttons[i].pressed != down) {
42 s->buttons[i].pressed = down;
43 qemu_set_irq(s->buttons[i].irq, down);
44 }
45 }
46
47 s->extension = 0;
48}
49
50static const VMStateDescription vmstate_stellaris_button = {
51 .name = "stellaris_button",
52 .version_id = 0,
53 .minimum_version_id = 0,
54 .minimum_version_id_old = 0,
55 .fields = (VMStateField[]) {
56 VMSTATE_UINT8(pressed, gamepad_button),
57 VMSTATE_END_OF_LIST()
58 }
59};
60
61static const VMStateDescription vmstate_stellaris_gamepad = {
62 .name = "stellaris_gamepad",
63 .version_id = 1,
64 .minimum_version_id = 1,
65 .minimum_version_id_old = 1,
66 .fields = (VMStateField[]) {
67 VMSTATE_INT32(extension, gamepad_state),
68 VMSTATE_STRUCT_VARRAY_INT32(buttons, gamepad_state, num_buttons, 0,
69 vmstate_stellaris_button, gamepad_button),
70 VMSTATE_END_OF_LIST()
71 }
72};
73
74
75void stellaris_gamepad_init(int n, qemu_irq *irq, const int *keycode)
76{
77 gamepad_state *s;
78 int i;
79
80 s = (gamepad_state *)g_malloc0(sizeof (gamepad_state));
81 s->buttons = (gamepad_button *)g_malloc0(n * sizeof (gamepad_button));
82 for (i = 0; i < n; i++) {
83 s->buttons[i].irq = irq[i];
84 s->buttons[i].keycode = keycode[i];
85 }
86 s->num_buttons = n;
87 qemu_add_kbd_event_handler(stellaris_gamepad_put_key, s);
88 vmstate_register(NULL, -1, &vmstate_stellaris_gamepad, s);
89}
90