Large-scale renaming of all Win32 functions and types to use the
[wine] / misc / system.c
1 /*
2  * SYSTEM DLL routines
3  *
4  * Copyright 1996 Alexandre Julliard
5  */
6
7 #include <stdlib.h>
8 #include <signal.h>
9 #include <time.h>
10 #include <sys/time.h>
11 #include <sys/timeb.h>
12 #include <sys/types.h>
13 #include <sys/wait.h>
14
15 #include "wine/winbase16.h"
16 #include "wine/winuser16.h"
17 #include "selectors.h"
18 #include "sig_context.h"
19 #include "miscemu.h"
20 #include "debug.h"
21
22 typedef struct
23 {
24     SYSTEMTIMERPROC callback;  /* NULL if not in use */
25     INT     rate;
26     INT     ticks;
27 } SYSTEM_TIMER;
28
29 #define NB_SYS_TIMERS   8
30 #define SYS_TIMER_RATE  54925
31
32 static SYSTEM_TIMER SYS_Timers[NB_SYS_TIMERS];
33 static int SYS_NbTimers = 0;
34 static BOOL SYS_TimersDisabled = FALSE;
35
36
37 /***********************************************************************
38  *           SYSTEM_TimerTick
39  */
40 static HANDLER_DEF(SYSTEM_TimerTick)
41 {
42     int i;
43     HANDLER_INIT();
44
45     for (i = 0; i < NB_SYS_TIMERS; i++)
46     {
47         if (!SYS_Timers[i].callback) continue;
48         if ((SYS_Timers[i].ticks -= SYS_TIMER_RATE) <= 0)
49         {
50             SYS_Timers[i].ticks += SYS_Timers[i].rate;
51             SYS_Timers[i].callback( i+1 );
52         }
53     }
54 }
55
56 /**********************************************************************
57  *           SYSTEM_StartTicks
58  *
59  * Start the system tick timer.
60  */
61 static void SYSTEM_StartTicks(void)
62 {
63     static BOOL handler_installed = FALSE;
64
65     if (!handler_installed)
66     {
67         handler_installed = TRUE;
68         SIGNAL_SetHandler( SIGALRM, SYSTEM_TimerTick, 1 );
69     }
70 #ifndef __EMX__ /* FIXME: Time don't work... Use BIOS directly instead */
71     {
72         struct itimerval vt_timer;
73
74         vt_timer.it_interval.tv_sec = 0;
75         vt_timer.it_interval.tv_usec = 54929;
76         vt_timer.it_value = vt_timer.it_interval;
77         setitimer( ITIMER_REAL, &vt_timer, NULL );
78     }
79 #endif
80 }
81
82
83 /**********************************************************************
84  *           SYSTEM_StopTicks
85  *
86  * Stop the system tick timer.
87  */
88 static void SYSTEM_StopTicks(void)
89 {
90 #ifndef __EMX__ /* FIXME: Time don't work... Use BIOS directly instead */
91     struct itimerval vt_timer;
92
93     vt_timer.it_interval.tv_sec = 0;
94     vt_timer.it_interval.tv_usec = 0;
95     vt_timer.it_value = vt_timer.it_interval;
96     setitimer( ITIMER_REAL, &vt_timer, NULL );
97 #endif
98 }
99
100
101 /***********************************************************************
102  *           InquireSystem   (SYSTEM.1)
103  *
104  * Note: the function always takes 2 WORD arguments, contrary to what
105  *       "Undocumented Windows" says.
106   */
107 DWORD WINAPI InquireSystem16( WORD code, WORD arg )
108 {
109     WORD drivetype;
110
111     switch(code)
112     {
113     case 0:  /* Get timer resolution */
114         return SYS_TIMER_RATE;
115
116     case 1:  /* Get drive type */
117         drivetype = GetDriveType16( arg );
118         return MAKELONG( drivetype, drivetype );
119
120     case 2:  /* Enable one-drive logic */
121         FIXME(system, "Case %d: set single-drive %d not supported\n", code, arg );
122         return 0;
123     }
124     WARN(system, "Unknown code %d\n", code );
125     return 0;
126 }
127
128
129 /***********************************************************************
130  *           CreateSystemTimer   (SYSTEM.2)
131  */
132 WORD WINAPI CreateSystemTimer( WORD rate, SYSTEMTIMERPROC callback )
133 {
134     int i;
135     for (i = 0; i < NB_SYS_TIMERS; i++)
136         if (!SYS_Timers[i].callback)  /* Found one */
137         {
138             SYS_Timers[i].rate = (UINT)rate * 1000;
139             if (SYS_Timers[i].rate < SYS_TIMER_RATE)
140                 SYS_Timers[i].rate = SYS_TIMER_RATE;
141             SYS_Timers[i].ticks = SYS_Timers[i].rate;
142             SYS_Timers[i].callback = callback;
143             if ((++SYS_NbTimers == 1) && !SYS_TimersDisabled)
144                 SYSTEM_StartTicks();
145             return i + 1;  /* 0 means error */
146         }
147     return 0;
148 }
149
150
151 /***********************************************************************
152  *           KillSystemTimer   (SYSTEM.3)
153  *
154  * Note: do not confuse this function with USER.182
155  */
156 WORD WINAPI SYSTEM_KillSystemTimer( WORD timer )
157 {
158     if (!timer || (timer > NB_SYS_TIMERS)) return timer;  /* Error */
159     SYS_Timers[timer-1].callback = NULL;
160     if ((!--SYS_NbTimers) && !SYS_TimersDisabled) SYSTEM_StopTicks();
161     return 0;
162 }
163
164
165 /***********************************************************************
166  *           EnableSystemTimers   (SYSTEM.4)
167  */
168 void WINAPI EnableSystemTimers16(void)
169 {
170     SYS_TimersDisabled = FALSE;
171     if (SYS_NbTimers) SYSTEM_StartTicks();
172 }
173
174
175 /***********************************************************************
176  *           DisableSystemTimers   (SYSTEM.5)
177  */
178 void WINAPI DisableSystemTimers16(void)
179 {
180     SYS_TimersDisabled = TRUE;
181     if (SYS_NbTimers) SYSTEM_StopTicks();
182 }