entry_glfw.cpp 22 KB

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