Blender  V2.59
GHOST_DisplayManagerCarbon.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: GHOST_DisplayManagerCarbon.cpp 35152 2011-02-25 11:28:33Z jesterking $
00003  * ***** BEGIN GPL LICENSE BLOCK *****
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software Foundation,
00017  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018  *
00019  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00020  * All rights reserved.
00021  *
00022  * The Original Code is: all of this file.
00023  *
00024  * Contributor(s): none yet.
00025  *
00026  * ***** END GPL LICENSE BLOCK *****
00027  */
00028 
00042 #include "GHOST_DisplayManagerCarbon.h"
00043 #include "GHOST_Debug.h"
00044 
00045 // We do not support multiple monitors at the moment
00046 
00047 
00048 GHOST_DisplayManagerCarbon::GHOST_DisplayManagerCarbon(void)
00049 {
00050         if (::CGGetActiveDisplayList(0, NULL, &m_numDisplays) != CGDisplayNoErr)
00051         {
00052                 m_numDisplays = 0;
00053                 m_displayIDs = NULL;
00054         }
00055         if (m_numDisplays > 0)
00056         {
00057                 m_displayIDs = new CGDirectDisplayID [m_numDisplays];
00058                 GHOST_ASSERT((m_displayIDs!=NULL), "GHOST_DisplayManagerCarbon::GHOST_DisplayManagerCarbon(): memory allocation failed");
00059                 ::CGGetActiveDisplayList(m_numDisplays, m_displayIDs, &m_numDisplays);
00060         }
00061 }
00062 
00063 
00064 GHOST_TSuccess GHOST_DisplayManagerCarbon::getNumDisplays(GHOST_TUns8& numDisplays) const
00065 {
00066         numDisplays = (GHOST_TUns8) m_numDisplays;
00067         return GHOST_kSuccess;
00068 }
00069 
00070 
00071 GHOST_TSuccess GHOST_DisplayManagerCarbon::getNumDisplaySettings(GHOST_TUns8 display, GHOST_TInt32& numSettings) const
00072 {
00073         GHOST_ASSERT((display==kMainDisplay), "GHOST_DisplayManagerCarbon::getNumDisplaySettings(): only main display is supported");
00074         
00075         CFArrayRef displayModes;
00076         displayModes = ::CGDisplayAvailableModes(m_displayIDs[display]);
00077         CFIndex numModes = ::CFArrayGetCount(displayModes);
00078         numSettings = (GHOST_TInt32)numModes;
00079         
00080         return GHOST_kSuccess;
00081 }
00082 
00083 
00084 GHOST_TSuccess GHOST_DisplayManagerCarbon::getDisplaySetting(GHOST_TUns8 display, GHOST_TInt32 index, GHOST_DisplaySetting& setting) const
00085 {
00086         GHOST_ASSERT((display==kMainDisplay), "GHOST_DisplayManagerCarbon::getDisplaySetting(): only main display is supported");
00087         
00088         CFArrayRef displayModes;
00089         CGDirectDisplayID d = m_displayIDs[display];
00090         displayModes = ::CGDisplayAvailableModes(d);
00091         //CFIndex numModes = ::CFArrayGetCount(displayModes);/*unused*/
00092         //GHOST_TInt32 numSettings = (GHOST_TInt32)numModes; /*unused*/
00093          CFDictionaryRef displayModeValues = (CFDictionaryRef)::CFArrayGetValueAtIndex(displayModes, index);
00094                         
00095         setting.xPixels         = getValue(displayModeValues, kCGDisplayWidth);
00096         setting.yPixels         = getValue(displayModeValues, kCGDisplayHeight);
00097         setting.bpp                     = getValue(displayModeValues, kCGDisplayBitsPerPixel);
00098         setting.frequency       = getValue(displayModeValues, kCGDisplayRefreshRate);
00099                         
00100 #ifdef GHOST_DEBUG
00101         printf("display mode: width=%d, height=%d, bpp=%d, frequency=%d\n", setting.xPixels, setting.yPixels, setting.bpp, setting.frequency);
00102 #endif // GHOST_DEBUG
00103 
00104         return GHOST_kSuccess;
00105 }
00106 
00107 
00108 GHOST_TSuccess GHOST_DisplayManagerCarbon::getCurrentDisplaySetting(GHOST_TUns8 display, GHOST_DisplaySetting& setting) const
00109 {
00110         GHOST_ASSERT((display==kMainDisplay), "GHOST_DisplayManagerCarbon::getCurrentDisplaySetting(): only main display is supported");
00111         
00112         CFDictionaryRef displayModeValues = ::CGDisplayCurrentMode(m_displayIDs[display]);
00113         
00114         setting.xPixels         = getValue(displayModeValues, kCGDisplayWidth);
00115         setting.yPixels         = getValue(displayModeValues, kCGDisplayHeight);
00116         setting.bpp                     = getValue(displayModeValues, kCGDisplayBitsPerPixel);
00117         setting.frequency       = getValue(displayModeValues, kCGDisplayRefreshRate);
00118 
00119 #ifdef GHOST_DEBUG
00120         printf("current display mode: width=%d, height=%d, bpp=%d, frequency=%d\n", setting.xPixels, setting.yPixels, setting.bpp, setting.frequency);
00121 #endif // GHOST_DEBUG
00122 
00123         return GHOST_kSuccess;
00124 }
00125 
00126 
00127 GHOST_TSuccess GHOST_DisplayManagerCarbon::setCurrentDisplaySetting(GHOST_TUns8 display, const GHOST_DisplaySetting& setting)
00128 {
00129         GHOST_ASSERT((display==kMainDisplay), "GHOST_DisplayManagerCarbon::setCurrentDisplaySetting(): only main display is supported");
00130 
00131 #ifdef GHOST_DEBUG
00132         printf("GHOST_DisplayManagerCarbon::setCurrentDisplaySetting(): requested settings:\n");
00133         printf("  setting.xPixels=%d\n", setting.xPixels);
00134         printf("  setting.yPixels=%d\n", setting.yPixels);
00135         printf("  setting.bpp=%d\n", setting.bpp);
00136         printf("  setting.frequency=%d\n", setting.frequency);
00137 #endif // GHOST_DEBUG
00138 
00139         CFDictionaryRef displayModeValues = ::CGDisplayBestModeForParametersAndRefreshRate(
00140                 m_displayIDs[display],
00141                 (size_t)setting.bpp,
00142                 (size_t)setting.xPixels,
00143                 (size_t)setting.yPixels,
00144                 (CGRefreshRate)setting.frequency,
00145                 NULL);
00146 
00147 #ifdef GHOST_DEBUG
00148         printf("GHOST_DisplayManagerCarbon::setCurrentDisplaySetting(): switching to:\n");
00149         printf("  setting.xPixels=%d\n", getValue(displayModeValues, kCGDisplayWidth));
00150         printf("  setting.yPixels=%d\n", getValue(displayModeValues, kCGDisplayHeight));
00151         printf("  setting.bpp=%d\n", getValue(displayModeValues, kCGDisplayBitsPerPixel));
00152         printf("  setting.frequency=%d\n", getValue(displayModeValues, kCGDisplayRefreshRate));
00153 #endif // GHOST_DEBUG
00154 
00155         CGDisplayErr err = ::CGDisplaySwitchToMode(m_displayIDs[display], displayModeValues);
00156         
00157         return err == CGDisplayNoErr ? GHOST_kSuccess : GHOST_kFailure;
00158 }
00159 
00160 
00161 long GHOST_DisplayManagerCarbon::getValue(CFDictionaryRef values, CFStringRef key) const
00162 {
00163     CFNumberRef numberValue = (CFNumberRef) CFDictionaryGetValue(values, key);
00164     
00165     if (!numberValue)
00166     {
00167         return -1;
00168     }
00169     
00170     long intValue;
00171     
00172     if (!CFNumberGetValue(numberValue, kCFNumberLongType, &intValue))
00173     {
00174         return -1;
00175     }
00176     
00177     return intValue;
00178 }
00179