macWindowManager.mm 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 <Cocoa/Cocoa.h>
  23. #include "windowManager/mac/macWindowManager.h"
  24. #include "windowManager/mac/macWindow.h"
  25. #include "core/util/journal/process.h"
  26. #include "console/console.h"
  27. #include "gfx/gfxDevice.h"
  28. PlatformWindowManager* CreatePlatformWindowManager()
  29. {
  30. return new MacWindowManager();
  31. }
  32. static inline RectI convertCGRectToRectI(NSRect r)
  33. {
  34. return RectI(r.origin.x, r.origin.y, r.size.width, r.size.height);
  35. }
  36. MacWindowManager::MacWindowManager() : mNotifyShutdownDelegate(this, &MacWindowManager::onShutdown), mIsShuttingDown(false)
  37. {
  38. mWindowList.clear();
  39. Process::notifyShutdown(mNotifyShutdownDelegate);
  40. }
  41. MacWindowManager::~MacWindowManager()
  42. {
  43. for(U32 i = 0; i < mWindowList.size(); i++)
  44. delete mWindowList[i];
  45. mWindowList.clear();
  46. CGReleaseDisplayFadeReservation(mFadeToken);
  47. }
  48. RectI MacWindowManager::getPrimaryDesktopArea()
  49. {
  50. // Get the area of the main desktop that isn't taken by the dock or menu bar.
  51. return convertCGRectToRectI([[NSScreen mainScreen] visibleFrame]);
  52. }
  53. void MacWindowManager::getMonitorRegions(Vector<RectI> &regions)
  54. {
  55. // Populate a vector with all monitors and their extents in window space.
  56. NSArray *screenList = [NSScreen screens];
  57. for(U32 i = 0; i < [screenList count]; i++)
  58. {
  59. NSRect screenBounds = [[screenList objectAtIndex: i] frame];
  60. regions.push_back(convertCGRectToRectI(screenBounds));
  61. }
  62. }
  63. S32 MacWindowManager::getDesktopBitDepth()
  64. {
  65. // get the current desktop bit depth
  66. // TODO: return -1 if an error occurred
  67. return NSBitsPerPixelFromDepth([[NSScreen mainScreen] depth]);
  68. }
  69. Point2I MacWindowManager::getDesktopResolution()
  70. {
  71. // get the current desktop width/height
  72. // TODO: return Point2I(-1,-1) if an error occurred
  73. NSRect desktopBounds = [[NSScreen mainScreen] frame];
  74. return Point2I((U32)desktopBounds.size.width, (U32)desktopBounds.size.height);
  75. }
  76. S32 MacWindowManager::getWindowCount()
  77. {
  78. // Get the number of PlatformWindow's in this manager
  79. return mWindowList.size();
  80. }
  81. void MacWindowManager::getWindows(VectorPtr<PlatformWindow*> &windows)
  82. {
  83. // Populate a list with references to all the windows created from this manager.
  84. windows.merge(mWindowList);
  85. }
  86. PlatformWindow * MacWindowManager::getFirstWindow()
  87. {
  88. if (mWindowList.size() > 0)
  89. return mWindowList[0];
  90. return NULL;
  91. }
  92. PlatformWindow* MacWindowManager::getFocusedWindow()
  93. {
  94. for (U32 i = 0; i < mWindowList.size(); i++)
  95. {
  96. if( mWindowList[i]->isFocused() )
  97. return mWindowList[i];
  98. }
  99. return NULL;
  100. }
  101. PlatformWindow* MacWindowManager::getWindowById(WindowId zid)
  102. {
  103. // Find the window by its arbirary WindowId.
  104. for(U32 i = 0; i < mWindowList.size(); i++)
  105. {
  106. PlatformWindow* w = mWindowList[i];
  107. if( w->getWindowId() == zid)
  108. return w;
  109. }
  110. return NULL;
  111. }
  112. void MacWindowManager::lowerCurtain()
  113. {
  114. // fade all displays.
  115. CGError err;
  116. err = CGAcquireDisplayFadeReservation(kCGMaxDisplayReservationInterval, &mFadeToken);
  117. AssertWarn(!err, "MacWindowManager::lowerCurtain() could not get a token");
  118. if(err) return;
  119. err = CGDisplayFade(mFadeToken, 0.3, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0, 0, 0, true);
  120. AssertWarn(!err, "MacWindowManager::lowerCurtain() failed the fade");
  121. if(err) return;
  122. // we do not release the token, because that will un-fade the screen!
  123. // the token will last for 15 sec, and then the screen will un-fade regardless.
  124. //CGReleaseDisplayFadeReservation(mFadeToken);
  125. }
  126. void MacWindowManager::raiseCurtain()
  127. {
  128. // release the fade on all displays
  129. CGError err;
  130. err = CGDisplayFade(mFadeToken, 0.3, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0, 0, 0, false);
  131. AssertWarn(!err, "MacWindowManager::raiseCurtain() failed the fade");
  132. err = CGReleaseDisplayFadeReservation(mFadeToken);
  133. AssertWarn(!err, "MacWindowManager::raiseCurtain() failed releasing the token");
  134. }
  135. void MacWindowManager::_processCmdLineArgs(const S32 argc, const char **argv)
  136. {
  137. // TODO: accept command line args if necessary.
  138. }
  139. PlatformWindow *MacWindowManager::createWindow(GFXDevice *device, const GFXVideoMode &mode)
  140. {
  141. MacWindow* window = new MacWindow(getNextId(), getEngineProductString(), mode.resolution);
  142. _addWindow(window);
  143. // Set the video mode on the window
  144. window->setVideoMode(mode);
  145. // Make sure our window is shown and drawn to.
  146. window->show();
  147. // Bind the window to the specified device.
  148. if(device)
  149. {
  150. window->mDevice = device;
  151. window->mTarget = device->allocWindowTarget(window);
  152. AssertISV(window->mTarget,
  153. "MacWindowManager::createWindow - failed to get a window target back from the device.");
  154. }
  155. else
  156. {
  157. Con::warnf("MacWindowManager::createWindow - created a window with no device!");
  158. }
  159. return window;
  160. }
  161. void MacWindowManager::_addWindow(MacWindow* window)
  162. {
  163. #ifdef TORQUE_DEBUG
  164. // Make sure we aren't adding the window twice
  165. for(U32 i = 0; i < mWindowList.size(); i++)
  166. AssertFatal(window != mWindowList[i], "MacWindowManager::_addWindow - Should not add a window more than once");
  167. #endif
  168. if (mWindowList.size() > 0)
  169. window->mNextWindow = mWindowList.last();
  170. else
  171. window->mNextWindow = NULL;
  172. mWindowList.push_back(window);
  173. window->mOwningWindowManager = this;
  174. window->appEvent.notify(this, &MacWindowManager::_onAppSignal);
  175. }
  176. void MacWindowManager::_removeWindow(MacWindow* window)
  177. {
  178. for(WindowList::iterator i = mWindowList.begin(); i != mWindowList.end(); i++)
  179. {
  180. if(*i == window)
  181. {
  182. mWindowList.erase(i);
  183. return;
  184. }
  185. }
  186. AssertFatal(false, avar("MacWindowManager::_removeWindow - Failed to remove window %x, perhaps it was already removed?", window));
  187. }
  188. void MacWindowManager::_onAppSignal(WindowId wnd, S32 event)
  189. {
  190. if(event != WindowHidden)
  191. return;
  192. for(U32 i = 0; i < mWindowList.size(); i++)
  193. {
  194. if(mWindowList[i]->getWindowId() == wnd)
  195. continue;
  196. mWindowList[i]->signalGainFocus();
  197. }
  198. }
  199. bool MacWindowManager::onShutdown()
  200. {
  201. mIsShuttingDown = true;
  202. return true;
  203. }
  204. bool MacWindowManager::canWindowGainFocus(MacWindow* window)
  205. {
  206. return !mIsShuttingDown;
  207. }