entry_windows.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /*
  2. * Copyright 2011-2013 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include "entry_p.h"
  6. #if ENTRY_CONFIG_USE_NATIVE && BX_PLATFORM_WINDOWS
  7. #include <bgfxplatform.h>
  8. #include <bx/uint32_t.h>
  9. #include <bx/thread.h>
  10. #include <windowsx.h>
  11. #define WM_USER_SET_WINDOW_SIZE (WM_USER+0)
  12. #define WM_USER_TOGGLE_WINDOW_FRAME (WM_USER+1)
  13. #define WM_USER_MOUSE_LOCK (WM_USER+2)
  14. namespace entry
  15. {
  16. struct TranslateKeyModifiers
  17. {
  18. int m_vk;
  19. Modifier::Enum m_modifier;
  20. };
  21. static const TranslateKeyModifiers s_translateKeyModifiers[8] =
  22. {
  23. { VK_LMENU, Modifier::LeftAlt },
  24. { VK_RMENU, Modifier::RightAlt },
  25. { VK_LCONTROL, Modifier::LeftCtrl },
  26. { VK_RCONTROL, Modifier::RightCtrl },
  27. { VK_LSHIFT, Modifier::LeftShift },
  28. { VK_RSHIFT, Modifier::RightShift },
  29. { VK_LWIN, Modifier::LeftMeta },
  30. { VK_RWIN, Modifier::RightMeta },
  31. };
  32. static uint8_t translateKeyModifiers()
  33. {
  34. uint8_t modifiers = 0;
  35. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateKeyModifiers); ++ii)
  36. {
  37. const TranslateKeyModifiers& tkm = s_translateKeyModifiers[ii];
  38. modifiers |= 0 > GetKeyState(tkm.m_vk) ? tkm.m_modifier : Modifier::None;
  39. }
  40. return modifiers;
  41. }
  42. static uint8_t s_translateKey[256];
  43. static Key::Enum translateKey(WPARAM _wparam)
  44. {
  45. return (Key::Enum)s_translateKey[_wparam&0xff];
  46. }
  47. struct MainThreadEntry
  48. {
  49. int m_argc;
  50. char** m_argv;
  51. static int32_t threadFunc(void* _userData);
  52. };
  53. struct Context
  54. {
  55. Context()
  56. : m_frame(true)
  57. , m_init(false)
  58. , m_exit(false)
  59. , m_mouseLock(false)
  60. {
  61. memset(s_translateKey, 0, sizeof(s_translateKey) );
  62. s_translateKey[VK_ESCAPE] = Key::Esc;
  63. s_translateKey[VK_RETURN] = Key::Return;
  64. s_translateKey[VK_TAB] = Key::Tab;
  65. s_translateKey[VK_BACK] = Key::Backspace;
  66. s_translateKey[VK_SPACE] = Key::Space;
  67. s_translateKey[VK_UP] = Key::Up;
  68. s_translateKey[VK_DOWN] = Key::Down;
  69. s_translateKey[VK_LEFT] = Key::Left;
  70. s_translateKey[VK_RIGHT] = Key::Right;
  71. s_translateKey[VK_PRIOR] = Key::PageUp;
  72. s_translateKey[VK_NEXT] = Key::PageUp;
  73. s_translateKey[VK_HOME] = Key::Home;
  74. s_translateKey[VK_END] = Key::End;
  75. s_translateKey[VK_SNAPSHOT] = Key::Print;
  76. s_translateKey[VK_OEM_PLUS] = Key::Plus;
  77. s_translateKey[VK_OEM_MINUS] = Key::Minus;
  78. s_translateKey[VK_F1] = Key::F1;
  79. s_translateKey[VK_F2] = Key::F2;
  80. s_translateKey[VK_F3] = Key::F3;
  81. s_translateKey[VK_F4] = Key::F4;
  82. s_translateKey[VK_F5] = Key::F5;
  83. s_translateKey[VK_F6] = Key::F6;
  84. s_translateKey[VK_F7] = Key::F7;
  85. s_translateKey[VK_F8] = Key::F8;
  86. s_translateKey[VK_F9] = Key::F9;
  87. s_translateKey[VK_F10] = Key::F10;
  88. s_translateKey[VK_F11] = Key::F11;
  89. s_translateKey[VK_F12] = Key::F12;
  90. s_translateKey[VK_NUMPAD0] = Key::NumPad0;
  91. s_translateKey[VK_NUMPAD1] = Key::NumPad1;
  92. s_translateKey[VK_NUMPAD2] = Key::NumPad2;
  93. s_translateKey[VK_NUMPAD3] = Key::NumPad3;
  94. s_translateKey[VK_NUMPAD4] = Key::NumPad4;
  95. s_translateKey[VK_NUMPAD5] = Key::NumPad5;
  96. s_translateKey[VK_NUMPAD6] = Key::NumPad6;
  97. s_translateKey[VK_NUMPAD7] = Key::NumPad7;
  98. s_translateKey[VK_NUMPAD8] = Key::NumPad8;
  99. s_translateKey[VK_NUMPAD9] = Key::NumPad9;
  100. s_translateKey['0'] = Key::Key0;
  101. s_translateKey['1'] = Key::Key1;
  102. s_translateKey['2'] = Key::Key2;
  103. s_translateKey['3'] = Key::Key3;
  104. s_translateKey['4'] = Key::Key4;
  105. s_translateKey['5'] = Key::Key5;
  106. s_translateKey['6'] = Key::Key6;
  107. s_translateKey['7'] = Key::Key7;
  108. s_translateKey['8'] = Key::Key8;
  109. s_translateKey['9'] = Key::Key9;
  110. s_translateKey['A'] = Key::KeyA;
  111. s_translateKey['B'] = Key::KeyB;
  112. s_translateKey['C'] = Key::KeyC;
  113. s_translateKey['D'] = Key::KeyD;
  114. s_translateKey['E'] = Key::KeyE;
  115. s_translateKey['F'] = Key::KeyF;
  116. s_translateKey['G'] = Key::KeyG;
  117. s_translateKey['H'] = Key::KeyH;
  118. s_translateKey['I'] = Key::KeyI;
  119. s_translateKey['J'] = Key::KeyJ;
  120. s_translateKey['K'] = Key::KeyK;
  121. s_translateKey['L'] = Key::KeyL;
  122. s_translateKey['M'] = Key::KeyM;
  123. s_translateKey['N'] = Key::KeyN;
  124. s_translateKey['O'] = Key::KeyO;
  125. s_translateKey['P'] = Key::KeyP;
  126. s_translateKey['Q'] = Key::KeyQ;
  127. s_translateKey['R'] = Key::KeyR;
  128. s_translateKey['S'] = Key::KeyS;
  129. s_translateKey['T'] = Key::KeyT;
  130. s_translateKey['U'] = Key::KeyU;
  131. s_translateKey['V'] = Key::KeyV;
  132. s_translateKey['W'] = Key::KeyW;
  133. s_translateKey['X'] = Key::KeyX;
  134. s_translateKey['Y'] = Key::KeyY;
  135. s_translateKey['Z'] = Key::KeyZ;
  136. }
  137. int32_t run(int _argc, char** _argv)
  138. {
  139. HINSTANCE instance = (HINSTANCE)GetModuleHandle(NULL);
  140. WNDCLASSEX wnd;
  141. memset(&wnd, 0, sizeof(wnd) );
  142. wnd.cbSize = sizeof(wnd);
  143. wnd.lpfnWndProc = DefWindowProc;
  144. wnd.hInstance = instance;
  145. wnd.hIcon = LoadIcon(instance, IDI_APPLICATION);
  146. wnd.hCursor = LoadCursor(instance, IDC_ARROW);
  147. wnd.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  148. wnd.lpszClassName = "bgfx_letterbox";
  149. wnd.hIconSm = LoadIcon(instance, IDI_APPLICATION);
  150. RegisterClassExA(&wnd);
  151. memset(&wnd, 0, sizeof(wnd) );
  152. wnd.cbSize = sizeof(wnd);
  153. wnd.style = CS_HREDRAW | CS_VREDRAW;
  154. wnd.lpfnWndProc = wndProc;
  155. wnd.hInstance = instance;
  156. wnd.hIcon = LoadIcon(instance, IDI_APPLICATION);
  157. wnd.hCursor = LoadCursor(instance, IDC_ARROW);
  158. wnd.lpszClassName = "bgfx";
  159. wnd.hIconSm = LoadIcon(instance, IDI_APPLICATION);
  160. RegisterClassExA(&wnd);
  161. HWND hwnd = CreateWindowA("bgfx_letterbox"
  162. , "BGFX"
  163. , WS_POPUP|WS_SYSMENU
  164. , -32000
  165. , -32000
  166. , 0
  167. , 0
  168. , NULL
  169. , NULL
  170. , instance
  171. , 0
  172. );
  173. m_hwnd = CreateWindowA("bgfx"
  174. , "BGFX"
  175. , WS_OVERLAPPEDWINDOW|WS_VISIBLE
  176. , 0
  177. , 0
  178. , ENTRY_DEFAULT_WIDTH
  179. , ENTRY_DEFAULT_HEIGHT
  180. , hwnd
  181. , NULL
  182. , instance
  183. , 0
  184. );
  185. bgfx::winSetHwnd(m_hwnd);
  186. adjust(ENTRY_DEFAULT_WIDTH, ENTRY_DEFAULT_HEIGHT, true);
  187. m_width = ENTRY_DEFAULT_WIDTH;
  188. m_height = ENTRY_DEFAULT_HEIGHT;
  189. m_oldWidth = ENTRY_DEFAULT_WIDTH;
  190. m_oldHeight = ENTRY_DEFAULT_HEIGHT;
  191. MainThreadEntry mte;
  192. mte.m_argc = _argc;
  193. mte.m_argv = _argv;
  194. bx::Thread thread;
  195. thread.init(mte.threadFunc, &mte);
  196. m_init = true;
  197. m_eventQueue.postSizeEvent(m_width, m_height);
  198. MSG msg;
  199. msg.message = WM_NULL;
  200. while (!m_exit)
  201. {
  202. WaitMessage();
  203. while (0 != PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE) )
  204. {
  205. TranslateMessage(&msg);
  206. DispatchMessage(&msg);
  207. }
  208. }
  209. thread.shutdown();
  210. DestroyWindow(m_hwnd);
  211. DestroyWindow(hwnd);
  212. return 0;
  213. }
  214. LRESULT process(HWND _hwnd, UINT _id, WPARAM _wparam, LPARAM _lparam)
  215. {
  216. if (m_init)
  217. {
  218. switch (_id)
  219. {
  220. case WM_USER_SET_WINDOW_SIZE:
  221. {
  222. uint32_t width = GET_X_LPARAM(_lparam);
  223. uint32_t height = GET_Y_LPARAM(_lparam);
  224. adjust(width, height, true);
  225. }
  226. break;
  227. case WM_USER_TOGGLE_WINDOW_FRAME:
  228. {
  229. if (m_frame)
  230. {
  231. m_oldWidth = m_width;
  232. m_oldHeight = m_height;
  233. }
  234. adjust(m_oldWidth, m_oldHeight, !m_frame);
  235. }
  236. break;
  237. case WM_USER_MOUSE_LOCK:
  238. setMouseLock(!!_lparam);
  239. break;
  240. case WM_DESTROY:
  241. break;
  242. case WM_QUIT:
  243. case WM_CLOSE:
  244. m_exit = true;
  245. m_eventQueue.postExitEvent();
  246. break;
  247. case WM_SIZING:
  248. {
  249. RECT& rect = *(RECT*)_lparam;
  250. uint32_t width = rect.right - rect.left - m_frameWidth;
  251. uint32_t height = rect.bottom - rect.top - m_frameHeight;
  252. //Recalculate size according to aspect ratio
  253. switch (_wparam)
  254. {
  255. case WMSZ_LEFT:
  256. case WMSZ_RIGHT:
  257. {
  258. float aspectRatio = 1.0f/m_aspectRatio;
  259. width = bx::uint32_max(ENTRY_DEFAULT_WIDTH/4, width);
  260. height = uint32_t(float(width)*aspectRatio);
  261. }
  262. break;
  263. default:
  264. {
  265. float aspectRatio = m_aspectRatio;
  266. height = bx::uint32_max(ENTRY_DEFAULT_HEIGHT/4, height);
  267. width = uint32_t(float(height)*aspectRatio);
  268. }
  269. break;
  270. }
  271. //Recalculate position using different anchor points
  272. switch(_wparam)
  273. {
  274. case WMSZ_LEFT:
  275. case WMSZ_TOPLEFT:
  276. case WMSZ_BOTTOMLEFT:
  277. rect.left = rect.right - width - m_frameWidth;
  278. rect.bottom = rect.top + height + m_frameHeight;
  279. break;
  280. default:
  281. rect.right = rect.left + width + m_frameWidth;
  282. rect.bottom = rect.top + height + m_frameHeight;
  283. break;
  284. }
  285. m_eventQueue.postSizeEvent(m_width, m_height);
  286. }
  287. return 0;
  288. case WM_SIZE:
  289. {
  290. uint32_t width = GET_X_LPARAM(_lparam);
  291. uint32_t height = GET_Y_LPARAM(_lparam);
  292. m_width = width;
  293. m_height = height;
  294. m_eventQueue.postSizeEvent(m_width, m_height);
  295. }
  296. break;
  297. case WM_SYSCOMMAND:
  298. switch (_wparam)
  299. {
  300. case SC_MINIMIZE:
  301. case SC_RESTORE:
  302. {
  303. HWND parent = GetWindow(_hwnd, GW_OWNER);
  304. if (NULL != parent)
  305. {
  306. PostMessage(parent, _id, _wparam, _lparam);
  307. }
  308. }
  309. }
  310. break;
  311. case WM_MOUSEMOVE:
  312. {
  313. int32_t mx = GET_X_LPARAM(_lparam);
  314. int32_t my = GET_Y_LPARAM(_lparam);
  315. if (m_mouseLock)
  316. {
  317. mx -= m_mx;
  318. my -= m_my;
  319. if (0 == mx
  320. && 0 == my)
  321. {
  322. break;
  323. }
  324. setMousePos(m_mx, m_my);
  325. }
  326. m_eventQueue.postMouseEvent(mx, my);
  327. }
  328. break;
  329. case WM_LBUTTONDOWN:
  330. case WM_LBUTTONUP:
  331. case WM_LBUTTONDBLCLK:
  332. {
  333. int32_t mx = GET_X_LPARAM(_lparam);
  334. int32_t my = GET_Y_LPARAM(_lparam);
  335. m_eventQueue.postMouseEvent(mx, my, MouseButton::Left, _id == WM_LBUTTONDOWN);
  336. }
  337. break;
  338. case WM_MBUTTONDOWN:
  339. case WM_MBUTTONUP:
  340. case WM_MBUTTONDBLCLK:
  341. {
  342. int32_t mx = GET_X_LPARAM(_lparam);
  343. int32_t my = GET_Y_LPARAM(_lparam);
  344. m_eventQueue.postMouseEvent(mx, my, MouseButton::Middle, _id == WM_MBUTTONDOWN);
  345. }
  346. break;
  347. case WM_RBUTTONUP:
  348. case WM_RBUTTONDOWN:
  349. case WM_RBUTTONDBLCLK:
  350. {
  351. int32_t mx = GET_X_LPARAM(_lparam);
  352. int32_t my = GET_Y_LPARAM(_lparam);
  353. m_eventQueue.postMouseEvent(mx, my, MouseButton::Right, _id == WM_RBUTTONDOWN);
  354. }
  355. break;
  356. case WM_KEYDOWN:
  357. case WM_SYSKEYDOWN:
  358. case WM_KEYUP:
  359. case WM_SYSKEYUP:
  360. {
  361. uint8_t modifiers = translateKeyModifiers();
  362. Key::Enum key = translateKey(_wparam);
  363. m_eventQueue.postKeyEvent(key, modifiers, _id == WM_KEYDOWN || _id == WM_SYSKEYDOWN);
  364. }
  365. break;
  366. default:
  367. break;
  368. }
  369. }
  370. return DefWindowProc(_hwnd, _id, _wparam, _lparam);
  371. }
  372. void adjust(uint32_t _width, uint32_t _height, bool _windowFrame)
  373. {
  374. m_width = _width;
  375. m_height = _height;
  376. m_aspectRatio = float(_width)/float(_height);
  377. ShowWindow(m_hwnd, SW_SHOWNORMAL);
  378. RECT rect;
  379. RECT newrect = {0, 0, (LONG)_width, (LONG)_height};
  380. DWORD style = WS_POPUP|WS_SYSMENU;
  381. if (m_frame)
  382. {
  383. GetWindowRect(m_hwnd, &m_rect);
  384. m_style = GetWindowLong(m_hwnd, GWL_STYLE);
  385. }
  386. if (_windowFrame)
  387. {
  388. rect = m_rect;
  389. style = m_style;
  390. }
  391. else
  392. {
  393. #if defined(__MINGW32__)
  394. rect = m_rect;
  395. style = m_style;
  396. #else
  397. HMONITOR monitor = MonitorFromWindow(m_hwnd, MONITOR_DEFAULTTONEAREST);
  398. MONITORINFO mi;
  399. mi.cbSize = sizeof(mi);
  400. GetMonitorInfo(monitor, &mi);
  401. newrect = mi.rcMonitor;
  402. rect = mi.rcMonitor;
  403. #endif // !defined(__MINGW__)
  404. }
  405. SetWindowLong(m_hwnd, GWL_STYLE, style);
  406. uint32_t prewidth = newrect.right - newrect.left;
  407. uint32_t preheight = newrect.bottom - newrect.top;
  408. AdjustWindowRect(&newrect, style, FALSE);
  409. m_frameWidth = (newrect.right - newrect.left) - prewidth;
  410. m_frameHeight = (newrect.bottom - newrect.top) - preheight;
  411. UpdateWindow(m_hwnd);
  412. if (rect.left == -32000
  413. || rect.top == -32000)
  414. {
  415. rect.left = 0;
  416. rect.top = 0;
  417. }
  418. int32_t left = rect.left;
  419. int32_t top = rect.top;
  420. int32_t width = (newrect.right-newrect.left);
  421. int32_t height = (newrect.bottom-newrect.top);
  422. if (!_windowFrame)
  423. {
  424. float aspectRatio = 1.0f/m_aspectRatio;
  425. width = bx::uint32_max(ENTRY_DEFAULT_WIDTH/4, width);
  426. height = uint32_t(float(width)*aspectRatio);
  427. left = newrect.left+(newrect.right-newrect.left-width)/2;
  428. top = newrect.top+(newrect.bottom-newrect.top-height)/2;
  429. }
  430. HWND parent = GetWindow(m_hwnd, GW_OWNER);
  431. if (NULL != parent)
  432. {
  433. if (_windowFrame)
  434. {
  435. SetWindowPos(parent
  436. , HWND_TOP
  437. , -32000
  438. , -32000
  439. , 0
  440. , 0
  441. , SWP_SHOWWINDOW
  442. );
  443. }
  444. else
  445. {
  446. SetWindowPos(parent
  447. , HWND_TOP
  448. , newrect.left
  449. , newrect.top
  450. , newrect.right-newrect.left
  451. , newrect.bottom-newrect.top
  452. , SWP_SHOWWINDOW
  453. );
  454. }
  455. }
  456. SetWindowPos(m_hwnd
  457. , HWND_TOP
  458. , left
  459. , top
  460. , width
  461. , height
  462. , SWP_SHOWWINDOW
  463. );
  464. ShowWindow(m_hwnd, SW_RESTORE);
  465. m_frame = _windowFrame;
  466. }
  467. void setMousePos(int32_t _mx, int32_t _my)
  468. {
  469. POINT pt = { _mx, _my };
  470. ClientToScreen(m_hwnd, &pt);
  471. SetCursorPos(pt.x, pt.y);
  472. }
  473. void setMouseLock(bool _lock)
  474. {
  475. if (_lock != m_mouseLock)
  476. {
  477. if (_lock)
  478. {
  479. m_mx = m_width/2;
  480. m_my = m_height/2;
  481. ShowCursor(false);
  482. setMousePos(m_mx, m_my);
  483. }
  484. else
  485. {
  486. setMousePos(m_mx, m_my);
  487. ShowCursor(true);
  488. }
  489. m_mouseLock = _lock;
  490. }
  491. }
  492. static LRESULT CALLBACK wndProc(HWND _hwnd, UINT _id, WPARAM _wparam, LPARAM _lparam);
  493. EventQueue m_eventQueue;
  494. HWND m_hwnd;
  495. RECT m_rect;
  496. DWORD m_style;
  497. uint32_t m_width;
  498. uint32_t m_height;
  499. uint32_t m_oldWidth;
  500. uint32_t m_oldHeight;
  501. uint32_t m_frameWidth;
  502. uint32_t m_frameHeight;
  503. float m_aspectRatio;
  504. int32_t m_mx;
  505. int32_t m_my;
  506. bool m_frame;
  507. bool m_mouseLock;
  508. bool m_init;
  509. bool m_exit;
  510. };
  511. static Context s_ctx;
  512. LRESULT CALLBACK Context::wndProc(HWND _hwnd, UINT _id, WPARAM _wparam, LPARAM _lparam)
  513. {
  514. return s_ctx.process(_hwnd, _id, _wparam, _lparam);
  515. }
  516. const Event* poll()
  517. {
  518. return s_ctx.m_eventQueue.poll();
  519. }
  520. void release(const Event* _event)
  521. {
  522. s_ctx.m_eventQueue.release(_event);
  523. }
  524. void setWindowSize(uint32_t _width, uint32_t _height)
  525. {
  526. PostMessage(s_ctx.m_hwnd, WM_USER_SET_WINDOW_SIZE, 0, (_height<<16) | (_width&0xffff) );
  527. }
  528. void toggleWindowFrame()
  529. {
  530. PostMessage(s_ctx.m_hwnd, WM_USER_TOGGLE_WINDOW_FRAME, 0, 0);
  531. }
  532. void setMouseLock(bool _lock)
  533. {
  534. PostMessage(s_ctx.m_hwnd, WM_USER_MOUSE_LOCK, 0, _lock);
  535. }
  536. int32_t MainThreadEntry::threadFunc(void* _userData)
  537. {
  538. MainThreadEntry* self = (MainThreadEntry*)_userData;
  539. int32_t result = main(self->m_argc, self->m_argv);
  540. PostMessage(s_ctx.m_hwnd, WM_QUIT, 0, 0);
  541. return result;
  542. }
  543. } // namespace entry
  544. int main(int _argc, char** _argv)
  545. {
  546. using namespace entry;
  547. return s_ctx.run(_argc, _argv);
  548. }
  549. #endif // BX_PLATFORM_WINDOWS