Define NONAMELESS{STRUCT,UNION} explicitly in the files that need them.
[wine] / misc / system.c
1 /*
2  * SYSTEM DLL routines
3  *
4  * Copyright 1996 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #define NONAMELESSUNION
22 #define NONAMELESSSTRUCT
23 #include "windef.h"
24 #include "wingdi.h"
25 #include "wine/winbase16.h"
26 #include "wine/winuser16.h"
27 #include "stackframe.h"
28 #include "wine/debug.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(system);
31
32 typedef struct
33 {
34     SYSTEMTIMERPROC callback;  /* NULL if not in use */
35     FARPROC16       callback16;
36     INT             rate;
37     INT             ticks;
38 } SYSTEM_TIMER;
39
40 #define NB_SYS_TIMERS   8
41 #define SYS_TIMER_RATE  54925
42
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;
48
49 /***********************************************************************
50  *           SYSTEM_TimerTick
51  */
52 static void CALLBACK SYSTEM_TimerTick( LPVOID arg, DWORD low, DWORD high )
53 {
54     int i;
55
56     if (SYS_timers_disabled) return;
57     for (i = 0; i < NB_SYS_TIMERS; i++)
58     {
59         if (!SYS_Timers[i].callback) continue;
60         if ((SYS_Timers[i].ticks -= SYS_TIMER_RATE) <= 0)
61         {
62             SYS_Timers[i].ticks += SYS_Timers[i].rate;
63             SYS_Timers[i].callback( i+1 );
64         }
65     }
66 }
67
68
69 /***********************************************************************
70  *           SYSTEM_TimerThread
71  */
72 static DWORD CALLBACK SYSTEM_TimerThread( void *dummy )
73 {
74     LARGE_INTEGER when;
75
76     if (!(SYS_timer = CreateWaitableTimerA( NULL, FALSE, NULL ))) return 0;
77
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 );
81 }
82
83
84 /**********************************************************************
85  *           SYSTEM_StartTicks
86  *
87  * Start the system tick timer.
88  */
89 static void SYSTEM_StartTicks(void)
90 {
91     if (!SYS_thread) SYS_thread = CreateThread( NULL, 0, SYSTEM_TimerThread, NULL, 0, NULL );
92 }
93
94
95 /**********************************************************************
96  *           SYSTEM_StopTicks
97  *
98  * Stop the system tick timer.
99  */
100 static void SYSTEM_StopTicks(void)
101 {
102     if (SYS_thread)
103     {
104         CancelWaitableTimer( SYS_timer );
105         TerminateThread( SYS_thread, 0 );
106         CloseHandle( SYS_thread );
107         CloseHandle( SYS_timer );
108         SYS_thread = 0;
109     }
110 }
111
112
113 /***********************************************************************
114  *           InquireSystem   (SYSTEM.1)
115  *
116  * Note: the function always takes 2 WORD arguments, contrary to what
117  *       "Undocumented Windows" says.
118   */
119 DWORD WINAPI InquireSystem16( WORD code, WORD arg )
120 {
121     WORD drivetype;
122
123     switch(code)
124     {
125     case 0:  /* Get timer resolution */
126         return SYS_TIMER_RATE;
127
128     case 1:  /* Get drive type */
129         drivetype = GetDriveType16( arg );
130         return MAKELONG( drivetype, drivetype );
131
132     case 2:  /* Enable one-drive logic */
133         FIXME("Case %d: set single-drive %d not supported\n", code, arg );
134         return 0;
135     }
136     WARN("Unknown code %d\n", code );
137     return 0;
138 }
139
140
141 /***********************************************************************
142  *           CreateSystemTimer   (SYSTEM.2)
143  */
144 WORD WINAPI CreateSystemTimer( WORD rate, SYSTEMTIMERPROC callback )
145 {
146     int i;
147     for (i = 0; i < NB_SYS_TIMERS; i++)
148         if (!SYS_Timers[i].callback)  /* Found one */
149         {
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 */
157         }
158     return 0;
159 }
160
161 /**********************************************************************/
162
163 static void call_timer_proc16( WORD timer )
164 {
165     CONTEXT86 context;
166     FARPROC16 proc = SYS_Timers[timer-1].callback16;
167
168     memset( &context, '\0', sizeof(context) );
169
170     context.SegCs = SELECTOROF( proc );
171     context.Eip   = OFFSETOF( proc );
172     context.Ebp   = OFFSETOF( NtCurrentTeb()->cur_stack )
173                           + (WORD)&((STACK16FRAME*)0)->bp;
174     context.Eax   = timer;
175
176     wine_call_to_16_regs_short( &context, 0 );
177 }
178
179 /**********************************************************************/
180
181 WORD WINAPI WIN16_CreateSystemTimer( WORD rate, FARPROC16 proc )
182 {
183     WORD ret = CreateSystemTimer( rate, call_timer_proc16 );
184     if (ret) SYS_Timers[ret - 1].callback16 = proc;
185     return ret;
186 }
187
188
189 /***********************************************************************
190  *           KillSystemTimer   (SYSTEM.3)
191  *
192  * Note: do not confuse this function with USER.182
193  */
194 WORD WINAPI SYSTEM_KillSystemTimer( WORD timer )
195 {
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();
200     return 0;
201 }
202
203
204 /***********************************************************************
205  *           EnableSystemTimers   (SYSTEM.4)
206  */
207 void WINAPI EnableSystemTimers16(void)
208 {
209     SYS_timers_disabled = 0;
210 }
211
212
213 /***********************************************************************
214  *           DisableSystemTimers   (SYSTEM.5)
215  */
216 void WINAPI DisableSystemTimers16(void)
217 {
218     SYS_timers_disabled = 1;
219 }
220
221
222 /***********************************************************************
223  *           Get80x87SaveSize   (SYSTEM.7)
224  */
225 WORD WINAPI Get80x87SaveSize16(void)
226 {
227     return 94;
228 }
229
230
231 /***********************************************************************
232  *           Save80x87State   (SYSTEM.8)
233  */
234 void WINAPI Save80x87State16( char *ptr )
235 {
236 #ifdef __i386__
237     __asm__(".byte 0x66; fsave %0; fwait" : "=m" (ptr) );
238 #endif
239 }
240
241
242 /***********************************************************************
243  *           Restore80x87State   (SYSTEM.9)
244  */
245 void WINAPI Restore80x87State16( const char *ptr )
246 {
247 #ifdef __i386__
248     __asm__(".byte 0x66; frstor %0" : : "m" (ptr) );
249 #endif
250 }