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

Last change on this file since 6501 was 6501, checked in by sandervl, 24 years ago

rewrote PostThreadMessage + query message queue of first thread with WinQueueFromId

File size: 18.2 KB
Line 
1/* $Id: oslibmsg.cpp,v 1.41 2001-08-09 08:45:40 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: not always right if mouse msg turns out to be for the client window
70 WM_MOUSEMOVE, WINWM_NCMOUSEMOVE,
71 WM_BUTTON1DOWN, WINWM_NCLBUTTONDOWN,
72 WM_BUTTON1UP, WINWM_NCLBUTTONUP,
73 WM_BUTTON1DBLCLK, WINWM_NCLBUTTONDBLCLK,
74 WM_BUTTON2DOWN, WINWM_NCRBUTTONDOWN,
75 WM_BUTTON2UP, WINWM_NCRBUTTONUP,
76 WM_BUTTON2DBLCLK, WINWM_NCRBUTTONDBLCLK,
77 WM_BUTTON3DOWN, WINWM_NCMBUTTONDOWN,
78 WM_BUTTON3UP, WINWM_NCMBUTTONUP,
79 WM_BUTTON3DBLCLK, WINWM_NCMBUTTONDBLCLK,
80
81 //TODO: Needs better translation!
82 WM_CHAR, WINWM_KEYDOWN,
83 WM_CHAR, WINWM_KEYUP,
84 WM_CHAR, WINWM_CHAR,
85 WM_CHAR, WINWM_DEADCHAR,
86 WM_CHAR, WINWM_SYSKEYDOWN,
87 WM_CHAR, WINWM_SYSKEYUP,
88 WM_CHAR, WINWM_SYSCHAR,
89 WM_CHAR, WINWM_SYSDEADCHAR,
90 WM_CHAR, WINWM_KEYLAST,
91
92 //
93 WM_TIMER, WINWM_TIMER,
94
95 //
96 //todo: not always right if mouse msg turns out to be for the nonclient window
97 WM_MOUSEMOVE, WINWM_MOUSEMOVE,
98 WM_BUTTON1DOWN, WINWM_LBUTTONDOWN,
99 WM_BUTTON1UP, WINWM_LBUTTONUP,
100 WM_BUTTON1DBLCLK, WINWM_LBUTTONDBLCLK,
101 WM_BUTTON2DOWN, WINWM_RBUTTONDOWN,
102 WM_BUTTON2UP, WINWM_RBUTTONUP,
103 WM_BUTTON2DBLCLK, WINWM_RBUTTONDBLCLK,
104 WM_BUTTON3DOWN, WINWM_MBUTTONDOWN,
105 WM_BUTTON3UP, WINWM_MBUTTONUP,
106 WM_BUTTON3DBLCLK, WINWM_MBUTTONDBLCLK,
107
108 999999999, 999999999,
109};
110#define MAX_MSGTRANSTAB (sizeof(MsgTransTab)/sizeof(MsgTransTab[0]))
111
112//******************************************************************************
113//******************************************************************************
114void WinToOS2MsgTranslate(MSG *winMsg, QMSG *os2Msg, BOOL isUnicode)
115{
116// memcpy(os2Msg, winMsg, sizeof(MSG));
117// os2Msg->hwnd = Win32ToOS2Handle(winMsg->hwnd);
118// os2Msg->reserved = 0;
119}
120//******************************************************************************
121//TODO: NOT COMPLETE nor 100% CORRECT!!!
122//If both the minimum & maximum message are unknown, the result can be wrong (max > min)!
123//******************************************************************************
124ULONG TranslateWinMsg(ULONG msg, BOOL fMinFilter)
125{
126 if(msg == 0)
127 return 0;
128
129 if(msg >= WINWM_USER)
130 return msg + WIN32APP_POSTMSG;
131
132 for(int i=0;i<MAX_MSGTRANSTAB;i++)
133 {
134 if(fMinFilter && MsgTransTab[i].msgWin32 >= msg) {
135 return MsgTransTab[i].msgOS2;
136 }
137 else
138 if(!fMinFilter && MsgTransTab[i].msgWin32 >= msg) {
139 if(MsgTransTab[i].msgWin32 == msg)
140 return MsgTransTab[i].msgOS2;
141 else return MsgTransTab[i-1].msgOS2;
142 }
143 }
144
145 //not found, get everything
146 dprintf(("WARNING: TranslateWinMsg: message %x not found", msg));
147 return 0;
148}
149//******************************************************************************
150//******************************************************************************
151void OSLibWinPostQuitMessage(ULONG nExitCode)
152{
153 APIRET rc;
154
155 rc = WinPostQueueMsg(NULLHANDLE, WM_QUIT, MPFROMLONG(nExitCode), 0);
156 dprintf(("WinPostQueueMsg %d returned %d", nExitCode, rc));
157}
158//******************************************************************************
159//******************************************************************************
160LONG OSLibWinDispatchMsg(MSG *msg, BOOL isUnicode)
161{
162 TEB *teb;
163 QMSG os2msg;
164 LONG rc;
165
166 teb = GetThreadTEB();
167 if(teb == NULL) {
168 DebugInt3();
169 return FALSE;
170 }
171
172 //TODO: What to do if app changed msg? (translate)
173 // WinToOS2MsgTranslate(msg, &qmsg, isUnicode);
174
175 if(!memcmp(msg, &teb->o.odin.winmsg, sizeof(MSG)) || msg->hwnd == 0) {
176 memcpy(&os2msg, &teb->o.odin.os2msg, sizeof(QMSG));
177 teb->o.odin.os2msg.time = -1;
178 teb->o.odin.winmsg.time = -1;
179 if(msg->hwnd) {
180 teb->o.odin.nrOfMsgs = 1;
181 teb->o.odin.msgstate++; //odd -> next call to our PM window handler should dispatch the translated msg
182 memcpy(&teb->o.odin.msg, msg, sizeof(MSG));
183 }
184 if(os2msg.hwnd || os2msg.msg == WM_QUIT) {
185 memset(&teb->o.odin.os2msg, 0, sizeof(teb->o.odin.os2msg));
186 memset(&teb->o.odin.winmsg, 0, sizeof(teb->o.odin.winmsg));
187 return (LONG)WinDispatchMsg(teb->o.odin.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 TEB *teb;
206 QMSG os2msg;
207 HWND hwndOS2 = 0;
208 ULONG filtermin, filtermax;
209
210 if(hwnd) {
211 hwndOS2 = Win32ToOS2Handle(hwnd);
212 if(hwndOS2 == NULL) {
213 memset(pMsg, 0, sizeof(MSG));
214 dprintf(("GetMsg: window %x NOT FOUND!", hwnd));
215 SetLastError(ERROR_INVALID_WINDOW_HANDLE_W);
216 return TRUE;
217 }
218 }
219
220 teb = GetThreadTEB();
221 if(teb == NULL) {
222 DebugInt3();
223 return TRUE;
224 }
225
226 if(teb->o.odin.fTranslated && (!hwnd || hwnd == teb->o.odin.msgWCHAR.hwnd)) {
227 if(uMsgFilterMin) {
228 if(teb->o.odin.msgWCHAR.message < uMsgFilterMin)
229 goto continuegetmsg;
230 }
231 if(uMsgFilterMax) {
232 if(teb->o.odin.msgWCHAR.message > uMsgFilterMax)
233 goto continuegetmsg;
234 }
235 teb->o.odin.fTranslated = FALSE;
236 memcpy(pMsg, &teb->o.odin.msgWCHAR, sizeof(MSG));
237 teb->o.odin.os2msg.msg = 0;
238 teb->o.odin.os2msg.hwnd = 0;
239 return (pMsg->message != WINWM_QUIT);
240 }
241
242continuegetmsg:
243 if(hwnd) {
244 filtermin = TranslateWinMsg(uMsgFilterMin, TRUE);
245 filtermax = TranslateWinMsg(uMsgFilterMax, FALSE);
246 if(filtermin > filtermax) {
247 ULONG tmp = filtermin;
248 filtermin = filtermax;
249 filtermax = filtermin;
250 }
251 do {
252 WinWaitMsg(teb->o.odin.hab, filtermin, filtermax);
253 rc = OSLibWinPeekMsg(pMsg, hwnd, uMsgFilterMin, uMsgFilterMax, PM_REMOVE_W, isUnicode);
254 }
255 while(rc == FALSE);
256
257 return (pMsg->message != WINWM_QUIT);
258 }
259 else
260 {
261 filtermin = TranslateWinMsg(uMsgFilterMin, TRUE);
262 filtermax = TranslateWinMsg(uMsgFilterMax, FALSE);
263 if(filtermin > filtermax) {
264 ULONG tmp = filtermin;
265 filtermin = filtermax;
266 filtermax = filtermin;
267 }
268 do {
269 eaten = FALSE;
270 rc = WinGetMsg(teb->o.odin.hab, &os2msg, filtermin, filtermax, 0);
271 if (os2msg.msg == WM_TIMER)
272 eaten = TIMER_HandleTimer(&os2msg);
273 } while (eaten);
274 }
275 if(OS2ToWinMsgTranslate((PVOID)teb, &os2msg, pMsg, isUnicode, MSG_REMOVE) == FALSE) {
276 //dispatch untranslated message immediately
277 WinDispatchMsg(teb->o.odin.hab, &os2msg);
278 //and get the next one
279 return OSLibWinGetMsg(pMsg, hwnd, uMsgFilterMin, uMsgFilterMax, isUnicode);
280 }
281
282 memcpy(&teb->o.odin.os2msg, &os2msg, sizeof(QMSG));
283 memcpy(&teb->o.odin.winmsg, pMsg, sizeof(MSG));
284
285 if(pMsg->message <= WINWM_KEYLAST && pMsg->message >= WINWM_KEYDOWN)
286 {
287 if(ProcessKbdHook(pMsg, TRUE))
288 goto continuegetmsg;
289 }
290 return rc;
291}
292//******************************************************************************
293//PeekMessage retrieves only messages associated with the window identified by the
294//hwnd parameter or any of its children as specified by the IsChild function, and within
295//the range of message values given by the uMsgFilterMin and uMsgFilterMax
296//parameters. If hwnd is NULL, PeekMessage retrieves messages for any window that
297//belongs to the current thread making the call. (PeekMessage does not retrieve
298//messages for windows that belong to other threads.) If hwnd is -1, PeekMessage only
299//returns messages with a hwnd value of NULL, as posted by the PostAppMessage
300//function. If uMsgFilterMin and uMsgFilterMax are both zero, PeekMessage returns all
301//available messages (no range filtering is performed).
302//TODO: Not working as specified right now!
303//******************************************************************************
304BOOL OSLibWinPeekMsg(LPMSG pMsg, HWND hwnd, UINT uMsgFilterMin, UINT uMsgFilterMax,
305 DWORD fRemove, BOOL isUnicode)
306{
307 BOOL rc, eaten;
308 TEB *teb;
309 QMSG os2msg;
310 HWND hwndOS2 = 0;
311
312 if(hwnd && hwnd != -1) {
313 hwndOS2 = Win32ToOS2Handle(hwnd);
314 if(hwndOS2 == NULL) {
315 dprintf(("PeekMsg: window %x NOT FOUND!", hwnd));
316 SetLastError(ERROR_INVALID_WINDOW_HANDLE_W);
317 return FALSE;
318 }
319 }
320
321 teb = GetThreadTEB();
322 if(teb == NULL) {
323 DebugInt3();
324 return FALSE;
325 }
326
327 if(teb->o.odin.fTranslated && (!hwnd || hwnd == teb->o.odin.msgWCHAR.hwnd)) {
328 if(uMsgFilterMin) {
329 if(teb->o.odin.msgWCHAR.message < uMsgFilterMin)
330 goto continuepeekmsg;
331 }
332 if(uMsgFilterMax) {
333 if(teb->o.odin.msgWCHAR.message > uMsgFilterMax)
334 goto continuepeekmsg;
335 }
336
337 if(fRemove & PM_REMOVE_W) {
338 teb->o.odin.fTranslated = FALSE;
339 teb->o.odin.os2msg.msg = 0;
340 teb->o.odin.os2msg.hwnd = 0;
341 }
342 memcpy(pMsg, &teb->o.odin.msgWCHAR, sizeof(MSG));
343 return TRUE;
344 }
345
346continuepeekmsg:
347 do {
348 eaten = FALSE;
349 rc = WinPeekMsg(teb->o.odin.hab, &os2msg, hwndOS2, TranslateWinMsg(uMsgFilterMin, TRUE),
350 TranslateWinMsg(uMsgFilterMax, FALSE), (fRemove & PM_REMOVE_W) ? PM_REMOVE : PM_NOREMOVE);
351
352 if (rc && (fRemove & PM_REMOVE_W) && os2msg.msg == WM_TIMER) {
353 eaten = TIMER_HandleTimer(&os2msg);
354 }
355 }
356 while (eaten && rc);
357
358 if(rc == FALSE) {
359 return FALSE;
360 }
361
362 if(OS2ToWinMsgTranslate((PVOID)teb, &os2msg, pMsg, isUnicode, (fRemove & PM_REMOVE_W) ? MSG_REMOVE : MSG_NOREMOVE) == FALSE)
363 {
364 //unused PM message; dispatch immediately and grab next one
365 dprintf2(("OSLibWinPeekMsg: Untranslated message; dispatched immediately"));
366 if(!(fRemove & PM_REMOVE_W)) {
367 rc = WinPeekMsg(teb->o.odin.hab, &os2msg, hwndOS2, TranslateWinMsg(uMsgFilterMin, TRUE),
368 TranslateWinMsg(uMsgFilterMax, FALSE), PM_REMOVE);
369 }
370 WinDispatchMsg(teb->o.odin.hab, &os2msg);
371 return OSLibWinPeekMsg(pMsg, hwnd, uMsgFilterMin, uMsgFilterMax,
372 fRemove, isUnicode);
373 }
374 //TODO: This is not safe! There's no guarantee this message will be dispatched and it might overwrite a previous message
375 if(fRemove & PM_REMOVE_W) {
376 memcpy(&teb->o.odin.os2msg, &os2msg, sizeof(QMSG));
377 memcpy(&teb->o.odin.winmsg, pMsg, sizeof(MSG));
378 }
379
380 if(pMsg->message <= WINWM_KEYLAST && pMsg->message >= WINWM_KEYDOWN)
381 {
382 if(ProcessKbdHook(pMsg, fRemove))
383 goto continuepeekmsg;
384 }
385
386 return rc;
387}
388//******************************************************************************
389//******************************************************************************
390ULONG OSLibWinQueryMsgTime()
391{
392 return WinQueryMsgTime(GetThreadHAB());
393}
394//******************************************************************************
395//******************************************************************************
396BOOL OSLibWinWaitMessage()
397{
398 return WinWaitMsg(GetThreadHAB(), 0, 0);
399}
400//******************************************************************************
401//TODO: QS_HOTKEY
402//******************************************************************************
403ULONG OSLibWinQueryQueueStatus()
404{
405 ULONG statusOS2, statusWin32 = 0;
406
407 statusOS2 = WinQueryQueueStatus(HWND_DESKTOP);
408
409 if(statusOS2 & QS_KEY)
410 statusWin32 |= QS_KEY_W;
411 if(statusOS2 & QS_MOUSEBUTTON)
412 statusWin32 |= QS_MOUSEBUTTON_W;
413 if(statusOS2 & QS_MOUSEMOVE)
414 statusWin32 |= QS_MOUSEMOVE_W;
415 if(statusOS2 & QS_TIMER)
416 statusWin32 |= QS_TIMER_W;
417 if(statusOS2 & QS_PAINT)
418 statusWin32 |= QS_PAINT_W;
419 if(statusOS2 & QS_POSTMSG)
420 statusWin32 |= QS_POSTMESSAGE_W;
421 if(statusOS2 & QS_SENDMSG)
422 statusWin32 |= QS_SENDMESSAGE_W;
423
424 return statusWin32;
425}
426//******************************************************************************
427//******************************************************************************
428BOOL OSLibWinInSendMessage()
429{
430 return WinInSendMsg(GetThreadHAB());
431}
432//******************************************************************************
433//******************************************************************************
434DWORD OSLibWinGetMessagePos()
435{
436 APIRET rc;
437 POINTL ptl;
438
439 rc = WinQueryMsgPos(GetThreadHAB(), &ptl);
440 if(!rc) {
441 return 0;
442 }
443 //convert to windows coordinates
444 return MAKEULONG(ptl.x,mapScreenY(ptl.y));
445}
446//******************************************************************************
447//******************************************************************************
448LONG OSLibWinGetMessageTime()
449{
450 return (LONG)WinQueryMsgTime(GetThreadHAB());
451}
452//******************************************************************************
453//******************************************************************************
454BOOL OSLibWinReplyMessage(ULONG result)
455{
456 return (BOOL)WinReplyMsg( NULLHANDLE, NULLHANDLE, HMQ_CURRENT, (MRESULT)result);
457}
458//******************************************************************************
459//******************************************************************************
460ULONG OSLibSendMessage(HWND hwnd, ULONG msg, ULONG wParam, ULONG lParam, BOOL fUnicode)
461{
462 POSTMSG_PACKET *packet = (POSTMSG_PACKET *)_smalloc(sizeof(POSTMSG_PACKET));
463
464 packet->wParam = wParam;
465 packet->lParam = lParam;
466
467 return (ULONG)WinSendMsg(hwnd, WIN32APP_POSTMSG+msg, (MPARAM)((fUnicode) ? WIN32MSG_MAGICW : WIN32MSG_MAGICA), (MPARAM)packet);
468}
469//******************************************************************************
470//******************************************************************************
471ULONG OSLibWinBroadcastMsg(ULONG msg, ULONG wParam, ULONG lParam, BOOL fSend)
472{
473 return WinBroadcastMsg(HWND_DESKTOP, WIN32APP_POSTMSG+msg, (MPARAM)wParam, (MPARAM)lParam,
474 (fSend) ? BMSG_SEND : BMSG_POST);
475}
476//******************************************************************************
477//******************************************************************************
478BOOL OSLibPostMessage(HWND hwnd, ULONG msg, ULONG wParam, ULONG lParam, BOOL fUnicode)
479{
480 POSTMSG_PACKET *packet = (POSTMSG_PACKET *)_smalloc(sizeof(POSTMSG_PACKET));
481
482 packet->wParam = wParam;
483 packet->lParam = lParam;
484 return WinPostMsg(hwnd, WIN32APP_POSTMSG+msg, (MPARAM)((fUnicode) ? WIN32MSG_MAGICW : WIN32MSG_MAGICA), (MPARAM)packet);
485}
486//******************************************************************************
487//Direct posting of messages that must remain invisible to the win32 app
488//******************************************************************************
489BOOL OSLibPostMessageDirect(HWND hwnd, ULONG msg, ULONG wParam, ULONG lParam)
490{
491 return WinPostMsg(hwnd, msg, (MPARAM)wParam, (MPARAM)lParam);
492}
493//******************************************************************************
494BOOL _System _O32_PostThreadMessage( DWORD, UINT, WPARAM, LPARAM );
495
496inline BOOL O32_PostThreadMessage(DWORD a, UINT b, WPARAM c, LPARAM d)
497{
498 BOOL yyrc;
499 USHORT sel = RestoreOS2FS();
500
501 yyrc = _O32_PostThreadMessage(a, b, c, d);
502 SetFS(sel);
503
504 return yyrc;
505}
506//******************************************************************************
507BOOL OSLibPostThreadMessage(ULONG threadid, UINT msg, WPARAM wParam, LPARAM lParam, BOOL fUnicode)
508{
509 TEB *teb = GetTEBFromThreadId(threadid);
510 POSTMSG_PACKET *packet = (POSTMSG_PACKET *)_smalloc(sizeof(POSTMSG_PACKET));
511 BOOL ret;
512
513 if(teb == NULL) {
514 dprintf(("OSLibPostThreadMessage: thread %x not found!", threadid));
515 return FALSE;
516 }
517 dprintf(("PostThreadMessageA %x %x %x %x -> hmq %x", threadid, msg, wParam, lParam, teb->o.odin.hmq));
518 packet->wParam = wParam;
519 packet->lParam = lParam;
520
521 ret = WinPostQueueMsg((HMQ)teb->o.odin.hmq, WIN32APP_POSTMSG+msg,
522 (MPARAM)((fUnicode) ? WIN32MSG_MAGICW : WIN32MSG_MAGICA),
523 (MPARAM)packet);
524
525 if(ret == FALSE)
526 {
527 SetLastError(ERROR_INVALID_PARAMETER_W);
528 return FALSE;
529 }
530 SetLastError(ERROR_SUCCESS_W);
531 return TRUE;
532}
533//******************************************************************************
534//******************************************************************************
535
Note: See TracBrowser for help on using the repository browser.