svn merge ^/trunk/blender -r42416:42422
[blender.git] / intern / ghost / intern / GHOST_System.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file ghost/intern/GHOST_System.cpp
29  *  \ingroup GHOST
30  *  \author     Maarten Gribnau
31  *  \date       May 7, 2001
32  */
33
34 #include "GHOST_System.h"
35
36 #include <time.h>
37 #include <stdio.h> /* just for printf */
38
39 #include "GHOST_DisplayManager.h"
40 #include "GHOST_EventManager.h"
41 #include "GHOST_NDOFManager.h"
42 #include "GHOST_TimerTask.h"
43 #include "GHOST_TimerManager.h"
44 #include "GHOST_WindowManager.h"
45
46
47 GHOST_System::GHOST_System()
48     : m_displayManager(0),
49       m_timerManager(0),
50       m_windowManager(0),
51       m_eventManager(0)
52 #ifdef WITH_INPUT_NDOF
53       , m_ndofManager(0)
54 #endif
55 {
56 }
57
58
59 GHOST_System::~GHOST_System()
60 {
61         exit();
62 }
63
64
65 GHOST_TUns64 GHOST_System::getMilliSeconds() const
66 {
67         GHOST_TUns64 millis = ::clock();
68         if (CLOCKS_PER_SEC != 1000) {
69                 millis *= 1000;
70                 millis /= CLOCKS_PER_SEC;
71         }
72         return millis;
73 }
74
75
76 GHOST_ITimerTask* GHOST_System::installTimer(GHOST_TUns64 delay, GHOST_TUns64 interval, GHOST_TimerProcPtr timerProc, GHOST_TUserDataPtr userData)
77 {
78         GHOST_TUns64 millis = getMilliSeconds();
79         GHOST_TimerTask* timer = new GHOST_TimerTask(millis+delay, interval, timerProc, userData);
80         if (timer) {
81                 if (m_timerManager->addTimer(timer) == GHOST_kSuccess) {
82                         // Check to see whether we need to fire the timer right away
83                         m_timerManager->fireTimers(millis);
84                 }
85                 else {
86                         delete timer;
87                         timer = 0;
88                 }
89         }
90         return timer;
91 }
92
93
94 GHOST_TSuccess GHOST_System::removeTimer(GHOST_ITimerTask* timerTask)
95 {
96         GHOST_TSuccess success = GHOST_kFailure;
97         if (timerTask) {
98                 success = m_timerManager->removeTimer((GHOST_TimerTask*)timerTask);
99         }
100         return success;
101 }
102
103
104 GHOST_TSuccess GHOST_System::disposeWindow(GHOST_IWindow* window)
105 {
106         GHOST_TSuccess success;
107
108         /*
109          * Remove all pending events for the window.
110          */ 
111         if (m_windowManager->getWindowFound(window)) {
112                 m_eventManager->removeWindowEvents(window);
113         }
114         if (window == m_windowManager->getFullScreenWindow()) {
115                 success = endFullScreen();
116         }
117         else {
118                 if (m_windowManager->getWindowFound(window)) {
119                         success = m_windowManager->removeWindow(window);
120                         if (success) {
121                                 delete window;
122                         }
123                 }
124                 else {
125                         success = GHOST_kFailure;
126                 }
127         }
128         return success;
129 }
130
131
132 bool GHOST_System::validWindow(GHOST_IWindow* window)
133 {
134         return m_windowManager->getWindowFound(window);
135 }
136
137
138 GHOST_TSuccess GHOST_System::beginFullScreen(const GHOST_DisplaySetting& setting, GHOST_IWindow** window,
139                                                                                          const bool stereoVisual)
140 {
141         GHOST_TSuccess success = GHOST_kFailure;
142         GHOST_ASSERT(m_windowManager, "GHOST_System::beginFullScreen(): invalid window manager")
143         if (m_displayManager) {
144                 if (!m_windowManager->getFullScreen()) {
145                         m_displayManager->getCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, m_preFullScreenSetting);
146
147                         //GHOST_PRINT("GHOST_System::beginFullScreen(): activating new display settings\n");
148                         success = m_displayManager->setCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, setting);
149                         if (success == GHOST_kSuccess) {
150                                 //GHOST_PRINT("GHOST_System::beginFullScreen(): creating full-screen window\n");
151                                 success = createFullScreenWindow((GHOST_Window**)window, stereoVisual);
152                                 if (success == GHOST_kSuccess) {
153                                         m_windowManager->beginFullScreen(*window, stereoVisual);
154                                 }
155                                 else {
156                                         m_displayManager->setCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, m_preFullScreenSetting);
157                                 }
158                         }
159                 }
160         }
161         if (success == GHOST_kFailure) {
162                 GHOST_PRINT("GHOST_System::beginFullScreen(): could not enter full-screen mode\n");
163         }
164         return success;
165 }
166
167
168 GHOST_TSuccess GHOST_System::endFullScreen(void)
169 {
170         GHOST_TSuccess success = GHOST_kFailure;
171         GHOST_ASSERT(m_windowManager, "GHOST_System::endFullScreen(): invalid window manager")
172         if (m_windowManager->getFullScreen()) {
173                 //GHOST_IWindow* window = m_windowManager->getFullScreenWindow();
174                 //GHOST_PRINT("GHOST_System::endFullScreen(): leaving window manager full-screen mode\n");
175                 success = m_windowManager->endFullScreen();
176                 GHOST_ASSERT(m_displayManager, "GHOST_System::endFullScreen(): invalid display manager")
177                 //GHOST_PRINT("GHOST_System::endFullScreen(): leaving full-screen mode\n");
178                 success = m_displayManager->setCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, m_preFullScreenSetting);
179         }
180         else {
181                 success = GHOST_kFailure;
182         }
183         return success;
184 }
185
186
187 bool GHOST_System::getFullScreen(void)
188 {
189         bool fullScreen;
190         if (m_windowManager) {
191                 fullScreen = m_windowManager->getFullScreen();
192         }
193         else {
194                 fullScreen = false;
195         }
196         return fullScreen;
197 }
198
199
200 bool GHOST_System::dispatchEvents()
201 {
202         bool handled = false;
203
204 #ifdef WITH_INPUT_NDOF
205         // NDOF Motion event is sent only once per dispatch, so do it now:
206         if (m_ndofManager) {
207                 handled |= m_ndofManager->sendMotionEvent();
208         }
209 #endif
210
211         if (m_eventManager) {
212                 handled |= m_eventManager->dispatchEvents();
213         }
214
215         m_timerManager->fireTimers(getMilliSeconds());
216         return handled;
217 }
218
219
220 GHOST_TSuccess GHOST_System::addEventConsumer(GHOST_IEventConsumer* consumer)
221 {
222         GHOST_TSuccess success;
223         if (m_eventManager) {
224                 success = m_eventManager->addConsumer(consumer);
225         }
226         else {
227                 success = GHOST_kFailure;
228         }
229         return success;
230 }
231
232 GHOST_TSuccess GHOST_System::removeEventConsumer(GHOST_IEventConsumer* consumer)
233 {
234         GHOST_TSuccess success;
235         if (m_eventManager) {
236                 success = m_eventManager->removeConsumer(consumer);
237         }
238         else {
239                 success = GHOST_kFailure;
240         }
241         return success;
242 }
243
244 GHOST_TSuccess GHOST_System::pushEvent(GHOST_IEvent* event)
245 {
246         GHOST_TSuccess success;
247         if (m_eventManager) {
248                 success = m_eventManager->pushEvent(event);
249         }
250         else {
251                 success = GHOST_kFailure;
252         }
253         return success;
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 GHOST_TSuccess GHOST_System::init()
282 {
283         m_timerManager = new GHOST_TimerManager ();
284         m_windowManager = new GHOST_WindowManager ();
285         m_eventManager = new GHOST_EventManager ();
286         
287 #ifdef GHOST_DEBUG
288         if (m_eventManager) {
289                 m_eventPrinter = new GHOST_EventPrinter();
290                 m_eventManager->addConsumer(m_eventPrinter);
291         }
292 #endif // GHOST_DEBUG
293
294         if (m_timerManager && m_windowManager && m_eventManager) {
295                 return GHOST_kSuccess;
296         } else {
297                 return GHOST_kFailure;
298         }
299 }
300
301
302 GHOST_TSuccess GHOST_System::exit()
303 {
304         if (getFullScreen()) {
305                 endFullScreen();
306         }
307         if (m_displayManager) {
308                 delete m_displayManager;
309                 m_displayManager = 0;
310         }
311         if (m_windowManager) {
312                 delete m_windowManager;
313                 m_windowManager = 0;
314         }
315         if (m_timerManager) {
316                 delete m_timerManager;
317                 m_timerManager = 0;
318         }
319         if (m_eventManager) {
320                 delete m_eventManager;
321                 m_eventManager = 0;
322         }
323 #ifdef WITH_INPUT_NDOF
324         if (m_ndofManager) {
325                 delete m_ndofManager;
326                 m_ndofManager = 0;
327         }
328 #endif
329         return GHOST_kSuccess;
330 }
331
332
333 GHOST_TSuccess GHOST_System::createFullScreenWindow(GHOST_Window** window, const bool stereoVisual)
334 {
335         GHOST_TSuccess success;
336         GHOST_ASSERT(m_displayManager, "GHOST_System::createFullScreenWindow(): invalid display manager")
337         GHOST_DisplaySetting settings;
338
339         success = m_displayManager->getCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, settings);
340         if (success) {
341                 //GHOST_PRINT("GHOST_System::createFullScreenWindow(): creating full-screen window\n");
342                 *window = (GHOST_Window*)createWindow(
343                                         STR_String (""),
344                                         0, 0, settings.xPixels, settings.yPixels,
345                                         GHOST_kWindowStateFullScreen,
346                                         GHOST_kDrawingContextTypeOpenGL,
347                                         stereoVisual);
348                 success = *window == 0 ? GHOST_kFailure : GHOST_kSuccess;
349         }
350         return success;
351 }