BsPlatformImpl.cpp 12 KB

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