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

Last change on this file since 2912 was 2804, checked in by sandervl, 26 years ago

Added new logging feature

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