Initial revision
[blender.git] / intern / ghost / intern / GHOST_System.cpp
1 /**
2  * $Id$
3  * ***** BEGIN GPL/BL DUAL 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. The Blender
9  * Foundation also sells licenses for use in proprietary software under
10  * the Blender License.  See http://www.blender.org/BL/ for information
11  * about this.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL/BL DUAL LICENSE BLOCK *****
30  */
31
32 /**
33
34  * $Id$
35  * Copyright (C) 2001 NaN Technologies B.V.
36  * @author      Maarten Gribnau
37  * @date        May 7, 2001
38  */
39
40 #include "GHOST_System.h"
41
42 #include <time.h>
43
44 #include "GHOST_DisplayManager.h"
45 #include "GHOST_EventManager.h"
46 #include "GHOST_TimerTask.h"
47 #include "GHOST_TimerManager.h"
48 #include "GHOST_WindowManager.h"
49
50
51 GHOST_System::GHOST_System()
52 : m_displayManager(0), m_timerManager(0), m_windowManager(0), m_eventManager(0)
53 {
54 }
55
56
57 GHOST_System::~GHOST_System()
58 {
59         exit();
60 }
61
62
63 GHOST_TUns64 GHOST_System::getMilliSeconds() const
64 {
65         GHOST_TUns64 millis = ::clock();
66         if (CLOCKS_PER_SEC != 1000) {
67                 millis *= 1000;
68                 millis /= CLOCKS_PER_SEC;
69         }
70         return millis;
71 }
72
73
74 GHOST_ITimerTask* GHOST_System::installTimer(GHOST_TUns64 delay, GHOST_TUns64 interval, GHOST_TimerProcPtr timerProc, GHOST_TUserDataPtr userData)
75 {
76         GHOST_TUns64 millis = getMilliSeconds();
77         GHOST_TimerTask* timer = new GHOST_TimerTask(millis+delay, interval, timerProc, userData);
78         if (timer) {
79                 if (m_timerManager->addTimer(timer) == GHOST_kSuccess) {
80                         // Check to see whether we need to fire the timer right away
81                         m_timerManager->fireTimers(millis);
82                 }
83                 else {
84                         delete timer;
85                         timer = 0;
86                 }
87         }
88         return timer;
89 }
90
91
92 GHOST_TSuccess GHOST_System::removeTimer(GHOST_ITimerTask* timerTask)
93 {
94         GHOST_TSuccess success = GHOST_kFailure;
95         if (timerTask) {
96                 success = m_timerManager->removeTimer((GHOST_TimerTask*)timerTask);
97         }
98         return success;
99 }
100
101
102 GHOST_TSuccess GHOST_System::disposeWindow(GHOST_IWindow* window)
103 {
104         GHOST_TSuccess success;
105         if (window == m_windowManager->getFullScreenWindow()) {
106                 success = endFullScreen();
107         }
108         else {
109                 if (m_windowManager->getWindowFound(window)) {
110                         success = m_windowManager->removeWindow(window);
111                         if (success) {
112                                 delete window;
113                         }
114                 }
115                 else {
116                         success = GHOST_kFailure;
117                 }
118         }
119         return success;
120 }
121
122
123 bool GHOST_System::validWindow(GHOST_IWindow* window)
124 {
125         return m_windowManager->getWindowFound(window);
126 }
127
128
129 GHOST_TSuccess GHOST_System::beginFullScreen(const GHOST_DisplaySetting& setting, GHOST_IWindow** window,
130                                                                                          bool stereoVisual)
131 {
132         GHOST_TSuccess success = GHOST_kFailure;
133         GHOST_ASSERT(m_windowManager, "GHOST_System::beginFullScreen(): invalid window manager")
134         if (m_displayManager) {
135                 if (!m_windowManager->getFullScreen()) {
136                         m_displayManager->getCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, m_preFullScreenSetting);
137
138             //GHOST_PRINT("GHOST_System::beginFullScreen(): activating new display settings\n");
139                         success = m_displayManager->setCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, setting);
140                         if (success == GHOST_kSuccess) {
141                 //GHOST_PRINT("GHOST_System::beginFullScreen(): creating full-screen window\n");
142                                 success = createFullScreenWindow((GHOST_Window**)window, stereoVisual);
143                                 if (success == GHOST_kSuccess) {
144                                         m_windowManager->beginFullScreen(*window, stereoVisual);
145                                 }
146                                 else {
147                                         m_displayManager->setCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, m_preFullScreenSetting);
148                                 }
149                         }
150                 }
151         }
152         if (success == GHOST_kFailure) {
153                 GHOST_PRINT("GHOST_System::beginFullScreen(): could not enter full-screen mode\n");
154         }
155         return success;
156 }
157
158
159 GHOST_TSuccess GHOST_System::endFullScreen(void)
160 {
161         GHOST_TSuccess success = GHOST_kFailure;
162         GHOST_ASSERT(m_windowManager, "GHOST_System::endFullScreen(): invalid window manager")
163         if (m_windowManager->getFullScreen()) {
164         GHOST_IWindow* window = m_windowManager->getFullScreenWindow();
165         //GHOST_PRINT("GHOST_System::endFullScreen(): leaving window manager full-screen mode\n");
166                 success = m_windowManager->endFullScreen();
167                 GHOST_ASSERT(m_displayManager, "GHOST_System::endFullScreen(): invalid display manager")
168         //GHOST_PRINT("GHOST_System::endFullScreen(): leaving full-screen mode\n");
169                 success = m_displayManager->setCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, m_preFullScreenSetting);
170         }
171         else {
172                 success = GHOST_kFailure;
173         }
174         return success;
175 }
176
177
178 bool GHOST_System::getFullScreen(void)
179 {
180         bool fullScreen;
181         if (m_windowManager) {
182                 fullScreen = m_windowManager->getFullScreen();
183         }
184         else {
185                 fullScreen = false;
186         }
187         return fullScreen;
188 }
189
190
191 bool GHOST_System::dispatchEvents()
192 {
193         bool handled;
194         if (m_eventManager) {
195                 handled = m_eventManager->dispatchEvents();
196         }
197         else {
198                 handled = false;
199         }
200
201         m_timerManager->fireTimers(getMilliSeconds());
202         return handled;
203 }
204
205
206 GHOST_TSuccess GHOST_System::addEventConsumer(GHOST_IEventConsumer* consumer)
207 {
208         GHOST_TSuccess success;
209         if (m_eventManager) {
210                 success = m_eventManager->addConsumer(consumer);
211         }
212         else {
213                 success = GHOST_kFailure;
214         }
215         return success;
216 }
217
218
219 GHOST_TSuccess GHOST_System::pushEvent(GHOST_IEvent* event)
220 {
221         GHOST_TSuccess success;
222         if (m_eventManager) {
223                 success = m_eventManager->pushEvent(event);
224         }
225         else {
226                 success = GHOST_kFailure;
227         }
228         return success;
229 }
230
231
232 GHOST_TSuccess GHOST_System::getModifierKeyState(GHOST_TModifierKeyMask mask, bool& isDown) const
233 {
234         GHOST_ModifierKeys keys;
235         // Get the state of all modifier keys 
236         GHOST_TSuccess success = getModifierKeys(keys);
237         if (success) {
238                 // Isolate the state of the key requested
239                 isDown = keys.get(mask);
240         }
241         return success;
242 }
243
244
245 GHOST_TSuccess GHOST_System::getButtonState(GHOST_TButtonMask mask, bool& isDown) const
246 {
247         GHOST_Buttons buttons;
248         // Get the state of all mouse buttons
249         GHOST_TSuccess success = getButtons(buttons);
250         if (success) {
251                 // Isolate the state of the mouse button requested
252                 isDown = buttons.get(mask);
253         }
254         return success;
255 }
256
257
258 GHOST_TSuccess GHOST_System::init()
259 {
260         m_timerManager = new GHOST_TimerManager ();
261         m_windowManager = new GHOST_WindowManager ();
262         m_eventManager = new GHOST_EventManager ();
263 #ifdef GHOST_DEBUG
264         if (m_eventManager) {
265                 //m_eventManager->addConsumer(&m_eventPrinter);
266         }
267 #endif // GHOST_DEBUG
268
269         if (m_timerManager && m_windowManager && m_eventManager) {
270                 return GHOST_kSuccess;
271         } else {
272                 return GHOST_kFailure;
273         }
274 }
275
276
277 GHOST_TSuccess GHOST_System::exit()
278 {
279         if (getFullScreen()) {
280                 endFullScreen();
281         }
282         if (m_displayManager) {
283                 delete m_displayManager;
284                 m_displayManager = 0;
285         }
286         if (m_windowManager) {
287                 delete m_windowManager;
288                 m_windowManager = 0;
289         }
290         if (m_timerManager) {
291                 delete m_timerManager;
292                 m_timerManager = 0;
293         }
294         if (m_eventManager) {
295                 delete m_eventManager;
296                 m_eventManager = 0;
297         }
298         return GHOST_kSuccess;
299 }
300
301
302 GHOST_TSuccess GHOST_System::createFullScreenWindow(GHOST_Window** window, bool stereoVisual)
303 {
304         GHOST_TSuccess success;
305         GHOST_ASSERT(m_displayManager, "GHOST_System::createFullScreenWindow(): invalid display manager")
306         GHOST_DisplaySetting settings;
307
308         success = m_displayManager->getCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, settings);
309         if (success) {
310         //GHOST_PRINT("GHOST_System::createFullScreenWindow(): creating full-screen window\n");
311                 *window = (GHOST_Window*)createWindow(
312                                         STR_String (""),
313                                         0, 0, settings.xPixels, settings.yPixels,
314                                         GHOST_kWindowStateFullScreen,
315                                         GHOST_kDrawingContextTypeOpenGL,
316                                         stereoVisual);
317                 success = *window == 0 ? GHOST_kFailure : GHOST_kSuccess;
318         }
319         return success;
320 }