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 #define NONAMELESSUNION
22 #define NONAMELESSSTRUCT
27 #include "wine/winbase16.h"
28 #include "wine/winuser16.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(system
);
37 SYSTEMTIMERPROC callback
; /* NULL if not in use */
43 #define NB_SYS_TIMERS 8
44 #define SYS_TIMER_RATE 54925
46 static SYSTEM_TIMER SYS_Timers
[NB_SYS_TIMERS
];
47 static int SYS_NbTimers
= 0;
48 static HANDLE SYS_timer
;
49 static HANDLE SYS_thread
;
50 static int 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
].callback
) continue;
63 if ((SYS_Timers
[i
].ticks
-= SYS_TIMER_RATE
) <= 0)
65 SYS_Timers
[i
].ticks
+= SYS_Timers
[i
].rate
;
66 SYS_Timers
[i
].callback( i
+1 );
72 /***********************************************************************
75 static DWORD CALLBACK
SYSTEM_TimerThread( void *dummy
)
79 if (!(SYS_timer
= CreateWaitableTimerA( NULL
, FALSE
, NULL
))) return 0;
81 when
.u
.LowPart
= when
.u
.HighPart
= 0;
82 SetWaitableTimer( SYS_timer
, &when
, (SYS_TIMER_RATE
+500)/1000, SYSTEM_TimerTick
, 0, FALSE
);
83 for (;;) SleepEx( INFINITE
, TRUE
);
87 /**********************************************************************
90 * Start the system tick timer.
92 static void SYSTEM_StartTicks(void)
94 if (!SYS_thread
) SYS_thread
= CreateThread( NULL
, 0, SYSTEM_TimerThread
, NULL
, 0, NULL
);
98 /**********************************************************************
101 * Stop the system tick timer.
103 static void SYSTEM_StopTicks(void)
107 CancelWaitableTimer( SYS_timer
);
108 TerminateThread( SYS_thread
, 0 );
109 CloseHandle( SYS_thread
);
110 CloseHandle( SYS_timer
);
116 /***********************************************************************
117 * InquireSystem (SYSTEM.1)
119 * Note: the function always takes 2 WORD arguments, contrary to what
120 * "Undocumented Windows" says.
122 DWORD WINAPI
InquireSystem16( WORD code
, WORD arg
)
129 case 0: /* Get timer resolution */
130 return SYS_TIMER_RATE
;
132 case 1: /* Get drive type */
136 drivetype
= GetDriveTypeW( root
);
137 if (drivetype
== DRIVE_CDROM
) drivetype
= DRIVE_REMOTE
;
138 else if (drivetype
== DRIVE_NO_ROOT_DIR
) drivetype
= DRIVE_UNKNOWN
;
139 return MAKELONG( drivetype
, drivetype
);
141 case 2: /* Enable one-drive logic */
142 FIXME("Case %d: set single-drive %d not supported\n", code
, arg
);
145 WARN("Unknown code %d\n", code
);
150 /***********************************************************************
151 * CreateSystemTimer (SYSTEM.2)
153 WORD WINAPI
CreateSystemTimer( WORD rate
, SYSTEMTIMERPROC callback
)
156 for (i
= 0; i
< NB_SYS_TIMERS
; i
++)
157 if (!SYS_Timers
[i
].callback
) /* Found one */
159 SYS_Timers
[i
].rate
= (UINT
)rate
* 1000;
160 if (SYS_Timers
[i
].rate
< SYS_TIMER_RATE
)
161 SYS_Timers
[i
].rate
= SYS_TIMER_RATE
;
162 SYS_Timers
[i
].ticks
= SYS_Timers
[i
].rate
;
163 SYS_Timers
[i
].callback
= callback
;
164 if (++SYS_NbTimers
== 1) SYSTEM_StartTicks();
165 return i
+ 1; /* 0 means error */
170 /**********************************************************************/
172 static void call_timer_proc16( WORD timer
)
175 FARPROC16 proc
= SYS_Timers
[timer
-1].callback16
;
177 memset( &context
, 0, sizeof(context
) );
179 context
.SegFs
= wine_get_fs();
180 context
.SegGs
= wine_get_gs();
181 context
.SegCs
= SELECTOROF( proc
);
182 context
.Eip
= OFFSETOF( proc
);
183 context
.Ebp
= OFFSETOF(NtCurrentTeb()->WOW32Reserved
) + FIELD_OFFSET(STACK16FRAME
,bp
);
186 WOWCallback16Ex( 0, WCB16_REGS
, 0, NULL
, (DWORD
*)&context
);
189 /**********************************************************************/
191 WORD WINAPI
WIN16_CreateSystemTimer( WORD rate
, FARPROC16 proc
)
193 WORD ret
= CreateSystemTimer( rate
, call_timer_proc16
);
194 if (ret
) SYS_Timers
[ret
- 1].callback16
= proc
;
199 /***********************************************************************
200 * KillSystemTimer (SYSTEM.3)
202 * Note: do not confuse this function with USER.182
204 WORD WINAPI
SYSTEM_KillSystemTimer( WORD timer
)
206 if ( !timer
|| timer
> NB_SYS_TIMERS
|| !SYS_Timers
[timer
-1].callback
)
207 return timer
; /* Error */
208 SYS_Timers
[timer
-1].callback
= NULL
;
209 if (!--SYS_NbTimers
) SYSTEM_StopTicks();
214 /***********************************************************************
215 * EnableSystemTimers (SYSTEM.4)
217 void WINAPI
EnableSystemTimers16(void)
219 SYS_timers_disabled
= 0;
223 /***********************************************************************
224 * DisableSystemTimers (SYSTEM.5)
226 void WINAPI
DisableSystemTimers16(void)
228 SYS_timers_disabled
= 1;
232 /***********************************************************************
233 * GetSystemMSecCount (SYSTEM.6)
235 DWORD WINAPI
GetSystemMSecCount16(void)
237 return GetTickCount();
241 /***********************************************************************
242 * Get80x87SaveSize (SYSTEM.7)
244 WORD WINAPI
Get80x87SaveSize16(void)
250 /***********************************************************************
251 * Save80x87State (SYSTEM.8)
253 void WINAPI
Save80x87State16( char *ptr
)
256 __asm__(".byte 0x66; fsave %0; fwait" : "=m" (ptr
) );
261 /***********************************************************************
262 * Restore80x87State (SYSTEM.9)
264 void WINAPI
Restore80x87State16( const char *ptr
)
267 __asm__(".byte 0x66; frstor %0" : : "m" (ptr
) );
272 /***********************************************************************
273 * A20_Proc (SYSTEM.20)
275 void WINAPI
A20_Proc16( WORD unused
)
277 /* this is also a NOP in Windows */