Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

DisplayManager.cpp

Go to the documentation of this file.
00001 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
00002  *
00003  * VR Juggler is (C) Copyright 1998-2003 by Iowa State University
00004  *
00005  * Original Authors:
00006  *   Allen Bierbaum, Christopher Just,
00007  *   Patrick Hartling, Kevin Meinert,
00008  *   Carolina Cruz-Neira, Albert Baker
00009  *
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Library General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2 of the License, or (at your option) any later version.
00014  *
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Library General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Library General Public
00021  * License along with this library; if not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00023  * Boston, MA 02111-1307, USA.
00024  *
00025  * -----------------------------------------------------------------
00026  * File:          $RCSfile: DisplayManager.cpp,v $
00027  * Date modified: $Date: 2003/08/21 05:25:45 $
00028  * Version:       $Revision: 1.61 $
00029  * -----------------------------------------------------------------
00030  *
00031  *************** <auto-copyright.pl END do not edit this line> ***************/
00032 
00033 #include <vrj/vrjConfig.h>
00034 #include <boost/concept_check.hpp>
00035 #include <jccl/Config/ConfigElement.h>
00036 #include <jccl/RTRC/ConfigManager.h>
00037 #include <vrj/Display/Display.h>
00038 #include <vrj/Display/SurfaceViewport.h>
00039 #include <vrj/Display/SimViewport.h>
00040 #include <vrj/Draw/DrawManager.h>
00041 #include <vrj/Kernel/Kernel.h>
00042 #include <vrj/Display/DisplayManager.h>
00043 
00044 
00045 namespace vrj
00046 {
00047 
00048 //vjDisplayManager* DisplayManager::_instance = NULL;
00049 vprSingletonImp(DisplayManager);
00050 
00051 std::vector<Display*> DisplayManager::getAllDisplays()
00052 {
00053    std::vector<Display*> ret_val;
00054    ret_val.insert(ret_val.end(), mActiveDisplays.begin(), mActiveDisplays.end());
00055    ret_val.insert(ret_val.end(), mInactiveDisplays.begin(), mInactiveDisplays.end());
00056    return ret_val;
00057 }
00058 
00059 jccl::ConfigElementPtr DisplayManager::getDisplaySystemElement()
00060 {
00061    if ( mDisplaySystemElement.get() == NULL )
00062    {
00063       jccl::ConfigManager* cfg_mgr = jccl::ConfigManager::instance();
00064 
00065       cfg_mgr->lockActive();
00066       {
00067          std::vector<jccl::ConfigElementPtr>::iterator i;
00068          for(i=cfg_mgr->getActiveBegin(); i != cfg_mgr->getActiveEnd();++i)
00069          {
00070             if( (*i)->getID() == std::string("display_system") )
00071             {
00072                mDisplaySystemElement = *i;
00073                break;         // This guarantees that we get the first displaySystem element.
00074             }
00075          }
00076       }
00077       cfg_mgr->unlockActive();
00078 
00079 //      vprASSERT(mDisplaySystemElement.get() != NULL && "No Display Manager config element found!");
00080    }
00081 
00082    return mDisplaySystemElement;
00083 }
00084 
00085 void DisplayManager::setDrawManager(DrawManager* drawMgr)
00086 {
00087    vprDEBUG(vrjDBG_DISP_MGR, vprDBG_STATE_LVL) << "vjDisplayManager: Setting draw manager.\n" << vprDEBUG_FLUSH;
00088 
00089    // set the draw manager
00090    mDrawManager = drawMgr;
00091 
00092    // Alert the draw manager about all the active windows currently configured
00093    if(mDrawManager != NULL)
00094    {
00095       for(unsigned int i=0;i<mActiveDisplays.size();i++)
00096       {
00097          mDrawManager->addDisplay(mActiveDisplays[i]);
00098       }
00099    }
00100 }
00101 
00106 bool DisplayManager::configAdd(jccl::ConfigElementPtr element)
00107 {
00108    vprASSERT(configCanHandle(element));
00109 
00110    const std::string element_type(element->getID());
00111 
00112    if(   (element_type == std::string("surfaceDisplay"))
00113       || (element_type == std::string("simDisplay")) )
00114    {
00115       vprDEBUG(vprDBG_ALL, vprDBG_CRITICAL_LVL)
00116          << "Element of type: " << element_type
00117          << " is no longer supported.  Use display_window type instead.\n"
00118          << vprDEBUG_FLUSH;
00119       return false;
00120    }
00121    else if( (element_type == std::string("display_window")))
00122    {
00123       return configAddDisplay(element);
00124    }
00125    else if(element_type == std::string("display_system"))
00126    {
00127       // XXX: Put signal here to tell draw manager to lookup new stuff
00128       mDisplaySystemElement = element; // Keep track of the display system element
00129       return true;                     // We successfully configured.
00130                                        // This tell processPending to add it to the active config
00131    }
00132 
00133    return false;
00134 }
00135 
00140 bool DisplayManager::configRemove(jccl::ConfigElementPtr element)
00141 {
00142    vprASSERT(configCanHandle(element));
00143 
00144    const std::string element_type(element->getID());
00145 
00146    if(  (element_type == std::string("surfaceDisplay"))
00147      || (element_type == std::string("simDisplay")) )
00148    {
00149       vprDEBUG(vprDBG_ALL, vprDBG_CRITICAL_LVL)
00150          << "Element of type: " << element_type
00151          << " is no longer supported.  Use display_window type instead.\n"
00152          << vprDEBUG_FLUSH;
00153       return false;
00154    }
00155    else if(element_type == std::string("display_window"))
00156    {
00157       return configRemoveDisplay(element);
00158    }
00159    else if(element_type == std::string("display_system"))
00160    {
00161       // XXX: Put signal here to tell draw manager to lookup new stuff
00162       mDisplaySystemElement.reset();   // Keep track of the display system element
00163       return true;                     // We successfully configured.
00164                                        // This tell processPending to remove it to the active config
00165    }
00166    else
00167    { return false; }
00168 
00169 }
00170 
00171 
00177 bool DisplayManager::configCanHandle(jccl::ConfigElementPtr element)
00178 {
00179    return (    (element->getID() == std::string("surfaceDisplay"))
00180             || (element->getID() == std::string("simDisplay"))
00181             || (element->getID() == std::string("display_system"))
00182             || (element->getID() == std::string("display_window"))
00183            );
00184 }
00185 
00194 bool DisplayManager::configAddDisplay(jccl::ConfigElementPtr element)
00195 {
00196    vprASSERT(configCanHandle(element)); // We must be able to handle it first of all
00197 
00198    vprDEBUG_BEGIN(vrjDBG_DISP_MGR,vprDBG_STATE_LVL) << "------- DisplayManager::configAddDisplay -------\n" << vprDEBUG_FLUSH;
00199 
00200    // Find out if we already have a window of this name
00201    // If so, then close it before we open a new one of the same name
00202    // This basically allows re-configuration of a window
00203    Display* cur_disp = findDisplayNamed(element->getName());
00204    if(cur_disp != NULL)                         // We have an old display
00205    {
00206       vprDEBUG(vrjDBG_DISP_MGR,vprDBG_CONFIG_LVL) << "Removing old window: " << cur_disp->getName().c_str() << vprDEBUG_FLUSH;
00207       closeDisplay(cur_disp,true);              // Close the display and notify the draw manager to close the window
00208    }
00209 
00210    // --- Add a display (of the correct type) ---- //
00211    if(element->getID() == std::string("display_window"))       // Display window
00212    {
00213       Display* newDisp = new Display();        // Create the display
00214       newDisp->config(element);
00215       addDisplay(newDisp,true);                    // Add it
00216       vprDEBUG(vrjDBG_DISP_MGR,vprDBG_STATE_LVL) << "Adding display: " << newDisp->getName().c_str() << std::endl << vprDEBUG_FLUSH;
00217       vprDEBUG(vrjDBG_DISP_MGR,vprDBG_STATE_LVL) << "Display: "  << newDisp << std::endl << vprDEBUG_FLUSH;
00218    }
00219 
00220    vprDEBUG_END(vrjDBG_DISP_MGR,vprDBG_STATE_LVL) << "------- DisplayManager::configAddDisplay Done. --------\n" << vprDEBUG_FLUSH;
00221    return true;
00222 }
00223 
00230 bool DisplayManager::configRemoveDisplay(jccl::ConfigElementPtr element)
00231 {
00232    vprASSERT(configCanHandle(element)); // We must be able to handle it first of all
00233 
00234    vprDEBUG_BEGIN(vrjDBG_DISP_MGR,vprDBG_STATE_LVL) << "------- DisplayManager::configRemoveDisplay -------\n" << vprDEBUG_FLUSH;
00235 
00236    bool success_flag(false);
00237 
00238    if(element->getID() == std::string("display_window"))      // It is a display
00239    {
00240       Display* remove_disp = findDisplayNamed(element->getName());
00241       if(remove_disp != NULL)
00242       {
00243          closeDisplay(remove_disp, true);                            // Remove it
00244          success_flag = true;
00245       }
00246    }
00247 
00248    vprDEBUG_END(vrjDBG_DISP_MGR,vprDBG_STATE_LVL) << "------- DisplayManager::configRemoveDisplay done. --------\n" << vprDEBUG_FLUSH;
00249    return success_flag;
00250 }
00251 
00252 
00253 
00254 
00255 // notifyDrawMgr = 0; Defaults to 0
00256 int DisplayManager::addDisplay(Display* disp, bool notifyDrawMgr)
00257 {
00258    vprDEBUG(vrjDBG_DISP_MGR, vprDBG_VERB_LVL) << "vjDisplayManager::addDisplay \n" << vprDEBUG_FLUSH;
00259 
00260    // Test if active or not, to determine correct list
00261    // The place it in the list
00262    // --- Update Local Display structures
00263    if(disp->isActive())
00264       mActiveDisplays.push_back(disp);
00265    else
00266       mInactiveDisplays.push_back(disp);
00267 
00268    // If we are supposed to notify about, and valid draw mgr, and disp is active
00269    if((notifyDrawMgr) && (mDrawManager != NULL) && (disp->isActive()))
00270       mDrawManager->addDisplay(disp);;    // Tell Draw Manager to add dislay;
00271 
00272    return 1;
00273 }
00274 
00283 int DisplayManager::closeDisplay(Display* disp, bool notifyDrawMgr)
00284 {
00285    vprASSERT(isMemberDisplay(disp));       // Make sure that display actually exists
00286 
00287    vprDEBUG(vrjDBG_DISP_MGR,vprDBG_STATE_LVL) << "closeDisplay: Closing display named: " << disp->getName() << std::endl<< vprDEBUG_FLUSH;
00288 
00289    // Notify the draw manager to get rid of it
00290    // Note: if it is not active, then the draw manager doesn't know about it
00291    if((notifyDrawMgr) && (mDrawManager != NULL) && (disp->isActive()))
00292       mDrawManager->removeDisplay(disp);
00293 
00294    // Remove it from local data structures
00295    unsigned int num_before_close = mActiveDisplays.size() + mInactiveDisplays.size();
00296    mActiveDisplays.erase( std::remove(mActiveDisplays.begin(), mActiveDisplays.end(), disp),
00297                           mActiveDisplays.end());
00298    mInactiveDisplays.erase( std::remove(mInactiveDisplays.begin(), mInactiveDisplays.end(), disp),
00299                             mInactiveDisplays.end());
00300    vprASSERT(num_before_close == (1+mActiveDisplays.size() + mInactiveDisplays.size()));
00301    boost::ignore_unused_variable_warning(num_before_close);
00302 
00303    // Delete the object
00304    // XXX: Memory leak.  Can't delete display here because the draw manager
00305    //      may need access to it when the draw manager closes the window up.
00306    // ex: the glDrawManager window has ref to the display to get current user, etc.
00307    //XXX//delete disp;
00308 
00309    return 1;
00310 }
00311 
00312 
00313 // Is the display a member of the display manager
00314 bool DisplayManager::isMemberDisplay(Display* disp)
00315 {
00316    std::vector<Display*>::iterator i;
00317 
00318    i = std::find(mActiveDisplays.begin(),mActiveDisplays.end(),disp);
00319    if(i != mActiveDisplays.end())
00320       return true;
00321 
00322    i = std::find(mInactiveDisplays.begin(),mInactiveDisplays.end(),disp);
00323    if(i != mInactiveDisplays.end())
00324       return true;
00325 
00326    return false;  // Didn't find any
00327 }
00328 
00333 Display* DisplayManager::findDisplayNamed(std::string name)
00334 {
00335    std::vector<Display*>::iterator i;
00336 
00337    for(i = mActiveDisplays.begin();i!=mActiveDisplays.end();i++)
00338       if((*i)->getName() == name)
00339          return (*i);
00340 
00341    for(i = mInactiveDisplays.begin();i!=mInactiveDisplays.end();i++)
00342       if((*i)->getName() == name)
00343          return (*i);
00344 
00345    return NULL;  // Didn't find any
00346 }
00347 
00348 
00349 void DisplayManager::updateProjections(const float scaleFactor)
00350 {
00351    // for (all displays) update the projections
00352    for (std::vector<Display*>::iterator i = mActiveDisplays.begin(); i != mActiveDisplays.end(); i++)
00353       (*i)->updateProjections(scaleFactor);
00354 
00355    for (std::vector<Display*>::iterator j = mInactiveDisplays.begin(); j != mInactiveDisplays.end(); j++)
00356       (*j)->updateProjections(scaleFactor);
00357 }
00358 
00359 
00360 };

Generated on Sun May 2 15:10:16 2004 for VR Juggler by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002