Release 941107
[wine/multimedia.git] / if1632 / callback.c
blob5e8d5eb1b1bb64a56b31a9fbcc6f6d81e06c30b7
1 #ifndef WINELIB
2 static char RCSId[] = "$Id: wine.c,v 1.2 1993/07/04 04:04:21 root Exp root $";
3 static char Copyright[] = "Copyright Robert J. Amstadt, 1993";
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include "windows.h"
8 #include "callback.h"
9 #include "wine.h"
10 #include "segmem.h"
11 #include <setjmp.h>
12 #include "dlls.h"
13 #include "stddebug.h"
14 /* #define DEBUG_CALLBACK */
15 /* #undef DEBUG_CALLBACK */
16 #include "debug.h"
19 extern SEGDESC Segments[];
20 extern unsigned short IF1632_Saved16_ss;
21 extern unsigned long IF1632_Saved16_ebp;
22 extern unsigned long IF1632_Saved16_esp;
23 extern unsigned short IF1632_Saved32_ss;
24 extern unsigned long IF1632_Saved32_ebp;
25 extern unsigned long IF1632_Saved32_esp;
27 extern struct segment_descriptor_s *MakeProcThunks;
29 struct thunk_s
31 int used;
32 unsigned char thunk[10];
36 /**********************************************************************
37 * PushOn16
39 static void
40 PushOn16(int size, unsigned int value)
42 char *p = (char *) (((unsigned int)IF1632_Saved16_ss << 16) +
43 (IF1632_Saved16_esp & 0xffff));
44 if (size)
46 unsigned long *lp = (unsigned long *) p - 1;
48 *lp = value;
49 IF1632_Saved16_esp -= 4;
51 else
53 unsigned short *sp = (unsigned short *) p - 1;
55 *sp = value;
56 IF1632_Saved16_esp -= 2;
60 /**********************************************************************
61 * FindDataSegmentForCode
63 static unsigned short
64 FindDataSegmentForCode(unsigned long csip)
66 unsigned int seg_idx;
68 seg_idx = (unsigned short) (csip >> 19);
69 return Segments[seg_idx].owner;
72 /**********************************************************************
73 * CallBack16
75 int
76 CallBack16(void *func, int n_args, ...)
78 va_list ap;
79 int i;
80 int arg_type, arg_value;
82 va_start(ap, n_args);
84 for (i = 0; i < n_args; i++)
86 arg_type = va_arg(ap, int);
87 arg_value = va_arg(ap, int);
88 PushOn16(arg_type, arg_value);
91 va_end(ap);
93 return CallTo16((unsigned int) func,
94 FindDataSegmentForCode((unsigned long) func));
97 /**********************************************************************
98 * CALLBACK_MakeProcInstance
100 void *
101 CALLBACK_MakeProcInstance(void *func, int instance)
103 struct thunk_s *tp;
104 int i;
106 tp = (struct thunk_s *) MakeProcThunks->base_addr;
107 for (i = 0; i < 0x10000 / sizeof(*tp); i++, tp++)
108 if (!tp->used)
109 break;
111 if (tp->used)
112 return (void *) 0;
114 tp->thunk[0] = 0xb8;
115 tp->thunk[1] = (unsigned char) instance;
116 tp->thunk[2] = (unsigned char) (instance >> 8);
117 tp->thunk[3] = 0x8e;
118 tp->thunk[4] = 0xd8;
119 tp->thunk[5] = 0xea;
120 memcpy(&tp->thunk[6], &func, 4);
121 tp->used = 1;
123 return tp->thunk;
126 /**********************************************************************
127 * FreeProcInstance (KERNEL.52)
129 void FreeProcInstance(FARPROC func)
131 struct thunk_s *tp;
132 int i;
134 tp = (struct thunk_s *) MakeProcThunks->base_addr;
135 for (i = 0; i < 0x10000 / sizeof(*tp); i++, tp++)
137 if ((void *) tp->thunk == (void *) func)
139 tp->used = 0;
140 break;
145 /**********************************************************************
146 * GetCodeHandle (KERNEL.93)
148 HANDLE GetCodeHandle( FARPROC proc )
150 struct thunk_s *tp = (struct thunk_s *)proc;
152 /* Return the code segment containing 'proc'. */
153 /* Not sure if this is really correct (shouldn't matter that much). */
154 printf( "STUB: GetCodeHandle(%p) returning %x\n",
155 proc, tp->thunk[8] + (tp->thunk[9] << 8) );
156 return tp->thunk[8] + (tp->thunk[9] << 8);
160 /**********************************************************************
161 * CallWindowProc (USER.122)
163 LONG CallWindowProc( WNDPROC func, HWND hwnd, WORD message,
164 WORD wParam, LONG lParam )
166 SpyMessage(hwnd, message, wParam, lParam);
168 if (HIWORD((LONG)func) == WINE_CODE_SELECTOR)
170 static struct dll_table_entry_s *user_tab = NULL;
171 void *address = (void *) ((LONG) func & 0xffff);
173 if (user_tab == NULL)
174 user_tab = FindDLLTable("USER");
176 /* DefWindowProc */
177 if (user_tab[107].address == address)
178 return DefWindowProc(hwnd, message, wParam, lParam);
180 /* DefDlgProc */
181 else if (user_tab[308].address == address)
182 return DefDlgProc(hwnd, message, wParam, lParam);
184 /* DefMDIChildProc */
185 else if (user_tab[447].address == address)
186 return DefMDIChildProc(hwnd, message, wParam, lParam);
188 /* default */
189 else
191 fprintf(stderr, "wine: Unknown wine callback %08x\n", func);
192 exit(1);
195 else if (IS_16_BIT_ADDRESS(func))
197 dprintf_callback(stddeb, "CallWindowProc // 16bit func=%p !\n", func);
198 PushOn16( CALLBACK_SIZE_WORD, hwnd );
199 PushOn16( CALLBACK_SIZE_WORD, message );
200 PushOn16( CALLBACK_SIZE_WORD, wParam );
201 PushOn16( CALLBACK_SIZE_LONG, lParam );
202 return CallTo16((unsigned int) func,
203 FindDataSegmentForCode((unsigned long) func));
205 else
207 dprintf_callback(stddeb, "CallWindowProc // 32bit func=%08X !\n", func);
208 return (*func)(hwnd, message, wParam, lParam);
212 /**********************************************************************
213 * CallLineDDAProc
215 void CallLineDDAProc(FARPROC func, short xPos, short yPos, long lParam)
217 if (IS_16_BIT_ADDRESS(func))
219 PushOn16( CALLBACK_SIZE_WORD, xPos );
220 PushOn16( CALLBACK_SIZE_WORD, yPos );
221 PushOn16( CALLBACK_SIZE_LONG, lParam );
222 CallTo16((unsigned int) func,
223 FindDataSegmentForCode((unsigned long) func));
225 else
227 (*func)(xPos, yPos, lParam);
231 /**********************************************************************
232 * CallHookProc
234 DWORD CallHookProc( HOOKPROC func, short code, WPARAM wParam, LPARAM lParam )
236 if (IS_16_BIT_ADDRESS(func))
238 PushOn16( CALLBACK_SIZE_WORD, code );
239 PushOn16( CALLBACK_SIZE_WORD, wParam );
240 PushOn16( CALLBACK_SIZE_LONG, lParam );
241 return CallTo16((unsigned int) func,
242 FindDataSegmentForCode((unsigned long) func));
244 else
246 return (*func)( code, wParam, lParam );
250 /**********************************************************************
251 * CallGrayStringProc
253 BOOL CallGrayStringProc(FARPROC func, HDC hdc, LPARAM lParam, INT cch )
255 if (IS_16_BIT_ADDRESS(func))
257 PushOn16( CALLBACK_SIZE_WORD, hdc );
258 PushOn16( CALLBACK_SIZE_LONG, lParam );
259 PushOn16( CALLBACK_SIZE_WORD, cch );
260 return CallTo16((unsigned int) func,
261 FindDataSegmentForCode((unsigned long) func));
263 else
265 return (*func)( hdc, lParam, cch );
269 /* ------------------------------------------------------------------------ */
271 * The following functions realize the Catch/Throw functionality.
272 * My thought is to use the setjmp, longjmp combination to do the
273 * major part of this one. All I have to remember, in addition to
274 * whatever the jmp_buf contains, is the contents of the 16-bit
275 * sp, bp and ss. I do this by storing them in the structure passed
276 * to me by the 16-bit program (including my own jmp_buf...).
277 * Hopefully there isn't any program that modifies the contents!
278 * Bad thing: I have to save part of the stack, since this will
279 * get reused on the next call after my return, leaving it in an
280 * undefined state.
282 #define STACK_DEPTH_16 28
284 struct special_buffer {
285 jmp_buf buffer;
286 long regs [6];
287 char stack_part [STACK_DEPTH_16];
288 } *sb;
290 int Catch (LPCATCHBUF cbuf)
292 WORD retval;
293 jmp_buf *tmp_jmp;
294 char *stack16 = (char *) (((unsigned int)IF1632_Saved16_ss << 16) +
295 (IF1632_Saved16_esp & 0xffff));
297 sb = malloc (sizeof (struct special_buffer));
299 sb -> regs [0] = IF1632_Saved16_esp;
300 sb -> regs [1] = IF1632_Saved16_ebp;
301 sb -> regs [2] = IF1632_Saved16_ss & 0xffff;
302 sb -> regs [3] = IF1632_Saved32_esp;
303 sb -> regs [4] = IF1632_Saved32_ebp;
304 sb -> regs [5] = IF1632_Saved32_ss & 0xffff;
305 memcpy (sb -> stack_part, stack16, STACK_DEPTH_16);
306 tmp_jmp = &sb -> buffer;
307 *((struct special_buffer **)cbuf) = sb;
309 if ((retval = setjmp (*tmp_jmp)))
311 IF1632_Saved16_esp = sb -> regs [0];
312 IF1632_Saved16_ebp = sb -> regs [1];
313 IF1632_Saved16_ss = sb -> regs [2] & 0xffff;
314 IF1632_Saved32_esp = sb -> regs [3];
315 IF1632_Saved32_ebp = sb -> regs [4];
316 IF1632_Saved32_ss = sb -> regs [5] & 0xffff;
317 stack16 = (char *) (((unsigned int)IF1632_Saved16_ss << 16) +
318 (IF1632_Saved16_esp & 0xffff));
320 memcpy (stack16, sb -> stack_part, STACK_DEPTH_16);
321 dprintf_catch (stddeb, "Been thrown here: %d, retval = %d\n",
322 sb, retval);
323 free ((void *) sb);
324 return (retval);
325 } else {
326 dprintf_catch (stddeb, "Will somtime get thrown here: %d\n", sb);
327 return (retval);
331 void Throw (LPCATCHBUF cbuf, int val)
333 sb = *((struct special_buffer **)cbuf);
334 dprintf_catch (stddeb, "Throwing to: %d\n", sb);
335 longjmp (sb -> buffer, val);
337 #endif /* !WINELIB */