react.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "eruta.h"
  2. #include "event.h"
  3. #include "mode.h"
  4. #include "state.h"
  5. #include "react.h"
  6. #include "zori.h"
  7. /** Initializes the react structure so it does nothing at all in all cases. */
  8. React * react_initempty(React * self, void * data) {
  9. if(!self) return NULL;
  10. self->data = data;
  11. self->joystick_axis = NULL;
  12. self->joystick_button_up = NULL;
  13. self->joystick_button_down = NULL;
  14. self->joystick_configuration= NULL;
  15. self->keyboard_key_down = NULL;
  16. self->keyboard_key_up = NULL;
  17. self->keyboard_key_char = NULL;
  18. self->mouse_axes = NULL;
  19. self->mouse_button_up = NULL;
  20. self->mouse_button_down = NULL;
  21. self->mouse_enter_display = NULL;
  22. self->mouse_leave_display = NULL;
  23. self->mouse_warped = NULL;
  24. self->timer = NULL;
  25. self->display_expose = NULL;
  26. self->display_resize = NULL;
  27. self->display_close = NULL;
  28. self->display_lost = NULL;
  29. self->display_found = NULL;
  30. self->display_switch_in = NULL;
  31. self->display_switch_out = NULL;
  32. self->display_orientation = NULL;
  33. return self;
  34. }
  35. #define DO_REACT(SELF, EVENT, MEMBER, HANDLER) \
  36. do { \
  37. if(!SELF->HANDLER) return NULL; \
  38. return self->HANDLER(SELF, &((EVENT)->MEMBER)); \
  39. } while(0)
  40. /** React to an allegro event. */
  41. React * react_react(React * self, ALLEGRO_EVENT * event) {
  42. switch(event->type) {
  43. case ALLEGRO_EVENT_JOYSTICK_AXIS:
  44. DO_REACT(self, event, joystick, joystick_axis);
  45. case ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN:
  46. DO_REACT(self, event, joystick, joystick_button_down);
  47. case ALLEGRO_EVENT_JOYSTICK_BUTTON_UP:
  48. DO_REACT(self, event, joystick, joystick_button_up);
  49. case ALLEGRO_EVENT_JOYSTICK_CONFIGURATION:
  50. DO_REACT(self, event, joystick, joystick_configuration);
  51. case ALLEGRO_EVENT_KEY_DOWN:
  52. DO_REACT(self, event, keyboard, keyboard_key_down);
  53. case ALLEGRO_EVENT_KEY_CHAR:
  54. DO_REACT(self, event, keyboard, keyboard_key_char);
  55. case ALLEGRO_EVENT_KEY_UP:
  56. DO_REACT(self, event, keyboard, keyboard_key_up);
  57. case ALLEGRO_EVENT_MOUSE_AXES:
  58. DO_REACT(self, event, mouse, mouse_axes);
  59. case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN:
  60. DO_REACT(self, event, mouse, mouse_button_down);
  61. case ALLEGRO_EVENT_MOUSE_BUTTON_UP:
  62. DO_REACT(self, event, mouse, mouse_button_up);
  63. case ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY:
  64. DO_REACT(self, event, mouse, mouse_enter_display);
  65. case ALLEGRO_EVENT_MOUSE_LEAVE_DISPLAY:
  66. DO_REACT(self, event, mouse, mouse_leave_display);
  67. case ALLEGRO_EVENT_MOUSE_WARPED:
  68. DO_REACT(self, event, mouse, mouse_warped);
  69. case ALLEGRO_EVENT_TIMER:
  70. DO_REACT(self, event, timer, timer);
  71. case ALLEGRO_EVENT_DISPLAY_EXPOSE:
  72. DO_REACT(self, event, display, display_expose);
  73. case ALLEGRO_EVENT_DISPLAY_RESIZE:
  74. DO_REACT(self, event, display, display_resize);
  75. case ALLEGRO_EVENT_DISPLAY_CLOSE:
  76. DO_REACT(self, event, display, display_close);
  77. case ALLEGRO_EVENT_DISPLAY_LOST:
  78. DO_REACT(self, event, display, display_lost);
  79. case ALLEGRO_EVENT_DISPLAY_FOUND:
  80. DO_REACT(self, event, display, display_found);
  81. case ALLEGRO_EVENT_DISPLAY_SWITCH_IN:
  82. DO_REACT(self, event, display, display_switch_in);
  83. case ALLEGRO_EVENT_DISPLAY_SWITCH_OUT:
  84. DO_REACT(self, event, display, display_switch_out);
  85. case ALLEGRO_EVENT_DISPLAY_ORIENTATION:
  86. DO_REACT(self, event, display, display_orientation);
  87. default:
  88. return NULL;
  89. }
  90. }
  91. /** Polls for allegro events and reacts to them. */
  92. React * react_poll(React * self, void * state) {
  93. int res;
  94. ALLEGRO_EVENT * event;
  95. struct zori_console * console = state_console(state);
  96. if(!self) return NULL;
  97. // yes an assignment is fine here :)
  98. while( (event = state_pollnew((State *)state)) ) {
  99. /* Let react react first, then if that fails, send to the gui,
  100. * but only if it is active. If not active,
  101. * send the event to ruby */
  102. if(!react_react(self, event)) {
  103. if (zori_handle_system_event(event) != ZORI_EVENT_DONE) {
  104. rh_poll_event(state_ruby(state_get()), event);
  105. }
  106. }
  107. event_free(event);
  108. // here we must free the event...
  109. }
  110. return self;
  111. }