| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | #include <osg/Referenced> |
|---|
| 11 | #include <osg/DeleteHandler> |
|---|
| 12 | #include "DarwinUtils.h" |
|---|
| 13 | #include <Cocoa/Cocoa.h> |
|---|
| 14 | |
|---|
| 15 | namespace osgDarwin { |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | static inline CGRect toCGRect(NSRect nsRect) |
|---|
| 19 | { |
|---|
| 20 | CGRect cgRect; |
|---|
| 21 | |
|---|
| 22 | cgRect.origin.x = nsRect.origin.x; |
|---|
| 23 | cgRect.origin.y = nsRect.origin.y; |
|---|
| 24 | cgRect.size.width = nsRect.size.width; |
|---|
| 25 | cgRect.size.height = nsRect.size.height; |
|---|
| 26 | |
|---|
| 27 | return cgRect; |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | MenubarController::MenubarController() |
|---|
| 32 | : osg::Referenced(), |
|---|
| 33 | _list(), |
|---|
| 34 | _menubarShown(false), |
|---|
| 35 | _mutex() |
|---|
| 36 | { |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | NSRect rect = [[[NSScreen screens] objectAtIndex: 0] visibleFrame]; |
|---|
| 40 | _availRect = toCGRect(rect); |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | _mainScreenBounds = CGDisplayBounds( CGMainDisplayID() ); |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | _availRect.origin.y = _mainScreenBounds.size.height - _availRect.size.height - _availRect.origin.y; |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | SetSystemUIMode(kUIModeAllHidden, kUIOptionAutoShowMenuBar); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | MenubarController* MenubarController::instance() |
|---|
| 58 | { |
|---|
| 59 | static osg::ref_ptr<MenubarController> s_menubar_controller = new MenubarController(); |
|---|
| 60 | return s_menubar_controller.get(); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | void MenubarController::attachWindow(WindowAdapter* win) |
|---|
| 65 | { |
|---|
| 66 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); |
|---|
| 67 | _list.push_back(win); |
|---|
| 68 | update(); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | void MenubarController::detachWindow(osgViewer::GraphicsWindow* win) |
|---|
| 73 | { |
|---|
| 74 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); |
|---|
| 75 | for(WindowList::iterator i = _list.begin(); i != _list.end(); ) { |
|---|
| 76 | if ((*i)->getWindow() == win) |
|---|
| 77 | i = _list.erase(i); |
|---|
| 78 | else |
|---|
| 79 | ++i; |
|---|
| 80 | } |
|---|
| 81 | update(); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | void MenubarController::update() |
|---|
| 87 | { |
|---|
| 88 | OSErr error(noErr); |
|---|
| 89 | unsigned int windowsCoveringMenubarArea = 0; |
|---|
| 90 | unsigned int windowsIntersectingMainScreen = 0; |
|---|
| 91 | for(WindowList::iterator i = _list.begin(); i != _list.end(); ) { |
|---|
| 92 | WindowAdapter* wi = (*i).get(); |
|---|
| 93 | if (wi->valid()) { |
|---|
| 94 | CGRect windowBounds; |
|---|
| 95 | wi->getWindowBounds(windowBounds); |
|---|
| 96 | |
|---|
| 97 | if (CGRectIntersectsRect(_mainScreenBounds, windowBounds)) |
|---|
| 98 | { |
|---|
| 99 | ++windowsIntersectingMainScreen; |
|---|
| 100 | osg::notify(osg::WARN) << "testint rect " << windowBounds.origin.x << "/" << windowBounds.origin.y << " " << windowBounds.size.width << "x" << windowBounds.size.height << std::endl; |
|---|
| 101 | osg::notify(osg::WARN) << "agains " << _availRect.origin.x << "/" << _availRect.origin.y << " " << _availRect.size.width << "x" << _availRect.size.height << std::endl; |
|---|
| 102 | |
|---|
| 103 | if (((_availRect.origin.y > _mainScreenBounds.origin.y) && (_availRect.origin.y > windowBounds.origin.y)) || |
|---|
| 104 | ((_availRect.origin.x > _mainScreenBounds.origin.x) && (_availRect.origin.x > windowBounds.origin.x)) || |
|---|
| 105 | ((_availRect.size.width < _mainScreenBounds.size.width) && (_availRect.origin.x + _availRect.size.width < windowBounds.origin.x + windowBounds.size.width)) || |
|---|
| 106 | ((_availRect.size.height < _mainScreenBounds.size.height) && (_availRect.origin.y + _availRect.size.height < windowBounds.origin.y + windowBounds.size.height) )) |
|---|
| 107 | { |
|---|
| 108 | ++windowsCoveringMenubarArea; |
|---|
| 109 | } |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | ++i; |
|---|
| 113 | } |
|---|
| 114 | else |
|---|
| 115 | i= _list.erase(i); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | if (windowsCoveringMenubarArea && _menubarShown) |
|---|
| 121 | error = SetSystemUIMode(kUIModeAllHidden, kUIOptionAutoShowMenuBar); |
|---|
| 122 | |
|---|
| 123 | if (!windowsCoveringMenubarArea && !_menubarShown) |
|---|
| 124 | error = SetSystemUIMode(kUIModeNormal, 0); |
|---|
| 125 | _menubarShown = !windowsCoveringMenubarArea; |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | static double getDictDouble (CFDictionaryRef refDict, CFStringRef key) |
|---|
| 134 | { |
|---|
| 135 | double value; |
|---|
| 136 | CFNumberRef number_value = (CFNumberRef) CFDictionaryGetValue(refDict, key); |
|---|
| 137 | if (!number_value) |
|---|
| 138 | return -1; |
|---|
| 139 | if (!CFNumberGetValue(number_value, kCFNumberDoubleType, &value)) |
|---|
| 140 | return -1; |
|---|
| 141 | return value; |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | static long getDictLong(CFDictionaryRef refDict, CFStringRef key) |
|---|
| 146 | { |
|---|
| 147 | long value = 0; |
|---|
| 148 | CFNumberRef number_value = (CFNumberRef)CFDictionaryGetValue(refDict, key); |
|---|
| 149 | if (!number_value) |
|---|
| 150 | return -1; |
|---|
| 151 | if (!CFNumberGetValue(number_value, kCFNumberLongType, &value)) |
|---|
| 152 | return -1; |
|---|
| 153 | return value; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | DarwinWindowingSystemInterface::DarwinWindowingSystemInterface() : |
|---|
| 160 | _displayCount(0), |
|---|
| 161 | _displayIds(NULL) |
|---|
| 162 | { |
|---|
| 163 | ProcessSerialNumber sn = { 0, kCurrentProcess }; |
|---|
| 164 | TransformProcessType(&sn,kProcessTransformToForegroundApplication); |
|---|
| 165 | SetFrontProcess(&sn); |
|---|
| 166 | |
|---|
| 167 | if( CGGetActiveDisplayList( 0, NULL, &_displayCount ) != CGDisplayNoErr ) |
|---|
| 168 | osg::notify(osg::WARN) << "DarwinWindowingSystemInterface: could not get # of screens" << std::endl; |
|---|
| 169 | |
|---|
| 170 | _displayIds = new CGDirectDisplayID[_displayCount]; |
|---|
| 171 | if( CGGetActiveDisplayList( _displayCount, _displayIds, &_displayCount ) != CGDisplayNoErr ) |
|---|
| 172 | osg::notify(osg::WARN) << "DarwinWindowingSystemInterface: CGGetActiveDisplayList failed" << std::endl; |
|---|
| 173 | |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | DarwinWindowingSystemInterface::~DarwinWindowingSystemInterface() |
|---|
| 178 | { |
|---|
| 179 | if (osg::Referenced::getDeleteHandler()) |
|---|
| 180 | { |
|---|
| 181 | osg::Referenced::getDeleteHandler()->setNumFramesToRetainObjects(0); |
|---|
| 182 | osg::Referenced::getDeleteHandler()->flushAll(); |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | if (_displayIds) delete[] _displayIds; |
|---|
| 186 | _displayIds = NULL; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | CGDirectDisplayID DarwinWindowingSystemInterface::getDisplayID(const osg::GraphicsContext::ScreenIdentifier& si) { |
|---|
| 191 | if (si.screenNum < _displayCount) |
|---|
| 192 | return _displayIds[si.screenNum]; |
|---|
| 193 | else { |
|---|
| 194 | osg::notify(osg::WARN) << "GraphicsWindowCarbon :: invalid screen # " << si.screenNum << ", returning main-screen instead" << std::endl; |
|---|
| 195 | return _displayIds[0]; |
|---|
| 196 | } |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | unsigned int DarwinWindowingSystemInterface::getNumScreens(const osg::GraphicsContext::ScreenIdentifier& si) |
|---|
| 201 | { |
|---|
| 202 | return _displayCount; |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | void DarwinWindowingSystemInterface::getScreenSettings(const osg::GraphicsContext::ScreenIdentifier& si, osg::GraphicsContext::ScreenSettings & resolution) |
|---|
| 206 | { |
|---|
| 207 | CGDirectDisplayID id = getDisplayID(si); |
|---|
| 208 | resolution.width = CGDisplayPixelsWide(id); |
|---|
| 209 | resolution.height = CGDisplayPixelsHigh(id); |
|---|
| 210 | resolution.colorDepth = CGDisplayBitsPerPixel(id); |
|---|
| 211 | resolution.refreshRate = getDictDouble (CGDisplayCurrentMode(id), kCGDisplayRefreshRate); |
|---|
| 212 | if (resolution.refreshRate<0) resolution.refreshRate = 0; |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | void DarwinWindowingSystemInterface::enumerateScreenSettings(const osg::GraphicsContext::ScreenIdentifier& screenIdentifier, osg::GraphicsContext::ScreenSettingsList & resolutionList) { |
|---|
| 217 | |
|---|
| 218 | resolutionList.clear(); |
|---|
| 219 | |
|---|
| 220 | CGDirectDisplayID displayID = getDisplayID(screenIdentifier); |
|---|
| 221 | CFArrayRef availableModes = CGDisplayAvailableModes(displayID); |
|---|
| 222 | unsigned int numberOfAvailableModes = CFArrayGetCount(availableModes); |
|---|
| 223 | for (unsigned int i=0; i<numberOfAvailableModes; ++i) { |
|---|
| 224 | |
|---|
| 225 | CFDictionaryRef mode = (CFDictionaryRef)CFArrayGetValueAtIndex(availableModes, i); |
|---|
| 226 | osg::GraphicsContext::ScreenSettings tmpSR; |
|---|
| 227 | |
|---|
| 228 | long width = getDictLong(mode, kCGDisplayWidth); |
|---|
| 229 | tmpSR.width = width<=0 ? 0 : width; |
|---|
| 230 | long height = getDictLong(mode, kCGDisplayHeight); |
|---|
| 231 | tmpSR.height = height<=0 ? 0 : height; |
|---|
| 232 | long rate = getDictLong(mode, kCGDisplayRefreshRate); |
|---|
| 233 | tmpSR.refreshRate = rate<=0 ? 0 : rate; |
|---|
| 234 | long depth = getDictLong(mode, kCGDisplayBitsPerPixel); |
|---|
| 235 | tmpSR.colorDepth = depth<=0 ? 0 : depth; |
|---|
| 236 | |
|---|
| 237 | resolutionList.push_back(tmpSR); |
|---|
| 238 | } |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | void DarwinWindowingSystemInterface::getScreenTopLeft(const osg::GraphicsContext::ScreenIdentifier& si, int& x, int& y) { |
|---|
| 243 | CGRect bounds = CGDisplayBounds( getDisplayID(si) ); |
|---|
| 244 | x = static_cast<int>(bounds.origin.x); |
|---|
| 245 | y = static_cast<int>(bounds.origin.y); |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | bool DarwinWindowingSystemInterface::setScreenResolution(const osg::GraphicsContext::ScreenIdentifier& screenIdentifier, unsigned int width, unsigned int height) |
|---|
| 254 | { |
|---|
| 255 | CGDirectDisplayID displayID = getDisplayID(screenIdentifier); |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | CGRefreshRate refresh = getDictDouble (CGDisplayCurrentMode(displayID), kCGDisplayRefreshRate); |
|---|
| 259 | CFDictionaryRef display_mode_values = |
|---|
| 260 | CGDisplayBestModeForParametersAndRefreshRate( |
|---|
| 261 | displayID, |
|---|
| 262 | CGDisplayBitsPerPixel(displayID), |
|---|
| 263 | width, height, |
|---|
| 264 | refresh, |
|---|
| 265 | NULL); |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | CGDisplaySwitchToMode(displayID, display_mode_values); |
|---|
| 269 | return true; |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | |
|---|
| 273 | bool DarwinWindowingSystemInterface::setScreenRefreshRate(const osg::GraphicsContext::ScreenIdentifier& screenIdentifier, double refreshRate) { |
|---|
| 274 | |
|---|
| 275 | boolean_t success(false); |
|---|
| 276 | unsigned width, height; |
|---|
| 277 | getScreenResolution(screenIdentifier, width, height); |
|---|
| 278 | |
|---|
| 279 | CGDirectDisplayID displayID = getDisplayID(screenIdentifier); |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | CFDictionaryRef display_mode_values = |
|---|
| 283 | CGDisplayBestModeForParametersAndRefreshRate( |
|---|
| 284 | displayID, |
|---|
| 285 | CGDisplayBitsPerPixel(displayID), |
|---|
| 286 | width, height, |
|---|
| 287 | refreshRate, |
|---|
| 288 | &success); |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | if (success) |
|---|
| 292 | CGDisplaySwitchToMode(displayID, display_mode_values); |
|---|
| 293 | |
|---|
| 294 | return (success != 0); |
|---|
| 295 | } |
|---|
| 296 | |
|---|
| 297 | |
|---|
| 298 | |
|---|
| 299 | |
|---|
| 300 | |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | |
|---|
| 304 | } |
|---|