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
25 #include "wine/winbase16.h"
26 #include "wine/winuser16.h"
27 #include "stackframe.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(system
);
34 SYSTEMTIMERPROC callback
; /* NULL if not in use */
40 #define NB_SYS_TIMERS 8
41 #define SYS_TIMER_RATE 54925
43 static SYSTEM_TIMER SYS_Timers
[NB_SYS_TIMERS
];
44 static int SYS_NbTimers
= 0;
45 static HANDLE SYS_timer
;
46 static HANDLE SYS_thread
;
47 static int SYS_timers_disabled
;
49 /***********************************************************************
52 static void CALLBACK
SYSTEM_TimerTick( LPVOID arg
, DWORD low
, DWORD high
)
56 if (SYS_timers_disabled
) return;
57 for (i
= 0; i
< NB_SYS_TIMERS
; i
++)
59 if (!SYS_Timers
[i
].callback
) continue;
60 if ((SYS_Timers
[i
].ticks
-= SYS_TIMER_RATE
) <= 0)
62 SYS_Timers
[i
].ticks
+= SYS_Timers
[i
].rate
;
63 SYS_Timers
[i
].callback( i
+1 );
69 /***********************************************************************
72 static DWORD CALLBACK
SYSTEM_TimerThread( void *dummy
)
76 if (!(SYS_timer
= CreateWaitableTimerA( NULL
, FALSE
, NULL
))) return 0;
78 when
.s
.LowPart
= when
.s
.HighPart
= 0;
79 SetWaitableTimer( SYS_timer
, &when
, (SYS_TIMER_RATE
+500)/1000, SYSTEM_TimerTick
, 0, FALSE
);
80 for (;;) WaitForMultipleObjectsEx( 0, NULL
, FALSE
, INFINITE
, TRUE
);
84 /**********************************************************************
87 * Start the system tick timer.
89 static void SYSTEM_StartTicks(void)
91 if (!SYS_thread
) SYS_thread
= CreateThread( NULL
, 0, SYSTEM_TimerThread
, NULL
, 0, NULL
);
95 /**********************************************************************
98 * Stop the system tick timer.
100 static void SYSTEM_StopTicks(void)
104 CancelWaitableTimer( SYS_timer
);
105 TerminateThread( SYS_thread
, 0 );
106 CloseHandle( SYS_thread
);
107 CloseHandle( SYS_timer
);
113 /***********************************************************************
114 * InquireSystem (SYSTEM.1)
116 * Note: the function always takes 2 WORD arguments, contrary to what
117 * "Undocumented Windows" says.
119 DWORD WINAPI
InquireSystem16( WORD code
, WORD arg
)
125 case 0: /* Get timer resolution */
126 return SYS_TIMER_RATE
;
128 case 1: /* Get drive type */
129 drivetype
= GetDriveType16( arg
);
130 return MAKELONG( drivetype
, drivetype
);
132 case 2: /* Enable one-drive logic */
133 FIXME("Case %d: set single-drive %d not supported\n", code
, arg
);
136 WARN("Unknown code %d\n", code
);
141 /***********************************************************************
142 * CreateSystemTimer (SYSTEM.2)
144 WORD WINAPI
CreateSystemTimer( WORD rate
, SYSTEMTIMERPROC callback
)
147 for (i
= 0; i
< NB_SYS_TIMERS
; i
++)
148 if (!SYS_Timers
[i
].callback
) /* Found one */
150 SYS_Timers
[i
].rate
= (UINT
)rate
* 1000;
151 if (SYS_Timers
[i
].rate
< SYS_TIMER_RATE
)
152 SYS_Timers
[i
].rate
= SYS_TIMER_RATE
;
153 SYS_Timers
[i
].ticks
= SYS_Timers
[i
].rate
;
154 SYS_Timers
[i
].callback
= callback
;
155 if (++SYS_NbTimers
== 1) SYSTEM_StartTicks();
156 return i
+ 1; /* 0 means error */
161 /**********************************************************************/
163 static void call_timer_proc16( WORD timer
)
166 FARPROC16 proc
= SYS_Timers
[timer
-1].callback16
;
168 memset( &context
, '\0', sizeof(context
) );
170 context
.SegCs
= SELECTOROF( proc
);
171 context
.Eip
= OFFSETOF( proc
);
172 context
.Ebp
= OFFSETOF( NtCurrentTeb()->cur_stack
)
173 + (WORD
)&((STACK16FRAME
*)0)->bp
;
176 wine_call_to_16_regs_short( &context
, 0 );
179 /**********************************************************************/
181 WORD WINAPI
WIN16_CreateSystemTimer( WORD rate
, FARPROC16 proc
)
183 WORD ret
= CreateSystemTimer( rate
, call_timer_proc16
);
184 if (ret
) SYS_Timers
[ret
- 1].callback16
= proc
;
189 /***********************************************************************
190 * KillSystemTimer (SYSTEM.3)
192 * Note: do not confuse this function with USER.182
194 WORD WINAPI
SYSTEM_KillSystemTimer( WORD timer
)
196 if ( !timer
|| timer
> NB_SYS_TIMERS
|| !SYS_Timers
[timer
-1].callback
)
197 return timer
; /* Error */
198 SYS_Timers
[timer
-1].callback
= NULL
;
199 if (!--SYS_NbTimers
) SYSTEM_StopTicks();
204 /***********************************************************************
205 * EnableSystemTimers (SYSTEM.4)
207 void WINAPI
EnableSystemTimers16(void)
209 SYS_timers_disabled
= 0;
213 /***********************************************************************
214 * DisableSystemTimers (SYSTEM.5)
216 void WINAPI
DisableSystemTimers16(void)
218 SYS_timers_disabled
= 1;
222 /***********************************************************************
223 * Get80x87SaveSize (SYSTEM.7)
225 WORD WINAPI
Get80x87SaveSize16(void)
231 /***********************************************************************
232 * Save80x87State (SYSTEM.8)
234 void WINAPI
Save80x87State16( char *ptr
)
237 __asm__(".byte 0x66; fsave %0; fwait" : "=m" (ptr
) );
242 /***********************************************************************
243 * Restore80x87State (SYSTEM.9)
245 void WINAPI
Restore80x87State16( const char *ptr
)
248 __asm__(".byte 0x66; frstor %0" : : "m" (ptr
) );