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

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

Lots of message fixes

File size: 11.7 KB
Line 
1/* $Id: oslibmsg.cpp,v 1.16 1999-12-27 14:41:41 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
35typedef BOOL (EXPENTRY FNTRANS)(MSG *, QMSG *);
36typedef FNTRANS *PFNTRANS;
37
38typedef struct
39{
40 ULONG msgOS2;
41 ULONG msgWin32;
42// PFNTRANS toOS2;
43// PFNTRANS toWIN32;
44} MSGTRANSTAB, *PMSGTRANSTAB;
45
46//NOTE: Must be ordered by win32 message id!!
47MSGTRANSTAB MsgTransTab[] = {
48 WM_NULL, WINWM_NULL,
49 WM_CREATE, WINWM_CREATE,
50 WM_DESTROY, WINWM_DESTROY,
51 WM_MOVE, WINWM_MOVE, //TODO: Sent directly
52 WM_SIZE, WINWM_SIZE, //TODO: Sent directly
53 WM_ACTIVATE, WINWM_ACTIVATE,
54 WM_SETFOCUS, WINWM_SETFOCUS,
55 WM_SETFOCUS, WINWM_KILLFOCUS,
56 WM_ENABLE, WINWM_ENABLE,
57 WM_PAINT, WINWM_PAINT,
58 WM_CLOSE, WINWM_CLOSE,
59 WM_QUIT, WINWM_QUIT,
60 WM_SHOW, WINWM_SHOWWINDOW,
61
62 WM_HITTEST, WINWM_NCHITTEST,
63
64 //TODO: Needs better translation!
65 WM_CHAR, WINWM_KEYDOWN,
66 WM_CHAR, WINWM_KEYUP,
67 WM_CHAR, WINWM_CHAR,
68 WM_CHAR, WINWM_DEADCHAR,
69 WM_CHAR, WINWM_SYSKEYDOWN,
70 WM_CHAR, WINWM_SYSKEYUP,
71 WM_CHAR, WINWM_SYSCHAR,
72 WM_CHAR, WINWM_SYSDEADCHAR,
73 WM_CHAR, WINWM_KEYLAST,
74
75 WM_COMMAND, WINWM_COMMAND,
76 WM_SYSCOMMAND, WINWM_SYSCOMMAND,
77 //
78 WM_TIMER, WINWM_TIMER,
79 WM_INITMENU, WINWM_INITMENU,
80 //
81 WM_MOUSEMOVE, WINWM_MOUSEMOVE,
82 WM_BUTTON1DOWN, WINWM_LBUTTONDOWN,
83 WM_BUTTON1UP, WINWM_LBUTTONUP,
84 WM_BUTTON1DBLCLK, WINWM_LBUTTONDBLCLK,
85 WM_BUTTON2DOWN, WINWM_RBUTTONDOWN,
86 WM_BUTTON2UP, WINWM_RBUTTONUP,
87 WM_BUTTON2DBLCLK, WINWM_RBUTTONDBLCLK,
88 WM_BUTTON3DOWN, WINWM_MBUTTONDOWN,
89 WM_BUTTON3UP, WINWM_MBUTTONUP,
90 WM_BUTTON3DBLCLK, WINWM_MBUTTONDBLCLK,
91
92 999999999, 999999999,
93};
94#define MAX_MSGTRANSTAB (sizeof(MsgTransTab)/sizeof(MsgTransTab[0]))
95
96QMSG *MsgThreadPtr = 0;
97
98LRESULT WIN32API SendMessageA(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
99
100//******************************************************************************
101//******************************************************************************
102BOOL OSLibInitMsgQueue()
103{
104 if(DosAllocThreadLocalMemory(sizeof(QMSG)/sizeof(ULONG), (PULONG *)&MsgThreadPtr) != 0)
105 {
106 dprintf(("OSLibInitMsgQueue: local thread memory alloc failed!!"));
107 DebugInt3();
108 return FALSE;
109 }
110 return TRUE;
111}
112//******************************************************************************
113//******************************************************************************
114void WinToOS2MsgTranslate(MSG *winMsg, QMSG *os2Msg, BOOL isUnicode)
115{
116// memcpy(os2Msg, winMsg, sizeof(MSG));
117// os2Msg->hwnd = Win32Window::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 POSTMSG_PACKET *packet;
127
128 if(msg == 0)
129 return 0;
130
131 if(msg >= WINWM_USER)
132 return WIN32APP_POSTMSG;
133
134 for(int i=0;i<MAX_MSGTRANSTAB;i++)
135 {
136 if(fMinFilter && MsgTransTab[i].msgWin32 >= msg) {
137 return MsgTransTab[i].msgOS2;
138 }
139 else
140 if(!fMinFilter && MsgTransTab[i].msgWin32 >= msg) {
141 if(MsgTransTab[i].msgWin32 == msg)
142 return MsgTransTab[i].msgOS2;
143 else return MsgTransTab[i-1].msgOS2;
144 }
145 }
146
147 return 0;
148}
149//******************************************************************************
150//******************************************************************************
151void OSLibWinPostQuitMessage(ULONG nExitCode)
152{
153 APIRET rc;
154
155 rc = WinPostQueueMsg(NULLHANDLE, WM_QUIT, (MPARAM)nExitCode, 0);
156 dprintf(("WinPostQueueMsg %d returned %d", nExitCode, rc));
157}
158//******************************************************************************
159//******************************************************************************
160LONG OSLibWinDispatchMsg(MSG *msg, BOOL isUnicode)
161{
162 THDB *thdb;
163 QMSG os2msg;
164 LONG rc;
165
166 thdb = GetThreadTHDB();
167 if(thdb == NULL) {
168 DebugInt3();
169 return FALSE;
170 }
171
172 //TODO: What to do if app changed msg? (translate)
173 // WinToOS2MsgTranslate(msg, &qmsg, isUnicode);
174
175 if(msg->time == MsgThreadPtr->time || msg->hwnd == 0) {
176 memcpy(&os2msg, MsgThreadPtr, sizeof(QMSG));
177 MsgThreadPtr->time = -1;
178 if(msg->hwnd) {
179 thdb->nrOfMsgs = 1;
180 thdb->msgstate++; //odd -> next call to our PM window handler should dispatch the translated msg
181 memcpy(&thdb->msg, msg, sizeof(MSG));
182 }
183 return (LONG)WinDispatchMsg(thdb->hab, &os2msg);
184 }
185 else {//is this allowed?
186// dprintf(("WARNING: OSLibWinDispatchMsg: called with own message!"));
187 return SendMessageA(msg->hwnd, msg->message, msg->wParam, msg->lParam);
188 }
189}
190//******************************************************************************
191//******************************************************************************
192BOOL OSLibWinGetMsg(LPMSG pMsg, HWND hwnd, UINT uMsgFilterMin, UINT uMsgFilterMax,
193 BOOL isUnicode)
194{
195 BOOL rc, eaten;
196 THDB *thdb;
197
198 thdb = GetThreadTHDB();
199 if(thdb == NULL) {
200 DebugInt3();
201 return FALSE;
202 }
203
204 if(thdb->fTranslated && (!hwnd || hwnd == thdb->msgWCHAR.hwnd)) {
205 if(uMsgFilterMin) {
206 if(thdb->msgWCHAR.message < uMsgFilterMin)
207 goto continuegetmsg;
208 }
209 if(uMsgFilterMax) {
210 if(thdb->msgWCHAR.message > uMsgFilterMax)
211 goto continuegetmsg;
212 }
213 thdb->fTranslated = FALSE;
214 memcpy(pMsg, &thdb->msgWCHAR, sizeof(MSG));
215 MsgThreadPtr->msg = 0;
216 MsgThreadPtr->hwnd = 0;
217 return TRUE;
218 }
219
220continuegetmsg:
221 if(hwnd) {
222 do {
223 WinWaitMsg(thdb->hab, TranslateWinMsg(uMsgFilterMin, TRUE), TranslateWinMsg(uMsgFilterMax, FALSE));
224 rc = OSLibWinPeekMsg(pMsg, hwnd, uMsgFilterMin, uMsgFilterMax, PM_REMOVE_W, isUnicode);
225 }
226 while(rc == FALSE);
227 }
228 else
229 {
230 do {
231 eaten = FALSE;
232 rc = WinGetMsg(thdb->hab, MsgThreadPtr, TranslateWinMsg(uMsgFilterMin, TRUE), TranslateWinMsg(uMsgFilterMax, FALSE), 0);
233 if (MsgThreadPtr->msg == WM_TIMER)
234 eaten = TIMER_HandleTimer (MsgThreadPtr);
235 } while (eaten);
236 }
237 OS2ToWinMsgTranslate((PVOID)thdb, MsgThreadPtr, pMsg, isUnicode);
238 return rc;
239}
240//******************************************************************************
241//******************************************************************************
242BOOL OSLibWinPeekMsg(LPMSG pMsg, HWND hwnd, UINT uMsgFilterMin, UINT uMsgFilterMax,
243 DWORD fRemove, BOOL isUnicode)
244{
245 BOOL rc, eaten;
246 THDB *thdb;
247 QMSG os2msg;
248
249 thdb = GetThreadTHDB();
250 if(thdb == NULL) {
251 DebugInt3();
252 return FALSE;
253 }
254
255 if(thdb->fTranslated && (!hwnd || hwnd == thdb->msgWCHAR.hwnd)) {
256 if(uMsgFilterMin) {
257 if(thdb->msgWCHAR.message < uMsgFilterMin)
258 goto continuepeekmsg;
259 }
260 if(uMsgFilterMax) {
261 if(thdb->msgWCHAR.message > uMsgFilterMax)
262 goto continuepeekmsg;
263 }
264
265 if(fRemove & PM_REMOVE_W) {
266 thdb->fTranslated = FALSE;
267 MsgThreadPtr->msg = 0;
268 MsgThreadPtr->hwnd = 0;
269 }
270 memcpy(pMsg, &thdb->msgWCHAR, sizeof(MSG));
271 return TRUE;
272 }
273continuepeekmsg:
274 do {
275 eaten = FALSE;
276 rc = WinPeekMsg(thdb->hab, &os2msg, Win32BaseWindow::OS2ToWin32Handle(hwnd), TranslateWinMsg(uMsgFilterMin, TRUE),
277 TranslateWinMsg(uMsgFilterMax, FALSE), (fRemove & PM_REMOVE_W) ? PM_REMOVE : PM_NOREMOVE);
278
279 if (rc && (fRemove & PM_REMOVE_W) && os2msg.msg == WM_TIMER) {
280 eaten = TIMER_HandleTimer(&os2msg);
281 }
282 }
283 while (eaten && rc);
284
285 if(rc == FALSE) {
286 return FALSE;
287 }
288
289 OS2ToWinMsgTranslate((PVOID)thdb, &os2msg, pMsg, isUnicode);
290 //TODO: This is not safe! There's no guarantee this message will be dispatched and it might overwrite a previous message
291 if(fRemove & PM_REMOVE_W) {
292 memcpy(MsgThreadPtr, &os2msg, sizeof(QMSG));
293 }
294 return rc;
295}
296//******************************************************************************
297//******************************************************************************
298ULONG OSLibWinQueryMsgTime()
299{
300 return WinQueryMsgTime(GetThreadHAB());
301}
302//******************************************************************************
303//******************************************************************************
304BOOL OSLibWinWaitMessage()
305{
306 return WinWaitMsg(GetThreadHAB(), 0, 0);
307}
308//******************************************************************************
309//TODO: QS_HOTKEY
310//******************************************************************************
311ULONG OSLibWinQueryQueueStatus()
312{
313 ULONG statusOS2, statusWin32 = 0;
314
315 statusOS2 = WinQueryQueueStatus(HWND_DESKTOP);
316
317 if(statusOS2 & QS_KEY)
318 statusWin32 |= QS_KEY_W;
319 if(statusOS2 & QS_MOUSEBUTTON)
320 statusWin32 |= QS_MOUSEBUTTON_W;
321 if(statusOS2 & QS_MOUSEMOVE)
322 statusWin32 |= QS_MOUSEMOVE_W;
323 if(statusOS2 & QS_TIMER)
324 statusWin32 |= QS_TIMER_W;
325 if(statusOS2 & QS_PAINT)
326 statusWin32 |= QS_PAINT_W;
327 if(statusOS2 & QS_POSTMSG)
328 statusWin32 |= QS_POSTMESSAGE_W;
329 if(statusOS2 & QS_SENDMSG)
330 statusWin32 |= QS_SENDMESSAGE_W;
331
332 return statusWin32;
333}
334//******************************************************************************
335//******************************************************************************
336BOOL OSLibWinInSendMessage()
337{
338 return WinInSendMsg(GetThreadHAB());
339}
340//******************************************************************************
341//******************************************************************************
342DWORD OSLibWinGetMessagePos()
343{
344 APIRET rc;
345 POINTL ptl;
346
347 rc = WinQueryMsgPos(GetThreadHAB(), &ptl);
348 if(!rc) {
349 return 0;
350 }
351 //convert to windows coordinates
352 return MAKEULONG(ptl.x, ScreenHeight - ptl.y - 1);
353}
354//******************************************************************************
355//******************************************************************************
356LONG OSLibWinGetMessageTime()
357{
358 return (LONG)WinQueryMsgTime(GetThreadHAB());
359}
360//******************************************************************************
361//******************************************************************************
362BOOL OSLibWinReplyMessage(ULONG result)
363{
364 return (BOOL)WinReplyMsg( NULLHANDLE, NULLHANDLE, HMQ_CURRENT, (MRESULT)result);
365}
366//******************************************************************************
367//******************************************************************************
Note: See TracBrowser for help on using the repository browser.