CmPlatformImpl.cpp 13 KB

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