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

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

Peek/GetMessage bugfixes

File size: 16.1 KB
Line 
1/* $Id: oslibmsg.cpp,v 1.31 2000-03-29 17:16:06 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 <win32wnd.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 WM_COMMAND, WINWM_COMMAND,
81 WM_SYSCOMMAND, WINWM_SYSCOMMAND,
82 //
83 WM_TIMER, WINWM_TIMER,
84 WM_INITMENU, WINWM_INITMENU,
85 //
86 WM_MOUSEMOVE, WINWM_MOUSEMOVE,
87 WM_BUTTON1DOWN, WINWM_LBUTTONDOWN,
88 WM_BUTTON1UP, WINWM_LBUTTONUP,
89 WM_BUTTON1DBLCLK, WINWM_LBUTTONDBLCLK,
90 WM_BUTTON2DOWN, WINWM_RBUTTONDOWN,
91 WM_BUTTON2UP, WINWM_RBUTTONUP,
92 WM_BUTTON2DBLCLK, WINWM_RBUTTONDBLCLK,
93 WM_BUTTON3DOWN, WINWM_MBUTTONDOWN,
94 WM_BUTTON3UP, WINWM_MBUTTONUP,
95 WM_BUTTON3DBLCLK, WINWM_MBUTTONDBLCLK,
96
97 999999999, 999999999,
98};
99#define MAX_MSGTRANSTAB (sizeof(MsgTransTab)/sizeof(MsgTransTab[0]))
100
101QMSG *MsgThreadPtr = 0;
102
103LRESULT WIN32API SendMessageA(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
104
105//******************************************************************************
106//******************************************************************************
107BOOL OSLibInitMsgQueue()
108{
109 if(DosAllocThreadLocalMemory(sizeof(QMSG)/sizeof(ULONG), (PULONG *)&MsgThreadPtr) != 0)
110 {
111 dprintf(("OSLibInitMsgQueue: local thread memory alloc failed!!"));
112 DebugInt3();
113 return FALSE;
114 }
115 return TRUE;
116}
117//******************************************************************************
118//******************************************************************************
119void WinToOS2MsgTranslate(MSG *winMsg, QMSG *os2Msg, BOOL isUnicode)
120{
121// memcpy(os2Msg, winMsg, sizeof(MSG));
122// os2Msg->hwnd = Win32Window::Win32ToOS2Handle(winMsg->hwnd);
123// os2Msg->reserved = 0;
124}
125//******************************************************************************
126//TODO: NOT COMPLETE nor 100% CORRECT!!!
127//If both the minimum & maximum message are unknown, the result can be wrong (max > min)!
128//******************************************************************************
129ULONG TranslateWinMsg(ULONG msg, BOOL fMinFilter)
130{
131 if(msg == 0)
132 return 0;
133
134 if(msg >= WINWM_USER)
135 return WIN32APP_POSTMSG;
136
137 for(int i=0;i<MAX_MSGTRANSTAB;i++)
138 {
139 if(fMinFilter && MsgTransTab[i].msgWin32 >= msg) {
140 return MsgTransTab[i].msgOS2;
141 }
142 else
143 if(!fMinFilter && MsgTransTab[i].msgWin32 >= msg) {
144 if(MsgTransTab[i].msgWin32 == msg)
145 return MsgTransTab[i].msgOS2;
146 else return MsgTransTab[i-1].msgOS2;
147 }
148 }
149
150 return 0;
151}
152//******************************************************************************
153//******************************************************************************
154void OSLibWinPostQuitMessage(ULONG nExitCode)
155{
156 APIRET rc;
157
158 rc = WinPostQueueMsg(NULLHANDLE, WM_QUIT, MPFROMLONG(nExitCode), 0);
159 dprintf(("WinPostQueueMsg %d returned %d", nExitCode, rc));
160}
161//******************************************************************************
162//******************************************************************************
163LONG OSLibWinDispatchMsg(MSG *msg, BOOL isUnicode)
164{
165 THDB *thdb;
166 QMSG os2msg;
167 LONG rc;
168
169 thdb = GetThreadTHDB();
170 if(thdb == NULL) {
171 DebugInt3();
172 return FALSE;
173 }
174
175 //TODO: What to do if app changed msg? (translate)
176 // WinToOS2MsgTranslate(msg, &qmsg, isUnicode);
177
178 if(msg->time == MsgThreadPtr->time || msg->hwnd == 0) {
179 memcpy(&os2msg, MsgThreadPtr, sizeof(QMSG));
180 MsgThreadPtr->time = -1;
181 if(msg->hwnd) {
182 thdb->nrOfMsgs = 1;
183 thdb->msgstate++; //odd -> next call to our PM window handler should dispatch the translated msg
184 memcpy(&thdb->msg, msg, sizeof(MSG));
185 }
186 if(os2msg.hwnd || os2msg.msg == WM_QUIT) {
187 return (LONG)WinDispatchMsg(thdb->hab, &os2msg);
188 }
189 //SvL: Don't dispatch messages sent by PostThreadMessage (correct??)
190 // Or WM_TIMER msgs with no window handle or timer proc
191 return 0;
192
193 }
194 else {//is this allowed?
195// dprintf(("WARNING: OSLibWinDispatchMsg: called with own message!"));
196 return SendMessageA(msg->hwnd, msg->message, msg->wParam, msg->lParam);
197 }
198}
199//******************************************************************************
200//******************************************************************************
201BOOL OSLibWinGetMsg(LPMSG pMsg, HWND hwnd, UINT uMsgFilterMin, UINT uMsgFilterMax,
202 BOOL isUnicode)
203{
204 BOOL rc, eaten;
205 THDB *thdb;
206 QMSG os2msg;
207 HWND hwndOS2 = 0;
208
209 if(hwnd) {
210 hwndOS2 = Win32BaseWindow::Win32ToOS2Handle(hwnd);
211 if(hwndOS2 == NULL) {
212 memset(pMsg, 0, sizeof(MSG));
213 dprintf(("GetMsg: window %x NOT FOUND!", hwnd));
214 SetLastError(ERROR_INVALID_WINDOW_HANDLE_W);
215 return TRUE;
216 }
217 }
218
219 thdb = GetThreadTHDB();
220 if(thdb == NULL) {
221 DebugInt3();
222 return TRUE;
223 }
224
225 if(thdb->fTranslated && (!hwnd || hwnd == thdb->msgWCHAR.hwnd)) {
226 if(uMsgFilterMin) {
227 if(thdb->msgWCHAR.message < uMsgFilterMin)
228 goto continuegetmsg;
229 }
230 if(uMsgFilterMax) {
231 if(thdb->msgWCHAR.message > uMsgFilterMax)
232 goto continuegetmsg;
233 }
234 thdb->fTranslated = FALSE;
235 memcpy(pMsg, &thdb->msgWCHAR, sizeof(MSG));
236 MsgThreadPtr->msg = 0;
237 MsgThreadPtr->hwnd = 0;
238 return (pMsg->message != WINWM_QUIT);
239 }
240
241continuegetmsg:
242 if(hwnd) {
243 do {
244 WinWaitMsg(thdb->hab, TranslateWinMsg(uMsgFilterMin, TRUE), TranslateWinMsg(uMsgFilterMax, FALSE));
245 rc = OSLibWinPeekMsg(pMsg, hwnd, uMsgFilterMin, uMsgFilterMax, PM_REMOVE_W, isUnicode);
246 }
247 while(rc == FALSE);
248
249 return (pMsg->message != WINWM_QUIT);
250 }
251 else
252 {
253 do {
254 eaten = FALSE;
255 rc = WinGetMsg(thdb->hab, &os2msg, TranslateWinMsg(uMsgFilterMin, TRUE), TranslateWinMsg(uMsgFilterMax, FALSE), 0);
256 if (os2msg.msg == WM_TIMER)
257 eaten = TIMER_HandleTimer(&os2msg);
258 } while (eaten);
259 }
260
261 OS2ToWinMsgTranslate((PVOID)thdb, &os2msg, pMsg, isUnicode, MSG_REMOVE);
262 memcpy(MsgThreadPtr, &os2msg, sizeof(QMSG));
263
264 if(pMsg->message <= WINWM_KEYLAST && pMsg->message >= WINWM_KEYDOWN)
265 {
266 if(ProcessKbdHook(pMsg, TRUE))
267 goto continuegetmsg;
268 }
269 return rc;
270}
271//******************************************************************************
272//PeekMessage retrieves only messages associated with the window identified by the
273//hwnd parameter or any of its children as specified by the IsChild function, and within
274//the range of message values given by the uMsgFilterMin and uMsgFilterMax
275//parameters. If hwnd is NULL, PeekMessage retrieves messages for any window that
276//belongs to the current thread making the call. (PeekMessage does not retrieve
277//messages for windows that belong to other threads.) If hwnd is -1, PeekMessage only
278//returns messages with a hwnd value of NULL, as posted by the PostAppMessage
279//function. If uMsgFilterMin and uMsgFilterMax are both zero, PeekMessage returns all
280//available messages (no range filtering is performed).
281//TODO: Not working as specified right now!
282//******************************************************************************
283BOOL OSLibWinPeekMsg(LPMSG pMsg, HWND hwnd, UINT uMsgFilterMin, UINT uMsgFilterMax,
284 DWORD fRemove, BOOL isUnicode)
285{
286 BOOL rc, eaten;
287 THDB *thdb;
288 QMSG os2msg;
289 HWND hwndOS2 = 0;
290
291 if(hwnd && hwnd != -1) {
292 hwndOS2 = Win32BaseWindow::Win32ToOS2Handle(hwnd);
293 if(hwndOS2 == NULL) {
294 dprintf(("PeekMsg: window %x NOT FOUND!", hwnd));
295 SetLastError(ERROR_INVALID_WINDOW_HANDLE_W);
296 return FALSE;
297 }
298 }
299
300 thdb = GetThreadTHDB();
301 if(thdb == NULL) {
302 DebugInt3();
303 return FALSE;
304 }
305
306 if(thdb->fTranslated && (!hwnd || hwnd == thdb->msgWCHAR.hwnd)) {
307 if(uMsgFilterMin) {
308 if(thdb->msgWCHAR.message < uMsgFilterMin)
309 goto continuepeekmsg;
310 }
311 if(uMsgFilterMax) {
312 if(thdb->msgWCHAR.message > uMsgFilterMax)
313 goto continuepeekmsg;
314 }
315
316 if(fRemove & PM_REMOVE_W) {
317 thdb->fTranslated = FALSE;
318 MsgThreadPtr->msg = 0;
319 MsgThreadPtr->hwnd = 0;
320 }
321 memcpy(pMsg, &thdb->msgWCHAR, sizeof(MSG));
322 return TRUE;
323 }
324
325continuepeekmsg:
326 do {
327 eaten = FALSE;
328 rc = WinPeekMsg(thdb->hab, &os2msg, hwndOS2, TranslateWinMsg(uMsgFilterMin, TRUE),
329 TranslateWinMsg(uMsgFilterMax, FALSE), (fRemove & PM_REMOVE_W) ? PM_REMOVE : PM_NOREMOVE);
330
331 if (rc && (fRemove & PM_REMOVE_W) && os2msg.msg == WM_TIMER) {
332 eaten = TIMER_HandleTimer(&os2msg);
333 }
334 }
335 while (eaten && rc);
336
337 if(rc == FALSE) {
338 return FALSE;
339 }
340
341 OS2ToWinMsgTranslate((PVOID)thdb, &os2msg, pMsg, isUnicode, (fRemove & PM_REMOVE_W) ? MSG_REMOVE : MSG_NOREMOVE);
342 //TODO: This is not safe! There's no guarantee this message will be dispatched and it might overwrite a previous message
343 if(fRemove & PM_REMOVE_W) {
344 memcpy(MsgThreadPtr, &os2msg, sizeof(QMSG));
345 }
346
347 if(pMsg->message <= WINWM_KEYLAST && pMsg->message >= WINWM_KEYDOWN)
348 {
349 if(ProcessKbdHook(pMsg, fRemove))
350 goto continuepeekmsg;
351 }
352
353 return rc;
354}
355//******************************************************************************
356//******************************************************************************
357ULONG OSLibWinQueryMsgTime()
358{
359 return WinQueryMsgTime(GetThreadHAB());
360}
361//******************************************************************************
362//******************************************************************************
363BOOL OSLibWinWaitMessage()
364{
365 return WinWaitMsg(GetThreadHAB(), 0, 0);
366}
367//******************************************************************************
368//TODO: QS_HOTKEY
369//******************************************************************************
370ULONG OSLibWinQueryQueueStatus()
371{
372 ULONG statusOS2, statusWin32 = 0;
373
374 statusOS2 = WinQueryQueueStatus(HWND_DESKTOP);
375
376 if(statusOS2 & QS_KEY)
377 statusWin32 |= QS_KEY_W;
378 if(statusOS2 & QS_MOUSEBUTTON)
379 statusWin32 |= QS_MOUSEBUTTON_W;
380 if(statusOS2 & QS_MOUSEMOVE)
381 statusWin32 |= QS_MOUSEMOVE_W;
382 if(statusOS2 & QS_TIMER)
383 statusWin32 |= QS_TIMER_W;
384 if(statusOS2 & QS_PAINT)
385 statusWin32 |= QS_PAINT_W;
386 if(statusOS2 & QS_POSTMSG)
387 statusWin32 |= QS_POSTMESSAGE_W;
388 if(statusOS2 & QS_SENDMSG)
389 statusWin32 |= QS_SENDMESSAGE_W;
390
391 return statusWin32;
392}
393//******************************************************************************
394//******************************************************************************
395BOOL OSLibWinInSendMessage()
396{
397 return WinInSendMsg(GetThreadHAB());
398}
399//******************************************************************************
400//******************************************************************************
401DWORD OSLibWinGetMessagePos()
402{
403 APIRET rc;
404 POINTL ptl;
405
406 rc = WinQueryMsgPos(GetThreadHAB(), &ptl);
407 if(!rc) {
408 return 0;
409 }
410 //convert to windows coordinates
411 return MAKEULONG(ptl.x,mapScreenY(ptl.y));
412}
413//******************************************************************************
414//******************************************************************************
415LONG OSLibWinGetMessageTime()
416{
417 return (LONG)WinQueryMsgTime(GetThreadHAB());
418}
419//******************************************************************************
420//******************************************************************************
421BOOL OSLibWinReplyMessage(ULONG result)
422{
423 return (BOOL)WinReplyMsg( NULLHANDLE, NULLHANDLE, HMQ_CURRENT, (MRESULT)result);
424}
425//******************************************************************************
426//******************************************************************************
427ULONG OSLibSendMessage(HWND hwnd, ULONG msg, ULONG wParam, ULONG lParam, BOOL fUnicode)
428{
429 POSTMSG_PACKET *packet = (POSTMSG_PACKET *)_smalloc(sizeof(POSTMSG_PACKET));
430
431 packet->Msg = msg;
432 packet->wParam = wParam;
433 packet->lParam = lParam;
434
435 return (ULONG)WinSendMsg(hwnd, WIN32APP_POSTMSG, (MPARAM)((fUnicode) ? WIN32MSG_MAGICW : WIN32MSG_MAGICA), (MPARAM)packet);
436}
437//******************************************************************************
438//******************************************************************************
439ULONG OSLibWinBroadcastMsg(ULONG msg, ULONG wParam, ULONG lParam, BOOL fSend)
440{
441 return WinBroadcastMsg(HWND_DESKTOP, msg, (MPARAM)wParam, (MPARAM)lParam,
442 (fSend) ? BMSG_SEND : BMSG_POST);
443}
444//******************************************************************************
445//******************************************************************************
446BOOL OSLibPostMessage(HWND hwnd, ULONG msg, ULONG wParam, ULONG lParam, BOOL fUnicode)
447{
448 POSTMSG_PACKET *packet = (POSTMSG_PACKET *)_smalloc(sizeof(POSTMSG_PACKET));
449
450 packet->Msg = msg;
451 packet->wParam = wParam;
452 packet->lParam = lParam;
453 return WinPostMsg(hwnd, WIN32APP_POSTMSG, (MPARAM)((fUnicode) ? WIN32MSG_MAGICW : WIN32MSG_MAGICA), (MPARAM)packet);
454}
455//******************************************************************************
456BOOL _System _O32_PostThreadMessage( DWORD, UINT, WPARAM, LPARAM );
457
458inline BOOL O32_PostThreadMessage(DWORD a, UINT b, WPARAM c, LPARAM d)
459{
460 BOOL yyrc;
461 USHORT sel = RestoreOS2FS();
462
463 yyrc = _O32_PostThreadMessage(a, b, c, d);
464 SetFS(sel);
465
466 return yyrc;
467}
468//******************************************************************************
469BOOL OSLibPostThreadMessage(ULONG threadid, UINT msg, WPARAM wParam, LPARAM lParam, BOOL fUnicode)
470{
471// THDB *thdb = GetTHDBFromThreadId(threadid);
472 POSTMSG_PACKET *packet = (POSTMSG_PACKET *)_smalloc(sizeof(POSTMSG_PACKET));
473
474// if(thdb == NULL) {
475// dprintf(("OSLibPostThreadMessage: thread %x not found!", threadid));
476// return FALSE;
477// }
478 dprintf(("PostThreadMessageA %x %x %x %x", threadid, msg, wParam, lParam));
479 packet->Msg = msg;
480 packet->wParam = wParam;
481 packet->lParam = lParam;
482 return O32_PostThreadMessage(threadid, WIN32APP_POSTMSG-OPEN32_MSGDIFF, ((fUnicode) ? WIN32MSG_MAGICW : WIN32MSG_MAGICA), (LPARAM)packet);
483}
484//******************************************************************************
485//******************************************************************************
486
Note: See TracBrowser for help on using the repository browser.