Added YUV routines needed for v4l driver, and in the future possibly
[wine/gsoc-2012-control.git] / dlls / kernel / system.c
blob82461c53e6c76a90e75d67cb9de4aba6dd55d624
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 "kernel_private.h"
32 #include "kernel16_private.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(system);
37 typedef struct
39 SYSTEMTIMERPROC callback; /* NULL if not in use */
40 FARPROC16 callback16;
41 INT rate;
42 INT ticks;
43 } SYSTEM_TIMER;
45 #define NB_SYS_TIMERS 8
46 #define SYS_TIMER_RATE 54925
48 static SYSTEM_TIMER SYS_Timers[NB_SYS_TIMERS];
49 static int SYS_NbTimers = 0;
50 static HANDLE SYS_timer;
51 static HANDLE SYS_thread;
52 static int SYS_timers_disabled;
54 /***********************************************************************
55 * SYSTEM_TimerTick
57 static void CALLBACK SYSTEM_TimerTick( LPVOID arg, DWORD low, DWORD high )
59 int i;
61 if (SYS_timers_disabled) return;
62 for (i = 0; i < NB_SYS_TIMERS; i++)
64 if (!SYS_Timers[i].callback) continue;
65 if ((SYS_Timers[i].ticks -= SYS_TIMER_RATE) <= 0)
67 SYS_Timers[i].ticks += SYS_Timers[i].rate;
68 SYS_Timers[i].callback( i+1 );
74 /***********************************************************************
75 * SYSTEM_TimerThread
77 static DWORD CALLBACK SYSTEM_TimerThread( void *dummy )
79 LARGE_INTEGER when;
81 if (!(SYS_timer = CreateWaitableTimerA( NULL, FALSE, NULL ))) return 0;
83 when.u.LowPart = when.u.HighPart = 0;
84 SetWaitableTimer( SYS_timer, &when, (SYS_TIMER_RATE+500)/1000, SYSTEM_TimerTick, 0, FALSE );
85 for (;;) SleepEx( INFINITE, TRUE );
89 /**********************************************************************
90 * SYSTEM_StartTicks
92 * Start the system tick timer.
94 static void SYSTEM_StartTicks(void)
96 if (!SYS_thread) SYS_thread = CreateThread( NULL, 0, SYSTEM_TimerThread, NULL, 0, NULL );
100 /**********************************************************************
101 * SYSTEM_StopTicks
103 * Stop the system tick timer.
105 static void SYSTEM_StopTicks(void)
107 if (SYS_thread)
109 CancelWaitableTimer( SYS_timer );
110 TerminateThread( SYS_thread, 0 );
111 CloseHandle( SYS_thread );
112 CloseHandle( SYS_timer );
113 SYS_thread = 0;
118 /***********************************************************************
119 * InquireSystem (SYSTEM.1)
121 * Note: the function always takes 2 WORD arguments, contrary to what
122 * "Undocumented Windows" says.
124 DWORD WINAPI InquireSystem16( WORD code, WORD arg )
126 WORD drivetype;
128 switch(code)
130 case 0: /* Get timer resolution */
131 return SYS_TIMER_RATE;
133 case 1: /* Get drive type */
134 drivetype = GetDriveType16( arg );
135 return MAKELONG( drivetype, drivetype );
137 case 2: /* Enable one-drive logic */
138 FIXME("Case %d: set single-drive %d not supported\n", code, arg );
139 return 0;
141 WARN("Unknown code %d\n", code );
142 return 0;
146 /***********************************************************************
147 * CreateSystemTimer (SYSTEM.2)
149 WORD WINAPI CreateSystemTimer( WORD rate, SYSTEMTIMERPROC callback )
151 int i;
152 for (i = 0; i < NB_SYS_TIMERS; i++)
153 if (!SYS_Timers[i].callback) /* Found one */
155 SYS_Timers[i].rate = (UINT)rate * 1000;
156 if (SYS_Timers[i].rate < SYS_TIMER_RATE)
157 SYS_Timers[i].rate = SYS_TIMER_RATE;
158 SYS_Timers[i].ticks = SYS_Timers[i].rate;
159 SYS_Timers[i].callback = callback;
160 if (++SYS_NbTimers == 1) SYSTEM_StartTicks();
161 return i + 1; /* 0 means error */
163 return 0;
166 /**********************************************************************/
168 static void call_timer_proc16( WORD timer )
170 CONTEXT86 context;
171 FARPROC16 proc = SYS_Timers[timer-1].callback16;
173 memset( &context, 0, sizeof(context) );
175 context.SegFs = wine_get_fs();
176 context.SegGs = wine_get_gs();
177 context.SegCs = SELECTOROF( proc );
178 context.Eip = OFFSETOF( proc );
179 context.Ebp = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + (WORD)&((STACK16FRAME*)0)->bp;
180 context.Eax = timer;
182 WOWCallback16Ex( 0, WCB16_REGS, 0, NULL, (DWORD *)&context );
185 /**********************************************************************/
187 WORD WINAPI WIN16_CreateSystemTimer( WORD rate, FARPROC16 proc )
189 WORD ret = CreateSystemTimer( rate, call_timer_proc16 );
190 if (ret) SYS_Timers[ret - 1].callback16 = proc;
191 return ret;
195 /***********************************************************************
196 * KillSystemTimer (SYSTEM.3)
198 * Note: do not confuse this function with USER.182
200 WORD WINAPI SYSTEM_KillSystemTimer( WORD timer )
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;
210 /***********************************************************************
211 * EnableSystemTimers (SYSTEM.4)
213 void WINAPI EnableSystemTimers16(void)
215 SYS_timers_disabled = 0;
219 /***********************************************************************
220 * DisableSystemTimers (SYSTEM.5)
222 void WINAPI DisableSystemTimers16(void)
224 SYS_timers_disabled = 1;
228 /***********************************************************************
229 * Get80x87SaveSize (SYSTEM.7)
231 WORD WINAPI Get80x87SaveSize16(void)
233 return 94;
237 /***********************************************************************
238 * Save80x87State (SYSTEM.8)
240 void WINAPI Save80x87State16( char *ptr )
242 #ifdef __i386__
243 __asm__(".byte 0x66; fsave %0; fwait" : "=m" (ptr) );
244 #endif
248 /***********************************************************************
249 * Restore80x87State (SYSTEM.9)
251 void WINAPI Restore80x87State16( const char *ptr )
253 #ifdef __i386__
254 __asm__(".byte 0x66; frstor %0" : : "m" (ptr) );
255 #endif