root/juggler/tags/1.0.7/Kernel/vjDisplayManager.cpp

Revision 8789, 11.4 kB (checked in by patrickh, 7 years ago)

Copyright update.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
2  *
3  * VR Juggler is (C) Copyright 1998, 1999, 2000, 2001, 2002
4  *   by Iowa State University
5  *
6  * Original Authors:
7  *   Allen Bierbaum, Christopher Just,
8  *   Patrick Hartling, Kevin Meinert,
9  *   Carolina Cruz-Neira, Albert Baker
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Library General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Library General Public License for more details.
20  *
21  * You should have received a copy of the GNU Library General Public
22  * License along with this library; if not, write to the
23  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24  * Boston, MA 02111-1307, USA.
25  *
26  * -----------------------------------------------------------------
27  * File:          $RCSfile$
28  * Date modified: $Date$
29  * Version:       $Revision$
30  * -----------------------------------------------------------------
31  *
32  *************** <auto-copyright.pl END do not edit this line> ***************/
33
34
35 #include <vjConfig.h>
36 #include <Kernel/vjDisplayManager.h>
37 #include <Kernel/vjDisplay.h>
38 #include <Kernel/vjSurfaceDisplay.h>
39 #include <Kernel/vjSimDisplay.h>
40 #include <Kernel/vjDrawManager.h>
41 #include <Kernel/vjKernel.h>
42 #include <Math/vjCoord.h>
43
44 //vjDisplayManager* vjDisplayManager::_instance = NULL;
45 vjSingletonImp(vjDisplayManager);
46
47 std::vector<vjDisplay*> vjDisplayManager::getAllDisplays()
48 {
49    std::vector<vjDisplay*> ret_val;
50    ret_val.insert(ret_val.end(), mActiveDisplays.begin(), mActiveDisplays.end());
51    ret_val.insert(ret_val.end(), mInactiveDisplays.begin(), mInactiveDisplays.end());
52    return ret_val;
53 }
54
55
56 void vjDisplayManager::setDrawManager(vjDrawManager* drawMgr)
57 {
58    vjDEBUG(vjDBG_DISP_MGR,3) << "vjDisplayManager: Setting draw manager.\n" << vjDEBUG_FLUSH;
59
60    // set the draw manager
61    mDrawManager = drawMgr;
62
63    // Alert the draw manager about all the active windows currently configured
64    if(mDrawManager != NULL)
65    {
66       for(unsigned int i=0;i<mActiveDisplays.size();i++)
67       {
68          mDrawManager->addDisplay(mActiveDisplays[i]);
69       }
70    }
71 }
72
73 //: Add the chunk to the configuration
74 //! PRE: configCanHandle(chunk) == true
75 bool vjDisplayManager::configAdd(vjConfigChunk* chunk)
76 {
77    vjASSERT(configCanHandle(chunk));
78
79    std::string chunk_type = (std::string)chunk->getType();
80
81    if(   (chunk_type == std::string("surfaceDisplay"))
82       || (chunk_type == std::string("simDisplay")) )
83    {
84       return configAddDisplay(chunk);
85    }
86    else if(chunk_type == std::string("displaySystem"))
87    {
88       // XXX: Put signal here to tell draw manager to lookup new stuff
89       mDisplaySystemChunk = chunk;     // Keep track of the display system chunk
90       return true;                     // We successfully configured.
91                                        // This tell processPending to add it to the active config
92    }
93    else
94    { return false; }
95 }
96
97 //: Remove the chunk from the current configuration
98 //! PRE: configCanHandle(chunk) == true
99 bool vjDisplayManager::configRemove(vjConfigChunk* chunk)
100 {
101    vjASSERT(configCanHandle(chunk));
102
103    std::string chunk_type = (std::string)chunk->getType();
104
105    if(  (chunk_type == std::string("surfaceDisplay"))
106      || (chunk_type == std::string("simDisplay")) )
107    {
108       return configRemoveDisplay(chunk);
109    }
110    else if(chunk_type == std::string("displaySystem"))
111    {
112       // XXX: Put signal here to tell draw manager to lookup new stuff
113       mDisplaySystemChunk = NULL;     // Keep track of the display system chunk
114       return true;                     // We successfully configured.
115                                        // This tell processPending to remove it to the active config
116    }
117    else
118    { return false; }
119
120 }
121
122
123 //: Is it a display chunk?
124 //! RETURNS: true - We have a display chunk
125 //+          false - We don't
126 bool vjDisplayManager::configCanHandle(vjConfigChunk* chunk)
127 {
128    return (    ((std::string)chunk->getType() == std::string("surfaceDisplay"))
129             || ((std::string)chunk->getType() == std::string("simDisplay"))
130             || ((std::string)chunk->getType() == std::string("displaySystem")) );
131 }
132
133
134
135
136 //: Add the chunk to the configuration
137 //! PRE: configCanHandle(chunk) == true
138 //! POST: (display of same name already loaded) ==> old display closed, new one opened
139 //+       (display is new) ==> (new display is added)
140 //+       draw manager is notified of the display change
141 bool vjDisplayManager::configAddDisplay(vjConfigChunk* chunk)
142 {
143    vjASSERT(configCanHandle(chunk));      // We must be able to handle it first of all
144
145    vjDEBUG_BEGIN(vjDBG_DISP_MGR,vjDBG_STATE_LVL) << "------- vjDisplayManager::configAddDisplay -------\n" << vjDEBUG_FLUSH;
146
147    // Find out if we already have a window of this name
148    // If so, then close it before we open a new one of the same name
149    // This basically allows re-configuration of a window
150    vjDisplay* cur_disp = findDisplayNamed(chunk->getProperty("name"));
151    if(cur_disp != NULL)                         // We have an old display
152    {
153       vjDEBUG(vjDBG_DISP_MGR,vjDBG_CONFIG_LVL) << "Removing old window: " << cur_disp->getName().c_str() << vjDEBUG_FLUSH;
154       closeDisplay(cur_disp,true);              // Close the display and notify the draw manager to close the window
155    }
156
157    // --- Add a display (of the correct type) ---- //
158    if((std::string)chunk->getType() == std::string("surfaceDisplay"))      // Surface DISPLAY
159    {
160       vjDisplay* newDisp = new vjSurfaceDisplay();    // Create display
161       newDisp->config(chunk);                         // Config it
162       addDisplay(newDisp, true);                            // Add it
163       vjDEBUG(vjDBG_DISP_MGR,vjDBG_STATE_LVL) << "Adding display: "
164                                                 << newDisp->getName().c_str()
165                                                 << std::endl << vjDEBUG_FLUSH;
166       vjDEBUG(vjDBG_DISP_MGR,vjDBG_STATE_LVL) << "Display: "  << newDisp
167                                              << std::endl << vjDEBUG_FLUSH;
168    }
169
170    if((std::string)chunk->getType() == std::string("simDisplay"))      // Surface DISPLAY
171    {
172       vjDisplay* newDisp = new vjSimDisplay();     // Create display
173       newDisp->config(chunk);                      // Config it
174       addDisplay(newDisp, true);                         // Add it
175       vjDEBUG(vjDBG_DISP_MGR,vjDBG_WARNING_LVL) << "Adding Display: "
176                                                 << newDisp->getName().c_str()
177                                                 << std::endl << vjDEBUG_FLUSH;
178       vjDEBUG(vjDBG_DISP_MGR,vjDBG_VERB_LVL) << "Display: " << newDisp
179                                              << std::endl << std::flush
180                                              << vjDEBUG_FLUSH;
181    }
182
183    vjDEBUG_END(vjDBG_DISP_MGR,vjDBG_STATE_LVL) << "------- vjDisplayManager::configAddDisplay Done. --------\n" << vjDEBUG_FLUSH;
184    return true;
185 }
186
187 //: Remove the chunk from the current configuration
188 //! PRE: configCanHandle(chunk) == true
189 //!RETURNS: success
190 bool vjDisplayManager::configRemoveDisplay(vjConfigChunk* chunk)
191 {
192    vjASSERT(configCanHandle(chunk));      // We must be able to handle it first of all
193
194    vjDEBUG_BEGIN(vjDBG_DISP_MGR,4) << "------- vjDisplayManager::configRemoveDisplay -------\n" << vjDEBUG_FLUSH;
195
196    bool success_flag(false);
197
198    if((std::string)chunk->getType() == std::string("surfaceDisplay") ||
199       (std::string)chunk->getType() == std::string("simDisplay"))      // It is a display
200    {
201       vjDisplay* remove_disp = findDisplayNamed(chunk->getProperty("name"));
202       if(remove_disp != NULL)
203       {
204          closeDisplay(remove_disp, true);                            // Remove it
205          success_flag = true;
206       }
207    }
208
209    vjDEBUG_END(vjDBG_DISP_MGR,4) << "------- vjDisplayManager::configRemoveDisplay done. --------\n" << vjDEBUG_FLUSH;
210    return success_flag;
211 }
212
213
214
215
216 // notifyDrawMgr = 0; Defaults to 0
217 int vjDisplayManager::addDisplay(vjDisplay* disp, bool notifyDrawMgr)
218 {
219    vjDEBUG(vjDBG_DISP_MGR,4) << "vjDisplayManager::addDisplay \n" << vjDEBUG_FLUSH;
220
221    // Test if active or not, to determine correct list
222    // The place it in the list
223    // --- Update Local Display structures
224    if(disp->isActive())
225       mActiveDisplays.push_back(disp);
226    else
227       mInactiveDisplays.push_back(disp);
228
229    // If we are supposed to notify about, and valid draw mgr, and disp is active
230    if((notifyDrawMgr) && (mDrawManager != NULL) && (disp->isActive()))
231       mDrawManager->addDisplay(disp);;    // Tell Draw Manager to add dislay;
232
233    return 1;
234 }
235
236 //: Close the given display
237 //! PRE: disp is a display we know about
238 //! POST: disp has been removed from the list of displays
239 //+   (notifyDrawMgr == true) && (drawMgr != NULL) && (disp is active)
240 //+   ==> Draw manager has been told to clode the window for the display
241 int vjDisplayManager::closeDisplay(vjDisplay* disp, bool notifyDrawMgr)
242 {
243    vjASSERT(isMemberDisplay(disp));       // Make sure that display actually exists
244
245    // Notify the draw manager to get rid of it
246    if((notifyDrawMgr) && (mDrawManager != NULL) && (disp->isActive()))
247       mDrawManager->removeDisplay(disp);
248
249    // Remove it from local data structures
250    unsigned int num_before_close = mActiveDisplays.size() + mInactiveDisplays.size();
251    mActiveDisplays.erase( std::remove(mActiveDisplays.begin(), mActiveDisplays.end(), disp),
252                           mActiveDisplays.end());
253    mInactiveDisplays.erase( std::remove(mInactiveDisplays.begin(), mInactiveDisplays.end(), disp),
254                             mInactiveDisplays.end());
255    vjASSERT(num_before_close == (1+mActiveDisplays.size() + mInactiveDisplays.size()));
256
257    // Delete the object
258    // XXX: Memory leak.  Can't delete display here because the draw manager
259    //      may need access to it when the draw manager closes the window up.
260    // ex: the glDrawManager window has ref to the display to get current user, etc.
261    //XXX//delete disp;
262
263    return 1;
264 }
265
266
267 // Is the display a member of the display manager
268 bool vjDisplayManager::isMemberDisplay(vjDisplay* disp)
269 {
270    std::vector<vjDisplay*>::iterator i;
271
272    i = std::find(mActiveDisplays.begin(),mActiveDisplays.end(),disp);
273    if(i != mActiveDisplays.end())
274       return true;
275
276    i = std::find(mInactiveDisplays.begin(),mInactiveDisplays.end(),disp);
277    if(i != mInactiveDisplays.end())
278       return true;
279
280    return false;  // Didn't find any
281 }
282
283 //: Find a display given the display name
284 //! RETURNS: NULL - not found
285 vjDisplay* vjDisplayManager::findDisplayNamed(std::string name)
286 {
287    std::vector<vjDisplay*>::iterator i;
288
289    for(i = mActiveDisplays.begin();i!=mActiveDisplays.end();i++)
290       if((*i)->getName() == name)
291          return (*i);
292
293    for(i = mInactiveDisplays.begin();i!=mInactiveDisplays.end();i++)
294       if((*i)->getName() == name)
295          return (*i);
296
297    return NULL;  // Didn't find any
298 }
299
300
301 void vjDisplayManager::updateProjections()
302 {
303    // for (all displays) update the projections
304    for (std::vector<vjDisplay*>::iterator i = mActiveDisplays.begin(); i != mActiveDisplays.end(); i++)
305       (*i)->updateProjections();
306
307    for (std::vector<vjDisplay*>::iterator j = mInactiveDisplays.begin(); j != mInactiveDisplays.end(); j++)
308       (*j)->updateProjections();
309
310    if(mDrawManager != NULL)
311       mDrawManager->updateProjections();
312 }
313
314
Note: See TracBrowser for help on using the browser.