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