Temporary fix: Activate 16-bit system timer callback routines, but
[wine/multimedia.git] / if1632 / thunk.c
blobfbc98ce6158a16c41ec31e5e635913eaf7882d66
1 /*
2 * Emulator thunks
4 * Copyright 1996, 1997 Alexandre Julliard
5 * Copyright 1998 Ulrich Weigand
6 */
8 #include <string.h>
9 #include "wine/winbase16.h"
10 #include "wine/winuser16.h"
11 #include "hook.h"
12 #include "callback.h"
13 #include "task.h"
14 #include "user.h"
15 #include "heap.h"
16 #include "module.h"
17 #include "process.h"
18 #include "stackframe.h"
19 #include "selectors.h"
20 #include "syslevel.h"
21 #include "task.h"
22 #include "except.h"
23 #include "win.h"
24 #include "flatthunk.h"
25 #include "mouse.h"
26 #include "keyboard.h"
27 #include "debug.h"
30 /* List of the 16-bit callback functions. This list is used */
31 /* by the build program to generate the file if1632/callto16.S */
33 /* ### start build ### */
34 extern LONG CALLBACK CallTo16_sreg_(const CONTEXT *context, INT offset);
35 extern LONG CALLBACK CallTo16_lreg_(const CONTEXT *context, INT offset);
36 extern WORD CALLBACK CallTo16_word_ (FARPROC16);
37 extern LONG CALLBACK CallTo16_long_ (FARPROC16);
38 extern WORD CALLBACK CallTo16_word_w (FARPROC16,WORD);
39 extern WORD CALLBACK CallTo16_word_l (FARPROC16,LONG);
40 extern LONG CALLBACK CallTo16_long_l (FARPROC16,LONG);
41 extern WORD CALLBACK CallTo16_word_ww (FARPROC16,WORD,WORD);
42 extern WORD CALLBACK CallTo16_word_wl (FARPROC16,WORD,LONG);
43 extern WORD CALLBACK CallTo16_word_ll (FARPROC16,LONG,LONG);
44 extern LONG CALLBACK CallTo16_long_ll (FARPROC16,LONG,LONG);
45 extern WORD CALLBACK CallTo16_word_www (FARPROC16,WORD,WORD,WORD);
46 extern WORD CALLBACK CallTo16_word_wwl (FARPROC16,WORD,WORD,LONG);
47 extern WORD CALLBACK CallTo16_word_wlw (FARPROC16,WORD,LONG,WORD);
48 extern LONG CALLBACK CallTo16_long_wwl (FARPROC16,WORD,WORD,LONG);
49 extern LONG CALLBACK CallTo16_long_lll (FARPROC16,LONG,LONG,LONG);
50 extern WORD CALLBACK CallTo16_word_llwl (FARPROC16,LONG,LONG,WORD,LONG);
51 extern WORD CALLBACK CallTo16_word_lwll (FARPROC16,LONG,WORD,LONG,LONG);
52 extern WORD CALLBACK CallTo16_word_lwww (FARPROC16,LONG,WORD,WORD,WORD);
53 extern WORD CALLBACK CallTo16_word_wlww (FARPROC16,WORD,LONG,WORD,WORD);
54 extern WORD CALLBACK CallTo16_word_wwll (FARPROC16,WORD,WORD,LONG,LONG);
55 extern WORD CALLBACK CallTo16_word_wwwl (FARPROC16,WORD,WORD,WORD,LONG);
56 extern LONG CALLBACK CallTo16_long_wwwl (FARPROC16,WORD,WORD,WORD,LONG);
57 extern WORD CALLBACK CallTo16_word_llll (FARPROC16,LONG,LONG,LONG,LONG);
58 extern LONG CALLBACK CallTo16_long_llll (FARPROC16,LONG,LONG,LONG,LONG);
59 extern WORD CALLBACK CallTo16_word_wllwl(FARPROC16,WORD,LONG,LONG,WORD,LONG);
60 extern WORD CALLBACK CallTo16_word_lwwww(FARPROC16,LONG,WORD,WORD,WORD,WORD);
61 extern LONG CALLBACK CallTo16_long_lwwll(FARPROC16,LONG,WORD,WORD,LONG,LONG);
62 extern WORD CALLBACK CallTo16_word_wwlll(FARPROC16,WORD,WORD,LONG,LONG,LONG);
63 extern WORD CALLBACK CallTo16_word_wwwww(FARPROC16,WORD,WORD,WORD,WORD,WORD);
64 extern WORD CALLBACK CallTo16_word_lwlll(FARPROC16,LONG,WORD,LONG,LONG,LONG);
65 extern LONG CALLBACK CallTo16_long_lwlll(FARPROC16,LONG,WORD,LONG,LONG,LONG);
66 extern LONG CALLBACK CallTo16_long_lllll(FARPROC16,LONG,LONG,LONG,LONG,LONG);
67 extern LONG CALLBACK CallTo16_long_llllll(FARPROC16,LONG,LONG,LONG,LONG,LONG,
68 LONG);
69 extern LONG CALLBACK CallTo16_long_lllllll(FARPROC16,LONG,LONG,LONG,LONG,LONG,
70 LONG,LONG);
71 extern WORD CALLBACK CallTo16_word_llwwlll(FARPROC16,LONG,LONG,WORD,WORD,LONG,
72 LONG,LONG);
73 extern LONG CALLBACK CallTo16_word_lwwlllll(FARPROC16,LONG,WORD,WORD,LONG,LONG,
74 LONG,LONG,LONG);
75 extern LONG CALLBACK CallTo16_long_llllllll(FARPROC16,LONG,LONG,LONG,LONG,LONG,
76 LONG,LONG,LONG);
77 extern LONG CALLBACK CallTo16_long_lllllllll(FARPROC16,LONG,LONG,LONG,LONG,
78 LONG,LONG,LONG,LONG,LONG);
79 extern LONG CALLBACK CallTo16_long_llllllllll(FARPROC16,LONG,LONG,LONG,LONG,
80 LONG,LONG,LONG,LONG,LONG,LONG);
81 extern LONG CALLBACK CallTo16_long_lllllllllll(FARPROC16,LONG,LONG,LONG,LONG,
82 LONG,LONG,LONG,LONG,LONG,LONG,
83 LONG);
84 extern LONG CALLBACK CallTo16_long_llllllllllll(FARPROC16,LONG,LONG,LONG,LONG,
85 LONG,LONG,LONG,LONG,LONG,LONG,
86 LONG,LONG);
87 extern LONG CALLBACK CallTo16_long_lwwllwlllllw(FARPROC16,LONG,WORD,WORD,LONG,
88 LONG,WORD,LONG,LONG,LONG,LONG,
89 LONG,WORD);
90 extern LONG CALLBACK CallTo16_long_lllllllllllll(FARPROC16,LONG,LONG,LONG,LONG,
91 LONG,LONG,LONG,LONG,LONG,LONG,
92 LONG,LONG,LONG);
93 extern LONG CALLBACK CallTo16_long_llllllllllllll(FARPROC16,LONG,LONG,LONG,
94 LONG,LONG,LONG,LONG,LONG,
95 LONG,LONG,LONG,LONG,LONG,
96 LONG);
97 extern LONG CALLBACK CallTo16_word_lwwwwlwwwwllll(FARPROC16,LONG,WORD,WORD,
98 WORD,WORD,LONG,WORD,WORD,
99 WORD,WORD,LONG,LONG,LONG,
100 LONG);
101 extern LONG CALLBACK CallTo16_long_lllllllllllllll(FARPROC16,LONG,LONG,LONG,
102 LONG,LONG,LONG,LONG,LONG,
103 LONG,LONG,LONG,LONG,LONG,
104 LONG,LONG);
105 extern LONG CALLBACK CallTo16_long_llllllllllllllll(FARPROC16,LONG,LONG,LONG,
106 LONG,LONG,LONG,LONG,LONG,
107 LONG,LONG,LONG,LONG,LONG,
108 LONG,LONG,LONG);
109 /* ### stop build ### */
112 typedef void (*RELAY)();
114 #pragma pack(1)
116 typedef struct tagTHUNK
118 BYTE popl_eax; /* 0x58 popl %eax (return address)*/
119 BYTE pushl_func; /* 0x68 pushl $proc */
120 FARPROC proc WINE_PACKED;
121 BYTE pushl_eax; /* 0x50 pushl %eax */
122 BYTE jmp; /* 0xe9 jmp relay (relative jump)*/
123 RELAY relay WINE_PACKED;
124 struct tagTHUNK *next WINE_PACKED;
125 } THUNK;
127 #pragma pack(4)
129 #define DECL_THUNK(name,proc,relay) \
130 THUNK name = { 0x58, 0x68, (FARPROC)(proc), 0x50, 0xe9, \
131 (RELAY)((char *)(relay) - (char *)(&(name).next)), NULL }
134 static THUNK *firstThunk = NULL;
136 static LRESULT WINAPI THUNK_CallWndProc16( WNDPROC16 proc, HWND16 hwnd,
137 UINT16 msg, WPARAM16 wParam,
138 LPARAM lParam );
139 static BOOL WINAPI THUNK_CallTaskReschedule(void);
140 static BOOL WINAPI THUNK_WOWCallback16Ex( FARPROC16,DWORD,DWORD,
141 LPVOID,LPDWORD );
143 /* TASK_Reschedule() 16-bit entry point */
144 static FARPROC16 TASK_RescheduleProc;
146 static BOOL THUNK_ThunkletInit( void );
148 extern void CallFrom16_p_long_wwwll(void);
150 /* Callbacks function table for the emulator */
151 static const CALLBACKS_TABLE CALLBACK_EmulatorTable =
153 (void *)CallTo16_sreg_, /* CallRegisterShortProc */
154 (void *)CallTo16_lreg_, /* CallRegisterLongProc */
155 THUNK_CallTaskReschedule, /* CallTaskRescheduleProc */
156 CallFrom16_p_long_wwwll, /* CallFrom16WndProc */
157 THUNK_CallWndProc16, /* CallWndProc */
158 (void *)CallTo16_long_lwwll, /* CallDriverProc */
159 (void *)CallTo16_word_wwlll, /* CallDriverCallback */
160 (void *)CallTo16_word_wwlll, /* CallTimeFuncProc */
161 (void *)CallTo16_word_w, /* CallWindowsExitProc */
162 (void *)CallTo16_word_lwww, /* CallWordBreakProc */
163 (void *)CallTo16_word_ww, /* CallBootAppProc */
164 (void *)CallTo16_word_www, /* CallLoadAppSegProc */
165 (void *)CallTo16_word_www, /* CallLocalNotifyFunc */
166 (void *)CallTo16_word_www, /* CallResourceHandlerProc */
167 (void *)CallTo16_long_l, /* CallWOWCallbackProc */
168 THUNK_WOWCallback16Ex, /* CallWOWCallback16Ex */
169 (void *)CallTo16_long_l, /* CallASPIPostProc */
170 (void *)CallTo16_word_lwll, /* CallDrvControlProc */
171 (void *)CallTo16_word_lwlll, /* CallDrvEnableProc */
172 (void *)CallTo16_word_llll, /* CallDrvEnumDFontsProc */
173 (void *)CallTo16_word_lwll, /* CallDrvEnumObjProc */
174 (void *)CallTo16_word_lwwlllll, /* CallDrvOutputProc */
175 (void *)CallTo16_long_lwlll, /* CallDrvRealizeProc */
176 (void *)CallTo16_word_lwwwwlwwwwllll, /* CallDrvStretchBltProc */
177 (void *)CallTo16_long_lwwllwlllllw, /* CallDrvExtTextOutProc */
178 (void *)CallTo16_word_llwwlll, /* CallDrvGetCharWidth */
179 (void *)CallTo16_word_ww /* CallDrvAbortProc */
183 /***********************************************************************
184 * THUNK_Init
186 BOOL THUNK_Init(void)
188 /* Set the window proc calling functions */
189 Callbacks = &CALLBACK_EmulatorTable;
190 /* Get the 16-bit reschedule function pointer */
191 TASK_RescheduleProc = MODULE_GetWndProcEntry16( "TASK_Reschedule" );
192 /* Initialize Thunklets */
193 return THUNK_ThunkletInit();
196 /***********************************************************************
197 * THUNK_Alloc
199 static THUNK *THUNK_Alloc( FARPROC func, RELAY relay )
201 THUNK *thunk = HeapAlloc( GetProcessHeap(), 0, sizeof(*thunk) );
202 if (thunk)
204 thunk->popl_eax = 0x58;
205 thunk->pushl_func = 0x68;
206 thunk->proc = func;
207 thunk->pushl_eax = 0x50;
208 thunk->jmp = 0xe9;
209 thunk->relay = (RELAY)((char *)relay - (char *)(&thunk->next));
210 thunk->next = firstThunk;
211 firstThunk = thunk;
213 return thunk;
217 /***********************************************************************
218 * THUNK_Find
220 static THUNK *THUNK_Find( FARPROC func )
222 THUNK *thunk = firstThunk;
223 while (thunk && (thunk->proc != func)) thunk = thunk->next;
224 return thunk;
228 /***********************************************************************
229 * THUNK_Free
231 static void THUNK_Free( THUNK *thunk )
233 if (HEAP_IsInsideHeap( GetProcessHeap(), 0, thunk ))
235 THUNK **prev = &firstThunk;
236 while (*prev && (*prev != thunk)) prev = &(*prev)->next;
237 if (*prev)
239 *prev = thunk->next;
240 HeapFree( GetProcessHeap(), 0, thunk );
241 return;
244 ERR(thunk, "invalid thunk addr %p\n", thunk );
248 /***********************************************************************
249 * THUNK_CallWndProc16
251 * Call a 16-bit window procedure
253 static LRESULT WINAPI THUNK_CallWndProc16( WNDPROC16 proc, HWND16 hwnd,
254 UINT16 msg, WPARAM16 wParam,
255 LPARAM lParam )
257 CONTEXT context;
258 LRESULT ret;
259 WORD *args;
260 WND *wndPtr = WIN_FindWndPtr( hwnd );
261 DWORD offset = 0;
262 THDB *thdb = THREAD_Current();
264 /* Window procedures want ax = hInstance, ds = es = ss */
266 memset(&context, '\0', sizeof(context));
267 DS_reg(&context) = SELECTOROF(thdb->cur_stack);
268 ES_reg(&context) = DS_reg(&context);
269 EAX_reg(&context) = wndPtr ? wndPtr->hInstance : DS_reg(&context);
270 CS_reg(&context) = SELECTOROF(proc);
271 EIP_reg(&context) = OFFSETOF(proc);
272 EBP_reg(&context) = OFFSETOF(thdb->cur_stack)
273 + (WORD)&((STACK16FRAME*)0)->bp;
275 if (lParam)
277 /* Some programs (eg. the "Undocumented Windows" examples, JWP) only
278 work if structures passed in lParam are placed in the stack/data
279 segment. Programmers easily make the mistake of converting lParam
280 to a near rather than a far pointer, since Windows apparently
281 allows this. We copy the structures to the 16 bit stack; this is
282 ugly but makes these programs work. */
283 switch (msg)
285 case WM_CREATE:
286 case WM_NCCREATE:
287 offset = sizeof(CREATESTRUCT16); break;
288 case WM_DRAWITEM:
289 offset = sizeof(DRAWITEMSTRUCT16); break;
290 case WM_COMPAREITEM:
291 offset = sizeof(COMPAREITEMSTRUCT16); break;
293 if (offset)
295 void *s = PTR_SEG_TO_LIN(lParam);
296 lParam = STACK16_PUSH( thdb, offset );
297 memcpy( PTR_SEG_TO_LIN(lParam), s, offset );
301 args = (WORD *)THREAD_STACK16(thdb) - 5;
302 args[0] = LOWORD(lParam);
303 args[1] = HIWORD(lParam);
304 args[2] = wParam;
305 args[3] = msg;
306 args[4] = hwnd;
308 ret = CallTo16_sreg_( &context, 5 * sizeof(WORD) );
309 if (offset) STACK16_POP( thdb, offset );
310 return ret;
314 /***********************************************************************
315 * THUNK_CallTaskReschedule
317 static BOOL WINAPI THUNK_CallTaskReschedule(void)
319 return CallTo16_word_(TASK_RescheduleProc);
323 /***********************************************************************
324 * THUNK_EnumObjects16 (GDI.71)
326 INT16 WINAPI THUNK_EnumObjects16( HDC16 hdc, INT16 nObjType,
327 GOBJENUMPROC16 func, LPARAM lParam )
329 DECL_THUNK( thunk, func, CallTo16_word_ll );
330 return EnumObjects16( hdc, nObjType, (GOBJENUMPROC16)&thunk, lParam );
334 /*************************************************************************
335 * THUNK_EnumFonts16 (GDI.70)
337 INT16 WINAPI THUNK_EnumFonts16( HDC16 hdc, LPCSTR lpFaceName,
338 FONTENUMPROC16 func, LPARAM lParam )
340 DECL_THUNK( thunk, func, CallTo16_word_llwl );
341 return EnumFonts16( hdc, lpFaceName, (FONTENUMPROC16)&thunk, lParam );
344 /******************************************************************
345 * THUNK_EnumMetaFile16 (GDI.175)
347 BOOL16 WINAPI THUNK_EnumMetaFile16( HDC16 hdc, HMETAFILE16 hmf,
348 MFENUMPROC16 func, LPARAM lParam )
350 DECL_THUNK( thunk, func, CallTo16_word_wllwl );
351 return EnumMetaFile16( hdc, hmf, (MFENUMPROC16)&thunk, lParam );
355 /*************************************************************************
356 * THUNK_EnumFontFamilies16 (GDI.330)
358 INT16 WINAPI THUNK_EnumFontFamilies16( HDC16 hdc, LPCSTR lpszFamily,
359 FONTENUMPROC16 func, LPARAM lParam )
361 DECL_THUNK( thunk, func, CallTo16_word_llwl );
362 return EnumFontFamilies16(hdc, lpszFamily, (FONTENUMPROC16)&thunk, lParam);
366 /*************************************************************************
367 * THUNK_EnumFontFamiliesEx16 (GDI.613)
369 INT16 WINAPI THUNK_EnumFontFamiliesEx16( HDC16 hdc, LPLOGFONT16 lpLF,
370 FONTENUMPROCEX16 func, LPARAM lParam,
371 DWORD reserved )
373 DECL_THUNK( thunk, func, CallTo16_word_llwl );
374 return EnumFontFamiliesEx16( hdc, lpLF, (FONTENUMPROCEX16)&thunk,
375 lParam, reserved );
379 /**********************************************************************
380 * THUNK_LineDDA16 (GDI.100)
382 void WINAPI THUNK_LineDDA16( INT16 nXStart, INT16 nYStart, INT16 nXEnd,
383 INT16 nYEnd, LINEDDAPROC16 func, LPARAM lParam )
385 DECL_THUNK( thunk, func, CallTo16_word_wwl );
386 LineDDA16( nXStart, nYStart, nXEnd, nYEnd, (LINEDDAPROC16)&thunk, lParam );
390 /*******************************************************************
391 * THUNK_EnumWindows16 (USER.54)
393 BOOL16 WINAPI THUNK_EnumWindows16( WNDENUMPROC16 func, LPARAM lParam )
395 DECL_THUNK( thunk, func, CallTo16_word_wl );
396 return EnumWindows16( (WNDENUMPROC16)&thunk, lParam );
400 /**********************************************************************
401 * THUNK_EnumChildWindows16 (USER.55)
403 BOOL16 WINAPI THUNK_EnumChildWindows16( HWND16 parent, WNDENUMPROC16 func,
404 LPARAM lParam )
406 DECL_THUNK( thunk, func, CallTo16_word_wl );
407 return EnumChildWindows16( parent, (WNDENUMPROC16)&thunk, lParam );
411 /**********************************************************************
412 * THUNK_EnumTaskWindows16 (USER.225)
414 BOOL16 WINAPI THUNK_EnumTaskWindows16( HTASK16 hTask, WNDENUMPROC16 func,
415 LPARAM lParam )
417 DECL_THUNK( thunk, func, CallTo16_word_wl );
418 return EnumTaskWindows16( hTask, (WNDENUMPROC16)&thunk, lParam );
422 /***********************************************************************
423 * THUNK_EnumProps16 (USER.27)
425 INT16 WINAPI THUNK_EnumProps16( HWND16 hwnd, PROPENUMPROC16 func )
427 DECL_THUNK( thunk, func, CallTo16_word_wlw );
428 return EnumProps16( hwnd, (PROPENUMPROC16)&thunk );
432 /***********************************************************************
433 * THUNK_GrayString16 (USER.185)
435 BOOL16 WINAPI THUNK_GrayString16( HDC16 hdc, HBRUSH16 hbr,
436 GRAYSTRINGPROC16 func, LPARAM lParam,
437 INT16 cch, INT16 x, INT16 y,
438 INT16 cx, INT16 cy )
440 DECL_THUNK( thunk, func, CallTo16_word_wlw );
441 if (!func)
442 return GrayString16( hdc, hbr, NULL, lParam, cch, x, y, cx, cy );
443 else
444 return GrayString16( hdc, hbr, (GRAYSTRINGPROC16)&thunk, lParam, cch,
445 x, y, cx, cy );
449 /***********************************************************************
450 * THUNK_SetWindowsHook16 (USER.121)
452 FARPROC16 WINAPI THUNK_SetWindowsHook16( INT16 id, HOOKPROC16 proc )
454 HINSTANCE16 hInst = FarGetOwner16( HIWORD(proc) );
455 HTASK16 hTask = (id == WH_MSGFILTER) ? GetCurrentTask() : 0;
456 THUNK *thunk = THUNK_Alloc( (FARPROC16)proc, (RELAY)CallTo16_long_wwl );
457 if (!thunk) return 0;
458 return (FARPROC16)SetWindowsHookEx16( id, (HOOKPROC16)thunk, hInst, hTask);
462 /***********************************************************************
463 * THUNK_UnhookWindowsHook16 (USER.234)
465 BOOL16 WINAPI THUNK_UnhookWindowsHook16( INT16 id, HOOKPROC16 proc )
467 BOOL16 ret;
468 THUNK *thunk = THUNK_Find( (FARPROC16)proc );
469 if (!thunk) return FALSE;
470 ret = UnhookWindowsHook16( id, (HOOKPROC16)thunk );
471 THUNK_Free( thunk );
472 return ret;
476 /***********************************************************************
477 * THUNK_SetWindowsHookEx16 (USER.291)
479 HHOOK WINAPI THUNK_SetWindowsHookEx16( INT16 id, HOOKPROC16 proc,
480 HINSTANCE16 hInst, HTASK16 hTask )
482 THUNK *thunk = THUNK_Alloc( (FARPROC16)proc, (RELAY)CallTo16_long_wwl );
483 if (!thunk) return 0;
484 return SetWindowsHookEx16( id, (HOOKPROC16)thunk, hInst, hTask );
488 /***********************************************************************
489 * THUNK_UnhookWindowHookEx16 (USER.292)
491 BOOL16 WINAPI THUNK_UnhookWindowsHookEx16( HHOOK hhook )
493 THUNK *thunk = (THUNK *)HOOK_GetProc16( hhook );
494 BOOL16 ret = UnhookWindowsHookEx16( hhook );
495 if (thunk) THUNK_Free( thunk );
496 return ret;
501 static FARPROC16 defDCHookProc = NULL;
503 /***********************************************************************
504 * THUNK_SetDCHook (GDI.190)
506 BOOL16 WINAPI THUNK_SetDCHook( HDC16 hdc, FARPROC16 proc, DWORD dwHookData )
508 THUNK *thunk, *oldThunk;
510 if (!defDCHookProc) /* Get DCHook Win16 entry point */
511 defDCHookProc = NE_GetEntryPoint( GetModuleHandle16("USER"), 362 );
513 if (proc != defDCHookProc)
515 thunk = THUNK_Alloc( proc, (RELAY)CallTo16_word_wwll );
516 if (!thunk) return FALSE;
518 else thunk = (THUNK *)DCHook16;
520 /* Free the previous thunk */
521 GetDCHook( hdc, (FARPROC16 *)&oldThunk );
522 if (oldThunk && (oldThunk != (THUNK *)DCHook16)) THUNK_Free( oldThunk );
524 return SetDCHook( hdc, (FARPROC16)thunk, dwHookData );
528 /***********************************************************************
529 * THUNK_GetDCHook (GDI.191)
531 DWORD WINAPI THUNK_GetDCHook( HDC16 hdc, FARPROC16 *phookProc )
533 THUNK *thunk = NULL;
534 DWORD ret = GetDCHook( hdc, (FARPROC16 *)&thunk );
535 if (thunk)
537 if (thunk == (THUNK *)DCHook16)
539 if (!defDCHookProc) /* Get DCHook Win16 entry point */
540 defDCHookProc = NE_GetEntryPoint(GetModuleHandle16("USER"),362);
541 *phookProc = defDCHookProc;
543 else *phookProc = thunk->proc;
545 return ret;
549 /***********************************************************************
550 * THUNK_SetTaskSignalProc (KERNEL.38)
552 FARPROC16 WINAPI THUNK_SetTaskSignalProc( HTASK16 hTask, FARPROC16 proc )
554 static FARPROC16 defSignalProc16 = NULL;
556 THUNK *thunk = NULL;
558 if( !defSignalProc16 )
559 defSignalProc16 = NE_GetEntryPoint(GetModuleHandle16("USER"), 314 );
561 if( proc == defSignalProc16 )
562 thunk = (THUNK*)SetTaskSignalProc( hTask, (FARPROC16)&USER_SignalProc );
563 else
565 thunk = THUNK_Alloc( proc, (RELAY)CallTo16_word_wwwww );
566 if( !thunk ) return FALSE;
567 thunk = (THUNK*)SetTaskSignalProc( hTask, (FARPROC16)thunk );
570 if( thunk != (THUNK*)USER_SignalProc )
572 if( !thunk ) return NULL;
574 proc = thunk->proc;
575 THUNK_Free( thunk );
576 return proc;
578 return defSignalProc16;
581 /***********************************************************************
582 * THUNK_CreateThread16 (KERNEL.441)
584 static DWORD CALLBACK THUNK_StartThread16( LPVOID threadArgs )
586 FARPROC16 start = ((FARPROC16 *)threadArgs)[0];
587 DWORD param = ((DWORD *)threadArgs)[1];
588 HeapFree( GetProcessHeap(), 0, threadArgs );
590 return CallTo16_long_l( start, param );
592 HANDLE WINAPI THUNK_CreateThread16( SECURITY_ATTRIBUTES *sa, DWORD stack,
593 FARPROC16 start, SEGPTR param,
594 DWORD flags, LPDWORD id )
596 DWORD *threadArgs = HeapAlloc( GetProcessHeap(), 0, 2*sizeof(DWORD) );
597 if (!threadArgs) return INVALID_HANDLE_VALUE;
598 threadArgs[0] = (DWORD)start;
599 threadArgs[1] = (DWORD)param;
601 return CreateThread( sa, stack, THUNK_StartThread16, threadArgs, flags, id );
604 /***********************************************************************
605 * THUNK_WOWCallback16Ex (WOW32.3)(KERNEL32.55)
606 * Generic thunking routine to call 16 bit functions from 32bit code.
608 * RETURNS
609 * TRUE if the call was done
611 static BOOL WINAPI THUNK_WOWCallback16Ex(
612 FARPROC16 proc, /* [in] 16bit function to call */
613 DWORD dwFlags, /* [in] flags (WCB_*) */
614 DWORD cbArgs, /* [in] number of arguments */
615 LPVOID xargs, /* [in/out] arguments */
616 LPDWORD pdwret /* [out] return value of the 16bit call */
618 LPDWORD args = (LPDWORD)xargs;
619 DWORD ret,i;
621 TRACE(relay,"(%p,0x%08lx,%ld,%p,%p)\n",
622 proc,dwFlags,cbArgs,xargs,pdwret
624 if (dwFlags == WCB16_CDECL) {
625 /* swap the arguments */
626 args = HeapAlloc(GetProcessHeap(),0,cbArgs*sizeof(DWORD));
627 for (i=0;i<cbArgs;i++)
628 args[i] = ((DWORD*)xargs)[cbArgs-i-1];
630 switch (cbArgs) {
631 case 0: ret = CallTo16_long_(proc);break;
632 case 1: ret = CallTo16_long_l(proc,args[0]);break;
633 case 2: ret = CallTo16_long_ll(proc,args[0],args[1]);break;
634 case 3: ret = CallTo16_long_lll(proc,args[0],args[1],args[2]);break;
635 case 4: ret = CallTo16_long_llll(proc,args[0],args[1],args[2],args[3]);
636 break;
637 case 5: ret = CallTo16_long_lllll(proc,args[0],args[1],args[2],args[3],
638 args[4]
640 break;
641 case 6: ret = CallTo16_long_llllll(proc,args[0],args[1],args[2],args[3],
642 args[4],args[5]
644 break;
645 case 7: ret = CallTo16_long_lllllll(proc,args[0],args[1],args[2],args[3],
646 args[4],args[5],args[6]
648 break;
649 case 8: ret = CallTo16_long_llllllll(proc,args[0],args[1],args[2],args[3],
650 args[4],args[5],args[6],args[7]
652 break;
653 case 9: ret = CallTo16_long_lllllllll(proc,args[0],args[1],args[2],args[3],
654 args[4],args[5],args[6],args[7],args[8]
656 break;
657 case 10:ret = CallTo16_long_llllllllll(proc,args[0],args[1],args[2],args[3],
658 args[4],args[5],args[6],args[7],args[8],args[9]
660 break;
661 case 11:ret = CallTo16_long_lllllllllll(proc,args[0],args[1],args[2],
662 args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10]
664 break;
665 case 12:ret = CallTo16_long_llllllllllll(proc,args[0],args[1],args[2],
666 args[3],args[4],args[5],args[6],args[7],args[8],args[9],
667 args[10],args[11]
669 break;
670 case 13:ret = CallTo16_long_lllllllllllll(proc,args[0],args[1],args[2],
671 args[3],args[4],args[5],args[6],args[7],args[8],args[9],
672 args[10],args[11],args[12]
674 break;
675 case 14:ret = CallTo16_long_llllllllllllll(proc,args[0],args[1],args[2],
676 args[3],args[4],args[5],args[6],args[7],args[8],args[9],
677 args[10],args[11],args[12],args[13]
679 break;
680 case 15:ret = CallTo16_long_lllllllllllllll(proc,args[0],args[1],args[2],
681 args[3],args[4],args[5],args[6],args[7],args[8],args[9],
682 args[10],args[11],args[12],args[13],args[14]
684 break;
685 case 16:ret = CallTo16_long_llllllllllllllll(proc,args[0],args[1],args[2],
686 args[3],args[4],args[5],args[6],args[7],args[8],args[9],
687 args[10],args[11],args[12],args[13],args[14],args[15]
689 break;
690 default:
691 ERR(thunk,"%ld arguments not supported.\n",cbArgs);
692 if (dwFlags == WCB16_CDECL)
693 HeapFree(GetProcessHeap(),0,args);
694 return FALSE;
696 if (dwFlags == WCB16_CDECL)
697 HeapFree(GetProcessHeap(),0,args);
698 if (pdwret)
699 *pdwret = ret;
700 return TRUE;
703 /***********************************************************************
704 * THUNK_MOUSE_Enable (MOUSE.2)
706 static VOID WINAPI THUNK_CallMouseEventProc( FARPROC16 proc,
707 DWORD dwFlags, DWORD dx, DWORD dy,
708 DWORD cButtons, DWORD dwExtraInfo )
710 CONTEXT context;
712 memset( &context, 0, sizeof(context) );
713 CS_reg(&context) = SELECTOROF( proc );
714 EIP_reg(&context) = OFFSETOF( proc );
715 AX_reg(&context) = (WORD)dwFlags;
716 BX_reg(&context) = (WORD)dx;
717 CX_reg(&context) = (WORD)dy;
718 DX_reg(&context) = (WORD)cButtons;
719 SI_reg(&context) = LOWORD( dwExtraInfo );
720 DI_reg(&context) = HIWORD( dwExtraInfo );
722 CallTo16_sreg_( &context, 0 );
724 VOID WINAPI THUNK_MOUSE_Enable( FARPROC16 proc )
726 static THUNK *lastThunk = NULL;
727 static FARPROC16 lastProc = NULL;
729 if ( lastProc != proc )
731 if ( lastThunk )
732 THUNK_Free( lastThunk );
734 if ( !proc )
735 lastThunk = NULL;
736 else
737 lastThunk = THUNK_Alloc( proc, (RELAY)THUNK_CallMouseEventProc );
739 lastProc = proc;
742 return MOUSE_Enable( (LPMOUSE_EVENT_PROC)lastThunk );
745 /***********************************************************************
746 * GetMouseEventProc (USER.337)
748 FARPROC16 WINAPI GetMouseEventProc16(void)
750 HMODULE16 hmodule = GetModuleHandle16("USER");
751 return NE_GetEntryPoint( hmodule, NE_GetOrdinal( hmodule, "mouse_event" ));
755 /***********************************************************************
756 * WIN16_mouse_event (USER.299)
758 void WINAPI WIN16_mouse_event( CONTEXT *context )
760 mouse_event( AX_reg(context), BX_reg(context), CX_reg(context),
761 DX_reg(context), MAKELONG(SI_reg(context), DI_reg(context)) );
765 /***********************************************************************
766 * THUNK_KEYBD_Enable (KEYBOARD.2)
768 static VOID WINAPI THUNK_CallKeybdEventProc( FARPROC16 proc,
769 BYTE bVk, BYTE bScan,
770 DWORD dwFlags, DWORD dwExtraInfo )
772 CONTEXT context;
774 memset( &context, 0, sizeof(context) );
775 CS_reg(&context) = SELECTOROF( proc );
776 EIP_reg(&context) = OFFSETOF( proc );
777 AH_reg(&context) = (dwFlags & KEYEVENTF_KEYUP)? 0x80 : 0;
778 AL_reg(&context) = bVk;
779 BH_reg(&context) = (dwFlags & KEYEVENTF_EXTENDEDKEY)? 1 : 0;
780 BL_reg(&context) = bScan;
781 SI_reg(&context) = LOWORD( dwExtraInfo );
782 DI_reg(&context) = HIWORD( dwExtraInfo );
784 CallTo16_sreg_( &context, 0 );
786 VOID WINAPI THUNK_KEYBOARD_Enable( FARPROC16 proc, LPBYTE lpKeyState )
788 static THUNK *lastThunk = NULL;
789 static FARPROC16 lastProc = NULL;
791 if ( lastProc != proc )
793 if ( lastThunk )
794 THUNK_Free( lastThunk );
796 if ( !proc )
797 lastThunk = NULL;
798 else
799 lastThunk = THUNK_Alloc( proc, (RELAY)THUNK_CallKeybdEventProc );
801 lastProc = proc;
804 return KEYBOARD_Enable( (LPKEYBD_EVENT_PROC)lastThunk, lpKeyState );
807 /***********************************************************************
808 * WIN16_keybd_event (USER.289)
810 void WINAPI WIN16_keybd_event( CONTEXT *context )
812 DWORD dwFlags = 0;
814 if (AH_reg(context) & 0x80) dwFlags |= KEYEVENTF_KEYUP;
815 if (BH_reg(context) & 1 ) dwFlags |= KEYEVENTF_EXTENDEDKEY;
817 keybd_event( AL_reg(context), BL_reg(context),
818 dwFlags, MAKELONG(SI_reg(context), DI_reg(context)) );
822 /***********************************************************************
823 * WIN16_CreateSystemTimer (SYSTEM.2)
825 static void THUNK_CallSystemTimerProc( FARPROC16 proc, WORD timer )
827 CONTEXT context;
828 memset( &context, '\0', sizeof(context) );
830 CS_reg( &context ) = SELECTOROF( proc );
831 IP_reg( &context ) = OFFSETOF( proc );
832 BP_reg( &context ) = OFFSETOF( THREAD_Current()->cur_stack )
833 + (WORD)&((STACK16FRAME*)0)->bp;
835 AX_reg( &context ) = timer;
837 if ( _ConfirmWin16Lock() )
839 FIXME( system, "Skipping timer %d callback because timer signal "
840 "arrived while we own the Win16Lock!\n", timer );
841 return;
844 CallTo16_sreg_( &context, 0 );
846 /* FIXME: This does not work if the signal occurs while this thread
847 is currently in 16-bit code. With the current structure
848 of the Wine thunking code, this seems to be hard to fix ... */
850 WORD WINAPI WIN16_CreateSystemTimer( WORD rate, FARPROC16 proc )
852 THUNK *thunk = THUNK_Alloc( proc, (RELAY)THUNK_CallSystemTimerProc );
853 WORD timer = CreateSystemTimer( rate, (SYSTEMTIMERPROC)thunk );
854 if (!timer) THUNK_Free( thunk );
855 return timer;
858 /***********************************************************************
859 * THUNK_InitCallout
861 void THUNK_InitCallout(void)
863 HMODULE hModule = GetModuleHandleA( "USER32" );
864 if ( hModule )
866 #define GETADDR( var, name ) \
867 *(FARPROC *)&Callout.##var = GetProcAddress( hModule, name )
869 GETADDR( PeekMessageA, "PeekMessageA" );
870 GETADDR( PeekMessageW, "PeekMessageW" );
871 GETADDR( GetMessageA, "GetMessageA" );
872 GETADDR( GetMessageW, "GetMessageW" );
873 GETADDR( SendMessageA, "SendMessageA" );
874 GETADDR( SendMessageW, "SendMessageW" );
875 GETADDR( PostMessageA, "PostMessageA" );
876 GETADDR( PostMessageW, "PostMessageW" );
877 GETADDR( PostThreadMessageA, "PostThreadMessageA" );
878 GETADDR( PostThreadMessageW, "PostThreadMessageW" );
879 GETADDR( TranslateMessage, "TranslateMessage" );
880 GETADDR( DispatchMessageW, "DispatchMessageW" );
881 GETADDR( DispatchMessageA, "DispatchMessageA" );
882 GETADDR( RedrawWindow, "RedrawWindow" );
884 #undef GETADDR
887 hModule = GetModuleHandle16( "USER" );
888 if ( hModule )
890 #define GETADDR( var, name, thk ) \
891 *(FARPROC *)&Callout.##var = (FARPROC) \
892 THUNK_Alloc( WIN32_GetProcAddress16( hModule, name ), \
893 (RELAY)CallTo16_##thk )
895 GETADDR( PeekMessage16, "PeekMessage", word_lwwww );
896 GETADDR( GetMessage16, "GetMessage", word_lwww );
897 GETADDR( SendMessage16, "SendMessage", long_wwwl );
898 GETADDR( PostMessage16, "PostMessage", word_wwwl );
899 GETADDR( PostAppMessage16, "PostAppMessage", word_wwwl );
900 GETADDR( TranslateMessage16, "TranslateMessage", word_l );
901 GETADDR( DispatchMessage16, "DispatchMessage", long_l );
902 GETADDR( RedrawWindow16, "RedrawWindow", word_wlww );
903 GETADDR( InitThreadInput16, "InitThreadInput", word_ww );
904 GETADDR( UserYield16, "UserYield", word_ );
905 GETADDR( DestroyIcon32, "DestroyIcon32", word_ww );
907 #undef GETADDR
911 /***********************************************************************
912 * 16->32 Flat Thunk routines:
915 /***********************************************************************
916 * ThunkConnect16 (KERNEL.651)
917 * Connects a 32bit and a 16bit thunkbuffer.
919 UINT WINAPI ThunkConnect16(
920 LPSTR module16, /* [in] name of win16 dll */
921 LPSTR module32, /* [in] name of win32 dll */
922 HINSTANCE16 hInst16, /* [in] hInst of win16 dll */
923 DWORD dwReason, /* [in] initialisation argument */
924 struct ThunkDataCommon *TD, /* [in/out] thunkbuffer */
925 LPSTR thunkfun32, /* [in] win32 thunkfunction */
926 WORD cs /* [in] CS of win16 dll */
928 BOOL directionSL;
930 if (!lstrncmpA(TD->magic, "SL01", 4))
932 directionSL = TRUE;
934 TRACE(thunk, "SL01 thunk %s (%lx) -> %s (%s), Reason: %ld\n",
935 module16, (DWORD)TD, module32, thunkfun32, dwReason);
937 else if (!lstrncmpA(TD->magic, "LS01", 4))
939 directionSL = FALSE;
941 TRACE(thunk, "LS01 thunk %s (%lx) <- %s (%s), Reason: %ld\n",
942 module16, (DWORD)TD, module32, thunkfun32, dwReason);
944 else
946 ERR(thunk, "Invalid magic %c%c%c%c\n",
947 TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
948 return 0;
951 switch (dwReason)
953 case DLL_PROCESS_ATTACH:
954 if (directionSL)
956 struct ThunkDataSL16 *SL16 = (struct ThunkDataSL16 *)TD;
957 struct ThunkDataSL *SL = SL16->fpData;
959 if (SL == NULL)
961 SL = HeapAlloc(GetProcessHeap(), 0, sizeof(*SL));
963 SL->common = SL16->common;
964 SL->flags1 = SL16->flags1;
965 SL->flags2 = SL16->flags2;
967 SL->apiDB = PTR_SEG_TO_LIN(SL16->apiDatabase);
968 SL->targetDB = NULL;
970 lstrcpynA(SL->pszDll16, module16, 255);
971 lstrcpynA(SL->pszDll32, module32, 255);
973 /* We should create a SEGPTR to the ThunkDataSL,
974 but since the contents are not in the original format,
975 any access to this by 16-bit code would crash anyway. */
976 SL16->spData = 0;
977 SL16->fpData = SL;
981 if (SL->flags2 & 0x80000000)
983 TRACE(thunk, "Preloading 32-bit library\n");
984 LoadLibraryA(module32);
987 else
989 /* nothing to do */
991 break;
993 case DLL_PROCESS_DETACH:
994 /* FIXME: cleanup */
995 break;
998 return 1;
1002 /***********************************************************************
1003 * C16ThkSL (KERNEL.630)
1006 void WINAPI C16ThkSL(CONTEXT *context)
1008 extern void CallFrom16_t_long_(void);
1009 LPBYTE stub = PTR_SEG_TO_LIN(EAX_reg(context)), x = stub;
1010 WORD cs, ds;
1011 GET_CS(cs);
1012 GET_DS(ds);
1014 /* We produce the following code:
1016 * mov ax, __FLATDS
1017 * mov es, ax
1018 * movzx ecx, cx
1019 * mov edx, es:[ecx + $EDX]
1020 * push bp
1021 * push edx
1022 * call __FLATCS:CallFrom16_t_long_
1025 *x++ = 0xB8; *((WORD *)x)++ = ds;
1026 *x++ = 0x8E; *x++ = 0xC0;
1027 *x++ = 0x66; *x++ = 0x0F; *x++ = 0xB7; *x++ = 0xC9;
1028 *x++ = 0x67; *x++ = 0x66; *x++ = 0x26; *x++ = 0x8B;
1029 *x++ = 0x91; *((DWORD *)x)++ = EDX_reg(context);
1031 *x++ = 0x55;
1032 *x++ = 0x66; *x++ = 0x52;
1033 *x++ = 0x66; *x++ = 0x9A; *((DWORD *)x)++ = (DWORD)CallFrom16_t_long_;
1034 *((WORD *)x)++ = cs;
1036 /* Jump to the stub code just created */
1037 IP_reg(context) = LOWORD(EAX_reg(context));
1038 CS_reg(context) = HIWORD(EAX_reg(context));
1040 /* Since C16ThkSL got called by a jmp, we need to leave the
1041 orginal return address on the stack */
1042 SP_reg(context) -= 4;
1045 /***********************************************************************
1046 * C16ThkSL01 (KERNEL.631)
1049 void WINAPI C16ThkSL01(CONTEXT *context)
1051 LPBYTE stub = PTR_SEG_TO_LIN(EAX_reg(context)), x = stub;
1053 if (stub)
1055 struct ThunkDataSL16 *SL16 = PTR_SEG_TO_LIN(EDX_reg(context));
1056 struct ThunkDataSL *td = SL16->fpData;
1058 extern void CallFrom16_t_long_(void);
1059 DWORD procAddress = (DWORD)GetProcAddress16(GetModuleHandle16("KERNEL"), 631);
1060 WORD cs;
1061 GET_CS(cs);
1063 if (!td)
1065 ERR(thunk, "ThunkConnect16 was not called!\n");
1066 return;
1069 TRACE(thunk, "Creating stub for ThunkDataSL %08lx\n", (DWORD)td);
1072 /* We produce the following code:
1074 * xor eax, eax
1075 * mov edx, $td
1076 * call C16ThkSL01
1077 * push bp
1078 * push edx
1079 * call __FLATCS:CallFrom16_t_long_
1082 *x++ = 0x66; *x++ = 0x33; *x++ = 0xC0;
1083 *x++ = 0x66; *x++ = 0xBA; *((DWORD *)x)++ = (DWORD)td;
1084 *x++ = 0x9A; *((DWORD *)x)++ = procAddress;
1086 *x++ = 0x55;
1087 *x++ = 0x66; *x++ = 0x52;
1088 *x++ = 0x66; *x++ = 0x9A; *((DWORD *)x)++ = (DWORD)CallFrom16_t_long_;
1089 *((WORD *)x)++ = cs;
1091 /* Jump to the stub code just created */
1092 IP_reg(context) = LOWORD(EAX_reg(context));
1093 CS_reg(context) = HIWORD(EAX_reg(context));
1095 /* Since C16ThkSL01 got called by a jmp, we need to leave the
1096 orginal return address on the stack */
1097 SP_reg(context) -= 4;
1099 else
1101 struct ThunkDataSL *td = (struct ThunkDataSL *)EDX_reg(context);
1102 DWORD targetNr = CX_reg(context) / 4;
1103 struct SLTargetDB *tdb;
1105 TRACE(thunk, "Process %08lx calling target %ld of ThunkDataSL %08lx\n",
1106 (DWORD)PROCESS_Current(), targetNr, (DWORD)td);
1108 for (tdb = td->targetDB; tdb; tdb = tdb->next)
1109 if (tdb->process == PROCESS_Current())
1110 break;
1112 if (!tdb)
1114 TRACE(thunk, "Loading 32-bit library %s\n", td->pszDll32);
1115 LoadLibraryA(td->pszDll32);
1117 for (tdb = td->targetDB; tdb; tdb = tdb->next)
1118 if (tdb->process == PROCESS_Current())
1119 break;
1122 if (tdb)
1124 EDX_reg(context) = tdb->targetTable[targetNr];
1126 TRACE(thunk, "Call target is %08lx\n", EDX_reg(context));
1128 else
1130 WORD *stack = PTR_SEG_OFF_TO_LIN(SS_reg(context), SP_reg(context));
1131 DX_reg(context) = HIWORD(td->apiDB[targetNr].errorReturnValue);
1132 AX_reg(context) = LOWORD(td->apiDB[targetNr].errorReturnValue);
1133 IP_reg(context) = stack[2];
1134 CS_reg(context) = stack[3];
1135 SP_reg(context) += td->apiDB[targetNr].nrArgBytes + 4;
1137 ERR(thunk, "Process %08lx did not ThunkConnect32 %s to %s\n",
1138 (DWORD)PROCESS_Current(), td->pszDll32, td->pszDll16);
1143 DWORD WINAPI
1144 WOW16Call(WORD x,WORD y,WORD z) {
1145 int i;
1146 DWORD calladdr;
1147 FIXME(thunk,"(0x%04x,0x%04x,%d),calling (",x,y,z);
1149 for (i=0;i<x/2;i++) {
1150 WORD a = STACK16_POP(THREAD_Current(),2);
1151 DPRINTF("%04x ",a);
1153 calladdr = STACK16_POP(THREAD_Current(),4);
1154 DPRINTF(") calling address was 0x%08lx\n",calladdr);
1155 return 0;
1159 /***********************************************************************
1160 * 16<->32 Thunklet/Callback API:
1163 #pragma pack(1)
1164 typedef struct _THUNKLET
1166 BYTE prefix_target;
1167 BYTE pushl_target;
1168 DWORD target;
1170 BYTE prefix_relay;
1171 BYTE pushl_relay;
1172 DWORD relay;
1174 BYTE jmp_glue;
1175 DWORD glue;
1177 BYTE type;
1178 HINSTANCE16 owner;
1179 struct _THUNKLET *next;
1180 } THUNKLET;
1181 #pragma pack(4)
1183 #define THUNKLET_TYPE_LS 1
1184 #define THUNKLET_TYPE_SL 2
1186 static HANDLE ThunkletHeap = 0;
1187 static THUNKLET *ThunkletAnchor = NULL;
1189 static FARPROC ThunkletSysthunkGlueLS = 0;
1190 static SEGPTR ThunkletSysthunkGlueSL = 0;
1192 static FARPROC ThunkletCallbackGlueLS = 0;
1193 static SEGPTR ThunkletCallbackGlueSL = 0;
1195 /***********************************************************************
1196 * THUNK_ThunkletInit
1198 static BOOL THUNK_ThunkletInit( void )
1200 LPBYTE thunk;
1202 ThunkletHeap = HeapCreate(HEAP_WINE_SEGPTR | HEAP_WINE_CODE16SEG, 0, 0);
1203 if (!ThunkletHeap) return FALSE;
1205 thunk = HeapAlloc( ThunkletHeap, 0, 5 );
1206 if (!thunk) return FALSE;
1208 ThunkletSysthunkGlueLS = (FARPROC)thunk;
1209 *thunk++ = 0x58; /* popl eax */
1210 *thunk++ = 0xC3; /* ret */
1212 ThunkletSysthunkGlueSL = HEAP_GetSegptr( ThunkletHeap, 0, thunk );
1213 *thunk++ = 0x66; *thunk++ = 0x58; /* popl eax */
1214 *thunk++ = 0xCB; /* lret */
1216 return TRUE;
1219 /***********************************************************************
1220 * SetThunkletCallbackGlue (KERNEL.560)
1222 void WINAPI SetThunkletCallbackGlue16( FARPROC glueLS, SEGPTR glueSL )
1224 ThunkletCallbackGlueLS = glueLS;
1225 ThunkletCallbackGlueSL = glueSL;
1229 /***********************************************************************
1230 * THUNK_FindThunklet
1232 THUNKLET *THUNK_FindThunklet( DWORD target, DWORD relay,
1233 DWORD glue, BYTE type )
1235 THUNKLET *thunk;
1237 for (thunk = ThunkletAnchor; thunk; thunk = thunk->next)
1238 if ( thunk->type == type
1239 && thunk->target == target
1240 && thunk->relay == relay
1241 && thunk->glue == glue )
1242 return thunk;
1244 return NULL;
1247 /***********************************************************************
1248 * THUNK_AllocLSThunklet
1250 FARPROC THUNK_AllocLSThunklet( SEGPTR target, DWORD relay,
1251 FARPROC glue, HTASK16 owner )
1253 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
1254 THUNKLET_TYPE_LS );
1255 if (!thunk)
1257 TDB *pTask = (TDB*)GlobalLock16( owner );
1259 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
1260 return 0;
1262 thunk->prefix_target = thunk->prefix_relay = 0x90;
1263 thunk->pushl_target = thunk->pushl_relay = 0x68;
1264 thunk->jmp_glue = 0xE9;
1266 thunk->target = (DWORD)target;
1267 thunk->relay = (DWORD)relay;
1268 thunk->glue = (DWORD)glue - (DWORD)&thunk->type;
1270 thunk->type = THUNKLET_TYPE_LS;
1271 thunk->owner = pTask? pTask->hInstance : 0;
1273 thunk->next = ThunkletAnchor;
1274 ThunkletAnchor = thunk;
1277 return (FARPROC)thunk;
1280 /***********************************************************************
1281 * THUNK_AllocSLThunklet
1283 SEGPTR THUNK_AllocSLThunklet( FARPROC target, DWORD relay,
1284 SEGPTR glue, HTASK16 owner )
1286 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
1287 THUNKLET_TYPE_SL );
1288 if (!thunk)
1290 TDB *pTask = (TDB*)GlobalLock16( owner );
1292 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
1293 return 0;
1295 thunk->prefix_target = thunk->prefix_relay = 0x66;
1296 thunk->pushl_target = thunk->pushl_relay = 0x68;
1297 thunk->jmp_glue = 0xEA;
1299 thunk->target = (DWORD)target;
1300 thunk->relay = (DWORD)relay;
1301 thunk->glue = (DWORD)glue;
1303 thunk->type = THUNKLET_TYPE_SL;
1304 thunk->owner = pTask? pTask->hInstance : 0;
1306 thunk->next = ThunkletAnchor;
1307 ThunkletAnchor = thunk;
1310 return HEAP_GetSegptr( ThunkletHeap, 0, thunk );
1313 /**********************************************************************
1314 * IsLSThunklet
1316 BOOL16 WINAPI IsLSThunklet( THUNKLET *thunk )
1318 return thunk->prefix_target == 0x90 && thunk->pushl_target == 0x68
1319 && thunk->prefix_relay == 0x90 && thunk->pushl_relay == 0x68
1320 && thunk->jmp_glue == 0xE9 && thunk->type == THUNKLET_TYPE_LS;
1323 /**********************************************************************
1324 * IsSLThunklet (KERNEL.612)
1326 BOOL16 WINAPI IsSLThunklet16( THUNKLET *thunk )
1328 return thunk->prefix_target == 0x66 && thunk->pushl_target == 0x68
1329 && thunk->prefix_relay == 0x66 && thunk->pushl_relay == 0x68
1330 && thunk->jmp_glue == 0xEA && thunk->type == THUNKLET_TYPE_SL;
1335 /***********************************************************************
1336 * AllocLSThunkletSysthunk (KERNEL.607)
1338 FARPROC WINAPI AllocLSThunkletSysthunk16( SEGPTR target,
1339 FARPROC relay, DWORD dummy )
1341 return THUNK_AllocLSThunklet( (SEGPTR)relay, (DWORD)target,
1342 ThunkletSysthunkGlueLS, GetCurrentTask() );
1345 /***********************************************************************
1346 * AllocSLThunkletSysthunk (KERNEL.608)
1348 SEGPTR WINAPI AllocSLThunkletSysthunk16( FARPROC target,
1349 SEGPTR relay, DWORD dummy )
1351 return THUNK_AllocSLThunklet( (FARPROC)relay, (DWORD)target,
1352 ThunkletSysthunkGlueSL, GetCurrentTask() );
1356 /***********************************************************************
1357 * AllocLSThunkletCallbackEx (KERNEL.567)
1359 FARPROC WINAPI AllocLSThunkletCallbackEx16( SEGPTR target,
1360 DWORD relay, HTASK16 task )
1362 THUNKLET *thunk = (THUNKLET *)PTR_SEG_TO_LIN( target );
1363 if ( IsSLThunklet16( thunk ) && thunk->relay == relay
1364 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
1365 return (FARPROC)thunk->target;
1367 return THUNK_AllocLSThunklet( target, relay,
1368 ThunkletCallbackGlueLS, task );
1371 /***********************************************************************
1372 * AllocSLThunkletCallbackEx (KERNEL.568)
1374 SEGPTR WINAPI AllocSLThunkletCallbackEx16( FARPROC target,
1375 DWORD relay, HTASK16 task )
1377 THUNKLET *thunk = (THUNKLET *)target;
1378 if ( IsLSThunklet( thunk ) && thunk->relay == relay
1379 && thunk->glue == (DWORD)ThunkletCallbackGlueLS )
1380 return (SEGPTR)thunk->target;
1382 return THUNK_AllocSLThunklet( target, relay,
1383 ThunkletCallbackGlueSL, task );
1386 /***********************************************************************
1387 * AllocLSThunkletCallback (KERNEL.561) (KERNEL.606)
1389 FARPROC WINAPI AllocLSThunkletCallback16( SEGPTR target, DWORD relay )
1391 return AllocLSThunkletCallbackEx16( target, relay, GetCurrentTask() );
1394 /***********************************************************************
1395 * AllocSLThunkletCallback (KERNEL.562) (KERNEL.605)
1397 SEGPTR WINAPI AllocSLThunkletCallback16( FARPROC target, DWORD relay )
1399 return AllocSLThunkletCallbackEx16( target, relay, GetCurrentTask() );
1402 /***********************************************************************
1403 * FindLSThunkletCallback (KERNEL.563) (KERNEL.609)
1405 FARPROC WINAPI FindLSThunkletCallback( SEGPTR target, DWORD relay )
1407 THUNKLET *thunk = (THUNKLET *)PTR_SEG_TO_LIN( target );
1408 if ( thunk && IsSLThunklet16( thunk ) && thunk->relay == relay
1409 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
1410 return (FARPROC)thunk->target;
1412 thunk = THUNK_FindThunklet( (DWORD)target, relay,
1413 (DWORD)ThunkletCallbackGlueLS,
1414 THUNKLET_TYPE_LS );
1415 return (FARPROC)thunk;
1418 /***********************************************************************
1419 * FindSLThunkletCallback (KERNEL.564) (KERNEL.610)
1421 SEGPTR WINAPI FindSLThunkletCallback( FARPROC target, DWORD relay )
1423 THUNKLET *thunk = (THUNKLET *)target;
1424 if ( thunk && IsLSThunklet( thunk ) && thunk->relay == relay
1425 && thunk->glue == (DWORD)ThunkletCallbackGlueLS )
1426 return (SEGPTR)thunk->target;
1428 thunk = THUNK_FindThunklet( (DWORD)target, relay,
1429 (DWORD)ThunkletCallbackGlueSL,
1430 THUNKLET_TYPE_SL );
1431 return HEAP_GetSegptr( ThunkletHeap, 0, thunk );
1435 /***********************************************************************
1436 * Callback Client API
1439 #define N_CBC_FIXED 20
1440 #define N_CBC_VARIABLE 10
1441 #define N_CBC_TOTAL (N_CBC_FIXED + N_CBC_VARIABLE)
1443 static SEGPTR *CBClientRelay16[ N_CBC_TOTAL ];
1444 static FARPROC *CBClientRelay32[ N_CBC_TOTAL ];
1446 /***********************************************************************
1447 * RegisterCBClient (KERNEL.619)
1449 INT16 WINAPI RegisterCBClient16( INT16 wCBCId,
1450 SEGPTR *relay16, FARPROC *relay32 )
1452 /* Search for free Callback ID */
1453 if ( wCBCId == -1 )
1454 for ( wCBCId = N_CBC_FIXED; wCBCId < N_CBC_TOTAL; wCBCId++ )
1455 if ( !CBClientRelay16[ wCBCId ] )
1456 break;
1458 /* Register Callback ID */
1459 if ( wCBCId > 0 && wCBCId < N_CBC_TOTAL )
1461 CBClientRelay16[ wCBCId ] = relay16;
1462 CBClientRelay32[ wCBCId ] = relay32;
1464 else
1465 wCBCId = 0;
1467 return wCBCId;
1470 /***********************************************************************
1471 * UnRegisterCBClient (KERNEL.622)
1473 INT16 WINAPI UnRegisterCBClient16( INT16 wCBCId,
1474 SEGPTR *relay16, FARPROC *relay32 )
1476 if ( wCBCId >= N_CBC_FIXED && wCBCId < N_CBC_TOTAL
1477 && CBClientRelay16[ wCBCId ] == relay16
1478 && CBClientRelay32[ wCBCId ] == relay32 )
1480 CBClientRelay16[ wCBCId ] = 0;
1481 CBClientRelay32[ wCBCId ] = 0;
1483 else
1484 wCBCId = 0;
1486 return wCBCId;
1490 /***********************************************************************
1491 * InitCBClient (KERNEL.623)
1493 void WINAPI InitCBClient16( FARPROC glueLS )
1495 HMODULE16 kernel = GetModuleHandle16( "KERNEL" );
1496 SEGPTR glueSL = (SEGPTR)WIN32_GetProcAddress16( kernel, (LPCSTR)604 );
1498 SetThunkletCallbackGlue16( glueLS, glueSL );
1501 /***********************************************************************
1502 * CBClientGlueSL (KERNEL.604)
1504 void WINAPI CBClientGlueSL( CONTEXT *context )
1506 /* Create stack frame */
1507 SEGPTR stackSeg = STACK16_PUSH( THREAD_Current(), 12 );
1508 LPWORD stackLin = PTR_SEG_TO_LIN( stackSeg );
1509 SEGPTR glue;
1511 stackLin[3] = BP_reg( context );
1512 stackLin[2] = SI_reg( context );
1513 stackLin[1] = DI_reg( context );
1514 stackLin[0] = DS_reg( context );
1516 BP_reg( context ) = OFFSETOF( stackSeg ) + 6;
1517 SP_reg( context ) = OFFSETOF( stackSeg ) - 4;
1518 GS_reg( context ) = 0;
1520 /* Jump to 16-bit relay code */
1521 glue = CBClientRelay16[ stackLin[5] ][ stackLin[4] ];
1522 CS_reg ( context ) = SELECTOROF( glue );
1523 EIP_reg( context ) = OFFSETOF ( glue );
1526 /***********************************************************************
1527 * CBClientThunkSL (KERNEL.620)
1529 void WINAPI CBClientThunkSL( CONTEXT *context )
1531 /* Call 32-bit relay code */
1532 extern DWORD WINAPI CALL32_CBClient( FARPROC proc, LPWORD args );
1534 LPWORD args = PTR_SEG_OFF_TO_LIN( SS_reg( context ), BP_reg( context ) );
1535 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
1537 EAX_reg(context) = CALL32_CBClient( proc, args );
1540 /***********************************************************************
1541 * CBClientThunkSLEx (KERNEL.621)
1543 void WINAPI CBClientThunkSLEx( CONTEXT *context )
1545 /* Call 32-bit relay code */
1546 extern DWORD WINAPI CALL32_CBClientEx( FARPROC proc,
1547 LPWORD args, INT *nArgs );
1549 LPWORD args = PTR_SEG_OFF_TO_LIN( SS_reg( context ), BP_reg( context ) );
1550 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
1551 INT nArgs;
1552 LPWORD stackLin;
1554 EAX_reg(context) = CALL32_CBClientEx( proc, args, &nArgs );
1556 /* Restore registers saved by CBClientGlueSL */
1557 stackLin = (LPWORD)((LPBYTE)CURRENT_STACK16 + sizeof(STACK16FRAME) - 4);
1558 BP_reg( context ) = stackLin[3];
1559 SI_reg( context ) = stackLin[2];
1560 DI_reg( context ) = stackLin[1];
1561 DS_reg( context ) = stackLin[0];
1562 SP_reg( context ) += 16+nArgs;
1564 /* Return to caller of CBClient thunklet */
1565 CS_reg ( context ) = stackLin[9];
1566 EIP_reg( context ) = stackLin[8];