evq.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* MegaZeux
  2. *
  3. * Copyright (C) 2004 Gilead Kutnick <exophase@adelphia.net>
  4. * Copyright (C) 2009 Kevin Vance <kvance@kvance.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /* Simple event queue for NDS input events */
  21. #include "../../src/compat.h"
  22. #include "evq.h"
  23. static NDSEvent *evlist_front = NULL;
  24. static NDSEvent *evlist_back = NULL;
  25. bool nds_event_poll(NDSEvent *dest)
  26. {
  27. NDSEvent *curr_event;
  28. bool retval = false;
  29. // Mask new input events while modifying the queue.
  30. irqDisable(IRQ_TIMER2);
  31. curr_event = evlist_front;
  32. if(curr_event)
  33. {
  34. // Copy the event to the destination.
  35. memcpy(dest, curr_event, sizeof(NDSEvent));
  36. dest->_next = NULL;
  37. // Advance to the next event.
  38. if(evlist_front != evlist_back)
  39. {
  40. evlist_front = evlist_front->_next;
  41. }
  42. else
  43. {
  44. evlist_front = NULL;
  45. evlist_back = NULL;
  46. }
  47. // Dispose of the old event.
  48. free(curr_event);
  49. retval = true;
  50. }
  51. // Allow new events again.
  52. irqEnable(IRQ_TIMER2);
  53. return retval;
  54. }
  55. void nds_event_push(NDSEvent *src)
  56. {
  57. // Copy the source event.
  58. NDSEvent *new_event = cmalloc(sizeof(NDSEvent));
  59. memcpy(new_event, src, sizeof(NDSEvent));
  60. new_event->_next = NULL;
  61. // Add it to the list.
  62. if(evlist_back)
  63. {
  64. evlist_back->_next = new_event;
  65. evlist_back = new_event;
  66. }
  67. else
  68. {
  69. evlist_front = new_event;
  70. evlist_back = new_event;
  71. }
  72. }
  73. // Fill in a key down event.
  74. void nds_event_fill_key_down(NDSEvent *dest, int key)
  75. {
  76. dest->type = NDS_EVENT_KEY_DOWN;
  77. dest->key = key;
  78. }
  79. // Fill in a key up event.
  80. void nds_event_fill_key_up(NDSEvent *dest, int key)
  81. {
  82. dest->type = NDS_EVENT_KEY_UP;
  83. dest->key = key;
  84. }
  85. // Fill in a software keyboard key down event.
  86. void nds_event_fill_keyboard_down(NDSEvent *dest, int key)
  87. {
  88. dest->type = NDS_EVENT_KEYBOARD_DOWN;
  89. dest->key = key;
  90. }
  91. // Fill in a software keyboard key up event.
  92. void nds_event_fill_keyboard_up(NDSEvent *dest, int key)
  93. {
  94. dest->type = NDS_EVENT_KEYBOARD_UP;
  95. dest->key = key;
  96. }
  97. // Fill in a touch event.
  98. void nds_event_fill_touch_down(NDSEvent *dest)
  99. {
  100. dest->type = NDS_EVENT_TOUCH_DOWN;
  101. }
  102. // Fill in a touch event.
  103. void nds_event_fill_touch_move(NDSEvent *dest, int x, int y)
  104. {
  105. dest->type = NDS_EVENT_TOUCH_MOVE;
  106. dest->x = x;
  107. dest->y = y;
  108. }
  109. // Fill in a touch event.
  110. void nds_event_fill_touch_up(NDSEvent *dest)
  111. {
  112. dest->type = NDS_EVENT_TOUCH_UP;
  113. }