main_linux.cpp 13 KB

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