Added implementation for WSARecvEx function. This is a Microsoft
[wine] / misc / system.c
1 /*
2  * SYSTEM DLL routines
3  *
4  * Copyright 1996 Alexandre Julliard
5  */
6
7 #include "wine/winbase16.h"
8 #include "wine/winuser16.h"
9 #include "services.h"
10 #include "callback.h"
11 #include "stackframe.h"
12 #include "builtin16.h"
13 #include "debugtools.h"
14
15 DEFAULT_DEBUG_CHANNEL(system)
16
17 typedef struct
18 {
19     SYSTEMTIMERPROC callback;  /* NULL if not in use */
20     INT     rate;
21     INT     ticks;
22 } SYSTEM_TIMER;
23
24 #define NB_SYS_TIMERS   8
25 #define SYS_TIMER_RATE  54925
26
27 static SYSTEM_TIMER SYS_Timers[NB_SYS_TIMERS];
28 static int SYS_NbTimers = 0;
29 static HANDLE SYS_Service = INVALID_HANDLE_VALUE;
30
31
32 /***********************************************************************
33  *           SYSTEM_TimerTick
34  */
35 static void CALLBACK SYSTEM_TimerTick( ULONG_PTR arg )
36 {
37     int i;
38
39     for (i = 0; i < NB_SYS_TIMERS; i++)
40     {
41         if (!SYS_Timers[i].callback) continue;
42         if ((SYS_Timers[i].ticks -= SYS_TIMER_RATE) <= 0)
43         {
44             SYS_Timers[i].ticks += SYS_Timers[i].rate;
45             SYS_Timers[i].callback( i+1 );
46         }
47     }
48 }
49
50 /**********************************************************************
51  *           SYSTEM_StartTicks
52  *
53  * Start the system tick timer.
54  */
55 static void SYSTEM_StartTicks(void)
56 {
57     if ( SYS_Service == INVALID_HANDLE_VALUE )
58         SYS_Service = SERVICE_AddTimer( SYS_TIMER_RATE, SYSTEM_TimerTick, 0L );
59 }
60
61
62 /**********************************************************************
63  *           SYSTEM_StopTicks
64  *
65  * Stop the system tick timer.
66  */
67 static void SYSTEM_StopTicks(void)
68 {
69     if ( SYS_Service != INVALID_HANDLE_VALUE )
70     {
71         SERVICE_Delete( SYS_Service );
72         SYS_Service = INVALID_HANDLE_VALUE;
73     }
74 }
75
76
77 /***********************************************************************
78  *           InquireSystem   (SYSTEM.1)
79  *
80  * Note: the function always takes 2 WORD arguments, contrary to what
81  *       "Undocumented Windows" says.
82   */
83 DWORD WINAPI InquireSystem16( WORD code, WORD arg )
84 {
85     WORD drivetype;
86
87     switch(code)
88     {
89     case 0:  /* Get timer resolution */
90         return SYS_TIMER_RATE;
91
92     case 1:  /* Get drive type */
93         drivetype = GetDriveType16( arg );
94         return MAKELONG( drivetype, drivetype );
95
96     case 2:  /* Enable one-drive logic */
97         FIXME("Case %d: set single-drive %d not supported\n", code, arg );
98         return 0;
99     }
100     WARN("Unknown code %d\n", code );
101     return 0;
102 }
103
104
105 /***********************************************************************
106  *           CreateSystemTimer   (SYSTEM.2)
107  */
108 WORD WINAPI CreateSystemTimer( WORD rate, SYSTEMTIMERPROC callback )
109 {
110     int i;
111     for (i = 0; i < NB_SYS_TIMERS; i++)
112         if (!SYS_Timers[i].callback)  /* Found one */
113         {
114             SYS_Timers[i].rate = (UINT)rate * 1000;
115             if (SYS_Timers[i].rate < SYS_TIMER_RATE)
116                 SYS_Timers[i].rate = SYS_TIMER_RATE;
117             SYS_Timers[i].ticks = SYS_Timers[i].rate;
118             SYS_Timers[i].callback = callback;
119             if (++SYS_NbTimers == 1) SYSTEM_StartTicks();
120             return i + 1;  /* 0 means error */
121         }
122     return 0;
123 }
124
125 static void SYSTEM_CallSystemTimerProc( FARPROC16 proc, WORD timer )
126 {
127     CONTEXT86 context;
128     memset( &context, '\0', sizeof(context) );
129
130     CS_reg( &context )  = SELECTOROF( proc );
131     EIP_reg( &context ) = OFFSETOF( proc );
132     EBP_reg( &context ) = OFFSETOF( NtCurrentTeb()->cur_stack )
133                           + (WORD)&((STACK16FRAME*)0)->bp;
134
135     AX_reg( &context ) = timer;
136
137     CallTo16RegisterShort( &context, 0 );
138 }
139
140 WORD WINAPI WIN16_CreateSystemTimer( WORD rate, FARPROC16 proc )
141 {
142     FARPROC thunk = THUNK_Alloc( proc, (RELAY)SYSTEM_CallSystemTimerProc );
143     WORD timer = CreateSystemTimer( rate, (SYSTEMTIMERPROC)thunk );
144     if (!timer) THUNK_Free( thunk );
145     return timer;
146 }
147
148
149 /***********************************************************************
150  *           KillSystemTimer   (SYSTEM.3)
151  *
152  * Note: do not confuse this function with USER.182
153  */
154 WORD WINAPI SYSTEM_KillSystemTimer( WORD timer )
155 {
156     if ( !timer || timer > NB_SYS_TIMERS || !SYS_Timers[timer-1].callback ) 
157         return timer;  /* Error */
158
159     THUNK_Free( (FARPROC)SYS_Timers[timer-1].callback );
160     SYS_Timers[timer-1].callback = NULL;
161
162     if (!--SYS_NbTimers) SYSTEM_StopTicks();
163     return 0;
164 }
165
166
167 /***********************************************************************
168  *           EnableSystemTimers   (SYSTEM.4)
169  */
170 void WINAPI EnableSystemTimers16(void)
171 {
172     if ( SYS_Service != INVALID_HANDLE_VALUE )
173         SERVICE_Enable( SYS_Service );
174 }
175
176
177 /***********************************************************************
178  *           DisableSystemTimers   (SYSTEM.5)
179  */
180 void WINAPI DisableSystemTimers16(void)
181 {
182     if ( SYS_Service != INVALID_HANDLE_VALUE )
183         SERVICE_Disable( SYS_Service );
184 }
185
186
187 /***********************************************************************
188  *           Get80x87SaveSize16   (SYSTEM.7)
189  */
190 WORD WINAPI Get80x87SaveSize16(void)
191 {
192     return 94;
193 }
194
195
196 /***********************************************************************
197  *           Save80x87State16   (SYSTEM.8)
198  */
199 void WINAPI Save80x87State16( char *ptr )
200 {
201 #ifdef __i386__
202     __asm__(".byte 0x66; fsave %0; fwait" : "=m" (ptr) );
203 #endif
204 }
205
206
207 /***********************************************************************
208  *           Restore80x87State16   (SYSTEM.9)
209  */
210 void WINAPI Restore80x87State16( const char *ptr )
211 {
212 #ifdef __i386__
213     __asm__(".byte 0x66; frstor %0" : : "m" (ptr) );
214 #endif
215 }
216