| 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 | @interface MenubarToggler : NSObject { |
|---|
| 16 | |
|---|
| 17 | } |
|---|
| 18 | |
|---|
| 19 | -(void) show: (id) data; |
|---|
| 20 | -(void) hide: (id) data; |
|---|
| 21 | |
|---|
| 22 | @end |
|---|
| 23 | |
|---|
| 24 | @implementation MenubarToggler |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | -(void) hide:(id) data |
|---|
| 29 | { |
|---|
| 30 | OSErr error = SetSystemUIMode(kUIModeAllHidden, kUIOptionAutoShowMenuBar); |
|---|
| 31 | if (error) { |
|---|
| 32 | osg::notify(osg::DEBUG_INFO) << "MenubarToggler::hide failed with " << error << std::endl; |
|---|
| 33 | } |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | -(void) show:(id) data |
|---|
| 38 | { |
|---|
| 39 | OSErr error = SetSystemUIMode(kUIModeNormal, 0); |
|---|
| 40 | if (error) { |
|---|
| 41 | osg::notify(osg::DEBUG_INFO) << "MenubarToggler::show failed with " << error << std::endl; |
|---|
| 42 | } |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | @end |
|---|
| 47 | |
|---|
| 48 | namespace osgDarwin { |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | static inline CGRect toCGRect(NSRect nsRect) |
|---|
| 52 | { |
|---|
| 53 | CGRect cgRect; |
|---|
| 54 | |
|---|
| 55 | cgRect.origin.x = nsRect.origin.x; |
|---|
| 56 | cgRect.origin.y = nsRect.origin.y; |
|---|
| 57 | cgRect.size.width = nsRect.size.width; |
|---|
| 58 | cgRect.size.height = nsRect.size.height; |
|---|
| 59 | |
|---|
| 60 | return cgRect; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | MenubarController::MenubarController() |
|---|
| 65 | : osg::Referenced(), |
|---|
| 66 | _list(), |
|---|
| 67 | _menubarShown(false), |
|---|
| 68 | _mutex() |
|---|
| 69 | { |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | NSRect rect = [[[NSScreen screens] objectAtIndex: 0] visibleFrame]; |
|---|
| 73 | _availRect = toCGRect(rect); |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | _mainScreenBounds = CGDisplayBounds( CGMainDisplayID() ); |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | _availRect.origin.y = _mainScreenBounds.size.height - _availRect.size.height - _availRect.origin.y; |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | SetSystemUIMode(kUIModeAllHidden, kUIOptionAutoShowMenuBar); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | MenubarController* MenubarController::instance() |
|---|
| 91 | { |
|---|
| 92 | static osg::ref_ptr<MenubarController> s_menubar_controller = new MenubarController(); |
|---|
| 93 | return s_menubar_controller.get(); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | void MenubarController::attachWindow(WindowAdapter* win) |
|---|
| 98 | { |
|---|
| 99 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); |
|---|
| 100 | _list.push_back(win); |
|---|
| 101 | update(); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | void MenubarController::detachWindow(osgViewer::GraphicsWindow* win) |
|---|
| 106 | { |
|---|
| 107 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); |
|---|
| 108 | for(WindowList::iterator i = _list.begin(); i != _list.end(); ) { |
|---|
| 109 | if ((*i)->getWindow() == win) |
|---|
| 110 | i = _list.erase(i); |
|---|
| 111 | else |
|---|
| 112 | ++i; |
|---|
| 113 | } |
|---|
| 114 | update(); |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | void MenubarController::update() |
|---|
| 121 | { |
|---|
| 122 | unsigned int windowsCoveringMenubarArea = 0; |
|---|
| 123 | unsigned int windowsIntersectingMainScreen = 0; |
|---|
| 124 | for(WindowList::iterator i = _list.begin(); i != _list.end(); ) { |
|---|
| 125 | WindowAdapter* wi = (*i).get(); |
|---|
| 126 | if (wi->valid()) { |
|---|
| 127 | CGRect windowBounds; |
|---|
| 128 | wi->getWindowBounds(windowBounds); |
|---|
| 129 | |
|---|
| 130 | if (CGRectIntersectsRect(_mainScreenBounds, windowBounds)) |
|---|
| 131 | { |
|---|
| 132 | ++windowsIntersectingMainScreen; |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | if (((_availRect.origin.y > _mainScreenBounds.origin.y) && (_availRect.origin.y > windowBounds.origin.y)) || |
|---|
| 137 | ((_availRect.origin.x > _mainScreenBounds.origin.x) && (_availRect.origin.x > windowBounds.origin.x)) || |
|---|
| 138 | ((_availRect.size.width < _mainScreenBounds.size.width) && (_availRect.origin.x + _availRect.size.width < windowBounds.origin.x + windowBounds.size.width)) || |
|---|
| 139 | ((_availRect.size.height < _mainScreenBounds.size.height) && (_availRect.origin.y + _availRect.size.height < windowBounds.origin.y + windowBounds.size.height) )) |
|---|
| 140 | { |
|---|
| 141 | ++windowsCoveringMenubarArea; |
|---|
| 142 | } |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | ++i; |
|---|
| 146 | } |
|---|
| 147 | else |
|---|
| 148 | i = _list.erase(i); |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | #ifdef USE_DARWIN_COCOA_IMPLEMENTATION |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; |
|---|
| 159 | if (windowsCoveringMenubarArea && _menubarShown) |
|---|
| 160 | { |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | MenubarToggler* toggler = [[MenubarToggler alloc] init]; |
|---|
| 164 | [toggler performSelectorOnMainThread: @selector(hide:) withObject:NULL waitUntilDone: YES]; |
|---|
| 165 | [toggler autorelease]; |
|---|
| 166 | } |
|---|
| 167 | if (!windowsCoveringMenubarArea && !_menubarShown) |
|---|
| 168 | { |
|---|
| 169 | |
|---|
| 170 | MenubarToggler* toggler = [[MenubarToggler alloc] init]; |
|---|
| 171 | [toggler performSelectorOnMainThread: @selector(show:) withObject:NULL waitUntilDone: YES]; |
|---|
| 172 | [toggler autorelease]; |
|---|
| 173 | } |
|---|
| 174 | [pool release]; |
|---|
| 175 | |
|---|
| 176 | #else |
|---|
| 177 | |
|---|
| 178 | OSErr error; |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | if (windowsCoveringMenubarArea && _menubarShown) |
|---|
| 182 | { |
|---|
| 183 | error = SetSystemUIMode(kUIModeAllHidden, kUIOptionAutoShowMenuBar); |
|---|
| 184 | } |
|---|
| 185 | if (!windowsCoveringMenubarArea && !_menubarShown) |
|---|
| 186 | { |
|---|
| 187 | error = SetSystemUIMode(kUIModeNormal, 0); |
|---|
| 188 | } |
|---|
| 189 | #endif |
|---|
| 190 | |
|---|
| 191 | _menubarShown = !windowsCoveringMenubarArea; |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | static double getDictDouble (CFDictionaryRef refDict, CFStringRef key) |
|---|
| 198 | { |
|---|
| 199 | double value; |
|---|
| 200 | CFNumberRef number_value = (CFNumberRef) CFDictionaryGetValue(refDict, key); |
|---|
| 201 | if (!number_value) |
|---|
| 202 | return -1; |
|---|
| 203 | if (!CFNumberGetValue(number_value, kCFNumberDoubleType, &value)) |
|---|
| 204 | return -1; |
|---|
| 205 | return value; |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | static long getDictLong(CFDictionaryRef refDict, CFStringRef key) |
|---|
| 210 | { |
|---|
| 211 | long value = 0; |
|---|
| 212 | CFNumberRef number_value = (CFNumberRef)CFDictionaryGetValue(refDict, key); |
|---|
| 213 | if (!number_value) |
|---|
| 214 | return -1; |
|---|
| 215 | if (!CFNumberGetValue(number_value, kCFNumberLongType, &value)) |
|---|
| 216 | return -1; |
|---|
| 217 | return value; |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | DarwinWindowingSystemInterface::DarwinWindowingSystemInterface() : |
|---|
| 224 | _displayCount(0), |
|---|
| 225 | _displayIds(NULL) |
|---|
| 226 | { |
|---|
| 227 | ProcessSerialNumber sn = { 0, kCurrentProcess }; |
|---|
| 228 | TransformProcessType(&sn,kProcessTransformToForegroundApplication); |
|---|
| 229 | SetFrontProcess(&sn); |
|---|
| 230 | |
|---|
| 231 | if( CGGetActiveDisplayList( 0, NULL, &_displayCount ) != CGDisplayNoErr ) |
|---|
| 232 | osg::notify(osg::WARN) << "DarwinWindowingSystemInterface: could not get # of screens" << std::endl; |
|---|
| 233 | |
|---|
| 234 | _displayIds = new CGDirectDisplayID[_displayCount]; |
|---|
| 235 | if( CGGetActiveDisplayList( _displayCount, _displayIds, &_displayCount ) != CGDisplayNoErr ) |
|---|
| 236 | osg::notify(osg::WARN) << "DarwinWindowingSystemInterface: CGGetActiveDisplayList failed" << std::endl; |
|---|
| 237 | |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | DarwinWindowingSystemInterface::~DarwinWindowingSystemInterface() |
|---|
| 242 | { |
|---|
| 243 | if (osg::Referenced::getDeleteHandler()) |
|---|
| 244 | { |
|---|
| 245 | osg::Referenced::getDeleteHandler()->setNumFramesToRetainObjects(0); |
|---|
| 246 | osg::Referenced::getDeleteHandler()->flushAll(); |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | if (_displayIds) delete[] _displayIds; |
|---|
| 250 | _displayIds = NULL; |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | CGDirectDisplayID DarwinWindowingSystemInterface::getDisplayID(const osg::GraphicsContext::ScreenIdentifier& si) { |
|---|
| 255 | if (si.screenNum < static_cast<int>(_displayCount)) |
|---|
| 256 | return _displayIds[si.screenNum]; |
|---|
| 257 | else { |
|---|
| 258 | osg::notify(osg::WARN) << "GraphicsWindowCarbon :: invalid screen # " << si.screenNum << ", returning main-screen instead" << std::endl; |
|---|
| 259 | return _displayIds[0]; |
|---|
| 260 | } |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | unsigned int DarwinWindowingSystemInterface::getNumScreens(const osg::GraphicsContext::ScreenIdentifier& si) |
|---|
| 265 | { |
|---|
| 266 | return _displayCount; |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | void DarwinWindowingSystemInterface::getScreenSettings(const osg::GraphicsContext::ScreenIdentifier& si, osg::GraphicsContext::ScreenSettings & resolution) |
|---|
| 270 | { |
|---|
| 271 | CGDirectDisplayID id = getDisplayID(si); |
|---|
| 272 | resolution.width = CGDisplayPixelsWide(id); |
|---|
| 273 | resolution.height = CGDisplayPixelsHigh(id); |
|---|
| 274 | resolution.colorDepth = CGDisplayBitsPerPixel(id); |
|---|
| 275 | resolution.refreshRate = getDictDouble (CGDisplayCurrentMode(id), kCGDisplayRefreshRate); |
|---|
| 276 | if (resolution.refreshRate<0) resolution.refreshRate = 0; |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | void DarwinWindowingSystemInterface::enumerateScreenSettings(const osg::GraphicsContext::ScreenIdentifier& screenIdentifier, osg::GraphicsContext::ScreenSettingsList & resolutionList) { |
|---|
| 281 | |
|---|
| 282 | resolutionList.clear(); |
|---|
| 283 | |
|---|
| 284 | CGDirectDisplayID displayid = getDisplayID(screenIdentifier); |
|---|
| 285 | CFArrayRef availableModes = CGDisplayAvailableModes(displayid); |
|---|
| 286 | unsigned int numberOfAvailableModes = CFArrayGetCount(availableModes); |
|---|
| 287 | for (unsigned int i=0; i<numberOfAvailableModes; ++i) { |
|---|
| 288 | |
|---|
| 289 | CFDictionaryRef mode = (CFDictionaryRef)CFArrayGetValueAtIndex(availableModes, i); |
|---|
| 290 | osg::GraphicsContext::ScreenSettings tmpSR; |
|---|
| 291 | |
|---|
| 292 | long width = getDictLong(mode, kCGDisplayWidth); |
|---|
| 293 | tmpSR.width = width<=0 ? 0 : width; |
|---|
| 294 | long height = getDictLong(mode, kCGDisplayHeight); |
|---|
| 295 | tmpSR.height = height<=0 ? 0 : height; |
|---|
| 296 | long rate = getDictLong(mode, kCGDisplayRefreshRate); |
|---|
| 297 | tmpSR.refreshRate = rate<=0 ? 0 : rate; |
|---|
| 298 | long depth = getDictLong(mode, kCGDisplayBitsPerPixel); |
|---|
| 299 | tmpSR.colorDepth = depth<=0 ? 0 : depth; |
|---|
| 300 | |
|---|
| 301 | resolutionList.push_back(tmpSR); |
|---|
| 302 | } |
|---|
| 303 | } |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | void DarwinWindowingSystemInterface::getScreenTopLeft(const osg::GraphicsContext::ScreenIdentifier& si, int& x, int& y) { |
|---|
| 307 | CGRect bounds = CGDisplayBounds( getDisplayID(si) ); |
|---|
| 308 | x = static_cast<int>(bounds.origin.x); |
|---|
| 309 | y = static_cast<int>(bounds.origin.y); |
|---|
| 310 | |
|---|
| 311 | |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | bool DarwinWindowingSystemInterface::setScreenSettings(const osg::GraphicsContext::ScreenIdentifier &si, const osg::GraphicsContext::ScreenSettings & settings) |
|---|
| 316 | { |
|---|
| 317 | bool result = setScreenResolutionImpl(si, settings.width, settings.height); |
|---|
| 318 | if (result) |
|---|
| 319 | setScreenRefreshRateImpl(si, settings.refreshRate); |
|---|
| 320 | |
|---|
| 321 | return result; |
|---|
| 322 | } |
|---|
| 323 | |
|---|
| 324 | |
|---|
| 325 | |
|---|
| 326 | |
|---|
| 327 | bool DarwinWindowingSystemInterface::setScreenResolutionImpl(const osg::GraphicsContext::ScreenIdentifier& screenIdentifier, unsigned int width, unsigned int height) |
|---|
| 328 | { |
|---|
| 329 | CGDirectDisplayID displayid = getDisplayID(screenIdentifier); |
|---|
| 330 | |
|---|
| 331 | |
|---|
| 332 | CGRefreshRate refresh = getDictDouble (CGDisplayCurrentMode(displayid), kCGDisplayRefreshRate); |
|---|
| 333 | CFDictionaryRef display_mode_values = |
|---|
| 334 | CGDisplayBestModeForParametersAndRefreshRate( |
|---|
| 335 | displayid, |
|---|
| 336 | CGDisplayBitsPerPixel(displayid), |
|---|
| 337 | width, height, |
|---|
| 338 | refresh, |
|---|
| 339 | NULL); |
|---|
| 340 | |
|---|
| 341 | |
|---|
| 342 | CGDisplaySwitchToMode(displayid, display_mode_values); |
|---|
| 343 | return true; |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | |
|---|
| 347 | bool DarwinWindowingSystemInterface::setScreenRefreshRateImpl(const osg::GraphicsContext::ScreenIdentifier& screenIdentifier, double refreshRate) { |
|---|
| 348 | |
|---|
| 349 | boolean_t success(false); |
|---|
| 350 | unsigned width, height; |
|---|
| 351 | getScreenResolution(screenIdentifier, width, height); |
|---|
| 352 | |
|---|
| 353 | CGDirectDisplayID displayid = getDisplayID(screenIdentifier); |
|---|
| 354 | |
|---|
| 355 | |
|---|
| 356 | CFDictionaryRef display_mode_values = |
|---|
| 357 | CGDisplayBestModeForParametersAndRefreshRate( |
|---|
| 358 | displayid, |
|---|
| 359 | CGDisplayBitsPerPixel(displayid), |
|---|
| 360 | width, height, |
|---|
| 361 | refreshRate, |
|---|
| 362 | &success); |
|---|
| 363 | |
|---|
| 364 | |
|---|
| 365 | if (success) |
|---|
| 366 | CGDisplaySwitchToMode(displayid, display_mode_values); |
|---|
| 367 | |
|---|
| 368 | return (success != 0); |
|---|
| 369 | } |
|---|
| 370 | |
|---|
| 371 | |
|---|
| 372 | unsigned int DarwinWindowingSystemInterface::getScreenContaining(int x, int y, int w, int h) |
|---|
| 373 | { |
|---|
| 374 | CGRect rect = CGRectMake(x,y,w,h); |
|---|
| 375 | for(unsigned int i = 0; i < _displayCount; ++i) { |
|---|
| 376 | CGRect bounds = CGDisplayBounds( getDisplayID(i) ); |
|---|
| 377 | if (CGRectIntersectsRect(bounds, rect)) { |
|---|
| 378 | return i; |
|---|
| 379 | } |
|---|
| 380 | } |
|---|
| 381 | |
|---|
| 382 | return 0; |
|---|
| 383 | } |
|---|
| 384 | |
|---|
| 385 | |
|---|
| 386 | |
|---|
| 387 | |
|---|
| 388 | |
|---|
| 389 | |
|---|
| 390 | } |
|---|