1 /* ptwinmm.c -- portable timer implementation for win32 */
12 static long time_offset = 0;
13 static int time_started_flag = FALSE;
14 static long time_resolution;
15 static MMRESULT timer_id;
16 static PtCallback *time_callback;
18 void CALLBACK winmm_time_callback(UINT uID, UINT uMsg, DWORD_PTR dwUser,
19 DWORD_PTR dw1, DWORD_PTR dw2)
21 (*time_callback)(Pt_Time(), (void *) dwUser);
25 PMEXPORT PtError Pt_Start(int resolution, PtCallback *callback, void *userData)
27 if (time_started_flag) return ptAlreadyStarted;
28 timeBeginPeriod(resolution);
29 time_resolution = resolution;
30 time_offset = timeGetTime();
31 time_started_flag = TRUE;
32 time_callback = callback;
34 timer_id = timeSetEvent(resolution, 1, winmm_time_callback,
35 (DWORD_PTR) userData, TIME_PERIODIC | TIME_CALLBACK_FUNCTION);
36 if (!timer_id) return ptHostError;
42 PMEXPORT PtError Pt_Stop()
44 if (!time_started_flag) return ptAlreadyStopped;
45 if (time_callback && timer_id) {
46 timeKillEvent(timer_id);
50 time_started_flag = FALSE;
51 timeEndPeriod(time_resolution);
56 PMEXPORT int Pt_Started()
58 return time_started_flag;
62 PMEXPORT PtTimestamp Pt_Time()
64 return timeGetTime() - time_offset;
68 PMEXPORT void Pt_Sleep(int32_t duration)