Store the 16-bit callback in the timer structure instead of using a
[wine] / misc / system.c
1 /*
2  * SYSTEM DLL routines
3  *
4  * Copyright 1996 Alexandre Julliard
5  */
6
7 #include "windef.h"
8 #include "wingdi.h"
9 #include "wine/winbase16.h"
10 #include "wine/winuser16.h"
11 #include "services.h"
12 #include "stackframe.h"
13 #include "debugtools.h"
14
15 DEFAULT_DEBUG_CHANNEL(system);
16
17 typedef struct
18 {
19     SYSTEMTIMERPROC callback;  /* NULL if not in use */
20     FARPROC16       callback16;
21     INT             rate;
22     INT             ticks;
23 } SYSTEM_TIMER;
24
25 #define NB_SYS_TIMERS   8
26 #define SYS_TIMER_RATE  54925
27
28 static SYSTEM_TIMER SYS_Timers[NB_SYS_TIMERS];
29 static int SYS_NbTimers = 0;
30 static HANDLE SYS_Service = INVALID_HANDLE_VALUE;
31
32
33 /***********************************************************************
34  *           SYSTEM_TimerTick
35  */
36 static void CALLBACK SYSTEM_TimerTick( ULONG_PTR arg )
37 {
38     int i;
39
40     for (i = 0; i < NB_SYS_TIMERS; i++)
41     {
42         if (!SYS_Timers[i].callback) continue;
43         if ((SYS_Timers[i].ticks -= SYS_TIMER_RATE) <= 0)
44         {
45             SYS_Timers[i].ticks += SYS_Timers[i].rate;
46             SYS_Timers[i].callback( i+1 );
47         }
48     }
49 }
50
51 /**********************************************************************
52  *           SYSTEM_StartTicks
53  *
54  * Start the system tick timer.
55  */
56 static void SYSTEM_StartTicks(void)
57 {
58     if ( SYS_Service == INVALID_HANDLE_VALUE )
59         SYS_Service = SERVICE_AddTimer( (SYS_TIMER_RATE+500)/1000, SYSTEM_TimerTick, 0L );
60 }
61
62
63 /**********************************************************************
64  *           SYSTEM_StopTicks
65  *
66  * Stop the system tick timer.
67  */
68 static void SYSTEM_StopTicks(void)
69 {
70     if ( SYS_Service != INVALID_HANDLE_VALUE )
71     {
72         SERVICE_Delete( SYS_Service );
73         SYS_Service = INVALID_HANDLE_VALUE;
74     }
75 }
76
77
78 /***********************************************************************
79  *           InquireSystem   (SYSTEM.1)
80  *
81  * Note: the function always takes 2 WORD arguments, contrary to what
82  *       "Undocumented Windows" says.
83   */
84 DWORD WINAPI InquireSystem16( WORD code, WORD arg )
85 {
86     WORD drivetype;
87
88     switch(code)
89     {
90     case 0:  /* Get timer resolution */
91         return SYS_TIMER_RATE;
92
93     case 1:  /* Get drive type */
94         drivetype = GetDriveType16( arg );
95         return MAKELONG( drivetype, drivetype );
96
97     case 2:  /* Enable one-drive logic */
98         FIXME("Case %d: set single-drive %d not supported\n", code, arg );
99         return 0;
100     }
101     WARN("Unknown code %d\n", code );
102     return 0;
103 }
104
105
106 /***********************************************************************
107  *           CreateSystemTimer   (SYSTEM.2)
108  */
109 WORD WINAPI CreateSystemTimer( WORD rate, SYSTEMTIMERPROC callback )
110 {
111     int i;
112     for (i = 0; i < NB_SYS_TIMERS; i++)
113         if (!SYS_Timers[i].callback)  /* Found one */
114         {
115             SYS_Timers[i].rate = (UINT)rate * 1000;
116             if (SYS_Timers[i].rate < SYS_TIMER_RATE)
117                 SYS_Timers[i].rate = SYS_TIMER_RATE;
118             SYS_Timers[i].ticks = SYS_Timers[i].rate;
119             SYS_Timers[i].callback = callback;
120             if (++SYS_NbTimers == 1) SYSTEM_StartTicks();
121             return i + 1;  /* 0 means error */
122         }
123     return 0;
124 }
125
126 /**********************************************************************/
127
128 static void call_timer_proc16( WORD timer )
129 {
130     CONTEXT86 context;
131     FARPROC16 proc = SYS_Timers[timer-1].callback16;
132
133     memset( &context, '\0', sizeof(context) );
134
135     context.SegCs = SELECTOROF( proc );
136     context.Eip   = OFFSETOF( proc );
137     context.Ebp   = OFFSETOF( NtCurrentTeb()->cur_stack )
138                           + (WORD)&((STACK16FRAME*)0)->bp;
139
140     AX_reg( &context ) = timer;
141
142     wine_call_to_16_regs_short( &context, 0 );
143 }
144
145 /**********************************************************************/
146
147 WORD WINAPI WIN16_CreateSystemTimer( WORD rate, FARPROC16 proc )
148 {
149     WORD ret = CreateSystemTimer( rate, call_timer_proc16 );
150     if (ret) SYS_Timers[ret - 1].callback16 = proc;
151     return ret;
152 }
153
154
155 /***********************************************************************
156  *           KillSystemTimer   (SYSTEM.3)
157  *
158  * Note: do not confuse this function with USER.182
159  */
160 WORD WINAPI SYSTEM_KillSystemTimer( WORD timer )
161 {
162     if ( !timer || timer > NB_SYS_TIMERS || !SYS_Timers[timer-1].callback ) 
163         return timer;  /* Error */
164     SYS_Timers[timer-1].callback = NULL;
165     if (!--SYS_NbTimers) SYSTEM_StopTicks();
166     return 0;
167 }
168
169
170 /***********************************************************************
171  *           EnableSystemTimers   (SYSTEM.4)
172  */
173 void WINAPI EnableSystemTimers16(void)
174 {
175     if ( SYS_Service != INVALID_HANDLE_VALUE )
176         SERVICE_Enable( SYS_Service );
177 }
178
179
180 /***********************************************************************
181  *           DisableSystemTimers   (SYSTEM.5)
182  */
183 void WINAPI DisableSystemTimers16(void)
184 {
185     if ( SYS_Service != INVALID_HANDLE_VALUE )
186         SERVICE_Disable( SYS_Service );
187 }
188
189
190 /***********************************************************************
191  *           Get80x87SaveSize   (SYSTEM.7)
192  */
193 WORD WINAPI Get80x87SaveSize16(void)
194 {
195     return 94;
196 }
197
198
199 /***********************************************************************
200  *           Save80x87State   (SYSTEM.8)
201  */
202 void WINAPI Save80x87State16( char *ptr )
203 {
204 #ifdef __i386__
205     __asm__(".byte 0x66; fsave %0; fwait" : "=m" (ptr) );
206 #endif
207 }
208
209
210 /***********************************************************************
211  *           Restore80x87State   (SYSTEM.9)
212  */
213 void WINAPI Restore80x87State16( const char *ptr )
214 {
215 #ifdef __i386__
216     __asm__(".byte 0x66; frstor %0" : : "m" (ptr) );
217 #endif
218 }
219