2 * Gamepad style buttons connected to IRQ/GPIO lines
4 * Copyright (c) 2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the GPL.
10 #include "qemu/osdep.h"
11 #include "hw/input/gamepad.h"
13 #include "migration/vmstate.h"
14 #include "ui/console.h"
23 gamepad_button
*buttons
;
28 static void stellaris_gamepad_put_key(void * opaque
, int keycode
)
30 gamepad_state
*s
= (gamepad_state
*)opaque
;
34 if (keycode
== 0xe0 && !s
->extension
) {
39 down
= (keycode
& 0x80) == 0;
40 keycode
= (keycode
& 0x7f) | s
->extension
;
42 for (i
= 0; i
< s
->num_buttons
; i
++) {
43 if (s
->buttons
[i
].keycode
== keycode
44 && s
->buttons
[i
].pressed
!= down
) {
45 s
->buttons
[i
].pressed
= down
;
46 qemu_set_irq(s
->buttons
[i
].irq
, down
);
53 static const VMStateDescription vmstate_stellaris_button
= {
54 .name
= "stellaris_button",
56 .minimum_version_id
= 0,
57 .fields
= (VMStateField
[]) {
58 VMSTATE_UINT8(pressed
, gamepad_button
),
63 static const VMStateDescription vmstate_stellaris_gamepad
= {
64 .name
= "stellaris_gamepad",
66 .minimum_version_id
= 2,
67 .fields
= (VMStateField
[]) {
68 VMSTATE_INT32(extension
, gamepad_state
),
69 VMSTATE_STRUCT_VARRAY_POINTER_INT32(buttons
, gamepad_state
,
71 vmstate_stellaris_button
,
77 /* Returns an array of 5 output slots. */
78 void stellaris_gamepad_init(int n
, qemu_irq
*irq
, const int *keycode
)
83 s
= g_new0(gamepad_state
, 1);
84 s
->buttons
= g_new0(gamepad_button
, n
);
85 for (i
= 0; i
< n
; i
++) {
86 s
->buttons
[i
].irq
= irq
[i
];
87 s
->buttons
[i
].keycode
= keycode
[i
];
90 qemu_add_kbd_event_handler(stellaris_gamepad_put_key
, s
);
91 vmstate_register(NULL
, VMSTATE_INSTANCE_ID_ANY
,
92 &vmstate_stellaris_gamepad
, s
);