Add support for specific EOI PIC command.
[wine] / dlls / kernel / 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #define NONAMELESSUNION
22 #define NONAMELESSSTRUCT
23 #include "windef.h"
24 #include "wingdi.h"
25 #include "wine/winbase16.h"
26 #include "wine/winuser16.h"
27 #include "wownt32.h"
28 #include "stackframe.h"
29 #include "wine/debug.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(system);
32
33 typedef struct
34 {
35     SYSTEMTIMERPROC callback;  /* NULL if not in use */
36     FARPROC16       callback16;
37     INT             rate;
38     INT             ticks;
39 } SYSTEM_TIMER;
40
41 #define NB_SYS_TIMERS   8
42 #define SYS_TIMER_RATE  54925
43
44 static SYSTEM_TIMER SYS_Timers[NB_SYS_TIMERS];
45 static int SYS_NbTimers = 0;
46 static HANDLE SYS_timer;
47 static HANDLE SYS_thread;
48 static int SYS_timers_disabled;
49
50 /***********************************************************************
51  *           SYSTEM_TimerTick
52  */
53 static void CALLBACK SYSTEM_TimerTick( LPVOID arg, DWORD low, DWORD high )
54 {
55     int i;
56
57     if (SYS_timers_disabled) return;
58     for (i = 0; i < NB_SYS_TIMERS; i++)
59     {
60         if (!SYS_Timers[i].callback) continue;
61         if ((SYS_Timers[i].ticks -= SYS_TIMER_RATE) <= 0)
62         {
63             SYS_Timers[i].ticks += SYS_Timers[i].rate;
64             SYS_Timers[i].callback( i+1 );
65         }
66     }
67 }
68
69
70 /***********************************************************************
71  *           SYSTEM_TimerThread
72  */
73 static DWORD CALLBACK SYSTEM_TimerThread( void *dummy )
74 {
75     LARGE_INTEGER when;
76
77     if (!(SYS_timer = CreateWaitableTimerA( NULL, FALSE, NULL ))) return 0;
78
79     when.s.LowPart = when.s.HighPart = 0;
80     SetWaitableTimer( SYS_timer, &when, (SYS_TIMER_RATE+500)/1000, SYSTEM_TimerTick, 0, FALSE );
81     for (;;) SleepEx( INFINITE, TRUE );
82 }
83
84
85 /**********************************************************************
86  *           SYSTEM_StartTicks
87  *
88  * Start the system tick timer.
89  */
90 static void SYSTEM_StartTicks(void)
91 {
92     if (!SYS_thread) SYS_thread = CreateThread( NULL, 0, SYSTEM_TimerThread, NULL, 0, NULL );
93 }
94
95
96 /**********************************************************************
97  *           SYSTEM_StopTicks
98  *
99  * Stop the system tick timer.
100  */
101 static void SYSTEM_StopTicks(void)
102 {
103     if (SYS_thread)
104     {
105         CancelWaitableTimer( SYS_timer );
106         TerminateThread( SYS_thread, 0 );
107         CloseHandle( SYS_thread );
108         CloseHandle( SYS_timer );
109         SYS_thread = 0;
110     }
111 }
112
113
114 /***********************************************************************
115  *           InquireSystem   (SYSTEM.1)
116  *
117  * Note: the function always takes 2 WORD arguments, contrary to what
118  *       "Undocumented Windows" says.
119   */
120 DWORD WINAPI InquireSystem16( WORD code, WORD arg )
121 {
122     WORD drivetype;
123
124     switch(code)
125     {
126     case 0:  /* Get timer resolution */
127         return SYS_TIMER_RATE;
128
129     case 1:  /* Get drive type */
130         drivetype = GetDriveType16( arg );
131         return MAKELONG( drivetype, drivetype );
132
133     case 2:  /* Enable one-drive logic */
134         FIXME("Case %d: set single-drive %d not supported\n", code, arg );
135         return 0;
136     }
137     WARN("Unknown code %d\n", code );
138     return 0;
139 }
140
141
142 /***********************************************************************
143  *           CreateSystemTimer   (SYSTEM.2)
144  */
145 WORD WINAPI CreateSystemTimer( WORD rate, SYSTEMTIMERPROC callback )
146 {
147     int i;
148     for (i = 0; i < NB_SYS_TIMERS; i++)
149         if (!SYS_Timers[i].callback)  /* Found one */
150         {
151             SYS_Timers[i].rate = (UINT)rate * 1000;
152             if (SYS_Timers[i].rate < SYS_TIMER_RATE)
153                 SYS_Timers[i].rate = SYS_TIMER_RATE;
154             SYS_Timers[i].ticks = SYS_Timers[i].rate;
155             SYS_Timers[i].callback = callback;
156             if (++SYS_NbTimers == 1) SYSTEM_StartTicks();
157             return i + 1;  /* 0 means error */
158         }
159     return 0;
160 }
161
162 /**********************************************************************/
163
164 static void call_timer_proc16( WORD timer )
165 {
166     CONTEXT86 context;
167     FARPROC16 proc = SYS_Timers[timer-1].callback16;
168
169     memset( &context, '\0', sizeof(context) );
170
171     context.SegCs = SELECTOROF( proc );
172     context.Eip   = OFFSETOF( proc );
173     context.Ebp   = OFFSETOF( NtCurrentTeb()->cur_stack )
174                           + (WORD)&((STACK16FRAME*)0)->bp;
175     context.Eax   = timer;
176
177     WOWCallback16Ex( 0, WCB16_REGS, 0, NULL, (DWORD *)&context );
178 }
179
180 /**********************************************************************/
181
182 WORD WINAPI WIN16_CreateSystemTimer( WORD rate, FARPROC16 proc )
183 {
184     WORD ret = CreateSystemTimer( rate, call_timer_proc16 );
185     if (ret) SYS_Timers[ret - 1].callback16 = proc;
186     return ret;
187 }
188
189
190 /***********************************************************************
191  *           KillSystemTimer   (SYSTEM.3)
192  *
193  * Note: do not confuse this function with USER.182
194  */
195 WORD WINAPI SYSTEM_KillSystemTimer( WORD timer )
196 {
197     if ( !timer || timer > NB_SYS_TIMERS || !SYS_Timers[timer-1].callback )
198         return timer;  /* Error */
199     SYS_Timers[timer-1].callback = NULL;
200     if (!--SYS_NbTimers) SYSTEM_StopTicks();
201     return 0;
202 }
203
204
205 /***********************************************************************
206  *           EnableSystemTimers   (SYSTEM.4)
207  */
208 void WINAPI EnableSystemTimers16(void)
209 {
210     SYS_timers_disabled = 0;
211 }
212
213
214 /***********************************************************************
215  *           DisableSystemTimers   (SYSTEM.5)
216  */
217 void WINAPI DisableSystemTimers16(void)
218 {
219     SYS_timers_disabled = 1;
220 }
221
222
223 /***********************************************************************
224  *           Get80x87SaveSize   (SYSTEM.7)
225  */
226 WORD WINAPI Get80x87SaveSize16(void)
227 {
228     return 94;
229 }
230
231
232 /***********************************************************************
233  *           Save80x87State   (SYSTEM.8)
234  */
235 void WINAPI Save80x87State16( char *ptr )
236 {
237 #ifdef __i386__
238     __asm__(".byte 0x66; fsave %0; fwait" : "=m" (ptr) );
239 #endif
240 }
241
242
243 /***********************************************************************
244  *           Restore80x87State   (SYSTEM.9)
245  */
246 void WINAPI Restore80x87State16( const char *ptr )
247 {
248 #ifdef __i386__
249     __asm__(".byte 0x66; frstor %0" : : "m" (ptr) );
250 #endif
251 }