entry_glfw.cpp 22 KB

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