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
|
|
|
*/
|
2019-08-12 08:23:42 +03:00
|
|
|
|
2016-01-26 21:17:05 +03:00
|
|
|
#include "qemu/osdep.h"
|
2023-10-30 14:48:01 +03:00
|
|
|
#include "qapi/error.h"
|
2023-10-30 14:47:57 +03:00
|
|
|
#include "hw/input/stellaris_gamepad.h"
|
2019-08-12 08:23:42 +03:00
|
|
|
#include "hw/irq.h"
|
2023-10-30 14:48:01 +03:00
|
|
|
#include "hw/qdev-properties.h"
|
2019-08-12 08:23:45 +03:00
|
|
|
#include "migration/vmstate.h"
|
2012-11-28 15:06:30 +04:00
|
|
|
#include "ui/console.h"
|
2007-11-18 17:36:08 +03:00
|
|
|
|
2023-10-30 14:48:02 +03:00
|
|
|
static void stellaris_gamepad_event(DeviceState *dev, QemuConsole *src,
|
|
|
|
InputEvent *evt)
|
2007-11-18 17:36:08 +03:00
|
|
|
{
|
2023-10-30 14:48:02 +03:00
|
|
|
StellarisGamepad *s = STELLARIS_GAMEPAD(dev);
|
|
|
|
InputKeyEvent *key = evt->u.key.data;
|
|
|
|
int qcode = qemu_input_key_value_to_qcode(key->key);
|
2007-11-18 17:36:08 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < s->num_buttons; i++) {
|
2023-10-30 14:48:02 +03:00
|
|
|
if (s->keycodes[i] == qcode && s->pressed[i] != key->down) {
|
|
|
|
s->pressed[i] = key->down;
|
|
|
|
qemu_set_irq(s->irqs[i], key->down);
|
2007-11-18 17:36:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-02 04:36:38 +03:00
|
|
|
static const VMStateDescription vmstate_stellaris_gamepad = {
|
|
|
|
.name = "stellaris_gamepad",
|
2023-10-30 14:48:02 +03:00
|
|
|
.version_id = 4,
|
|
|
|
.minimum_version_id = 4,
|
2023-12-21 06:16:14 +03:00
|
|
|
.fields = (const VMStateField[]) {
|
2023-10-30 14:48:00 +03:00
|
|
|
VMSTATE_VARRAY_UINT32(pressed, StellarisGamepad, num_buttons,
|
|
|
|
0, vmstate_info_uint8, uint8_t),
|
2010-12-02 04:36:38 +03:00
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
2008-07-02 20:48:32 +04:00
|
|
|
|
2023-10-30 14:48:02 +03:00
|
|
|
static const QemuInputHandler stellaris_gamepad_handler = {
|
|
|
|
.name = "Stellaris Gamepad",
|
|
|
|
.mask = INPUT_EVENT_MASK_KEY,
|
|
|
|
.event = stellaris_gamepad_event,
|
|
|
|
};
|
|
|
|
|
2023-10-30 14:48:01 +03:00
|
|
|
static void stellaris_gamepad_realize(DeviceState *dev, Error **errp)
|
2007-11-18 17:36:08 +03:00
|
|
|
{
|
2023-10-30 14:48:01 +03:00
|
|
|
StellarisGamepad *s = STELLARIS_GAMEPAD(dev);
|
2007-11-18 17:36:08 +03:00
|
|
|
|
2023-10-30 14:48:01 +03:00
|
|
|
if (s->num_buttons == 0) {
|
|
|
|
error_setg(errp, "keycodes property array must be set");
|
|
|
|
return;
|
2007-11-18 17:36:08 +03:00
|
|
|
}
|
2023-10-30 14:48:01 +03:00
|
|
|
|
|
|
|
s->irqs = g_new0(qemu_irq, s->num_buttons);
|
|
|
|
s->pressed = g_new0(uint8_t, s->num_buttons);
|
|
|
|
qdev_init_gpio_out(dev, s->irqs, s->num_buttons);
|
2023-10-30 14:48:02 +03:00
|
|
|
qemu_input_handler_register(dev, &stellaris_gamepad_handler);
|
2007-11-18 17:36:08 +03:00
|
|
|
}
|
2023-10-30 14:48:01 +03:00
|
|
|
|
2023-11-21 20:40:51 +03:00
|
|
|
static void stellaris_gamepad_finalize(Object *obj)
|
|
|
|
{
|
|
|
|
StellarisGamepad *s = STELLARIS_GAMEPAD(obj);
|
|
|
|
|
|
|
|
g_free(s->keycodes);
|
|
|
|
}
|
|
|
|
|
2023-10-30 14:48:01 +03:00
|
|
|
static void stellaris_gamepad_reset_enter(Object *obj, ResetType type)
|
|
|
|
{
|
|
|
|
StellarisGamepad *s = STELLARIS_GAMEPAD(obj);
|
|
|
|
|
|
|
|
memset(s->pressed, 0, s->num_buttons * sizeof(uint8_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
static Property stellaris_gamepad_properties[] = {
|
|
|
|
DEFINE_PROP_ARRAY("keycodes", StellarisGamepad, num_buttons,
|
|
|
|
keycodes, qdev_prop_uint32, uint32_t),
|
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
|
|
|
static void stellaris_gamepad_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
|
|
|
ResettableClass *rc = RESETTABLE_CLASS(klass);
|
|
|
|
|
|
|
|
rc->phases.enter = stellaris_gamepad_reset_enter;
|
|
|
|
dc->realize = stellaris_gamepad_realize;
|
|
|
|
dc->vmsd = &vmstate_stellaris_gamepad;
|
|
|
|
device_class_set_props(dc, stellaris_gamepad_properties);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo stellaris_gamepad_info[] = {
|
|
|
|
{
|
|
|
|
.name = TYPE_STELLARIS_GAMEPAD,
|
|
|
|
.parent = TYPE_SYS_BUS_DEVICE,
|
|
|
|
.instance_size = sizeof(StellarisGamepad),
|
2023-11-21 20:40:51 +03:00
|
|
|
.instance_finalize = stellaris_gamepad_finalize,
|
2023-10-30 14:48:01 +03:00
|
|
|
.class_init = stellaris_gamepad_class_init,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
DEFINE_TYPES(stellaris_gamepad_info);
|