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

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

KOM: WM_IME_CHAR generation + processing added for DBCS input

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