Release 961208
[wine.git] / windows / message.c
blob05f8ad56efe1bc120508b5280345031df2a0039d
1 /*
2 * Message queues related functions
4 * Copyright 1993, 1994 Alexandre Julliard
5 */
7 #include <stdlib.h>
8 #include <string.h>
9 #include <ctype.h>
10 #include <sys/time.h>
11 #include <sys/types.h>
13 #include "message.h"
14 #include "win.h"
15 #include "gdi.h"
16 #include "sysmetrics.h"
17 #include "heap.h"
18 #include "hook.h"
19 #include "spy.h"
20 #include "winpos.h"
21 #include "atom.h"
22 #include "dde.h"
23 #include "queue.h"
24 #include "winproc.h"
25 #include "stddebug.h"
26 /* #define DEBUG_MSG */
27 #include "debug.h"
29 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
30 #define WM_NCMOUSELAST WM_NCMBUTTONDBLCLK
32 #define HWND_BROADCAST16 ((HWND16)0xffff)
33 #define HWND_BROADCAST32 ((HWND32)0xffffffff)
35 typedef enum { SYSQ_MSG_ABANDON, SYSQ_MSG_SKIP, SYSQ_MSG_ACCEPT } SYSQ_STATUS;
37 extern BOOL MouseButtonsStates[3];
38 extern BOOL AsyncMouseButtonsStates[3];
39 extern BYTE InputKeyStateTable[256];
40 extern BYTE AsyncKeyStateTable[256];
42 BYTE QueueKeyStateTable[256];
44 extern MESSAGEQUEUE *pCursorQueue; /* queue.c */
45 extern MESSAGEQUEUE *pActiveQueue;
47 DWORD MSG_WineStartTicks; /* Ticks at Wine startup */
49 static WORD doubleClickSpeed = 452;
50 static INT32 debugSMRL = 0; /* intertask SendMessage() recursion level */
52 /***********************************************************************
53 * MSG_TranslateMouseMsg
55 * Translate an mouse hardware event into a real mouse message.
56 * Return value indicates whether the translated message must be passed
57 * to the user.
58 * Actions performed:
59 * - Find the window for this message.
60 * - Translate button-down messages in double-clicks.
61 * - Send the WM_NCHITTEST message to find where the cursor is.
62 * - Activate the window if needed.
63 * - Translate the message into a non-client message, or translate
64 * the coordinates to client coordinates.
65 * - Send the WM_SETCURSOR message.
67 static SYSQ_STATUS MSG_TranslateMouseMsg( MSG16 *msg, BOOL remove )
69 WND *pWnd;
70 BOOL eatMsg = FALSE;
71 INT16 hittest;
72 MOUSEHOOKSTRUCT16 *hook;
73 BOOL32 ret;
74 static DWORD lastClickTime = 0;
75 static WORD lastClickMsg = 0;
76 static POINT16 lastClickPos = { 0, 0 };
77 POINT16 pt = msg->pt;
78 MESSAGEQUEUE *queue = (MESSAGEQUEUE *)GlobalLock16(GetTaskQueue(0));
80 BOOL mouseClick = ((msg->message == WM_LBUTTONDOWN) ||
81 (msg->message == WM_RBUTTONDOWN) ||
82 (msg->message == WM_MBUTTONDOWN));
84 /* Find the window */
86 if ((msg->hwnd = GetCapture16()) != 0)
88 BOOL32 ret;
90 ScreenToClient16( msg->hwnd, &pt );
91 msg->lParam = MAKELONG( pt.x, pt.y );
92 /* No need to further process the message */
94 if (!HOOK_IsHooked( WH_MOUSE ) ||
95 !(hook = SEGPTR_NEW(MOUSEHOOKSTRUCT16)))
96 return SYSQ_MSG_ACCEPT;
97 hook->pt = msg->pt;
98 hook->hwnd = msg->hwnd;
99 hook->wHitTestCode = HTCLIENT;
100 hook->dwExtraInfo = 0;
101 ret = !HOOK_CallHooks16( WH_MOUSE, remove ? HC_ACTION : HC_NOREMOVE,
102 msg->message, (LPARAM)SEGPTR_GET(hook));
103 SEGPTR_FREE(hook);
104 return ret ? SYSQ_MSG_ACCEPT : SYSQ_MSG_SKIP ;
107 hittest = WINPOS_WindowFromPoint( WIN_GetDesktop(), msg->pt, &pWnd );
108 if (pWnd->hmemTaskQ != GetTaskQueue(0))
110 /* Not for the current task */
111 if (queue) QUEUE_ClearWakeBit( queue, QS_MOUSE );
112 /* Wake up the other task */
113 queue = (MESSAGEQUEUE *)GlobalLock16( pWnd->hmemTaskQ );
114 if (queue) QUEUE_SetWakeBit( queue, QS_MOUSE );
115 return SYSQ_MSG_ABANDON;
117 pCursorQueue = queue;
118 msg->hwnd = pWnd->hwndSelf;
120 if ((hittest != HTERROR) && mouseClick)
122 HWND hwndTop = WIN_GetTopParent( msg->hwnd );
124 /* Send the WM_PARENTNOTIFY message */
126 WIN_SendParentNotify( msg->hwnd, msg->message, 0,
127 MAKELPARAM( msg->pt.x, msg->pt.y ) );
129 /* Activate the window if needed */
131 if (msg->hwnd != GetActiveWindow() &&
132 msg->hwnd != GetDesktopWindow16())
134 LONG ret = SendMessage16( msg->hwnd, WM_MOUSEACTIVATE, hwndTop,
135 MAKELONG( hittest, msg->message ) );
137 if ((ret == MA_ACTIVATEANDEAT) || (ret == MA_NOACTIVATEANDEAT))
138 eatMsg = TRUE;
140 if (((ret == MA_ACTIVATE) || (ret == MA_ACTIVATEANDEAT))
141 && hwndTop != GetActiveWindow() )
142 WINPOS_SetActiveWindow( hwndTop, TRUE , TRUE );
146 /* Send the WM_SETCURSOR message */
148 SendMessage16( msg->hwnd, WM_SETCURSOR, (WPARAM16)msg->hwnd,
149 MAKELONG( hittest, msg->message ));
150 if (eatMsg) return SYSQ_MSG_SKIP;
152 /* Check for double-click */
154 if (mouseClick)
156 BOOL dbl_click = FALSE;
158 if ((msg->message == lastClickMsg) &&
159 (msg->time - lastClickTime < doubleClickSpeed) &&
160 (abs(msg->pt.x - lastClickPos.x) < SYSMETRICS_CXDOUBLECLK/2) &&
161 (abs(msg->pt.y - lastClickPos.y) < SYSMETRICS_CYDOUBLECLK/2))
162 dbl_click = TRUE;
164 if (dbl_click && (hittest == HTCLIENT))
166 /* Check whether window wants the double click message. */
167 dbl_click = (pWnd->class->style & CS_DBLCLKS) != 0;
170 if (dbl_click) switch(msg->message)
172 case WM_LBUTTONDOWN: msg->message = WM_LBUTTONDBLCLK; break;
173 case WM_RBUTTONDOWN: msg->message = WM_RBUTTONDBLCLK; break;
174 case WM_MBUTTONDOWN: msg->message = WM_MBUTTONDBLCLK; break;
177 if (remove)
179 lastClickTime = msg->time;
180 lastClickMsg = msg->message;
181 lastClickPos = msg->pt;
185 /* Build the translated message */
187 if (hittest == HTCLIENT)
188 ScreenToClient16( msg->hwnd, &pt );
189 else
191 msg->wParam = hittest;
192 msg->message += WM_NCLBUTTONDOWN - WM_LBUTTONDOWN;
194 msg->lParam = MAKELONG( pt.x, pt.y );
196 /* Call the WH_MOUSE hook */
198 if (!HOOK_IsHooked( WH_MOUSE ) ||
199 !(hook = SEGPTR_NEW(MOUSEHOOKSTRUCT16)))
200 return SYSQ_MSG_ACCEPT;
202 hook->pt = msg->pt;
203 hook->hwnd = msg->hwnd;
204 hook->wHitTestCode = hittest;
205 hook->dwExtraInfo = 0;
206 ret = !HOOK_CallHooks16( WH_MOUSE, remove ? HC_ACTION : HC_NOREMOVE,
207 msg->message, (LPARAM)SEGPTR_GET(hook) );
208 SEGPTR_FREE(hook);
209 return ret ? SYSQ_MSG_ACCEPT : SYSQ_MSG_SKIP;
213 /***********************************************************************
214 * MSG_TranslateKeyboardMsg
216 * Translate an keyboard hardware event into a real message.
217 * Return value indicates whether the translated message must be passed
218 * to the user.
220 static SYSQ_STATUS MSG_TranslateKeyboardMsg( MSG16 *msg, BOOL remove )
222 WND *pWnd;
224 /* Should check Ctrl-Esc and PrintScreen here */
226 msg->hwnd = GetFocus16();
227 if (!msg->hwnd)
229 /* Send the message to the active window instead, */
230 /* translating messages to their WM_SYS equivalent */
232 msg->hwnd = GetActiveWindow();
234 if( msg->message < WM_SYSKEYDOWN )
235 msg->message += WM_SYSKEYDOWN - WM_KEYDOWN;
237 pWnd = WIN_FindWndPtr( msg->hwnd );
238 if (pWnd && (pWnd->hmemTaskQ != GetTaskQueue(0)))
240 /* Not for the current task */
241 MESSAGEQUEUE *queue = (MESSAGEQUEUE *)GlobalLock16( GetTaskQueue(0) );
242 if (queue) QUEUE_ClearWakeBit( queue, QS_KEY );
243 /* Wake up the other task */
244 queue = (MESSAGEQUEUE *)GlobalLock16( pWnd->hmemTaskQ );
245 if (queue) QUEUE_SetWakeBit( queue, QS_KEY );
246 return SYSQ_MSG_ABANDON;
248 return (HOOK_CallHooks16( WH_KEYBOARD, remove ? HC_ACTION : HC_NOREMOVE,
249 msg->wParam, msg->lParam )
250 ? SYSQ_MSG_SKIP : SYSQ_MSG_ACCEPT);
254 /***********************************************************************
255 * MSG_JournalRecordMsg
257 * Build an EVENTMSG structure and call JOURNALRECORD hook
259 static void MSG_JournalRecordMsg( MSG16 *msg )
261 EVENTMSG16 *event = SEGPTR_NEW(EVENTMSG16);
262 if (!event) return;
263 event->message = msg->message;
264 event->time = msg->time;
265 if ((msg->message >= WM_KEYFIRST) && (msg->message <= WM_KEYLAST))
267 event->paramL = (msg->wParam & 0xFF) | (HIWORD(msg->lParam) << 8);
268 event->paramH = msg->lParam & 0x7FFF;
269 if (HIWORD(msg->lParam) & 0x0100)
270 event->paramH |= 0x8000; /* special_key - bit */
271 HOOK_CallHooks16( WH_JOURNALRECORD, HC_ACTION, 0,
272 (LPARAM)SEGPTR_GET(event) );
274 else if ((msg->message >= WM_MOUSEFIRST) && (msg->message <= WM_MOUSELAST))
276 event->paramL = LOWORD(msg->lParam); /* X pos */
277 event->paramH = HIWORD(msg->lParam); /* Y pos */
278 ClientToScreen16( msg->hwnd, (LPPOINT16)&event->paramL );
279 HOOK_CallHooks16( WH_JOURNALRECORD, HC_ACTION, 0,
280 (LPARAM)SEGPTR_GET(event) );
282 else if ((msg->message >= WM_NCMOUSEFIRST) &&
283 (msg->message <= WM_NCMOUSELAST))
285 event->paramL = LOWORD(msg->lParam); /* X pos */
286 event->paramH = HIWORD(msg->lParam); /* Y pos */
287 event->message += WM_MOUSEMOVE-WM_NCMOUSEMOVE;/* give no info about NC area */
288 HOOK_CallHooks16( WH_JOURNALRECORD, HC_ACTION, 0,
289 (LPARAM)SEGPTR_GET(event) );
291 SEGPTR_FREE(event);
294 /***********************************************************************
295 * MSG_JournalPlayBackMsg
297 * Get an EVENTMSG struct via call JOURNALPLAYBACK hook function
299 static int MSG_JournalPlayBackMsg(void)
301 EVENTMSG16 *tmpMsg;
302 long wtime,lParam;
303 WORD keyDown,i,wParam,result=0;
305 if ( HOOK_IsHooked( WH_JOURNALPLAYBACK ) )
307 tmpMsg = SEGPTR_NEW(EVENTMSG16);
308 wtime=HOOK_CallHooks16( WH_JOURNALPLAYBACK, HC_GETNEXT, 0,
309 (LPARAM)SEGPTR_GET(tmpMsg));
310 /* dprintf_msg(stddeb,"Playback wait time =%ld\n",wtime); */
311 if (wtime<=0)
313 wtime=0;
314 if ((tmpMsg->message>= WM_KEYFIRST) && (tmpMsg->message <= WM_KEYLAST))
316 wParam=tmpMsg->paramL & 0xFF;
317 lParam=MAKELONG(tmpMsg->paramH&0x7ffff,tmpMsg->paramL>>8);
318 if (tmpMsg->message == WM_KEYDOWN || tmpMsg->message == WM_SYSKEYDOWN)
320 for (keyDown=i=0; i<256 && !keyDown; i++)
321 if (InputKeyStateTable[i] & 0x80)
322 keyDown++;
323 if (!keyDown)
324 lParam |= 0x40000000;
325 AsyncKeyStateTable[wParam]=InputKeyStateTable[wParam] |= 0x80;
327 else /* WM_KEYUP, WM_SYSKEYUP */
329 lParam |= 0xC0000000;
330 AsyncKeyStateTable[wParam]=InputKeyStateTable[wParam] &= ~0x80;
332 if (InputKeyStateTable[VK_MENU] & 0x80)
333 lParam |= 0x20000000;
334 if (tmpMsg->paramH & 0x8000) /*special_key bit*/
335 lParam |= 0x01000000;
336 hardware_event( tmpMsg->message, wParam, lParam,0, 0, tmpMsg->time, 0 );
338 else
340 if ((tmpMsg->message>= WM_MOUSEFIRST) && (tmpMsg->message <= WM_MOUSELAST))
342 switch (tmpMsg->message)
344 case WM_LBUTTONDOWN:MouseButtonsStates[0]=AsyncMouseButtonsStates[0]=1;break;
345 case WM_LBUTTONUP: MouseButtonsStates[0]=AsyncMouseButtonsStates[0]=0;break;
346 case WM_MBUTTONDOWN:MouseButtonsStates[1]=AsyncMouseButtonsStates[1]=1;break;
347 case WM_MBUTTONUP: MouseButtonsStates[1]=AsyncMouseButtonsStates[1]=0;break;
348 case WM_RBUTTONDOWN:MouseButtonsStates[2]=AsyncMouseButtonsStates[2]=1;break;
349 case WM_RBUTTONUP: MouseButtonsStates[2]=AsyncMouseButtonsStates[2]=0;break;
351 AsyncKeyStateTable[VK_LBUTTON]= InputKeyStateTable[VK_LBUTTON] = MouseButtonsStates[0] << 8;
352 AsyncKeyStateTable[VK_MBUTTON]= InputKeyStateTable[VK_MBUTTON] = MouseButtonsStates[1] << 8;
353 AsyncKeyStateTable[VK_RBUTTON]= InputKeyStateTable[VK_RBUTTON] = MouseButtonsStates[2] << 8;
354 SetCursorPos(tmpMsg->paramL,tmpMsg->paramH);
355 lParam=MAKELONG(tmpMsg->paramL,tmpMsg->paramH);
356 wParam=0;
357 if (MouseButtonsStates[0]) wParam |= MK_LBUTTON;
358 if (MouseButtonsStates[1]) wParam |= MK_MBUTTON;
359 if (MouseButtonsStates[2]) wParam |= MK_RBUTTON;
360 hardware_event( tmpMsg->message, wParam, lParam,
361 tmpMsg->paramL, tmpMsg->paramH, tmpMsg->time, 0 );
364 HOOK_CallHooks16( WH_JOURNALPLAYBACK, HC_SKIP, 0,
365 (LPARAM)SEGPTR_GET(tmpMsg));
367 else
368 result= QS_MOUSE | QS_KEY;
369 SEGPTR_FREE(tmpMsg);
371 return result;
374 /***********************************************************************
375 * MSG_PeekHardwareMsg
377 * Peek for a hardware message matching the hwnd and message filters.
379 static BOOL MSG_PeekHardwareMsg( MSG16 *msg, HWND hwnd, WORD first, WORD last,
380 BOOL remove )
382 SYSQ_STATUS status = SYSQ_MSG_ACCEPT;
383 MESSAGEQUEUE *sysMsgQueue = QUEUE_GetSysQueue();
384 int i, pos = sysMsgQueue->nextMessage;
386 /* If the queue is empty, attempt to fill it */
387 if (!sysMsgQueue->msgCount && XPending(display))
388 EVENT_WaitXEvent( FALSE, FALSE );
390 for (i = 0; i < sysMsgQueue->msgCount; i++, pos++)
392 if (pos >= sysMsgQueue->queueSize) pos = 0;
393 *msg = sysMsgQueue->messages[pos].msg;
395 /* Translate message; return FALSE immediately on SYSQ_MSG_ABANDON */
397 if ((msg->message >= WM_MOUSEFIRST) && (msg->message <= WM_MOUSELAST))
399 if ((status = MSG_TranslateMouseMsg(msg,remove)) == SYSQ_MSG_ABANDON)
400 return FALSE;
402 else if ((msg->message >= WM_KEYFIRST) && (msg->message <= WM_KEYLAST))
404 if ((status = MSG_TranslateKeyboardMsg(msg,remove)) == SYSQ_MSG_ABANDON)
405 return FALSE;
407 else /* Non-standard hardware event */
409 HARDWAREHOOKSTRUCT16 *hook;
410 if ((hook = SEGPTR_NEW(HARDWAREHOOKSTRUCT16)))
412 BOOL32 ret;
413 hook->hWnd = msg->hwnd;
414 hook->wMessage = msg->message;
415 hook->wParam = msg->wParam;
416 hook->lParam = msg->lParam;
417 ret = HOOK_CallHooks16( WH_HARDWARE,
418 remove ? HC_ACTION : HC_NOREMOVE,
419 0, (LPARAM)SEGPTR_GET(hook) );
420 SEGPTR_FREE(hook);
421 status = ret ? SYSQ_MSG_SKIP : SYSQ_MSG_ACCEPT;
425 if (status == SYSQ_MSG_SKIP)
427 if (remove) QUEUE_RemoveMsg( sysMsgQueue, pos );
428 /* FIXME: call CBT_CLICKSKIPPED from here */
429 continue;
432 /* Check message against filters */
434 if (hwnd && (msg->hwnd != hwnd)) continue;
435 if ((first || last) &&
436 ((msg->message < first) || (msg->message > last))) continue;
437 if (remove)
439 if (HOOK_IsHooked( WH_JOURNALRECORD ))
440 MSG_JournalRecordMsg( msg );
441 QUEUE_RemoveMsg( sysMsgQueue, pos );
443 return TRUE;
445 return FALSE;
449 /**********************************************************************
450 * SetDoubleClickTime (USER.20)
452 void SetDoubleClickTime( WORD interval )
454 doubleClickSpeed = interval ? interval : 500;
458 /**********************************************************************
459 * GetDoubleClickTime (USER.21)
461 WORD GetDoubleClickTime()
463 return doubleClickSpeed;
467 /***********************************************************************
468 * MSG_SendMessage
470 * Implementation of an inter-task SendMessage.
472 static LRESULT MSG_SendMessage( HQUEUE16 hDestQueue, HWND hwnd, UINT msg,
473 WPARAM16 wParam, LPARAM lParam )
475 INT32 prevSMRL = debugSMRL;
476 QSMCTRL qCtrl = { 0, 1};
477 MESSAGEQUEUE *queue, *destQ;
479 if (!(queue = (MESSAGEQUEUE*)GlobalLock16( GetTaskQueue(0) ))) return 0;
480 if (!(destQ = (MESSAGEQUEUE*)GlobalLock16( hDestQueue ))) return 0;
482 if (IsTaskLocked() || !IsWindow(hwnd)) return 0;
484 debugSMRL+=4;
485 dprintf_sendmsg(stddeb,"%*sSM: %s [%04x] (%04x -> %04x)\n",
486 prevSMRL, "", SPY_GetMsgName(msg), msg, queue->self, hDestQueue );
488 if( !(queue->wakeBits & QS_SMPARAMSFREE) )
490 dprintf_sendmsg(stddeb,"\tIntertask SendMessage: sleeping since unreplied SendMessage pending\n");
491 queue->changeBits &= ~QS_SMPARAMSFREE;
492 QUEUE_WaitBits( QS_SMPARAMSFREE );
495 /* resume sending */
497 queue->hWnd = hwnd;
498 queue->msg = msg;
499 queue->wParam = wParam;
500 queue->lParam = lParam;
501 queue->hPrevSendingTask = destQ->hSendingTask;
502 destQ->hSendingTask = GetTaskQueue(0);
504 queue->wakeBits &= ~QS_SMPARAMSFREE;
506 dprintf_sendmsg(stddeb,"%*ssm: smResultInit = %08x\n", prevSMRL, "", (unsigned)&qCtrl);
508 queue->smResultInit = &qCtrl;
510 QUEUE_SetWakeBit( destQ, QS_SENDMESSAGE );
512 /* perform task switch and wait for the result */
514 while( qCtrl.bPending )
516 if (!(queue->wakeBits & QS_SMRESULT))
518 queue->changeBits &= ~QS_SMRESULT;
519 DirectedYield( destQ->hTask );
520 QUEUE_WaitBits( QS_SMRESULT );
521 dprintf_sendmsg(stddeb,"\tsm: have result!\n");
523 /* got something */
525 dprintf_sendmsg(stddeb,"%*ssm: smResult = %08x\n", prevSMRL, "", (unsigned)queue->smResult );
527 queue->smResult->lResult = queue->SendMessageReturn;
528 queue->smResult->bPending = FALSE;
529 queue->wakeBits &= ~QS_SMRESULT;
531 if( queue->smResult != &qCtrl )
532 dprintf_msg(stddeb,"%*ssm: weird scenes inside the goldmine!\n", prevSMRL, "");
534 queue->smResultInit = NULL;
536 dprintf_sendmsg(stddeb,"%*sSM: [%04x] returning %08lx\n", prevSMRL, "", msg, qCtrl.lResult);
537 debugSMRL-=4;
539 return qCtrl.lResult;
543 /***********************************************************************
544 * ReplyMessage (USER.115)
546 void ReplyMessage( LRESULT result )
548 MESSAGEQUEUE *senderQ;
549 MESSAGEQUEUE *queue;
551 if (!(queue = (MESSAGEQUEUE*)GlobalLock16( GetTaskQueue(0) ))) return;
553 dprintf_msg(stddeb,"ReplyMessage, queue %04x\n", queue->self);
555 while( (senderQ = (MESSAGEQUEUE*)GlobalLock16( queue->InSendMessageHandle)))
557 dprintf_msg(stddeb,"\trpm: replying to %04x (%04x -> %04x)\n",
558 queue->msg, queue->self, senderQ->self);
560 if( queue->wakeBits & QS_SENDMESSAGE )
562 QUEUE_ReceiveMessage( queue );
563 continue; /* ReceiveMessage() already called us */
566 if(!(senderQ->wakeBits & QS_SMRESULT) ) break;
567 OldYield();
569 if( !senderQ ) { dprintf_msg(stddeb,"\trpm: done\n"); return; }
571 senderQ->SendMessageReturn = result;
572 dprintf_msg(stddeb,"\trpm: smResult = %08x, result = %08lx\n",
573 (unsigned)queue->smResultCurrent, result );
575 senderQ->smResult = queue->smResultCurrent;
576 queue->InSendMessageHandle = 0;
578 QUEUE_SetWakeBit( senderQ, QS_SMRESULT );
579 DirectedYield( queue->hSendingTask );
583 /***********************************************************************
584 * MSG_PeekMessage
586 static BOOL MSG_PeekMessage( LPMSG16 msg, HWND hwnd, WORD first, WORD last,
587 WORD flags, BOOL peek )
589 int pos, mask;
590 MESSAGEQUEUE *msgQueue;
591 HQUEUE16 hQueue;
593 #ifdef CONFIG_IPC
594 DDE_TestDDE(hwnd); /* do we have dde handling in the window ?*/
595 DDE_GetRemoteMessage();
596 #endif /* CONFIG_IPC */
598 mask = QS_POSTMESSAGE | QS_SENDMESSAGE; /* Always selected */
599 if (first || last)
601 /* MSWord gets stuck if we do not check for nonclient mouse messages */
603 if ((first <= WM_KEYLAST) && (last >= WM_KEYFIRST)) mask |= QS_KEY;
604 if ( ((first <= WM_MOUSELAST) && (last >= WM_MOUSEFIRST)) ||
605 ((first <= WM_NCMOUSELAST) && (last >= WM_NCMOUSEFIRST)) ) mask |= QS_MOUSE;
606 if ((first <= WM_TIMER) && (last >= WM_TIMER)) mask |= QS_TIMER;
607 if ((first <= WM_SYSTIMER) && (last >= WM_SYSTIMER)) mask |= QS_TIMER;
608 if ((first <= WM_PAINT) && (last >= WM_PAINT)) mask |= QS_PAINT;
610 else mask |= QS_MOUSE | QS_KEY | QS_TIMER | QS_PAINT;
612 if (IsTaskLocked()) flags |= PM_NOYIELD;
614 while(1)
616 hQueue = GetTaskQueue(0);
617 msgQueue = (MESSAGEQUEUE *)GlobalLock16( hQueue );
618 if (!msgQueue) return FALSE;
619 msgQueue->changeBits = 0;
621 /* First handle a message put by SendMessage() */
623 while (msgQueue->wakeBits & QS_SENDMESSAGE)
624 QUEUE_ReceiveMessage( msgQueue );
626 /* Now handle a WM_QUIT message
628 * FIXME: PostQuitMessage() should post WM_QUIT and
629 * set QS_POSTMESSAGE wakebit instead of this.
632 if (msgQueue->wPostQMsg &&
633 (!first || WM_QUIT >= first) &&
634 (!last || WM_QUIT <= last) )
636 msg->hwnd = hwnd;
637 msg->message = WM_QUIT;
638 msg->wParam = msgQueue->wExitCode;
639 msg->lParam = 0;
640 if( !peek ) msgQueue->wPostQMsg = 0;
641 break;
644 /* Now find a normal message */
646 if (((msgQueue->wakeBits & mask) & QS_POSTMESSAGE) &&
647 ((pos = QUEUE_FindMsg( msgQueue, hwnd, first, last )) != -1))
649 QMSG *qmsg = &msgQueue->messages[pos];
650 *msg = qmsg->msg;
651 msgQueue->GetMessageTimeVal = msg->time;
652 msgQueue->GetMessagePosVal = *(DWORD *)&msg->pt;
653 msgQueue->GetMessageExtraInfoVal = qmsg->extraInfo;
655 if (flags & PM_REMOVE) QUEUE_RemoveMsg( msgQueue, pos );
656 break;
659 msgQueue->changeBits |= MSG_JournalPlayBackMsg();
661 /* Now find a hardware event */
663 if (((msgQueue->wakeBits & mask) & (QS_MOUSE | QS_KEY)) &&
664 MSG_PeekHardwareMsg( msg, hwnd, first, last, flags & PM_REMOVE ))
666 /* Got one */
667 msgQueue->GetMessageTimeVal = msg->time;
668 msgQueue->GetMessagePosVal = *(DWORD *)&msg->pt;
669 msgQueue->GetMessageExtraInfoVal = 0; /* Always 0 for now */
670 break;
673 /* Check again for SendMessage */
675 while (msgQueue->wakeBits & QS_SENDMESSAGE)
676 QUEUE_ReceiveMessage( msgQueue );
678 /* Now find a WM_PAINT message */
680 if ((msgQueue->wakeBits & mask) & QS_PAINT)
682 WND* wndPtr;
683 msg->hwnd = WIN_FindWinToRepaint( hwnd , hQueue );
684 msg->message = WM_PAINT;
685 msg->wParam = 0;
686 msg->lParam = 0;
688 if ((wndPtr = WIN_FindWndPtr(msg->hwnd)))
690 if( wndPtr->dwStyle & WS_MINIMIZE &&
691 wndPtr->class->hIcon )
693 msg->message = WM_PAINTICON;
694 msg->wParam = 1;
697 if( !hwnd || msg->hwnd == hwnd || IsChild(hwnd,msg->hwnd) )
699 if( wndPtr->flags & WIN_INTERNAL_PAINT && !wndPtr->hrgnUpdate)
701 wndPtr->flags &= ~WIN_INTERNAL_PAINT;
702 QUEUE_DecPaintCount( hQueue );
704 break;
709 /* Check for timer messages, but yield first */
711 if (!(flags & PM_NOYIELD))
713 UserYield();
714 while (msgQueue->wakeBits & QS_SENDMESSAGE)
715 QUEUE_ReceiveMessage( msgQueue );
717 if ((msgQueue->wakeBits & mask) & QS_TIMER)
719 if (TIMER_GetTimerMsg(msg, hwnd, hQueue, flags & PM_REMOVE)) break;
722 if (peek)
724 if (!(flags & PM_NOYIELD)) UserYield();
725 return FALSE;
727 msgQueue->wakeMask = mask;
728 QUEUE_WaitBits( mask );
731 /* We got a message */
732 if (flags & PM_REMOVE)
734 WORD message = msg->message;
736 if (message == WM_KEYDOWN || message == WM_SYSKEYDOWN)
738 BYTE *p = &QueueKeyStateTable[msg->wParam & 0xff];
740 if (!(*p & 0x80))
741 *p ^= 0x01;
742 *p |= 0x80;
744 else if (message == WM_KEYUP || message == WM_SYSKEYUP)
745 QueueKeyStateTable[msg->wParam & 0xff] &= ~0x80;
747 if (peek) return TRUE;
748 else return (msg->message != WM_QUIT);
752 /***********************************************************************
753 * MSG_InternalGetMessage
755 * GetMessage() function for internal use. Behave like GetMessage(),
756 * but also call message filters and optionally send WM_ENTERIDLE messages.
757 * 'hwnd' must be the handle of the dialog or menu window.
758 * 'code' is the message filter value (MSGF_??? codes).
760 BOOL32 MSG_InternalGetMessage( MSG16 *msg, HWND32 hwnd, HWND32 hwndOwner,
761 WPARAM32 code, WORD flags, BOOL32 sendIdle )
763 for (;;)
765 if (sendIdle)
767 if (!MSG_PeekMessage( msg, 0, 0, 0, flags, TRUE ))
769 /* No message present -> send ENTERIDLE and wait */
770 if (IsWindow(hwndOwner))
771 SendMessage16( hwndOwner, WM_ENTERIDLE,
772 code, (LPARAM)hwnd );
773 MSG_PeekMessage( msg, 0, 0, 0, flags, FALSE );
776 else /* Always wait for a message */
777 MSG_PeekMessage( msg, 0, 0, 0, flags, FALSE );
779 /* Call message filters */
781 if (HOOK_IsHooked( WH_SYSMSGFILTER ) || HOOK_IsHooked( WH_MSGFILTER ))
783 MSG16 *pmsg = SEGPTR_NEW(MSG16);
784 if (pmsg)
786 BOOL32 ret;
787 *pmsg = *msg;
788 ret = ((BOOL16)HOOK_CallHooks16( WH_SYSMSGFILTER, code, 0,
789 (LPARAM)SEGPTR_GET(pmsg) ) ||
790 (BOOL16)HOOK_CallHooks16( WH_MSGFILTER, code, 0,
791 (LPARAM)SEGPTR_GET(pmsg) ));
792 SEGPTR_FREE(pmsg);
793 if (ret)
795 /* Message filtered -> remove it from the queue */
796 /* if it's still there. */
797 if (!(flags & PM_REMOVE))
798 MSG_PeekMessage( msg, 0, 0, 0, PM_REMOVE, TRUE );
799 continue;
804 return (msg->message != WM_QUIT);
809 /***********************************************************************
810 * PeekMessage16 (USER.109)
812 BOOL16 PeekMessage16( LPMSG16 msg, HWND16 hwnd, UINT16 first,
813 UINT16 last, UINT16 flags )
815 return MSG_PeekMessage( msg, hwnd, first, last, flags, TRUE );
819 /***********************************************************************
820 * GetMessage (USER.108)
822 BOOL GetMessage( SEGPTR msg, HWND hwnd, UINT first, UINT last )
824 MSG16 *lpmsg = (MSG16 *)PTR_SEG_TO_LIN(msg);
825 MSG_PeekMessage( lpmsg,
826 hwnd, first, last, PM_REMOVE, FALSE );
828 dprintf_msg(stddeb,"message %04x, hwnd %04x, filter(%04x - %04x)\n", lpmsg->message,
829 hwnd, first, last );
830 HOOK_CallHooks16( WH_GETMESSAGE, HC_ACTION, 0, (LPARAM)msg );
831 return (lpmsg->message != WM_QUIT);
835 /***********************************************************************
836 * PostMessage (USER.110)
838 BOOL PostMessage( HWND hwnd, WORD message, WORD wParam, LONG lParam )
840 MSG16 msg;
841 WND *wndPtr;
843 msg.hwnd = hwnd;
844 msg.message = message;
845 msg.wParam = wParam;
846 msg.lParam = lParam;
847 msg.time = GetTickCount();
848 msg.pt.x = 0;
849 msg.pt.y = 0;
851 #ifdef CONFIG_IPC
852 if (DDE_PostMessage(&msg))
853 return TRUE;
854 #endif /* CONFIG_IPC */
856 if (hwnd == HWND_BROADCAST16)
858 dprintf_msg(stddeb,"PostMessage // HWND_BROADCAST !\n");
859 for (wndPtr = WIN_GetDesktop()->child; wndPtr; wndPtr = wndPtr->next)
861 if (wndPtr->dwStyle & WS_POPUP || wndPtr->dwStyle & WS_CAPTION)
863 dprintf_msg(stddeb,"BROADCAST Message to hWnd=%04x m=%04X w=%04X l=%08lX !\n",
864 wndPtr->hwndSelf, message, wParam, lParam);
865 PostMessage( wndPtr->hwndSelf, message, wParam, lParam );
868 dprintf_msg(stddeb,"PostMessage // End of HWND_BROADCAST !\n");
869 return TRUE;
872 wndPtr = WIN_FindWndPtr( hwnd );
873 if (!wndPtr || !wndPtr->hmemTaskQ) return FALSE;
875 return QUEUE_AddMsg( wndPtr->hmemTaskQ, &msg, 0 );
878 /***********************************************************************
879 * PostAppMessage16 (USER.116)
881 BOOL16 PostAppMessage16( HTASK16 hTask, UINT16 message, WPARAM16 wParam,
882 LPARAM lParam )
884 MSG16 msg;
886 if (GetTaskQueue(hTask) == 0) return FALSE;
887 msg.hwnd = 0;
888 msg.message = message;
889 msg.wParam = wParam;
890 msg.lParam = lParam;
891 msg.time = GetTickCount();
892 msg.pt.x = 0;
893 msg.pt.y = 0;
895 return QUEUE_AddMsg( GetTaskQueue(hTask), &msg, 0 );
899 /***********************************************************************
900 * SendMessage16 (USER.111)
902 LRESULT SendMessage16( HWND16 hwnd, UINT16 msg, WPARAM16 wParam, LPARAM lParam)
904 WND * wndPtr;
905 LRESULT ret;
907 #ifdef CONFIG_IPC
908 MSG16 DDE_msg = { hwnd, msg, wParam, lParam };
909 if (DDE_SendMessage(&DDE_msg)) return TRUE;
910 #endif /* CONFIG_IPC */
912 if (hwnd == HWND_BROADCAST16)
914 dprintf_msg(stddeb,"SendMessage // HWND_BROADCAST !\n");
915 for (wndPtr = WIN_GetDesktop()->child; wndPtr; wndPtr = wndPtr->next)
917 if (wndPtr->dwStyle & WS_POPUP || wndPtr->dwStyle & WS_CAPTION)
919 dprintf_msg(stddeb,"BROADCAST Message to hWnd=%04x m=%04X w=%04lX l=%08lX !\n",
920 wndPtr->hwndSelf, msg, (DWORD)wParam, lParam);
921 SendMessage16( wndPtr->hwndSelf, msg, wParam, lParam );
924 dprintf_msg(stddeb,"SendMessage // End of HWND_BROADCAST !\n");
925 return TRUE;
928 if (HOOK_IsHooked( WH_CALLWNDPROC ))
930 struct msgstruct
932 LPARAM lParam;
933 WPARAM16 wParam;
934 UINT16 wMsg;
935 HWND16 hWnd;
936 } *pmsg;
938 if ((pmsg = SEGPTR_NEW(struct msgstruct)))
940 pmsg->hWnd = hwnd;
941 pmsg->wMsg = msg;
942 pmsg->wParam = wParam;
943 pmsg->lParam = lParam;
944 HOOK_CallHooks16( WH_CALLWNDPROC, HC_ACTION, 1,
945 (LPARAM)SEGPTR_GET(pmsg) );
946 hwnd = pmsg->hWnd;
947 msg = pmsg->wMsg;
948 wParam = pmsg->wParam;
949 lParam = pmsg->lParam;
950 SEGPTR_FREE( pmsg );
954 if (!(wndPtr = WIN_FindWndPtr( hwnd )))
956 fprintf( stderr, "SendMessage16: invalid hwnd %04x\n", hwnd );
957 return 0;
959 if (QUEUE_IsDoomedQueue(wndPtr->hmemTaskQ))
960 return 0; /* Don't send anything if the task is dying */
961 if (wndPtr->hmemTaskQ != GetTaskQueue(0))
962 return MSG_SendMessage( wndPtr->hmemTaskQ, hwnd, msg, wParam, lParam );
964 SPY_EnterMessage( SPY_SENDMESSAGE16, hwnd, msg, wParam, lParam );
965 ret = CallWindowProc16( (WNDPROC16)wndPtr->winproc,
966 hwnd, msg, wParam, lParam );
967 SPY_ExitMessage( SPY_RESULT_OK16, hwnd, msg, ret );
968 return ret;
972 /***********************************************************************
973 * SendMessage32A (USER32.453)
975 LRESULT SendMessage32A(HWND32 hwnd, UINT32 msg, WPARAM32 wParam, LPARAM lParam)
977 WND * wndPtr;
978 LRESULT ret;
980 if (hwnd == HWND_BROADCAST32)
982 for (wndPtr = WIN_GetDesktop()->child; wndPtr; wndPtr = wndPtr->next)
984 /* FIXME: should use something like EnumWindows here */
985 if (wndPtr->dwStyle & WS_POPUP || wndPtr->dwStyle & WS_CAPTION)
986 SendMessage32A( wndPtr->hwndSelf, msg, wParam, lParam );
988 return TRUE;
991 /* FIXME: call hooks */
993 if (!(wndPtr = WIN_FindWndPtr( hwnd )))
995 fprintf( stderr, "SendMessage32A: invalid hwnd %08x\n", hwnd );
996 return 0;
999 if (WINPROC_GetProcType( wndPtr->winproc ) == WIN_PROC_16)
1001 /* Use SendMessage16 for now to get hooks right */
1002 UINT16 msg16;
1003 WPARAM16 wParam16;
1004 if (WINPROC_MapMsg32ATo16( msg, wParam, &msg16, &wParam16, &lParam ) == -1)
1005 return 0;
1006 ret = SendMessage16( hwnd, msg16, wParam16, lParam );
1007 WINPROC_UnmapMsg32ATo16( msg, wParam16, lParam );
1008 return ret;
1011 if (QUEUE_IsDoomedQueue(wndPtr->hmemTaskQ))
1012 return 0; /* Don't send anything if the task is dying */
1014 if (wndPtr->hmemTaskQ != GetTaskQueue(0))
1016 fprintf( stderr, "SendMessage32A: intertask message not supported\n" );
1017 return 0;
1020 SPY_EnterMessage( SPY_SENDMESSAGE32, hwnd, msg, wParam, lParam );
1021 ret = CallWindowProc32A( (WNDPROC32)wndPtr->winproc,
1022 hwnd, msg, wParam, lParam );
1023 SPY_ExitMessage( SPY_RESULT_OK32, hwnd, msg, ret );
1024 return ret;
1028 /***********************************************************************
1029 * SendMessage32W (USER32.458)
1031 LRESULT SendMessage32W(HWND32 hwnd, UINT32 msg, WPARAM32 wParam, LPARAM lParam)
1033 WND * wndPtr;
1034 LRESULT ret;
1036 if (hwnd == HWND_BROADCAST32)
1038 for (wndPtr = WIN_GetDesktop()->child; wndPtr; wndPtr = wndPtr->next)
1040 /* FIXME: should use something like EnumWindows here */
1041 if (wndPtr->dwStyle & WS_POPUP || wndPtr->dwStyle & WS_CAPTION)
1042 SendMessage32W( wndPtr->hwndSelf, msg, wParam, lParam );
1044 return TRUE;
1047 /* FIXME: call hooks */
1049 if (!(wndPtr = WIN_FindWndPtr( hwnd )))
1051 fprintf( stderr, "SendMessage32W: invalid hwnd %08x\n", hwnd );
1052 return 0;
1054 if (QUEUE_IsDoomedQueue(wndPtr->hmemTaskQ))
1055 return 0; /* Don't send anything if the task is dying */
1056 if (wndPtr->hmemTaskQ != GetTaskQueue(0))
1058 fprintf( stderr, "SendMessage32W: intertask message not supported\n" );
1059 return 0;
1062 SPY_EnterMessage( SPY_SENDMESSAGE32, hwnd, msg, wParam, lParam );
1063 ret = CallWindowProc32W( (WNDPROC32)wndPtr->winproc,
1064 hwnd, msg, wParam, lParam );
1065 SPY_ExitMessage( SPY_RESULT_OK32, hwnd, msg, ret );
1066 return ret;
1070 /***********************************************************************
1071 * WaitMessage (USER.112)
1073 void WaitMessage( void )
1075 QUEUE_WaitBits( QS_ALLINPUT );
1079 /***********************************************************************
1080 * TranslateMessage (USER.113)
1082 * TranlateMessage translate virtual-key messages into character-messages,
1083 * as follows :
1084 * WM_KEYDOWN/WM_KEYUP combinations produce a WM_CHAR or WM_DEADCHAR message.
1085 * ditto replacing WM_* with WM_SYS*
1086 * This produces WM_CHAR messages only for keys mapped to ASCII characters
1087 * by the keyboard driver.
1091 BOOL TranslateMessage( LPMSG16 msg )
1093 UINT message = msg->message;
1094 BYTE wparam[2];
1096 if ((debugging_msg
1097 && message != WM_MOUSEMOVE && message != WM_TIMER)
1098 || (debugging_key
1099 && message >= WM_KEYFIRST && message <= WM_KEYLAST))
1100 fprintf(stddeb, "TranslateMessage(%s, %04x, %08lx)\n",
1101 SPY_GetMsgName(msg->message), msg->wParam, msg->lParam);
1102 if ((message == WM_KEYDOWN) || (message == WM_SYSKEYDOWN))
1104 if (debugging_msg || debugging_key)
1105 fprintf(stddeb, "Translating key %04x, scancode %04x\n",
1106 msg->wParam, HIWORD(msg->lParam) );
1108 /* FIXME : should handle ToAscii yielding 2 */
1109 if (ToAscii(msg->wParam, HIWORD(msg->lParam), (LPSTR)&QueueKeyStateTable,
1110 wparam, 0))
1112 /* Map WM_KEY* to WM_*CHAR */
1113 message += 2 - (message & 0x0001);
1115 PostMessage( msg->hwnd, message, wparam[0], msg->lParam );
1117 return TRUE;
1120 return FALSE;
1124 /***********************************************************************
1125 * DispatchMessage (USER.114)
1127 LONG DispatchMessage( const MSG16* msg )
1129 WND * wndPtr;
1130 LONG retval;
1131 int painting;
1133 /* Process timer messages */
1134 if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
1136 if (msg->lParam)
1138 /* HOOK_CallHooks16( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1139 return CallWindowProc16( (WNDPROC16)msg->lParam, msg->hwnd,
1140 msg->message, msg->wParam, GetTickCount() );
1144 if (!msg->hwnd) return 0;
1145 if (!(wndPtr = WIN_FindWndPtr( msg->hwnd ))) return 0;
1146 if (!wndPtr->winproc) return 0;
1147 painting = (msg->message == WM_PAINT);
1148 if (painting) wndPtr->flags |= WIN_NEEDS_BEGINPAINT;
1149 /* HOOK_CallHooks16( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1151 SPY_EnterMessage( SPY_DISPATCHMESSAGE16, msg->hwnd, msg->message,
1152 msg->wParam, msg->lParam );
1153 retval = CallWindowProc16( (WNDPROC16)wndPtr->winproc,
1154 msg->hwnd, msg->message,
1155 msg->wParam, msg->lParam );
1156 SPY_ExitMessage( SPY_RESULT_OK16, msg->hwnd, msg->message, retval );
1158 if (painting && (wndPtr = WIN_FindWndPtr( msg->hwnd )) &&
1159 (wndPtr->flags & WIN_NEEDS_BEGINPAINT) && wndPtr->hrgnUpdate)
1161 fprintf(stderr, "BeginPaint not called on WM_PAINT for hwnd %04x!\n",
1162 msg->hwnd);
1163 wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
1164 /* Validate the update region to avoid infinite WM_PAINT loop */
1165 ValidateRect32( msg->hwnd, NULL );
1167 return retval;
1171 /***********************************************************************
1172 * RegisterWindowMessage16 (USER.118)
1174 WORD RegisterWindowMessage16( SEGPTR str )
1176 dprintf_msg(stddeb, "RegisterWindowMessage16: %08lx\n", (DWORD)str );
1177 return GlobalAddAtom16( str );
1181 /***********************************************************************
1182 * RegisterWindowMessage32A (USER32.436)
1184 WORD RegisterWindowMessage32A( LPCSTR str )
1186 dprintf_msg(stddeb, "RegisterWindowMessage32A: %s\n", str );
1187 return GlobalAddAtom32A( str );
1191 /***********************************************************************
1192 * RegisterWindowMessage32W (USER32.437)
1194 WORD RegisterWindowMessage32W( LPCWSTR str )
1196 dprintf_msg(stddeb, "RegisterWindowMessage32W: %p\n", str );
1197 return GlobalAddAtom32W( str );
1201 /***********************************************************************
1202 * GetTickCount (USER.13) (KERNEL32.299)
1204 DWORD GetTickCount(void)
1206 struct timeval t;
1207 gettimeofday( &t, NULL );
1208 return ((t.tv_sec * 1000) + (t.tv_usec / 1000)) - MSG_WineStartTicks;
1212 /***********************************************************************
1213 * GetCurrentTime (USER.15)
1215 * (effectively identical to GetTickCount)
1217 DWORD GetCurrentTime(void)
1219 return GetTickCount();
1223 /***********************************************************************
1224 * InSendMessage (USER.192)
1226 BOOL InSendMessage()
1228 MESSAGEQUEUE *queue;
1230 if (!(queue = (MESSAGEQUEUE *)GlobalLock16( GetTaskQueue(0) )))
1231 return 0;
1232 return (BOOL)queue->InSendMessageHandle;