CmPlatformImpl.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. #include "CmPlatform.h"
  2. #include "CmRenderWindow.h"
  3. #include "CmPixelUtil.h"
  4. #include "CmApplication.h"
  5. #include "CmWin32Defs.h"
  6. #include "CmDebug.h"
  7. #include "Win32/CmWin32DropTarget.h"
  8. namespace CamelotFramework
  9. {
  10. boost::signal<void(RenderWindow*)> Platform::onMouseLeftWindow;
  11. boost::signal<void(const Vector2I&, OSPositionalInputButtonStates)> Platform::onCursorMoved;
  12. boost::signal<void(const Vector2I&, OSMouseButton button, OSPositionalInputButtonStates)> Platform::onCursorButtonPressed;
  13. boost::signal<void(const Vector2I&, OSMouseButton button, OSPositionalInputButtonStates)> Platform::onCursorButtonReleased;
  14. boost::signal<void(const Vector2I&, OSPositionalInputButtonStates)> Platform::onCursorDoubleClick;
  15. boost::signal<void(InputCommandType)> Platform::onInputCommand;
  16. boost::signal<void(float)> Platform::onMouseWheelScrolled;
  17. boost::signal<void(UINT32)> Platform::onCharInput;
  18. boost::signal<void(RenderWindow*)> Platform::onWindowFocusReceived;
  19. boost::signal<void(RenderWindow*)> Platform::onWindowFocusLost;
  20. boost::signal<void(RenderWindow*)> Platform::onWindowMovedOrResized;
  21. boost::signal<void()> Platform::onMouseCaptureChanged;
  22. Map<const RenderWindow*, WindowNonClientAreaData>::type Platform::mNonClientAreas;
  23. bool Platform::mIsTrackingMouse = false;
  24. Vector<RenderWindow*>::type Platform::mMouseLeftWindows;
  25. Stack<RenderWindow*>::type Platform::mModalWindowStack;
  26. NativeDropTargetData Platform::mDropTargets;
  27. bool Platform::mRequiresStartUp = false;
  28. bool Platform::mRequiresShutDown = false;
  29. CM_STATIC_MUTEX_CLASS_INSTANCE(mSync, Platform);
  30. struct NativeCursorData::Pimpl
  31. {
  32. HCURSOR cursor;
  33. };
  34. NativeCursorData::NativeCursorData()
  35. {
  36. data = cm_new<Pimpl>();
  37. }
  38. NativeCursorData::~NativeCursorData()
  39. {
  40. cm_delete(data);
  41. }
  42. struct NativeDropTargetData::Pimpl
  43. {
  44. Map<const RenderWindow*, Win32DropTarget*>::type dropTargetsPerWindow;
  45. Vector<Win32DropTarget*>::type dropTargetsToInitialize;
  46. Vector<Win32DropTarget*>::type dropTargetsToDestroy;
  47. };
  48. NativeDropTargetData::NativeDropTargetData()
  49. {
  50. data = cm_new<Pimpl>();
  51. }
  52. NativeDropTargetData::~NativeDropTargetData()
  53. {
  54. cm_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 = gApplication().getPrimaryWindow();
  75. HWND hwnd;
  76. primaryWindow->getCustomAttribute("WINDOW", &hwnd);
  77. PostMessage(hwnd, WM_CM_SETCAPTURE, WPARAM(hwnd), 0);
  78. }
  79. void Platform::releaseMouseCapture()
  80. {
  81. RenderWindowPtr primaryWindow = gApplication().getPrimaryWindow();
  82. HWND hwnd;
  83. primaryWindow->getCustomAttribute("WINDOW", &hwnd);
  84. PostMessage(hwnd, WM_CM_RELEASECAPTURE, WPARAM(hwnd), 0);
  85. }
  86. bool Platform::isPointOverWindow(const RenderWindow& window, const Vector2I& screenPos)
  87. {
  88. RenderWindowPtr primaryWindow = gApplication().getPrimaryWindow();
  89. POINT point;
  90. point.x = screenPos.x;
  91. point.y = screenPos.y;
  92. HWND hwndToCheck;
  93. window.getCustomAttribute("WINDOW", &hwndToCheck);
  94. HWND hwndUnderPos = WindowFromPoint(point);
  95. return hwndUnderPos == 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 = gApplication().getPrimaryWindow();
  103. HWND hwnd;
  104. primaryWindow->getCustomAttribute("WINDOW", &hwnd);
  105. PostMessage(hwnd, WM_SETCURSOR, WPARAM(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 = gApplication().getPrimaryWindow();
  113. HWND hwnd;
  114. primaryWindow->getCustomAttribute("WINDOW", &hwnd);
  115. PostMessage(hwnd, WM_SETCURSOR, WPARAM(hwnd), (LPARAM)MAKELONG(HTCLIENT, WM_MOUSEMOVE));
  116. }
  117. void Platform::clipCursorToWindow(const RenderWindow& window)
  118. {
  119. HWND hwnd;
  120. window.getCustomAttribute("WINDOW", &hwnd);
  121. // Clip cursor to the window
  122. RECT clipWindowRect;
  123. if(GetWindowRect(hwnd, &clipWindowRect))
  124. {
  125. ClipCursor(&clipWindowRect);
  126. }
  127. }
  128. void Platform::clipCursorToRect(const RectI& 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 = gApplication().getPrimaryWindow();
  197. HWND hwnd;
  198. primaryWindow->getCustomAttribute("WINDOW", &hwnd);
  199. PostMessage(hwnd, WM_SETCURSOR, WPARAM(hwnd), (LPARAM)MAKELONG(HTCLIENT, WM_MOUSEMOVE));
  200. }
  201. void Platform::setCaptionNonClientAreas(const RenderWindow& window, const Vector<RectI>::type& nonClientAreas)
  202. {
  203. CM_LOCK_MUTEX(mSync);
  204. mNonClientAreas[&window].moveAreas = nonClientAreas;
  205. }
  206. void Platform::setResizeNonClientAreas(const RenderWindow& window, const Vector<NonClientResizeArea>::type& nonClientAreas)
  207. {
  208. CM_LOCK_MUTEX(mSync);
  209. mNonClientAreas[&window].resizeAreas = nonClientAreas;
  210. }
  211. void Platform::resetNonClientAreas(const RenderWindow& window)
  212. {
  213. CM_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. double Platform::queryPerformanceTimerMs()
  258. {
  259. LARGE_INTEGER counterValue;
  260. QueryPerformanceCounter(&counterValue);
  261. LARGE_INTEGER counterFreq;
  262. QueryPerformanceFrequency(&counterFreq);
  263. return (double)counterValue.QuadPart / (counterFreq.QuadPart * 0.001);
  264. }
  265. OSDropTarget& Platform::createDropTarget(const RenderWindow* window, int x, int y, unsigned int width, unsigned int height)
  266. {
  267. Win32DropTarget* win32DropTarget = nullptr;
  268. auto iterFind = mDropTargets.data->dropTargetsPerWindow.find(window);
  269. if(iterFind == mDropTargets.data->dropTargetsPerWindow.end())
  270. {
  271. HWND hwnd;
  272. window->getCustomAttribute("WINDOW", &hwnd);
  273. win32DropTarget = cm_new<Win32DropTarget>(hwnd);
  274. mDropTargets.data->dropTargetsPerWindow[window] = win32DropTarget;
  275. {
  276. CM_LOCK_MUTEX(mSync);
  277. mDropTargets.data->dropTargetsToInitialize.push_back(win32DropTarget);
  278. }
  279. }
  280. else
  281. win32DropTarget = iterFind->second;
  282. OSDropTarget* newDropTarget = new (cm_alloc<OSDropTarget>()) OSDropTarget(window, x, y, width, height);
  283. win32DropTarget->registerDropTarget(newDropTarget);
  284. return *newDropTarget;
  285. }
  286. void Platform::destroyDropTarget(OSDropTarget& target)
  287. {
  288. auto iterFind = mDropTargets.data->dropTargetsPerWindow.find(target.getOwnerWindow());
  289. if(iterFind == mDropTargets.data->dropTargetsPerWindow.end())
  290. {
  291. LOGWRN("Attempting to destroy a drop target but cannot find its parent window.");
  292. }
  293. else
  294. {
  295. Win32DropTarget* win32DropTarget = iterFind->second;
  296. win32DropTarget->unregisterDropTarget(&target);
  297. if(win32DropTarget->getNumDropTargets() == 0)
  298. {
  299. mDropTargets.data->dropTargetsPerWindow.erase(iterFind);
  300. {
  301. CM_LOCK_MUTEX(mSync);
  302. mDropTargets.data->dropTargetsToDestroy.push_back(win32DropTarget);
  303. }
  304. }
  305. }
  306. CM_PVT_DELETE(OSDropTarget, &target);
  307. }
  308. void Platform::messagePump()
  309. {
  310. MSG msg;
  311. while(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
  312. {
  313. TranslateMessage(&msg);
  314. DispatchMessage(&msg);
  315. }
  316. }
  317. void Platform::startUp()
  318. {
  319. CM_LOCK_MUTEX(mSync);
  320. mRequiresStartUp = true;
  321. }
  322. void Platform::update()
  323. {
  324. Vector<RenderWindow*>::type windowsCopy;
  325. {
  326. CM_LOCK_MUTEX(mSync);
  327. windowsCopy = mMouseLeftWindows;
  328. mMouseLeftWindows.clear();
  329. }
  330. for(auto& window : windowsCopy)
  331. {
  332. if(!onMouseLeftWindow.empty())
  333. onMouseLeftWindow(window);
  334. }
  335. for(auto& dropTarget : mDropTargets.data->dropTargetsPerWindow)
  336. {
  337. dropTarget.second->update();
  338. }
  339. }
  340. void Platform::coreUpdate()
  341. {
  342. {
  343. CM_LOCK_MUTEX(mSync);
  344. if(mRequiresStartUp)
  345. {
  346. OleInitialize(nullptr);
  347. mRequiresStartUp = false;
  348. }
  349. }
  350. {
  351. CM_LOCK_MUTEX(mSync);
  352. for(auto& dropTargetToInit : mDropTargets.data->dropTargetsToInitialize)
  353. {
  354. dropTargetToInit->registerWithOS();
  355. }
  356. mDropTargets.data->dropTargetsToInitialize.clear();
  357. }
  358. {
  359. CM_LOCK_MUTEX(mSync);
  360. for(auto& dropTargetToDestroy : mDropTargets.data->dropTargetsToDestroy)
  361. {
  362. dropTargetToDestroy->unregisterWithOS();
  363. dropTargetToDestroy->Release();
  364. }
  365. mDropTargets.data->dropTargetsToDestroy.clear();
  366. }
  367. messagePump();
  368. {
  369. CM_LOCK_MUTEX(mSync);
  370. if(mRequiresShutDown)
  371. {
  372. OleUninitialize();
  373. mRequiresShutDown = false;
  374. }
  375. }
  376. }
  377. void Platform::shutDown()
  378. {
  379. CM_LOCK_MUTEX(mSync);
  380. mRequiresShutDown = true;
  381. }
  382. void Platform::windowFocusReceived(RenderWindow* window)
  383. {
  384. if(!onWindowFocusReceived.empty())
  385. onWindowFocusReceived(window);
  386. }
  387. void Platform::windowFocusLost(RenderWindow* window)
  388. {
  389. if(!onWindowFocusLost.empty())
  390. onWindowFocusLost(window);
  391. }
  392. void Platform::windowMovedOrResized(RenderWindow* window)
  393. {
  394. if(!onWindowMovedOrResized.empty())
  395. onWindowMovedOrResized(window);
  396. }
  397. void Platform::win32ShowCursor()
  398. {
  399. SetCursor(mCursor.data->cursor);
  400. }
  401. void Platform::win32HideCursor()
  402. {
  403. SetCursor(nullptr);
  404. }
  405. }