xref: /AOO41X/main/vcl/win/source/app/saltimer.cxx (revision 79aad27f7f29270c03e208e3d687e8e3850af11d)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_vcl.hxx"
26 #include <tools/svwin.h>
27 #ifdef __MINGW32__
28 #include <excpt.h>
29 #endif
30 #include <win/saldata.hxx>
31 #include <win/saltimer.h>
32 #include <win/salinst.h>
33 
34 // =======================================================================
35 
36 // Maximale Periode
37 #define MAX_SYSPERIOD     65533
38 
39 // =======================================================================
40 
ImplSalStartTimer(sal_uLong nMS,sal_Bool bMutex)41 void ImplSalStartTimer( sal_uLong nMS, sal_Bool bMutex )
42 {
43     SalData* pSalData = GetSalData();
44 
45     // Remenber the time of the timer
46     pSalData->mnTimerMS = nMS;
47     if ( !bMutex )
48         pSalData->mnTimerOrgMS = nMS;
49 
50     // Periode darf nicht zu gross sein, da Windows mit sal_uInt16 arbeitet
51     if ( nMS > MAX_SYSPERIOD )
52         nMS = MAX_SYSPERIOD;
53 
54     // Gibt es einen Timer, dann zerstoren
55     if ( pSalData->mnTimerId )
56         KillTimer( 0, pSalData->mnTimerId );
57 
58     // Make a new timer with new period
59     pSalData->mnTimerId = SetTimer( 0, 0, (UINT)nMS, SalTimerProc );
60     pSalData->mnNextTimerTime = pSalData->mnLastEventTime + nMS;
61 }
62 
63 // -----------------------------------------------------------------------
64 
~WinSalTimer()65 WinSalTimer::~WinSalTimer()
66 {
67 }
68 
Start(sal_uLong nMS)69 void WinSalTimer::Start( sal_uLong nMS )
70 {
71     // switch to main thread
72     SalData* pSalData = GetSalData();
73     if ( pSalData->mpFirstInstance )
74     {
75         if ( pSalData->mnAppThreadId != GetCurrentThreadId() )
76             ImplPostMessage( pSalData->mpFirstInstance->mhComWnd, SAL_MSG_STARTTIMER, 0, (LPARAM)nMS );
77         else
78             ImplSendMessage( pSalData->mpFirstInstance->mhComWnd, SAL_MSG_STARTTIMER, 0, (LPARAM)nMS );
79     }
80     else
81         ImplSalStartTimer( nMS, FALSE );
82 }
83 
Stop()84 void WinSalTimer::Stop()
85 {
86     SalData* pSalData = GetSalData();
87 
88     // If we have a timer, than
89     if ( pSalData->mnTimerId )
90     {
91         KillTimer( 0, pSalData->mnTimerId );
92         pSalData->mnTimerId = 0;
93         pSalData->mnNextTimerTime = 0;
94     }
95 }
96 
97 // -----------------------------------------------------------------------
98 
SalTimerProc(HWND,UINT,UINT_PTR nId,DWORD)99 void CALLBACK SalTimerProc( HWND, UINT, UINT_PTR nId, DWORD )
100 {
101 #ifdef __MINGW32__
102     jmp_buf jmpbuf;
103     __SEHandler han;
104     if (__builtin_setjmp(jmpbuf) == 0)
105     {
106         han.Set(jmpbuf, NULL, (__SEHandler::PF)EXCEPTION_EXECUTE_HANDLER);
107 #else
108     __try
109     {
110 #endif
111         SalData* pSalData = GetSalData();
112         ImplSVData* pSVData = ImplGetSVData();
113 
114         // Test for MouseLeave
115         SalTestMouseLeave();
116 
117         bool bRecursive = pSalData->mbInTimerProc && (nId != SALTIMERPROC_RECURSIVE);
118         if ( pSVData->mpSalTimer && ! bRecursive )
119         {
120             // Try to aquire the mutex. If we don't get the mutex then we
121             // try this a short time later again.
122             if ( ImplSalYieldMutexTryToAcquire() )
123             {
124                 bRecursive = pSalData->mbInTimerProc && (nId != SALTIMERPROC_RECURSIVE);
125                 if ( pSVData->mpSalTimer && ! bRecursive )
126                 {
127                     pSalData->mbInTimerProc = TRUE;
128                     pSVData->mpSalTimer->CallCallback();
129                     pSalData->mbInTimerProc = FALSE;
130                     ImplSalYieldMutexRelease();
131 
132                     // Run the timer in the correct time, if we start this
133                     // with a small timeout, because we don't get the mutex
134                     if ( pSalData->mnTimerId &&
135                         (pSalData->mnTimerMS != pSalData->mnTimerOrgMS) )
136                         ImplSalStartTimer( pSalData->mnTimerOrgMS, FALSE );
137                 }
138             }
139             else
140                 ImplSalStartTimer( 10, TRUE );
141         }
142     }
143 #ifdef __MINGW32__
144     han.Reset();
145 #else
146     __except(WinSalInstance::WorkaroundExceptionHandlingInUSER32Lib(GetExceptionCode(), GetExceptionInformation()))
147     {
148     }
149 #endif
150 }
151