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_vcl.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include <tools/time.hxx> 32*cdf0e10cSrcweir #include <tools/debug.hxx> 33*cdf0e10cSrcweir 34*cdf0e10cSrcweir #include <vcl/svapp.hxx> 35*cdf0e10cSrcweir #include <vcl/timer.hxx> 36*cdf0e10cSrcweir 37*cdf0e10cSrcweir #include <saltimer.hxx> 38*cdf0e10cSrcweir #include <svdata.hxx> 39*cdf0e10cSrcweir #include <salinst.hxx> 40*cdf0e10cSrcweir 41*cdf0e10cSrcweir 42*cdf0e10cSrcweir // ======================================================================= 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir #define MAX_TIMER_PERIOD ((sal_uLong)0xFFFFFFFF) 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir // --------------------- 47*cdf0e10cSrcweir // - TimeManager-Types - 48*cdf0e10cSrcweir // --------------------- 49*cdf0e10cSrcweir 50*cdf0e10cSrcweir struct ImplTimerData 51*cdf0e10cSrcweir { 52*cdf0e10cSrcweir ImplTimerData* mpNext; // Pointer to the next Instance 53*cdf0e10cSrcweir Timer* mpSVTimer; // Pointer to SV Timer instance 54*cdf0e10cSrcweir sal_uLong mnUpdateTime; // Last Update Time 55*cdf0e10cSrcweir sal_uLong mnTimerUpdate; // TimerCallbackProcs on stack 56*cdf0e10cSrcweir sal_Bool mbDelete; // Wurde Timer waehren Update() geloescht 57*cdf0e10cSrcweir sal_Bool mbInTimeout; // Befinden wir uns im Timeout-Handler 58*cdf0e10cSrcweir }; 59*cdf0e10cSrcweir 60*cdf0e10cSrcweir // ======================================================================= 61*cdf0e10cSrcweir 62*cdf0e10cSrcweir void Timer::ImplDeInitTimer() 63*cdf0e10cSrcweir { 64*cdf0e10cSrcweir ImplSVData* pSVData = ImplGetSVData(); 65*cdf0e10cSrcweir ImplTimerData* pTimerData = pSVData->mpFirstTimerData; 66*cdf0e10cSrcweir 67*cdf0e10cSrcweir if ( pTimerData ) 68*cdf0e10cSrcweir { 69*cdf0e10cSrcweir do 70*cdf0e10cSrcweir { 71*cdf0e10cSrcweir ImplTimerData* pTempTimerData = pTimerData; 72*cdf0e10cSrcweir if ( pTimerData->mpSVTimer ) 73*cdf0e10cSrcweir { 74*cdf0e10cSrcweir pTimerData->mpSVTimer->mbActive = sal_False; 75*cdf0e10cSrcweir pTimerData->mpSVTimer->mpTimerData = NULL; 76*cdf0e10cSrcweir } 77*cdf0e10cSrcweir pTimerData = pTimerData->mpNext; 78*cdf0e10cSrcweir delete pTempTimerData; 79*cdf0e10cSrcweir } 80*cdf0e10cSrcweir while ( pTimerData ); 81*cdf0e10cSrcweir 82*cdf0e10cSrcweir pSVData->mpFirstTimerData = NULL; 83*cdf0e10cSrcweir pSVData->mnTimerPeriod = 0; 84*cdf0e10cSrcweir delete pSVData->mpSalTimer; 85*cdf0e10cSrcweir pSVData->mpSalTimer = NULL; 86*cdf0e10cSrcweir } 87*cdf0e10cSrcweir } 88*cdf0e10cSrcweir 89*cdf0e10cSrcweir // ----------------------------------------------------------------------- 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir static void ImplStartTimer( ImplSVData* pSVData, sal_uLong nMS ) 92*cdf0e10cSrcweir { 93*cdf0e10cSrcweir if ( !nMS ) 94*cdf0e10cSrcweir nMS = 1; 95*cdf0e10cSrcweir 96*cdf0e10cSrcweir if ( nMS != pSVData->mnTimerPeriod ) 97*cdf0e10cSrcweir { 98*cdf0e10cSrcweir pSVData->mnTimerPeriod = nMS; 99*cdf0e10cSrcweir pSVData->mpSalTimer->Start( nMS ); 100*cdf0e10cSrcweir } 101*cdf0e10cSrcweir } 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir // ----------------------------------------------------------------------- 104*cdf0e10cSrcweir 105*cdf0e10cSrcweir void Timer::ImplTimerCallbackProc() 106*cdf0e10cSrcweir { 107*cdf0e10cSrcweir ImplSVData* pSVData = ImplGetSVData(); 108*cdf0e10cSrcweir ImplTimerData* pTimerData; 109*cdf0e10cSrcweir ImplTimerData* pPrevTimerData; 110*cdf0e10cSrcweir sal_uLong nMinPeriod = MAX_TIMER_PERIOD; 111*cdf0e10cSrcweir sal_uLong nDeltaTime; 112*cdf0e10cSrcweir sal_uLong nTime = Time::GetSystemTicks(); 113*cdf0e10cSrcweir 114*cdf0e10cSrcweir if ( pSVData->mbNoCallTimer ) 115*cdf0e10cSrcweir return; 116*cdf0e10cSrcweir 117*cdf0e10cSrcweir pSVData->mnTimerUpdate++; 118*cdf0e10cSrcweir pSVData->mbNotAllTimerCalled = sal_True; 119*cdf0e10cSrcweir 120*cdf0e10cSrcweir // Suche Timer raus, wo der Timeout-Handler gerufen werden muss 121*cdf0e10cSrcweir pTimerData = pSVData->mpFirstTimerData; 122*cdf0e10cSrcweir while ( pTimerData ) 123*cdf0e10cSrcweir { 124*cdf0e10cSrcweir // Wenn Timer noch nicht neu ist und noch nicht geloescht wurde 125*cdf0e10cSrcweir // und er sich nicht im Timeout-Handler befindet, 126*cdf0e10cSrcweir // dann den Handler rufen, wenn die Zeit abgelaufen ist 127*cdf0e10cSrcweir if ( (pTimerData->mnTimerUpdate < pSVData->mnTimerUpdate) && 128*cdf0e10cSrcweir !pTimerData->mbDelete && !pTimerData->mbInTimeout ) 129*cdf0e10cSrcweir { 130*cdf0e10cSrcweir // Zeit abgelaufen 131*cdf0e10cSrcweir if ( (pTimerData->mnUpdateTime+pTimerData->mpSVTimer->mnTimeout) <= nTime ) 132*cdf0e10cSrcweir { 133*cdf0e10cSrcweir // Neue Updatezeit setzen 134*cdf0e10cSrcweir pTimerData->mnUpdateTime = nTime; 135*cdf0e10cSrcweir 136*cdf0e10cSrcweir // kein AutoTimer, dann anhalten 137*cdf0e10cSrcweir if ( !pTimerData->mpSVTimer->mbAuto ) 138*cdf0e10cSrcweir { 139*cdf0e10cSrcweir pTimerData->mpSVTimer->mbActive = sal_False; 140*cdf0e10cSrcweir pTimerData->mbDelete = sal_True; 141*cdf0e10cSrcweir } 142*cdf0e10cSrcweir 143*cdf0e10cSrcweir // call Timeout 144*cdf0e10cSrcweir pTimerData->mbInTimeout = sal_True; 145*cdf0e10cSrcweir pTimerData->mpSVTimer->Timeout(); 146*cdf0e10cSrcweir pTimerData->mbInTimeout = sal_False; 147*cdf0e10cSrcweir } 148*cdf0e10cSrcweir } 149*cdf0e10cSrcweir 150*cdf0e10cSrcweir pTimerData = pTimerData->mpNext; 151*cdf0e10cSrcweir } 152*cdf0e10cSrcweir 153*cdf0e10cSrcweir // Neue Zeit ermitteln 154*cdf0e10cSrcweir sal_uLong nNewTime = Time::GetSystemTicks(); 155*cdf0e10cSrcweir pPrevTimerData = NULL; 156*cdf0e10cSrcweir pTimerData = pSVData->mpFirstTimerData; 157*cdf0e10cSrcweir while ( pTimerData ) 158*cdf0e10cSrcweir { 159*cdf0e10cSrcweir // Befindet sich Timer noch im Timeout-Handler, dann ignorieren 160*cdf0e10cSrcweir if ( pTimerData->mbInTimeout ) 161*cdf0e10cSrcweir { 162*cdf0e10cSrcweir pPrevTimerData = pTimerData; 163*cdf0e10cSrcweir pTimerData = pTimerData->mpNext; 164*cdf0e10cSrcweir } 165*cdf0e10cSrcweir // Wurde Timer zwischenzeitlich zerstoert ? 166*cdf0e10cSrcweir else if ( pTimerData->mbDelete ) 167*cdf0e10cSrcweir { 168*cdf0e10cSrcweir if ( pPrevTimerData ) 169*cdf0e10cSrcweir pPrevTimerData->mpNext = pTimerData->mpNext; 170*cdf0e10cSrcweir else 171*cdf0e10cSrcweir pSVData->mpFirstTimerData = pTimerData->mpNext; 172*cdf0e10cSrcweir if ( pTimerData->mpSVTimer ) 173*cdf0e10cSrcweir pTimerData->mpSVTimer->mpTimerData = NULL; 174*cdf0e10cSrcweir ImplTimerData* pTempTimerData = pTimerData; 175*cdf0e10cSrcweir pTimerData = pTimerData->mpNext; 176*cdf0e10cSrcweir delete pTempTimerData; 177*cdf0e10cSrcweir } 178*cdf0e10cSrcweir else 179*cdf0e10cSrcweir { 180*cdf0e10cSrcweir pTimerData->mnTimerUpdate = 0; 181*cdf0e10cSrcweir // kleinste Zeitspanne ermitteln 182*cdf0e10cSrcweir if ( pTimerData->mnUpdateTime == nTime ) 183*cdf0e10cSrcweir { 184*cdf0e10cSrcweir nDeltaTime = pTimerData->mpSVTimer->mnTimeout; 185*cdf0e10cSrcweir if ( nDeltaTime < nMinPeriod ) 186*cdf0e10cSrcweir nMinPeriod = nDeltaTime; 187*cdf0e10cSrcweir } 188*cdf0e10cSrcweir else 189*cdf0e10cSrcweir { 190*cdf0e10cSrcweir nDeltaTime = pTimerData->mnUpdateTime + pTimerData->mpSVTimer->mnTimeout; 191*cdf0e10cSrcweir if ( nDeltaTime < nNewTime ) 192*cdf0e10cSrcweir nMinPeriod = 1; 193*cdf0e10cSrcweir else 194*cdf0e10cSrcweir { 195*cdf0e10cSrcweir nDeltaTime -= nNewTime; 196*cdf0e10cSrcweir if ( nDeltaTime < nMinPeriod ) 197*cdf0e10cSrcweir nMinPeriod = nDeltaTime; 198*cdf0e10cSrcweir } 199*cdf0e10cSrcweir } 200*cdf0e10cSrcweir pPrevTimerData = pTimerData; 201*cdf0e10cSrcweir pTimerData = pTimerData->mpNext; 202*cdf0e10cSrcweir } 203*cdf0e10cSrcweir } 204*cdf0e10cSrcweir 205*cdf0e10cSrcweir // Wenn keine Timer mehr existieren, dann Clock loeschen 206*cdf0e10cSrcweir if ( !pSVData->mpFirstTimerData ) 207*cdf0e10cSrcweir { 208*cdf0e10cSrcweir pSVData->mpSalTimer->Stop(); 209*cdf0e10cSrcweir pSVData->mnTimerPeriod = MAX_TIMER_PERIOD; 210*cdf0e10cSrcweir } 211*cdf0e10cSrcweir else 212*cdf0e10cSrcweir ImplStartTimer( pSVData, nMinPeriod ); 213*cdf0e10cSrcweir 214*cdf0e10cSrcweir pSVData->mnTimerUpdate--; 215*cdf0e10cSrcweir pSVData->mbNotAllTimerCalled = sal_False; 216*cdf0e10cSrcweir } 217*cdf0e10cSrcweir 218*cdf0e10cSrcweir // ======================================================================= 219*cdf0e10cSrcweir 220*cdf0e10cSrcweir Timer::Timer() 221*cdf0e10cSrcweir { 222*cdf0e10cSrcweir mpTimerData = NULL; 223*cdf0e10cSrcweir mnTimeout = 1; 224*cdf0e10cSrcweir mbAuto = sal_False; 225*cdf0e10cSrcweir mbActive = sal_False; 226*cdf0e10cSrcweir } 227*cdf0e10cSrcweir 228*cdf0e10cSrcweir // ----------------------------------------------------------------------- 229*cdf0e10cSrcweir 230*cdf0e10cSrcweir Timer::Timer( const Timer& rTimer ) 231*cdf0e10cSrcweir { 232*cdf0e10cSrcweir mpTimerData = NULL; 233*cdf0e10cSrcweir mnTimeout = rTimer.mnTimeout; 234*cdf0e10cSrcweir mbAuto = sal_False; 235*cdf0e10cSrcweir mbActive = sal_False; 236*cdf0e10cSrcweir maTimeoutHdl = rTimer.maTimeoutHdl; 237*cdf0e10cSrcweir 238*cdf0e10cSrcweir if ( rTimer.IsActive() ) 239*cdf0e10cSrcweir Start(); 240*cdf0e10cSrcweir } 241*cdf0e10cSrcweir 242*cdf0e10cSrcweir // ----------------------------------------------------------------------- 243*cdf0e10cSrcweir 244*cdf0e10cSrcweir Timer::~Timer() 245*cdf0e10cSrcweir { 246*cdf0e10cSrcweir if ( mpTimerData ) 247*cdf0e10cSrcweir { 248*cdf0e10cSrcweir mpTimerData->mbDelete = sal_True; 249*cdf0e10cSrcweir mpTimerData->mpSVTimer = NULL; 250*cdf0e10cSrcweir } 251*cdf0e10cSrcweir } 252*cdf0e10cSrcweir 253*cdf0e10cSrcweir // ----------------------------------------------------------------------- 254*cdf0e10cSrcweir 255*cdf0e10cSrcweir void Timer::Timeout() 256*cdf0e10cSrcweir { 257*cdf0e10cSrcweir maTimeoutHdl.Call( this ); 258*cdf0e10cSrcweir } 259*cdf0e10cSrcweir 260*cdf0e10cSrcweir // ----------------------------------------------------------------------- 261*cdf0e10cSrcweir 262*cdf0e10cSrcweir void Timer::SetTimeout( sal_uLong nNewTimeout ) 263*cdf0e10cSrcweir { 264*cdf0e10cSrcweir mnTimeout = nNewTimeout; 265*cdf0e10cSrcweir 266*cdf0e10cSrcweir // Wenn Timer aktiv, dann Clock erneuern 267*cdf0e10cSrcweir if ( mbActive ) 268*cdf0e10cSrcweir { 269*cdf0e10cSrcweir ImplSVData* pSVData = ImplGetSVData(); 270*cdf0e10cSrcweir if ( !pSVData->mnTimerUpdate && (mnTimeout < pSVData->mnTimerPeriod) ) 271*cdf0e10cSrcweir ImplStartTimer( pSVData, mnTimeout ); 272*cdf0e10cSrcweir } 273*cdf0e10cSrcweir } 274*cdf0e10cSrcweir 275*cdf0e10cSrcweir // ----------------------------------------------------------------------- 276*cdf0e10cSrcweir 277*cdf0e10cSrcweir void Timer::Start() 278*cdf0e10cSrcweir { 279*cdf0e10cSrcweir mbActive = sal_True; 280*cdf0e10cSrcweir 281*cdf0e10cSrcweir ImplSVData* pSVData = ImplGetSVData(); 282*cdf0e10cSrcweir if ( !mpTimerData ) 283*cdf0e10cSrcweir { 284*cdf0e10cSrcweir if ( !pSVData->mpFirstTimerData ) 285*cdf0e10cSrcweir { 286*cdf0e10cSrcweir pSVData->mnTimerPeriod = MAX_TIMER_PERIOD; 287*cdf0e10cSrcweir if( ! pSVData->mpSalTimer ) 288*cdf0e10cSrcweir { 289*cdf0e10cSrcweir pSVData->mpSalTimer = pSVData->mpDefInst->CreateSalTimer(); 290*cdf0e10cSrcweir pSVData->mpSalTimer->SetCallback( ImplTimerCallbackProc ); 291*cdf0e10cSrcweir } 292*cdf0e10cSrcweir } 293*cdf0e10cSrcweir 294*cdf0e10cSrcweir // insert timer and start 295*cdf0e10cSrcweir mpTimerData = new ImplTimerData; 296*cdf0e10cSrcweir mpTimerData->mpSVTimer = this; 297*cdf0e10cSrcweir mpTimerData->mnUpdateTime = Time::GetSystemTicks(); 298*cdf0e10cSrcweir mpTimerData->mnTimerUpdate = pSVData->mnTimerUpdate; 299*cdf0e10cSrcweir mpTimerData->mbDelete = sal_False; 300*cdf0e10cSrcweir mpTimerData->mbInTimeout = sal_False; 301*cdf0e10cSrcweir 302*cdf0e10cSrcweir // !!!!! Wegen SFX hinten einordnen !!!!! 303*cdf0e10cSrcweir ImplTimerData* pPrev = NULL; 304*cdf0e10cSrcweir ImplTimerData* pData = pSVData->mpFirstTimerData; 305*cdf0e10cSrcweir while ( pData ) 306*cdf0e10cSrcweir { 307*cdf0e10cSrcweir pPrev = pData; 308*cdf0e10cSrcweir pData = pData->mpNext; 309*cdf0e10cSrcweir } 310*cdf0e10cSrcweir mpTimerData->mpNext = NULL; 311*cdf0e10cSrcweir if ( pPrev ) 312*cdf0e10cSrcweir pPrev->mpNext = mpTimerData; 313*cdf0e10cSrcweir else 314*cdf0e10cSrcweir pSVData->mpFirstTimerData = mpTimerData; 315*cdf0e10cSrcweir 316*cdf0e10cSrcweir if ( mnTimeout < pSVData->mnTimerPeriod ) 317*cdf0e10cSrcweir ImplStartTimer( pSVData, mnTimeout ); 318*cdf0e10cSrcweir } 319*cdf0e10cSrcweir else if( !mpTimerData->mpSVTimer ) // TODO: remove when guilty found 320*cdf0e10cSrcweir { 321*cdf0e10cSrcweir DBG_ERROR( "Timer::Start() on a destroyed Timer!" ); 322*cdf0e10cSrcweir } 323*cdf0e10cSrcweir else 324*cdf0e10cSrcweir { 325*cdf0e10cSrcweir mpTimerData->mnUpdateTime = Time::GetSystemTicks(); 326*cdf0e10cSrcweir mpTimerData->mnTimerUpdate = pSVData->mnTimerUpdate; 327*cdf0e10cSrcweir mpTimerData->mbDelete = sal_False; 328*cdf0e10cSrcweir } 329*cdf0e10cSrcweir } 330*cdf0e10cSrcweir 331*cdf0e10cSrcweir // ----------------------------------------------------------------------- 332*cdf0e10cSrcweir 333*cdf0e10cSrcweir void Timer::Stop() 334*cdf0e10cSrcweir { 335*cdf0e10cSrcweir mbActive = sal_False; 336*cdf0e10cSrcweir 337*cdf0e10cSrcweir if ( mpTimerData ) 338*cdf0e10cSrcweir mpTimerData->mbDelete = sal_True; 339*cdf0e10cSrcweir } 340*cdf0e10cSrcweir 341*cdf0e10cSrcweir // ----------------------------------------------------------------------- 342*cdf0e10cSrcweir 343*cdf0e10cSrcweir Timer& Timer::operator=( const Timer& rTimer ) 344*cdf0e10cSrcweir { 345*cdf0e10cSrcweir if ( IsActive() ) 346*cdf0e10cSrcweir Stop(); 347*cdf0e10cSrcweir 348*cdf0e10cSrcweir mbActive = sal_False; 349*cdf0e10cSrcweir mnTimeout = rTimer.mnTimeout; 350*cdf0e10cSrcweir maTimeoutHdl = rTimer.maTimeoutHdl; 351*cdf0e10cSrcweir 352*cdf0e10cSrcweir if ( rTimer.IsActive() ) 353*cdf0e10cSrcweir Start(); 354*cdf0e10cSrcweir 355*cdf0e10cSrcweir return *this; 356*cdf0e10cSrcweir } 357*cdf0e10cSrcweir 358*cdf0e10cSrcweir // ======================================================================= 359*cdf0e10cSrcweir 360*cdf0e10cSrcweir AutoTimer::AutoTimer() 361*cdf0e10cSrcweir { 362*cdf0e10cSrcweir mbAuto = sal_True; 363*cdf0e10cSrcweir } 364*cdf0e10cSrcweir 365*cdf0e10cSrcweir // ----------------------------------------------------------------------- 366*cdf0e10cSrcweir 367*cdf0e10cSrcweir AutoTimer::AutoTimer( const AutoTimer& rTimer ) : Timer( rTimer ) 368*cdf0e10cSrcweir { 369*cdf0e10cSrcweir mbAuto = sal_True; 370*cdf0e10cSrcweir } 371*cdf0e10cSrcweir 372*cdf0e10cSrcweir // ----------------------------------------------------------------------- 373*cdf0e10cSrcweir 374*cdf0e10cSrcweir AutoTimer& AutoTimer::operator=( const AutoTimer& rTimer ) 375*cdf0e10cSrcweir { 376*cdf0e10cSrcweir Timer::operator=( rTimer ); 377*cdf0e10cSrcweir return *this; 378*cdf0e10cSrcweir } 379