update to trunk r14199
[blender.git] / intern / ghost / intern / GHOST_System.cpp
1 /**
2  * $Id$
3  * ***** BEGIN GPL/BL DUAL 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. The Blender
9  * Foundation also sells licenses for use in proprietary software under
10  * the Blender License.  See http://www.blender.org/BL/ for information
11  * about this.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL/BL DUAL LICENSE BLOCK *****
30  */
31
32 /**
33
34  * $Id$
35  * Copyright (C) 2001 NaN Technologies B.V.
36  * @author      Maarten Gribnau
37  * @date        May 7, 2001
38  */
39
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
43
44 #include "GHOST_System.h"
45
46 #include <time.h>
47
48 #include "GHOST_DisplayManager.h"
49 #include "GHOST_EventManager.h"
50 #include "GHOST_NDOFManager.h"
51 #include "GHOST_TimerTask.h"
52 #include "GHOST_TimerManager.h"
53 #include "GHOST_WindowManager.h"
54
55
56 GHOST_System::GHOST_System()
57 : m_displayManager(0), m_timerManager(0), m_windowManager(0), m_eventManager(0), m_ndofManager(0)
58 {
59 }
60
61
62 GHOST_System::~GHOST_System()
63 {
64         exit();
65 }
66
67
68 GHOST_TUns64 GHOST_System::getMilliSeconds() const
69 {
70         GHOST_TUns64 millis = ::clock();
71         if (CLOCKS_PER_SEC != 1000) {
72                 millis *= 1000;
73                 millis /= CLOCKS_PER_SEC;
74         }
75         return millis;
76 }
77
78
79 GHOST_ITimerTask* GHOST_System::installTimer(GHOST_TUns64 delay, GHOST_TUns64 interval, GHOST_TimerProcPtr timerProc, GHOST_TUserDataPtr userData)
80 {
81         GHOST_TUns64 millis = getMilliSeconds();
82         GHOST_TimerTask* timer = new GHOST_TimerTask(millis+delay, interval, timerProc, userData);
83         if (timer) {
84                 if (m_timerManager->addTimer(timer) == GHOST_kSuccess) {
85                         // Check to see whether we need to fire the timer right away
86                         m_timerManager->fireTimers(millis);
87                 }
88                 else {
89                         delete timer;
90                         timer = 0;
91                 }
92         }
93         return timer;
94 }
95
96
97 GHOST_TSuccess GHOST_System::removeTimer(GHOST_ITimerTask* timerTask)
98 {
99         GHOST_TSuccess success = GHOST_kFailure;
100         if (timerTask) {
101                 success = m_timerManager->removeTimer((GHOST_TimerTask*)timerTask);
102         }
103         return success;
104 }
105
106
107 GHOST_TSuccess GHOST_System::disposeWindow(GHOST_IWindow* window)
108 {
109         GHOST_TSuccess success;
110
111         /*
112          * Remove all pending events for the window.
113          */ 
114         if (m_windowManager->getWindowFound(window)) {
115                 m_eventManager->removeWindowEvents(window);
116         }
117         if (window == m_windowManager->getFullScreenWindow()) {
118                 success = endFullScreen();
119         }
120         else {
121                 if (m_windowManager->getWindowFound(window)) {
122                         success = m_windowManager->removeWindow(window);
123                         if (success) {
124                                 delete window;
125                         }
126                 }
127                 else {
128                         success = GHOST_kFailure;
129                 }
130         }
131         return success;
132 }
133
134
135 bool GHOST_System::validWindow(GHOST_IWindow* window)
136 {
137         return m_windowManager->getWindowFound(window);
138 }
139
140
141 GHOST_TSuccess GHOST_System::beginFullScreen(const GHOST_DisplaySetting& setting, GHOST_IWindow** window,
142                                                                                          const bool stereoVisual)
143 {
144         GHOST_TSuccess success = GHOST_kFailure;
145         GHOST_ASSERT(m_windowManager, "GHOST_System::beginFullScreen(): invalid window manager")
146         if (m_displayManager) {
147                 if (!m_windowManager->getFullScreen()) {
148                         m_displayManager->getCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, m_preFullScreenSetting);
149
150             //GHOST_PRINT("GHOST_System::beginFullScreen(): activating new display settings\n");
151                         success = m_displayManager->setCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, setting);
152                         if (success == GHOST_kSuccess) {
153                 //GHOST_PRINT("GHOST_System::beginFullScreen(): creating full-screen window\n");
154                                 success = createFullScreenWindow((GHOST_Window**)window, stereoVisual);
155                                 if (success == GHOST_kSuccess) {
156                                         m_windowManager->beginFullScreen(*window, stereoVisual);
157                                 }
158                                 else {
159                                         m_displayManager->setCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, m_preFullScreenSetting);
160                                 }
161                         }
162                 }
163         }
164         if (success == GHOST_kFailure) {
165                 GHOST_PRINT("GHOST_System::beginFullScreen(): could not enter full-screen mode\n");
166         }
167         return success;
168 }
169
170
171 GHOST_TSuccess GHOST_System::endFullScreen(void)
172 {
173         GHOST_TSuccess success = GHOST_kFailure;
174         GHOST_ASSERT(m_windowManager, "GHOST_System::endFullScreen(): invalid window manager")
175         if (m_windowManager->getFullScreen()) {
176         //GHOST_IWindow* window = m_windowManager->getFullScreenWindow();
177         //GHOST_PRINT("GHOST_System::endFullScreen(): leaving window manager full-screen mode\n");
178                 success = m_windowManager->endFullScreen();
179                 GHOST_ASSERT(m_displayManager, "GHOST_System::endFullScreen(): invalid display manager")
180         //GHOST_PRINT("GHOST_System::endFullScreen(): leaving full-screen mode\n");
181                 success = m_displayManager->setCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, m_preFullScreenSetting);
182         }
183         else {
184                 success = GHOST_kFailure;
185         }
186         return success;
187 }
188
189
190 bool GHOST_System::getFullScreen(void)
191 {
192         bool fullScreen;
193         if (m_windowManager) {
194                 fullScreen = m_windowManager->getFullScreen();
195         }
196         else {
197                 fullScreen = false;
198         }
199         return fullScreen;
200 }
201
202
203 bool GHOST_System::dispatchEvents()
204 {
205         bool handled;
206         if (m_eventManager) {
207                 handled = m_eventManager->dispatchEvents();
208         }
209         else {
210                 handled = false;
211         }
212
213         m_timerManager->fireTimers(getMilliSeconds());
214         return handled;
215 }
216
217
218 GHOST_TSuccess GHOST_System::addEventConsumer(GHOST_IEventConsumer* consumer)
219 {
220         GHOST_TSuccess success;
221         if (m_eventManager) {
222                 success = m_eventManager->addConsumer(consumer);
223         }
224         else {
225                 success = GHOST_kFailure;
226         }
227         return success;
228 }
229
230
231 GHOST_TSuccess GHOST_System::pushEvent(GHOST_IEvent* event)
232 {
233         GHOST_TSuccess success;
234         if (m_eventManager) {
235                 success = m_eventManager->pushEvent(event);
236         }
237         else {
238                 success = GHOST_kFailure;
239         }
240         return success;
241 }
242
243 int GHOST_System::openNDOF(GHOST_IWindow* w,
244         GHOST_NDOFLibraryInit_fp setNdofLibraryInit, 
245         GHOST_NDOFLibraryShutdown_fp setNdofLibraryShutdown,
246         GHOST_NDOFDeviceOpen_fp setNdofDeviceOpen)
247 {
248  return   m_ndofManager->deviceOpen(w,
249         setNdofLibraryInit, 
250         setNdofLibraryShutdown,
251         setNdofDeviceOpen);
252 }
253
254
255 GHOST_TSuccess GHOST_System::getModifierKeyState(GHOST_TModifierKeyMask mask, bool& isDown) const
256 {
257         GHOST_ModifierKeys keys;
258         // Get the state of all modifier keys 
259         GHOST_TSuccess success = getModifierKeys(keys);
260         if (success) {
261                 // Isolate the state of the key requested
262                 isDown = keys.get(mask);
263         }
264         return success;
265 }
266
267
268 GHOST_TSuccess GHOST_System::getButtonState(GHOST_TButtonMask mask, bool& isDown) const
269 {
270         GHOST_Buttons buttons;
271         // Get the state of all mouse buttons
272         GHOST_TSuccess success = getButtons(buttons);
273         if (success) {
274                 // Isolate the state of the mouse button requested
275                 isDown = buttons.get(mask);
276         }
277         return success;
278 }
279
280
281 GHOST_TSuccess GHOST_System::init()
282 {
283         m_timerManager = new GHOST_TimerManager ();
284         m_windowManager = new GHOST_WindowManager ();
285         m_eventManager = new GHOST_EventManager ();
286     m_ndofManager = new GHOST_NDOFManager();
287
288         if(m_ndofManager)
289                 printf("ndof manager \n");
290
291 #ifdef GHOST_DEBUG
292         if (m_eventManager) {
293                 m_eventManager->addConsumer(&m_eventPrinter);
294         }
295 #endif // GHOST_DEBUG
296
297         if (m_timerManager && m_windowManager && m_eventManager) {
298                 return GHOST_kSuccess;
299         } else {
300                 return GHOST_kFailure;
301         }
302 }
303
304
305 GHOST_TSuccess GHOST_System::exit()
306 {
307         if (getFullScreen()) {
308                 endFullScreen();
309         }
310         if (m_displayManager) {
311                 delete m_displayManager;
312                 m_displayManager = 0;
313         }
314         if (m_windowManager) {
315                 delete m_windowManager;
316                 m_windowManager = 0;
317         }
318         if (m_timerManager) {
319                 delete m_timerManager;
320                 m_timerManager = 0;
321         }
322         if (m_eventManager) {
323                 delete m_eventManager;
324                 m_eventManager = 0;
325         }
326     if (m_ndofManager) {
327         delete m_ndofManager;
328         m_ndofManager = 0;
329     }
330         return GHOST_kSuccess;
331 }
332
333
334 GHOST_TSuccess GHOST_System::createFullScreenWindow(GHOST_Window** window, const bool stereoVisual)
335 {
336         GHOST_TSuccess success;
337         GHOST_ASSERT(m_displayManager, "GHOST_System::createFullScreenWindow(): invalid display manager")
338         GHOST_DisplaySetting settings;
339
340         success = m_displayManager->getCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, settings);
341         if (success) {
342         //GHOST_PRINT("GHOST_System::createFullScreenWindow(): creating full-screen window\n");
343                 *window = (GHOST_Window*)createWindow(
344                                         STR_String (""),
345                                         0, 0, settings.xPixels, settings.yPixels,
346                                         GHOST_kWindowStateFullScreen,
347                                         GHOST_kDrawingContextTypeOpenGL,
348                                         stereoVisual);
349                 success = *window == 0 ? GHOST_kFailure : GHOST_kSuccess;
350         }
351         return success;
352 }