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