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
28 #include "wine/winbase16.h"
29 #include "wine/winuser16.h"
32 #include "kernel_private.h"
33 #include "kernel16_private.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(system
);
40 SYSTEMTIMERPROC callback
; /* NULL if not in use */
46 #define NB_SYS_TIMERS 8
47 #define SYS_TIMER_RATE 54925
49 static SYSTEM_TIMER SYS_Timers
[NB_SYS_TIMERS
];
50 static int SYS_NbTimers
= 0;
51 static HANDLE SYS_timer
;
52 static HANDLE SYS_thread
;
53 static int SYS_timers_disabled
;
55 /***********************************************************************
58 static void CALLBACK
SYSTEM_TimerTick( LPVOID arg
, DWORD low
, DWORD high
)
62 if (SYS_timers_disabled
) return;
63 for (i
= 0; i
< NB_SYS_TIMERS
; i
++)
65 if (!SYS_Timers
[i
].callback
) continue;
66 if ((SYS_Timers
[i
].ticks
-= SYS_TIMER_RATE
) <= 0)
68 SYS_Timers
[i
].ticks
+= SYS_Timers
[i
].rate
;
69 SYS_Timers
[i
].callback( i
+1 );
75 /***********************************************************************
78 static DWORD CALLBACK
SYSTEM_TimerThread( void *dummy
)
82 if (!(SYS_timer
= CreateWaitableTimerA( NULL
, FALSE
, NULL
))) return 0;
84 when
.u
.LowPart
= when
.u
.HighPart
= 0;
85 SetWaitableTimer( SYS_timer
, &when
, (SYS_TIMER_RATE
+500)/1000, SYSTEM_TimerTick
, 0, FALSE
);
86 for (;;) SleepEx( INFINITE
, TRUE
);
90 /**********************************************************************
93 * Start the system tick timer.
95 static void SYSTEM_StartTicks(void)
97 if (!SYS_thread
) SYS_thread
= CreateThread( NULL
, 0, SYSTEM_TimerThread
, NULL
, 0, NULL
);
101 /**********************************************************************
104 * Stop the system tick timer.
106 static void SYSTEM_StopTicks(void)
110 CancelWaitableTimer( SYS_timer
);
111 TerminateThread( SYS_thread
, 0 );
112 CloseHandle( SYS_thread
);
113 CloseHandle( SYS_timer
);
119 /***********************************************************************
120 * InquireSystem (SYSTEM.1)
122 * Note: the function always takes 2 WORD arguments, contrary to what
123 * "Undocumented Windows" says.
125 DWORD WINAPI
InquireSystem16( WORD code
, WORD arg
)
131 case 0: /* Get timer resolution */
132 return SYS_TIMER_RATE
;
134 case 1: /* Get drive type */
135 drivetype
= GetDriveType16( arg
);
136 return MAKELONG( drivetype
, drivetype
);
138 case 2: /* Enable one-drive logic */
139 FIXME("Case %d: set single-drive %d not supported\n", code
, arg
);
142 WARN("Unknown code %d\n", code
);
147 /***********************************************************************
148 * CreateSystemTimer (SYSTEM.2)
150 WORD WINAPI
CreateSystemTimer( WORD rate
, SYSTEMTIMERPROC callback
)
153 for (i
= 0; i
< NB_SYS_TIMERS
; i
++)
154 if (!SYS_Timers
[i
].callback
) /* Found one */
156 SYS_Timers
[i
].rate
= (UINT
)rate
* 1000;
157 if (SYS_Timers
[i
].rate
< SYS_TIMER_RATE
)
158 SYS_Timers
[i
].rate
= SYS_TIMER_RATE
;
159 SYS_Timers
[i
].ticks
= SYS_Timers
[i
].rate
;
160 SYS_Timers
[i
].callback
= callback
;
161 if (++SYS_NbTimers
== 1) SYSTEM_StartTicks();
162 return i
+ 1; /* 0 means error */
167 /**********************************************************************/
169 static void call_timer_proc16( WORD timer
)
172 FARPROC16 proc
= SYS_Timers
[timer
-1].callback16
;
174 memset( &context
, 0, sizeof(context
) );
176 context
.SegFs
= wine_get_fs();
177 context
.SegGs
= wine_get_gs();
178 context
.SegCs
= SELECTOROF( proc
);
179 context
.Eip
= OFFSETOF( proc
);
180 context
.Ebp
= OFFSETOF(NtCurrentTeb()->WOW32Reserved
) + (WORD
)&((STACK16FRAME
*)0)->bp
;
183 WOWCallback16Ex( 0, WCB16_REGS
, 0, NULL
, (DWORD
*)&context
);
186 /**********************************************************************/
188 WORD WINAPI
WIN16_CreateSystemTimer( WORD rate
, FARPROC16 proc
)
190 WORD ret
= CreateSystemTimer( rate
, call_timer_proc16
);
191 if (ret
) SYS_Timers
[ret
- 1].callback16
= proc
;
196 /***********************************************************************
197 * KillSystemTimer (SYSTEM.3)
199 * Note: do not confuse this function with USER.182
201 WORD WINAPI
SYSTEM_KillSystemTimer( WORD timer
)
203 if ( !timer
|| timer
> NB_SYS_TIMERS
|| !SYS_Timers
[timer
-1].callback
)
204 return timer
; /* Error */
205 SYS_Timers
[timer
-1].callback
= NULL
;
206 if (!--SYS_NbTimers
) SYSTEM_StopTicks();
211 /***********************************************************************
212 * EnableSystemTimers (SYSTEM.4)
214 void WINAPI
EnableSystemTimers16(void)
216 SYS_timers_disabled
= 0;
220 /***********************************************************************
221 * DisableSystemTimers (SYSTEM.5)
223 void WINAPI
DisableSystemTimers16(void)
225 SYS_timers_disabled
= 1;
229 /***********************************************************************
230 * Get80x87SaveSize (SYSTEM.7)
232 WORD WINAPI
Get80x87SaveSize16(void)
238 /***********************************************************************
239 * Save80x87State (SYSTEM.8)
241 void WINAPI
Save80x87State16( char *ptr
)
244 __asm__(".byte 0x66; fsave %0; fwait" : "=m" (ptr
) );
249 /***********************************************************************
250 * Restore80x87State (SYSTEM.9)
252 void WINAPI
Restore80x87State16( const char *ptr
)
255 __asm__(".byte 0x66; frstor %0" : : "m" (ptr
) );