| 1 | /* $Id: timer.cpp,v 1.2 2000-01-10 23:29:13 sandervl 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
|
|---|
| 31 | typedef VOID (CALLBACK *TIMERPROC)(HWND hwnd, UINT msg, UINT id, DWORD dwTime);
|
|---|
| 32 |
|
|---|
| 33 | typedef 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 |
|
|---|
| 48 | static TIMER TimersArray[NB_TIMERS];
|
|---|
| 49 |
|
|---|
| 50 | HMTX hSemTimer;
|
|---|
| 51 |
|
|---|
| 52 | inline void EnterCriticalSection (void)
|
|---|
| 53 | {
|
|---|
| 54 | if (hSemTimer == NULLHANDLE)
|
|---|
| 55 | DosCreateMutexSem (NULL, &hSemTimer, 0L, 1);
|
|---|
| 56 | else
|
|---|
| 57 | DosRequestMutexSem (hSemTimer, SEM_INDEFINITE_WAIT);
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | inline void LeaveCriticalSection (void)
|
|---|
| 61 | {
|
|---|
| 62 | DosReleaseMutexSem (hSemTimer);
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | BOOL 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 |
|
|---|
| 83 | BOOL 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 | dprintf2(("TIMER_HandleTimer %x %x %x", pTimer->hwnd, pTimer->id, pMsg->time));
|
|---|
| 103 | pTimer->proc (pTimer->hwnd, (pTimer->inUse == TIMER::SystemTimer) ? WM_SYSTIMER_W:WM_TIMER_W, pTimer->id, pMsg->time);
|
|---|
| 104 | }
|
|---|
| 105 | return (TRUE);
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | static UINT TIMER_SetTimer (HWND hwnd, UINT id, UINT timeout, TIMERPROC proc, BOOL sys)
|
|---|
| 109 | {
|
|---|
| 110 | int i;
|
|---|
| 111 | TIMER *pTimer;
|
|---|
| 112 | Win32BaseWindow *wnd = Win32BaseWindow::GetWindowFromHandle(hwnd);
|
|---|
| 113 |
|
|---|
| 114 | if (hwnd && !wnd) return 0;
|
|---|
| 115 |
|
|---|
| 116 | EnterCriticalSection ();
|
|---|
| 117 |
|
|---|
| 118 | /* Check if there's already a timer with the same hwnd and id */
|
|---|
| 119 |
|
|---|
| 120 | for (i = 0, pTimer = TimersArray; i < NB_TIMERS; i++, pTimer++)
|
|---|
| 121 | if (pTimer->inUse && (pTimer->hwnd == hwnd) && (pTimer->id == id) && ((sys && pTimer->inUse == TIMER::SystemTimer) || !sys))
|
|---|
| 122 | break;
|
|---|
| 123 |
|
|---|
| 124 | if (i == NB_TIMERS) /* no matching timer found */
|
|---|
| 125 | {
|
|---|
| 126 | /* Find a free timer */
|
|---|
| 127 |
|
|---|
| 128 | for (i = 0, pTimer = TimersArray; i < NB_TIMERS; i++, pTimer++)
|
|---|
| 129 | if (!pTimer->inUse) break;
|
|---|
| 130 |
|
|---|
| 131 | if ((i >= NB_TIMERS) ||
|
|---|
| 132 | (!sys && (i >= NB_TIMERS-NB_RESERVED_TIMERS)))
|
|---|
| 133 | {
|
|---|
| 134 | LeaveCriticalSection();
|
|---|
| 135 | return 0;
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | if (!hwnd) id = i + 1;
|
|---|
| 139 |
|
|---|
| 140 | /* Add the timer */
|
|---|
| 141 |
|
|---|
| 142 | pTimer->inUse = sys ? TIMER::SystemTimer : TIMER::UserTimer;
|
|---|
| 143 | pTimer->hwnd = hwnd;
|
|---|
| 144 | pTimer->id = id;
|
|---|
| 145 | pTimer->proc = proc;
|
|---|
| 146 | pTimer->PMhwnd = hwnd ? wnd->getOS2WindowHandle() : NULLHANDLE;
|
|---|
| 147 | pTimer->PMid = WinStartTimer (GetThreadHAB(), pTimer->PMhwnd,
|
|---|
| 148 | i + 1, timeout);
|
|---|
| 149 |
|
|---|
| 150 | if (!pTimer->PMid) id = pTimer->id = 0;
|
|---|
| 151 | } else {
|
|---|
| 152 | WinStartTimer (GetThreadHAB(), pTimer->PMhwnd, pTimer->PMid, timeout);
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | LeaveCriticalSection();
|
|---|
| 156 |
|
|---|
| 157 | if (!id) return TRUE;
|
|---|
| 158 | else return id;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | static BOOL TIMER_KillTimer (HWND hwnd, UINT id, BOOL sys)
|
|---|
| 162 | {
|
|---|
| 163 | int i;
|
|---|
| 164 | TIMER * pTimer;
|
|---|
| 165 |
|
|---|
| 166 | EnterCriticalSection();
|
|---|
| 167 |
|
|---|
| 168 | /* Find the timer */
|
|---|
| 169 |
|
|---|
| 170 | for (i = 0, pTimer = TimersArray; i < NB_TIMERS; i++, pTimer++)
|
|---|
| 171 | if (pTimer->inUse &&
|
|---|
| 172 | (pTimer->hwnd == hwnd) && (pTimer->id == id) && ((sys && pTimer->inUse == TIMER::SystemTimer) || !sys)) break;
|
|---|
| 173 |
|
|---|
| 174 | if ((i >= NB_TIMERS) ||
|
|---|
| 175 | (!sys && (i >= NB_TIMERS-NB_RESERVED_TIMERS)) ||
|
|---|
| 176 | (!sys && (pTimer->inUse != TIMER::UserTimer)) ||
|
|---|
| 177 | (sys && (pTimer->inUse != TIMER::SystemTimer)) )
|
|---|
| 178 | {
|
|---|
| 179 | LeaveCriticalSection();
|
|---|
| 180 | return FALSE;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | /* Delete the timer */
|
|---|
| 184 |
|
|---|
| 185 | WinStopTimer (GetThreadHAB(), pTimer->PMhwnd, pTimer->PMid);
|
|---|
| 186 |
|
|---|
| 187 | pTimer->inUse = TIMER::free;
|
|---|
| 188 | pTimer->PMhwnd = 0;
|
|---|
| 189 | pTimer->PMid = 0;
|
|---|
| 190 |
|
|---|
| 191 | LeaveCriticalSection();
|
|---|
| 192 |
|
|---|
| 193 | return TRUE;
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | VOID TIMER_KillTimerFromWindow(HWND hwnd)
|
|---|
| 197 | {
|
|---|
| 198 | Win32BaseWindow *wnd = Win32BaseWindow::GetWindowFromHandle(hwnd);
|
|---|
| 199 | int i;
|
|---|
| 200 | TIMER * pTimer;
|
|---|
| 201 |
|
|---|
| 202 | if (hwnd && !wnd) return;
|
|---|
| 203 |
|
|---|
| 204 | EnterCriticalSection();
|
|---|
| 205 |
|
|---|
| 206 | for (i = 0, pTimer = TimersArray; i < NB_TIMERS; i++, pTimer++)
|
|---|
| 207 | if (pTimer->inUse && pTimer->hwnd == hwnd)
|
|---|
| 208 | {
|
|---|
| 209 | pTimer->inUse = TIMER::free;
|
|---|
| 210 | pTimer->PMhwnd = 0;
|
|---|
| 211 | pTimer->PMid = 0;
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | LeaveCriticalSection();
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | /***********************************************************************
|
|---|
| 218 | * SetTimer32 (USER32.511)
|
|---|
| 219 | */
|
|---|
| 220 | UINT WIN32API SetTimer (HWND hwnd, UINT id, UINT timeout, TIMERPROC proc)
|
|---|
| 221 | {
|
|---|
| 222 | UINT rc;
|
|---|
| 223 |
|
|---|
| 224 | dprintf(("USER32: SetTimer %x %d %d %08lx", hwnd, id, timeout, (LONG)proc));
|
|---|
| 225 |
|
|---|
| 226 | rc = TIMER_SetTimer (hwnd, id, timeout, proc, FALSE);
|
|---|
| 227 | return (rc);
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | /***********************************************************************
|
|---|
| 231 | * SetSystemTimer32 (USER32.509)
|
|---|
| 232 | */
|
|---|
| 233 | UINT WIN32API SetSystemTimer (HWND hwnd, UINT id, UINT timeout, TIMERPROC proc)
|
|---|
| 234 | {
|
|---|
| 235 | UINT rc;
|
|---|
| 236 |
|
|---|
| 237 | dprintf(("USER32: SetSystemTimer %04x %d %d %08lx", hwnd, id, timeout, (LONG)proc));
|
|---|
| 238 |
|
|---|
| 239 | rc = TIMER_SetTimer (hwnd, id, timeout, proc, TRUE);
|
|---|
| 240 | return (rc);
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | /***********************************************************************
|
|---|
| 244 | * KillTimer32 (USER32.354)
|
|---|
| 245 | */
|
|---|
| 246 | BOOL WIN32API KillTimer (HWND hwnd, UINT id)
|
|---|
| 247 | {
|
|---|
| 248 | BOOL rc;
|
|---|
| 249 |
|
|---|
| 250 | dprintf(("USER32: KillTimer %x %d", hwnd, id));
|
|---|
| 251 |
|
|---|
| 252 | rc = TIMER_KillTimer (hwnd, id, FALSE);
|
|---|
| 253 | return (rc);
|
|---|
| 254 | }
|
|---|
| 255 |
|
|---|
| 256 | /***********************************************************************
|
|---|
| 257 | * KillSystemTimer32 (USER32.353)
|
|---|
| 258 | */
|
|---|
| 259 | BOOL WIN32API KillSystemTimer (HWND hwnd, UINT id)
|
|---|
| 260 | {
|
|---|
| 261 | BOOL rc;
|
|---|
| 262 |
|
|---|
| 263 | dprintf(("USER32: KillSystemTimer %x %d", hwnd, id));
|
|---|
| 264 |
|
|---|
| 265 | rc = TIMER_KillTimer (hwnd, id, TRUE);
|
|---|
| 266 | return (rc);
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|