netstat: Initial implementation.
[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     FARPROC16       callback16;
38     INT             rate;
39     INT             ticks;
40 } SYSTEM_TIMER;
41
42 #define NB_SYS_TIMERS   8
43 #define SYS_TIMER_RATE  54925
44
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;
50
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].callback16) continue;
63         if ((SYS_Timers[i].ticks -= SYS_TIMER_RATE) <= 0)
64         {
65             FARPROC16 proc = SYS_Timers[i].callback16;
66             CONTEXT context;
67
68             SYS_Timers[i].ticks += SYS_Timers[i].rate;
69
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;
77
78             WOWCallback16Ex( 0, WCB16_REGS, 0, NULL, (DWORD *)&context );
79         }
80     }
81 }
82
83
84 /***********************************************************************
85  *           SYSTEM_TimerThread
86  */
87 static DWORD CALLBACK SYSTEM_TimerThread( void *dummy )
88 {
89     LARGE_INTEGER when;
90
91     if (!(SYS_timer = CreateWaitableTimerA( NULL, FALSE, NULL ))) return 0;
92
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 );
96 }
97
98
99 /**********************************************************************
100  *           SYSTEM_StartTicks
101  *
102  * Start the system tick timer.
103  */
104 static void SYSTEM_StartTicks(void)
105 {
106     if (!SYS_thread) SYS_thread = CreateThread( NULL, 0, SYSTEM_TimerThread, NULL, 0, NULL );
107 }
108
109
110 /**********************************************************************
111  *           SYSTEM_StopTicks
112  *
113  * Stop the system tick timer.
114  */
115 static void SYSTEM_StopTicks(void)
116 {
117     if (SYS_thread)
118     {
119         CancelWaitableTimer( SYS_timer );
120         TerminateThread( SYS_thread, 0 );
121         CloseHandle( SYS_thread );
122         CloseHandle( SYS_timer );
123         SYS_thread = 0;
124     }
125 }
126
127
128 /***********************************************************************
129  *           InquireSystem   (SYSTEM.1)
130  *
131  * Note: the function always takes 2 WORD arguments, contrary to what
132  *       "Undocumented Windows" says.
133   */
134 DWORD WINAPI InquireSystem16( WORD code, WORD arg )
135 {
136     WORD drivetype;
137     WCHAR root[3];
138
139     switch(code)
140     {
141     case 0:  /* Get timer resolution */
142         return SYS_TIMER_RATE;
143
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 );
152
153     case 2:  /* Enable one-drive logic */
154         FIXME("Case %d: set single-drive %d not supported\n", code, arg );
155         return 0;
156     }
157     WARN("Unknown code %d\n", code );
158     return 0;
159 }
160
161
162 /***********************************************************************
163  *           CreateSystemTimer   (SYSTEM.2)
164  */
165 WORD WINAPI CreateSystemTimer16( WORD rate, FARPROC16 proc )
166 {
167     int i;
168     for (i = 0; i < NB_SYS_TIMERS; i++)
169         if (!SYS_Timers[i].callback16)  /* Found one */
170         {
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 */
178         }
179     return 0;
180 }
181
182
183 /***********************************************************************
184  *           KillSystemTimer   (SYSTEM.3)
185  *
186  * Note: do not confuse this function with USER.182
187  */
188 WORD WINAPI SYSTEM_KillSystemTimer( WORD timer )
189 {
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;
195 }
196
197
198 /***********************************************************************
199  *           EnableSystemTimers   (SYSTEM.4)
200  */
201 void WINAPI EnableSystemTimers16(void)
202 {
203     SYS_timers_disabled = 0;
204 }
205
206
207 /***********************************************************************
208  *           DisableSystemTimers   (SYSTEM.5)
209  */
210 void WINAPI DisableSystemTimers16(void)
211 {
212     SYS_timers_disabled = 1;
213 }
214
215
216 /***********************************************************************
217  *           GetSystemMSecCount (SYSTEM.6)
218  */
219 DWORD WINAPI GetSystemMSecCount16(void)
220 {
221     return GetTickCount();
222 }
223
224
225 /***********************************************************************
226  *           Get80x87SaveSize   (SYSTEM.7)
227  */
228 WORD WINAPI Get80x87SaveSize16(void)
229 {
230     return 94;
231 }
232
233
234 /***********************************************************************
235  *           Save80x87State   (SYSTEM.8)
236  */
237 void WINAPI Save80x87State16( char *ptr )
238 {
239 #ifdef __i386__
240     __asm__(".byte 0x66; fsave %0; fwait" : "=m" (ptr) );
241 #endif
242 }
243
244
245 /***********************************************************************
246  *           Restore80x87State   (SYSTEM.9)
247  */
248 void WINAPI Restore80x87State16( const char *ptr )
249 {
250 #ifdef __i386__
251     __asm__(".byte 0x66; frstor %0" : : "m" (ptr) );
252 #endif
253 }
254
255
256 /***********************************************************************
257  *           A20_Proc  (SYSTEM.20)
258  */
259 void WINAPI A20_Proc16( WORD unused )
260 {
261     /* this is also a NOP in Windows */
262 }