entry_x11.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  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_NATIVE && (BX_PLATFORM_BSD || BX_PLATFORM_LINUX || BX_PLATFORM_RPI)
  7. #define XK_MISCELLANY
  8. #define XK_LATIN1
  9. #include <X11/keysymdef.h>
  10. #include <X11/Xlib.h> // will include X11 which #defines None... Don't mess with order of includes.
  11. #include <X11/Xutil.h>
  12. #include <bgfx/platform.h>
  13. #include <unistd.h> // syscall
  14. #undef None
  15. #include <bx/thread.h>
  16. #include <bx/os.h>
  17. #include <bx/handlealloc.h>
  18. #include <bx/mutex.h>
  19. #include <string>
  20. #include <fcntl.h>
  21. namespace entry
  22. {
  23. static const char* s_applicationName = "BGFX";
  24. static const char* s_applicationClass = "bgfx";
  25. ///
  26. inline void x11SetDisplayWindow(void* _display, uint32_t _window, void* _glx = NULL)
  27. {
  28. bgfx::PlatformData pd;
  29. pd.ndt = _display;
  30. pd.nwh = (void*)(uintptr_t)_window;
  31. pd.context = _glx;
  32. pd.backBuffer = NULL;
  33. pd.backBufferDS = NULL;
  34. bgfx::setPlatformData(pd);
  35. }
  36. #define JS_EVENT_BUTTON 0x01 /* button pressed/released */
  37. #define JS_EVENT_AXIS 0x02 /* joystick moved */
  38. #define JS_EVENT_INIT 0x80 /* initial state of device */
  39. struct JoystickEvent
  40. {
  41. uint32_t time; /* event timestamp in milliseconds */
  42. int16_t value; /* value */
  43. uint8_t type; /* event type */
  44. uint8_t number; /* axis/button number */
  45. };
  46. static Key::Enum s_translateButton[] =
  47. {
  48. Key::GamepadA,
  49. Key::GamepadB,
  50. Key::GamepadX,
  51. Key::GamepadY,
  52. Key::GamepadShoulderL,
  53. Key::GamepadShoulderR,
  54. Key::GamepadBack,
  55. Key::GamepadStart,
  56. Key::GamepadGuide,
  57. Key::GamepadThumbL,
  58. Key::GamepadThumbR,
  59. };
  60. static GamepadAxis::Enum s_translateAxis[] =
  61. {
  62. GamepadAxis::LeftX,
  63. GamepadAxis::LeftY,
  64. GamepadAxis::LeftZ,
  65. GamepadAxis::RightX,
  66. GamepadAxis::RightY,
  67. GamepadAxis::RightZ,
  68. };
  69. struct AxisDpadRemap
  70. {
  71. Key::Enum first;
  72. Key::Enum second;
  73. };
  74. static AxisDpadRemap s_axisDpad[] =
  75. {
  76. { Key::GamepadLeft, Key::GamepadRight },
  77. { Key::GamepadUp, Key::GamepadDown },
  78. { Key::None, Key::None },
  79. { Key::GamepadLeft, Key::GamepadRight },
  80. { Key::GamepadUp, Key::GamepadDown },
  81. { Key::None, Key::None },
  82. };
  83. BX_STATIC_ASSERT(BX_COUNTOF(s_translateAxis) == BX_COUNTOF(s_axisDpad) );
  84. struct Joystick
  85. {
  86. Joystick()
  87. : m_fd(-1)
  88. {
  89. }
  90. void init()
  91. {
  92. m_fd = open("/dev/input/js0", O_RDONLY | O_NONBLOCK);
  93. bx::memSet(m_value, 0, sizeof(m_value) );
  94. // Deadzone values from xinput.h
  95. m_deadzone[GamepadAxis::LeftX ] =
  96. m_deadzone[GamepadAxis::LeftY ] = 7849;
  97. m_deadzone[GamepadAxis::RightX] =
  98. m_deadzone[GamepadAxis::RightY] = 8689;
  99. m_deadzone[GamepadAxis::LeftZ ] =
  100. m_deadzone[GamepadAxis::RightZ] = 30;
  101. }
  102. void shutdown()
  103. {
  104. if (-1 != m_fd)
  105. {
  106. close(m_fd);
  107. }
  108. }
  109. bool filter(GamepadAxis::Enum _axis, int32_t* _value)
  110. {
  111. const int32_t old = m_value[_axis];
  112. const int32_t deadzone = m_deadzone[_axis];
  113. int32_t value = *_value;
  114. value = value > deadzone || value < -deadzone ? value : 0;
  115. m_value[_axis] = value;
  116. *_value = value;
  117. return old != value;
  118. }
  119. bool update(EventQueue& _eventQueue)
  120. {
  121. if (-1 == m_fd)
  122. {
  123. return false;
  124. }
  125. JoystickEvent event;
  126. int32_t bytes = read(m_fd, &event, sizeof(JoystickEvent) );
  127. if (bytes != sizeof(JoystickEvent) )
  128. {
  129. return false;
  130. }
  131. WindowHandle defaultWindow = { 0 };
  132. GamepadHandle handle = { 0 };
  133. if (event.type & JS_EVENT_BUTTON)
  134. {
  135. if (event.number < BX_COUNTOF(s_translateButton) )
  136. {
  137. _eventQueue.postKeyEvent(defaultWindow, s_translateButton[event.number], 0, 0 != event.value);
  138. }
  139. }
  140. else if (event.type & JS_EVENT_AXIS)
  141. {
  142. if (event.number < BX_COUNTOF(s_translateAxis) )
  143. {
  144. GamepadAxis::Enum axis = s_translateAxis[event.number];
  145. int32_t value = event.value;
  146. if (filter(axis, &value) )
  147. {
  148. _eventQueue.postAxisEvent(defaultWindow, handle, axis, value);
  149. if (Key::None != s_axisDpad[axis].first)
  150. {
  151. if (m_value[axis] == 0)
  152. {
  153. _eventQueue.postKeyEvent(defaultWindow, s_axisDpad[axis].first, 0, false);
  154. _eventQueue.postKeyEvent(defaultWindow, s_axisDpad[axis].second, 0, false);
  155. }
  156. else
  157. {
  158. _eventQueue.postKeyEvent(defaultWindow
  159. , 0 > m_value[axis] ? s_axisDpad[axis].first : s_axisDpad[axis].second
  160. , 0
  161. , true
  162. );
  163. }
  164. }
  165. }
  166. }
  167. }
  168. return true;
  169. }
  170. int m_fd;
  171. int32_t m_value[GamepadAxis::Count];
  172. int32_t m_deadzone[GamepadAxis::Count];
  173. };
  174. static Joystick s_joystick;
  175. static uint8_t s_translateKey[512];
  176. static void initTranslateKey(uint16_t _xk, Key::Enum _key)
  177. {
  178. _xk += 256;
  179. BX_CHECK(_xk < BX_COUNTOF(s_translateKey), "Out of bounds %d.", _xk);
  180. s_translateKey[_xk&0x1ff] = (uint8_t)_key;
  181. }
  182. Key::Enum fromXk(uint16_t _xk)
  183. {
  184. _xk += 256;
  185. return 512 > _xk ? (Key::Enum)s_translateKey[_xk] : Key::None;
  186. }
  187. struct MainThreadEntry
  188. {
  189. int32_t m_argc;
  190. const char* const* m_argv;
  191. static int32_t threadFunc(bx::Thread* _thread, void* _userData);
  192. };
  193. struct Msg
  194. {
  195. Msg()
  196. : m_x(0)
  197. , m_y(0)
  198. , m_width(0)
  199. , m_height(0)
  200. , m_flags(0)
  201. {
  202. }
  203. int32_t m_x;
  204. int32_t m_y;
  205. uint32_t m_width;
  206. uint32_t m_height;
  207. uint32_t m_flags;
  208. std::string m_title;
  209. };
  210. struct Context
  211. {
  212. Context()
  213. : m_modifiers(Modifier::None)
  214. , m_exit(false)
  215. {
  216. bx::memSet(s_translateKey, 0, sizeof(s_translateKey) );
  217. initTranslateKey(XK_Escape, Key::Esc);
  218. initTranslateKey(XK_Return, Key::Return);
  219. initTranslateKey(XK_Tab, Key::Tab);
  220. initTranslateKey(XK_BackSpace, Key::Backspace);
  221. initTranslateKey(XK_space, Key::Space);
  222. initTranslateKey(XK_Up, Key::Up);
  223. initTranslateKey(XK_Down, Key::Down);
  224. initTranslateKey(XK_Left, Key::Left);
  225. initTranslateKey(XK_Right, Key::Right);
  226. initTranslateKey(XK_Insert, Key::Insert);
  227. initTranslateKey(XK_Delete, Key::Delete);
  228. initTranslateKey(XK_Home, Key::Home);
  229. initTranslateKey(XK_KP_End, Key::End);
  230. initTranslateKey(XK_Page_Up, Key::PageUp);
  231. initTranslateKey(XK_Page_Down, Key::PageDown);
  232. initTranslateKey(XK_Print, Key::Print);
  233. initTranslateKey(XK_equal, Key::Plus);
  234. initTranslateKey(XK_minus, Key::Minus);
  235. initTranslateKey(XK_bracketleft, Key::LeftBracket);
  236. initTranslateKey(XK_bracketright, Key::RightBracket);
  237. initTranslateKey(XK_semicolon, Key::Semicolon);
  238. initTranslateKey(XK_apostrophe, Key::Quote);
  239. initTranslateKey(XK_comma, Key::Comma);
  240. initTranslateKey(XK_period, Key::Period);
  241. initTranslateKey(XK_slash, Key::Slash);
  242. initTranslateKey(XK_backslash, Key::Backslash);
  243. initTranslateKey(XK_grave, Key::Tilde);
  244. initTranslateKey(XK_F1, Key::F1);
  245. initTranslateKey(XK_F2, Key::F2);
  246. initTranslateKey(XK_F3, Key::F3);
  247. initTranslateKey(XK_F4, Key::F4);
  248. initTranslateKey(XK_F5, Key::F5);
  249. initTranslateKey(XK_F6, Key::F6);
  250. initTranslateKey(XK_F7, Key::F7);
  251. initTranslateKey(XK_F8, Key::F8);
  252. initTranslateKey(XK_F9, Key::F9);
  253. initTranslateKey(XK_F10, Key::F10);
  254. initTranslateKey(XK_F11, Key::F11);
  255. initTranslateKey(XK_F12, Key::F12);
  256. initTranslateKey(XK_KP_Insert, Key::NumPad0);
  257. initTranslateKey(XK_KP_End, Key::NumPad1);
  258. initTranslateKey(XK_KP_Down, Key::NumPad2);
  259. initTranslateKey(XK_KP_Page_Down, Key::NumPad3);
  260. initTranslateKey(XK_KP_Left, Key::NumPad4);
  261. initTranslateKey(XK_KP_Begin, Key::NumPad5);
  262. initTranslateKey(XK_KP_Right, Key::NumPad6);
  263. initTranslateKey(XK_KP_Home, Key::NumPad7);
  264. initTranslateKey(XK_KP_Up, Key::NumPad8);
  265. initTranslateKey(XK_KP_Page_Up, Key::NumPad9);
  266. initTranslateKey('0', Key::Key0);
  267. initTranslateKey('1', Key::Key1);
  268. initTranslateKey('2', Key::Key2);
  269. initTranslateKey('3', Key::Key3);
  270. initTranslateKey('4', Key::Key4);
  271. initTranslateKey('5', Key::Key5);
  272. initTranslateKey('6', Key::Key6);
  273. initTranslateKey('7', Key::Key7);
  274. initTranslateKey('8', Key::Key8);
  275. initTranslateKey('9', Key::Key9);
  276. initTranslateKey('a', Key::KeyA);
  277. initTranslateKey('b', Key::KeyB);
  278. initTranslateKey('c', Key::KeyC);
  279. initTranslateKey('d', Key::KeyD);
  280. initTranslateKey('e', Key::KeyE);
  281. initTranslateKey('f', Key::KeyF);
  282. initTranslateKey('g', Key::KeyG);
  283. initTranslateKey('h', Key::KeyH);
  284. initTranslateKey('i', Key::KeyI);
  285. initTranslateKey('j', Key::KeyJ);
  286. initTranslateKey('k', Key::KeyK);
  287. initTranslateKey('l', Key::KeyL);
  288. initTranslateKey('m', Key::KeyM);
  289. initTranslateKey('n', Key::KeyN);
  290. initTranslateKey('o', Key::KeyO);
  291. initTranslateKey('p', Key::KeyP);
  292. initTranslateKey('q', Key::KeyQ);
  293. initTranslateKey('r', Key::KeyR);
  294. initTranslateKey('s', Key::KeyS);
  295. initTranslateKey('t', Key::KeyT);
  296. initTranslateKey('u', Key::KeyU);
  297. initTranslateKey('v', Key::KeyV);
  298. initTranslateKey('w', Key::KeyW);
  299. initTranslateKey('x', Key::KeyX);
  300. initTranslateKey('y', Key::KeyY);
  301. initTranslateKey('z', Key::KeyZ);
  302. m_mx = 0;
  303. m_my = 0;
  304. m_mz = 0;
  305. }
  306. int32_t run(int _argc, const char* const* _argv)
  307. {
  308. XInitThreads();
  309. m_display = XOpenDisplay(0);
  310. int32_t screen = DefaultScreen(m_display);
  311. m_depth = DefaultDepth(m_display, screen);
  312. m_visual = DefaultVisual(m_display, screen);
  313. m_root = RootWindow(m_display, screen);
  314. bx::memSet(&m_windowAttrs, 0, sizeof(m_windowAttrs) );
  315. m_windowAttrs.background_pixmap = 0;
  316. m_windowAttrs.border_pixel = 0;
  317. m_windowAttrs.event_mask = 0
  318. | ButtonPressMask
  319. | ButtonReleaseMask
  320. | ExposureMask
  321. | KeyPressMask
  322. | KeyReleaseMask
  323. | PointerMotionMask
  324. | StructureNotifyMask
  325. ;
  326. m_windowAlloc.alloc();
  327. m_window[0] = XCreateWindow(m_display
  328. , m_root
  329. , 0, 0
  330. , 1, 1, 0
  331. , m_depth
  332. , InputOutput
  333. , m_visual
  334. , CWBorderPixel|CWEventMask
  335. , &m_windowAttrs
  336. );
  337. // Clear window to black.
  338. XSetWindowAttributes attr;
  339. bx::memSet(&attr, 0, sizeof(attr) );
  340. XChangeWindowAttributes(m_display, m_window[0], CWBackPixel, &attr);
  341. const char* wmDeleteWindowName = "WM_DELETE_WINDOW";
  342. Atom wmDeleteWindow;
  343. XInternAtoms(m_display, (char **)&wmDeleteWindowName, 1, False, &wmDeleteWindow);
  344. XSetWMProtocols(m_display, m_window[0], &wmDeleteWindow, 1);
  345. XMapWindow(m_display, m_window[0]);
  346. XStoreName(m_display, m_window[0], s_applicationName);
  347. XClassHint* hint = XAllocClassHint();
  348. hint->res_name = (char*)s_applicationName;
  349. hint->res_class = (char*)s_applicationClass;
  350. XSetClassHint(m_display, m_window[0], hint);
  351. XFree(hint);
  352. XIM im;
  353. im = XOpenIM(m_display, NULL, NULL, NULL);
  354. XIC ic;
  355. ic = XCreateIC(im
  356. , XNInputStyle
  357. , 0
  358. | XIMPreeditNothing
  359. | XIMStatusNothing
  360. , XNClientWindow
  361. , m_window[0]
  362. , NULL
  363. );
  364. //
  365. x11SetDisplayWindow(m_display, m_window[0]);
  366. MainThreadEntry mte;
  367. mte.m_argc = _argc;
  368. mte.m_argv = _argv;
  369. bx::Thread thread;
  370. thread.init(mte.threadFunc, &mte);
  371. WindowHandle defaultWindow = { 0 };
  372. m_eventQueue.postSizeEvent(defaultWindow, 1, 1);
  373. s_joystick.init();
  374. while (!m_exit)
  375. {
  376. bool joystick = s_joystick.update(m_eventQueue);
  377. bool xpending = XPending(m_display);
  378. if (!xpending)
  379. {
  380. bx::sleep(joystick ? 8 : 16);
  381. }
  382. else
  383. {
  384. XEvent event;
  385. XNextEvent(m_display, &event);
  386. switch (event.type)
  387. {
  388. case Expose:
  389. break;
  390. case ClientMessage:
  391. if ( (Atom)event.xclient.data.l[0] == wmDeleteWindow)
  392. {
  393. m_eventQueue.postExitEvent();
  394. }
  395. break;
  396. case ButtonPress:
  397. case ButtonRelease:
  398. {
  399. const XButtonEvent& xbutton = event.xbutton;
  400. MouseButton::Enum mb = MouseButton::None;
  401. switch (xbutton.button)
  402. {
  403. case Button1: mb = MouseButton::Left; break;
  404. case Button2: mb = MouseButton::Middle; break;
  405. case Button3: mb = MouseButton::Right; break;
  406. case Button4: ++m_mz; break;
  407. case Button5: --m_mz; break;
  408. }
  409. WindowHandle handle = findHandle(xbutton.window);
  410. if (MouseButton::None != mb)
  411. {
  412. m_eventQueue.postMouseEvent(handle
  413. , xbutton.x
  414. , xbutton.y
  415. , m_mz
  416. , mb
  417. , event.type == ButtonPress
  418. );
  419. }
  420. else
  421. {
  422. m_eventQueue.postMouseEvent(handle
  423. , m_mx
  424. , m_my
  425. , m_mz
  426. );
  427. }
  428. }
  429. break;
  430. case MotionNotify:
  431. {
  432. const XMotionEvent& xmotion = event.xmotion;
  433. WindowHandle handle = findHandle(xmotion.window);
  434. m_mx = xmotion.x;
  435. m_my = xmotion.y;
  436. m_eventQueue.postMouseEvent(handle
  437. , m_mx
  438. , m_my
  439. , m_mz
  440. );
  441. }
  442. break;
  443. case KeyPress:
  444. case KeyRelease:
  445. {
  446. XKeyEvent& xkey = event.xkey;
  447. KeySym keysym = XLookupKeysym(&xkey, 0);
  448. switch (keysym)
  449. {
  450. case XK_Meta_L: setModifier(Modifier::LeftMeta, KeyPress == event.type); break;
  451. case XK_Meta_R: setModifier(Modifier::RightMeta, KeyPress == event.type); break;
  452. case XK_Control_L: setModifier(Modifier::LeftCtrl, KeyPress == event.type); break;
  453. case XK_Control_R: setModifier(Modifier::RightCtrl, KeyPress == event.type); break;
  454. case XK_Shift_L: setModifier(Modifier::LeftShift, KeyPress == event.type); break;
  455. case XK_Shift_R: setModifier(Modifier::RightShift, KeyPress == event.type); break;
  456. case XK_Alt_L: setModifier(Modifier::LeftAlt, KeyPress == event.type); break;
  457. case XK_Alt_R: setModifier(Modifier::RightAlt, KeyPress == event.type); break;
  458. default:
  459. {
  460. WindowHandle handle = findHandle(xkey.window);
  461. if (KeyPress == event.type)
  462. {
  463. Status status = 0;
  464. uint8_t utf8[4];
  465. int len = Xutf8LookupString(ic, &xkey, (char*)utf8, sizeof(utf8), &keysym, &status);
  466. switch (status)
  467. {
  468. case XLookupChars:
  469. case XLookupBoth:
  470. if (0 != len)
  471. {
  472. m_eventQueue.postCharEvent(handle, len, utf8);
  473. }
  474. break;
  475. default:
  476. break;
  477. }
  478. }
  479. Key::Enum key = fromXk(keysym);
  480. if (Key::None != key)
  481. {
  482. m_eventQueue.postKeyEvent(handle, key, m_modifiers, KeyPress == event.type);
  483. }
  484. }
  485. break;
  486. }
  487. }
  488. break;
  489. case ConfigureNotify:
  490. {
  491. const XConfigureEvent& xev = event.xconfigure;
  492. WindowHandle handle = findHandle(xev.window);
  493. if (isValid(handle) )
  494. {
  495. m_eventQueue.postSizeEvent(handle, xev.width, xev.height);
  496. }
  497. }
  498. break;
  499. }
  500. }
  501. }
  502. thread.shutdown();
  503. s_joystick.shutdown();
  504. XDestroyIC(ic);
  505. XCloseIM(im);
  506. XUnmapWindow(m_display, m_window[0]);
  507. XDestroyWindow(m_display, m_window[0]);
  508. return thread.getExitCode();
  509. }
  510. void setModifier(Modifier::Enum _modifier, bool _set)
  511. {
  512. m_modifiers &= ~_modifier;
  513. m_modifiers |= _set ? _modifier : 0;
  514. }
  515. void createWindow(WindowHandle _handle, Msg* msg)
  516. {
  517. Window window = XCreateWindow(m_display
  518. , m_root
  519. , msg->m_x
  520. , msg->m_y
  521. , msg->m_width
  522. , msg->m_height
  523. , 0
  524. , m_depth
  525. , InputOutput
  526. , m_visual
  527. , CWBorderPixel|CWEventMask
  528. , &m_windowAttrs
  529. );
  530. m_window[_handle.idx] = window;
  531. // Clear window to black.
  532. XSetWindowAttributes attr;
  533. bx::memSet(&attr, 0, sizeof(attr) );
  534. XChangeWindowAttributes(m_display, window, CWBackPixel, &attr);
  535. const char* wmDeleteWindowName = "WM_DELETE_WINDOW";
  536. Atom wmDeleteWindow;
  537. XInternAtoms(m_display, (char **)&wmDeleteWindowName, 1, False, &wmDeleteWindow);
  538. XSetWMProtocols(m_display, window, &wmDeleteWindow, 1);
  539. XMapWindow(m_display, window);
  540. XStoreName(m_display, window, msg->m_title.c_str() );
  541. XClassHint* hint = XAllocClassHint();
  542. hint->res_name = (char*)msg->m_title.c_str();
  543. hint->res_class = (char*)s_applicationClass;
  544. XSetClassHint(m_display, window, hint);
  545. XFree(hint);
  546. m_eventQueue.postSizeEvent(_handle, msg->m_width, msg->m_height);
  547. union cast
  548. {
  549. void* p;
  550. ::Window w;
  551. };
  552. cast c;
  553. c.w = window;
  554. m_eventQueue.postWindowEvent(_handle, c.p);
  555. delete msg;
  556. }
  557. WindowHandle findHandle(Window _window)
  558. {
  559. bx::MutexScope scope(m_lock);
  560. for (uint32_t ii = 0, num = m_windowAlloc.getNumHandles(); ii < num; ++ii)
  561. {
  562. uint16_t idx = m_windowAlloc.getHandleAt(ii);
  563. if (_window == m_window[idx])
  564. {
  565. WindowHandle handle = { idx };
  566. return handle;
  567. }
  568. }
  569. WindowHandle invalid = { UINT16_MAX };
  570. return invalid;
  571. }
  572. uint8_t m_modifiers;
  573. bool m_exit;
  574. int32_t m_mx;
  575. int32_t m_my;
  576. int32_t m_mz;
  577. EventQueue m_eventQueue;
  578. bx::Mutex m_lock;
  579. bx::HandleAllocT<ENTRY_CONFIG_MAX_WINDOWS> m_windowAlloc;
  580. int32_t m_depth;
  581. Visual* m_visual;
  582. Window m_root;
  583. XSetWindowAttributes m_windowAttrs;
  584. Display* m_display;
  585. Window m_window[ENTRY_CONFIG_MAX_WINDOWS];
  586. uint32_t m_flags[ENTRY_CONFIG_MAX_WINDOWS];
  587. };
  588. static Context s_ctx;
  589. int32_t MainThreadEntry::threadFunc(bx::Thread* _thread, void* _userData)
  590. {
  591. BX_UNUSED(_thread);
  592. MainThreadEntry* self = (MainThreadEntry*)_userData;
  593. int32_t result = main(self->m_argc, self->m_argv);
  594. s_ctx.m_exit = true;
  595. return result;
  596. }
  597. const Event* poll()
  598. {
  599. return s_ctx.m_eventQueue.poll();
  600. }
  601. const Event* poll(WindowHandle _handle)
  602. {
  603. return s_ctx.m_eventQueue.poll(_handle);
  604. }
  605. void release(const Event* _event)
  606. {
  607. s_ctx.m_eventQueue.release(_event);
  608. }
  609. WindowHandle createWindow(int32_t _x, int32_t _y, uint32_t _width, uint32_t _height, uint32_t _flags, const char* _title)
  610. {
  611. bx::MutexScope scope(s_ctx.m_lock);
  612. WindowHandle handle = { s_ctx.m_windowAlloc.alloc() };
  613. if (isValid(handle) )
  614. {
  615. Msg* msg = new Msg;
  616. msg->m_x = _x;
  617. msg->m_y = _y;
  618. msg->m_width = _width;
  619. msg->m_height = _height;
  620. msg->m_title = _title;
  621. msg->m_flags = _flags;
  622. s_ctx.createWindow(handle, msg);
  623. }
  624. return handle;
  625. }
  626. void destroyWindow(WindowHandle _handle)
  627. {
  628. if (isValid(_handle) )
  629. {
  630. s_ctx.m_eventQueue.postWindowEvent(_handle, NULL);
  631. XUnmapWindow(s_ctx.m_display, s_ctx.m_window[_handle.idx]);
  632. XDestroyWindow(s_ctx.m_display, s_ctx.m_window[_handle.idx]);
  633. bx::MutexScope scope(s_ctx.m_lock);
  634. s_ctx.m_windowAlloc.free(_handle.idx);
  635. }
  636. }
  637. void setWindowPos(WindowHandle _handle, int32_t _x, int32_t _y)
  638. {
  639. Display* display = s_ctx.m_display;
  640. Window window = s_ctx.m_window[_handle.idx];
  641. XMoveWindow(display, window, _x, _y);
  642. }
  643. void setWindowSize(WindowHandle _handle, uint32_t _width, uint32_t _height)
  644. {
  645. Display* display = s_ctx.m_display;
  646. Window window = s_ctx.m_window[_handle.idx];
  647. XResizeWindow(display, window, int32_t(_width), int32_t(_height) );
  648. }
  649. void setWindowTitle(WindowHandle _handle, const char* _title)
  650. {
  651. Display* display = s_ctx.m_display;
  652. Window window = s_ctx.m_window[_handle.idx];
  653. XStoreName(display, window, _title);
  654. }
  655. void setWindowFlags(WindowHandle _handle, uint32_t _flags, bool _enabled)
  656. {
  657. BX_UNUSED(_handle, _flags, _enabled);
  658. }
  659. void toggleFullscreen(WindowHandle _handle)
  660. {
  661. BX_UNUSED(_handle);
  662. }
  663. void setMouseLock(WindowHandle _handle, bool _lock)
  664. {
  665. BX_UNUSED(_handle, _lock);
  666. }
  667. } // namespace entry
  668. int main(int _argc, const char* const* _argv)
  669. {
  670. using namespace entry;
  671. return s_ctx.run(_argc, _argv);
  672. }
  673. #endif // ENTRY_CONFIG_USE_NATIVE && (BX_PLATFORM_BSD || BX_PLATFORM_LINUX || BX_PLATFORM_RPI)