merge with trunk/2.5 at r24463
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 /**
30
31  * $Id$
32  * Copyright (C) 2001 NaN Technologies B.V.
33  * @author      Maarten Gribnau
34  * @date        May 7, 2001
35  */
36
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 #include "GHOST_System.h"
42
43 #include <time.h>
44 #include <stdio.h> /* just for printf */
45
46 #include "GHOST_DisplayManager.h"
47 #include "GHOST_EventManager.h"
48 #include "GHOST_NDOFManager.h"
49 #include "GHOST_TimerTask.h"
50 #include "GHOST_TimerManager.h"
51 #include "GHOST_WindowManager.h"
52
53
54 GHOST_System::GHOST_System()
55 : m_displayManager(0), m_timerManager(0), m_windowManager(0), m_eventManager(0), m_ndofManager(0)
56 {
57         m_canAcceptDragOperation = false;
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::pushEvent(GHOST_IEvent* event)
231 {
232         GHOST_TSuccess success;
233         if (m_eventManager) {
234                 success = m_eventManager->pushEvent(event);
235         }
236         else {
237                 success = GHOST_kFailure;
238         }
239         return success;
240 }
241
242 int GHOST_System::openNDOF(GHOST_IWindow* w,
243         GHOST_NDOFLibraryInit_fp setNdofLibraryInit, 
244         GHOST_NDOFLibraryShutdown_fp setNdofLibraryShutdown,
245         GHOST_NDOFDeviceOpen_fp setNdofDeviceOpen)
246 {
247  return   m_ndofManager->deviceOpen(w,
248         setNdofLibraryInit, 
249         setNdofLibraryShutdown,
250         setNdofDeviceOpen);
251 }
252
253
254 GHOST_TSuccess GHOST_System::getModifierKeyState(GHOST_TModifierKeyMask mask, bool& isDown) const
255 {
256         GHOST_ModifierKeys keys;
257         // Get the state of all modifier keys 
258         GHOST_TSuccess success = getModifierKeys(keys);
259         if (success) {
260                 // Isolate the state of the key requested
261                 isDown = keys.get(mask);
262         }
263         return success;
264 }
265
266
267 GHOST_TSuccess GHOST_System::getButtonState(GHOST_TButtonMask mask, bool& isDown) const
268 {
269         GHOST_Buttons buttons;
270         // Get the state of all mouse buttons
271         GHOST_TSuccess success = getButtons(buttons);
272         if (success) {
273                 // Isolate the state of the mouse button requested
274                 isDown = buttons.get(mask);
275         }
276         return success;
277 }
278
279 void GHOST_System::setAcceptDragOperation(bool canAccept)
280 {
281         m_canAcceptDragOperation = canAccept;
282 }
283
284 bool GHOST_System::canAcceptDragOperation() const
285 {
286         return m_canAcceptDragOperation;
287 }
288
289 GHOST_TSuccess GHOST_System::init()
290 {
291         m_timerManager = new GHOST_TimerManager ();
292         m_windowManager = new GHOST_WindowManager ();
293         m_eventManager = new GHOST_EventManager ();
294     m_ndofManager = new GHOST_NDOFManager();
295
296 #if 0
297         if(m_ndofManager)
298                 printf("ndof manager \n");
299 #endif
300         
301 #ifdef GHOST_DEBUG
302         if (m_eventManager) {
303                 m_eventPrinter = new GHOST_EventPrinter();
304                 m_eventManager->addConsumer(m_eventPrinter);
305         }
306 #endif // GHOST_DEBUG
307
308         if (m_timerManager && m_windowManager && m_eventManager) {
309                 return GHOST_kSuccess;
310         } else {
311                 return GHOST_kFailure;
312         }
313 }
314
315
316 GHOST_TSuccess GHOST_System::exit()
317 {
318         if (getFullScreen()) {
319                 endFullScreen();
320         }
321         if (m_displayManager) {
322                 delete m_displayManager;
323                 m_displayManager = 0;
324         }
325         if (m_windowManager) {
326                 delete m_windowManager;
327                 m_windowManager = 0;
328         }
329         if (m_timerManager) {
330                 delete m_timerManager;
331                 m_timerManager = 0;
332         }
333         if (m_eventManager) {
334                 delete m_eventManager;
335                 m_eventManager = 0;
336         }
337     if (m_ndofManager) {
338         delete m_ndofManager;
339         m_ndofManager = 0;
340     }
341         return GHOST_kSuccess;
342 }
343
344
345 GHOST_TSuccess GHOST_System::createFullScreenWindow(GHOST_Window** window, const bool stereoVisual)
346 {
347         GHOST_TSuccess success;
348         GHOST_ASSERT(m_displayManager, "GHOST_System::createFullScreenWindow(): invalid display manager")
349         GHOST_DisplaySetting settings;
350
351         success = m_displayManager->getCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, settings);
352         if (success) {
353         //GHOST_PRINT("GHOST_System::createFullScreenWindow(): creating full-screen window\n");
354                 *window = (GHOST_Window*)createWindow(
355                                         STR_String (""),
356                                         0, 0, settings.xPixels, settings.yPixels,
357                                         GHOST_kWindowStateFullScreen,
358                                         GHOST_kDrawingContextTypeOpenGL,
359                                         stereoVisual);
360                 success = *window == 0 ? GHOST_kFailure : GHOST_kSuccess;
361         }
362         return success;
363 }