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>
22 #include <tools/time.hxx>
25 #include <win/saldata.hxx>
26 #include <win/saltimer.h>
27 #include <win/salinst.h>
29 void CALLBACK
SalTimerProc(PVOID pParameter
, BOOLEAN bTimerOrWaitFired
);
31 // See http://msdn.microsoft.com/en-us/library/windows/desktop/ms687003%28v=vs.85%29.aspx
32 // (and related pages) for details about the Timer Queues.
34 // in order to prevent concurrent execution of ImplSalStartTimer and double
35 // deletion of timer (which is extremely likely, given that
36 // INVALID_HANDLE_VALUE waits for the callback to run on the main thread),
37 // this must run on the main thread too
38 void WinSalTimer::ImplStop()
40 SalData
*const pSalData
= GetSalData();
41 const WinSalInstance
*pInst
= pSalData
->mpInstance
;
42 assert( !pInst
|| pSalData
->mnAppThreadId
== GetCurrentThreadId() );
44 if ( m_bForceRealTimer
&& m_bDirectTimeout
)
45 KillTimer( GetSalData()->mpInstance
->mhComWnd
, m_aWmTimerId
);
46 m_bDirectTimeout
= false;
48 const HANDLE hTimer
= m_nTimerId
;
49 if ( nullptr == hTimer
)
53 DeleteTimerQueueTimer( nullptr, hTimer
, INVALID_HANDLE_VALUE
);
54 // Keep InvalidateEvent after DeleteTimerQueueTimer, because the event id
55 // is set in SalTimerProc, which DeleteTimerQueueTimer will finish or cancel.
59 void WinSalTimer::ImplStart( sal_uLong nMS
)
61 SalData
* pSalData
= GetSalData();
62 assert( !pSalData
->mpInstance
|| pSalData
->mnAppThreadId
== GetCurrentThreadId() );
64 // DueTime parameter is a DWORD, which is always an unsigned 32bit
65 if (nMS
> SAL_MAX_UINT32
)
68 // cannot change a one-shot timer, so delete it and create a new one
71 // directly indicate an elapsed timer
72 m_bDirectTimeout
= ( 0 == nMS
);
73 // probably WT_EXECUTEONLYONCE is not needed, but it enforces Period
74 // to be 0 and should not hurt; also see
75 // https://www.microsoft.com/msj/0499/pooling/pooling.aspx
76 if ( !m_bDirectTimeout
)
77 CreateTimerQueueTimer(&m_nTimerId
, nullptr, SalTimerProc
, this,
78 nMS
, 0, WT_EXECUTEINTIMERTHREAD
| WT_EXECUTEONLYONCE
);
79 else if ( m_bForceRealTimer
)
81 // so we don't block the nested message queue in move and resize
82 // with posted 0ms SAL_MSG_TIMER_CALLBACK messages
83 SetTimer( GetSalData()->mpInstance
->mhComWnd
, m_aWmTimerId
,
84 USER_TIMER_MINIMUM
, nullptr );
86 // we don't need any wakeup message, as this code can just run in the
90 WinSalTimer::WinSalTimer()
91 : m_nTimerId( nullptr )
92 , m_bDirectTimeout( false )
93 , m_bForceRealTimer( false )
97 WinSalTimer::~WinSalTimer()
102 void WinSalTimer::Start( sal_uLong nMS
)
104 WinSalInstance
*pInst
= GetSalData()->mpInstance
;
105 if ( pInst
&& !pInst
->IsMainThread() )
107 BOOL
const ret
= PostMessageW(pInst
->mhComWnd
,
108 SAL_MSG_STARTTIMER
, 0, static_cast<LPARAM
>(tools::Time::GetSystemTicks()) + nMS
);
109 SAL_WARN_IF(0 == ret
, "vcl", "ERROR: PostMessage() failed!");
115 void WinSalTimer::Stop()
117 WinSalInstance
*pInst
= GetSalData()->mpInstance
;
118 if ( pInst
&& !pInst
->IsMainThread() )
120 BOOL
const ret
= PostMessageW(pInst
->mhComWnd
,
121 SAL_MSG_STOPTIMER
, 0, 0);
122 SAL_WARN_IF(0 == ret
, "vcl", "ERROR: PostMessage() failed!");
129 * This gets invoked from a Timer Queue thread.
130 * Don't acquire the SolarMutex to avoid deadlocks.
132 void CALLBACK
SalTimerProc(PVOID data
, BOOLEAN
)
136 WinSalTimer
*pTimer
= static_cast<WinSalTimer
*>( data
);
137 BOOL
const ret
= PostMessageW(
138 GetSalData()->mpInstance
->mhComWnd
, SAL_MSG_TIMER_CALLBACK
,
139 static_cast<WPARAM
>(pTimer
->GetNextEventVersion()), 0 );
140 #if OSL_DEBUG_LEVEL > 0
141 if (0 == ret
) // SEH prevents using SAL_WARN here?
142 fputs("ERROR: PostMessage() failed!\n", stderr
);
145 __except(WinSalInstance::WorkaroundExceptionHandlingInUSER32Lib(GetExceptionCode(), GetExceptionInformation()))
150 void WinSalTimer::ImplHandleElapsedTimer()
152 // Test for MouseLeave
155 m_bDirectTimeout
= false;
156 ImplSalYieldMutexAcquireWithWait();
158 ImplSalYieldMutexRelease();
161 bool WinSalTimer::ImplHandleTimerEvent( const WPARAM aWPARAM
)
163 assert( aWPARAM
<= SAL_MAX_INT32
);
164 if ( !IsValidEventVersion( static_cast<sal_Int32
>( aWPARAM
) ) )
167 ImplHandleElapsedTimer();
171 void WinSalTimer::SetForceRealTimer( const bool bVal
)
173 if ( m_bForceRealTimer
== bVal
)
176 m_bForceRealTimer
= bVal
;
178 // we need a real timer, as m_bDirectTimeout won't be processed
179 if ( bVal
&& m_bDirectTimeout
)
183 bool WinSalTimer::ImplHandle_WM_TIMER( const WPARAM aWPARAM
)
185 assert( m_aWmTimerId
== aWPARAM
);
186 if ( !(m_aWmTimerId
== aWPARAM
&& m_bDirectTimeout
&& m_bForceRealTimer
) )
189 ImplHandleElapsedTimer();
193 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */