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