tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / vcl / win / app / saltimer.cxx
blob834b183476e5ea6463f984325384804ac854db45
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
25 #include <svsys.h>
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 assert(pInst);
49 KillTimer( pInst->mhComWnd, m_aWmTimerId );
51 m_bDirectTimeout = false;
53 const HANDLE hTimer = m_nTimerId;
54 if ( nullptr == hTimer )
55 return;
57 m_nTimerId = nullptr;
58 DeleteTimerQueueTimer( nullptr, hTimer, INVALID_HANDLE_VALUE );
59 // Keep InvalidateEvent after DeleteTimerQueueTimer, because the event id
60 // is set in SalTimerProc, which DeleteTimerQueueTimer will finish or cancel.
61 InvalidateEvent();
64 void WinSalTimer::ImplStart( sal_uInt64 nMS )
66 #if !defined NDEBUG
67 SalData* pSalData = GetSalData();
68 assert( !pSalData->mpInstance || pSalData->mnAppThreadId == GetCurrentThreadId() );
69 #endif
71 // DueTime parameter is a DWORD, which is always an unsigned 32bit
72 if (nMS > SAL_MAX_UINT32)
73 nMS = SAL_MAX_UINT32;
75 // cannot change a one-shot timer, so delete it and create a new one
76 ImplStop();
78 // directly indicate an elapsed timer
79 m_bDirectTimeout = ( 0 == nMS );
80 // probably WT_EXECUTEONLYONCE is not needed, but it enforces Period
81 // to be 0 and should not hurt; also see
82 // https://www.microsoft.com/msj/0499/pooling/pooling.aspx
83 if ( !m_bDirectTimeout )
84 CreateTimerQueueTimer(&m_nTimerId, nullptr, SalTimerProc, this,
85 nMS, 0, WT_EXECUTEINTIMERTHREAD | WT_EXECUTEONLYONCE);
86 else if ( m_bForceRealTimer )
88 // so we don't block the nested message queue in move and resize
89 // with posted 0ms SAL_MSG_TIMER_CALLBACK messages
90 SetTimer( GetSalData()->mpInstance->mhComWnd, m_aWmTimerId,
91 USER_TIMER_MINIMUM, nullptr );
92 m_bSetTimerRunning = true;
94 // we don't need any wakeup message, as this code can just run in the
95 // main thread!
98 WinSalTimer::WinSalTimer()
99 : m_nTimerId( nullptr )
100 , m_bDirectTimeout( false )
101 , m_bForceRealTimer( false )
102 , m_bSetTimerRunning( false )
106 WinSalTimer::~WinSalTimer()
108 Stop();
111 void WinSalTimer::Start( sal_uInt64 nMS )
113 WinSalInstance *pInst = GetSalData()->mpInstance;
114 if ( pInst && !pInst->IsMainThread() )
116 bool const ret = PostMessageW(pInst->mhComWnd,
117 SAL_MSG_STARTTIMER, 0, static_cast<LPARAM>(tools::Time::GetSystemTicks()) + nMS);
118 SAL_WARN_IF(!ret, "vcl", "ERROR: PostMessage() failed!");
120 else
121 ImplStart( nMS );
124 void WinSalTimer::Stop()
126 WinSalInstance *pInst = GetSalData()->mpInstance;
127 if ( pInst && !pInst->IsMainThread() )
129 bool const ret = PostMessageW(pInst->mhComWnd,
130 SAL_MSG_STOPTIMER, 0, 0);
131 SAL_WARN_IF(!ret, "vcl", "ERROR: PostMessage() failed!");
133 else
134 ImplStop();
138 * This gets invoked from a Timer Queue thread.
139 * Don't acquire the SolarMutex to avoid deadlocks.
141 void CALLBACK SalTimerProc(PVOID data, BOOLEAN)
143 __try
145 WinSalTimer *pTimer = static_cast<WinSalTimer*>( data );
146 bool const ret = PostMessageW(
147 GetSalData()->mpInstance->mhComWnd, SAL_MSG_TIMER_CALLBACK,
148 static_cast<WPARAM>(pTimer->GetNextEventVersion()), 0 );
149 #if OSL_DEBUG_LEVEL > 0
150 if (!ret) // SEH prevents using SAL_WARN here?
151 fputs("ERROR: PostMessage() failed!\n", stderr);
152 #else
153 (void)ret;
154 #endif
156 __except(WinSalInstance::WorkaroundExceptionHandlingInUSER32Lib(GetExceptionCode(), GetExceptionInformation()))
161 void WinSalTimer::ImplHandleElapsedTimer()
163 // Test for MouseLeave
164 SalTestMouseLeave();
166 m_bDirectTimeout = false;
167 ImplSalYieldMutexAcquireWithWait();
168 CallCallback();
169 ImplSalYieldMutexRelease();
172 void WinSalTimer::ImplHandleTimerEvent( const WPARAM aWPARAM )
174 assert( aWPARAM <= SAL_MAX_INT32 );
175 if ( !IsValidEventVersion( static_cast<sal_Int32>( aWPARAM ) ) )
176 return;
178 ImplHandleElapsedTimer();
181 void WinSalTimer::SetForceRealTimer( const bool bVal )
183 if ( m_bForceRealTimer == bVal )
184 return;
186 m_bForceRealTimer = bVal;
188 // we need a real timer, as m_bDirectTimeout won't be processed
189 if ( bVal && m_bDirectTimeout )
190 Start( 0 );
193 void WinSalTimer::ImplHandle_WM_TIMER( const WPARAM aWPARAM )
195 assert( m_aWmTimerId == aWPARAM );
196 if ( !(m_aWmTimerId == aWPARAM && m_bSetTimerRunning) )
197 return;
199 m_bSetTimerRunning = false;
200 KillTimer( GetSalData()->mpInstance->mhComWnd, m_aWmTimerId );
201 ImplHandleElapsedTimer();
204 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */