Fixed HANDLE<->HFILE conversions, since they will be different types
[wine/multimedia.git] / misc / system.c
bloba5d2d2a1456a01867f511ef1f1efe0d43cb9a712
1 /*
2 * SYSTEM DLL routines
4 * Copyright 1996 Alexandre Julliard
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.
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.
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
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"
28 WINE_DEFAULT_DEBUG_CHANNEL(system);
30 typedef struct
32 SYSTEMTIMERPROC callback; /* NULL if not in use */
33 FARPROC16 callback16;
34 INT rate;
35 INT ticks;
36 } SYSTEM_TIMER;
38 #define NB_SYS_TIMERS 8
39 #define SYS_TIMER_RATE 54925
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;
47 /***********************************************************************
48 * SYSTEM_TimerTick
50 static void CALLBACK SYSTEM_TimerTick( LPVOID arg, DWORD low, DWORD high )
52 int i;
54 if (SYS_timers_disabled) return;
55 for (i = 0; i < NB_SYS_TIMERS; i++)
57 if (!SYS_Timers[i].callback) continue;
58 if ((SYS_Timers[i].ticks -= SYS_TIMER_RATE) <= 0)
60 SYS_Timers[i].ticks += SYS_Timers[i].rate;
61 SYS_Timers[i].callback( i+1 );
67 /***********************************************************************
68 * SYSTEM_TimerThread
70 static DWORD CALLBACK SYSTEM_TimerThread( void *dummy )
72 LARGE_INTEGER when;
74 if (!(SYS_timer = CreateWaitableTimerA( NULL, FALSE, NULL ))) return 0;
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 );
82 /**********************************************************************
83 * SYSTEM_StartTicks
85 * Start the system tick timer.
87 static void SYSTEM_StartTicks(void)
89 if (!SYS_thread) SYS_thread = CreateThread( NULL, 0, SYSTEM_TimerThread, NULL, 0, NULL );
93 /**********************************************************************
94 * SYSTEM_StopTicks
96 * Stop the system tick timer.
98 static void SYSTEM_StopTicks(void)
100 if (SYS_thread)
102 CancelWaitableTimer( SYS_timer );
103 TerminateThread( SYS_thread, 0 );
104 CloseHandle( SYS_thread );
105 CloseHandle( SYS_timer );
106 SYS_thread = 0;
111 /***********************************************************************
112 * InquireSystem (SYSTEM.1)
114 * Note: the function always takes 2 WORD arguments, contrary to what
115 * "Undocumented Windows" says.
117 DWORD WINAPI InquireSystem16( WORD code, WORD arg )
119 WORD drivetype;
121 switch(code)
123 case 0: /* Get timer resolution */
124 return SYS_TIMER_RATE;
126 case 1: /* Get drive type */
127 drivetype = GetDriveType16( arg );
128 return MAKELONG( drivetype, drivetype );
130 case 2: /* Enable one-drive logic */
131 FIXME("Case %d: set single-drive %d not supported\n", code, arg );
132 return 0;
134 WARN("Unknown code %d\n", code );
135 return 0;
139 /***********************************************************************
140 * CreateSystemTimer (SYSTEM.2)
142 WORD WINAPI CreateSystemTimer( WORD rate, SYSTEMTIMERPROC callback )
144 int i;
145 for (i = 0; i < NB_SYS_TIMERS; i++)
146 if (!SYS_Timers[i].callback) /* Found one */
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 */
156 return 0;
159 /**********************************************************************/
161 static void call_timer_proc16( WORD timer )
163 CONTEXT86 context;
164 FARPROC16 proc = SYS_Timers[timer-1].callback16;
166 memset( &context, '\0', sizeof(context) );
168 context.SegCs = SELECTOROF( proc );
169 context.Eip = OFFSETOF( proc );
170 context.Ebp = OFFSETOF( NtCurrentTeb()->cur_stack )
171 + (WORD)&((STACK16FRAME*)0)->bp;
173 AX_reg( &context ) = timer;
175 wine_call_to_16_regs_short( &context, 0 );
178 /**********************************************************************/
180 WORD WINAPI WIN16_CreateSystemTimer( WORD rate, FARPROC16 proc )
182 WORD ret = CreateSystemTimer( rate, call_timer_proc16 );
183 if (ret) SYS_Timers[ret - 1].callback16 = proc;
184 return ret;
188 /***********************************************************************
189 * KillSystemTimer (SYSTEM.3)
191 * Note: do not confuse this function with USER.182
193 WORD WINAPI SYSTEM_KillSystemTimer( WORD timer )
195 if ( !timer || timer > NB_SYS_TIMERS || !SYS_Timers[timer-1].callback )
196 return timer; /* Error */
197 SYS_Timers[timer-1].callback = NULL;
198 if (!--SYS_NbTimers) SYSTEM_StopTicks();
199 return 0;
203 /***********************************************************************
204 * EnableSystemTimers (SYSTEM.4)
206 void WINAPI EnableSystemTimers16(void)
208 SYS_timers_disabled = 0;
212 /***********************************************************************
213 * DisableSystemTimers (SYSTEM.5)
215 void WINAPI DisableSystemTimers16(void)
217 SYS_timers_disabled = 1;
221 /***********************************************************************
222 * Get80x87SaveSize (SYSTEM.7)
224 WORD WINAPI Get80x87SaveSize16(void)
226 return 94;
230 /***********************************************************************
231 * Save80x87State (SYSTEM.8)
233 void WINAPI Save80x87State16( char *ptr )
235 #ifdef __i386__
236 __asm__(".byte 0x66; fsave %0; fwait" : "=m" (ptr) );
237 #endif
241 /***********************************************************************
242 * Restore80x87State (SYSTEM.9)
244 void WINAPI Restore80x87State16( const char *ptr )
246 #ifdef __i386__
247 __asm__(".byte 0x66; frstor %0" : : "m" (ptr) );
248 #endif