entry_glfw.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. /*
  2. * Copyright 2011-2023 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. #include "entry_p.h"
  6. #if ENTRY_CONFIG_USE_GLFW
  7. #define GLFW_INCLUDE_NONE
  8. #include <GLFW/glfw3.h>
  9. #if GLFW_VERSION_MINOR < 2
  10. # error "GLFW 3.2 or later is required"
  11. #endif // GLFW_VERSION_MINOR < 2
  12. #if BX_PLATFORM_LINUX || BX_PLATFORM_BSD
  13. # if ENTRY_CONFIG_USE_WAYLAND
  14. # include <wayland-egl.h>
  15. # define GLFW_EXPOSE_NATIVE_WAYLAND
  16. # else
  17. # define GLFW_EXPOSE_NATIVE_X11
  18. # define GLFW_EXPOSE_NATIVE_GLX
  19. # endif
  20. #elif BX_PLATFORM_OSX
  21. # define GLFW_EXPOSE_NATIVE_COCOA
  22. # define GLFW_EXPOSE_NATIVE_NSGL
  23. #elif BX_PLATFORM_WINDOWS
  24. # define GLFW_EXPOSE_NATIVE_WIN32
  25. # define GLFW_EXPOSE_NATIVE_WGL
  26. #endif //
  27. #include <GLFW/glfw3native.h>
  28. #include <bgfx/platform.h>
  29. #include <bx/handlealloc.h>
  30. #include <bx/thread.h>
  31. #include <bx/mutex.h>
  32. #include <tinystl/string.h>
  33. #include "dbg.h"
  34. namespace entry
  35. {
  36. static void* glfwNativeWindowHandle(GLFWwindow* _window)
  37. {
  38. # if BX_PLATFORM_LINUX || BX_PLATFORM_BSD
  39. # if ENTRY_CONFIG_USE_WAYLAND
  40. wl_egl_window *win_impl = (wl_egl_window*)glfwGetWindowUserPointer(_window);
  41. if(!win_impl)
  42. {
  43. int width, height;
  44. glfwGetWindowSize(_window, &width, &height);
  45. struct wl_surface* surface = (struct wl_surface*)glfwGetWaylandWindow(_window);
  46. if(!surface)
  47. return nullptr;
  48. win_impl = wl_egl_window_create(surface, width, height);
  49. glfwSetWindowUserPointer(_window, (void*)(uintptr_t)win_impl);
  50. }
  51. return (void*)(uintptr_t)win_impl;
  52. # else
  53. return (void*)(uintptr_t)glfwGetX11Window(_window);
  54. # endif
  55. # elif BX_PLATFORM_OSX
  56. return glfwGetCocoaWindow(_window);
  57. # elif BX_PLATFORM_WINDOWS
  58. return glfwGetWin32Window(_window);
  59. # endif // BX_PLATFORM_
  60. }
  61. static void glfwDestroyWindowImpl(GLFWwindow *_window)
  62. {
  63. if(!_window)
  64. return;
  65. # if BX_PLATFORM_LINUX || BX_PLATFORM_BSD
  66. # if ENTRY_CONFIG_USE_WAYLAND
  67. wl_egl_window *win_impl = (wl_egl_window*)glfwGetWindowUserPointer(_window);
  68. if(win_impl)
  69. {
  70. glfwSetWindowUserPointer(_window, nullptr);
  71. wl_egl_window_destroy(win_impl);
  72. }
  73. # endif
  74. # endif
  75. glfwDestroyWindow(_window);
  76. }
  77. static uint8_t translateKeyModifiers(int _glfw)
  78. {
  79. uint8_t modifiers = 0;
  80. if (_glfw & GLFW_MOD_ALT)
  81. {
  82. modifiers |= Modifier::LeftAlt;
  83. }
  84. if (_glfw & GLFW_MOD_CONTROL)
  85. {
  86. modifiers |= Modifier::LeftCtrl;
  87. }
  88. if (_glfw & GLFW_MOD_SUPER)
  89. {
  90. modifiers |= Modifier::LeftMeta;
  91. }
  92. if (_glfw & GLFW_MOD_SHIFT)
  93. {
  94. modifiers |= Modifier::LeftShift;
  95. }
  96. return modifiers;
  97. }
  98. static Key::Enum s_translateKey[GLFW_KEY_LAST + 1];
  99. static Key::Enum translateKey(int _key)
  100. {
  101. return s_translateKey[_key];
  102. }
  103. static MouseButton::Enum translateMouseButton(int _button)
  104. {
  105. if (_button == GLFW_MOUSE_BUTTON_LEFT)
  106. {
  107. return MouseButton::Left;
  108. }
  109. else if (_button == GLFW_MOUSE_BUTTON_RIGHT)
  110. {
  111. return MouseButton::Right;
  112. }
  113. return MouseButton::Middle;
  114. }
  115. static GamepadAxis::Enum translateGamepadAxis(int _axis)
  116. {
  117. // HACK: Map XInput 360 controller until GLFW gamepad API
  118. static GamepadAxis::Enum axes[] =
  119. {
  120. GamepadAxis::LeftX,
  121. GamepadAxis::LeftY,
  122. GamepadAxis::RightX,
  123. GamepadAxis::RightY,
  124. GamepadAxis::LeftZ,
  125. GamepadAxis::RightZ,
  126. };
  127. return axes[_axis];
  128. }
  129. static Key::Enum translateGamepadButton(int _button)
  130. {
  131. // HACK: Map XInput 360 controller until GLFW gamepad API
  132. static Key::Enum buttons[] =
  133. {
  134. Key::GamepadA,
  135. Key::GamepadB,
  136. Key::GamepadX,
  137. Key::GamepadY,
  138. Key::GamepadShoulderL,
  139. Key::GamepadShoulderR,
  140. Key::GamepadBack,
  141. Key::GamepadStart,
  142. Key::GamepadThumbL,
  143. Key::GamepadThumbR,
  144. Key::GamepadUp,
  145. Key::GamepadRight,
  146. Key::GamepadDown,
  147. Key::GamepadLeft,
  148. Key::GamepadGuide,
  149. };
  150. return buttons[_button];
  151. }
  152. struct GamepadGLFW
  153. {
  154. GamepadGLFW()
  155. : m_connected(false)
  156. {
  157. bx::memSet(m_axes, 0, sizeof(m_axes));
  158. bx::memSet(m_buttons, 0, sizeof(m_buttons));
  159. }
  160. void update(EventQueue& _eventQueue)
  161. {
  162. int numButtons, numAxes;
  163. const unsigned char* buttons = glfwGetJoystickButtons(m_handle.idx, &numButtons);
  164. const float* axes = glfwGetJoystickAxes(m_handle.idx, &numAxes);
  165. if (NULL == buttons || NULL == axes)
  166. {
  167. return;
  168. }
  169. if (numAxes > GamepadAxis::Count)
  170. {
  171. numAxes = GamepadAxis::Count;
  172. }
  173. if (numButtons > Key::Count - Key::GamepadA)
  174. {
  175. numButtons = Key::Count - Key::GamepadA;
  176. }
  177. WindowHandle defaultWindow = { 0 };
  178. for (int ii = 0; ii < numAxes; ++ii)
  179. {
  180. GamepadAxis::Enum axis = translateGamepadAxis(ii);
  181. int32_t value = (int32_t) (axes[ii] * 32768.f);
  182. if (GamepadAxis::LeftY == axis || GamepadAxis::RightY == axis)
  183. {
  184. value = -value;
  185. }
  186. if (m_axes[ii] != value)
  187. {
  188. m_axes[ii] = value;
  189. _eventQueue.postAxisEvent(defaultWindow
  190. , m_handle
  191. , axis
  192. , value);
  193. }
  194. }
  195. for (int ii = 0; ii < numButtons; ++ii)
  196. {
  197. Key::Enum key = translateGamepadButton(ii);
  198. if (m_buttons[ii] != buttons[ii])
  199. {
  200. m_buttons[ii] = buttons[ii];
  201. _eventQueue.postKeyEvent(defaultWindow
  202. , key
  203. , 0
  204. , buttons[ii] != 0);
  205. }
  206. }
  207. }
  208. bool m_connected;
  209. GamepadHandle m_handle;
  210. int32_t m_axes[GamepadAxis::Count];
  211. uint8_t m_buttons[Key::Count - Key::GamepadA];
  212. };
  213. struct MainThreadEntry
  214. {
  215. int m_argc;
  216. const char* const* m_argv;
  217. static int32_t threadFunc(bx::Thread* _thread, void* _userData);
  218. };
  219. enum MsgType
  220. {
  221. GLFW_WINDOW_CREATE,
  222. GLFW_WINDOW_DESTROY,
  223. GLFW_WINDOW_SET_TITLE,
  224. GLFW_WINDOW_SET_POS,
  225. GLFW_WINDOW_SET_SIZE,
  226. GLFW_WINDOW_TOGGLE_FRAME,
  227. GLFW_WINDOW_TOGGLE_FULL_SCREEN,
  228. GLFW_WINDOW_MOUSE_LOCK,
  229. };
  230. struct Msg
  231. {
  232. Msg(MsgType _type)
  233. : m_type(_type)
  234. , m_x(0)
  235. , m_y(0)
  236. , m_width(0)
  237. , m_height(0)
  238. , m_value(false)
  239. {
  240. }
  241. MsgType m_type;
  242. int32_t m_x;
  243. int32_t m_y;
  244. uint32_t m_width;
  245. uint32_t m_height;
  246. uint32_t m_flags;
  247. bool m_value;
  248. tinystl::string m_title;
  249. WindowHandle m_handle;
  250. };
  251. static void errorCb(int _error, const char* _description)
  252. {
  253. DBG("GLFW error %d: %s", _error, _description);
  254. }
  255. static void joystickCb(int _jid, int _action);
  256. // Based on cutef8 by Jeff Bezanson (Public Domain)
  257. static uint8_t encodeUTF8(uint8_t _chars[4], uint32_t _scancode)
  258. {
  259. uint8_t length = 0;
  260. if (_scancode < 0x80)
  261. {
  262. _chars[length++] = (char) _scancode;
  263. }
  264. else if (_scancode < 0x800)
  265. {
  266. _chars[length++] = (_scancode >> 6) | 0xc0;
  267. _chars[length++] = (_scancode & 0x3f) | 0x80;
  268. }
  269. else if (_scancode < 0x10000)
  270. {
  271. _chars[length++] = (_scancode >> 12) | 0xe0;
  272. _chars[length++] = ((_scancode >> 6) & 0x3f) | 0x80;
  273. _chars[length++] = (_scancode & 0x3f) | 0x80;
  274. }
  275. else if (_scancode < 0x110000)
  276. {
  277. _chars[length++] = (_scancode >> 18) | 0xf0;
  278. _chars[length++] = ((_scancode >> 12) & 0x3f) | 0x80;
  279. _chars[length++] = ((_scancode >> 6) & 0x3f) | 0x80;
  280. _chars[length++] = (_scancode & 0x3f) | 0x80;
  281. }
  282. return length;
  283. }
  284. struct Context
  285. {
  286. Context()
  287. : m_msgs(getAllocator() )
  288. , m_scrollPos(0.0f)
  289. {
  290. bx::memSet(s_translateKey, 0, sizeof(s_translateKey));
  291. s_translateKey[GLFW_KEY_ESCAPE] = Key::Esc;
  292. s_translateKey[GLFW_KEY_ENTER] = Key::Return;
  293. s_translateKey[GLFW_KEY_TAB] = Key::Tab;
  294. s_translateKey[GLFW_KEY_BACKSPACE] = Key::Backspace;
  295. s_translateKey[GLFW_KEY_SPACE] = Key::Space;
  296. s_translateKey[GLFW_KEY_UP] = Key::Up;
  297. s_translateKey[GLFW_KEY_DOWN] = Key::Down;
  298. s_translateKey[GLFW_KEY_LEFT] = Key::Left;
  299. s_translateKey[GLFW_KEY_RIGHT] = Key::Right;
  300. s_translateKey[GLFW_KEY_PAGE_UP] = Key::PageUp;
  301. s_translateKey[GLFW_KEY_PAGE_DOWN] = Key::PageDown;
  302. s_translateKey[GLFW_KEY_HOME] = Key::Home;
  303. s_translateKey[GLFW_KEY_END] = Key::End;
  304. s_translateKey[GLFW_KEY_PRINT_SCREEN] = Key::Print;
  305. s_translateKey[GLFW_KEY_KP_ADD] = Key::Plus;
  306. s_translateKey[GLFW_KEY_EQUAL] = Key::Plus;
  307. s_translateKey[GLFW_KEY_KP_SUBTRACT] = Key::Minus;
  308. s_translateKey[GLFW_KEY_MINUS] = Key::Minus;
  309. s_translateKey[GLFW_KEY_COMMA] = Key::Comma;
  310. s_translateKey[GLFW_KEY_PERIOD] = Key::Period;
  311. s_translateKey[GLFW_KEY_SLASH] = Key::Slash;
  312. s_translateKey[GLFW_KEY_F1] = Key::F1;
  313. s_translateKey[GLFW_KEY_F2] = Key::F2;
  314. s_translateKey[GLFW_KEY_F3] = Key::F3;
  315. s_translateKey[GLFW_KEY_F4] = Key::F4;
  316. s_translateKey[GLFW_KEY_F5] = Key::F5;
  317. s_translateKey[GLFW_KEY_F6] = Key::F6;
  318. s_translateKey[GLFW_KEY_F7] = Key::F7;
  319. s_translateKey[GLFW_KEY_F8] = Key::F8;
  320. s_translateKey[GLFW_KEY_F9] = Key::F9;
  321. s_translateKey[GLFW_KEY_F10] = Key::F10;
  322. s_translateKey[GLFW_KEY_F11] = Key::F11;
  323. s_translateKey[GLFW_KEY_F12] = Key::F12;
  324. s_translateKey[GLFW_KEY_KP_0] = Key::NumPad0;
  325. s_translateKey[GLFW_KEY_KP_1] = Key::NumPad1;
  326. s_translateKey[GLFW_KEY_KP_2] = Key::NumPad2;
  327. s_translateKey[GLFW_KEY_KP_3] = Key::NumPad3;
  328. s_translateKey[GLFW_KEY_KP_4] = Key::NumPad4;
  329. s_translateKey[GLFW_KEY_KP_5] = Key::NumPad5;
  330. s_translateKey[GLFW_KEY_KP_6] = Key::NumPad6;
  331. s_translateKey[GLFW_KEY_KP_7] = Key::NumPad7;
  332. s_translateKey[GLFW_KEY_KP_8] = Key::NumPad8;
  333. s_translateKey[GLFW_KEY_KP_9] = Key::NumPad9;
  334. s_translateKey[GLFW_KEY_0] = Key::Key0;
  335. s_translateKey[GLFW_KEY_1] = Key::Key1;
  336. s_translateKey[GLFW_KEY_2] = Key::Key2;
  337. s_translateKey[GLFW_KEY_3] = Key::Key3;
  338. s_translateKey[GLFW_KEY_4] = Key::Key4;
  339. s_translateKey[GLFW_KEY_5] = Key::Key5;
  340. s_translateKey[GLFW_KEY_6] = Key::Key6;
  341. s_translateKey[GLFW_KEY_7] = Key::Key7;
  342. s_translateKey[GLFW_KEY_8] = Key::Key8;
  343. s_translateKey[GLFW_KEY_9] = Key::Key9;
  344. s_translateKey[GLFW_KEY_A] = Key::KeyA;
  345. s_translateKey[GLFW_KEY_B] = Key::KeyB;
  346. s_translateKey[GLFW_KEY_C] = Key::KeyC;
  347. s_translateKey[GLFW_KEY_D] = Key::KeyD;
  348. s_translateKey[GLFW_KEY_E] = Key::KeyE;
  349. s_translateKey[GLFW_KEY_F] = Key::KeyF;
  350. s_translateKey[GLFW_KEY_G] = Key::KeyG;
  351. s_translateKey[GLFW_KEY_H] = Key::KeyH;
  352. s_translateKey[GLFW_KEY_I] = Key::KeyI;
  353. s_translateKey[GLFW_KEY_J] = Key::KeyJ;
  354. s_translateKey[GLFW_KEY_K] = Key::KeyK;
  355. s_translateKey[GLFW_KEY_L] = Key::KeyL;
  356. s_translateKey[GLFW_KEY_M] = Key::KeyM;
  357. s_translateKey[GLFW_KEY_N] = Key::KeyN;
  358. s_translateKey[GLFW_KEY_O] = Key::KeyO;
  359. s_translateKey[GLFW_KEY_P] = Key::KeyP;
  360. s_translateKey[GLFW_KEY_Q] = Key::KeyQ;
  361. s_translateKey[GLFW_KEY_R] = Key::KeyR;
  362. s_translateKey[GLFW_KEY_S] = Key::KeyS;
  363. s_translateKey[GLFW_KEY_T] = Key::KeyT;
  364. s_translateKey[GLFW_KEY_U] = Key::KeyU;
  365. s_translateKey[GLFW_KEY_V] = Key::KeyV;
  366. s_translateKey[GLFW_KEY_W] = Key::KeyW;
  367. s_translateKey[GLFW_KEY_X] = Key::KeyX;
  368. s_translateKey[GLFW_KEY_Y] = Key::KeyY;
  369. s_translateKey[GLFW_KEY_Z] = Key::KeyZ;
  370. }
  371. int run(int _argc, const char* const* _argv)
  372. {
  373. m_mte.m_argc = _argc;
  374. m_mte.m_argv = _argv;
  375. glfwSetErrorCallback(errorCb);
  376. if (!glfwInit() )
  377. {
  378. DBG("glfwInit failed!");
  379. return bx::kExitFailure;
  380. }
  381. glfwSetJoystickCallback(joystickCb);
  382. glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
  383. WindowHandle handle = { m_windowAlloc.alloc() };
  384. m_window[0] = glfwCreateWindow(ENTRY_DEFAULT_WIDTH
  385. , ENTRY_DEFAULT_HEIGHT
  386. , "bgfx"
  387. , NULL
  388. , NULL
  389. );
  390. if (!m_window[0])
  391. {
  392. DBG("glfwCreateWindow failed!");
  393. glfwTerminate();
  394. return bx::kExitFailure;
  395. }
  396. glfwSetKeyCallback(m_window[0], keyCb);
  397. glfwSetCharCallback(m_window[0], charCb);
  398. glfwSetScrollCallback(m_window[0], scrollCb);
  399. glfwSetCursorPosCallback(m_window[0], cursorPosCb);
  400. glfwSetMouseButtonCallback(m_window[0], mouseButtonCb);
  401. glfwSetWindowSizeCallback(m_window[0], windowSizeCb);
  402. glfwSetDropCallback(m_window[0], dropFileCb);
  403. m_eventQueue.postSizeEvent(handle, ENTRY_DEFAULT_WIDTH, ENTRY_DEFAULT_HEIGHT);
  404. for (uint32_t ii = 0; ii < ENTRY_CONFIG_MAX_GAMEPADS; ++ii)
  405. {
  406. m_gamepad[ii].m_handle.idx = ii;
  407. if (glfwJoystickPresent(ii))
  408. {
  409. m_gamepad[ii].m_connected = true;
  410. m_eventQueue.postGamepadEvent(handle
  411. , m_gamepad[ii].m_handle
  412. , true);
  413. }
  414. }
  415. m_thread.init(MainThreadEntry::threadFunc, &m_mte);
  416. while (NULL != m_window[0]
  417. && !glfwWindowShouldClose(m_window[0]))
  418. {
  419. glfwWaitEventsTimeout(0.016);
  420. for (uint32_t ii = 0; ii < ENTRY_CONFIG_MAX_GAMEPADS; ++ii)
  421. {
  422. if (m_gamepad[ii].m_connected)
  423. {
  424. m_gamepad[ii].update(m_eventQueue);
  425. }
  426. }
  427. while (Msg* msg = m_msgs.pop())
  428. {
  429. switch (msg->m_type)
  430. {
  431. case GLFW_WINDOW_CREATE:
  432. {
  433. GLFWwindow* window = glfwCreateWindow(msg->m_width
  434. , msg->m_height
  435. , msg->m_title.c_str()
  436. , NULL
  437. , NULL);
  438. if (!window)
  439. {
  440. break;
  441. }
  442. glfwSetWindowPos(window, msg->m_x, msg->m_y);
  443. if (msg->m_flags & ENTRY_WINDOW_FLAG_ASPECT_RATIO)
  444. {
  445. glfwSetWindowAspectRatio(window, msg->m_width, msg->m_height);
  446. }
  447. glfwSetKeyCallback(window, keyCb);
  448. glfwSetCharCallback(window, charCb);
  449. glfwSetScrollCallback(window, scrollCb);
  450. glfwSetCursorPosCallback(window, cursorPosCb);
  451. glfwSetMouseButtonCallback(window, mouseButtonCb);
  452. glfwSetWindowSizeCallback(window, windowSizeCb);
  453. glfwSetDropCallback(window, dropFileCb);
  454. m_window[msg->m_handle.idx] = window;
  455. m_eventQueue.postSizeEvent(msg->m_handle, msg->m_width, msg->m_height);
  456. m_eventQueue.postWindowEvent(msg->m_handle, glfwNativeWindowHandle(window));
  457. }
  458. break;
  459. case GLFW_WINDOW_DESTROY:
  460. {
  461. if (isValid(msg->m_handle) )
  462. {
  463. GLFWwindow* window = m_window[msg->m_handle.idx];
  464. m_eventQueue.postWindowEvent(msg->m_handle);
  465. glfwDestroyWindowImpl(window);
  466. m_window[msg->m_handle.idx] = NULL;
  467. }
  468. }
  469. break;
  470. case GLFW_WINDOW_SET_TITLE:
  471. {
  472. GLFWwindow* window = m_window[msg->m_handle.idx];
  473. glfwSetWindowTitle(window, msg->m_title.c_str());
  474. }
  475. break;
  476. case GLFW_WINDOW_SET_POS:
  477. {
  478. GLFWwindow* window = m_window[msg->m_handle.idx];
  479. glfwSetWindowPos(window, msg->m_x, msg->m_y);
  480. }
  481. break;
  482. case GLFW_WINDOW_SET_SIZE:
  483. {
  484. GLFWwindow* window = m_window[msg->m_handle.idx];
  485. glfwSetWindowSize(window, msg->m_width, msg->m_height);
  486. }
  487. break;
  488. case GLFW_WINDOW_TOGGLE_FRAME:
  489. {
  490. // Wait for glfwSetWindowDecorated to exist
  491. }
  492. break;
  493. case GLFW_WINDOW_TOGGLE_FULL_SCREEN:
  494. {
  495. GLFWwindow* window = m_window[msg->m_handle.idx];
  496. if (glfwGetWindowMonitor(window) )
  497. {
  498. glfwSetWindowMonitor(window
  499. , NULL
  500. , m_oldX
  501. , m_oldY
  502. , m_oldWidth
  503. , m_oldHeight
  504. , 0
  505. );
  506. }
  507. else
  508. {
  509. GLFWmonitor* monitor = glfwGetPrimaryMonitor();
  510. if (NULL != monitor)
  511. {
  512. glfwGetWindowPos(window, &m_oldX, &m_oldY);
  513. glfwGetWindowSize(window, &m_oldWidth, &m_oldHeight);
  514. const GLFWvidmode* mode = glfwGetVideoMode(monitor);
  515. glfwSetWindowMonitor(window
  516. , monitor
  517. , 0
  518. , 0
  519. , mode->width
  520. , mode->height
  521. , mode->refreshRate
  522. );
  523. }
  524. }
  525. }
  526. break;
  527. case GLFW_WINDOW_MOUSE_LOCK:
  528. {
  529. GLFWwindow* window = m_window[msg->m_handle.idx];
  530. if (msg->m_value)
  531. {
  532. glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
  533. }
  534. else
  535. {
  536. glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
  537. }
  538. }
  539. break;
  540. }
  541. delete msg;
  542. }
  543. }
  544. m_eventQueue.postExitEvent();
  545. m_thread.shutdown();
  546. glfwDestroyWindowImpl(m_window[0]);
  547. glfwTerminate();
  548. return m_thread.getExitCode();
  549. }
  550. WindowHandle findHandle(GLFWwindow* _window)
  551. {
  552. bx::MutexScope scope(m_lock);
  553. for (uint32_t ii = 0, num = m_windowAlloc.getNumHandles(); ii < num; ++ii)
  554. {
  555. uint16_t idx = m_windowAlloc.getHandleAt(ii);
  556. if (_window == m_window[idx])
  557. {
  558. WindowHandle handle = { idx };
  559. return handle;
  560. }
  561. }
  562. WindowHandle invalid = { UINT16_MAX };
  563. return invalid;
  564. }
  565. static void keyCb(GLFWwindow* _window, int32_t _key, int32_t _scancode, int32_t _action, int32_t _mods);
  566. static void charCb(GLFWwindow* _window, uint32_t _scancode);
  567. static void scrollCb(GLFWwindow* _window, double _dx, double _dy);
  568. static void cursorPosCb(GLFWwindow* _window, double _mx, double _my);
  569. static void mouseButtonCb(GLFWwindow* _window, int32_t _button, int32_t _action, int32_t _mods);
  570. static void windowSizeCb(GLFWwindow* _window, int32_t _width, int32_t _height);
  571. static void dropFileCb(GLFWwindow* _window, int32_t _count, const char** _filePaths);
  572. MainThreadEntry m_mte;
  573. bx::Thread m_thread;
  574. EventQueue m_eventQueue;
  575. bx::Mutex m_lock;
  576. GLFWwindow* m_window[ENTRY_CONFIG_MAX_WINDOWS];
  577. bx::HandleAllocT<ENTRY_CONFIG_MAX_WINDOWS> m_windowAlloc;
  578. GamepadGLFW m_gamepad[ENTRY_CONFIG_MAX_GAMEPADS];
  579. bx::SpScUnboundedQueueT<Msg> m_msgs;
  580. int32_t m_oldX;
  581. int32_t m_oldY;
  582. int32_t m_oldWidth;
  583. int32_t m_oldHeight;
  584. double m_scrollPos;
  585. };
  586. Context s_ctx;
  587. void Context::keyCb(GLFWwindow* _window, int32_t _key, int32_t _scancode, int32_t _action, int32_t _mods)
  588. {
  589. BX_UNUSED(_scancode);
  590. if (_key == GLFW_KEY_UNKNOWN)
  591. {
  592. return;
  593. }
  594. WindowHandle handle = s_ctx.findHandle(_window);
  595. int mods = translateKeyModifiers(_mods);
  596. Key::Enum key = translateKey(_key);
  597. bool down = (_action == GLFW_PRESS || _action == GLFW_REPEAT);
  598. s_ctx.m_eventQueue.postKeyEvent(handle, key, mods, down);
  599. }
  600. void Context::charCb(GLFWwindow* _window, uint32_t _scancode)
  601. {
  602. WindowHandle handle = s_ctx.findHandle(_window);
  603. uint8_t chars[4];
  604. uint8_t length = encodeUTF8(chars, _scancode);
  605. if (!length)
  606. {
  607. return;
  608. }
  609. s_ctx.m_eventQueue.postCharEvent(handle, length, chars);
  610. }
  611. void Context::scrollCb(GLFWwindow* _window, double _dx, double _dy)
  612. {
  613. BX_UNUSED(_dx);
  614. WindowHandle handle = s_ctx.findHandle(_window);
  615. double mx, my;
  616. glfwGetCursorPos(_window, &mx, &my);
  617. s_ctx.m_scrollPos += _dy;
  618. s_ctx.m_eventQueue.postMouseEvent(handle
  619. , (int32_t) mx
  620. , (int32_t) my
  621. , (int32_t) s_ctx.m_scrollPos
  622. );
  623. }
  624. void Context::cursorPosCb(GLFWwindow* _window, double _mx, double _my)
  625. {
  626. WindowHandle handle = s_ctx.findHandle(_window);
  627. s_ctx.m_eventQueue.postMouseEvent(handle
  628. , (int32_t) _mx
  629. , (int32_t) _my
  630. , (int32_t) s_ctx.m_scrollPos
  631. );
  632. }
  633. void Context::mouseButtonCb(GLFWwindow* _window, int32_t _button, int32_t _action, int32_t _mods)
  634. {
  635. BX_UNUSED(_mods);
  636. WindowHandle handle = s_ctx.findHandle(_window);
  637. bool down = _action == GLFW_PRESS;
  638. double mx, my;
  639. glfwGetCursorPos(_window, &mx, &my);
  640. s_ctx.m_eventQueue.postMouseEvent(handle
  641. , (int32_t) mx
  642. , (int32_t) my
  643. , (int32_t) s_ctx.m_scrollPos
  644. , translateMouseButton(_button)
  645. , down
  646. );
  647. }
  648. void Context::windowSizeCb(GLFWwindow* _window, int32_t _width, int32_t _height)
  649. {
  650. WindowHandle handle = s_ctx.findHandle(_window);
  651. s_ctx.m_eventQueue.postSizeEvent(handle, _width, _height);
  652. }
  653. void Context::dropFileCb(GLFWwindow* _window, int32_t _count, const char** _filePaths)
  654. {
  655. WindowHandle handle = s_ctx.findHandle(_window);
  656. for (int32_t ii = 0; ii < _count; ++ii)
  657. {
  658. s_ctx.m_eventQueue.postDropFileEvent(handle, _filePaths[ii]);
  659. }
  660. }
  661. static void joystickCb(int _jid, int _action)
  662. {
  663. if (_jid >= ENTRY_CONFIG_MAX_GAMEPADS)
  664. {
  665. return;
  666. }
  667. WindowHandle defaultWindow = { 0 };
  668. GamepadHandle handle = { (uint16_t) _jid };
  669. if (_action == GLFW_CONNECTED)
  670. {
  671. s_ctx.m_gamepad[_jid].m_connected = true;
  672. s_ctx.m_eventQueue.postGamepadEvent(defaultWindow, handle, true);
  673. }
  674. else if (_action == GLFW_DISCONNECTED)
  675. {
  676. s_ctx.m_gamepad[_jid].m_connected = false;
  677. s_ctx.m_eventQueue.postGamepadEvent(defaultWindow, handle, false);
  678. }
  679. }
  680. const Event* poll()
  681. {
  682. return s_ctx.m_eventQueue.poll();
  683. }
  684. const Event* poll(WindowHandle _handle)
  685. {
  686. return s_ctx.m_eventQueue.poll(_handle);
  687. }
  688. void release(const Event* _event)
  689. {
  690. s_ctx.m_eventQueue.release(_event);
  691. }
  692. WindowHandle createWindow(int32_t _x, int32_t _y, uint32_t _width, uint32_t _height, uint32_t _flags, const char* _title)
  693. {
  694. Msg* msg = new Msg(GLFW_WINDOW_CREATE);
  695. msg->m_x = _x;
  696. msg->m_y = _y;
  697. msg->m_width = _width;
  698. msg->m_height = _height;
  699. msg->m_flags = _flags;
  700. msg->m_title = _title;
  701. msg->m_handle.idx = s_ctx.m_windowAlloc.alloc();
  702. s_ctx.m_msgs.push(msg);
  703. return msg->m_handle;
  704. }
  705. void destroyWindow(WindowHandle _handle)
  706. {
  707. Msg* msg = new Msg(GLFW_WINDOW_DESTROY);
  708. msg->m_handle = _handle;
  709. s_ctx.m_msgs.push(msg);
  710. }
  711. void setWindowPos(WindowHandle _handle, int32_t _x, int32_t _y)
  712. {
  713. Msg* msg = new Msg(GLFW_WINDOW_SET_POS);
  714. msg->m_x = _x;
  715. msg->m_y = _y;
  716. msg->m_handle = _handle;
  717. s_ctx.m_msgs.push(msg);
  718. }
  719. void setWindowSize(WindowHandle _handle, uint32_t _width, uint32_t _height)
  720. {
  721. Msg* msg = new Msg(GLFW_WINDOW_SET_SIZE);
  722. msg->m_width = _width;
  723. msg->m_height = _height;
  724. msg->m_handle = _handle;
  725. s_ctx.m_msgs.push(msg);
  726. }
  727. void setWindowTitle(WindowHandle _handle, const char* _title)
  728. {
  729. Msg* msg = new Msg(GLFW_WINDOW_SET_TITLE);
  730. msg->m_title = _title;
  731. msg->m_handle = _handle;
  732. s_ctx.m_msgs.push(msg);
  733. }
  734. void setWindowFlags(WindowHandle _handle, uint32_t _flags, bool _enabled)
  735. {
  736. BX_UNUSED(_handle, _flags, _enabled);
  737. }
  738. void toggleFullscreen(WindowHandle _handle)
  739. {
  740. Msg* msg = new Msg(GLFW_WINDOW_TOGGLE_FULL_SCREEN);
  741. msg->m_handle = _handle;
  742. s_ctx.m_msgs.push(msg);
  743. }
  744. void setMouseLock(WindowHandle _handle, bool _lock)
  745. {
  746. Msg* msg = new Msg(GLFW_WINDOW_MOUSE_LOCK);
  747. msg->m_value = _lock;
  748. msg->m_handle = _handle;
  749. s_ctx.m_msgs.push(msg);
  750. }
  751. void* getNativeWindowHandle(WindowHandle _handle)
  752. {
  753. return glfwNativeWindowHandle(s_ctx.m_window[_handle.idx]);
  754. }
  755. void* getNativeDisplayHandle()
  756. {
  757. # if BX_PLATFORM_LINUX || BX_PLATFORM_BSD
  758. # if ENTRY_CONFIG_USE_WAYLAND
  759. return glfwGetWaylandDisplay();
  760. # else
  761. return glfwGetX11Display();
  762. # endif // ENTRY_CONFIG_USE_WAYLAND
  763. # else
  764. return NULL;
  765. # endif // BX_PLATFORM_*
  766. }
  767. bgfx::NativeWindowHandleType::Enum getNativeWindowHandleType(WindowHandle _handle)
  768. {
  769. # if BX_PLATFORM_LINUX || BX_PLATFORM_BSD
  770. # if ENTRY_CONFIG_USE_WAYLAND
  771. return bgfx::NativeWindowHandleType::Wayland;
  772. # else
  773. return bgfx::NativeWindowHandleType::Default;
  774. # endif // ENTRY_CONFIG_USE_WAYLAND
  775. # else
  776. return bgfx::NativeWindowHandleType::Default;
  777. # endif // BX_PLATFORM_*
  778. }
  779. int32_t MainThreadEntry::threadFunc(bx::Thread* _thread, void* _userData)
  780. {
  781. BX_UNUSED(_thread);
  782. MainThreadEntry* self = (MainThreadEntry*)_userData;
  783. int32_t result = main(self->m_argc, self->m_argv);
  784. // Destroy main window on exit...
  785. Msg* msg = new Msg(GLFW_WINDOW_DESTROY);
  786. msg->m_handle.idx = 0;
  787. s_ctx.m_msgs.push(msg);
  788. return result;
  789. }
  790. }
  791. int main(int _argc, const char* const* _argv)
  792. {
  793. using namespace entry;
  794. return s_ctx.run(_argc, _argv);
  795. }
  796. #endif // ENTRY_CONFIG_USE_GLFW