BsPlatformImpl.cpp 13 KB

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