2
0

win32WindowMgr.cpp 15 KB

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