Fixed winebuild operation in Darwin.
[wine/multimedia.git] / dlls / kernel / system.c
blobec1d25e070953fa896114b67dfb63a80ee14b527
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 #define NONAMELESSUNION
22 #define NONAMELESSSTRUCT
23 #include <stdarg.h>
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 "winternl.h"
32 #include "kernel_private.h"
33 #include "kernel16_private.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(system);
38 typedef struct
40 SYSTEMTIMERPROC callback; /* NULL if not in use */
41 FARPROC16 callback16;
42 INT rate;
43 INT ticks;
44 } SYSTEM_TIMER;
46 #define NB_SYS_TIMERS 8
47 #define SYS_TIMER_RATE 54925
49 static SYSTEM_TIMER SYS_Timers[NB_SYS_TIMERS];
50 static int SYS_NbTimers = 0;
51 static HANDLE SYS_timer;
52 static HANDLE SYS_thread;
53 static int SYS_timers_disabled;
55 /***********************************************************************
56 * SYSTEM_TimerTick
58 static void CALLBACK SYSTEM_TimerTick( LPVOID arg, DWORD low, DWORD high )
60 int i;
62 if (SYS_timers_disabled) return;
63 for (i = 0; i < NB_SYS_TIMERS; i++)
65 if (!SYS_Timers[i].callback) continue;
66 if ((SYS_Timers[i].ticks -= SYS_TIMER_RATE) <= 0)
68 SYS_Timers[i].ticks += SYS_Timers[i].rate;
69 SYS_Timers[i].callback( i+1 );
75 /***********************************************************************
76 * SYSTEM_TimerThread
78 static DWORD CALLBACK SYSTEM_TimerThread( void *dummy )
80 LARGE_INTEGER when;
82 if (!(SYS_timer = CreateWaitableTimerA( NULL, FALSE, NULL ))) return 0;
84 when.u.LowPart = when.u.HighPart = 0;
85 SetWaitableTimer( SYS_timer, &when, (SYS_TIMER_RATE+500)/1000, SYSTEM_TimerTick, 0, FALSE );
86 for (;;) SleepEx( INFINITE, TRUE );
90 /**********************************************************************
91 * SYSTEM_StartTicks
93 * Start the system tick timer.
95 static void SYSTEM_StartTicks(void)
97 if (!SYS_thread) SYS_thread = CreateThread( NULL, 0, SYSTEM_TimerThread, NULL, 0, NULL );
101 /**********************************************************************
102 * SYSTEM_StopTicks
104 * Stop the system tick timer.
106 static void SYSTEM_StopTicks(void)
108 if (SYS_thread)
110 CancelWaitableTimer( SYS_timer );
111 TerminateThread( SYS_thread, 0 );
112 CloseHandle( SYS_thread );
113 CloseHandle( SYS_timer );
114 SYS_thread = 0;
119 /***********************************************************************
120 * InquireSystem (SYSTEM.1)
122 * Note: the function always takes 2 WORD arguments, contrary to what
123 * "Undocumented Windows" says.
125 DWORD WINAPI InquireSystem16( WORD code, WORD arg )
127 WORD drivetype;
129 switch(code)
131 case 0: /* Get timer resolution */
132 return SYS_TIMER_RATE;
134 case 1: /* Get drive type */
135 drivetype = GetDriveType16( arg );
136 return MAKELONG( drivetype, drivetype );
138 case 2: /* Enable one-drive logic */
139 FIXME("Case %d: set single-drive %d not supported\n", code, arg );
140 return 0;
142 WARN("Unknown code %d\n", code );
143 return 0;
147 /***********************************************************************
148 * CreateSystemTimer (SYSTEM.2)
150 WORD WINAPI CreateSystemTimer( WORD rate, SYSTEMTIMERPROC callback )
152 int i;
153 for (i = 0; i < NB_SYS_TIMERS; i++)
154 if (!SYS_Timers[i].callback) /* Found one */
156 SYS_Timers[i].rate = (UINT)rate * 1000;
157 if (SYS_Timers[i].rate < SYS_TIMER_RATE)
158 SYS_Timers[i].rate = SYS_TIMER_RATE;
159 SYS_Timers[i].ticks = SYS_Timers[i].rate;
160 SYS_Timers[i].callback = callback;
161 if (++SYS_NbTimers == 1) SYSTEM_StartTicks();
162 return i + 1; /* 0 means error */
164 return 0;
167 /**********************************************************************/
169 static void call_timer_proc16( WORD timer )
171 CONTEXT86 context;
172 FARPROC16 proc = SYS_Timers[timer-1].callback16;
174 memset( &context, 0, sizeof(context) );
176 context.SegFs = wine_get_fs();
177 context.SegGs = wine_get_gs();
178 context.SegCs = SELECTOROF( proc );
179 context.Eip = OFFSETOF( proc );
180 context.Ebp = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + (WORD)&((STACK16FRAME*)0)->bp;
181 context.Eax = timer;
183 WOWCallback16Ex( 0, WCB16_REGS, 0, NULL, (DWORD *)&context );
186 /**********************************************************************/
188 WORD WINAPI WIN16_CreateSystemTimer( WORD rate, FARPROC16 proc )
190 WORD ret = CreateSystemTimer( rate, call_timer_proc16 );
191 if (ret) SYS_Timers[ret - 1].callback16 = proc;
192 return ret;
196 /***********************************************************************
197 * KillSystemTimer (SYSTEM.3)
199 * Note: do not confuse this function with USER.182
201 WORD WINAPI SYSTEM_KillSystemTimer( WORD timer )
203 if ( !timer || timer > NB_SYS_TIMERS || !SYS_Timers[timer-1].callback )
204 return timer; /* Error */
205 SYS_Timers[timer-1].callback = NULL;
206 if (!--SYS_NbTimers) SYSTEM_StopTicks();
207 return 0;
211 /***********************************************************************
212 * EnableSystemTimers (SYSTEM.4)
214 void WINAPI EnableSystemTimers16(void)
216 SYS_timers_disabled = 0;
220 /***********************************************************************
221 * DisableSystemTimers (SYSTEM.5)
223 void WINAPI DisableSystemTimers16(void)
225 SYS_timers_disabled = 1;
229 /***********************************************************************
230 * Get80x87SaveSize (SYSTEM.7)
232 WORD WINAPI Get80x87SaveSize16(void)
234 return 94;
238 /***********************************************************************
239 * Save80x87State (SYSTEM.8)
241 void WINAPI Save80x87State16( char *ptr )
243 #ifdef __i386__
244 __asm__(".byte 0x66; fsave %0; fwait" : "=m" (ptr) );
245 #endif
249 /***********************************************************************
250 * Restore80x87State (SYSTEM.9)
252 void WINAPI Restore80x87State16( const char *ptr )
254 #ifdef __i386__
255 __asm__(".byte 0x66; frstor %0" : : "m" (ptr) );
256 #endif