4 * Copyright 1996 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #define NONAMELESSUNION
22 #define NONAMELESSSTRUCT
28 #include "wine/winbase16.h"
29 #include "wine/winuser16.h"
31 #include "kernel_private.h"
32 #include "kernel16_private.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(system);
39 SYSTEMTIMERPROC callback; /* NULL if not in use */
45 #define NB_SYS_TIMERS 8
46 #define SYS_TIMER_RATE 54925
48 static SYSTEM_TIMER SYS_Timers[NB_SYS_TIMERS];
49 static int SYS_NbTimers = 0;
50 static HANDLE SYS_timer;
51 static HANDLE SYS_thread;
52 static int SYS_timers_disabled;
54 /***********************************************************************
57 static void CALLBACK SYSTEM_TimerTick( LPVOID arg, DWORD low, DWORD high )
61 if (SYS_timers_disabled) return;
62 for (i = 0; i < NB_SYS_TIMERS; i++)
64 if (!SYS_Timers[i].callback) continue;
65 if ((SYS_Timers[i].ticks -= SYS_TIMER_RATE) <= 0)
67 SYS_Timers[i].ticks += SYS_Timers[i].rate;
68 SYS_Timers[i].callback( i+1 );
74 /***********************************************************************
77 static DWORD CALLBACK SYSTEM_TimerThread( void *dummy )
81 if (!(SYS_timer = CreateWaitableTimerA( NULL, FALSE, NULL ))) return 0;
83 when.u.LowPart = when.u.HighPart = 0;
84 SetWaitableTimer( SYS_timer, &when, (SYS_TIMER_RATE+500)/1000, SYSTEM_TimerTick, 0, FALSE );
85 for (;;) SleepEx( INFINITE, TRUE );
89 /**********************************************************************
92 * Start the system tick timer.
94 static void SYSTEM_StartTicks(void)
96 if (!SYS_thread) SYS_thread = CreateThread( NULL, 0, SYSTEM_TimerThread, NULL, 0, NULL );
100 /**********************************************************************
103 * Stop the system tick timer.
105 static void SYSTEM_StopTicks(void)
109 CancelWaitableTimer( SYS_timer );
110 TerminateThread( SYS_thread, 0 );
111 CloseHandle( SYS_thread );
112 CloseHandle( SYS_timer );
118 /***********************************************************************
119 * InquireSystem (SYSTEM.1)
121 * Note: the function always takes 2 WORD arguments, contrary to what
122 * "Undocumented Windows" says.
124 DWORD WINAPI InquireSystem16( 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("Case %d: set single-drive %d not supported\n", code, arg );
141 WARN("Unknown code %d\n", code );
146 /***********************************************************************
147 * CreateSystemTimer (SYSTEM.2)
149 WORD WINAPI CreateSystemTimer( WORD rate, SYSTEMTIMERPROC callback )
152 for (i = 0; i < NB_SYS_TIMERS; i++)
153 if (!SYS_Timers[i].callback) /* Found one */
155 SYS_Timers[i].rate = (UINT)rate * 1000;
156 if (SYS_Timers[i].rate < SYS_TIMER_RATE)
157 SYS_Timers[i].rate = SYS_TIMER_RATE;
158 SYS_Timers[i].ticks = SYS_Timers[i].rate;
159 SYS_Timers[i].callback = callback;
160 if (++SYS_NbTimers == 1) SYSTEM_StartTicks();
161 return i + 1; /* 0 means error */
166 /**********************************************************************/
168 static void call_timer_proc16( WORD timer )
171 FARPROC16 proc = SYS_Timers[timer-1].callback16;
173 memset( &context, 0, sizeof(context) );
175 context.SegFs = wine_get_fs();
176 context.SegGs = wine_get_gs();
177 context.SegCs = SELECTOROF( proc );
178 context.Eip = OFFSETOF( proc );
179 context.Ebp = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + (WORD)&((STACK16FRAME*)0)->bp;
182 WOWCallback16Ex( 0, WCB16_REGS, 0, NULL, (DWORD *)&context );
185 /**********************************************************************/
187 WORD WINAPI WIN16_CreateSystemTimer( WORD rate, FARPROC16 proc )
189 WORD ret = CreateSystemTimer( rate, call_timer_proc16 );
190 if (ret) SYS_Timers[ret - 1].callback16 = proc;
195 /***********************************************************************
196 * KillSystemTimer (SYSTEM.3)
198 * Note: do not confuse this function with USER.182
200 WORD WINAPI SYSTEM_KillSystemTimer( WORD timer )
202 if ( !timer || timer > NB_SYS_TIMERS || !SYS_Timers[timer-1].callback )
203 return timer; /* Error */
204 SYS_Timers[timer-1].callback = NULL;
205 if (!--SYS_NbTimers) SYSTEM_StopTicks();
210 /***********************************************************************
211 * EnableSystemTimers (SYSTEM.4)
213 void WINAPI EnableSystemTimers16(void)
215 SYS_timers_disabled = 0;
219 /***********************************************************************
220 * DisableSystemTimers (SYSTEM.5)
222 void WINAPI DisableSystemTimers16(void)
224 SYS_timers_disabled = 1;
228 /***********************************************************************
229 * Get80x87SaveSize (SYSTEM.7)
231 WORD WINAPI Get80x87SaveSize16(void)
237 /***********************************************************************
238 * Save80x87State (SYSTEM.8)
240 void WINAPI Save80x87State16( char *ptr )
243 __asm__(".byte 0x66; fsave %0; fwait" : "=m" (ptr) );
248 /***********************************************************************
249 * Restore80x87State (SYSTEM.9)
251 void WINAPI Restore80x87State16( const char *ptr )
254 __asm__(".byte 0x66; frstor %0" : : "m" (ptr) );