4 * Copyright 1996 Alexandre Julliard
12 #include <sys/timeb.h>
13 #include <sys/types.h>
21 FARPROC16 callback
; /* NULL if not in use */
26 #define NB_SYS_TIMERS 8
27 #define SYS_TIMER_RATE 54925
29 static SYSTEM_TIMER SYS_Timers
[NB_SYS_TIMERS
];
30 static int SYS_NbTimers
= 0;
31 static BOOL32 SYS_TimersDisabled
= FALSE
;
33 /***********************************************************************
36 static void SYSTEM_TimerTick(void)
40 for (i
= 0; i
< NB_SYS_TIMERS
; i
++)
42 if (!SYS_Timers
[i
].callback
) continue;
43 if ((SYS_Timers
[i
].ticks
-= SYS_TIMER_RATE
) <= 0)
45 SYS_Timers
[i
].ticks
+= SYS_Timers
[i
].rate
;
46 SYS_Timers
[i
].callback();
52 /**********************************************************************
55 * Start the system tick timer.
57 static void SYSTEM_StartTicks(void)
59 static BOOL32 handler_installed
= FALSE
;
61 if (!handler_installed
)
63 handler_installed
= TRUE
;
64 SIGNAL_SetHandler( SIGALRM
, SYSTEM_TimerTick
, 1 );
66 #ifndef __EMX__ /* FIXME: Time don't work... Use BIOS directly instead */
68 struct itimerval vt_timer
;
70 vt_timer
.it_interval
.tv_sec
= 0;
71 vt_timer
.it_interval
.tv_usec
= 54929;
72 vt_timer
.it_value
= vt_timer
.it_interval
;
73 setitimer( ITIMER_REAL
, &vt_timer
, NULL
);
79 /**********************************************************************
82 * Stop the system tick timer.
84 static void SYSTEM_StopTicks(void)
86 #ifndef __EMX__ /* FIXME: Time don't work... Use BIOS directly instead */
87 struct itimerval vt_timer
;
89 vt_timer
.it_interval
.tv_sec
= 0;
90 vt_timer
.it_interval
.tv_usec
= 0;
91 vt_timer
.it_value
= vt_timer
.it_interval
;
92 setitimer( ITIMER_REAL
, &vt_timer
, NULL
);
97 /***********************************************************************
98 * InquireSystem (SYSTEM.1)
100 * Note: the function always takes 2 WORD arguments, contrary to what
101 * "Undocumented Windows" says.
103 DWORD
InquireSystem( WORD code
, WORD arg
)
109 case 0: /* Get timer resolution */
110 return SYS_TIMER_RATE
;
112 case 1: /* Get drive type */
113 drivetype
= GetDriveType16( arg
);
114 return MAKELONG( drivetype
, drivetype
);
116 case 2: /* Enable one-drive logic */
117 fprintf( stderr
, "InquireSystem(2): set single-drive %d not supported\n", arg
);
120 fprintf( stderr
, "InquireSystem: unknown code %d\n", code
);
125 /***********************************************************************
126 * CreateSystemTimer (SYSTEM.2)
128 WORD
CreateSystemTimer( WORD rate
, FARPROC16 callback
)
132 for (i
= 0; i
< NB_SYS_TIMERS
; i
++)
133 if (!SYS_Timers
[i
].callback
) /* Found one */
135 SYS_Timers
[i
].rate
= (UINT32
)rate
* 1000;
136 if (SYS_Timers
[i
].rate
< SYS_TIMER_RATE
)
137 SYS_Timers
[i
].rate
= SYS_TIMER_RATE
;
138 SYS_Timers
[i
].ticks
= SYS_Timers
[i
].rate
;
139 SYS_Timers
[i
].callback
= callback
;
140 if ((++SYS_NbTimers
== 1) && !SYS_TimersDisabled
)
142 return i
+ 1; /* 0 means error */
148 /***********************************************************************
149 * KillSystemTimer (SYSTEM.3)
151 * Note: do not confuse this function with USER.182
153 WORD
SYSTEM_KillSystemTimer( WORD timer
)
155 if (!timer
|| (timer
> NB_SYS_TIMERS
)) return timer
; /* Error */
156 SYS_Timers
[timer
-1].callback
= NULL
;
157 if ((!--SYS_NbTimers
) && !SYS_TimersDisabled
) SYSTEM_StopTicks();
162 /***********************************************************************
163 * EnableSystemTimers (SYSTEM.4)
165 void EnableSystemTimers(void)
167 SYS_TimersDisabled
= FALSE
;
168 if (SYS_NbTimers
) SYSTEM_StartTicks();
172 /***********************************************************************
173 * DisableSystemTimers (SYSTEM.5)
175 void DisableSystemTimers(void)
177 SYS_TimersDisabled
= TRUE
;
178 if (SYS_NbTimers
) SYSTEM_StopTicks();
182 /***********************************************************************
183 * SYSTEM_GetTimerProc
185 * Return the timer proc of a system timer. Used by thunking code.
187 FARPROC16
SYSTEM_GetTimerProc( WORD timer
)
189 if (!timer
|| (timer
> NB_SYS_TIMERS
)) return NULL
;
190 return SYS_Timers
[timer
-1].callback
;