entry_windows.cpp 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  1. /*
  2. * Copyright 2011-2016 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/bgfxplatform.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. 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. memset(m_connected, 0, sizeof(m_connected) );
  66. 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. 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. GamepadHandle handle = { 0 };
  122. for (uint32_t ii = 0; ii < BX_COUNTOF(m_state); ++ii)
  123. {
  124. XINPUT_STATE state;
  125. DWORD result = XInputGetState(ii, &state);
  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. 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. 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. PostMessageA(m_hwnd[_wparam], WM_CLOSE, 0, 0);
  484. m_eventQueue.postWindowEvent(handle);
  485. DestroyWindow(m_hwnd[_wparam]);
  486. m_hwnd[_wparam] = 0;
  487. }
  488. break;
  489. case WM_USER_WINDOW_SET_TITLE:
  490. {
  491. Msg* msg = (Msg*)_lparam;
  492. SetWindowTextA(m_hwnd[_wparam], msg->m_title.c_str() );
  493. delete msg;
  494. }
  495. break;
  496. case WM_USER_WINDOW_SET_POS:
  497. {
  498. Msg* msg = (Msg*)_lparam;
  499. SetWindowPos(m_hwnd[_wparam], 0, msg->m_x, msg->m_y, 0, 0
  500. , SWP_NOACTIVATE
  501. | SWP_NOOWNERZORDER
  502. | SWP_NOSIZE
  503. );
  504. delete msg;
  505. }
  506. break;
  507. case WM_USER_WINDOW_SET_SIZE:
  508. {
  509. uint32_t width = GET_X_LPARAM(_lparam);
  510. uint32_t height = GET_Y_LPARAM(_lparam);
  511. adjust(m_hwnd[_wparam], width, height, true);
  512. }
  513. break;
  514. case WM_USER_WINDOW_TOGGLE_FRAME:
  515. {
  516. if (m_frame)
  517. {
  518. m_oldWidth = m_width;
  519. m_oldHeight = m_height;
  520. }
  521. adjust(m_hwnd[_wparam], m_oldWidth, m_oldHeight, !m_frame);
  522. }
  523. break;
  524. case WM_USER_WINDOW_MOUSE_LOCK:
  525. setMouseLock(m_hwnd[_wparam], !!_lparam);
  526. break;
  527. case WM_DESTROY:
  528. break;
  529. case WM_QUIT:
  530. case WM_CLOSE:
  531. if (_hwnd == m_hwnd[0])
  532. {
  533. m_exit = true;
  534. m_eventQueue.postExitEvent();
  535. }
  536. else
  537. {
  538. destroyWindow(findHandle(_hwnd) );
  539. }
  540. // Don't process message. Window will be destroyed later.
  541. return 0;
  542. case WM_SIZING:
  543. {
  544. WindowHandle handle = findHandle(_hwnd);
  545. if (isValid(handle)
  546. && ENTRY_WINDOW_FLAG_ASPECT_RATIO & m_flags[handle.idx])
  547. {
  548. RECT& rect = *(RECT*)_lparam;
  549. uint32_t width = rect.right - rect.left - m_frameWidth;
  550. uint32_t height = rect.bottom - rect.top - m_frameHeight;
  551. // Recalculate size according to aspect ratio
  552. switch (_wparam)
  553. {
  554. case WMSZ_LEFT:
  555. case WMSZ_RIGHT:
  556. {
  557. float aspectRatio = 1.0f/m_aspectRatio;
  558. width = bx::uint32_max(ENTRY_DEFAULT_WIDTH/4, width);
  559. height = uint32_t(float(width)*aspectRatio);
  560. }
  561. break;
  562. default:
  563. {
  564. float aspectRatio = m_aspectRatio;
  565. height = bx::uint32_max(ENTRY_DEFAULT_HEIGHT/4, height);
  566. width = uint32_t(float(height)*aspectRatio);
  567. }
  568. break;
  569. }
  570. // Recalculate position using different anchor points
  571. switch(_wparam)
  572. {
  573. case WMSZ_LEFT:
  574. case WMSZ_TOPLEFT:
  575. case WMSZ_BOTTOMLEFT:
  576. rect.left = rect.right - width - m_frameWidth;
  577. rect.bottom = rect.top + height + m_frameHeight;
  578. break;
  579. default:
  580. rect.right = rect.left + width + m_frameWidth;
  581. rect.bottom = rect.top + height + m_frameHeight;
  582. break;
  583. }
  584. m_eventQueue.postSizeEvent(findHandle(_hwnd), width, height);
  585. }
  586. }
  587. return 0;
  588. case WM_SIZE:
  589. {
  590. WindowHandle handle = findHandle(_hwnd);
  591. if (isValid(handle) )
  592. {
  593. uint32_t width = GET_X_LPARAM(_lparam);
  594. uint32_t height = GET_Y_LPARAM(_lparam);
  595. m_width = width;
  596. m_height = height;
  597. m_eventQueue.postSizeEvent(handle, m_width, m_height);
  598. }
  599. }
  600. break;
  601. case WM_SYSCOMMAND:
  602. switch (_wparam)
  603. {
  604. case SC_MINIMIZE:
  605. case SC_RESTORE:
  606. {
  607. HWND parent = GetWindow(_hwnd, GW_OWNER);
  608. if (NULL != parent)
  609. {
  610. PostMessage(parent, _id, _wparam, _lparam);
  611. }
  612. }
  613. }
  614. break;
  615. case WM_MOUSEMOVE:
  616. {
  617. int32_t mx = GET_X_LPARAM(_lparam);
  618. int32_t my = GET_Y_LPARAM(_lparam);
  619. if (_hwnd == m_mouseLock)
  620. {
  621. mx -= m_mx;
  622. my -= m_my;
  623. if (0 == mx
  624. && 0 == my)
  625. {
  626. break;
  627. }
  628. setMousePos(_hwnd, m_mx, m_my);
  629. }
  630. m_eventQueue.postMouseEvent(findHandle(_hwnd), mx, my, m_mz);
  631. }
  632. break;
  633. case WM_MOUSEWHEEL:
  634. {
  635. POINT pt = { GET_X_LPARAM(_lparam), GET_Y_LPARAM(_lparam) };
  636. ScreenToClient(_hwnd, &pt);
  637. int32_t mx = pt.x;
  638. int32_t my = pt.y;
  639. m_mz += GET_WHEEL_DELTA_WPARAM(_wparam)/WHEEL_DELTA;
  640. m_eventQueue.postMouseEvent(findHandle(_hwnd), mx, my, m_mz);
  641. }
  642. break;
  643. case WM_LBUTTONDOWN:
  644. case WM_LBUTTONUP:
  645. case WM_LBUTTONDBLCLK:
  646. {
  647. mouseCapture(_hwnd, _id == WM_LBUTTONDOWN);
  648. int32_t mx = GET_X_LPARAM(_lparam);
  649. int32_t my = GET_Y_LPARAM(_lparam);
  650. m_eventQueue.postMouseEvent(findHandle(_hwnd), mx, my, m_mz, MouseButton::Left, _id == WM_LBUTTONDOWN);
  651. }
  652. break;
  653. case WM_MBUTTONDOWN:
  654. case WM_MBUTTONUP:
  655. case WM_MBUTTONDBLCLK:
  656. {
  657. mouseCapture(_hwnd, _id == WM_MBUTTONDOWN);
  658. int32_t mx = GET_X_LPARAM(_lparam);
  659. int32_t my = GET_Y_LPARAM(_lparam);
  660. m_eventQueue.postMouseEvent(findHandle(_hwnd), mx, my, m_mz, MouseButton::Middle, _id == WM_MBUTTONDOWN);
  661. }
  662. break;
  663. case WM_RBUTTONDOWN:
  664. case WM_RBUTTONUP:
  665. case WM_RBUTTONDBLCLK:
  666. {
  667. mouseCapture(_hwnd, _id == WM_RBUTTONDOWN);
  668. int32_t mx = GET_X_LPARAM(_lparam);
  669. int32_t my = GET_Y_LPARAM(_lparam);
  670. m_eventQueue.postMouseEvent(findHandle(_hwnd), mx, my, m_mz, MouseButton::Right, _id == WM_RBUTTONDOWN);
  671. }
  672. break;
  673. case WM_KEYDOWN:
  674. case WM_SYSKEYDOWN:
  675. case WM_KEYUP:
  676. case WM_SYSKEYUP:
  677. {
  678. uint8_t modifiers = translateKeyModifiers();
  679. Key::Enum key = translateKey(_wparam);
  680. WindowHandle handle = findHandle(_hwnd);
  681. if (Key::Print == key
  682. && 0x3 == ( (uint32_t)(_lparam)>>30) )
  683. {
  684. // VK_SNAPSHOT doesn't generate keydown event. Fire on down event when previous
  685. // key state bit is set to 1 and transition state bit is set to 1.
  686. //
  687. // http://msdn.microsoft.com/en-us/library/windows/desktop/ms646280%28v=vs.85%29.aspx
  688. m_eventQueue.postKeyEvent(handle, key, modifiers, true);
  689. }
  690. m_eventQueue.postKeyEvent(handle, key, modifiers, _id == WM_KEYDOWN || _id == WM_SYSKEYDOWN);
  691. }
  692. break;
  693. case WM_CHAR:
  694. {
  695. uint8_t utf8[4] = {};
  696. uint8_t len = (uint8_t)WideCharToMultiByte(CP_UTF8
  697. , 0
  698. , (LPCWSTR)&_wparam
  699. , 1
  700. , (LPSTR)utf8
  701. , BX_COUNTOF(utf8)
  702. , NULL
  703. , NULL
  704. );
  705. if (0 != len)
  706. {
  707. WindowHandle handle = findHandle(_hwnd);
  708. m_eventQueue.postCharEvent(handle, len, utf8);
  709. }
  710. }
  711. break;
  712. default:
  713. break;
  714. }
  715. }
  716. return DefWindowProc(_hwnd, _id, _wparam, _lparam);
  717. }
  718. WindowHandle findHandle(HWND _hwnd)
  719. {
  720. bx::LwMutexScope scope(m_lock);
  721. for (uint16_t ii = 0, num = m_windowAlloc.getNumHandles(); ii < num; ++ii)
  722. {
  723. uint16_t idx = m_windowAlloc.getHandleAt(ii);
  724. if (_hwnd == m_hwnd[idx])
  725. {
  726. WindowHandle handle = { idx };
  727. return handle;
  728. }
  729. }
  730. WindowHandle invalid = { UINT16_MAX };
  731. return invalid;
  732. }
  733. void clear(HWND _hwnd)
  734. {
  735. RECT rect;
  736. GetWindowRect(_hwnd, &rect);
  737. HBRUSH brush = CreateSolidBrush(RGB(0, 0, 0) );
  738. HDC hdc = GetDC(_hwnd);
  739. SelectObject(hdc, brush);
  740. FillRect(hdc, &rect, brush);
  741. }
  742. void adjust(HWND _hwnd, uint32_t _width, uint32_t _height, bool _windowFrame)
  743. {
  744. m_width = _width;
  745. m_height = _height;
  746. m_aspectRatio = float(_width)/float(_height);
  747. ShowWindow(_hwnd, SW_SHOWNORMAL);
  748. RECT rect;
  749. RECT newrect = {0, 0, (LONG)_width, (LONG)_height};
  750. DWORD style = WS_POPUP|WS_SYSMENU;
  751. if (m_frame)
  752. {
  753. GetWindowRect(_hwnd, &m_rect);
  754. m_style = GetWindowLong(_hwnd, GWL_STYLE);
  755. }
  756. if (_windowFrame)
  757. {
  758. rect = m_rect;
  759. style = m_style;
  760. }
  761. else
  762. {
  763. #if defined(__MINGW32__)
  764. rect = m_rect;
  765. style = m_style;
  766. #else
  767. HMONITOR monitor = MonitorFromWindow(_hwnd, MONITOR_DEFAULTTONEAREST);
  768. MONITORINFO mi;
  769. mi.cbSize = sizeof(mi);
  770. GetMonitorInfo(monitor, &mi);
  771. newrect = mi.rcMonitor;
  772. rect = mi.rcMonitor;
  773. m_aspectRatio = float(newrect.right - newrect.left)/float(newrect.bottom - newrect.top);
  774. #endif // !defined(__MINGW__)
  775. }
  776. SetWindowLong(_hwnd, GWL_STYLE, style);
  777. uint32_t prewidth = newrect.right - newrect.left;
  778. uint32_t preheight = newrect.bottom - newrect.top;
  779. AdjustWindowRect(&newrect, style, FALSE);
  780. m_frameWidth = (newrect.right - newrect.left) - prewidth;
  781. m_frameHeight = (newrect.bottom - newrect.top ) - preheight;
  782. UpdateWindow(_hwnd);
  783. if (rect.left == -32000
  784. || rect.top == -32000)
  785. {
  786. rect.left = 0;
  787. rect.top = 0;
  788. }
  789. int32_t left = rect.left;
  790. int32_t top = rect.top;
  791. int32_t width = (newrect.right-newrect.left);
  792. int32_t height = (newrect.bottom-newrect.top);
  793. if (!_windowFrame)
  794. {
  795. float aspectRatio = 1.0f/m_aspectRatio;
  796. width = bx::uint32_max(ENTRY_DEFAULT_WIDTH/4, width);
  797. height = uint32_t(float(width)*aspectRatio);
  798. left = newrect.left+(newrect.right -newrect.left-width)/2;
  799. top = newrect.top +(newrect.bottom-newrect.top-height)/2;
  800. }
  801. SetWindowPos(_hwnd
  802. , HWND_TOP
  803. , left
  804. , top
  805. , width
  806. , height
  807. , SWP_SHOWWINDOW
  808. );
  809. ShowWindow(_hwnd, SW_RESTORE);
  810. m_frame = _windowFrame;
  811. }
  812. void setMousePos(HWND _hwnd, int32_t _mx, int32_t _my)
  813. {
  814. POINT pt = { _mx, _my };
  815. ClientToScreen(_hwnd, &pt);
  816. SetCursorPos(pt.x, pt.y);
  817. }
  818. void setMouseLock(HWND _hwnd, bool _lock)
  819. {
  820. if (_hwnd != m_mouseLock)
  821. {
  822. if (_lock)
  823. {
  824. m_mx = m_width/2;
  825. m_my = m_height/2;
  826. ShowCursor(false);
  827. setMousePos(_hwnd, m_mx, m_my);
  828. }
  829. else
  830. {
  831. setMousePos(_hwnd, m_mx, m_my);
  832. ShowCursor(true);
  833. }
  834. m_mouseLock = _hwnd;
  835. }
  836. }
  837. static LRESULT CALLBACK wndProc(HWND _hwnd, UINT _id, WPARAM _wparam, LPARAM _lparam);
  838. EventQueue m_eventQueue;
  839. bx::LwMutex m_lock;
  840. bx::HandleAllocT<ENTRY_CONFIG_MAX_WINDOWS> m_windowAlloc;
  841. HWND m_hwnd[ENTRY_CONFIG_MAX_WINDOWS];
  842. uint32_t m_flags[ENTRY_CONFIG_MAX_WINDOWS];
  843. RECT m_rect;
  844. DWORD m_style;
  845. uint32_t m_width;
  846. uint32_t m_height;
  847. uint32_t m_oldWidth;
  848. uint32_t m_oldHeight;
  849. uint32_t m_frameWidth;
  850. uint32_t m_frameHeight;
  851. float m_aspectRatio;
  852. int32_t m_mx;
  853. int32_t m_my;
  854. int32_t m_mz;
  855. bool m_frame;
  856. HWND m_mouseLock;
  857. bool m_init;
  858. bool m_exit;
  859. };
  860. static Context s_ctx;
  861. LRESULT CALLBACK Context::wndProc(HWND _hwnd, UINT _id, WPARAM _wparam, LPARAM _lparam)
  862. {
  863. return s_ctx.process(_hwnd, _id, _wparam, _lparam);
  864. }
  865. const Event* poll()
  866. {
  867. return s_ctx.m_eventQueue.poll();
  868. }
  869. const Event* poll(WindowHandle _handle)
  870. {
  871. return s_ctx.m_eventQueue.poll(_handle);
  872. }
  873. void release(const Event* _event)
  874. {
  875. s_ctx.m_eventQueue.release(_event);
  876. }
  877. WindowHandle createWindow(int32_t _x, int32_t _y, uint32_t _width, uint32_t _height, uint32_t _flags, const char* _title)
  878. {
  879. bx::LwMutexScope scope(s_ctx.m_lock);
  880. WindowHandle handle = { s_ctx.m_windowAlloc.alloc() };
  881. if (UINT16_MAX != handle.idx)
  882. {
  883. Msg* msg = new Msg;
  884. msg->m_x = _x;
  885. msg->m_y = _y;
  886. msg->m_width = _width;
  887. msg->m_height = _height;
  888. msg->m_title = _title;
  889. msg->m_flags = _flags;
  890. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_CREATE, handle.idx, (LPARAM)msg);
  891. }
  892. return handle;
  893. }
  894. void destroyWindow(WindowHandle _handle)
  895. {
  896. if (UINT16_MAX != _handle.idx)
  897. {
  898. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_DESTROY, _handle.idx, 0);
  899. bx::LwMutexScope scope(s_ctx.m_lock);
  900. s_ctx.m_windowAlloc.free(_handle.idx);
  901. }
  902. }
  903. void setWindowPos(WindowHandle _handle, int32_t _x, int32_t _y)
  904. {
  905. Msg* msg = new Msg;
  906. msg->m_x = _x;
  907. msg->m_y = _y;
  908. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_SET_POS, _handle.idx, (LPARAM)msg);
  909. }
  910. void setWindowSize(WindowHandle _handle, uint32_t _width, uint32_t _height)
  911. {
  912. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_SET_SIZE, _handle.idx, (_height<<16) | (_width&0xffff) );
  913. }
  914. void setWindowTitle(WindowHandle _handle, const char* _title)
  915. {
  916. Msg* msg = new Msg;
  917. msg->m_title = _title;
  918. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_SET_TITLE, _handle.idx, (LPARAM)msg);
  919. }
  920. void toggleWindowFrame(WindowHandle _handle)
  921. {
  922. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_TOGGLE_FRAME, _handle.idx, 0);
  923. }
  924. void toggleFullscreen(WindowHandle _handle)
  925. {
  926. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_TOGGLE_FRAME, _handle.idx, 0);
  927. }
  928. void setMouseLock(WindowHandle _handle, bool _lock)
  929. {
  930. PostMessage(s_ctx.m_hwnd[0], WM_USER_WINDOW_MOUSE_LOCK, _handle.idx, _lock);
  931. }
  932. int32_t MainThreadEntry::threadFunc(void* _userData)
  933. {
  934. MainThreadEntry* self = (MainThreadEntry*)_userData;
  935. int32_t result = main(self->m_argc, self->m_argv);
  936. PostMessage(s_ctx.m_hwnd[0], WM_QUIT, 0, 0);
  937. return result;
  938. }
  939. } // namespace entry
  940. int main(int _argc, char** _argv)
  941. {
  942. using namespace entry;
  943. return s_ctx.run(_argc, _argv);
  944. }
  945. #endif // BX_PLATFORM_WINDOWS