]>
Commit | Line | Data |
---|---|---|
cf0dbb21 PB |
1 | /* |
2 | * Gamepad style buttons connected to IRQ/GPIO lines | |
3 | * | |
4 | * Copyright (c) 2007 CodeSourcery. | |
5 | * Written by Paul Brook | |
6 | * | |
8e31bf38 | 7 | * This code is licensed under the GPL. |
cf0dbb21 | 8 | */ |
83c9f4ca | 9 | #include "hw/hw.h" |
bd2be150 | 10 | #include "hw/devices.h" |
28ecbaee | 11 | #include "ui/console.h" |
cf0dbb21 PB |
12 | |
13 | typedef struct { | |
14 | qemu_irq irq; | |
15 | int keycode; | |
4483c7ac | 16 | uint8_t pressed; |
cf0dbb21 PB |
17 | } gamepad_button; |
18 | ||
19 | typedef struct { | |
20 | gamepad_button *buttons; | |
21 | int num_buttons; | |
22 | int extension; | |
23 | } gamepad_state; | |
24 | ||
25 | static 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 | ||
4483c7ac JQ |
50 | static const VMStateDescription vmstate_stellaris_button = { |
51 | .name = "stellaris_button", | |
52 | .version_id = 0, | |
53 | .minimum_version_id = 0, | |
8f1e884b | 54 | .fields = (VMStateField[]) { |
4483c7ac JQ |
55 | VMSTATE_UINT8(pressed, gamepad_button), |
56 | VMSTATE_END_OF_LIST() | |
57 | } | |
58 | }; | |
23e39294 | 59 | |
4483c7ac JQ |
60 | static const VMStateDescription vmstate_stellaris_gamepad = { |
61 | .name = "stellaris_gamepad", | |
62 | .version_id = 1, | |
63 | .minimum_version_id = 1, | |
8f1e884b | 64 | .fields = (VMStateField[]) { |
4483c7ac JQ |
65 | VMSTATE_INT32(extension, gamepad_state), |
66 | VMSTATE_STRUCT_VARRAY_INT32(buttons, gamepad_state, num_buttons, 0, | |
67 | vmstate_stellaris_button, gamepad_button), | |
68 | VMSTATE_END_OF_LIST() | |
69 | } | |
70 | }; | |
23e39294 | 71 | |
67cc32eb | 72 | /* Returns an array of 5 output slots. */ |
cf0dbb21 PB |
73 | void stellaris_gamepad_init(int n, qemu_irq *irq, const int *keycode) |
74 | { | |
75 | gamepad_state *s; | |
76 | int i; | |
77 | ||
b45c03f5 MA |
78 | s = g_new0(gamepad_state, 1); |
79 | s->buttons = g_new0(gamepad_button, n); | |
cf0dbb21 PB |
80 | for (i = 0; i < n; i++) { |
81 | s->buttons[i].irq = irq[i]; | |
82 | s->buttons[i].keycode = keycode[i]; | |
83 | } | |
84 | s->num_buttons = n; | |
85 | qemu_add_kbd_event_handler(stellaris_gamepad_put_key, s); | |
4483c7ac | 86 | vmstate_register(NULL, -1, &vmstate_stellaris_gamepad, s); |
cf0dbb21 | 87 | } |