1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_sdext.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include "PresenterTimer.hxx" 32*cdf0e10cSrcweir #include <osl/doublecheckedlocking.h> 33*cdf0e10cSrcweir #include <osl/thread.hxx> 34*cdf0e10cSrcweir #include <boost/bind.hpp> 35*cdf0e10cSrcweir #include <boost/function.hpp> 36*cdf0e10cSrcweir #include <boost/enable_shared_from_this.hpp> 37*cdf0e10cSrcweir #include <set> 38*cdf0e10cSrcweir 39*cdf0e10cSrcweir using namespace ::com::sun::star; 40*cdf0e10cSrcweir using namespace ::com::sun::star::uno; 41*cdf0e10cSrcweir 42*cdf0e10cSrcweir #define A2S(pString) (::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(pString))) 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir namespace sdext { namespace presenter { 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir namespace { 47*cdf0e10cSrcweir class TimerTask 48*cdf0e10cSrcweir { 49*cdf0e10cSrcweir public: 50*cdf0e10cSrcweir TimerTask ( 51*cdf0e10cSrcweir const PresenterTimer::Task& rTask, 52*cdf0e10cSrcweir const TimeValue& rDueTime, 53*cdf0e10cSrcweir const sal_Int64 nRepeatIntervall, 54*cdf0e10cSrcweir const sal_Int32 nTaskId); 55*cdf0e10cSrcweir ~TimerTask (void) {} 56*cdf0e10cSrcweir 57*cdf0e10cSrcweir PresenterTimer::Task maTask; 58*cdf0e10cSrcweir TimeValue maDueTime; 59*cdf0e10cSrcweir const sal_Int64 mnRepeatIntervall; 60*cdf0e10cSrcweir const sal_Int32 mnTaskId; 61*cdf0e10cSrcweir bool mbIsCanceled; 62*cdf0e10cSrcweir }; 63*cdf0e10cSrcweir 64*cdf0e10cSrcweir typedef ::boost::shared_ptr<TimerTask> SharedTimerTask; 65*cdf0e10cSrcweir 66*cdf0e10cSrcweir 67*cdf0e10cSrcweir class TimerTaskComparator 68*cdf0e10cSrcweir { 69*cdf0e10cSrcweir public: 70*cdf0e10cSrcweir bool operator() (const SharedTimerTask& rpTask1, const SharedTimerTask& rpTask2) 71*cdf0e10cSrcweir { 72*cdf0e10cSrcweir return rpTask1->maDueTime.Seconds < rpTask2->maDueTime.Seconds 73*cdf0e10cSrcweir || (rpTask1->maDueTime.Seconds == rpTask2->maDueTime.Seconds 74*cdf0e10cSrcweir && rpTask1->maDueTime.Nanosec < rpTask2->maDueTime.Nanosec); 75*cdf0e10cSrcweir } 76*cdf0e10cSrcweir }; 77*cdf0e10cSrcweir 78*cdf0e10cSrcweir 79*cdf0e10cSrcweir 80*cdf0e10cSrcweir 81*cdf0e10cSrcweir /** Queue all scheduled tasks and process them when their time has come. 82*cdf0e10cSrcweir */ 83*cdf0e10cSrcweir class TimerScheduler 84*cdf0e10cSrcweir : public ::boost::enable_shared_from_this<TimerScheduler>, 85*cdf0e10cSrcweir public ::osl::Thread 86*cdf0e10cSrcweir { 87*cdf0e10cSrcweir public: 88*cdf0e10cSrcweir static ::boost::shared_ptr<TimerScheduler> Instance (void); 89*cdf0e10cSrcweir static SharedTimerTask CreateTimerTask ( 90*cdf0e10cSrcweir const PresenterTimer::Task& rTask, 91*cdf0e10cSrcweir const TimeValue& rDueTime, 92*cdf0e10cSrcweir const sal_Int64 nRepeatIntervall); 93*cdf0e10cSrcweir 94*cdf0e10cSrcweir void ScheduleTask (const SharedTimerTask& rpTask); 95*cdf0e10cSrcweir void CancelTask (const sal_Int32 nTaskId); 96*cdf0e10cSrcweir 97*cdf0e10cSrcweir static bool GetCurrentTime (TimeValue& rCurrentTime); 98*cdf0e10cSrcweir static sal_Int64 GetTimeDifference ( 99*cdf0e10cSrcweir const TimeValue& rTargetTime, 100*cdf0e10cSrcweir const TimeValue& rCurrentTime); 101*cdf0e10cSrcweir static void ConvertToTimeValue ( 102*cdf0e10cSrcweir TimeValue& rTimeValue, 103*cdf0e10cSrcweir const sal_Int64 nTimeDifference); 104*cdf0e10cSrcweir static sal_Int64 ConvertFromTimeValue ( 105*cdf0e10cSrcweir const TimeValue& rTimeValue); 106*cdf0e10cSrcweir 107*cdf0e10cSrcweir private: 108*cdf0e10cSrcweir static ::boost::shared_ptr<TimerScheduler> mpInstance; 109*cdf0e10cSrcweir static ::osl::Mutex maInstanceMutex; 110*cdf0e10cSrcweir static sal_Int32 mnTaskId; 111*cdf0e10cSrcweir 112*cdf0e10cSrcweir ::osl::Mutex maTaskContainerMutex; 113*cdf0e10cSrcweir typedef ::std::set<SharedTimerTask,TimerTaskComparator> TaskContainer; 114*cdf0e10cSrcweir TaskContainer maScheduledTasks; 115*cdf0e10cSrcweir bool mbIsRunning; 116*cdf0e10cSrcweir ::osl::Mutex maCurrentTaskMutex; 117*cdf0e10cSrcweir SharedTimerTask mpCurrentTask; 118*cdf0e10cSrcweir 119*cdf0e10cSrcweir static void Release (void); 120*cdf0e10cSrcweir 121*cdf0e10cSrcweir TimerScheduler (void); 122*cdf0e10cSrcweir virtual ~TimerScheduler (void); 123*cdf0e10cSrcweir class Deleter {public: void operator () (TimerScheduler* pScheduler) { delete pScheduler; } }; 124*cdf0e10cSrcweir friend class Deleter; 125*cdf0e10cSrcweir 126*cdf0e10cSrcweir virtual void SAL_CALL run (void); 127*cdf0e10cSrcweir virtual void SAL_CALL onTerminated (void); 128*cdf0e10cSrcweir }; 129*cdf0e10cSrcweir 130*cdf0e10cSrcweir 131*cdf0e10cSrcweir 132*cdf0e10cSrcweir 133*cdf0e10cSrcweir bool GetDateTime (oslDateTime& rDateTime); 134*cdf0e10cSrcweir } // end of anonymous namespace 135*cdf0e10cSrcweir 136*cdf0e10cSrcweir 137*cdf0e10cSrcweir //===== PresenterTimer ======================================================== 138*cdf0e10cSrcweir 139*cdf0e10cSrcweir sal_Int32 PresenterTimer::ScheduleSingleTaskRelative ( 140*cdf0e10cSrcweir const Task& rTask, 141*cdf0e10cSrcweir const sal_Int64 nDelay) 142*cdf0e10cSrcweir { 143*cdf0e10cSrcweir return ScheduleRepeatedTask(rTask, nDelay, 0); 144*cdf0e10cSrcweir } 145*cdf0e10cSrcweir 146*cdf0e10cSrcweir 147*cdf0e10cSrcweir 148*cdf0e10cSrcweir 149*cdf0e10cSrcweir sal_Int32 PresenterTimer::ScheduleSingleTaskAbsolute ( 150*cdf0e10cSrcweir const Task& rTask, 151*cdf0e10cSrcweir const TimeValue& rDueTime) 152*cdf0e10cSrcweir { 153*cdf0e10cSrcweir SharedTimerTask pTask (TimerScheduler::CreateTimerTask(rTask, rDueTime, 0)); 154*cdf0e10cSrcweir TimerScheduler::Instance()->ScheduleTask(pTask); 155*cdf0e10cSrcweir return pTask->mnTaskId; 156*cdf0e10cSrcweir } 157*cdf0e10cSrcweir 158*cdf0e10cSrcweir 159*cdf0e10cSrcweir 160*cdf0e10cSrcweir 161*cdf0e10cSrcweir sal_Int32 PresenterTimer::ScheduleRepeatedTask ( 162*cdf0e10cSrcweir const Task& rTask, 163*cdf0e10cSrcweir const sal_Int64 nDelay, 164*cdf0e10cSrcweir const sal_Int64 nIntervall) 165*cdf0e10cSrcweir { 166*cdf0e10cSrcweir TimeValue aCurrentTime; 167*cdf0e10cSrcweir if (TimerScheduler::GetCurrentTime(aCurrentTime)) 168*cdf0e10cSrcweir { 169*cdf0e10cSrcweir TimeValue aDueTime; 170*cdf0e10cSrcweir TimerScheduler::ConvertToTimeValue( 171*cdf0e10cSrcweir aDueTime, 172*cdf0e10cSrcweir TimerScheduler::ConvertFromTimeValue (aCurrentTime) + nDelay); 173*cdf0e10cSrcweir SharedTimerTask pTask (TimerScheduler::CreateTimerTask(rTask, aDueTime, nIntervall)); 174*cdf0e10cSrcweir TimerScheduler::Instance()->ScheduleTask(pTask); 175*cdf0e10cSrcweir return pTask->mnTaskId; 176*cdf0e10cSrcweir } 177*cdf0e10cSrcweir 178*cdf0e10cSrcweir return NotAValidTaskId; 179*cdf0e10cSrcweir } 180*cdf0e10cSrcweir 181*cdf0e10cSrcweir 182*cdf0e10cSrcweir 183*cdf0e10cSrcweir 184*cdf0e10cSrcweir void PresenterTimer::CancelTask (const sal_Int32 nTaskId) 185*cdf0e10cSrcweir { 186*cdf0e10cSrcweir return TimerScheduler::Instance()->CancelTask(nTaskId); 187*cdf0e10cSrcweir } 188*cdf0e10cSrcweir 189*cdf0e10cSrcweir 190*cdf0e10cSrcweir 191*cdf0e10cSrcweir 192*cdf0e10cSrcweir //===== TimerScheduler ======================================================== 193*cdf0e10cSrcweir 194*cdf0e10cSrcweir ::boost::shared_ptr<TimerScheduler> TimerScheduler::mpInstance; 195*cdf0e10cSrcweir ::osl::Mutex TimerScheduler::maInstanceMutex; 196*cdf0e10cSrcweir sal_Int32 TimerScheduler::mnTaskId = PresenterTimer::NotAValidTaskId; 197*cdf0e10cSrcweir 198*cdf0e10cSrcweir ::boost::shared_ptr<TimerScheduler> TimerScheduler::Instance (void) 199*cdf0e10cSrcweir { 200*cdf0e10cSrcweir ::boost::shared_ptr<TimerScheduler> pInstance = mpInstance; 201*cdf0e10cSrcweir if (pInstance.get() == NULL) 202*cdf0e10cSrcweir { 203*cdf0e10cSrcweir ::osl::MutexGuard aGuard (maInstanceMutex); 204*cdf0e10cSrcweir pInstance = mpInstance; 205*cdf0e10cSrcweir if (pInstance.get() == NULL) 206*cdf0e10cSrcweir { 207*cdf0e10cSrcweir pInstance.reset(new TimerScheduler(), TimerScheduler::Deleter()); 208*cdf0e10cSrcweir OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER(); 209*cdf0e10cSrcweir mpInstance = pInstance; 210*cdf0e10cSrcweir } 211*cdf0e10cSrcweir } 212*cdf0e10cSrcweir else 213*cdf0e10cSrcweir { 214*cdf0e10cSrcweir OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER(); 215*cdf0e10cSrcweir } 216*cdf0e10cSrcweir return pInstance; 217*cdf0e10cSrcweir } 218*cdf0e10cSrcweir 219*cdf0e10cSrcweir 220*cdf0e10cSrcweir 221*cdf0e10cSrcweir 222*cdf0e10cSrcweir void TimerScheduler::Release (void) 223*cdf0e10cSrcweir { 224*cdf0e10cSrcweir ::osl::MutexGuard aGuard (maInstanceMutex); 225*cdf0e10cSrcweir mpInstance.reset(); 226*cdf0e10cSrcweir } 227*cdf0e10cSrcweir 228*cdf0e10cSrcweir 229*cdf0e10cSrcweir 230*cdf0e10cSrcweir 231*cdf0e10cSrcweir TimerScheduler::TimerScheduler (void) 232*cdf0e10cSrcweir : maTaskContainerMutex(), 233*cdf0e10cSrcweir maScheduledTasks(), 234*cdf0e10cSrcweir mbIsRunning(false), 235*cdf0e10cSrcweir maCurrentTaskMutex(), 236*cdf0e10cSrcweir mpCurrentTask() 237*cdf0e10cSrcweir { 238*cdf0e10cSrcweir } 239*cdf0e10cSrcweir 240*cdf0e10cSrcweir 241*cdf0e10cSrcweir 242*cdf0e10cSrcweir 243*cdf0e10cSrcweir TimerScheduler::~TimerScheduler (void) 244*cdf0e10cSrcweir { 245*cdf0e10cSrcweir } 246*cdf0e10cSrcweir 247*cdf0e10cSrcweir 248*cdf0e10cSrcweir 249*cdf0e10cSrcweir SharedTimerTask TimerScheduler::CreateTimerTask ( 250*cdf0e10cSrcweir const PresenterTimer::Task& rTask, 251*cdf0e10cSrcweir const TimeValue& rDueTime, 252*cdf0e10cSrcweir const sal_Int64 nRepeatIntervall) 253*cdf0e10cSrcweir { 254*cdf0e10cSrcweir return SharedTimerTask(new TimerTask(rTask, rDueTime, nRepeatIntervall, ++mnTaskId)); 255*cdf0e10cSrcweir } 256*cdf0e10cSrcweir 257*cdf0e10cSrcweir 258*cdf0e10cSrcweir 259*cdf0e10cSrcweir 260*cdf0e10cSrcweir void TimerScheduler::ScheduleTask (const SharedTimerTask& rpTask) 261*cdf0e10cSrcweir { 262*cdf0e10cSrcweir if (rpTask.get() == NULL) 263*cdf0e10cSrcweir return; 264*cdf0e10cSrcweir if (rpTask->mbIsCanceled) 265*cdf0e10cSrcweir return; 266*cdf0e10cSrcweir 267*cdf0e10cSrcweir osl::MutexGuard aGuard (maTaskContainerMutex); 268*cdf0e10cSrcweir maScheduledTasks.insert(rpTask); 269*cdf0e10cSrcweir 270*cdf0e10cSrcweir if ( ! mbIsRunning) 271*cdf0e10cSrcweir { 272*cdf0e10cSrcweir mbIsRunning = true; 273*cdf0e10cSrcweir create(); 274*cdf0e10cSrcweir } 275*cdf0e10cSrcweir } 276*cdf0e10cSrcweir 277*cdf0e10cSrcweir 278*cdf0e10cSrcweir 279*cdf0e10cSrcweir 280*cdf0e10cSrcweir void TimerScheduler::CancelTask (const sal_Int32 nTaskId) 281*cdf0e10cSrcweir { 282*cdf0e10cSrcweir // Set of scheduled tasks is sorted after their due times, not their 283*cdf0e10cSrcweir // task ids. Therefore we have to do a linear search for the task to 284*cdf0e10cSrcweir // cancel. 285*cdf0e10cSrcweir { 286*cdf0e10cSrcweir ::osl::MutexGuard aGuard (maTaskContainerMutex); 287*cdf0e10cSrcweir TaskContainer::iterator iTask (maScheduledTasks.begin()); 288*cdf0e10cSrcweir TaskContainer::const_iterator iEnd (maScheduledTasks.end()); 289*cdf0e10cSrcweir for ( ; iTask!=iEnd; ++iTask) 290*cdf0e10cSrcweir { 291*cdf0e10cSrcweir if ((*iTask)->mnTaskId == nTaskId) 292*cdf0e10cSrcweir { 293*cdf0e10cSrcweir maScheduledTasks.erase(iTask); 294*cdf0e10cSrcweir break; 295*cdf0e10cSrcweir } 296*cdf0e10cSrcweir } 297*cdf0e10cSrcweir } 298*cdf0e10cSrcweir 299*cdf0e10cSrcweir // The task that is to be canceled may be currently about to be 300*cdf0e10cSrcweir // processed. Mark it with a flag that a) prevents a repeating task 301*cdf0e10cSrcweir // from being scheduled again and b) tries to prevent its execution. 302*cdf0e10cSrcweir if (mpCurrentTask.get() != NULL 303*cdf0e10cSrcweir && mpCurrentTask->mnTaskId == nTaskId) 304*cdf0e10cSrcweir { 305*cdf0e10cSrcweir mpCurrentTask->mbIsCanceled = true; 306*cdf0e10cSrcweir } 307*cdf0e10cSrcweir 308*cdf0e10cSrcweir // When the last active task was canceled then the timer can be 309*cdf0e10cSrcweir // stopped. 310*cdf0e10cSrcweir if (maScheduledTasks.size() == 0) 311*cdf0e10cSrcweir { 312*cdf0e10cSrcweir mbIsRunning = false; 313*cdf0e10cSrcweir resume(); 314*cdf0e10cSrcweir // join(); 315*cdf0e10cSrcweir } 316*cdf0e10cSrcweir } 317*cdf0e10cSrcweir 318*cdf0e10cSrcweir 319*cdf0e10cSrcweir 320*cdf0e10cSrcweir 321*cdf0e10cSrcweir void SAL_CALL TimerScheduler::run (void) 322*cdf0e10cSrcweir { 323*cdf0e10cSrcweir while (mbIsRunning) 324*cdf0e10cSrcweir { 325*cdf0e10cSrcweir // Get the current time. 326*cdf0e10cSrcweir TimeValue aCurrentTime; 327*cdf0e10cSrcweir if ( ! GetCurrentTime(aCurrentTime)) 328*cdf0e10cSrcweir { 329*cdf0e10cSrcweir // We can not get the current time and thus can not schedule anything. 330*cdf0e10cSrcweir break; 331*cdf0e10cSrcweir } 332*cdf0e10cSrcweir 333*cdf0e10cSrcweir // Restrict access to the maScheduledTasks member to one, mutext 334*cdf0e10cSrcweir // guarded, block. 335*cdf0e10cSrcweir SharedTimerTask pTask; 336*cdf0e10cSrcweir sal_Int64 nDifference = 0; 337*cdf0e10cSrcweir { 338*cdf0e10cSrcweir ::osl::MutexGuard aGuard (maTaskContainerMutex); 339*cdf0e10cSrcweir 340*cdf0e10cSrcweir // There are no more scheduled task. Leave this loop, function and 341*cdf0e10cSrcweir // live of the TimerScheduler. 342*cdf0e10cSrcweir if (maScheduledTasks.empty()) 343*cdf0e10cSrcweir break; 344*cdf0e10cSrcweir 345*cdf0e10cSrcweir nDifference = GetTimeDifference( 346*cdf0e10cSrcweir (*maScheduledTasks.begin())->maDueTime, 347*cdf0e10cSrcweir aCurrentTime); 348*cdf0e10cSrcweir if (nDifference <= 0) 349*cdf0e10cSrcweir { 350*cdf0e10cSrcweir pTask = *maScheduledTasks.begin(); 351*cdf0e10cSrcweir maScheduledTasks.erase(maScheduledTasks.begin()); 352*cdf0e10cSrcweir } 353*cdf0e10cSrcweir } 354*cdf0e10cSrcweir 355*cdf0e10cSrcweir // Acquire a reference to the current task. 356*cdf0e10cSrcweir { 357*cdf0e10cSrcweir ::osl::MutexGuard aGuard (maCurrentTaskMutex); 358*cdf0e10cSrcweir mpCurrentTask = pTask; 359*cdf0e10cSrcweir } 360*cdf0e10cSrcweir 361*cdf0e10cSrcweir if (mpCurrentTask.get() == NULL) 362*cdf0e10cSrcweir { 363*cdf0e10cSrcweir // Wait until the first task becomes due. 364*cdf0e10cSrcweir TimeValue aTimeValue; 365*cdf0e10cSrcweir ConvertToTimeValue(aTimeValue, nDifference); 366*cdf0e10cSrcweir wait(aTimeValue); 367*cdf0e10cSrcweir } 368*cdf0e10cSrcweir else 369*cdf0e10cSrcweir { 370*cdf0e10cSrcweir // Execute task. 371*cdf0e10cSrcweir if ( ! mpCurrentTask->maTask.empty() 372*cdf0e10cSrcweir && ! mpCurrentTask->mbIsCanceled) 373*cdf0e10cSrcweir { 374*cdf0e10cSrcweir mpCurrentTask->maTask(aCurrentTime); 375*cdf0e10cSrcweir 376*cdf0e10cSrcweir // Re-schedule repeating tasks. 377*cdf0e10cSrcweir if (mpCurrentTask->mnRepeatIntervall > 0) 378*cdf0e10cSrcweir { 379*cdf0e10cSrcweir ConvertToTimeValue( 380*cdf0e10cSrcweir mpCurrentTask->maDueTime, 381*cdf0e10cSrcweir ConvertFromTimeValue(mpCurrentTask->maDueTime) 382*cdf0e10cSrcweir + mpCurrentTask->mnRepeatIntervall); 383*cdf0e10cSrcweir ScheduleTask(mpCurrentTask); 384*cdf0e10cSrcweir } 385*cdf0e10cSrcweir } 386*cdf0e10cSrcweir 387*cdf0e10cSrcweir } 388*cdf0e10cSrcweir 389*cdf0e10cSrcweir // Release reference to the current task. 390*cdf0e10cSrcweir { 391*cdf0e10cSrcweir ::osl::MutexGuard aGuard (maCurrentTaskMutex); 392*cdf0e10cSrcweir mpCurrentTask.reset(); 393*cdf0e10cSrcweir } 394*cdf0e10cSrcweir } 395*cdf0e10cSrcweir } 396*cdf0e10cSrcweir 397*cdf0e10cSrcweir 398*cdf0e10cSrcweir 399*cdf0e10cSrcweir 400*cdf0e10cSrcweir void SAL_CALL TimerScheduler::onTerminated (void) 401*cdf0e10cSrcweir { 402*cdf0e10cSrcweir Release(); 403*cdf0e10cSrcweir } 404*cdf0e10cSrcweir 405*cdf0e10cSrcweir 406*cdf0e10cSrcweir 407*cdf0e10cSrcweir 408*cdf0e10cSrcweir bool TimerScheduler::GetCurrentTime (TimeValue& rCurrentTime) 409*cdf0e10cSrcweir { 410*cdf0e10cSrcweir TimeValue aSystemTime; 411*cdf0e10cSrcweir if (osl_getSystemTime(&aSystemTime)) 412*cdf0e10cSrcweir return osl_getLocalTimeFromSystemTime(&aSystemTime, &rCurrentTime); 413*cdf0e10cSrcweir return false; 414*cdf0e10cSrcweir } 415*cdf0e10cSrcweir 416*cdf0e10cSrcweir 417*cdf0e10cSrcweir 418*cdf0e10cSrcweir 419*cdf0e10cSrcweir sal_Int64 TimerScheduler::GetTimeDifference ( 420*cdf0e10cSrcweir const TimeValue& rTargetTime, 421*cdf0e10cSrcweir const TimeValue& rCurrentTime) 422*cdf0e10cSrcweir { 423*cdf0e10cSrcweir return ConvertFromTimeValue(rTargetTime) - ConvertFromTimeValue(rCurrentTime); 424*cdf0e10cSrcweir } 425*cdf0e10cSrcweir 426*cdf0e10cSrcweir 427*cdf0e10cSrcweir 428*cdf0e10cSrcweir 429*cdf0e10cSrcweir void TimerScheduler::ConvertToTimeValue ( 430*cdf0e10cSrcweir TimeValue& rTimeValue, 431*cdf0e10cSrcweir const sal_Int64 nTimeDifference) 432*cdf0e10cSrcweir { 433*cdf0e10cSrcweir rTimeValue.Seconds = sal::static_int_cast<sal_Int32>(nTimeDifference / 1000000000L); 434*cdf0e10cSrcweir rTimeValue.Nanosec = sal::static_int_cast<sal_Int32>(nTimeDifference % 1000000000L); 435*cdf0e10cSrcweir } 436*cdf0e10cSrcweir 437*cdf0e10cSrcweir 438*cdf0e10cSrcweir 439*cdf0e10cSrcweir 440*cdf0e10cSrcweir sal_Int64 TimerScheduler::ConvertFromTimeValue ( 441*cdf0e10cSrcweir const TimeValue& rTimeValue) 442*cdf0e10cSrcweir { 443*cdf0e10cSrcweir return sal_Int64(rTimeValue.Seconds) * 1000000000L + rTimeValue.Nanosec; 444*cdf0e10cSrcweir } 445*cdf0e10cSrcweir 446*cdf0e10cSrcweir 447*cdf0e10cSrcweir 448*cdf0e10cSrcweir 449*cdf0e10cSrcweir //===== TimerTask ============================================================= 450*cdf0e10cSrcweir 451*cdf0e10cSrcweir namespace { 452*cdf0e10cSrcweir 453*cdf0e10cSrcweir TimerTask::TimerTask ( 454*cdf0e10cSrcweir const PresenterTimer::Task& rTask, 455*cdf0e10cSrcweir const TimeValue& rDueTime, 456*cdf0e10cSrcweir const sal_Int64 nRepeatIntervall, 457*cdf0e10cSrcweir const sal_Int32 nTaskId) 458*cdf0e10cSrcweir : maTask(rTask), 459*cdf0e10cSrcweir maDueTime(rDueTime), 460*cdf0e10cSrcweir mnRepeatIntervall(nRepeatIntervall), 461*cdf0e10cSrcweir mnTaskId(nTaskId), 462*cdf0e10cSrcweir mbIsCanceled(false) 463*cdf0e10cSrcweir { 464*cdf0e10cSrcweir } 465*cdf0e10cSrcweir 466*cdf0e10cSrcweir } // end of anonymous namespace 467*cdf0e10cSrcweir 468*cdf0e10cSrcweir 469*cdf0e10cSrcweir 470*cdf0e10cSrcweir 471*cdf0e10cSrcweir //===== PresenterTimer ======================================================== 472*cdf0e10cSrcweir 473*cdf0e10cSrcweir 474*cdf0e10cSrcweir ::rtl::Reference<PresenterClockTimer> PresenterClockTimer::mpInstance; 475*cdf0e10cSrcweir 476*cdf0e10cSrcweir ::rtl::Reference<PresenterClockTimer> PresenterClockTimer::Instance ( 477*cdf0e10cSrcweir const css::uno::Reference<css::uno::XComponentContext>& rxContext) 478*cdf0e10cSrcweir { 479*cdf0e10cSrcweir ::osl::MutexGuard aSolarGuard (::osl::Mutex::getGlobalMutex()); 480*cdf0e10cSrcweir 481*cdf0e10cSrcweir ::rtl::Reference<PresenterClockTimer> pTimer; 482*cdf0e10cSrcweir if (mpInstance.is()) 483*cdf0e10cSrcweir { 484*cdf0e10cSrcweir pTimer = mpInstance; 485*cdf0e10cSrcweir } 486*cdf0e10cSrcweir if ( ! pTimer.is()) 487*cdf0e10cSrcweir { 488*cdf0e10cSrcweir pTimer = ::rtl::Reference<PresenterClockTimer>(new PresenterClockTimer(rxContext)); 489*cdf0e10cSrcweir mpInstance = pTimer; 490*cdf0e10cSrcweir } 491*cdf0e10cSrcweir return pTimer; 492*cdf0e10cSrcweir } 493*cdf0e10cSrcweir 494*cdf0e10cSrcweir 495*cdf0e10cSrcweir 496*cdf0e10cSrcweir 497*cdf0e10cSrcweir PresenterClockTimer::PresenterClockTimer (const Reference<XComponentContext>& rxContext) 498*cdf0e10cSrcweir : PresenterClockTimerInterfaceBase(m_aMutex), 499*cdf0e10cSrcweir maListeners(), 500*cdf0e10cSrcweir maDateTime(), 501*cdf0e10cSrcweir mnTimerTaskId(PresenterTimer::NotAValidTaskId), 502*cdf0e10cSrcweir mbIsCallbackPending(false), 503*cdf0e10cSrcweir mxRequestCallback() 504*cdf0e10cSrcweir { 505*cdf0e10cSrcweir Reference<lang::XMultiComponentFactory> xFactory ( 506*cdf0e10cSrcweir rxContext->getServiceManager(), UNO_QUERY); 507*cdf0e10cSrcweir if (xFactory.is()) 508*cdf0e10cSrcweir mxRequestCallback = Reference<awt::XRequestCallback>( 509*cdf0e10cSrcweir xFactory->createInstanceWithContext( 510*cdf0e10cSrcweir A2S("com.sun.star.awt.AsyncCallback"), 511*cdf0e10cSrcweir rxContext), 512*cdf0e10cSrcweir UNO_QUERY_THROW); 513*cdf0e10cSrcweir } 514*cdf0e10cSrcweir 515*cdf0e10cSrcweir 516*cdf0e10cSrcweir 517*cdf0e10cSrcweir 518*cdf0e10cSrcweir PresenterClockTimer::~PresenterClockTimer (void) 519*cdf0e10cSrcweir { 520*cdf0e10cSrcweir if (mnTimerTaskId != PresenterTimer::NotAValidTaskId) 521*cdf0e10cSrcweir { 522*cdf0e10cSrcweir PresenterTimer::CancelTask(mnTimerTaskId); 523*cdf0e10cSrcweir mnTimerTaskId = PresenterTimer::NotAValidTaskId; 524*cdf0e10cSrcweir } 525*cdf0e10cSrcweir 526*cdf0e10cSrcweir Reference<lang::XComponent> xComponent (mxRequestCallback, UNO_QUERY); 527*cdf0e10cSrcweir if (xComponent.is()) 528*cdf0e10cSrcweir xComponent->dispose(); 529*cdf0e10cSrcweir mxRequestCallback = NULL; 530*cdf0e10cSrcweir } 531*cdf0e10cSrcweir 532*cdf0e10cSrcweir 533*cdf0e10cSrcweir 534*cdf0e10cSrcweir 535*cdf0e10cSrcweir void PresenterClockTimer::AddListener (const SharedListener& rListener) 536*cdf0e10cSrcweir { 537*cdf0e10cSrcweir osl::MutexGuard aGuard (maMutex); 538*cdf0e10cSrcweir 539*cdf0e10cSrcweir maListeners.push_back(rListener); 540*cdf0e10cSrcweir 541*cdf0e10cSrcweir // Create a timer task when the first listener is added. 542*cdf0e10cSrcweir if (mnTimerTaskId==PresenterTimer::NotAValidTaskId) 543*cdf0e10cSrcweir { 544*cdf0e10cSrcweir mnTimerTaskId = PresenterTimer::ScheduleRepeatedTask( 545*cdf0e10cSrcweir ::boost::bind(&PresenterClockTimer::CheckCurrentTime, this, _1), 546*cdf0e10cSrcweir 0, 547*cdf0e10cSrcweir 250000000 /*ns*/); 548*cdf0e10cSrcweir } 549*cdf0e10cSrcweir } 550*cdf0e10cSrcweir 551*cdf0e10cSrcweir 552*cdf0e10cSrcweir 553*cdf0e10cSrcweir 554*cdf0e10cSrcweir void PresenterClockTimer::RemoveListener (const SharedListener& rListener) 555*cdf0e10cSrcweir { 556*cdf0e10cSrcweir osl::MutexGuard aGuard (maMutex); 557*cdf0e10cSrcweir 558*cdf0e10cSrcweir ListenerContainer::iterator iListener (::std::find( 559*cdf0e10cSrcweir maListeners.begin(), 560*cdf0e10cSrcweir maListeners.end(), 561*cdf0e10cSrcweir rListener)); 562*cdf0e10cSrcweir if (iListener != maListeners.end()) 563*cdf0e10cSrcweir maListeners.erase(iListener); 564*cdf0e10cSrcweir if (maListeners.size() == 0) 565*cdf0e10cSrcweir { 566*cdf0e10cSrcweir // We have no more clients and therefore are not interested in time changes. 567*cdf0e10cSrcweir if (mnTimerTaskId != PresenterTimer::NotAValidTaskId) 568*cdf0e10cSrcweir { 569*cdf0e10cSrcweir PresenterTimer::CancelTask(mnTimerTaskId); 570*cdf0e10cSrcweir mnTimerTaskId = PresenterTimer::NotAValidTaskId; 571*cdf0e10cSrcweir } 572*cdf0e10cSrcweir mpInstance = NULL; 573*cdf0e10cSrcweir } 574*cdf0e10cSrcweir } 575*cdf0e10cSrcweir 576*cdf0e10cSrcweir 577*cdf0e10cSrcweir 578*cdf0e10cSrcweir 579*cdf0e10cSrcweir oslDateTime PresenterClockTimer::GetCurrentTime (void) 580*cdf0e10cSrcweir { 581*cdf0e10cSrcweir TimeValue aCurrentTime; 582*cdf0e10cSrcweir TimerScheduler::GetCurrentTime(aCurrentTime); 583*cdf0e10cSrcweir oslDateTime aDateTime; 584*cdf0e10cSrcweir osl_getDateTimeFromTimeValue(&aCurrentTime, &aDateTime); 585*cdf0e10cSrcweir return aDateTime; 586*cdf0e10cSrcweir } 587*cdf0e10cSrcweir 588*cdf0e10cSrcweir 589*cdf0e10cSrcweir 590*cdf0e10cSrcweir 591*cdf0e10cSrcweir sal_Int64 PresenterClockTimer::GetTimeDifference ( 592*cdf0e10cSrcweir const oslDateTime& rNow, 593*cdf0e10cSrcweir const oslDateTime& rThen) 594*cdf0e10cSrcweir { 595*cdf0e10cSrcweir TimeValue aNow; 596*cdf0e10cSrcweir TimeValue aThen; 597*cdf0e10cSrcweir if (osl_getTimeValueFromDateTime(const_cast<oslDateTime*>(&rNow),&aNow) 598*cdf0e10cSrcweir && osl_getTimeValueFromDateTime(const_cast<oslDateTime*>(&rThen),&aThen)) 599*cdf0e10cSrcweir { 600*cdf0e10cSrcweir return TimerScheduler::GetTimeDifference(aNow, aThen); 601*cdf0e10cSrcweir } 602*cdf0e10cSrcweir else 603*cdf0e10cSrcweir return -1; 604*cdf0e10cSrcweir } 605*cdf0e10cSrcweir 606*cdf0e10cSrcweir 607*cdf0e10cSrcweir 608*cdf0e10cSrcweir 609*cdf0e10cSrcweir void PresenterClockTimer::CheckCurrentTime (const TimeValue& rCurrentTime) 610*cdf0e10cSrcweir { 611*cdf0e10cSrcweir css::uno::Reference<css::awt::XRequestCallback> xRequestCallback; 612*cdf0e10cSrcweir css::uno::Reference<css::awt::XCallback> xCallback; 613*cdf0e10cSrcweir { 614*cdf0e10cSrcweir osl::MutexGuard aGuard (maMutex); 615*cdf0e10cSrcweir 616*cdf0e10cSrcweir TimeValue aCurrentTime (rCurrentTime); 617*cdf0e10cSrcweir oslDateTime aDateTime; 618*cdf0e10cSrcweir if (osl_getDateTimeFromTimeValue(&aCurrentTime, &aDateTime)) 619*cdf0e10cSrcweir { 620*cdf0e10cSrcweir if (aDateTime.Seconds != maDateTime.Seconds 621*cdf0e10cSrcweir || aDateTime.Minutes != maDateTime.Minutes 622*cdf0e10cSrcweir || aDateTime.Seconds != maDateTime.Seconds) 623*cdf0e10cSrcweir { 624*cdf0e10cSrcweir // The displayed part of the current time has changed. 625*cdf0e10cSrcweir // Prepare to call the listeners. 626*cdf0e10cSrcweir maDateTime = aDateTime; 627*cdf0e10cSrcweir 628*cdf0e10cSrcweir // Schedule notification of listeners. 629*cdf0e10cSrcweir if (mxRequestCallback.is() && ! mbIsCallbackPending) 630*cdf0e10cSrcweir { 631*cdf0e10cSrcweir mbIsCallbackPending = true; 632*cdf0e10cSrcweir xRequestCallback = mxRequestCallback; 633*cdf0e10cSrcweir xCallback = this; 634*cdf0e10cSrcweir } 635*cdf0e10cSrcweir } 636*cdf0e10cSrcweir } 637*cdf0e10cSrcweir } 638*cdf0e10cSrcweir if (mxRequestCallback.is() && xCallback.is()) 639*cdf0e10cSrcweir xRequestCallback->addCallback(xCallback, Any()); 640*cdf0e10cSrcweir } 641*cdf0e10cSrcweir 642*cdf0e10cSrcweir 643*cdf0e10cSrcweir 644*cdf0e10cSrcweir 645*cdf0e10cSrcweir //----- XCallback ------------------------------------------------------------- 646*cdf0e10cSrcweir 647*cdf0e10cSrcweir void SAL_CALL PresenterClockTimer::notify (const css::uno::Any& rUserData) 648*cdf0e10cSrcweir throw (css::uno::RuntimeException) 649*cdf0e10cSrcweir { 650*cdf0e10cSrcweir (void)rUserData; 651*cdf0e10cSrcweir 652*cdf0e10cSrcweir ListenerContainer aListenerCopy (maListeners); 653*cdf0e10cSrcweir 654*cdf0e10cSrcweir { 655*cdf0e10cSrcweir osl::MutexGuard aGuard (maMutex); 656*cdf0e10cSrcweir 657*cdf0e10cSrcweir mbIsCallbackPending = false; 658*cdf0e10cSrcweir 659*cdf0e10cSrcweir ::std::copy( 660*cdf0e10cSrcweir maListeners.begin(), 661*cdf0e10cSrcweir maListeners.end(), 662*cdf0e10cSrcweir ::std::back_inserter(aListenerCopy)); 663*cdf0e10cSrcweir } 664*cdf0e10cSrcweir 665*cdf0e10cSrcweir if (aListenerCopy.size() > 0) 666*cdf0e10cSrcweir { 667*cdf0e10cSrcweir ListenerContainer::const_iterator iListener; 668*cdf0e10cSrcweir ListenerContainer::const_iterator iEnd (aListenerCopy.end()); 669*cdf0e10cSrcweir for (iListener=aListenerCopy.begin(); iListener!=iEnd; ++iListener) 670*cdf0e10cSrcweir { 671*cdf0e10cSrcweir (*iListener)->TimeHasChanged(maDateTime); 672*cdf0e10cSrcweir } 673*cdf0e10cSrcweir } 674*cdf0e10cSrcweir } 675*cdf0e10cSrcweir 676*cdf0e10cSrcweir 677*cdf0e10cSrcweir 678*cdf0e10cSrcweir } } // end of namespace ::sdext::presenter 679