xref: /AOO41X/main/vcl/os2/source/app/saltimer.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include <svpm.h>
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #define _SV_SALTIMER_CXX
31*cdf0e10cSrcweir #include <saldata.hxx>
32*cdf0e10cSrcweir #include <saltimer.h>
33*cdf0e10cSrcweir #include <salinst.h>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir // =======================================================================
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir // Maximale Periode
38*cdf0e10cSrcweir #define MAX_SYSPERIOD	  65533
39*cdf0e10cSrcweir #define IDTIMER				10
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir // =======================================================================
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir void ImplSalStartTimer( ULONG nMS, BOOL bMutex )
44*cdf0e10cSrcweir {
45*cdf0e10cSrcweir 	SalData*	pSalData = GetSalData();
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir 	// Periode darf nicht zu gross sein, da OS2 2.11 mit USHORT arbeitet
48*cdf0e10cSrcweir 	// Remenber the time of the timer
49*cdf0e10cSrcweir 	pSalData->mnTimerMS = nMS;
50*cdf0e10cSrcweir 	if ( !bMutex )
51*cdf0e10cSrcweir 		pSalData->mnTimerOrgMS = nMS;
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir 	// Periode darf nicht zu gross sein, da Windows mit USHORT arbeitet
54*cdf0e10cSrcweir 	if ( nMS > MAX_SYSPERIOD )
55*cdf0e10cSrcweir 		nMS = MAX_SYSPERIOD;
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir 	// Gibt es einen Timer, dann zerstoren
58*cdf0e10cSrcweir 	if ( pSalData->mnTimerId )
59*cdf0e10cSrcweir 		WinStopTimer( pSalData->mhAB, pSalData->mpFirstInstance->mhComWnd, pSalData->mnTimerId );
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir 	// Make a new timer with new period
62*cdf0e10cSrcweir 	pSalData->mnTimerId = WinStartTimer( pSalData->mhAB, pSalData->mpFirstInstance->mhComWnd, IDTIMER, nMS );
63*cdf0e10cSrcweir 	pSalData->mnNextTimerTime = pSalData->mnLastEventTime + nMS;
64*cdf0e10cSrcweir }
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir // -----------------------------------------------------------------------
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir Os2SalTimer::~Os2SalTimer()
69*cdf0e10cSrcweir {
70*cdf0e10cSrcweir }
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir // -----------------------------------------------------------------------
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir void Os2SalTimer::Start( ULONG nMS )
75*cdf0e10cSrcweir {
76*cdf0e10cSrcweir 	// Um auf Main-Thread umzuschalten
77*cdf0e10cSrcweir 	SalData* pSalData = GetSalData();
78*cdf0e10cSrcweir 	if ( pSalData->mpFirstInstance )
79*cdf0e10cSrcweir 	{
80*cdf0e10cSrcweir 		if ( pSalData->mnAppThreadId != GetCurrentThreadId() )
81*cdf0e10cSrcweir 			WinPostMsg( pSalData->mpFirstInstance->mhComWnd, SAL_MSG_STARTTIMER, 0, (MPARAM)nMS );
82*cdf0e10cSrcweir 		else
83*cdf0e10cSrcweir 			WinSendMsg( pSalData->mpFirstInstance->mhComWnd, SAL_MSG_STARTTIMER, 0, (MPARAM)nMS );
84*cdf0e10cSrcweir 	}
85*cdf0e10cSrcweir 	else
86*cdf0e10cSrcweir 		ImplSalStartTimer( nMS, FALSE);
87*cdf0e10cSrcweir }
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir // -----------------------------------------------------------------------
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir void Os2SalTimer::Stop()
92*cdf0e10cSrcweir {
93*cdf0e10cSrcweir 	SalData*	pSalData = GetSalData();
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir 	// Exitstiert ein Timer, dann diesen zerstoeren
96*cdf0e10cSrcweir 	if ( pSalData->mnTimerId ) {
97*cdf0e10cSrcweir 		WinStopTimer( pSalData->mhAB, pSalData->mpFirstInstance->mhComWnd, pSalData->mnTimerId );
98*cdf0e10cSrcweir 		pSalData->mnTimerId = 0;
99*cdf0e10cSrcweir 		pSalData->mnNextTimerTime = 0;
100*cdf0e10cSrcweir 	}
101*cdf0e10cSrcweir }
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir // -----------------------------------------------------------------------
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir void SalTimerProc( HWND, UINT, UINT nId, ULONG )
106*cdf0e10cSrcweir {
107*cdf0e10cSrcweir 	SalData* pSalData = GetSalData();
108*cdf0e10cSrcweir 	ImplSVData* pSVData = ImplGetSVData();
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir 	// Test for MouseLeave
111*cdf0e10cSrcweir 	SalTestMouseLeave();
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir 	bool bRecursive = pSalData->mbInTimerProc && (nId != SALTIMERPROC_RECURSIVE);
114*cdf0e10cSrcweir 	if ( pSVData->mpSalTimer && ! bRecursive )
115*cdf0e10cSrcweir 	{
116*cdf0e10cSrcweir 		// Try to aquire the mutex. If we don't get the mutex then we
117*cdf0e10cSrcweir 		// try this a short time later again.
118*cdf0e10cSrcweir 		if ( ImplSalYieldMutexTryToAcquire() )
119*cdf0e10cSrcweir 		{
120*cdf0e10cSrcweir 			bRecursive = pSalData->mbInTimerProc && (nId != SALTIMERPROC_RECURSIVE);
121*cdf0e10cSrcweir 			if ( pSVData->mpSalTimer && ! bRecursive )
122*cdf0e10cSrcweir 			{
123*cdf0e10cSrcweir 				pSalData->mbInTimerProc = TRUE;
124*cdf0e10cSrcweir 				pSVData->mpSalTimer->CallCallback();
125*cdf0e10cSrcweir 				pSalData->mbInTimerProc = FALSE;
126*cdf0e10cSrcweir 				ImplSalYieldMutexRelease();
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir 				// Run the timer in the correct time, if we start this
129*cdf0e10cSrcweir 				// with a small timeout, because we don't get the mutex
130*cdf0e10cSrcweir 				if ( pSalData->mnTimerId &&
131*cdf0e10cSrcweir 					(pSalData->mnTimerMS != pSalData->mnTimerOrgMS) )
132*cdf0e10cSrcweir 					ImplSalStartTimer( pSalData->mnTimerOrgMS, FALSE );
133*cdf0e10cSrcweir 			}
134*cdf0e10cSrcweir 		}
135*cdf0e10cSrcweir 		else
136*cdf0e10cSrcweir 			ImplSalStartTimer( 10, TRUE );
137*cdf0e10cSrcweir 	}
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir }
140*cdf0e10cSrcweir 
141