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

Last change on this file since 10190 was 10190, checked in by sandervl, 22 years ago

KOMH: DBCS updates/fixes

File size: 31.2 KB
Line 
1/* $Id: oslibmsg.cpp,v 1.72 2003-07-31 15:56:43 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 * TODO: Flaw in our message handling; we don't handle posted/sent (by the app)
19 * system messages properly if removed from the queue with PeekMessage.
20 * e.g.
21 * PostMessage(WM_KEYDOWN)
22 * PeekMessage(any, PM_NOREMOVE)
23 * ...
24 * PeekMessage(WM_KEYDOWN, WM_KEYDOWN, PM_REMOVE)
25 *
26 * So what we really need is a complete win to os2 message translation
27 * in Post/SendMessage. Quite a lot of work though...
28 *
29 */
30#define INCL_WIN
31#define INCL_PM
32#define INCL_DOSPROCESS
33#define INCL_WINMESSAGEMGR
34#define INCL_DOSSEMAPHORES
35#define INCL_DOSERRORS
36#include <os2wrap.h>
37#include <odinwrap.h>
38#include <string.h>
39#include <misc.h>
40#include "oslibmsg.h"
41#include <winconst.h>
42#include <win32api.h>
43#include <winuser32.h>
44#include "oslibutil.h"
45#include "timer.h"
46#include <thread.h>
47#include <wprocess.h>
48#include "pmwindow.h"
49#include "oslibwin.h"
50#include <win\hook.h>
51#include <winscan.h>
52#include <winkeyboard.h>
53#include "user32api.h"
54
55#define DBG_LOCALLOG DBG_oslibmsg
56#include "dbglocal.h"
57
58
59ODINDEBUGCHANNEL(USER32-OSLIBMSG)
60
61
62
63typedef BOOL (EXPENTRY FNTRANS)(MSG *, QMSG *);
64typedef FNTRANS *PFNTRANS;
65
66typedef struct
67{
68 ULONG msgOS2;
69 ULONG msgWin32;
70// PFNTRANS toOS2;
71// PFNTRANS toWIN32;
72} MSGTRANSTAB, *PMSGTRANSTAB;
73
74//NOTE: Must be ordered by win32 message id!!
75MSGTRANSTAB MsgTransTab[] = {
76 WM_NULL, WINWM_NULL,
77 WM_CREATE, WINWM_CREATE,
78 WM_DESTROY, WINWM_DESTROY,
79 WM_MOVE, WINWM_MOVE, //TODO: Sent directly
80 WM_SIZE, WINWM_SIZE, //TODO: Sent directly
81 WM_ACTIVATE, WINWM_ACTIVATE,
82 WM_SETFOCUS, WINWM_SETFOCUS,
83 WM_SETFOCUS, WINWM_KILLFOCUS,
84 WM_ENABLE, WINWM_ENABLE,
85 WM_PAINT, WINWM_PAINT,
86 WM_CLOSE, WINWM_CLOSE,
87 WM_QUIT, WINWM_QUIT,
88 WM_SHOW, WINWM_SHOWWINDOW,
89
90 WM_HITTEST, WINWM_NCHITTEST,
91
92 //todo: not always right if mouse msg turns out to be for the client window
93 WM_MOUSEMOVE, WINWM_NCMOUSEMOVE,
94 WM_BUTTON1DOWN, WINWM_NCLBUTTONDOWN,
95 WM_BUTTON1UP, WINWM_NCLBUTTONUP,
96 WM_BUTTON1DBLCLK, WINWM_NCLBUTTONDBLCLK,
97 WM_BUTTON2DOWN, WINWM_NCRBUTTONDOWN,
98 WM_BUTTON2UP, WINWM_NCRBUTTONUP,
99 WM_BUTTON2DBLCLK, WINWM_NCRBUTTONDBLCLK,
100 WM_BUTTON3DOWN, WINWM_NCMBUTTONDOWN,
101 WM_BUTTON3UP, WINWM_NCMBUTTONUP,
102 WM_BUTTON3DBLCLK, WINWM_NCMBUTTONDBLCLK,
103
104 //TODO: Needs better translation!
105 WM_CHAR, WINWM_KEYDOWN, //WM_KEYFIRST
106 WM_CHAR, WINWM_KEYUP,
107 WM_CHAR, WINWM_CHAR,
108 WM_CHAR, WINWM_DEADCHAR,
109 WM_CHAR, WINWM_SYSKEYDOWN,
110 WM_CHAR, WINWM_SYSKEYUP,
111 WM_CHAR, WINWM_SYSCHAR,
112 WM_CHAR, WINWM_SYSDEADCHAR,
113 WM_CHAR, WINWM_KEYLAST,
114
115 //
116 WM_TIMER, WINWM_TIMER,
117
118 //
119 //todo: not always right if mouse msg turns out to be for the nonclient window
120 WM_MOUSEMOVE, WINWM_MOUSEMOVE, //WM_MOUSEFIRST
121 WM_BUTTON1DOWN, WINWM_LBUTTONDOWN,
122 WM_BUTTON1UP, WINWM_LBUTTONUP,
123 WM_BUTTON1DBLCLK, WINWM_LBUTTONDBLCLK,
124 WM_BUTTON2DOWN, WINWM_RBUTTONDOWN,
125 WM_BUTTON2UP, WINWM_RBUTTONUP,
126 WM_BUTTON2DBLCLK, WINWM_RBUTTONDBLCLK,
127 WM_BUTTON3DOWN, WINWM_MBUTTONDOWN,
128 WM_BUTTON3UP, WINWM_MBUTTONUP,
129 WM_BUTTON3DBLCLK, WINWM_MBUTTONDBLCLK,
130 WM_BUTTON3DBLCLK, WINWM_MOUSEWHEEL, //WM_MOUSELAST
131 999999999, 999999999,
132};
133#define MAX_MSGTRANSTAB (sizeof(MsgTransTab)/sizeof(MsgTransTab[0]))
134
135//******************************************************************************
136//******************************************************************************
137void WinToOS2MsgTranslate(MSG *winMsg, QMSG *os2Msg, BOOL isUnicode)
138{
139 dprintf(("WinToOS2MsgTranslate not implemented"));
140// memcpy(os2Msg, winMsg, sizeof(MSG));
141// os2Msg->hwnd = Win32ToOS2Handle(winMsg->hwnd);
142// os2Msg->reserved = 0;
143}
144//******************************************************************************
145//TODO: NOT COMPLETE nor 100% CORRECT!!!
146//If both the minimum & maximum message are unknown, the result can be wrong (max > min)!
147//******************************************************************************
148ULONG TranslateWinMsg(ULONG msg, BOOL fMinFilter, BOOL fExactMatch = FALSE)
149{
150 if(msg == 0)
151 return 0;
152
153 if(msg >= WINWM_USER)
154 return msg + WIN32APP_POSTMSG;
155
156 for(int i=0;i<MAX_MSGTRANSTAB;i++)
157 {
158 if(fExactMatch) {
159 if(MsgTransTab[i].msgWin32 == msg)
160 return MsgTransTab[i].msgOS2;
161 }
162 else {
163 if(fMinFilter && MsgTransTab[i].msgWin32 >= msg) {
164 return MsgTransTab[i].msgOS2;
165 }
166 else
167 if(!fMinFilter && MsgTransTab[i].msgWin32 >= msg) {
168 if(MsgTransTab[i].msgWin32 == msg)
169 return MsgTransTab[i].msgOS2;
170 else return MsgTransTab[i-1].msgOS2;
171 }
172 }
173 }
174
175 //not found, get everything
176 dprintf2(("WARNING: TranslateWinMsg: message %x not found", msg));
177 return 0;
178}
179//******************************************************************************
180//******************************************************************************
181void OSLibWinPostQuitMessage(ULONG nExitCode)
182{
183 APIRET rc;
184
185 //NOTE: mp2 must always be zero or else we won't be able to distinguish
186 // between the WM_QUIT sent by us and the one sent by the window list!!
187 rc = WinPostQueueMsg(NULLHANDLE, WM_QUIT, MPFROMLONG(nExitCode), 0);
188 dprintf(("WinPostQueueMsg %d returned %d", nExitCode, rc));
189}
190//******************************************************************************
191//******************************************************************************
192LONG OSLibWinDispatchMsg(MSG *msg, BOOL isUnicode)
193{
194 TEB *teb;
195 QMSG os2msg;
196 LONG rc;
197
198 teb = GetThreadTEB();
199 if(teb == NULL) {
200 DebugInt3();
201 return FALSE;
202 }
203
204 //TODO: What to do if app changed msg? (translate)
205 // WinToOS2MsgTranslate(msg, &qmsg, isUnicode);
206
207 if(!memcmp(msg, &teb->o.odin.winmsg, sizeof(MSG)) || msg->hwnd == 0) {
208 memcpy(&os2msg, &teb->o.odin.os2msg, sizeof(QMSG));
209 teb->o.odin.os2msg.time = -1;
210 teb->o.odin.winmsg.time = -1;
211 if(msg->hwnd) {
212 teb->o.odin.nrOfMsgs = 1;
213 teb->o.odin.msgstate++; //odd -> next call to our PM window handler should dispatch the translated msg
214 memcpy(&teb->o.odin.msg, msg, sizeof(MSG));
215 }
216 if(os2msg.hwnd || os2msg.msg == WM_QUIT) {
217 memset(&teb->o.odin.os2msg, 0, sizeof(teb->o.odin.os2msg));
218 memset(&teb->o.odin.winmsg, 0, sizeof(teb->o.odin.winmsg));
219 return (LONG)WinDispatchMsg(teb->o.odin.hab, &os2msg);
220 }
221 //SvL: Don't dispatch messages sent by PostThreadMessage (correct??)
222 // Or WM_TIMER msgs with no window handle or timer proc
223 return 0;
224
225 }
226 else {//is this allowed?
227// dprintf(("WARNING: OSLibWinDispatchMsg: called with own message!"));
228 return isUnicode ? SendMessageW(msg->hwnd, msg->message, msg->wParam, msg->lParam) :
229 SendMessageA(msg->hwnd, msg->message, msg->wParam, msg->lParam);
230 }
231}
232//******************************************************************************
233//******************************************************************************
234BOOL OSLibWinGetMsg(LPMSG pMsg, HWND hwnd, UINT uMsgFilterMin, UINT uMsgFilterMax,
235 BOOL isUnicode)
236{
237 BOOL rc, eaten;
238 TEB *teb;
239 QMSG os2msg;
240 HWND hwndOS2 = 0;
241 ULONG filtermin, filtermax;
242
243 if(hwnd) {
244 hwndOS2 = Win32ToOS2Handle(hwnd);
245 if(hwndOS2 == NULL) {
246 memset(pMsg, 0, sizeof(MSG));
247 dprintf(("GetMsg: window %x NOT FOUND!", hwnd));
248 SetLastError(ERROR_INVALID_WINDOW_HANDLE_W);
249 return TRUE;
250 }
251 }
252
253 teb = GetThreadTEB();
254 if(teb == NULL) {
255 DebugInt3();
256 return TRUE;
257 }
258
259 if(teb->o.odin.fTranslated && (!hwnd || hwnd == teb->o.odin.msgWCHAR.hwnd))
260 {
261 dprintf(("Return queued WM_CHAR message hwnd=%x msg=%d wParam=%x lParam=%x", teb->o.odin.msgWCHAR.hwnd, teb->o.odin.msgWCHAR.message, teb->o.odin.msgWCHAR.wParam, teb->o.odin.msgWCHAR.lParam));
262 if(uMsgFilterMin) {
263 if(teb->o.odin.msgWCHAR.message < uMsgFilterMin)
264 goto continuegetmsg;
265 }
266 if(uMsgFilterMax) {
267 if(teb->o.odin.msgWCHAR.message > uMsgFilterMax)
268 goto continuegetmsg;
269 }
270 teb->o.odin.fTranslated = FALSE;
271 memcpy(pMsg, &teb->o.odin.msgWCHAR, sizeof(MSG));
272
273 //After SetFocus(0), all keystrokes are converted in WM_SYS*
274 if(pMsg->message == WINWM_CHAR && fIgnoreKeystrokes) {
275 pMsg->message = WINWM_SYSCHAR;
276 }
277
278 teb->o.odin.os2msg.msg = 0;
279 teb->o.odin.os2msg.hwnd = 0;
280
281 if(!IsWindow(pMsg->hwnd)) {
282 //could be a queued char message for a window that was just destroyed
283 //when that's the case, we ignore it (MFC assertions are triggered by this)
284 goto continuegetmsg;
285 }
286
287 // @@@PH verify this
288 // if this is a keyup or keydown message, we've got to
289 // call the keyboard hook here
290 // send keyboard messages to the registered hooks
291 switch (pMsg->message)
292 {
293 case WINWM_KEYDOWN:
294 case WINWM_KEYUP:
295 case WINWM_SYSKEYDOWN:
296 case WINWM_SYSKEYUP:
297 // only supposed to be called upon WM_KEYDOWN
298 // and WM_KEYUP according to docs.
299 if(ProcessKbdHook(pMsg, TRUE))
300 goto continuegetmsg;
301 break;
302 }
303
304 return (pMsg->message != WINWM_QUIT);
305 }
306
307continuegetmsg:
308 if(hwnd) {
309 filtermin = TranslateWinMsg(uMsgFilterMin, TRUE);
310 filtermax = TranslateWinMsg(uMsgFilterMax, FALSE);
311 if(filtermin > filtermax) {
312 ULONG tmp = filtermin;
313 filtermin = filtermax;
314 filtermax = filtermin;
315 }
316 do {
317 WinWaitMsg(teb->o.odin.hab, filtermin, filtermax);
318 rc = OSLibWinPeekMsg(pMsg, hwnd, uMsgFilterMin, uMsgFilterMax, PM_REMOVE_W, isUnicode);
319 }
320 while(rc == FALSE);
321
322 return (pMsg->message != WINWM_QUIT);
323 }
324 else
325 {
326 filtermin = TranslateWinMsg(uMsgFilterMin, TRUE);
327 filtermax = TranslateWinMsg(uMsgFilterMax, FALSE);
328 if(filtermin > filtermax) {
329 ULONG tmp = filtermin;
330 filtermin = filtermax;
331 filtermax = filtermin;
332 }
333 rc = WinGetMsg(teb->o.odin.hab, &os2msg, 0, filtermin, filtermax);
334 if (os2msg.msg == WM_QUIT && ((ULONG)os2msg.mp2 != 0) ) {
335 // Don't return FALSE when the window list sends us
336 // a WM_QUIT message, improper killing can lead to
337 // application crashes.
338 // In the WM_QUIT handler in pmwindow we send a WM_CLOSE
339 // in this case. When the app calls PostQuitMessage (mp2 == 0),
340 // then we handle it the normal way
341 rc = 1;
342 }
343 }
344 if(OS2ToWinMsgTranslate((PVOID)teb, &os2msg, pMsg, isUnicode, MSG_REMOVE) == FALSE) {
345 //dispatch untranslated message immediately
346 WinDispatchMsg(teb->o.odin.hab, &os2msg);
347 //and get the next one
348 return OSLibWinGetMsg(pMsg, hwnd, uMsgFilterMin, uMsgFilterMax, isUnicode);
349 }
350
351 memcpy(&teb->o.odin.os2msg, &os2msg, sizeof(QMSG));
352 memcpy(&teb->o.odin.winmsg, pMsg, sizeof(MSG));
353
354 // send keyboard messages to the registered hooks
355 switch (pMsg->message)
356 {
357 case WINWM_KEYDOWN:
358 case WINWM_KEYUP:
359 case WINWM_SYSKEYDOWN:
360 case WINWM_SYSKEYUP:
361 // only supposed to be called upon WM_KEYDOWN
362 // and WM_KEYUP according to docs.
363 if(ProcessKbdHook(pMsg, TRUE))
364 goto continuegetmsg;
365 break;
366 }
367 return rc;
368}
369
370
371//******************************************************************************
372//PeekMessage retrieves only messages associated with the window identified by the
373//hwnd parameter or any of its children as specified by the IsChild function, and within
374//the range of message values given by the uMsgFilterMin and uMsgFilterMax
375//parameters. If hwnd is NULL, PeekMessage retrieves messages for any window that
376//belongs to the current thread making the call. (PeekMessage does not retrieve
377//messages for windows that belong to other threads.) If hwnd is -1, PeekMessage only
378//returns messages with a hwnd value of NULL, as posted by the PostAppMessage
379//function. If uMsgFilterMin and uMsgFilterMax are both zero, PeekMessage returns all
380//available messages (no range filtering is performed).
381//TODO: Not working as specified right now!
382//******************************************************************************
383BOOL OSLibWinPeekMsg(LPMSG pMsg, HWND hwnd, UINT uMsgFilterMin, UINT uMsgFilterMax,
384 DWORD fRemove, BOOL isUnicode)
385{
386 BOOL rc, eaten;
387 TEB *teb;
388 QMSG os2msg;
389 HWND hwndOS2 = 0;
390
391 if(uMsgFilterMin > uMsgFilterMax) {
392 //TODO: is this correct behaviour?
393 dprintf(("!ERROR!: invalid message filter range!!!"));
394 SetLastError(ERROR_INVALID_PARAMETER_W);
395 return FALSE;
396 }
397 if(hwnd && hwnd != -1) {
398 hwndOS2 = Win32ToOS2Handle(hwnd);
399 if(hwndOS2 == NULL) {
400 dprintf(("PeekMsg: window %x NOT FOUND!", hwnd));
401 SetLastError(ERROR_INVALID_WINDOW_HANDLE_W);
402 return FALSE;
403 }
404 }
405
406 teb = GetThreadTEB();
407 if(teb == NULL) {
408 DebugInt3();
409 return FALSE;
410 }
411
412 if(teb->o.odin.fTranslated && (!hwnd || hwnd == teb->o.odin.msgWCHAR.hwnd))
413 {
414 dprintf(("Return queued WM_CHAR message hwnd=%x msg=%d wParam=%x lParam=%x", teb->o.odin.msgWCHAR.hwnd, teb->o.odin.msgWCHAR.message, teb->o.odin.msgWCHAR.wParam, teb->o.odin.msgWCHAR.lParam));
415 if(uMsgFilterMin) {
416 if(teb->o.odin.msgWCHAR.message < uMsgFilterMin)
417 goto continuepeekmsg;
418 }
419 if(uMsgFilterMax) {
420 if(teb->o.odin.msgWCHAR.message > uMsgFilterMax)
421 goto continuepeekmsg;
422 }
423
424 if(fRemove & PM_REMOVE_W) {
425 teb->o.odin.fTranslated = FALSE;
426 teb->o.odin.os2msg.msg = 0;
427 teb->o.odin.os2msg.hwnd = 0;
428 }
429 memcpy(pMsg, &teb->o.odin.msgWCHAR, sizeof(MSG));
430 //After SetFocus(0), all keystrokes are converted in WM_SYS*
431 if(pMsg->message == WINWM_CHAR && fIgnoreKeystrokes) {
432 pMsg->message = WINWM_SYSCHAR;
433 }
434
435
436 if(!IsWindow(pMsg->hwnd)) {
437 //could be a queued char message for a window that was just destroyed
438 //when that's the case, we ignore it (MFC assertions are triggered by this)
439 teb->o.odin.fTranslated = FALSE;
440 teb->o.odin.os2msg.msg = 0;
441 teb->o.odin.os2msg.hwnd = 0;
442 goto continuepeekmsg;
443 }
444
445 // @@@PH verify this
446 // if this is a keyup or keydown message, we've got to
447 // call the keyboard hook here
448 // send keyboard messages to the registered hooks
449 if(fRemove & PM_REMOVE_W) {
450 switch (pMsg->message)
451 {
452 case WINWM_KEYDOWN:
453 case WINWM_KEYUP:
454 case WINWM_SYSKEYDOWN:
455 case WINWM_SYSKEYUP:
456 // only supposed to be called upon WM_KEYDOWN
457 // and WM_KEYUP according to docs.
458 if(ProcessKbdHook(pMsg, fRemove))
459 goto continuepeekmsg;
460 break;
461 }
462 }
463
464 return TRUE;
465 }
466
467continuepeekmsg:
468 if(uMsgFilterMin && uMsgFilterMax)
469 { //we can't use the PM message filter (since the message nrs aren't similar), so we must
470 //filter each message seperately
471 //to do so, we will translate each win32 message in the filter range and call WinPeekMsg
472 ULONG ulPMFilter;
473
474 for(int i=0;i<uMsgFilterMax-uMsgFilterMin+1;i++)
475 {
476 rc = 0;
477
478 ulPMFilter = TranslateWinMsg(uMsgFilterMin+i, TRUE, TRUE);
479 if(ulPMFilter) {
480 rc = WinPeekMsg(teb->o.odin.hab, &os2msg, hwndOS2, ulPMFilter, ulPMFilter,
481 (fRemove & PM_REMOVE_W) ? PM_REMOVE : PM_NOREMOVE);
482 //Sadly indeed WinPeekMsg sometimes does not filter well!
483 if (rc && (os2msg.msg != ulPMFilter)) {// drop this message
484 dprintf(("WARNING: WinPeekMsg returns %x even though we filter for %x", os2msg.msg, ulPMFilter));
485 rc = 0;
486 }
487 }
488 if(rc) {
489 break;
490 }
491 }
492 }
493 else {
494 rc = WinPeekMsg(teb->o.odin.hab, &os2msg, hwndOS2, 0, 0, (fRemove & PM_REMOVE_W) ? PM_REMOVE : PM_NOREMOVE);
495 }
496 if(rc == FALSE) {
497 return FALSE;
498 }
499
500 // @@@PH
501 // warning - OS2ToWinMsgTranslate might insert additional messages
502 // into the queue
503 if(OS2ToWinMsgTranslate((PVOID)teb, &os2msg, pMsg, isUnicode, (fRemove & PM_REMOVE_W) ? MSG_REMOVE : MSG_NOREMOVE) == FALSE)
504 {
505 //unused PM message; dispatch immediately and grab next one
506 dprintf2(("OSLibWinPeekMsg: Untranslated message; dispatched immediately"));
507 if(!(fRemove & PM_REMOVE_W)) {
508 rc = WinPeekMsg(teb->o.odin.hab, &os2msg, os2msg.hwnd, os2msg.msg,
509 os2msg.msg, PM_REMOVE);
510 }
511 WinDispatchMsg(teb->o.odin.hab, &os2msg);
512 return OSLibWinPeekMsg(pMsg, hwnd, uMsgFilterMin, uMsgFilterMax, fRemove, isUnicode);
513 }
514 //TODO: This is not safe! There's no guarantee this message will be dispatched and it might overwrite a previous message
515 if(fRemove & PM_REMOVE_W) {
516 memcpy(&teb->o.odin.os2msg, &os2msg, sizeof(QMSG));
517 memcpy(&teb->o.odin.winmsg, pMsg, sizeof(MSG));
518 }
519
520 // send keyboard messages to the registered hooks
521 if(fRemove & PM_REMOVE_W) {
522 switch (pMsg->message)
523 {
524 case WINWM_KEYDOWN:
525 case WINWM_KEYUP:
526 case WINWM_SYSKEYDOWN:
527 case WINWM_SYSKEYUP:
528 // only supposed to be called upon WM_KEYDOWN
529 // and WM_KEYUP according to docs.
530 if(ProcessKbdHook(pMsg, fRemove))
531 goto continuepeekmsg;
532 break;
533 }
534 }
535
536 return rc;
537}
538//******************************************************************************
539//******************************************************************************
540ULONG OSLibWinQueryMsgTime()
541{
542 return WinQueryMsgTime(GetThreadHAB());
543}
544//******************************************************************************
545//******************************************************************************
546BOOL OSLibWinWaitMessage()
547{
548 return WinWaitMsg(GetThreadHAB(), 0, 0);
549}
550//******************************************************************************
551//TODO: QS_HOTKEY
552//******************************************************************************
553ULONG OSLibWinQueryQueueStatus()
554{
555 ULONG statusOS2, statusWin32 = 0;
556
557 statusOS2 = WinQueryQueueStatus(HWND_DESKTOP);
558
559 if(statusOS2 & QS_KEY)
560 statusWin32 |= QS_KEY_W;
561 if(statusOS2 & QS_MOUSEBUTTON)
562 statusWin32 |= QS_MOUSEBUTTON_W;
563 if(statusOS2 & QS_MOUSEMOVE)
564 statusWin32 |= QS_MOUSEMOVE_W;
565 if(statusOS2 & QS_TIMER)
566 statusWin32 |= QS_TIMER_W;
567 if(statusOS2 & QS_PAINT)
568 statusWin32 |= QS_PAINT_W;
569 if(statusOS2 & QS_POSTMSG)
570 statusWin32 |= QS_POSTMESSAGE_W;
571 if(statusOS2 & QS_SENDMSG)
572 statusWin32 |= QS_SENDMESSAGE_W;
573
574 return statusWin32;
575}
576//******************************************************************************
577//******************************************************************************
578BOOL OSLibWinInSendMessage()
579{
580 return WinInSendMsg(GetThreadHAB());
581}
582//******************************************************************************
583//******************************************************************************
584DWORD OSLibWinGetMessagePos()
585{
586 APIRET rc;
587 POINTL ptl;
588
589 rc = WinQueryMsgPos(GetThreadHAB(), &ptl);
590 if(!rc) {
591 return 0;
592 }
593 //convert to windows coordinates
594 return MAKEULONG(ptl.x,mapScreenY(ptl.y));
595}
596//******************************************************************************
597//******************************************************************************
598LONG OSLibWinGetMessageTime()
599{
600 return (LONG)WinQueryMsgTime(GetThreadHAB());
601}
602//******************************************************************************
603//******************************************************************************
604BOOL OSLibWinReplyMessage(ULONG result)
605{
606 return (BOOL)WinReplyMsg( NULLHANDLE, NULLHANDLE, HMQ_CURRENT, (MRESULT)result);
607}
608
609//******************************************************************************
610
611/**
612 * Send and Post message helper for packing down interprocess and interthread messages.
613 *
614 * @returns Pointer to packet on success. (shared memory)
615 * @returns NULL on failure with SendMessage return code suggestion in *pRc if pRc is set.
616 * @param hwndOdin Odin window handle. (NULL allowed)
617 * @param hwndOS2 OS/2 window handle.
618 * @param msg Message id.
619 * @param wParam Message param.
620 * @param lParam Message param.
621 * @param fUnicode Unicode or ansi indicator.
622 * @param pRc Where to store SendMessage return code. Optional.
623 * @author knut st. osmundsen<bird@anduin.net>
624 */
625void * OSLibPackMessage(HWND hwndOdin, HWND hwndOS2, ULONG msg, ULONG wParam, ULONG lParam, BOOL fUnicode, PULONG pRc)
626{
627 POSTMSG_PACKET * pMsgPacket;
628
629 /*
630 * Pack message by id.
631 */
632 switch (msg)
633 {
634 /*
635 * lParam = PCOPYDATASTRUCT.
636 * Must move this to shared memory together with any
637 * data it's pointing at.
638 *
639 * We put everything into the package that might ease cleanup...
640 * WARNING! Currently there are cleanup hacks which works with acrobat.
641 */
642 case WINWM_COPYDATA:
643 {
644 PCOPYDATASTRUCT_W pOrg = (PCOPYDATASTRUCT_W)lParam;
645 dprintf(("user32::oslibmsg::OSLibPackMessage - WM_COPYDATA: lParam=%#x dwData=%#x cbData=%d lpData=%#x",
646 pOrg, pOrg ? pOrg->dwData : -1, pOrg ? pOrg->cbData : -1, pOrg ? pOrg->lpData : (LPVOID)-1));
647
648 /*
649 * Calc packet size.
650 */
651 unsigned cb = sizeof(POSTMSG_PACKET);
652 if (pOrg)
653 {
654 cb += sizeof(COPYDATASTRUCT_W);
655 if (pOrg->lpData && pOrg->cbData)
656 cb += 16 + pOrg->cbData; //add 16 Bytes for safty and alignment.
657 }
658
659 /*
660 * Allocate packet.
661 */
662 pMsgPacket = (POSTMSG_PACKET *)_smalloc(cb);
663 if (!pMsgPacket)
664 {
665 dprintf(("user32::oslibmsg::OSLibPackMessage - WM_COPYDATA: failed to allocate %d shared bytes for packing", cb));
666 DebugInt3();
667 if (pRc)
668 *pRc = FALSE;
669 //@todo figure out which error to set. This is plain guesswork!
670 SetLastError(ERROR_NOT_ENOUGH_MEMORY_W);
671 break;
672 }
673
674 /*
675 * Initialize packet.
676 */
677 PCOPYDATASTRUCT_W pNew = (PCOPYDATASTRUCT_W)(pMsgPacket + 1);
678 pMsgPacket->wParam = wParam;
679 pMsgPacket->lParam = (LPARAM)pNew;
680 *pNew = *pOrg;
681 if (pNew->cbData && pNew->lpData)
682 {
683 pNew->lpData = (LPVOID)(((unsigned)(pNew + 1) + 15) & ~15); //16byte align for safty.
684 //@todo what about a safe_memcpy?
685 memcpy(pNew->lpData, pOrg->lpData, pNew->cbData);
686 }
687
688 /* done! */
689 dprintf(("user32::oslibmsg::OSLibPackMessage - WM_COPYDATA: Packed down %d bytes at %#x (pMsgPacket)",
690 cb, pMsgPacket));
691 break;
692 }
693
694
695 /*
696 * Default packing
697 */
698 default:
699 {
700 pMsgPacket = (POSTMSG_PACKET *)_smalloc(sizeof(POSTMSG_PACKET));
701 if (!pMsgPacket)
702 {
703 dprintf(("user32::oslibmsg::OSLibPackMessage - allocated packet structure is NULL"));
704 if (pRc)
705 { // Can't find any better return code than 0 :/
706 *pRc = 0;
707 }
708 DebugInt3();
709 break;
710 }
711 pMsgPacket->wParam = wParam;
712 pMsgPacket->lParam = lParam;
713 }
714
715 }
716
717 return pMsgPacket;
718}
719
720
721/**
722 * Send an inter thread/proces message.
723 *
724 * @returns
725 * @param hwnd OS/2 hwnd.
726 * @param msg Odin message id.
727 * @param wParam Message param.
728 * @param lParam Message param.
729 * @param fUnicode Unicode indicator.
730 */
731ULONG OSLibSendMessage(HWND hwnd, ULONG msg, ULONG wParam, ULONG lParam, BOOL fUnicode)
732{
733 ULONG rc; /* return code on packing failure */
734 void * pvMsgPacket; /* packed message (shared memory) */
735
736 /*
737 * Call message packer.
738 */
739 pvMsgPacket = OSLibPackMessage(NULLHANDLE, hwnd, msg, wParam, lParam, fUnicode, &rc);
740 if (!pvMsgPacket)
741 {
742 dprintf(("user32::oslibmsg::OSLibSendMessage - Failed to pack message !!"));
743 DebugInt3();
744 return rc;
745 }
746
747 return (ULONG)WinSendMsg(hwnd, WIN32APP_POSTMSG+msg, (MPARAM)((fUnicode) ? WIN32MSG_MAGICW : WIN32MSG_MAGICA), pvMsgPacket);
748}
749//******************************************************************************
750//******************************************************************************
751BOOL OSLibSendWinMessage(HWND hwnd, ULONG winmsg)
752{
753 return (BOOL)WinSendMsg(Win32ToOS2Handle(hwnd), TranslateWinMsg(winmsg, TRUE), 0, 0);
754}
755//******************************************************************************
756//******************************************************************************
757ULONG OSLibWinBroadcastMsg(ULONG msg, ULONG wParam, ULONG lParam, BOOL fSend)
758{
759 #ifdef DEBUG
760 if (msg == WINWM_COPYDATA)
761 {
762 dprintf(("user32::oslibmsg::OSLibWinBroadcastMsg - WM_COPYDATA will not work outside this process !!!"));
763 DebugInt3();
764 }
765 #endif
766 return WinBroadcastMsg(HWND_DESKTOP, WIN32APP_POSTMSG+msg, (MPARAM)wParam, (MPARAM)lParam,
767 (fSend) ? BMSG_SEND : BMSG_POST);
768}
769
770
771/**
772 * Post a message.
773 *
774 * @returns Success indicator.
775 *
776 * @param hwndWin32 Odin window handle.
777 * @param hwndOS2 OS/2 window handle
778 * @param msg Odin message id.
779 * @param wParam Message param.
780 * @param lParam Message param.
781 * @param fUnicode Unicode indicator.
782 */
783BOOL OSLibPostMessage(HWND hwndWin32, HWND hwndOS2, ULONG msg, ULONG wParam, ULONG lParam, BOOL fUnicode)
784{
785 void * pvMsgPacket; /* packed message (shared memory) */
786
787 /*
788 * Call message packer.
789 */
790 pvMsgPacket = OSLibPackMessage(hwndWin32, hwndOS2, msg, wParam, lParam, fUnicode, NULL);
791 if (!pvMsgPacket)
792 {
793 dprintf(("user32::oslibmsg::OSLibPostMessage - Failed to pack message !!"));
794 DebugInt3();
795 return FALSE;
796 }
797
798 /*
799 * Post the message.
800 * Signal the receiver for if it's doing MsgWaitForMultipleObjects() at the time.
801 */
802 TEB *teb = GetTEBFromThreadId(GetWindowThreadProcessId(hwndWin32, NULL));
803 BOOL ret = WinPostMsg(hwndOS2,
804 WIN32APP_POSTMSG+msg,
805 (MPARAM)((fUnicode) ? WIN32MSG_MAGICW : WIN32MSG_MAGICA),
806 pvMsgPacket);
807 if (teb && (teb->o.odin.dwWakeMask & QS_POSTMESSAGE_W))
808 {
809 //thread is blocked in MsgWaitForMultipleObjects waiting for
810 //posted messages
811 dprintf(("PostMessage: Wake up thread %x which is blocked in MsgWaitForMultipleObjects", teb->o.odin.threadId));
812 SetEvent(teb->o.odin.hPostMsgEvent);
813 }
814 return ret;
815}
816//******************************************************************************
817//Direct posting of messages that must remain invisible to the win32 app
818//******************************************************************************
819BOOL OSLibPostMessageDirect(HWND hwnd, ULONG msg, ULONG wParam, ULONG lParam)
820{
821 return WinPostMsg(hwnd, msg, (MPARAM)wParam, (MPARAM)lParam);
822}
823//******************************************************************************
824//******************************************************************************
825BOOL OSLibPostThreadMessage(ULONG threadid, UINT msg, WPARAM wParam, LPARAM lParam, BOOL fUnicode)
826{
827 TEB *teb = GetTEBFromThreadId(threadid);
828 POSTMSG_PACKET *packet = (POSTMSG_PACKET *)_smalloc(sizeof(POSTMSG_PACKET));
829 BOOL ret;
830
831 if (NULL == packet)
832 {
833 dprintf(("user32::oslibmsg::OSLibPostMessage - allocated packet structure is NULL"));
834
835 DebugInt3();
836 // PH: we can provide a correct returncode
837 return FALSE;
838 }
839
840 if(teb == NULL) {
841 dprintf(("OSLibPostThreadMessage: thread %x not found!", threadid));
842 return FALSE;
843 }
844 dprintf(("PostThreadMessageA %x %x %x %x -> hmq %x", threadid, msg, wParam, lParam, teb->o.odin.hmq));
845 packet->wParam = wParam;
846 packet->lParam = lParam;
847
848 ret = WinPostQueueMsg((HMQ)teb->o.odin.hmq, WIN32APP_POSTMSG+msg,
849 (MPARAM)((fUnicode) ? WIN32MSG_MAGICW : WIN32MSG_MAGICA),
850 (MPARAM)packet);
851
852 if(ret == FALSE)
853 {
854 SetLastError(ERROR_INVALID_PARAMETER_W);
855 return FALSE;
856 }
857
858 if(teb->o.odin.dwWakeMask & QS_POSTMESSAGE_W) {
859 //thread is blocked in MsgWaitForMultipleObjects waiting for
860 //posted messages
861 dprintf(("PostMessage: Wake up thread %x which is blocked in MsgWaitForMultipleObjects", teb->o.odin.threadId));
862 SetEvent(teb->o.odin.hPostMsgEvent);
863 }
864
865 SetLastError(ERROR_SUCCESS_W);
866 return TRUE;
867}
868//******************************************************************************
869//******************************************************************************
870DWORD GetThreadMessageExtraInfo()
871{
872 TEB *teb;
873
874 teb = GetThreadTEB();
875 if(teb)
876 {
877 return teb->o.odin.dwMsgExtraInfo;
878 }
879 dprintf(("GetThreadMessageExtraInfo: teb == NULL!!"));
880 return 0;
881}
882//******************************************************************************
883//******************************************************************************
884DWORD SetThreadMessageExtraInfo(DWORD lParam)
885{
886 TEB *teb;
887
888 teb = GetThreadTEB();
889 if(teb)
890 {
891 teb->o.odin.dwMsgExtraInfo = lParam;
892 }
893 else dprintf(("SetThreadMessageExtraInfo: teb == NULL!!"));
894 return 0;
895}
896//******************************************************************************
897//******************************************************************************
Note: See TracBrowser for help on using the repository browser.