entry_x11.cpp 20 KB

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