os_event_queue.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include <cstring>
  25. #include "types.h"
  26. #include "mouse.h"
  27. #include "keyboard.h"
  28. #include "atomic_int.h"
  29. namespace crown
  30. {
  31. struct OsMetricsEvent
  32. {
  33. uint16_t x;
  34. uint16_t y;
  35. uint16_t width;
  36. uint16_t height;
  37. };
  38. struct OsExitEvent
  39. {
  40. int32_t code;
  41. };
  42. /// Represents an event fired by mouse.
  43. struct OsMouseEvent
  44. {
  45. enum Enum
  46. {
  47. BUTTON,
  48. WHEEL,
  49. MOVE
  50. };
  51. OsMouseEvent::Enum type;
  52. MouseButton::Enum button;
  53. uint16_t x;
  54. uint16_t y;
  55. bool pressed;
  56. float wheel;
  57. };
  58. /// Represents an event fired by keyboard.
  59. struct OsKeyboardEvent
  60. {
  61. KeyboardButton::Enum button;
  62. uint32_t modifier;
  63. bool pressed;
  64. };
  65. /// Represents an event fired by touch screen.
  66. struct OsTouchEvent
  67. {
  68. enum Enum
  69. {
  70. POINTER,
  71. MOVE
  72. };
  73. OsTouchEvent::Enum type;
  74. uint8_t pointer_id;
  75. uint16_t x;
  76. uint16_t y;
  77. bool pressed;
  78. };
  79. /// Represents an event fired by accelerometer.
  80. struct OsAccelerometerEvent
  81. {
  82. float x;
  83. float y;
  84. float z;
  85. };
  86. struct OsEvent
  87. {
  88. /// Represents an event fired by the OS
  89. enum Enum
  90. {
  91. NONE = 0,
  92. KEYBOARD = 1,
  93. MOUSE = 2,
  94. TOUCH = 3,
  95. ACCELEROMETER = 4,
  96. METRICS,
  97. PAUSE,
  98. RESUME,
  99. // Exit from program
  100. EXIT
  101. };
  102. OsEvent::Enum type;
  103. union
  104. {
  105. OsMetricsEvent metrics;
  106. OsExitEvent exit;
  107. OsMouseEvent mouse;
  108. OsKeyboardEvent keyboard;
  109. OsTouchEvent touch;
  110. OsAccelerometerEvent accelerometer;
  111. };
  112. };
  113. #define MAX_OS_EVENTS 64
  114. /// Single Producer Single Consumer event queue.
  115. /// Used only to pass events from os thread to main thread.
  116. struct OsEventQueue
  117. {
  118. OsEventQueue()
  119. : _tail(0)
  120. , _head(0)
  121. {
  122. }
  123. void push_mouse_event(uint16_t x, uint16_t y)
  124. {
  125. OsEvent ev;
  126. ev.type = OsEvent::MOUSE;
  127. ev.mouse.type = OsMouseEvent::MOVE;
  128. ev.mouse.x = x;
  129. ev.mouse.y = y;
  130. push_event(ev);
  131. }
  132. void push_mouse_event(uint16_t x, uint16_t y, MouseButton::Enum b, bool pressed)
  133. {
  134. OsEvent ev;
  135. ev.type = OsEvent::MOUSE;
  136. ev.mouse.type = OsMouseEvent::BUTTON;
  137. ev.mouse.x = x;
  138. ev.mouse.y = y;
  139. ev.mouse.button = b;
  140. ev.mouse.pressed = pressed;
  141. push_event(ev);
  142. }
  143. void push_mouse_event(uint16_t x, uint16_t y, float wheel)
  144. {
  145. OsEvent ev;
  146. ev.type = OsEvent::MOUSE;
  147. ev.mouse.type = OsMouseEvent::WHEEL;
  148. ev.mouse.x = x;
  149. ev.mouse.y = y;
  150. ev.mouse.wheel = wheel;
  151. push_event(ev);
  152. }
  153. void push_keyboard_event(uint32_t modifier, KeyboardButton::Enum b, bool pressed)
  154. {
  155. OsEvent ev;
  156. ev.type = OsEvent::KEYBOARD;
  157. ev.keyboard.button = b;
  158. ev.keyboard.modifier = modifier;
  159. ev.keyboard.pressed = pressed;
  160. push_event(ev);
  161. }
  162. void push_touch_event(uint16_t x, uint16_t y, uint8_t pointer_id)
  163. {
  164. OsEvent ev;
  165. ev.type = OsEvent::TOUCH;
  166. ev.touch.type = OsTouchEvent::MOVE;
  167. ev.touch.x = x;
  168. ev.touch.y = y;
  169. ev.touch.pointer_id = pointer_id;
  170. push_event(ev);
  171. }
  172. void push_touch_event(uint16_t x, uint16_t y, uint8_t pointer_id, bool pressed)
  173. {
  174. OsEvent ev;
  175. ev.type = OsEvent::TOUCH;
  176. ev.touch.type = OsTouchEvent::POINTER;
  177. ev.touch.x = x;
  178. ev.touch.y = y;
  179. ev.touch.pointer_id = pointer_id;
  180. ev.touch.pressed = pressed;
  181. push_event(ev);
  182. }
  183. void push_exit_event(int32_t code)
  184. {
  185. OsEvent ev;
  186. ev.type = OsEvent::EXIT;
  187. ev.exit.code = code;
  188. push_event(ev);
  189. }
  190. void push_pause_event()
  191. {
  192. OsEvent ev;
  193. ev.type = OsEvent::PAUSE;
  194. push_event(ev);
  195. }
  196. void push_resume_event()
  197. {
  198. OsEvent ev;
  199. ev.type = OsEvent::RESUME;
  200. push_event(ev);
  201. }
  202. void push_metrics_event(uint16_t x, uint16_t y, uint16_t width, uint16_t height)
  203. {
  204. OsEvent ev;
  205. ev.type = OsEvent::METRICS;
  206. ev.metrics.x = x;
  207. ev.metrics.y = y;
  208. ev.metrics.width = width;
  209. ev.metrics.height = height;
  210. push_event(ev);
  211. }
  212. void push_none_event()
  213. {
  214. OsEvent ev;
  215. ev.type = OsEvent::NONE;
  216. push_event(ev);
  217. }
  218. bool push_event(const OsEvent& ev)
  219. {
  220. int cur_tail = _tail.load();
  221. int next_tail = increment(cur_tail);
  222. if(next_tail != _head.load())
  223. {
  224. _queue[cur_tail] = ev;
  225. _tail.store(next_tail);
  226. return true;
  227. }
  228. return false;
  229. }
  230. bool pop_event(OsEvent& ev)
  231. {
  232. const int cur_head = _head.load();
  233. if(cur_head == _tail.load()) return false;
  234. ev = _queue[cur_head];
  235. _head.store(increment(cur_head));
  236. return true;
  237. }
  238. int increment(int idx) const
  239. {
  240. return (idx + 1) % MAX_OS_EVENTS;
  241. }
  242. private:
  243. OsEvent _queue[MAX_OS_EVENTS];
  244. AtomicInt _tail;
  245. AtomicInt _head;
  246. };
  247. } // namespace crown