ptwinmm.c (1533B)
1 /* ptwinmm.c -- portable timer implementation for win32 */ 2 3 4 #include "porttime.h" 5 #include "windows.h" 6 #include "time.h" 7 8 9 TIMECAPS caps; 10 11 static long time_offset = 0; 12 static int time_started_flag = FALSE; 13 static long time_resolution; 14 static MMRESULT timer_id; 15 static PtCallback *time_callback; 16 17 void CALLBACK winmm_time_callback(UINT uID, UINT uMsg, DWORD_PTR dwUser, 18 DWORD_PTR dw1, DWORD_PTR dw2) 19 { 20 (*time_callback)(Pt_Time(), (void *) dwUser); 21 } 22 23 24 PMEXPORT PtError Pt_Start(int resolution, PtCallback *callback, void *userData) 25 { 26 if (time_started_flag) return ptAlreadyStarted; 27 timeBeginPeriod(resolution); 28 time_resolution = resolution; 29 time_offset = timeGetTime(); 30 time_started_flag = TRUE; 31 time_callback = callback; 32 if (callback) { 33 timer_id = timeSetEvent(resolution, 1, winmm_time_callback, 34 (DWORD_PTR) userData, TIME_PERIODIC | TIME_CALLBACK_FUNCTION); 35 if (!timer_id) return ptHostError; 36 } 37 return ptNoError; 38 } 39 40 41 PMEXPORT PtError Pt_Stop() 42 { 43 if (!time_started_flag) return ptAlreadyStopped; 44 if (time_callback && timer_id) { 45 timeKillEvent(timer_id); 46 time_callback = NULL; 47 timer_id = 0; 48 } 49 time_started_flag = FALSE; 50 timeEndPeriod(time_resolution); 51 return ptNoError; 52 } 53 54 55 PMEXPORT int Pt_Started() 56 { 57 return time_started_flag; 58 } 59 60 61 PMEXPORT PtTimestamp Pt_Time() 62 { 63 return timeGetTime() - time_offset; 64 } 65 66 67 PMEXPORT void Pt_Sleep(int32_t duration) 68 { 69 Sleep(duration); 70 }