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

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

* empty log message *

File size: 16.2 KB
Line 
1/* $Id: oslibmsg.cpp,v 1.32 2000-04-13 18:50:44 sandervl Exp $ */
2/*
3 * Window message translation functions for OS/2
4 *
5 *
6 * Copyright 1999 Sander van Leeuwen (sandervl@xs4all.nl)
7 *
8 *
9 * Project Odin Software License can be found in LICENSE.TXT
10 *
11 * TODO: Some messages that are sent to the frame window are directly passed on to the client
12 * -> Get/PeekMessage never gets them as we return a dummy message for non-client windows
13 * (i.e. menu WM_COMMAND messages)
14 *
15 * TODO: Filter translation isn't correct! (for posted messages or messages that don't have
16 * a PM version.
17 *
18 */
19#define INCL_WIN
20#define INCL_PM
21#define INCL_DOSPROCESS
22#include <os2wrap.h>
23#include <string.h>
24#include <misc.h>
25#include "oslibmsg.h"
26#include <winconst.h>
27#include <win32api.h>
28#include <win32wnd.h>
29#include "oslibutil.h"
30#include "timer.h"
31#include <thread.h>
32#include <wprocess.h>
33#include "pmwindow.h"
34#include "oslibwin.h"
35#include <win\hook.h>
36
37#define DBG_LOCALLOG DBG_oslibmsg
38#include "dbglocal.h"
39
40typedef BOOL (EXPENTRY FNTRANS)(MSG *, QMSG *);
41typedef FNTRANS *PFNTRANS;
42
43typedef struct
44{
45 ULONG msgOS2;
46 ULONG msgWin32;
47// PFNTRANS toOS2;
48// PFNTRANS toWIN32;
49} MSGTRANSTAB, *PMSGTRANSTAB;
50
51//NOTE: Must be ordered by win32 message id!!
52MSGTRANSTAB MsgTransTab[] = {
53 WM_NULL, WINWM_NULL,
54 WM_CREATE, WINWM_CREATE,
55 WM_DESTROY, WINWM_DESTROY,
56 WM_MOVE, WINWM_MOVE, //TODO: Sent directly
57 WM_SIZE, WINWM_SIZE, //TODO: Sent directly
58 WM_ACTIVATE, WINWM_ACTIVATE,
59 WM_SETFOCUS, WINWM_SETFOCUS,
60 WM_SETFOCUS, WINWM_KILLFOCUS,
61 WM_ENABLE, WINWM_ENABLE,
62 WM_PAINT, WINWM_PAINT,
63 WM_CLOSE, WINWM_CLOSE,
64 WM_QUIT, WINWM_QUIT,
65 WM_SHOW, WINWM_SHOWWINDOW,
66
67 WM_HITTEST, WINWM_NCHITTEST,
68
69 //TODO: Needs better translation!
70 WM_CHAR, WINWM_KEYDOWN,
71 WM_CHAR, WINWM_KEYUP,
72 WM_CHAR, WINWM_CHAR,
73 WM_CHAR, WINWM_DEADCHAR,
74 WM_CHAR, WINWM_SYSKEYDOWN,
75 WM_CHAR, WINWM_SYSKEYUP,
76 WM_CHAR, WINWM_SYSCHAR,
77 WM_CHAR, WINWM_SYSDEADCHAR,
78 WM_CHAR, WINWM_KEYLAST,
79
80 WM_COMMAND, WINWM_COMMAND,
81 WM_SYSCOMMAND, WINWM_SYSCOMMAND,
82 //
83 WM_TIMER, WINWM_TIMER,
84 WM_INITMENU, WINWM_INITMENU,
85 //
86 WM_MOUSEMOVE, WINWM_MOUSEMOVE,
87 WM_BUTTON1DOWN, WINWM_LBUTTONDOWN,
88 WM_BUTTON1UP, WINWM_LBUTTONUP,
89 WM_BUTTON1DBLCLK, WINWM_LBUTTONDBLCLK,
90 WM_BUTTON2DOWN, WINWM_RBUTTONDOWN,
91 WM_BUTTON2UP, WINWM_RBUTTONUP,
92 WM_BUTTON2DBLCLK, WINWM_RBUTTONDBLCLK,
93 WM_BUTTON3DOWN, WINWM_MBUTTONDOWN,
94 WM_BUTTON3UP, WINWM_MBUTTONUP,
95 WM_BUTTON3DBLCLK, WINWM_MBUTTONDBLCLK,
96
97 999999999, 999999999,
98};
99#define MAX_MSGTRANSTAB (sizeof(MsgTransTab)/sizeof(MsgTransTab[0]))
100
101QMSG *MsgThreadPtr = 0;
102
103LRESULT WIN32API SendMessageA(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
104
105//******************************************************************************
106//******************************************************************************
107BOOL OSLibInitMsgQueue()
108{
109 if(DosAllocThreadLocalMemory(sizeof(QMSG)/sizeof(ULONG), (PULONG *)&MsgThreadPtr) != 0)
110 {
111 dprintf(("OSLibInitMsgQueue: local thread memory alloc failed!!"));
112 DebugInt3();
113 return FALSE;
114 }
115 return TRUE;
116}
117//******************************************************************************
118//******************************************************************************
119void WinToOS2MsgTranslate(MSG *winMsg, QMSG *os2Msg, BOOL isUnicode)
120{
121// memcpy(os2Msg, winMsg, sizeof(MSG));
122// os2Msg->hwnd = Win32Window::Win32ToOS2Handle(winMsg->hwnd);
123// os2Msg->reserved = 0;
124}
125//******************************************************************************
126//TODO: NOT COMPLETE nor 100% CORRECT!!!
127//If both the minimum & maximum message are unknown, the result can be wrong (max > min)!
128//******************************************************************************
129ULONG TranslateWinMsg(ULONG msg, BOOL fMinFilter)
130{
131 if(msg == 0)
132 return 0;
133
134 if(msg >= WINWM_USER)
135 return WIN32APP_POSTMSG;
136
137 for(int i=0;i<MAX_MSGTRANSTAB;i++)
138 {
139 if(fMinFilter && MsgTransTab[i].msgWin32 >= msg) {
140 return MsgTransTab[i].msgOS2;
141 }
142 else
143 if(!fMinFilter && MsgTransTab[i].msgWin32 >= msg) {
144 if(MsgTransTab[i].msgWin32 == msg)
145 return MsgTransTab[i].msgOS2;
146 else return MsgTransTab[i-1].msgOS2;
147 }
148 }
149
150 return 0;
151}
152//******************************************************************************
153//******************************************************************************
154void OSLibWinPostQuitMessage(ULONG nExitCode)
155{
156 APIRET rc;
157
158 rc = WinPostQueueMsg(NULLHANDLE, WM_QUIT, MPFROMLONG(nExitCode), 0);
159 dprintf(("WinPostQueueMsg %d returned %d", nExitCode, rc));
160}
161//******************************************************************************
162//******************************************************************************
163LONG OSLibWinDispatchMsg(MSG *msg, BOOL isUnicode)
164{
165 THDB *thdb;
166 QMSG os2msg;
167 LONG rc;
168
169 thdb = GetThreadTHDB();
170 if(thdb == NULL) {
171 DebugInt3();
172 return FALSE;
173 }
174
175 //TODO: What to do if app changed msg? (translate)
176 // WinToOS2MsgTranslate(msg, &qmsg, isUnicode);
177
178 if(msg->time == MsgThreadPtr->time || msg->hwnd == 0) {
179 memcpy(&os2msg, MsgThreadPtr, sizeof(QMSG));
180 MsgThreadPtr->time = -1;
181 if(msg->hwnd) {
182 thdb->nrOfMsgs = 1;
183 thdb->msgstate++; //odd -> next call to our PM window handler should dispatch the translated msg
184 memcpy(&thdb->msg, msg, sizeof(MSG));
185 }
186 if(os2msg.hwnd || os2msg.msg == WM_QUIT) {
187 memset(MsgThreadPtr, 0, sizeof(*MsgThreadPtr));
188 return (LONG)WinDispatchMsg(thdb->hab, &os2msg);
189 }
190 //SvL: Don't dispatch messages sent by PostThreadMessage (correct??)
191 // Or WM_TIMER msgs with no window handle or timer proc
192 return 0;
193
194 }
195 else {//is this allowed?
196// dprintf(("WARNING: OSLibWinDispatchMsg: called with own message!"));
197 return SendMessageA(msg->hwnd, msg->message, msg->wParam, msg->lParam);
198 }
199}
200//******************************************************************************
201//******************************************************************************
202BOOL OSLibWinGetMsg(LPMSG pMsg, HWND hwnd, UINT uMsgFilterMin, UINT uMsgFilterMax,
203 BOOL isUnicode)
204{
205 BOOL rc, eaten;
206 THDB *thdb;
207 QMSG os2msg;
208 HWND hwndOS2 = 0;
209
210 if(hwnd) {
211 hwndOS2 = Win32BaseWindow::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 thdb = GetThreadTHDB();
221 if(thdb == NULL) {
222 DebugInt3();
223 return TRUE;
224 }
225
226 if(thdb->fTranslated && (!hwnd || hwnd == thdb->msgWCHAR.hwnd)) {
227 if(uMsgFilterMin) {
228 if(thdb->msgWCHAR.message < uMsgFilterMin)
229 goto continuegetmsg;
230 }
231 if(uMsgFilterMax) {
232 if(thdb->msgWCHAR.message > uMsgFilterMax)
233 goto continuegetmsg;
234 }
235 thdb->fTranslated = FALSE;
236 memcpy(pMsg, &thdb->msgWCHAR, sizeof(MSG));
237 MsgThreadPtr->msg = 0;
238 MsgThreadPtr->hwnd = 0;
239 return (pMsg->message != WINWM_QUIT);
240 }
241
242continuegetmsg:
243 if(hwnd) {
244 do {
245 WinWaitMsg(thdb->hab, TranslateWinMsg(uMsgFilterMin, TRUE), TranslateWinMsg(uMsgFilterMax, FALSE));
246 rc = OSLibWinPeekMsg(pMsg, hwnd, uMsgFilterMin, uMsgFilterMax, PM_REMOVE_W, isUnicode);
247 }
248 while(rc == FALSE);
249
250 return (pMsg->message != WINWM_QUIT);
251 }
252 else
253 {
254 do {
255 eaten = FALSE;
256 rc = WinGetMsg(thdb->hab, &os2msg, TranslateWinMsg(uMsgFilterMin, TRUE), TranslateWinMsg(uMsgFilterMax, FALSE), 0);
257 if (os2msg.msg == WM_TIMER)
258 eaten = TIMER_HandleTimer(&os2msg);
259 } while (eaten);
260 }
261
262 OS2ToWinMsgTranslate((PVOID)thdb, &os2msg, pMsg, isUnicode, MSG_REMOVE);
263 memcpy(MsgThreadPtr, &os2msg, sizeof(QMSG));
264
265 if(pMsg->message <= WINWM_KEYLAST && pMsg->message >= WINWM_KEYDOWN)
266 {
267 if(ProcessKbdHook(pMsg, TRUE))
268 goto continuegetmsg;
269 }
270 return rc;
271}
272//******************************************************************************
273//PeekMessage retrieves only messages associated with the window identified by the
274//hwnd parameter or any of its children as specified by the IsChild function, and within
275//the range of message values given by the uMsgFilterMin and uMsgFilterMax
276//parameters. If hwnd is NULL, PeekMessage retrieves messages for any window that
277//belongs to the current thread making the call. (PeekMessage does not retrieve
278//messages for windows that belong to other threads.) If hwnd is -1, PeekMessage only
279//returns messages with a hwnd value of NULL, as posted by the PostAppMessage
280//function. If uMsgFilterMin and uMsgFilterMax are both zero, PeekMessage returns all
281//available messages (no range filtering is performed).
282//TODO: Not working as specified right now!
283//******************************************************************************
284BOOL OSLibWinPeekMsg(LPMSG pMsg, HWND hwnd, UINT uMsgFilterMin, UINT uMsgFilterMax,
285 DWORD fRemove, BOOL isUnicode)
286{
287 BOOL rc, eaten;
288 THDB *thdb;
289 QMSG os2msg;
290 HWND hwndOS2 = 0;
291
292 if(hwnd && hwnd != -1) {
293 hwndOS2 = Win32BaseWindow::Win32ToOS2Handle(hwnd);
294 if(hwndOS2 == NULL) {
295 dprintf(("PeekMsg: window %x NOT FOUND!", hwnd));
296 SetLastError(ERROR_INVALID_WINDOW_HANDLE_W);
297 return FALSE;
298 }
299 }
300
301 thdb = GetThreadTHDB();
302 if(thdb == NULL) {
303 DebugInt3();
304 return FALSE;
305 }
306
307 if(thdb->fTranslated && (!hwnd || hwnd == thdb->msgWCHAR.hwnd)) {
308 if(uMsgFilterMin) {
309 if(thdb->msgWCHAR.message < uMsgFilterMin)
310 goto continuepeekmsg;
311 }
312 if(uMsgFilterMax) {
313 if(thdb->msgWCHAR.message > uMsgFilterMax)
314 goto continuepeekmsg;
315 }
316
317 if(fRemove & PM_REMOVE_W) {
318 thdb->fTranslated = FALSE;
319 MsgThreadPtr->msg = 0;
320 MsgThreadPtr->hwnd = 0;
321 }
322 memcpy(pMsg, &thdb->msgWCHAR, sizeof(MSG));
323 return TRUE;
324 }
325
326continuepeekmsg:
327 do {
328 eaten = FALSE;
329 rc = WinPeekMsg(thdb->hab, &os2msg, hwndOS2, TranslateWinMsg(uMsgFilterMin, TRUE),
330 TranslateWinMsg(uMsgFilterMax, FALSE), (fRemove & PM_REMOVE_W) ? PM_REMOVE : PM_NOREMOVE);
331
332 if (rc && (fRemove & PM_REMOVE_W) && os2msg.msg == WM_TIMER) {
333 eaten = TIMER_HandleTimer(&os2msg);
334 }
335 }
336 while (eaten && rc);
337
338 if(rc == FALSE) {
339 return FALSE;
340 }
341
342 OS2ToWinMsgTranslate((PVOID)thdb, &os2msg, pMsg, isUnicode, (fRemove & PM_REMOVE_W) ? MSG_REMOVE : MSG_NOREMOVE);
343 //TODO: This is not safe! There's no guarantee this message will be dispatched and it might overwrite a previous message
344 if(fRemove & PM_REMOVE_W) {
345 memcpy(MsgThreadPtr, &os2msg, sizeof(QMSG));
346 }
347
348 if(pMsg->message <= WINWM_KEYLAST && pMsg->message >= WINWM_KEYDOWN)
349 {
350 if(ProcessKbdHook(pMsg, fRemove))
351 goto continuepeekmsg;
352 }
353
354 return rc;
355}
356//******************************************************************************
357//******************************************************************************
358ULONG OSLibWinQueryMsgTime()
359{
360 return WinQueryMsgTime(GetThreadHAB());
361}
362//******************************************************************************
363//******************************************************************************
364BOOL OSLibWinWaitMessage()
365{
366 return WinWaitMsg(GetThreadHAB(), 0, 0);
367}
368//******************************************************************************
369//TODO: QS_HOTKEY
370//******************************************************************************
371ULONG OSLibWinQueryQueueStatus()
372{
373 ULONG statusOS2, statusWin32 = 0;
374
375 statusOS2 = WinQueryQueueStatus(HWND_DESKTOP);
376
377 if(statusOS2 & QS_KEY)
378 statusWin32 |= QS_KEY_W;
379 if(statusOS2 & QS_MOUSEBUTTON)
380 statusWin32 |= QS_MOUSEBUTTON_W;
381 if(statusOS2 & QS_MOUSEMOVE)
382 statusWin32 |= QS_MOUSEMOVE_W;
383 if(statusOS2 & QS_TIMER)
384 statusWin32 |= QS_TIMER_W;
385 if(statusOS2 & QS_PAINT)
386 statusWin32 |= QS_PAINT_W;
387 if(statusOS2 & QS_POSTMSG)
388 statusWin32 |= QS_POSTMESSAGE_W;
389 if(statusOS2 & QS_SENDMSG)
390 statusWin32 |= QS_SENDMESSAGE_W;
391
392 return statusWin32;
393}
394//******************************************************************************
395//******************************************************************************
396BOOL OSLibWinInSendMessage()
397{
398 return WinInSendMsg(GetThreadHAB());
399}
400//******************************************************************************
401//******************************************************************************
402DWORD OSLibWinGetMessagePos()
403{
404 APIRET rc;
405 POINTL ptl;
406
407 rc = WinQueryMsgPos(GetThreadHAB(), &ptl);
408 if(!rc) {
409 return 0;
410 }
411 //convert to windows coordinates
412 return MAKEULONG(ptl.x,mapScreenY(ptl.y));
413}
414//******************************************************************************
415//******************************************************************************
416LONG OSLibWinGetMessageTime()
417{
418 return (LONG)WinQueryMsgTime(GetThreadHAB());
419}
420//******************************************************************************
421//******************************************************************************
422BOOL OSLibWinReplyMessage(ULONG result)
423{
424 return (BOOL)WinReplyMsg( NULLHANDLE, NULLHANDLE, HMQ_CURRENT, (MRESULT)result);
425}
426//******************************************************************************
427//******************************************************************************
428ULONG OSLibSendMessage(HWND hwnd, ULONG msg, ULONG wParam, ULONG lParam, BOOL fUnicode)
429{
430 POSTMSG_PACKET *packet = (POSTMSG_PACKET *)_smalloc(sizeof(POSTMSG_PACKET));
431
432 packet->Msg = msg;
433 packet->wParam = wParam;
434 packet->lParam = lParam;
435
436 return (ULONG)WinSendMsg(hwnd, WIN32APP_POSTMSG, (MPARAM)((fUnicode) ? WIN32MSG_MAGICW : WIN32MSG_MAGICA), (MPARAM)packet);
437}
438//******************************************************************************
439//******************************************************************************
440ULONG OSLibWinBroadcastMsg(ULONG msg, ULONG wParam, ULONG lParam, BOOL fSend)
441{
442 return WinBroadcastMsg(HWND_DESKTOP, msg, (MPARAM)wParam, (MPARAM)lParam,
443 (fSend) ? BMSG_SEND : BMSG_POST);
444}
445//******************************************************************************
446//******************************************************************************
447BOOL OSLibPostMessage(HWND hwnd, ULONG msg, ULONG wParam, ULONG lParam, BOOL fUnicode)
448{
449 POSTMSG_PACKET *packet = (POSTMSG_PACKET *)_smalloc(sizeof(POSTMSG_PACKET));
450
451 packet->Msg = msg;
452 packet->wParam = wParam;
453 packet->lParam = lParam;
454 return WinPostMsg(hwnd, WIN32APP_POSTMSG, (MPARAM)((fUnicode) ? WIN32MSG_MAGICW : WIN32MSG_MAGICA), (MPARAM)packet);
455}
456//******************************************************************************
457BOOL _System _O32_PostThreadMessage( DWORD, UINT, WPARAM, LPARAM );
458
459inline BOOL O32_PostThreadMessage(DWORD a, UINT b, WPARAM c, LPARAM d)
460{
461 BOOL yyrc;
462 USHORT sel = RestoreOS2FS();
463
464 yyrc = _O32_PostThreadMessage(a, b, c, d);
465 SetFS(sel);
466
467 return yyrc;
468}
469//******************************************************************************
470BOOL OSLibPostThreadMessage(ULONG threadid, UINT msg, WPARAM wParam, LPARAM lParam, BOOL fUnicode)
471{
472// THDB *thdb = GetTHDBFromThreadId(threadid);
473 POSTMSG_PACKET *packet = (POSTMSG_PACKET *)_smalloc(sizeof(POSTMSG_PACKET));
474
475// if(thdb == NULL) {
476// dprintf(("OSLibPostThreadMessage: thread %x not found!", threadid));
477// return FALSE;
478// }
479 dprintf(("PostThreadMessageA %x %x %x %x", threadid, msg, wParam, lParam));
480 packet->Msg = msg;
481 packet->wParam = wParam;
482 packet->lParam = lParam;
483 return O32_PostThreadMessage(threadid, WIN32APP_POSTMSG-OPEN32_MSGDIFF, ((fUnicode) ? WIN32MSG_MAGICW : WIN32MSG_MAGICA), (LPARAM)packet);
484}
485//******************************************************************************
486//******************************************************************************
487
Note: See TracBrowser for help on using the repository browser.