main_linux.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. /*
  2. * Copyright (c) 2012-2018 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/dbartolini/crown/blob/master/LICENSE
  4. */
  5. #include "config.h"
  6. #if CROWN_PLATFORM_LINUX
  7. #include "core/command_line.h"
  8. #include "core/containers/array.h"
  9. #include "core/os.h"
  10. #include "core/thread/thread.h"
  11. #include "core/unit_tests.h"
  12. #include "device/device.h"
  13. #include "device/device_event_queue.h"
  14. #include "device/display.h"
  15. #include "device/window.h"
  16. #include "resource/data_compiler.h"
  17. #include <bgfx/platform.h>
  18. #include <fcntl.h> // O_RDONLY, ...
  19. #include <stdlib.h>
  20. #include <string.h> // memset
  21. #include <unistd.h> // close
  22. #include <X11/extensions/Xrandr.h>
  23. #include <X11/Xatom.h>
  24. #include <X11/XKBlib.h>
  25. #include <X11/Xlib.h>
  26. #include <X11/Xutil.h>
  27. namespace crown
  28. {
  29. static KeyboardButton::Enum x11_translate_key(KeySym x11_key)
  30. {
  31. switch (x11_key)
  32. {
  33. case XK_BackSpace: return KeyboardButton::BACKSPACE;
  34. case XK_Tab: return KeyboardButton::TAB;
  35. case XK_space: return KeyboardButton::SPACE;
  36. case XK_Escape: return KeyboardButton::ESCAPE;
  37. case XK_Return: return KeyboardButton::ENTER;
  38. case XK_F1: return KeyboardButton::F1;
  39. case XK_F2: return KeyboardButton::F2;
  40. case XK_F3: return KeyboardButton::F3;
  41. case XK_F4: return KeyboardButton::F4;
  42. case XK_F5: return KeyboardButton::F5;
  43. case XK_F6: return KeyboardButton::F6;
  44. case XK_F7: return KeyboardButton::F7;
  45. case XK_F8: return KeyboardButton::F8;
  46. case XK_F9: return KeyboardButton::F9;
  47. case XK_F10: return KeyboardButton::F10;
  48. case XK_F11: return KeyboardButton::F11;
  49. case XK_F12: return KeyboardButton::F12;
  50. case XK_Home: return KeyboardButton::HOME;
  51. case XK_Left: return KeyboardButton::LEFT;
  52. case XK_Up: return KeyboardButton::UP;
  53. case XK_Right: return KeyboardButton::RIGHT;
  54. case XK_Down: return KeyboardButton::DOWN;
  55. case XK_Page_Up: return KeyboardButton::PAGE_UP;
  56. case XK_Page_Down: return KeyboardButton::PAGE_DOWN;
  57. case XK_Insert: return KeyboardButton::INS;
  58. case XK_Delete: return KeyboardButton::DEL;
  59. case XK_End: return KeyboardButton::END;
  60. case XK_Shift_L: return KeyboardButton::SHIFT_LEFT;
  61. case XK_Shift_R: return KeyboardButton::SHIFT_RIGHT;
  62. case XK_Control_L: return KeyboardButton::CTRL_LEFT;
  63. case XK_Control_R: return KeyboardButton::CTRL_RIGHT;
  64. case XK_Caps_Lock: return KeyboardButton::CAPS_LOCK;
  65. case XK_Alt_L: return KeyboardButton::ALT_LEFT;
  66. case XK_Alt_R: return KeyboardButton::ALT_RIGHT;
  67. case XK_Super_L: return KeyboardButton::SUPER_LEFT;
  68. case XK_Super_R: return KeyboardButton::SUPER_RIGHT;
  69. case XK_Num_Lock: return KeyboardButton::NUM_LOCK;
  70. case XK_KP_Enter: return KeyboardButton::NUMPAD_ENTER;
  71. case XK_KP_Delete: return KeyboardButton::NUMPAD_DELETE;
  72. case XK_KP_Multiply: return KeyboardButton::NUMPAD_MULTIPLY;
  73. case XK_KP_Add: return KeyboardButton::NUMPAD_ADD;
  74. case XK_KP_Subtract: return KeyboardButton::NUMPAD_SUBTRACT;
  75. case XK_KP_Divide: return KeyboardButton::NUMPAD_DIVIDE;
  76. case XK_KP_Insert:
  77. case XK_KP_0: return KeyboardButton::NUMPAD_0;
  78. case XK_KP_End:
  79. case XK_KP_1: return KeyboardButton::NUMPAD_1;
  80. case XK_KP_Down:
  81. case XK_KP_2: return KeyboardButton::NUMPAD_2;
  82. case XK_KP_Page_Down: // or XK_KP_Next
  83. case XK_KP_3: return KeyboardButton::NUMPAD_3;
  84. case XK_KP_Left:
  85. case XK_KP_4: return KeyboardButton::NUMPAD_4;
  86. case XK_KP_Begin:
  87. case XK_KP_5: return KeyboardButton::NUMPAD_5;
  88. case XK_KP_Right:
  89. case XK_KP_6: return KeyboardButton::NUMPAD_6;
  90. case XK_KP_Home:
  91. case XK_KP_7: return KeyboardButton::NUMPAD_7;
  92. case XK_KP_Up:
  93. case XK_KP_8: return KeyboardButton::NUMPAD_8;
  94. case XK_KP_Page_Up: // or XK_KP_Prior
  95. case XK_KP_9: return KeyboardButton::NUMPAD_9;
  96. case '0': return KeyboardButton::NUMBER_0;
  97. case '1': return KeyboardButton::NUMBER_1;
  98. case '2': return KeyboardButton::NUMBER_2;
  99. case '3': return KeyboardButton::NUMBER_3;
  100. case '4': return KeyboardButton::NUMBER_4;
  101. case '5': return KeyboardButton::NUMBER_5;
  102. case '6': return KeyboardButton::NUMBER_6;
  103. case '7': return KeyboardButton::NUMBER_7;
  104. case '8': return KeyboardButton::NUMBER_8;
  105. case '9': return KeyboardButton::NUMBER_9;
  106. case 'a': return KeyboardButton::A;
  107. case 'b': return KeyboardButton::B;
  108. case 'c': return KeyboardButton::C;
  109. case 'd': return KeyboardButton::D;
  110. case 'e': return KeyboardButton::E;
  111. case 'f': return KeyboardButton::F;
  112. case 'g': return KeyboardButton::G;
  113. case 'h': return KeyboardButton::H;
  114. case 'i': return KeyboardButton::I;
  115. case 'j': return KeyboardButton::J;
  116. case 'k': return KeyboardButton::K;
  117. case 'l': return KeyboardButton::L;
  118. case 'm': return KeyboardButton::M;
  119. case 'n': return KeyboardButton::N;
  120. case 'o': return KeyboardButton::O;
  121. case 'p': return KeyboardButton::P;
  122. case 'q': return KeyboardButton::Q;
  123. case 'r': return KeyboardButton::R;
  124. case 's': return KeyboardButton::S;
  125. case 't': return KeyboardButton::T;
  126. case 'u': return KeyboardButton::U;
  127. case 'v': return KeyboardButton::V;
  128. case 'w': return KeyboardButton::W;
  129. case 'x': return KeyboardButton::X;
  130. case 'y': return KeyboardButton::Y;
  131. case 'z': return KeyboardButton::Z;
  132. default: return KeyboardButton::COUNT;
  133. }
  134. }
  135. #define JS_EVENT_BUTTON 0x01 /* button pressed/released */
  136. #define JS_EVENT_AXIS 0x02 /* joystick moved */
  137. #define JS_EVENT_INIT 0x80 /* initial state of device */
  138. static u8 s_button[] =
  139. {
  140. JoypadButton::A,
  141. JoypadButton::B,
  142. JoypadButton::X,
  143. JoypadButton::Y,
  144. JoypadButton::SHOULDER_LEFT,
  145. JoypadButton::SHOULDER_RIGHT,
  146. JoypadButton::BACK,
  147. JoypadButton::START,
  148. JoypadButton::GUIDE,
  149. JoypadButton::THUMB_LEFT,
  150. JoypadButton::THUMB_RIGHT,
  151. JoypadButton::UP, // FIXME (reported as axis...)
  152. JoypadButton::DOWN,
  153. JoypadButton::LEFT,
  154. JoypadButton::RIGHT
  155. };
  156. struct JoypadEvent
  157. {
  158. u32 time; /* event timestamp in milliseconds */
  159. s16 value; /* value */
  160. u8 type; /* event type */
  161. u8 number; /* axis/button number */
  162. };
  163. struct Joypad
  164. {
  165. int _fd[CROWN_MAX_JOYPADS];
  166. bool _connected[CROWN_MAX_JOYPADS];
  167. struct AxisData
  168. {
  169. s16 left[3];
  170. s16 right[3];
  171. };
  172. AxisData _axis[CROWN_MAX_JOYPADS];
  173. void init()
  174. {
  175. char jspath[] = "/dev/input/jsX";
  176. char* num = strchr(jspath, 'X');
  177. for (u8 i = 0; i < CROWN_MAX_JOYPADS; ++i)
  178. {
  179. *num = '0' + i;
  180. _fd[i] = open(jspath, O_RDONLY | O_NONBLOCK);
  181. }
  182. memset(_connected, 0, sizeof(_connected));
  183. memset(_axis, 0, sizeof(_axis));
  184. }
  185. void shutdown()
  186. {
  187. for (u8 i = 0; i < CROWN_MAX_JOYPADS; ++i)
  188. {
  189. if (_fd[i] != -1)
  190. close(_fd[i]);
  191. }
  192. }
  193. void update(DeviceEventQueue& queue)
  194. {
  195. JoypadEvent ev;
  196. memset(&ev, 0, sizeof(ev));
  197. for (u8 i = 0; i < CROWN_MAX_JOYPADS; ++i)
  198. {
  199. const int fd = _fd[i];
  200. const bool connected = fd != -1;
  201. if (connected != _connected[i])
  202. queue.push_status_event(InputDeviceType::JOYPAD, i, connected);
  203. _connected[i] = connected;
  204. if (!connected)
  205. continue;
  206. while(read(fd, &ev, sizeof(ev)) != -1)
  207. {
  208. s16 val = ev.value;
  209. switch (ev.type &= ~JS_EVENT_INIT)
  210. {
  211. case JS_EVENT_AXIS:
  212. {
  213. // Indices into axis.left/right respectively
  214. const u8 axis_idx[] = { 0, 1, 2, 0, 1, 2 };
  215. const u8 axis_map[] =
  216. {
  217. JoypadAxis::LEFT,
  218. JoypadAxis::LEFT,
  219. JoypadAxis::TRIGGER_LEFT,
  220. JoypadAxis::RIGHT,
  221. JoypadAxis::RIGHT,
  222. JoypadAxis::TRIGGER_RIGHT
  223. };
  224. // Remap triggers to [0, INT16_MAX]
  225. if (ev.number == 2 || ev.number == 5)
  226. val = (val + INT16_MAX) >> 1;
  227. s16* values = ev.number > 2 ? _axis[i].right : _axis[i].left;
  228. values[axis_idx[ev.number]] = val;
  229. if (ev.number == 2 || ev.number == 5)
  230. {
  231. queue.push_axis_event(InputDeviceType::JOYPAD
  232. , i
  233. , axis_map[ev.number]
  234. , 0
  235. , 0
  236. , values[2]
  237. );
  238. }
  239. else if (ev.number < countof(axis_map))
  240. {
  241. queue.push_axis_event(InputDeviceType::JOYPAD
  242. , i
  243. , axis_map[ev.number]
  244. , values[0]
  245. , -values[1]
  246. , 0
  247. );
  248. }
  249. }
  250. break;
  251. case JS_EVENT_BUTTON:
  252. if (ev.number < countof(s_button))
  253. {
  254. queue.push_button_event(InputDeviceType::JOYPAD
  255. , i
  256. , s_button[ev.number]
  257. , val == 1
  258. );
  259. }
  260. break;
  261. default:
  262. break;
  263. }
  264. }
  265. }
  266. }
  267. };
  268. static bool s_exit = false;
  269. struct MainThreadArgs
  270. {
  271. DeviceOptions* opts;
  272. };
  273. s32 func(void* data)
  274. {
  275. MainThreadArgs* args = (MainThreadArgs*)data;
  276. crown::run(*args->opts);
  277. s_exit = true;
  278. return EXIT_SUCCESS;
  279. }
  280. struct LinuxDevice
  281. {
  282. ::Display* _x11_display;
  283. Atom _wm_delete_message;
  284. XRRScreenConfiguration* _screen_config;
  285. bool _x11_detectable_autorepeat;
  286. DeviceEventQueue _queue;
  287. Joypad _joypad;
  288. LinuxDevice()
  289. : _x11_display(NULL)
  290. , _screen_config(NULL)
  291. , _x11_detectable_autorepeat(false)
  292. {
  293. }
  294. int run(DeviceOptions* opts)
  295. {
  296. // http://tronche.com/gui/x/xlib/display/XInitThreads.html
  297. Status xs = XInitThreads();
  298. CE_ASSERT(xs != 0, "XInitThreads: error");
  299. CE_UNUSED(xs);
  300. _x11_display = XOpenDisplay(NULL);
  301. CE_ASSERT(_x11_display != NULL, "XOpenDisplay: error");
  302. ::Window root_window = RootWindow(_x11_display, DefaultScreen(_x11_display));
  303. // Do we have detectable autorepeat?
  304. Bool detectable;
  305. _x11_detectable_autorepeat = (bool)XkbSetDetectableAutoRepeat(_x11_display, true, &detectable);
  306. _wm_delete_message = XInternAtom(_x11_display, "WM_DELETE_WINDOW", False);
  307. // Save screen configuration
  308. _screen_config = XRRGetScreenInfo(_x11_display, root_window);
  309. Rotation rr_old_rot;
  310. const SizeID rr_old_sizeid = XRRConfigCurrentConfiguration(_screen_config, &rr_old_rot);
  311. XIM im;
  312. im = XOpenIM(_x11_display, NULL, NULL, NULL);
  313. CE_ASSERT(im != NULL, "XOpenIM: error");
  314. XIC ic;
  315. ic = XCreateIC(im
  316. , XNInputStyle
  317. , 0
  318. | XIMPreeditNothing
  319. | XIMStatusNothing
  320. , XNClientWindow
  321. , root_window
  322. , NULL
  323. );
  324. CE_ASSERT(ic != NULL, "XCreateIC: error");
  325. // Start main thread
  326. MainThreadArgs mta;
  327. mta.opts = opts;
  328. Thread main_thread;
  329. main_thread.start(func, &mta);
  330. _joypad.init();
  331. while (!s_exit)
  332. {
  333. _joypad.update(_queue);
  334. int pending = XPending(_x11_display);
  335. if (!pending)
  336. {
  337. os::sleep(8);
  338. }
  339. else
  340. {
  341. XEvent event;
  342. XNextEvent(_x11_display, &event);
  343. switch (event.type)
  344. {
  345. case EnterNotify:
  346. _queue.push_axis_event(InputDeviceType::MOUSE
  347. , 0
  348. , MouseAxis::CURSOR
  349. , event.xcrossing.x
  350. , event.xcrossing.y
  351. , 0
  352. );
  353. break;
  354. case ClientMessage:
  355. if ((Atom)event.xclient.data.l[0] == _wm_delete_message)
  356. _queue.push_exit_event();
  357. break;
  358. case ConfigureNotify:
  359. _queue.push_resolution_event(event.xconfigure.width
  360. , event.xconfigure.height
  361. );
  362. break;
  363. case ButtonPress:
  364. case ButtonRelease:
  365. {
  366. if (event.xbutton.button == Button4 || event.xbutton.button == Button5)
  367. {
  368. _queue.push_axis_event(InputDeviceType::MOUSE
  369. , 0
  370. , MouseAxis::WHEEL
  371. , 0
  372. , event.xbutton.button == Button4 ? 1 : -1
  373. , 0
  374. );
  375. break;
  376. }
  377. MouseButton::Enum mb;
  378. switch (event.xbutton.button)
  379. {
  380. case Button1: mb = MouseButton::LEFT; break;
  381. case Button2: mb = MouseButton::MIDDLE; break;
  382. case Button3: mb = MouseButton::RIGHT; break;
  383. default: mb = MouseButton::COUNT; break;
  384. }
  385. if (mb != MouseButton::COUNT)
  386. {
  387. _queue.push_button_event(InputDeviceType::MOUSE
  388. , 0
  389. , mb
  390. , event.type == ButtonPress
  391. );
  392. }
  393. }
  394. break;
  395. case MotionNotify:
  396. _queue.push_axis_event(InputDeviceType::MOUSE
  397. , 0
  398. , MouseAxis::CURSOR
  399. , event.xmotion.x
  400. , event.xmotion.y
  401. , 0
  402. );
  403. break;
  404. case KeyPress:
  405. case KeyRelease:
  406. {
  407. KeySym keysym = XLookupKeysym(&event.xkey, 0);
  408. if (event.type == KeyPress)
  409. {
  410. Status status = 0;
  411. u8 utf8[4] = { 0 };
  412. int len = Xutf8LookupString(ic
  413. , &event.xkey
  414. , (char*)utf8
  415. , sizeof(utf8)
  416. , &keysym
  417. , &status
  418. );
  419. if (status == XLookupChars || status == XLookupBoth)
  420. {
  421. if (len)
  422. _queue.push_text_event(len, utf8);
  423. }
  424. }
  425. KeyboardButton::Enum kb = x11_translate_key(keysym);
  426. if (kb != KeyboardButton::COUNT)
  427. {
  428. _queue.push_button_event(InputDeviceType::KEYBOARD
  429. , 0
  430. , kb
  431. , event.type == KeyPress
  432. );
  433. }
  434. }
  435. break;
  436. case KeymapNotify:
  437. XRefreshKeyboardMapping(&event.xmapping);
  438. break;
  439. default:
  440. break;
  441. }
  442. }
  443. }
  444. _joypad.shutdown();
  445. main_thread.stop();
  446. XDestroyIC(ic);
  447. XCloseIM(im);
  448. // Restore previous screen configuration
  449. Rotation rr_rot;
  450. const SizeID rr_sizeid = XRRConfigCurrentConfiguration(_screen_config, &rr_rot);
  451. if (rr_rot != rr_old_rot || rr_sizeid != rr_old_sizeid)
  452. {
  453. XRRSetScreenConfig(_x11_display
  454. , _screen_config
  455. , root_window
  456. , rr_old_sizeid
  457. , rr_old_rot
  458. , CurrentTime
  459. );
  460. }
  461. XRRFreeScreenConfigInfo(_screen_config);
  462. XCloseDisplay(_x11_display);
  463. return EXIT_SUCCESS;
  464. }
  465. };
  466. static LinuxDevice s_ldvc;
  467. struct WindowX11 : public Window
  468. {
  469. ::Window _x11_window;
  470. Cursor _x11_hidden_cursor;
  471. Atom _wm_delete_message;
  472. WindowX11()
  473. : _x11_window(None)
  474. , _x11_hidden_cursor(None)
  475. {
  476. }
  477. void open(u16 x, u16 y, u16 width, u16 height, u32 parent)
  478. {
  479. int screen = DefaultScreen(s_ldvc._x11_display);
  480. int depth = DefaultDepth(s_ldvc._x11_display, screen);
  481. Visual* visual = DefaultVisual(s_ldvc._x11_display, screen);
  482. ::Window root_window = RootWindow(s_ldvc._x11_display, screen);
  483. ::Window parent_window = (parent == 0) ? root_window : (::Window)parent;
  484. // Create main window
  485. XSetWindowAttributes win_attribs;
  486. win_attribs.background_pixmap = 0;
  487. win_attribs.border_pixel = 0;
  488. win_attribs.event_mask = FocusChangeMask
  489. | StructureNotifyMask
  490. ;
  491. if (!parent)
  492. {
  493. win_attribs.event_mask |= KeyPressMask
  494. | KeyReleaseMask
  495. | ButtonPressMask
  496. | ButtonReleaseMask
  497. | PointerMotionMask
  498. | EnterWindowMask
  499. ;
  500. }
  501. else
  502. {
  503. XWindowAttributes parent_attrs;
  504. XGetWindowAttributes(s_ldvc._x11_display, parent_window, &parent_attrs);
  505. depth = parent_attrs.depth;
  506. visual = parent_attrs.visual;
  507. }
  508. _x11_window = XCreateWindow(s_ldvc._x11_display
  509. , parent_window
  510. , x
  511. , y
  512. , width
  513. , height
  514. , 0
  515. , depth
  516. , InputOutput
  517. , visual
  518. , CWBorderPixel | CWEventMask
  519. , &win_attribs
  520. );
  521. CE_ASSERT(_x11_window != None, "XCreateWindow: error");
  522. // Build hidden cursor
  523. Pixmap bm_no;
  524. XColor black, dummy;
  525. Colormap colormap;
  526. static char no_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  527. colormap = XDefaultColormap(s_ldvc._x11_display, screen);
  528. XAllocNamedColor(s_ldvc._x11_display, colormap, "black", &black, &dummy);
  529. bm_no = XCreateBitmapFromData(s_ldvc._x11_display, _x11_window, no_data, 8, 8);
  530. _x11_hidden_cursor = XCreatePixmapCursor(s_ldvc._x11_display, bm_no, bm_no, &black, &black, 0, 0);
  531. _wm_delete_message = XInternAtom(s_ldvc._x11_display, "WM_DELETE_WINDOW", False);
  532. XSetWMProtocols(s_ldvc._x11_display, _x11_window, &_wm_delete_message, 1);
  533. XMapRaised(s_ldvc._x11_display, _x11_window);
  534. }
  535. void close()
  536. {
  537. XDestroyWindow(s_ldvc._x11_display, _x11_window);
  538. }
  539. void bgfx_setup()
  540. {
  541. bgfx::PlatformData pd;
  542. pd.ndt = s_ldvc._x11_display;
  543. pd.nwh = (void*)(uintptr_t)_x11_window;
  544. pd.context = NULL;
  545. pd.backBuffer = NULL;
  546. pd.backBufferDS = NULL;
  547. bgfx::setPlatformData(pd);
  548. }
  549. void show()
  550. {
  551. XMapRaised(s_ldvc._x11_display, _x11_window);
  552. }
  553. void hide()
  554. {
  555. XUnmapWindow(s_ldvc._x11_display, _x11_window);
  556. }
  557. void resize(u16 width, u16 height)
  558. {
  559. XResizeWindow(s_ldvc._x11_display, _x11_window, width, height);
  560. }
  561. void move(u16 x, u16 y)
  562. {
  563. XMoveWindow(s_ldvc._x11_display, _x11_window, x, y);
  564. }
  565. void minimize()
  566. {
  567. }
  568. void restore()
  569. {
  570. }
  571. const char* title()
  572. {
  573. static char buf[512];
  574. memset(buf, 0, sizeof(buf));
  575. char* name;
  576. XFetchName(s_ldvc._x11_display, _x11_window, &name);
  577. strncpy(buf, name, sizeof(buf)-1);
  578. XFree(name);
  579. return buf;
  580. }
  581. void set_title (const char* title)
  582. {
  583. XStoreName(s_ldvc._x11_display, _x11_window, title);
  584. }
  585. void* handle()
  586. {
  587. return (void*)(uintptr_t)_x11_window;
  588. }
  589. void show_cursor(bool show)
  590. {
  591. XDefineCursor(s_ldvc._x11_display
  592. , _x11_window
  593. , show ? None : _x11_hidden_cursor
  594. );
  595. }
  596. void set_fullscreen(bool full)
  597. {
  598. XEvent e;
  599. e.xclient.type = ClientMessage;
  600. e.xclient.window = _x11_window;
  601. e.xclient.message_type = XInternAtom(s_ldvc._x11_display, "_NET_WM_STATE", False);
  602. e.xclient.format = 32;
  603. e.xclient.data.l[0] = full ? 1 : 0;
  604. e.xclient.data.l[1] = XInternAtom(s_ldvc._x11_display, "_NET_WM_STATE_FULLSCREEN", False);
  605. XSendEvent(s_ldvc._x11_display, DefaultRootWindow(s_ldvc._x11_display), False, SubstructureNotifyMask, &e);
  606. }
  607. };
  608. namespace window
  609. {
  610. Window* create(Allocator& a)
  611. {
  612. return CE_NEW(a, WindowX11)();
  613. }
  614. void destroy(Allocator& a, Window& w)
  615. {
  616. CE_DELETE(a, &w);
  617. }
  618. } // namespace window
  619. struct DisplayXRandr : public Display
  620. {
  621. void modes(Array<DisplayMode>& modes)
  622. {
  623. int num = 0;
  624. XRRScreenSize* sizes = XRRConfigSizes(s_ldvc._screen_config, &num);
  625. if (!sizes)
  626. return;
  627. for (int i = 0; i < num; ++i)
  628. {
  629. DisplayMode dm;
  630. dm.id = (u32)i;
  631. dm.width = sizes[i].width;
  632. dm.height = sizes[i].height;
  633. array::push_back(modes, dm);
  634. }
  635. }
  636. void set_mode(u32 id)
  637. {
  638. int num = 0;
  639. XRRScreenSize* sizes = XRRConfigSizes(s_ldvc._screen_config, &num);
  640. if (!sizes || (int)id >= num)
  641. return;
  642. XRRSetScreenConfig(s_ldvc._x11_display
  643. , s_ldvc._screen_config
  644. , RootWindow(s_ldvc._x11_display, DefaultScreen(s_ldvc._x11_display))
  645. , (int)id
  646. , RR_Rotate_0
  647. , CurrentTime
  648. );
  649. }
  650. };
  651. namespace display
  652. {
  653. Display* create(Allocator& a)
  654. {
  655. return CE_NEW(a, DisplayXRandr)();
  656. }
  657. void destroy(Allocator& a, Display& d)
  658. {
  659. CE_DELETE(a, &d);
  660. }
  661. } // namespace display
  662. bool next_event(OsEvent& ev)
  663. {
  664. return s_ldvc._queue.pop_event(ev);
  665. }
  666. } // namespace crown
  667. struct InitMemoryGlobals
  668. {
  669. InitMemoryGlobals()
  670. {
  671. crown::memory_globals::init();
  672. }
  673. ~InitMemoryGlobals()
  674. {
  675. crown::memory_globals::shutdown();
  676. }
  677. };
  678. int main(int argc, char** argv)
  679. {
  680. using namespace crown;
  681. #if CROWN_BUILD_UNIT_TESTS
  682. CommandLine cl(argc, (const char**)argv);
  683. if (cl.has_option("run-unit-tests"))
  684. {
  685. return main_unit_tests();
  686. }
  687. #endif // CROWN_BUILD_UNIT_TESTS
  688. if (cl.has_option("compile") || cl.has_option("server"))
  689. {
  690. if (main_data_compiler(argc, argv) != EXIT_SUCCESS || !cl.has_option("continue"))
  691. return EXIT_FAILURE;
  692. }
  693. InitMemoryGlobals m;
  694. CE_UNUSED(m);
  695. DeviceOptions opts(default_allocator(), argc, (const char**)argv);
  696. int ec = opts.parse();
  697. if (ec == EXIT_SUCCESS)
  698. ec = s_ldvc.run(&opts);
  699. return ec;
  700. }
  701. #endif // CROWN_PLATFORM_LINUX