win32WindowMgr.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platformWin32/platformWin32.h"
  23. #include "windowManager/win32/win32WindowMgr.h"
  24. #include "gfx/gfxDevice.h"
  25. #include "windowManager/win32/winDispatch.h"
  26. #include "core/util/journal/process.h"
  27. #include "core/strings/unicode.h"
  28. // ------------------------------------------------------------------------
  29. void CloseSplashWindow(HINSTANCE hinst);
  30. PlatformWindowManager * CreatePlatformWindowManager()
  31. {
  32. return new Win32WindowManager();
  33. }
  34. // ------------------------------------------------------------------------
  35. Win32WindowManager::Win32WindowManager()
  36. {
  37. // Register in the process list.
  38. Process::notify(this, &Win32WindowManager::_process, PROCESS_INPUT_ORDER);
  39. // Init our list of allocated windows.
  40. mWindowListHead = NULL;
  41. // By default, we have no parent window.
  42. mParentWindow = NULL;
  43. mCurtainWindow = NULL;
  44. mOffscreenRender = false;
  45. buildMonitorsList();
  46. }
  47. Win32WindowManager::~Win32WindowManager()
  48. {
  49. // Get ourselves off the process list.
  50. Process::remove(this, &Win32WindowManager::_process);
  51. // Kill all our windows first.
  52. while(mWindowListHead)
  53. // The destructors update the list, so this works just fine.
  54. delete mWindowListHead;
  55. }
  56. RectI Win32WindowManager::getPrimaryDesktopArea()
  57. {
  58. RECT primaryWorkRect;
  59. SystemParametersInfo(SPI_GETWORKAREA, 0, &primaryWorkRect, 0);
  60. RectI res;
  61. res.point.x = primaryWorkRect.left;
  62. res.point.y = primaryWorkRect.top;
  63. res.extent.x = primaryWorkRect.right - primaryWorkRect.left;
  64. res.extent.y = primaryWorkRect.bottom - primaryWorkRect.top;
  65. return res;
  66. }
  67. Point2I Win32WindowManager::getDesktopResolution()
  68. {
  69. DEVMODE devMode;
  70. dMemset( &devMode, 0, sizeof( devMode ) );
  71. devMode.dmSize = sizeof( devMode );
  72. if (!::EnumDisplaySettings(NULL, ENUM_REGISTRY_SETTINGS, &devMode))
  73. return Point2I(-1,-1);
  74. // Return Resolution
  75. return Point2I(devMode.dmPelsWidth, devMode.dmPelsHeight);
  76. }
  77. S32 Win32WindowManager::getDesktopBitDepth()
  78. {
  79. DEVMODE devMode;
  80. dMemset( &devMode, 0, sizeof( devMode ) );
  81. devMode.dmSize = sizeof( devMode );
  82. if (!::EnumDisplaySettings(NULL, ENUM_REGISTRY_SETTINGS, &devMode))
  83. return -1;
  84. // Return Bits per Pixel
  85. return (S32)devMode.dmBitsPerPel;
  86. }
  87. BOOL Win32WindowManager::MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData )
  88. {
  89. Vector<MonitorInfo> * monitors = (Vector<MonitorInfo>*)dwData;
  90. // Fill out the new monitor structure
  91. monitors->increment();
  92. MonitorInfo& monitor = monitors->last();
  93. monitor.monitorHandle = hMonitor;
  94. monitor.region.point.x = lprcMonitor->left;
  95. monitor.region.point.y = lprcMonitor->top;
  96. monitor.region.extent.x = lprcMonitor->right - lprcMonitor->left;
  97. monitor.region.extent.y = lprcMonitor->bottom - lprcMonitor->top;
  98. MONITORINFOEX info;
  99. info.cbSize = sizeof(MONITORINFOEX);
  100. if(GetMonitorInfo(hMonitor, &info))
  101. {
  102. monitor.name = info.szDevice;
  103. }
  104. return true;
  105. }
  106. void Win32WindowManager::buildMonitorsList()
  107. {
  108. // Clear the list
  109. mMonitors.clear();
  110. // Enumerate all monitors
  111. EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, (U32)(void*)&mMonitors);
  112. }
  113. S32 Win32WindowManager::findFirstMatchingMonitor(const char* name)
  114. {
  115. // Try and match the first part of the output device display name. For example,
  116. // a Monitor name of "\\.\DISPLAY1" might correspond to a display name
  117. // of "\\.\DISPLAY1\Monitor0". If two monitors are set up in duplicate mode then
  118. // they will have the same 'display' part in their display name.
  119. for(U32 i=0; i<mMonitors.size(); ++i)
  120. {
  121. if(dStrstr(name, mMonitors[i].name) == name)
  122. return i;
  123. }
  124. return -1;
  125. }
  126. U32 Win32WindowManager::getMonitorCount()
  127. {
  128. return mMonitors.size();
  129. }
  130. const char* Win32WindowManager::getMonitorName(U32 index)
  131. {
  132. if(index >= mMonitors.size())
  133. return "";
  134. return mMonitors[index].name.c_str();
  135. }
  136. RectI Win32WindowManager::getMonitorRect(U32 index)
  137. {
  138. if(index >= mMonitors.size())
  139. return RectI(0, 0, 0, 0);
  140. return mMonitors[index].region;
  141. }
  142. BOOL Win32WindowManager::MonitorRegionEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData )
  143. {
  144. Vector<RectI> * regions = (Vector<RectI>*)dwData;
  145. regions->increment();
  146. regions->last().point.x = lprcMonitor->left;
  147. regions->last().point.y = lprcMonitor->top;
  148. regions->last().extent.x = lprcMonitor->right - lprcMonitor->left;
  149. regions->last().extent.y = lprcMonitor->bottom - lprcMonitor->top;
  150. return true;
  151. }
  152. void Win32WindowManager::getMonitorRegions(Vector<RectI> &regions)
  153. {
  154. EnumDisplayMonitors(NULL, NULL, MonitorRegionEnumProc, (U32)(void*)&regions);
  155. }
  156. void Win32WindowManager::getWindows(VectorPtr<PlatformWindow*> &windows)
  157. {
  158. Win32Window *win = mWindowListHead;
  159. while(win)
  160. {
  161. windows.push_back(win);
  162. win = win->mNextWindow;
  163. }
  164. }
  165. PlatformWindow *Win32WindowManager::createWindow(GFXDevice *device, const GFXVideoMode &mode)
  166. {
  167. // Do the allocation.
  168. Win32Window *w32w = new Win32Window();
  169. w32w->setOffscreenRender(mOffscreenRender);
  170. w32w->mWindowId = getNextId();
  171. w32w->mOwningManager = this;
  172. // Link into our list of windows.
  173. linkWindow(w32w);
  174. DWORD dwExStyle;
  175. DWORD dwStyle = WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
  176. dwStyle |= WS_OVERLAPPEDWINDOW | WS_THICKFRAME | WS_CAPTION;
  177. dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
  178. // If we're parented, we want a different set of window styles.
  179. if(mParentWindow)
  180. dwStyle = WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_CHILDWINDOW;
  181. if (mOffscreenRender)
  182. {
  183. dwStyle = WS_OVERLAPPEDWINDOW;
  184. dwExStyle = 0;
  185. }
  186. // Create the window handle
  187. w32w->mWindowHandle = CreateWindowEx(
  188. dwExStyle,
  189. Win32Window::getWindowClassName(), //class name
  190. String( getEngineProductString() ).utf16(), //window title
  191. dwStyle, //style - need clip siblings/children for opengl
  192. 0,
  193. 0,
  194. 0,
  195. 0,
  196. mParentWindow, //parent window
  197. NULL, //menu? No.
  198. NULL, //the hInstance
  199. NULL ); //no funky params
  200. // Note the style we created with so we can switch back to it when we're
  201. // done with full-screen mode.
  202. w32w->mWindowedWindowStyle = dwStyle;
  203. // Set the video mode on the window
  204. w32w->setVideoMode(mode);
  205. // Associate our window struct with the HWND.
  206. SetWindowLongPtrW(w32w->mWindowHandle, GWLP_USERDATA, (LONG)w32w);
  207. // Do some error checking.
  208. AssertFatal(w32w->mWindowHandle != NULL, "Win32WindowManager::createWindow - Could not create window!");
  209. if(w32w->mWindowHandle == NULL)
  210. {
  211. Con::errorf("Win32WindowManager::createWindow - Could not create window!");
  212. delete w32w;
  213. return NULL;
  214. }
  215. // If we're not rendering offscreen, make sure our window is shown and drawn to.
  216. if (!mOffscreenRender)
  217. ShowWindow( w32w->mWindowHandle, SW_SHOWDEFAULT );
  218. // Close any splash screen we created
  219. CloseSplashWindow(winState.appInstance);
  220. // Bind the window to the specified device.
  221. if(device)
  222. {
  223. w32w->mDevice = device;
  224. w32w->mTarget = device->allocWindowTarget(w32w);
  225. AssertISV(w32w->mTarget,
  226. "Win32WindowManager::createWindow - failed to get a window target back from the device.");
  227. }
  228. else
  229. {
  230. Con::warnf("Win32WindowManager::createWindow - created a window with no device!");
  231. }
  232. // Update it if needed.
  233. UpdateWindow( w32w->mWindowHandle );
  234. return w32w;
  235. }
  236. void Win32WindowManager::setParentWindow(void* newParent)
  237. {
  238. Con::printf( "Setting parent HWND: %d", newParent );
  239. mParentWindow = (HWND)newParent;
  240. if( mWindowListHead && mWindowListHead->mWindowHandle )
  241. ::SetParent( mWindowListHead->mWindowHandle, mParentWindow);
  242. }
  243. void* Win32WindowManager::getParentWindow()
  244. {
  245. return (void*)mParentWindow;
  246. }
  247. void Win32WindowManager::_process()
  248. {
  249. MSG msg;
  250. bool _blocking = false;
  251. // CodeReview [tom, 4/30/2007] Maintaining two completely separate message
  252. // handlers that are essentially the same is silly. The first one never
  253. // seems to run as _blocking is hard coded to false above, so is this even
  254. // needed ? If it is, this should be rewritten to use the one loop that
  255. // adjusts as needed based on _blocking and Journal::IsPlaying()
  256. if (_blocking && !Journal::IsPlaying())
  257. {
  258. // In blocking mode, we process one message at a time.
  259. if (GetMessage(&msg, NULL, 0, 0))
  260. {
  261. bool noTranslate = false;
  262. Win32Window *w32w = mWindowListHead;
  263. while(w32w)
  264. {
  265. noTranslate = w32w->translateMessage(msg);
  266. if(noTranslate) break;
  267. w32w = w32w->mNextWindow;
  268. }
  269. if(! noTranslate)
  270. {
  271. TranslateMessage(&msg);
  272. DispatchMessage(&msg);
  273. }
  274. }
  275. else
  276. // This should be WM_QUIT
  277. Dispatch(ImmediateDispatch,0,msg.message,msg.wParam,msg.lParam);
  278. }
  279. else
  280. {
  281. // Process all queued up messages
  282. while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  283. {
  284. bool translated = false;
  285. // Win32Window *w32w = mWindowListHead;
  286. // while(w32w)
  287. // {
  288. // noTranslate = w32w->translateMessage(msg);
  289. // if(noTranslate) break;
  290. // w32w = w32w->mNextWindow;
  291. // }
  292. // [tom, 4/30/2007] I think this should work, but leaving the above commented
  293. // out just in case this is actually fubared with multiple windows.
  294. Win32Window* window = (Win32Window*)(GetWindowLong(msg.hwnd, GWL_USERDATA));
  295. if(window)
  296. translated = window->translateMessage(msg);
  297. if(! translated)
  298. {
  299. // Win32Window::translateMessage() will post a WM_COMMAND event for
  300. // translated accelerator events, so dispatching again will cause a
  301. // the input event to be dispatched, which is usually not what we want.
  302. TranslateMessage(&msg);
  303. DispatchMessage(&msg);
  304. }
  305. if (msg.message == WM_QUIT)
  306. {
  307. Dispatch(ImmediateDispatch,0,msg.message,msg.wParam,msg.lParam);
  308. break;
  309. }
  310. }
  311. }
  312. // Dispatch any delayed events
  313. while (DispatchNext());
  314. // Fire off idle events for every window.
  315. Win32Window *w32w = mWindowListHead;
  316. while(w32w)
  317. {
  318. w32w->idleEvent.trigger();
  319. w32w = w32w->mNextWindow;
  320. }
  321. }
  322. PlatformWindow * Win32WindowManager::getWindowById( WindowId id )
  323. {
  324. // Walk the list and find the matching id, if any.
  325. Win32Window *win = mWindowListHead;
  326. while(win)
  327. {
  328. if(win->getWindowId() == id)
  329. return win;
  330. win = win->mNextWindow;
  331. }
  332. return NULL;
  333. }
  334. PlatformWindow * Win32WindowManager::getFirstWindow()
  335. {
  336. return mWindowListHead != NULL ? mWindowListHead : NULL;
  337. }
  338. PlatformWindow* Win32WindowManager::getFocusedWindow()
  339. {
  340. Win32Window* window = mWindowListHead;
  341. while( window )
  342. {
  343. if( window->isFocused() )
  344. return window;
  345. window = window->mNextWindow;
  346. }
  347. return NULL;
  348. }
  349. void Win32WindowManager::linkWindow( Win32Window *w )
  350. {
  351. w->mNextWindow = mWindowListHead;
  352. mWindowListHead = w;
  353. }
  354. void Win32WindowManager::unlinkWindow( Win32Window *w )
  355. {
  356. Win32Window **walk = &mWindowListHead;
  357. while(*walk)
  358. {
  359. if(*walk != w)
  360. {
  361. // Advance to next item in list.
  362. walk = &(*walk)->mNextWindow;
  363. continue;
  364. }
  365. // Got a match - unlink and return.
  366. *walk = (*walk)->mNextWindow;
  367. return;
  368. }
  369. }
  370. void Win32WindowManager::_processCmdLineArgs( const S32 argc, const char **argv )
  371. {
  372. if (argc > 1)
  373. {
  374. for (int i = 1; i < argc; i++)
  375. {
  376. if ( dStrnicmp( argv[i], "-window", 7 ) == 0 )
  377. {
  378. i++;
  379. if ( i >= argc )
  380. {
  381. Con::errorf( "Command line error: -window requires an argument" );
  382. break;
  383. }
  384. S32 hwnd = dAtoi( argv[i] );
  385. if ( hwnd == 0 || hwnd == S32_MAX )
  386. {
  387. Con::errorf( "Command line error: -window requires a number, found [%s]", argv[i] );
  388. break;
  389. }
  390. mParentWindow = (HWND)hwnd;
  391. Con::printf( "HWND from command line: %d", hwnd );
  392. }
  393. if ( dStrnicmp( argv[i], "-offscreen", 10 ) == 0 )
  394. {
  395. mOffscreenRender = true;
  396. }
  397. }
  398. }
  399. }
  400. void Win32WindowManager::lowerCurtain()
  401. {
  402. if(mCurtainWindow)
  403. return;
  404. // For now just grab monitor of the first window... we may need to
  405. // beef this up later on, maybe by passing in the window that's entering
  406. // leaving full-screen to lowerCurtain.
  407. HMONITOR hMon = MonitorFromWindow(mWindowListHead->getHWND(), MONITOR_DEFAULTTOPRIMARY);
  408. // Get the monitor's extents.
  409. MONITORINFO monInfo;
  410. dMemset(&monInfo, 0, sizeof MONITORINFO);
  411. monInfo.cbSize = sizeof MONITORINFO;
  412. GetMonitorInfo(hMon, &monInfo);
  413. mCurtainWindow = CreateWindow(Win32Window::getCurtainWindowClassName(),
  414. dT(""), (WS_POPUP | WS_MAXIMIZE | WS_VISIBLE),
  415. monInfo.rcWork.left, monInfo.rcWork.top,
  416. monInfo.rcWork.right - monInfo.rcWork.left,
  417. monInfo.rcWork.bottom - monInfo.rcWork.top,
  418. NULL, NULL, NULL, NULL);
  419. if (!mOffscreenRender)
  420. SetWindowPos(mCurtainWindow, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
  421. }
  422. void Win32WindowManager::raiseCurtain()
  423. {
  424. if(!mCurtainWindow)
  425. return;
  426. DestroyWindow(mCurtainWindow);
  427. mCurtainWindow = NULL;
  428. }