source: trunk/src/user32/timer.cpp@ 1418

Last change on this file since 1418 was 1418, checked in by cbratschi, 26 years ago

GetSystemMetrics, timer and scrollbar fixes

File size: 6.1 KB
Line 
1/* $Id: timer.cpp,v 1.6 1999-10-23 16:45:21 cbratschi Exp $ */
2
3/*
4 * timer functions for USER32
5 *
6 * Copyright 1993 Alexandre Julliard
7 * Copyright 1999 Daniela Engert (dani@ngrt.de)
8 *
9 * Project Odin Software License can be found in LICENSE.TXT
10 *
11 */
12
13#define INCL_WIN
14#define INCL_DOSSEMAPHORES
15#include <os2.h>
16#include <os2sel.h>
17#include <stdlib.h>
18#include "win32type.h"
19#include <winconst.h>
20#include <misc.h>
21#include <win32wbase.h>
22#include "oslibutil.h"
23#include "timer.h"
24
25#ifndef OPEN32API
26#define OPEN32API _System
27#endif
28
29#define WM_TIMER_W 0x0113
30#define WM_SYSTIMER_W 0x0118
31typedef VOID (CALLBACK *TIMERPROC)(HWND hwnd, UINT msg, UINT id, DWORD dwTime);
32
33typedef struct tagTIMER
34{
35 enum {free = 0, UserTimer, SystemTimer} inUse;
36 HWND hwnd;
37 UINT id;
38 HWND PMhwnd;
39 ULONG PMid;
40 TIMERPROC proc;
41} TIMER;
42
43#define NB_TIMERS 34
44#define NB_RESERVED_TIMERS 2 /* for SetSystemTimer */
45
46#define SYS_TIMER_RATE 54925
47
48static TIMER TimersArray[NB_TIMERS];
49
50HMTX hSemTimer;
51
52inline void EnterCriticalSection (void)
53{
54 if (hSemTimer == NULLHANDLE)
55 DosCreateMutexSem (NULL, &hSemTimer, 0L, 1);
56 else
57 DosRequestMutexSem (hSemTimer, SEM_INDEFINITE_WAIT);
58}
59
60inline void LeaveCriticalSection (void)
61{
62 DosReleaseMutexSem (hSemTimer);
63}
64
65BOOL TIMER_GetTimerInfo(HWND PMhwnd,ULONG PMid,PBOOL sys,PULONG id)
66{
67 int i;
68 TIMER *pTimer;
69
70 for (i = 0, pTimer = TimersArray; i < NB_TIMERS; i++, pTimer++)
71 if (pTimer->inUse && (pTimer->PMhwnd == PMhwnd) && (pTimer->PMid == PMid))
72 break;
73
74 if (i == NB_TIMERS) /* no matching timer found */
75 return (FALSE); /* forward message */
76
77 *sys = pTimer->inUse == TIMER::SystemTimer;
78 *id = pTimer->id;
79
80 return TRUE;
81}
82
83BOOL TIMER_HandleTimer (PQMSG pMsg)
84{
85 int i;
86 TIMER *pTimer;
87 HWND PMhwnd = pMsg->hwnd;
88 ULONG PMid = (ULONG)(pMsg->mp1);
89
90 for (i = 0, pTimer = TimersArray; i < NB_TIMERS; i++, pTimer++)
91 if (pTimer->inUse && (pTimer->PMhwnd == PMhwnd) && (pTimer->PMid == PMid))
92 break;
93
94 if (i == NB_TIMERS) /* no matching timer found */
95 return (FALSE); /* forward message */
96
97 pMsg->mp2 = MPFROMLONG (TRUE); /* mark for Win32 */
98 if (!pTimer->proc)
99 return (FALSE); /* forward message */
100
101 if (!WinInSendMsg (GetThreadHAB()))
102 pTimer->proc (pTimer->hwnd, (pTimer->inUse == TIMER::SystemTimer) ? WM_SYSTIMER_W:WM_TIMER_W, pTimer->id, pMsg->time);
103
104 return (TRUE);
105}
106
107static UINT TIMER_SetTimer (HWND hwnd, UINT id, UINT timeout, TIMERPROC proc, BOOL sys)
108{
109 int i;
110 TIMER *pTimer;
111 Win32BaseWindow *wnd = Win32BaseWindow::GetWindowFromHandle(hwnd);
112
113 if (hwnd && !wnd) return 0;
114
115 EnterCriticalSection ();
116
117 /* Check if there's already a timer with the same hwnd and id */
118
119 for (i = 0, pTimer = TimersArray; i < NB_TIMERS; i++, pTimer++)
120 if (pTimer->inUse && (pTimer->hwnd == hwnd) && (pTimer->id == id) && ((sys && pTimer->inUse == TIMER::SystemTimer) || !sys))
121 break;
122
123 if (i == NB_TIMERS) /* no matching timer found */
124 {
125 /* Find a free timer */
126
127 for (i = 0, pTimer = TimersArray; i < NB_TIMERS; i++, pTimer++)
128 if (!pTimer->inUse) break;
129
130 if ((i >= NB_TIMERS) ||
131 (!sys && (i >= NB_TIMERS-NB_RESERVED_TIMERS)))
132 {
133 LeaveCriticalSection();
134 return 0;
135 }
136
137 if (!hwnd) id = i + 1;
138
139 /* Add the timer */
140
141 pTimer->inUse = sys ? TIMER::SystemTimer : TIMER::UserTimer;
142 pTimer->hwnd = hwnd;
143 pTimer->id = id;
144 pTimer->proc = proc;
145 pTimer->PMhwnd = hwnd ? wnd->getOS2WindowHandle() : NULLHANDLE;
146 pTimer->PMid = WinStartTimer (GetThreadHAB(), pTimer->PMhwnd,
147 i + 1, timeout);
148
149 if (!pTimer->PMid) id = pTimer->id = 0;
150 } else {
151 WinStartTimer (GetThreadHAB(), pTimer->PMhwnd, pTimer->PMid, timeout);
152 }
153
154 LeaveCriticalSection();
155
156 if (!id) return TRUE;
157 else return id;
158}
159
160static BOOL TIMER_KillTimer (HWND hwnd, UINT id, BOOL sys)
161{
162 int i;
163 TIMER * pTimer;
164
165 EnterCriticalSection();
166
167 /* Find the timer */
168
169 for (i = 0, pTimer = TimersArray; i < NB_TIMERS; i++, pTimer++)
170 if (pTimer->inUse &&
171 (pTimer->hwnd == hwnd) && (pTimer->id == id) && ((sys && pTimer->inUse == TIMER::SystemTimer) || !sys)) break;
172
173 if ((i >= NB_TIMERS) ||
174 (!sys && (i >= NB_TIMERS-NB_RESERVED_TIMERS)) ||
175 (!sys && (pTimer->inUse != TIMER::UserTimer)) ||
176 (sys && (pTimer->inUse != TIMER::SystemTimer)) )
177 {
178 LeaveCriticalSection();
179 return FALSE;
180 }
181
182 /* Delete the timer */
183
184 WinStopTimer (GetThreadHAB(), pTimer->PMhwnd, pTimer->PMid);
185
186 LeaveCriticalSection();
187
188 return TRUE;
189}
190
191/***********************************************************************
192 * SetTimer32 (USER32.511)
193 */
194UINT WIN32API SetTimer (HWND hwnd, UINT id, UINT timeout, TIMERPROC proc)
195{
196 UINT rc;
197
198 dprintf(("USER32: SetTimer %04x %d %d %08lx", hwnd, id, timeout, (LONG)proc));
199
200 rc = TIMER_SetTimer (hwnd, id, timeout, proc, FALSE);
201 return (rc);
202}
203
204/***********************************************************************
205 * SetSystemTimer32 (USER32.509)
206 */
207UINT WIN32API SetSystemTimer (HWND hwnd, UINT id, UINT timeout, TIMERPROC proc)
208{
209 UINT rc;
210
211 dprintf(("USER32: SetSystemTimer %04x %d %d %08lx", hwnd, id, timeout, (LONG)proc));
212
213 rc = TIMER_SetTimer (hwnd, id, timeout, proc, TRUE);
214 return (rc);
215}
216
217/***********************************************************************
218 * KillTimer32 (USER32.354)
219 */
220BOOL WIN32API KillTimer (HWND hwnd, UINT id)
221{
222 BOOL rc;
223
224 dprintf(("USER32: KillTimer %04x %d", hwnd, id));
225
226 rc = TIMER_KillTimer (hwnd, id, FALSE);
227 return (rc);
228}
229
230/***********************************************************************
231 * KillSystemTimer32 (USER32.353)
232 */
233BOOL WIN32API KillSystemTimer (HWND hwnd, UINT id)
234{
235 BOOL rc;
236
237 dprintf(("USER32: KillSystemTimer %04x %d", hwnd, id));
238
239 rc = TIMER_KillTimer (hwnd, id, TRUE);
240 return (rc);
241}
242
Note: See TracBrowser for help on using the repository browser.