Cocoa port start:
[blender.git] / intern / ghost / intern / GHOST_DisplayManagerCocoa.mm
1 /**
2  * $Id: GHOST_DisplayManagerCocoa.mm 13161 2008-01-07 19:13:47Z hos $
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 /**
30
31  * $Id: GHOST_DisplayManagerCocoa.mm 13161 2008-01-07 19:13:47Z hos $
32  * Copyright (C) 2001 NaN Technologies B.V.
33  * @author      Maarten Gribnau
34  * @date        September 21, 2001
35  */
36
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 #include <Carbon/Carbon.h>
42
43 #include "GHOST_DisplayManagerCocoa.h"
44 #include "GHOST_Debug.h"
45
46 // We do not support multiple monitors at the moment
47
48
49 GHOST_DisplayManagerCocoa::GHOST_DisplayManagerCocoa(void)
50 {
51         if (::CGGetActiveDisplayList(0, NULL, &m_numDisplays) != CGDisplayNoErr)
52         {
53                 m_numDisplays = 0;
54                 m_displayIDs = NULL;
55         }
56         if (m_numDisplays > 0)
57         {
58                 m_displayIDs = new CGDirectDisplayID [m_numDisplays];
59                 GHOST_ASSERT((m_displayIDs!=NULL), "GHOST_DisplayManagerCocoa::GHOST_DisplayManagerCocoa(): memory allocation failed");
60                 ::CGGetActiveDisplayList(m_numDisplays, m_displayIDs, &m_numDisplays);
61         }
62 }
63
64
65 GHOST_TSuccess GHOST_DisplayManagerCocoa::getNumDisplays(GHOST_TUns8& numDisplays) const
66 {
67         numDisplays = (GHOST_TUns8) m_numDisplays;
68         return GHOST_kSuccess;
69 }
70
71
72 GHOST_TSuccess GHOST_DisplayManagerCocoa::getNumDisplaySettings(GHOST_TUns8 display, GHOST_TInt32& numSettings) const
73 {
74         GHOST_ASSERT((display==kMainDisplay), "GHOST_DisplayManagerCocoa::getNumDisplaySettings(): only main display is supported");
75         
76         CFArrayRef displayModes;
77         displayModes = ::CGDisplayAvailableModes(m_displayIDs[display]);
78         CFIndex numModes = ::CFArrayGetCount(displayModes);
79         numSettings = (GHOST_TInt32)numModes;
80         
81         return GHOST_kSuccess;
82 }
83
84
85 GHOST_TSuccess GHOST_DisplayManagerCocoa::getDisplaySetting(GHOST_TUns8 display, GHOST_TInt32 index, GHOST_DisplaySetting& setting) const
86 {
87         GHOST_ASSERT((display==kMainDisplay), "GHOST_DisplayManagerCocoa::getDisplaySetting(): only main display is supported");
88         
89         CFArrayRef displayModes;
90         CGDirectDisplayID d = m_displayIDs[display];
91         displayModes = ::CGDisplayAvailableModes(d);
92         //CFIndex numModes = ::CFArrayGetCount(displayModes);/*unused*/
93         //GHOST_TInt32 numSettings = (GHOST_TInt32)numModes; /*unused*/
94          CFDictionaryRef displayModeValues = (CFDictionaryRef)::CFArrayGetValueAtIndex(displayModes, index);
95                         
96         setting.xPixels         = getValue(displayModeValues, kCGDisplayWidth);
97         setting.yPixels         = getValue(displayModeValues, kCGDisplayHeight);
98         setting.bpp                     = getValue(displayModeValues, kCGDisplayBitsPerPixel);
99         setting.frequency       = getValue(displayModeValues, kCGDisplayRefreshRate);
100                         
101 #ifdef GHOST_DEBUG
102         printf("display mode: width=%d, height=%d, bpp=%d, frequency=%d\n", setting.xPixels, setting.yPixels, setting.bpp, setting.frequency);
103 #endif // GHOST_DEBUG
104
105         return GHOST_kSuccess;
106 }
107
108
109 GHOST_TSuccess GHOST_DisplayManagerCocoa::getCurrentDisplaySetting(GHOST_TUns8 display, GHOST_DisplaySetting& setting) const
110 {
111         GHOST_ASSERT((display==kMainDisplay), "GHOST_DisplayManagerCocoa::getCurrentDisplaySetting(): only main display is supported");
112         
113         CFDictionaryRef displayModeValues = ::CGDisplayCurrentMode(m_displayIDs[display]);
114         
115         setting.xPixels         = getValue(displayModeValues, kCGDisplayWidth);
116         setting.yPixels         = getValue(displayModeValues, kCGDisplayHeight);
117         setting.bpp                     = getValue(displayModeValues, kCGDisplayBitsPerPixel);
118         setting.frequency       = getValue(displayModeValues, kCGDisplayRefreshRate);
119
120 #ifdef GHOST_DEBUG
121         printf("current display mode: width=%d, height=%d, bpp=%d, frequency=%d\n", setting.xPixels, setting.yPixels, setting.bpp, setting.frequency);
122 #endif // GHOST_DEBUG
123
124         return GHOST_kSuccess;
125 }
126
127
128 GHOST_TSuccess GHOST_DisplayManagerCocoa::setCurrentDisplaySetting(GHOST_TUns8 display, const GHOST_DisplaySetting& setting)
129 {
130         GHOST_ASSERT((display==kMainDisplay), "GHOST_DisplayManagerCocoa::setCurrentDisplaySetting(): only main display is supported");
131
132 #ifdef GHOST_DEBUG
133         printf("GHOST_DisplayManagerCocoa::setCurrentDisplaySetting(): requested settings:\n");
134         printf("  setting.xPixels=%d\n", setting.xPixels);
135         printf("  setting.yPixels=%d\n", setting.yPixels);
136         printf("  setting.bpp=%d\n", setting.bpp);
137         printf("  setting.frequency=%d\n", setting.frequency);
138 #endif // GHOST_DEBUG
139
140         CFDictionaryRef displayModeValues = ::CGDisplayBestModeForParametersAndRefreshRate(
141                 m_displayIDs[display],
142                 (size_t)setting.bpp,
143                 (size_t)setting.xPixels,
144                 (size_t)setting.yPixels,
145                 (CGRefreshRate)setting.frequency,
146                 NULL);
147
148 #ifdef GHOST_DEBUG
149         printf("GHOST_DisplayManagerCocoa::setCurrentDisplaySetting(): switching to:\n");
150         printf("  setting.xPixels=%d\n", getValue(displayModeValues, kCGDisplayWidth));
151         printf("  setting.yPixels=%d\n", getValue(displayModeValues, kCGDisplayHeight));
152         printf("  setting.bpp=%d\n", getValue(displayModeValues, kCGDisplayBitsPerPixel));
153         printf("  setting.frequency=%d\n", getValue(displayModeValues, kCGDisplayRefreshRate));
154 #endif // GHOST_DEBUG
155
156         CGDisplayErr err = ::CGDisplaySwitchToMode(m_displayIDs[display], displayModeValues);
157         
158         return err == CGDisplayNoErr ? GHOST_kSuccess : GHOST_kFailure;
159 }
160
161
162 long GHOST_DisplayManagerCocoa::getValue(CFDictionaryRef values, CFStringRef key) const
163 {
164     CFNumberRef numberValue = (CFNumberRef) CFDictionaryGetValue(values, key);
165     
166     if (!numberValue)
167     {
168         return -1;
169     }
170     
171     long intValue;
172     
173     if (!CFNumberGetValue(numberValue, kCFNumberLongType, &intValue))
174     {
175         return -1;
176     }
177     
178     return intValue;
179 }
180