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
28 #include "wine/winbase16.h"
29 #include "wine/winuser16.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(system
);
38 SYSTEMTIMERPROC callback
; /* NULL if not in use */
44 #define NB_SYS_TIMERS 8
45 #define SYS_TIMER_RATE 54925
47 static SYSTEM_TIMER SYS_Timers
[NB_SYS_TIMERS
];
48 static int SYS_NbTimers
= 0;
49 static HANDLE SYS_timer
;
50 static HANDLE SYS_thread
;
51 static int SYS_timers_disabled
;
53 /***********************************************************************
56 static void CALLBACK
SYSTEM_TimerTick( LPVOID arg
, DWORD low
, DWORD high
)
60 if (SYS_timers_disabled
) return;
61 for (i
= 0; i
< NB_SYS_TIMERS
; i
++)
63 if (!SYS_Timers
[i
].callback
) continue;
64 if ((SYS_Timers
[i
].ticks
-= SYS_TIMER_RATE
) <= 0)
66 SYS_Timers
[i
].ticks
+= SYS_Timers
[i
].rate
;
67 SYS_Timers
[i
].callback( i
+1 );
73 /***********************************************************************
76 static DWORD CALLBACK
SYSTEM_TimerThread( void *dummy
)
80 if (!(SYS_timer
= CreateWaitableTimerA( NULL
, FALSE
, NULL
))) return 0;
82 when
.u
.LowPart
= when
.u
.HighPart
= 0;
83 SetWaitableTimer( SYS_timer
, &when
, (SYS_TIMER_RATE
+500)/1000, SYSTEM_TimerTick
, 0, FALSE
);
84 for (;;) SleepEx( INFINITE
, TRUE
);
88 /**********************************************************************
91 * Start the system tick timer.
93 static void SYSTEM_StartTicks(void)
95 if (!SYS_thread
) SYS_thread
= CreateThread( NULL
, 0, SYSTEM_TimerThread
, NULL
, 0, NULL
);
99 /**********************************************************************
102 * Stop the system tick timer.
104 static void SYSTEM_StopTicks(void)
108 CancelWaitableTimer( SYS_timer
);
109 TerminateThread( SYS_thread
, 0 );
110 CloseHandle( SYS_thread
);
111 CloseHandle( SYS_timer
);
117 /***********************************************************************
118 * InquireSystem (SYSTEM.1)
120 * Note: the function always takes 2 WORD arguments, contrary to what
121 * "Undocumented Windows" says.
123 DWORD WINAPI
InquireSystem16( WORD code
, WORD arg
)
129 case 0: /* Get timer resolution */
130 return SYS_TIMER_RATE
;
132 case 1: /* Get drive type */
133 drivetype
= GetDriveType16( arg
);
134 return MAKELONG( drivetype
, drivetype
);
136 case 2: /* Enable one-drive logic */
137 FIXME("Case %d: set single-drive %d not supported\n", code
, arg
);
140 WARN("Unknown code %d\n", code
);
145 /***********************************************************************
146 * CreateSystemTimer (SYSTEM.2)
148 WORD WINAPI
CreateSystemTimer( WORD rate
, SYSTEMTIMERPROC callback
)
151 for (i
= 0; i
< NB_SYS_TIMERS
; i
++)
152 if (!SYS_Timers
[i
].callback
) /* Found one */
154 SYS_Timers
[i
].rate
= (UINT
)rate
* 1000;
155 if (SYS_Timers
[i
].rate
< SYS_TIMER_RATE
)
156 SYS_Timers
[i
].rate
= SYS_TIMER_RATE
;
157 SYS_Timers
[i
].ticks
= SYS_Timers
[i
].rate
;
158 SYS_Timers
[i
].callback
= callback
;
159 if (++SYS_NbTimers
== 1) SYSTEM_StartTicks();
160 return i
+ 1; /* 0 means error */
165 /**********************************************************************/
167 static void call_timer_proc16( WORD timer
)
170 FARPROC16 proc
= SYS_Timers
[timer
-1].callback16
;
172 memset( &context
, 0, sizeof(context
) );
174 context
.SegFs
= wine_get_fs();
175 context
.SegGs
= wine_get_gs();
176 context
.SegCs
= SELECTOROF( proc
);
177 context
.Eip
= OFFSETOF( proc
);
178 context
.Ebp
= OFFSETOF(NtCurrentTeb()->WOW32Reserved
) + (WORD
)&((STACK16FRAME
*)0)->bp
;
181 WOWCallback16Ex( 0, WCB16_REGS
, 0, NULL
, (DWORD
*)&context
);
184 /**********************************************************************/
186 WORD WINAPI
WIN16_CreateSystemTimer( WORD rate
, FARPROC16 proc
)
188 WORD ret
= CreateSystemTimer( rate
, call_timer_proc16
);
189 if (ret
) SYS_Timers
[ret
- 1].callback16
= proc
;
194 /***********************************************************************
195 * KillSystemTimer (SYSTEM.3)
197 * Note: do not confuse this function with USER.182
199 WORD WINAPI
SYSTEM_KillSystemTimer( WORD timer
)
201 if ( !timer
|| timer
> NB_SYS_TIMERS
|| !SYS_Timers
[timer
-1].callback
)
202 return timer
; /* Error */
203 SYS_Timers
[timer
-1].callback
= NULL
;
204 if (!--SYS_NbTimers
) SYSTEM_StopTicks();
209 /***********************************************************************
210 * EnableSystemTimers (SYSTEM.4)
212 void WINAPI
EnableSystemTimers16(void)
214 SYS_timers_disabled
= 0;
218 /***********************************************************************
219 * DisableSystemTimers (SYSTEM.5)
221 void WINAPI
DisableSystemTimers16(void)
223 SYS_timers_disabled
= 1;
227 /***********************************************************************
228 * GetSystemMSecCount (SYSTEM.6)
230 DWORD WINAPI
GetSystemMSecCount16(void)
232 return GetTickCount();
236 /***********************************************************************
237 * Get80x87SaveSize (SYSTEM.7)
239 WORD WINAPI
Get80x87SaveSize16(void)
245 /***********************************************************************
246 * Save80x87State (SYSTEM.8)
248 void WINAPI
Save80x87State16( char *ptr
)
251 __asm__(".byte 0x66; fsave %0; fwait" : "=m" (ptr
) );
256 /***********************************************************************
257 * Restore80x87State (SYSTEM.9)
259 void WINAPI
Restore80x87State16( const char *ptr
)
262 __asm__(".byte 0x66; frstor %0" : : "m" (ptr
) );
267 /***********************************************************************
268 * A20_Proc (SYSTEM.20)
270 void WINAPI
A20_Proc16( WORD unused
)
272 /* this is also a NOP in Windows */