Merged changes in the trunk up to revision 43748.
[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, const GHOST_TUns16 numOfAASamples)
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, numOfAASamples);
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::updateFullScreen(const GHOST_DisplaySetting& setting, GHOST_IWindow** window)
172 {
173         GHOST_TSuccess success = GHOST_kFailure;
174         GHOST_ASSERT(m_windowManager, "GHOST_System::updateFullScreen(): invalid window manager");
175         if(m_displayManager) {
176                 if (m_windowManager->getFullScreen()) {
177                         success = m_displayManager->setCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, setting);
178                 }
179         }
180
181         return success;
182 }
183
184 GHOST_TSuccess GHOST_System::endFullScreen(void)
185 {
186         GHOST_TSuccess success = GHOST_kFailure;
187         GHOST_ASSERT(m_windowManager, "GHOST_System::endFullScreen(): invalid window manager")
188         if (m_windowManager->getFullScreen()) {
189                 //GHOST_IWindow* window = m_windowManager->getFullScreenWindow();
190                 //GHOST_PRINT("GHOST_System::endFullScreen(): leaving window manager full-screen mode\n");
191                 success = m_windowManager->endFullScreen();
192                 GHOST_ASSERT(m_displayManager, "GHOST_System::endFullScreen(): invalid display manager")
193                 //GHOST_PRINT("GHOST_System::endFullScreen(): leaving full-screen mode\n");
194                 success = m_displayManager->setCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, m_preFullScreenSetting);
195         }
196         else {
197                 success = GHOST_kFailure;
198         }
199         return success;
200 }
201
202
203 bool GHOST_System::getFullScreen(void)
204 {
205         bool fullScreen;
206         if (m_windowManager) {
207                 fullScreen = m_windowManager->getFullScreen();
208         }
209         else {
210                 fullScreen = false;
211         }
212         return fullScreen;
213 }
214
215
216 bool GHOST_System::dispatchEvents()
217 {
218         bool handled = false;
219
220 #ifdef WITH_INPUT_NDOF
221         // NDOF Motion event is sent only once per dispatch, so do it now:
222         if (m_ndofManager) {
223                 handled |= m_ndofManager->sendMotionEvent();
224         }
225 #endif
226
227         if (m_eventManager) {
228                 handled |= m_eventManager->dispatchEvents();
229         }
230
231         m_timerManager->fireTimers(getMilliSeconds());
232         return handled;
233 }
234
235
236 GHOST_TSuccess GHOST_System::addEventConsumer(GHOST_IEventConsumer* consumer)
237 {
238         GHOST_TSuccess success;
239         if (m_eventManager) {
240                 success = m_eventManager->addConsumer(consumer);
241         }
242         else {
243                 success = GHOST_kFailure;
244         }
245         return success;
246 }
247
248 GHOST_TSuccess GHOST_System::removeEventConsumer(GHOST_IEventConsumer* consumer)
249 {
250         GHOST_TSuccess success;
251         if (m_eventManager) {
252                 success = m_eventManager->removeConsumer(consumer);
253         }
254         else {
255                 success = GHOST_kFailure;
256         }
257         return success;
258 }
259
260 GHOST_TSuccess GHOST_System::pushEvent(GHOST_IEvent* event)
261 {
262         GHOST_TSuccess success;
263         if (m_eventManager) {
264                 success = m_eventManager->pushEvent(event);
265         }
266         else {
267                 success = GHOST_kFailure;
268         }
269         return success;
270 }
271
272 GHOST_TSuccess GHOST_System::getModifierKeyState(GHOST_TModifierKeyMask mask, bool& isDown) const
273 {
274         GHOST_ModifierKeys keys;
275         // Get the state of all modifier keys 
276         GHOST_TSuccess success = getModifierKeys(keys);
277         if (success) {
278                 // Isolate the state of the key requested
279                 isDown = keys.get(mask);
280         }
281         return success;
282 }
283
284
285 GHOST_TSuccess GHOST_System::getButtonState(GHOST_TButtonMask mask, bool& isDown) const
286 {
287         GHOST_Buttons buttons;
288         // Get the state of all mouse buttons
289         GHOST_TSuccess success = getButtons(buttons);
290         if (success) {
291                 // Isolate the state of the mouse button requested
292                 isDown = buttons.get(mask);
293         }
294         return success;
295 }
296
297 GHOST_TSuccess GHOST_System::init()
298 {
299         m_timerManager = new GHOST_TimerManager ();
300         m_windowManager = new GHOST_WindowManager ();
301         m_eventManager = new GHOST_EventManager ();
302         
303 #ifdef GHOST_DEBUG
304         if (m_eventManager) {
305                 m_eventPrinter = new GHOST_EventPrinter();
306                 m_eventManager->addConsumer(m_eventPrinter);
307         }
308 #endif // GHOST_DEBUG
309
310         if (m_timerManager && m_windowManager && m_eventManager) {
311                 return GHOST_kSuccess;
312         } else {
313                 return GHOST_kFailure;
314         }
315 }
316
317
318 GHOST_TSuccess GHOST_System::exit()
319 {
320         if (getFullScreen()) {
321                 endFullScreen();
322         }
323         if (m_displayManager) {
324                 delete m_displayManager;
325                 m_displayManager = 0;
326         }
327         if (m_windowManager) {
328                 delete m_windowManager;
329                 m_windowManager = 0;
330         }
331         if (m_timerManager) {
332                 delete m_timerManager;
333                 m_timerManager = 0;
334         }
335         if (m_eventManager) {
336                 delete m_eventManager;
337                 m_eventManager = 0;
338         }
339 #ifdef WITH_INPUT_NDOF
340         if (m_ndofManager) {
341                 delete m_ndofManager;
342                 m_ndofManager = 0;
343         }
344 #endif
345         return GHOST_kSuccess;
346 }
347
348
349 GHOST_TSuccess GHOST_System::createFullScreenWindow(GHOST_Window** window, const bool stereoVisual, const GHOST_TUns16 numOfAASamples)
350 {
351         GHOST_TSuccess success;
352         GHOST_ASSERT(m_displayManager, "GHOST_System::createFullScreenWindow(): invalid display manager")
353         GHOST_DisplaySetting settings;
354
355         success = m_displayManager->getCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, settings);
356         if (success) {
357                 //GHOST_PRINT("GHOST_System::createFullScreenWindow(): creating full-screen window\n");
358                 *window = (GHOST_Window*)createWindow(
359                                         STR_String (""),
360                                         0, 0, settings.xPixels, settings.yPixels,
361                                         GHOST_kWindowStateFullScreen,
362                                         GHOST_kDrawingContextTypeOpenGL,
363                                         stereoVisual,
364                                         numOfAASamples);
365                 success = *window == 0 ? GHOST_kFailure : GHOST_kSuccess;
366         }
367         return success;
368 }