2007-11-18 14:36:08 +00:00
|
|
|
/*
|
|
|
|
* Gamepad style buttons connected to IRQ/GPIO lines
|
|
|
|
*
|
|
|
|
* Copyright (c) 2007 CodeSourcery.
|
|
|
|
* Written by Paul Brook
|
|
|
|
*
|
2011-06-26 02:21:35 +00:00
|
|
|
* This code is licensed under the GPL.
|
2007-11-18 14:36:08 +00:00
|
|
|
*/
|
2019-08-12 05:23:42 +00:00
|
|
|
|
2016-01-26 18:17:05 +00:00
|
|
|
#include "qemu/osdep.h"
|
2023-10-30 11:47:57 +00:00
|
|
|
#include "hw/input/stellaris_gamepad.h"
|
2019-08-12 05:23:42 +00:00
|
|
|
#include "hw/irq.h"
|
2019-08-12 05:23:45 +00:00
|
|
|
#include "migration/vmstate.h"
|
2012-11-28 11:06:30 +00:00
|
|
|
#include "ui/console.h"
|
2007-11-18 14:36:08 +00:00
|
|
|
|
|
|
|
typedef struct {
|
2023-10-30 11:48:00 +00:00
|
|
|
uint32_t num_buttons;
|
2007-11-18 14:36:08 +00:00
|
|
|
int extension;
|
2023-10-30 11:48:00 +00:00
|
|
|
qemu_irq *irqs;
|
|
|
|
uint32_t *keycodes;
|
|
|
|
uint8_t *pressed;
|
2023-10-30 11:47:58 +00:00
|
|
|
} StellarisGamepad;
|
2007-11-18 14:36:08 +00:00
|
|
|
|
|
|
|
static void stellaris_gamepad_put_key(void * opaque, int keycode)
|
|
|
|
{
|
2023-10-30 11:47:58 +00:00
|
|
|
StellarisGamepad *s = (StellarisGamepad *)opaque;
|
2007-11-18 14:36:08 +00:00
|
|
|
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++) {
|
2023-10-30 11:48:00 +00:00
|
|
|
if (s->keycodes[i] == keycode && s->pressed[i] != down) {
|
|
|
|
s->pressed[i] = down;
|
|
|
|
qemu_set_irq(s->irqs[i], down);
|
2007-11-18 14:36:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s->extension = 0;
|
|
|
|
}
|
|
|
|
|
2010-12-02 01:36:38 +00:00
|
|
|
static const VMStateDescription vmstate_stellaris_gamepad = {
|
|
|
|
.name = "stellaris_gamepad",
|
2023-10-30 11:48:00 +00:00
|
|
|
.version_id = 3,
|
|
|
|
.minimum_version_id = 3,
|
2014-05-13 15:09:35 +00:00
|
|
|
.fields = (VMStateField[]) {
|
2023-10-30 11:47:58 +00:00
|
|
|
VMSTATE_INT32(extension, StellarisGamepad),
|
2023-10-30 11:48:00 +00:00
|
|
|
VMSTATE_VARRAY_UINT32(pressed, StellarisGamepad, num_buttons,
|
|
|
|
0, vmstate_info_uint8, uint8_t),
|
2010-12-02 01:36:38 +00:00
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
2008-07-02 16:48:32 +00:00
|
|
|
|
2015-09-08 21:45:14 +00:00
|
|
|
/* Returns an array of 5 output slots. */
|
2007-11-18 14:36:08 +00:00
|
|
|
void stellaris_gamepad_init(int n, qemu_irq *irq, const int *keycode)
|
|
|
|
{
|
2023-10-30 11:47:58 +00:00
|
|
|
StellarisGamepad *s;
|
2007-11-18 14:36:08 +00:00
|
|
|
int i;
|
|
|
|
|
2023-10-30 11:47:58 +00:00
|
|
|
s = g_new0(StellarisGamepad, 1);
|
2023-10-30 11:48:00 +00:00
|
|
|
s->irqs = g_new0(qemu_irq, n);
|
|
|
|
s->keycodes = g_new0(uint32_t, n);
|
|
|
|
s->pressed = g_new0(uint8_t, n);
|
2007-11-18 14:36:08 +00:00
|
|
|
for (i = 0; i < n; i++) {
|
2023-10-30 11:48:00 +00:00
|
|
|
s->irqs[i] = irq[i];
|
|
|
|
s->keycodes[i] = keycode[i];
|
2007-11-18 14:36:08 +00:00
|
|
|
}
|
|
|
|
s->num_buttons = n;
|
|
|
|
qemu_add_kbd_event_handler(stellaris_gamepad_put_key, s);
|
2019-10-16 02:29:30 +00:00
|
|
|
vmstate_register(NULL, VMSTATE_INSTANCE_ID_ANY,
|
|
|
|
&vmstate_stellaris_gamepad, s);
|
2007-11-18 14:36:08 +00:00
|
|
|
}
|