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