COLLADA branch: merge from trunk -r 24522:24758.
[blender.git] / intern / ghost / intern / GHOST_System.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., 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$
32  * Copyright (C) 2001 NaN Technologies B.V.
33  * @author      Maarten Gribnau
34  * @date        May 7, 2001
35  */
36
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 #include "GHOST_System.h"
42
43 #include <time.h>
44 #include <stdio.h> /* just for printf */
45
46 #include "GHOST_DisplayManager.h"
47 #include "GHOST_EventManager.h"
48 #include "GHOST_NDOFManager.h"
49 #include "GHOST_TimerTask.h"
50 #include "GHOST_TimerManager.h"
51 #include "GHOST_WindowManager.h"
52
53
54 GHOST_System::GHOST_System()
55 : m_displayManager(0), m_timerManager(0), m_windowManager(0), m_eventManager(0), m_ndofManager(0)
56 {
57 }
58
59
60 GHOST_System::~GHOST_System()
61 {
62         exit();
63 }
64
65
66 GHOST_TUns64 GHOST_System::getMilliSeconds() const
67 {
68         GHOST_TUns64 millis = ::clock();
69         if (CLOCKS_PER_SEC != 1000) {
70                 millis *= 1000;
71                 millis /= CLOCKS_PER_SEC;
72         }
73         return millis;
74 }
75
76
77 GHOST_ITimerTask* GHOST_System::installTimer(GHOST_TUns64 delay, GHOST_TUns64 interval, GHOST_TimerProcPtr timerProc, GHOST_TUserDataPtr userData)
78 {
79         GHOST_TUns64 millis = getMilliSeconds();
80         GHOST_TimerTask* timer = new GHOST_TimerTask(millis+delay, interval, timerProc, userData);
81         if (timer) {
82                 if (m_timerManager->addTimer(timer) == GHOST_kSuccess) {
83                         // Check to see whether we need to fire the timer right away
84                         m_timerManager->fireTimers(millis);
85                 }
86                 else {
87                         delete timer;
88                         timer = 0;
89                 }
90         }
91         return timer;
92 }
93
94
95 GHOST_TSuccess GHOST_System::removeTimer(GHOST_ITimerTask* timerTask)
96 {
97         GHOST_TSuccess success = GHOST_kFailure;
98         if (timerTask) {
99                 success = m_timerManager->removeTimer((GHOST_TimerTask*)timerTask);
100         }
101         return success;
102 }
103
104
105 GHOST_TSuccess GHOST_System::disposeWindow(GHOST_IWindow* window)
106 {
107         GHOST_TSuccess success;
108
109         /*
110          * Remove all pending events for the window.
111          */ 
112         if (m_windowManager->getWindowFound(window)) {
113                 m_eventManager->removeWindowEvents(window);
114         }
115         if (window == m_windowManager->getFullScreenWindow()) {
116                 success = endFullScreen();
117         }
118         else {
119                 if (m_windowManager->getWindowFound(window)) {
120                         success = m_windowManager->removeWindow(window);
121                         if (success) {
122                                 delete window;
123                         }
124                 }
125                 else {
126                         success = GHOST_kFailure;
127                 }
128         }
129         return success;
130 }
131
132
133 bool GHOST_System::validWindow(GHOST_IWindow* window)
134 {
135         return m_windowManager->getWindowFound(window);
136 }
137
138
139 GHOST_TSuccess GHOST_System::beginFullScreen(const GHOST_DisplaySetting& setting, GHOST_IWindow** window,
140                                                                                          const bool stereoVisual)
141 {
142         GHOST_TSuccess success = GHOST_kFailure;
143         GHOST_ASSERT(m_windowManager, "GHOST_System::beginFullScreen(): invalid window manager")
144         if (m_displayManager) {
145                 if (!m_windowManager->getFullScreen()) {
146                         m_displayManager->getCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, m_preFullScreenSetting);
147
148             //GHOST_PRINT("GHOST_System::beginFullScreen(): activating new display settings\n");
149                         success = m_displayManager->setCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, setting);
150                         if (success == GHOST_kSuccess) {
151                 //GHOST_PRINT("GHOST_System::beginFullScreen(): creating full-screen window\n");
152                                 success = createFullScreenWindow((GHOST_Window**)window, stereoVisual);
153                                 if (success == GHOST_kSuccess) {
154                                         m_windowManager->beginFullScreen(*window, stereoVisual);
155                                 }
156                                 else {
157                                         m_displayManager->setCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, m_preFullScreenSetting);
158                                 }
159                         }
160                 }
161         }
162         if (success == GHOST_kFailure) {
163                 GHOST_PRINT("GHOST_System::beginFullScreen(): could not enter full-screen mode\n");
164         }
165         return success;
166 }
167
168
169 GHOST_TSuccess GHOST_System::endFullScreen(void)
170 {
171         GHOST_TSuccess success = GHOST_kFailure;
172         GHOST_ASSERT(m_windowManager, "GHOST_System::endFullScreen(): invalid window manager")
173         if (m_windowManager->getFullScreen()) {
174         //GHOST_IWindow* window = m_windowManager->getFullScreenWindow();
175         //GHOST_PRINT("GHOST_System::endFullScreen(): leaving window manager full-screen mode\n");
176                 success = m_windowManager->endFullScreen();
177                 GHOST_ASSERT(m_displayManager, "GHOST_System::endFullScreen(): invalid display manager")
178         //GHOST_PRINT("GHOST_System::endFullScreen(): leaving full-screen mode\n");
179                 success = m_displayManager->setCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, m_preFullScreenSetting);
180         }
181         else {
182                 success = GHOST_kFailure;
183         }
184         return success;
185 }
186
187
188 bool GHOST_System::getFullScreen(void)
189 {
190         bool fullScreen;
191         if (m_windowManager) {
192                 fullScreen = m_windowManager->getFullScreen();
193         }
194         else {
195                 fullScreen = false;
196         }
197         return fullScreen;
198 }
199
200
201 bool GHOST_System::dispatchEvents()
202 {
203         bool handled;
204         if (m_eventManager) {
205                 handled = m_eventManager->dispatchEvents();
206         }
207         else {
208                 handled = false;
209         }
210
211         m_timerManager->fireTimers(getMilliSeconds());
212         return handled;
213 }
214
215
216 GHOST_TSuccess GHOST_System::addEventConsumer(GHOST_IEventConsumer* consumer)
217 {
218         GHOST_TSuccess success;
219         if (m_eventManager) {
220                 success = m_eventManager->addConsumer(consumer);
221         }
222         else {
223                 success = GHOST_kFailure;
224         }
225         return success;
226 }
227
228
229 GHOST_TSuccess GHOST_System::pushEvent(GHOST_IEvent* event)
230 {
231         GHOST_TSuccess success;
232         if (m_eventManager) {
233                 success = m_eventManager->pushEvent(event);
234         }
235         else {
236                 success = GHOST_kFailure;
237         }
238         return success;
239 }
240
241 int GHOST_System::openNDOF(GHOST_IWindow* w,
242         GHOST_NDOFLibraryInit_fp setNdofLibraryInit, 
243         GHOST_NDOFLibraryShutdown_fp setNdofLibraryShutdown,
244         GHOST_NDOFDeviceOpen_fp setNdofDeviceOpen)
245 {
246  return   m_ndofManager->deviceOpen(w,
247         setNdofLibraryInit, 
248         setNdofLibraryShutdown,
249         setNdofDeviceOpen);
250 }
251
252
253 GHOST_TSuccess GHOST_System::getModifierKeyState(GHOST_TModifierKeyMask mask, bool& isDown) const
254 {
255         GHOST_ModifierKeys keys;
256         // Get the state of all modifier keys 
257         GHOST_TSuccess success = getModifierKeys(keys);
258         if (success) {
259                 // Isolate the state of the key requested
260                 isDown = keys.get(mask);
261         }
262         return success;
263 }
264
265
266 GHOST_TSuccess GHOST_System::getButtonState(GHOST_TButtonMask mask, bool& isDown) const
267 {
268         GHOST_Buttons buttons;
269         // Get the state of all mouse buttons
270         GHOST_TSuccess success = getButtons(buttons);
271         if (success) {
272                 // Isolate the state of the mouse button requested
273                 isDown = buttons.get(mask);
274         }
275         return success;
276 }
277
278 GHOST_TSuccess GHOST_System::init()
279 {
280         m_timerManager = new GHOST_TimerManager ();
281         m_windowManager = new GHOST_WindowManager ();
282         m_eventManager = new GHOST_EventManager ();
283     m_ndofManager = new GHOST_NDOFManager();
284
285 #if 0
286         if(m_ndofManager)
287                 printf("ndof manager \n");
288 #endif
289         
290 #ifdef GHOST_DEBUG
291         if (m_eventManager) {
292                 m_eventPrinter = new GHOST_EventPrinter();
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 }