Converted to the new debug interface, using script written by Patrik
[wine] / misc / system.c
1 /*
2  * SYSTEM DLL routines
3  *
4  * Copyright 1996 Alexandre Julliard
5  */
6
7 #include "wine/winbase16.h"
8 #include "wine/winuser16.h"
9 #include "services.h"
10 #include "debugtools.h"
11
12 DEFAULT_DEBUG_CHANNEL(system)
13
14 typedef struct
15 {
16     SYSTEMTIMERPROC callback;  /* NULL if not in use */
17     INT     rate;
18     INT     ticks;
19 } SYSTEM_TIMER;
20
21 #define NB_SYS_TIMERS   8
22 #define SYS_TIMER_RATE  54925
23
24 static SYSTEM_TIMER SYS_Timers[NB_SYS_TIMERS];
25 static int SYS_NbTimers = 0;
26 static HANDLE SYS_Service = INVALID_HANDLE_VALUE;
27
28
29 /***********************************************************************
30  *           SYSTEM_TimerTick
31  */
32 static void CALLBACK SYSTEM_TimerTick( ULONG_PTR arg )
33 {
34     int i;
35
36     for (i = 0; i < NB_SYS_TIMERS; i++)
37     {
38         if (!SYS_Timers[i].callback) continue;
39         if ((SYS_Timers[i].ticks -= SYS_TIMER_RATE) <= 0)
40         {
41             SYS_Timers[i].ticks += SYS_Timers[i].rate;
42             SYS_Timers[i].callback( i+1 );
43         }
44     }
45 }
46
47 /**********************************************************************
48  *           SYSTEM_StartTicks
49  *
50  * Start the system tick timer.
51  */
52 static void SYSTEM_StartTicks(void)
53 {
54     if ( SYS_Service == INVALID_HANDLE_VALUE )
55         SYS_Service = SERVICE_AddTimer( SYS_TIMER_RATE, SYSTEM_TimerTick, 0L );
56 }
57
58
59 /**********************************************************************
60  *           SYSTEM_StopTicks
61  *
62  * Stop the system tick timer.
63  */
64 static void SYSTEM_StopTicks(void)
65 {
66     if ( SYS_Service != INVALID_HANDLE_VALUE )
67     {
68         SERVICE_Delete( SYS_Service );
69         SYS_Service = INVALID_HANDLE_VALUE;
70     }
71 }
72
73
74 /***********************************************************************
75  *           InquireSystem   (SYSTEM.1)
76  *
77  * Note: the function always takes 2 WORD arguments, contrary to what
78  *       "Undocumented Windows" says.
79   */
80 DWORD WINAPI InquireSystem16( WORD code, WORD arg )
81 {
82     WORD drivetype;
83
84     switch(code)
85     {
86     case 0:  /* Get timer resolution */
87         return SYS_TIMER_RATE;
88
89     case 1:  /* Get drive type */
90         drivetype = GetDriveType16( arg );
91         return MAKELONG( drivetype, drivetype );
92
93     case 2:  /* Enable one-drive logic */
94         FIXME("Case %d: set single-drive %d not supported\n", code, arg );
95         return 0;
96     }
97     WARN("Unknown code %d\n", code );
98     return 0;
99 }
100
101
102 /***********************************************************************
103  *           CreateSystemTimer   (SYSTEM.2)
104  */
105 WORD WINAPI CreateSystemTimer( WORD rate, SYSTEMTIMERPROC callback )
106 {
107     int i;
108     for (i = 0; i < NB_SYS_TIMERS; i++)
109         if (!SYS_Timers[i].callback)  /* Found one */
110         {
111             SYS_Timers[i].rate = (UINT)rate * 1000;
112             if (SYS_Timers[i].rate < SYS_TIMER_RATE)
113                 SYS_Timers[i].rate = SYS_TIMER_RATE;
114             SYS_Timers[i].ticks = SYS_Timers[i].rate;
115             SYS_Timers[i].callback = callback;
116             if (++SYS_NbTimers == 1) SYSTEM_StartTicks();
117             return i + 1;  /* 0 means error */
118         }
119     return 0;
120 }
121
122
123 /***********************************************************************
124  *           KillSystemTimer   (SYSTEM.3)
125  *
126  * Note: do not confuse this function with USER.182
127  */
128 WORD WINAPI SYSTEM_KillSystemTimer( WORD timer )
129 {
130     if (!timer || (timer > NB_SYS_TIMERS)) return timer;  /* Error */
131     SYS_Timers[timer-1].callback = NULL;
132     if (!--SYS_NbTimers) SYSTEM_StopTicks();
133     return 0;
134 }
135
136
137 /***********************************************************************
138  *           EnableSystemTimers   (SYSTEM.4)
139  */
140 void WINAPI EnableSystemTimers16(void)
141 {
142     if ( SYS_Service != INVALID_HANDLE_VALUE )
143         SERVICE_Enable( SYS_Service );
144 }
145
146
147 /***********************************************************************
148  *           DisableSystemTimers   (SYSTEM.5)
149  */
150 void WINAPI DisableSystemTimers16(void)
151 {
152     if ( SYS_Service != INVALID_HANDLE_VALUE )
153         SERVICE_Disable( SYS_Service );
154 }