| 1 | /* $Id: time.cpp,v 1.14 2001-10-03 13:47:59 sandervl Exp $ */ | 
|---|
| 2 |  | 
|---|
| 3 | /* | 
|---|
| 4 | * Timer MM apis | 
|---|
| 5 | * | 
|---|
| 6 | * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl) | 
|---|
| 7 | * Copyright 1999 Patrick Haller     (phaller@gmx.net) | 
|---|
| 8 | * | 
|---|
| 9 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 10 | * | 
|---|
| 11 | */ | 
|---|
| 12 |  | 
|---|
| 13 |  | 
|---|
| 14 | /**************************************************************************** | 
|---|
| 15 | * Includes                                                                 * | 
|---|
| 16 | ****************************************************************************/ | 
|---|
| 17 | #include <os2win.h> | 
|---|
| 18 | #include <odinwrap.h> | 
|---|
| 19 | #include <misc.h> | 
|---|
| 20 | #include <handlemanager.h> | 
|---|
| 21 |  | 
|---|
| 22 | #include "os2timer.h" | 
|---|
| 23 | #include "time.h" | 
|---|
| 24 |  | 
|---|
| 25 | #define DBG_LOCALLOG    DBG_time | 
|---|
| 26 | #include "dbglocal.h" | 
|---|
| 27 |  | 
|---|
| 28 | ODINDEBUGCHANNEL(WINMM-TIME) | 
|---|
| 29 |  | 
|---|
| 30 |  | 
|---|
| 31 | /***************************************************************************** | 
|---|
| 32 | * Name      : mmsystemGetVersion | 
|---|
| 33 | * Purpose   : determine version of MM system | 
|---|
| 34 | * Parameters: | 
|---|
| 35 | * Variables : | 
|---|
| 36 | * Result    : | 
|---|
| 37 | * Remark    : | 
|---|
| 38 | * Status    : UNTESTED STUB | 
|---|
| 39 | * | 
|---|
| 40 | * Author    : Patrick Haller [Tue, 1998/06/16 23:00] | 
|---|
| 41 | *****************************************************************************/ | 
|---|
| 42 |  | 
|---|
| 43 |  | 
|---|
| 44 | ODINFUNCTION0(UINT, mmsystemGetVersion) | 
|---|
| 45 | { | 
|---|
| 46 | //Returned by winmm.dll from NT4, SP6 | 
|---|
| 47 | return 0x030A; | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 | /***************************************************************************** | 
|---|
| 51 | * Name      : | 
|---|
| 52 | * Purpose   : | 
|---|
| 53 | * Parameters: | 
|---|
| 54 | * Variables : | 
|---|
| 55 | * Result    : | 
|---|
| 56 | * Remark    : | 
|---|
| 57 | * Status    : UNTESTED STUB | 
|---|
| 58 | * | 
|---|
| 59 | * Author    : Patrick Haller [Tue, 1998/06/16 23:00] | 
|---|
| 60 | *****************************************************************************/ | 
|---|
| 61 |  | 
|---|
| 62 | ODINFUNCTION2(MMRESULT,   timeGetDevCaps, | 
|---|
| 63 | LPTIMECAPS, ptc, | 
|---|
| 64 | UINT,       cbtc) | 
|---|
| 65 | { | 
|---|
| 66 | dprintf(("WINMM:timeGetDevCaps Not really Implemented\n")); | 
|---|
| 67 |  | 
|---|
| 68 | /* 2001-09-16 PH | 
|---|
| 69 | add dynamic detection of OS/2's minimum timer resolution | 
|---|
| 70 | */ | 
|---|
| 71 |  | 
|---|
| 72 | ptc->wPeriodMin = OS2TIMER_RESOLUTION_MINIMUM; | 
|---|
| 73 | ptc->wPeriodMax = OS2TIMER_RESOLUTION_MAXIMUM; | 
|---|
| 74 |  | 
|---|
| 75 | return TIMERR_NOERROR; | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 |  | 
|---|
| 79 | /***************************************************************************** | 
|---|
| 80 | * Name      : | 
|---|
| 81 | * Purpose   : | 
|---|
| 82 | * Parameters: | 
|---|
| 83 | * Variables : | 
|---|
| 84 | * Result    : | 
|---|
| 85 | * Remark    : | 
|---|
| 86 | * Status    : UNTESTED STUB | 
|---|
| 87 | * | 
|---|
| 88 | * Author    : Patrick Haller [Tue, 1998/06/16 23:00] | 
|---|
| 89 | *****************************************************************************/ | 
|---|
| 90 |  | 
|---|
| 91 | ODINFUNCTION1(MMRESULT, timeBeginPeriod, | 
|---|
| 92 | UINT,     cMilliseconds) | 
|---|
| 93 | { | 
|---|
| 94 | if (TRUE == OS2TimerResolution::enterResolutionScope(cMilliseconds)) | 
|---|
| 95 | return TIMERR_NOERROR; | 
|---|
| 96 | else | 
|---|
| 97 | return TIMERR_NOCANDO; | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 |  | 
|---|
| 101 | /***************************************************************************** | 
|---|
| 102 | * Name      : | 
|---|
| 103 | * Purpose   : | 
|---|
| 104 | * Parameters: | 
|---|
| 105 | * Variables : | 
|---|
| 106 | * Result    : | 
|---|
| 107 | * Remark    : | 
|---|
| 108 | * Status    : UNTESTED STUB | 
|---|
| 109 | * | 
|---|
| 110 | * Author    : Patrick Haller [Tue, 1998/06/16 23:00] | 
|---|
| 111 | *****************************************************************************/ | 
|---|
| 112 |  | 
|---|
| 113 | ODINFUNCTION1(MMRESULT, timeEndPeriod, | 
|---|
| 114 | UINT,     cMilliseconds) | 
|---|
| 115 | { | 
|---|
| 116 | if (TRUE == OS2TimerResolution::leaveResolutionScope(cMilliseconds)) | 
|---|
| 117 | return TIMERR_NOERROR; | 
|---|
| 118 | else | 
|---|
| 119 | { | 
|---|
| 120 | dprintf(("WINMM: timeEndPeriod didn't match timeBeginPeriod.\n")); | 
|---|
| 121 | return TIMERR_NOCANDO; | 
|---|
| 122 | } | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 |  | 
|---|
| 126 | /***************************************************************************** | 
|---|
| 127 | * Name      : | 
|---|
| 128 | * Purpose   : | 
|---|
| 129 | * Parameters: | 
|---|
| 130 | * Variables : | 
|---|
| 131 | * Result    : | 
|---|
| 132 | * Remark    : | 
|---|
| 133 | * Status    : UNTESTED STUB | 
|---|
| 134 | * | 
|---|
| 135 | * Author    : Patrick Haller [Tue, 1998/06/16 23:00] | 
|---|
| 136 | *****************************************************************************/ | 
|---|
| 137 |  | 
|---|
| 138 | ODINFUNCTION1(MMRESULT, timeKillEvent, | 
|---|
| 139 | UINT,     IDEvent) | 
|---|
| 140 | { | 
|---|
| 141 | OS2Timer *os2timer = NULL; | 
|---|
| 142 |  | 
|---|
| 143 | if(HMHandleTranslateToOS2(IDEvent, (PULONG)&os2timer) != NO_ERROR) { | 
|---|
| 144 | dprintf(("invalid timer id")); | 
|---|
| 145 | return TIMERR_NOERROR; //TODO: should we return an error here?? | 
|---|
| 146 | } | 
|---|
| 147 | HMHandleFree(IDEvent); | 
|---|
| 148 | // return OS2Timer::killEvent(UINT IDEvent) | 
|---|
| 149 |  | 
|---|
| 150 | delete os2timer; | 
|---|
| 151 | return TIMERR_NOERROR; | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 |  | 
|---|
| 155 | /***************************************************************************** | 
|---|
| 156 | * Name      : | 
|---|
| 157 | * Purpose   : | 
|---|
| 158 | * Parameters: | 
|---|
| 159 | * Variables : | 
|---|
| 160 | * Result    : | 
|---|
| 161 | * Remark    : | 
|---|
| 162 | * Status    : UNTESTED STUB | 
|---|
| 163 | * | 
|---|
| 164 | * Author    : Patrick Haller [Tue, 1998/06/16 23:00] | 
|---|
| 165 | *****************************************************************************/ | 
|---|
| 166 |  | 
|---|
| 167 | ODINFUNCTION5(MMRESULT,       timeSetEvent, | 
|---|
| 168 | UINT,           wDelay, | 
|---|
| 169 | UINT,           wResolution, | 
|---|
| 170 | LPTIMECALLBACK, lptc, | 
|---|
| 171 | DWORD,          dwUser, | 
|---|
| 172 | UINT,           fuEvent) | 
|---|
| 173 | { | 
|---|
| 174 | OS2Timer *timer; | 
|---|
| 175 | ULONG     timerID = 0; | 
|---|
| 176 |  | 
|---|
| 177 | // @@@PH 1999/10/26 hack for RA95 | 
|---|
| 178 | if (wDelay      < OS2TIMER_RESOLUTION_MINIMUM) | 
|---|
| 179 | { | 
|---|
| 180 | dprintf(("WINMM:Time:timeSetEvent - Warning: requested delay too low (%08xh)\n", | 
|---|
| 181 | wDelay)); | 
|---|
| 182 | wDelay = OS2TIMER_RESOLUTION_MINIMUM; | 
|---|
| 183 | } | 
|---|
| 184 |  | 
|---|
| 185 | if (wResolution < OS2TIMER_RESOLUTION_MINIMUM) | 
|---|
| 186 | { | 
|---|
| 187 | dprintf(("WINMM:Time:timeSetEvent - Warning: requested resolution too low (%08xh)\n", | 
|---|
| 188 | wResolution)); | 
|---|
| 189 | wResolution = OS2TIMER_RESOLUTION_MINIMUM; | 
|---|
| 190 | } | 
|---|
| 191 |  | 
|---|
| 192 |  | 
|---|
| 193 | // check parameters | 
|---|
| 194 | if ((wDelay < OS2TIMER_RESOLUTION_MINIMUM) || | 
|---|
| 195 | (wDelay > OS2TIMER_RESOLUTION_MAXIMUM)) | 
|---|
| 196 | return NULL; | 
|---|
| 197 |  | 
|---|
| 198 | if (wResolution == 0) | 
|---|
| 199 | wResolution = OS2TIMER_RESOLUTION_MINIMUM; | 
|---|
| 200 | else | 
|---|
| 201 | if ((wResolution < OS2TIMER_RESOLUTION_MINIMUM) || | 
|---|
| 202 | (wResolution > OS2TIMER_RESOLUTION_MAXIMUM)) | 
|---|
| 203 | return NULL; | 
|---|
| 204 |  | 
|---|
| 205 | timer = new OS2Timer(); | 
|---|
| 206 | if(timer == NULL) | 
|---|
| 207 | return(0); | 
|---|
| 208 |  | 
|---|
| 209 | if(HMHandleAllocate(&timerID, (ULONG)timer) != NO_ERROR) { | 
|---|
| 210 | dprintf(("HMHandleAllocate failed!!")); | 
|---|
| 211 | delete timer; | 
|---|
| 212 | return 0; | 
|---|
| 213 | } | 
|---|
| 214 |  | 
|---|
| 215 | timer->setTimerID(timerID); | 
|---|
| 216 | if(timer->StartTimer(wDelay, wResolution, lptc, dwUser, fuEvent) == FALSE) | 
|---|
| 217 | { | 
|---|
| 218 | dprintf(("WINMM:timeSetEvent: couldn't start timer!\n")); | 
|---|
| 219 | delete(timer); | 
|---|
| 220 | return(0); | 
|---|
| 221 | } | 
|---|
| 222 | return(MMRESULT)timerID; | 
|---|
| 223 | } | 
|---|
| 224 |  | 
|---|
| 225 | ULONG OPEN32API WinGetCurrentTime(ULONG hab); | 
|---|
| 226 |  | 
|---|
| 227 | inline ULONG _WinGetCurrentTime(ULONG a) | 
|---|
| 228 | { | 
|---|
| 229 | ULONG yyrc; | 
|---|
| 230 | USHORT sel = RestoreOS2FS(); | 
|---|
| 231 |  | 
|---|
| 232 | yyrc = WinGetCurrentTime(a); | 
|---|
| 233 | SetFS(sel); | 
|---|
| 234 |  | 
|---|
| 235 | return yyrc; | 
|---|
| 236 | } | 
|---|
| 237 |  | 
|---|
| 238 | #undef  WinGetCurrentTime | 
|---|
| 239 | #define WinGetCurrentTime _WinGetCurrentTime | 
|---|
| 240 |  | 
|---|
| 241 | /***************************************************************************** | 
|---|
| 242 | * Name      : | 
|---|
| 243 | * Purpose   : | 
|---|
| 244 | * Parameters: | 
|---|
| 245 | * Variables : | 
|---|
| 246 | * Result    : | 
|---|
| 247 | * Remark    : | 
|---|
| 248 | * Status    : UNTESTED STUB | 
|---|
| 249 | * | 
|---|
| 250 | * Author    : Patrick Haller [Tue, 1998/06/16 23:00] | 
|---|
| 251 | *****************************************************************************/ | 
|---|
| 252 |  | 
|---|
| 253 | ODINFUNCTION2(MMRESULT, timeGetSystemTime, | 
|---|
| 254 | LPMMTIME, pTime, | 
|---|
| 255 | UINT,     cbTime) | 
|---|
| 256 | { | 
|---|
| 257 | dprintf2(("timeGetSystemTime %x %d", pTime, cbTime)); | 
|---|
| 258 |  | 
|---|
| 259 | if(pTime == NULL || cbTime < sizeof(MMTIME)) { | 
|---|
| 260 | SetLastError(ERROR_INVALID_PARAMETER); | 
|---|
| 261 | return 0; | 
|---|
| 262 | } | 
|---|
| 263 | pTime->wType = TIME_MS; | 
|---|
| 264 | pTime->u.ms = WinGetCurrentTime(0); | 
|---|
| 265 |  | 
|---|
| 266 | SetLastError(ERROR_SUCCESS); | 
|---|
| 267 | return 0; | 
|---|
| 268 | } | 
|---|
| 269 |  | 
|---|
| 270 |  | 
|---|
| 271 | /***************************************************************************** | 
|---|
| 272 | * Name      : | 
|---|
| 273 | * Purpose   : | 
|---|
| 274 | * Parameters: | 
|---|
| 275 | * Variables : | 
|---|
| 276 | * Result    : | 
|---|
| 277 | * Remark    : | 
|---|
| 278 | * Status    : UNTESTED STUB | 
|---|
| 279 | * | 
|---|
| 280 | * Author    : Patrick Haller [Tue, 1998/06/16 23:00] | 
|---|
| 281 | *****************************************************************************/ | 
|---|
| 282 |  | 
|---|
| 283 | DWORD WIN32API timeGetTime() | 
|---|
| 284 | { | 
|---|
| 285 | DWORD time; | 
|---|
| 286 | #if 0 | 
|---|
| 287 | LARGE_INTEGER lint; | 
|---|
| 288 | static LARGE_INTEGER freq; | 
|---|
| 289 | static BOOL fInit = FALSE; | 
|---|
| 290 |  | 
|---|
| 291 | if(fInit == FALSE) { | 
|---|
| 292 | QueryPerformanceFrequency(&freq); | 
|---|
| 293 | freq.LowPart /= 1000; | 
|---|
| 294 | fInit = TRUE; | 
|---|
| 295 | } | 
|---|
| 296 | QueryPerformanceCounter(&lint); | 
|---|
| 297 | time = lint.LowPart/freq.LowPart; | 
|---|
| 298 | dprintf2(("timeGetTime %x (%x:%x)", time, lint.LowPart, lint.HighPart)); | 
|---|
| 299 | #else | 
|---|
| 300 | //SvL: TODO: Inaccurate | 
|---|
| 301 | time = WinGetCurrentTime(0); | 
|---|
| 302 | dprintf2(("timeGetTime %x", time)); | 
|---|
| 303 | #endif | 
|---|
| 304 | return time; | 
|---|
| 305 | } | 
|---|
| 306 |  | 
|---|