entry_x11.cpp 20 KB

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