Forward CBEM_SETEXSTYLE to CBEM_SETEXTENDEDSTYLE, pass WM_SETTEXT and
[wine.git] / if1632 / thunk.c
blob3aa43ec2adc832e0fe9633c879099b9bec9926f0
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 "callback.h"
11 #include "builtin16.h"
12 #include "heap.h"
13 #include "module.h"
14 #include "neexe.h"
15 #include "stackframe.h"
16 #include "selectors.h"
17 #include "debugtools.h"
19 DEFAULT_DEBUG_CHANNEL(thunk);
22 /* List of the 16-bit callback functions. This list is used */
23 /* by the build program to generate the file if1632/callto16.S */
25 /* ### start build ### */
26 extern WORD CALLBACK THUNK_CallTo16_word_ (FARPROC16);
27 extern WORD CALLBACK THUNK_CallTo16_word_l (FARPROC16,LONG);
28 extern LONG CALLBACK THUNK_CallTo16_long_l (FARPROC16,LONG);
29 extern WORD CALLBACK THUNK_CallTo16_word_lllw (FARPROC16,LONG,LONG,LONG,WORD);
30 extern WORD CALLBACK THUNK_CallTo16_word_lwww (FARPROC16,LONG,WORD,WORD,WORD);
31 extern LONG CALLBACK THUNK_CallTo16_long_wwwl (FARPROC16,WORD,WORD,WORD,LONG);
32 extern WORD CALLBACK THUNK_CallTo16_word_lwwww(FARPROC16,LONG,WORD,WORD,WORD,WORD);
33 extern WORD CALLBACK THUNK_CallTo16_word_w (FARPROC16,WORD);
34 extern WORD CALLBACK THUNK_CallTo16_word_wlww (FARPROC16,WORD,LONG,WORD,WORD);
35 extern WORD CALLBACK THUNK_CallTo16_word_ww (FARPROC16,WORD,WORD);
36 extern WORD CALLBACK THUNK_CallTo16_word_wwwl (FARPROC16,WORD,WORD,WORD,LONG);
37 /* ### stop build ### */
39 static THUNK *firstThunk = NULL;
41 CALLOUT_TABLE Callout = {
42 /* PeekMessageA */ NULL,
43 /* GetMessageA */ NULL,
44 /* SendMessageA */ NULL,
45 /* PostMessageA */ NULL,
46 /* PostAppMessage16 */ NULL,
47 /* TranslateMessage */ NULL,
48 /* DispatchMessageA */ NULL,
49 /* RedrawWindow */ NULL,
50 /* UserSignalProc */ NULL,
51 /* FinalUserInit16 */ NULL,
52 /* InitThreadInput16 */ NULL,
53 /* UserYield16) */ NULL,
54 /* DestroyIcon32 */ NULL,
55 /* WaitForInputIdle */ NULL,
56 /* MsgWaitForMultipleObjects */ NULL,
57 /* WindowFromDC */ NULL,
58 /* MessageBoxA */ NULL,
59 /* MessageBoxW */ NULL
62 /***********************************************************************
63 * THUNK_Alloc
65 FARPROC THUNK_Alloc( FARPROC16 func, RELAY relay )
67 HANDLE16 hSeg;
68 NE_MODULE *pModule;
69 THUNK *thunk;
71 /* NULL maps to NULL */
72 if ( !func ) return NULL;
74 /*
75 * If we got an 16-bit built-in API entry point, retrieve the Wine
76 * 32-bit handler for that API routine.
78 * NOTE: For efficiency reasons, we only check whether the selector
79 * of 'func' points to the code segment of a built-in module.
80 * It might be theoretically possible that the offset is such
81 * that 'func' does not point, in fact, to an API entry point.
82 * In this case, however, the pointer is corrupt anyway.
84 hSeg = GlobalHandle16( SELECTOROF( func ) );
85 pModule = NE_GetPtr( FarGetOwner16( hSeg ) );
87 if ( pModule && (pModule->flags & NE_FFLAGS_BUILTIN)
88 && NE_SEG_TABLE(pModule)[0].hSeg == hSeg )
90 FARPROC proc = (FARPROC)((ENTRYPOINT16 *)PTR_SEG_TO_LIN( func ))->target;
92 TRACE( "(%04x:%04x, %p) -> built-in API %p\n",
93 SELECTOROF( func ), OFFSETOF( func ), relay, proc );
94 return proc;
97 /* Otherwise, we need to alloc a thunk */
98 thunk = HeapAlloc( GetProcessHeap(), 0, sizeof(*thunk) );
99 if (thunk)
101 thunk->popl_eax = 0x58;
102 thunk->pushl_func = 0x68;
103 thunk->proc = func;
104 thunk->pushl_eax = 0x50;
105 thunk->jmp = 0xe9;
106 thunk->relay = (RELAY)((char *)relay - (char *)(&thunk->next));
107 thunk->magic = CALLTO16_THUNK_MAGIC;
108 thunk->next = firstThunk;
109 firstThunk = thunk;
112 TRACE( "(%04x:%04x, %p) -> allocated thunk %p\n",
113 SELECTOROF( func ), OFFSETOF( func ), relay, thunk );
114 return (FARPROC)thunk;
117 /***********************************************************************
118 * THUNK_Free
120 void THUNK_Free( FARPROC thunk )
122 THUNK *t = (THUNK*)thunk;
123 if ( !t || IsBadReadPtr( t, sizeof(*t) )
124 || t->magic != CALLTO16_THUNK_MAGIC )
125 return;
127 if (HEAP_IsInsideHeap( GetProcessHeap(), 0, t ))
129 THUNK **prev = &firstThunk;
130 while (*prev && (*prev != t)) prev = &(*prev)->next;
131 if (*prev)
133 *prev = t->next;
134 HeapFree( GetProcessHeap(), 0, t );
135 return;
138 ERR("invalid thunk addr %p\n", thunk );
139 return;
143 /***********************************************************************
144 * THUNK_GetCalloutThunk
146 * Retrieve API entry point with given name from given module.
147 * If module is builtin, return the 32-bit entry point, otherwise
148 * create a 32->16 thunk to the 16-bit entry point, using the
149 * given relay code.
152 static FARPROC THUNK_GetCalloutThunk( NE_MODULE *pModule, LPSTR name, RELAY relay )
154 FARPROC16 proc = WIN32_GetProcAddress16( pModule->self, name );
155 if ( !proc ) return 0;
157 if ( pModule->flags & NE_FFLAGS_BUILTIN )
158 return (FARPROC)((ENTRYPOINT16 *)PTR_SEG_TO_LIN( proc ))->target;
159 else
160 return (FARPROC)THUNK_Alloc( proc, relay );
163 /***********************************************************************
164 * THUNK_InitCallout
166 void THUNK_InitCallout(void)
168 HMODULE hModule;
169 NE_MODULE *pModule;
171 hModule = LoadLibraryA( "user32.dll" );
172 if ( hModule )
174 #define GETADDR( name ) \
175 *(FARPROC *)&Callout.name = GetProcAddress( hModule, #name )
177 GETADDR( PeekMessageA );
178 GETADDR( GetMessageA );
179 GETADDR( SendMessageA );
180 GETADDR( PostMessageA );
181 GETADDR( TranslateMessage );
182 GETADDR( DispatchMessageA );
183 GETADDR( RedrawWindow );
184 GETADDR( WaitForInputIdle );
185 GETADDR( MsgWaitForMultipleObjects );
186 GETADDR( WindowFromDC );
187 GETADDR( MessageBoxA );
188 GETADDR( MessageBoxW );
189 #undef GETADDR
192 pModule = NE_GetPtr( LoadLibrary16( "USER.EXE" ) );
193 if ( pModule )
195 #define GETADDR( var, name, thk ) \
196 *(FARPROC *)&Callout.var = THUNK_GetCalloutThunk( pModule, name, \
197 (RELAY)THUNK_CallTo16_##thk )
199 GETADDR( PostAppMessage16, "PostAppMessage", word_wwwl );
200 GETADDR( FinalUserInit16, "FinalUserInit", word_ );
201 GETADDR( InitThreadInput16, "InitThreadInput", word_ww );
202 GETADDR( UserYield16, "UserYield", word_ );
203 GETADDR( DestroyIcon32, "DestroyIcon32", word_ww );
204 GETADDR( UserSignalProc, "SignalProc32", word_lllw );
206 #undef GETADDR