4 * Copyright 1996 Alexandre Julliard
11 #include <sys/timeb.h>
12 #include <sys/types.h>
18 #include "selectors.h"
19 #include "sig_context.h"
24 FARPROC16 callback; /* NULL if not in use */
29 #define NB_SYS_TIMERS 8
30 #define SYS_TIMER_RATE 54925
32 static SYSTEM_TIMER SYS_Timers[NB_SYS_TIMERS];
33 static int SYS_NbTimers = 0;
34 static BOOL32 SYS_TimersDisabled = FALSE;
36 /***********************************************************************
38 * FIXME: It is a very bad idea to call 16bit code in a signal handler:
39 * If the signal reached us in 16 bit code, we could have a broken
40 * %FS, which is in turned saved into the single global
41 * CALLTO16_Current_fs temporary storage, so a single misplaced
42 * signal crashes the whole WINE process.
43 * This needs more thought. -MM
45 static HANDLER_DEF(SYSTEM_TimerTick)
50 for (i = 0; i < NB_SYS_TIMERS; i++)
52 if (!SYS_Timers[i].callback) continue;
53 if ((SYS_Timers[i].ticks -= SYS_TIMER_RATE) <= 0)
55 SYS_Timers[i].ticks += SYS_Timers[i].rate;
57 if (SYS_Timers[i].callback == (FARPROC16)DOSMEM_Tick) {
60 Callbacks->CallSystemTimerProc( SYS_Timers[i].callback );
65 /**********************************************************************
68 * Start the system tick timer.
70 static void SYSTEM_StartTicks(void)
72 static BOOL32 handler_installed = FALSE;
74 if (!handler_installed)
76 handler_installed = TRUE;
77 SIGNAL_SetHandler( SIGALRM, SYSTEM_TimerTick, 1 );
79 #ifndef __EMX__ /* FIXME: Time don't work... Use BIOS directly instead */
81 struct itimerval vt_timer;
83 vt_timer.it_interval.tv_sec = 0;
84 vt_timer.it_interval.tv_usec = 54929;
85 vt_timer.it_value = vt_timer.it_interval;
86 setitimer( ITIMER_REAL, &vt_timer, NULL );
92 /**********************************************************************
95 * Stop the system tick timer.
97 static void SYSTEM_StopTicks(void)
99 #ifndef __EMX__ /* FIXME: Time don't work... Use BIOS directly instead */
100 struct itimerval vt_timer;
102 vt_timer.it_interval.tv_sec = 0;
103 vt_timer.it_interval.tv_usec = 0;
104 vt_timer.it_value = vt_timer.it_interval;
105 setitimer( ITIMER_REAL, &vt_timer, NULL );
110 /***********************************************************************
111 * InquireSystem (SYSTEM.1)
113 * Note: the function always takes 2 WORD arguments, contrary to what
114 * "Undocumented Windows" says.
116 DWORD WINAPI InquireSystem( WORD code, WORD arg )
122 case 0: /* Get timer resolution */
123 return SYS_TIMER_RATE;
125 case 1: /* Get drive type */
126 drivetype = GetDriveType16( arg );
127 return MAKELONG( drivetype, drivetype );
129 case 2: /* Enable one-drive logic */
130 FIXME(system, "Case %d: set single-drive %d not supported\n", code, arg );
133 WARN(system, "Unknown code %d\n", code );
138 /***********************************************************************
139 * CreateSystemTimer (SYSTEM.2)
141 WORD WINAPI CreateSystemTimer( WORD rate, FARPROC16 callback )
145 /* FIXME: HACK: do not create system timers due to problems mentioned
146 * above, except DOSMEM_Tick().
148 if (callback!=(FARPROC16)DOSMEM_Tick) {
149 FIXME(system,"are currently broken, returning 0.\n");
153 for (i = 0; i < NB_SYS_TIMERS; i++)
154 if (!SYS_Timers[i].callback) /* Found one */
156 SYS_Timers[i].rate = (UINT32)rate * 1000;
157 if (SYS_Timers[i].rate < SYS_TIMER_RATE)
158 SYS_Timers[i].rate = SYS_TIMER_RATE;
159 SYS_Timers[i].ticks = SYS_Timers[i].rate;
160 SYS_Timers[i].callback = callback;
161 if ((++SYS_NbTimers == 1) && !SYS_TimersDisabled)
163 return i + 1; /* 0 means error */
169 /***********************************************************************
170 * KillSystemTimer (SYSTEM.3)
172 * Note: do not confuse this function with USER.182
174 WORD WINAPI SYSTEM_KillSystemTimer( WORD timer )
176 if (!timer || (timer > NB_SYS_TIMERS)) return timer; /* Error */
177 SYS_Timers[timer-1].callback = NULL;
178 if ((!--SYS_NbTimers) && !SYS_TimersDisabled) SYSTEM_StopTicks();
183 /***********************************************************************
184 * EnableSystemTimers (SYSTEM.4)
186 void WINAPI EnableSystemTimers(void)
188 SYS_TimersDisabled = FALSE;
189 if (SYS_NbTimers) SYSTEM_StartTicks();
193 /***********************************************************************
194 * DisableSystemTimers (SYSTEM.5)
196 void WINAPI DisableSystemTimers(void)
198 SYS_TimersDisabled = TRUE;
199 if (SYS_NbTimers) SYSTEM_StopTicks();