Merge branch 'blender2.7'
[blender.git] / intern / ghost / intern / GHOST_DisplayManagerCocoa.mm
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):      Maarten Gribnau 09/2001
24                                         Damien Plisson  10/2009
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <Cocoa/Cocoa.h>
30
31 #include "GHOST_DisplayManagerCocoa.h"
32 #include "GHOST_Debug.h"
33
34 // We do not support multiple monitors at the moment
35
36
37 GHOST_DisplayManagerCocoa::GHOST_DisplayManagerCocoa(void)
38 {
39 }
40
41
42 GHOST_TSuccess GHOST_DisplayManagerCocoa::getNumDisplays(GHOST_TUns8& numDisplays) const
43 {
44         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
45         
46         numDisplays = (GHOST_TUns8) [[NSScreen screens] count];
47         
48         [pool drain];
49         return GHOST_kSuccess;
50 }
51
52
53 GHOST_TSuccess GHOST_DisplayManagerCocoa::getNumDisplaySettings(GHOST_TUns8 display, GHOST_TInt32& numSettings) const
54 {
55         numSettings = (GHOST_TInt32)3; //Width, Height, BitsPerPixel
56         
57         return GHOST_kSuccess;
58 }
59
60
61 GHOST_TSuccess GHOST_DisplayManagerCocoa::getDisplaySetting(GHOST_TUns8 display, GHOST_TInt32 index, GHOST_DisplaySetting& setting) const
62 {
63         NSScreen *askedDisplay;
64         
65         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
66         
67         if (display == kMainDisplay) //Screen #0 may not be the main one
68                 askedDisplay = [NSScreen mainScreen];
69         else
70                 askedDisplay = [[NSScreen screens] objectAtIndex:display];
71         
72         if (askedDisplay == nil) {
73                 [pool drain];
74                 return GHOST_kFailure;
75         }
76         
77         NSRect frame = [askedDisplay visibleFrame];
78         setting.xPixels = frame.size.width;
79         setting.yPixels = frame.size.height;
80         
81         setting.bpp = NSBitsPerPixelFromDepth([askedDisplay depth]);
82         
83         setting.frequency = 0; //No more CRT display...
84                                 
85 #ifdef GHOST_DEBUG
86         printf("display mode: width=%d, height=%d, bpp=%d, frequency=%d\n", setting.xPixels, setting.yPixels, setting.bpp, setting.frequency);
87 #endif // GHOST_DEBUG
88
89         [pool drain];
90         return GHOST_kSuccess;
91 }
92
93
94 GHOST_TSuccess GHOST_DisplayManagerCocoa::getCurrentDisplaySetting(GHOST_TUns8 display, GHOST_DisplaySetting& setting) const
95 {
96         NSScreen *askedDisplay;
97         
98         GHOST_ASSERT((display==kMainDisplay), "GHOST_DisplayManagerCocoa::getCurrentDisplaySetting(): only main display is supported");
99
100         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
101
102         if (display == kMainDisplay) //Screen #0 may not be the main one
103                 askedDisplay = [NSScreen mainScreen];
104         else
105                 askedDisplay = [[NSScreen screens] objectAtIndex:display];
106         
107         if (askedDisplay == nil) {
108                 [pool drain];
109                 return GHOST_kFailure;
110         }
111         
112         NSRect frame = [askedDisplay visibleFrame];
113         setting.xPixels = frame.size.width;
114         setting.yPixels = frame.size.height;
115         
116         setting.bpp = NSBitsPerPixelFromDepth([askedDisplay depth]);
117         
118         setting.frequency = 0; //No more CRT display...
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         [pool drain];
125         return GHOST_kSuccess;
126 }
127
128
129 GHOST_TSuccess GHOST_DisplayManagerCocoa::setCurrentDisplaySetting(GHOST_TUns8 display, const GHOST_DisplaySetting& setting)
130 {
131         GHOST_ASSERT((display==kMainDisplay), "GHOST_DisplayManagerCocoa::setCurrentDisplaySetting(): only main display is supported");
132
133 #ifdef GHOST_DEBUG
134         printf("GHOST_DisplayManagerCocoa::setCurrentDisplaySetting(): requested settings:\n");
135         printf("  setting.xPixels=%d\n", setting.xPixels);
136         printf("  setting.yPixels=%d\n", setting.yPixels);
137         printf("  setting.bpp=%d\n", setting.bpp);
138         printf("  setting.frequency=%d\n", setting.frequency);
139 #endif // GHOST_DEBUG
140
141         //Display configuration is no more available in 10.6
142         
143 /*      CFDictionaryRef displayModeValues = ::CGDisplayBestModeForParametersAndRefreshRate(
144                 m_displayIDs[display],
145                 (size_t)setting.bpp,
146                 (size_t)setting.xPixels,
147                 (size_t)setting.yPixels,
148                 (CGRefreshRate)setting.frequency,
149                 NULL);*/
150
151 #ifdef GHOST_DEBUG
152 /*      printf("GHOST_DisplayManagerCocoa::setCurrentDisplaySetting(): switching to:\n");
153         printf("  setting.xPixels=%d\n", getValue(displayModeValues, kCGDisplayWidth));
154         printf("  setting.yPixels=%d\n", getValue(displayModeValues, kCGDisplayHeight));
155         printf("  setting.bpp=%d\n", getValue(displayModeValues, kCGDisplayBitsPerPixel));
156         printf("  setting.frequency=%d\n", getValue(displayModeValues, kCGDisplayRefreshRate)); */
157 #endif // GHOST_DEBUG
158
159         //CGDisplayErr err = ::CGDisplaySwitchToMode(m_displayIDs[display], displayModeValues);
160
161         return /*err == CGDisplayNoErr ?*/ GHOST_kSuccess /*: GHOST_kFailure*/;
162 }