fltlib: Add a stub dll.
[wine] / dlls / system.drv16 / system.c
1 /*
2  * SYSTEM DLL routines
3  *
4  * Copyright 1996 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #define NONAMELESSUNION
22 #define NONAMELESSSTRUCT
23 #include <stdarg.h>
24
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"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(system);
34
35 typedef struct
36 {
37     SYSTEMTIMERPROC callback;  /* NULL if not in use */
38     FARPROC16       callback16;
39     INT             rate;
40     INT             ticks;
41 } SYSTEM_TIMER;
42
43 #define NB_SYS_TIMERS   8
44 #define SYS_TIMER_RATE  54925
45
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;
51
52 /***********************************************************************
53  *           SYSTEM_TimerTick
54  */
55 static void CALLBACK SYSTEM_TimerTick( LPVOID arg, DWORD low, DWORD high )
56 {
57     int i;
58
59     if (SYS_timers_disabled) return;
60     for (i = 0; i < NB_SYS_TIMERS; i++)
61     {
62         if (!SYS_Timers[i].callback) continue;
63         if ((SYS_Timers[i].ticks -= SYS_TIMER_RATE) <= 0)
64         {
65             SYS_Timers[i].ticks += SYS_Timers[i].rate;
66             SYS_Timers[i].callback( i+1 );
67         }
68     }
69 }
70
71
72 /***********************************************************************
73  *           SYSTEM_TimerThread
74  */
75 static DWORD CALLBACK SYSTEM_TimerThread( void *dummy )
76 {
77     LARGE_INTEGER when;
78
79     if (!(SYS_timer = CreateWaitableTimerA( NULL, FALSE, NULL ))) return 0;
80
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 );
84 }
85
86
87 /**********************************************************************
88  *           SYSTEM_StartTicks
89  *
90  * Start the system tick timer.
91  */
92 static void SYSTEM_StartTicks(void)
93 {
94     if (!SYS_thread) SYS_thread = CreateThread( NULL, 0, SYSTEM_TimerThread, NULL, 0, NULL );
95 }
96
97
98 /**********************************************************************
99  *           SYSTEM_StopTicks
100  *
101  * Stop the system tick timer.
102  */
103 static void SYSTEM_StopTicks(void)
104 {
105     if (SYS_thread)
106     {
107         CancelWaitableTimer( SYS_timer );
108         TerminateThread( SYS_thread, 0 );
109         CloseHandle( SYS_thread );
110         CloseHandle( SYS_timer );
111         SYS_thread = 0;
112     }
113 }
114
115
116 /***********************************************************************
117  *           InquireSystem   (SYSTEM.1)
118  *
119  * Note: the function always takes 2 WORD arguments, contrary to what
120  *       "Undocumented Windows" says.
121   */
122 DWORD WINAPI InquireSystem16( WORD code, WORD arg )
123 {
124     WORD drivetype;
125     WCHAR root[3];
126
127     switch(code)
128     {
129     case 0:  /* Get timer resolution */
130         return SYS_TIMER_RATE;
131
132     case 1:  /* Get drive type */
133         root[0] = 'A' + arg;
134         root[1] = ':';
135         root[2] = 0;
136         drivetype = GetDriveTypeW( root );
137         if (drivetype == DRIVE_CDROM) drivetype = DRIVE_REMOTE;
138         else if (drivetype == DRIVE_NO_ROOT_DIR) drivetype = DRIVE_UNKNOWN;
139         return MAKELONG( drivetype, drivetype );
140
141     case 2:  /* Enable one-drive logic */
142         FIXME("Case %d: set single-drive %d not supported\n", code, arg );
143         return 0;
144     }
145     WARN("Unknown code %d\n", code );
146     return 0;
147 }
148
149
150 /***********************************************************************
151  *           CreateSystemTimer   (SYSTEM.2)
152  */
153 WORD WINAPI CreateSystemTimer( WORD rate, SYSTEMTIMERPROC callback )
154 {
155     int i;
156     for (i = 0; i < NB_SYS_TIMERS; i++)
157         if (!SYS_Timers[i].callback)  /* Found one */
158         {
159             SYS_Timers[i].rate = (UINT)rate * 1000;
160             if (SYS_Timers[i].rate < SYS_TIMER_RATE)
161                 SYS_Timers[i].rate = SYS_TIMER_RATE;
162             SYS_Timers[i].ticks = SYS_Timers[i].rate;
163             SYS_Timers[i].callback = callback;
164             if (++SYS_NbTimers == 1) SYSTEM_StartTicks();
165             return i + 1;  /* 0 means error */
166         }
167     return 0;
168 }
169
170 /**********************************************************************/
171
172 static void call_timer_proc16( WORD timer )
173 {
174     CONTEXT86 context;
175     FARPROC16 proc = SYS_Timers[timer-1].callback16;
176
177     memset( &context, 0, sizeof(context) );
178
179     context.SegFs = wine_get_fs();
180     context.SegGs = wine_get_gs();
181     context.SegCs = SELECTOROF( proc );
182     context.Eip   = OFFSETOF( proc );
183     context.Ebp   = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + FIELD_OFFSET(STACK16FRAME,bp);
184     context.Eax   = timer;
185
186     WOWCallback16Ex( 0, WCB16_REGS, 0, NULL, (DWORD *)&context );
187 }
188
189 /**********************************************************************/
190
191 WORD WINAPI WIN16_CreateSystemTimer( WORD rate, FARPROC16 proc )
192 {
193     WORD ret = CreateSystemTimer( rate, call_timer_proc16 );
194     if (ret) SYS_Timers[ret - 1].callback16 = proc;
195     return ret;
196 }
197
198
199 /***********************************************************************
200  *           KillSystemTimer   (SYSTEM.3)
201  *
202  * Note: do not confuse this function with USER.182
203  */
204 WORD WINAPI SYSTEM_KillSystemTimer( WORD timer )
205 {
206     if ( !timer || timer > NB_SYS_TIMERS || !SYS_Timers[timer-1].callback )
207         return timer;  /* Error */
208     SYS_Timers[timer-1].callback = NULL;
209     if (!--SYS_NbTimers) SYSTEM_StopTicks();
210     return 0;
211 }
212
213
214 /***********************************************************************
215  *           EnableSystemTimers   (SYSTEM.4)
216  */
217 void WINAPI EnableSystemTimers16(void)
218 {
219     SYS_timers_disabled = 0;
220 }
221
222
223 /***********************************************************************
224  *           DisableSystemTimers   (SYSTEM.5)
225  */
226 void WINAPI DisableSystemTimers16(void)
227 {
228     SYS_timers_disabled = 1;
229 }
230
231
232 /***********************************************************************
233  *           GetSystemMSecCount (SYSTEM.6)
234  */
235 DWORD WINAPI GetSystemMSecCount16(void)
236 {
237     return GetTickCount();
238 }
239
240
241 /***********************************************************************
242  *           Get80x87SaveSize   (SYSTEM.7)
243  */
244 WORD WINAPI Get80x87SaveSize16(void)
245 {
246     return 94;
247 }
248
249
250 /***********************************************************************
251  *           Save80x87State   (SYSTEM.8)
252  */
253 void WINAPI Save80x87State16( char *ptr )
254 {
255 #ifdef __i386__
256     __asm__(".byte 0x66; fsave %0; fwait" : "=m" (ptr) );
257 #endif
258 }
259
260
261 /***********************************************************************
262  *           Restore80x87State   (SYSTEM.9)
263  */
264 void WINAPI Restore80x87State16( const char *ptr )
265 {
266 #ifdef __i386__
267     __asm__(".byte 0x66; frstor %0" : : "m" (ptr) );
268 #endif
269 }
270
271
272 /***********************************************************************
273  *           A20_Proc  (SYSTEM.20)
274  */
275 void WINAPI A20_Proc16( WORD unused )
276 {
277     /* this is also a NOP in Windows */
278 }