From 974fd10c6940995332a6d4d9c5332f28ecc405fc Mon Sep 17 00:00:00 2001
From: Ulrich Weigand <weigand@informatik.uni-erlangen.de>
Date: Mon, 20 Sep 1999 18:45:53 +0000
Subject: [PATCH] System timer proc thunk creation moved out of if1632/thunk.c.

---
 misc/system.c | 32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)

diff --git a/misc/system.c b/misc/system.c
index 2bdd04962b2..93026714daa 100644
--- a/misc/system.c
+++ b/misc/system.c
@@ -7,6 +7,9 @@
 #include "wine/winbase16.h"
 #include "wine/winuser16.h"
 #include "services.h"
+#include "callback.h"
+#include "stackframe.h"
+#include "builtin16.h"
 #include "debugtools.h"
 
 DEFAULT_DEBUG_CHANNEL(system)
@@ -119,6 +122,29 @@ WORD WINAPI CreateSystemTimer( WORD rate, SYSTEMTIMERPROC callback )
     return 0;
 }
 
+static void SYSTEM_CallSystemTimerProc( FARPROC16 proc, WORD timer )
+{
+    CONTEXT86 context;
+    memset( &context, '\0', sizeof(context) );
+
+    CS_reg( &context )  = SELECTOROF( proc );
+    EIP_reg( &context ) = OFFSETOF( proc );
+    EBP_reg( &context ) = OFFSETOF( NtCurrentTeb()->cur_stack )
+                          + (WORD)&((STACK16FRAME*)0)->bp;
+
+    AX_reg( &context ) = timer;
+
+    CallTo16RegisterShort( &context, 0 );
+}
+
+WORD WINAPI WIN16_CreateSystemTimer( WORD rate, FARPROC16 proc )
+{
+    FARPROC thunk = THUNK_Alloc( proc, (RELAY)SYSTEM_CallSystemTimerProc );
+    WORD timer = CreateSystemTimer( rate, (SYSTEMTIMERPROC)thunk );
+    if (!timer) THUNK_Free( thunk );
+    return timer;
+}
+
 
 /***********************************************************************
  *           KillSystemTimer   (SYSTEM.3)
@@ -127,8 +153,12 @@ WORD WINAPI CreateSystemTimer( WORD rate, SYSTEMTIMERPROC callback )
  */
 WORD WINAPI SYSTEM_KillSystemTimer( WORD timer )
 {
-    if (!timer || (timer > NB_SYS_TIMERS)) return timer;  /* Error */
+    if ( !timer || timer > NB_SYS_TIMERS || !SYS_Timers[timer-1].callback ) 
+        return timer;  /* Error */
+
+    THUNK_Free( (FARPROC)SYS_Timers[timer-1].callback );
     SYS_Timers[timer-1].callback = NULL;
+
     if (!--SYS_NbTimers) SYSTEM_StopTicks();
     return 0;
 }
-- 
GitLab