stellaris_input.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 licenced under the GPL.
  8. */
  9. #include "hw.h"
  10. #include "devices.h"
  11. #include "console.h"
  12. typedef struct {
  13. qemu_irq irq;
  14. int keycode;
  15. int 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 void stellaris_gamepad_save(QEMUFile *f, void *opaque)
  43. {
  44. gamepad_state *s = (gamepad_state *)opaque;
  45. int i;
  46. qemu_put_be32(f, s->extension);
  47. for (i = 0; i < s->num_buttons; i++)
  48. qemu_put_byte(f, s->buttons[i].pressed);
  49. }
  50. static int stellaris_gamepad_load(QEMUFile *f, void *opaque, int version_id)
  51. {
  52. gamepad_state *s = (gamepad_state *)opaque;
  53. int i;
  54. if (version_id != 1)
  55. return -EINVAL;
  56. s->extension = qemu_get_be32(f);
  57. for (i = 0; i < s->num_buttons; i++)
  58. s->buttons[i].pressed = qemu_get_byte(f);
  59. return 0;
  60. }
  61. /* Returns an array 5 ouput slots. */
  62. void stellaris_gamepad_init(int n, qemu_irq *irq, const int *keycode)
  63. {
  64. gamepad_state *s;
  65. int i;
  66. s = (gamepad_state *)qemu_mallocz(sizeof (gamepad_state));
  67. s->buttons = (gamepad_button *)qemu_mallocz(n * sizeof (gamepad_button));
  68. for (i = 0; i < n; i++) {
  69. s->buttons[i].irq = irq[i];
  70. s->buttons[i].keycode = keycode[i];
  71. }
  72. s->num_buttons = n;
  73. qemu_add_kbd_event_handler(stellaris_gamepad_put_key, s);
  74. register_savevm("stellaris_gamepad", -1, 1,
  75. stellaris_gamepad_save, stellaris_gamepad_load, s);
  76. }