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