Statistics
| Branch: | Revision:

root / hw / stellaris_input.c @ df0db221

History | View | Annotate | Download (2.2 kB)

1 cf0dbb21 pbrook
/*
2 cf0dbb21 pbrook
 * Gamepad style buttons connected to IRQ/GPIO lines
3 cf0dbb21 pbrook
 *
4 cf0dbb21 pbrook
 * Copyright (c) 2007 CodeSourcery.
5 cf0dbb21 pbrook
 * Written by Paul Brook
6 cf0dbb21 pbrook
 *
7 cf0dbb21 pbrook
 * This code is licenced under the GPL.
8 cf0dbb21 pbrook
 */
9 cf0dbb21 pbrook
#include "hw.h"
10 cf0dbb21 pbrook
#include "devices.h"
11 cf0dbb21 pbrook
#include "console.h"
12 cf0dbb21 pbrook
13 cf0dbb21 pbrook
typedef struct {
14 cf0dbb21 pbrook
    qemu_irq irq;
15 cf0dbb21 pbrook
    int keycode;
16 cf0dbb21 pbrook
    int pressed;
17 cf0dbb21 pbrook
} gamepad_button;
18 cf0dbb21 pbrook
19 cf0dbb21 pbrook
typedef struct {
20 cf0dbb21 pbrook
    gamepad_button *buttons;
21 cf0dbb21 pbrook
    int num_buttons;
22 cf0dbb21 pbrook
    int extension;
23 cf0dbb21 pbrook
} gamepad_state;
24 cf0dbb21 pbrook
25 cf0dbb21 pbrook
static void stellaris_gamepad_put_key(void * opaque, int keycode)
26 cf0dbb21 pbrook
{
27 cf0dbb21 pbrook
    gamepad_state *s = (gamepad_state *)opaque;
28 cf0dbb21 pbrook
    int i;
29 cf0dbb21 pbrook
    int down;
30 cf0dbb21 pbrook
31 cf0dbb21 pbrook
    if (keycode == 0xe0 && !s->extension) {
32 cf0dbb21 pbrook
        s->extension = 0x80;
33 cf0dbb21 pbrook
        return;
34 cf0dbb21 pbrook
    }
35 cf0dbb21 pbrook
36 cf0dbb21 pbrook
    down = (keycode & 0x80) == 0;
37 cf0dbb21 pbrook
    keycode = (keycode & 0x7f) | s->extension;
38 cf0dbb21 pbrook
39 cf0dbb21 pbrook
    for (i = 0; i < s->num_buttons; i++) {
40 cf0dbb21 pbrook
        if (s->buttons[i].keycode == keycode
41 cf0dbb21 pbrook
                && s->buttons[i].pressed != down) {
42 cf0dbb21 pbrook
            s->buttons[i].pressed = down;
43 cf0dbb21 pbrook
            qemu_set_irq(s->buttons[i].irq, down);
44 cf0dbb21 pbrook
        }
45 cf0dbb21 pbrook
    }
46 cf0dbb21 pbrook
47 cf0dbb21 pbrook
    s->extension = 0;
48 cf0dbb21 pbrook
}
49 cf0dbb21 pbrook
50 23e39294 pbrook
static void stellaris_gamepad_save(QEMUFile *f, void *opaque)
51 23e39294 pbrook
{
52 23e39294 pbrook
    gamepad_state *s = (gamepad_state *)opaque;
53 23e39294 pbrook
    int i;
54 23e39294 pbrook
55 23e39294 pbrook
    qemu_put_be32(f, s->extension);
56 23e39294 pbrook
    for (i = 0; i < s->num_buttons; i++)
57 23e39294 pbrook
        qemu_put_byte(f, s->buttons[i].pressed);
58 23e39294 pbrook
}
59 23e39294 pbrook
60 23e39294 pbrook
static int stellaris_gamepad_load(QEMUFile *f, void *opaque, int version_id)
61 23e39294 pbrook
{
62 23e39294 pbrook
    gamepad_state *s = (gamepad_state *)opaque;
63 23e39294 pbrook
    int i;
64 23e39294 pbrook
65 23e39294 pbrook
    if (version_id != 1)
66 23e39294 pbrook
        return -EINVAL;
67 23e39294 pbrook
68 23e39294 pbrook
    s->extension = qemu_get_be32(f);
69 23e39294 pbrook
    for (i = 0; i < s->num_buttons; i++)
70 23e39294 pbrook
        s->buttons[i].pressed = qemu_get_byte(f);
71 23e39294 pbrook
72 23e39294 pbrook
    return 0;
73 23e39294 pbrook
}
74 23e39294 pbrook
75 cf0dbb21 pbrook
/* Returns an array 5 ouput slots.  */
76 cf0dbb21 pbrook
void stellaris_gamepad_init(int n, qemu_irq *irq, const int *keycode)
77 cf0dbb21 pbrook
{
78 cf0dbb21 pbrook
    gamepad_state *s;
79 cf0dbb21 pbrook
    int i;
80 cf0dbb21 pbrook
81 cf0dbb21 pbrook
    s = (gamepad_state *)qemu_mallocz(sizeof (gamepad_state));
82 cf0dbb21 pbrook
    s->buttons = (gamepad_button *)qemu_mallocz(n * sizeof (gamepad_button));
83 cf0dbb21 pbrook
    for (i = 0; i < n; i++) {
84 cf0dbb21 pbrook
        s->buttons[i].irq = irq[i];
85 cf0dbb21 pbrook
        s->buttons[i].keycode = keycode[i];
86 cf0dbb21 pbrook
    }
87 cf0dbb21 pbrook
    s->num_buttons = n;
88 cf0dbb21 pbrook
    qemu_add_kbd_event_handler(stellaris_gamepad_put_key, s);
89 0be71e32 Alex Williamson
    register_savevm(NULL, "stellaris_gamepad", -1, 1,
90 23e39294 pbrook
                    stellaris_gamepad_save, stellaris_gamepad_load, s);
91 cf0dbb21 pbrook
}