win32WindowMgr.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. mOnProcessSignalSlot.setDelegate( this, &Win32WindowManager::_process );
  39. Process::notify( mOnProcessSignalSlot, PROCESS_INPUT_ORDER );
  40. // Init our list of allocated windows.
  41. mWindowListHead = NULL;
  42. // By default, we have no parent window.
  43. mParentWindow = NULL;
  44. mCurtainWindow = NULL;
  45. mOffscreenRender = false;
  46. mDisplayWindow = false;
  47. buildMonitorsList();
  48. }
  49. Win32WindowManager::~Win32WindowManager()
  50. {
  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, (size_t)(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. SetWindowLongPtr(w32w->mWindowHandle, GWLP_USERDATA, (LONG_PTR)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. w32w->setDisplayWindow(mDisplayWindow);
  217. if (!mOffscreenRender && mDisplayWindow)
  218. {
  219. ShowWindow( w32w->mWindowHandle, SW_SHOWDEFAULT );
  220. CloseSplashWindow(winState.appInstance);
  221. }
  222. // Bind the window to the specified device.
  223. if(device)
  224. {
  225. w32w->mDevice = device;
  226. w32w->mTarget = device->allocWindowTarget(w32w);
  227. AssertISV(w32w->mTarget,
  228. "Win32WindowManager::createWindow - failed to get a window target back from the device.");
  229. }
  230. else
  231. {
  232. Con::warnf("Win32WindowManager::createWindow - created a window with no device!");
  233. }
  234. // Update it if needed.
  235. UpdateWindow( w32w->mWindowHandle );
  236. return w32w;
  237. }
  238. void Win32WindowManager::setParentWindow(void* newParent)
  239. {
  240. Con::printf( "Setting parent HWND: %d", newParent );
  241. mParentWindow = (HWND)newParent;
  242. if( mWindowListHead && mWindowListHead->mWindowHandle )
  243. ::SetParent( mWindowListHead->mWindowHandle, mParentWindow);
  244. }
  245. void* Win32WindowManager::getParentWindow()
  246. {
  247. return (void*)mParentWindow;
  248. }
  249. void Win32WindowManager::_process()
  250. {
  251. MSG msg;
  252. bool _blocking = false;
  253. // CodeReview [tom, 4/30/2007] Maintaining two completely separate message
  254. // handlers that are essentially the same is silly. The first one never
  255. // seems to run as _blocking is hard coded to false above, so is this even
  256. // needed ? If it is, this should be rewritten to use the one loop that
  257. // adjusts as needed based on _blocking and Journal::IsPlaying()
  258. if (_blocking && !Journal::IsPlaying())
  259. {
  260. // In blocking mode, we process one message at a time.
  261. if (GetMessage(&msg, NULL, 0, 0))
  262. {
  263. bool noTranslate = false;
  264. Win32Window *w32w = mWindowListHead;
  265. while(w32w)
  266. {
  267. noTranslate = w32w->translateMessage(msg);
  268. if(noTranslate) break;
  269. w32w = w32w->mNextWindow;
  270. }
  271. if(! noTranslate)
  272. {
  273. TranslateMessage(&msg);
  274. DispatchMessage(&msg);
  275. }
  276. }
  277. else
  278. // This should be WM_QUIT
  279. Dispatch(ImmediateDispatch,0,msg.message,msg.wParam,msg.lParam);
  280. }
  281. else
  282. {
  283. // Process all queued up messages
  284. while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  285. {
  286. bool translated = false;
  287. // Win32Window *w32w = mWindowListHead;
  288. // while(w32w)
  289. // {
  290. // noTranslate = w32w->translateMessage(msg);
  291. // if(noTranslate) break;
  292. // w32w = w32w->mNextWindow;
  293. // }
  294. // [tom, 4/30/2007] I think this should work, but leaving the above commented
  295. // out just in case this is actually fubared with multiple windows.
  296. Win32Window* window = (Win32Window*)(GetWindowLongPtr(msg.hwnd, GWLP_USERDATA));
  297. if(window)
  298. translated = window->translateMessage(msg);
  299. if(! translated)
  300. {
  301. // Win32Window::translateMessage() will post a WM_COMMAND event for
  302. // translated accelerator events, so dispatching again will cause a
  303. // the input event to be dispatched, which is usually not what we want.
  304. TranslateMessage(&msg);
  305. DispatchMessage(&msg);
  306. }
  307. if (msg.message == WM_QUIT)
  308. {
  309. Dispatch(ImmediateDispatch,0,msg.message,msg.wParam,msg.lParam);
  310. break;
  311. }
  312. }
  313. }
  314. // Dispatch any delayed events
  315. while (DispatchNext());
  316. // Fire off idle events for every window.
  317. Win32Window *w32w = mWindowListHead;
  318. while(w32w)
  319. {
  320. w32w->idleEvent.trigger();
  321. w32w = w32w->mNextWindow;
  322. }
  323. }
  324. PlatformWindow * Win32WindowManager::getWindowById( WindowId id )
  325. {
  326. // Walk the list and find the matching id, if any.
  327. Win32Window *win = mWindowListHead;
  328. while(win)
  329. {
  330. if(win->getWindowId() == id)
  331. return win;
  332. win = win->mNextWindow;
  333. }
  334. return NULL;
  335. }
  336. PlatformWindow * Win32WindowManager::getFirstWindow()
  337. {
  338. return mWindowListHead != NULL ? mWindowListHead : NULL;
  339. }
  340. PlatformWindow* Win32WindowManager::getFocusedWindow()
  341. {
  342. Win32Window* window = mWindowListHead;
  343. while( window )
  344. {
  345. if( window->isFocused() )
  346. return window;
  347. window = window->mNextWindow;
  348. }
  349. return NULL;
  350. }
  351. void Win32WindowManager::linkWindow( Win32Window *w )
  352. {
  353. w->mNextWindow = mWindowListHead;
  354. mWindowListHead = w;
  355. }
  356. void Win32WindowManager::unlinkWindow( Win32Window *w )
  357. {
  358. Win32Window **walk = &mWindowListHead;
  359. while(*walk)
  360. {
  361. if(*walk != w)
  362. {
  363. // Advance to next item in list.
  364. walk = &(*walk)->mNextWindow;
  365. continue;
  366. }
  367. // Got a match - unlink and return.
  368. *walk = (*walk)->mNextWindow;
  369. return;
  370. }
  371. }
  372. void Win32WindowManager::_processCmdLineArgs( const S32 argc, const char **argv )
  373. {
  374. if (argc > 1)
  375. {
  376. for (S32 i = 1; i < argc; i++)
  377. {
  378. if ( dStrnicmp( argv[i], "-window", 7 ) == 0 )
  379. {
  380. i++;
  381. if ( i >= argc )
  382. {
  383. Con::errorf( "Command line error: -window requires an argument" );
  384. break;
  385. }
  386. S32 hwnd = dAtoi( argv[i] );
  387. if ( hwnd == 0 || hwnd == S32_MAX )
  388. {
  389. Con::errorf( "Command line error: -window requires a number, found [%s]", argv[i] );
  390. break;
  391. }
  392. mParentWindow = (HWND)hwnd;
  393. Con::printf( "HWND from command line: %d", hwnd );
  394. }
  395. if ( dStrnicmp( argv[i], "-offscreen", 10 ) == 0 )
  396. {
  397. mOffscreenRender = true;
  398. }
  399. }
  400. }
  401. }
  402. void Win32WindowManager::lowerCurtain()
  403. {
  404. if(mCurtainWindow)
  405. return;
  406. // For now just grab monitor of the first window... we may need to
  407. // beef this up later on, maybe by passing in the window that's entering
  408. // leaving full-screen to lowerCurtain.
  409. HMONITOR hMon = MonitorFromWindow(mWindowListHead->getHWND(), MONITOR_DEFAULTTOPRIMARY);
  410. // Get the monitor's extents.
  411. MONITORINFO monInfo;
  412. dMemset(&monInfo, 0, sizeof MONITORINFO);
  413. monInfo.cbSize = sizeof MONITORINFO;
  414. GetMonitorInfo(hMon, &monInfo);
  415. mCurtainWindow = CreateWindow(Win32Window::getCurtainWindowClassName(),
  416. dT(""), (WS_POPUP | WS_MAXIMIZE | WS_VISIBLE),
  417. monInfo.rcWork.left, monInfo.rcWork.top,
  418. monInfo.rcWork.right - monInfo.rcWork.left,
  419. monInfo.rcWork.bottom - monInfo.rcWork.top,
  420. NULL, NULL, NULL, NULL);
  421. if (!mOffscreenRender)
  422. SetWindowPos(mCurtainWindow, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
  423. }
  424. void Win32WindowManager::raiseCurtain()
  425. {
  426. if(!mCurtainWindow)
  427. return;
  428. DestroyWindow(mCurtainWindow);
  429. mCurtainWindow = NULL;
  430. }