Release 941030
[wine/multimedia.git] / if1632 / callback.c
blob6f27756961f3dc416c0c1e66fef5a986184977f8
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];
35 static __inline__ int Is16bitAddress(void *address)
37 return ((unsigned int) address
38 >= (((FIRST_SELECTOR << 3) | 0x0007) << 16));
41 /**********************************************************************
42 * PushOn16
44 static void
45 PushOn16(int size, unsigned int value)
47 char *p = (char *) (((unsigned int)IF1632_Saved16_ss << 16) +
48 (IF1632_Saved16_esp & 0xffff));
49 if (size)
51 unsigned long *lp = (unsigned long *) p - 1;
53 *lp = value;
54 IF1632_Saved16_esp -= 4;
56 else
58 unsigned short *sp = (unsigned short *) p - 1;
60 *sp = value;
61 IF1632_Saved16_esp -= 2;
65 /**********************************************************************
66 * FindDataSegmentForCode
68 static unsigned short
69 FindDataSegmentForCode(unsigned long csip)
71 unsigned int seg_idx;
73 seg_idx = (unsigned short) (csip >> 19);
74 return Segments[seg_idx].owner;
77 /**********************************************************************
78 * CallBack16
80 int
81 CallBack16(void *func, int n_args, ...)
83 va_list ap;
84 int i;
85 int arg_type, arg_value;
87 va_start(ap, n_args);
89 for (i = 0; i < n_args; i++)
91 arg_type = va_arg(ap, int);
92 arg_value = va_arg(ap, int);
93 PushOn16(arg_type, arg_value);
96 va_end(ap);
98 return CallTo16((unsigned int) func,
99 FindDataSegmentForCode((unsigned long) func));
102 /**********************************************************************
103 * CALLBACK_MakeProcInstance
105 void *
106 CALLBACK_MakeProcInstance(void *func, int instance)
108 struct thunk_s *tp;
109 int i;
111 tp = (struct thunk_s *) MakeProcThunks->base_addr;
112 for (i = 0; i < 0x10000 / sizeof(*tp); i++, tp++)
113 if (!tp->used)
114 break;
116 if (tp->used)
117 return (void *) 0;
119 tp->thunk[0] = 0xb8;
120 tp->thunk[1] = (unsigned char) instance;
121 tp->thunk[2] = (unsigned char) (instance >> 8);
122 tp->thunk[3] = 0x8e;
123 tp->thunk[4] = 0xd8;
124 tp->thunk[5] = 0xea;
125 memcpy(&tp->thunk[6], &func, 4);
126 tp->used = 1;
128 return tp->thunk;
131 /**********************************************************************
132 * FreeProcInstance (KERNEL.52)
134 void FreeProcInstance(FARPROC func)
136 struct thunk_s *tp;
137 int i;
139 tp = (struct thunk_s *) MakeProcThunks->base_addr;
140 for (i = 0; i < 0x10000 / sizeof(*tp); i++, tp++)
142 if ((void *) tp->thunk == (void *) func)
144 tp->used = 0;
145 break;
150 /**********************************************************************
151 * GetCodeHandle (KERNEL.93)
153 HANDLE GetCodeHandle( FARPROC proc )
155 struct thunk_s *tp = (struct thunk_s *)proc;
157 /* Return the code segment containing 'proc'. */
158 /* Not sure if this is really correct (shouldn't matter that much). */
159 printf( "STUB: GetCodeHandle(%p) returning %x\n",
160 proc, tp->thunk[8] + (tp->thunk[9] << 8) );
161 return tp->thunk[8] + (tp->thunk[9] << 8);
165 /**********************************************************************
166 * CallWindowProc (USER.122)
168 LONG CallWindowProc( WNDPROC func, HWND hwnd, WORD message,
169 WORD wParam, LONG lParam )
171 SpyMessage(hwnd, message, wParam, lParam);
173 if (HIWORD((LONG)func) == WINE_CODE_SELECTOR)
175 static struct dll_table_entry_s *user_tab = NULL;
176 void *address = (void *) ((LONG) func & 0xffff);
178 if (user_tab == NULL)
179 user_tab = FindDLLTable("USER");
181 /* DefWindowProc */
182 if (user_tab[107].address == address)
183 return DefWindowProc(hwnd, message, wParam, lParam);
185 /* DefDlgProc */
186 else if (user_tab[308].address == address)
187 return DefDlgProc(hwnd, message, wParam, lParam);
189 /* DefMDIChildProc */
190 else if (user_tab[447].address == address)
191 return DefMDIChildProc(hwnd, message, wParam, lParam);
193 /* default */
194 else
196 fprintf(stderr, "wine: Unknown wine callback %08x\n", func);
197 exit(1);
200 else if (Is16bitAddress(func))
202 dprintf_callback(stddeb, "CallWindowProc // 16bit func=%p !\n", func);
203 PushOn16( CALLBACK_SIZE_WORD, hwnd );
204 PushOn16( CALLBACK_SIZE_WORD, message );
205 PushOn16( CALLBACK_SIZE_WORD, wParam );
206 PushOn16( CALLBACK_SIZE_LONG, lParam );
207 return CallTo16((unsigned int) func,
208 FindDataSegmentForCode((unsigned long) func));
210 else
212 dprintf_callback(stddeb, "CallWindowProc // 32bit func=%08X !\n", func);
213 return (*func)(hwnd, message, wParam, lParam);
217 /**********************************************************************
218 * CallLineDDAProc
220 void CallLineDDAProc(FARPROC func, short xPos, short yPos, long lParam)
222 if (Is16bitAddress(func))
224 PushOn16( CALLBACK_SIZE_WORD, xPos );
225 PushOn16( CALLBACK_SIZE_WORD, yPos );
226 PushOn16( CALLBACK_SIZE_LONG, lParam );
227 CallTo16((unsigned int) func,
228 FindDataSegmentForCode((unsigned long) func));
230 else
232 (*func)(xPos, yPos, lParam);
236 /**********************************************************************
237 * CallHookProc
239 DWORD CallHookProc( HOOKPROC func, short code, WPARAM wParam, LPARAM lParam )
241 if (Is16bitAddress(func))
243 PushOn16( CALLBACK_SIZE_WORD, code );
244 PushOn16( CALLBACK_SIZE_WORD, wParam );
245 PushOn16( CALLBACK_SIZE_LONG, lParam );
246 return CallTo16((unsigned int) func,
247 FindDataSegmentForCode((unsigned long) func));
249 else
251 return (*func)( code, wParam, lParam );
255 /**********************************************************************
256 * CallGrayStringProc
258 BOOL CallGrayStringProc(FARPROC func, HDC hdc, LPARAM lParam, INT cch )
260 if (Is16bitAddress(func))
262 PushOn16( CALLBACK_SIZE_WORD, hdc );
263 PushOn16( CALLBACK_SIZE_LONG, lParam );
264 PushOn16( CALLBACK_SIZE_WORD, cch );
265 return CallTo16((unsigned int) func,
266 FindDataSegmentForCode((unsigned long) func));
268 else
270 return (*func)( hdc, lParam, cch );
274 /* ------------------------------------------------------------------------ */
276 * The following functions realize the Catch/Throw functionality.
277 * My thought is to use the setjmp, longjmp combination to do the
278 * major part of this one. All I have to remember, in addition to
279 * whatever the jmp_buf contains, is the contents of the 16-bit
280 * sp, bp and ss. I do this by storing them in the structure passed
281 * to me by the 16-bit program (including my own jmp_buf...).
282 * Hopefully there isn't any program that modifies the contents!
283 * Bad thing: I have to save part of the stack, since this will
284 * get reused on the next call after my return, leaving it in an
285 * undefined state.
287 #define STACK_DEPTH_16 28
289 struct special_buffer {
290 jmp_buf buffer;
291 long regs [6];
292 char stack_part [STACK_DEPTH_16];
293 } *sb;
295 int Catch (LPCATCHBUF cbuf)
297 WORD retval;
298 jmp_buf *tmp_jmp;
299 char *stack16 = (char *) (((unsigned int)IF1632_Saved16_ss << 16) +
300 (IF1632_Saved16_esp & 0xffff));
302 sb = malloc (sizeof (struct special_buffer));
304 sb -> regs [0] = IF1632_Saved16_esp;
305 sb -> regs [1] = IF1632_Saved16_ebp;
306 sb -> regs [2] = IF1632_Saved16_ss & 0xffff;
307 sb -> regs [3] = IF1632_Saved32_esp;
308 sb -> regs [4] = IF1632_Saved32_ebp;
309 sb -> regs [5] = IF1632_Saved32_ss & 0xffff;
310 memcpy (sb -> stack_part, stack16, STACK_DEPTH_16);
311 tmp_jmp = &sb -> buffer;
312 *((struct special_buffer **)cbuf) = sb;
314 if ((retval = setjmp (*tmp_jmp)))
316 IF1632_Saved16_esp = sb -> regs [0];
317 IF1632_Saved16_ebp = sb -> regs [1];
318 IF1632_Saved16_ss = sb -> regs [2] & 0xffff;
319 IF1632_Saved32_esp = sb -> regs [3];
320 IF1632_Saved32_ebp = sb -> regs [4];
321 IF1632_Saved32_ss = sb -> regs [5] & 0xffff;
322 stack16 = (char *) (((unsigned int)IF1632_Saved16_ss << 16) +
323 (IF1632_Saved16_esp & 0xffff));
325 memcpy (stack16, sb -> stack_part, STACK_DEPTH_16);
326 dprintf_catch (stddeb, "Been thrown here: %d, retval = %d\n",
327 sb, retval);
328 free ((void *) sb);
329 return (retval);
330 } else {
331 dprintf_catch (stddeb, "Will somtime get thrown here: %d\n", sb);
332 return (retval);
336 void Throw (LPCATCHBUF cbuf, int val)
338 sb = *((struct special_buffer **)cbuf);
339 dprintf_catch (stddeb, "Throwing to: %d\n", sb);
340 longjmp (sb -> buffer, val);
342 #endif /* !WINELIB */