xref: /trunk/main/vcl/os2/source/app/saltimer.cxx (revision 9f62ea84)
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 #include <svpm.h>
25 
26 #define _SV_SALTIMER_CXX
27 #include <saldata.hxx>
28 #include <saltimer.h>
29 #include <salinst.h>
30 
31 // =======================================================================
32 
33 // Maximale Periode
34 #define MAX_SYSPERIOD	  65533
35 #define IDTIMER				10
36 
37 // =======================================================================
38 
39 void ImplSalStartTimer( ULONG nMS, BOOL bMutex )
40 {
41 	SalData*	pSalData = GetSalData();
42 
43 	// Periode darf nicht zu gross sein, da OS2 2.11 mit USHORT arbeitet
44 	// Remenber the time of the timer
45 	pSalData->mnTimerMS = nMS;
46 	if ( !bMutex )
47 		pSalData->mnTimerOrgMS = nMS;
48 
49 	// Periode darf nicht zu gross sein, da Windows mit USHORT arbeitet
50 	if ( nMS > MAX_SYSPERIOD )
51 		nMS = MAX_SYSPERIOD;
52 
53 	// Gibt es einen Timer, dann zerstoren
54 	if ( pSalData->mnTimerId )
55 		WinStopTimer( pSalData->mhAB, pSalData->mpFirstInstance->mhComWnd, pSalData->mnTimerId );
56 
57 	// Make a new timer with new period
58 	pSalData->mnTimerId = WinStartTimer( pSalData->mhAB, pSalData->mpFirstInstance->mhComWnd, IDTIMER, nMS );
59 	pSalData->mnNextTimerTime = pSalData->mnLastEventTime + nMS;
60 }
61 
62 // -----------------------------------------------------------------------
63 
64 Os2SalTimer::~Os2SalTimer()
65 {
66 }
67 
68 // -----------------------------------------------------------------------
69 
70 void Os2SalTimer::Start( ULONG nMS )
71 {
72 	// Um auf Main-Thread umzuschalten
73 	SalData* pSalData = GetSalData();
74 	if ( pSalData->mpFirstInstance )
75 	{
76 		if ( pSalData->mnAppThreadId != GetCurrentThreadId() )
77 			WinPostMsg( pSalData->mpFirstInstance->mhComWnd, SAL_MSG_STARTTIMER, 0, (MPARAM)nMS );
78 		else
79 			WinSendMsg( pSalData->mpFirstInstance->mhComWnd, SAL_MSG_STARTTIMER, 0, (MPARAM)nMS );
80 	}
81 	else
82 		ImplSalStartTimer( nMS, FALSE);
83 }
84 
85 // -----------------------------------------------------------------------
86 
87 void Os2SalTimer::Stop()
88 {
89 	SalData*	pSalData = GetSalData();
90 
91 	// Exitstiert ein Timer, dann diesen zerstoeren
92 	if ( pSalData->mnTimerId ) {
93 		WinStopTimer( pSalData->mhAB, pSalData->mpFirstInstance->mhComWnd, pSalData->mnTimerId );
94 		pSalData->mnTimerId = 0;
95 		pSalData->mnNextTimerTime = 0;
96 	}
97 }
98 
99 // -----------------------------------------------------------------------
100 
101 void SalTimerProc( HWND, UINT, UINT nId, ULONG )
102 {
103 	SalData* pSalData = GetSalData();
104 	ImplSVData* pSVData = ImplGetSVData();
105 
106 	// Test for MouseLeave
107 	SalTestMouseLeave();
108 
109 	bool bRecursive = pSalData->mbInTimerProc && (nId != SALTIMERPROC_RECURSIVE);
110 	if ( pSVData->mpSalTimer && ! bRecursive )
111 	{
112 		// Try to aquire the mutex. If we don't get the mutex then we
113 		// try this a short time later again.
114 		if ( ImplSalYieldMutexTryToAcquire() )
115 		{
116 			bRecursive = pSalData->mbInTimerProc && (nId != SALTIMERPROC_RECURSIVE);
117 			if ( pSVData->mpSalTimer && ! bRecursive )
118 			{
119 				pSalData->mbInTimerProc = TRUE;
120 				pSVData->mpSalTimer->CallCallback();
121 				pSalData->mbInTimerProc = FALSE;
122 				ImplSalYieldMutexRelease();
123 
124 				// Run the timer in the correct time, if we start this
125 				// with a small timeout, because we don't get the mutex
126 				if ( pSalData->mnTimerId &&
127 					(pSalData->mnTimerMS != pSalData->mnTimerOrgMS) )
128 					ImplSalStartTimer( pSalData->mnTimerOrgMS, FALSE );
129 			}
130 		}
131 		else
132 			ImplSalStartTimer( 10, TRUE );
133 	}
134 
135 }
136 
137