add back ghost/guardedalloc from trunk
[blender-staging.git] / intern / ghost / intern / GHOST_DisplayManagerCarbon.cpp
1 /*
2  * $Id$
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s): none yet.
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /** \file ghost/intern/GHOST_DisplayManagerCarbon.cpp
30  *  \ingroup GHOST
31  */
32
33
34 /**
35
36  * $Id$
37  * Copyright (C) 2001 NaN Technologies B.V.
38  * @author      Maarten Gribnau
39  * @date        September 21, 2001
40  */
41
42 #include "GHOST_DisplayManagerCarbon.h"
43 #include "GHOST_Debug.h"
44
45 // We do not support multiple monitors at the moment
46
47
48 GHOST_DisplayManagerCarbon::GHOST_DisplayManagerCarbon(void)
49 {
50         if (::CGGetActiveDisplayList(0, NULL, &m_numDisplays) != CGDisplayNoErr)
51         {
52                 m_numDisplays = 0;
53                 m_displayIDs = NULL;
54         }
55         if (m_numDisplays > 0)
56         {
57                 m_displayIDs = new CGDirectDisplayID [m_numDisplays];
58                 GHOST_ASSERT((m_displayIDs!=NULL), "GHOST_DisplayManagerCarbon::GHOST_DisplayManagerCarbon(): memory allocation failed");
59                 ::CGGetActiveDisplayList(m_numDisplays, m_displayIDs, &m_numDisplays);
60         }
61 }
62
63
64 GHOST_TSuccess GHOST_DisplayManagerCarbon::getNumDisplays(GHOST_TUns8& numDisplays) const
65 {
66         numDisplays = (GHOST_TUns8) m_numDisplays;
67         return GHOST_kSuccess;
68 }
69
70
71 GHOST_TSuccess GHOST_DisplayManagerCarbon::getNumDisplaySettings(GHOST_TUns8 display, GHOST_TInt32& numSettings) const
72 {
73         GHOST_ASSERT((display==kMainDisplay), "GHOST_DisplayManagerCarbon::getNumDisplaySettings(): only main display is supported");
74         
75         CFArrayRef displayModes;
76         displayModes = ::CGDisplayAvailableModes(m_displayIDs[display]);
77         CFIndex numModes = ::CFArrayGetCount(displayModes);
78         numSettings = (GHOST_TInt32)numModes;
79         
80         return GHOST_kSuccess;
81 }
82
83
84 GHOST_TSuccess GHOST_DisplayManagerCarbon::getDisplaySetting(GHOST_TUns8 display, GHOST_TInt32 index, GHOST_DisplaySetting& setting) const
85 {
86         GHOST_ASSERT((display==kMainDisplay), "GHOST_DisplayManagerCarbon::getDisplaySetting(): only main display is supported");
87         
88         CFArrayRef displayModes;
89         CGDirectDisplayID d = m_displayIDs[display];
90         displayModes = ::CGDisplayAvailableModes(d);
91         //CFIndex numModes = ::CFArrayGetCount(displayModes);/*unused*/
92         //GHOST_TInt32 numSettings = (GHOST_TInt32)numModes; /*unused*/
93          CFDictionaryRef displayModeValues = (CFDictionaryRef)::CFArrayGetValueAtIndex(displayModes, index);
94                         
95         setting.xPixels         = getValue(displayModeValues, kCGDisplayWidth);
96         setting.yPixels         = getValue(displayModeValues, kCGDisplayHeight);
97         setting.bpp                     = getValue(displayModeValues, kCGDisplayBitsPerPixel);
98         setting.frequency       = getValue(displayModeValues, kCGDisplayRefreshRate);
99                         
100 #ifdef GHOST_DEBUG
101         printf("display mode: width=%d, height=%d, bpp=%d, frequency=%d\n", setting.xPixels, setting.yPixels, setting.bpp, setting.frequency);
102 #endif // GHOST_DEBUG
103
104         return GHOST_kSuccess;
105 }
106
107
108 GHOST_TSuccess GHOST_DisplayManagerCarbon::getCurrentDisplaySetting(GHOST_TUns8 display, GHOST_DisplaySetting& setting) const
109 {
110         GHOST_ASSERT((display==kMainDisplay), "GHOST_DisplayManagerCarbon::getCurrentDisplaySetting(): only main display is supported");
111         
112         CFDictionaryRef displayModeValues = ::CGDisplayCurrentMode(m_displayIDs[display]);
113         
114         setting.xPixels         = getValue(displayModeValues, kCGDisplayWidth);
115         setting.yPixels         = getValue(displayModeValues, kCGDisplayHeight);
116         setting.bpp                     = getValue(displayModeValues, kCGDisplayBitsPerPixel);
117         setting.frequency       = getValue(displayModeValues, kCGDisplayRefreshRate);
118
119 #ifdef GHOST_DEBUG
120         printf("current display mode: width=%d, height=%d, bpp=%d, frequency=%d\n", setting.xPixels, setting.yPixels, setting.bpp, setting.frequency);
121 #endif // GHOST_DEBUG
122
123         return GHOST_kSuccess;
124 }
125
126
127 GHOST_TSuccess GHOST_DisplayManagerCarbon::setCurrentDisplaySetting(GHOST_TUns8 display, const GHOST_DisplaySetting& setting)
128 {
129         GHOST_ASSERT((display==kMainDisplay), "GHOST_DisplayManagerCarbon::setCurrentDisplaySetting(): only main display is supported");
130
131 #ifdef GHOST_DEBUG
132         printf("GHOST_DisplayManagerCarbon::setCurrentDisplaySetting(): requested settings:\n");
133         printf("  setting.xPixels=%d\n", setting.xPixels);
134         printf("  setting.yPixels=%d\n", setting.yPixels);
135         printf("  setting.bpp=%d\n", setting.bpp);
136         printf("  setting.frequency=%d\n", setting.frequency);
137 #endif // GHOST_DEBUG
138
139         CFDictionaryRef displayModeValues = ::CGDisplayBestModeForParametersAndRefreshRate(
140                 m_displayIDs[display],
141                 (size_t)setting.bpp,
142                 (size_t)setting.xPixels,
143                 (size_t)setting.yPixels,
144                 (CGRefreshRate)setting.frequency,
145                 NULL);
146
147 #ifdef GHOST_DEBUG
148         printf("GHOST_DisplayManagerCarbon::setCurrentDisplaySetting(): switching to:\n");
149         printf("  setting.xPixels=%d\n", getValue(displayModeValues, kCGDisplayWidth));
150         printf("  setting.yPixels=%d\n", getValue(displayModeValues, kCGDisplayHeight));
151         printf("  setting.bpp=%d\n", getValue(displayModeValues, kCGDisplayBitsPerPixel));
152         printf("  setting.frequency=%d\n", getValue(displayModeValues, kCGDisplayRefreshRate));
153 #endif // GHOST_DEBUG
154
155         CGDisplayErr err = ::CGDisplaySwitchToMode(m_displayIDs[display], displayModeValues);
156         
157         return err == CGDisplayNoErr ? GHOST_kSuccess : GHOST_kFailure;
158 }
159
160
161 long GHOST_DisplayManagerCarbon::getValue(CFDictionaryRef values, CFStringRef key) const
162 {
163     CFNumberRef numberValue = (CFNumberRef) CFDictionaryGetValue(values, key);
164     
165     if (!numberValue)
166     {
167         return -1;
168     }
169     
170     long intValue;
171     
172     if (!CFNumberGetValue(numberValue, kCFNumberLongType, &intValue))
173     {
174         return -1;
175     }
176     
177     return intValue;
178 }
179