system.c 6.92 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4
/*
 * SYSTEM DLL routines
 *
 * Copyright 1996 Alexandre Julliard
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliard's avatar
Alexandre Julliard committed
19 20
 */

21 22
#define NONAMELESSUNION
#define NONAMELESSSTRUCT
23 24
#include <stdarg.h>

25
#include "windef.h"
26
#include "winbase.h"
27 28
#include "wine/winbase16.h"
#include "wine/winuser16.h"
29
#include "wownt32.h"
30
#include "winternl.h"
31
#include "wine/debug.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
32

33
WINE_DEFAULT_DEBUG_CHANNEL(system);
34

Alexandre Julliard's avatar
Alexandre Julliard committed
35 36
typedef struct
{
37 38 39
    FARPROC16       callback16;
    INT             rate;
    INT             ticks;
Alexandre Julliard's avatar
Alexandre Julliard committed
40
} SYSTEM_TIMER;
Alexandre Julliard's avatar
Alexandre Julliard committed
41

Alexandre Julliard's avatar
Alexandre Julliard committed
42 43 44 45 46
#define NB_SYS_TIMERS   8
#define SYS_TIMER_RATE  54925

static SYSTEM_TIMER SYS_Timers[NB_SYS_TIMERS];
static int SYS_NbTimers = 0;
47 48 49
static HANDLE SYS_timer;
static HANDLE SYS_thread;
static int SYS_timers_disabled;
50

51

Alexandre Julliard's avatar
Alexandre Julliard committed
52
/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
53 54
 *           SYSTEM_TimerTick
 */
55
static void CALLBACK SYSTEM_TimerTick( LPVOID arg, DWORD low, DWORD high )
Alexandre Julliard's avatar
Alexandre Julliard committed
56 57 58
{
    int i;

59
    if (SYS_timers_disabled) return;
Alexandre Julliard's avatar
Alexandre Julliard committed
60 61
    for (i = 0; i < NB_SYS_TIMERS; i++)
    {
62
        if (!SYS_Timers[i].callback16) continue;
Alexandre Julliard's avatar
Alexandre Julliard committed
63 64
        if ((SYS_Timers[i].ticks -= SYS_TIMER_RATE) <= 0)
        {
65 66 67
            FARPROC16 proc = SYS_Timers[i].callback16;
            CONTEXT86 context;

Alexandre Julliard's avatar
Alexandre Julliard committed
68
            SYS_Timers[i].ticks += SYS_Timers[i].rate;
69 70 71 72 73 74 75 76 77 78

            memset( &context, 0, sizeof(context) );
            context.SegFs = wine_get_fs();
            context.SegGs = wine_get_gs();
            context.SegCs = SELECTOROF( proc );
            context.Eip   = OFFSETOF( proc );
            context.Ebp   = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + FIELD_OFFSET(STACK16FRAME, bp);
            context.Eax   = i + 1;

            WOWCallback16Ex( 0, WCB16_REGS, 0, NULL, (DWORD *)&context );
Alexandre Julliard's avatar
Alexandre Julliard committed
79 80 81 82
        }
    }
}

83 84 85 86 87 88 89 90 91 92

/***********************************************************************
 *           SYSTEM_TimerThread
 */
static DWORD CALLBACK SYSTEM_TimerThread( void *dummy )
{
    LARGE_INTEGER when;

    if (!(SYS_timer = CreateWaitableTimerA( NULL, FALSE, NULL ))) return 0;

93
    when.u.LowPart = when.u.HighPart = 0;
94
    SetWaitableTimer( SYS_timer, &when, (SYS_TIMER_RATE+500)/1000, SYSTEM_TimerTick, 0, FALSE );
95
    for (;;) SleepEx( INFINITE, TRUE );
96 97 98
}


Alexandre Julliard's avatar
Alexandre Julliard committed
99 100 101 102
/**********************************************************************
 *           SYSTEM_StartTicks
 *
 * Start the system tick timer.
Alexandre Julliard's avatar
Alexandre Julliard committed
103
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
104 105
static void SYSTEM_StartTicks(void)
{
106
    if (!SYS_thread) SYS_thread = CreateThread( NULL, 0, SYSTEM_TimerThread, NULL, 0, NULL );
Alexandre Julliard's avatar
Alexandre Julliard committed
107 108 109 110 111 112 113 114 115 116
}


/**********************************************************************
 *           SYSTEM_StopTicks
 *
 * Stop the system tick timer.
 */
