Alexandre Julliard | ef702d8 | 1996-05-28 18:54:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SYSTEM DLL routines |
| 3 | * |
| 4 | * Copyright 1996 Alexandre Julliard |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 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 |
Alexandre Julliard | ef702d8 | 1996-05-28 18:54:58 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
Jeremy White | d3e22d9 | 2000-02-10 19:03:02 +0000 | [diff] [blame] | 21 | #include "windef.h" |
| 22 | #include "wingdi.h" |
Marcus Meissner | 04c3e1d | 1999-02-19 10:37:02 +0000 | [diff] [blame] | 23 | #include "wine/winbase16.h" |
| 24 | #include "wine/winuser16.h" |
Ulrich Weigand | 974fd10 | 1999-09-20 18:45:53 +0000 | [diff] [blame] | 25 | #include "stackframe.h" |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 26 | #include "wine/debug.h" |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 27 | |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 28 | WINE_DEFAULT_DEBUG_CHANNEL(system); |
Patrik Stridvall | b4b9fae | 1999-04-19 14:56:29 +0000 | [diff] [blame] | 29 | |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 30 | typedef struct |
| 31 | { |
Ulrich Weigand | f6c4828 | 1998-12-10 10:44:38 +0000 | [diff] [blame] | 32 | SYSTEMTIMERPROC callback; /* NULL if not in use */ |
Alexandre Julliard | 88ff58e | 2001-12-17 22:13:31 +0000 | [diff] [blame] | 33 | FARPROC16 callback16; |
| 34 | INT rate; |
| 35 | INT ticks; |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 36 | } SYSTEM_TIMER; |
| 37 | |
| 38 | #define NB_SYS_TIMERS 8 |
| 39 | #define SYS_TIMER_RATE 54925 |
| 40 | |
| 41 | static SYSTEM_TIMER SYS_Timers[NB_SYS_TIMERS]; |
| 42 | static int SYS_NbTimers = 0; |
Alexandre Julliard | ac0e137 | 2002-03-23 18:48:53 +0000 | [diff] [blame] | 43 | static HANDLE SYS_timer; |
| 44 | static HANDLE SYS_thread; |
| 45 | static int SYS_timers_disabled; |
Ulrich Weigand | f6c4828 | 1998-12-10 10:44:38 +0000 | [diff] [blame] | 46 | |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 47 | /*********************************************************************** |
| 48 | * SYSTEM_TimerTick |
| 49 | */ |
Alexandre Julliard | ac0e137 | 2002-03-23 18:48:53 +0000 | [diff] [blame] | 50 | static void CALLBACK SYSTEM_TimerTick( LPVOID arg, DWORD low, DWORD high ) |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 51 | { |
| 52 | int i; |
| 53 | |
Alexandre Julliard | ac0e137 | 2002-03-23 18:48:53 +0000 | [diff] [blame] | 54 | if (SYS_timers_disabled) return; |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 55 | for (i = 0; i < NB_SYS_TIMERS; i++) |
| 56 | { |
| 57 | if (!SYS_Timers[i].callback) continue; |
| 58 | if ((SYS_Timers[i].ticks -= SYS_TIMER_RATE) <= 0) |
| 59 | { |
| 60 | SYS_Timers[i].ticks += SYS_Timers[i].rate; |
Ulrich Weigand | f6c4828 | 1998-12-10 10:44:38 +0000 | [diff] [blame] | 61 | SYS_Timers[i].callback( i+1 ); |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
Alexandre Julliard | ac0e137 | 2002-03-23 18:48:53 +0000 | [diff] [blame] | 66 | |
| 67 | /*********************************************************************** |
| 68 | * SYSTEM_TimerThread |
| 69 | */ |
| 70 | static DWORD CALLBACK SYSTEM_TimerThread( void *dummy ) |
| 71 | { |
| 72 | LARGE_INTEGER when; |
| 73 | |
| 74 | if (!(SYS_timer = CreateWaitableTimerA( NULL, FALSE, NULL ))) return 0; |
| 75 | |
| 76 | when.s.LowPart = when.s.HighPart = 0; |
| 77 | SetWaitableTimer( SYS_timer, &when, (SYS_TIMER_RATE+500)/1000, SYSTEM_TimerTick, 0, FALSE ); |
| 78 | for (;;) WaitForMultipleObjectsEx( 0, NULL, FALSE, INFINITE, TRUE ); |
| 79 | } |
| 80 | |
| 81 | |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 82 | /********************************************************************** |
| 83 | * SYSTEM_StartTicks |
| 84 | * |
| 85 | * Start the system tick timer. |
| 86 | */ |
| 87 | static void SYSTEM_StartTicks(void) |
| 88 | { |
Alexandre Julliard | ac0e137 | 2002-03-23 18:48:53 +0000 | [diff] [blame] | 89 | if (!SYS_thread) SYS_thread = CreateThread( NULL, 0, SYSTEM_TimerThread, NULL, 0, NULL ); |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | |
| 93 | /********************************************************************** |
| 94 | * SYSTEM_StopTicks |
| 95 | * |
| 96 | * Stop the system tick timer. |
| 97 | */ |
| 98 | static void SYSTEM_StopTicks(void) |
| 99 | { |
Alexandre Julliard | ac0e137 | 2002-03-23 18:48:53 +0000 | [diff] [blame] | 100 | if (SYS_thread) |
Ulrich Weigand | 06bcab8 | 1999-04-11 15:04:00 +0000 | [diff] [blame] | 101 | { |
Alexandre Julliard | ac0e137 | 2002-03-23 18:48:53 +0000 | [diff] [blame] | 102 | CancelWaitableTimer( SYS_timer ); |
| 103 | TerminateThread( SYS_thread, 0 ); |
| 104 | CloseHandle( SYS_thread ); |
| 105 | CloseHandle( SYS_timer ); |
| 106 | SYS_thread = 0; |
Ulrich Weigand | 06bcab8 | 1999-04-11 15:04:00 +0000 | [diff] [blame] | 107 | } |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 108 | } |
Alexandre Julliard | ef702d8 | 1996-05-28 18:54:58 +0000 | [diff] [blame] | 109 | |
| 110 | |
| 111 | /*********************************************************************** |
| 112 | * InquireSystem (SYSTEM.1) |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 113 | * |
| 114 | * Note: the function always takes 2 WORD arguments, contrary to what |
| 115 | * "Undocumented Windows" says. |
| 116 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 117 | DWORD WINAPI InquireSystem16( WORD code, WORD arg ) |
Alexandre Julliard | ef702d8 | 1996-05-28 18:54:58 +0000 | [diff] [blame] | 118 | { |
| 119 | WORD drivetype; |
| 120 | |
| 121 | switch(code) |
| 122 | { |
| 123 | case 0: /* Get timer resolution */ |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 124 | return SYS_TIMER_RATE; |
Alexandre Julliard | ef702d8 | 1996-05-28 18:54:58 +0000 | [diff] [blame] | 125 | |
| 126 | case 1: /* Get drive type */ |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 127 | drivetype = GetDriveType16( arg ); |
Alexandre Julliard | ef702d8 | 1996-05-28 18:54:58 +0000 | [diff] [blame] | 128 | return MAKELONG( drivetype, drivetype ); |
| 129 | |
| 130 | case 2: /* Enable one-drive logic */ |
Alexandre Julliard | 61fece0 | 1999-06-26 19:09:08 +0000 | [diff] [blame] | 131 | FIXME("Case %d: set single-drive %d not supported\n", code, arg ); |
Alexandre Julliard | ef702d8 | 1996-05-28 18:54:58 +0000 | [diff] [blame] | 132 | return 0; |
| 133 | } |
Alexandre Julliard | 61fece0 | 1999-06-26 19:09:08 +0000 | [diff] [blame] | 134 | WARN("Unknown code %d\n", code ); |
Alexandre Julliard | ef702d8 | 1996-05-28 18:54:58 +0000 | [diff] [blame] | 135 | return 0; |
| 136 | } |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 137 | |
| 138 | |
| 139 | /*********************************************************************** |
| 140 | * CreateSystemTimer (SYSTEM.2) |
| 141 | */ |
Ulrich Weigand | f6c4828 | 1998-12-10 10:44:38 +0000 | [diff] [blame] | 142 | WORD WINAPI CreateSystemTimer( WORD rate, SYSTEMTIMERPROC callback ) |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 143 | { |
| 144 | int i; |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 145 | for (i = 0; i < NB_SYS_TIMERS; i++) |
| 146 | if (!SYS_Timers[i].callback) /* Found one */ |
| 147 | { |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 148 | SYS_Timers[i].rate = (UINT)rate * 1000; |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 149 | if (SYS_Timers[i].rate < SYS_TIMER_RATE) |
| 150 | SYS_Timers[i].rate = SYS_TIMER_RATE; |
| 151 | SYS_Timers[i].ticks = SYS_Timers[i].rate; |
| 152 | SYS_Timers[i].callback = callback; |
Ulrich Weigand | 06bcab8 | 1999-04-11 15:04:00 +0000 | [diff] [blame] | 153 | if (++SYS_NbTimers == 1) SYSTEM_StartTicks(); |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 154 | return i + 1; /* 0 means error */ |
| 155 | } |
| 156 | return 0; |
| 157 | } |
| 158 | |
Patrik Stridvall | 2b3aa61 | 2000-12-01 23:58:28 +0000 | [diff] [blame] | 159 | /**********************************************************************/ |
| 160 | |
Alexandre Julliard | 88ff58e | 2001-12-17 22:13:31 +0000 | [diff] [blame] | 161 | static void call_timer_proc16( WORD timer ) |
Ulrich Weigand | 974fd10 | 1999-09-20 18:45:53 +0000 | [diff] [blame] | 162 | { |
| 163 | CONTEXT86 context; |
Alexandre Julliard | 88ff58e | 2001-12-17 22:13:31 +0000 | [diff] [blame] | 164 | FARPROC16 proc = SYS_Timers[timer-1].callback16; |
| 165 | |
Ulrich Weigand | 974fd10 | 1999-09-20 18:45:53 +0000 | [diff] [blame] | 166 | memset( &context, '\0', sizeof(context) ); |
| 167 | |
Alexandre Julliard | d8fab2e | 2000-09-25 23:53:07 +0000 | [diff] [blame] | 168 | context.SegCs = SELECTOROF( proc ); |
| 169 | context.Eip = OFFSETOF( proc ); |
| 170 | context.Ebp = OFFSETOF( NtCurrentTeb()->cur_stack ) |
Ulrich Weigand | 974fd10 | 1999-09-20 18:45:53 +0000 | [diff] [blame] | 171 | + (WORD)&((STACK16FRAME*)0)->bp; |
Alexandre Julliard | 3fa613c | 2002-08-31 18:47:00 +0000 | [diff] [blame] | 172 | context.Eax = timer; |
Ulrich Weigand | 974fd10 | 1999-09-20 18:45:53 +0000 | [diff] [blame] | 173 | |
Alexandre Julliard | e296bf3 | 2000-11-29 19:39:30 +0000 | [diff] [blame] | 174 | wine_call_to_16_regs_short( &context, 0 ); |
Ulrich Weigand | 974fd10 | 1999-09-20 18:45:53 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Patrik Stridvall | 2b3aa61 | 2000-12-01 23:58:28 +0000 | [diff] [blame] | 177 | /**********************************************************************/ |
| 178 | |
Ulrich Weigand | 974fd10 | 1999-09-20 18:45:53 +0000 | [diff] [blame] | 179 | WORD WINAPI WIN16_CreateSystemTimer( WORD rate, FARPROC16 proc ) |
| 180 | { |
Alexandre Julliard | 88ff58e | 2001-12-17 22:13:31 +0000 | [diff] [blame] | 181 | WORD ret = CreateSystemTimer( rate, call_timer_proc16 ); |
| 182 | if (ret) SYS_Timers[ret - 1].callback16 = proc; |
| 183 | return ret; |
Ulrich Weigand | 974fd10 | 1999-09-20 18:45:53 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 186 | |
| 187 | /*********************************************************************** |
| 188 | * KillSystemTimer (SYSTEM.3) |
| 189 | * |
| 190 | * Note: do not confuse this function with USER.182 |
| 191 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 192 | WORD WINAPI SYSTEM_KillSystemTimer( WORD timer ) |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 193 | { |
Vincent BĂ©ron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 194 | if ( !timer || timer > NB_SYS_TIMERS || !SYS_Timers[timer-1].callback ) |
Ulrich Weigand | 974fd10 | 1999-09-20 18:45:53 +0000 | [diff] [blame] | 195 | return timer; /* Error */ |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 196 | SYS_Timers[timer-1].callback = NULL; |
Ulrich Weigand | 06bcab8 | 1999-04-11 15:04:00 +0000 | [diff] [blame] | 197 | if (!--SYS_NbTimers) SYSTEM_StopTicks(); |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | |
| 202 | /*********************************************************************** |
| 203 | * EnableSystemTimers (SYSTEM.4) |
| 204 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 205 | void WINAPI EnableSystemTimers16(void) |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 206 | { |
Alexandre Julliard | ac0e137 | 2002-03-23 18:48:53 +0000 | [diff] [blame] | 207 | SYS_timers_disabled = 0; |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | |
| 211 | /*********************************************************************** |
| 212 | * DisableSystemTimers (SYSTEM.5) |
| 213 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 214 | void WINAPI DisableSystemTimers16(void) |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 215 | { |
Alexandre Julliard | ac0e137 | 2002-03-23 18:48:53 +0000 | [diff] [blame] | 216 | SYS_timers_disabled = 1; |
Alexandre Julliard | 641ee76 | 1997-08-04 16:34:36 +0000 | [diff] [blame] | 217 | } |
Alexandre Julliard | 4220b29 | 1999-07-11 17:20:01 +0000 | [diff] [blame] | 218 | |
| 219 | |
| 220 | /*********************************************************************** |
Patrik Stridvall | 01d5e5b | 2001-07-02 19:59:40 +0000 | [diff] [blame] | 221 | * Get80x87SaveSize (SYSTEM.7) |
Alexandre Julliard | 4220b29 | 1999-07-11 17:20:01 +0000 | [diff] [blame] | 222 | */ |
Patrik Stridvall | b8684a2 | 1999-07-31 17:32:05 +0000 | [diff] [blame] | 223 | WORD WINAPI Get80x87SaveSize16(void) |
Alexandre Julliard | 4220b29 | 1999-07-11 17:20:01 +0000 | [diff] [blame] | 224 | { |
| 225 | return 94; |
| 226 | } |
| 227 | |
| 228 | |
| 229 | /*********************************************************************** |
Patrik Stridvall | 01d5e5b | 2001-07-02 19:59:40 +0000 | [diff] [blame] | 230 | * Save80x87State (SYSTEM.8) |
Alexandre Julliard | 4220b29 | 1999-07-11 17:20:01 +0000 | [diff] [blame] | 231 | */ |
Patrik Stridvall | b8684a2 | 1999-07-31 17:32:05 +0000 | [diff] [blame] | 232 | void WINAPI Save80x87State16( char *ptr ) |
Alexandre Julliard | 4220b29 | 1999-07-11 17:20:01 +0000 | [diff] [blame] | 233 | { |
| 234 | #ifdef __i386__ |
| 235 | __asm__(".byte 0x66; fsave %0; fwait" : "=m" (ptr) ); |
| 236 | #endif |
| 237 | } |
| 238 | |
| 239 | |
| 240 | /*********************************************************************** |
Patrik Stridvall | 01d5e5b | 2001-07-02 19:59:40 +0000 | [diff] [blame] | 241 | * Restore80x87State (SYSTEM.9) |
Alexandre Julliard | 4220b29 | 1999-07-11 17:20:01 +0000 | [diff] [blame] | 242 | */ |
Patrik Stridvall | b8684a2 | 1999-07-31 17:32:05 +0000 | [diff] [blame] | 243 | void WINAPI Restore80x87State16( const char *ptr ) |
Alexandre Julliard | 4220b29 | 1999-07-11 17:20:01 +0000 | [diff] [blame] | 244 | { |
| 245 | #ifdef __i386__ |
| 246 | __asm__(".byte 0x66; frstor %0" : : "m" (ptr) ); |
| 247 | #endif |
| 248 | } |