entry_windows.cpp 28 KB

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