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( SystemHeap, 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( SystemHeap, 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( SystemHeap ); /* FIXME: a bit overkill */
313 queue = GlobalLock16( hQueue );
314 if ( !queue || (queue->magic != QUEUE_MAGIC) )
316 HeapUnlock( SystemHeap );
321 HeapUnlock( SystemHeap );
326 /***********************************************************************
329 * Use with QUEUE_Lock to get a thread safe access to message queue
332 void QUEUE_Unlock( MESSAGEQUEUE *queue )
336 HeapLock( SystemHeap ); /* 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( SystemHeap );
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 );
457 struct get_msg_queue_request *req = server_alloc_req( sizeof(*req), 0 );
458 server_call( REQ_GET_MSG_QUEUE );
459 handle = req->handle;
464 ERR_(msg)("Cannot get thread queue");
465 GlobalFree16( hQueue );
468 msgQueue->server_queue = handle;
469 msgQueue->server_queue = ConvertToGlobalHandle( msgQueue->server_queue );
471 msgQueue->self = hQueue;
472 msgQueue->wakeBits = msgQueue->changeBits = 0;
474 InitializeCriticalSection( &msgQueue->cSection );
475 MakeCriticalSectionGlobal( &msgQueue->cSection );
477 msgQueue->lockCount = 1;
478 msgQueue->magic = QUEUE_MAGIC;
480 /* Create and initialize our per queue data */
481 msgQueue->pQData = bCreatePerQData ? PERQDATA_CreateInstance() : NULL;
487 /***********************************************************************
490 * Try to reply to all pending sent messages on exit.
492 static void QUEUE_FlushMessages( MESSAGEQUEUE *queue )
495 MESSAGEQUEUE *senderQ = 0;
499 EnterCriticalSection( &queue->cSection );
501 /* empty the list of pending SendMessage waiting to be received */
502 while (queue->smPending)
504 smsg = QUEUE_RemoveSMSG( queue, SM_PENDING_LIST, 0);
506 senderQ = QUEUE_Lock( smsg->hSrcQueue );
510 /* return 0, to unblock other thread */
512 smsg->flags |= SMSG_HAVE_RESULT;
513 QUEUE_SetWakeBit( senderQ, QS_SMRESULT);
515 QUEUE_Unlock( senderQ );
518 QUEUE_ClearWakeBit( queue, QS_SENDMESSAGE );
520 LeaveCriticalSection( &queue->cSection );
525 /***********************************************************************
526 * QUEUE_DeleteMsgQueue
528 * Unlinks and deletes a message queue.
530 * Note: We need to mask asynchronous events to make sure PostMessage works
531 * even in the signal handler.
533 BOOL QUEUE_DeleteMsgQueue( HQUEUE16 hQueue )
535 MESSAGEQUEUE * msgQueue = QUEUE_Lock(hQueue);
538 TRACE_(msg)("(): Deleting message queue %04x\n", hQueue);
540 if (!hQueue || !msgQueue)
542 ERR_(msg)("invalid argument.\n");
548 if( hCursorQueue == hQueue ) hCursorQueue = 0;
549 if( hActiveQueue == hQueue ) hActiveQueue = 0;
551 /* flush sent messages */
552 QUEUE_FlushMessages( msgQueue );
554 HeapLock( SystemHeap ); /* FIXME: a bit overkill */
556 /* Release per queue data if present */
557 if ( msgQueue->pQData )
559 PERQDATA_Release( msgQueue->pQData );
560 msgQueue->pQData = 0;
563 /* remove the message queue from the global link list */
564 pPrev = &hFirstQueue;
565 while (*pPrev && (*pPrev != hQueue))
567 MESSAGEQUEUE *msgQ = (MESSAGEQUEUE*)GlobalLock16(*pPrev);
570 if ( !msgQ || (msgQ->magic != QUEUE_MAGIC) )
572 /* HQUEUE link list is corrupted, try to exit gracefully */
573 ERR_(msg)("HQUEUE link list corrupted!\n");
579 if (pPrev && *pPrev) *pPrev = msgQueue->next;
582 HeapUnlock( SystemHeap );
584 /* free up resource used by MESSAGEQUEUE structure */
585 msgQueue->lockCount--;
586 QUEUE_Unlock( msgQueue );
592 /***********************************************************************
593 * QUEUE_CreateSysMsgQueue
595 * Create the system message queue, and set the double-click speed.
596 * Must be called only once.
598 BOOL QUEUE_CreateSysMsgQueue( int size )
600 /* Note: We dont need perQ data for the system message queue */
601 if (!(hmemSysMsgQueue = QUEUE_CreateMsgQueue( FALSE )))
604 sysMsgQueue = (MESSAGEQUEUE *) GlobalLock16( hmemSysMsgQueue );
609 /***********************************************************************
612 MESSAGEQUEUE *QUEUE_GetSysQueue(void)
618 /***********************************************************************
621 * See "Windows Internals", p.449
623 static BOOL QUEUE_TrySetWakeBit( MESSAGEQUEUE *queue, WORD bit, BOOL always )
627 EnterCriticalSection( &queue->cSection );
629 TRACE_(msg)("queue = %04x (wm=%04x), bit = %04x, always = %d\n",
630 queue->self, queue->wakeMask, bit, always );
632 if ((queue->wakeMask & bit) || always)
634 if (bit & QS_MOUSE) pMouseQueue = queue;
635 if (bit & QS_KEY) pKbdQueue = queue;
636 queue->changeBits |= bit;
637 queue->wakeBits |= bit;
639 if (queue->wakeMask & bit)
645 LeaveCriticalSection( &queue->cSection );
649 /* Wake up thread waiting for message */
650 if ( THREAD_IsWin16( queue->teb ) )
652 int iWndsLock = WIN_SuspendWndsLock();
653 PostEvent16( queue->teb->htask16 );
654 WIN_RestoreWndsLock( iWndsLock );
660 struct wake_queue_request *req = server_alloc_req( sizeof(*req), 0 );
661 req->handle = queue->server_queue;
663 server_call( REQ_WAKE_QUEUE );
671 void QUEUE_SetWakeBit( MESSAGEQUEUE *queue, WORD bit )
673 QUEUE_TrySetWakeBit( queue, bit, TRUE );
677 /***********************************************************************
680 void QUEUE_ClearWakeBit( MESSAGEQUEUE *queue, WORD bit )
682 EnterCriticalSection( &queue->cSection );
683 queue->changeBits &= ~bit;
684 queue->wakeBits &= ~bit;
685 LeaveCriticalSection( &queue->cSection );
688 /***********************************************************************
691 WORD QUEUE_TestWakeBit( MESSAGEQUEUE *queue, WORD bit )
694 EnterCriticalSection( &queue->cSection );
695 ret = queue->wakeBits & bit;
696 LeaveCriticalSection( &queue->cSection );
701 /***********************************************************************
704 * See "Windows Internals", p.447
707 * 0 if exit with timeout
710 int QUEUE_WaitBits( WORD bits, DWORD timeout )
716 TRACE_(msg)("q %04x waiting for %04x\n", GetFastQueue16(), bits);
718 if ( THREAD_IsWin16( NtCurrentTeb() ) && (timeout != INFINITE) )
719 curTime = GetTickCount();
721 hQueue = GetFastQueue16();
722 if (!(queue = QUEUE_Lock( hQueue ))) return 0;
726 EnterCriticalSection( &queue->cSection );
728 if (queue->changeBits & bits)
730 /* One of the bits is set; we can return */
733 LeaveCriticalSection( &queue->cSection );
734 QUEUE_Unlock( queue );
737 if (queue->wakeBits & QS_SENDMESSAGE)
739 /* Process the sent message immediately */
742 LeaveCriticalSection( &queue->cSection );
743 QUEUE_ReceiveMessage( queue );
744 continue; /* nested sm crux */
747 queue->wakeMask = bits | QS_SENDMESSAGE;
748 TRACE_(msg)("%04x) wakeMask is %04x, waiting\n", queue->self, queue->wakeMask);
749 LeaveCriticalSection( &queue->cSection );
751 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
756 if ( (bHasWin16Lock = _ConfirmWin16Lock()) )
758 TRACE_(msg)("bHasWin16Lock=TRUE\n");
759 ReleaseThunkLock( &dwlc );
762 WaitForSingleObject( queue->server_queue, timeout );
766 RestoreThunkLock( dwlc );
771 if ( timeout == INFINITE )
772 WaitEvent16( 0 ); /* win 16 thread, use WaitEvent */
775 /* check for timeout, then give control to other tasks */
776 if (GetTickCount() - curTime > timeout)
779 QUEUE_Unlock( queue );
780 return 0; /* exit with timeout */
789 /***********************************************************************
792 * This routine is called when a SMSG need to be added to one of the three
793 * SM list. (SM_PROCESSING_LIST, SM_PENDING_LIST, SM_WAITING_LIST)
795 BOOL QUEUE_AddSMSG( MESSAGEQUEUE *queue, int list, SMSG *smsg )
797 TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
798 smsg, SPY_GetMsgName(smsg->msg));
802 case SM_PROCESSING_LIST:
803 /* don't need to be thread safe, only accessed by the
804 thread associated with the sender queue */
805 smsg->nextProcessing = queue->smProcessing;
806 queue->smProcessing = smsg;
809 case SM_WAITING_LIST:
810 /* don't need to be thread safe, only accessed by the
811 thread associated with the receiver queue */
812 smsg->nextWaiting = queue->smWaiting;
813 queue->smWaiting = smsg;
816 case SM_PENDING_LIST:
818 /* make it thread safe, could be accessed by the sender and
822 EnterCriticalSection( &queue->cSection );
823 smsg->nextPending = NULL;
824 prev = &queue->smPending;
826 prev = &(*prev)->nextPending;
828 LeaveCriticalSection( &queue->cSection );
830 QUEUE_SetWakeBit( queue, QS_SENDMESSAGE );
835 ERR_(sendmsg)("Invalid list: %d", list);
843 /***********************************************************************
846 * This routine is called when a SMSG needs to be removed from one of the three
847 * SM lists (SM_PROCESSING_LIST, SM_PENDING_LIST, SM_WAITING_LIST).
848 * If smsg == 0, remove the first smsg from the specified list
850 SMSG *QUEUE_RemoveSMSG( MESSAGEQUEUE *queue, int list, SMSG *smsg )
855 case SM_PROCESSING_LIST:
856 /* don't need to be thread safe, only accessed by the
857 thread associated with the sender queue */
859 /* if smsg is equal to null, it means the first in the list */
861 smsg = queue->smProcessing;
863 TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
864 smsg, SPY_GetMsgName(smsg->msg));
865 /* In fact SM_PROCESSING_LIST is a stack, and smsg
866 should be always at the top of the list */
867 if ( (smsg != queue->smProcessing) || !queue->smProcessing )
869 ERR_(sendmsg)("smsg not at the top of Processing list, smsg=0x%p queue=0x%p\n", smsg, queue);
874 queue->smProcessing = smsg->nextProcessing;
875 smsg->nextProcessing = 0;
879 case SM_WAITING_LIST:
880 /* don't need to be thread safe, only accessed by the
881 thread associated with the receiver queue */
883 /* if smsg is equal to null, it means the first in the list */
885 smsg = queue->smWaiting;
887 TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
888 smsg, SPY_GetMsgName(smsg->msg));
889 /* In fact SM_WAITING_LIST is a stack, and smsg
890 should be always at the top of the list */
891 if ( (smsg != queue->smWaiting) || !queue->smWaiting )
893 ERR_(sendmsg)("smsg not at the top of Waiting list, smsg=0x%p queue=0x%p\n", smsg, queue);
898 queue->smWaiting = smsg->nextWaiting;
899 smsg->nextWaiting = 0;
903 case SM_PENDING_LIST:
904 /* make it thread safe, could be accessed by the sender and
906 EnterCriticalSection( &queue->cSection );
909 smsg = queue->smPending;
910 if ( (smsg != queue->smPending) || !queue->smPending )
912 ERR_(sendmsg)("should always remove the top one in Pending list, smsg=0x%p queue=0x%p\n", smsg, queue);
913 LeaveCriticalSection( &queue->cSection );
917 TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
918 smsg, SPY_GetMsgName(smsg->msg));
920 queue->smPending = smsg->nextPending;
921 smsg->nextPending = 0;
923 /* if no more SMSG in Pending list, clear QS_SENDMESSAGE flag */
924 if (!queue->smPending)
925 QUEUE_ClearWakeBit( queue, QS_SENDMESSAGE );
927 LeaveCriticalSection( &queue->cSection );
931 ERR_(sendmsg)("Invalid list: %d\n", list);
939 /***********************************************************************
940 * QUEUE_ReceiveMessage
942 * This routine is called to check whether a sent message is waiting
943 * for the queue. If so, it is received and processed.
945 BOOL QUEUE_ReceiveMessage( MESSAGEQUEUE *queue )
949 MESSAGEQUEUE *senderQ;
951 EnterCriticalSection( &queue->cSection );
952 if ( !((queue->wakeBits & QS_SENDMESSAGE) && queue->smPending) )
954 LeaveCriticalSection( &queue->cSection );
957 LeaveCriticalSection( &queue->cSection );
959 TRACE_(sendmsg)("queue %04x\n", queue->self );
961 /* remove smsg on the top of the pending list and put it in the processing list */
962 smsg = QUEUE_RemoveSMSG(queue, SM_PENDING_LIST, 0);
963 QUEUE_AddSMSG(queue, SM_WAITING_LIST, smsg);
965 TRACE_(sendmsg)("RM: %s [%04x] (%04x -> %04x)\n",
966 SPY_GetMsgName(smsg->msg), smsg->msg, smsg->hSrcQueue, smsg->hDstQueue );
968 if (IsWindow( smsg->hWnd ))
970 WND *wndPtr = WIN_FindWndPtr( smsg->hWnd );
971 DWORD extraInfo = queue->GetMessageExtraInfoVal; /* save ExtraInfo */
973 /* use sender queue extra info value while calling the window proc */
974 senderQ = QUEUE_Lock( smsg->hSrcQueue );
977 queue->GetMessageExtraInfoVal = senderQ->GetMessageExtraInfoVal;
978 QUEUE_Unlock( senderQ );
981 /* call the right version of CallWindowProcXX */
982 if (smsg->flags & SMSG_WIN32)
984 TRACE_(sendmsg)("\trcm: msg is Win32\n" );
985 if (smsg->flags & SMSG_UNICODE)
986 result = CallWindowProcW( wndPtr->winproc,
987 smsg->hWnd, smsg->msg,
988 smsg->wParam, smsg->lParam );
990 result = CallWindowProcA( wndPtr->winproc,
991 smsg->hWnd, smsg->msg,
992 smsg->wParam, smsg->lParam );
994 else /* Win16 message */
995 result = CallWindowProc16( (WNDPROC16)wndPtr->winproc,
998 LOWORD (smsg->wParam),
1001 queue->GetMessageExtraInfoVal = extraInfo; /* Restore extra info */
1002 WIN_ReleaseWndPtr(wndPtr);
1003 TRACE_(sendmsg)("result = %08x\n", (unsigned)result );
1005 else WARN_(sendmsg)("\trcm: bad hWnd\n");
1008 /* set SMSG_SENDING_REPLY flag to tell ReplyMessage16, it's not
1010 smsg->flags |= SMSG_SENDING_REPLY;
1011 ReplyMessage( result );
1013 TRACE_(sendmsg)("done!\n" );
1019 /***********************************************************************
1022 * Add a message to the queue. Return FALSE if queue is full.
1024 BOOL QUEUE_AddMsg( HQUEUE16 hQueue, int type, MSG *msg, DWORD extraInfo )
1026 MESSAGEQUEUE *msgQueue;
1030 if (!(msgQueue = QUEUE_Lock( hQueue ))) return FALSE;
1032 /* allocate new message in global heap for now */
1033 if (!(qmsg = (QMSG *) HeapAlloc( SystemHeap, 0, sizeof(QMSG) ) ))
1035 QUEUE_Unlock( msgQueue );
1039 EnterCriticalSection( &msgQueue->cSection );
1044 qmsg->extraInfo = extraInfo;
1046 /* insert the message in the link list */
1048 qmsg->prevMsg = msgQueue->lastMsg;
1050 if (msgQueue->lastMsg)
1051 msgQueue->lastMsg->nextMsg = qmsg;
1053 /* update first and last anchor in message queue */
1054 msgQueue->lastMsg = qmsg;
1055 if (!msgQueue->firstMsg)
1056 msgQueue->firstMsg = qmsg;
1058 msgQueue->msgCount++;
1060 LeaveCriticalSection( &msgQueue->cSection );
1062 QUEUE_SetWakeBit( msgQueue, QS_POSTMESSAGE );
1063 QUEUE_Unlock( msgQueue );
1070 /***********************************************************************
1073 * Find a message matching the given parameters. Return -1 if none available.
1075 QMSG* QUEUE_FindMsg( MESSAGEQUEUE * msgQueue, HWND hwnd, int first, int last )
1079 EnterCriticalSection( &msgQueue->cSection );
1081 if (!msgQueue->msgCount)
1083 else if (!hwnd && !first && !last)
1084 qmsg = msgQueue->firstMsg;
1087 /* look in linked list for message matching first and last criteria */
1088 for (qmsg = msgQueue->firstMsg; qmsg; qmsg = qmsg->nextMsg)
1090 MSG *msg = &(qmsg->msg);
1092 if (!hwnd || (msg->hwnd == hwnd))
1094 if (!first && !last)
1095 break; /* found it */
1097 if ((msg->message >= first) && (!last || (msg->message <= last)))
1098 break; /* found it */
1103 LeaveCriticalSection( &msgQueue->cSection );
1110 /***********************************************************************
1113 * Remove a message from the queue (pos must be a valid position).
1115 void QUEUE_RemoveMsg( MESSAGEQUEUE * msgQueue, QMSG *qmsg )
1117 EnterCriticalSection( &msgQueue->cSection );
1119 /* set the linked list */
1121 qmsg->prevMsg->nextMsg = qmsg->nextMsg;
1124 qmsg->nextMsg->prevMsg = qmsg->prevMsg;
1126 if (msgQueue->firstMsg == qmsg)
1127 msgQueue->firstMsg = qmsg->nextMsg;
1129 if (msgQueue->lastMsg == qmsg)
1130 msgQueue->lastMsg = qmsg->prevMsg;
1132 /* deallocate the memory for the message */
1133 HeapFree( SystemHeap, 0, qmsg );
1135 msgQueue->msgCount--;
1136 if (!msgQueue->msgCount) msgQueue->wakeBits &= ~QS_POSTMESSAGE;
1138 LeaveCriticalSection( &msgQueue->cSection );
1142 /***********************************************************************
1145 * Wake a queue upon reception of a hardware event.
1147 static void QUEUE_WakeSomeone( UINT message )
1152 HQUEUE16 hQueue = 0;
1153 MESSAGEQUEUE *queue = NULL;
1156 hQueue = hCursorQueue;
1158 if( (message >= WM_KEYFIRST) && (message <= WM_KEYLAST) )
1162 hQueue = hActiveQueue;
1166 wakeBit = (message == WM_MOUSEMOVE) ? QS_MOUSEMOVE : QS_MOUSEBUTTON;
1167 if( (hwnd = GetCapture()) )
1168 if( (wndPtr = WIN_FindWndPtr( hwnd )) )
1170 hQueue = wndPtr->hmemTaskQ;
1171 WIN_ReleaseWndPtr(wndPtr);
1175 if( (hwnd = GetSysModalWindow16()) )
1177 if( (wndPtr = WIN_FindWndPtr( hwnd )) )
1179 hQueue = wndPtr->hmemTaskQ;
1180 WIN_ReleaseWndPtr(wndPtr);
1186 queue = QUEUE_Lock( hQueue );
1187 QUEUE_SetWakeBit( queue, wakeBit );
1188 QUEUE_Unlock( queue );
1192 /* Search for someone to wake */
1193 hQueue = hFirstQueue;
1194 while ( (queue = QUEUE_Lock( hQueue )) )
1196 if (QUEUE_TrySetWakeBit( queue, wakeBit, FALSE ))
1198 QUEUE_Unlock( queue );
1202 hQueue = queue->next;
1203 QUEUE_Unlock( queue );
1206 WARN_(msg)("couldn't find queue\n");
1210 /***********************************************************************
1213 * Add an event to the system message queue.
1214 * Note: the position is relative to the desktop window.
1216 void hardware_event( UINT message, WPARAM wParam, LPARAM lParam,
1217 int xPos, int yPos, DWORD time, DWORD extraInfo )
1223 if (!sysMsgQueue) return;
1225 EnterCriticalSection( &sysMsgQueue->cSection );
1227 /* Merge with previous event if possible */
1228 qmsg = sysMsgQueue->lastMsg;
1230 if ((message == WM_MOUSEMOVE) && sysMsgQueue->lastMsg)
1232 msg = &(sysMsgQueue->lastMsg->msg);
1234 if ((msg->message == message) && (msg->wParam == wParam))
1237 qmsg = sysMsgQueue->lastMsg;
1244 /* Should I limit the number of messages in
1245 the system message queue??? */
1247 /* Don't merge allocate a new msg in the global heap */
1249 if (!(qmsg = (QMSG *) HeapAlloc( SystemHeap, 0, sizeof(QMSG) ) ))
1251 LeaveCriticalSection( &sysMsgQueue->cSection );
1255 /* put message at the end of the linked list */
1257 qmsg->prevMsg = sysMsgQueue->lastMsg;
1259 if (sysMsgQueue->lastMsg)
1260 sysMsgQueue->lastMsg->nextMsg = qmsg;
1262 /* set last and first anchor index in system message queue */
1263 sysMsgQueue->lastMsg = qmsg;
1264 if (!sysMsgQueue->firstMsg)
1265 sysMsgQueue->firstMsg = qmsg;
1267 sysMsgQueue->msgCount++;
1273 msg->message = message;
1274 msg->wParam = wParam;
1275 msg->lParam = lParam;
1279 qmsg->extraInfo = extraInfo;
1280 qmsg->type = QMSG_HARDWARE;
1282 LeaveCriticalSection( &sysMsgQueue->cSection );
1284 QUEUE_WakeSomeone( message );
1288 /***********************************************************************
1289 * QUEUE_GetQueueTask
1291 HTASK16 QUEUE_GetQueueTask( HQUEUE16 hQueue )
1295 MESSAGEQUEUE *queue = QUEUE_Lock( hQueue );
1299 hTask = queue->teb->htask16;
1300 QUEUE_Unlock( queue );
1308 /***********************************************************************
1309 * QUEUE_IncPaintCount
1311 void QUEUE_IncPaintCount( HQUEUE16 hQueue )
1313 MESSAGEQUEUE *queue;
1315 if (!(queue = QUEUE_Lock( hQueue ))) return;
1316 EnterCriticalSection( &queue->cSection );
1317 queue->wPaintCount++;
1318 LeaveCriticalSection( &queue->cSection );
1319 QUEUE_SetWakeBit( queue, QS_PAINT );
1320 QUEUE_Unlock( queue );
1324 /***********************************************************************
1325 * QUEUE_DecPaintCount
1327 void QUEUE_DecPaintCount( HQUEUE16 hQueue )
1329 MESSAGEQUEUE *queue;
1331 if (!(queue = QUEUE_Lock( hQueue ))) return;
1332 EnterCriticalSection( &queue->cSection );
1333 queue->wPaintCount--;
1334 if (!queue->wPaintCount) queue->wakeBits &= ~QS_PAINT;
1335 LeaveCriticalSection( &queue->cSection );
1336 QUEUE_Unlock( queue );
1340 /***********************************************************************
1341 * QUEUE_IncTimerCount
1343 void QUEUE_IncTimerCount( HQUEUE16 hQueue )
1345 MESSAGEQUEUE *queue;
1347 if (!(queue = QUEUE_Lock( hQueue ))) return;
1348 EnterCriticalSection( &queue->cSection );
1349 queue->wTimerCount++;
1350 LeaveCriticalSection( &queue->cSection );
1351 QUEUE_SetWakeBit( queue, QS_TIMER );
1352 QUEUE_Unlock( queue );
1356 /***********************************************************************
1357 * QUEUE_DecTimerCount
1359 void QUEUE_DecTimerCount( HQUEUE16 hQueue )
1361 MESSAGEQUEUE *queue;
1363 if (!(queue = QUEUE_Lock( hQueue ))) return;
1364 EnterCriticalSection( &queue->cSection );
1365 queue->wTimerCount--;
1366 if (!queue->wTimerCount) queue->wakeBits &= ~QS_TIMER;
1367 LeaveCriticalSection( &queue->cSection );
1368 QUEUE_Unlock( queue );
1372 /***********************************************************************
1373 * PostQuitMessage (USER.6)
1375 void WINAPI PostQuitMessage16( INT16 exitCode )
1377 PostQuitMessage( exitCode );
1381 /***********************************************************************
1382 * PostQuitMessage (USER32.@)
1384 * PostQuitMessage() posts a message to the system requesting an
1385 * application to terminate execution. As a result of this function,
1386 * the WM_QUIT message is posted to the application, and
1387 * PostQuitMessage() returns immediately. The exitCode parameter
1388 * specifies an application-defined exit code, which appears in the
1389 * _wParam_ parameter of the WM_QUIT message posted to the application.
1395 void WINAPI PostQuitMessage( INT exitCode )
1397 MESSAGEQUEUE *queue;
1399 if (!(queue = QUEUE_Lock( GetFastQueue16() ))) return;
1400 EnterCriticalSection( &queue->cSection );
1401 queue->wPostQMsg = TRUE;
1402 queue->wExitCode = (WORD)exitCode;
1403 LeaveCriticalSection( &queue->cSection );
1404 QUEUE_Unlock( queue );
1408 /***********************************************************************
1409 * GetWindowTask (USER.224)
1411 HTASK16 WINAPI GetWindowTask16( HWND16 hwnd )
1414 WND *wndPtr = WIN_FindWndPtr( hwnd );
1416 if (!wndPtr) return 0;
1417 retvalue = QUEUE_GetQueueTask( wndPtr->hmemTaskQ );
1418 WIN_ReleaseWndPtr(wndPtr);
1422 /***********************************************************************
1423 * GetWindowThreadProcessId (USER32.@)
1425 DWORD WINAPI GetWindowThreadProcessId( HWND hwnd, LPDWORD process )
1428 MESSAGEQUEUE *queue;
1430 WND *wndPtr = WIN_FindWndPtr( hwnd );
1431 if (!wndPtr) return 0;
1433 queue = QUEUE_Lock( wndPtr->hmemTaskQ );
1434 WIN_ReleaseWndPtr(wndPtr);
1436 if (!queue) return 0;
1438 if ( process ) *process = (DWORD)queue->teb->pid;
1439 retvalue = (DWORD)queue->teb->tid;
1441 QUEUE_Unlock( queue );
1446 /***********************************************************************
1447 * SetMessageQueue (USER.266)
1449 BOOL16 WINAPI SetMessageQueue16( INT16 size )
1451 return SetMessageQueue( size );
1455 /***********************************************************************
1456 * SetMessageQueue (USER32.@)
1458 BOOL WINAPI SetMessageQueue( INT size )
1460 /* now obsolete the message queue will be expanded dynamically
1463 /* access the queue to create it if it's not existing */
1469 /***********************************************************************
1470 * InitThreadInput (USER.409)
1472 HQUEUE16 WINAPI InitThreadInput16( WORD unknown, WORD flags )
1475 MESSAGEQUEUE *queuePtr;
1477 TEB *teb = NtCurrentTeb();
1482 hQueue = teb->queue;
1486 /* Create thread message queue */
1487 if( !(hQueue = QUEUE_CreateMsgQueue( TRUE )))
1489 ERR_(msg)("failed!\n");
1493 /* Link new queue into list */
1494 queuePtr = QUEUE_Lock( hQueue );
1495 queuePtr->teb = NtCurrentTeb();
1497 HeapLock( SystemHeap ); /* FIXME: a bit overkill */
1498 SetThreadQueue16( 0, hQueue );
1499 teb->queue = hQueue;
1501 queuePtr->next = hFirstQueue;
1502 hFirstQueue = hQueue;
1503 HeapUnlock( SystemHeap );
1505 QUEUE_Unlock( queuePtr );
1511 /***********************************************************************
1512 * GetQueueStatus (USER.334)
1514 DWORD WINAPI GetQueueStatus16( UINT16 flags )
1516 MESSAGEQUEUE *queue;
1519 if (!(queue = QUEUE_Lock( GetFastQueue16() ))) return 0;
1520 EnterCriticalSection( &queue->cSection );
1521 ret = MAKELONG( queue->changeBits, queue->wakeBits );
1522 queue->changeBits = 0;
1523 LeaveCriticalSection( &queue->cSection );
1524 QUEUE_Unlock( queue );
1526 return ret & MAKELONG( flags, flags );
1529 /***********************************************************************
1530 * GetQueueStatus (USER32.@)
1532 DWORD WINAPI GetQueueStatus( UINT flags )
1534 MESSAGEQUEUE *queue;
1537 if (!(queue = QUEUE_Lock( GetFastQueue16() ))) return 0;
1538 EnterCriticalSection( &queue->cSection );
1539 ret = MAKELONG( queue->changeBits, queue->wakeBits );
1540 queue->changeBits = 0;
1541 LeaveCriticalSection( &queue->cSection );
1542 QUEUE_Unlock( queue );
1544 return ret & MAKELONG( flags, flags );
1548 /***********************************************************************
1549 * GetInputState (USER.335)
1551 BOOL16 WINAPI GetInputState16(void)
1553 return GetInputState();
1556 /***********************************************************************
1557 * WaitForInputIdle (USER32.@)
1559 DWORD WINAPI WaitForInputIdle (HANDLE hProcess, DWORD dwTimeOut)
1561 DWORD cur_time, ret;
1562 HANDLE idle_event = -1;
1566 struct wait_input_idle_request *req = server_alloc_req( sizeof(*req), 0 );
1567 req->handle = hProcess;
1568 req->timeout = dwTimeOut;
1569 if (!(ret = server_call( REQ_WAIT_INPUT_IDLE ))) idle_event = req->event;
1572 if (ret) return 0xffffffff; /* error */
1573 if (!idle_event) return 0; /* no event to wait on */
1575 cur_time = GetTickCount();
1577 TRACE_(msg)("waiting for %x\n", idle_event );
1578 while ( dwTimeOut > GetTickCount() - cur_time || dwTimeOut == INFINITE )
1580 ret = MsgWaitForMultipleObjects ( 1, &idle_event, FALSE, dwTimeOut, QS_SENDMESSAGE );
1581 if ( ret == ( WAIT_OBJECT_0 + 1 ))
1583 MESSAGEQUEUE * queue;
1584 if (!(queue = QUEUE_Lock( GetFastQueue16() ))) return 0xFFFFFFFF;
1585 QUEUE_ReceiveMessage ( queue );
1586 QUEUE_Unlock ( queue );
1589 if ( ret == WAIT_TIMEOUT || ret == 0xFFFFFFFF )
1591 TRACE_(msg)("timeout or error\n");
1596 TRACE_(msg)("finished\n");
1601 return WAIT_TIMEOUT;
1604 /***********************************************************************
1605 * GetInputState (USER32.@)
1607 BOOL WINAPI GetInputState(void)
1609 MESSAGEQUEUE *queue;
1612 if (!(queue = QUEUE_Lock( GetFastQueue16() )))
1614 EnterCriticalSection( &queue->cSection );
1615 ret = queue->wakeBits & (QS_KEY | QS_MOUSEBUTTON);
1616 LeaveCriticalSection( &queue->cSection );
1617 QUEUE_Unlock( queue );
1622 /***********************************************************************
1623 * UserYield (USER.332)
1624 * UserYield16 (USER32.@)
1626 void WINAPI UserYield16(void)
1628 MESSAGEQUEUE *queue;
1630 /* Handle sent messages */
1631 queue = QUEUE_Lock( GetFastQueue16() );
1633 while ( queue && QUEUE_ReceiveMessage( queue ) )
1636 QUEUE_Unlock( queue );
1639 if ( THREAD_IsWin16( NtCurrentTeb() ) )
1644 /* Handle sent messages again */
1645 queue = QUEUE_Lock( GetFastQueue16() );
1647 while ( queue && QUEUE_ReceiveMessage( queue ) )
1650 QUEUE_Unlock( queue );
1653 /***********************************************************************
1654 * GetMessagePos (USER.119) (USER32.@)
1656 * The GetMessagePos() function returns a long value representing a
1657 * cursor position, in screen coordinates, when the last message
1658 * retrieved by the GetMessage() function occurs. The x-coordinate is
1659 * in the low-order word of the return value, the y-coordinate is in
1660 * the high-order word. The application can use the MAKEPOINT()
1661 * macro to obtain a POINT structure from the return value.
1663 * For the current cursor position, use GetCursorPos().
1667 * Cursor position of last message on success, zero on failure.
1674 DWORD WINAPI GetMessagePos(void)
1676 MESSAGEQUEUE *queue;
1679 if (!(queue = QUEUE_Lock( GetFastQueue16() ))) return 0;
1680 ret = queue->GetMessagePosVal;
1681 QUEUE_Unlock( queue );
1687 /***********************************************************************
1688 * GetMessageTime (USER.120) (USER32.@)
1690 * GetMessageTime() returns the message time for the last message
1691 * retrieved by the function. The time is measured in milliseconds with
1692 * the same offset as GetTickCount().
1694 * Since the tick count wraps, this is only useful for moderately short
1695 * relative time comparisons.
1699 * Time of last message on success, zero on failure.
1706 LONG WINAPI GetMessageTime(void)
1708 MESSAGEQUEUE *queue;
1711 if (!(queue = QUEUE_Lock( GetFastQueue16() ))) return 0;
1712 ret = queue->GetMessageTimeVal;
1713 QUEUE_Unlock( queue );
1719 /***********************************************************************
1720 * GetMessageExtraInfo (USER.288) (USER32.@)
1722 LONG WINAPI GetMessageExtraInfo(void)
1724 MESSAGEQUEUE *queue;
1727 if (!(queue = QUEUE_Lock( GetFastQueue16() ))) return 0;
1728 ret = queue->GetMessageExtraInfoVal;
1729 QUEUE_Unlock( queue );
1735 /**********************************************************************
1736 * AttachThreadInput (USER32.@) Attaches input of 1 thread to other
1738 * Attaches the input processing mechanism of one thread to that of
1746 * 1. Reset the Key State (currenly per thread key state is not maintained)
1748 BOOL WINAPI AttachThreadInput(
1749 DWORD idAttach, /* [in] Thread to attach */
1750 DWORD idAttachTo, /* [in] Thread to attach to */
1751 BOOL fAttach) /* [in] Attach or detach */
1753 MESSAGEQUEUE *pSrcMsgQ = 0, *pTgtMsgQ = 0;
1756 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1758 /* A thread cannot attach to itself */
1759 if ( idAttach == idAttachTo )
1762 /* According to the docs this method should fail if a
1763 * "Journal record" hook is installed. (attaches all input queues together)
1765 if ( HOOK_IsHooked( WH_JOURNALRECORD ) )
1768 /* Retrieve message queues corresponding to the thread id's */
1769 pTgtMsgQ = QUEUE_Lock( GetThreadQueue16( idAttach ) );
1770 pSrcMsgQ = QUEUE_Lock( GetThreadQueue16( idAttachTo ) );
1772 /* Ensure we have message queues and that Src and Tgt threads
1773 * are not system threads.
1775 if ( !pSrcMsgQ || !pTgtMsgQ || !pSrcMsgQ->pQData || !pTgtMsgQ->pQData )
1778 if (fAttach) /* Attach threads */
1780 /* Only attach if currently detached */
1781 if ( pTgtMsgQ->pQData != pSrcMsgQ->pQData )
1783 /* First release the target threads perQData */
1784 PERQDATA_Release( pTgtMsgQ->pQData );
1786 /* Share a reference to the source threads perQDATA */
1787 PERQDATA_Addref( pSrcMsgQ->pQData );
1788 pTgtMsgQ->pQData = pSrcMsgQ->pQData;
1791 else /* Detach threads */
1793 /* Only detach if currently attached */
1794 if ( pTgtMsgQ->pQData == pSrcMsgQ->pQData )
1796 /* First release the target threads perQData */
1797 PERQDATA_Release( pTgtMsgQ->pQData );
1799 /* Give the target thread its own private perQDATA once more */
1800 pTgtMsgQ->pQData = PERQDATA_CreateInstance();
1804 /* TODO: Reset the Key State */
1806 bRet = 1; /* Success */
1810 /* Unlock the queues before returning */
1812 QUEUE_Unlock( pSrcMsgQ );
1814 QUEUE_Unlock( pTgtMsgQ );