entry_glfw.cpp 22 KB

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