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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #define NONAMELESSUNION
22 #define NONAMELESSSTRUCT
27 #include "wine/winbase16.h"
28 #include "wine/winuser16.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(system);
37 SYSTEMTIMERPROC callback; /* NULL if not in use */
43 #define NB_SYS_TIMERS 8
44 #define SYS_TIMER_RATE 54925
46 static SYSTEM_TIMER SYS_Timers[NB_SYS_TIMERS];
47 static int SYS_NbTimers = 0;
48 static HANDLE SYS_timer;
49 static HANDLE SYS_thread;
50 static int SYS_timers_disabled;
52 /***********************************************************************
55 static void CALLBACK SYSTEM_TimerTick( LPVOID arg, DWORD low, DWORD high )
59 if (SYS_timers_disabled) return;
60 for (i = 0; i < NB_SYS_TIMERS; i++)
62 if (!SYS_Timers[i].callback) continue;
63 if ((SYS_Timers[i].ticks -= SYS_TIMER_RATE) <= 0)
65 SYS_Timers[i].ticks += SYS_Timers[i].rate;
66 SYS_Timers[i].callback( i+1 );
72 /***********************************************************************
75 static DWORD CALLBACK SYSTEM_TimerThread( void *dummy )
79 if (!(SYS_timer = CreateWaitableTimerA( NULL, FALSE, NULL ))) return 0;
81 when.u.LowPart = when.u.HighPart = 0;
82 SetWaitableTimer( SYS_timer, &when, (SYS_TIMER_RATE+500)/1000, SYSTEM_TimerTick, 0, FALSE );
83 for (;;) SleepEx( INFINITE, TRUE );
87 /**********************************************************************
90 * Start the system tick timer.
92 static void SYSTEM_StartTicks(void)
94 if (!SYS_thread) SYS_thread = CreateThread( NULL, 0, SYSTEM_TimerThread, NULL, 0, NULL );
98 /**********************************************************************
101 * Stop the system tick timer.
103 static void SYSTEM_StopTicks(void)
107 CancelWaitableTimer( SYS_timer );
108 TerminateThread( SYS_thread, 0 );
109 CloseHandle( SYS_thread );
110 CloseHandle( SYS_timer );
116 /***********************************************************************
117 * InquireSystem (SYSTEM.1)
119 * Note: the function always takes 2 WORD arguments, contrary to what
120 * "Undocumented Windows" says.
122 DWORD WINAPI InquireSystem16( WORD code, WORD arg )
128 case 0: /* Get timer resolution */
129 return SYS_TIMER_RATE;
131 case 1: /* Get drive type */
132 drivetype = GetDriveType16( arg );
133 return MAKELONG( drivetype, drivetype );
135 case 2: /* Enable one-drive logic */
136 FIXME("Case %d: set single-drive %d not supported\n", code, arg );
139 WARN("Unknown code %d\n", code );
144 /***********************************************************************
145 * CreateSystemTimer (SYSTEM.2)
147 WORD WINAPI CreateSystemTimer( WORD rate, SYSTEMTIMERPROC callback )
150 for (i = 0; i < NB_SYS_TIMERS; i++)
151 if (!SYS_Timers[i].callback) /* Found one */
153 SYS_Timers[i].rate = (UINT)rate * 1000;
154 if (SYS_Timers[i].rate < SYS_TIMER_RATE)
155 SYS_Timers[i].rate = SYS_TIMER_RATE;
156 SYS_Timers[i].ticks = SYS_Timers[i].rate;
157 SYS_Timers[i].callback = callback;
158 if (++SYS_NbTimers == 1) SYSTEM_StartTicks();
159 return i + 1; /* 0 means error */
164 /**********************************************************************/
166 static void call_timer_proc16( WORD timer )
169 FARPROC16 proc = SYS_Timers[timer-1].callback16;
171 memset( &context, 0, sizeof(context) );
173 context.SegFs = wine_get_fs();
174 context.SegGs = wine_get_gs();
175 context.SegCs = SELECTOROF( proc );
176 context.Eip = OFFSETOF( proc );
177 context.Ebp = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + (WORD)&((STACK16FRAME*)0)->bp;
180 WOWCallback16Ex( 0, WCB16_REGS, 0, NULL, (DWORD *)&context );
183 /**********************************************************************/
185 WORD WINAPI WIN16_CreateSystemTimer( WORD rate, FARPROC16 proc )
187 WORD ret = CreateSystemTimer( rate, call_timer_proc16 );
188 if (ret) SYS_Timers[ret - 1].callback16 = proc;
193 /***********************************************************************
194 * KillSystemTimer (SYSTEM.3)
196 * Note: do not confuse this function with USER.182
198 WORD WINAPI SYSTEM_KillSystemTimer( WORD timer )
200 if ( !timer || timer > NB_SYS_TIMERS || !SYS_Timers[timer-1].callback )
201 return timer; /* Error */
202 SYS_Timers[timer-1].callback = NULL;
203 if (!--SYS_NbTimers) SYSTEM_StopTicks();
208 /***********************************************************************
209 * EnableSystemTimers (SYSTEM.4)
211 void WINAPI EnableSystemTimers16(void)
213 SYS_timers_disabled = 0;
217 /***********************************************************************
218 * DisableSystemTimers (SYSTEM.5)
220 void WINAPI DisableSystemTimers16(void)
222 SYS_timers_disabled = 1;
226 /***********************************************************************
227 * GetSystemMSecCount (SYSTEM.6)
229 DWORD WINAPI GetSystemMSecCount16(void)
231 return GetTickCount();
235 /***********************************************************************
236 * Get80x87SaveSize (SYSTEM.7)
238 WORD WINAPI Get80x87SaveSize16(void)
244 /***********************************************************************
245 * Save80x87State (SYSTEM.8)
247 void WINAPI Save80x87State16( char *ptr )
250 __asm__(".byte 0x66; fsave %0; fwait" : "=m" (ptr) );
255 /***********************************************************************
256 * Restore80x87State (SYSTEM.9)
258 void WINAPI Restore80x87State16( const char *ptr )
261 __asm__(".byte 0x66; frstor %0" : : "m" (ptr) );
266 /***********************************************************************
267 * A20_Proc (SYSTEM.20)
269 void WINAPI A20_Proc16( WORD unused )
271 /* this is also a NOP in Windows */