2007-11-18 17:36:08 +03:00
|
|
|
/*
|
|
|
|
* Gamepad style buttons connected to IRQ/GPIO lines
|
|
|
|
*
|
|
|
|
* Copyright (c) 2007 CodeSourcery.
|
|
|
|
* Written by Paul Brook
|
|
|
|
*
|
2011-06-26 06:21:35 +04:00
|
|
|
* This code is licensed under the GPL.
|
2007-11-18 17:36:08 +03:00
|
|
|
*/
|
2013-02-04 18:40:22 +04:00
|
|
|
#include "hw/hw.h"
|
2013-04-09 18:26:55 +04:00
|
|
|
#include "hw/devices.h"
|
2012-11-28 15:06:30 +04:00
|
|
|
#include "ui/console.h"
|
2007-11-18 17:36:08 +03:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
qemu_irq irq;
|
|
|
|
int keycode;
|
2010-12-02 04:36:38 +03:00
|
|
|
uint8_t pressed;
|
2007-11-18 17:36:08 +03:00
|
|
|
} gamepad_button;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
gamepad_button *buttons;
|
|
|
|
int num_buttons;
|
|
|
|
int extension;
|
|
|
|
} gamepad_state;
|
|
|
|
|
|
|
|
static void stellaris_gamepad_put_key(void * opaque, int keycode)
|
|
|
|
{
|
|
|
|
gamepad_state *s = (gamepad_state *)opaque;
|
|
|
|
int i;
|
|
|
|
int down;
|
|
|
|
|
|
|
|
if (keycode == 0xe0 && !s->extension) {
|
|
|
|
s->extension = 0x80;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
down = (keycode & 0x80) == 0;
|
|
|
|
keycode = (keycode & 0x7f) | s->extension;
|
|
|
|
|
|
|
|
for (i = 0; i < s->num_buttons; i++) {
|
|
|
|
if (s->buttons[i].keycode == keycode
|
|
|
|
&& s->buttons[i].pressed != down) {
|
|
|
|
s->buttons[i].pressed = down;
|
|
|
|
qemu_set_irq(s->buttons[i].irq, down);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s->extension = 0;
|
|
|
|
}
|
|
|
|
|
2010-12-02 04:36:38 +03:00
|
|
|
static const VMStateDescription vmstate_stellaris_button = {
|
|
|
|
.name = "stellaris_button",
|
|
|
|
.version_id = 0,
|
|
|
|
.minimum_version_id = 0,
|
2014-05-13 19:09:35 +04:00
|
|
|
.fields = (VMStateField[]) {
|
2010-12-02 04:36:38 +03:00
|
|
|
VMSTATE_UINT8(pressed, gamepad_button),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
2008-07-02 20:48:32 +04:00
|
|
|
|
2010-12-02 04:36:38 +03:00
|
|
|
static const VMStateDescription vmstate_stellaris_gamepad = {
|
|
|
|
.name = "stellaris_gamepad",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
2014-05-13 19:09:35 +04:00
|
|
|
.fields = (VMStateField[]) {
|
2010-12-02 04:36:38 +03:00
|
|
|
VMSTATE_INT32(extension, gamepad_state),
|
|
|
|
VMSTATE_STRUCT_VARRAY_INT32(buttons, gamepad_state, num_buttons, 0,
|
|
|
|
vmstate_stellaris_button, gamepad_button),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
2008-07-02 20:48:32 +04:00
|
|
|
|
2007-11-18 17:36:08 +03:00
|
|
|
/* Returns an array 5 ouput slots. */
|
|
|
|
void stellaris_gamepad_init(int n, qemu_irq *irq, const int *keycode)
|
|
|
|
{
|
|
|
|
gamepad_state *s;
|
|
|
|
int i;
|
|
|
|
|
2011-08-21 07:09:37 +04:00
|
|
|
s = (gamepad_state *)g_malloc0(sizeof (gamepad_state));
|
|
|
|
s->buttons = (gamepad_button *)g_malloc0(n * sizeof (gamepad_button));
|
2007-11-18 17:36:08 +03:00
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
s->buttons[i].irq = irq[i];
|
|
|
|
s->buttons[i].keycode = keycode[i];
|
|
|
|
}
|
|
|
|
s->num_buttons = n;
|
|
|
|
qemu_add_kbd_event_handler(stellaris_gamepad_put_key, s);
|
2010-12-02 04:36:38 +03:00
|
|
|
vmstate_register(NULL, -1, &vmstate_stellaris_gamepad, s);
|
2007-11-18 17:36:08 +03:00
|
|
|
}
|