event.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #include "event.h"
  2. #include "mem.h"
  3. /** Helkper functions for Allegro's events.
  4. * This mostly exists for use with scripting.
  5. */
  6. /** Allocates a new allegro event */
  7. ALLEGRO_EVENT * event_alloc() {
  8. return mem_alloc(sizeof(ALLEGRO_EVENT));
  9. }
  10. /** Frees an allegro event that was allocated though event_alloc.
  11. Returns NULL;*/
  12. ALLEGRO_EVENT * event_free(ALLEGRO_EVENT * self) {
  13. mem_free(self);
  14. return NULL;
  15. }
  16. /** Returns the type of an event, or 0 if not valid. */
  17. unsigned int event_type(Event * self) {
  18. if(!self) return 0;
  19. return self->type;
  20. }
  21. /** Returns the datestamp of an event, or -1.0 if not valid. */
  22. double event_timestamp(Event * self) {
  23. if(!self) return -1.0;
  24. return self->any.timestamp;
  25. }
  26. /** Returns nonzero if the event's type is mouse related, zero if not.*/
  27. int event_ismouse(Event * self) {
  28. if ((self->type >= ALLEGRO_EVENT_MOUSE_AXES)
  29. && (self->type <= ALLEGRO_EVENT_MOUSE_WARPED))
  30. return TRUE;
  31. return FALSE;
  32. }
  33. /** Returns nonzero if the event's type is display related, zero if not.*/
  34. int event_isdisplay(Event * self) {
  35. if ((self->type >= ALLEGRO_EVENT_DISPLAY_EXPOSE)
  36. && (self->type <= ALLEGRO_EVENT_DISPLAY_ORIENTATION))
  37. return TRUE;
  38. return FALSE;
  39. }
  40. /** Returns nonzero if the event's type is joystick related, zero if not.*/
  41. int event_isjoystick(Event * self) {
  42. if ((self->type >= ALLEGRO_EVENT_JOYSTICK_AXIS)
  43. && (self->type <= ALLEGRO_EVENT_JOYSTICK_CONFIGURATION))
  44. return TRUE;
  45. return FALSE;
  46. }
  47. /** Returns nonzero if the event's type is keyboard related, zero if not.*/
  48. int event_iskeyboard(Event * self) {
  49. if ((self->type >= ALLEGRO_EVENT_KEY_DOWN)
  50. && (self->type <= ALLEGRO_EVENT_KEY_UP))
  51. return TRUE;
  52. return FALSE;
  53. }
  54. /** Returns nonzero if the event's type is keyboard related, zero if not.*/
  55. int event_istimer(Event * self) {
  56. if (self->type == ALLEGRO_EVENT_TIMER) return TRUE;
  57. return FALSE;
  58. }
  59. /** Returns the x position of an event (for mouse
  60. and display event) return -1 if not valid. */
  61. int event_x(Event * self) {
  62. if(!self) return -1;
  63. if(event_ismouse(self)) return self->mouse.x;
  64. if(event_isdisplay(self)) return self->display.x;
  65. return -1;
  66. }
  67. /** Returns the y position of an event (for mouse
  68. and display event) return -1 if not valid. */
  69. int event_y(Event * self) {
  70. if(!self) return -1;
  71. if(event_ismouse(self)) return self->mouse.y;
  72. if(event_isdisplay(self)) return self->display.y;
  73. return -1;
  74. }
  75. /** Returns the width of an event (for display event)
  76. returns -1 if not valid. */
  77. int event_width(Event * self) {
  78. if(!self) return -1;
  79. if(event_isdisplay(self)) return self->display.width;
  80. return -1;
  81. }
  82. /** Returns the height of an event (for display event)
  83. returns -1 if not valid. */
  84. int event_height(Event * self) {
  85. if(!self) return -1;
  86. if(event_isdisplay(self)) return self->display.height;
  87. return -1;
  88. }
  89. /** Returns the orientation of an event (for display event)
  90. returns -1 if not valid. */
  91. int event_orientation(Event * self) {
  92. if(!self) return -1;
  93. if(event_isdisplay(self)) return self->display.orientation;
  94. return -1;
  95. }
  96. /** Returns the stick number of a joystick event, -1 on error. */
  97. int event_stick(Event * self) {
  98. if(!self) return -1;
  99. if(event_isjoystick(self)) return self->joystick.stick;
  100. return -1;
  101. }
  102. /** Returns the axis number of a joystick event, -1 on error. */
  103. int event_axis(Event * self) {
  104. if(!self) return -1;
  105. if(event_isjoystick(self)) return self->joystick.axis;
  106. return -1;
  107. }
  108. /** Returns the position of a joystick event, 0.0 on error */
  109. float event_pos(Event * self) {
  110. if(!self) return -1;
  111. if(event_isjoystick(self)) return self->joystick.pos;
  112. return -1;
  113. }
  114. /** Returns the button number of a joystick event or mouse event, -1 on error */
  115. int event_button(Event * self) {
  116. if(!self) return -1;
  117. if(event_isjoystick(self)) return self->joystick.button;
  118. if(event_ismouse(self)) return (int) self->mouse.button;
  119. return -1;
  120. }
  121. /** returns the keycode for the keyboard event, -1 n error.*/
  122. int event_keycode(Event * self) {
  123. if(!self) return -1;
  124. if(event_iskeyboard(self)) return self->keyboard.keycode;
  125. return -1;
  126. }
  127. /** returns the unicode character for the keyboard event, -1 n error.*/
  128. int event_unichar(Event * self) {
  129. if(!self) return -1;
  130. if(event_iskeyboard(self)) return self->keyboard.unichar;
  131. return -1;
  132. }
  133. /** Returns the modifiers for the keyboard event, -1 n error.*/
  134. int event_modifiers(Event * self) {
  135. if(!self) return -1;
  136. if(event_iskeyboard(self)) return (int)self->keyboard.modifiers;
  137. return -1;
  138. }
  139. /** Returns the autorepeat field of the keyboard event, -1 n error.*/
  140. int event_repeat(Event * self) {
  141. if(!self) return -1;
  142. if(event_iskeyboard(self)) return (int)self->keyboard.repeat;
  143. return -1;
  144. }
  145. /** Returns the z for a mouse event, -1 on error */
  146. int event_z(Event * self) {
  147. if(!self) return -1;
  148. if(event_ismouse(self)) return (int) self->mouse.z;
  149. return -1;
  150. }
  151. /** Returns the z for a mouse event, -1 on error */
  152. int event_w(Event * self) {
  153. if(!self) return -1;
  154. if(event_ismouse(self)) return (int) self->mouse.w;
  155. return -1;
  156. }
  157. /** Returns the dx for a mouse event, -1 on error */
  158. int event_dx(Event * self) {
  159. if(!self) return -1;
  160. if(event_ismouse(self)) return (int) self->mouse.dx;
  161. return -1;
  162. }
  163. /** Returns the dy for a mouse event, -1 on error */
  164. int event_dy(Event * self) {
  165. if(!self) return -1;
  166. if(event_ismouse(self)) return (int) self->mouse.dy;
  167. return -1;
  168. }
  169. /** Returns the dz for a mouse event, -1 on error */
  170. int event_dz(Event * self) {
  171. if(!self) return -1;
  172. if(event_ismouse(self)) return (int) self->mouse.dz;
  173. return -1;
  174. }
  175. /** Returns the dw for a mouse event, -1 on error */
  176. int event_dw(Event * self) {
  177. if(!self) return -1;
  178. if(event_ismouse(self)) return (int) self->mouse.dw;
  179. return -1;
  180. }
  181. /** Returns the pressure for a mouse event, -1.0 on error */
  182. float event_pressure(Event * self) {
  183. if(!self) return -1.0;
  184. if(event_ismouse(self)) return self->mouse.pressure;
  185. return -1.0;
  186. }
  187. /** Returns the count for a timer event, -1 on error */
  188. int64_t event_count(Event * self) {
  189. if(!self) return -1;
  190. if(event_istimer(self)) return self->timer.count;
  191. return -1;
  192. }
  193. /** Returns the timer error for a timer event, 0.0 on error */
  194. double event_error(Event * self) {
  195. if(!self) return 0.0;
  196. if(event_istimer(self)) return self->timer.error;
  197. return 0.0;
  198. }