entry_windows.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  1. /*
  2. * Copyright 2011-2017 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include "entry_p.h"
  6. #if ENTRY_CONFIG_USE_NATIVE && BX_PLATFORM_WINDOWS
  7. #include <bgfx/platform.h>
  8. #include <bx/uint32_t.h>
  9. #include <bx/thread.h>
  10. #include <bx/mutex.h>
  11. #include <bx/handlealloc.h>
  12. #include <bx/timer.h>
  13. #include <tinystl/allocator.h>
  14. #include <tinystl/string.h>
  15. #include <windowsx.h>
  16. #include <xinput.h>
  17. #ifndef XINPUT_GAMEPAD_GUIDE
  18. # define XINPUT_GAMEPAD_GUIDE 0x400
  19. #endif // XINPUT_GAMEPAD_GUIDE
  20. #ifndef XINPUT_DLL_A
  21. # define XINPUT_DLL_A "xinput.dll"
  22. #endif // XINPUT_DLL_A
  23. namespace entry
  24. {
  25. ///
  26. inline void winSetHwnd(::HWND _window)
  27. {
  28. bgfx::PlatformData pd;
  29. bx::memSet(&pd, 0, sizeof(pd) );
  30. pd.nwh = _window;
  31. bgfx::setPlatformData(pd);
  32. }
  33. typedef DWORD (WINAPI* PFN_XINPUT_GET_STATE)(DWORD dwUserIndex, XINPUT_STATE* pState);
  34. typedef void (WINAPI* PFN_XINPUT_ENABLE)(BOOL enable); // 1.4+
  35. PFN_XINPUT_GET_STATE XInputGetState;
  36. PFN_XINPUT_ENABLE XInputEnable;
  37. struct XInputRemap
  38. {
  39. uint16_t m_bit;
  40. Key::Enum m_key;
  41. };
  42. static XInputRemap s_xinputRemap[] =
  43. {
  44. { XINPUT_GAMEPAD_DPAD_UP, Key::GamepadUp },
  45. { XINPUT_GAMEPAD_DPAD_DOWN, Key::GamepadDown },
  46. { XINPUT_GAMEPAD_DPAD_LEFT, Key::GamepadLeft },
  47. { XINPUT_GAMEPAD_DPAD_RIGHT, Key::GamepadRight },
  48. { XINPUT_GAMEPAD_START, Key::GamepadStart },
  49. { XINPUT_GAMEPAD_BACK, Key::GamepadBack },
  50. { XINPUT_GAMEPAD_LEFT_THUMB, Key::GamepadThumbL },
  51. { XINPUT_GAMEPAD_RIGHT_THUMB, Key::GamepadThumbR },
  52. { XINPUT_GAMEPAD_LEFT_SHOULDER, Key::GamepadShoulderL },
  53. { XINPUT_GAMEPAD_RIGHT_SHOULDER, Key::GamepadShoulderR },
  54. { XINPUT_GAMEPAD_GUIDE, Key::GamepadGuide },
  55. { XINPUT_GAMEPAD_A, Key::GamepadA },
  56. { XINPUT_GAMEPAD_B, Key::GamepadB },
  57. { XINPUT_GAMEPAD_X, Key::GamepadX },
  58. { XINPUT_GAMEPAD_Y, Key::GamepadY },
  59. };
  60. struct XInput
  61. {
  62. XInput()
  63. : m_xinputdll(NULL)
  64. {
  65. bx::memSet(m_connected, 0, sizeof(m_connected) );
  66. bx::memSet(m_state, 0, sizeof(m_state) );
  67. m_deadzone[GamepadAxis::LeftX ] =
  68. m_deadzone[GamepadAxis::LeftY ] = XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE;
  69. m_deadzone[GamepadAxis::RightX] =
  70. m_deadzone[GamepadAxis::RightY] = XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE;
  71. m_deadzone[GamepadAxis::LeftZ ] =
  72. m_deadzone[GamepadAxis::RightZ] = XINPUT_GAMEPAD_TRIGGER_THRESHOLD;
  73. bx::memSet(m_flip, 1, sizeof(m_flip) );
  74. m_flip[GamepadAxis::LeftY ] =
  75. m_flip[GamepadAxis::RightY] = -1;
  76. }
  77. void init()
  78. {
  79. m_xinputdll = bx::dlopen(XINPUT_DLL_A);
  80. if (NULL != m_xinputdll)
  81. {
  82. XInputGetState = (PFN_XINPUT_GET_STATE)bx::dlsym(m_xinputdll, "XInputGetState");
  83. // XInputEnable = (PFN_XINPUT_ENABLE )bx::dlsym(m_xinputdll, "XInputEnable" );
  84. if (NULL == XInputGetState)
  85. {
  86. shutdown();
  87. }
  88. }
  89. }
  90. void shutdown()
  91. {
  92. if (NULL != m_xinputdll)
  93. {
  94. bx::dlclose(m_xinputdll);
  95. m_xinputdll = NULL;
  96. }
  97. }
  98. bool filter(GamepadAxis::Enum _axis, int32_t _old, int32_t* _value)
  99. {
  100. const int32_t deadzone = m_deadzone[_axis];
  101. int32_t value = *_value;
  102. value = value > deadzone || value < -deadzone ? value : 0;
  103. *_value = value * m_flip[_axis];
  104. return _old != value;
  105. }
  106. void update(EventQueue& _eventQueue)
  107. {
  108. int64_t now = bx::getHPCounter();
  109. static int64_t next = now;
  110. if (now < next)
  111. {
  112. return;
  113. }
  114. const int64_t timerFreq = bx::getHPFrequency();
  115. next = now + timerFreq/60;
  116. if (NULL == m_xinputdll)
  117. {
  118. return;
  119. }
  120. WindowHandle defaultWindow = { 0 };
  121. for (uint16_t ii = 0; ii < BX_COUNTOF(m_state); ++ii)
  122. {
  123. XINPUT_STATE state;
  124. DWORD result = XInputGetState(ii, &state);
  125. GamepadHandle handle = { ii };
  126. bool connected = ERROR_SUCCESS == result;
  127. if (connected != m_connected[ii])
  128. {
  129. _eventQueue.postGamepadEvent(defaultWindow, handle, connected);
  130. }
  131. m_connected[ii] = connected;
  132. if (connected
  133. && m_state[ii].dwPacketNumber != state.dwPacketNumber)
  134. {
  135. XINPUT_GAMEPAD& gamepad = m_state[ii].Gamepad;
  136. const uint16_t changed = gamepad.wButtons ^ state.Gamepad.wButtons;
  137. const uint16_t current = gamepad.wButtons;
  138. if (0 != changed)
  139. {
  140. for (uint32_t jj = 0; jj < BX_COUNTOF(s_xinputRemap); ++jj)
  141. {
  142. uint16_t bit = s_xinputRemap[jj].m_bit;
  143. if (bit & changed)
  144. {
  145. _eventQueue.postKeyEvent(defaultWindow, s_xinputRemap[jj].m_key, 0, 0 == (current & bit) );
  146. }
  147. }
  148. gamepad.wButtons = state.Gamepad.wButtons;
  149. }
  150. if (gamepad.bLeftTrigger != state.Gamepad.bLeftTrigger)
  151. {
  152. int32_t value = state.Gamepad.bLeftTrigger;
  153. if (filter(GamepadAxis::LeftZ, gamepad.bLeftTrigger, &value) )
  154. {
  155. _eventQueue.postAxisEvent(defaultWindow, handle, GamepadAxis::LeftZ, value);
  156. }
  157. gamepad.bLeftTrigger = state.Gamepad.bLeftTrigger;
  158. }
  159. if (gamepad.bRightTrigger != state.Gamepad.bRightTrigger)
  160. {
  161. int32_t value = state.Gamepad.bRightTrigger;
  162. if (filter(GamepadAxis::RightZ, gamepad.bRightTrigger, &value) )
  163. {
  164. _eventQueue.postAxisEvent(defaultWindow, handle, GamepadAxis::RightZ, value);
  165. }
  166. gamepad.bRightTrigger = state.Gamepad.bRightTrigger;
  167. }
  168. if (gamepad.sThumbLX != state.Gamepad.sThumbLX)
  169. {
  170. int32_t value = state.Gamepad.sThumbLX;
  171. if (filter(GamepadAxis::LeftX, gamepad.sThumbLX, &value) )
  172. {
  173. _eventQueue.postAxisEvent(defaultWindow, handle, GamepadAxis::LeftX, value);
  174. }
  175. gamepad.sThumbLX = state.Gamepad.sThumbLX;
  176. }
  177. if (gamepad.sThumbLY != state.Gamepad.sThumbLY)
  178. {
  179. int32_t value = state.Gamepad.sThumbLY;
  180. if (filter(GamepadAxis::LeftY, gamepad.sThumbLY, &value) )
  181. {
  182. _eventQueue.postAxisEvent(defaultWindow, handle, GamepadAxis::LeftY, value);
  183. }
  184. gamepad.sThumbLY = state.Gamepad.sThumbLY;
  185. }
  186. if (gamepad.sThumbRX != state.Gamepad.sThumbRX)
  187. {
  188. int32_t value = state.Gamepad.sThumbRX;
  189. if (filter(GamepadAxis::RightX, gamepad.sThumbRX, &value) )
  190. {
  191. _eventQueue.postAxisEvent(defaultWindow, handle, GamepadAxis::RightX, value);
  192. }
  193. gamepad.sThumbRX = state.Gamepad.sThumbRX;
  194. }
  195. if (gamepad.sThumbRY != state.Gamepad.sThumbRY)
  196. {
  197. int32_t value = state.Gamepad.sThumbRY;
  198. if (filter(GamepadAxis::RightY, gamepad.sThumbRY, &value) )
  199. {
  200. _eventQueue.postAxisEvent(defaultWindow, handle, GamepadAxis::RightY, value);
  201. }
  202. gamepad.sThumbRY = state.Gamepad.sThumbRY;
  203. }
  204. }
  205. }
  206. }
  207. void* m_xinputdll;
  208. int32_t m_deadzone[GamepadAxis::Count];
  209. int8_t m_flip[GamepadAxis::Count];
  210. XINPUT_STATE m_state[ENTRY_CONFIG_MAX_GAMEPADS];
  211. bool m_connected[ENTRY_CONFIG_MAX_GAMEPADS];
  212. };
  213. XInput s_xinput;
  214. enum
  215. {
  216. WM_USER_WINDOW_CREATE = WM_USER,
  217. WM_USER_WINDOW_DESTROY,
  218. WM_USER_WINDOW_SET_TITLE,
  219. WM_USER_WINDOW_SET_POS,
  220. WM_USER_WINDOW_SET_SIZE,
  221. WM_USER_WINDOW_TOGGLE_FRAME,
  222. WM_USER_WINDOW_MOUSE_LOCK,
  223. };
  224. struct TranslateKeyModifiers
  225. {
  226. int m_vk;
  227. Modifier::Enum m_modifier;
  228. };
  229. static const TranslateKeyModifiers s_translateKeyModifiers[8] =
  230. {
  231. { VK_LMENU, Modifier::LeftAlt },
  232. { VK_RMENU, Modifier::RightAlt },
  233. { VK_LCONTROL, Modifier::LeftCtrl },
  234. { VK_RCONTROL, Modifier::RightCtrl },
  235. { VK_LSHIFT, Modifier::LeftShift },
  236. { VK_RSHIFT, Modifier::RightShift },
  237. { VK_LWIN, Modifier::LeftMeta },
  238. { VK_RWIN, Modifier::RightMeta },
  239. };
  240. static uint8_t translateKeyModifiers()
  241. {
  242. uint8_t modifiers = 0;
  243. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateKeyModifiers); ++ii)
  244. {
  245. const TranslateKeyModifiers& tkm = s_translateKeyModifiers[ii];
  246. modifiers |= 0 > GetKeyState(tkm.m_vk) ? tkm.m_modifier : Modifier::None;
  247. }
  248. return modifiers;
  249. }
  250. static uint8_t s_translateKey[256];
  251. static Key::Enum translateKey(WPARAM _wparam)
  252. {
  253. return (Key::Enum)s_translateKey[_wparam&0xff];
  254. }
  255. struct MainThreadEntry
  256. {
  257. int m_argc;
  258. char** m_argv;
  259. static int32_t threadFunc(void* _userData);
  260. };
  261. struct Msg
  262. {
  263. Msg()
  264. : m_x(0)
  265. , m_y(0)
  266. , m_width(0)
  267. , m_height(0)
  268. , m_flags(0)
  269. {
  270. }
  271. int32_t m_x;
  272. int32_t m_y;
  273. uint32_t m_width;
  274. uint32_t m_height;
  275. uint32_t m_flags;
  276. tinystl::string m_title;
  277. };
  278. static void mouseCapture(HWND _hwnd, bool _capture)
  279. {
  280. if (_capture)
  281. {
  282. SetCapture(_hwnd);
  283. }
  284. else
  285. {
  286. ReleaseCapture();
  287. }
  288. }
  289. struct Context
  290. {
  291. Context()
  292. : m_mz(0)
  293. , m_frame(true)
  294. , m_mouseLock(NULL)
  295. , m_init(false)
  296. , m_exit(false)
  297. {
  298. bx::memSet(s_translateKey, 0, sizeof(s_translateKey) );
  299. s_translateKey[VK_ESCAPE] = Key::Esc;
  300. s_translateKey[VK_RETURN] = Key::Return;
  301. s_translateKey[VK_TAB] = Key::Tab;
  302. s_translateKey[VK_BACK] = Key::Backspace;
  303. s_translateKey[VK_SPACE] = Key::Space;
  304. s_translateKey[VK_UP] = Key::Up;
  305. s_translateKey[VK_DOWN] = Key::Down;
  306. s_translateKey[VK_LEFT] = Key::Left;
  307. s_translateKey[VK_RIGHT] = Key::Right;
  308. s_translateKey[VK_INSERT] = Key::Insert;
  309. s_translateKey[VK_DELETE] = Key::Delete;
  310. s_translateKey[VK_HOME] = Key::Home;
  311. s_translateKey[VK_END] = Key::End;
  312. s_translateKey[VK_PRIOR] = Key::PageUp;
  313. s_translateKey[VK_NEXT] = Key::PageDown;
  314. s_translateKey[VK_SNAPSHOT] = Key::Print;
  315. s_translateKey[VK_OEM_PLUS] = Key::Plus;
  316. s_translateKey[VK_OEM_MINUS] = Key::Minus;
  317. s_translateKey[VK_OEM_4] = Key::LeftBracket;
  318. s_translateKey[VK_OEM_6] = Key::RightBracket;
  319. s_translateKey[VK_OEM_1] = Key::Semicolon;
  320. s_translateKey[VK_OEM_7] = Key::Quote;
  321. s_translateKey[VK_OEM_COMMA] = Key::Comma;
  322. s_translateKey[VK_OEM_PERIOD] = Key::Period;
  323. s_translateKey[VK_DECIMAL] = Key::Period;
  324. s_translateKey[VK_OEM_2] = Key::Slash;
  325. s_translateKey[VK_OEM_5] = Key::Backslash;
  326. s_translateKey[VK_OEM_3] = Key::Tilde;
  327. s_translateKey[VK_F1] = Key::F1;
  328. s_translateKey[VK_F2] = Key::F2;
  329. s_translateKey[VK_F3] = Key::F3;
  330. s_translateKey[VK_F4] = Key::F4;
  331. s_translateKey[VK_F5] = Key::F5;
  332. s_translateKey[VK_F6] = Key::F6;
  333. s_translateKey[VK_F7] = Key::F7;
  334. s_translateKey[VK_F8] = Key::F8;
  335. s_translateKey[VK_F9] = Key::F9;
  336. s_translateKey[VK_F10] = Key::F10;
  337. s_translateKey[VK_F11] = Key::F11;
  338. s_translateKey[VK_F12] = Key::F12;
  339. s_translateKey[VK_NUMPAD0] = Key::NumPad0;
  340. s_translateKey[VK_NUMPAD1] = Key::NumPad1;
  341. s_translateKey[VK_NUMPAD2] = Key::NumPad2;
  342. s_translateKey[VK_NUMPAD3] = Key::NumPad3;
  343. s_translateKey[VK_NUMPAD4] = Key::NumPad4;
  344. s_translateKey[VK_NUMPAD5] = Key::NumPad5;
  345. s_translateKey[VK_NUMPAD6] = Key::NumPad6;
  346. s_translateKey[VK_NUMPAD7] = Key::NumPad7;
  347. s_translateKey[VK_NUMPAD8] = Key::NumPad8;
  348. s_translateKey[VK_NUMPAD9] = Key::NumPad9;
  349. s_translateKey[uint8_t('0')] = Key::Key0;
  350. s_translateKey[uint8_t('1')] = Key::Key1;
  351. s_translateKey[uint8_t('2')] = Key::Key2;
  352. s_translateKey[uint8_t('3')] = Key::Key3;
  353. s_translateKey[uint8_t('4')] = Key::Key4;
  354. s_translateKey[uint8_t('5')] = Key::Key5;
  355. s_translateKey[uint8_t('6')] = Key::Key6;
  356. s_translateKey[uint8_t('7')] = Key::Key7;
  357. s_translateKey[uint8_t('8')] = Key::Key8;
  358. s_translateKey[uint8_t('9')] = Key::Key9;
  359. s_translateKey[uint8_t('A')] = Key::KeyA;
  360. s_translateKey[uint8_t('B')] = Key::KeyB;
  361. s_translateKey[uint8_t('C')] = Key::KeyC;
  362. s_translateKey[uint8_t('D')] = Key::KeyD;
  363. s_translateKey[uint8_t('E')] = Key::KeyE;
  364. s_translateKey[uint8_t('F')] = Key::KeyF;
  365. s_translateKey[uint8_t('G')] = Key::KeyG;
  366. s_translateKey[uint8_t('H')] = Key::KeyH;
  367. s_translateKey[uint8_t('I')] = Key::KeyI;
  368. s_translateKey[uint8_t('J')] = Key::KeyJ;
  369. s_translateKey[uint8_t('K')] = Key::KeyK;
  370. s_translateKey[uint8_t('L')] = Key::KeyL;
  371. s_translateKey[uint8_t('M')] = Key::KeyM;
  372. s_translateKey[uint8_t('N')] = Key::KeyN;
  373. s_translateKey[uint8_t('O')] = Key::KeyO;
  374. s_translateKey[uint8_t('P')] = Key::KeyP;
  375. s_translateKey[uint8_t('Q')] = Key::KeyQ;
  376. s_translateKey[uint8_t('R')] = Key::KeyR;
  377. s_translateKey[uint8_t('S')] = Key::KeyS;
  378. s_translateKey[uint8_t('T')] = Key::KeyT;
  379. s_translateKey[uint8_t('U')] = Key::KeyU;
  380. s_translateKey[uint8_t('V')] = Key::KeyV;
  381. s_translateKey[uint8_t('W')] = Key::KeyW;
  382. s_translateKey[uint8_t('X')] = Key::KeyX;
  383. s_translateKey[uint8_t('Y')] = Key::KeyY;
  384. s_translateKey[uint8_t('Z')] = Key::KeyZ;
  385. }
  386. int32_t run(int _argc, char** _argv)
  387. {
  388. SetDllDirectoryA(".");
  389. s_xinput.init();
  390. HINSTANCE instance = (HINSTANCE)GetModuleHandle(NULL);
  391. WNDCLASSEXA wnd;
  392. bx::memSet(&wnd, 0, sizeof(wnd) );
  393. wnd.cbSize = sizeof(wnd);
  394. wnd.style = CS_HREDRAW | CS_VREDRAW;
  395. wnd.lpfnWndProc = wndProc;
  396. wnd.hInstance = instance;
  397. wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  398. wnd.hCursor = LoadCursor(NULL, IDC_ARROW);
  399. wnd.lpszClassName = "bgfx";
  400. wnd.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  401. RegisterClassExA(&wnd);
  402. m_windowAlloc.alloc();
  403. m_hwnd[0] = CreateWindowA("bgfx"
  404. , "BGFX"
  405. , WS_OVERLAPPEDWINDOW|WS_VISIBLE
  406. , 0
  407. , 0
  408. , ENTRY_DEFAULT_WIDTH
  409. , ENTRY_DEFAULT_HEIGHT
  410. , NULL
  411. , NULL
  412. , instance
  413. , 0
  414. );
  415. m_flags[0] = 0
  416. | ENTRY_WINDOW_FLAG_ASPECT_RATIO
  417. | ENTRY_WINDOW_FLAG_FRAME
  418. ;
  419. winSetHwnd(m_hwnd[0]);
  420. adjust(m_hwnd[0], ENTRY_DEFAULT_WIDTH, ENTRY_DEFAULT_HEIGHT, true);
  421. clear(m_hwnd[0]);
  422. m_width = ENTRY_DEFAULT_WIDTH;
  423. m_height = ENTRY_DEFAULT_HEIGHT;
  424. m_oldWidth = ENTRY_DEFAULT_WIDTH;
  425. m_oldHeight = ENTRY_DEFAULT_HEIGHT;
  426. MainThreadEntry mte;
  427. mte.m_argc = _argc;
  428. mte.m_argv = _argv;
  429. bx::Thread thread;
  430. thread.init(mte.threadFunc, &mte);
  431. m_init = true;
  432. m_eventQueue.postSizeEvent(findHandle(m_hwnd[0]), m_width, m_height);
  433. MSG msg;
  434. msg.message = WM_NULL;
  435. while (!m_exit)
  436. {
  437. s_xinput.update(m_eventQueue);
  438. WaitForInputIdle(GetCurrentProcess(), 16);
  439. while (0 != PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE) )
  440. {
  441. TranslateMessage(&msg);
  442. DispatchMessage(&msg);
  443. }
  444. }
  445. thread.shutdown();
  446. DestroyWindow(m_hwnd[0]);
  447. s_xinput.shutdown();
  448. return thread.getExitCode();
  449. }
  450. LRESULT process(HWND _hwnd, UINT _id, WPARAM _wparam, LPARAM _lparam)
  451. {
  452. if (m_init)
  453. {
  454. switch (_id)
  455. {
  456. case WM_USER_WINDOW_CREATE:
  457. {
  458. Msg* msg = (Msg*)_lparam;
  459. HWND hwnd = CreateWindowA("bgfx"
  460. , msg->m_title.c_str()
  461. , WS_OVERLAPPEDWINDOW|WS_VISIBLE
  462. , msg->m_x
  463. , msg->m_y
  464. , msg->m_width
  465. , msg->m_height
  466. , NULL
  467. , NULL
  468. , (HINSTANCE)GetModuleHandle(NULL)
  469. , 0
  470. );
  471. clear(hwnd);
  472. m_hwnd[_wparam] = hwnd;
  473. m_flags[_wparam] = msg->m_flags;
  474. WindowHandle handle = { (uint16_t)_wparam };
  475. m_eventQueue.postSizeEvent(handle, msg->m_width, msg->m_height);
  476. m_eventQueue.postWindowEvent(handle, hwnd);
  477. delete msg;
  478. }
  479. break;
  480. case WM_USER_WINDOW_DESTROY:
  481. {
  482. WindowHandle handle = { (uint16_t)_wparam };
  483. m_eventQueue.postWindowEvent(handle);
  484. DestroyWindow(m_hwnd[_wparam]);
  485. m_hwnd[_wparam] = 0;
  486. if (0 == handle.idx)
  487. {
  488. m_exit = true;
  489. m_eventQueue.postExitEvent();
  490. }
  491. }
  492. break;
  493. case WM_USER_WINDOW_SET_TITLE:
  494. {
  495. Msg* msg = (Msg*)_lparam;
  496. SetWindowTextA(m_hwnd[_wparam], msg->m_title.c_str() );
  497. delete msg;
  498. }
  499. break;
  500. case WM_USER_WINDOW_SET_POS:
  501. {
  502. Msg* msg = (Msg*)_lparam;
  503. SetWindowPos(m_hwnd[_wparam], 0, msg->m_x, msg->m_y, 0, 0
  504. , SWP_NOACTIVATE
  505. | SWP_NOOWNERZORDER
  506. | SWP_NOSIZE
  507. );
  508. delete msg;
  509. }
  510. break;
  511. case WM_USER_WINDOW_SET_SIZE:
  512. {
  513. uint32_t width = GET_X_LPARAM(_lparam);
  514. uint32_t height = GET_Y_LPARAM(_lparam);
  515. adjust(m_hwnd[_wparam], width, height, true);
  516. }
  517. break;
  518. case WM_USER_WINDOW_TOGGLE_FRAME:
  519. {
  520. if (m_frame)
  521. {
  522. m_oldWidth = m_width;
  523. m_oldHeight = m_height;
  524. }
  525. adjust(m_hwnd[_wparam], m_oldWidth, m_oldHeight, !m_frame);
  526. }
  527. break;
  528. case WM_USER_WINDOW_MOUSE_LOCK:
  529. setMouseLock(m_hwnd[_wparam], !!_lparam);
  530. break;
  531. case WM_DESTROY:
  532. break;
  533. case WM_QUIT:
  534. case WM_CLOSE:
  535. destroyWindow(findHandle(_hwnd) );
  536. // Don't process message. Window will be destroyed later.
  537. return 0;
  538. case WM_SIZING:
  539. {
  540. WindowHandle handle = findHandle(_hwnd);
  541. if (isValid(handle)
  542. && ENTRY_WINDOW_FLAG_ASPECT_RATIO & m_flags[handle.idx])
  543. {
  544. RECT& rect = *(RECT*)_lparam;
  545. uint32_t width = rect.right - rect.left - m_frameWidth;
  546. uint32_t height = rect.bottom - rect.top - m_frameHeight;
  547. // Recalculate size according to aspect ratio
  548. switch (_wparam)
  549. {
  550. case WMSZ_LEFT:
  551. case WMSZ_RIGHT:
  552. {
  553. float aspectRatio = 1.0f/m_aspectRatio;
  554. width = bx::uint32_max(ENTRY_DEFAULT_WIDTH/4, width);
  555. height = uint32_t(float(width)*aspectRatio);
  556. }
  557. break;
  558. default:
  559. {
  560. float aspectRatio = m_aspectRatio;
  561. height = bx::uint32_max(ENTRY_DEFAULT_HEIGHT/4, height);
  562. width = uint32_t(float(height)*aspectRatio);
  563. }
  564. break;
  565. }
  566. // Recalculate position using different anchor points
  567. switch(_wparam)
  568. {
  569. case WMSZ_LEFT:
  570. case WMSZ_TOPLEFT:
  571. case WMSZ_BOTTOMLEFT:
  572. rect.left = rect.right - width - m_frameWidth;
  573. rect.bottom = rect.top + height + m_frameHeight;
  574. break;
  575. default:
  576. rect.right = rect.left + width + m_frameWidth;
  577. rect.bottom = rect.top + height + m_frameHeight;
  578. break;
  579. }
  580. m_eventQueue.postSizeEvent(findHandle(_hwnd), width, height);
  581. }
  582. }
  583. return 0;
  584. case WM_SIZE:
  585. {
  586. WindowHandle handle = findHandle(_hwnd);
  587. if (isValid(handle) )
  588. {
  589. uint32_t width = GET_X_LPARAM(_lparam);
  590. uint32_t height = GET_Y_LPARAM(_lparam);
  591. m_width = width;
  592. m_height = height;
  593. m_eventQueue.postSizeEvent(handle, m_width, m_height);
  594. }
  595. }
  596. break;
  597. case WM_SYSCOMMAND:
  598. switch (_wparam)
  599. {
  600. case SC_MINIMIZE:
  601. case SC_RESTORE:
  602. {
  603. HWND parent = GetWindow(_hwnd, GW_OWNER);
  604. if (NULL != parent)
  605. {
  606. PostMessage(parent, _id, _wparam, _lparam);
  607. }
  608. }
  609. }
  610. break;
  611. case WM_MOUSEMOVE:
  612. {
  613. int32_t mx = GET_X_LPARAM(_lparam);
  614. int32_t my = GET_Y_LPARAM(_lparam);
  615. if (_hwnd == m_mouseLock)
  616. {
  617. mx -= m_mx;
  618. my -= m_my;
  619. if (0 == mx
  620. && 0 == my)
  621. {
  622. break;
  623. }
  624. setMousePos(_hwnd, m_mx, m_my);
  625. }
  626. m_eventQueue.postMouseEvent(findHandle(_hwnd), mx, my, m_mz);
  627. }
  628. break;
  629. case WM_MOUSEWHEEL:
  630. {
  631. POINT pt = { GET_X_LPARAM(_lparam), GET_Y_LPARAM(_lparam) };
  632. ScreenToClient(_hwnd, &pt);
  633. int32_t mx = pt.x;
  634. int32_t my = pt.y;
  635. m_mz += GET_WHEEL_DELTA_WPARAM(_wparam)/WHEEL_DELTA;
  636. m_eventQueue.postMouseEvent(findHandle(_hwnd), mx, my, m_mz);
  637. }
  638. break;
  639. case WM_LBUTTONDOWN:
  640. case WM_LBUTTONUP:
  641. case WM_LBUTTONDBLCLK:
  642. {
  643. mouseCapture(_hwnd, _id == WM_LBUTTONDOWN);
  644. int32_t mx = GET_X_LPARAM(_lparam);
  645. int32_t my = GET_Y_LPARAM(_lparam);
  646. m_eventQueue.postMouseEvent(findHandle(_hwnd), mx, my, m_mz, MouseButton::Left, _id == WM_LBUTTONDOWN);
  647. }
  648. break;
  649. case WM_MBUTTONDOWN:
  650. case WM_MBUTTONUP:
  651. case WM_MBUTTONDBLCLK:
  652. {
  653. mouseCapture(_hwnd, _id == WM_MBUTTONDOWN);
  654. int32_t mx = GET_X_LPARAM(_lparam);
  655. int32_t my = GET_Y_LPARAM(_lparam);
  656. m_eventQueue.postMouseEvent(findHandle(_hwnd), mx, my, m_mz, MouseButton::Middle, _id == WM_MBUTTONDOWN);
  657. }
  658. break;
  659. case WM_RBUTTONDOWN:
  660. case WM_RBUTTONUP:
  661. case WM_RBUTTONDBLCLK:
  662. {
  663. mouseCapture(_hwnd, _id == WM_RBUTTONDOWN);
  664. int32_t mx = GET_X_LPARAM(_lparam);
  665. int32_t my = GET_Y_LPARAM(_lparam);
  666. m_eventQueue.postMouseEvent(findHandle(_hwnd), mx, my, m_mz, MouseButton::Right, _id == WM_RBUTTONDOWN);
  667. }
  668. break;
  669. case WM_KEYDOWN:
  670. case WM_SYSKEYDOWN:
  671. case WM_KEYUP:
  672. case WM_SYSKEYUP:
  673. {
  674. uint8_t modifiers = translateKeyModifiers();
  675. Key::Enum key = translateKey(_wparam);
  676. WindowHandle handle = findHandle(_hwnd);
  677. if (Key::Print == key
  678. && 0x3 == ( (uint32_t)(_lparam)>>30) )
  679. {
  680. // VK_SNAPSHOT doesn't generate keydown event. Fire on down event when previous
  681. // key state bit is set to 1 and transition state bit is set to 1.
  682. //
  683. // http://msdn.microsoft.com/en-us/library/windows/desktop/ms646280%28v=vs.85%29.aspx
  684. m_eventQueue.postKeyEvent(handle, key, modifiers, true);
  685. }
  686. m_eventQueue.postKeyEvent(handle, key, modifiers, _id == WM_KEYDOWN || _id == WM_SYSKEYDOWN);
  687. }
  688. break;
  689. case WM_CHAR:
  690. {
  691. uint8_t utf8[4] = {};
  692. uint8_t len = (uint8_t)WideCharToMultiByte(CP_UTF8
  693. , 0
  694. , (LPCWSTR)&_wparam
  695. , 1
  696. , (LPSTR)utf8
  697. , BX_COUNTOF(utf8)
  698. , NULL
  699. , NULL
  700. );
  701. if (0 != len)
  702. {
  703. WindowHandle handle = findHandle(_hwnd);
  704. m_eventQueue.postCharEvent(handle, len, utf8);
  705. }
  706. }
  707. break;
  708. default:
  709. break;
  710. }
  711. }
  712. return DefWindowProc(_hwnd, _id, _wparam, _lparam);
  713. }
  714. WindowHandle findHandle(HWND _hwnd)
  715. {
  716. bx::LwMutexScope scope(m_lock);
  717. for (uint16_t ii = 0, num = m_windowAlloc.getNumHandles(); ii < num; ++ii)
  718. {
  719. uint16_t idx = m_windowAlloc.getHandleAt(ii);
  720. if (_hwnd == m_hwnd[idx])
  721. {
  722. WindowHandle handle = { idx };
  723. return handle;
  724. }
  725. }
  726. WindowHandle invalid = { UINT16_MAX };
  727. return invalid;
  728. }
  729. void clear(HWND _hwnd)
  730. {
  731. RECT rect;
  732. GetWindowRect(_hwnd, &rect);
  733. HBRUSH brush = CreateSolidBrush(RGB(0, 0, 0) );
  734. HDC hdc = GetDC(_hwnd);
  735. SelectObject(hdc, brush);
  736. FillRect(hdc, &rect, brush);
  737. }
  738. void adjust(HWND _hwnd, uint32_t _width, uint32_t _height, bool _windowFrame)
  739. {
  740. m_width = _width;
  741. m_height = _height;
  742. m_aspectRatio = float(_width)/float(_height);
  743. ShowWindow(_hwnd, SW_SHOWNORMAL);
  744. RECT rect;
  745. RECT newrect = {0, 0, (LONG)_width, (LONG)_height};
  746. DWORD style = WS_POPUP|WS_SYSMENU;
  747. if (m_frame)
  748. {
  749. GetWindowRect(_hwnd, &m_rect);
  750. m_style = GetWindowLong(_hwnd, GWL_STYLE);
  751. }
  752. if (_windowFrame)
  753. {
  754. rect = m_rect;
  755. style = m_style;
  756. }
  757. else
  758. {
  759. #if defined(__MINGW32__)
  760. rect = m_rect;
  761. style = m_style;
  762. #else
  763. HMONITOR monitor = MonitorFromWindow(_hwnd, MONITOR_DEFAULTTONEAREST);
  764. MONITORINFO mi;
  765. mi.cbSize = sizeof(mi);
  766. GetMonitorInfo(monitor, &mi);
  767. newrect = mi.rcMonitor;
  768. rect = mi.rcMonitor;
  769. m_aspectRatio = float(newrect.right - newrect.left)/float(newrect.bottom - newrect.top);
  770. #endif // !defined(__MINGW__)
  771. }
  772. SetWindowLong(_hwnd, GWL_STYLE, style);
  773. uint32_t prewidth = newrect.right - newrect.left;
  774. uint32_t preheight = newrect.bottom - newrect.top;
  775. AdjustWindowRect(&newrect, style, FALSE);
  776. m_frameWidth = (newrect.right - newrect.left) - prewidth;
  777. m_frameHeight = (newrect.bottom - newrect.top ) - preheight;
  778. UpdateWindow(_hwnd);
  779. if (rect.left == -32000
  780. || rect.top == -32000)
  781. {
  782. rect.left = 0;
  783. rect.top = 0;
  784. }
  785. int32_t left = rect.left;
  786. int32_t top = rect.top;
  787. int32_t width = (newrect.right-newrect.left);
  788. int32_t height = (newrect.bottom-newrect.top);
  789. if (!_windowFrame)
  790. {
  791. float aspectRatio = 1.0f/m_aspectRatio;
  792. width = bx::uint32_max(ENTRY_DEFAULT_WIDTH/4, width);
  793. height = uint32_t(float(width)*aspectRatio);
  794. left = newrect.left+(newrect.right -newrect.left-width)/2;
  795. top = newrect.top +(newrect.bottom-newrect.top-height)/2;
  796. }
  797. SetWindowPos(_hwnd
  798. , HWND_TOP
  799. , left
  800. , top
  801. , width
  802. , height
  803. , SWP_SHOWWINDOW
  804. );
  805. ShowWindow(_hwnd, SW_RESTORE);
  806. m_frame = _windowFrame;
  807. }
  808. void setMousePos(HWND _hwnd, int32_t _mx, int32_t _my)
  809. {
  810. POINT pt = { _mx, _my };
  811. ClientToScreen(_hwnd, &pt);
  812. SetCursorPos(pt.x, pt.y);
  813. }
  814. void setMouseLock(HWND _hwnd, bool _lock)
  815. {
  816. if (_hwnd != m_mouseLock)
  817. {
  818. if (_lock)
  819. {
  820. m_mx = m_width/2;
  821. m_my = m_height/2;
  822. ShowCursor(false);
  823. setMousePos(_hwnd, m_mx, m_my);
  824. }
  825. else
  826. {
  827. setMousePos(_hwnd, m_mx, m_my);
  828. ShowCursor(true);
  829. }
  830. m_mouseLock = _hwnd;
  831. }
  832. }
  833. static LRESULT CALLBACK wndProc(HWND _hwnd, UINT _id, WPARAM _wparam, LPARAM _lparam);
  834. EventQueue m_eventQueue;
  835. bx::LwMutex m_lock;
  836. bx::HandleAllocT<ENTRY_CONFIG_MAX_WINDOWS> m_windowAlloc;
  837. HWND m_hwnd[ENTRY_CONFIG_MAX_WINDOWS];
  838. uint32_t m_flags[ENTRY_CONFIG_MAX_WINDOWS];
  839. RECT m_rect;
  840. DWORD m_style;
  841. uint32_t m_width;
  842. uint32_t m_height;
  843. uint32_t m_oldWidth;
  844. uint32_t m_oldHeight;
  845. uint32_t m_frameWidth;
  846. uint32_t m_frameHeight;
  847. float m_aspectRatio;
  848. int32_t m_mx;
  849. int32_t m_my;
  850. int32_t m_mz;
  851. bool m_frame;
  852. HWND m_mouseLock;
  853. bool m_init;
  854. bool m_exit;
  855. };
  856. static Context s_ctx;
  857. LRESULT CALLBACK Context::wndProc(HWND _hwnd, UINT _id, WPARAM _wparam, LPARAM _lparam)
  858. {
  859. return s_ctx.process(_hwnd, _id, _wparam, _lparam);
  860. }
  861. const Event* poll()
  862. {
  863. return s_ctx.m_eventQueue.poll();
  864. }
  865. const Event* poll(WindowHandle _handle)
  866. {
  867. return s_ctx.m_eventQueue.poll(_handle);
  868. }
  869. void release(const Event* _event)
  870. {
  871. s_ctx.m_eventQueue.release(_event);
  872. }
  873. WindowHandle createWindow(int32_t _x, int32_t _y, uint32_t _width, uint32_t _height, uint32_t _flags, const char* _title)
  874. {
  875. bx::LwMutexScope scope(s_ctx.m_lock);
  876. WindowHandle handle = { s_ctx.m_windowAlloc.alloc() };
  877. if (UINT16_MAX != handle.idx)
  878. {
  879. Msg* msg = new Msg;
  880. msg->m_x = _x;
  881. msg->m_y = _y;
  882. msg->m_width = _width;
  883. msg->m_height = _height;
  884. msg->m_title = _title;
  885. msg->m_flags = _flags;
  886. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_CREATE, handle.idx, (LPARAM)msg);
  887. }
  888. return handle;
  889. }
  890. void destroyWindow(WindowHandle _handle)
  891. {
  892. if (UINT16_MAX != _handle.idx)
  893. {
  894. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_DESTROY, _handle.idx, 0);
  895. bx::LwMutexScope scope(s_ctx.m_lock);
  896. s_ctx.m_windowAlloc.free(_handle.idx);
  897. }
  898. }
  899. void setWindowPos(WindowHandle _handle, int32_t _x, int32_t _y)
  900. {
  901. Msg* msg = new Msg;
  902. msg->m_x = _x;
  903. msg->m_y = _y;
  904. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_SET_POS, _handle.idx, (LPARAM)msg);
  905. }
  906. void setWindowSize(WindowHandle _handle, uint32_t _width, uint32_t _height)
  907. {
  908. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_SET_SIZE, _handle.idx, (_height<<16) | (_width&0xffff) );
  909. }
  910. void setWindowTitle(WindowHandle _handle, const char* _title)
  911. {
  912. Msg* msg = new Msg;
  913. msg->m_title = _title;
  914. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_SET_TITLE, _handle.idx, (LPARAM)msg);
  915. }
  916. void toggleWindowFrame(WindowHandle _handle)
  917. {
  918. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_TOGGLE_FRAME, _handle.idx, 0);
  919. }
  920. void toggleFullscreen(WindowHandle _handle)
  921. {
  922. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_TOGGLE_FRAME, _handle.idx, 0);
  923. }
  924. void setMouseLock(WindowHandle _handle, bool _lock)
  925. {
  926. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_MOUSE_LOCK, _handle.idx, _lock);
  927. }
  928. int32_t MainThreadEntry::threadFunc(void* _userData)
  929. {
  930. MainThreadEntry* self = (MainThreadEntry*)_userData;
  931. int32_t result = main(self->m_argc, self->m_argv);
  932. PostMessage(s_ctx.m_hwnd[0], WM_QUIT, 0, 0);
  933. return result;
  934. }
  935. } // namespace entry
  936. int main(int _argc, char** _argv)
  937. {
  938. using namespace entry;
  939. return s_ctx.run(_argc, _argv);
  940. }
  941. #endif // BX_PLATFORM_WINDOWS