]>
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 | */ |
8ef94f0b | 9 | #include "qemu/osdep.h" |
83c9f4ca | 10 | #include "hw/hw.h" |
bd2be150 | 11 | #include "hw/devices.h" |
28ecbaee | 12 | #include "ui/console.h" |
cf0dbb21 PB |
13 | |
14 | typedef struct { | |
15 | qemu_irq irq; | |
16 | int keycode; | |
4483c7ac | 17 | uint8_t pressed; |
cf0dbb21 PB |
18 | } gamepad_button; |
19 | ||
20 | typedef struct { | |
21 | gamepad_button *buttons; | |
22 | int num_buttons; | |
23 | int extension; | |
24 | } gamepad_state; | |
25 | ||
26 | static void stellaris_gamepad_put_key(void * opaque, int keycode) | |
27 | { | |
28 | gamepad_state *s = (gamepad_state *)opaque; | |
29 | int i; | |
30 | int down; | |
31 | ||
32 | if (keycode == 0xe0 && !s->extension) { | |
33 | s->extension = 0x80; | |
34 | return; | |
35 | } | |
36 | ||
37 | down = (keycode & 0x80) == 0; | |
38 | keycode = (keycode & 0x7f) | s->extension; | |
39 | ||
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); | |
45 | } | |
46 | } | |
47 | ||
48 | s->extension = 0; | |
49 | } | |
50 | ||
4483c7ac JQ |
51 | static const VMStateDescription vmstate_stellaris_button = { |
52 | .name = "stellaris_button", | |
53 | .version_id = 0, | |
54 | .minimum_version_id = 0, | |
8f1e884b | 55 | .fields = (VMStateField[]) { |
4483c7ac JQ |
56 | VMSTATE_UINT8(pressed, gamepad_button), |
57 | VMSTATE_END_OF_LIST() | |
58 | } | |
59 | }; | |
23e39294 | 60 | |
4483c7ac JQ |
61 | static const VMStateDescription vmstate_stellaris_gamepad = { |
62 | .name = "stellaris_gamepad", | |
63 | .version_id = 1, | |
64 | .minimum_version_id = 1, | |
8f1e884b | 65 | .fields = (VMStateField[]) { |
4483c7ac JQ |
66 | VMSTATE_INT32(extension, gamepad_state), |
67 | VMSTATE_STRUCT_VARRAY_INT32(buttons, gamepad_state, num_buttons, 0, | |
68 | vmstate_stellaris_button, gamepad_button), | |
69 | VMSTATE_END_OF_LIST() | |
70 | } | |
71 | }; | |
23e39294 | 72 | |
67cc32eb | 73 | /* Returns an array of 5 output slots. */ |
cf0dbb21 PB |
74 | void stellaris_gamepad_init(int n, qemu_irq *irq, const int *keycode) |
75 | { | |
76 | gamepad_state *s; | |
77 | int i; | |
78 | ||
b45c03f5 MA |
79 | s = g_new0(gamepad_state, 1); |
80 | s->buttons = g_new0(gamepad_button, n); | |
cf0dbb21 PB |
81 | for (i = 0; i < n; i++) { |
82 | s->buttons[i].irq = irq[i]; | |
83 | s->buttons[i].keycode = keycode[i]; | |
84 | } | |
85 | s->num_buttons = n; | |
86 | qemu_add_kbd_event_handler(stellaris_gamepad_put_key, s); | |
4483c7ac | 87 | vmstate_register(NULL, -1, &vmstate_stellaris_gamepad, s); |
cf0dbb21 | 88 | } |