fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / slideshow / source / engine / eventqueue.cxx
blob6e5d2b49a9ea5db25a6d6e34c1a9c600bf109990
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 // must be first
22 #include <canvas/debug.hxx>
23 #include <tools/diagnose_ex.h>
24 #include <canvas/verbosetrace.hxx>
26 #include <comphelper/anytostring.hxx>
27 #include <cppuhelper/exc_hlp.hxx>
29 #include <event.hxx>
30 #include <eventqueue.hxx>
31 #include <slideshowexceptions.hxx>
33 #include <boost/shared_ptr.hpp>
34 #include <limits>
37 using namespace ::com::sun::star;
39 namespace slideshow
41 namespace internal
43 bool EventQueue::EventEntry::operator<( const EventEntry& rEvent ) const
45 // negate comparison, we want priority queue to be sorted
46 // in increasing order of activation times
47 return this->nTime > rEvent.nTime;
51 EventQueue::EventQueue(
52 boost::shared_ptr<canvas::tools::ElapsedTime> const & pPresTimer )
53 : maMutex(),
54 maEvents(),
55 maNextEvents(),
56 maNextNextEvents(),
57 mpTimer( pPresTimer )
61 EventQueue::~EventQueue()
63 // add in all that have been added explicitly for this round:
64 EventEntryVector::const_iterator const iEnd( maNextEvents.end() );
65 for ( EventEntryVector::const_iterator iPos( maNextEvents.begin() );
66 iPos != iEnd; ++iPos )
68 maEvents.push(*iPos);
70 EventEntryVector().swap( maNextEvents );
72 // dispose event queue
73 while( !maEvents.empty() )
75 try
77 maEvents.top().pEvent->dispose();
79 catch (uno::Exception &)
81 OSL_FAIL( OUStringToOString(
82 comphelper::anyToString(
83 cppu::getCaughtException() ),
84 RTL_TEXTENCODING_UTF8 ).getStr() );
86 maEvents.pop();
90 bool EventQueue::addEvent( const EventSharedPtr& rEvent )
92 ::osl::MutexGuard aGuard( maMutex );
94 SAL_INFO("slideshow.eventqueue", "adding event \"" << rEvent->GetDescription()
95 << "\" [" << rEvent.get()
96 << "] at " << mpTimer->getElapsedTime()
97 << " with delay " << rEvent->getActivationTime(0.0)
99 ENSURE_OR_RETURN_FALSE( rEvent,
100 "EventQueue::addEvent: event ptr NULL" );
102 // prepare entry
104 // A seemingly obvious optimization cannot be used here,
105 // because it breaks assumed order of notification: zero
106 // timeout events could be fired() immediately, but that
107 // would not unwind the stack and furthermore changes
108 // order of notification
110 // add entry
111 maEvents.push( EventEntry( rEvent, rEvent->getActivationTime(
112 mpTimer->getElapsedTime()) ) );
113 return true;
116 bool EventQueue::addEventForNextRound( EventSharedPtr const& rEvent )
118 ::osl::MutexGuard aGuard( maMutex );
120 SAL_INFO("slideshow.eventqueue", "adding event \"" << rEvent->GetDescription()
121 << "\" [" << rEvent.get()
122 << "] for the next round at " << mpTimer->getElapsedTime()
123 << " with delay " << rEvent->getActivationTime(0.0)
126 ENSURE_OR_RETURN_FALSE( rEvent.get() != NULL,
127 "EventQueue::addEvent: event ptr NULL" );
128 maNextEvents.push_back(
129 EventEntry( rEvent, rEvent->getActivationTime(
130 mpTimer->getElapsedTime()) ) );
131 return true;
134 bool EventQueue::addEventWhenQueueIsEmpty (const EventSharedPtr& rpEvent)
136 ::osl::MutexGuard aGuard( maMutex );
138 SAL_INFO("slideshow.eventqueue", "adding event \"" << rpEvent->GetDescription()
139 << "\" [" << rpEvent.get()
140 << "] for execution when the queue is empty at " << mpTimer->getElapsedTime()
141 << " with delay " << rpEvent->getActivationTime(0.0)
144 ENSURE_OR_RETURN_FALSE(
145 rpEvent.get() != NULL,
146 "EventQueue::addEvent: event ptr NULL");
148 maNextNextEvents.push(
149 EventEntry(
150 rpEvent,
151 rpEvent->getActivationTime(mpTimer->getElapsedTime())));
153 return true;
156 void EventQueue::forceEmpty()
158 ::osl::MutexGuard aGuard( maMutex );
160 process_(true);
163 void EventQueue::process()
165 ::osl::MutexGuard aGuard( maMutex );
167 process_(false);
170 void EventQueue::process_( bool bFireAllEvents )
172 VERBOSE_TRACE( "EventQueue: heartbeat" );
174 // add in all that have been added explicitly for this round:
175 EventEntryVector::const_iterator const iEnd( maNextEvents.end() );
176 for ( EventEntryVector::const_iterator iPos( maNextEvents.begin() );
177 iPos != iEnd; ++iPos ) {
178 maEvents.push(*iPos);
180 EventEntryVector().swap( maNextEvents );
182 // perform topmost, ready-to-execute event
183 // =======================================
185 const double nCurrTime( mpTimer->getElapsedTime() );
187 // When maEvents does not contain any events that are due now
188 // then process one event from maNextNextEvents.
189 if (!maNextNextEvents.empty()
190 && !bFireAllEvents
191 && (maEvents.empty() || maEvents.top().nTime > nCurrTime))
193 const EventEntry aEvent (maNextNextEvents.top());
194 maNextNextEvents.pop();
195 maEvents.push(aEvent);
198 // process ready/elapsed events. Note that the 'perceived'
199 // current time remains constant for this loop, thus we're
200 // processing only those events which where ready when we
201 // entered this method.
202 while( !maEvents.empty() &&
203 (bFireAllEvents || maEvents.top().nTime <= nCurrTime) )
205 EventEntry event( maEvents.top() );
206 maEvents.pop();
208 // only process event, if it is still 'charged',
209 // i.e. the fire() call effects something. This is
210 // used when e.g. having events registered at multiple
211 // places, which should fire only once: after the
212 // initial fire() call, those events become inactive
213 // and return false on isCharged. This frees us from
214 // the need to prune queues of those inactive shells.
215 if( event.pEvent->isCharged() )
219 SAL_INFO("slideshow.eventqueue", "firing event \""
220 << event.pEvent->GetDescription()
221 << "\" [" << event.pEvent.get()
222 << "] at " << mpTimer->getElapsedTime()
223 << " with delay " << event.pEvent->getActivationTime(0.0)
225 event.pEvent->fire();
226 SAL_INFO("slideshow.eventqueue", "event \""
227 << event.pEvent->GetDescription()
228 << "\" [" << event.pEvent.get() << "] fired"
231 catch( uno::RuntimeException& )
233 throw;
235 catch( uno::Exception& )
237 // catch anything here, we don't want
238 // to leave this scope under _any_
239 // circumstance. Although, do _not_
240 // reinsert an activity that threw
241 // once.
243 // NOTE: we explicitly don't catch(...) here,
244 // since this will also capture segmentation
245 // violations and the like. In such a case, we
246 // still better let our clients now...
247 OSL_FAIL( OUStringToOString(
248 comphelper::anyToString( cppu::getCaughtException() ),
249 RTL_TEXTENCODING_UTF8 ).getStr() );
251 catch( SlideShowException& )
253 // catch anything here, we don't want
254 // to leave this scope under _any_
255 // circumstance. Although, do _not_
256 // reinsert an activity that threw
257 // once.
259 // NOTE: we explicitly don't catch(...) here,
260 // since this will also capture segmentation
261 // violations and the like. In such a case, we
262 // still better let our clients now...
263 OSL_TRACE( "::presentation::internal::EventQueue: Event threw a SlideShowException, action might not have been fully performed" );
266 else
268 SAL_INFO(
269 "slideshow.eventqueue",
270 "Ignoring discharged event: unknown ("
271 << event.pEvent.get() << "), timeout was: "
272 << event.pEvent->getActivationTime(0.0));
277 bool EventQueue::isEmpty() const
279 ::osl::MutexGuard aGuard( maMutex );
281 return maEvents.empty() && maNextEvents.empty() && maNextNextEvents.empty();
284 double EventQueue::nextTimeout() const
286 ::osl::MutexGuard aGuard( maMutex );
288 // return time for next entry (if any)
289 double nTimeout (::std::numeric_limits<double>::max());
290 const double nCurrentTime (mpTimer->getElapsedTime());
291 if ( ! maEvents.empty())
292 nTimeout = maEvents.top().nTime - nCurrentTime;
293 if ( ! maNextEvents.empty())
294 nTimeout = ::std::min(nTimeout, maNextEvents.front().nTime - nCurrentTime);
295 if ( ! maNextNextEvents.empty())
296 nTimeout = ::std::min(nTimeout, maNextNextEvents.top().nTime - nCurrentTime);
298 return nTimeout;
301 void EventQueue::clear()
303 ::osl::MutexGuard aGuard( maMutex );
305 // TODO(P1): Maybe a plain vector and vector.swap will
306 // be faster here. Profile.
307 maEvents = ImplQueueType();
309 maNextEvents.clear();
310 maNextNextEvents = ImplQueueType();
315 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */