Line data Source code
1 :
2 : /* Copyright (c) 2011-2013, Stefan Eilemann <eile@eyescale.ch>
3 : * 2012, Daniel Nachbaur <danielnachbaur@gmail.com>
4 : *
5 : * This library is free software; you can redistribute it and/or modify it under
6 : * the terms of the GNU Lesser General Public License version 2.1 as published
7 : * by the Free Software Foundation.
8 : *
9 : * This library is distributed in the hope that it will be useful, but WITHOUT
10 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 : * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12 : * details.
13 : *
14 : * You should have received a copy of the GNU Lesser General Public License
15 : * along with this library; if not, write to the Free Software Foundation, Inc.,
16 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 : */
18 :
19 : #include "masterConfig.h"
20 :
21 : #include "objectMap.h"
22 : #include "view.h"
23 :
24 : #include <seq/application.h>
25 : #ifndef EQ_2_0_API
26 : # include <eq/configEvent.h>
27 : #endif
28 : #include <eq/fabric/configVisitor.h>
29 : #include <eq/fabric/event.h>
30 : #include <eq/eventICommand.h>
31 :
32 : namespace seq
33 : {
34 : namespace detail
35 : {
36 0 : MasterConfig::MasterConfig( eq::ServerPtr parent )
37 : : Config( parent )
38 0 : , _redraw( false )
39 0 : {}
40 :
41 0 : MasterConfig::~MasterConfig()
42 0 : {}
43 :
44 0 : bool MasterConfig::init()
45 : {
46 0 : LBASSERT( !_objects );
47 0 : _objects = new ObjectMap( *this, *getApplication( ));
48 :
49 0 : co::Object* initData = getInitData();
50 0 : if( initData )
51 0 : LBCHECK( _objects->register_( initData, OBJECTTYPE_INITDATA ));
52 0 : _objects->setInitData( initData );
53 :
54 0 : LBCHECK( registerObject( _objects ));
55 :
56 0 : if( !eq::Config::init( _objects->getID( )))
57 : {
58 0 : LBWARN << "Error during initialization" << std::endl;
59 0 : exit();
60 0 : return false;
61 : }
62 :
63 0 : _redraw = true;
64 0 : return true;
65 : }
66 :
67 0 : bool MasterConfig::exit()
68 : {
69 0 : const bool retVal = eq::Config::exit();
70 :
71 0 : if( _objects )
72 0 : deregisterObject( _objects );
73 0 : _objects->clear();
74 0 : delete _objects;
75 0 : _objects = 0;
76 :
77 0 : return retVal;
78 : }
79 :
80 0 : bool MasterConfig::run( co::Object* frameData )
81 : {
82 0 : LBASSERT( _objects );
83 0 : if( frameData )
84 0 : LBCHECK( _objects->register_( frameData, OBJECTTYPE_FRAMEDATA ));
85 0 : _objects->setFrameData( frameData );
86 :
87 0 : seq::Application* const app = getApplication();
88 0 : while( isRunning( ))
89 : {
90 0 : startFrame();
91 0 : finishFrame();
92 :
93 0 : while( !needRedraw( )) // wait for an event requiring redraw
94 : {
95 0 : if( app->hasCommands( )) // execute non-critical pending commands
96 : {
97 0 : app->processCommand();
98 0 : handleEvents(); // non-blocking
99 : }
100 : else // no pending commands, block on user event
101 : {
102 0 : const eq::EventICommand& event = getNextEvent();
103 0 : if( !handleEvent( event ))
104 0 : LBVERB << "Unhandled " << event << std::endl;
105 : }
106 : }
107 0 : handleEvents(); // process all pending events
108 : }
109 0 : finishAllFrames();
110 0 : return true;
111 : }
112 :
113 0 : uint32_t MasterConfig::startFrame()
114 : {
115 0 : _redraw = false;
116 0 : return eq::Config::startFrame( _objects->commit( ));
117 : }
118 :
119 : namespace
120 : {
121 : class ViewUpdateVisitor : public eq::ConfigVisitor
122 : {
123 : public:
124 0 : explicit ViewUpdateVisitor( bool &redraw ) : _redraw( redraw ) {}
125 0 : virtual~ ViewUpdateVisitor() {}
126 :
127 0 : virtual eq::VisitorResult visit( eq::View* v )
128 : {
129 0 : View* view = static_cast< View* >( v );
130 0 : if( view->updateData( ))
131 : {
132 0 : LBVERB << "Redraw: new view data" << std::endl;
133 0 : _redraw = true;
134 : }
135 0 : return eq::TRAVERSE_CONTINUE;
136 : }
137 :
138 : private:
139 : bool& _redraw;
140 : };
141 : }
142 : #ifndef EQ_2_0_API
143 0 : bool MasterConfig::handleEvent( const eq::ConfigEvent* event )
144 : {
145 0 : switch( event->data.type )
146 : {
147 : case eq::Event::CHANNEL_POINTER_BUTTON_PRESS:
148 0 : _currentViewID = event->data.context.view.identifier;
149 0 : return true;
150 :
151 : case eq::Event::KEY_PRESS:
152 : case eq::Event::KEY_RELEASE:
153 0 : if( Config::handleEvent( event ))
154 : {
155 0 : _redraw = true;
156 0 : LBVERB << "Redraw: requested by eq::Config" << std::endl;
157 : }
158 : // no break;
159 : case eq::Event::CHANNEL_POINTER_BUTTON_RELEASE:
160 : case eq::Event::CHANNEL_POINTER_MOTION:
161 : case eq::Event::CHANNEL_POINTER_WHEEL:
162 : case eq::Event::MAGELLAN_AXIS:
163 : {
164 0 : if( _currentViewID == 0 )
165 0 : return false;
166 :
167 0 : View* view = static_cast<View*>( find<eq::View>( _currentViewID ));
168 0 : if( view->handleEvent( event ))
169 : {
170 0 : _redraw = true;
171 0 : LBVERB << "Redraw: requested by view event handler" << std::endl;
172 : }
173 0 : return true;
174 : }
175 :
176 : case eq::Event::STATISTIC:
177 : {
178 0 : Config::handleEvent( event );
179 0 : if( event->data.statistic.type != eq::Statistic::CONFIG_FINISH_FRAME )
180 0 : return false;
181 :
182 0 : ViewUpdateVisitor viewUpdate( _redraw );
183 0 : accept( viewUpdate );
184 0 : return _redraw;
185 : }
186 :
187 : case eq::Event::WINDOW_EXPOSE:
188 : case eq::Event::WINDOW_RESIZE:
189 : case eq::Event::WINDOW_CLOSE:
190 : case eq::Event::VIEW_RESIZE:
191 0 : _redraw = true;
192 0 : LBVERB << "Redraw: window change" << std::endl;
193 0 : break;
194 :
195 : case EVENT_REDRAW:
196 0 : _redraw = true;
197 0 : LBVERB << "Redraw request" << std::endl;
198 0 : break;
199 :
200 : default:
201 0 : break;
202 : }
203 :
204 0 : if( eq::Config::handleEvent( event ))
205 : {
206 0 : _redraw = true;
207 0 : LBVERB << "Redraw: requested by config event handler" << std::endl;
208 : }
209 0 : return _redraw;
210 : }
211 : #endif
212 :
213 0 : bool MasterConfig::handleEvent( eq::EventICommand command )
214 : {
215 0 : switch( command.getEventType( ))
216 : {
217 : case eq::Event::CHANNEL_POINTER_BUTTON_PRESS:
218 : {
219 0 : const eq::Event& event = command.read< eq::Event >();
220 0 : _currentViewID = event.context.view.identifier;
221 0 : return true;
222 : }
223 :
224 : case eq::Event::KEY_PRESS:
225 : case eq::Event::KEY_RELEASE:
226 0 : if( Config::handleEvent( command ))
227 : {
228 0 : _redraw = true;
229 0 : LBVERB << "Redraw: requested by eq::Config" << std::endl;
230 : }
231 : // no break;
232 : case eq::Event::CHANNEL_POINTER_BUTTON_RELEASE:
233 : case eq::Event::CHANNEL_POINTER_MOTION:
234 : case eq::Event::CHANNEL_POINTER_WHEEL:
235 : case eq::Event::MAGELLAN_AXIS:
236 : {
237 0 : if( _currentViewID == 0 )
238 0 : return false;
239 :
240 0 : View* view = static_cast<View*>( find<eq::View>( _currentViewID ));
241 0 : if( view->handleEvent( command ))
242 : {
243 0 : _redraw = true;
244 0 : LBVERB << "Redraw: requested by view event handler" << std::endl;
245 : }
246 0 : return true;
247 : }
248 :
249 : case eq::Event::STATISTIC:
250 : {
251 0 : Config::handleEvent( command );
252 0 : const eq::Event& event = command.read< eq::Event >();
253 0 : if( event.statistic.type != eq::Statistic::CONFIG_FINISH_FRAME )
254 0 : return false;
255 :
256 0 : ViewUpdateVisitor viewUpdate( _redraw );
257 0 : accept( viewUpdate );
258 0 : return _redraw;
259 : }
260 :
261 : case eq::Event::WINDOW_EXPOSE:
262 : case eq::Event::WINDOW_RESIZE:
263 : case eq::Event::WINDOW_CLOSE:
264 : case eq::Event::VIEW_RESIZE:
265 0 : _redraw = true;
266 0 : LBVERB << "Redraw: window change" << std::endl;
267 0 : break;
268 :
269 : case EVENT_REDRAW:
270 0 : _redraw = true;
271 0 : LBVERB << "Redraw request" << std::endl;
272 0 : break;
273 :
274 : default:
275 0 : break;
276 : }
277 :
278 0 : if( eq::Config::handleEvent( command ))
279 : {
280 0 : _redraw = true;
281 0 : LBVERB << "Redraw: requested by config event handler" << std::endl;
282 : }
283 0 : return _redraw;
284 : }
285 :
286 : }
287 42 : }
|