main_linux.cpp 22 KB

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