Merge branch 'qemu-cvs'
[qemu-kvm/fedora.git] / hw / stellaris_input.c
blob461868b2eefad2e49ec8f349925b964b74537eec
1 /*
2 * Gamepad style buttons connected to IRQ/GPIO lines
4 * Copyright (c) 2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licenced under the GPL.
8 */
9 #include "hw.h"
10 #include "devices.h"
11 #include "console.h"
13 typedef struct {
14 qemu_irq irq;
15 int keycode;
16 int pressed;
17 } gamepad_button;
19 typedef struct {
20 gamepad_button *buttons;
21 int num_buttons;
22 int extension;
23 } gamepad_state;
25 static void stellaris_gamepad_put_key(void * opaque, int keycode)
27 gamepad_state *s = (gamepad_state *)opaque;
28 int i;
29 int down;
31 if (keycode == 0xe0 && !s->extension) {
32 s->extension = 0x80;
33 return;
36 down = (keycode & 0x80) == 0;
37 keycode = (keycode & 0x7f) | s->extension;
39 for (i = 0; i < s->num_buttons; i++) {
40 if (s->buttons[i].keycode == keycode
41 && s->buttons[i].pressed != down) {
42 s->buttons[i].pressed = down;
43 qemu_set_irq(s->buttons[i].irq, down);
47 s->extension = 0;
50 /* Returns an array 5 ouput slots. */
51 void stellaris_gamepad_init(int n, qemu_irq *irq, const int *keycode)
53 gamepad_state *s;
54 int i;
56 s = (gamepad_state *)qemu_mallocz(sizeof (gamepad_state));
57 s->buttons = (gamepad_button *)qemu_mallocz(n * sizeof (gamepad_button));
58 for (i = 0; i < n; i++) {
59 s->buttons[i].irq = irq[i];
60 s->buttons[i].keycode = keycode[i];
62 s->num_buttons = n;
63 qemu_add_kbd_event_handler(stellaris_gamepad_put_key, s);