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