blob: 0609e80868032cbc52849eab4f177234467bf4cf [file] [log] [blame]
pbrookcf0dbb22007-11-18 14:36:08 +00001/*
2 * Gamepad style buttons connected to IRQ/GPIO lines
3 *
4 * Copyright (c) 2007 CodeSourcery.
5 * Written by Paul Brook
6 *
Matthew Fernandez8e31bf32011-06-26 12:21:35 +10007 * This code is licensed under the GPL.
pbrookcf0dbb22007-11-18 14:36:08 +00008 */
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +01009#include "hw/hw.h"
Peter Maydellbd2be152013-04-09 15:26:55 +010010#include "hw/devices.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010011#include "ui/console.h"
pbrookcf0dbb22007-11-18 14:36:08 +000012
13typedef struct {
14 qemu_irq irq;
15 int keycode;
Juan Quintela4483c7a2010-12-02 02:36:38 +010016 uint8_t pressed;
pbrookcf0dbb22007-11-18 14:36:08 +000017} 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
Juan Quintela4483c7a2010-12-02 02:36:38 +010050static const VMStateDescription vmstate_stellaris_button = {
51 .name = "stellaris_button",
52 .version_id = 0,
53 .minimum_version_id = 0,
Juan Quintela8f1e8842014-05-13 16:09:35 +010054 .fields = (VMStateField[]) {
Juan Quintela4483c7a2010-12-02 02:36:38 +010055 VMSTATE_UINT8(pressed, gamepad_button),
56 VMSTATE_END_OF_LIST()
57 }
58};
pbrook23e39292008-07-02 16:48:32 +000059
Juan Quintela4483c7a2010-12-02 02:36:38 +010060static const VMStateDescription vmstate_stellaris_gamepad = {
61 .name = "stellaris_gamepad",
62 .version_id = 1,
63 .minimum_version_id = 1,
Juan Quintela8f1e8842014-05-13 16:09:35 +010064 .fields = (VMStateField[]) {
Juan Quintela4483c7a2010-12-02 02:36:38 +010065 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};
pbrook23e39292008-07-02 16:48:32 +000071
pbrookcf0dbb22007-11-18 14:36:08 +000072/* Returns an array 5 ouput slots. */
73void stellaris_gamepad_init(int n, qemu_irq *irq, const int *keycode)
74{
75 gamepad_state *s;
76 int i;
77
Anthony Liguori7267c092011-08-20 22:09:37 -050078 s = (gamepad_state *)g_malloc0(sizeof (gamepad_state));
79 s->buttons = (gamepad_button *)g_malloc0(n * sizeof (gamepad_button));
pbrookcf0dbb22007-11-18 14:36:08 +000080 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);
Juan Quintela4483c7a2010-12-02 02:36:38 +010086 vmstate_register(NULL, -1, &vmstate_stellaris_gamepad, s);
pbrookcf0dbb22007-11-18 14:36:08 +000087}