haiku_direct_window.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*************************************************************************/
  2. /* haiku_direct_window.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include <UnicodeChar.h>
  31. #include "core/os/keyboard.h"
  32. #include "haiku_direct_window.h"
  33. #include "key_mapping_haiku.h"
  34. #include "main/main.h"
  35. HaikuDirectWindow::HaikuDirectWindow(BRect p_frame) :
  36. BDirectWindow(p_frame, "Godot", B_TITLED_WINDOW, B_QUIT_ON_WINDOW_CLOSE) {
  37. last_mouse_pos_valid = false;
  38. last_buttons_state = 0;
  39. last_button_mask = 0;
  40. last_key_modifier_state = 0;
  41. view = NULL;
  42. update_runner = NULL;
  43. input = NULL;
  44. main_loop = NULL;
  45. }
  46. HaikuDirectWindow::~HaikuDirectWindow() {
  47. }
  48. void HaikuDirectWindow::SetHaikuGLView(HaikuGLView *p_view) {
  49. view = p_view;
  50. }
  51. void HaikuDirectWindow::StartMessageRunner() {
  52. update_runner = new BMessageRunner(BMessenger(this),
  53. new BMessage(REDRAW_MSG), 1000000 / 60 /* 60 fps */);
  54. }
  55. void HaikuDirectWindow::StopMessageRunner() {
  56. delete update_runner;
  57. }
  58. void HaikuDirectWindow::SetInput(InputDefault *p_input) {
  59. input = p_input;
  60. }
  61. void HaikuDirectWindow::SetMainLoop(MainLoop *p_main_loop) {
  62. main_loop = p_main_loop;
  63. }
  64. bool HaikuDirectWindow::QuitRequested() {
  65. StopMessageRunner();
  66. main_loop->notification(MainLoop::NOTIFICATION_WM_QUIT_REQUEST);
  67. return false;
  68. }
  69. void HaikuDirectWindow::DirectConnected(direct_buffer_info *info) {
  70. view->DirectConnected(info);
  71. view->EnableDirectMode(true);
  72. }
  73. void HaikuDirectWindow::MessageReceived(BMessage *message) {
  74. switch (message->what) {
  75. case REDRAW_MSG:
  76. if (Main::iteration()) {
  77. view->EnableDirectMode(false);
  78. Quit();
  79. }
  80. break;
  81. default:
  82. BDirectWindow::MessageReceived(message);
  83. }
  84. }
  85. void HaikuDirectWindow::DispatchMessage(BMessage *message, BHandler *handler) {
  86. switch (message->what) {
  87. case B_MOUSE_DOWN:
  88. case B_MOUSE_UP:
  89. HandleMouseButton(message);
  90. break;
  91. case B_MOUSE_MOVED:
  92. HandleMouseMoved(message);
  93. break;
  94. case B_MOUSE_WHEEL_CHANGED:
  95. HandleMouseWheelChanged(message);
  96. break;
  97. case B_KEY_DOWN:
  98. case B_KEY_UP:
  99. HandleKeyboardEvent(message);
  100. break;
  101. case B_MODIFIERS_CHANGED:
  102. HandleKeyboardModifierEvent(message);
  103. break;
  104. case B_WINDOW_RESIZED:
  105. HandleWindowResized(message);
  106. break;
  107. case LOCKGL_MSG:
  108. view->LockGL();
  109. break;
  110. case UNLOCKGL_MSG:
  111. view->UnlockGL();
  112. break;
  113. default:
  114. BDirectWindow::DispatchMessage(message, handler);
  115. }
  116. }
  117. void HaikuDirectWindow::HandleMouseButton(BMessage *message) {
  118. BPoint where;
  119. if (message->FindPoint("where", &where) != B_OK) {
  120. return;
  121. }
  122. uint32 modifiers = message->FindInt32("modifiers");
  123. uint32 buttons = message->FindInt32("buttons");
  124. uint32 button = buttons ^ last_buttons_state;
  125. last_buttons_state = buttons;
  126. // TODO: implement the mouse_mode checks
  127. /*
  128. if (mouse_mode == MOUSE_MODE_CAPTURED) {
  129. event.xbutton.x=last_mouse_pos.x;
  130. event.xbutton.y=last_mouse_pos.y;
  131. }
  132. */
  133. Ref<InputEventMouseButton> mouse_event;
  134. mouse_event.instance();
  135. mouse_event->set_button_mask(GetMouseButtonState(buttons));
  136. mouse_event->set_position({ where.x, where.y });
  137. mouse_event->set_global_position({ where.x, where.y });
  138. GetKeyModifierState(mouse_event, modifiers);
  139. switch (button) {
  140. default:
  141. case B_PRIMARY_MOUSE_BUTTON:
  142. mouse_event->set_button_index(1);
  143. break;
  144. case B_SECONDARY_MOUSE_BUTTON:
  145. mouse_event->set_button_index(2);
  146. break;
  147. case B_TERTIARY_MOUSE_BUTTON:
  148. mouse_event->set_button_index(3);
  149. break;
  150. }
  151. mouse_event->set_pressed(message->what == B_MOUSE_DOWN);
  152. if (message->what == B_MOUSE_DOWN && mouse_event->get_button_index() == 1) {
  153. int32 clicks = message->FindInt32("clicks");
  154. if (clicks > 1) {
  155. mouse_event->set_doubleclick(true);
  156. }
  157. }
  158. input->parse_input_event(mouse_event);
  159. }
  160. void HaikuDirectWindow::HandleMouseMoved(BMessage *message) {
  161. BPoint where;
  162. if (message->FindPoint("where", &where) != B_OK) {
  163. return;
  164. }
  165. Point2i pos(where.x, where.y);
  166. uint32 modifiers = message->FindInt32("modifiers");
  167. uint32 buttons = message->FindInt32("buttons");
  168. if (!last_mouse_pos_valid) {
  169. last_mouse_position = pos;
  170. last_mouse_pos_valid = true;
  171. }
  172. Point2i rel = pos - last_mouse_position;
  173. Ref<InputEventMouseMotion> motion_event;
  174. motion_event.instance();
  175. GetKeyModifierState(motion_event, modifiers);
  176. motion_event->set_button_mask(GetMouseButtonState(buttons));
  177. motion_event->set_position({ pos.x, pos.y });
  178. input->set_mouse_position(pos);
  179. motion_event->set_global_position({ pos.x, pos.y });
  180. motion_event->set_speed({ input->get_last_mouse_speed().x,
  181. input->get_last_mouse_speed().y });
  182. motion_event->set_relative({ rel.x, rel.y });
  183. last_mouse_position = pos;
  184. input->parse_input_event(motion_event);
  185. }
  186. void HaikuDirectWindow::HandleMouseWheelChanged(BMessage *message) {
  187. float wheel_delta_y = 0;
  188. if (message->FindFloat("be:wheel_delta_y", &wheel_delta_y) != B_OK) {
  189. return;
  190. }
  191. Ref<InputEventMouseButton> mouse_event;
  192. mouse_event.instance();
  193. //GetKeyModifierState(mouse_event, modifiers);
  194. mouse_event->set_button_index(wheel_delta_y < 0 ? 4 : 5);
  195. mouse_event->set_button_mask(last_button_mask);
  196. mouse_event->set_position({ last_mouse_position.x,
  197. last_mouse_position.y });
  198. mouse_event->set_global_position({ last_mouse_position.x,
  199. last_mouse_position.y });
  200. mouse_event->set_pressed(true);
  201. input->parse_input_event(mouse_event);
  202. mouse_event->set_pressed(false);
  203. input->parse_input_event(mouse_event);
  204. }
  205. void HaikuDirectWindow::HandleKeyboardEvent(BMessage *message) {
  206. int32 raw_char = 0;
  207. int32 key = 0;
  208. int32 modifiers = 0;
  209. if (message->FindInt32("raw_char", &raw_char) != B_OK) {
  210. return;
  211. }
  212. if (message->FindInt32("key", &key) != B_OK) {
  213. return;
  214. }
  215. if (message->FindInt32("modifiers", &modifiers) != B_OK) {
  216. return;
  217. }
  218. Ref<InputEventKey> event;
  219. event.instance();
  220. GetKeyModifierState(event, modifiers);
  221. event->set_pressed(message->what == B_KEY_DOWN);
  222. event->set_scancode(KeyMappingHaiku::get_keysym(raw_char, key));
  223. event->set_echo(message->HasInt32("be:key_repeat"));
  224. event->set_unicode(0);
  225. const char *bytes = NULL;
  226. if (message->FindString("bytes", &bytes) == B_OK) {
  227. event->set_unicode(BUnicodeChar::FromUTF8(&bytes));
  228. }
  229. //make it consistent across platforms.
  230. if (event->get_scancode() == KEY_BACKTAB) {
  231. event->set_scancode(KEY_TAB);
  232. event->set_shift(true);
  233. }
  234. input->parse_input_event(event);
  235. }
  236. void HaikuDirectWindow::HandleKeyboardModifierEvent(BMessage *message) {
  237. int32 old_modifiers = 0;
  238. int32 modifiers = 0;
  239. if (message->FindInt32("be:old_modifiers", &old_modifiers) != B_OK) {
  240. return;
  241. }
  242. if (message->FindInt32("modifiers", &modifiers) != B_OK) {
  243. return;
  244. }
  245. int32 key = old_modifiers ^ modifiers;
  246. Ref<InputEventWithModifiers> event;
  247. event.instance();
  248. GetKeyModifierState(event, modifiers);
  249. event->set_shift(key & B_SHIFT_KEY);
  250. event->set_alt(key & B_OPTION_KEY);
  251. event->set_control(key & B_CONTROL_KEY);
  252. event->set_command(key & B_COMMAND_KEY);
  253. input->parse_input_event(event);
  254. }
  255. void HaikuDirectWindow::HandleWindowResized(BMessage *message) {
  256. int32 width = 0;
  257. int32 height = 0;
  258. if ((message->FindInt32("width", &width) != B_OK) || (message->FindInt32("height", &height) != B_OK)) {
  259. return;
  260. }
  261. current_video_mode->width = width;
  262. current_video_mode->height = height;
  263. }
  264. inline void HaikuDirectWindow::GetKeyModifierState(Ref<InputEventWithModifiers> event, uint32 p_state) {
  265. last_key_modifier_state = p_state;
  266. event->set_shift(p_state & B_SHIFT_KEY);
  267. event->set_control(p_state & B_CONTROL_KEY);
  268. event->set_alt(p_state & B_OPTION_KEY);
  269. event->set_metakey(p_state & B_COMMAND_KEY);
  270. return state;
  271. }
  272. inline int HaikuDirectWindow::GetMouseButtonState(uint32 p_state) {
  273. int state = 0;
  274. if (p_state & B_PRIMARY_MOUSE_BUTTON) {
  275. state |= 1 << 0;
  276. }
  277. if (p_state & B_SECONDARY_MOUSE_BUTTON) {
  278. state |= 1 << 1;
  279. }
  280. if (p_state & B_TERTIARY_MOUSE_BUTTON) {
  281. state |= 1 << 2;
  282. }
  283. last_button_mask = state;
  284. return state;
  285. }