main_linux.cpp 25 KB

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