win32WindowMgr.cpp 15 KB

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