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 .
20 #include <sal/config.h>
21 #include <sal/log.hxx>
23 #include <tools/time.hxx>
26 #include <win/saldata.hxx>
27 #include <win/saltimer.h>
28 #include <win/salinst.h>
30 void CALLBACK
SalTimerProc(PVOID pParameter
, BOOLEAN bTimerOrWaitFired
);
32 // See http://msdn.microsoft.com/en-us/library/windows/desktop/ms687003%28v=vs.85%29.aspx
33 // (and related pages) for details about the Timer Queues.
35 // in order to prevent concurrent execution of ImplSalStartTimer and double
36 // deletion of timer (which is extremely likely, given that
37 // INVALID_HANDLE_VALUE waits for the callback to run on the main thread),
38 // this must run on the main thread too
39 void WinSalTimer::ImplStop()
41 SalData
*const pSalData
= GetSalData();
42 const WinSalInstance
*pInst
= pSalData
->mpInstance
;
43 assert( !pInst
|| pSalData
->mnAppThreadId
== GetCurrentThreadId() );
45 if ( m_bSetTimerRunning
)
47 m_bSetTimerRunning
= false;
48 KillTimer( pInst
->mhComWnd
, m_aWmTimerId
);
50 m_bDirectTimeout
= false;
52 const HANDLE hTimer
= m_nTimerId
;
53 if ( nullptr == hTimer
)
57 DeleteTimerQueueTimer( nullptr, hTimer
, INVALID_HANDLE_VALUE
);
58 // Keep InvalidateEvent after DeleteTimerQueueTimer, because the event id
59 // is set in SalTimerProc, which DeleteTimerQueueTimer will finish or cancel.
63 void WinSalTimer::ImplStart( sal_uInt64 nMS
)
66 SalData
* pSalData
= GetSalData();
67 assert( !pSalData
->mpInstance
|| pSalData
->mnAppThreadId
== GetCurrentThreadId() );
70 // DueTime parameter is a DWORD, which is always an unsigned 32bit
71 if (nMS
> SAL_MAX_UINT32
)
74 // cannot change a one-shot timer, so delete it and create a new one
77 // directly indicate an elapsed timer
78 m_bDirectTimeout
= ( 0 == nMS
);
79 // probably WT_EXECUTEONLYONCE is not needed, but it enforces Period
80 // to be 0 and should not hurt; also see
81 // https://www.microsoft.com/msj/0499/pooling/pooling.aspx
82 if ( !m_bDirectTimeout
)
83 CreateTimerQueueTimer(&m_nTimerId
, nullptr, SalTimerProc
, this,
84 nMS
, 0, WT_EXECUTEINTIMERTHREAD
| WT_EXECUTEONLYONCE
);
85 else if ( m_bForceRealTimer
)
87 // so we don't block the nested message queue in move and resize
88 // with posted 0ms SAL_MSG_TIMER_CALLBACK messages
89 SetTimer( GetSalData()->mpInstance
->mhComWnd
, m_aWmTimerId
,
90 USER_TIMER_MINIMUM
, nullptr );
91 m_bSetTimerRunning
= true;
93 // we don't need any wakeup message, as this code can just run in the
97 WinSalTimer::WinSalTimer()
98 : m_nTimerId( nullptr )
99 , m_bDirectTimeout( false )
100 , m_bForceRealTimer( false )
101 , m_bSetTimerRunning( false )
105 WinSalTimer::~WinSalTimer()
110 void WinSalTimer::Start( sal_uInt64 nMS
)
112 WinSalInstance
*pInst
= GetSalData()->mpInstance
;
113 if ( pInst
&& !pInst
->IsMainThread() )
115 bool const ret
= PostMessageW(pInst
->mhComWnd
,
116 SAL_MSG_STARTTIMER
, 0, static_cast<LPARAM
>(tools::Time::GetSystemTicks()) + nMS
);
117 SAL_WARN_IF(!ret
, "vcl", "ERROR: PostMessage() failed!");
123 void WinSalTimer::Stop()
125 WinSalInstance
*pInst
= GetSalData()->mpInstance
;
126 if ( pInst
&& !pInst
->IsMainThread() )
128 bool const ret
= PostMessageW(pInst
->mhComWnd
,
129 SAL_MSG_STOPTIMER
, 0, 0);
130 SAL_WARN_IF(!ret
, "vcl", "ERROR: PostMessage() failed!");
137 * This gets invoked from a Timer Queue thread.
138 * Don't acquire the SolarMutex to avoid deadlocks.
140 void CALLBACK
SalTimerProc(PVOID data
, BOOLEAN
)
144 WinSalTimer
*pTimer
= static_cast<WinSalTimer
*>( data
);
145 bool const ret
= PostMessageW(
146 GetSalData()->mpInstance
->mhComWnd
, SAL_MSG_TIMER_CALLBACK
,
147 static_cast<WPARAM
>(pTimer
->GetNextEventVersion()), 0 );
148 #if OSL_DEBUG_LEVEL > 0
149 if (!ret
) // SEH prevents using SAL_WARN here?
150 fputs("ERROR: PostMessage() failed!\n", stderr
);
153 __except(WinSalInstance::WorkaroundExceptionHandlingInUSER32Lib(GetExceptionCode(), GetExceptionInformation()))
158 void WinSalTimer::ImplHandleElapsedTimer()
160 // Test for MouseLeave
163 m_bDirectTimeout
= false;
164 ImplSalYieldMutexAcquireWithWait();
166 ImplSalYieldMutexRelease();
169 void WinSalTimer::ImplHandleTimerEvent( const WPARAM aWPARAM
)
171 assert( aWPARAM
<= SAL_MAX_INT32
);
172 if ( !IsValidEventVersion( static_cast<sal_Int32
>( aWPARAM
) ) )
175 ImplHandleElapsedTimer();
178 void WinSalTimer::SetForceRealTimer( const bool bVal
)
180 if ( m_bForceRealTimer
== bVal
)
183 m_bForceRealTimer
= bVal
;
185 // we need a real timer, as m_bDirectTimeout won't be processed
186 if ( bVal
&& m_bDirectTimeout
)
190 void WinSalTimer::ImplHandle_WM_TIMER( const WPARAM aWPARAM
)
192 assert( m_aWmTimerId
== aWPARAM
);
193 if ( !(m_aWmTimerId
== aWPARAM
&& m_bSetTimerRunning
) )
196 m_bSetTimerRunning
= false;
197 KillTimer( GetSalData()->mpInstance
->mhComWnd
, m_aWmTimerId
);
198 ImplHandleElapsedTimer();
201 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */