2 * Message queues related functions
4 * Copyright 1993, 1994 Alexandre Julliard
11 #include <sys/types.h>
16 #include "sysmetrics.h"
26 /* #define DEBUG_MSG */
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
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 )
72 MOUSEHOOKSTRUCT16 *hook;
74 static DWORD lastClickTime = 0;
75 static WORD lastClickMsg = 0;
76 static POINT16 lastClickPos = { 0, 0 };
78 MESSAGEQUEUE *queue = (MESSAGEQUEUE *)GlobalLock16(GetTaskQueue(0));
80 BOOL mouseClick = ((msg->message == WM_LBUTTONDOWN) ||
81 (msg->message == WM_RBUTTONDOWN) ||
82 (msg->message == WM_MBUTTONDOWN));
86 if ((msg->hwnd = GetCapture16()) != 0)
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;
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));
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))
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 */
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))
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;
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 );
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;
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) );
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
220 static SYSQ_STATUS MSG_TranslateKeyboardMsg( MSG16 *msg, BOOL remove )
224 /* Should check Ctrl-Esc and PrintScreen here */
226 msg->hwnd = GetFocus16();
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);
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) );
294 /***********************************************************************
295 * MSG_JournalPlayBackMsg
297 * Get an EVENTMSG struct via call JOURNALPLAYBACK hook function
299 static int MSG_JournalPlayBackMsg(void)
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); */
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)
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 );
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);
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));
368 result= QS_MOUSE | QS_KEY;
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,
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)
402 else if ((msg->message >= WM_KEYFIRST) && (msg->message <= WM_KEYLAST))
404 if ((status = MSG_TranslateKeyboardMsg(msg,remove)) == SYSQ_MSG_ABANDON)
407 else /* Non-standard hardware event */
409 HARDWAREHOOKSTRUCT16 *hook;
410 if ((hook = SEGPTR_NEW(HARDWAREHOOKSTRUCT16)))
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) );
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 */
432 /* Check message against filters */
434 if (hwnd && (msg->hwnd != hwnd)) continue;
435 if ((first || last) &&
436 ((msg->message < first) || (msg->message > last))) continue;
439 if (HOOK_IsHooked( WH_JOURNALRECORD ))
440 MSG_JournalRecordMsg( msg );
441 QUEUE_RemoveMsg( sysMsgQueue, pos );
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 /***********************************************************************
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;
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 );
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");
525 dprintf_sendmsg(stddeb,"%*ssm: smResult = %08x\n", prevSMRL, "", (unsigned)queue->smResult );
527 if (queue->smResult) { /* FIXME, smResult should always be set */
528 queue->smResult->lResult = queue->SendMessageReturn;
529 queue->smResult->bPending = FALSE;
531 queue->wakeBits &= ~QS_SMRESULT;
533 if( queue->smResult != &qCtrl )
534 dprintf_msg(stddeb,"%*ssm: weird scenes inside the goldmine!\n", prevSMRL, "");
536 queue->smResultInit = NULL;
538 dprintf_sendmsg(stddeb,"%*sSM: [%04x] returning %08lx\n", prevSMRL, "", msg, qCtrl.lResult);
541 return qCtrl.lResult;
545 /***********************************************************************
546 * ReplyMessage (USER.115)
548 void ReplyMessage( LRESULT result )
550 MESSAGEQUEUE *senderQ;
553 if (!(queue = (MESSAGEQUEUE*)GlobalLock16( GetTaskQueue(0) ))) return;
555 dprintf_msg(stddeb,"ReplyMessage, queue %04x\n", queue->self);
557 while( (senderQ = (MESSAGEQUEUE*)GlobalLock16( queue->InSendMessageHandle)))
559 dprintf_msg(stddeb,"\trpm: replying to %04x (%04x -> %04x)\n",
560 queue->msg, queue->self, senderQ->self);
562 if( queue->wakeBits & QS_SENDMESSAGE )
564 QUEUE_ReceiveMessage( queue );
565 continue; /* ReceiveMessage() already called us */
568 if(!(senderQ->wakeBits & QS_SMRESULT) ) break;
571 if( !senderQ ) { dprintf_msg(stddeb,"\trpm: done\n"); return; }
573 senderQ->SendMessageReturn = result;
574 dprintf_msg(stddeb,"\trpm: smResult = %08x, result = %08lx\n",
575 (unsigned)queue->smResultCurrent, result );
577 senderQ->smResult = queue->smResultCurrent;
578 queue->InSendMessageHandle = 0;
580 QUEUE_SetWakeBit( senderQ, QS_SMRESULT );
581 DirectedYield( queue->hSendingTask );
585 /***********************************************************************
588 static BOOL MSG_PeekMessage( LPMSG16 msg, HWND hwnd, WORD first, WORD last,
589 WORD flags, BOOL peek )
592 MESSAGEQUEUE *msgQueue;
596 DDE_TestDDE(hwnd); /* do we have dde handling in the window ?*/
597 DDE_GetRemoteMessage();
598 #endif /* CONFIG_IPC */
600 mask = QS_POSTMESSAGE | QS_SENDMESSAGE; /* Always selected */
603 /* MSWord gets stuck if we do not check for nonclient mouse messages */
605 if ((first <= WM_KEYLAST) && (last >= WM_KEYFIRST)) mask |= QS_KEY;
606 if ( ((first <= WM_MOUSELAST) && (last >= WM_MOUSEFIRST)) ||
607 ((first <= WM_NCMOUSELAST) && (last >= WM_NCMOUSEFIRST)) ) mask |= QS_MOUSE;
608 if ((first <= WM_TIMER) && (last >= WM_TIMER)) mask |= QS_TIMER;
609 if ((first <= WM_SYSTIMER) && (last >= WM_SYSTIMER)) mask |= QS_TIMER;
610 if ((first <= WM_PAINT) && (last >= WM_PAINT)) mask |= QS_PAINT;
612 else mask |= QS_MOUSE | QS_KEY | QS_TIMER | QS_PAINT;
614 if (IsTaskLocked()) flags |= PM_NOYIELD;
618 hQueue = GetTaskQueue(0);
619 msgQueue = (MESSAGEQUEUE *)GlobalLock16( hQueue );
620 if (!msgQueue) return FALSE;
621 msgQueue->changeBits = 0;
623 /* First handle a message put by SendMessage() */
625 while (msgQueue->wakeBits & QS_SENDMESSAGE)
626 QUEUE_ReceiveMessage( msgQueue );
628 /* Now handle a WM_QUIT message
630 * FIXME: PostQuitMessage() should post WM_QUIT and
631 * set QS_POSTMESSAGE wakebit instead of this.
634 if (msgQueue->wPostQMsg &&
635 (!first || WM_QUIT >= first) &&
636 (!last || WM_QUIT <= last) )
639 msg->message = WM_QUIT;
640 msg->wParam = msgQueue->wExitCode;
642 if (flags & PM_REMOVE) msgQueue->wPostQMsg = 0;
646 /* Now find a normal message */
648 if (((msgQueue->wakeBits & mask) & QS_POSTMESSAGE) &&
649 ((pos = QUEUE_FindMsg( msgQueue, hwnd, first, last )) != -1))
651 QMSG *qmsg = &msgQueue->messages[pos];
653 msgQueue->GetMessageTimeVal = msg->time;
654 msgQueue->GetMessagePosVal = *(DWORD *)&msg->pt;
655 msgQueue->GetMessageExtraInfoVal = qmsg->extraInfo;
657 if (flags & PM_REMOVE) QUEUE_RemoveMsg( msgQueue, pos );
661 msgQueue->changeBits |= MSG_JournalPlayBackMsg();
663 /* Now find a hardware event */
665 if (((msgQueue->wakeBits & mask) & (QS_MOUSE | QS_KEY)) &&
666 MSG_PeekHardwareMsg( msg, hwnd, first, last, flags & PM_REMOVE ))
669 msgQueue->GetMessageTimeVal = msg->time;
670 msgQueue->GetMessagePosVal = *(DWORD *)&msg->pt;
671 msgQueue->GetMessageExtraInfoVal = 0; /* Always 0 for now */
675 /* Check again for SendMessage */
677 while (msgQueue->wakeBits & QS_SENDMESSAGE)
678 QUEUE_ReceiveMessage( msgQueue );
680 /* Now find a WM_PAINT message */
682 if ((msgQueue->wakeBits & mask) & QS_PAINT)
685 msg->hwnd = WIN_FindWinToRepaint( hwnd , hQueue );
686 msg->message = WM_PAINT;
690 if ((wndPtr = WIN_FindWndPtr(msg->hwnd)))
692 if( wndPtr->dwStyle & WS_MINIMIZE &&
693 wndPtr->class->hIcon )
695 msg->message = WM_PAINTICON;
699 if( !hwnd || msg->hwnd == hwnd || IsChild(hwnd,msg->hwnd) )
701 if( wndPtr->flags & WIN_INTERNAL_PAINT && !wndPtr->hrgnUpdate)
703 wndPtr->flags &= ~WIN_INTERNAL_PAINT;
704 QUEUE_DecPaintCount( hQueue );
711 /* Check for timer messages, but yield first */
713 if (!(flags & PM_NOYIELD))
716 while (msgQueue->wakeBits & QS_SENDMESSAGE)
717 QUEUE_ReceiveMessage( msgQueue );
719 if ((msgQueue->wakeBits & mask) & QS_TIMER)
721 if (TIMER_GetTimerMsg(msg, hwnd, hQueue, flags & PM_REMOVE)) break;
726 if (!(flags & PM_NOYIELD)) UserYield();
729 msgQueue->wakeMask = mask;
730 QUEUE_WaitBits( mask );
733 /* We got a message */
734 if (flags & PM_REMOVE)
736 WORD message = msg->message;
738 if (message == WM_KEYDOWN || message == WM_SYSKEYDOWN)
740 BYTE *p = &QueueKeyStateTable[msg->wParam & 0xff];
746 else if (message == WM_KEYUP || message == WM_SYSKEYUP)
747 QueueKeyStateTable[msg->wParam & 0xff] &= ~0x80;
749 if (peek) return TRUE;
750 else return (msg->message != WM_QUIT);
754 /***********************************************************************
755 * MSG_InternalGetMessage
757 * GetMessage() function for internal use. Behave like GetMessage(),
758 * but also call message filters and optionally send WM_ENTERIDLE messages.
759 * 'hwnd' must be the handle of the dialog or menu window.
760 * 'code' is the message filter value (MSGF_??? codes).
762 BOOL32 MSG_InternalGetMessage( MSG16 *msg, HWND32 hwnd, HWND32 hwndOwner,
763 WPARAM32 code, WORD flags, BOOL32 sendIdle )
769 if (!MSG_PeekMessage( msg, 0, 0, 0, flags, TRUE ))
771 /* No message present -> send ENTERIDLE and wait */
772 if (IsWindow(hwndOwner))
773 SendMessage16( hwndOwner, WM_ENTERIDLE,
774 code, (LPARAM)hwnd );
775 MSG_PeekMessage( msg, 0, 0, 0, flags, FALSE );
778 else /* Always wait for a message */
779 MSG_PeekMessage( msg, 0, 0, 0, flags, FALSE );
781 /* Call message filters */
783 if (HOOK_IsHooked( WH_SYSMSGFILTER ) || HOOK_IsHooked( WH_MSGFILTER ))
785 MSG16 *pmsg = SEGPTR_NEW(MSG16);
790 ret = ((BOOL16)HOOK_CallHooks16( WH_SYSMSGFILTER, code, 0,
791 (LPARAM)SEGPTR_GET(pmsg) ) ||
792 (BOOL16)HOOK_CallHooks16( WH_MSGFILTER, code, 0,
793 (LPARAM)SEGPTR_GET(pmsg) ));
797 /* Message filtered -> remove it from the queue */
798 /* if it's still there. */
799 if (!(flags & PM_REMOVE))
800 MSG_PeekMessage( msg, 0, 0, 0, PM_REMOVE, TRUE );
806 return (msg->message != WM_QUIT);
811 /***********************************************************************
812 * PeekMessage16 (USER.109)
814 BOOL16 PeekMessage16( LPMSG16 msg, HWND16 hwnd, UINT16 first,
815 UINT16 last, UINT16 flags )
817 return MSG_PeekMessage( msg, hwnd, first, last, flags, TRUE );
821 /***********************************************************************
822 * GetMessage (USER.108)
824 BOOL GetMessage( SEGPTR msg, HWND hwnd, UINT first, UINT last )
826 MSG16 *lpmsg = (MSG16 *)PTR_SEG_TO_LIN(msg);
827 MSG_PeekMessage( lpmsg,
828 hwnd, first, last, PM_REMOVE, FALSE );
830 dprintf_msg(stddeb,"message %04x, hwnd %04x, filter(%04x - %04x)\n", lpmsg->message,
832 HOOK_CallHooks16( WH_GETMESSAGE, HC_ACTION, 0, (LPARAM)msg );
833 return (lpmsg->message != WM_QUIT);
837 /***********************************************************************
838 * PostMessage (USER.110)
840 BOOL PostMessage( HWND hwnd, WORD message, WORD wParam, LONG lParam )
846 msg.message = message;
849 msg.time = GetTickCount();
854 if (DDE_PostMessage(&msg))
856 #endif /* CONFIG_IPC */
858 if (hwnd == HWND_BROADCAST16)
860 dprintf_msg(stddeb,"PostMessage // HWND_BROADCAST !\n");
861 for (wndPtr = WIN_GetDesktop()->child; wndPtr; wndPtr = wndPtr->next)
863 if (wndPtr->dwStyle & WS_POPUP || wndPtr->dwStyle & WS_CAPTION)
865 dprintf_msg(stddeb,"BROADCAST Message to hWnd=%04x m=%04X w=%04X l=%08lX !\n",
866 wndPtr->hwndSelf, message, wParam, lParam);
867 PostMessage( wndPtr->hwndSelf, message, wParam, lParam );
870 dprintf_msg(stddeb,"PostMessage // End of HWND_BROADCAST !\n");
874 wndPtr = WIN_FindWndPtr( hwnd );
875 if (!wndPtr || !wndPtr->hmemTaskQ) return FALSE;
877 return QUEUE_AddMsg( wndPtr->hmemTaskQ, &msg, 0 );
880 /***********************************************************************
881 * PostAppMessage16 (USER.116)
883 BOOL16 PostAppMessage16( HTASK16 hTask, UINT16 message, WPARAM16 wParam,
888 if (GetTaskQueue(hTask) == 0) return FALSE;
890 msg.message = message;
893 msg.time = GetTickCount();
897 return QUEUE_AddMsg( GetTaskQueue(hTask), &msg, 0 );
901 /***********************************************************************
902 * SendMessage16 (USER.111)
904 LRESULT SendMessage16( HWND16 hwnd, UINT16 msg, WPARAM16 wParam, LPARAM lParam)
911 MSG16 DDE_msg = { hwnd, msg, wParam, lParam };
912 if (DDE_SendMessage(&DDE_msg)) return TRUE;
913 #endif /* CONFIG_IPC */
915 if (hwnd == HWND_BROADCAST16)
917 dprintf_msg(stddeb,"SendMessage // HWND_BROADCAST !\n");
918 list = WIN_BuildWinArray( WIN_GetDesktop() );
919 for (ppWnd = list; *ppWnd; ppWnd++)
922 if (!IsWindow(wndPtr->hwndSelf)) continue;
923 if (wndPtr->dwStyle & WS_POPUP || wndPtr->dwStyle & WS_CAPTION)
925 dprintf_msg(stddeb,"BROADCAST Message to hWnd=%04x m=%04X w=%04lX l=%08lX !\n",
926 wndPtr->hwndSelf, msg, (DWORD)wParam, lParam);
927 SendMessage16( wndPtr->hwndSelf, msg, wParam, lParam );
930 HeapFree( SystemHeap, 0, list );
931 dprintf_msg(stddeb,"SendMessage // End of HWND_BROADCAST !\n");
935 if (HOOK_IsHooked( WH_CALLWNDPROC ))
945 if ((pmsg = SEGPTR_NEW(struct msgstruct)))
949 pmsg->wParam = wParam;
950 pmsg->lParam = lParam;
951 HOOK_CallHooks16( WH_CALLWNDPROC, HC_ACTION, 1,
952 (LPARAM)SEGPTR_GET(pmsg) );
955 wParam = pmsg->wParam;
956 lParam = pmsg->lParam;
961 if (!(wndPtr = WIN_FindWndPtr( hwnd )))
963 fprintf( stderr, "SendMessage16: invalid hwnd %04x\n", hwnd );
966 if (QUEUE_IsDoomedQueue(wndPtr->hmemTaskQ))
967 return 0; /* Don't send anything if the task is dying */
968 if (wndPtr->hmemTaskQ != GetTaskQueue(0))
969 return MSG_SendMessage( wndPtr->hmemTaskQ, hwnd, msg, wParam, lParam );
971 SPY_EnterMessage( SPY_SENDMESSAGE16, hwnd, msg, wParam, lParam );
972 ret = CallWindowProc16( (WNDPROC16)wndPtr->winproc,
973 hwnd, msg, wParam, lParam );
974 SPY_ExitMessage( SPY_RESULT_OK16, hwnd, msg, ret );
979 /***********************************************************************
980 * SendMessage32A (USER32.453)
982 LRESULT SendMessage32A(HWND32 hwnd, UINT32 msg, WPARAM32 wParam, LPARAM lParam)
988 if (hwnd == HWND_BROADCAST32)
990 list = WIN_BuildWinArray( WIN_GetDesktop() );
991 for (ppWnd = list; *ppWnd; ppWnd++)
994 if (!IsWindow(wndPtr->hwndSelf)) continue;
995 if (wndPtr->dwStyle & WS_POPUP || wndPtr->dwStyle & WS_CAPTION)
996 SendMessage32A( wndPtr->hwndSelf, msg, wParam, lParam );
998 HeapFree( SystemHeap, 0, list );
1002 /* FIXME: call hooks */
1004 if (!(wndPtr = WIN_FindWndPtr( hwnd )))
1006 fprintf( stderr, "SendMessage32A: invalid hwnd %08x\n", hwnd );
1010 if (WINPROC_GetProcType( wndPtr->winproc ) == WIN_PROC_16)
1012 /* Use SendMessage16 for now to get hooks right */
1015 if (WINPROC_MapMsg32ATo16( msg, wParam, &msg16, &wParam16, &lParam ) == -1)
1017 ret = SendMessage16( hwnd, msg16, wParam16, lParam );
1018 WINPROC_UnmapMsg32ATo16( msg, wParam16, lParam );
1022 if (QUEUE_IsDoomedQueue(wndPtr->hmemTaskQ))
1023 return 0; /* Don't send anything if the task is dying */
1025 if (wndPtr->hmemTaskQ != GetTaskQueue(0))
1027 fprintf( stderr, "SendMessage32A: intertask message not supported\n" );
1031 SPY_EnterMessage( SPY_SENDMESSAGE32, hwnd, msg, wParam, lParam );
1032 ret = CallWindowProc32A( (WNDPROC32)wndPtr->winproc,
1033 hwnd, msg, wParam, lParam );
1034 SPY_ExitMessage( SPY_RESULT_OK32, hwnd, msg, ret );
1039 /***********************************************************************
1040 * SendMessage32W (USER32.458)
1042 LRESULT SendMessage32W(HWND32 hwnd, UINT32 msg, WPARAM32 wParam, LPARAM lParam)
1045 WND **list, **ppWnd;
1048 if (hwnd == HWND_BROADCAST32)
1050 list = WIN_BuildWinArray( WIN_GetDesktop() );
1051 for (ppWnd = list; *ppWnd; ppWnd++)
1054 if (!IsWindow(wndPtr->hwndSelf)) continue;
1055 if (wndPtr->dwStyle & WS_POPUP || wndPtr->dwStyle & WS_CAPTION)
1056 SendMessage32W( wndPtr->hwndSelf, msg, wParam, lParam );
1058 HeapFree( SystemHeap, 0, list );
1062 /* FIXME: call hooks */
1064 if (!(wndPtr = WIN_FindWndPtr( hwnd )))
1066 fprintf( stderr, "SendMessage32W: invalid hwnd %08x\n", hwnd );
1069 if (QUEUE_IsDoomedQueue(wndPtr->hmemTaskQ))
1070 return 0; /* Don't send anything if the task is dying */
1071 if (wndPtr->hmemTaskQ != GetTaskQueue(0))
1073 fprintf( stderr, "SendMessage32W: intertask message not supported\n" );
1077 SPY_EnterMessage( SPY_SENDMESSAGE32, hwnd, msg, wParam, lParam );
1078 ret = CallWindowProc32W( (WNDPROC32)wndPtr->winproc,
1079 hwnd, msg, wParam, lParam );
1080 SPY_ExitMessage( SPY_RESULT_OK32, hwnd, msg, ret );
1085 /***********************************************************************
1086 * WaitMessage (USER.112)
1088 void WaitMessage( void )
1090 QUEUE_WaitBits( QS_ALLINPUT );
1094 /***********************************************************************
1095 * TranslateMessage (USER.113)
1097 * TranlateMessage translate virtual-key messages into character-messages,
1099 * WM_KEYDOWN/WM_KEYUP combinations produce a WM_CHAR or WM_DEADCHAR message.
1100 * ditto replacing WM_* with WM_SYS*
1101 * This produces WM_CHAR messages only for keys mapped to ASCII characters
1102 * by the keyboard driver.
1106 BOOL TranslateMessage( LPMSG16 msg )
1108 UINT message = msg->message;
1112 && message != WM_MOUSEMOVE && message != WM_TIMER)
1114 && message >= WM_KEYFIRST && message <= WM_KEYLAST))
1115 fprintf(stddeb, "TranslateMessage(%s, %04x, %08lx)\n",
1116 SPY_GetMsgName(msg->message), msg->wParam, msg->lParam);
1117 if ((message == WM_KEYDOWN) || (message == WM_SYSKEYDOWN))
1119 if (debugging_msg || debugging_key)
1120 fprintf(stddeb, "Translating key %04x, scancode %04x\n",
1121 msg->wParam, HIWORD(msg->lParam) );
1123 /* FIXME : should handle ToAscii yielding 2 */
1124 if (ToAscii(msg->wParam, HIWORD(msg->lParam), (LPSTR)&QueueKeyStateTable,
1127 /* Map WM_KEY* to WM_*CHAR */
1128 message += 2 - (message & 0x0001);
1130 PostMessage( msg->hwnd, message, wparam[0], msg->lParam );
1139 /***********************************************************************
1140 * DispatchMessage (USER.114)
1142 LONG DispatchMessage( const MSG16* msg )
1148 /* Process timer messages */
1149 if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
1153 /* HOOK_CallHooks16( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1154 return CallWindowProc16( (WNDPROC16)msg->lParam, msg->hwnd,
1155 msg->message, msg->wParam, GetTickCount() );
1159 if (!msg->hwnd) return 0;
1160 if (!(wndPtr = WIN_FindWndPtr( msg->hwnd ))) return 0;
1161 if (!wndPtr->winproc) return 0;
1162 painting = (msg->message == WM_PAINT);
1163 if (painting) wndPtr->flags |= WIN_NEEDS_BEGINPAINT;
1164 /* HOOK_CallHooks16( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1166 SPY_EnterMessage( SPY_DISPATCHMESSAGE16, msg->hwnd, msg->message,
1167 msg->wParam, msg->lParam );
1168 retval = CallWindowProc16( (WNDPROC16)wndPtr->winproc,
1169 msg->hwnd, msg->message,
1170 msg->wParam, msg->lParam );
1171 SPY_ExitMessage( SPY_RESULT_OK16, msg->hwnd, msg->message, retval );
1173 if (painting && (wndPtr = WIN_FindWndPtr( msg->hwnd )) &&
1174 (wndPtr->flags & WIN_NEEDS_BEGINPAINT) && wndPtr->hrgnUpdate)
1176 fprintf(stderr, "BeginPaint not called on WM_PAINT for hwnd %04x!\n",
1178 wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
1179 /* Validate the update region to avoid infinite WM_PAINT loop */
1180 ValidateRect32( msg->hwnd, NULL );
1186 /***********************************************************************
1187 * RegisterWindowMessage16 (USER.118)
1189 WORD RegisterWindowMessage16( SEGPTR str )
1191 dprintf_msg(stddeb, "RegisterWindowMessage16: %08lx\n", (DWORD)str );
1192 return GlobalAddAtom16( str );
1196 /***********************************************************************
1197 * RegisterWindowMessage32A (USER32.436)
1199 WORD RegisterWindowMessage32A( LPCSTR str )
1201 dprintf_msg(stddeb, "RegisterWindowMessage32A: %s\n", str );
1202 return GlobalAddAtom32A( str );
1206 /***********************************************************************
1207 * RegisterWindowMessage32W (USER32.437)
1209 WORD RegisterWindowMessage32W( LPCWSTR str )
1211 dprintf_msg(stddeb, "RegisterWindowMessage32W: %p\n", str );
1212 return GlobalAddAtom32W( str );
1216 /***********************************************************************
1217 * GetTickCount (USER.13) (KERNEL32.299)
1219 DWORD GetTickCount(void)
1222 gettimeofday( &t, NULL );
1223 return ((t.tv_sec * 1000) + (t.tv_usec / 1000)) - MSG_WineStartTicks;
1227 /***********************************************************************
1228 * GetCurrentTime (USER.15)
1230 * (effectively identical to GetTickCount)
1232 DWORD GetCurrentTime(void)
1234 return GetTickCount();
1238 /***********************************************************************
1239 * InSendMessage (USER.192)
1241 BOOL InSendMessage()
1243 MESSAGEQUEUE *queue;
1245 if (!(queue = (MESSAGEQUEUE *)GlobalLock16( GetTaskQueue(0) )))
1247 return (BOOL)queue->InSendMessageHandle;