NDOF related edits
[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),
50       m_timerManager(0),
51       m_windowManager(0),
52       m_eventManager(0)
53 #ifdef WITH_INPUT_NDOF
54       , m_ndofManager(0)
55 #endif
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 = false;
204
205         // NDOF Motion event is sent only once per dispatch, so do it now:
206         if (m_ndofManager) {
207                 handled |= m_ndofManager->sendMotionEvent();
208         }
209
210         if (m_eventManager) {
211                 handled |= m_eventManager->dispatchEvents();
212         }
213
214         m_timerManager->fireTimers(getMilliSeconds());
215         return handled;
216 }
217
218
219 GHOST_TSuccess GHOST_System::addEventConsumer(GHOST_IEventConsumer* consumer)
220 {
221         GHOST_TSuccess success;
222         if (m_eventManager) {
223                 success = m_eventManager->addConsumer(consumer);
224         }
225         else {
226                 success = GHOST_kFailure;
227         }
228         return success;
229 }
230
231 GHOST_TSuccess GHOST_System::removeEventConsumer(GHOST_IEventConsumer* consumer)
232 {
233         GHOST_TSuccess success;
234         if (m_eventManager) {
235                 success = m_eventManager->removeConsumer(consumer);
236         }
237         else {
238                 success = GHOST_kFailure;
239         }
240         return success;
241 }
242
243 GHOST_TSuccess GHOST_System::pushEvent(GHOST_IEvent* event)
244 {
245         GHOST_TSuccess success;
246         if (m_eventManager) {
247                 success = m_eventManager->pushEvent(event);
248         }
249         else {
250                 success = GHOST_kFailure;
251         }
252         return success;
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 GHOST_TSuccess GHOST_System::init()
281 {
282         m_timerManager = new GHOST_TimerManager ();
283         m_windowManager = new GHOST_WindowManager ();
284         m_eventManager = new GHOST_EventManager ();
285         
286 #ifdef GHOST_DEBUG
287         if (m_eventManager) {
288                 m_eventPrinter = new GHOST_EventPrinter();
289                 m_eventManager->addConsumer(m_eventPrinter);
290         }
291 #endif // GHOST_DEBUG
292
293         if (m_timerManager && m_windowManager && m_eventManager) {
294                 return GHOST_kSuccess;
295         } else {
296                 return GHOST_kFailure;
297         }
298 }
299
300
301 GHOST_TSuccess GHOST_System::exit()
302 {
303         if (getFullScreen()) {
304                 endFullScreen();
305         }
306         if (m_displayManager) {
307                 delete m_displayManager;
308                 m_displayManager = 0;
309         }
310         if (m_windowManager) {
311                 delete m_windowManager;
312                 m_windowManager = 0;
313         }
314         if (m_timerManager) {
315                 delete m_timerManager;
316                 m_timerManager = 0;
317         }
318         if (m_eventManager) {
319                 delete m_eventManager;
320                 m_eventManager = 0;
321         }
322         if (m_ndofManager) {
323                 delete m_ndofManager;
324                 m_ndofManager = 0;
325         }
326         return GHOST_kSuccess;
327 }
328
329
330 GHOST_TSuccess GHOST_System::createFullScreenWindow(GHOST_Window** window, const bool stereoVisual)
331 {
332         GHOST_TSuccess success;
333         GHOST_ASSERT(m_displayManager, "GHOST_System::createFullScreenWindow(): invalid display manager")
334         GHOST_DisplaySetting settings;
335
336         success = m_displayManager->getCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, settings);
337         if (success) {
338                 //GHOST_PRINT("GHOST_System::createFullScreenWindow(): creating full-screen window\n");
339                 *window = (GHOST_Window*)createWindow(
340                                         STR_String (""),
341                                         0, 0, settings.xPixels, settings.yPixels,
342                                         GHOST_kWindowStateFullScreen,
343                                         GHOST_kDrawingContextTypeOpenGL,
344                                         stereoVisual);
345                 success = *window == 0 ? GHOST_kFailure : GHOST_kSuccess;
346         }
347         return success;
348 }