static void SYSTEM_StopTicks(void)
{
117
    if (SYS_thread)
118
    {
119 120 121 122 123
        CancelWaitableTimer( SYS_timer );
        TerminateThread( SYS_thread, 0 );
        CloseHandle( SYS_thread );
        CloseHandle( SYS_timer );
        SYS_thread = 0;
124
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
125 126 127 128 129 130 131 132 133
}


/***********************************************************************
 *           InquireSystem   (SYSTEM.1)
 *
 * Note: the function always takes 2 WORD arguments, contrary to what
 *       "Undocumented Windows" says.
  */
134
DWORD WINAPI InquireSystem16( WORD code, WORD arg )
Alexandre Julliard's avatar
Alexandre Julliard committed
135 136
{
    WORD drivetype;
137
    WCHAR root[3];
Alexandre Julliard's avatar
Alexandre Julliard committed
138 139 140 141

    switch(code)
    {
    case 0:  /* Get timer resolution */
Alexandre Julliard's avatar
Alexandre Julliard committed
142
        return SYS_TIMER_RATE;
Alexandre Julliard's avatar
Alexandre Julliard committed
143 144

    case 1:  /* Get drive type */
145 146 147 148 149 150
        root[0] = 'A' + arg;
        root[1] = ':';
        root[2] = 0;
        drivetype = GetDriveTypeW( root );
        if (drivetype == DRIVE_CDROM) drivetype = DRIVE_REMOTE;
        else if (drivetype == DRIVE_NO_ROOT_DIR) drivetype = DRIVE_UNKNOWN;
Alexandre Julliard's avatar
Alexandre Julliard committed
151 152 153
        return MAKELONG( drivetype, drivetype );

    case 2:  /* Enable one-drive logic */
154
        FIXME("Case %d: set single-drive %d not supported\n", code, arg );
Alexandre Julliard's avatar
Alexandre Julliard committed
155 156
        return 0;
    }
157
    WARN("Unknown code %d\n", code );
Alexandre Julliard's avatar
Alexandre Julliard committed
158 159
    return 0;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
160 161 162 163 164


/***********************************************************************
 *           CreateSystemTimer   (SYSTEM.2)
 */
165
WORD WINAPI CreateSystemTimer16( WORD rate, FARPROC16 proc )
Alexandre Julliard's avatar
Alexandre Julliard committed
166 167 168
{
    int i;
    for (i = 0; i < NB_SYS_TIMERS; i++)
169
        if (!SYS_Timers[i].callback16)  /* Found one */
Alexandre Julliard's avatar
Alexandre Julliard committed
170
        {
171
            SYS_Timers[i].rate = (UINT)rate * 1000;
Alexandre Julliard's avatar
Alexandre Julliard committed
172 173 174
            if (SYS_Timers[i].rate < SYS_TIMER_RATE)
                SYS_Timers[i].rate = SYS_TIMER_RATE;
            SYS_Timers[i].ticks = SYS_Timers[i].rate;
175
            SYS_Timers[i].callback16 = proc;
176
            if (++SYS_NbTimers == 1) SYSTEM_StartTicks();
Alexandre Julliard's avatar
Alexandre Julliard committed
177 178 179 180 181 182 183 184 185 186 187
            return i + 1;  /* 0 means error */
        }
    return 0;
}


/***********************************************************************
 *           KillSystemTimer   (SYSTEM.3)
 *
 * Note: do not confuse this function with USER.182
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
188
WORD WINAPI SYSTEM_KillSystemTimer( WORD timer )
Alexandre Julliard's avatar
Alexandre Julliard committed
189
{
190
    if ( !timer || timer > NB_SYS_TIMERS || !SYS_Timers[timer-1].callback16 )
191
        return timer;  /* Error */
192
    SYS_Timers[timer-1].callback16 = 0;
193
    if (!--SYS_NbTimers) SYSTEM_StopTicks();
Alexandre Julliard's avatar
Alexandre Julliard committed
194 195 196 197 198 199 200
    return 0;
}


/***********************************************************************
 *           EnableSystemTimers   (SYSTEM.4)
 */
201
void WINAPI EnableSystemTimers16(void)
Alexandre Julliard's avatar
Alexandre Julliard committed
202
{
203
    SYS_timers_disabled = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
204 205 206 207 208 209
}


/***********************************************************************
 *           DisableSystemTimers   (SYSTEM.5)
 */
210
void WINAPI DisableSystemTimers16(void)
Alexandre Julliard's avatar
Alexandre Julliard committed
211
{
212
    SYS_timers_disabled = 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
213
}
214 215


216 217 218 219 220 221 222 223 224
/***********************************************************************
 *           GetSystemMSecCount (SYSTEM.6)
 */
DWORD WINAPI GetSystemMSecCount16(void)
{
    return GetTickCount();
}


225
/***********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
226
 *           Get80x87SaveSize   (SYSTEM.7)
227
 */
228
WORD WINAPI Get80x87SaveSize16(void)
229 230 231 232 233 234
{
    return 94;
}


/***********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
235
 *           Save80x87State   (SYSTEM.8)
236
 */
237
void WINAPI Save80x87State16( char *ptr )
238 239 240 241 242 243 244 245
{
#ifdef __i386__
    __asm__(".byte 0x66; fsave %0; fwait" : "=m" (ptr) );
#endif
}


/***********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
246
 *           Restore80x87State   (SYSTEM.9)
247
 */
248
void WINAPI Restore80x87State16( const char *ptr )
249 250 251 252 253
{
#ifdef __i386__
    __asm__(".byte 0x66; frstor %0" : : "m" (ptr) );
#endif
}
254 255 256 257 258 259 260 261 262


/***********************************************************************
 *           A20_Proc  (SYSTEM.20)
 */
void WINAPI A20_Proc16( WORD unused )
{
    /* this is also a NOP in Windows */
}