main_linux.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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 bool s_exit = false;
  153. struct MainThreadArgs
  154. {
  155. Filesystem* fs;
  156. ConfigSettings* cs;
  157. };
  158. int32_t func(void* data)
  159. {
  160. MainThreadArgs* args = (MainThreadArgs*) data;
  161. crown::init(*args->fs, *args->cs);
  162. crown::update();
  163. crown::shutdown();
  164. s_exit = true;
  165. return EXIT_SUCCESS;
  166. }
  167. struct LinuxDevice
  168. {
  169. LinuxDevice()
  170. : _x11_display(NULL)
  171. , _x11_window(None)
  172. , _x11_parent_window(None)
  173. , _x11_hidden_cursor(None)
  174. , _screen_config(NULL)
  175. {
  176. }
  177. int32_t run(Filesystem* fs, ConfigSettings* cs)
  178. {
  179. // Create main window
  180. XInitThreads();
  181. _x11_display = XOpenDisplay(NULL);
  182. CE_ASSERT(_x11_display != NULL, "Unable to open X11 display");
  183. int screen = DefaultScreen(_x11_display);
  184. int depth = DefaultDepth(_x11_display, screen);
  185. Visual* visual = DefaultVisual(_x11_display, screen);
  186. _x11_parent_window = (cs->parent_window == 0) ? RootWindow(_x11_display, screen) :
  187. (Window) cs->parent_window;
  188. // Create main window
  189. XSetWindowAttributes win_attribs;
  190. win_attribs.background_pixmap = 0;
  191. win_attribs.border_pixel = 0;
  192. win_attribs.event_mask = FocusChangeMask
  193. | StructureNotifyMask
  194. | KeyPressMask
  195. | KeyReleaseMask
  196. | ButtonPressMask
  197. | ButtonReleaseMask
  198. | PointerMotionMask;
  199. _x11_window = XCreateWindow(_x11_display,
  200. _x11_parent_window,
  201. 0, 0,
  202. cs->window_width,
  203. cs->window_height,
  204. 0,
  205. depth,
  206. InputOutput,
  207. visual,
  208. CWBorderPixel | CWEventMask,
  209. &win_attribs
  210. );
  211. CE_ASSERT(_x11_window != None, "Unable to create X window");
  212. // Do we have detectable autorepeat?
  213. Bool detectable;
  214. _x11_detectable_autorepeat = (bool) XkbSetDetectableAutoRepeat(_x11_display, true, &detectable);
  215. // Build hidden cursor
  216. Pixmap bm_no;
  217. XColor black, dummy;
  218. Colormap colormap;
  219. static char no_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  220. colormap = XDefaultColormap(_x11_display, screen);
  221. XAllocNamedColor(_x11_display, colormap, "black", &black, &dummy);
  222. bm_no = XCreateBitmapFromData(_x11_display, _x11_window, no_data, 8, 8);
  223. _x11_hidden_cursor = XCreatePixmapCursor(_x11_display, bm_no, bm_no, &black, &black, 0, 0);
  224. _wm_delete_message = XInternAtom(_x11_display, "WM_DELETE_WINDOW", False);
  225. XSetWMProtocols(_x11_display, _x11_window, &_wm_delete_message, 1);
  226. oswindow_set_window(_x11_display, _x11_window);
  227. bgfx::x11SetDisplayWindow(_x11_display, _x11_window);
  228. XMapRaised(_x11_display, _x11_window);
  229. // Get screen configuration
  230. _screen_config = XRRGetScreenInfo(_x11_display, RootWindow(_x11_display, screen));
  231. Rotation rr_old_rot;
  232. const SizeID rr_old_sizeid = XRRConfigCurrentConfiguration(_screen_config, &rr_old_rot);
  233. // Start main thread
  234. MainThreadArgs mta;
  235. mta.fs = fs;
  236. mta.cs = cs;
  237. Thread main_thread;
  238. main_thread.start(func, &mta);
  239. while (!s_exit)
  240. {
  241. pump_events();
  242. }
  243. main_thread.stop();
  244. // Restore previous screen configuration if changed
  245. Rotation rr_cur_rot;
  246. const SizeID rr_cur_sizeid = XRRConfigCurrentConfiguration(_screen_config, &rr_cur_rot);
  247. if (rr_cur_rot != rr_old_rot || rr_cur_sizeid != rr_old_sizeid)
  248. {
  249. XRRSetScreenConfig(_x11_display,
  250. _screen_config,
  251. RootWindow(_x11_display, screen),
  252. rr_old_sizeid,
  253. rr_old_rot,
  254. CurrentTime);
  255. }
  256. XRRFreeScreenConfigInfo(_screen_config);
  257. XDestroyWindow(_x11_display, _x11_window);
  258. XCloseDisplay(_x11_display);
  259. return EXIT_SUCCESS;
  260. }
  261. void pump_events()
  262. {
  263. while (XPending(_x11_display))
  264. {
  265. XEvent event;
  266. XNextEvent(_x11_display, &event);
  267. switch (event.type)
  268. {
  269. case ClientMessage:
  270. {
  271. if ((Atom)event.xclient.data.l[0] == _wm_delete_message)
  272. {
  273. _queue.push_exit_event(0);
  274. }
  275. break;
  276. }
  277. case ConfigureNotify:
  278. {
  279. _queue.push_metrics_event(event.xconfigure.x, event.xconfigure.y,
  280. event.xconfigure.width, event.xconfigure.height);
  281. break;
  282. }
  283. case ButtonPress:
  284. case ButtonRelease:
  285. {
  286. if (event.xbutton.button == Button4 || event.xbutton.button == Button5)
  287. {
  288. _queue.push_mouse_event(event.xbutton.x, event.xbutton.y,
  289. event.xbutton.button == Button4 ? 1.0f : -1.0f);
  290. break;
  291. }
  292. MouseButton::Enum mb;
  293. switch (event.xbutton.button)
  294. {
  295. case Button1: mb = MouseButton::LEFT; break;
  296. case Button2: mb = MouseButton::MIDDLE; break;
  297. case Button3: mb = MouseButton::RIGHT; break;
  298. default: mb = MouseButton::NONE; break;
  299. }
  300. if (mb != MouseButton::NONE)
  301. {
  302. _queue.push_mouse_event(event.xbutton.x, event.xbutton.y, mb, event.type == ButtonPress);
  303. }
  304. break;
  305. }
  306. case MotionNotify:
  307. {
  308. _queue.push_mouse_event(event.xmotion.x, event.xmotion.y);
  309. break;
  310. }
  311. case KeyPress:
  312. case KeyRelease:
  313. {
  314. KeySym keysym = XLookupKeysym(&event.xkey, 0);
  315. KeyboardButton::Enum kb = x11_translate_key(keysym);
  316. // Check if any modifier key is pressed or released
  317. int32_t modifier_mask = 0;
  318. if (kb == KeyboardButton::LSHIFT || kb == KeyboardButton::RSHIFT)
  319. {
  320. (event.type == KeyPress) ? modifier_mask |= ModifierButton::SHIFT : modifier_mask &= ~ModifierButton::SHIFT;
  321. }
  322. else if (kb == KeyboardButton::LCONTROL || kb == KeyboardButton::RCONTROL)
  323. {
  324. (event.type == KeyPress) ? modifier_mask |= ModifierButton::CTRL : modifier_mask &= ~ModifierButton::CTRL;
  325. }
  326. else if (kb == KeyboardButton::LALT || kb == KeyboardButton::RALT)
  327. {
  328. (event.type == KeyPress) ? modifier_mask |= ModifierButton::ALT : modifier_mask &= ~ModifierButton::ALT;
  329. }
  330. _queue.push_keyboard_event(modifier_mask, kb, event.type == KeyPress);
  331. break;
  332. }
  333. case KeymapNotify:
  334. {
  335. XRefreshKeyboardMapping(&event.xmapping);
  336. break;
  337. }
  338. default:
  339. {
  340. break;
  341. }
  342. }
  343. }
  344. }
  345. public:
  346. Display* _x11_display;
  347. Window _x11_window;
  348. Window _x11_parent_window;
  349. Cursor _x11_hidden_cursor;
  350. Atom _wm_delete_message;
  351. XRRScreenConfiguration* _screen_config;
  352. bool _x11_detectable_autorepeat;
  353. OsEventQueue _queue;
  354. };
  355. static LinuxDevice s_ldvc;
  356. bool next_event(OsEvent& ev)
  357. {
  358. return s_ldvc._queue.pop_event(ev);
  359. }
  360. } // namespace crown
  361. int main(int argc, char** argv)
  362. {
  363. using namespace crown;
  364. ConfigSettings cs;
  365. parse_command_line(argc, argv, cs);
  366. memory_globals::init();
  367. {
  368. DiskFilesystem fs(cs.source_dir);
  369. parse_config_file(fs, cs);
  370. }
  371. console_server_globals::init(cs.console_port, cs.wait_console);
  372. bundle_compiler_globals::init(cs.source_dir, cs.bundle_dir);
  373. bool do_continue = true;
  374. int exitcode = EXIT_SUCCESS;
  375. do_continue = bundle_compiler::main(cs.do_compile, cs.do_continue, cs.platform);
  376. if (do_continue)
  377. {
  378. DiskFilesystem dst_fs(cs.bundle_dir);
  379. exitcode = crown::s_ldvc.run(&dst_fs, &cs);
  380. }
  381. bundle_compiler_globals::shutdown();
  382. console_server_globals::shutdown();
  383. memory_globals::shutdown();
  384. return exitcode;
  385. }
  386. #endif // CROWN_PLATFORM_LINUX