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"
33 #include "kernel_private.h"
34 #include "kernel16_private.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(system);
41 SYSTEMTIMERPROC callback; /* NULL if not in use */
47 #define NB_SYS_TIMERS 8
48 #define SYS_TIMER_RATE 54925
50 static SYSTEM_TIMER SYS_Timers[NB_SYS_TIMERS];
51 static int SYS_NbTimers = 0;
52 static HANDLE SYS_timer;
53 static HANDLE SYS_thread;
54 static int SYS_timers_disabled;
56 /***********************************************************************
59 static void CALLBACK SYSTEM_TimerTick( LPVOID arg, DWORD low, DWORD high )
63 if (SYS_timers_disabled) return;
64 for (i = 0; i < NB_SYS_TIMERS; i++)
66 if (!SYS_Timers[i].callback) continue;
67 if ((SYS_Timers[i].ticks -= SYS_TIMER_RATE) <= 0)
69 SYS_Timers[i].ticks += SYS_Timers[i].rate;
70 SYS_Timers[i].callback( i+1 );
76 /***********************************************************************
79 static DWORD CALLBACK SYSTEM_TimerThread( void *dummy )
83 if (!(SYS_timer = CreateWaitableTimerA( NULL, FALSE, NULL ))) return 0;
85 when.u.LowPart = when.u.HighPart = 0;
86 SetWaitableTimer( SYS_timer, &when, (SYS_TIMER_RATE+500)/1000, SYSTEM_TimerTick, 0, FALSE );
87 for (;;) SleepEx( INFINITE, TRUE );
91 /**********************************************************************
94 * Start the system tick timer.
96 static void SYSTEM_StartTicks(void)
98 if (!SYS_thread) SYS_thread = CreateThread( NULL, 0, SYSTEM_TimerThread, NULL, 0, NULL );
102 /**********************************************************************
105 * Stop the system tick timer.
107 static void SYSTEM_StopTicks(void)
111 CancelWaitableTimer( SYS_timer );
112 TerminateThread( SYS_thread, 0 );
113 CloseHandle( SYS_thread );
114 CloseHandle( SYS_timer );
120 /***********************************************************************
121 * InquireSystem (SYSTEM.1)
123 * Note: the function always takes 2 WORD arguments, contrary to what
124 * "Undocumented Windows" says.
126 DWORD WINAPI InquireSystem16( WORD code, WORD arg )
132 case 0: /* Get timer resolution */
133 return SYS_TIMER_RATE;
135 case 1: /* Get drive type */
136 drivetype = GetDriveType16( arg );
137 return MAKELONG( drivetype, drivetype );
139 case 2: /* Enable one-drive logic */
140 FIXME("Case %d: set single-drive %d not supported\n", code, arg );
143 WARN("Unknown code %d\n", code );
148 /***********************************************************************
149 * CreateSystemTimer (SYSTEM.2)
151 WORD WINAPI CreateSystemTimer( WORD rate, SYSTEMTIMERPROC callback )
154 for (i = 0; i < NB_SYS_TIMERS; i++)
155 if (!SYS_Timers[i].callback) /* Found one */
157 SYS_Timers[i].rate = (UINT)rate * 1000;
158 if (SYS_Timers[i].rate < SYS_TIMER_RATE)
159 SYS_Timers[i].rate = SYS_TIMER_RATE;
160 SYS_Timers[i].ticks = SYS_Timers[i].rate;
161 SYS_Timers[i].callback = callback;
162 if (++SYS_NbTimers == 1) SYSTEM_StartTicks();
163 return i + 1; /* 0 means error */
168 /**********************************************************************/
170 static void call_timer_proc16( WORD timer )
173 FARPROC16 proc = SYS_Timers[timer-1].callback16;
175 memset( &context, 0, sizeof(context) );
177 context.SegFs = wine_get_fs();
178 context.SegGs = wine_get_gs();
179 context.SegCs = SELECTOROF( proc );
180 context.Eip = OFFSETOF( proc );
181 context.Ebp = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + (WORD)&((STACK16FRAME*)0)->bp;
184 WOWCallback16Ex( 0, WCB16_REGS, 0, NULL, (DWORD *)&context );
187 /**********************************************************************/
189 WORD WINAPI WIN16_CreateSystemTimer( WORD rate, FARPROC16 proc )
191 WORD ret = CreateSystemTimer( rate, call_timer_proc16 );
192 if (ret) SYS_Timers[ret - 1].callback16 = proc;
197 /***********************************************************************
198 * KillSystemTimer (SYSTEM.3)
200 * Note: do not confuse this function with USER.182
202 WORD WINAPI SYSTEM_KillSystemTimer( WORD timer )
204 if ( !timer || timer > NB_SYS_TIMERS || !SYS_Timers[timer-1].callback )
205 return timer; /* Error */
206 SYS_Timers[timer-1].callback = NULL;
207 if (!--SYS_NbTimers) SYSTEM_StopTicks();
212 /***********************************************************************
213 * EnableSystemTimers (SYSTEM.4)
215 void WINAPI EnableSystemTimers16(void)
217 SYS_timers_disabled = 0;
221 /***********************************************************************
222 * DisableSystemTimers (SYSTEM.5)
224 void WINAPI DisableSystemTimers16(void)
226 SYS_timers_disabled = 1;
230 /***********************************************************************
231 * Get80x87SaveSize (SYSTEM.7)
233 WORD WINAPI Get80x87SaveSize16(void)
239 /***********************************************************************
240 * Save80x87State (SYSTEM.8)
242 void WINAPI Save80x87State16( char *ptr )
245 __asm__(".byte 0x66; fsave %0; fwait" : "=m" (ptr) );
250 /***********************************************************************
251 * Restore80x87State (SYSTEM.9)
253 void WINAPI Restore80x87State16( const char *ptr )
256 __asm__(".byte 0x66; frstor %0" : : "m" (ptr) );