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