1 /* * Message queues related functions
3 * Copyright 1993, 1994 Alexandre Julliard
12 #include "wine/winbase16.h"
13 #include "wine/winuser16.h"
19 #include "debugtools.h"
23 DECLARE_DEBUG_CHANNEL(msg);
24 DECLARE_DEBUG_CHANNEL(sendmsg);
26 #define MAX_QUEUE_SIZE 120 /* Max. size of a message queue */
28 static HQUEUE16 hFirstQueue = 0;
29 static HQUEUE16 hExitingQueue = 0;
30 static HQUEUE16 hmemSysMsgQueue = 0;
31 static MESSAGEQUEUE *sysMsgQueue = NULL;
32 static PERQUEUEDATA *pQDataWin16 = NULL; /* Global perQData for Win16 tasks */
34 static MESSAGEQUEUE *pMouseQueue = NULL; /* Queue for last mouse message */
35 static MESSAGEQUEUE *pKbdQueue = NULL; /* Queue for last kbd message */
37 HQUEUE16 hCursorQueue = 0;
38 HQUEUE16 hActiveQueue = 0;
41 /***********************************************************************
42 * PERQDATA_CreateInstance
44 * Creates an instance of a reference counted PERQUEUEDATA element
45 * for the message queue. perQData is stored globally for 16 bit tasks.
47 * Note: We don't implement perQdata exactly the same way Windows does.
48 * Each perQData element is reference counted since it may be potentially
49 * shared by multiple message Queues (via AttachThreadInput).
50 * We only store the current values for Active, Capture and focus windows
53 PERQUEUEDATA * PERQDATA_CreateInstance( )
61 /* Share a single instance of perQData for all 16 bit tasks */
62 if ( ( bIsWin16 = THREAD_IsWin16( NtCurrentTeb() ) ) )
64 /* If previously allocated, just bump up ref count */
67 PERQDATA_Addref( pQDataWin16 );
72 /* Allocate PERQUEUEDATA from the system heap */
73 if (!( pQData = (PERQUEUEDATA *) HeapAlloc( GetProcessHeap(), 0,
74 sizeof(PERQUEUEDATA) ) ))
78 pQData->hWndCapture = pQData->hWndFocus = pQData->hWndActive = 0;
79 pQData->ulRefCount = 1;
80 pQData->nCaptureHT = HTCLIENT;
82 /* Note: We have an independent critical section for the per queue data
83 * since this may be shared by different threads. see AttachThreadInput()
85 InitializeCriticalSection( &pQData->cSection );
86 /* FIXME: not all per queue data critical sections should be global */
87 MakeCriticalSectionGlobal( &pQData->cSection );
89 /* Save perQData globally for 16 bit tasks */
97 /***********************************************************************
100 * Increment reference count for the PERQUEUEDATA instance
101 * Returns reference count for debugging purposes
103 ULONG PERQDATA_Addref( PERQUEUEDATA *pQData )
105 assert(pQData != 0 );
106 TRACE_(msg)("(): current refcount %lu ...\n", pQData->ulRefCount);
108 EnterCriticalSection( &pQData->cSection );
109 ++pQData->ulRefCount;
110 LeaveCriticalSection( &pQData->cSection );
112 return pQData->ulRefCount;
116 /***********************************************************************
119 * Release a reference to a PERQUEUEDATA instance.
120 * Destroy the instance if no more references exist
121 * Returns reference count for debugging purposes
123 ULONG PERQDATA_Release( PERQUEUEDATA *pQData )
125 assert(pQData != 0 );
126 TRACE_(msg)("(): current refcount %lu ...\n",
127 (LONG)pQData->ulRefCount );
129 EnterCriticalSection( &pQData->cSection );
130 if ( --pQData->ulRefCount == 0 )
132 LeaveCriticalSection( &pQData->cSection );
133 DeleteCriticalSection( &pQData->cSection );
135 TRACE_(msg)("(): deleting PERQUEUEDATA instance ...\n" );
137 /* Deleting our global 16 bit perQData? */
138 if ( pQData == pQDataWin16 )
141 /* Free the PERQUEUEDATA instance */
142 HeapFree( GetProcessHeap(), 0, pQData );
146 LeaveCriticalSection( &pQData->cSection );
148 return pQData->ulRefCount;
152 /***********************************************************************
153 * PERQDATA_GetFocusWnd
155 * Get the focus hwnd member in a threadsafe manner
157 HWND PERQDATA_GetFocusWnd( PERQUEUEDATA *pQData )
160 assert(pQData != 0 );
162 EnterCriticalSection( &pQData->cSection );
163 hWndFocus = pQData->hWndFocus;
164 LeaveCriticalSection( &pQData->cSection );
170 /***********************************************************************
171 * PERQDATA_SetFocusWnd
173 * Set the focus hwnd member in a threadsafe manner
175 HWND PERQDATA_SetFocusWnd( PERQUEUEDATA *pQData, HWND hWndFocus )
178 assert(pQData != 0 );
180 EnterCriticalSection( &pQData->cSection );
181 hWndFocusPrv = pQData->hWndFocus;
182 pQData->hWndFocus = hWndFocus;
183 LeaveCriticalSection( &pQData->cSection );
189 /***********************************************************************
190 * PERQDATA_GetActiveWnd
192 * Get the active hwnd member in a threadsafe manner
194 HWND PERQDATA_GetActiveWnd( PERQUEUEDATA *pQData )
197 assert(pQData != 0 );
199 EnterCriticalSection( &pQData->cSection );
200 hWndActive = pQData->hWndActive;
201 LeaveCriticalSection( &pQData->cSection );
207 /***********************************************************************
208 * PERQDATA_SetActiveWnd
210 * Set the active focus hwnd member in a threadsafe manner
212 HWND PERQDATA_SetActiveWnd( PERQUEUEDATA *pQData, HWND hWndActive )
215 assert(pQData != 0 );
217 EnterCriticalSection( &pQData->cSection );
218 hWndActivePrv = pQData->hWndActive;
219 pQData->hWndActive = hWndActive;
220 LeaveCriticalSection( &pQData->cSection );
222 return hWndActivePrv;
226 /***********************************************************************
227 * PERQDATA_GetCaptureWnd
229 * Get the capture hwnd member in a threadsafe manner
231 HWND PERQDATA_GetCaptureWnd( PERQUEUEDATA *pQData )
234 assert(pQData != 0 );
236 EnterCriticalSection( &pQData->cSection );
237 hWndCapture = pQData->hWndCapture;
238 LeaveCriticalSection( &pQData->cSection );
244 /***********************************************************************
245 * PERQDATA_SetCaptureWnd
247 * Set the capture hwnd member in a threadsafe manner
249 HWND PERQDATA_SetCaptureWnd( PERQUEUEDATA *pQData, HWND hWndCapture )
252 assert(pQData != 0 );
254 EnterCriticalSection( &pQData->cSection );
255 hWndCapturePrv = pQData->hWndCapture;
256 pQData->hWndCapture = hWndCapture;
257 LeaveCriticalSection( &pQData->cSection );
259 return hWndCapturePrv;
263 /***********************************************************************
264 * PERQDATA_GetCaptureInfo
266 * Get the capture info member in a threadsafe manner
268 INT16 PERQDATA_GetCaptureInfo( PERQUEUEDATA *pQData )
271 assert(pQData != 0 );
273 EnterCriticalSection( &pQData->cSection );
274 nCaptureHT = pQData->nCaptureHT;
275 LeaveCriticalSection( &pQData->cSection );
281 /***********************************************************************
282 * PERQDATA_SetCaptureInfo
284 * Set the capture info member in a threadsafe manner
286 INT16 PERQDATA_SetCaptureInfo( PERQUEUEDATA *pQData, INT16 nCaptureHT )
289 assert(pQData != 0 );
291 EnterCriticalSection( &pQData->cSection );
292 nCaptureHTPrv = pQData->nCaptureHT;
293 pQData->nCaptureHT = nCaptureHT;
294 LeaveCriticalSection( &pQData->cSection );
296 return nCaptureHTPrv;
300 /***********************************************************************
303 * Function for getting a 32 bit pointer on queue structure. For thread
304 * safeness programmers should use this function instead of GlobalLock to
305 * retrieve a pointer on the structure. QUEUE_Unlock should also be called
306 * when access to the queue structure is not required anymore.
308 MESSAGEQUEUE *QUEUE_Lock( HQUEUE16 hQueue )
312 HeapLock( GetProcessHeap() ); /* FIXME: a bit overkill */
313 queue = GlobalLock16( hQueue );
314 if ( !queue || (queue->magic != QUEUE_MAGIC) )
316 HeapUnlock( GetProcessHeap() );
321 HeapUnlock( GetProcessHeap() );
326 /***********************************************************************
329 * Use with QUEUE_Lock to get a thread safe access to message queue
332 void QUEUE_Unlock( MESSAGEQUEUE *queue )
336 HeapLock( GetProcessHeap() ); /* FIXME: a bit overkill */
338 if ( --queue->lockCount == 0 )
340 DeleteCriticalSection ( &queue->cSection );
341 if (queue->server_queue)
342 CloseHandle( queue->server_queue );
343 GlobalFree16( queue->self );
346 HeapUnlock( GetProcessHeap() );
351 /***********************************************************************
354 void QUEUE_DumpQueue( HQUEUE16 hQueue )
358 if (!(pq = QUEUE_Lock( hQueue )) )
360 WARN_(msg)("%04x is not a queue handle\n", hQueue );
364 EnterCriticalSection( &pq->cSection );
366 DPRINTF( "next: %12.4x Intertask SendMessage:\n"
367 "thread: %10p ----------------------\n"
368 "firstMsg: %8p smWaiting: %10p\n"
369 "lastMsg: %8p smPending: %10p\n"
370 "msgCount: %8.4x smProcessing: %10p\n"
377 pq->next, pq->teb, pq->firstMsg, pq->smWaiting, pq->lastMsg,
378 pq->smPending, pq->msgCount, pq->smProcessing,
379 (unsigned)pq->lockCount, pq->wPaintCount, pq->wTimerCount,
380 pq->wakeBits, pq->wakeMask, pq->hCurHook);
382 LeaveCriticalSection( &pq->cSection );
388 /***********************************************************************
391 void QUEUE_WalkQueues(void)
394 HQUEUE16 hQueue = hFirstQueue;
396 DPRINTF( "Queue Msgs Thread Task Module\n" );
399 MESSAGEQUEUE *queue = QUEUE_Lock( hQueue );
402 WARN_(msg)("Bad queue handle %04x\n", hQueue );
405 if (!GetModuleName16( queue->teb->htask16, module, sizeof(module )))
406 strcpy( module, "???" );
407 DPRINTF( "%04x %4d %p %04x %s\n", hQueue,queue->msgCount,
408 queue->teb, queue->teb->htask16, module );
409 hQueue = queue->next;
410 QUEUE_Unlock( queue );
416 /***********************************************************************
417 * QUEUE_IsExitingQueue
419 BOOL QUEUE_IsExitingQueue( HQUEUE16 hQueue )
421 return (hExitingQueue && (hQueue == hExitingQueue));
425 /***********************************************************************
426 * QUEUE_SetExitingQueue
428 void QUEUE_SetExitingQueue( HQUEUE16 hQueue )
430 hExitingQueue = hQueue;
434 /***********************************************************************
435 * QUEUE_CreateMsgQueue
437 * Creates a message queue. Doesn't link it into queue list!
439 static HQUEUE16 QUEUE_CreateMsgQueue( BOOL16 bCreatePerQData )
443 MESSAGEQUEUE * msgQueue;
445 TRACE_(msg)("(): Creating message queue...\n");
447 if (!(hQueue = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT,
448 sizeof(MESSAGEQUEUE) )))
451 msgQueue = (MESSAGEQUEUE *) GlobalLock16( hQueue );
455 SERVER_START_REQ( get_msg_queue )
458 handle = req->handle;
463 ERR_(msg)("Cannot get thread queue");
464 GlobalFree16( hQueue );
467 msgQueue->server_queue = handle;
468 msgQueue->server_queue = ConvertToGlobalHandle( msgQueue->server_queue );
470 msgQueue->self = hQueue;
471 msgQueue->wakeBits = msgQueue->changeBits = 0;
473 InitializeCriticalSection( &msgQueue->cSection );
474 MakeCriticalSectionGlobal( &msgQueue->cSection );
476 msgQueue->lockCount = 1;
477 msgQueue->magic = QUEUE_MAGIC;
479 /* Create and initialize our per queue data */
480 msgQueue->pQData = bCreatePerQData ? PERQDATA_CreateInstance() : NULL;
486 /***********************************************************************
489 * Try to reply to all pending sent messages on exit.
491 static void QUEUE_FlushMessages( MESSAGEQUEUE *queue )
494 MESSAGEQUEUE *senderQ = 0;
498 EnterCriticalSection( &queue->cSection );
500 /* empty the list of pending SendMessage waiting to be received */
501 while (queue->smPending)
503 smsg = QUEUE_RemoveSMSG( queue, SM_PENDING_LIST, 0);
505 senderQ = QUEUE_Lock( smsg->hSrcQueue );
509 /* return 0, to unblock other thread */
511 smsg->flags |= SMSG_HAVE_RESULT;
512 QUEUE_SetWakeBit( senderQ, QS_SMRESULT);
514 QUEUE_Unlock( senderQ );
517 QUEUE_ClearWakeBit( queue, QS_SENDMESSAGE );
519 LeaveCriticalSection( &queue->cSection );
524 /***********************************************************************
525 * QUEUE_DeleteMsgQueue
527 * Unlinks and deletes a message queue.
529 * Note: We need to mask asynchronous events to make sure PostMessage works
530 * even in the signal handler.
532 BOOL QUEUE_DeleteMsgQueue( HQUEUE16 hQueue )
534 MESSAGEQUEUE * msgQueue = QUEUE_Lock(hQueue);
537 TRACE_(msg)("(): Deleting message queue %04x\n", hQueue);
539 if (!hQueue || !msgQueue)
541 ERR_(msg)("invalid argument.\n");
547 if( hCursorQueue == hQueue ) hCursorQueue = 0;
548 if( hActiveQueue == hQueue ) hActiveQueue = 0;
550 /* flush sent messages */
551 QUEUE_FlushMessages( msgQueue );
553 HeapLock( GetProcessHeap() ); /* FIXME: a bit overkill */
555 /* Release per queue data if present */
556 if ( msgQueue->pQData )
558 PERQDATA_Release( msgQueue->pQData );
559 msgQueue->pQData = 0;
562 /* remove the message queue from the global link list */
563 pPrev = &hFirstQueue;
564 while (*pPrev && (*pPrev != hQueue))
566 MESSAGEQUEUE *msgQ = (MESSAGEQUEUE*)GlobalLock16(*pPrev);
569 if ( !msgQ || (msgQ->magic != QUEUE_MAGIC) )
571 /* HQUEUE link list is corrupted, try to exit gracefully */
572 ERR_(msg)("HQUEUE link list corrupted!\n");
578 if (pPrev && *pPrev) *pPrev = msgQueue->next;
581 HeapUnlock( GetProcessHeap() );
583 /* free up resource used by MESSAGEQUEUE structure */
584 msgQueue->lockCount--;
585 QUEUE_Unlock( msgQueue );
591 /***********************************************************************
592 * QUEUE_CreateSysMsgQueue
594 * Create the system message queue, and set the double-click speed.
595 * Must be called only once.
597 BOOL QUEUE_CreateSysMsgQueue( int size )
599 /* Note: We dont need perQ data for the system message queue */
600 if (!(hmemSysMsgQueue = QUEUE_CreateMsgQueue( FALSE )))
602 FarSetOwner16( hmemSysMsgQueue, 0 );
603 sysMsgQueue = (MESSAGEQUEUE *) GlobalLock16( hmemSysMsgQueue );
608 /***********************************************************************
611 MESSAGEQUEUE *QUEUE_GetSysQueue(void)
617 /***********************************************************************
620 * See "Windows Internals", p.449
622 static BOOL QUEUE_TrySetWakeBit( MESSAGEQUEUE *queue, WORD bit, BOOL always )
626 EnterCriticalSection( &queue->cSection );
628 TRACE_(msg)("queue = %04x (wm=%04x), bit = %04x, always = %d\n",
629 queue->self, queue->wakeMask, bit, always );
631 if ((queue->wakeMask & bit) || always)
633 if (bit & QS_MOUSE) pMouseQueue = queue;
634 if (bit & QS_KEY) pKbdQueue = queue;
635 queue->changeBits |= bit;
636 queue->wakeBits |= bit;
638 if (queue->wakeMask & bit)
644 LeaveCriticalSection( &queue->cSection );
648 /* Wake up thread waiting for message */
649 SERVER_START_REQ( wake_queue )
651 req->handle = queue->server_queue;
660 void QUEUE_SetWakeBit( MESSAGEQUEUE *queue, WORD bit )
662 QUEUE_TrySetWakeBit( queue, bit, TRUE );
666 /***********************************************************************
669 void QUEUE_ClearWakeBit( MESSAGEQUEUE *queue, WORD bit )
671 EnterCriticalSection( &queue->cSection );
672 queue->changeBits &= ~bit;
673 queue->wakeBits &= ~bit;
674 LeaveCriticalSection( &queue->cSection );
677 /***********************************************************************
680 WORD QUEUE_TestWakeBit( MESSAGEQUEUE *queue, WORD bit )
683 EnterCriticalSection( &queue->cSection );
684 ret = queue->wakeBits & bit;
685 LeaveCriticalSection( &queue->cSection );
690 /***********************************************************************
693 * See "Windows Internals", p.447
696 * 0 if exit with timeout
699 int QUEUE_WaitBits( WORD bits, DWORD timeout )
704 TRACE_(msg)("q %04x waiting for %04x\n", GetFastQueue16(), bits);
706 hQueue = GetFastQueue16();
707 if (!(queue = QUEUE_Lock( hQueue ))) return 0;
713 EnterCriticalSection( &queue->cSection );
715 if (queue->changeBits & bits)
717 /* One of the bits is set; we can return */
720 LeaveCriticalSection( &queue->cSection );
721 QUEUE_Unlock( queue );
724 if (queue->wakeBits & QS_SENDMESSAGE)
726 /* Process the sent message immediately */
729 LeaveCriticalSection( &queue->cSection );
730 QUEUE_ReceiveMessage( queue );
731 continue; /* nested sm crux */
734 queue->wakeMask = bits | QS_SENDMESSAGE;
735 TRACE_(msg)("%04x: wakeMask is %04x, waiting\n", queue->self, queue->wakeMask);
736 LeaveCriticalSection( &queue->cSection );
738 ReleaseThunkLock( &dwlc );
739 if (dwlc) TRACE_(msg)("had win16 lock\n");
741 if (USER_Driver.pMsgWaitForMultipleObjects)
742 USER_Driver.pMsgWaitForMultipleObjects( 1, &queue->server_queue, FALSE, timeout );
744 WaitForSingleObject( queue->server_queue, timeout );
745 if (dwlc) RestoreThunkLock( dwlc );
750 /***********************************************************************
753 * This routine is called when a SMSG need to be added to one of the three
754 * SM list. (SM_PROCESSING_LIST, SM_PENDING_LIST, SM_WAITING_LIST)
756 BOOL QUEUE_AddSMSG( MESSAGEQUEUE *queue, int list, SMSG *smsg )
758 TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
759 smsg, SPY_GetMsgName(smsg->msg));
763 case SM_PROCESSING_LIST:
764 /* don't need to be thread safe, only accessed by the
765 thread associated with the sender queue */
766 smsg->nextProcessing = queue->smProcessing;
767 queue->smProcessing = smsg;
770 case SM_WAITING_LIST:
771 /* don't need to be thread safe, only accessed by the
772 thread associated with the receiver queue */
773 smsg->nextWaiting = queue->smWaiting;
774 queue->smWaiting = smsg;
777 case SM_PENDING_LIST:
779 /* make it thread safe, could be accessed by the sender and
783 EnterCriticalSection( &queue->cSection );
784 smsg->nextPending = NULL;
785 prev = &queue->smPending;
787 prev = &(*prev)->nextPending;
789 LeaveCriticalSection( &queue->cSection );
791 QUEUE_SetWakeBit( queue, QS_SENDMESSAGE );
796 ERR_(sendmsg)("Invalid list: %d", list);
804 /***********************************************************************
807 * This routine is called when a SMSG needs to be removed from one of the three
808 * SM lists (SM_PROCESSING_LIST, SM_PENDING_LIST, SM_WAITING_LIST).
809 * If smsg == 0, remove the first smsg from the specified list
811 SMSG *QUEUE_RemoveSMSG( MESSAGEQUEUE *queue, int list, SMSG *smsg )
816 case SM_PROCESSING_LIST:
817 /* don't need to be thread safe, only accessed by the
818 thread associated with the sender queue */
820 /* if smsg is equal to null, it means the first in the list */
822 smsg = queue->smProcessing;
824 TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
825 smsg, SPY_GetMsgName(smsg->msg));
826 /* In fact SM_PROCESSING_LIST is a stack, and smsg
827 should be always at the top of the list */
828 if ( (smsg != queue->smProcessing) || !queue->smProcessing )
830 ERR_(sendmsg)("smsg not at the top of Processing list, smsg=0x%p queue=0x%p\n", smsg, queue);
835 queue->smProcessing = smsg->nextProcessing;
836 smsg->nextProcessing = 0;
840 case SM_WAITING_LIST:
841 /* don't need to be thread safe, only accessed by the
842 thread associated with the receiver queue */
844 /* if smsg is equal to null, it means the first in the list */
846 smsg = queue->smWaiting;
848 TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
849 smsg, SPY_GetMsgName(smsg->msg));
850 /* In fact SM_WAITING_LIST is a stack, and smsg
851 should be always at the top of the list */
852 if ( (smsg != queue->smWaiting) || !queue->smWaiting )
854 ERR_(sendmsg)("smsg not at the top of Waiting list, smsg=0x%p queue=0x%p\n", smsg, queue);
859 queue->smWaiting = smsg->nextWaiting;
860 smsg->nextWaiting = 0;
864 case SM_PENDING_LIST:
865 /* make it thread safe, could be accessed by the sender and
867 EnterCriticalSection( &queue->cSection );
870 smsg = queue->smPending;
871 if ( (smsg != queue->smPending) || !queue->smPending )
873 ERR_(sendmsg)("should always remove the top one in Pending list, smsg=0x%p queue=0x%p\n", smsg, queue);
874 LeaveCriticalSection( &queue->cSection );
878 TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
879 smsg, SPY_GetMsgName(smsg->msg));
881 queue->smPending = smsg->nextPending;
882 smsg->nextPending = 0;
884 /* if no more SMSG in Pending list, clear QS_SENDMESSAGE flag */
885 if (!queue->smPending)
886 QUEUE_ClearWakeBit( queue, QS_SENDMESSAGE );
888 LeaveCriticalSection( &queue->cSection );
892 ERR_(sendmsg)("Invalid list: %d\n", list);
900 /***********************************************************************
901 * QUEUE_ReceiveMessage
903 * This routine is called to check whether a sent message is waiting
904 * for the queue. If so, it is received and processed.
906 BOOL QUEUE_ReceiveMessage( MESSAGEQUEUE *queue )
910 MESSAGEQUEUE *senderQ;
912 EnterCriticalSection( &queue->cSection );
913 if ( !((queue->wakeBits & QS_SENDMESSAGE) && queue->smPending) )
915 LeaveCriticalSection( &queue->cSection );
918 LeaveCriticalSection( &queue->cSection );
920 TRACE_(sendmsg)("queue %04x\n", queue->self );
922 /* remove smsg on the top of the pending list and put it in the processing list */
923 smsg = QUEUE_RemoveSMSG(queue, SM_PENDING_LIST, 0);
924 QUEUE_AddSMSG(queue, SM_WAITING_LIST, smsg);
926 TRACE_(sendmsg)("RM: %s [%04x] (%04x -> %04x)\n",
927 SPY_GetMsgName(smsg->msg), smsg->msg, smsg->hSrcQueue, smsg->hDstQueue );
929 if (IsWindow( smsg->hWnd ))
931 WND *wndPtr = WIN_FindWndPtr( smsg->hWnd );
932 DWORD extraInfo = queue->GetMessageExtraInfoVal; /* save ExtraInfo */
934 /* use sender queue extra info value while calling the window proc */
935 senderQ = QUEUE_Lock( smsg->hSrcQueue );
938 queue->GetMessageExtraInfoVal = senderQ->GetMessageExtraInfoVal;
939 QUEUE_Unlock( senderQ );
942 /* call the right version of CallWindowProcXX */
943 if (smsg->flags & SMSG_WIN32)
945 TRACE_(sendmsg)("\trcm: msg is Win32\n" );
946 if (smsg->flags & SMSG_UNICODE)
947 result = CallWindowProcW( wndPtr->winproc,
948 smsg->hWnd, smsg->msg,
949 smsg->wParam, smsg->lParam );
951 result = CallWindowProcA( wndPtr->winproc,
952 smsg->hWnd, smsg->msg,
953 smsg->wParam, smsg->lParam );
955 else /* Win16 message */
956 result = CallWindowProc16( (WNDPROC16)wndPtr->winproc,
959 LOWORD (smsg->wParam),
962 queue->GetMessageExtraInfoVal = extraInfo; /* Restore extra info */
963 WIN_ReleaseWndPtr(wndPtr);
964 TRACE_(sendmsg)("result = %08x\n", (unsigned)result );
966 else WARN_(sendmsg)("\trcm: bad hWnd\n");
969 /* set SMSG_SENDING_REPLY flag to tell ReplyMessage16, it's not
971 smsg->flags |= SMSG_SENDING_REPLY;
972 ReplyMessage( result );
974 TRACE_(sendmsg)("done!\n" );
980 /***********************************************************************
983 * Add a message to the queue. Return FALSE if queue is full.
985 BOOL QUEUE_AddMsg( HQUEUE16 hQueue, int type, MSG *msg, DWORD extraInfo )
987 MESSAGEQUEUE *msgQueue;
991 if (!(msgQueue = QUEUE_Lock( hQueue ))) return FALSE;
993 /* allocate new message in global heap for now */
994 if (!(qmsg = (QMSG *) HeapAlloc( GetProcessHeap(), 0, sizeof(QMSG) ) ))
996 QUEUE_Unlock( msgQueue );
1000 EnterCriticalSection( &msgQueue->cSection );
1005 qmsg->extraInfo = extraInfo;
1007 /* insert the message in the link list */
1009 qmsg->prevMsg = msgQueue->lastMsg;
1011 if (msgQueue->lastMsg)
1012 msgQueue->lastMsg->nextMsg = qmsg;
1014 /* update first and last anchor in message queue */
1015 msgQueue->lastMsg = qmsg;
1016 if (!msgQueue->firstMsg)
1017 msgQueue->firstMsg = qmsg;
1019 msgQueue->msgCount++;
1021 LeaveCriticalSection( &msgQueue->cSection );
1023 QUEUE_SetWakeBit( msgQueue, QS_POSTMESSAGE );
1024 QUEUE_Unlock( msgQueue );
1031 /***********************************************************************
1034 * Find a message matching the given parameters. Return -1 if none available.
1036 QMSG* QUEUE_FindMsg( MESSAGEQUEUE * msgQueue, HWND hwnd, int first, int last )
1040 EnterCriticalSection( &msgQueue->cSection );
1042 if (!msgQueue->msgCount)
1044 else if (!hwnd && !first && !last)
1045 qmsg = msgQueue->firstMsg;
1048 /* look in linked list for message matching first and last criteria */
1049 for (qmsg = msgQueue->firstMsg; qmsg; qmsg = qmsg->nextMsg)
1051 MSG *msg = &(qmsg->msg);
1053 if (!hwnd || (msg->hwnd == hwnd))
1055 if (!first && !last)
1056 break; /* found it */
1058 if ((msg->message >= first) && (!last || (msg->message <= last)))
1059 break; /* found it */
1064 LeaveCriticalSection( &msgQueue->cSection );
1071 /***********************************************************************
1074 * Remove a message from the queue (pos must be a valid position).
1076 void QUEUE_RemoveMsg( MESSAGEQUEUE * msgQueue, QMSG *qmsg )
1078 EnterCriticalSection( &msgQueue->cSection );
1080 /* set the linked list */
1082 qmsg->prevMsg->nextMsg = qmsg->nextMsg;
1085 qmsg->nextMsg->prevMsg = qmsg->prevMsg;
1087 if (msgQueue->firstMsg == qmsg)
1088 msgQueue->firstMsg = qmsg->nextMsg;
1090 if (msgQueue->lastMsg == qmsg)
1091 msgQueue->lastMsg = qmsg->prevMsg;
1093 /* deallocate the memory for the message */
1094 HeapFree( GetProcessHeap(), 0, qmsg );
1096 msgQueue->msgCount--;
1097 if (!msgQueue->msgCount) msgQueue->wakeBits &= ~QS_POSTMESSAGE;
1099 LeaveCriticalSection( &msgQueue->cSection );
1104 /***********************************************************************
1107 * Wake a queue upon reception of a hardware event.
1109 static void QUEUE_WakeSomeone( UINT message )
1114 HQUEUE16 hQueue = 0;
1115 MESSAGEQUEUE *queue = NULL;
1118 hQueue = hCursorQueue;
1120 if( (message >= WM_KEYFIRST) && (message <= WM_KEYLAST) )
1124 hQueue = hActiveQueue;
1128 wakeBit = (message == WM_MOUSEMOVE) ? QS_MOUSEMOVE : QS_MOUSEBUTTON;
1129 if( (hwnd = GetCapture()) )
1130 if( (wndPtr = WIN_FindWndPtr( hwnd )) )
1132 hQueue = wndPtr->hmemTaskQ;
1133 WIN_ReleaseWndPtr(wndPtr);
1137 if( (hwnd = GetSysModalWindow16()) )
1139 if( (wndPtr = WIN_FindWndPtr( hwnd )) )
1141 hQueue = wndPtr->hmemTaskQ;
1142 WIN_ReleaseWndPtr(wndPtr);
1148 queue = QUEUE_Lock( hQueue );
1149 QUEUE_SetWakeBit( queue, wakeBit );
1150 QUEUE_Unlock( queue );
1154 /* Search for someone to wake */
1155 hQueue = hFirstQueue;
1156 while ( (queue = QUEUE_Lock( hQueue )) )
1158 if (QUEUE_TrySetWakeBit( queue, wakeBit, FALSE ))
1160 QUEUE_Unlock( queue );
1164 hQueue = queue->next;
1165 QUEUE_Unlock( queue );
1168 WARN_(msg)("couldn't find queue\n");
1173 /***********************************************************************
1176 * Add an event to the system message queue.
1177 * Note: the position is relative to the desktop window.
1179 void hardware_event( UINT message, WPARAM wParam, LPARAM lParam,
1180 int xPos, int yPos, DWORD time, DWORD extraInfo )
1184 MESSAGEQUEUE *queue;
1187 if (!sysMsgQueue) return;
1189 EnterCriticalSection( &sysMsgQueue->cSection );
1191 /* Merge with previous event if possible */
1192 qmsg = sysMsgQueue->lastMsg;
1194 if ((message == WM_MOUSEMOVE) && sysMsgQueue->lastMsg)
1196 msg = &(sysMsgQueue->lastMsg->msg);
1198 if ((msg->message == message) && (msg->wParam == wParam))
1201 qmsg = sysMsgQueue->lastMsg;
1208 /* Should I limit the number of messages in
1209 the system message queue??? */
1211 /* Don't merge allocate a new msg in the global heap */
1213 if (!(qmsg = (QMSG *) HeapAlloc( GetProcessHeap(), 0, sizeof(QMSG) ) ))
1215 LeaveCriticalSection( &sysMsgQueue->cSection );
1219 /* put message at the end of the linked list */
1221 qmsg->prevMsg = sysMsgQueue->lastMsg;
1223 if (sysMsgQueue->lastMsg)
1224 sysMsgQueue->lastMsg->nextMsg = qmsg;
1226 /* set last and first anchor index in system message queue */
1227 sysMsgQueue->lastMsg = qmsg;
1228 if (!sysMsgQueue->firstMsg)
1229 sysMsgQueue->firstMsg = qmsg;
1231 sysMsgQueue->msgCount++;
1237 msg->message = message;
1238 msg->wParam = wParam;
1239 msg->lParam = lParam;
1243 qmsg->extraInfo = extraInfo;
1244 qmsg->type = QMSG_HARDWARE;
1246 LeaveCriticalSection( &sysMsgQueue->cSection );
1248 if ((queue = QUEUE_Lock( GetFastQueue16() )))
1252 if ((message >= WM_KEYFIRST) && (message <= WM_KEYLAST)) wakeBit = QS_KEY;
1253 else wakeBit = (message == WM_MOUSEMOVE) ? QS_MOUSEMOVE : QS_MOUSEBUTTON;
1255 QUEUE_SetWakeBit( queue, wakeBit );
1256 QUEUE_Unlock( queue );
1261 /***********************************************************************
1262 * QUEUE_GetQueueTask
1264 HTASK16 QUEUE_GetQueueTask( HQUEUE16 hQueue )
1268 MESSAGEQUEUE *queue = QUEUE_Lock( hQueue );
1272 hTask = queue->teb->htask16;
1273 QUEUE_Unlock( queue );
1281 /***********************************************************************
1282 * QUEUE_IncPaintCount
1284 void QUEUE_IncPaintCount( HQUEUE16 hQueue )
1286 MESSAGEQUEUE *queue;
1288 if (!(queue = QUEUE_Lock( hQueue ))) return;
1289 EnterCriticalSection( &queue->cSection );
1290 queue->wPaintCount++;
1291 LeaveCriticalSection( &queue->cSection );
1292 QUEUE_SetWakeBit( queue, QS_PAINT );
1293 QUEUE_Unlock( queue );
1297 /***********************************************************************
1298 * QUEUE_DecPaintCount
1300 void QUEUE_DecPaintCount( HQUEUE16 hQueue )
1302 MESSAGEQUEUE *queue;
1304 if (!(queue = QUEUE_Lock( hQueue ))) return;
1305 EnterCriticalSection( &queue->cSection );
1306 queue->wPaintCount--;
1307 if (!queue->wPaintCount) queue->wakeBits &= ~QS_PAINT;
1308 LeaveCriticalSection( &queue->cSection );
1309 QUEUE_Unlock( queue );
1313 /***********************************************************************
1314 * QUEUE_IncTimerCount
1316 void QUEUE_IncTimerCount( HQUEUE16 hQueue )
1318 MESSAGEQUEUE *queue;
1320 if (!(queue = QUEUE_Lock( hQueue ))) return;
1321 EnterCriticalSection( &queue->cSection );
1322 queue->wTimerCount++;
1323 LeaveCriticalSection( &queue->cSection );
1324 QUEUE_SetWakeBit( queue, QS_TIMER );
1325 QUEUE_Unlock( queue );
1329 /***********************************************************************
1330 * QUEUE_DecTimerCount
1332 void QUEUE_DecTimerCount( HQUEUE16 hQueue )
1334 MESSAGEQUEUE *queue;
1336 if (!(queue = QUEUE_Lock( hQueue ))) return;
1337 EnterCriticalSection( &queue->cSection );
1338 queue->wTimerCount--;
1339 if (!queue->wTimerCount) queue->wakeBits &= ~QS_TIMER;
1340 LeaveCriticalSection( &queue->cSection );
1341 QUEUE_Unlock( queue );
1345 /***********************************************************************
1346 * PostQuitMessage (USER.6)
1348 void WINAPI PostQuitMessage16( INT16 exitCode )
1350 PostQuitMessage( exitCode );
1354 /***********************************************************************
1355 * PostQuitMessage (USER32.@)
1357 * PostQuitMessage() posts a message to the system requesting an
1358 * application to terminate execution. As a result of this function,
1359 * the WM_QUIT message is posted to the application, and
1360 * PostQuitMessage() returns immediately. The exitCode parameter
1361 * specifies an application-defined exit code, which appears in the
1362 * _wParam_ parameter of the WM_QUIT message posted to the application.
1368 void WINAPI PostQuitMessage( INT exitCode )
1370 MESSAGEQUEUE *queue;
1372 if (!(queue = QUEUE_Lock( GetFastQueue16() ))) return;
1373 EnterCriticalSection( &queue->cSection );
1374 queue->wPostQMsg = TRUE;
1375 queue->wExitCode = (WORD)exitCode;
1376 LeaveCriticalSection( &queue->cSection );
1377 QUEUE_Unlock( queue );
1381 /***********************************************************************
1382 * GetWindowTask (USER.224)
1384 HTASK16 WINAPI GetWindowTask16( HWND16 hwnd )
1387 WND *wndPtr = WIN_FindWndPtr( hwnd );
1389 if (!wndPtr) return 0;
1390 retvalue = QUEUE_GetQueueTask( wndPtr->hmemTaskQ );
1391 WIN_ReleaseWndPtr(wndPtr);
1395 /***********************************************************************
1396 * GetWindowThreadProcessId (USER32.@)
1398 DWORD WINAPI GetWindowThreadProcessId( HWND hwnd, LPDWORD process )
1401 MESSAGEQUEUE *queue;
1403 WND *wndPtr = WIN_FindWndPtr( hwnd );
1404 if (!wndPtr) return 0;
1406 queue = QUEUE_Lock( wndPtr->hmemTaskQ );
1407 WIN_ReleaseWndPtr(wndPtr);
1409 if (!queue) return 0;
1411 if ( process ) *process = (DWORD)queue->teb->pid;
1412 retvalue = (DWORD)queue->teb->tid;
1414 QUEUE_Unlock( queue );
1419 /***********************************************************************
1420 * SetMessageQueue (USER.266)
1422 BOOL16 WINAPI SetMessageQueue16( INT16 size )
1424 return SetMessageQueue( size );
1428 /***********************************************************************
1429 * SetMessageQueue (USER32.@)
1431 BOOL WINAPI SetMessageQueue( INT size )
1433 /* now obsolete the message queue will be expanded dynamically
1436 /* access the queue to create it if it's not existing */
1442 /***********************************************************************
1443 * InitThreadInput (USER.409)
1445 HQUEUE16 WINAPI InitThreadInput16( WORD unknown, WORD flags )
1448 MESSAGEQUEUE *queuePtr;
1450 TEB *teb = NtCurrentTeb();
1455 hQueue = teb->queue;
1459 /* Create thread message queue */
1460 if( !(hQueue = QUEUE_CreateMsgQueue( TRUE )))
1462 ERR_(msg)("failed!\n");
1466 /* Link new queue into list */
1467 queuePtr = QUEUE_Lock( hQueue );
1468 queuePtr->teb = NtCurrentTeb();
1470 HeapLock( GetProcessHeap() ); /* FIXME: a bit overkill */
1471 SetThreadQueue16( 0, hQueue );
1472 teb->queue = hQueue;
1474 queuePtr->next = hFirstQueue;
1475 hFirstQueue = hQueue;
1476 HeapUnlock( GetProcessHeap() );
1478 QUEUE_Unlock( queuePtr );
1484 /***********************************************************************
1485 * GetQueueStatus (USER.334)
1487 DWORD WINAPI GetQueueStatus16( UINT16 flags )
1489 MESSAGEQUEUE *queue;
1492 if (!(queue = QUEUE_Lock( GetFastQueue16() ))) return 0;
1493 EnterCriticalSection( &queue->cSection );
1494 ret = MAKELONG( queue->changeBits, queue->wakeBits );
1495 queue->changeBits = 0;
1496 LeaveCriticalSection( &queue->cSection );
1497 QUEUE_Unlock( queue );
1499 return ret & MAKELONG( flags, flags );
1502 /***********************************************************************
1503 * GetQueueStatus (USER32.@)
1505 DWORD WINAPI GetQueueStatus( UINT flags )
1507 MESSAGEQUEUE *queue;
1510 if (!(queue = QUEUE_Lock( GetFastQueue16() ))) return 0;
1511 EnterCriticalSection( &queue->cSection );
1512 ret = MAKELONG( queue->changeBits, queue->wakeBits );
1513 queue->changeBits = 0;
1514 LeaveCriticalSection( &queue->cSection );
1515 QUEUE_Unlock( queue );
1517 return ret & MAKELONG( flags, flags );
1521 /***********************************************************************
1522 * GetInputState (USER.335)
1524 BOOL16 WINAPI GetInputState16(void)
1526 return GetInputState();
1529 /***********************************************************************
1530 * WaitForInputIdle (USER32.@)
1532 DWORD WINAPI WaitForInputIdle (HANDLE hProcess, DWORD dwTimeOut)
1534 DWORD cur_time, ret;
1535 HANDLE idle_event = -1;
1537 SERVER_START_REQ( wait_input_idle )
1539 req->handle = hProcess;
1540 req->timeout = dwTimeOut;
1541 if (!(ret = SERVER_CALL_ERR())) idle_event = req->event;
1544 if (ret) return 0xffffffff; /* error */
1545 if (!idle_event) return 0; /* no event to wait on */
1547 cur_time = GetTickCount();
1549 TRACE_(msg)("waiting for %x\n", idle_event );
1550 while ( dwTimeOut > GetTickCount() - cur_time || dwTimeOut == INFINITE )
1552 ret = MsgWaitForMultipleObjects ( 1, &idle_event, FALSE, dwTimeOut, QS_SENDMESSAGE );
1553 if ( ret == ( WAIT_OBJECT_0 + 1 ))
1555 MESSAGEQUEUE * queue;
1556 if (!(queue = QUEUE_Lock( GetFastQueue16() ))) return 0xFFFFFFFF;
1557 QUEUE_ReceiveMessage ( queue );
1558 QUEUE_Unlock ( queue );
1561 if ( ret == WAIT_TIMEOUT || ret == 0xFFFFFFFF )
1563 TRACE_(msg)("timeout or error\n");
1568 TRACE_(msg)("finished\n");
1573 return WAIT_TIMEOUT;
1576 /***********************************************************************
1577 * GetInputState (USER32.@)
1579 BOOL WINAPI GetInputState(void)
1581 MESSAGEQUEUE *queue;
1584 if (!(queue = QUEUE_Lock( GetFastQueue16() )))
1586 EnterCriticalSection( &queue->cSection );
1587 ret = queue->wakeBits & (QS_KEY | QS_MOUSEBUTTON);
1588 LeaveCriticalSection( &queue->cSection );
1589 QUEUE_Unlock( queue );
1594 /***********************************************************************
1595 * UserYield (USER.332)
1596 * UserYield16 (USER32.@)
1598 void WINAPI UserYield16(void)
1600 MESSAGEQUEUE *queue;
1602 /* Handle sent messages */
1603 queue = QUEUE_Lock( GetFastQueue16() );
1605 while ( queue && QUEUE_ReceiveMessage( queue ) )
1608 QUEUE_Unlock( queue );
1613 /* Handle sent messages again */
1614 queue = QUEUE_Lock( GetFastQueue16() );
1616 while ( queue && QUEUE_ReceiveMessage( queue ) )
1619 QUEUE_Unlock( queue );
1622 /***********************************************************************
1623 * GetMessagePos (USER.119) (USER32.@)
1625 * The GetMessagePos() function returns a long value representing a
1626 * cursor position, in screen coordinates, when the last message
1627 * retrieved by the GetMessage() function occurs. The x-coordinate is
1628 * in the low-order word of the return value, the y-coordinate is in
1629 * the high-order word. The application can use the MAKEPOINT()
1630 * macro to obtain a POINT structure from the return value.
1632 * For the current cursor position, use GetCursorPos().
1636 * Cursor position of last message on success, zero on failure.
1643 DWORD WINAPI GetMessagePos(void)
1645 MESSAGEQUEUE *queue;
1648 if (!(queue = QUEUE_Lock( GetFastQueue16() ))) return 0;
1649 ret = queue->GetMessagePosVal;
1650 QUEUE_Unlock( queue );
1656 /***********************************************************************
1657 * GetMessageTime (USER.120) (USER32.@)
1659 * GetMessageTime() returns the message time for the last message
1660 * retrieved by the function. The time is measured in milliseconds with
1661 * the same offset as GetTickCount().
1663 * Since the tick count wraps, this is only useful for moderately short
1664 * relative time comparisons.
1668 * Time of last message on success, zero on failure.
1675 LONG WINAPI GetMessageTime(void)
1677 MESSAGEQUEUE *queue;
1680 if (!(queue = QUEUE_Lock( GetFastQueue16() ))) return 0;
1681 ret = queue->GetMessageTimeVal;
1682 QUEUE_Unlock( queue );
1688 /***********************************************************************
1689 * GetMessageExtraInfo (USER.288) (USER32.@)
1691 LONG WINAPI GetMessageExtraInfo(void)
1693 MESSAGEQUEUE *queue;
1696 if (!(queue = QUEUE_Lock( GetFastQueue16() ))) return 0;
1697 ret = queue->GetMessageExtraInfoVal;
1698 QUEUE_Unlock( queue );
1704 /**********************************************************************
1705 * AttachThreadInput (USER32.@) Attaches input of 1 thread to other
1707 * Attaches the input processing mechanism of one thread to that of
1715 * 1. Reset the Key State (currenly per thread key state is not maintained)
1717 BOOL WINAPI AttachThreadInput(
1718 DWORD idAttach, /* [in] Thread to attach */
1719 DWORD idAttachTo, /* [in] Thread to attach to */
1720 BOOL fAttach) /* [in] Attach or detach */
1722 MESSAGEQUEUE *pSrcMsgQ = 0, *pTgtMsgQ = 0;
1725 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1727 /* A thread cannot attach to itself */
1728 if ( idAttach == idAttachTo )
1731 /* According to the docs this method should fail if a
1732 * "Journal record" hook is installed. (attaches all input queues together)
1734 if ( HOOK_IsHooked( WH_JOURNALRECORD ) )
1737 /* Retrieve message queues corresponding to the thread id's */
1738 pTgtMsgQ = QUEUE_Lock( GetThreadQueue16( idAttach ) );
1739 pSrcMsgQ = QUEUE_Lock( GetThreadQueue16( idAttachTo ) );
1741 /* Ensure we have message queues and that Src and Tgt threads
1742 * are not system threads.
1744 if ( !pSrcMsgQ || !pTgtMsgQ || !pSrcMsgQ->pQData || !pTgtMsgQ->pQData )
1747 if (fAttach) /* Attach threads */
1749 /* Only attach if currently detached */
1750 if ( pTgtMsgQ->pQData != pSrcMsgQ->pQData )
1752 /* First release the target threads perQData */
1753 PERQDATA_Release( pTgtMsgQ->pQData );
1755 /* Share a reference to the source threads perQDATA */
1756 PERQDATA_Addref( pSrcMsgQ->pQData );
1757 pTgtMsgQ->pQData = pSrcMsgQ->pQData;
1760 else /* Detach threads */
1762 /* Only detach if currently attached */
1763 if ( pTgtMsgQ->pQData == pSrcMsgQ->pQData )
1765 /* First release the target threads perQData */
1766 PERQDATA_Release( pTgtMsgQ->pQData );
1768 /* Give the target thread its own private perQDATA once more */
1769 pTgtMsgQ->pQData = PERQDATA_CreateInstance();
1773 /* TODO: Reset the Key State */
1775 bRet = 1; /* Success */
1779 /* Unlock the queues before returning */
1781 QUEUE_Unlock( pSrcMsgQ );
1783 QUEUE_Unlock( pTgtMsgQ );