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