BsPlatformImpl.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. #include "BsPlatform.h"
  2. #include "BsRenderWindow.h"
  3. #include "BsPixelUtil.h"
  4. #include "BsCoreApplication.h"
  5. #include "BsDebug.h"
  6. #include "Win32/BsWin32Defs.h"
  7. #include "Win32/BsWin32DropTarget.h"
  8. #include <iphlpapi.h>
  9. namespace BansheeEngine
  10. {
  11. Event<void(RenderWindow*)> Platform::onMouseLeftWindow;
  12. Event<void(const Vector2I&, OSPointerButtonStates)> Platform::onCursorMoved;
  13. Event<void(const Vector2I&, OSMouseButton button, OSPointerButtonStates)> Platform::onCursorButtonPressed;
  14. Event<void(const Vector2I&, OSMouseButton button, OSPointerButtonStates)> Platform::onCursorButtonReleased;
  15. Event<void(const Vector2I&, OSPointerButtonStates)> Platform::onCursorDoubleClick;
  16. Event<void(InputCommandType)> Platform::onInputCommand;
  17. Event<void(float)> Platform::onMouseWheelScrolled;
  18. Event<void(UINT32)> Platform::onCharInput;
  19. Event<void(RenderWindowCore*)> Platform::onWindowFocusReceived;
  20. Event<void(RenderWindowCore*)> Platform::onWindowFocusLost;
  21. Event<void(RenderWindowCore*)> Platform::onWindowMovedOrResized;
  22. Event<void()> Platform::onMouseCaptureChanged;
  23. Map<const RenderWindowCore*, WindowNonClientAreaData> Platform::mNonClientAreas;
  24. bool Platform::mIsTrackingMouse = false;
  25. Vector<RenderWindowCore*> Platform::mMouseLeftWindows;
  26. Stack<RenderWindowCore*> Platform::mModalWindowStack;
  27. NativeDropTargetData Platform::mDropTargets;
  28. bool Platform::mRequiresStartUp = false;
  29. bool Platform::mRequiresShutDown = false;
  30. BS_STATIC_MUTEX_CLASS_INSTANCE(mSync, Platform);
  31. struct NativeCursorData::Pimpl
  32. {
  33. HCURSOR cursor;
  34. };
  35. NativeCursorData::NativeCursorData()
  36. {
  37. data = bs_new<Pimpl>();
  38. }
  39. NativeCursorData::~NativeCursorData()
  40. {
  41. bs_delete(data);
  42. }
  43. struct NativeDropTargetData::Pimpl
  44. {
  45. Map<const RenderWindow*, Win32DropTarget*> dropTargetsPerWindow;
  46. Vector<Win32DropTarget*> dropTargetsToInitialize;
  47. Vector<Win32DropTarget*> dropTargetsToDestroy;
  48. };
  49. NativeDropTargetData::NativeDropTargetData()
  50. {
  51. data = bs_new<Pimpl>();
  52. }
  53. NativeDropTargetData::~NativeDropTargetData()
  54. {
  55. bs_delete(data);
  56. }
  57. bool Platform::mIsCursorHidden = false;
  58. NativeCursorData Platform::mCursor;
  59. bool Platform::mUsingCustomCursor = false;
  60. Vector2I Platform::getCursorPosition()
  61. {
  62. Vector2I screenPos;
  63. POINT cursorPos;
  64. GetCursorPos(&cursorPos);
  65. screenPos.x = cursorPos.x;
  66. screenPos.y = cursorPos.y;
  67. return screenPos;
  68. }
  69. void Platform::setCursorPosition(const Vector2I& screenPos)
  70. {
  71. SetCursorPos(screenPos.x, screenPos.y);
  72. }
  73. void Platform::captureMouse(const RenderWindow& window)
  74. {
  75. RenderWindowPtr primaryWindow = gCoreApplication().getPrimaryWindow();
  76. HWND hwnd;
  77. primaryWindow->getCustomAttribute("WINDOW", &hwnd);
  78. PostMessage(hwnd, WM_BS_SETCAPTURE, WPARAM(hwnd), 0);
  79. }
  80. void Platform::releaseMouseCapture()
  81. {
  82. RenderWindowPtr primaryWindow = gCoreApplication().getPrimaryWindow();
  83. HWND hwnd;
  84. primaryWindow->getCustomAttribute("WINDOW", &hwnd);
  85. PostMessage(hwnd, WM_BS_RELEASECAPTURE, WPARAM(hwnd), 0);
  86. }
  87. bool Platform::isPointOverWindow(const RenderWindow& window, const Vector2I& screenPos)
  88. {
  89. RenderWindowPtr primaryWindow = gCoreApplication().getPrimaryWindow();
  90. POINT point;
  91. point.x = screenPos.x;
  92. point.y = screenPos.y;
  93. HWND hwndToCheck;
  94. window.getCustomAttribute("WINDOW", &hwndToCheck);
  95. HWND hwndUnderPos = WindowFromPoint(point);
  96. return hwndUnderPos == hwndToCheck;
  97. }
  98. void Platform::hideCursor()
  99. {
  100. mIsCursorHidden = true;
  101. // ShowCursor(FALSE) doesn't work. Presumably because we're in the wrong thread, and using
  102. // WM_SETCURSOR in message loop to hide the cursor is smarter solution anyway.
  103. RenderWindowPtr primaryWindow = gCoreApplication().getPrimaryWindow();
  104. HWND hwnd;
  105. primaryWindow->getCustomAttribute("WINDOW", &hwnd);
  106. PostMessage(hwnd, WM_SETCURSOR, WPARAM(hwnd), (LPARAM)MAKELONG(HTCLIENT, WM_MOUSEMOVE));
  107. }
  108. void Platform::showCursor()
  109. {
  110. mIsCursorHidden = false;
  111. // ShowCursor(FALSE) doesn't work. Presumably because we're in the wrong thread, and using
  112. // WM_SETCURSOR in message loop to hide the cursor is smarter solution anyway.
  113. RenderWindowPtr primaryWindow = gCoreApplication().getPrimaryWindow();
  114. HWND hwnd;
  115. primaryWindow->getCustomAttribute("WINDOW", &hwnd);
  116. PostMessage(hwnd, WM_SETCURSOR, WPARAM(hwnd), (LPARAM)MAKELONG(HTCLIENT, WM_MOUSEMOVE));
  117. }
  118. void Platform::clipCursorToWindow(const RenderWindow& window)
  119. {
  120. HWND hwnd;
  121. window.getCustomAttribute("WINDOW", &hwnd);
  122. // Clip cursor to the window
  123. RECT clipWindowRect;
  124. if(GetWindowRect(hwnd, &clipWindowRect))
  125. {
  126. ClipCursor(&clipWindowRect);
  127. }
  128. }
  129. void Platform::clipCursorToRect(const RectI& screenRect)
  130. {
  131. RECT clipWindowRect;
  132. clipWindowRect.left = screenRect.x;
  133. clipWindowRect.top = screenRect.y;
  134. clipWindowRect.right = screenRect.x + screenRect.width;
  135. clipWindowRect.bottom = screenRect.y + screenRect.height;
  136. ClipCursor(&clipWindowRect);
  137. }
  138. void Platform::clipCursorDisable()
  139. {
  140. ClipCursor(NULL);
  141. }
  142. // TODO - Add support for animated custom cursor
  143. void Platform::setCursor(PixelData& pixelData, const Vector2I& hotSpot)
  144. {
  145. if(mUsingCustomCursor)
  146. {
  147. SetCursor(0);
  148. DestroyIcon(mCursor.data->cursor);
  149. }
  150. mUsingCustomCursor = true;
  151. BITMAPV5HEADER bi;
  152. ZeroMemory(&bi,sizeof(BITMAPV5HEADER));
  153. bi.bV5Size = sizeof(BITMAPV5HEADER);
  154. bi.bV5Width = pixelData.getWidth();
  155. bi.bV5Height = pixelData.getHeight();
  156. bi.bV5Planes = 1;
  157. bi.bV5BitCount = 32;
  158. bi.bV5Compression = BI_BITFIELDS;
  159. bi.bV5RedMask = 0x00FF0000;
  160. bi.bV5GreenMask = 0x0000FF00;
  161. bi.bV5BlueMask = 0x000000FF;
  162. bi.bV5AlphaMask = 0xFF000000;
  163. HDC hDC = GetDC(NULL);
  164. void* data = nullptr;
  165. HBITMAP hBitmap = CreateDIBSection(hDC, (BITMAPINFO *)&bi, DIB_RGB_COLORS,
  166. (void**)&data, NULL, (DWORD)0);
  167. HDC hBitmapDC = CreateCompatibleDC(hDC);
  168. ReleaseDC(NULL, hDC);
  169. // Create an empty mask bitmap.
  170. HBITMAP hMonoBitmap = CreateBitmap(pixelData.getWidth(), pixelData.getHeight(), 1, 1, NULL);
  171. //Select the bitmaps to DC
  172. HBITMAP hOldBitmap = (HBITMAP)SelectObject(hBitmapDC, hBitmap);
  173. //Scan each pixel of the source bitmap and create the masks
  174. Color pixel;
  175. DWORD *dst = (DWORD*)data;
  176. for(UINT32 y = 0; y < pixelData.getHeight(); ++y)
  177. {
  178. for(UINT32 x = 0; x < pixelData.getWidth(); ++x)
  179. {
  180. pixel = pixelData.getColorAt(x, pixelData.getHeight() - y - 1);
  181. *dst = pixel.getAsBGRA();
  182. dst++;
  183. }
  184. }
  185. SelectObject(hBitmapDC, hOldBitmap);
  186. DeleteDC(hBitmapDC);
  187. ICONINFO iconinfo = {0};
  188. iconinfo.fIcon = FALSE;
  189. iconinfo.xHotspot = (DWORD)hotSpot.x;
  190. iconinfo.yHotspot = (DWORD)hotSpot.y;
  191. iconinfo.hbmMask = hMonoBitmap;
  192. iconinfo.hbmColor = hBitmap;
  193. mCursor.data->cursor = CreateIconIndirect(&iconinfo);
  194. DeleteObject(hBitmap);
  195. DeleteObject(hMonoBitmap);
  196. // Make sure we notify the message loop to perform the actual cursor update
  197. RenderWindowPtr primaryWindow = gCoreApplication().getPrimaryWindow();
  198. HWND hwnd;
  199. primaryWindow->getCustomAttribute("WINDOW", &hwnd);
  200. PostMessage(hwnd, WM_SETCURSOR, WPARAM(hwnd), (LPARAM)MAKELONG(HTCLIENT, WM_MOUSEMOVE));
  201. }
  202. void Platform::setCaptionNonClientAreas(const RenderWindow& window, const Vector<RectI>& nonClientAreas)
  203. {
  204. BS_LOCK_MUTEX(mSync);
  205. mNonClientAreas[window.getCore()].moveAreas = nonClientAreas;
  206. }
  207. void Platform::setResizeNonClientAreas(const RenderWindow& window, const Vector<NonClientResizeArea>& nonClientAreas)
  208. {
  209. BS_LOCK_MUTEX(mSync);
  210. mNonClientAreas[window.getCore()].resizeAreas = nonClientAreas;
  211. }
  212. void Platform::resetNonClientAreas(const RenderWindow& window)
  213. {
  214. BS_LOCK_MUTEX(mSync);
  215. auto iterFind = mNonClientAreas.find(window.getCore());
  216. if(iterFind != end(mNonClientAreas))
  217. mNonClientAreas.erase(iterFind);
  218. }
  219. void Platform::copyToClipboard(const WString& string)
  220. {
  221. HANDLE hData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (string.size() + 1) * sizeof(WString::value_type));
  222. WString::value_type* buffer = (WString::value_type*)GlobalLock(hData);
  223. string.copy(buffer, string.size());
  224. buffer[string.size()] = '\0';
  225. GlobalUnlock(hData);
  226. if(OpenClipboard(NULL))
  227. {
  228. EmptyClipboard();
  229. SetClipboardData(CF_UNICODETEXT, hData);
  230. CloseClipboard();
  231. }
  232. else
  233. {
  234. GlobalFree(hData);
  235. }
  236. }
  237. WString Platform::copyFromClipboard()
  238. {
  239. if(OpenClipboard(NULL))
  240. {
  241. HANDLE hData = GetClipboardData(CF_UNICODETEXT);
  242. if(hData != NULL)
  243. {
  244. WString::value_type* buffer = (WString::value_type*)GlobalLock(hData);
  245. WString string(buffer);
  246. GlobalUnlock(hData);
  247. CloseClipboard();
  248. return string;
  249. }
  250. else
  251. {
  252. CloseClipboard();
  253. return L"";
  254. }
  255. }
  256. return L"";
  257. }
  258. bool Platform::getMACAddress(MACAddress& address)
  259. {
  260. std::memset(&address, 0, sizeof(address));
  261. PIP_ADAPTER_INFO adapterInfo = bs_alloc<IP_ADAPTER_INFO>();
  262. ULONG len = sizeof(IP_ADAPTER_INFO);
  263. DWORD rc = GetAdaptersInfo(adapterInfo, &len);
  264. if (rc == ERROR_BUFFER_OVERFLOW)
  265. {
  266. bs_free(adapterInfo);
  267. adapterInfo = reinterpret_cast<IP_ADAPTER_INFO*>(bs_alloc(len));
  268. }
  269. else if (rc != ERROR_SUCCESS)
  270. {
  271. bs_free(adapterInfo);
  272. return false;
  273. }
  274. if (GetAdaptersInfo(adapterInfo, &len) == NO_ERROR)
  275. {
  276. PIP_ADAPTER_INFO curAdapter = nullptr;
  277. curAdapter = adapterInfo;
  278. while (curAdapter)
  279. {
  280. if (curAdapter->Type == MIB_IF_TYPE_ETHERNET && curAdapter->AddressLength == sizeof(address))
  281. {
  282. std::memcpy(&address, curAdapter->Address, curAdapter->AddressLength);
  283. return true;
  284. }
  285. curAdapter = curAdapter->Next;
  286. }
  287. }
  288. bs_free(adapterInfo);
  289. return false;
  290. }
  291. double Platform::queryPerformanceTimerMs()
  292. {
  293. LARGE_INTEGER counterValue;
  294. QueryPerformanceCounter(&counterValue);
  295. LARGE_INTEGER counterFreq;
  296. QueryPerformanceFrequency(&counterFreq);
  297. return (double)counterValue.QuadPart / (counterFreq.QuadPart * 0.001);
  298. }
  299. OSDropTarget& Platform::createDropTarget(const RenderWindow* window, int x, int y, unsigned int width, unsigned int height)
  300. {
  301. Win32DropTarget* win32DropTarget = nullptr;
  302. auto iterFind = mDropTargets.data->dropTargetsPerWindow.find(window);
  303. if(iterFind == mDropTargets.data->dropTargetsPerWindow.end())
  304. {
  305. HWND hwnd;
  306. window->getCustomAttribute("WINDOW", &hwnd);
  307. win32DropTarget = bs_new<Win32DropTarget>(hwnd);
  308. mDropTargets.data->dropTargetsPerWindow[window] = win32DropTarget;
  309. {
  310. BS_LOCK_MUTEX(mSync);
  311. mDropTargets.data->dropTargetsToInitialize.push_back(win32DropTarget);
  312. }
  313. }
  314. else
  315. win32DropTarget = iterFind->second;
  316. OSDropTarget* newDropTarget = new (bs_alloc<OSDropTarget>()) OSDropTarget(window, x, y, width, height);
  317. win32DropTarget->registerDropTarget(newDropTarget);
  318. return *newDropTarget;
  319. }
  320. void Platform::destroyDropTarget(OSDropTarget& target)
  321. {
  322. auto iterFind = mDropTargets.data->dropTargetsPerWindow.find(target.getOwnerWindow());
  323. if(iterFind == mDropTargets.data->dropTargetsPerWindow.end())
  324. {
  325. LOGWRN("Attempting to destroy a drop target but cannot find its parent window.");
  326. }
  327. else
  328. {
  329. Win32DropTarget* win32DropTarget = iterFind->second;
  330. win32DropTarget->unregisterDropTarget(&target);
  331. if(win32DropTarget->getNumDropTargets() == 0)
  332. {
  333. mDropTargets.data->dropTargetsPerWindow.erase(iterFind);
  334. {
  335. BS_LOCK_MUTEX(mSync);
  336. mDropTargets.data->dropTargetsToDestroy.push_back(win32DropTarget);
  337. }
  338. }
  339. }
  340. BS_PVT_DELETE(OSDropTarget, &target);
  341. }
  342. void Platform::_messagePump()
  343. {
  344. MSG msg;
  345. while(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
  346. {
  347. TranslateMessage(&msg);
  348. DispatchMessage(&msg);
  349. }
  350. }
  351. void Platform::_startUp()
  352. {
  353. BS_LOCK_MUTEX(mSync);
  354. mRequiresStartUp = true;
  355. }
  356. void Platform::_update()
  357. {
  358. Vector<RenderWindowCore*> windowsCopy;
  359. {
  360. BS_LOCK_MUTEX(mSync);
  361. windowsCopy = mMouseLeftWindows;
  362. mMouseLeftWindows.clear();
  363. }
  364. for(auto& window : windowsCopy)
  365. {
  366. if(!onMouseLeftWindow.empty())
  367. onMouseLeftWindow(window->getNonCore());
  368. }
  369. for(auto& dropTarget : mDropTargets.data->dropTargetsPerWindow)
  370. {
  371. dropTarget.second->update();
  372. }
  373. }
  374. void Platform::_coreUpdate()
  375. {
  376. {
  377. BS_LOCK_MUTEX(mSync);
  378. if(mRequiresStartUp)
  379. {
  380. OleInitialize(nullptr);
  381. mRequiresStartUp = false;
  382. }
  383. }
  384. {
  385. BS_LOCK_MUTEX(mSync);
  386. for(auto& dropTargetToInit : mDropTargets.data->dropTargetsToInitialize)
  387. {
  388. dropTargetToInit->registerWithOS();
  389. }
  390. mDropTargets.data->dropTargetsToInitialize.clear();
  391. }
  392. {
  393. BS_LOCK_MUTEX(mSync);
  394. for(auto& dropTargetToDestroy : mDropTargets.data->dropTargetsToDestroy)
  395. {
  396. dropTargetToDestroy->unregisterWithOS();
  397. dropTargetToDestroy->Release();
  398. }
  399. mDropTargets.data->dropTargetsToDestroy.clear();
  400. }
  401. _messagePump();
  402. {
  403. BS_LOCK_MUTEX(mSync);
  404. if(mRequiresShutDown)
  405. {
  406. OleUninitialize();
  407. mRequiresShutDown = false;
  408. }
  409. }
  410. }
  411. void Platform::_shutDown()
  412. {
  413. BS_LOCK_MUTEX(mSync);
  414. mRequiresShutDown = true;
  415. }
  416. void Platform::windowFocusReceived(RenderWindowCore* window)
  417. {
  418. if(!onWindowFocusReceived.empty())
  419. onWindowFocusReceived(window);
  420. }
  421. void Platform::windowFocusLost(RenderWindowCore* window)
  422. {
  423. if(!onWindowFocusLost.empty())
  424. onWindowFocusLost(window);
  425. }
  426. void Platform::windowMovedOrResized(RenderWindowCore* window)
  427. {
  428. if(!onWindowMovedOrResized.empty())
  429. onWindowMovedOrResized(window);
  430. }
  431. void Platform::win32ShowCursor()
  432. {
  433. SetCursor(mCursor.data->cursor);
  434. }
  435. void Platform::win32HideCursor()
  436. {
  437. SetCursor(nullptr);
  438. }
  439. }