Release 1.1.37.
[wine/gsoc-2012-control.git] / dlls / system.drv16 / system.c
blob7d8433a2aff00d4a0f024ff8183feccc1c95202f
1 /*
2 * SYSTEM DLL routines
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
23 #include <stdarg.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wine/winbase16.h"
28 #include "wine/winuser16.h"
29 #include "wownt32.h"
30 #include "winternl.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(system);
35 typedef struct
37 FARPROC16 callback16;
38 INT rate;
39 INT ticks;
40 } SYSTEM_TIMER;
42 #define NB_SYS_TIMERS 8
43 #define SYS_TIMER_RATE 54925
45 static SYSTEM_TIMER SYS_Timers[NB_SYS_TIMERS];
46 static int SYS_NbTimers = 0;
47 static HANDLE SYS_timer;
48 static HANDLE SYS_thread;
49 static int SYS_timers_disabled;
52 /***********************************************************************
53 * SYSTEM_TimerTick
55 static void CALLBACK SYSTEM_TimerTick( LPVOID arg, DWORD low, DWORD high )
57 int i;
59 if (SYS_timers_disabled) return;
60 for (i = 0; i < NB_SYS_TIMERS; i++)
62 if (!SYS_Timers[i].callback16) continue;
63 if ((SYS_Timers[i].ticks -= SYS_TIMER_RATE) <= 0)
65 FARPROC16 proc = SYS_Timers[i].callback16;
66 CONTEXT86 context;
68 SYS_Timers[i].ticks += SYS_Timers[i].rate;
70 memset( &context, 0, sizeof(context) );
71 context.SegFs = wine_get_fs();
72 context.SegGs = wine_get_gs();
73 context.SegCs = SELECTOROF( proc );
74 context.Eip = OFFSETOF( proc );
75 context.Ebp = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + FIELD_OFFSET(STACK16FRAME, bp);
76 context.Eax = i + 1;
78 WOWCallback16Ex( 0, WCB16_REGS, 0, NULL, (DWORD *)&context );
84 /***********************************************************************
85 * SYSTEM_TimerThread
87 static DWORD CALLBACK SYSTEM_TimerThread( void *dummy )
89 LARGE_INTEGER when;
91 if (!(SYS_timer = CreateWaitableTimerA( NULL, FALSE, NULL ))) return 0;
93 when.u.LowPart = when.u.HighPart = 0;
94 SetWaitableTimer( SYS_timer, &when, (SYS_TIMER_RATE+500)/1000, SYSTEM_TimerTick, 0, FALSE );
95 for (;;) SleepEx( INFINITE, TRUE );
99 /**********************************************************************
100 * SYSTEM_StartTicks
102 * Start the system tick timer.
104 static void SYSTEM_StartTicks(void)
106 if (!SYS_thread) SYS_thread = CreateThread( NULL, 0, SYSTEM_TimerThread, NULL, 0, NULL );
110 /**********************************************************************
111 * SYSTEM_StopTicks
113 * Stop the system tick timer.
115 static void SYSTEM_StopTicks(void)
117 if (SYS_thread)
119 CancelWaitableTimer( SYS_timer );
120 TerminateThread( SYS_thread, 0 );
121 CloseHandle( SYS_thread );
122 CloseHandle( SYS_timer );
123 SYS_thread = 0;
128 /***********************************************************************
129 * InquireSystem (SYSTEM.1)
131 * Note: the function always takes 2 WORD arguments, contrary to what
132 * "Undocumented Windows" says.
134 DWORD WINAPI InquireSystem16( WORD code, WORD arg )
136 WORD drivetype;
137 WCHAR root[3];
139 switch(code)
141 case 0: /* Get timer resolution */
142 return SYS_TIMER_RATE;
144 case 1: /* Get drive type */
145 root[0] = 'A' + arg;
146 root[1] = ':';
147 root[2] = 0;
148 drivetype = GetDriveTypeW( root );
149 if (drivetype == DRIVE_CDROM) drivetype = DRIVE_REMOTE;
150 else if (drivetype == DRIVE_NO_ROOT_DIR) drivetype = DRIVE_UNKNOWN;
151 return MAKELONG( drivetype, drivetype );
153 case 2: /* Enable one-drive logic */
154 FIXME("Case %d: set single-drive %d not supported\n", code, arg );
155 return 0;
157 WARN("Unknown code %d\n", code );
158 return 0;
162 /***********************************************************************
163 * CreateSystemTimer (SYSTEM.2)
165 WORD WINAPI CreateSystemTimer16( WORD rate, FARPROC16 proc )
167 int i;
168 for (i = 0; i < NB_SYS_TIMERS; i++)
169 if (!SYS_Timers[i].callback16) /* Found one */
171 SYS_Timers[i].rate = (UINT)rate * 1000;
172 if (SYS_Timers[i].rate < SYS_TIMER_RATE)
173 SYS_Timers[i].rate = SYS_TIMER_RATE;
174 SYS_Timers[i].ticks = SYS_Timers[i].rate;
175 SYS_Timers[i].callback16 = proc;
176 if (++SYS_NbTimers == 1) SYSTEM_StartTicks();
177 return i + 1; /* 0 means error */
179 return 0;
183 /***********************************************************************
184 * KillSystemTimer (SYSTEM.3)
186 * Note: do not confuse this function with USER.182
188 WORD WINAPI SYSTEM_KillSystemTimer( WORD timer )
190 if ( !timer || timer > NB_SYS_TIMERS || !SYS_Timers[timer-1].callback16 )
191 return timer; /* Error */
192 SYS_Timers[timer-1].callback16 = 0;
193 if (!--SYS_NbTimers) SYSTEM_StopTicks();
194 return 0;
198 /***********************************************************************
199 * EnableSystemTimers (SYSTEM.4)
201 void WINAPI EnableSystemTimers16(void)
203 SYS_timers_disabled = 0;
207 /***********************************************************************
208 * DisableSystemTimers (SYSTEM.5)
210 void WINAPI DisableSystemTimers16(void)
212 SYS_timers_disabled = 1;
216 /***********************************************************************
217 * GetSystemMSecCount (SYSTEM.6)
219 DWORD WINAPI GetSystemMSecCount16(void)
221 return GetTickCount();
225 /***********************************************************************
226 * Get80x87SaveSize (SYSTEM.7)
228 WORD WINAPI Get80x87SaveSize16(void)
230 return 94;
234 /***********************************************************************
235 * Save80x87State (SYSTEM.8)
237 void WINAPI Save80x87State16( char *ptr )
239 #ifdef __i386__
240 __asm__(".byte 0x66; fsave %0; fwait" : "=m" (ptr) );
241 #endif
245 /***********************************************************************
246 * Restore80x87State (SYSTEM.9)
248 void WINAPI Restore80x87State16( const char *ptr )
250 #ifdef __i386__
251 __asm__(".byte 0x66; frstor %0" : : "m" (ptr) );
252 #endif
256 /***********************************************************************
257 * A20_Proc (SYSTEM.20)
259 void WINAPI A20_Proc16( WORD unused )
261 /* this is also a NOP in Windows */