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];
36 /**********************************************************************
40 PushOn16(int size
, unsigned int value
)
42 char *p
= (char *) (((unsigned int)IF1632_Saved16_ss
<< 16) +
43 (IF1632_Saved16_esp
& 0xffff));
46 unsigned long *lp
= (unsigned long *) p
- 1;
49 IF1632_Saved16_esp
-= 4;
53 unsigned short *sp
= (unsigned short *) p
- 1;
56 IF1632_Saved16_esp
-= 2;
60 /**********************************************************************
61 * FindDataSegmentForCode
64 FindDataSegmentForCode(unsigned long csip
)
68 seg_idx
= (unsigned short) (csip
>> 19);
69 return Segments
[seg_idx
].owner
;
72 /**********************************************************************
76 CallBack16(void *func
, int n_args
, ...)
80 int arg_type
, arg_value
;
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
);
93 return CallTo16((unsigned int) func
,
94 FindDataSegmentForCode((unsigned long) func
));
97 /**********************************************************************
98 * CALLBACK_MakeProcInstance
101 CALLBACK_MakeProcInstance(void *func
, int instance
)
106 tp
= (struct thunk_s
*) MakeProcThunks
->base_addr
;
107 for (i
= 0; i
< 0x10000 / sizeof(*tp
); i
++, tp
++)
115 tp
->thunk
[1] = (unsigned char) instance
;
116 tp
->thunk
[2] = (unsigned char) (instance
>> 8);
120 memcpy(&tp
->thunk
[6], &func
, 4);
126 /**********************************************************************
127 * FreeProcInstance (KERNEL.52)
129 void FreeProcInstance(FARPROC func
)
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
)
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");
177 if (user_tab
[107].address
== address
)
178 return DefWindowProc(hwnd
, message
, wParam
, lParam
);
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
);
191 fprintf(stderr
, "wine: Unknown wine callback %08x\n", func
);
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
));
207 dprintf_callback(stddeb
, "CallWindowProc // 32bit func=%08X !\n", func
);
208 return (*func
)(hwnd
, message
, wParam
, lParam
);
212 /**********************************************************************
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
));
227 (*func
)(xPos
, yPos
, lParam
);
231 /**********************************************************************
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
));
246 return (*func
)( code
, wParam
, lParam
);
250 /**********************************************************************
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
));
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
282 #define STACK_DEPTH_16 28
284 struct special_buffer
{
287 char stack_part
[STACK_DEPTH_16
];
290 int Catch (LPCATCHBUF cbuf
)
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",
326 dprintf_catch (stddeb
, "Will somtime get thrown here: %d\n", sb
);
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 */