main_linux.cpp 13 KB

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