master
c 107 lines 3.13 KB
Raw
1 /*
2 * Gamepad style buttons connected to IRQ/GPIO lines
3 *
4 * Copyright (c) 2007 CodeSourcery.
5 * Written by Paul Brook
6 *
7 * This code is licensed under the GPL.
8 */
9
10 #include "qemu/osdep.h"
11 #include "qapi/error.h"
12 #include "hw/input/stellaris_gamepad.h"
13 #include "hw/core/irq.h"
14 #include "hw/core/qdev-properties.h"
15 #include "migration/vmstate.h"
16 #include "ui/console.h"
17
18 static void stellaris_gamepad_event(DeviceState *dev, QemuConsole *src,
19 QemuInputEvent *evt)
20 {
21 StellarisGamepad *s = STELLARIS_GAMEPAD(dev);
22 int qcode = qemu_input_linux_to_qcode(evt->key.key);
23 int i;
24
25 for (i = 0; i < s->num_buttons; i++) {
26 if (s->keycodes[i] == qcode && s->pressed[i] != evt->key.down) {
27 s->pressed[i] = evt->key.down;
28 qemu_set_irq(s->irqs[i], evt->key.down);
29 }
30 }
31 }
32
33 static const VMStateDescription vmstate_stellaris_gamepad = {
34 .name = "stellaris_gamepad",
35 .version_id = 4,
36 .minimum_version_id = 4,
37 .fields = (const VMStateField[]) {
38 VMSTATE_VARRAY_UINT32(pressed, StellarisGamepad, num_buttons,
39 0, vmstate_info_uint8, uint8_t),
40 VMSTATE_END_OF_LIST()
41 }
42 };
43
44 static const QemuInputHandler stellaris_gamepad_handler = {
45 .name = "Stellaris Gamepad",
46 .mask = INPUT_EVENT_MASK_KEY,
47 .event = stellaris_gamepad_event,
48 };
49
50 static void stellaris_gamepad_realize(DeviceState *dev, Error **errp)
51 {
52 StellarisGamepad *s = STELLARIS_GAMEPAD(dev);
53
54 if (s->num_buttons == 0) {
55 error_setg(errp, "keycodes property array must be set");
56 return;
57 }
58
59 s->irqs = g_new0(qemu_irq, s->num_buttons);
60 s->pressed = g_new0(uint8_t, s->num_buttons);
61 qdev_init_gpio_out(dev, s->irqs, s->num_buttons);
62 s->hs = qemu_input_handler_register(dev, &stellaris_gamepad_handler);
63 }
64
65 static void stellaris_gamepad_unrealize(DeviceState *dev)
66 {
67 StellarisGamepad *s = STELLARIS_GAMEPAD(dev);
68
69 g_clear_pointer(&s->irqs, g_free);
70 g_clear_pointer(&s->pressed, g_free);
71 g_clear_pointer(&s->hs, qemu_input_handler_unregister);
72 }
73
74 static void stellaris_gamepad_reset_enter(Object *obj, ResetType type)
75 {
76 StellarisGamepad *s = STELLARIS_GAMEPAD(obj);
77
78 memset(s->pressed, 0, s->num_buttons * sizeof(uint8_t));
79 }
80
81 static const Property stellaris_gamepad_properties[] = {
82 DEFINE_PROP_ARRAY("keycodes", StellarisGamepad, num_buttons,
83 keycodes, qdev_prop_uint32, uint32_t),
84 };
85
86 static void stellaris_gamepad_class_init(ObjectClass *klass, const void *data)
87 {
88 DeviceClass *dc = DEVICE_CLASS(klass);
89 ResettableClass *rc = RESETTABLE_CLASS(klass);
90
91 rc->phases.enter = stellaris_gamepad_reset_enter;
92 dc->realize = stellaris_gamepad_realize;
93 dc->unrealize = stellaris_gamepad_unrealize;
94 dc->vmsd = &vmstate_stellaris_gamepad;
95 device_class_set_props(dc, stellaris_gamepad_properties);
96 }
97
98 static const TypeInfo stellaris_gamepad_info[] = {
99 {
100 .name = TYPE_STELLARIS_GAMEPAD,
101 .parent = TYPE_SYS_BUS_DEVICE,
102 .instance_size = sizeof(StellarisGamepad),
103 .class_init = stellaris_gamepad_class_init,
104 },
105 };
106
107 DEFINE_TYPES(stellaris_gamepad_info);