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";
14 /* #define DEBUG_CALLBACK */
15 /* #undef DEBUG_CALLBACK */
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
;
32 unsigned char thunk
[10];
35 static __inline__
int Is16bitAddress(void *address
)
37 return ((unsigned int) address
38 >= (((FIRST_SELECTOR
<< 3) | 0x0007) << 16));
41 /**********************************************************************
45 PushOn16(int size
, unsigned int value
)
47 char *p
= (char *) (((unsigned int)IF1632_Saved16_ss
<< 16) +
48 (IF1632_Saved16_esp
& 0xffff));
51 unsigned long *lp
= (unsigned long *) p
- 1;
54 IF1632_Saved16_esp
-= 4;
58 unsigned short *sp
= (unsigned short *) p
- 1;
61 IF1632_Saved16_esp
-= 2;
65 /**********************************************************************
66 * FindDataSegmentForCode
69 FindDataSegmentForCode(unsigned long csip
)
73 seg_idx
= (unsigned short) (csip
>> 19);
74 return Segments
[seg_idx
].owner
;
77 /**********************************************************************
81 CallBack16(void *func
, int n_args
, ...)
85 int arg_type
, arg_value
;
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
);
98 return CallTo16((unsigned int) func
,
99 FindDataSegmentForCode((unsigned long) func
));
102 /**********************************************************************
103 * CALLBACK_MakeProcInstance
106 CALLBACK_MakeProcInstance(void *func
, int instance
)
111 tp
= (struct thunk_s
*) MakeProcThunks
->base_addr
;
112 for (i
= 0; i
< 0x10000 / sizeof(*tp
); i
++, tp
++)
120 tp
->thunk
[1] = (unsigned char) instance
;
121 tp
->thunk
[2] = (unsigned char) (instance
>> 8);
125 memcpy(&tp
->thunk
[6], &func
, 4);
131 /**********************************************************************
132 * FreeProcInstance (KERNEL.52)
134 void FreeProcInstance(FARPROC func
)
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
)
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");
182 if (user_tab
[107].address
== address
)
183 return DefWindowProc(hwnd
, message
, wParam
, lParam
);
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
);
196 fprintf(stderr
, "wine: Unknown wine callback %08x\n", func
);
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
));
212 dprintf_callback(stddeb
, "CallWindowProc // 32bit func=%08X !\n", func
);
213 return (*func
)(hwnd
, message
, wParam
, lParam
);
217 /**********************************************************************
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
));
232 (*func
)(xPos
, yPos
, lParam
);
236 /**********************************************************************
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
));
251 return (*func
)( code
, wParam
, lParam
);
255 /**********************************************************************
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
));
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
287 #define STACK_DEPTH_16 28
289 struct special_buffer
{
292 char stack_part
[STACK_DEPTH_16
];
295 int Catch (LPCATCHBUF cbuf
)
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",
331 dprintf_catch (stddeb
, "Will somtime get thrown here: %d\n", sb
);
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 */