]> git.proxmox.com Git - mirror_qemu.git/blame - hw/input/stellaris_input.c
Merge remote-tracking branch 'remotes/kevin/tags/for-upstream' into staging
[mirror_qemu.git] / hw / input / stellaris_input.c
CommitLineData
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 */
64552b6b 9
8ef94f0b 10#include "qemu/osdep.h"
98fa3327 11#include "hw/input/gamepad.h"
64552b6b 12#include "hw/irq.h"
d6454270 13#include "migration/vmstate.h"
28ecbaee 14#include "ui/console.h"
cf0dbb21
PB
15
16typedef struct {
17 qemu_irq irq;
18 int keycode;
4483c7ac 19 uint8_t pressed;
cf0dbb21
PB
20} gamepad_button;
21
22typedef struct {
23 gamepad_button *buttons;
24 int num_buttons;
25 int extension;
26} gamepad_state;
27
28static void stellaris_gamepad_put_key(void * opaque, int keycode)
29{
30 gamepad_state *s = (gamepad_state *)opaque;
31 int i;
32 int down;
33
34 if (keycode == 0xe0 && !s->extension) {
35 s->extension = 0x80;
36 return;
37 }
38
39 down = (keycode & 0x80) == 0;
40 keycode = (keycode & 0x7f) | s->extension;
41
42 for (i = 0; i < s->num_buttons; i++) {
43 if (s->buttons[i].keycode == keycode
44 && s->buttons[i].pressed != down) {
45 s->buttons[i].pressed = down;
46 qemu_set_irq(s->buttons[i].irq, down);
47 }
48 }
49
50 s->extension = 0;
51}
52
4483c7ac
JQ
53static const VMStateDescription vmstate_stellaris_button = {
54 .name = "stellaris_button",
55 .version_id = 0,
56 .minimum_version_id = 0,
8f1e884b 57 .fields = (VMStateField[]) {
4483c7ac
JQ
58 VMSTATE_UINT8(pressed, gamepad_button),
59 VMSTATE_END_OF_LIST()
60 }
61};
23e39294 62
4483c7ac
JQ
63static const VMStateDescription vmstate_stellaris_gamepad = {
64 .name = "stellaris_gamepad",
372e458e
PM
65 .version_id = 2,
66 .minimum_version_id = 2,
8f1e884b 67 .fields = (VMStateField[]) {
4483c7ac 68 VMSTATE_INT32(extension, gamepad_state),
372e458e
PM
69 VMSTATE_STRUCT_VARRAY_POINTER_INT32(buttons, gamepad_state,
70 num_buttons,
71 vmstate_stellaris_button,
72 gamepad_button),
4483c7ac
JQ
73 VMSTATE_END_OF_LIST()
74 }
75};
23e39294 76
67cc32eb 77/* Returns an array of 5 output slots. */
cf0dbb21
PB
78void stellaris_gamepad_init(int n, qemu_irq *irq, const int *keycode)
79{
80 gamepad_state *s;
81 int i;
82
b45c03f5
MA
83 s = g_new0(gamepad_state, 1);
84 s->buttons = g_new0(gamepad_button, n);
cf0dbb21
PB
85 for (i = 0; i < n; i++) {
86 s->buttons[i].irq = irq[i];
87 s->buttons[i].keycode = keycode[i];
88 }
89 s->num_buttons = n;
90 qemu_add_kbd_event_handler(stellaris_gamepad_put_key, s);
4483c7ac 91 vmstate_register(NULL, -1, &vmstate_stellaris_gamepad, s);
cf0dbb21 92}