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