Merge branch 'blender2.7'
[blender.git] / intern / ghost / intern / GHOST_DisplayManager.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_DisplayManager.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_DisplayManager.h"
40 #include "GHOST_Debug.h"
41
42
43 GHOST_DisplayManager::GHOST_DisplayManager(void)
44     : m_settingsInitialized(false)
45 {
46 }
47
48
49 GHOST_DisplayManager::~GHOST_DisplayManager(void)
50 {
51 }
52
53
54 GHOST_TSuccess
55 GHOST_DisplayManager::initialize(
56                 void)
57 {
58         GHOST_TSuccess success;
59         if (!m_settingsInitialized) {
60                 success = initializeSettings();
61                 m_settingsInitialized = true;
62         }
63         else {
64                 success = GHOST_kSuccess;
65         }
66         return success;
67 }
68
69
70 GHOST_TSuccess
71 GHOST_DisplayManager::getNumDisplays(
72                 GHOST_TUns8& /*numDisplays*/) const
73 {
74         // Don't know if we have a display...
75         return GHOST_kFailure;
76 }
77
78
79 GHOST_TSuccess
80 GHOST_DisplayManager::getNumDisplaySettings(
81                 GHOST_TUns8 display,
82                 GHOST_TInt32& numSettings) const
83 {
84         GHOST_TSuccess success;
85
86         GHOST_ASSERT(m_settingsInitialized, "GHOST_DisplayManager::getNumDisplaySettings(): m_settingsInitialized=false");
87         GHOST_TUns8 numDisplays;
88         success = getNumDisplays(numDisplays);
89         if (success == GHOST_kSuccess) {
90                 if (display < numDisplays) {
91                         numSettings = m_settings[display].size();
92                 }
93                 else {
94                         success = GHOST_kFailure;
95                 }
96         }
97         return success;
98 }
99
100
101 GHOST_TSuccess
102 GHOST_DisplayManager::getDisplaySetting(
103                 GHOST_TUns8 display,
104                 GHOST_TInt32 index,
105                 GHOST_DisplaySetting& setting) const
106 {
107         GHOST_TSuccess success;
108
109         GHOST_ASSERT(m_settingsInitialized, "GHOST_DisplayManager::getNumDisplaySettings(): m_settingsInitialized=false");
110         GHOST_TUns8 numDisplays;
111         success = getNumDisplays(numDisplays);
112         if (success == GHOST_kSuccess) {
113                 if (display < numDisplays && ((GHOST_TUns8)index < m_settings[display].size())) {
114                         setting = m_settings[display][index];
115                 }
116                 else {
117                         success = GHOST_kFailure;
118                 }
119         }
120         return success;
121 }
122
123
124 GHOST_TSuccess
125 GHOST_DisplayManager::getCurrentDisplaySetting(
126                 GHOST_TUns8 /*display*/,
127                 GHOST_DisplaySetting& /*setting*/) const
128 {
129         return GHOST_kFailure;
130 }
131
132
133 GHOST_TSuccess
134 GHOST_DisplayManager::setCurrentDisplaySetting(
135                 GHOST_TUns8 /*display*/,
136                 const GHOST_DisplaySetting& /*setting*/)
137 {
138         return GHOST_kFailure;
139 }
140
141
142 GHOST_TSuccess
143 GHOST_DisplayManager::findMatch(
144                 GHOST_TUns8 display,
145                 const GHOST_DisplaySetting& setting,
146                 GHOST_DisplaySetting& match) const
147 {
148         GHOST_TSuccess success = GHOST_kSuccess;
149         GHOST_ASSERT(m_settingsInitialized, "GHOST_DisplayManager::findMatch(): m_settingsInitialized=false");
150
151         int criteria[4] = {
152                 (int)setting.xPixels,
153                 (int)setting.yPixels,
154                 (int)setting.bpp,
155                 (int)setting.frequency
156         };
157         int capabilities[4];
158         double field, score;
159         double best = 1e12; // A big number
160         int found = 0;
161
162         // Look at all the display modes
163         for (int i = 0; (i < (int)m_settings[display].size()); i++) {
164                 // Store the capabilities of the display device
165                 capabilities[0] = m_settings[display][i].xPixels;
166                 capabilities[1] = m_settings[display][i].yPixels;
167                 capabilities[2] = m_settings[display][i].bpp;
168                 capabilities[3] = m_settings[display][i].frequency;
169
170                 // Match against all the fields of the display settings
171                 score = 0;
172                 for (int j = 0; j < 4; j++) {
173                         field = capabilities[j] - criteria[j];
174                         score += field * field;
175                 }
176
177                 if (score < best) {
178                         found = i;
179                         best = score;
180                 }
181         }
182
183         match = m_settings[display][found];
184
185         GHOST_PRINT("GHOST_DisplayManager::findMatch(): settings of match:\n");
186         GHOST_PRINT("  setting.xPixels=" << match.xPixels << "\n");
187         GHOST_PRINT("  setting.yPixels=" << match.yPixels << "\n");
188         GHOST_PRINT("  setting.bpp=" << match.bpp << "\n");
189         GHOST_PRINT("  setting.frequency=" << match.frequency << "\n");
190
191         return success;
192 }
193
194
195 GHOST_TSuccess
196 GHOST_DisplayManager::initializeSettings(
197                 void)
198 {
199         GHOST_TUns8 numDisplays;
200         GHOST_TSuccess success = getNumDisplays(numDisplays);
201         if (success == GHOST_kSuccess) {
202                 for (GHOST_TUns8 display = 0; (display < numDisplays) && (success == GHOST_kSuccess); display++) {
203                         GHOST_DisplaySettings displaySettings;
204                         m_settings.push_back(displaySettings);
205                         GHOST_TInt32 numSettings;
206                         success = getNumDisplaySettings(display, numSettings);
207                         if (success == GHOST_kSuccess) {
208                                 GHOST_TInt32 index;
209                                 GHOST_DisplaySetting setting;
210                                 for (index = 0; (index < numSettings) && (success == GHOST_kSuccess); index++) {
211                                         success = getDisplaySetting(display, index, setting);
212                                         m_settings[display].push_back(setting);
213                                 }
214                         }
215                         else {
216                                 break;
217                         }
218                 }
219         }
220         return success;
221 }