entry_glfw.cpp 23 KB

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