main_linux.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * Copyright (c) 2012-2015 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 "device.h"
  8. #include "memory.h"
  9. #include "os_event_queue.h"
  10. #include "thread.h"
  11. #include "main.h"
  12. #include "command_line.h"
  13. #include "crown.h"
  14. #include "bundle_compiler.h"
  15. #include "console_server.h"
  16. #include <X11/Xutil.h>
  17. #include <X11/Xatom.h>
  18. #include <X11/Xlib.h>
  19. #include <X11/XKBlib.h>
  20. #include <X11/extensions/Xrandr.h>
  21. #include <bgfxplatform.h>
  22. #include <bgfx.h>
  23. namespace crown
  24. {
  25. // void display_modes(Array<DisplayMode>& modes)
  26. // {
  27. // int num_rrsizes = 0;
  28. // XRRScreenSize* rrsizes = XRRConfigSizes(m_screen_config, &num_rrsizes);
  29. // for (int i = 0; i < num_rrsizes; i++)
  30. // {
  31. // DisplayMode dm;
  32. // dm.id = (uint32_t) i;
  33. // dm.width = rrsizes[i].width;
  34. // dm.height = rrsizes[i].height;
  35. // array::push_back(modes, dm);
  36. // }
  37. // }
  38. // void set_display_mode(uint32_t id)
  39. // {
  40. // // Check if id is valid
  41. // int num_rrsizes = 0;
  42. // XRRScreenSize* rrsizes = XRRConfigSizes(m_screen_config, &num_rrsizes);
  43. // (void) rrsizes;
  44. // if ((int) id >= num_rrsizes)
  45. // return;
  46. // XRRSetScreenConfig(m_x11_display,
  47. // m_screen_config,
  48. // RootWindow(m_x11_display, DefaultScreen(m_x11_display)),
  49. // (int) id,
  50. // RR_Rotate_0,
  51. // CurrentTime);
  52. // }
  53. // void set_fullscreen(bool full)
  54. // {
  55. // XEvent e;
  56. // e.xclient.type = ClientMessage;
  57. // e.xclient.window = m_x11_window;
  58. // e.xclient.message_type = XInternAtom(m_x11_display, "_NET_WM_STATE", False );
  59. // e.xclient.format = 32;
  60. // e.xclient.data.l[0] = full ? 1 : 0;
  61. // e.xclient.data.l[1] = XInternAtom(m_x11_display, "_NET_WM_STATE_FULLSCREEN", False);
  62. // XSendEvent(m_x11_display, DefaultRootWindow(m_x11_display), False, SubstructureNotifyMask, &e);
  63. // }
  64. static KeyboardButton::Enum x11_translate_key(KeySym x11_key)
  65. {
  66. switch (x11_key)
  67. {
  68. case XK_BackSpace: return KeyboardButton::BACKSPACE;
  69. case XK_Tab: return KeyboardButton::TAB;
  70. case XK_space: return KeyboardButton::SPACE;
  71. case XK_Escape: return KeyboardButton::ESCAPE;
  72. case XK_Return: return KeyboardButton::ENTER;
  73. case XK_F1: return KeyboardButton::F1;
  74. case XK_F2: return KeyboardButton::F2;
  75. case XK_F3: return KeyboardButton::F3;
  76. case XK_F4: return KeyboardButton::F4;
  77. case XK_F5: return KeyboardButton::F5;
  78. case XK_F6: return KeyboardButton::F6;
  79. case XK_F7: return KeyboardButton::F7;
  80. case XK_F8: return KeyboardButton::F8;
  81. case XK_F9: return KeyboardButton::F9;
  82. case XK_F10: return KeyboardButton::F10;
  83. case XK_F11: return KeyboardButton::F11;
  84. case XK_F12: return KeyboardButton::F12;
  85. case XK_Home: return KeyboardButton::HOME;
  86. case XK_Left: return KeyboardButton::LEFT;
  87. case XK_Up: return KeyboardButton::UP;
  88. case XK_Right: return KeyboardButton::RIGHT;
  89. case XK_Down: return KeyboardButton::DOWN;
  90. case XK_Page_Up: return KeyboardButton::PAGE_UP;
  91. case XK_Page_Down: return KeyboardButton::PAGE_DOWN;
  92. case XK_Delete: return KeyboardButton::DELETE;
  93. case XK_End: return KeyboardButton::END;
  94. case XK_Shift_L: return KeyboardButton::LEFT_SHIFT;
  95. case XK_Shift_R: return KeyboardButton::RIGHT_SHIFT;
  96. case XK_Control_L: return KeyboardButton::LEFT_CTRL;
  97. case XK_Control_R: return KeyboardButton::RIGHT_CTRL;
  98. case XK_Caps_Lock: return KeyboardButton::CAPS_LOCK;
  99. case XK_Alt_L: return KeyboardButton::LEFT_ALT;
  100. case XK_Alt_R: return KeyboardButton::RIGHT_ALT;
  101. case XK_Super_L: return KeyboardButton::LEFT_SUPER;
  102. case XK_Super_R: return KeyboardButton::RIGHT_SUPER;
  103. case XK_Num_Lock: return KeyboardButton::NUM_LOCK;
  104. case XK_KP_Enter: return KeyboardButton::NUMPAD_ENTER;
  105. case XK_KP_Delete: return KeyboardButton::NUMPAD_DELETE;
  106. case XK_KP_Multiply: return KeyboardButton::NUMPAD_MULTIPLY;
  107. case XK_KP_Add: return KeyboardButton::NUMPAD_ADD;
  108. case XK_KP_Subtract: return KeyboardButton::NUMPAD_SUBTRACT;
  109. case XK_KP_Divide: return KeyboardButton::NUMPAD_DIVIDE;
  110. case XK_KP_Insert:
  111. case XK_KP_0: return KeyboardButton::NUMPAD_0;
  112. case XK_KP_End:
  113. case XK_KP_1: return KeyboardButton::NUMPAD_1;
  114. case XK_KP_Down:
  115. case XK_KP_2: return KeyboardButton::NUMPAD_2;
  116. case XK_KP_Page_Down: // or XK_KP_Next
  117. case XK_KP_3: return KeyboardButton::NUMPAD_3;
  118. case XK_KP_Left:
  119. case XK_KP_4: return KeyboardButton::NUMPAD_4;
  120. case XK_KP_Begin:
  121. case XK_KP_5: return KeyboardButton::NUMPAD_5;
  122. case XK_KP_Right:
  123. case XK_KP_6: return KeyboardButton::NUMPAD_6;
  124. case XK_KP_Home:
  125. case XK_KP_7: return KeyboardButton::NUMPAD_7;
  126. case XK_KP_Up:
  127. case XK_KP_8: return KeyboardButton::NUMPAD_8;
  128. case XK_KP_Page_Up: // or XK_KP_Prior
  129. case XK_KP_9: return KeyboardButton::NUMPAD_9;
  130. case '0': return KeyboardButton::NUMBER_0;
  131. case '1': return KeyboardButton::NUMBER_1;
  132. case '2': return KeyboardButton::NUMBER_2;
  133. case '3': return KeyboardButton::NUMBER_3;
  134. case '4': return KeyboardButton::NUMBER_4;
  135. case '5': return KeyboardButton::NUMBER_5;
  136. case '6': return KeyboardButton::NUMBER_6;
  137. case '7': return KeyboardButton::NUMBER_7;
  138. case '8': return KeyboardButton::NUMBER_8;
  139. case '9': return KeyboardButton::NUMBER_9;
  140. case 'a': return KeyboardButton::A;
  141. case 'b': return KeyboardButton::B;
  142. case 'c': return KeyboardButton::C;
  143. case 'd': return KeyboardButton::D;
  144. case 'e': return KeyboardButton::E;
  145. case 'f': return KeyboardButton::F;
  146. case 'g': return KeyboardButton::G;
  147. case 'h': return KeyboardButton::H;
  148. case 'i': return KeyboardButton::I;
  149. case 'j': return KeyboardButton::J;
  150. case 'k': return KeyboardButton::K;
  151. case 'l': return KeyboardButton::L;
  152. case 'm': return KeyboardButton::M;
  153. case 'n': return KeyboardButton::N;
  154. case 'o': return KeyboardButton::O;
  155. case 'p': return KeyboardButton::P;
  156. case 'q': return KeyboardButton::Q;
  157. case 'r': return KeyboardButton::R;
  158. case 's': return KeyboardButton::S;
  159. case 't': return KeyboardButton::T;
  160. case 'u': return KeyboardButton::U;
  161. case 'v': return KeyboardButton::V;
  162. case 'w': return KeyboardButton::W;
  163. case 'x': return KeyboardButton::X;
  164. case 'y': return KeyboardButton::Y;
  165. case 'z': return KeyboardButton::Z;
  166. default: return KeyboardButton::COUNT;
  167. }
  168. }
  169. static bool s_exit = false;
  170. struct MainThreadArgs
  171. {
  172. DeviceOptions* opts;
  173. };
  174. int32_t func(void* data)
  175. {
  176. MainThreadArgs* args = (MainThreadArgs*)data;
  177. crown::init(*args->opts);
  178. crown::update();
  179. crown::shutdown();
  180. s_exit = true;
  181. return EXIT_SUCCESS;
  182. }
  183. struct LinuxDevice
  184. {
  185. LinuxDevice()
  186. : _x11_display(NULL)
  187. , _x11_window(None)
  188. , _x11_hidden_cursor(None)
  189. , _screen_config(NULL)
  190. {
  191. }
  192. int32_t run(DeviceOptions* opts)
  193. {
  194. // http://tronche.com/gui/x/xlib/display/XInitThreads.html
  195. Status xs = XInitThreads();
  196. CE_ASSERT(xs != 0, "XInitThreads: error");
  197. CE_UNUSED(xs);
  198. _x11_display = XOpenDisplay(NULL);
  199. CE_ASSERT(_x11_display != NULL, "XOpenDisplay: error");
  200. int screen = DefaultScreen(_x11_display);
  201. int depth = DefaultDepth(_x11_display, screen);
  202. Visual* visual = DefaultVisual(_x11_display, screen);
  203. Window root_window = RootWindow(_x11_display, screen);
  204. uint32_t pw = opts->parent_window();
  205. Window parent_window = (pw == 0) ? root_window : (Window)pw;
  206. // Create main window
  207. XSetWindowAttributes win_attribs;
  208. win_attribs.background_pixmap = 0;
  209. win_attribs.border_pixel = 0;
  210. win_attribs.event_mask = FocusChangeMask
  211. | StructureNotifyMask
  212. | KeyPressMask
  213. | KeyReleaseMask
  214. | ButtonPressMask
  215. | ButtonReleaseMask
  216. | PointerMotionMask;
  217. _x11_window = XCreateWindow(_x11_display
  218. , parent_window
  219. , opts->window_x()
  220. , opts->window_y()
  221. , opts->window_width()
  222. , opts->window_height()
  223. , 0
  224. , depth
  225. , InputOutput
  226. , visual
  227. , CWBorderPixel | CWEventMask
  228. , &win_attribs
  229. );
  230. CE_ASSERT(_x11_window != None, "XCreateWindow: error");
  231. _wm_delete_message = XInternAtom(_x11_display, "WM_DELETE_WINDOW", False);
  232. XSetWMProtocols(_x11_display, _x11_window, &_wm_delete_message, 1);
  233. // Do we have detectable autorepeat?
  234. Bool detectable;
  235. _x11_detectable_autorepeat = (bool)XkbSetDetectableAutoRepeat(_x11_display, true, &detectable);
  236. // Build hidden cursor
  237. Pixmap bm_no;
  238. XColor black, dummy;
  239. Colormap colormap;
  240. static char no_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  241. colormap = XDefaultColormap(_x11_display, screen);
  242. XAllocNamedColor(_x11_display, colormap, "black", &black, &dummy);
  243. bm_no = XCreateBitmapFromData(_x11_display, _x11_window, no_data, 8, 8);
  244. _x11_hidden_cursor = XCreatePixmapCursor(_x11_display, bm_no, bm_no, &black, &black, 0, 0);
  245. bgfx::x11SetDisplayWindow(_x11_display, _x11_window);
  246. XMapRaised(_x11_display, _x11_window);
  247. // Save screen configuration
  248. _screen_config = XRRGetScreenInfo(_x11_display, parent_window);
  249. Rotation rr_old_rot;
  250. const SizeID rr_old_sizeid = XRRConfigCurrentConfiguration(_screen_config, &rr_old_rot);
  251. // Start main thread
  252. MainThreadArgs mta;
  253. mta.opts = opts;
  254. Thread main_thread;
  255. main_thread.start(func, &mta);
  256. // Push initial mouse position
  257. Window dummy1;
  258. int dummy2;
  259. unsigned int dummy3;
  260. int mx, my;
  261. XQueryPointer(_x11_display
  262. , _x11_window
  263. , &dummy1
  264. , &dummy1
  265. , &dummy2
  266. , &dummy2
  267. , &mx, &my
  268. , &dummy3
  269. );
  270. _queue.push_mouse_event(mx, my);
  271. while (!s_exit)
  272. {
  273. pump_events();
  274. }
  275. main_thread.stop();
  276. // Restore previous screen configuration
  277. Rotation rr_rot;
  278. const SizeID rr_sizeid = XRRConfigCurrentConfiguration(_screen_config, &rr_rot);
  279. if (rr_rot != rr_old_rot || rr_sizeid != rr_old_sizeid)
  280. {
  281. XRRSetScreenConfig(_x11_display
  282. , _screen_config
  283. , root_window
  284. , rr_old_sizeid
  285. , rr_old_rot
  286. , CurrentTime
  287. );
  288. }
  289. XRRFreeScreenConfigInfo(_screen_config);
  290. XDestroyWindow(_x11_display, _x11_window);
  291. XCloseDisplay(_x11_display);
  292. return EXIT_SUCCESS;
  293. }
  294. void pump_events()
  295. {
  296. while (XPending(_x11_display))
  297. {
  298. XEvent event;
  299. XNextEvent(_x11_display, &event);
  300. switch (event.type)
  301. {
  302. case ClientMessage:
  303. {
  304. if ((Atom)event.xclient.data.l[0] == _wm_delete_message)
  305. _queue.push_exit_event(0);
  306. break;
  307. }
  308. case ConfigureNotify:
  309. {
  310. _queue.push_metrics_event(event.xconfigure.x
  311. , event.xconfigure.y
  312. , event.xconfigure.width
  313. , event.xconfigure.height
  314. );
  315. break;
  316. }
  317. case ButtonPress:
  318. case ButtonRelease:
  319. {
  320. if (event.xbutton.button == Button4 || event.xbutton.button == Button5)
  321. {
  322. _queue.push_mouse_event(event.xbutton.x
  323. , event.xbutton.y
  324. , event.xbutton.button == Button4 ? 1.0f : -1.0f
  325. );
  326. break;
  327. }
  328. MouseButton::Enum mb;
  329. switch (event.xbutton.button)
  330. {
  331. case Button1: mb = MouseButton::LEFT; break;
  332. case Button2: mb = MouseButton::MIDDLE; break;
  333. case Button3: mb = MouseButton::RIGHT; break;
  334. default: mb = MouseButton::COUNT; break;
  335. }
  336. if (mb != MouseButton::COUNT)
  337. {
  338. _queue.push_mouse_event(event.xbutton.x
  339. , event.xbutton.y
  340. , mb
  341. , event.type == ButtonPress
  342. );
  343. }
  344. break;
  345. }
  346. case MotionNotify:
  347. {
  348. _queue.push_mouse_event(event.xmotion.x, event.xmotion.y);
  349. break;
  350. }
  351. case KeyPress:
  352. case KeyRelease:
  353. {
  354. KeySym keysym = XLookupKeysym(&event.xkey, 0);
  355. KeyboardButton::Enum kb = x11_translate_key(keysym);
  356. if (kb != KeyboardButton::COUNT)
  357. _queue.push_keyboard_event(kb, event.type == KeyPress);
  358. break;
  359. }
  360. case KeymapNotify:
  361. {
  362. XRefreshKeyboardMapping(&event.xmapping);
  363. break;
  364. }
  365. default:
  366. {
  367. break;
  368. }
  369. }
  370. }
  371. }
  372. public:
  373. Display* _x11_display;
  374. Window _x11_window;
  375. Cursor _x11_hidden_cursor;
  376. Atom _wm_delete_message;
  377. XRRScreenConfiguration* _screen_config;
  378. bool _x11_detectable_autorepeat;
  379. OsEventQueue _queue;
  380. };
  381. static LinuxDevice s_ldvc;
  382. bool next_event(OsEvent& ev)
  383. {
  384. return s_ldvc._queue.pop_event(ev);
  385. }
  386. } // namespace crown
  387. int main(int argc, char** argv)
  388. {
  389. using namespace crown;
  390. memory_globals::init();
  391. DeviceOptions opts(argc, argv);
  392. console_server_globals::init(opts.console_port(), opts.wait_console());
  393. bundle_compiler_globals::init(opts.source_dir(), opts.bundle_dir());
  394. bool do_continue = true;
  395. int exitcode = EXIT_SUCCESS;
  396. do_continue = bundle_compiler::main(opts.do_compile(), opts.do_continue(), opts.platform());
  397. if (do_continue)
  398. exitcode = crown::s_ldvc.run(&opts);
  399. bundle_compiler_globals::shutdown();
  400. console_server_globals::shutdown();
  401. memory_globals::shutdown();
  402. return exitcode;
  403. }
  404. #endif // CROWN_PLATFORM_LINUX