main_windows.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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_WINDOWS
  25. #include "os_event_queue.h"
  26. #include "os_window_windows.h"
  27. #include "thread.h"
  28. #include "crown.h"
  29. #include "command_line.h"
  30. #include "keyboard.h"
  31. #include "console_server.h"
  32. #include "bundle_compiler.h"
  33. #include "disk_filesystem.h"
  34. #include <bgfxplatform.h>
  35. #include <winsock2.h>
  36. #ifndef WIN32_LEAN_AND_MEAN
  37. #define WIN32_LEAN_AND_MEAN
  38. #endif
  39. #include <windowsx.h>
  40. namespace crown
  41. {
  42. static bool s_exit = false;
  43. struct MainThreadArgs
  44. {
  45. Filesystem* fs;
  46. ConfigSettings* cs;
  47. };
  48. int32_t func(void* data)
  49. {
  50. MainThreadArgs* args = (MainThreadArgs*)data;
  51. crown::init(*args->fs, *args->cs);
  52. crown::update();
  53. crown::shutdown();
  54. s_exit = true;
  55. return EXIT_SUCCESS;
  56. }
  57. //-----------------------------------------------------------------------------
  58. static KeyboardButton::Enum win_translate_key(int32_t winkey)
  59. {
  60. switch (winkey)
  61. {
  62. case VK_BACK: return KeyboardButton::BACKSPACE;
  63. case VK_TAB: return KeyboardButton::TAB;
  64. case VK_SPACE: return KeyboardButton::SPACE;
  65. case VK_ESCAPE: return KeyboardButton::ESCAPE;
  66. case VK_RETURN: return KeyboardButton::ENTER;
  67. case VK_F1: return KeyboardButton::F1;
  68. case VK_F2: return KeyboardButton::F2;
  69. case VK_F3: return KeyboardButton::F3;
  70. case VK_F4: return KeyboardButton::F4;
  71. case VK_F5: return KeyboardButton::F5;
  72. case VK_F6: return KeyboardButton::F6;
  73. case VK_F7: return KeyboardButton::F7;
  74. case VK_F8: return KeyboardButton::F8;
  75. case VK_F9: return KeyboardButton::F9;
  76. case VK_F10: return KeyboardButton::F10;
  77. case VK_F11: return KeyboardButton::F11;
  78. case VK_F12: return KeyboardButton::F12;
  79. case VK_HOME: return KeyboardButton::HOME;
  80. case VK_LEFT: return KeyboardButton::LEFT;
  81. case VK_UP: return KeyboardButton::UP;
  82. case VK_RIGHT: return KeyboardButton::RIGHT;
  83. case VK_DOWN: return KeyboardButton::DOWN;
  84. case VK_PRIOR: return KeyboardButton::PAGE_UP;
  85. case VK_NEXT: return KeyboardButton::PAGE_DOWN;
  86. case VK_LSHIFT: return KeyboardButton::LSHIFT;
  87. case VK_RSHIFT: return KeyboardButton::RSHIFT;
  88. case VK_LCONTROL: return KeyboardButton::LCONTROL;
  89. case VK_RCONTROL: return KeyboardButton::RCONTROL;
  90. case VK_CAPITAL: return KeyboardButton::CAPS_LOCK;
  91. case VK_LMENU: return KeyboardButton::LALT;
  92. case VK_RMENU: return KeyboardButton::RALT;
  93. case VK_LWIN: return KeyboardButton::LSUPER;
  94. case VK_RWIN: return KeyboardButton::RSUPER;
  95. case VK_NUMPAD0: return KeyboardButton::KP_0;
  96. case VK_NUMPAD1: return KeyboardButton::KP_1;
  97. case VK_NUMPAD2: return KeyboardButton::KP_2;
  98. case VK_NUMPAD3: return KeyboardButton::KP_3;
  99. case VK_NUMPAD4: return KeyboardButton::KP_4;
  100. case VK_NUMPAD5: return KeyboardButton::KP_5;
  101. case VK_NUMPAD6: return KeyboardButton::KP_6;
  102. case VK_NUMPAD7: return KeyboardButton::KP_7;
  103. case VK_NUMPAD8: return KeyboardButton::KP_8;
  104. case VK_NUMPAD9: return KeyboardButton::KP_9;
  105. case '0': return KeyboardButton::NUM_0;
  106. case '1': return KeyboardButton::NUM_1;
  107. case '2': return KeyboardButton::NUM_2;
  108. case '3': return KeyboardButton::NUM_3;
  109. case '4': return KeyboardButton::NUM_4;
  110. case '5': return KeyboardButton::NUM_5;
  111. case '6': return KeyboardButton::NUM_6;
  112. case '7': return KeyboardButton::NUM_7;
  113. case '8': return KeyboardButton::NUM_8;
  114. case '9': return KeyboardButton::NUM_9;
  115. case 'A': return KeyboardButton::A;
  116. case 'B': return KeyboardButton::B;
  117. case 'C': return KeyboardButton::C;
  118. case 'D': return KeyboardButton::D;
  119. case 'E': return KeyboardButton::E;
  120. case 'F': return KeyboardButton::F;
  121. case 'G': return KeyboardButton::G;
  122. case 'H': return KeyboardButton::H;
  123. case 'I': return KeyboardButton::I;
  124. case 'J': return KeyboardButton::J;
  125. case 'K': return KeyboardButton::K;
  126. case 'L': return KeyboardButton::L;
  127. case 'M': return KeyboardButton::M;
  128. case 'N': return KeyboardButton::N;
  129. case 'O': return KeyboardButton::O;
  130. case 'P': return KeyboardButton::P;
  131. case 'Q': return KeyboardButton::Q;
  132. case 'R': return KeyboardButton::R;
  133. case 'S': return KeyboardButton::S;
  134. case 'T': return KeyboardButton::T;
  135. case 'U': return KeyboardButton::U;
  136. case 'V': return KeyboardButton::V;
  137. case 'W': return KeyboardButton::W;
  138. case 'X': return KeyboardButton::X;
  139. case 'Y': return KeyboardButton::Y;
  140. case 'Z': return KeyboardButton::Z;
  141. default: return KeyboardButton::NONE;
  142. }
  143. }
  144. struct WindowsDevice
  145. {
  146. WindowsDevice()
  147. : m_hwnd(0)
  148. {
  149. }
  150. //-----------------------------------------------------------------------------
  151. int32_t run(Filesystem* fs, ConfigSettings* cs)
  152. {
  153. HINSTANCE instance = (HINSTANCE)GetModuleHandle(NULL);
  154. WNDCLASSEX wnd;
  155. memset(&wnd, 0, sizeof(wnd));
  156. wnd.cbSize = sizeof(wnd);
  157. wnd.style = CS_HREDRAW | CS_VREDRAW;
  158. wnd.lpfnWndProc = window_proc;
  159. wnd.hInstance = instance;
  160. wnd.hIcon = LoadIcon(instance, IDI_APPLICATION);
  161. wnd.hCursor = LoadCursor(instance, IDC_ARROW);
  162. wnd.lpszClassName = "crown";
  163. wnd.hIconSm = LoadIcon(instance, IDI_APPLICATION);
  164. RegisterClassExA(&wnd);
  165. m_hwnd = CreateWindowA(
  166. "crown",
  167. "Crown",
  168. WS_OVERLAPPEDWINDOW|WS_VISIBLE,
  169. 0,
  170. 0,
  171. cs->window_width,
  172. cs->window_height,
  173. 0,
  174. NULL,
  175. instance,
  176. 0);
  177. bgfx::winSetHwnd(m_hwnd);
  178. // Start main thread
  179. MainThreadArgs mta;
  180. mta.fs = fs;
  181. mta.cs = cs;
  182. Thread main_thread;
  183. main_thread.start(func, &mta);
  184. MSG msg;
  185. msg.message = WM_NULL;
  186. while (!s_exit)
  187. {
  188. while (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE) != 0)
  189. {
  190. TranslateMessage(&msg);
  191. DispatchMessage(&msg);
  192. }
  193. }
  194. main_thread.stop();
  195. DestroyWindow(m_hwnd);
  196. return EXIT_SUCCESS;
  197. }
  198. //-----------------------------------------------------------------------------
  199. LRESULT pump_events(HWND hwnd, UINT id, WPARAM wparam, LPARAM lparam)
  200. {
  201. switch (id)
  202. {
  203. case WM_DESTROY:
  204. {
  205. break;
  206. }
  207. case WM_QUIT:
  208. case WM_CLOSE:
  209. {
  210. m_queue.push_exit_event(0);
  211. break;
  212. }
  213. case WM_SIZE:
  214. {
  215. uint32_t width = GET_X_LPARAM(lparam);
  216. uint32_t height = GET_Y_LPARAM(lparam);
  217. m_queue.push_metrics_event(0, 0, width, height);
  218. break;
  219. }
  220. case WM_SYSCOMMAND:
  221. {
  222. switch (wparam)
  223. {
  224. case SC_MINIMIZE:
  225. case SC_RESTORE:
  226. {
  227. HWND parent = GetWindow(hwnd, GW_OWNER);
  228. if (NULL != parent)
  229. {
  230. PostMessage(parent, id, wparam, lparam);
  231. }
  232. }
  233. }
  234. break;
  235. }
  236. case WM_MOUSEMOVE:
  237. {
  238. int32_t mx = GET_X_LPARAM(lparam);
  239. int32_t my = GET_Y_LPARAM(lparam);
  240. m_queue.push_mouse_event(mx, my);
  241. break;
  242. }
  243. case WM_LBUTTONDOWN:
  244. case WM_LBUTTONUP:
  245. {
  246. int32_t mx = GET_X_LPARAM(lparam);
  247. int32_t my = GET_Y_LPARAM(lparam);
  248. m_queue.push_mouse_event(mx, my, MouseButton::LEFT, id == WM_LBUTTONDOWN);
  249. break;
  250. }
  251. case WM_RBUTTONUP:
  252. case WM_RBUTTONDOWN:
  253. {
  254. int32_t mx = GET_X_LPARAM(lparam);
  255. int32_t my = GET_Y_LPARAM(lparam);
  256. m_queue.push_mouse_event(mx, my, MouseButton::RIGHT, id == WM_RBUTTONDOWN);
  257. break;
  258. }
  259. case WM_MBUTTONDOWN:
  260. case WM_MBUTTONUP:
  261. {
  262. int32_t mx = GET_X_LPARAM(lparam);
  263. int32_t my = GET_Y_LPARAM(lparam);
  264. m_queue.push_mouse_event(mx, my, MouseButton::MIDDLE, id == WM_MBUTTONDOWN);
  265. break;
  266. }
  267. case WM_KEYDOWN:
  268. case WM_SYSKEYDOWN:
  269. case WM_KEYUP:
  270. case WM_SYSKEYUP:
  271. {
  272. KeyboardButton::Enum kb = win_translate_key(wparam & 0xff);
  273. int32_t modifier_mask = 0;
  274. if (kb == KeyboardButton::LSHIFT || kb == KeyboardButton::RSHIFT)
  275. {
  276. (id == WM_KEYDOWN || id == WM_SYSKEYDOWN) ? modifier_mask |= ModifierButton::SHIFT : modifier_mask &= ~ModifierButton::SHIFT;
  277. }
  278. else if (kb == KeyboardButton::LCONTROL || kb == KeyboardButton::RCONTROL)
  279. {
  280. (id == WM_KEYDOWN || id == WM_SYSKEYDOWN) ? modifier_mask |= ModifierButton::CTRL : modifier_mask &= ~ModifierButton::CTRL;
  281. }
  282. else if (kb == KeyboardButton::LALT || kb == KeyboardButton::RALT)
  283. {
  284. (id == WM_KEYDOWN || id == WM_SYSKEYDOWN) ? modifier_mask |= ModifierButton::ALT : modifier_mask &= ~ModifierButton::ALT;
  285. }
  286. m_queue.push_keyboard_event(modifier_mask, kb, (id == WM_KEYDOWN || id == WM_SYSKEYDOWN));
  287. break;
  288. }
  289. default:
  290. break;
  291. }
  292. return DefWindowProc(hwnd, id, wparam, lparam);
  293. }
  294. private:
  295. static LRESULT CALLBACK window_proc(HWND hwnd, UINT id, WPARAM wparam, LPARAM lparam);
  296. public:
  297. HWND m_hwnd;
  298. HDC m_hdc;
  299. uint32_t m_parent_window_handle;
  300. OsEventQueue m_queue;
  301. };
  302. static WindowsDevice s_wdvc;
  303. LRESULT CALLBACK WindowsDevice::window_proc(HWND hwnd, UINT id, WPARAM wparam, LPARAM lparam)
  304. {
  305. return s_wdvc.pump_events(hwnd, id, wparam, lparam);
  306. }
  307. bool next_event(OsEvent& ev)
  308. {
  309. return s_wdvc.m_queue.pop_event(ev);
  310. }
  311. } // namespace crown
  312. int main(int argc, char** argv)
  313. {
  314. using namespace crown;
  315. WSADATA WsaData;
  316. int res = WSAStartup(MAKEWORD(2, 2), &WsaData);
  317. CE_ASSERT(res == 0, "Unable to initialize socket");
  318. CE_UNUSED(WsaData);
  319. CE_UNUSED(res);
  320. CommandLineSettings cls = parse_command_line(argc, argv);
  321. memory_globals::init();
  322. DiskFilesystem src_fs(cls.source_dir);
  323. ConfigSettings cs = parse_config_file(src_fs);
  324. console_server_globals::init();
  325. console_server_globals::console().init(cs.console_port, cls.wait_console);
  326. bundle_compiler_globals::init();
  327. bool do_continue = true;
  328. int exitcode = EXIT_SUCCESS;
  329. do_continue = bundle_compiler::main(cls);
  330. if (do_continue)
  331. {
  332. DiskFilesystem dst_fs(cls.bundle_dir);
  333. exitcode = crown::s_wdvc.run(&dst_fs, &cs);
  334. }
  335. bundle_compiler_globals::shutdown();
  336. console_server_globals::shutdown();
  337. memory_globals::shutdown();
  338. WSACleanup();
  339. return exitcode;
  340. }
  341. #endif // CROWN_PLATFORM_WINDOWS