3 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
19 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20 * All rights reserved.
22 * The Original Code is: all of this file.
24 * Contributor(s): none yet.
26 * ***** END GPL LICENSE BLOCK *****
32 * Copyright (C) 2001 NaN Technologies B.V.
33 * @author Maarten Gribnau
41 #include "GHOST_System.h"
44 #include <stdio.h> /* just for printf */
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"
54 GHOST_System::GHOST_System()
55 : m_displayManager(0), m_timerManager(0), m_windowManager(0), m_eventManager(0), m_ndofManager(0)
60 GHOST_System::~GHOST_System()
66 GHOST_TUns64 GHOST_System::getMilliSeconds() const
68 GHOST_TUns64 millis = ::clock();
69 if (CLOCKS_PER_SEC != 1000) {
71 millis /= CLOCKS_PER_SEC;
77 GHOST_ITimerTask* GHOST_System::installTimer(GHOST_TUns64 delay, GHOST_TUns64 interval, GHOST_TimerProcPtr timerProc, GHOST_TUserDataPtr userData)
79 GHOST_TUns64 millis = getMilliSeconds();
80 GHOST_TimerTask* timer = new GHOST_TimerTask(millis+delay, interval, timerProc, userData);
82 if (m_timerManager->addTimer(timer) == GHOST_kSuccess) {
83 // Check to see whether we need to fire the timer right away
84 m_timerManager->fireTimers(millis);
95 GHOST_TSuccess GHOST_System::removeTimer(GHOST_ITimerTask* timerTask)
97 GHOST_TSuccess success = GHOST_kFailure;
99 success = m_timerManager->removeTimer((GHOST_TimerTask*)timerTask);
105 GHOST_TSuccess GHOST_System::disposeWindow(GHOST_IWindow* window)
107 GHOST_TSuccess success;
110 * Remove all pending events for the window.
112 if (m_windowManager->getWindowFound(window)) {
113 m_eventManager->removeWindowEvents(window);
115 if (window == m_windowManager->getFullScreenWindow()) {
116 success = endFullScreen();
119 if (m_windowManager->getWindowFound(window)) {
120 success = m_windowManager->removeWindow(window);
126 success = GHOST_kFailure;
133 bool GHOST_System::validWindow(GHOST_IWindow* window)
135 return m_windowManager->getWindowFound(window);
139 GHOST_TSuccess GHOST_System::beginFullScreen(const GHOST_DisplaySetting& setting, GHOST_IWindow** window,
140 const bool stereoVisual)
142 GHOST_TSuccess success = GHOST_kFailure;
143 GHOST_ASSERT(m_windowManager, "GHOST_System::beginFullScreen(): invalid window manager")
144 if (m_displayManager) {
145 if (!m_windowManager->getFullScreen()) {
146 m_displayManager->getCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, m_preFullScreenSetting);
148 //GHOST_PRINT("GHOST_System::beginFullScreen(): activating new display settings\n");
149 success = m_displayManager->setCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, setting);
150 if (success == GHOST_kSuccess) {
151 //GHOST_PRINT("GHOST_System::beginFullScreen(): creating full-screen window\n");
152 success = createFullScreenWindow((GHOST_Window**)window, stereoVisual);
153 if (success == GHOST_kSuccess) {
154 m_windowManager->beginFullScreen(*window, stereoVisual);
157 m_displayManager->setCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, m_preFullScreenSetting);
162 if (success == GHOST_kFailure) {
163 GHOST_PRINT("GHOST_System::beginFullScreen(): could not enter full-screen mode\n");
169 GHOST_TSuccess GHOST_System::endFullScreen(void)
171 GHOST_TSuccess success = GHOST_kFailure;
172 GHOST_ASSERT(m_windowManager, "GHOST_System::endFullScreen(): invalid window manager")
173 if (m_windowManager->getFullScreen()) {
174 //GHOST_IWindow* window = m_windowManager->getFullScreenWindow();
175 //GHOST_PRINT("GHOST_System::endFullScreen(): leaving window manager full-screen mode\n");
176 success = m_windowManager->endFullScreen();
177 GHOST_ASSERT(m_displayManager, "GHOST_System::endFullScreen(): invalid display manager")
178 //GHOST_PRINT("GHOST_System::endFullScreen(): leaving full-screen mode\n");
179 success = m_displayManager->setCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, m_preFullScreenSetting);
182 success = GHOST_kFailure;
188 bool GHOST_System::getFullScreen(void)
191 if (m_windowManager) {
192 fullScreen = m_windowManager->getFullScreen();
201 bool GHOST_System::dispatchEvents()
204 if (m_eventManager) {
205 handled = m_eventManager->dispatchEvents();
211 m_timerManager->fireTimers(getMilliSeconds());
216 GHOST_TSuccess GHOST_System::addEventConsumer(GHOST_IEventConsumer* consumer)
218 GHOST_TSuccess success;
219 if (m_eventManager) {
220 success = m_eventManager->addConsumer(consumer);
223 success = GHOST_kFailure;
229 GHOST_TSuccess GHOST_System::pushEvent(GHOST_IEvent* event)
231 GHOST_TSuccess success;
232 if (m_eventManager) {
233 success = m_eventManager->pushEvent(event);
236 success = GHOST_kFailure;
241 int GHOST_System::openNDOF(GHOST_IWindow* w,
242 GHOST_NDOFLibraryInit_fp setNdofLibraryInit,
243 GHOST_NDOFLibraryShutdown_fp setNdofLibraryShutdown,
244 GHOST_NDOFDeviceOpen_fp setNdofDeviceOpen)
246 return m_ndofManager->deviceOpen(w,
248 setNdofLibraryShutdown,
253 GHOST_TSuccess GHOST_System::getModifierKeyState(GHOST_TModifierKeyMask mask, bool& isDown) const
255 GHOST_ModifierKeys keys;
256 // Get the state of all modifier keys
257 GHOST_TSuccess success = getModifierKeys(keys);
259 // Isolate the state of the key requested
260 isDown = keys.get(mask);
266 GHOST_TSuccess GHOST_System::getButtonState(GHOST_TButtonMask mask, bool& isDown) const
268 GHOST_Buttons buttons;
269 // Get the state of all mouse buttons
270 GHOST_TSuccess success = getButtons(buttons);
272 // Isolate the state of the mouse button requested
273 isDown = buttons.get(mask);
279 GHOST_TSuccess GHOST_System::init()
281 m_timerManager = new GHOST_TimerManager ();
282 m_windowManager = new GHOST_WindowManager ();
283 m_eventManager = new GHOST_EventManager ();
284 m_ndofManager = new GHOST_NDOFManager();
288 printf("ndof manager \n");
292 if (m_eventManager) {
293 m_eventManager->addConsumer(&m_eventPrinter);
295 #endif // GHOST_DEBUG
297 if (m_timerManager && m_windowManager && m_eventManager) {
298 return GHOST_kSuccess;
300 return GHOST_kFailure;
305 GHOST_TSuccess GHOST_System::exit()
307 if (getFullScreen()) {
310 if (m_displayManager) {
311 delete m_displayManager;
312 m_displayManager = 0;
314 if (m_windowManager) {
315 delete m_windowManager;
318 if (m_timerManager) {
319 delete m_timerManager;
322 if (m_eventManager) {
323 delete m_eventManager;
327 delete m_ndofManager;
330 return GHOST_kSuccess;
334 GHOST_TSuccess GHOST_System::createFullScreenWindow(GHOST_Window** window, const bool stereoVisual)
336 GHOST_TSuccess success;
337 GHOST_ASSERT(m_displayManager, "GHOST_System::createFullScreenWindow(): invalid display manager")
338 GHOST_DisplaySetting settings;
340 success = m_displayManager->getCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, settings);
342 //GHOST_PRINT("GHOST_System::createFullScreenWindow(): creating full-screen window\n");
343 *window = (GHOST_Window*)createWindow(
345 0, 0, settings.xPixels, settings.yPixels,
346 GHOST_kWindowStateFullScreen,
347 GHOST_kDrawingContextTypeOpenGL,
349 success = *window == 0 ? GHOST_kFailure : GHOST_kSuccess;