Extended WOWCallback16Ex to support register functions too. This
[wine/multimedia.git] / dlls / kernel / system.c
blob55f017d4dc3458b6fd3dc821e27d5901c926b84b
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 "windef.h"
24 #include "wingdi.h"
25 #include "wine/winbase16.h"
26 #include "wine/winuser16.h"
27 #include "wownt32.h"
28 #include "stackframe.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(system);
33 typedef struct
35 SYSTEMTIMERPROC callback; /* NULL if not in use */
36 FARPROC16 callback16;
37 INT rate;
38 INT ticks;
39 } SYSTEM_TIMER;
41 #define NB_SYS_TIMERS 8
42 #define SYS_TIMER_RATE 54925
44 static SYSTEM_TIMER SYS_Timers[NB_SYS_TIMERS];
45 static int SYS_NbTimers = 0;
46 static HANDLE SYS_timer;
47 static HANDLE SYS_thread;
48 static int SYS_timers_disabled;
50 /***********************************************************************
51 * SYSTEM_TimerTick
53 static void CALLBACK SYSTEM_TimerTick( LPVOID arg, DWORD low, DWORD high )
55 int i;
57 if (SYS_timers_disabled) return;
58 for (i = 0; i < NB_SYS_TIMERS; i++)
60 if (!SYS_Timers[i].callback) continue;
61 if ((SYS_Timers[i].ticks -= SYS_TIMER_RATE) <= 0)
63 SYS_Timers[i].ticks += SYS_Timers[i].rate;
64 SYS_Timers[i].callback( i+1 );
70 /***********************************************************************
71 * SYSTEM_TimerThread
73 static DWORD CALLBACK SYSTEM_TimerThread( void *dummy )
75 LARGE_INTEGER when;
77 if (!(SYS_timer = CreateWaitableTimerA( NULL, FALSE, NULL ))) return 0;
79 when.s.LowPart = when.s.HighPart = 0;
80 SetWaitableTimer( SYS_timer, &when, (SYS_TIMER_RATE+500)/1000, SYSTEM_TimerTick, 0, FALSE );
81 for (;;) SleepEx( INFINITE, TRUE );
85 /**********************************************************************
86 * SYSTEM_StartTicks
88 * Start the system tick timer.
90 static void SYSTEM_StartTicks(void)
92 if (!SYS_thread) SYS_thread = CreateThread( NULL, 0, SYSTEM_TimerThread, NULL, 0, NULL );
96 /**********************************************************************
97 * SYSTEM_StopTicks
99 * Stop the system tick timer.
101 static void SYSTEM_StopTicks(void)
103 if (SYS_thread)
105 CancelWaitableTimer( SYS_timer );
106 TerminateThread( SYS_thread, 0 );
107 CloseHandle( SYS_thread );
108 CloseHandle( SYS_timer );
109 SYS_thread = 0;
114 /***********************************************************************
115 * InquireSystem (SYSTEM.1)
117 * Note: the function always takes 2 WORD arguments, contrary to what
118 * "Undocumented Windows" says.
120 DWORD WINAPI InquireSystem16( WORD code, WORD arg )
122 WORD drivetype;
124 switch(code)
126 case 0: /* Get timer resolution */
127 return SYS_TIMER_RATE;
129 case 1: /* Get drive type */
130 drivetype = GetDriveType16( arg );
131 return MAKELONG( drivetype, drivetype );
133 case 2: /* Enable one-drive logic */
134 FIXME("Case %d: set single-drive %d not supported\n", code, arg );
135 return 0;
137 WARN("Unknown code %d\n", code );
138 return 0;
142 /***********************************************************************
143 * CreateSystemTimer (SYSTEM.2)
145 WORD WINAPI CreateSystemTimer( WORD rate, SYSTEMTIMERPROC callback )
147 int i;
148 for (i = 0; i < NB_SYS_TIMERS; i++)
149 if (!SYS_Timers[i].callback) /* Found one */
151 SYS_Timers[i].rate = (UINT)rate * 1000;
152 if (SYS_Timers[i].rate < SYS_TIMER_RATE)
153 SYS_Timers[i].rate = SYS_TIMER_RATE;
154 SYS_Timers[i].ticks = SYS_Timers[i].rate;
155 SYS_Timers[i].callback = callback;
156 if (++SYS_NbTimers == 1) SYSTEM_StartTicks();
157 return i + 1; /* 0 means error */
159 return 0;
162 /**********************************************************************/
164 static void call_timer_proc16( WORD timer )
166 CONTEXT86 context;
167 FARPROC16 proc = SYS_Timers[timer-1].callback16;
169 memset( &context, '\0', sizeof(context) );
171 context.SegCs = SELECTOROF( proc );
172 context.Eip = OFFSETOF( proc );
173 context.Ebp = OFFSETOF( NtCurrentTeb()->cur_stack )
174 + (WORD)&((STACK16FRAME*)0)->bp;
175 context.Eax = timer;
177 WOWCallback16Ex( 0, WCB16_REGS, 0, NULL, (DWORD *)&context );
180 /**********************************************************************/
182 WORD WINAPI WIN16_CreateSystemTimer( WORD rate, FARPROC16 proc )
184 WORD ret = CreateSystemTimer( rate, call_timer_proc16 );
185 if (ret) SYS_Timers[ret - 1].callback16 = proc;
186 return ret;
190 /***********************************************************************
191 * KillSystemTimer (SYSTEM.3)
193 * Note: do not confuse this function with USER.182
195 WORD WINAPI SYSTEM_KillSystemTimer( WORD timer )
197 if ( !timer || timer > NB_SYS_TIMERS || !SYS_Timers[timer-1].callback )
198 return timer; /* Error */
199 SYS_Timers[timer-1].callback = NULL;
200 if (!--SYS_NbTimers) SYSTEM_StopTicks();
201 return 0;
205 /***********************************************************************
206 * EnableSystemTimers (SYSTEM.4)
208 void WINAPI EnableSystemTimers16(void)
210 SYS_timers_disabled = 0;
214 /***********************************************************************
215 * DisableSystemTimers (SYSTEM.5)
217 void WINAPI DisableSystemTimers16(void)
219 SYS_timers_disabled = 1;
223 /***********************************************************************
224 * Get80x87SaveSize (SYSTEM.7)
226 WORD WINAPI Get80x87SaveSize16(void)
228 return 94;
232 /***********************************************************************
233 * Save80x87State (SYSTEM.8)
235 void WINAPI Save80x87State16( char *ptr )
237 #ifdef __i386__
238 __asm__(".byte 0x66; fsave %0; fwait" : "=m" (ptr) );
239 #endif
243 /***********************************************************************
244 * Restore80x87State (SYSTEM.9)
246 void WINAPI Restore80x87State16( const char *ptr )
248 #ifdef __i386__
249 __asm__(".byte 0x66; frstor %0" : : "m" (ptr) );
250 #endif