Merging r42800 through r42895 from trunk into soc-2011-tomato
[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,
77                                              GHOST_TUns64 interval,
78                                              GHOST_TimerProcPtr timerProc,
79                                              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 = false;
206
207 #ifdef WITH_INPUT_NDOF
208         // NDOF Motion event is sent only once per dispatch, so do it now:
209         if (m_ndofManager) {
210                 handled |= m_ndofManager->sendMotionEvent();
211         }
212 #endif
213
214         if (m_eventManager) {
215                 handled |= m_eventManager->dispatchEvents();
216         }
217
218         m_timerManager->fireTimers(getMilliSeconds());
219         return handled;
220 }
221
222
223 GHOST_TSuccess GHOST_System::addEventConsumer(GHOST_IEventConsumer* consumer)
224 {
225         GHOST_TSuccess success;
226         if (m_eventManager) {
227                 success = m_eventManager->addConsumer(consumer);
228         }
229         else {
230                 success = GHOST_kFailure;
231         }
232         return success;
233 }
234
235 GHOST_TSuccess GHOST_System::removeEventConsumer(GHOST_IEventConsumer* consumer)
236 {
237         GHOST_TSuccess success;
238         if (m_eventManager) {
239                 success = m_eventManager->removeConsumer(consumer);
240         }
241         else {
242                 success = GHOST_kFailure;
243         }
244         return success;
245 }
246
247 GHOST_TSuccess GHOST_System::pushEvent(GHOST_IEvent* event)
248 {
249         GHOST_TSuccess success;
250         if (m_eventManager) {
251                 success = m_eventManager->pushEvent(event);
252         }
253         else {
254                 success = GHOST_kFailure;
255         }
256         return success;
257 }
258
259 GHOST_TSuccess GHOST_System::getModifierKeyState(GHOST_TModifierKeyMask mask, bool& isDown) const
260 {
261         GHOST_ModifierKeys keys;
262         // Get the state of all modifier keys 
263         GHOST_TSuccess success = getModifierKeys(keys);
264         if (success) {
265                 // Isolate the state of the key requested
266                 isDown = keys.get(mask);
267         }
268         return success;
269 }
270
271
272 GHOST_TSuccess GHOST_System::getButtonState(GHOST_TButtonMask mask, bool& isDown) const
273 {
274         GHOST_Buttons buttons;
275         // Get the state of all mouse buttons
276         GHOST_TSuccess success = getButtons(buttons);
277         if (success) {
278                 // Isolate the state of the mouse button requested
279                 isDown = buttons.get(mask);
280         }
281         return success;
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         
290 #ifdef GHOST_DEBUG
291         if (m_eventManager) {
292                 m_eventPrinter = new GHOST_EventPrinter();
293                 m_eventManager->addConsumer(m_eventPrinter);
294         }
295 #endif // GHOST_DEBUG
296
297         if (m_timerManager && m_windowManager && m_eventManager) {
298                 return GHOST_kSuccess;
299         } else {
300                 return GHOST_kFailure;
301         }
302 }
303
304
305 GHOST_TSuccess GHOST_System::exit()
306 {
307         if (getFullScreen()) {
308                 endFullScreen();
309         }
310         if (m_displayManager) {
311                 delete m_displayManager;
312                 m_displayManager = 0;
313         }
314         if (m_windowManager) {
315                 delete m_windowManager;
316                 m_windowManager = 0;
317         }
318         if (m_timerManager) {
319                 delete m_timerManager;
320                 m_timerManager = 0;
321         }
322         if (m_eventManager) {
323                 delete m_eventManager;
324                 m_eventManager = 0;
325         }
326 #ifdef WITH_INPUT_NDOF
327         if (m_ndofManager) {
328                 delete m_ndofManager;
329                 m_ndofManager = 0;
330         }
331 #endif
332         return GHOST_kSuccess;
333 }
334
335
336 GHOST_TSuccess GHOST_System::createFullScreenWindow(GHOST_Window** window, const bool stereoVisual)
337 {
338         GHOST_TSuccess success;
339         GHOST_ASSERT(m_displayManager, "GHOST_System::createFullScreenWindow(): invalid display manager")
340         GHOST_DisplaySetting settings;
341
342         success = m_displayManager->getCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, settings);
343         if (success) {
344                 //GHOST_PRINT("GHOST_System::createFullScreenWindow(): creating full-screen window\n");
345                 *window = (GHOST_Window*)createWindow(
346                                         STR_String (""),
347                                         0, 0, settings.xPixels, settings.yPixels,
348                                         GHOST_kWindowStateFullScreen,
349                                         GHOST_kDrawingContextTypeOpenGL,
350                                         stereoVisual);
351                 success = *window == 0 ? GHOST_kFailure : GHOST_kSuccess;
352         }
353         return success;
354 }