1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <tools/diagnose_ex.h>
23 #include <comphelper/anytostring.hxx>
24 #include <cppuhelper/exc_hlp.hxx>
27 #include <eventqueue.hxx>
28 #include <slideshowexceptions.hxx>
34 using namespace ::com::sun::star
;
40 bool EventQueue::EventEntry::operator<( const EventEntry
& rEvent
) const
42 // negate comparison, we want priority queue to be sorted
43 // in increasing order of activation times
44 return nTime
> rEvent
.nTime
;
48 EventQueue::EventQueue(
49 std::shared_ptr
<canvas::tools::ElapsedTime
> const & pPresTimer
)
58 EventQueue::~EventQueue()
60 // add in all that have been added explicitly for this round:
61 EventEntryVector::const_iterator
const iEnd( maNextEvents
.end() );
62 for ( EventEntryVector::const_iterator
iPos( maNextEvents
.begin() );
63 iPos
!= iEnd
; ++iPos
)
67 EventEntryVector().swap( maNextEvents
);
69 // dispose event queue
70 while( !maEvents
.empty() )
74 maEvents
.top().pEvent
->dispose();
76 catch (const uno::Exception
& e
)
78 SAL_WARN("slideshow", e
);
84 bool EventQueue::addEvent( const EventSharedPtr
& rEvent
)
86 ::osl::MutexGuard
aGuard( maMutex
);
88 SAL_INFO("slideshow.eventqueue", "adding event \"" << rEvent
->GetDescription()
89 << "\" [" << rEvent
.get()
90 << "] at " << mpTimer
->getElapsedTime()
91 << " with delay " << rEvent
->getActivationTime(0.0)
93 ENSURE_OR_RETURN_FALSE( rEvent
,
94 "EventQueue::addEvent: event ptr NULL" );
98 // A seemingly obvious optimization cannot be used here,
99 // because it breaks assumed order of notification: zero
100 // timeout events could be fired() immediately, but that
101 // would not unwind the stack and furthermore changes
102 // order of notification
105 maEvents
.push( EventEntry( rEvent
, rEvent
->getActivationTime(
106 mpTimer
->getElapsedTime()) ) );
110 bool EventQueue::addEventForNextRound( EventSharedPtr
const& rEvent
)
112 ::osl::MutexGuard
aGuard( maMutex
);
114 SAL_INFO("slideshow.eventqueue", "adding event \"" << rEvent
->GetDescription()
115 << "\" [" << rEvent
.get()
116 << "] for the next round at " << mpTimer
->getElapsedTime()
117 << " with delay " << rEvent
->getActivationTime(0.0)
120 ENSURE_OR_RETURN_FALSE( rEvent
.get() != nullptr,
121 "EventQueue::addEvent: event ptr NULL" );
122 maNextEvents
.emplace_back( rEvent
, rEvent
->getActivationTime(
123 mpTimer
->getElapsedTime()) );
127 bool EventQueue::addEventWhenQueueIsEmpty (const EventSharedPtr
& rpEvent
)
129 ::osl::MutexGuard
aGuard( maMutex
);
131 SAL_INFO("slideshow.eventqueue", "adding event \"" << rpEvent
->GetDescription()
132 << "\" [" << rpEvent
.get()
133 << "] for execution when the queue is empty at " << mpTimer
->getElapsedTime()
134 << " with delay " << rpEvent
->getActivationTime(0.0)
137 ENSURE_OR_RETURN_FALSE(
138 rpEvent
.get() != nullptr,
139 "EventQueue::addEvent: event ptr NULL");
141 maNextNextEvents
.push(
144 rpEvent
->getActivationTime(mpTimer
->getElapsedTime())));
149 void EventQueue::forceEmpty()
151 ::osl::MutexGuard
aGuard( maMutex
);
156 void EventQueue::process()
158 ::osl::MutexGuard
aGuard( maMutex
);
163 void EventQueue::process_( bool bFireAllEvents
)
165 SAL_INFO("slideshow.verbose", "EventQueue: heartbeat" );
167 // add in all that have been added explicitly for this round:
168 EventEntryVector::const_iterator
const iEnd( maNextEvents
.end() );
169 for ( EventEntryVector::const_iterator
iPos( maNextEvents
.begin() );
170 iPos
!= iEnd
; ++iPos
) {
171 maEvents
.push(*iPos
);
173 EventEntryVector().swap( maNextEvents
);
175 // perform topmost, ready-to-execute event
176 // =======================================
178 const double nCurrTime( mpTimer
->getElapsedTime() );
180 // When maEvents does not contain any events that are due now
181 // then process one event from maNextNextEvents.
182 if (!maNextNextEvents
.empty()
184 && (maEvents
.empty() || maEvents
.top().nTime
> nCurrTime
))
186 const EventEntry
aEvent (maNextNextEvents
.top());
187 maNextNextEvents
.pop();
188 maEvents
.push(aEvent
);
191 // process ready/elapsed events. Note that the 'perceived'
192 // current time remains constant for this loop, thus we're
193 // processing only those events which where ready when we
194 // entered this method.
195 while( !maEvents
.empty() &&
196 (bFireAllEvents
|| maEvents
.top().nTime
<= nCurrTime
) )
198 EventEntry
event( maEvents
.top() );
201 // only process event, if it is still 'charged',
202 // i.e. the fire() call effects something. This is
203 // used when e.g. having events registered at multiple
204 // places, which should fire only once: after the
205 // initial fire() call, those events become inactive
206 // and return false on isCharged. This frees us from
207 // the need to prune queues of those inactive shells.
208 if( event
.pEvent
->isCharged() )
212 SAL_INFO("slideshow.eventqueue", "firing event \""
213 << event
.pEvent
->GetDescription()
214 << "\" [" << event
.pEvent
.get()
215 << "] at " << mpTimer
->getElapsedTime()
216 << " with delay " << event
.pEvent
->getActivationTime(0.0)
218 event
.pEvent
->fire();
219 SAL_INFO("slideshow.eventqueue", "event \""
220 << event
.pEvent
->GetDescription()
221 << "\" [" << event
.pEvent
.get() << "] fired"
224 catch( uno::RuntimeException
& )
228 catch( uno::Exception
& )
230 // catch anything here, we don't want
231 // to leave this scope under _any_
232 // circumstance. Although, do _not_
233 // reinsert an activity that threw
236 // NOTE: we explicitly don't catch(...) here,
237 // since this will also capture segmentation
238 // violations and the like. In such a case, we
239 // still better let our clients now...
240 SAL_WARN( "slideshow", comphelper::anyToString( cppu::getCaughtException() ) );
242 catch( SlideShowException
& )
244 // catch anything here, we don't want
245 // to leave this scope under _any_
246 // circumstance. Although, do _not_
247 // reinsert an activity that threw
250 // NOTE: we explicitly don't catch(...) here,
251 // since this will also capture segmentation
252 // violations and the like. In such a case, we
253 // still better let our clients now...
254 SAL_WARN("slideshow.eventqueue", "::presentation::internal::EventQueue: Event threw a SlideShowException, action might not have been fully performed" );
260 "slideshow.eventqueue",
261 "Ignoring discharged event: unknown ("
262 << event
.pEvent
.get() << "), timeout was: "
263 << event
.pEvent
->getActivationTime(0.0));
268 bool EventQueue::isEmpty() const
270 ::osl::MutexGuard
aGuard( maMutex
);
272 return maEvents
.empty() && maNextEvents
.empty() && maNextNextEvents
.empty();
275 double EventQueue::nextTimeout() const
277 ::osl::MutexGuard
aGuard( maMutex
);
279 // return time for next entry (if any)
280 double nTimeout (::std::numeric_limits
<double>::max());
281 const double nCurrentTime (mpTimer
->getElapsedTime());
282 if ( ! maEvents
.empty())
283 nTimeout
= maEvents
.top().nTime
- nCurrentTime
;
284 if ( ! maNextEvents
.empty())
285 nTimeout
= ::std::min(nTimeout
, maNextEvents
.front().nTime
- nCurrentTime
);
286 if ( ! maNextNextEvents
.empty())
287 nTimeout
= ::std::min(nTimeout
, maNextNextEvents
.top().nTime
- nCurrentTime
);
292 void EventQueue::clear()
294 ::osl::MutexGuard
aGuard( maMutex
);
296 // TODO(P1): Maybe a plain vector and vector.swap will
297 // be faster here. Profile.
298 maEvents
= ImplQueueType();
300 maNextEvents
.clear();
301 maNextNextEvents
= ImplQueueType();
306 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */