OsEventQueue.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 "Queue.h"
  26. #include "Mutex.h"
  27. #include "ProxyAllocator.h"
  28. #include "OsTypes.h"
  29. namespace crown
  30. {
  31. struct OsEventQueue
  32. {
  33. //-----------------------------------------------------------------------------
  34. OsEventQueue()
  35. : m_allocator("os-event-queue", default_allocator()), m_queue(m_allocator)
  36. {
  37. }
  38. //-----------------------------------------------------------------------------
  39. void push_mouse_event(uint16_t x, uint16_t y)
  40. {
  41. OsEvent ev;
  42. ev.type = OsEvent::MOUSE;
  43. ev.mouse.type = OsMouseEvent::MOVE;
  44. ev.mouse.x = x;
  45. ev.mouse.y = y;
  46. push_event(&ev);
  47. }
  48. //-----------------------------------------------------------------------------
  49. void push_mouse_event(uint16_t x, uint16_t y, MouseButton::Enum b, bool pressed)
  50. {
  51. OsEvent ev;
  52. ev.type = OsEvent::MOUSE;
  53. ev.mouse.type = OsMouseEvent::BUTTON;
  54. ev.mouse.x = x;
  55. ev.mouse.y = y;
  56. ev.mouse.button = b;
  57. ev.mouse.pressed = pressed;
  58. push_event(&ev);
  59. }
  60. //-----------------------------------------------------------------------------
  61. void push_keyboard_event(uint32_t modifier, KeyboardButton::Enum b, bool pressed)
  62. {
  63. OsEvent ev;
  64. ev.type = OsEvent::KEYBOARD;
  65. ev.keyboard.button = b;
  66. ev.keyboard.modifier = modifier;
  67. ev.keyboard.pressed = pressed;
  68. push_event(&ev);
  69. }
  70. //-----------------------------------------------------------------------------
  71. void push_touch_event(uint16_t x, uint16_t y, uint8_t pointer_id)
  72. {
  73. OsEvent ev;
  74. ev.type = OsEvent::TOUCH;
  75. ev.touch.type = OsTouchEvent::MOVE;
  76. ev.touch.x = x;
  77. ev.touch.y = y;
  78. ev.touch.pointer_id = pointer_id;
  79. push_event(&ev);
  80. }
  81. //-----------------------------------------------------------------------------
  82. void push_touch_event(uint16_t x, uint16_t y, uint8_t pointer_id, bool pressed)
  83. {
  84. OsEvent ev;
  85. ev.type = OsEvent::TOUCH;
  86. ev.touch.type = OsTouchEvent::POINTER;
  87. ev.touch.x = x;
  88. ev.touch.y = y;
  89. ev.touch.pointer_id = pointer_id;
  90. ev.touch.pressed = pressed;
  91. push_event(&ev);
  92. }
  93. //-----------------------------------------------------------------------------
  94. void push_exit_event(int32_t code)
  95. {
  96. OsEvent ev;
  97. ev.type = OsEvent::EXIT;
  98. ev.exit.code = code;
  99. push_event(&ev);
  100. }
  101. //-----------------------------------------------------------------------------
  102. void push_metrics_event(uint16_t x, uint16_t y, uint16_t width, uint16_t height)
  103. {
  104. OsEvent ev;
  105. ev.type = OsEvent::METRICS;
  106. ev.metrics.x = x;
  107. ev.metrics.y = y;
  108. ev.metrics.width = width;
  109. ev.metrics.height = height;
  110. push_event(&ev);
  111. }
  112. //-----------------------------------------------------------------------------
  113. void push_event(OsEvent* ev)
  114. {
  115. CE_ASSERT_NOT_NULL(ev);
  116. m_mutex.lock();
  117. m_queue.push(ev, sizeof(OsEvent));
  118. m_mutex.unlock();
  119. }
  120. //-----------------------------------------------------------------------------
  121. void pop_event(OsEvent* ev)
  122. {
  123. CE_ASSERT_NOT_NULL(ev);
  124. m_mutex.lock();
  125. if (m_queue.size() > 0)
  126. {
  127. memcpy(ev, m_queue.begin(), sizeof(OsEvent));
  128. m_queue.pop(sizeof(OsEvent));
  129. }
  130. else
  131. {
  132. ev->type = OsEvent::NONE;
  133. }
  134. m_mutex.unlock();
  135. }
  136. private:
  137. ProxyAllocator m_allocator;
  138. Queue<OsEvent> m_queue;
  139. Mutex m_mutex;
  140. };
  141. } // namespace crown