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
23 #define NONAMELESSUNION
27 #include "wine/winbase16.h"
28 #include "wine/winuser16.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(system
);
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 /***********************************************************************
55 static void CALLBACK
SYSTEM_TimerTick( LPVOID arg
, DWORD low
, DWORD high
)
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
;
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
);
76 WOWCallback16Ex( 0, WCB16_REGS
, 0, NULL
, (DWORD
*)&context
);
82 /***********************************************************************
85 static DWORD CALLBACK
SYSTEM_TimerThread( void *dummy
)
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 /**********************************************************************
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 /**********************************************************************
111 * Stop the system tick timer.
113 static void SYSTEM_StopTicks(void)
117 CancelWaitableTimer( SYS_timer
);
118 TerminateThread( SYS_thread
, 0 );
119 CloseHandle( SYS_thread
);
120 CloseHandle( SYS_timer
);
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
)
139 case 0: /* Get timer resolution */
140 return SYS_TIMER_RATE
;
142 case 1: /* Get drive type */
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
);
155 WARN("Unknown code %d\n", code
);
160 /***********************************************************************
161 * CreateSystemTimer (SYSTEM.2)
163 WORD WINAPI
CreateSystemTimer16( WORD rate
, FARPROC16 proc
)
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 */
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();
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)
232 /***********************************************************************
233 * Save80x87State (SYSTEM.8)
235 void WINAPI
Save80x87State16( char *ptr
)
238 __asm__(".byte 0x66; fsave %0; fwait" : "=m" (ptr
) );
243 /***********************************************************************
244 * Restore80x87State (SYSTEM.9)
246 void WINAPI
Restore80x87State16( const char *ptr
)
249 __asm__(".byte 0x66; frstor %0" : : "m" (ptr
) );
254 /***********************************************************************
255 * A20_Proc (SYSTEM.20)
257 void WINAPI
A20_Proc16( WORD unused
)
259 /* this is also a NOP in Windows */