main_linux.cpp 18 KB

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