2
0

stellaris_input.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #include "hw.h"
  10. #include "devices.h"
  11. #include "ui/console.h"
  12. typedef struct {
  13. qemu_irq irq;
  14. int keycode;
  15. uint8_t pressed;
  16. } gamepad_button;
  17. typedef struct {
  18. gamepad_button *buttons;
  19. int num_buttons;
  20. int extension;
  21. } gamepad_state;
  22. static void stellaris_gamepad_put_key(void * opaque, int keycode)
  23. {
  24. gamepad_state *s = (gamepad_state *)opaque;
  25. int i;
  26. int down;
  27. if (keycode == 0xe0 && !s->extension) {
  28. s->extension = 0x80;
  29. return;
  30. }
  31. down = (keycode & 0x80) == 0;
  32. keycode = (keycode & 0x7f) | s->extension;
  33. for (i = 0; i < s->num_buttons; i++) {
  34. if (s->buttons[i].keycode == keycode
  35. && s->buttons[i].pressed != down) {
  36. s->buttons[i].pressed = down;
  37. qemu_set_irq(s->buttons[i].irq, down);
  38. }
  39. }
  40. s->extension = 0;
  41. }
  42. static const VMStateDescription vmstate_stellaris_button = {
  43. .name = "stellaris_button",
  44. .version_id = 0,
  45. .minimum_version_id = 0,
  46. .minimum_version_id_old = 0,
  47. .fields = (VMStateField[]) {
  48. VMSTATE_UINT8(pressed, gamepad_button),
  49. VMSTATE_END_OF_LIST()
  50. }
  51. };
  52. static const VMStateDescription vmstate_stellaris_gamepad = {
  53. .name = "stellaris_gamepad",
  54. .version_id = 1,
  55. .minimum_version_id = 1,
  56. .minimum_version_id_old = 1,
  57. .fields = (VMStateField[]) {
  58. VMSTATE_INT32(extension, gamepad_state),
  59. VMSTATE_STRUCT_VARRAY_INT32(buttons, gamepad_state, num_buttons, 0,
  60. vmstate_stellaris_button, gamepad_button),
  61. VMSTATE_END_OF_LIST()
  62. }
  63. };
  64. /* Returns an array 5 ouput slots. */
  65. void stellaris_gamepad_init(int n, qemu_irq *irq, const int *keycode)
  66. {
  67. gamepad_state *s;
  68. int i;
  69. s = (gamepad_state *)g_malloc0(sizeof (gamepad_state));
  70. s->buttons = (gamepad_button *)g_malloc0(n * sizeof (gamepad_button));
  71. for (i = 0; i < n; i++) {
  72. s->buttons[i].irq = irq[i];
  73. s->buttons[i].keycode = keycode[i];
  74. }
  75. s->num_buttons = n;
  76. qemu_add_kbd_event_handler(stellaris_gamepad_put_key, s);
  77. vmstate_register(NULL, -1, &vmstate_stellaris_gamepad, s);
  78. }