4 * Copyright 1996 Alexandre Julliard
11 #include <sys/timeb.h>
12 #include <sys/types.h>
20 #include "selectors.h"
21 #include "sig_context.h"
26 FARPROC16 callback
; /* NULL if not in use */
31 #define NB_SYS_TIMERS 8
32 #define SYS_TIMER_RATE 54925
34 static SYSTEM_TIMER SYS_Timers
[NB_SYS_TIMERS
];
35 static int SYS_NbTimers
= 0;
36 static BOOL32 SYS_TimersDisabled
= FALSE
;
38 /***********************************************************************
40 * FIXME: It is a very bad idea to call 16bit code in a signal handler:
41 * If the signal reached us in 16 bit code, we could have a broken
42 * %FS, which is in turned saved into the single global
43 * CALLTO16_Current_fs temporary storage, so a single misplaced
44 * signal crashes the whole WINE process.
45 * This needs more thought. -MM
47 static HANDLER_DEF(SYSTEM_TimerTick
)
52 for (i
= 0; i
< NB_SYS_TIMERS
; i
++)
54 if (!SYS_Timers
[i
].callback
) continue;
55 if ((SYS_Timers
[i
].ticks
-= SYS_TIMER_RATE
) <= 0)
57 SYS_Timers
[i
].ticks
+= SYS_Timers
[i
].rate
;
59 if (SYS_Timers
[i
].callback
== (FARPROC16
)DOSMEM_Tick
) {
62 if (SYS_Timers
[i
].callback
== (FARPROC16
)MZ_Tick
) {
65 if (SYS_Timers
[i
].callback
== (FARPROC16
)VGA_Poll
) {
68 Callbacks
->CallSystemTimerProc( SYS_Timers
[i
].callback
);
73 /**********************************************************************
76 * Start the system tick timer.
78 static void SYSTEM_StartTicks(void)
80 static BOOL32 handler_installed
= FALSE
;
82 if (!handler_installed
)
84 handler_installed
= TRUE
;
85 SIGNAL_SetHandler( SIGALRM
, SYSTEM_TimerTick
, 1 );
87 #ifndef __EMX__ /* FIXME: Time don't work... Use BIOS directly instead */
89 struct itimerval vt_timer
;
91 vt_timer
.it_interval
.tv_sec
= 0;
92 vt_timer
.it_interval
.tv_usec
= 54929;
93 vt_timer
.it_value
= vt_timer
.it_interval
;
94 setitimer( ITIMER_REAL
, &vt_timer
, NULL
);
100 /**********************************************************************
103 * Stop the system tick timer.
105 static void SYSTEM_StopTicks(void)
107 #ifndef __EMX__ /* FIXME: Time don't work... Use BIOS directly instead */
108 struct itimerval vt_timer
;
110 vt_timer
.it_interval
.tv_sec
= 0;
111 vt_timer
.it_interval
.tv_usec
= 0;
112 vt_timer
.it_value
= vt_timer
.it_interval
;
113 setitimer( ITIMER_REAL
, &vt_timer
, NULL
);
118 /***********************************************************************
119 * InquireSystem (SYSTEM.1)
121 * Note: the function always takes 2 WORD arguments, contrary to what
122 * "Undocumented Windows" says.
124 DWORD WINAPI
InquireSystem( WORD code
, WORD arg
)
130 case 0: /* Get timer resolution */
131 return SYS_TIMER_RATE
;
133 case 1: /* Get drive type */
134 drivetype
= GetDriveType16( arg
);
135 return MAKELONG( drivetype
, drivetype
);
137 case 2: /* Enable one-drive logic */
138 FIXME(system
, "Case %d: set single-drive %d not supported\n", code
, arg
);
141 WARN(system
, "Unknown code %d\n", code
);
146 /***********************************************************************
147 * CreateSystemTimer (SYSTEM.2)
149 WORD WINAPI
CreateSystemTimer( WORD rate
, FARPROC16 callback
)
153 /* FIXME: HACK: do not create system timers due to problems mentioned
154 * above, except DOSMEM_Tick(), MZ_Tick(), and VGA_Poll().
156 if ((callback
!=(FARPROC16
)DOSMEM_Tick
)&&
157 (callback
!=(FARPROC16
)MZ_Tick
)&&
158 (callback
!=(FARPROC16
)VGA_Poll
)) {
159 FIXME(system
,"are currently broken, returning 0.\n");
163 for (i
= 0; i
< NB_SYS_TIMERS
; i
++)
164 if (!SYS_Timers
[i
].callback
) /* Found one */
166 SYS_Timers
[i
].rate
= (UINT32
)rate
* 1000;
167 if (SYS_Timers
[i
].rate
< SYS_TIMER_RATE
)
168 SYS_Timers
[i
].rate
= SYS_TIMER_RATE
;
169 SYS_Timers
[i
].ticks
= SYS_Timers
[i
].rate
;
170 SYS_Timers
[i
].callback
= callback
;
171 if ((++SYS_NbTimers
== 1) && !SYS_TimersDisabled
)
173 return i
+ 1; /* 0 means error */
179 /***********************************************************************
180 * KillSystemTimer (SYSTEM.3)
182 * Note: do not confuse this function with USER.182
184 WORD WINAPI
SYSTEM_KillSystemTimer( WORD timer
)
186 if (!timer
|| (timer
> NB_SYS_TIMERS
)) return timer
; /* Error */
187 SYS_Timers
[timer
-1].callback
= NULL
;
188 if ((!--SYS_NbTimers
) && !SYS_TimersDisabled
) SYSTEM_StopTicks();
193 /***********************************************************************
194 * EnableSystemTimers (SYSTEM.4)
196 void WINAPI
EnableSystemTimers(void)
198 SYS_TimersDisabled
= FALSE
;
199 if (SYS_NbTimers
) SYSTEM_StartTicks();
203 /***********************************************************************
204 * DisableSystemTimers (SYSTEM.5)
206 void WINAPI
DisableSystemTimers(void)
208 SYS_TimersDisabled
= TRUE
;
209 if (SYS_NbTimers
) SYSTEM_StopTicks();