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( GetSalData()->mpInstance
->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
)
65 SalData
* pSalData
= GetSalData();
66 assert( !pSalData
->mpInstance
|| pSalData
->mnAppThreadId
== GetCurrentThreadId() );
68 // DueTime parameter is a DWORD, which is always an unsigned 32bit
69 if (nMS
> SAL_MAX_UINT32
)
72 // cannot change a one-shot timer, so delete it and create a new one
75 // directly indicate an elapsed timer
76 m_bDirectTimeout
= ( 0 == nMS
);
77 // probably WT_EXECUTEONLYONCE is not needed, but it enforces Period
78 // to be 0 and should not hurt; also see
79 // https://www.microsoft.com/msj/0499/pooling/pooling.aspx
80 if ( !m_bDirectTimeout
)
81 CreateTimerQueueTimer(&m_nTimerId
, nullptr, SalTimerProc
, this,
82 nMS
, 0, WT_EXECUTEINTIMERTHREAD
| WT_EXECUTEONLYONCE
);
83 else if ( m_bForceRealTimer
)
85 // so we don't block the nested message queue in move and resize
86 // with posted 0ms SAL_MSG_TIMER_CALLBACK messages
87 SetTimer( GetSalData()->mpInstance
->mhComWnd
, m_aWmTimerId
,
88 USER_TIMER_MINIMUM
, nullptr );
89 m_bSetTimerRunning
= true;
91 // we don't need any wakeup message, as this code can just run in the
95 WinSalTimer::WinSalTimer()
96 : m_nTimerId( nullptr )
97 , m_bDirectTimeout( false )
98 , m_bForceRealTimer( false )
99 , m_bSetTimerRunning( false )
103 WinSalTimer::~WinSalTimer()
108 void WinSalTimer::Start( sal_uInt64 nMS
)
110 WinSalInstance
*pInst
= GetSalData()->mpInstance
;
111 if ( pInst
&& !pInst
->IsMainThread() )
113 bool const ret
= PostMessageW(pInst
->mhComWnd
,
114 SAL_MSG_STARTTIMER
, 0, static_cast<LPARAM
>(tools::Time::GetSystemTicks()) + nMS
);
115 SAL_WARN_IF(!ret
, "vcl", "ERROR: PostMessage() failed!");
121 void WinSalTimer::Stop()
123 WinSalInstance
*pInst
= GetSalData()->mpInstance
;
124 if ( pInst
&& !pInst
->IsMainThread() )
126 bool const ret
= PostMessageW(pInst
->mhComWnd
,
127 SAL_MSG_STOPTIMER
, 0, 0);
128 SAL_WARN_IF(!ret
, "vcl", "ERROR: PostMessage() failed!");
135 * This gets invoked from a Timer Queue thread.
136 * Don't acquire the SolarMutex to avoid deadlocks.
138 void CALLBACK
SalTimerProc(PVOID data
, BOOLEAN
)
142 WinSalTimer
*pTimer
= static_cast<WinSalTimer
*>( data
);
143 bool const ret
= PostMessageW(
144 GetSalData()->mpInstance
->mhComWnd
, SAL_MSG_TIMER_CALLBACK
,
145 static_cast<WPARAM
>(pTimer
->GetNextEventVersion()), 0 );
146 #if OSL_DEBUG_LEVEL > 0
147 if (!ret
) // SEH prevents using SAL_WARN here?
148 fputs("ERROR: PostMessage() failed!\n", stderr
);
151 __except(WinSalInstance::WorkaroundExceptionHandlingInUSER32Lib(GetExceptionCode(), GetExceptionInformation()))
156 void WinSalTimer::ImplHandleElapsedTimer()
158 // Test for MouseLeave
161 m_bDirectTimeout
= false;
162 ImplSalYieldMutexAcquireWithWait();
164 ImplSalYieldMutexRelease();
167 void WinSalTimer::ImplHandleTimerEvent( const WPARAM aWPARAM
)
169 assert( aWPARAM
<= SAL_MAX_INT32
);
170 if ( !IsValidEventVersion( static_cast<sal_Int32
>( aWPARAM
) ) )
173 ImplHandleElapsedTimer();
176 void WinSalTimer::SetForceRealTimer( const bool bVal
)
178 if ( m_bForceRealTimer
== bVal
)
181 m_bForceRealTimer
= bVal
;
183 // we need a real timer, as m_bDirectTimeout won't be processed
184 if ( bVal
&& m_bDirectTimeout
)
188 void WinSalTimer::ImplHandle_WM_TIMER( const WPARAM aWPARAM
)
190 assert( m_aWmTimerId
== aWPARAM
);
191 if ( !(m_aWmTimerId
== aWPARAM
&& m_bSetTimerRunning
) )
194 m_bSetTimerRunning
= false;
195 KillTimer( GetSalData()->mpInstance
->mhComWnd
, m_aWmTimerId
);
196 ImplHandleElapsedTimer();
199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */