|
From: <kr_...@us...> - 2003-03-26 02:20:00
|
Update of /cvsroot/htoolkit/port/src/cbits/Win32
In directory sc8-pr-cvs1:/tmp/cvs-serv5945/port/src/cbits/Win32
Modified Files:
Timer.c
Log Message:
Efficient implementation for timers
Index: Timer.c
===================================================================
RCS file: /cvsroot/htoolkit/port/src/cbits/Win32/Timer.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** Timer.c 10 Feb 2003 22:42:10 -0000 1.2
--- Timer.c 26 Mar 2003 02:19:57 -0000 1.3
***************
*** 3,22 ****
#include "Internals.h"
! static VOID CALLBACK osTimerProc(HWND hwnd, UINT msg, UINT_PTR timer, DWORD time)
{
! handleTimer((TimerHandle) timer);
}
TimerHandle osCreateTimer(int msecs)
{
! TimerHandle r = (TimerHandle)SetTimer( NULL, 0, msecs, osTimerProc );
! if (!r)
{
! printf( "Timer: failed to create timer\n" );
! return NULL;
! }
gActiveObjects++;
! return r;
}
--- 3,61 ----
#include "Internals.h"
! static HWND ghTimerWnd = NULL;
!
! static LRESULT CALLBACK HTimerWindowFunction(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
! switch (uMsg)
! {
! case WM_TIMER:
! {
! TimerHandle timer = (TimerHandle) wParam;
! if (timer->enabled) handleTimer(timer);
! }
! return 0;
! default:
! return DefWindowProc(hWnd, uMsg, wParam, lParam);
! }
}
TimerHandle osCreateTimer(int msecs)
{
! TimerHandle timer;
!
! if (!ghTimerWnd)
{
! WNDCLASS wc;
!
! wc.style = CS_DBLCLKS;
! wc.lpfnWndProc = HTimerWindowFunction;
! wc.cbClsExtra = 0;
! wc.cbWndExtra = 0;
! wc.hInstance = ghModule;
! wc.hIcon = NULL;
! wc.hCursor = NULL;
! wc.hbrBackground = NULL;
! wc.lpszMenuName = NULL;
! wc.lpszClassName = "HTIMERWINDOW";
! RegisterClass(&wc);
!
! ghTimerWnd = CreateWindow(
! "HTIMERWINDOW",
! NULL,
! 0,
! CW_USEDEFAULT,0,0,0,
! NULL,
! NULL,
! ghModule,
! NULL
! );
! };
+ timer = (TimerHandle) rmalloc(sizeof(*timer));
+ timer->interval = msecs;
+ timer->enabled = TRUE;
+ timer->id = (msecs > 0) ? SetTimer(ghTimerWnd, (WPARAM) timer, msecs, NULL) : 0;
gActiveObjects++;
! return timer;
}
***************
*** 25,31 ****
if (timer!=NULL)
{
! KillTimer(NULL,(UINT_PTR)timer);
gActiveObjects--;
}
}
--- 64,111 ----
if (timer!=NULL)
{
! if (timer->id > 0)
! KillTimer(ghTimerWnd,timer->id);
! rfree(timer);
gActiveObjects--;
}
}
+ void osSetTimerInterval(TimerHandle timer, int msecs)
+ {
+ if (timer->interval != msecs)
+ {
+ timer->interval = msecs;
+ if (timer->enabled)
+ {
+ if (timer->id > 0)
+ KillTimer(ghTimerWnd,timer->id);
+ timer->id = (msecs > 0) ? SetTimer(ghTimerWnd, (WPARAM) timer, msecs, NULL) : 0;
+ }
+ }
+ };
+
+ int osGetTimerInterval(TimerHandle timer)
+ {
+ return timer->interval;
+ };
+
+ void osEnableTimer(TimerHandle timer, BOOL enabled)
+ {
+ timer->enabled = enabled;
+ if (timer->enabled)
+ {
+ if (timer->id <= 0 && timer->interval > 0)
+ timer->id = SetTimer(ghTimerWnd, (WPARAM) timer, timer->interval, NULL);
+ }
+ else
+ {
+ if (timer->id > 0)
+ KillTimer(ghTimerWnd,timer->id);
+ timer->id = 0;
+ }
+ };
+
+ BOOL osIsTimerEnabled(TimerHandle timer)
+ {
+ return timer->enabled;
+ };
|