1 /* * Message queues related functions
3 * Copyright 1993, 1994 Alexandre Julliard
12 #include "wine/winbase16.h"
13 #include "wine/winuser16.h"
18 #include "debugtools.h"
22 DECLARE_DEBUG_CHANNEL(msg);
23 DECLARE_DEBUG_CHANNEL(sendmsg);
25 #define MAX_QUEUE_SIZE 120 /* Max. size of a message queue */
27 static HQUEUE16 hFirstQueue = 0;
28 static HQUEUE16 hExitingQueue = 0;
29 static HQUEUE16 hmemSysMsgQueue = 0;
30 static MESSAGEQUEUE *sysMsgQueue = NULL;
31 static PERQUEUEDATA *pQDataWin16 = NULL; /* Global perQData for Win16 tasks */
33 static MESSAGEQUEUE *pMouseQueue = NULL; /* Queue for last mouse message */
34 static MESSAGEQUEUE *pKbdQueue = NULL; /* Queue for last kbd message */
36 HQUEUE16 hCursorQueue = 0;
37 HQUEUE16 hActiveQueue = 0;
40 /***********************************************************************
41 * PERQDATA_CreateInstance
43 * Creates an instance of a reference counted PERQUEUEDATA element
44 * for the message queue. perQData is stored globally for 16 bit tasks.
46 * Note: We don't implement perQdata exactly the same way Windows does.
47 * Each perQData element is reference counted since it may be potentially
48 * shared by multiple message Queues (via AttachThreadInput).
49 * We only store the current values for Active, Capture and focus windows
52 PERQUEUEDATA * PERQDATA_CreateInstance( )
60 /* Share a single instance of perQData for all 16 bit tasks */
61 if ( ( bIsWin16 = THREAD_IsWin16( NtCurrentTeb() ) ) )
63 /* If previously allocated, just bump up ref count */
66 PERQDATA_Addref( pQDataWin16 );
71 /* Allocate PERQUEUEDATA from the system heap */
72 if (!( pQData = (PERQUEUEDATA *) HeapAlloc( GetProcessHeap(), 0,
73 sizeof(PERQUEUEDATA) ) ))
77 pQData->hWndCapture = pQData->hWndFocus = pQData->hWndActive = 0;
78 pQData->ulRefCount = 1;
79 pQData->nCaptureHT = HTCLIENT;
81 /* Note: We have an independent critical section for the per queue data
82 * since this may be shared by different threads. see AttachThreadInput()
84 InitializeCriticalSection( &pQData->cSection );
85 /* FIXME: not all per queue data critical sections should be global */
86 MakeCriticalSectionGlobal( &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( GetProcessHeap(), 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 structure. 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( GetProcessHeap() ); /* FIXME: a bit overkill */
312 queue = GlobalLock16( hQueue );
313 if ( !queue || (queue->magic != QUEUE_MAGIC) )
315 HeapUnlock( GetProcessHeap() );
320 HeapUnlock( GetProcessHeap() );
325 /***********************************************************************
328 * Use with QUEUE_Lock to get a thread safe access to message queue
331 void QUEUE_Unlock( MESSAGEQUEUE *queue )
335 HeapLock( GetProcessHeap() ); /* FIXME: a bit overkill */
337 if ( --queue->lockCount == 0 )
339 DeleteCriticalSection ( &queue->cSection );
340 if (queue->server_queue)
341 CloseHandle( queue->server_queue );
342 GlobalFree16( queue->self );
345 HeapUnlock( GetProcessHeap() );
350 /***********************************************************************
353 void QUEUE_DumpQueue( HQUEUE16 hQueue )
357 if (!(pq = QUEUE_Lock( hQueue )) )
359 WARN_(msg)("%04x is not a queue handle\n", hQueue );
363 EnterCriticalSection( &pq->cSection );
365 DPRINTF( "next: %12.4x Intertask SendMessage:\n"
366 "thread: %10p ----------------------\n"
367 "firstMsg: %8p smWaiting: %10p\n"
368 "lastMsg: %8p smPending: %10p\n"
369 "msgCount: %8.4x smProcessing: %10p\n"
376 pq->next, pq->teb, pq->firstMsg, pq->smWaiting, pq->lastMsg,
377 pq->smPending, pq->msgCount, pq->smProcessing,
378 (unsigned)pq->lockCount, pq->wPaintCount, pq->wTimerCount,
379 pq->wakeBits, pq->wakeMask, pq->hCurHook);
381 LeaveCriticalSection( &pq->cSection );
387 /***********************************************************************
390 void QUEUE_WalkQueues(void)
393 HQUEUE16 hQueue = hFirstQueue;
395 DPRINTF( "Queue Msgs Thread Task Module\n" );
398 MESSAGEQUEUE *queue = QUEUE_Lock( hQueue );
401 WARN_(msg)("Bad queue handle %04x\n", hQueue );
404 if (!GetModuleName16( queue->teb->htask16, module, sizeof(module )))
405 strcpy( module, "???" );
406 DPRINTF( "%04x %4d %p %04x %s\n", hQueue,queue->msgCount,
407 queue->teb, queue->teb->htask16, module );
408 hQueue = queue->next;
409 QUEUE_Unlock( queue );
415 /***********************************************************************
416 * QUEUE_IsExitingQueue
418 BOOL QUEUE_IsExitingQueue( HQUEUE16 hQueue )
420 return (hExitingQueue && (hQueue == hExitingQueue));
424 /***********************************************************************
425 * QUEUE_SetExitingQueue
427 void QUEUE_SetExitingQueue( HQUEUE16 hQueue )
429 hExitingQueue = hQueue;
433 /***********************************************************************
434 * QUEUE_CreateMsgQueue
436 * Creates a message queue. Doesn't link it into queue list!
438 static HQUEUE16 QUEUE_CreateMsgQueue( BOOL16 bCreatePerQData )
442 MESSAGEQUEUE * msgQueue;
444 TRACE_(msg)("(): Creating message queue...\n");
446 if (!(hQueue = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT,
447 sizeof(MESSAGEQUEUE) )))
450 msgQueue = (MESSAGEQUEUE *) GlobalLock16( hQueue );
456 struct get_msg_queue_request *req = server_alloc_req( sizeof(*req), 0 );
457 server_call( 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 )))
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 if ( THREAD_IsWin16( queue->teb ) )
651 int iWndsLock = WIN_SuspendWndsLock();
652 PostEvent16( queue->teb->htask16 );
653 WIN_RestoreWndsLock( iWndsLock );
659 struct wake_queue_request *req = server_alloc_req( sizeof(*req), 0 );
660 req->handle = queue->server_queue;
662 server_call( REQ_WAKE_QUEUE );
670 void QUEUE_SetWakeBit( MESSAGEQUEUE *queue, WORD bit )
672 QUEUE_TrySetWakeBit( queue, bit, TRUE );
676 /***********************************************************************
679 void QUEUE_ClearWakeBit( MESSAGEQUEUE *queue, WORD bit )
681 EnterCriticalSection( &queue->cSection );
682 queue->changeBits &= ~bit;
683 queue->wakeBits &= ~bit;
684 LeaveCriticalSection( &queue->cSection );
687 /***********************************************************************
690 WORD QUEUE_TestWakeBit( MESSAGEQUEUE *queue, WORD bit )
693 EnterCriticalSection( &queue->cSection );
694 ret = queue->wakeBits & bit;
695 LeaveCriticalSection( &queue->cSection );
700 /***********************************************************************
703 * See "Windows Internals", p.447
706 * 0 if exit with timeout
709 int QUEUE_WaitBits( WORD bits, DWORD timeout )
715 TRACE_(msg)("q %04x waiting for %04x\n", GetFastQueue16(), bits);
717 if ( THREAD_IsWin16( NtCurrentTeb() ) && (timeout != INFINITE) )
718 curTime = GetTickCount();
720 hQueue = GetFastQueue16();
721 if (!(queue = QUEUE_Lock( hQueue ))) return 0;
725 EnterCriticalSection( &queue->cSection );
727 if (queue->changeBits & bits)
729 /* One of the bits is set; we can return */
732 LeaveCriticalSection( &queue->cSection );
733 QUEUE_Unlock( queue );
736 if (queue->wakeBits & QS_SENDMESSAGE)
738 /* Process the sent message immediately */
741 LeaveCriticalSection( &queue->cSection );
742 QUEUE_ReceiveMessage( queue );
743 continue; /* nested sm crux */
746 queue->wakeMask = bits | QS_SENDMESSAGE;
747 TRACE_(msg)("%04x) wakeMask is %04x, waiting\n", queue->self, queue->wakeMask);
748 LeaveCriticalSection( &queue->cSection );
750 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
755 if ( (bHasWin16Lock = _ConfirmWin16Lock()) )
757 TRACE_(msg)("bHasWin16Lock=TRUE\n");
758 ReleaseThunkLock( &dwlc );
761 WaitForSingleObject( queue->server_queue, timeout );
765 RestoreThunkLock( dwlc );
770 if ( timeout == INFINITE )
771 WaitEvent16( 0 ); /* win 16 thread, use WaitEvent */
774 /* check for timeout, then give control to other tasks */
775 if (GetTickCount() - curTime > timeout)
778 QUEUE_Unlock( queue );
779 return 0; /* exit with timeout */
788 /***********************************************************************
791 * This routine is called when a SMSG need to be added to one of the three
792 * SM list. (SM_PROCESSING_LIST, SM_PENDING_LIST, SM_WAITING_LIST)
794 BOOL QUEUE_AddSMSG( MESSAGEQUEUE *queue, int list, SMSG *smsg )
796 TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
797 smsg, SPY_GetMsgName(smsg->msg));
801 case SM_PROCESSING_LIST:
802 /* don't need to be thread safe, only accessed by the
803 thread associated with the sender queue */
804 smsg->nextProcessing = queue->smProcessing;
805 queue->smProcessing = smsg;
808 case SM_WAITING_LIST:
809 /* don't need to be thread safe, only accessed by the
810 thread associated with the receiver queue */
811 smsg->nextWaiting = queue->smWaiting;
812 queue->smWaiting = smsg;
815 case SM_PENDING_LIST:
817 /* make it thread safe, could be accessed by the sender and
821 EnterCriticalSection( &queue->cSection );
822 smsg->nextPending = NULL;
823 prev = &queue->smPending;
825 prev = &(*prev)->nextPending;
827 LeaveCriticalSection( &queue->cSection );
829 QUEUE_SetWakeBit( queue, QS_SENDMESSAGE );
834 ERR_(sendmsg)("Invalid list: %d", list);
842 /***********************************************************************
845 * This routine is called when a SMSG needs to be removed from one of the three
846 * SM lists (SM_PROCESSING_LIST, SM_PENDING_LIST, SM_WAITING_LIST).
847 * If smsg == 0, remove the first smsg from the specified list
849 SMSG *QUEUE_RemoveSMSG( MESSAGEQUEUE *queue, int list, SMSG *smsg )
854 case SM_PROCESSING_LIST:
855 /* don't need to be thread safe, only accessed by the
856 thread associated with the sender queue */
858 /* if smsg is equal to null, it means the first in the list */
860 smsg = queue->smProcessing;
862 TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
863 smsg, SPY_GetMsgName(smsg->msg));
864 /* In fact SM_PROCESSING_LIST is a stack, and smsg
865 should be always at the top of the list */
866 if ( (smsg != queue->smProcessing) || !queue->smProcessing )
868 ERR_(sendmsg)("smsg not at the top of Processing list, smsg=0x%p queue=0x%p\n", smsg, queue);
873 queue->smProcessing = smsg->nextProcessing;
874 smsg->nextProcessing = 0;
878 case SM_WAITING_LIST:
879 /* don't need to be thread safe, only accessed by the
880 thread associated with the receiver queue */
882 /* if smsg is equal to null, it means the first in the list */
884 smsg = queue->smWaiting;
886 TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
887 smsg, SPY_GetMsgName(smsg->msg));
888 /* In fact SM_WAITING_LIST is a stack, and smsg
889 should be always at the top of the list */
890 if ( (smsg != queue->smWaiting) || !queue->smWaiting )
892 ERR_(sendmsg)("smsg not at the top of Waiting list, smsg=0x%p queue=0x%p\n", smsg, queue);
897 queue->smWaiting = smsg->nextWaiting;
898 smsg->nextWaiting = 0;
902 case SM_PENDING_LIST:
903 /* make it thread safe, could be accessed by the sender and
905 EnterCriticalSection( &queue->cSection );
908 smsg = queue->smPending;
909 if ( (smsg != queue->smPending) || !queue->smPending )
911 ERR_(sendmsg)("should always remove the top one in Pending list, smsg=0x%p queue=0x%p\n", smsg, queue);
912 LeaveCriticalSection( &queue->cSection );
916 TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
917 smsg, SPY_GetMsgName(smsg->msg));
919 queue->smPending = smsg->nextPending;
920 smsg->nextPending = 0;
922 /* if no more SMSG in Pending list, clear QS_SENDMESSAGE flag */
923 if (!queue->smPending)
924 QUEUE_ClearWakeBit( queue, QS_SENDMESSAGE );
926 LeaveCriticalSection( &queue->cSection );
930 ERR_(sendmsg)("Invalid list: %d\n", list);
938 /***********************************************************************
939 * QUEUE_ReceiveMessage
941 * This routine is called to check whether a sent message is waiting
942 * for the queue. If so, it is received and processed.
944 BOOL QUEUE_ReceiveMessage( MESSAGEQUEUE *queue )
948 MESSAGEQUEUE *senderQ;
950 EnterCriticalSection( &queue->cSection );
951 if ( !((queue->wakeBits & QS_SENDMESSAGE) && queue->smPending) )
953 LeaveCriticalSection( &queue->cSection );
956 LeaveCriticalSection( &queue->cSection );
958 TRACE_(sendmsg)("queue %04x\n", queue->self );
960 /* remove smsg on the top of the pending list and put it in the processing list */
961 smsg = QUEUE_RemoveSMSG(queue, SM_PENDING_LIST, 0);
962 QUEUE_AddSMSG(queue, SM_WAITING_LIST, smsg);
964 TRACE_(sendmsg)("RM: %s [%04x] (%04x -> %04x)\n",
965 SPY_GetMsgName(smsg->msg), smsg->msg, smsg->hSrcQueue, smsg->hDstQueue );
967 if (IsWindow( smsg->hWnd ))
969 WND *wndPtr = WIN_FindWndPtr( smsg->hWnd );
970 DWORD extraInfo = queue->GetMessageExtraInfoVal; /* save ExtraInfo */
972 /* use sender queue extra info value while calling the window proc */
973 senderQ = QUEUE_Lock( smsg->hSrcQueue );
976 queue->GetMessageExtraInfoVal = senderQ->GetMessageExtraInfoVal;
977 QUEUE_Unlock( senderQ );
980 /* call the right version of CallWindowProcXX */
981 if (smsg->flags & SMSG_WIN32)
983 TRACE_(sendmsg)("\trcm: msg is Win32\n" );
984 if (smsg->flags & SMSG_UNICODE)
985 result = CallWindowProcW( wndPtr->winproc,
986 smsg->hWnd, smsg->msg,
987 smsg->wParam, smsg->lParam );
989 result = CallWindowProcA( wndPtr->winproc,
990 smsg->hWnd, smsg->msg,
991 smsg->wParam, smsg->lParam );
993 else /* Win16 message */
994 result = CallWindowProc16( (WNDPROC16)wndPtr->winproc,
997 LOWORD (smsg->wParam),
1000 queue->GetMessageExtraInfoVal = extraInfo; /* Restore extra info */
1001 WIN_ReleaseWndPtr(wndPtr);
1002 TRACE_(sendmsg)("result = %08x\n", (unsigned)result );
1004 else WARN_(sendmsg)("\trcm: bad hWnd\n");
1007 /* set SMSG_SENDING_REPLY flag to tell ReplyMessage16, it's not
1009 smsg->flags |= SMSG_SENDING_REPLY;
1010 ReplyMessage( result );
1012 TRACE_(sendmsg)("done!\n" );
1018 /***********************************************************************
1021 * Add a message to the queue. Return FALSE if queue is full.
1023 BOOL QUEUE_AddMsg( HQUEUE16 hQueue, int type, MSG *msg, DWORD extraInfo )
1025 MESSAGEQUEUE *msgQueue;
1029 if (!(msgQueue = QUEUE_Lock( hQueue ))) return FALSE;
1031 /* allocate new message in global heap for now */
1032 if (!(qmsg = (QMSG *) HeapAlloc( GetProcessHeap(), 0, sizeof(QMSG) ) ))
1034 QUEUE_Unlock( msgQueue );
1038 EnterCriticalSection( &msgQueue->cSection );
1043 qmsg->extraInfo = extraInfo;
1045 /* insert the message in the link list */
1047 qmsg->prevMsg = msgQueue->lastMsg;
1049 if (msgQueue->lastMsg)
1050 msgQueue->lastMsg->nextMsg = qmsg;
1052 /* update first and last anchor in message queue */
1053 msgQueue->lastMsg = qmsg;
1054 if (!msgQueue->firstMsg)
1055 msgQueue->firstMsg = qmsg;
1057 msgQueue->msgCount++;
1059 LeaveCriticalSection( &msgQueue->cSection );
1061 QUEUE_SetWakeBit( msgQueue, QS_POSTMESSAGE );
1062 QUEUE_Unlock( msgQueue );
1069 /***********************************************************************
1072 * Find a message matching the given parameters. Return -1 if none available.
1074 QMSG* QUEUE_FindMsg( MESSAGEQUEUE * msgQueue, HWND hwnd, int first, int last )
1078 EnterCriticalSection( &msgQueue->cSection );
1080 if (!msgQueue->msgCount)
1082 else if (!hwnd && !first && !last)
1083 qmsg = msgQueue->firstMsg;
1086 /* look in linked list for message matching first and last criteria */
1087 for (qmsg = msgQueue->firstMsg; qmsg; qmsg = qmsg->nextMsg)
1089 MSG *msg = &(qmsg->msg);
1091 if (!hwnd || (msg->hwnd == hwnd))
1093 if (!first && !last)
1094 break; /* found it */
1096 if ((msg->message >= first) && (!last || (msg->message <= last)))
1097 break; /* found it */
1102 LeaveCriticalSection( &msgQueue->cSection );
1109 /***********************************************************************
1112 * Remove a message from the queue (pos must be a valid position).
1114 void QUEUE_RemoveMsg( MESSAGEQUEUE * msgQueue, QMSG *qmsg )
1116 EnterCriticalSection( &msgQueue->cSection );
1118 /* set the linked list */
1120 qmsg->prevMsg->nextMsg = qmsg->nextMsg;
1123 qmsg->nextMsg->prevMsg = qmsg->prevMsg;
1125 if (msgQueue->firstMsg == qmsg)
1126 msgQueue->firstMsg = qmsg->nextMsg;
1128 if (msgQueue->lastMsg == qmsg)
1129 msgQueue->lastMsg = qmsg->prevMsg;
1131 /* deallocate the memory for the message */
1132 HeapFree( GetProcessHeap(), 0, qmsg );
1134 msgQueue->msgCount--;
1135 if (!msgQueue->msgCount) msgQueue->wakeBits &= ~QS_POSTMESSAGE;
1137 LeaveCriticalSection( &msgQueue->cSection );
1141 /***********************************************************************
1144 * Wake a queue upon reception of a hardware event.
1146 static void QUEUE_WakeSomeone( UINT message )
1151 HQUEUE16 hQueue = 0;
1152 MESSAGEQUEUE *queue = NULL;
1155 hQueue = hCursorQueue;
1157 if( (message >= WM_KEYFIRST) && (message <= WM_KEYLAST) )
1161 hQueue = hActiveQueue;
1165 wakeBit = (message == WM_MOUSEMOVE) ? QS_MOUSEMOVE : QS_MOUSEBUTTON;
1166 if( (hwnd = GetCapture()) )
1167 if( (wndPtr = WIN_FindWndPtr( hwnd )) )
1169 hQueue = wndPtr->hmemTaskQ;
1170 WIN_ReleaseWndPtr(wndPtr);
1174 if( (hwnd = GetSysModalWindow16()) )
1176 if( (wndPtr = WIN_FindWndPtr( hwnd )) )
1178 hQueue = wndPtr->hmemTaskQ;
1179 WIN_ReleaseWndPtr(wndPtr);
1185 queue = QUEUE_Lock( hQueue );
1186 QUEUE_SetWakeBit( queue, wakeBit );
1187 QUEUE_Unlock( queue );
1191 /* Search for someone to wake */
1192 hQueue = hFirstQueue;
1193 while ( (queue = QUEUE_Lock( hQueue )) )
1195 if (QUEUE_TrySetWakeBit( queue, wakeBit, FALSE ))
1197 QUEUE_Unlock( queue );
1201 hQueue = queue->next;
1202 QUEUE_Unlock( queue );
1205 WARN_(msg)("couldn't find queue\n");
1209 /***********************************************************************
1212 * Add an event to the system message queue.
1213 * Note: the position is relative to the desktop window.
1215 void hardware_event( UINT message, WPARAM wParam, LPARAM lParam,
1216 int xPos, int yPos, DWORD time, DWORD extraInfo )
1222 if (!sysMsgQueue) return;
1224 EnterCriticalSection( &sysMsgQueue->cSection );
1226 /* Merge with previous event if possible */
1227 qmsg = sysMsgQueue->lastMsg;
1229 if ((message == WM_MOUSEMOVE) && sysMsgQueue->lastMsg)
1231 msg = &(sysMsgQueue->lastMsg->msg);
1233 if ((msg->message == message) && (msg->wParam == wParam))
1236 qmsg = sysMsgQueue->lastMsg;
1243 /* Should I limit the number of messages in
1244 the system message queue??? */
1246 /* Don't merge allocate a new msg in the global heap */
1248 if (!(qmsg = (QMSG *) HeapAlloc( GetProcessHeap(), 0, sizeof(QMSG) ) ))
1250 LeaveCriticalSection( &sysMsgQueue->cSection );
1254 /* put message at the end of the linked list */
1256 qmsg->prevMsg = sysMsgQueue->lastMsg;
1258 if (sysMsgQueue->lastMsg)
1259 sysMsgQueue->lastMsg->nextMsg = qmsg;
1261 /* set last and first anchor index in system message queue */
1262 sysMsgQueue->lastMsg = qmsg;
1263 if (!sysMsgQueue->firstMsg)
1264 sysMsgQueue->firstMsg = qmsg;
1266 sysMsgQueue->msgCount++;
1272 msg->message = message;
1273 msg->wParam = wParam;
1274 msg->lParam = lParam;
1278 qmsg->extraInfo = extraInfo;
1279 qmsg->type = QMSG_HARDWARE;
1281 LeaveCriticalSection( &sysMsgQueue->cSection );
1283 QUEUE_WakeSomeone( message );
1287 /***********************************************************************
1288 * QUEUE_GetQueueTask
1290 HTASK16 QUEUE_GetQueueTask( HQUEUE16 hQueue )
1294 MESSAGEQUEUE *queue = QUEUE_Lock( hQueue );
1298 hTask = queue->teb->htask16;
1299 QUEUE_Unlock( queue );
1307 /***********************************************************************
1308 * QUEUE_IncPaintCount
1310 void QUEUE_IncPaintCount( HQUEUE16 hQueue )
1312 MESSAGEQUEUE *queue;
1314 if (!(queue = QUEUE_Lock( hQueue ))) return;
1315 EnterCriticalSection( &queue->cSection );
1316 queue->wPaintCount++;
1317 LeaveCriticalSection( &queue->cSection );
1318 QUEUE_SetWakeBit( queue, QS_PAINT );
1319 QUEUE_Unlock( queue );
1323 /***********************************************************************
1324 * QUEUE_DecPaintCount
1326 void QUEUE_DecPaintCount( HQUEUE16 hQueue )
1328 MESSAGEQUEUE *queue;
1330 if (!(queue = QUEUE_Lock( hQueue ))) return;
1331 EnterCriticalSection( &queue->cSection );
1332 queue->wPaintCount--;
1333 if (!queue->wPaintCount) queue->wakeBits &= ~QS_PAINT;
1334 LeaveCriticalSection( &queue->cSection );
1335 QUEUE_Unlock( queue );
1339 /***********************************************************************
1340 * QUEUE_IncTimerCount
1342 void QUEUE_IncTimerCount( HQUEUE16 hQueue )
1344 MESSAGEQUEUE *queue;
1346 if (!(queue = QUEUE_Lock( hQueue ))) return;
1347 EnterCriticalSection( &queue->cSection );
1348 queue->wTimerCount++;
1349 LeaveCriticalSection( &queue->cSection );
1350 QUEUE_SetWakeBit( queue, QS_TIMER );
1351 QUEUE_Unlock( queue );
1355 /***********************************************************************
1356 * QUEUE_DecTimerCount
1358 void QUEUE_DecTimerCount( HQUEUE16 hQueue )
1360 MESSAGEQUEUE *queue;
1362 if (!(queue = QUEUE_Lock( hQueue ))) return;
1363 EnterCriticalSection( &queue->cSection );
1364 queue->wTimerCount--;
1365 if (!queue->wTimerCount) queue->wakeBits &= ~QS_TIMER;
1366 LeaveCriticalSection( &queue->cSection );
1367 QUEUE_Unlock( queue );
1371 /***********************************************************************
1372 * PostQuitMessage (USER.6)
1374 void WINAPI PostQuitMessage16( INT16 exitCode )
1376 PostQuitMessage( exitCode );
1380 /***********************************************************************
1381 * PostQuitMessage (USER32.@)
1383 * PostQuitMessage() posts a message to the system requesting an
1384 * application to terminate execution. As a result of this function,
1385 * the WM_QUIT message is posted to the application, and
1386 * PostQuitMessage() returns immediately. The exitCode parameter
1387 * specifies an application-defined exit code, which appears in the
1388 * _wParam_ parameter of the WM_QUIT message posted to the application.
1394 void WINAPI PostQuitMessage( INT exitCode )
1396 MESSAGEQUEUE *queue;
1398 if (!(queue = QUEUE_Lock( GetFastQueue16() ))) return;
1399 EnterCriticalSection( &queue->cSection );
1400 queue->wPostQMsg = TRUE;
1401 queue->wExitCode = (WORD)exitCode;
1402 LeaveCriticalSection( &queue->cSection );
1403 QUEUE_Unlock( queue );
1407 /***********************************************************************
1408 * GetWindowTask (USER.224)
1410 HTASK16 WINAPI GetWindowTask16( HWND16 hwnd )
1413 WND *wndPtr = WIN_FindWndPtr( hwnd );
1415 if (!wndPtr) return 0;
1416 retvalue = QUEUE_GetQueueTask( wndPtr->hmemTaskQ );
1417 WIN_ReleaseWndPtr(wndPtr);
1421 /***********************************************************************
1422 * GetWindowThreadProcessId (USER32.@)
1424 DWORD WINAPI GetWindowThreadProcessId( HWND hwnd, LPDWORD process )
1427 MESSAGEQUEUE *queue;
1429 WND *wndPtr = WIN_FindWndPtr( hwnd );
1430 if (!wndPtr) return 0;
1432 queue = QUEUE_Lock( wndPtr->hmemTaskQ );
1433 WIN_ReleaseWndPtr(wndPtr);
1435 if (!queue) return 0;
1437 if ( process ) *process = (DWORD)queue->teb->pid;
1438 retvalue = (DWORD)queue->teb->tid;
1440 QUEUE_Unlock( queue );
1445 /***********************************************************************
1446 * SetMessageQueue (USER.266)
1448 BOOL16 WINAPI SetMessageQueue16( INT16 size )
1450 return SetMessageQueue( size );
1454 /***********************************************************************
1455 * SetMessageQueue (USER32.@)
1457 BOOL WINAPI SetMessageQueue( INT size )
1459 /* now obsolete the message queue will be expanded dynamically
1462 /* access the queue to create it if it's not existing */
1468 /***********************************************************************
1469 * InitThreadInput (USER.409)
1471 HQUEUE16 WINAPI InitThreadInput16( WORD unknown, WORD flags )
1474 MESSAGEQUEUE *queuePtr;
1476 TEB *teb = NtCurrentTeb();
1481 hQueue = teb->queue;
1485 /* Create thread message queue */
1486 if( !(hQueue = QUEUE_CreateMsgQueue( TRUE )))
1488 ERR_(msg)("failed!\n");
1492 /* Link new queue into list */
1493 queuePtr = QUEUE_Lock( hQueue );
1494 queuePtr->teb = NtCurrentTeb();
1496 HeapLock( GetProcessHeap() ); /* FIXME: a bit overkill */
1497 SetThreadQueue16( 0, hQueue );
1498 teb->queue = hQueue;
1500 queuePtr->next = hFirstQueue;
1501 hFirstQueue = hQueue;
1502 HeapUnlock( GetProcessHeap() );
1504 QUEUE_Unlock( queuePtr );
1510 /***********************************************************************
1511 * GetQueueStatus (USER.334)
1513 DWORD WINAPI GetQueueStatus16( UINT16 flags )
1515 MESSAGEQUEUE *queue;
1518 if (!(queue = QUEUE_Lock( GetFastQueue16() ))) return 0;
1519 EnterCriticalSection( &queue->cSection );
1520 ret = MAKELONG( queue->changeBits, queue->wakeBits );
1521 queue->changeBits = 0;
1522 LeaveCriticalSection( &queue->cSection );
1523 QUEUE_Unlock( queue );
1525 return ret & MAKELONG( flags, flags );
1528 /***********************************************************************
1529 * GetQueueStatus (USER32.@)
1531 DWORD WINAPI GetQueueStatus( UINT flags )
1533 MESSAGEQUEUE *queue;
1536 if (!(queue = QUEUE_Lock( GetFastQueue16() ))) return 0;
1537 EnterCriticalSection( &queue->cSection );
1538 ret = MAKELONG( queue->changeBits, queue->wakeBits );
1539 queue->changeBits = 0;
1540 LeaveCriticalSection( &queue->cSection );
1541 QUEUE_Unlock( queue );
1543 return ret & MAKELONG( flags, flags );
1547 /***********************************************************************
1548 * GetInputState (USER.335)
1550 BOOL16 WINAPI GetInputState16(void)
1552 return GetInputState();
1555 /***********************************************************************
1556 * WaitForInputIdle (USER32.@)
1558 DWORD WINAPI WaitForInputIdle (HANDLE hProcess, DWORD dwTimeOut)
1560 DWORD cur_time, ret;
1561 HANDLE idle_event = -1;
1565 struct wait_input_idle_request *req = server_alloc_req( sizeof(*req), 0 );
1566 req->handle = hProcess;
1567 req->timeout = dwTimeOut;
1568 if (!(ret = server_call( REQ_WAIT_INPUT_IDLE ))) idle_event = req->event;
1571 if (ret) return 0xffffffff; /* error */
1572 if (!idle_event) return 0; /* no event to wait on */
1574 cur_time = GetTickCount();
1576 TRACE_(msg)("waiting for %x\n", idle_event );
1577 while ( dwTimeOut > GetTickCount() - cur_time || dwTimeOut == INFINITE )
1579 ret = MsgWaitForMultipleObjects ( 1, &idle_event, FALSE, dwTimeOut, QS_SENDMESSAGE );
1580 if ( ret == ( WAIT_OBJECT_0 + 1 ))
1582 MESSAGEQUEUE * queue;
1583 if (!(queue = QUEUE_Lock( GetFastQueue16() ))) return 0xFFFFFFFF;
1584 QUEUE_ReceiveMessage ( queue );
1585 QUEUE_Unlock ( queue );
1588 if ( ret == WAIT_TIMEOUT || ret == 0xFFFFFFFF )
1590 TRACE_(msg)("timeout or error\n");
1595 TRACE_(msg)("finished\n");
1600 return WAIT_TIMEOUT;
1603 /***********************************************************************
1604 * GetInputState (USER32.@)
1606 BOOL WINAPI GetInputState(void)
1608 MESSAGEQUEUE *queue;
1611 if (!(queue = QUEUE_Lock( GetFastQueue16() )))
1613 EnterCriticalSection( &queue->cSection );
1614 ret = queue->wakeBits & (QS_KEY | QS_MOUSEBUTTON);
1615 LeaveCriticalSection( &queue->cSection );
1616 QUEUE_Unlock( queue );
1621 /***********************************************************************
1622 * UserYield (USER.332)
1623 * UserYield16 (USER32.@)
1625 void WINAPI UserYield16(void)
1627 MESSAGEQUEUE *queue;
1629 /* Handle sent messages */
1630 queue = QUEUE_Lock( GetFastQueue16() );
1632 while ( queue && QUEUE_ReceiveMessage( queue ) )
1635 QUEUE_Unlock( queue );
1638 if ( THREAD_IsWin16( NtCurrentTeb() ) )
1643 /* Handle sent messages again */
1644 queue = QUEUE_Lock( GetFastQueue16() );
1646 while ( queue && QUEUE_ReceiveMessage( queue ) )
1649 QUEUE_Unlock( queue );
1652 /***********************************************************************
1653 * GetMessagePos (USER.119) (USER32.@)
1655 * The GetMessagePos() function returns a long value representing a
1656 * cursor position, in screen coordinates, when the last message
1657 * retrieved by the GetMessage() function occurs. The x-coordinate is
1658 * in the low-order word of the return value, the y-coordinate is in
1659 * the high-order word. The application can use the MAKEPOINT()
1660 * macro to obtain a POINT structure from the return value.
1662 * For the current cursor position, use GetCursorPos().
1666 * Cursor position of last message on success, zero on failure.
1673 DWORD WINAPI GetMessagePos(void)
1675 MESSAGEQUEUE *queue;
1678 if (!(queue = QUEUE_Lock( GetFastQueue16() ))) return 0;
1679 ret = queue->GetMessagePosVal;
1680 QUEUE_Unlock( queue );
1686 /***********************************************************************
1687 * GetMessageTime (USER.120) (USER32.@)
1689 * GetMessageTime() returns the message time for the last message
1690 * retrieved by the function. The time is measured in milliseconds with
1691 * the same offset as GetTickCount().
1693 * Since the tick count wraps, this is only useful for moderately short
1694 * relative time comparisons.
1698 * Time of last message on success, zero on failure.
1705 LONG WINAPI GetMessageTime(void)
1707 MESSAGEQUEUE *queue;
1710 if (!(queue = QUEUE_Lock( GetFastQueue16() ))) return 0;
1711 ret = queue->GetMessageTimeVal;
1712 QUEUE_Unlock( queue );
1718 /***********************************************************************
1719 * GetMessageExtraInfo (USER.288) (USER32.@)
1721 LONG WINAPI GetMessageExtraInfo(void)
1723 MESSAGEQUEUE *queue;
1726 if (!(queue = QUEUE_Lock( GetFastQueue16() ))) return 0;
1727 ret = queue->GetMessageExtraInfoVal;
1728 QUEUE_Unlock( queue );
1734 /**********************************************************************
1735 * AttachThreadInput (USER32.@) Attaches input of 1 thread to other
1737 * Attaches the input processing mechanism of one thread to that of
1745 * 1. Reset the Key State (currenly per thread key state is not maintained)
1747 BOOL WINAPI AttachThreadInput(
1748 DWORD idAttach, /* [in] Thread to attach */
1749 DWORD idAttachTo, /* [in] Thread to attach to */
1750 BOOL fAttach) /* [in] Attach or detach */
1752 MESSAGEQUEUE *pSrcMsgQ = 0, *pTgtMsgQ = 0;
1755 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1757 /* A thread cannot attach to itself */
1758 if ( idAttach == idAttachTo )
1761 /* According to the docs this method should fail if a
1762 * "Journal record" hook is installed. (attaches all input queues together)
1764 if ( HOOK_IsHooked( WH_JOURNALRECORD ) )
1767 /* Retrieve message queues corresponding to the thread id's */
1768 pTgtMsgQ = QUEUE_Lock( GetThreadQueue16( idAttach ) );
1769 pSrcMsgQ = QUEUE_Lock( GetThreadQueue16( idAttachTo ) );
1771 /* Ensure we have message queues and that Src and Tgt threads
1772 * are not system threads.
1774 if ( !pSrcMsgQ || !pTgtMsgQ || !pSrcMsgQ->pQData || !pTgtMsgQ->pQData )
1777 if (fAttach) /* Attach threads */
1779 /* Only attach if currently detached */
1780 if ( pTgtMsgQ->pQData != pSrcMsgQ->pQData )
1782 /* First release the target threads perQData */
1783 PERQDATA_Release( pTgtMsgQ->pQData );
1785 /* Share a reference to the source threads perQDATA */
1786 PERQDATA_Addref( pSrcMsgQ->pQData );
1787 pTgtMsgQ->pQData = pSrcMsgQ->pQData;
1790 else /* Detach threads */
1792 /* Only detach if currently attached */
1793 if ( pTgtMsgQ->pQData == pSrcMsgQ->pQData )
1795 /* First release the target threads perQData */
1796 PERQDATA_Release( pTgtMsgQ->pQData );
1798 /* Give the target thread its own private perQDATA once more */
1799 pTgtMsgQ->pQData = PERQDATA_CreateInstance();
1803 /* TODO: Reset the Key State */
1805 bRet = 1; /* Success */
1809 /* Unlock the queues before returning */
1811 QUEUE_Unlock( pSrcMsgQ );
1813 QUEUE_Unlock( pTgtMsgQ );