main_linux.cpp 13 KB

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