os_event_queue.h 6.2 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. MOVE
  49. };
  50. OsMouseEvent::Enum type;
  51. MouseButton::Enum button;
  52. uint16_t x;
  53. uint16_t y;
  54. bool pressed;
  55. };
  56. /// Represents an event fired by keyboard.
  57. struct OsKeyboardEvent
  58. {
  59. KeyboardButton::Enum button;
  60. uint32_t modifier;
  61. bool pressed;
  62. };
  63. /// Represents an event fired by touch screen.
  64. struct OsTouchEvent
  65. {
  66. enum Enum
  67. {
  68. POINTER,
  69. MOVE
  70. };
  71. OsTouchEvent::Enum type;
  72. uint8_t pointer_id;
  73. uint16_t x;
  74. uint16_t y;
  75. bool pressed;
  76. };
  77. /// Represents an event fired by accelerometer.
  78. struct OsAccelerometerEvent
  79. {
  80. float x;
  81. float y;
  82. float z;
  83. };
  84. struct OsEvent
  85. {
  86. /// Represents an event fired by the OS
  87. enum Enum
  88. {
  89. NONE = 0,
  90. KEYBOARD = 1,
  91. MOUSE = 2,
  92. TOUCH = 3,
  93. ACCELEROMETER = 4,
  94. METRICS,
  95. PAUSE,
  96. RESUME,
  97. // Exit from program
  98. EXIT
  99. };
  100. OsEvent::Enum type;
  101. union
  102. {
  103. OsMetricsEvent metrics;
  104. OsExitEvent exit;
  105. OsMouseEvent mouse;
  106. OsKeyboardEvent keyboard;
  107. OsTouchEvent touch;
  108. OsAccelerometerEvent accelerometer;
  109. };
  110. };
  111. #define MAX_OS_EVENTS 64
  112. /// Single Producer Single Consumer event queue.
  113. /// Used only to pass events from os thread to main thread.
  114. struct OsEventQueue
  115. {
  116. //-----------------------------------------------------------------------------
  117. OsEventQueue()
  118. : m_tail(0)
  119. , m_head(0)
  120. {
  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. //-----------------------------------------------------------------------------
  133. void push_mouse_event(uint16_t x, uint16_t y, MouseButton::Enum b, bool pressed)
  134. {
  135. OsEvent ev;
  136. ev.type = OsEvent::MOUSE;
  137. ev.mouse.type = OsMouseEvent::BUTTON;
  138. ev.mouse.x = x;
  139. ev.mouse.y = y;
  140. ev.mouse.button = b;
  141. ev.mouse.pressed = pressed;
  142. push_event(ev);
  143. }
  144. //-----------------------------------------------------------------------------
  145. void push_keyboard_event(uint32_t modifier, KeyboardButton::Enum b, bool pressed)
  146. {
  147. OsEvent ev;
  148. ev.type = OsEvent::KEYBOARD;
  149. ev.keyboard.button = b;
  150. ev.keyboard.modifier = modifier;
  151. ev.keyboard.pressed = pressed;
  152. push_event(ev);
  153. }
  154. //-----------------------------------------------------------------------------
  155. void push_touch_event(uint16_t x, uint16_t y, uint8_t pointer_id)
  156. {
  157. OsEvent ev;
  158. ev.type = OsEvent::TOUCH;
  159. ev.touch.type = OsTouchEvent::MOVE;
  160. ev.touch.x = x;
  161. ev.touch.y = y;
  162. ev.touch.pointer_id = pointer_id;
  163. push_event(ev);
  164. }
  165. //-----------------------------------------------------------------------------
  166. void push_touch_event(uint16_t x, uint16_t y, uint8_t pointer_id, bool pressed)
  167. {
  168. OsEvent ev;
  169. ev.type = OsEvent::TOUCH;
  170. ev.touch.type = OsTouchEvent::POINTER;
  171. ev.touch.x = x;
  172. ev.touch.y = y;
  173. ev.touch.pointer_id = pointer_id;
  174. ev.touch.pressed = pressed;
  175. push_event(ev);
  176. }
  177. //-----------------------------------------------------------------------------
  178. void push_exit_event(int32_t code)
  179. {
  180. OsEvent ev;
  181. ev.type = OsEvent::EXIT;
  182. ev.exit.code = code;
  183. push_event(ev);
  184. }
  185. //-----------------------------------------------------------------------------
  186. void push_pause_event()
  187. {
  188. OsEvent ev;
  189. ev.type = OsEvent::PAUSE;
  190. push_event(ev);
  191. }
  192. //-----------------------------------------------------------------------------
  193. void push_resume_event()
  194. {
  195. OsEvent ev;
  196. ev.type = OsEvent::RESUME;
  197. push_event(ev);
  198. }
  199. //-----------------------------------------------------------------------------
  200. void push_metrics_event(uint16_t x, uint16_t y, uint16_t width, uint16_t height)
  201. {
  202. OsEvent ev;
  203. ev.type = OsEvent::METRICS;
  204. ev.metrics.x = x;
  205. ev.metrics.y = y;
  206. ev.metrics.width = width;
  207. ev.metrics.height = height;
  208. push_event(ev);
  209. }
  210. //-----------------------------------------------------------------------------
  211. void push_none_event()
  212. {
  213. OsEvent ev;
  214. ev.type = OsEvent::NONE;
  215. push_event(ev);
  216. }
  217. //-----------------------------------------------------------------------------
  218. bool push_event(const OsEvent& ev)
  219. {
  220. int cur_tail = m_tail.load();
  221. int next_tail = increment(cur_tail);
  222. if(next_tail != m_head.load())
  223. {
  224. m_queue[cur_tail] = ev;
  225. m_tail.store(next_tail);
  226. return true;
  227. }
  228. return false;
  229. }
  230. //-----------------------------------------------------------------------------
  231. bool pop_event(OsEvent& ev)
  232. {
  233. const int cur_head = m_head.load();
  234. if(cur_head == m_tail.load()) return false;
  235. ev = m_queue[cur_head];
  236. m_head.store(increment(cur_head));
  237. return true;
  238. }
  239. //-----------------------------------------------------------------------------
  240. int increment(int idx) const
  241. {
  242. return (idx + 1) % MAX_OS_EVENTS;
  243. }
  244. private:
  245. OsEvent m_queue[MAX_OS_EVENTS];
  246. AtomicInt m_tail;
  247. AtomicInt m_head;
  248. };
  249. } // namespace crown