root / hw / stellaris_input.c @ 2c44375d
History | View | Annotate | Download (1.5 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 | cf0dbb21 | pbrook | /* Returns an array 5 ouput slots. */
|
51 | cf0dbb21 | pbrook | void stellaris_gamepad_init(int n, qemu_irq *irq, const int *keycode) |
52 | cf0dbb21 | pbrook | { |
53 | cf0dbb21 | pbrook | gamepad_state *s; |
54 | cf0dbb21 | pbrook | int i;
|
55 | cf0dbb21 | pbrook | |
56 | cf0dbb21 | pbrook | s = (gamepad_state *)qemu_mallocz(sizeof (gamepad_state));
|
57 | cf0dbb21 | pbrook | s->buttons = (gamepad_button *)qemu_mallocz(n * sizeof (gamepad_button));
|
58 | cf0dbb21 | pbrook | for (i = 0; i < n; i++) { |
59 | cf0dbb21 | pbrook | s->buttons[i].irq = irq[i]; |
60 | cf0dbb21 | pbrook | s->buttons[i].keycode = keycode[i]; |
61 | cf0dbb21 | pbrook | } |
62 | cf0dbb21 | pbrook | s->num_buttons = n; |
63 | cf0dbb21 | pbrook | qemu_add_kbd_event_handler(stellaris_gamepad_put_key, s); |
64 | cf0dbb21 | pbrook | } |
65 | cf0dbb21 | pbrook |