source: trunk/src/user32/oslibmsg.cpp@ 4848

Last change on this file since 4848 was 4848, checked in by sandervl, 25 years ago

Win32ToOS2Handle & OS2ToWin32Handle exported with stdcall calling convention

File size: 16.3 KB
Line 
1/* $Id: oslibmsg.cpp,v 1.35 2000-12-29 18:39:58 sandervl Exp $ */
2/*
3 * Window message translation functions for OS/2
4 *
5 *
6 * Copyright 1999 Sander van Leeuwen (sandervl@xs4all.nl)
7 *
8 *
9 * Project Odin Software License can be found in LICENSE.TXT
10 *
11 * TODO: Some messages that are sent to the frame window are directly passed on to the client
12 * -> Get/PeekMessage never gets them as we return a dummy message for non-client windows
13 * (i.e. menu WM_COMMAND messages)
14 *
15 * TODO: Filter translation isn't correct! (for posted messages or messages that don't have
16 * a PM version.
17 *
18 */
19#define INCL_WIN
20#define INCL_PM
21#define INCL_DOSPROCESS
22#include <os2wrap.h>
23#include <string.h>
24#include <misc.h>
25#include "oslibmsg.h"
26#include <winconst.h>
27#include <win32api.h>
28#include <winuser32.h>
29#include "oslibutil.h"
30#include "timer.h"
31#include <thread.h>
32#include <wprocess.h>
33#include "pmwindow.h"
34#include "oslibwin.h"
35#include <win\hook.h>
36
37#define DBG_LOCALLOG DBG_oslibmsg
38#include "dbglocal.h"
39
40typedef BOOL (EXPENTRY FNTRANS)(MSG *, QMSG *);
41typedef FNTRANS *PFNTRANS;
42
43typedef struct
44{
45 ULONG msgOS2;
46 ULONG msgWin32;
47// PFNTRANS toOS2;
48// PFNTRANS toWIN32;
49} MSGTRANSTAB, *PMSGTRANSTAB;
50
51//NOTE: Must be ordered by win32 message id!!
52MSGTRANSTAB MsgTransTab[] = {
53 WM_NULL, WINWM_NULL,
54 WM_CREATE, WINWM_CREATE,
55 WM_DESTROY, WINWM_DESTROY,
56 WM_MOVE, WINWM_MOVE, //TODO: Sent directly
57 WM_SIZE, WINWM_SIZE, //TODO: Sent directly
58 WM_ACTIVATE, WINWM_ACTIVATE,
59 WM_SETFOCUS, WINWM_SETFOCUS,
60 WM_SETFOCUS, WINWM_KILLFOCUS,
61 WM_ENABLE, WINWM_ENABLE,
62 WM_PAINT, WINWM_PAINT,
63 WM_CLOSE, WINWM_CLOSE,
64 WM_QUIT, WINWM_QUIT,
65 WM_SHOW, WINWM_SHOWWINDOW,
66
67 WM_HITTEST, WINWM_NCHITTEST,
68
69 //TODO: Needs better translation!
70 WM_CHAR, WINWM_KEYDOWN,
71 WM_CHAR, WINWM_KEYUP,
72 WM_CHAR, WINWM_CHAR,
73 WM_CHAR, WINWM_DEADCHAR,
74 WM_CHAR, WINWM_SYSKEYDOWN,
75 WM_CHAR, WINWM_SYSKEYUP,
76 WM_CHAR, WINWM_SYSCHAR,
77 WM_CHAR, WINWM_SYSDEADCHAR,
78 WM_CHAR, WINWM_KEYLAST,
79
80 //
81 WM_TIMER, WINWM_TIMER,
82
83 //
84 WM_MOUSEMOVE, WINWM_MOUSEMOVE,
85 WM_BUTTON1DOWN, WINWM_LBUTTONDOWN,
86 WM_BUTTON1UP, WINWM_LBUTTONUP,
87 WM_BUTTON1DBLCLK, WINWM_LBUTTONDBLCLK,
88 WM_BUTTON2DOWN, WINWM_RBUTTONDOWN,
89 WM_BUTTON2UP, WINWM_RBUTTONUP,
90 WM_BUTTON2DBLCLK, WINWM_RBUTTONDBLCLK,
91 WM_BUTTON3DOWN, WINWM_MBUTTONDOWN,
92 WM_BUTTON3UP, WINWM_MBUTTONUP,
93 WM_BUTTON3DBLCLK, WINWM_MBUTTONDBLCLK,
94
95 999999999, 999999999,
96};
97#define MAX_MSGTRANSTAB (sizeof(MsgTransTab)/sizeof(MsgTransTab[0]))
98
99//******************************************************************************
100//******************************************************************************
101void WinToOS2MsgTranslate(MSG *winMsg, QMSG *os2Msg, BOOL isUnicode)
102{
103// memcpy(os2Msg, winMsg, sizeof(MSG));
104// os2Msg->hwnd = Win32ToOS2Handle(winMsg->hwnd);
105// os2Msg->reserved = 0;
106}
107//******************************************************************************
108//TODO: NOT COMPLETE nor 100% CORRECT!!!
109//If both the minimum & maximum message are unknown, the result can be wrong (max > min)!
110//******************************************************************************
111ULONG TranslateWinMsg(ULONG msg, BOOL fMinFilter)
112{
113 if(msg == 0)
114 return 0;
115
116 if(msg >= WINWM_USER)
117 return WIN32APP_POSTMSG;
118
119 for(int i=0;i<MAX_MSGTRANSTAB;i++)
120 {
121 if(fMinFilter && MsgTransTab[i].msgWin32 >= msg) {
122 return MsgTransTab[i].msgOS2;
123 }
124 else
125 if(!fMinFilter && MsgTransTab[i].msgWin32 >= msg) {
126 if(MsgTransTab[i].msgWin32 == msg)
127 return MsgTransTab[i].msgOS2;
128 else return MsgTransTab[i-1].msgOS2;
129 }
130 }
131
132 //not found, probaby WIN32APP_POSTMSG
133 return WIN32APP_POSTMSG;
134}
135//******************************************************************************
136//******************************************************************************
137void OSLibWinPostQuitMessage(ULONG nExitCode)
138{
139 APIRET rc;
140
141 rc = WinPostQueueMsg(NULLHANDLE, WM_QUIT, MPFROMLONG(nExitCode), 0);
142 dprintf(("WinPostQueueMsg %d returned %d", nExitCode, rc));
143}
144//******************************************************************************
145//******************************************************************************
146LONG OSLibWinDispatchMsg(MSG *msg, BOOL isUnicode)
147{
148 TEB *teb;
149 QMSG os2msg;
150 LONG rc;
151
152 teb = GetThreadTEB();
153 if(teb == NULL) {
154 DebugInt3();
155 return FALSE;
156 }
157
158 //TODO: What to do if app changed msg? (translate)
159 // WinToOS2MsgTranslate(msg, &qmsg, isUnicode);
160
161 if(!memcmp(msg, &teb->o.odin.winmsg, sizeof(MSG)) || msg->hwnd == 0) {
162 memcpy(&os2msg, &teb->o.odin.os2msg, sizeof(QMSG));
163 teb->o.odin.os2msg.time = -1;
164 teb->o.odin.winmsg.time = -1;
165 if(msg->hwnd) {
166 teb->o.odin.nrOfMsgs = 1;
167 teb->o.odin.msgstate++; //odd -> next call to our PM window handler should dispatch the translated msg
168 memcpy(&teb->o.odin.msg, msg, sizeof(MSG));
169 }
170 if(os2msg.hwnd || os2msg.msg == WM_QUIT) {
171 memset(&teb->o.odin.os2msg, 0, sizeof(teb->o.odin.os2msg));
172 memset(&teb->o.odin.winmsg, 0, sizeof(teb->o.odin.winmsg));
173 return (LONG)WinDispatchMsg(teb->o.odin.hab, &os2msg);
174 }
175 //SvL: Don't dispatch messages sent by PostThreadMessage (correct??)
176 // Or WM_TIMER msgs with no window handle or timer proc
177 return 0;
178
179 }
180 else {//is this allowed?
181// dprintf(("WARNING: OSLibWinDispatchMsg: called with own message!"));
182 return SendMessageA(msg->hwnd, msg->message, msg->wParam, msg->lParam);
183 }
184}
185//******************************************************************************
186//******************************************************************************
187BOOL OSLibWinGetMsg(LPMSG pMsg, HWND hwnd, UINT uMsgFilterMin, UINT uMsgFilterMax,
188 BOOL isUnicode)
189{
190 BOOL rc, eaten;
191 TEB *teb;
192 QMSG os2msg;
193 HWND hwndOS2 = 0;
194 ULONG filtermin, filtermax;
195
196 if(hwnd) {
197 hwndOS2 = Win32ToOS2Handle(hwnd);
198 if(hwndOS2 == NULL) {
199 memset(pMsg, 0, sizeof(MSG));
200 dprintf(("GetMsg: window %x NOT FOUND!", hwnd));
201 SetLastError(ERROR_INVALID_WINDOW_HANDLE_W);
202 return TRUE;
203 }
204 }
205
206 teb = GetThreadTEB();
207 if(teb == NULL) {
208 DebugInt3();
209 return TRUE;
210 }
211
212 if(teb->o.odin.fTranslated && (!hwnd || hwnd == teb->o.odin.msgWCHAR.hwnd)) {
213 if(uMsgFilterMin) {
214 if(teb->o.odin.msgWCHAR.message < uMsgFilterMin)
215 goto continuegetmsg;
216 }
217 if(uMsgFilterMax) {
218 if(teb->o.odin.msgWCHAR.message > uMsgFilterMax)
219 goto continuegetmsg;
220 }
221 teb->o.odin.fTranslated = FALSE;
222 memcpy(pMsg, &teb->o.odin.msgWCHAR, sizeof(MSG));
223 teb->o.odin.os2msg.msg = 0;
224 teb->o.odin.os2msg.hwnd = 0;
225 return (pMsg->message != WINWM_QUIT);
226 }
227
228continuegetmsg:
229 if(hwnd) {
230 filtermin = TranslateWinMsg(uMsgFilterMin, TRUE);
231 filtermax = TranslateWinMsg(uMsgFilterMax, FALSE);
232 if(filtermin > filtermax) {
233 ULONG tmp = filtermin;
234 filtermin = filtermax;
235 filtermax = filtermin;
236 }
237 do {
238 WinWaitMsg(teb->o.odin.hab, filtermin, filtermax);
239 rc = OSLibWinPeekMsg(pMsg, hwnd, uMsgFilterMin, uMsgFilterMax, PM_REMOVE_W, isUnicode);
240 }
241 while(rc == FALSE);
242
243 return (pMsg->message != WINWM_QUIT);
244 }
245 else
246 {
247 filtermin = TranslateWinMsg(uMsgFilterMin, TRUE);
248 filtermax = TranslateWinMsg(uMsgFilterMax, FALSE);
249 if(filtermin > filtermax) {
250 ULONG tmp = filtermin;
251 filtermin = filtermax;
252 filtermax = filtermin;
253 }
254 do {
255 eaten = FALSE;
256 rc = WinGetMsg(teb->o.odin.hab, &os2msg, filtermin, filtermax, 0);
257 if (os2msg.msg == WM_TIMER)
258 eaten = TIMER_HandleTimer(&os2msg);
259 } while (eaten);
260 }
261
262 OS2ToWinMsgTranslate((PVOID)teb, &os2msg, pMsg, isUnicode, MSG_REMOVE);
263 memcpy(&teb->o.odin.os2msg, &os2msg, sizeof(QMSG));
264 memcpy(&teb->o.odin.winmsg, pMsg, sizeof(MSG));
265
266 if(pMsg->message <= WINWM_KEYLAST && pMsg->message >= WINWM_KEYDOWN)
267 {
268 if(ProcessKbdHook(pMsg, TRUE))
269 goto continuegetmsg;
270 }
271 return rc;
272}
273//******************************************************************************
274//PeekMessage retrieves only messages associated with the window identified by the
275//hwnd parameter or any of its children as specified by the IsChild function, and within
276//the range of message values given by the uMsgFilterMin and uMsgFilterMax
277//parameters. If hwnd is NULL, PeekMessage retrieves messages for any window that
278//belongs to the current thread making the call. (PeekMessage does not retrieve
279//messages for windows that belong to other threads.) If hwnd is -1, PeekMessage only
280//returns messages with a hwnd value of NULL, as posted by the PostAppMessage
281//function. If uMsgFilterMin and uMsgFilterMax are both zero, PeekMessage returns all
282//available messages (no range filtering is performed).
283//TODO: Not working as specified right now!
284//******************************************************************************
285BOOL OSLibWinPeekMsg(LPMSG pMsg, HWND hwnd, UINT uMsgFilterMin, UINT uMsgFilterMax,
286 DWORD fRemove, BOOL isUnicode)
287{
288 BOOL rc, eaten;
289 TEB *teb;
290 QMSG os2msg;
291 HWND hwndOS2 = 0;
292
293 if(hwnd && hwnd != -1) {
294 hwndOS2 = Win32ToOS2Handle(hwnd);
295 if(hwndOS2 == NULL) {
296 dprintf(("PeekMsg: window %x NOT FOUND!", hwnd));
297 SetLastError(ERROR_INVALID_WINDOW_HANDLE_W);
298 return FALSE;
299 }
300 }
301
302 teb = GetThreadTEB();
303 if(teb == NULL) {
304 DebugInt3();
305 return FALSE;
306 }
307
308 if(teb->o.odin.fTranslated && (!hwnd || hwnd == teb->o.odin.msgWCHAR.hwnd)) {
309 if(uMsgFilterMin) {
310 if(teb->o.odin.msgWCHAR.message < uMsgFilterMin)
311 goto continuepeekmsg;
312 }
313 if(uMsgFilterMax) {
314 if(teb->o.odin.msgWCHAR.message > uMsgFilterMax)
315 goto continuepeekmsg;
316 }
317
318 if(fRemove & PM_REMOVE_W) {
319 teb->o.odin.fTranslated = FALSE;
320 teb->o.odin.os2msg.msg = 0;
321 teb->o.odin.os2msg.hwnd = 0;
322 }
323 memcpy(pMsg, &teb->o.odin.msgWCHAR, sizeof(MSG));
324 return TRUE;
325 }
326
327continuepeekmsg:
328 do {
329 eaten = FALSE;
330 rc = WinPeekMsg(teb->o.odin.hab, &os2msg, hwndOS2, TranslateWinMsg(uMsgFilterMin, TRUE),
331 TranslateWinMsg(uMsgFilterMax, FALSE), (fRemove & PM_REMOVE_W) ? PM_REMOVE : PM_NOREMOVE);
332
333 if (rc && (fRemove & PM_REMOVE_W) && os2msg.msg == WM_TIMER) {
334 eaten = TIMER_HandleTimer(&os2msg);
335 }
336 }
337 while (eaten && rc);
338
339 if(rc == FALSE) {
340 return FALSE;
341 }
342
343 OS2ToWinMsgTranslate((PVOID)teb, &os2msg, pMsg, isUnicode, (fRemove & PM_REMOVE_W) ? MSG_REMOVE : MSG_NOREMOVE);
344 //TODO: This is not safe! There's no guarantee this message will be dispatched and it might overwrite a previous message
345 if(fRemove & PM_REMOVE_W) {
346 memcpy(&teb->o.odin.os2msg, &os2msg, sizeof(QMSG));
347 memcpy(&teb->o.odin.winmsg, pMsg, sizeof(MSG));
348 }
349
350 if(pMsg->message <= WINWM_KEYLAST && pMsg->message >= WINWM_KEYDOWN)
351 {
352 if(ProcessKbdHook(pMsg, fRemove))
353 goto continuepeekmsg;
354 }
355
356 return rc;
357}
358//******************************************************************************
359//******************************************************************************
360ULONG OSLibWinQueryMsgTime()
361{
362 return WinQueryMsgTime(GetThreadHAB());
363}
364//******************************************************************************
365//******************************************************************************
366BOOL OSLibWinWaitMessage()
367{
368 return WinWaitMsg(GetThreadHAB(), 0, 0);
369}
370//******************************************************************************
371//TODO: QS_HOTKEY
372//******************************************************************************
373ULONG OSLibWinQueryQueueStatus()
374{
375 ULONG statusOS2, statusWin32 = 0;
376
377 statusOS2 = WinQueryQueueStatus(HWND_DESKTOP);
378
379 if(statusOS2 & QS_KEY)
380 statusWin32 |= QS_KEY_W;
381 if(statusOS2 & QS_MOUSEBUTTON)
382 statusWin32 |= QS_MOUSEBUTTON_W;
383 if(statusOS2 & QS_MOUSEMOVE)
384 statusWin32 |= QS_MOUSEMOVE_W;
385 if(statusOS2 & QS_TIMER)
386 statusWin32 |= QS_TIMER_W;
387 if(statusOS2 & QS_PAINT)
388 statusWin32 |= QS_PAINT_W;
389 if(statusOS2 & QS_POSTMSG)
390 statusWin32 |= QS_POSTMESSAGE_W;
391 if(statusOS2 & QS_SENDMSG)
392 statusWin32 |= QS_SENDMESSAGE_W;
393
394 return statusWin32;
395}
396//******************************************************************************
397//******************************************************************************
398BOOL OSLibWinInSendMessage()
399{
400 return WinInSendMsg(GetThreadHAB());
401}
402//******************************************************************************
403//******************************************************************************
404DWORD OSLibWinGetMessagePos()
405{
406 APIRET rc;
407 POINTL ptl;
408
409 rc = WinQueryMsgPos(GetThreadHAB(), &ptl);
410 if(!rc) {
411 return 0;
412 }
413 //convert to windows coordinates
414 return MAKEULONG(ptl.x,mapScreenY(ptl.y));
415}
416//******************************************************************************
417//******************************************************************************
418LONG OSLibWinGetMessageTime()
419{
420 return (LONG)WinQueryMsgTime(GetThreadHAB());
421}
422//******************************************************************************
423//******************************************************************************
424BOOL OSLibWinReplyMessage(ULONG result)
425{
426 return (BOOL)WinReplyMsg( NULLHANDLE, NULLHANDLE, HMQ_CURRENT, (MRESULT)result);
427}
428//******************************************************************************
429//******************************************************************************
430ULONG OSLibSendMessage(HWND hwnd, ULONG msg, ULONG wParam, ULONG lParam, BOOL fUnicode)
431{
432 POSTMSG_PACKET *packet = (POSTMSG_PACKET *)_smalloc(sizeof(POSTMSG_PACKET));
433
434 packet->Msg = msg;
435 packet->wParam = wParam;
436 packet->lParam = lParam;
437
438 return (ULONG)WinSendMsg(hwnd, WIN32APP_POSTMSG, (MPARAM)((fUnicode) ? WIN32MSG_MAGICW : WIN32MSG_MAGICA), (MPARAM)packet);
439}
440//******************************************************************************
441//******************************************************************************
442ULONG OSLibWinBroadcastMsg(ULONG msg, ULONG wParam, ULONG lParam, BOOL fSend)
443{
444 return WinBroadcastMsg(HWND_DESKTOP, msg, (MPARAM)wParam, (MPARAM)lParam,
445 (fSend) ? BMSG_SEND : BMSG_POST);
446}
447//******************************************************************************
448//******************************************************************************
449BOOL OSLibPostMessage(HWND hwnd, ULONG msg, ULONG wParam, ULONG lParam, BOOL fUnicode)
450{
451 POSTMSG_PACKET *packet = (POSTMSG_PACKET *)_smalloc(sizeof(POSTMSG_PACKET));
452
453 packet->Msg = msg;
454 packet->wParam = wParam;
455 packet->lParam = lParam;
456 return WinPostMsg(hwnd, WIN32APP_POSTMSG, (MPARAM)((fUnicode) ? WIN32MSG_MAGICW : WIN32MSG_MAGICA), (MPARAM)packet);
457}
458//******************************************************************************
459BOOL _System _O32_PostThreadMessage( DWORD, UINT, WPARAM, LPARAM );
460
461inline BOOL O32_PostThreadMessage(DWORD a, UINT b, WPARAM c, LPARAM d)
462{
463 BOOL yyrc;
464 USHORT sel = RestoreOS2FS();
465
466 yyrc = _O32_PostThreadMessage(a, b, c, d);
467 SetFS(sel);
468
469 return yyrc;
470}
471//******************************************************************************
472BOOL OSLibPostThreadMessage(ULONG threadid, UINT msg, WPARAM wParam, LPARAM lParam, BOOL fUnicode)
473{
474// TEB *teb = GetTEBFromThreadId(threadid);
475 POSTMSG_PACKET *packet = (POSTMSG_PACKET *)_smalloc(sizeof(POSTMSG_PACKET));
476
477// if(teb == NULL) {
478// dprintf(("OSLibPostThreadMessage: thread %x not found!", threadid));
479// return FALSE;
480// }
481 dprintf(("PostThreadMessageA %x %x %x %x", threadid, msg, wParam, lParam));
482 packet->Msg = msg;
483 packet->wParam = wParam;
484 packet->lParam = lParam;
485 return O32_PostThreadMessage(threadid, WIN32APP_POSTMSG-OPEN32_MSGDIFF, ((fUnicode) ? WIN32MSG_MAGICW : WIN32MSG_MAGICA), (LPARAM)packet);
486}
487//******************************************************************************
488//******************************************************************************
489
Note: See TracBrowser for help on using the repository browser.