main_linux.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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 "command_line.h"
  9. #include "console_server.h"
  10. #include "device.h"
  11. #include "os_event_queue.h"
  12. #include "thread.h"
  13. #include <stdlib.h>
  14. #include <X11/extensions/Xrandr.h>
  15. #include <X11/Xatom.h>
  16. #include <X11/XKBlib.h>
  17. #include <X11/Xlib.h>
  18. #include <X11/Xutil.h>
  19. #include <bgfx/bgfxplatform.h>
  20. namespace crown
  21. {
  22. // void display_modes(Array<DisplayMode>& modes)
  23. // {
  24. // int num_rrsizes = 0;
  25. // XRRScreenSize* rrsizes = XRRConfigSizes(m_screen_config, &num_rrsizes);
  26. // for (int i = 0; i < num_rrsizes; i++)
  27. // {
  28. // DisplayMode dm;
  29. // dm.id = (uint32_t) i;
  30. // dm.width = rrsizes[i].width;
  31. // dm.height = rrsizes[i].height;
  32. // array::push_back(modes, dm);
  33. // }
  34. // }
  35. // void set_display_mode(uint32_t id)
  36. // {
  37. // // Check if id is valid
  38. // int num_rrsizes = 0;
  39. // XRRScreenSize* rrsizes = XRRConfigSizes(m_screen_config, &num_rrsizes);
  40. // (void) rrsizes;
  41. // if ((int) id >= num_rrsizes)
  42. // return;
  43. // XRRSetScreenConfig(m_x11_display,
  44. // m_screen_config,
  45. // RootWindow(m_x11_display, DefaultScreen(m_x11_display)),
  46. // (int) id,
  47. // RR_Rotate_0,
  48. // CurrentTime);
  49. // }
  50. // void set_fullscreen(bool full)
  51. // {
  52. // XEvent e;
  53. // e.xclient.type = ClientMessage;
  54. // e.xclient.window = m_x11_window;
  55. // e.xclient.message_type = XInternAtom(m_x11_display, "_NET_WM_STATE", False );
  56. // e.xclient.format = 32;
  57. // e.xclient.data.l[0] = full ? 1 : 0;
  58. // e.xclient.data.l[1] = XInternAtom(m_x11_display, "_NET_WM_STATE_FULLSCREEN", False);
  59. // XSendEvent(m_x11_display, DefaultRootWindow(m_x11_display), False, SubstructureNotifyMask, &e);
  60. // }
  61. static KeyboardButton::Enum x11_translate_key(KeySym x11_key)
  62. {
  63. switch (x11_key)
  64. {
  65. case XK_BackSpace: return KeyboardButton::BACKSPACE;
  66. case XK_Tab: return KeyboardButton::TAB;
  67. case XK_space: return KeyboardButton::SPACE;
  68. case XK_Escape: return KeyboardButton::ESCAPE;
  69. case XK_Return: return KeyboardButton::ENTER;
  70. case XK_F1: return KeyboardButton::F1;
  71. case XK_F2: return KeyboardButton::F2;
  72. case XK_F3: return KeyboardButton::F3;
  73. case XK_F4: return KeyboardButton::F4;
  74. case XK_F5: return KeyboardButton::F5;
  75. case XK_F6: return KeyboardButton::F6;
  76. case XK_F7: return KeyboardButton::F7;
  77. case XK_F8: return KeyboardButton::F8;
  78. case XK_F9: return KeyboardButton::F9;
  79. case XK_F10: return KeyboardButton::F10;
  80. case XK_F11: return KeyboardButton::F11;
  81. case XK_F12: return KeyboardButton::F12;
  82. case XK_Home: return KeyboardButton::HOME;
  83. case XK_Left: return KeyboardButton::LEFT;
  84. case XK_Up: return KeyboardButton::UP;
  85. case XK_Right: return KeyboardButton::RIGHT;
  86. case XK_Down: return KeyboardButton::DOWN;
  87. case XK_Page_Up: return KeyboardButton::PAGE_UP;
  88. case XK_Page_Down: return KeyboardButton::PAGE_DOWN;
  89. case XK_Delete: return KeyboardButton::DELETE;
  90. case XK_End: return KeyboardButton::END;
  91. case XK_Shift_L: return KeyboardButton::LEFT_SHIFT;
  92. case XK_Shift_R: return KeyboardButton::RIGHT_SHIFT;
  93. case XK_Control_L: return KeyboardButton::LEFT_CTRL;
  94. case XK_Control_R: return KeyboardButton::RIGHT_CTRL;
  95. case XK_Caps_Lock: return KeyboardButton::CAPS_LOCK;
  96. case XK_Alt_L: return KeyboardButton::LEFT_ALT;
  97. case XK_Alt_R: return KeyboardButton::RIGHT_ALT;
  98. case XK_Super_L: return KeyboardButton::LEFT_SUPER;
  99. case XK_Super_R: return KeyboardButton::RIGHT_SUPER;
  100. case XK_Num_Lock: return KeyboardButton::NUM_LOCK;
  101. case XK_KP_Enter: return KeyboardButton::NUMPAD_ENTER;
  102. case XK_KP_Delete: return KeyboardButton::NUMPAD_DELETE;
  103. case XK_KP_Multiply: return KeyboardButton::NUMPAD_MULTIPLY;
  104. case XK_KP_Add: return KeyboardButton::NUMPAD_ADD;
  105. case XK_KP_Subtract: return KeyboardButton::NUMPAD_SUBTRACT;
  106. case XK_KP_Divide: return KeyboardButton::NUMPAD_DIVIDE;
  107. case XK_KP_Insert:
  108. case XK_KP_0: return KeyboardButton::NUMPAD_0;
  109. case XK_KP_End:
  110. case XK_KP_1: return KeyboardButton::NUMPAD_1;
  111. case XK_KP_Down:
  112. case XK_KP_2: return KeyboardButton::NUMPAD_2;
  113. case XK_KP_Page_Down: // or XK_KP_Next
  114. case XK_KP_3: return KeyboardButton::NUMPAD_3;
  115. case XK_KP_Left:
  116. case XK_KP_4: return KeyboardButton::NUMPAD_4;
  117. case XK_KP_Begin:
  118. case XK_KP_5: return KeyboardButton::NUMPAD_5;
  119. case XK_KP_Right:
  120. case XK_KP_6: return KeyboardButton::NUMPAD_6;
  121. case XK_KP_Home:
  122. case XK_KP_7: return KeyboardButton::NUMPAD_7;
  123. case XK_KP_Up:
  124. case XK_KP_8: return KeyboardButton::NUMPAD_8;
  125. case XK_KP_Page_Up: // or XK_KP_Prior
  126. case XK_KP_9: return KeyboardButton::NUMPAD_9;
  127. case '0': return KeyboardButton::NUMBER_0;
  128. case '1': return KeyboardButton::NUMBER_1;
  129. case '2': return KeyboardButton::NUMBER_2;
  130. case '3': return KeyboardButton::NUMBER_3;
  131. case '4': return KeyboardButton::NUMBER_4;
  132. case '5': return KeyboardButton::NUMBER_5;
  133. case '6': return KeyboardButton::NUMBER_6;
  134. case '7': return KeyboardButton::NUMBER_7;
  135. case '8': return KeyboardButton::NUMBER_8;
  136. case '9': return KeyboardButton::NUMBER_9;
  137. case 'a': return KeyboardButton::A;
  138. case 'b': return KeyboardButton::B;
  139. case 'c': return KeyboardButton::C;
  140. case 'd': return KeyboardButton::D;
  141. case 'e': return KeyboardButton::E;
  142. case 'f': return KeyboardButton::F;
  143. case 'g': return KeyboardButton::G;
  144. case 'h': return KeyboardButton::H;
  145. case 'i': return KeyboardButton::I;
  146. case 'j': return KeyboardButton::J;
  147. case 'k': return KeyboardButton::K;
  148. case 'l': return KeyboardButton::L;
  149. case 'm': return KeyboardButton::M;
  150. case 'n': return KeyboardButton::N;
  151. case 'o': return KeyboardButton::O;
  152. case 'p': return KeyboardButton::P;
  153. case 'q': return KeyboardButton::Q;
  154. case 'r': return KeyboardButton::R;
  155. case 's': return KeyboardButton::S;
  156. case 't': return KeyboardButton::T;
  157. case 'u': return KeyboardButton::U;
  158. case 'v': return KeyboardButton::V;
  159. case 'w': return KeyboardButton::W;
  160. case 'x': return KeyboardButton::X;
  161. case 'y': return KeyboardButton::Y;
  162. case 'z': return KeyboardButton::Z;
  163. default: return KeyboardButton::COUNT;
  164. }
  165. }
  166. #define JS_EVENT_BUTTON 0x01 /* button pressed/released */
  167. #define JS_EVENT_AXIS 0x02 /* joystick moved */
  168. #define JS_EVENT_INIT 0x80 /* initial state of device */
  169. #define XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE 7849
  170. #define XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE 8689
  171. #define XINPUT_GAMEPAD_THRESHOLD 30
  172. static uint8_t s_button[] =
  173. {
  174. JoypadButton::A,
  175. JoypadButton::B,
  176. JoypadButton::X,
  177. JoypadButton::Y,
  178. JoypadButton::LEFT_SHOULDER,
  179. JoypadButton::RIGHT_SHOULDER,
  180. JoypadButton::BACK,
  181. JoypadButton::START,
  182. JoypadButton::GUIDE,
  183. JoypadButton::LEFT_THUMB,
  184. JoypadButton::RIGHT_THUMB,
  185. JoypadButton::UP, // FIXME (reported as axis...)
  186. JoypadButton::DOWN,
  187. JoypadButton::LEFT,
  188. JoypadButton::RIGHT
  189. };
  190. static uint16_t s_deadzone[] =
  191. {
  192. XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE,
  193. XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE,
  194. XINPUT_GAMEPAD_THRESHOLD,
  195. XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE,
  196. XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE,
  197. XINPUT_GAMEPAD_THRESHOLD
  198. };
  199. struct JoypadEvent
  200. {
  201. uint32_t time; /* event timestamp in milliseconds */
  202. int16_t value; /* value */
  203. uint8_t type; /* event type */
  204. uint8_t number; /* axis/button number */
  205. };
  206. struct Joypad
  207. {
  208. void init()
  209. {
  210. char jspath[] = "/dev/input/jsX";
  211. char* num = strchr(jspath, 'X');
  212. for (uint8_t i = 0; i < CROWN_MAX_JOYPADS; ++i)
  213. {
  214. *num = '0' + i;
  215. _fd[i] = open(jspath, O_RDONLY | O_NONBLOCK);
  216. }
  217. memset(_connected, 0, sizeof(_connected));
  218. memset(_axis, 0, sizeof(_axis));
  219. }
  220. void shutdown()
  221. {
  222. for (uint8_t i = 0; i < CROWN_MAX_JOYPADS; ++i)
  223. {
  224. if (_fd[i] != -1)
  225. close(_fd[i]);
  226. }
  227. }
  228. void update(OsEventQueue& queue)
  229. {
  230. JoypadEvent ev;
  231. memset(&ev, 0, sizeof(ev));
  232. for (uint8_t i = 0; i < CROWN_MAX_JOYPADS; ++i)
  233. {
  234. const int fd = _fd[i];
  235. const bool connected = fd != -1;
  236. if (connected != _connected[i])
  237. queue.push_joypad_event(i, connected);
  238. _connected[i] = connected;
  239. if (!connected)
  240. continue;
  241. while(read(fd, &ev, sizeof(ev)) != -1)
  242. {
  243. const uint8_t num = ev.number;
  244. const int16_t val = ev.value;
  245. switch (ev.type &= ~JS_EVENT_INIT)
  246. {
  247. case JS_EVENT_AXIS:
  248. {
  249. AxisData& axis = _axis[i];
  250. // Indices into axis.left/right respectively
  251. const uint8_t axis_idx[] = { 0, 1, 2, 0, 1, 2 };
  252. const int16_t deadzone = s_deadzone[num];
  253. int16_t value = val > deadzone || val < -deadzone ? val : 0;
  254. // Remap triggers to [0, INT16_MAX]
  255. if (num == 2 || num == 5)
  256. value = (value + INT16_MAX) >> 1;
  257. float* values = num > 2 ? axis.right : axis.left;
  258. values[axis_idx[num]] = value != 0
  259. ? float(value + (value < 0 ? deadzone : -deadzone)) / float(INT16_MAX - deadzone)
  260. : 0.0f
  261. ;
  262. queue.push_joypad_event(i
  263. , num > 2 ? 1 : 0
  264. , values[0]
  265. , -values[1]
  266. , values[2]
  267. );
  268. break;
  269. }
  270. case JS_EVENT_BUTTON:
  271. {
  272. if (ev.number < CE_COUNTOF(s_button))
  273. {
  274. queue.push_joypad_event(i
  275. , s_button[ev.number]
  276. , val == 1
  277. );
  278. }
  279. break;
  280. }
  281. }
  282. }
  283. }
  284. }
  285. int _fd[CROWN_MAX_JOYPADS];
  286. bool _connected[CROWN_MAX_JOYPADS];
  287. struct AxisData
  288. {
  289. float left[3];
  290. float right[3];
  291. };
  292. AxisData _axis[CROWN_MAX_JOYPADS];
  293. };
  294. static bool s_exit = false;
  295. struct MainThreadArgs
  296. {
  297. DeviceOptions* opts;
  298. };
  299. int32_t func(void* data)
  300. {
  301. MainThreadArgs* args = (MainThreadArgs*)data;
  302. crown::init(*args->opts);
  303. crown::update();
  304. crown::shutdown();
  305. s_exit = true;
  306. return EXIT_SUCCESS;
  307. }
  308. extern void set_x11_display(Display*);
  309. struct LinuxDevice
  310. {
  311. LinuxDevice()
  312. : _x11_display(NULL)
  313. , _screen_config(NULL)
  314. , _x11_detectable_autorepeat(false)
  315. {
  316. }
  317. int32_t run(DeviceOptions* opts)
  318. {
  319. // http://tronche.com/gui/x/xlib/display/XInitThreads.html
  320. Status xs = XInitThreads();
  321. CE_ASSERT(xs != 0, "XInitThreads: error");
  322. CE_UNUSED(xs);
  323. _x11_display = XOpenDisplay(NULL);
  324. CE_ASSERT(_x11_display != NULL, "XOpenDisplay: error");
  325. crown::set_x11_display(_x11_display);
  326. ::Window root_window = RootWindow(_x11_display, DefaultScreen(_x11_display));
  327. // Do we have detectable autorepeat?
  328. Bool detectable;
  329. _x11_detectable_autorepeat = (bool)XkbSetDetectableAutoRepeat(_x11_display, true, &detectable);
  330. _wm_delete_message = XInternAtom(_x11_display, "WM_DELETE_WINDOW", False);
  331. // Save screen configuration
  332. _screen_config = XRRGetScreenInfo(_x11_display, root_window);
  333. Rotation rr_old_rot;
  334. const SizeID rr_old_sizeid = XRRConfigCurrentConfiguration(_screen_config, &rr_old_rot);
  335. // Start main thread
  336. MainThreadArgs mta;
  337. mta.opts = opts;
  338. Thread main_thread;
  339. main_thread.start(func, &mta);
  340. _joypad.init();
  341. while (!s_exit)
  342. {
  343. pump_events();
  344. }
  345. _joypad.shutdown();
  346. main_thread.stop();
  347. // Restore previous screen configuration
  348. Rotation rr_rot;
  349. const SizeID rr_sizeid = XRRConfigCurrentConfiguration(_screen_config, &rr_rot);
  350. if (rr_rot != rr_old_rot || rr_sizeid != rr_old_sizeid)
  351. {
  352. XRRSetScreenConfig(_x11_display
  353. , _screen_config
  354. , root_window
  355. , rr_old_sizeid
  356. , rr_old_rot
  357. , CurrentTime
  358. );
  359. }
  360. XRRFreeScreenConfigInfo(_screen_config);
  361. XCloseDisplay(_x11_display);
  362. return EXIT_SUCCESS;
  363. }
  364. void pump_events()
  365. {
  366. _joypad.update(_queue);
  367. while (XPending(_x11_display))
  368. {
  369. XEvent event;
  370. XNextEvent(_x11_display, &event);
  371. switch (event.type)
  372. {
  373. case EnterNotify:
  374. {
  375. _queue.push_mouse_event(event.xcrossing.x, event.xcrossing.y);
  376. break;
  377. }
  378. case ClientMessage:
  379. {
  380. if ((Atom)event.xclient.data.l[0] == _wm_delete_message)
  381. {
  382. _queue.push_exit_event(0);
  383. }
  384. break;
  385. }
  386. case ConfigureNotify:
  387. {
  388. _queue.push_metrics_event(event.xconfigure.x
  389. , event.xconfigure.y
  390. , event.xconfigure.width
  391. , event.xconfigure.height
  392. );
  393. break;
  394. }
  395. case ButtonPress:
  396. case ButtonRelease:
  397. {
  398. if (event.xbutton.button == Button4 || event.xbutton.button == Button5)
  399. {
  400. _queue.push_mouse_event(event.xbutton.x
  401. , event.xbutton.y
  402. , event.xbutton.button == Button4 ? 1.0f : -1.0f
  403. );
  404. break;
  405. }
  406. MouseButton::Enum mb;
  407. switch (event.xbutton.button)
  408. {
  409. case Button1: mb = MouseButton::LEFT; break;
  410. case Button2: mb = MouseButton::MIDDLE; break;
  411. case Button3: mb = MouseButton::RIGHT; break;
  412. default: mb = MouseButton::COUNT; break;
  413. }
  414. if (mb != MouseButton::COUNT)
  415. {
  416. _queue.push_mouse_event(event.xbutton.x
  417. , event.xbutton.y
  418. , mb
  419. , event.type == ButtonPress
  420. );
  421. }
  422. break;
  423. }
  424. case MotionNotify:
  425. {
  426. _queue.push_mouse_event(event.xmotion.x, event.xmotion.y);
  427. break;
  428. }
  429. case KeyPress:
  430. case KeyRelease:
  431. {
  432. KeySym keysym = XLookupKeysym(&event.xkey, 0);
  433. KeyboardButton::Enum kb = x11_translate_key(keysym);
  434. if (kb != KeyboardButton::COUNT)
  435. _queue.push_keyboard_event(kb, event.type == KeyPress);
  436. break;
  437. }
  438. case KeymapNotify:
  439. {
  440. XRefreshKeyboardMapping(&event.xmapping);
  441. break;
  442. }
  443. default:
  444. {
  445. break;
  446. }
  447. }
  448. }
  449. }
  450. public:
  451. Display* _x11_display;
  452. Atom _wm_delete_message;
  453. XRRScreenConfiguration* _screen_config;
  454. bool _x11_detectable_autorepeat;
  455. OsEventQueue _queue;
  456. Joypad _joypad;
  457. };
  458. static LinuxDevice s_ldvc;
  459. bool next_event(OsEvent& ev)
  460. {
  461. return s_ldvc._queue.pop_event(ev);
  462. }
  463. } // namespace crown
  464. int main(int argc, char** argv)
  465. {
  466. using namespace crown;
  467. memory_globals::init();
  468. DeviceOptions opts(argc, argv);
  469. int exitcode = opts.parse();
  470. if (exitcode == EXIT_FAILURE)
  471. {
  472. return exitcode;
  473. }
  474. console_server_globals::init(opts.console_port(), opts.wait_console());
  475. bool do_continue = true;
  476. if (opts.do_compile())
  477. {
  478. bundle_compiler_globals::init(opts.source_dir(), opts.bundle_dir());
  479. do_continue = bundle_compiler::main(opts.do_compile(), opts.do_continue(), opts.platform());
  480. }
  481. if (do_continue)
  482. exitcode = crown::s_ldvc.run(&opts);
  483. if (opts.do_compile())
  484. bundle_compiler_globals::shutdown();
  485. console_server_globals::shutdown();
  486. memory_globals::shutdown();
  487. return exitcode;
  488. }
  489. #endif // CROWN_PLATFORM_LINUX