3 * ***** BEGIN GPL/BL DUAL 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. 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
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.
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.
22 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23 * All rights reserved.
25 * The Original Code is: all of this file.
27 * Contributor(s): none yet.
29 * ***** END GPL/BL DUAL LICENSE BLOCK *****
35 * Copyright (C) 2001 NaN Technologies B.V.
36 * @author Maarten Gribnau
44 #include "GHOST_System.h"
47 #include <stdio.h> /* just for printf */
49 #include "GHOST_DisplayManager.h"
50 #include "GHOST_EventManager.h"
51 #include "GHOST_NDOFManager.h"
52 #include "GHOST_TimerTask.h"
53 #include "GHOST_TimerManager.h"
54 #include "GHOST_WindowManager.h"
57 GHOST_System::GHOST_System()
58 : m_displayManager(0), m_timerManager(0), m_windowManager(0), m_eventManager(0), m_ndofManager(0)
63 GHOST_System::~GHOST_System()
69 GHOST_TUns64 GHOST_System::getMilliSeconds() const
71 GHOST_TUns64 millis = ::clock();
72 if (CLOCKS_PER_SEC != 1000) {
74 millis /= CLOCKS_PER_SEC;
80 GHOST_ITimerTask* GHOST_System::installTimer(GHOST_TUns64 delay, GHOST_TUns64 interval, GHOST_TimerProcPtr timerProc, GHOST_TUserDataPtr userData)
82 GHOST_TUns64 millis = getMilliSeconds();
83 GHOST_TimerTask* timer = new GHOST_TimerTask(millis+delay, interval, timerProc, userData);
85 if (m_timerManager->addTimer(timer) == GHOST_kSuccess) {
86 // Check to see whether we need to fire the timer right away
87 m_timerManager->fireTimers(millis);
98 GHOST_TSuccess GHOST_System::removeTimer(GHOST_ITimerTask* timerTask)
100 GHOST_TSuccess success = GHOST_kFailure;
102 success = m_timerManager->removeTimer((GHOST_TimerTask*)timerTask);
108 GHOST_TSuccess GHOST_System::disposeWindow(GHOST_IWindow* window)
110 GHOST_TSuccess success;
113 * Remove all pending events for the window.
115 if (m_windowManager->getWindowFound(window)) {
116 m_eventManager->removeWindowEvents(window);
118 if (window == m_windowManager->getFullScreenWindow()) {
119 success = endFullScreen();
122 if (m_windowManager->getWindowFound(window)) {
123 success = m_windowManager->removeWindow(window);
129 success = GHOST_kFailure;
136 bool GHOST_System::validWindow(GHOST_IWindow* window)
138 return m_windowManager->getWindowFound(window);
142 GHOST_TSuccess GHOST_System::beginFullScreen(const GHOST_DisplaySetting& setting, GHOST_IWindow** window,
143 const bool stereoVisual)
145 GHOST_TSuccess success = GHOST_kFailure;
146 GHOST_ASSERT(m_windowManager, "GHOST_System::beginFullScreen(): invalid window manager")
147 if (m_displayManager) {
148 if (!m_windowManager->getFullScreen()) {
149 m_displayManager->getCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, m_preFullScreenSetting);
151 //GHOST_PRINT("GHOST_System::beginFullScreen(): activating new display settings\n");
152 success = m_displayManager->setCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, setting);
153 if (success == GHOST_kSuccess) {
154 //GHOST_PRINT("GHOST_System::beginFullScreen(): creating full-screen window\n");
155 success = createFullScreenWindow((GHOST_Window**)window, stereoVisual);
156 if (success == GHOST_kSuccess) {
157 m_windowManager->beginFullScreen(*window, stereoVisual);
160 m_displayManager->setCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, m_preFullScreenSetting);
165 if (success == GHOST_kFailure) {
166 GHOST_PRINT("GHOST_System::beginFullScreen(): could not enter full-screen mode\n");
172 GHOST_TSuccess GHOST_System::endFullScreen(void)
174 GHOST_TSuccess success = GHOST_kFailure;
175 GHOST_ASSERT(m_windowManager, "GHOST_System::endFullScreen(): invalid window manager")
176 if (m_windowManager->getFullScreen()) {
177 //GHOST_IWindow* window = m_windowManager->getFullScreenWindow();
178 //GHOST_PRINT("GHOST_System::endFullScreen(): leaving window manager full-screen mode\n");
179 success = m_windowManager->endFullScreen();
180 GHOST_ASSERT(m_displayManager, "GHOST_System::endFullScreen(): invalid display manager")
181 //GHOST_PRINT("GHOST_System::endFullScreen(): leaving full-screen mode\n");
182 success = m_displayManager->setCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, m_preFullScreenSetting);
185 success = GHOST_kFailure;
191 bool GHOST_System::getFullScreen(void)
194 if (m_windowManager) {
195 fullScreen = m_windowManager->getFullScreen();
204 bool GHOST_System::dispatchEvents()
207 if (m_eventManager) {
208 handled = m_eventManager->dispatchEvents();
214 m_timerManager->fireTimers(getMilliSeconds());
219 GHOST_TSuccess GHOST_System::addEventConsumer(GHOST_IEventConsumer* consumer)
221 GHOST_TSuccess success;
222 if (m_eventManager) {
223 success = m_eventManager->addConsumer(consumer);
226 success = GHOST_kFailure;
232 GHOST_TSuccess GHOST_System::pushEvent(GHOST_IEvent* event)
234 GHOST_TSuccess success;
235 if (m_eventManager) {
236 success = m_eventManager->pushEvent(event);
239 success = GHOST_kFailure;
244 int GHOST_System::openNDOF(GHOST_IWindow* w,
245 GHOST_NDOFLibraryInit_fp setNdofLibraryInit,
246 GHOST_NDOFLibraryShutdown_fp setNdofLibraryShutdown,
247 GHOST_NDOFDeviceOpen_fp setNdofDeviceOpen)
249 return m_ndofManager->deviceOpen(w,
251 setNdofLibraryShutdown,
256 GHOST_TSuccess GHOST_System::getModifierKeyState(GHOST_TModifierKeyMask mask, bool& isDown) const
258 GHOST_ModifierKeys keys;
259 // Get the state of all modifier keys
260 GHOST_TSuccess success = getModifierKeys(keys);
262 // Isolate the state of the key requested
263 isDown = keys.get(mask);
269 GHOST_TSuccess GHOST_System::getButtonState(GHOST_TButtonMask mask, bool& isDown) const
271 GHOST_Buttons buttons;
272 // Get the state of all mouse buttons
273 GHOST_TSuccess success = getButtons(buttons);
275 // Isolate the state of the mouse button requested
276 isDown = buttons.get(mask);
282 GHOST_TSuccess GHOST_System::init()
284 m_timerManager = new GHOST_TimerManager ();
285 m_windowManager = new GHOST_WindowManager ();
286 m_eventManager = new GHOST_EventManager ();
287 m_ndofManager = new GHOST_NDOFManager();
290 printf("ndof manager \n");
293 if (m_eventManager) {
294 m_eventManager->addConsumer(&m_eventPrinter);
296 #endif // GHOST_DEBUG
298 if (m_timerManager && m_windowManager && m_eventManager) {
299 return GHOST_kSuccess;
301 return GHOST_kFailure;
306 GHOST_TSuccess GHOST_System::exit()
308 if (getFullScreen()) {
311 if (m_displayManager) {
312 delete m_displayManager;
313 m_displayManager = 0;
315 if (m_windowManager) {
316 delete m_windowManager;
319 if (m_timerManager) {
320 delete m_timerManager;
323 if (m_eventManager) {
324 delete m_eventManager;
328 delete m_ndofManager;
331 return GHOST_kSuccess;
335 GHOST_TSuccess GHOST_System::createFullScreenWindow(GHOST_Window** window, const bool stereoVisual)
337 GHOST_TSuccess success;
338 GHOST_ASSERT(m_displayManager, "GHOST_System::createFullScreenWindow(): invalid display manager")
339 GHOST_DisplaySetting settings;
341 success = m_displayManager->getCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, settings);
343 //GHOST_PRINT("GHOST_System::createFullScreenWindow(): creating full-screen window\n");
344 *window = (GHOST_Window*)createWindow(
346 0, 0, settings.xPixels, settings.yPixels,
347 GHOST_kWindowStateFullScreen,
348 GHOST_kDrawingContextTypeOpenGL,
350 success = *window == 0 ? GHOST_kFailure : GHOST_kSuccess;