1 /* * Message queues related functions
3 * Copyright 1993, 1994 Alexandre Julliard
8 #include "wine/winbase16.h"
9 #include "wine/winuser16.h"
15 #include "clipboard.h"
21 #include "debugtools.h"
24 DECLARE_DEBUG_CHANNEL(msg)
25 DECLARE_DEBUG_CHANNEL(sendmsg)
27 #define MAX_QUEUE_SIZE 120 /* Max. size of a message queue */
29 static HQUEUE16 hFirstQueue = 0;
30 static HQUEUE16 hExitingQueue = 0;
31 static HQUEUE16 hmemSysMsgQueue = 0;
32 static MESSAGEQUEUE *sysMsgQueue = NULL;
33 static PERQUEUEDATA *pQDataWin16 = NULL; /* Global perQData for Win16 tasks */
35 static MESSAGEQUEUE *pMouseQueue = NULL; /* Queue for last mouse message */
36 static MESSAGEQUEUE *pKbdQueue = NULL; /* Queue for last kbd message */
38 HQUEUE16 hCursorQueue = 0;
39 HQUEUE16 hActiveQueue = 0;
42 /***********************************************************************
43 * PERQDATA_CreateInstance
45 * Creates an instance of a reference counted PERQUEUEDATA element
46 * for the message queue. perQData is stored globally for 16 bit tasks.
48 * Note: We don't implement perQdata exactly the same way Windows does.
49 * Each perQData element is reference counted since it may be potentially
50 * shared by multiple message Queues (via AttachThreadInput).
51 * We only store the current values for Active, Capture and focus windows
54 PERQUEUEDATA * PERQDATA_CreateInstance( )
62 /* Share a single instance of perQData for all 16 bit tasks */
63 if ( ( bIsWin16 = THREAD_IsWin16( THREAD_Current() ) ) )
65 /* If previously allocated, just bump up ref count */
68 PERQDATA_Addref( pQDataWin16 );
73 /* Allocate PERQUEUEDATA from the system heap */
74 if (!( pQData = (PERQUEUEDATA *) HeapAlloc( SystemHeap, 0,
75 sizeof(PERQUEUEDATA) ) ))
79 pQData->hWndCapture = pQData->hWndFocus = pQData->hWndActive = 0;
80 pQData->ulRefCount = 1;
81 pQData->nCaptureHT = HTCLIENT;
83 /* Note: We have an independent critical section for the per queue data
84 * since this may be shared by different threads. see AttachThreadInput()
86 InitializeCriticalSection( &pQData->cSection );
88 /* Save perQData globally for 16 bit tasks */
96 /***********************************************************************
99 * Increment reference count for the PERQUEUEDATA instance
100 * Returns reference count for debugging purposes
102 ULONG PERQDATA_Addref( PERQUEUEDATA *pQData )
104 assert(pQData != 0 );
105 TRACE_(msg)("(): current refcount %lu ...\n", pQData->ulRefCount);
107 EnterCriticalSection( &pQData->cSection );
108 ++pQData->ulRefCount;
109 LeaveCriticalSection( &pQData->cSection );
111 return pQData->ulRefCount;
115 /***********************************************************************
118 * Release a reference to a PERQUEUEDATA instance.
119 * Destroy the instance if no more references exist
120 * Returns reference count for debugging purposes
122 ULONG PERQDATA_Release( PERQUEUEDATA *pQData )
124 assert(pQData != 0 );
125 TRACE_(msg)("(): current refcount %lu ...\n",
126 (LONG)pQData->ulRefCount );
128 EnterCriticalSection( &pQData->cSection );
129 if ( --pQData->ulRefCount == 0 )
131 LeaveCriticalSection( &pQData->cSection );
132 DeleteCriticalSection( &pQData->cSection );
134 TRACE_(msg)("(): deleting PERQUEUEDATA instance ...\n" );
136 /* Deleting our global 16 bit perQData? */
137 if ( pQData == pQDataWin16 )
140 /* Free the PERQUEUEDATA instance */
141 HeapFree( SystemHeap, 0, pQData );
145 LeaveCriticalSection( &pQData->cSection );
147 return pQData->ulRefCount;
151 /***********************************************************************
152 * PERQDATA_GetFocusWnd
154 * Get the focus hwnd member in a threadsafe manner
156 HWND PERQDATA_GetFocusWnd( PERQUEUEDATA *pQData )
159 assert(pQData != 0 );
161 EnterCriticalSection( &pQData->cSection );
162 hWndFocus = pQData->hWndFocus;
163 LeaveCriticalSection( &pQData->cSection );
169 /***********************************************************************
170 * PERQDATA_SetFocusWnd
172 * Set the focus hwnd member in a threadsafe manner
174 HWND PERQDATA_SetFocusWnd( PERQUEUEDATA *pQData, HWND hWndFocus )
177 assert(pQData != 0 );
179 EnterCriticalSection( &pQData->cSection );
180 hWndFocusPrv = pQData->hWndFocus;
181 pQData->hWndFocus = hWndFocus;
182 LeaveCriticalSection( &pQData->cSection );
188 /***********************************************************************
189 * PERQDATA_GetActiveWnd
191 * Get the active hwnd member in a threadsafe manner
193 HWND PERQDATA_GetActiveWnd( PERQUEUEDATA *pQData )
196 assert(pQData != 0 );
198 EnterCriticalSection( &pQData->cSection );
199 hWndActive = pQData->hWndActive;
200 LeaveCriticalSection( &pQData->cSection );
206 /***********************************************************************
207 * PERQDATA_SetActiveWnd
209 * Set the active focus hwnd member in a threadsafe manner
211 HWND PERQDATA_SetActiveWnd( PERQUEUEDATA *pQData, HWND hWndActive )
214 assert(pQData != 0 );
216 EnterCriticalSection( &pQData->cSection );
217 hWndActivePrv = pQData->hWndActive;
218 pQData->hWndActive = hWndActive;
219 LeaveCriticalSection( &pQData->cSection );
221 return hWndActivePrv;
225 /***********************************************************************
226 * PERQDATA_GetCaptureWnd
228 * Get the capture hwnd member in a threadsafe manner
230 HWND PERQDATA_GetCaptureWnd( PERQUEUEDATA *pQData )
233 assert(pQData != 0 );
235 EnterCriticalSection( &pQData->cSection );
236 hWndCapture = pQData->hWndCapture;
237 LeaveCriticalSection( &pQData->cSection );
243 /***********************************************************************
244 * PERQDATA_SetCaptureWnd
246 * Set the capture hwnd member in a threadsafe manner
248 HWND PERQDATA_SetCaptureWnd( PERQUEUEDATA *pQData, HWND hWndCapture )
251 assert(pQData != 0 );
253 EnterCriticalSection( &pQData->cSection );
254 hWndCapturePrv = pQData->hWndCapture;
255 pQData->hWndCapture = hWndCapture;
256 LeaveCriticalSection( &pQData->cSection );
258 return hWndCapturePrv;
262 /***********************************************************************
263 * PERQDATA_GetCaptureInfo
265 * Get the capture info member in a threadsafe manner
267 INT16 PERQDATA_GetCaptureInfo( PERQUEUEDATA *pQData )
270 assert(pQData != 0 );
272 EnterCriticalSection( &pQData->cSection );
273 nCaptureHT = pQData->nCaptureHT;
274 LeaveCriticalSection( &pQData->cSection );
280 /***********************************************************************
281 * PERQDATA_SetCaptureInfo
283 * Set the capture info member in a threadsafe manner
285 INT16 PERQDATA_SetCaptureInfo( PERQUEUEDATA *pQData, INT16 nCaptureHT )
288 assert(pQData != 0 );
290 EnterCriticalSection( &pQData->cSection );
291 nCaptureHTPrv = pQData->nCaptureHT;
292 pQData->nCaptureHT = nCaptureHT;
293 LeaveCriticalSection( &pQData->cSection );
295 return nCaptureHTPrv;
299 /***********************************************************************
302 * Function for getting a 32 bit pointer on queue strcture. For thread
303 * safeness programmers should use this function instead of GlobalLock to
304 * retrieve a pointer on the structure. QUEUE_Unlock should also be called
305 * when access to the queue structure is not required anymore.
307 MESSAGEQUEUE *QUEUE_Lock( HQUEUE16 hQueue )
311 HeapLock( SystemHeap ); /* FIXME: a bit overkill */
312 queue = GlobalLock16( hQueue );
313 if ( !queue || (queue->magic != QUEUE_MAGIC) )
315 HeapUnlock( SystemHeap );
320 HeapUnlock( SystemHeap );
325 /***********************************************************************
328 * Use with QUEUE_Lock to get a thread safe access to message queue
331 void QUEUE_Unlock( MESSAGEQUEUE *queue )
335 HeapLock( SystemHeap ); /* FIXME: a bit overkill */
337 if ( --queue->lockCount == 0 )
339 DeleteCriticalSection ( &queue->cSection );
341 CloseHandle( queue->hEvent );
342 GlobalFree16( queue->self );
345 HeapUnlock( SystemHeap );
350 /***********************************************************************
353 void QUEUE_DumpQueue( HQUEUE16 hQueue )
357 if (!(pq = (MESSAGEQUEUE*) QUEUE_Lock( hQueue )) )
359 WARN_(msg)("%04x is not a queue handle\n", hQueue );
363 DPRINTF( "next: %12.4x Intertask SendMessage:\n"
364 "thread: %10p ----------------------\n"
365 "firstMsg: %8p smWaiting: %10p\n"
366 "lastMsg: %8p smPending: %10p\n"
367 "msgCount: %8.4x smProcessing: %10p\n"
375 pq->next, pq->thdb, pq->firstMsg, pq->smWaiting, pq->lastMsg,
376 pq->smPending, pq->msgCount, pq->smProcessing,
377 (unsigned)pq->lockCount, pq->wWinVersion,
378 pq->wPaintCount, pq->wTimerCount,
379 pq->wakeBits, pq->wakeMask, pq->hCurHook);
385 /***********************************************************************
388 void QUEUE_WalkQueues(void)
391 HQUEUE16 hQueue = hFirstQueue;
393 DPRINTF( "Queue Msgs Thread Task Module\n" );
396 MESSAGEQUEUE *queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue );
399 WARN_(msg)("Bad queue handle %04x\n", hQueue );
402 if (!GetModuleName16( queue->thdb->process->task, module, sizeof(module )))
403 strcpy( module, "???" );
404 DPRINTF( "%04x %4d %p %04x %s\n", hQueue,queue->msgCount,
405 queue->thdb, queue->thdb->process->task, module );
406 hQueue = queue->next;
407 QUEUE_Unlock( queue );
413 /***********************************************************************
414 * QUEUE_IsExitingQueue
416 BOOL QUEUE_IsExitingQueue( HQUEUE16 hQueue )
418 return (hExitingQueue && (hQueue == hExitingQueue));
422 /***********************************************************************
423 * QUEUE_SetExitingQueue
425 void QUEUE_SetExitingQueue( HQUEUE16 hQueue )
427 hExitingQueue = hQueue;
431 /***********************************************************************
432 * QUEUE_CreateMsgQueue
434 * Creates a message queue. Doesn't link it into queue list!
436 static HQUEUE16 QUEUE_CreateMsgQueue( BOOL16 bCreatePerQData )
439 MESSAGEQUEUE * msgQueue;
440 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
442 TRACE_(msg)("(): Creating message queue...\n");
444 if (!(hQueue = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT,
445 sizeof(MESSAGEQUEUE) )))
448 msgQueue = (MESSAGEQUEUE *) GlobalLock16( hQueue );
452 msgQueue->self = hQueue;
453 msgQueue->wakeBits = msgQueue->changeBits = 0;
454 msgQueue->wWinVersion = pTask ? pTask->version : 0;
456 InitializeCriticalSection( &msgQueue->cSection );
457 MakeCriticalSectionGlobal( &msgQueue->cSection );
459 /* Create an Event object for waiting on message, used by win32 thread
461 if ( !THREAD_IsWin16( THREAD_Current() ) )
463 msgQueue->hEvent = CreateEventA( NULL, FALSE, FALSE, NULL);
465 if (msgQueue->hEvent == 0)
467 WARN_(msg)("CreateEvent32A is not able to create an event object");
470 msgQueue->hEvent = ConvertToGlobalHandle( msgQueue->hEvent );
473 msgQueue->hEvent = 0;
475 msgQueue->lockCount = 1;
476 msgQueue->magic = QUEUE_MAGIC;
478 /* Create and initialize our per queue data */
479 msgQueue->pQData = bCreatePerQData ? PERQDATA_CreateInstance() : NULL;
485 /***********************************************************************
488 * Try to reply to all pending sent messages on exit.
490 void QUEUE_FlushMessages( MESSAGEQUEUE *queue )
493 MESSAGEQUEUE *senderQ = 0;
497 EnterCriticalSection( &queue->cSection );
499 /* empty the list of pending SendMessage waiting to be received */
500 while (queue->smPending)
502 smsg = QUEUE_RemoveSMSG( queue, SM_PENDING_LIST, 0);
504 senderQ = (MESSAGEQUEUE*)QUEUE_Lock( smsg->hSrcQueue );
508 /* return 0, to unblock other thread */
510 smsg->flags |= SMSG_HAVE_RESULT;
511 QUEUE_SetWakeBit( senderQ, QS_SMRESULT);
513 QUEUE_Unlock( senderQ );
516 QUEUE_ClearWakeBit( queue, QS_SENDMESSAGE );
518 LeaveCriticalSection( &queue->cSection );
523 /***********************************************************************
524 * QUEUE_DeleteMsgQueue
526 * Unlinks and deletes a message queue.
528 * Note: We need to mask asynchronous events to make sure PostMessage works
529 * even in the signal handler.
531 BOOL QUEUE_DeleteMsgQueue( HQUEUE16 hQueue )
533 MESSAGEQUEUE * msgQueue = (MESSAGEQUEUE*)QUEUE_Lock(hQueue);
536 TRACE_(msg)("(): Deleting message queue %04x\n", hQueue);
538 if (!hQueue || !msgQueue)
540 WARN_(msg)("invalid argument.\n");
546 if( hCursorQueue == hQueue ) hCursorQueue = 0;
547 if( hActiveQueue == hQueue ) hActiveQueue = 0;
549 /* flush sent messages */
550 QUEUE_FlushMessages( msgQueue );
552 HeapLock( SystemHeap ); /* FIXME: a bit overkill */
554 /* Release per queue data if present */
555 if ( msgQueue->pQData )
557 PERQDATA_Release( msgQueue->pQData );
558 msgQueue->pQData = 0;
561 /* remove the message queue from the global link list */
562 pPrev = &hFirstQueue;
563 while (*pPrev && (*pPrev != hQueue))
565 MESSAGEQUEUE *msgQ = (MESSAGEQUEUE*)GlobalLock16(*pPrev);
568 if ( !msgQ || (msgQ->magic != QUEUE_MAGIC) )
570 /* HQUEUE link list is corrupted, try to exit gracefully */
571 WARN_(msg)("HQUEUE link list corrupted!\n");
577 if (pPrev && *pPrev) *pPrev = msgQueue->next;
580 HeapUnlock( SystemHeap );
582 /* free up resource used by MESSAGEQUEUE strcture */
583 msgQueue->lockCount--;
584 QUEUE_Unlock( msgQueue );
590 /***********************************************************************
591 * QUEUE_CreateSysMsgQueue
593 * Create the system message queue, and set the double-click speed.
594 * Must be called only once.
596 BOOL QUEUE_CreateSysMsgQueue( int size )
598 /* Note: We dont need perQ data for the system message queue */
599 if (!(hmemSysMsgQueue = QUEUE_CreateMsgQueue( FALSE )))
602 sysMsgQueue = (MESSAGEQUEUE *) GlobalLock16( hmemSysMsgQueue );
607 /***********************************************************************
610 MESSAGEQUEUE *QUEUE_GetSysQueue(void)
616 /***********************************************************************
619 * See "Windows Internals", p.449
621 void QUEUE_SetWakeBit( MESSAGEQUEUE *queue, WORD bit )
623 TRACE_(msg)("queue = %04x (wm=%04x), bit = %04x\n",
624 queue->self, queue->wakeMask, bit );
626 if (bit & QS_MOUSE) pMouseQueue = queue;
627 if (bit & QS_KEY) pKbdQueue = queue;
628 queue->changeBits |= bit;
629 queue->wakeBits |= bit;
630 if (queue->wakeMask & bit)
634 /* Wake up thread waiting for message */
635 if ( THREAD_IsWin16( queue->thdb ) )
636 PostEvent16( queue->thdb->process->task );
639 SetEvent( queue->hEvent );
645 /***********************************************************************
648 void QUEUE_ClearWakeBit( MESSAGEQUEUE *queue, WORD bit )
650 queue->changeBits &= ~bit;
651 queue->wakeBits &= ~bit;
655 /***********************************************************************
658 * See "Windows Internals", p.447
661 * 0 if exit with timeout
664 int QUEUE_WaitBits( WORD bits, DWORD timeout )
669 TRACE_(msg)("q %04x waiting for %04x\n", GetFastQueue16(), bits);
671 if ( THREAD_IsWin16( THREAD_Current() ) && (timeout != INFINITE) )
672 curTime = GetTickCount();
674 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
678 if (queue->changeBits & bits)
680 /* One of the bits is set; we can return */
682 QUEUE_Unlock( queue );
685 if (queue->wakeBits & QS_SENDMESSAGE)
687 /* Process the sent message immediately */
690 QUEUE_ReceiveMessage( queue );
691 continue; /* nested sm crux */
694 queue->wakeMask = bits | QS_SENDMESSAGE;
695 if(queue->changeBits & bits)
700 TRACE_(msg)("%04x) wakeMask is %04x, waiting\n", queue->self, queue->wakeMask);
702 if ( !THREAD_IsWin16( THREAD_Current() ) )
707 if ( (bHasWin16Lock = _ConfirmWin16Lock()) )
709 TRACE_(msg)("bHasWin16Lock=TRUE\n");
710 ReleaseThunkLock( &dwlc );
712 WaitForSingleObject( queue->hEvent, timeout );
715 RestoreThunkLock( dwlc );
720 if ( timeout == INFINITE )
721 WaitEvent16( 0 ); /* win 16 thread, use WaitEvent */
724 /* check for timeout, then give control to other tasks */
725 if (GetTickCount() - curTime > timeout)
728 QUEUE_Unlock( queue );
729 return 0; /* exit with timeout */
738 /***********************************************************************
741 * This routine is called when a SMSG need to be added to one of the three
742 * SM list. (SM_PROCESSING_LIST, SM_PENDING_LIST, SM_WAITING_LIST)
744 BOOL QUEUE_AddSMSG( MESSAGEQUEUE *queue, int list, SMSG *smsg )
746 TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
747 smsg, SPY_GetMsgName(smsg->msg));
751 case SM_PROCESSING_LIST:
752 /* don't need to be thread safe, only accessed by the
753 thread associated with the sender queue */
754 smsg->nextProcessing = queue->smProcessing;
755 queue->smProcessing = smsg;
758 case SM_WAITING_LIST:
759 /* don't need to be thread safe, only accessed by the
760 thread associated with the receiver queue */
761 smsg->nextWaiting = queue->smWaiting;
762 queue->smWaiting = smsg;
765 case SM_PENDING_LIST:
767 /* make it thread safe, could be accessed by the sender and
771 EnterCriticalSection( &queue->cSection );
772 smsg->nextPending = NULL;
773 prev = &queue->smPending;
775 prev = &(*prev)->nextPending;
777 LeaveCriticalSection( &queue->cSection );
779 QUEUE_SetWakeBit( queue, QS_SENDMESSAGE );
784 WARN_(sendmsg)("Invalid list: %d", list);
792 /***********************************************************************
795 * This routine is called when a SMSG need to be remove from one of the three
796 * SM list. (SM_PROCESSING_LIST, SM_PENDING_LIST, SM_WAITING_LIST)
797 * If smsg == 0, remove the first smsg from the specified list
799 SMSG *QUEUE_RemoveSMSG( MESSAGEQUEUE *queue, int list, SMSG *smsg )
804 case SM_PROCESSING_LIST:
805 /* don't need to be thread safe, only accessed by the
806 thread associated with the sender queue */
808 /* if smsg is equal to null, it means the first in the list */
810 smsg = queue->smProcessing;
812 TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
813 smsg, SPY_GetMsgName(smsg->msg));
814 /* In fact SM_PROCESSING_LIST is a stack, and smsg
815 should be always at the top of the list */
816 if ( (smsg != queue->smProcessing) || !queue->smProcessing )
818 ERR_(sendmsg)("smsg not at the top of Processing list, smsg=0x%p queue=0x%p", smsg, queue);
823 queue->smProcessing = smsg->nextProcessing;
824 smsg->nextProcessing = 0;
828 case SM_WAITING_LIST:
829 /* don't need to be thread safe, only accessed by the
830 thread associated with the receiver queue */
832 /* if smsg is equal to null, it means the first in the list */
834 smsg = queue->smWaiting;
836 TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
837 smsg, SPY_GetMsgName(smsg->msg));
838 /* In fact SM_WAITING_LIST is a stack, and smsg
839 should be always at the top of the list */
840 if ( (smsg != queue->smWaiting) || !queue->smWaiting )
842 ERR_(sendmsg)("smsg not at the top of Waiting list, smsg=0x%p queue=0x%p", smsg, queue);
847 queue->smWaiting = smsg->nextWaiting;
848 smsg->nextWaiting = 0;
852 case SM_PENDING_LIST:
853 /* make it thread safe, could be accessed by the sender and
855 EnterCriticalSection( &queue->cSection );
857 if (!smsg || !queue->smPending)
858 smsg = queue->smPending;
861 ERR_(sendmsg)("should always remove the top one in Pending list, smsg=0x%p queue=0x%p", smsg, queue);
862 LeaveCriticalSection( &queue->cSection );
866 TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
867 smsg, SPY_GetMsgName(smsg->msg));
869 queue->smPending = smsg->nextPending;
870 smsg->nextPending = 0;
872 /* if no more SMSG in Pending list, clear QS_SENDMESSAGE flag */
873 if (!queue->smPending)
874 QUEUE_ClearWakeBit( queue, QS_SENDMESSAGE );
876 LeaveCriticalSection( &queue->cSection );
880 WARN_(sendmsg)("Invalid list: %d", list);
888 /***********************************************************************
889 * QUEUE_ReceiveMessage
891 * This routine is called when a sent message is waiting for the queue.
893 void QUEUE_ReceiveMessage( MESSAGEQUEUE *queue )
897 MESSAGEQUEUE *senderQ;
899 TRACE_(sendmsg)("queue %04x\n", queue->self );
901 if ( !(queue->wakeBits & QS_SENDMESSAGE) && queue->smPending )
903 TRACE_(sendmsg)("\trcm: nothing to do\n");
907 /* remove smsg on the top of the pending list and put it in the processing list */
908 smsg = QUEUE_RemoveSMSG(queue, SM_PENDING_LIST, 0);
909 QUEUE_AddSMSG(queue, SM_WAITING_LIST, smsg);
911 TRACE_(sendmsg)("RM: %s [%04x] (%04x -> %04x)\n",
912 SPY_GetMsgName(smsg->msg), smsg->msg, smsg->hSrcQueue, smsg->hDstQueue );
914 if (IsWindow( smsg->hWnd ))
916 WND *wndPtr = WIN_FindWndPtr( smsg->hWnd );
917 DWORD extraInfo = queue->GetMessageExtraInfoVal; /* save ExtraInfo */
919 /* use sender queue extra info value while calling the window proc */
920 senderQ = (MESSAGEQUEUE*)QUEUE_Lock( smsg->hSrcQueue );
923 queue->GetMessageExtraInfoVal = senderQ->GetMessageExtraInfoVal;
924 QUEUE_Unlock( senderQ );
927 /* call the right version of CallWindowProcXX */
928 if (smsg->flags & SMSG_WIN32)
930 TRACE_(sendmsg)("\trcm: msg is Win32\n" );
931 if (smsg->flags & SMSG_UNICODE)
932 result = CallWindowProcW( wndPtr->winproc,
933 smsg->hWnd, smsg->msg,
934 smsg->wParam, smsg->lParam );
936 result = CallWindowProcA( wndPtr->winproc,
937 smsg->hWnd, smsg->msg,
938 smsg->wParam, smsg->lParam );
940 else /* Win16 message */
941 result = CallWindowProc16( (WNDPROC16)wndPtr->winproc,
944 LOWORD (smsg->wParam),
947 queue->GetMessageExtraInfoVal = extraInfo; /* Restore extra info */
948 WIN_ReleaseWndPtr(wndPtr);
949 TRACE_(sendmsg)("result = %08x\n", (unsigned)result );
951 else WARN_(sendmsg)("\trcm: bad hWnd\n");
954 /* set SMSG_SENDING_REPLY flag to tell ReplyMessage16, it's not
956 smsg->flags |= SMSG_SENDING_REPLY;
957 ReplyMessage( result );
959 TRACE_(sendmsg)("done! \n" );
964 /***********************************************************************
967 * Add a message to the queue. Return FALSE if queue is full.
969 BOOL QUEUE_AddMsg( HQUEUE16 hQueue, MSG *msg, DWORD extraInfo )
971 MESSAGEQUEUE *msgQueue;
975 if (!(msgQueue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return FALSE;
977 /* allocate new message in global heap for now */
978 if (!(qmsg = (QMSG *) HeapAlloc( SystemHeap, 0, sizeof(QMSG) ) ))
980 QUEUE_Unlock( msgQueue );
984 EnterCriticalSection( &msgQueue->cSection );
988 qmsg->extraInfo = extraInfo;
990 /* insert the message in the link list */
992 qmsg->prevMsg = msgQueue->lastMsg;
994 if (msgQueue->lastMsg)
995 msgQueue->lastMsg->nextMsg = qmsg;
997 /* update first and last anchor in message queue */
998 msgQueue->lastMsg = qmsg;
999 if (!msgQueue->firstMsg)
1000 msgQueue->firstMsg = qmsg;
1002 msgQueue->msgCount++;
1004 LeaveCriticalSection( &msgQueue->cSection );
1006 QUEUE_SetWakeBit( msgQueue, QS_POSTMESSAGE );
1007 QUEUE_Unlock( msgQueue );
1014 /***********************************************************************
1017 * Find a message matching the given parameters. Return -1 if none available.
1019 QMSG* QUEUE_FindMsg( MESSAGEQUEUE * msgQueue, HWND hwnd, int first, int last )
1023 EnterCriticalSection( &msgQueue->cSection );
1025 if (!msgQueue->msgCount)
1027 else if (!hwnd && !first && !last)
1028 qmsg = msgQueue->firstMsg;
1031 /* look in linked list for message matching first and last criteria */
1032 for (qmsg = msgQueue->firstMsg; qmsg; qmsg = qmsg->nextMsg)
1034 MSG *msg = &(qmsg->msg);
1036 if (!hwnd || (msg->hwnd == hwnd))
1038 if (!first && !last)
1039 break; /* found it */
1041 if ((msg->message >= first) && (!last || (msg->message <= last)))
1042 break; /* found it */
1047 LeaveCriticalSection( &msgQueue->cSection );
1054 /***********************************************************************
1057 * Remove a message from the queue (pos must be a valid position).
1059 void QUEUE_RemoveMsg( MESSAGEQUEUE * msgQueue, QMSG *qmsg )
1061 EnterCriticalSection( &msgQueue->cSection );
1063 /* set the linked list */
1065 qmsg->prevMsg->nextMsg = qmsg->nextMsg;
1068 qmsg->nextMsg->prevMsg = qmsg->prevMsg;
1070 if (msgQueue->firstMsg == qmsg)
1071 msgQueue->firstMsg = qmsg->nextMsg;
1073 if (msgQueue->lastMsg == qmsg)
1074 msgQueue->lastMsg = qmsg->prevMsg;
1076 /* deallocate the memory for the message */
1077 HeapFree( SystemHeap, 0, qmsg );
1079 msgQueue->msgCount--;
1080 if (!msgQueue->msgCount) msgQueue->wakeBits &= ~QS_POSTMESSAGE;
1082 LeaveCriticalSection( &msgQueue->cSection );
1086 /***********************************************************************
1089 * Wake a queue upon reception of a hardware event.
1091 static void QUEUE_WakeSomeone( UINT message )
1096 HQUEUE16 hQueue = 0;
1097 MESSAGEQUEUE *queue = NULL;
1100 hQueue = hCursorQueue;
1102 if( (message >= WM_KEYFIRST) && (message <= WM_KEYLAST) )
1106 hQueue = hActiveQueue;
1110 wakeBit = (message == WM_MOUSEMOVE) ? QS_MOUSEMOVE : QS_MOUSEBUTTON;
1111 if( (hwnd = GetCapture()) )
1112 if( (wndPtr = WIN_FindWndPtr( hwnd )) )
1114 hQueue = wndPtr->hmemTaskQ;
1115 WIN_ReleaseWndPtr(wndPtr);
1119 if( (hwnd = GetSysModalWindow16()) )
1121 if( (wndPtr = WIN_FindWndPtr( hwnd )) )
1123 hQueue = wndPtr->hmemTaskQ;
1124 WIN_ReleaseWndPtr(wndPtr);
1129 queue = QUEUE_Lock( hQueue );
1133 queue = QUEUE_Lock( hFirstQueue );
1136 if (queue->wakeMask & wakeBit) break;
1138 QUEUE_Unlock(queue);
1139 queue = QUEUE_Lock( queue->next );
1143 WARN_(msg)("couldn't find queue\n");
1148 QUEUE_SetWakeBit( queue, wakeBit );
1150 QUEUE_Unlock( queue );
1154 /***********************************************************************
1157 * Add an event to the system message queue.
1158 * Note: the position is relative to the desktop window.
1160 void hardware_event( WORD message, WORD wParam, LONG lParam,
1161 int xPos, int yPos, DWORD time, DWORD extraInfo )
1167 if (!sysMsgQueue) return;
1169 EnterCriticalSection( &sysMsgQueue->cSection );
1171 /* Merge with previous event if possible */
1172 qmsg = sysMsgQueue->lastMsg;
1174 if ((message == WM_MOUSEMOVE) && sysMsgQueue->lastMsg)
1176 msg = &(sysMsgQueue->lastMsg->msg);
1178 if ((msg->message == message) && (msg->wParam == wParam))
1181 qmsg = sysMsgQueue->lastMsg;
1188 /* Should I limit the number of message in
1189 the system message queue??? */
1191 /* Don't merge allocate a new msg in the global heap */
1193 if (!(qmsg = (QMSG *) HeapAlloc( SystemHeap, 0, sizeof(QMSG) ) ))
1195 LeaveCriticalSection( &sysMsgQueue->cSection );
1199 /* put message at the end of the linked list */
1201 qmsg->prevMsg = sysMsgQueue->lastMsg;
1203 if (sysMsgQueue->lastMsg)
1204 sysMsgQueue->lastMsg->nextMsg = qmsg;
1206 /* set last and first anchor index in system message queue */
1207 sysMsgQueue->lastMsg = qmsg;
1208 if (!sysMsgQueue->firstMsg)
1209 sysMsgQueue->firstMsg = qmsg;
1211 sysMsgQueue->msgCount++;
1217 msg->message = message;
1218 msg->wParam = wParam;
1219 msg->lParam = lParam;
1223 qmsg->extraInfo = extraInfo;
1225 LeaveCriticalSection( &sysMsgQueue->cSection );
1227 QUEUE_WakeSomeone( message );
1231 /***********************************************************************
1232 * QUEUE_GetQueueTask
1234 HTASK16 QUEUE_GetQueueTask( HQUEUE16 hQueue )
1238 MESSAGEQUEUE *queue = QUEUE_Lock( hQueue );
1242 hTask = queue->thdb->process->task;
1243 QUEUE_Unlock( queue );
1251 /***********************************************************************
1252 * QUEUE_IncPaintCount
1254 void QUEUE_IncPaintCount( HQUEUE16 hQueue )
1256 MESSAGEQUEUE *queue;
1258 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1259 queue->wPaintCount++;
1260 QUEUE_SetWakeBit( queue, QS_PAINT );
1261 QUEUE_Unlock( queue );
1265 /***********************************************************************
1266 * QUEUE_DecPaintCount
1268 void QUEUE_DecPaintCount( HQUEUE16 hQueue )
1270 MESSAGEQUEUE *queue;
1272 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1273 queue->wPaintCount--;
1274 if (!queue->wPaintCount) queue->wakeBits &= ~QS_PAINT;
1275 QUEUE_Unlock( queue );
1279 /***********************************************************************
1280 * QUEUE_IncTimerCount
1282 void QUEUE_IncTimerCount( HQUEUE16 hQueue )
1284 MESSAGEQUEUE *queue;
1286 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1287 queue->wTimerCount++;
1288 QUEUE_SetWakeBit( queue, QS_TIMER );
1289 QUEUE_Unlock( queue );
1293 /***********************************************************************
1294 * QUEUE_DecTimerCount
1296 void QUEUE_DecTimerCount( HQUEUE16 hQueue )
1298 MESSAGEQUEUE *queue;
1300 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1301 queue->wTimerCount--;
1302 if (!queue->wTimerCount) queue->wakeBits &= ~QS_TIMER;
1303 QUEUE_Unlock( queue );
1307 /***********************************************************************
1308 * PostQuitMessage16 (USER.6)
1310 void WINAPI PostQuitMessage16( INT16 exitCode )
1312 PostQuitMessage( exitCode );
1316 /***********************************************************************
1317 * PostQuitMessage32 (USER32.421)
1319 * PostQuitMessage() posts a message to the system requesting an
1320 * application to terminate execution. As a result of this function,
1321 * the WM_QUIT message is posted to the application, and
1322 * PostQuitMessage() returns immediately. The exitCode parameter
1323 * specifies an application-defined exit code, which appears in the
1324 * _wParam_ parameter of the WM_QUIT message posted to the application.
1330 void WINAPI PostQuitMessage( INT exitCode )
1332 MESSAGEQUEUE *queue;
1334 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return;
1335 queue->wPostQMsg = TRUE;
1336 queue->wExitCode = (WORD)exitCode;
1337 QUEUE_Unlock( queue );
1341 /***********************************************************************
1342 * GetWindowTask16 (USER.224)
1344 HTASK16 WINAPI GetWindowTask16( HWND16 hwnd )
1347 WND *wndPtr = WIN_FindWndPtr( hwnd );
1349 if (!wndPtr) return 0;
1350 retvalue = QUEUE_GetQueueTask( wndPtr->hmemTaskQ );
1351 WIN_ReleaseWndPtr(wndPtr);
1355 /***********************************************************************
1356 * GetWindowThreadProcessId (USER32.313)
1358 DWORD WINAPI GetWindowThreadProcessId( HWND hwnd, LPDWORD process )
1363 WND *wndPtr = WIN_FindWndPtr( hwnd );
1365 if (!wndPtr) return 0;
1366 htask=QUEUE_GetQueueTask( wndPtr->hmemTaskQ );
1367 WIN_ReleaseWndPtr(wndPtr);
1368 tdb = (TDB*)GlobalLock16(htask);
1369 if (!tdb || !tdb->thdb) return 0;
1370 if (process) *process = (DWORD)tdb->thdb->process->server_pid;
1371 return (DWORD)tdb->thdb->server_tid;
1375 /***********************************************************************
1376 * SetMessageQueue16 (USER.266)
1378 BOOL16 WINAPI SetMessageQueue16( INT16 size )
1380 return SetMessageQueue( size );
1384 /***********************************************************************
1385 * SetMessageQueue32 (USER32.494)
1387 BOOL WINAPI SetMessageQueue( INT size )
1389 /* now obsolete the message queue will be expanded dynamically
1392 /* access the queue to create it if it's not existing */
1398 /***********************************************************************
1399 * InitThreadInput (USER.409)
1401 HQUEUE16 WINAPI InitThreadInput16( WORD unknown, WORD flags )
1404 MESSAGEQUEUE *queuePtr;
1406 THDB *thdb = THREAD_Current();
1411 hQueue = thdb->teb.queue;
1415 /* Create thread message queue */
1416 if( !(hQueue = QUEUE_CreateMsgQueue( TRUE )))
1418 WARN_(msg)("failed!\n");
1422 /* Link new queue into list */
1423 queuePtr = (MESSAGEQUEUE *)QUEUE_Lock( hQueue );
1424 queuePtr->thdb = THREAD_Current();
1426 HeapLock( SystemHeap ); /* FIXME: a bit overkill */
1427 SetThreadQueue16( 0, hQueue );
1428 thdb->teb.queue = hQueue;
1430 queuePtr->next = hFirstQueue;
1431 hFirstQueue = hQueue;
1432 HeapUnlock( SystemHeap );
1434 QUEUE_Unlock( queuePtr );
1440 /***********************************************************************
1441 * GetQueueStatus16 (USER.334)
1443 DWORD WINAPI GetQueueStatus16( UINT16 flags )
1445 MESSAGEQUEUE *queue;
1448 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1449 ret = MAKELONG( queue->changeBits, queue->wakeBits );
1450 queue->changeBits = 0;
1451 QUEUE_Unlock( queue );
1453 return ret & MAKELONG( flags, flags );
1456 /***********************************************************************
1457 * GetQueueStatus32 (USER32.283)
1459 DWORD WINAPI GetQueueStatus( UINT flags )
1461 MESSAGEQUEUE *queue;
1464 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1465 ret = MAKELONG( queue->changeBits, queue->wakeBits );
1466 queue->changeBits = 0;
1467 QUEUE_Unlock( queue );
1469 return ret & MAKELONG( flags, flags );
1473 /***********************************************************************
1474 * GetInputState16 (USER.335)
1476 BOOL16 WINAPI GetInputState16(void)
1478 return GetInputState();
1481 /***********************************************************************
1482 * WaitForInputIdle (USER32.577)
1484 DWORD WINAPI WaitForInputIdle (HANDLE hProcess, DWORD dwTimeOut)
1486 FIXME_(msg)("(hProcess=%d, dwTimeOut=%ld): stub\n", hProcess, dwTimeOut);
1488 return WAIT_TIMEOUT;
1492 /***********************************************************************
1493 * GetInputState32 (USER32.244)
1495 BOOL WINAPI GetInputState(void)
1497 MESSAGEQUEUE *queue;
1500 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() )))
1502 ret = queue->wakeBits & (QS_KEY | QS_MOUSEBUTTON);
1503 QUEUE_Unlock( queue );
1508 /***********************************************************************
1509 * UserYield (USER.332)
1511 void WINAPI UserYield16(void)
1513 MESSAGEQUEUE *queue;
1515 /* Handle sent messages */
1516 queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() );
1518 while (queue && (queue->wakeBits & QS_SENDMESSAGE))
1519 QUEUE_ReceiveMessage( queue );
1521 QUEUE_Unlock( queue );
1524 if ( THREAD_IsWin16( THREAD_Current() ) )
1530 ReleaseThunkLock(&count);
1531 RestoreThunkLock(count);
1534 /* Handle sent messages again */
1535 queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() );
1537 while (queue && (queue->wakeBits & QS_SENDMESSAGE))
1538 QUEUE_ReceiveMessage( queue );
1540 QUEUE_Unlock( queue );
1543 /***********************************************************************
1544 * GetMessagePos (USER.119) (USER32.272)
1546 * The GetMessagePos() function returns a long value representing a
1547 * cursor position, in screen coordinates, when the last message
1548 * retrieved by the GetMessage() function occurs. The x-coordinate is
1549 * in the low-order word of the return value, the y-coordinate is in
1550 * the high-order word. The application can use the MAKEPOINT()
1551 * macro to obtain a POINT structure from the return value.
1553 * For the current cursor position, use GetCursorPos().
1557 * Cursor position of last message on success, zero on failure.
1564 DWORD WINAPI GetMessagePos(void)
1566 MESSAGEQUEUE *queue;
1569 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1570 ret = queue->GetMessagePosVal;
1571 QUEUE_Unlock( queue );
1577 /***********************************************************************
1578 * GetMessageTime (USER.120) (USER32.273)
1580 * GetMessageTime() returns the message time for the last message
1581 * retrieved by the function. The time is measured in milliseconds with
1582 * the same offset as GetTickCount().
1584 * Since the tick count wraps, this is only useful for moderately short
1585 * relative time comparisons.
1589 * Time of last message on success, zero on failure.
1596 LONG WINAPI GetMessageTime(void)
1598 MESSAGEQUEUE *queue;
1601 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1602 ret = queue->GetMessageTimeVal;
1603 QUEUE_Unlock( queue );
1609 /***********************************************************************
1610 * GetMessageExtraInfo (USER.288) (USER32.271)
1612 LONG WINAPI GetMessageExtraInfo(void)
1614 MESSAGEQUEUE *queue;
1617 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1618 ret = queue->GetMessageExtraInfoVal;
1619 QUEUE_Unlock( queue );