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