mfreadwrite/reader: Add missing allocation check (Coverity).
[wine/zf.git] / dlls / system.drv16 / system.c
blob78cc0b03c7bd8c44ed31dc314ab38f282043a6cb
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
23 #define NONAMELESSUNION
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"
33 WINE_DEFAULT_DEBUG_CHANNEL(system);
35 typedef struct
37 FARPROC16 callback16;
38 INT rate;
39 INT ticks;
40 } SYSTEM_TIMER;
42 #define NB_SYS_TIMERS 8
43 #define SYS_TIMER_RATE 54925
45 static SYSTEM_TIMER SYS_Timers[NB_SYS_TIMERS];
46 static int SYS_NbTimers = 0;
47 static HANDLE SYS_timer;
48 static HANDLE SYS_thread;
49 static BOOL SYS_timers_disabled;
52 /***********************************************************************
53 * SYSTEM_TimerTick
55 static void CALLBACK SYSTEM_TimerTick( LPVOID arg, DWORD low, DWORD high )
57 int i;
59 if (SYS_timers_disabled) return;
60 for (i = 0; i < NB_SYS_TIMERS; i++)
62 if (!SYS_Timers[i].callback16) continue;
63 if ((SYS_Timers[i].ticks -= SYS_TIMER_RATE) <= 0)
65 FARPROC16 proc = SYS_Timers[i].callback16;
66 CONTEXT context;
68 SYS_Timers[i].ticks += SYS_Timers[i].rate;
70 memset( &context, 0, sizeof(context) );
71 context.SegCs = SELECTOROF( proc );
72 context.Eip = OFFSETOF( proc );
73 context.Ebp = CURRENT_SP + FIELD_OFFSET(STACK16FRAME, bp);
74 context.Eax = i + 1;
76 WOWCallback16Ex( 0, WCB16_REGS, 0, NULL, (DWORD *)&context );
82 /***********************************************************************
83 * SYSTEM_TimerThread
85 static DWORD CALLBACK SYSTEM_TimerThread( void *dummy )
87 LARGE_INTEGER when;
89 if (!(SYS_timer = CreateWaitableTimerA( NULL, FALSE, NULL ))) return 0;
91 when.u.LowPart = when.u.HighPart = 0;
92 SetWaitableTimer( SYS_timer, &when, (SYS_TIMER_RATE+500)/1000, SYSTEM_TimerTick, 0, FALSE );
93 for (;;) SleepEx( INFINITE, TRUE );
97 /**********************************************************************
98 * SYSTEM_StartTicks
100 * Start the system tick timer.
102 static void SYSTEM_StartTicks(void)
104 if (!SYS_thread) SYS_thread = CreateThread( NULL, 0, SYSTEM_TimerThread, NULL, 0, NULL );
108 /**********************************************************************
109 * SYSTEM_StopTicks
111 * Stop the system tick timer.
113 static void SYSTEM_StopTicks(void)
115 if (SYS_thread)
117 CancelWaitableTimer( SYS_timer );
118 TerminateThread( SYS_thread, 0 );
119 CloseHandle( SYS_thread );
120 CloseHandle( SYS_timer );
121 SYS_thread = 0;
126 /***********************************************************************
127 * InquireSystem (SYSTEM.1)
129 * Note: the function always takes 2 WORD arguments, contrary to what
130 * "Undocumented Windows" says.
132 DWORD WINAPI InquireSystem16( WORD code, WORD arg )
134 WORD drivetype;
135 WCHAR root[3];
137 switch(code)
139 case 0: /* Get timer resolution */
140 return SYS_TIMER_RATE;
142 case 1: /* Get drive type */
143 root[0] = 'A' + arg;
144 root[1] = ':';
145 root[2] = 0;
146 drivetype = GetDriveTypeW( root );
147 if (drivetype == DRIVE_CDROM) drivetype = DRIVE_REMOTE;
148 else if (drivetype == DRIVE_NO_ROOT_DIR) drivetype = DRIVE_UNKNOWN;
149 return MAKELONG( drivetype, drivetype );
151 case 2: /* Enable one-drive logic */
152 FIXME("Case %d: set single-drive %d not supported\n", code, arg );
153 return 0;
155 WARN("Unknown code %d\n", code );
156 return 0;
160 /***********************************************************************
161 * CreateSystemTimer (SYSTEM.2)
163 WORD WINAPI CreateSystemTimer16( WORD rate, FARPROC16 proc )
165 int i;
166 for (i = 0; i < NB_SYS_TIMERS; i++)
167 if (!SYS_Timers[i].callback16) /* Found one */
169 SYS_Timers[i].rate = (UINT)rate * 1000;
170 if (SYS_Timers[i].rate < SYS_TIMER_RATE)
171 SYS_Timers[i].rate = SYS_TIMER_RATE;
172 SYS_Timers[i].ticks = SYS_Timers[i].rate;
173 SYS_Timers[i].callback16 = proc;
174 if (++SYS_NbTimers == 1) SYSTEM_StartTicks();
175 return i + 1; /* 0 means error */
177 return 0;
181 /***********************************************************************
182 * KillSystemTimer (SYSTEM.3)
184 * Note: do not confuse this function with USER.182
186 WORD WINAPI SYSTEM_KillSystemTimer( WORD timer )
188 if ( !timer || timer > NB_SYS_TIMERS || !SYS_Timers[timer-1].callback16 )
189 return timer; /* Error */
190 SYS_Timers[timer-1].callback16 = 0;
191 if (!--SYS_NbTimers) SYSTEM_StopTicks();
192 return 0;
196 /***********************************************************************
197 * EnableSystemTimers (SYSTEM.4)
199 void WINAPI EnableSystemTimers16(void)
201 SYS_timers_disabled = FALSE;
205 /***********************************************************************
206 * DisableSystemTimers (SYSTEM.5)
208 void WINAPI DisableSystemTimers16(void)
210 SYS_timers_disabled = TRUE;
214 /***********************************************************************
215 * GetSystemMSecCount (SYSTEM.6)
217 DWORD WINAPI GetSystemMSecCount16(void)
219 return GetTickCount();
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
254 /***********************************************************************
255 * A20_Proc (SYSTEM.20)
257 void WINAPI A20_Proc16( WORD unused )
259 /* this is also a NOP in Windows */