entry_glfw.cpp 21 KB

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