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

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

mouse message translation + dc reset after resize fixes

File size: 17.7 KB
Line 
1/* $Id: oslibmsg.cpp,v 1.37 2001-04-27 17:36:37 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 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, probaby WIN32APP_POSTMSG
146 return WIN32APP_POSTMSG;
147}
148//******************************************************************************
149//******************************************************************************
150void OSLibWinPostQuitMessage(ULONG nExitCode)
151{
152 APIRET rc;
153
154 rc = WinPostQueueMsg(NULLHANDLE, WM_QUIT, MPFROMLONG(nExitCode), 0);
155 dprintf(("WinPostQueueMsg %d returned %d", nExitCode, rc));
156}
157//******************************************************************************
158//******************************************************************************
159LONG OSLibWinDispatchMsg(MSG *msg, BOOL isUnicode)
160{
161 TEB *teb;
162 QMSG os2msg;
163 LONG rc;
164
165 teb = GetThreadTEB();
166 if(teb == NULL) {
167 DebugInt3();
168 return FALSE;
169 }
170
171 //TODO: What to do if app changed msg? (translate)
172 // WinToOS2MsgTranslate(msg, &qmsg, isUnicode);
173
174 if(!memcmp(msg, &teb->o.odin.winmsg, sizeof(MSG)) || msg->hwnd == 0) {
175 memcpy(&os2msg, &teb->o.odin.os2msg, sizeof(QMSG));
176 teb->o.odin.os2msg.time = -1;
177 teb->o.odin.winmsg.time = -1;
178 if(msg->hwnd) {
179 teb->o.odin.nrOfMsgs = 1;
180 teb->o.odin.msgstate++; //odd -> next call to our PM window handler should dispatch the translated msg
181 memcpy(&teb->o.odin.msg, msg, sizeof(MSG));
182 }
183 if(os2msg.hwnd || os2msg.msg == WM_QUIT) {
184 memset(&teb->o.odin.os2msg, 0, sizeof(teb->o.odin.os2msg));
185 memset(&teb->o.odin.winmsg, 0, sizeof(teb->o.odin.winmsg));
186 return (LONG)WinDispatchMsg(teb->o.odin.hab, &os2msg);
187 }
188 //SvL: Don't dispatch messages sent by PostThreadMessage (correct??)
189 // Or WM_TIMER msgs with no window handle or timer proc
190 return 0;
191
192 }
193 else {//is this allowed?
194// dprintf(("WARNING: OSLibWinDispatchMsg: called with own message!"));
195 return SendMessageA(msg->hwnd, msg->message, msg->wParam, msg->lParam);
196 }
197}
198//******************************************************************************
199//******************************************************************************
200BOOL OSLibWinGetMsg(LPMSG pMsg, HWND hwnd, UINT uMsgFilterMin, UINT uMsgFilterMax,
201 BOOL isUnicode)
202{
203 BOOL rc, eaten;
204 TEB *teb;
205 QMSG os2msg;
206 HWND hwndOS2 = 0;
207 ULONG filtermin, filtermax;
208
209 if(hwnd) {
210 hwndOS2 = 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 teb = GetThreadTEB();
220 if(teb == NULL) {
221 DebugInt3();
222 return TRUE;
223 }
224
225 if(teb->o.odin.fTranslated && (!hwnd || hwnd == teb->o.odin.msgWCHAR.hwnd)) {
226 if(uMsgFilterMin) {
227 if(teb->o.odin.msgWCHAR.message < uMsgFilterMin)
228 goto continuegetmsg;
229 }
230 if(uMsgFilterMax) {
231 if(teb->o.odin.msgWCHAR.message > uMsgFilterMax)
232 goto continuegetmsg;
233 }
234 teb->o.odin.fTranslated = FALSE;
235 memcpy(pMsg, &teb->o.odin.msgWCHAR, sizeof(MSG));
236 teb->o.odin.os2msg.msg = 0;
237 teb->o.odin.os2msg.hwnd = 0;
238 return (pMsg->message != WINWM_QUIT);
239 }
240
241continuegetmsg:
242 if(hwnd) {
243 filtermin = TranslateWinMsg(uMsgFilterMin, TRUE);
244 filtermax = TranslateWinMsg(uMsgFilterMax, FALSE);
245 if(filtermin > filtermax) {
246 ULONG tmp = filtermin;
247 filtermin = filtermax;
248 filtermax = filtermin;
249 }
250 do {
251 WinWaitMsg(teb->o.odin.hab, filtermin, filtermax);
252 rc = OSLibWinPeekMsg(pMsg, hwnd, uMsgFilterMin, uMsgFilterMax, PM_REMOVE_W, isUnicode);
253 }
254 while(rc == FALSE);
255
256 return (pMsg->message != WINWM_QUIT);
257 }
258 else
259 {
260 filtermin = TranslateWinMsg(uMsgFilterMin, TRUE);
261 filtermax = TranslateWinMsg(uMsgFilterMax, FALSE);
262 if(filtermin > filtermax) {
263 ULONG tmp = filtermin;
264 filtermin = filtermax;
265 filtermax = filtermin;
266 }
267 do {
268 eaten = FALSE;
269 rc = WinGetMsg(teb->o.odin.hab, &os2msg, filtermin, filtermax, 0);
270 if (os2msg.msg == WM_TIMER)
271 eaten = TIMER_HandleTimer(&os2msg);
272 } while (eaten);
273 }
274
275 OS2ToWinMsgTranslate((PVOID)teb, &os2msg, pMsg, isUnicode, MSG_REMOVE);
276 memcpy(&teb->o.odin.os2msg, &os2msg, sizeof(QMSG));
277 memcpy(&teb->o.odin.winmsg, pMsg, sizeof(MSG));
278
279 if(pMsg->message <= WINWM_KEYLAST && pMsg->message >= WINWM_KEYDOWN)
280 {
281 if(ProcessKbdHook(pMsg, TRUE))
282 goto continuegetmsg;
283 }
284 return rc;
285}
286//******************************************************************************
287//PeekMessage retrieves only messages associated with the window identified by the
288//hwnd parameter or any of its children as specified by the IsChild function, and within
289//the range of message values given by the uMsgFilterMin and uMsgFilterMax
290//parameters. If hwnd is NULL, PeekMessage retrieves messages for any window that
291//belongs to the current thread making the call. (PeekMessage does not retrieve
292//messages for windows that belong to other threads.) If hwnd is -1, PeekMessage only
293//returns messages with a hwnd value of NULL, as posted by the PostAppMessage
294//function. If uMsgFilterMin and uMsgFilterMax are both zero, PeekMessage returns all
295//available messages (no range filtering is performed).
296//TODO: Not working as specified right now!
297//******************************************************************************
298BOOL OSLibWinPeekMsg(LPMSG pMsg, HWND hwnd, UINT uMsgFilterMin, UINT uMsgFilterMax,
299 DWORD fRemove, BOOL isUnicode)
300{
301 BOOL rc, eaten;
302 TEB *teb;
303 QMSG os2msg;
304 HWND hwndOS2 = 0;
305
306 if(hwnd && hwnd != -1) {
307 hwndOS2 = Win32ToOS2Handle(hwnd);
308 if(hwndOS2 == NULL) {
309 dprintf(("PeekMsg: window %x NOT FOUND!", hwnd));
310 SetLastError(ERROR_INVALID_WINDOW_HANDLE_W);
311 return FALSE;
312 }
313 }
314
315 teb = GetThreadTEB();
316 if(teb == NULL) {
317 DebugInt3();
318 return FALSE;
319 }
320
321 if(teb->o.odin.fTranslated && (!hwnd || hwnd == teb->o.odin.msgWCHAR.hwnd)) {
322 if(uMsgFilterMin) {
323 if(teb->o.odin.msgWCHAR.message < uMsgFilterMin)
324 goto continuepeekmsg;
325 }
326 if(uMsgFilterMax) {
327 if(teb->o.odin.msgWCHAR.message > uMsgFilterMax)
328 goto continuepeekmsg;
329 }
330
331 if(fRemove & PM_REMOVE_W) {
332 teb->o.odin.fTranslated = FALSE;
333 teb->o.odin.os2msg.msg = 0;
334 teb->o.odin.os2msg.hwnd = 0;
335 }
336 memcpy(pMsg, &teb->o.odin.msgWCHAR, sizeof(MSG));
337 return TRUE;
338 }
339
340continuepeekmsg:
341 do {
342 eaten = FALSE;
343 rc = WinPeekMsg(teb->o.odin.hab, &os2msg, hwndOS2, TranslateWinMsg(uMsgFilterMin, TRUE),
344 TranslateWinMsg(uMsgFilterMax, FALSE), (fRemove & PM_REMOVE_W) ? PM_REMOVE : PM_NOREMOVE);
345
346 if (rc && (fRemove & PM_REMOVE_W) && os2msg.msg == WM_TIMER) {
347 eaten = TIMER_HandleTimer(&os2msg);
348 }
349 }
350 while (eaten && rc);
351
352 if(rc == FALSE) {
353 return FALSE;
354 }
355
356 if(OS2ToWinMsgTranslate((PVOID)teb, &os2msg, pMsg, isUnicode, (fRemove & PM_REMOVE_W) ? MSG_REMOVE : MSG_NOREMOVE) == FALSE)
357 {
358 //unused PM message; dispatch immediately and grab next one
359 dprintf2(("OSLibWinPeekMsg: Untranslated message; dispatched immediately"));
360 rc = WinPeekMsg(teb->o.odin.hab, &os2msg, hwndOS2, TranslateWinMsg(uMsgFilterMin, TRUE),
361 TranslateWinMsg(uMsgFilterMax, FALSE), PM_REMOVE);
362 WinDispatchMsg(teb->o.odin.hab, &os2msg);
363 return OSLibWinPeekMsg(pMsg, hwnd, uMsgFilterMin, uMsgFilterMax,
364 fRemove, isUnicode);
365 }
366 //TODO: This is not safe! There's no guarantee this message will be dispatched and it might overwrite a previous message
367 if(fRemove & PM_REMOVE_W) {
368 memcpy(&teb->o.odin.os2msg, &os2msg, sizeof(QMSG));
369 memcpy(&teb->o.odin.winmsg, pMsg, sizeof(MSG));
370 }
371
372 if(pMsg->message <= WINWM_KEYLAST && pMsg->message >= WINWM_KEYDOWN)
373 {
374 if(ProcessKbdHook(pMsg, fRemove))
375 goto continuepeekmsg;
376 }
377
378 return rc;
379}
380//******************************************************************************
381//******************************************************************************
382ULONG OSLibWinQueryMsgTime()
383{
384 return WinQueryMsgTime(GetThreadHAB());
385}
386//******************************************************************************
387//******************************************************************************
388BOOL OSLibWinWaitMessage()
389{
390 return WinWaitMsg(GetThreadHAB(), 0, 0);
391}
392//******************************************************************************
393//TODO: QS_HOTKEY
394//******************************************************************************
395ULONG OSLibWinQueryQueueStatus()
396{
397 ULONG statusOS2, statusWin32 = 0;
398
399 statusOS2 = WinQueryQueueStatus(HWND_DESKTOP);
400
401 if(statusOS2 & QS_KEY)
402 statusWin32 |= QS_KEY_W;
403 if(statusOS2 & QS_MOUSEBUTTON)
404 statusWin32 |= QS_MOUSEBUTTON_W;
405 if(statusOS2 & QS_MOUSEMOVE)
406 statusWin32 |= QS_MOUSEMOVE_W;
407 if(statusOS2 & QS_TIMER)
408 statusWin32 |= QS_TIMER_W;
409 if(statusOS2 & QS_PAINT)
410 statusWin32 |= QS_PAINT_W;
411 if(statusOS2 & QS_POSTMSG)
412 statusWin32 |= QS_POSTMESSAGE_W;
413 if(statusOS2 & QS_SENDMSG)
414 statusWin32 |= QS_SENDMESSAGE_W;
415
416 return statusWin32;
417}
418//******************************************************************************
419//******************************************************************************
420BOOL OSLibWinInSendMessage()
421{
422 return WinInSendMsg(GetThreadHAB());
423}
424//******************************************************************************
425//******************************************************************************
426DWORD OSLibWinGetMessagePos()
427{
428 APIRET rc;
429 POINTL ptl;
430
431 rc = WinQueryMsgPos(GetThreadHAB(), &ptl);
432 if(!rc) {
433 return 0;
434 }
435 //convert to windows coordinates
436 return MAKEULONG(ptl.x,mapScreenY(ptl.y));
437}
438//******************************************************************************
439//******************************************************************************
440LONG OSLibWinGetMessageTime()
441{
442 return (LONG)WinQueryMsgTime(GetThreadHAB());
443}
444//******************************************************************************
445//******************************************************************************
446BOOL OSLibWinReplyMessage(ULONG result)
447{
448 return (BOOL)WinReplyMsg( NULLHANDLE, NULLHANDLE, HMQ_CURRENT, (MRESULT)result);
449}
450//******************************************************************************
451//******************************************************************************
452ULONG OSLibSendMessage(HWND hwnd, ULONG msg, ULONG wParam, ULONG lParam, BOOL fUnicode)
453{
454 POSTMSG_PACKET *packet = (POSTMSG_PACKET *)_smalloc(sizeof(POSTMSG_PACKET));
455
456 packet->Msg = msg;
457 packet->wParam = wParam;
458 packet->lParam = lParam;
459
460 return (ULONG)WinSendMsg(hwnd, WIN32APP_POSTMSG, (MPARAM)((fUnicode) ? WIN32MSG_MAGICW : WIN32MSG_MAGICA), (MPARAM)packet);
461}
462//******************************************************************************
463//******************************************************************************
464ULONG OSLibWinBroadcastMsg(ULONG msg, ULONG wParam, ULONG lParam, BOOL fSend)
465{
466 return WinBroadcastMsg(HWND_DESKTOP, msg, (MPARAM)wParam, (MPARAM)lParam,
467 (fSend) ? BMSG_SEND : BMSG_POST);
468}
469//******************************************************************************
470//******************************************************************************
471BOOL OSLibPostMessage(HWND hwnd, ULONG msg, ULONG wParam, ULONG lParam, BOOL fUnicode)
472{
473 POSTMSG_PACKET *packet = (POSTMSG_PACKET *)_smalloc(sizeof(POSTMSG_PACKET));
474
475 packet->Msg = msg;
476 packet->wParam = wParam;
477 packet->lParam = lParam;
478 return WinPostMsg(hwnd, WIN32APP_POSTMSG, (MPARAM)((fUnicode) ? WIN32MSG_MAGICW : WIN32MSG_MAGICA), (MPARAM)packet);
479}
480//******************************************************************************
481//Direct posting of messages that must remain invisible to the win32 app
482//******************************************************************************
483BOOL OSLibPostMessageDirect(HWND hwnd, ULONG msg, ULONG wParam, ULONG lParam)
484{
485 return WinPostMsg(hwnd, msg, (MPARAM)wParam, (MPARAM)lParam);
486}
487//******************************************************************************
488BOOL _System _O32_PostThreadMessage( DWORD, UINT, WPARAM, LPARAM );
489
490inline BOOL O32_PostThreadMessage(DWORD a, UINT b, WPARAM c, LPARAM d)
491{
492 BOOL yyrc;
493 USHORT sel = RestoreOS2FS();
494
495 yyrc = _O32_PostThreadMessage(a, b, c, d);
496 SetFS(sel);
497
498 return yyrc;
499}
500//******************************************************************************
501BOOL OSLibPostThreadMessage(ULONG threadid, UINT msg, WPARAM wParam, LPARAM lParam, BOOL fUnicode)
502{
503// TEB *teb = GetTEBFromThreadId(threadid);
504 POSTMSG_PACKET *packet = (POSTMSG_PACKET *)_smalloc(sizeof(POSTMSG_PACKET));
505
506// if(teb == NULL) {
507// dprintf(("OSLibPostThreadMessage: thread %x not found!", threadid));
508// return FALSE;
509// }
510 dprintf(("PostThreadMessageA %x %x %x %x", threadid, msg, wParam, lParam));
511 packet->Msg = msg;
512 packet->wParam = wParam;
513 packet->lParam = lParam;
514 return O32_PostThreadMessage(threadid, WIN32APP_POSTMSG-OPEN32_MSGDIFF, ((fUnicode) ? WIN32MSG_MAGICW : WIN32MSG_MAGICA), (LPARAM)packet);
515}
516//******************************************************************************
517//******************************************************************************
518
Note: See TracBrowser for help on using the repository browser.