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