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