Blender V2.61 - r43446

GHOST_System.cpp

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00019  * All rights reserved.
00020  *
00021  * The Original Code is: all of this file.
00022  *
00023  * Contributor(s): none yet.
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  */
00027 
00034 #include "GHOST_System.h"
00035 
00036 #include <time.h>
00037 #include <stdio.h> /* just for printf */
00038 
00039 #include "GHOST_DisplayManager.h"
00040 #include "GHOST_EventManager.h"
00041 #include "GHOST_NDOFManager.h"
00042 #include "GHOST_TimerTask.h"
00043 #include "GHOST_TimerManager.h"
00044 #include "GHOST_WindowManager.h"
00045 
00046 
00047 GHOST_System::GHOST_System()
00048     : m_displayManager(0),
00049       m_timerManager(0),
00050       m_windowManager(0),
00051       m_eventManager(0)
00052 #ifdef WITH_INPUT_NDOF
00053       , m_ndofManager(0)
00054 #endif
00055 {
00056 }
00057 
00058 
00059 GHOST_System::~GHOST_System()
00060 {
00061     exit();
00062 }
00063 
00064 
00065 GHOST_TUns64 GHOST_System::getMilliSeconds() const
00066 {
00067     GHOST_TUns64 millis = ::clock();
00068     if (CLOCKS_PER_SEC != 1000) {
00069         millis *= 1000;
00070         millis /= CLOCKS_PER_SEC;
00071     }
00072     return millis;
00073 }
00074 
00075 
00076 GHOST_ITimerTask* GHOST_System::installTimer(GHOST_TUns64 delay,
00077                                              GHOST_TUns64 interval,
00078                                              GHOST_TimerProcPtr timerProc,
00079                                              GHOST_TUserDataPtr userData)
00080 {
00081     GHOST_TUns64 millis = getMilliSeconds();
00082     GHOST_TimerTask* timer = new GHOST_TimerTask(millis+delay, interval, timerProc, userData);
00083     if (timer) {
00084         if (m_timerManager->addTimer(timer) == GHOST_kSuccess) {
00085             // Check to see whether we need to fire the timer right away
00086             m_timerManager->fireTimers(millis);
00087         }
00088         else {
00089             delete timer;
00090             timer = 0;
00091         }
00092     }
00093     return timer;
00094 }
00095 
00096 
00097 GHOST_TSuccess GHOST_System::removeTimer(GHOST_ITimerTask* timerTask)
00098 {
00099     GHOST_TSuccess success = GHOST_kFailure;
00100     if (timerTask) {
00101         success = m_timerManager->removeTimer((GHOST_TimerTask*)timerTask);
00102     }
00103     return success;
00104 }
00105 
00106 
00107 GHOST_TSuccess GHOST_System::disposeWindow(GHOST_IWindow* window)
00108 {
00109     GHOST_TSuccess success;
00110 
00111     /*
00112      * Remove all pending events for the window.
00113      */ 
00114     if (m_windowManager->getWindowFound(window)) {
00115         m_eventManager->removeWindowEvents(window);
00116     }
00117     if (window == m_windowManager->getFullScreenWindow()) {
00118         success = endFullScreen();
00119     }
00120     else {
00121         if (m_windowManager->getWindowFound(window)) {
00122             success = m_windowManager->removeWindow(window);
00123             if (success) {
00124                 delete window;
00125             }
00126         }
00127         else {
00128             success = GHOST_kFailure;
00129         }
00130     }
00131     return success;
00132 }
00133 
00134 
00135 bool GHOST_System::validWindow(GHOST_IWindow* window)
00136 {
00137     return m_windowManager->getWindowFound(window);
00138 }
00139 
00140 
00141 GHOST_TSuccess GHOST_System::beginFullScreen(const GHOST_DisplaySetting& setting, GHOST_IWindow** window,
00142                                              const bool stereoVisual, const GHOST_TUns16 numOfAASamples)
00143 {
00144     GHOST_TSuccess success = GHOST_kFailure;
00145     GHOST_ASSERT(m_windowManager, "GHOST_System::beginFullScreen(): invalid window manager")
00146     if (m_displayManager) {
00147         if (!m_windowManager->getFullScreen()) {
00148             m_displayManager->getCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, m_preFullScreenSetting);
00149 
00150             //GHOST_PRINT("GHOST_System::beginFullScreen(): activating new display settings\n");
00151             success = m_displayManager->setCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, setting);
00152             if (success == GHOST_kSuccess) {
00153                 //GHOST_PRINT("GHOST_System::beginFullScreen(): creating full-screen window\n");
00154                 success = createFullScreenWindow((GHOST_Window**)window, stereoVisual, numOfAASamples);
00155                 if (success == GHOST_kSuccess) {
00156                     m_windowManager->beginFullScreen(*window, stereoVisual);
00157                 }
00158                 else {
00159                     m_displayManager->setCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, m_preFullScreenSetting);
00160                 }
00161             }
00162         }
00163     }
00164     if (success == GHOST_kFailure) {
00165         GHOST_PRINT("GHOST_System::beginFullScreen(): could not enter full-screen mode\n");
00166     }
00167     return success;
00168 }
00169 
00170 
00171 GHOST_TSuccess GHOST_System::endFullScreen(void)
00172 {
00173     GHOST_TSuccess success = GHOST_kFailure;
00174     GHOST_ASSERT(m_windowManager, "GHOST_System::endFullScreen(): invalid window manager")
00175     if (m_windowManager->getFullScreen()) {
00176         //GHOST_IWindow* window = m_windowManager->getFullScreenWindow();
00177         //GHOST_PRINT("GHOST_System::endFullScreen(): leaving window manager full-screen mode\n");
00178         success = m_windowManager->endFullScreen();
00179         GHOST_ASSERT(m_displayManager, "GHOST_System::endFullScreen(): invalid display manager")
00180         //GHOST_PRINT("GHOST_System::endFullScreen(): leaving full-screen mode\n");
00181         success = m_displayManager->setCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, m_preFullScreenSetting);
00182     }
00183     else {
00184         success = GHOST_kFailure;
00185     }
00186     return success;
00187 }
00188 
00189 
00190 bool GHOST_System::getFullScreen(void)
00191 {
00192     bool fullScreen;
00193     if (m_windowManager) {
00194         fullScreen = m_windowManager->getFullScreen();
00195     }
00196     else {
00197         fullScreen = false;
00198     }
00199     return fullScreen;
00200 }
00201 
00202 
00203 bool GHOST_System::dispatchEvents()
00204 {
00205     bool handled = false;
00206 
00207 #ifdef WITH_INPUT_NDOF
00208     // NDOF Motion event is sent only once per dispatch, so do it now:
00209     if (m_ndofManager) {
00210         handled |= m_ndofManager->sendMotionEvent();
00211     }
00212 #endif
00213 
00214     if (m_eventManager) {
00215         handled |= m_eventManager->dispatchEvents();
00216     }
00217 
00218     m_timerManager->fireTimers(getMilliSeconds());
00219     return handled;
00220 }
00221 
00222 
00223 GHOST_TSuccess GHOST_System::addEventConsumer(GHOST_IEventConsumer* consumer)
00224 {
00225     GHOST_TSuccess success;
00226     if (m_eventManager) {
00227         success = m_eventManager->addConsumer(consumer);
00228     }
00229     else {
00230         success = GHOST_kFailure;
00231     }
00232     return success;
00233 }
00234 
00235 GHOST_TSuccess GHOST_System::removeEventConsumer(GHOST_IEventConsumer* consumer)
00236 {
00237     GHOST_TSuccess success;
00238     if (m_eventManager) {
00239         success = m_eventManager->removeConsumer(consumer);
00240     }
00241     else {
00242         success = GHOST_kFailure;
00243     }
00244     return success;
00245 }
00246 
00247 GHOST_TSuccess GHOST_System::pushEvent(GHOST_IEvent* event)
00248 {
00249     GHOST_TSuccess success;
00250     if (m_eventManager) {
00251         success = m_eventManager->pushEvent(event);
00252     }
00253     else {
00254         success = GHOST_kFailure;
00255     }
00256     return success;
00257 }
00258 
00259 GHOST_TSuccess GHOST_System::getModifierKeyState(GHOST_TModifierKeyMask mask, bool& isDown) const
00260 {
00261     GHOST_ModifierKeys keys;
00262     // Get the state of all modifier keys 
00263     GHOST_TSuccess success = getModifierKeys(keys);
00264     if (success) {
00265         // Isolate the state of the key requested
00266         isDown = keys.get(mask);
00267     }
00268     return success;
00269 }
00270 
00271 
00272 GHOST_TSuccess GHOST_System::getButtonState(GHOST_TButtonMask mask, bool& isDown) const
00273 {
00274     GHOST_Buttons buttons;
00275     // Get the state of all mouse buttons
00276     GHOST_TSuccess success = getButtons(buttons);
00277     if (success) {
00278         // Isolate the state of the mouse button requested
00279         isDown = buttons.get(mask);
00280     }
00281     return success;
00282 }
00283 
00284 GHOST_TSuccess GHOST_System::init()
00285 {
00286     m_timerManager = new GHOST_TimerManager ();
00287     m_windowManager = new GHOST_WindowManager ();
00288     m_eventManager = new GHOST_EventManager ();
00289     
00290 #ifdef GHOST_DEBUG
00291     if (m_eventManager) {
00292         m_eventPrinter = new GHOST_EventPrinter();
00293         m_eventManager->addConsumer(m_eventPrinter);
00294     }
00295 #endif // GHOST_DEBUG
00296 
00297     if (m_timerManager && m_windowManager && m_eventManager) {
00298         return GHOST_kSuccess;
00299     } else {
00300         return GHOST_kFailure;
00301     }
00302 }
00303 
00304 
00305 GHOST_TSuccess GHOST_System::exit()
00306 {
00307     if (getFullScreen()) {
00308         endFullScreen();
00309     }
00310     if (m_displayManager) {
00311         delete m_displayManager;
00312         m_displayManager = 0;
00313     }
00314     if (m_windowManager) {
00315         delete m_windowManager;
00316         m_windowManager = 0;
00317     }
00318     if (m_timerManager) {
00319         delete m_timerManager;
00320         m_timerManager = 0;
00321     }
00322     if (m_eventManager) {
00323         delete m_eventManager;
00324         m_eventManager = 0;
00325     }
00326 #ifdef WITH_INPUT_NDOF
00327     if (m_ndofManager) {
00328         delete m_ndofManager;
00329         m_ndofManager = 0;
00330     }
00331 #endif
00332     return GHOST_kSuccess;
00333 }
00334 
00335 
00336 GHOST_TSuccess GHOST_System::createFullScreenWindow(GHOST_Window** window, const bool stereoVisual, const GHOST_TUns16 numOfAASamples)
00337 {
00338     GHOST_TSuccess success;
00339     GHOST_ASSERT(m_displayManager, "GHOST_System::createFullScreenWindow(): invalid display manager")
00340     GHOST_DisplaySetting settings;
00341 
00342     success = m_displayManager->getCurrentDisplaySetting(GHOST_DisplayManager::kMainDisplay, settings);
00343     if (success) {
00344         //GHOST_PRINT("GHOST_System::createFullScreenWindow(): creating full-screen window\n");
00345         *window = (GHOST_Window*)createWindow(
00346                     STR_String (""),
00347                     0, 0, settings.xPixels, settings.yPixels,
00348                     GHOST_kWindowStateFullScreen,
00349                     GHOST_kDrawingContextTypeOpenGL,
00350                     stereoVisual,
00351                     numOfAASamples);
00352         success = *window == 0 ? GHOST_kFailure : GHOST_kSuccess;
00353     }
00354     return success;
00355 }