1 /* * Message queues related functions
3 * Copyright 1993, 1994 Alexandre Julliard
8 #include "wine/winbase16.h"
9 #include "wine/winuser16.h"
16 #include "clipboard.h"
22 #include "debugtools.h"
25 DECLARE_DEBUG_CHANNEL(msg
)
26 DECLARE_DEBUG_CHANNEL(sendmsg
)
28 #define MAX_QUEUE_SIZE 120 /* Max. size of a message queue */
30 static HQUEUE16 hFirstQueue
= 0;
31 static HQUEUE16 hExitingQueue
= 0;
32 static HQUEUE16 hmemSysMsgQueue
= 0;
33 static MESSAGEQUEUE
*sysMsgQueue
= NULL
;
34 static PERQUEUEDATA
*pQDataWin16
= NULL
; /* Global perQData for Win16 tasks */
36 static MESSAGEQUEUE
*pMouseQueue
= NULL
; /* Queue for last mouse message */
37 static MESSAGEQUEUE
*pKbdQueue
= NULL
; /* Queue for last kbd message */
39 HQUEUE16 hCursorQueue
= 0;
40 HQUEUE16 hActiveQueue
= 0;
43 /***********************************************************************
44 * PERQDATA_CreateInstance
46 * Creates an instance of a reference counted PERQUEUEDATA element
47 * for the message queue. perQData is stored globally for 16 bit tasks.
49 * Note: We don't implement perQdata exactly the same way Windows does.
50 * Each perQData element is reference counted since it may be potentially
51 * shared by multiple message Queues (via AttachThreadInput).
52 * We only store the current values for Active, Capture and focus windows
55 PERQUEUEDATA
* PERQDATA_CreateInstance( )
63 /* Share a single instance of perQData for all 16 bit tasks */
64 if ( ( bIsWin16
= THREAD_IsWin16( NtCurrentTeb() ) ) )
66 /* If previously allocated, just bump up ref count */
69 PERQDATA_Addref( pQDataWin16
);
74 /* Allocate PERQUEUEDATA from the system heap */
75 if (!( pQData
= (PERQUEUEDATA
*) HeapAlloc( SystemHeap
, 0,
76 sizeof(PERQUEUEDATA
) ) ))
80 pQData
->hWndCapture
= pQData
->hWndFocus
= pQData
->hWndActive
= 0;
81 pQData
->ulRefCount
= 1;
82 pQData
->nCaptureHT
= HTCLIENT
;
84 /* Note: We have an independent critical section for the per queue data
85 * since this may be shared by different threads. see AttachThreadInput()
87 InitializeCriticalSection( &pQData
->cSection
);
88 /* FIXME: not all per queue data critical sections should be global */
89 MakeCriticalSectionGlobal( &pQData
->cSection
);
91 /* Save perQData globally for 16 bit tasks */
99 /***********************************************************************
102 * Increment reference count for the PERQUEUEDATA instance
103 * Returns reference count for debugging purposes
105 ULONG
PERQDATA_Addref( PERQUEUEDATA
*pQData
)
107 assert(pQData
!= 0 );
108 TRACE_(msg
)("(): current refcount %lu ...\n", pQData
->ulRefCount
);
110 EnterCriticalSection( &pQData
->cSection
);
111 ++pQData
->ulRefCount
;
112 LeaveCriticalSection( &pQData
->cSection
);
114 return pQData
->ulRefCount
;
118 /***********************************************************************
121 * Release a reference to a PERQUEUEDATA instance.
122 * Destroy the instance if no more references exist
123 * Returns reference count for debugging purposes
125 ULONG
PERQDATA_Release( PERQUEUEDATA
*pQData
)
127 assert(pQData
!= 0 );
128 TRACE_(msg
)("(): current refcount %lu ...\n",
129 (LONG
)pQData
->ulRefCount
);
131 EnterCriticalSection( &pQData
->cSection
);
132 if ( --pQData
->ulRefCount
== 0 )
134 LeaveCriticalSection( &pQData
->cSection
);
135 DeleteCriticalSection( &pQData
->cSection
);
137 TRACE_(msg
)("(): deleting PERQUEUEDATA instance ...\n" );
139 /* Deleting our global 16 bit perQData? */
140 if ( pQData
== pQDataWin16
)
143 /* Free the PERQUEUEDATA instance */
144 HeapFree( SystemHeap
, 0, pQData
);
148 LeaveCriticalSection( &pQData
->cSection
);
150 return pQData
->ulRefCount
;
154 /***********************************************************************
155 * PERQDATA_GetFocusWnd
157 * Get the focus hwnd member in a threadsafe manner
159 HWND
PERQDATA_GetFocusWnd( PERQUEUEDATA
*pQData
)
162 assert(pQData
!= 0 );
164 EnterCriticalSection( &pQData
->cSection
);
165 hWndFocus
= pQData
->hWndFocus
;
166 LeaveCriticalSection( &pQData
->cSection
);
172 /***********************************************************************
173 * PERQDATA_SetFocusWnd
175 * Set the focus hwnd member in a threadsafe manner
177 HWND
PERQDATA_SetFocusWnd( PERQUEUEDATA
*pQData
, HWND hWndFocus
)
180 assert(pQData
!= 0 );
182 EnterCriticalSection( &pQData
->cSection
);
183 hWndFocusPrv
= pQData
->hWndFocus
;
184 pQData
->hWndFocus
= hWndFocus
;
185 LeaveCriticalSection( &pQData
->cSection
);
191 /***********************************************************************
192 * PERQDATA_GetActiveWnd
194 * Get the active hwnd member in a threadsafe manner
196 HWND
PERQDATA_GetActiveWnd( PERQUEUEDATA
*pQData
)
199 assert(pQData
!= 0 );
201 EnterCriticalSection( &pQData
->cSection
);
202 hWndActive
= pQData
->hWndActive
;
203 LeaveCriticalSection( &pQData
->cSection
);
209 /***********************************************************************
210 * PERQDATA_SetActiveWnd
212 * Set the active focus hwnd member in a threadsafe manner
214 HWND
PERQDATA_SetActiveWnd( PERQUEUEDATA
*pQData
, HWND hWndActive
)
217 assert(pQData
!= 0 );
219 EnterCriticalSection( &pQData
->cSection
);
220 hWndActivePrv
= pQData
->hWndActive
;
221 pQData
->hWndActive
= hWndActive
;
222 LeaveCriticalSection( &pQData
->cSection
);
224 return hWndActivePrv
;
228 /***********************************************************************
229 * PERQDATA_GetCaptureWnd
231 * Get the capture hwnd member in a threadsafe manner
233 HWND
PERQDATA_GetCaptureWnd( PERQUEUEDATA
*pQData
)
236 assert(pQData
!= 0 );
238 EnterCriticalSection( &pQData
->cSection
);
239 hWndCapture
= pQData
->hWndCapture
;
240 LeaveCriticalSection( &pQData
->cSection
);
246 /***********************************************************************
247 * PERQDATA_SetCaptureWnd
249 * Set the capture hwnd member in a threadsafe manner
251 HWND
PERQDATA_SetCaptureWnd( PERQUEUEDATA
*pQData
, HWND hWndCapture
)
254 assert(pQData
!= 0 );
256 EnterCriticalSection( &pQData
->cSection
);
257 hWndCapturePrv
= pQData
->hWndCapture
;
258 pQData
->hWndCapture
= hWndCapture
;
259 LeaveCriticalSection( &pQData
->cSection
);
261 return hWndCapturePrv
;
265 /***********************************************************************
266 * PERQDATA_GetCaptureInfo
268 * Get the capture info member in a threadsafe manner
270 INT16
PERQDATA_GetCaptureInfo( PERQUEUEDATA
*pQData
)
273 assert(pQData
!= 0 );
275 EnterCriticalSection( &pQData
->cSection
);
276 nCaptureHT
= pQData
->nCaptureHT
;
277 LeaveCriticalSection( &pQData
->cSection
);
283 /***********************************************************************
284 * PERQDATA_SetCaptureInfo
286 * Set the capture info member in a threadsafe manner
288 INT16
PERQDATA_SetCaptureInfo( PERQUEUEDATA
*pQData
, INT16 nCaptureHT
)
291 assert(pQData
!= 0 );
293 EnterCriticalSection( &pQData
->cSection
);
294 nCaptureHTPrv
= pQData
->nCaptureHT
;
295 pQData
->nCaptureHT
= nCaptureHT
;
296 LeaveCriticalSection( &pQData
->cSection
);
298 return nCaptureHTPrv
;
302 /***********************************************************************
305 * Function for getting a 32 bit pointer on queue strcture. For thread
306 * safeness programmers should use this function instead of GlobalLock to
307 * retrieve a pointer on the structure. QUEUE_Unlock should also be called
308 * when access to the queue structure is not required anymore.
310 MESSAGEQUEUE
*QUEUE_Lock( HQUEUE16 hQueue
)
314 HeapLock( SystemHeap
); /* FIXME: a bit overkill */
315 queue
= GlobalLock16( hQueue
);
316 if ( !queue
|| (queue
->magic
!= QUEUE_MAGIC
) )
318 HeapUnlock( SystemHeap
);
323 HeapUnlock( SystemHeap
);
328 /***********************************************************************
331 * Use with QUEUE_Lock to get a thread safe access to message queue
334 void QUEUE_Unlock( MESSAGEQUEUE
*queue
)
338 HeapLock( SystemHeap
); /* FIXME: a bit overkill */
340 if ( --queue
->lockCount
== 0 )
342 DeleteCriticalSection ( &queue
->cSection
);
344 CloseHandle( queue
->hEvent
);
345 GlobalFree16( queue
->self
);
348 HeapUnlock( SystemHeap
);
353 /***********************************************************************
356 void QUEUE_DumpQueue( HQUEUE16 hQueue
)
360 if (!(pq
= (MESSAGEQUEUE
*) QUEUE_Lock( hQueue
)) )
362 WARN_(msg
)("%04x is not a queue handle\n", hQueue
);
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"
378 pq
->next
, pq
->teb
, pq
->firstMsg
, pq
->smWaiting
, pq
->lastMsg
,
379 pq
->smPending
, pq
->msgCount
, pq
->smProcessing
,
380 (unsigned)pq
->lockCount
, pq
->wWinVersion
,
381 pq
->wPaintCount
, pq
->wTimerCount
,
382 pq
->wakeBits
, pq
->wakeMask
, pq
->hCurHook
);
388 /***********************************************************************
391 void QUEUE_WalkQueues(void)
394 HQUEUE16 hQueue
= hFirstQueue
;
396 DPRINTF( "Queue Msgs Thread Task Module\n" );
399 MESSAGEQUEUE
*queue
= (MESSAGEQUEUE
*)QUEUE_Lock( hQueue
);
402 WARN_(msg
)("Bad queue handle %04x\n", hQueue
);
405 if (!GetModuleName16( queue
->teb
->process
->task
, module
, sizeof(module
)))
406 strcpy( module
, "???" );
407 DPRINTF( "%04x %4d %p %04x %s\n", hQueue
,queue
->msgCount
,
408 queue
->teb
, queue
->teb
->process
->task
, 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
)
442 MESSAGEQUEUE
* msgQueue
;
443 TDB
*pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
445 TRACE_(msg
)("(): Creating message queue...\n");
447 if (!(hQueue
= GlobalAlloc16( GMEM_FIXED
| GMEM_ZEROINIT
,
448 sizeof(MESSAGEQUEUE
) )))
451 msgQueue
= (MESSAGEQUEUE
*) GlobalLock16( hQueue
);
455 msgQueue
->self
= hQueue
;
456 msgQueue
->wakeBits
= msgQueue
->changeBits
= 0;
457 msgQueue
->wWinVersion
= pTask
? pTask
->version
: 0;
459 InitializeCriticalSection( &msgQueue
->cSection
);
460 MakeCriticalSectionGlobal( &msgQueue
->cSection
);
462 /* Create an Event object for waiting on message, used by win32 thread
464 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
466 msgQueue
->hEvent
= CreateEventA( NULL
, FALSE
, FALSE
, NULL
);
468 if (msgQueue
->hEvent
== 0)
470 WARN_(msg
)("CreateEvent32A is not able to create an event object");
473 msgQueue
->hEvent
= ConvertToGlobalHandle( msgQueue
->hEvent
);
476 msgQueue
->hEvent
= 0;
478 msgQueue
->lockCount
= 1;
479 msgQueue
->magic
= QUEUE_MAGIC
;
481 /* Create and initialize our per queue data */
482 msgQueue
->pQData
= bCreatePerQData
? PERQDATA_CreateInstance() : NULL
;
488 /***********************************************************************
491 * Try to reply to all pending sent messages on exit.
493 static void QUEUE_FlushMessages( MESSAGEQUEUE
*queue
)
496 MESSAGEQUEUE
*senderQ
= 0;
500 EnterCriticalSection( &queue
->cSection
);
502 /* empty the list of pending SendMessage waiting to be received */
503 while (queue
->smPending
)
505 smsg
= QUEUE_RemoveSMSG( queue
, SM_PENDING_LIST
, 0);
507 senderQ
= (MESSAGEQUEUE
*)QUEUE_Lock( smsg
->hSrcQueue
);
511 /* return 0, to unblock other thread */
513 smsg
->flags
|= SMSG_HAVE_RESULT
;
514 QUEUE_SetWakeBit( senderQ
, QS_SMRESULT
);
516 QUEUE_Unlock( senderQ
);
519 QUEUE_ClearWakeBit( queue
, QS_SENDMESSAGE
);
521 LeaveCriticalSection( &queue
->cSection
);
526 /***********************************************************************
527 * QUEUE_DeleteMsgQueue
529 * Unlinks and deletes a message queue.
531 * Note: We need to mask asynchronous events to make sure PostMessage works
532 * even in the signal handler.
534 BOOL
QUEUE_DeleteMsgQueue( HQUEUE16 hQueue
)
536 MESSAGEQUEUE
* msgQueue
= (MESSAGEQUEUE
*)QUEUE_Lock(hQueue
);
539 TRACE_(msg
)("(): Deleting message queue %04x\n", hQueue
);
541 if (!hQueue
|| !msgQueue
)
543 WARN_(msg
)("invalid argument.\n");
549 if( hCursorQueue
== hQueue
) hCursorQueue
= 0;
550 if( hActiveQueue
== hQueue
) hActiveQueue
= 0;
552 /* flush sent messages */
553 QUEUE_FlushMessages( msgQueue
);
555 HeapLock( SystemHeap
); /* FIXME: a bit overkill */
557 /* Release per queue data if present */
558 if ( msgQueue
->pQData
)
560 PERQDATA_Release( msgQueue
->pQData
);
561 msgQueue
->pQData
= 0;
564 /* remove the message queue from the global link list */
565 pPrev
= &hFirstQueue
;
566 while (*pPrev
&& (*pPrev
!= hQueue
))
568 MESSAGEQUEUE
*msgQ
= (MESSAGEQUEUE
*)GlobalLock16(*pPrev
);
571 if ( !msgQ
|| (msgQ
->magic
!= QUEUE_MAGIC
) )
573 /* HQUEUE link list is corrupted, try to exit gracefully */
574 WARN_(msg
)("HQUEUE link list corrupted!\n");
580 if (pPrev
&& *pPrev
) *pPrev
= msgQueue
->next
;
583 HeapUnlock( SystemHeap
);
585 /* free up resource used by MESSAGEQUEUE strcture */
586 msgQueue
->lockCount
--;
587 QUEUE_Unlock( msgQueue
);
593 /***********************************************************************
594 * QUEUE_CreateSysMsgQueue
596 * Create the system message queue, and set the double-click speed.
597 * Must be called only once.
599 BOOL
QUEUE_CreateSysMsgQueue( int size
)
601 /* Note: We dont need perQ data for the system message queue */
602 if (!(hmemSysMsgQueue
= QUEUE_CreateMsgQueue( FALSE
)))
605 sysMsgQueue
= (MESSAGEQUEUE
*) GlobalLock16( hmemSysMsgQueue
);
610 /***********************************************************************
613 MESSAGEQUEUE
*QUEUE_GetSysQueue(void)
619 /***********************************************************************
622 * See "Windows Internals", p.449
624 void QUEUE_SetWakeBit( MESSAGEQUEUE
*queue
, WORD bit
)
626 TRACE_(msg
)("queue = %04x (wm=%04x), bit = %04x\n",
627 queue
->self
, queue
->wakeMask
, bit
);
629 if (bit
& QS_MOUSE
) pMouseQueue
= queue
;
630 if (bit
& QS_KEY
) pKbdQueue
= queue
;
631 queue
->changeBits
|= bit
;
632 queue
->wakeBits
|= bit
;
633 if (queue
->wakeMask
& bit
)
637 /* Wake up thread waiting for message */
638 if ( THREAD_IsWin16( queue
->teb
) )
640 int iWndsLock
= WIN_SuspendWndsLock();
641 PostEvent16( queue
->teb
->process
->task
);
642 WIN_RestoreWndsLock( iWndsLock
);
646 SetEvent( queue
->hEvent
);
652 /***********************************************************************
655 void QUEUE_ClearWakeBit( MESSAGEQUEUE
*queue
, WORD bit
)
657 queue
->changeBits
&= ~bit
;
658 queue
->wakeBits
&= ~bit
;
662 /***********************************************************************
665 * See "Windows Internals", p.447
668 * 0 if exit with timeout
671 int QUEUE_WaitBits( WORD bits
, DWORD timeout
)
676 TRACE_(msg
)("q %04x waiting for %04x\n", GetFastQueue16(), bits
);
678 if ( THREAD_IsWin16( NtCurrentTeb() ) && (timeout
!= INFINITE
) )
679 curTime
= GetTickCount();
681 if (!(queue
= (MESSAGEQUEUE
*)QUEUE_Lock( GetFastQueue16() ))) return 0;
685 if (queue
->changeBits
& bits
)
687 /* One of the bits is set; we can return */
689 QUEUE_Unlock( queue
);
692 if (queue
->wakeBits
& QS_SENDMESSAGE
)
694 /* Process the sent message immediately */
697 QUEUE_ReceiveMessage( queue
);
698 continue; /* nested sm crux */
701 queue
->wakeMask
= bits
| QS_SENDMESSAGE
;
702 if(queue
->changeBits
& bits
)
707 TRACE_(msg
)("%04x) wakeMask is %04x, waiting\n", queue
->self
, queue
->wakeMask
);
709 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
714 if ( (bHasWin16Lock
= _ConfirmWin16Lock()) )
716 TRACE_(msg
)("bHasWin16Lock=TRUE\n");
717 ReleaseThunkLock( &dwlc
);
719 WaitForSingleObject( queue
->hEvent
, timeout
);
722 RestoreThunkLock( dwlc
);
727 if ( timeout
== INFINITE
)
728 WaitEvent16( 0 ); /* win 16 thread, use WaitEvent */
731 /* check for timeout, then give control to other tasks */
732 if (GetTickCount() - curTime
> timeout
)
735 QUEUE_Unlock( queue
);
736 return 0; /* exit with timeout */
745 /***********************************************************************
748 * This routine is called when a SMSG need to be added to one of the three
749 * SM list. (SM_PROCESSING_LIST, SM_PENDING_LIST, SM_WAITING_LIST)
751 BOOL
QUEUE_AddSMSG( MESSAGEQUEUE
*queue
, int list
, SMSG
*smsg
)
753 TRACE_(sendmsg
)("queue=%x, list=%d, smsg=%p msg=%s\n", queue
->self
, list
,
754 smsg
, SPY_GetMsgName(smsg
->msg
));
758 case SM_PROCESSING_LIST
:
759 /* don't need to be thread safe, only accessed by the
760 thread associated with the sender queue */
761 smsg
->nextProcessing
= queue
->smProcessing
;
762 queue
->smProcessing
= smsg
;
765 case SM_WAITING_LIST
:
766 /* don't need to be thread safe, only accessed by the
767 thread associated with the receiver queue */
768 smsg
->nextWaiting
= queue
->smWaiting
;
769 queue
->smWaiting
= smsg
;
772 case SM_PENDING_LIST
:
774 /* make it thread safe, could be accessed by the sender and
778 EnterCriticalSection( &queue
->cSection
);
779 smsg
->nextPending
= NULL
;
780 prev
= &queue
->smPending
;
782 prev
= &(*prev
)->nextPending
;
784 LeaveCriticalSection( &queue
->cSection
);
786 QUEUE_SetWakeBit( queue
, QS_SENDMESSAGE
);
791 WARN_(sendmsg
)("Invalid list: %d", list
);
799 /***********************************************************************
802 * This routine is called when a SMSG need to be remove from one of the three
803 * SM list. (SM_PROCESSING_LIST, SM_PENDING_LIST, SM_WAITING_LIST)
804 * If smsg == 0, remove the first smsg from the specified list
806 SMSG
*QUEUE_RemoveSMSG( MESSAGEQUEUE
*queue
, int list
, SMSG
*smsg
)
811 case SM_PROCESSING_LIST
:
812 /* don't need to be thread safe, only accessed by the
813 thread associated with the sender queue */
815 /* if smsg is equal to null, it means the first in the list */
817 smsg
= queue
->smProcessing
;
819 TRACE_(sendmsg
)("queue=%x, list=%d, smsg=%p msg=%s\n", queue
->self
, list
,
820 smsg
, SPY_GetMsgName(smsg
->msg
));
821 /* In fact SM_PROCESSING_LIST is a stack, and smsg
822 should be always at the top of the list */
823 if ( (smsg
!= queue
->smProcessing
) || !queue
->smProcessing
)
825 ERR_(sendmsg
)("smsg not at the top of Processing list, smsg=0x%p queue=0x%p", smsg
, queue
);
830 queue
->smProcessing
= smsg
->nextProcessing
;
831 smsg
->nextProcessing
= 0;
835 case SM_WAITING_LIST
:
836 /* don't need to be thread safe, only accessed by the
837 thread associated with the receiver queue */
839 /* if smsg is equal to null, it means the first in the list */
841 smsg
= queue
->smWaiting
;
843 TRACE_(sendmsg
)("queue=%x, list=%d, smsg=%p msg=%s\n", queue
->self
, list
,
844 smsg
, SPY_GetMsgName(smsg
->msg
));
845 /* In fact SM_WAITING_LIST is a stack, and smsg
846 should be always at the top of the list */
847 if ( (smsg
!= queue
->smWaiting
) || !queue
->smWaiting
)
849 ERR_(sendmsg
)("smsg not at the top of Waiting list, smsg=0x%p queue=0x%p", smsg
, queue
);
854 queue
->smWaiting
= smsg
->nextWaiting
;
855 smsg
->nextWaiting
= 0;
859 case SM_PENDING_LIST
:
860 /* make it thread safe, could be accessed by the sender and
862 EnterCriticalSection( &queue
->cSection
);
864 if (!smsg
|| !queue
->smPending
)
865 smsg
= queue
->smPending
;
868 ERR_(sendmsg
)("should always remove the top one in Pending list, smsg=0x%p queue=0x%p", smsg
, queue
);
869 LeaveCriticalSection( &queue
->cSection
);
873 TRACE_(sendmsg
)("queue=%x, list=%d, smsg=%p msg=%s\n", queue
->self
, list
,
874 smsg
, SPY_GetMsgName(smsg
->msg
));
876 queue
->smPending
= smsg
->nextPending
;
877 smsg
->nextPending
= 0;
879 /* if no more SMSG in Pending list, clear QS_SENDMESSAGE flag */
880 if (!queue
->smPending
)
881 QUEUE_ClearWakeBit( queue
, QS_SENDMESSAGE
);
883 LeaveCriticalSection( &queue
->cSection
);
887 WARN_(sendmsg
)("Invalid list: %d", list
);
895 /***********************************************************************
896 * QUEUE_ReceiveMessage
898 * This routine is called when a sent message is waiting for the queue.
900 void QUEUE_ReceiveMessage( MESSAGEQUEUE
*queue
)
904 MESSAGEQUEUE
*senderQ
;
906 TRACE_(sendmsg
)("queue %04x\n", queue
->self
);
908 if ( !(queue
->wakeBits
& QS_SENDMESSAGE
) && queue
->smPending
)
910 TRACE_(sendmsg
)("\trcm: nothing to do\n");
914 /* remove smsg on the top of the pending list and put it in the processing list */
915 smsg
= QUEUE_RemoveSMSG(queue
, SM_PENDING_LIST
, 0);
916 QUEUE_AddSMSG(queue
, SM_WAITING_LIST
, smsg
);
918 TRACE_(sendmsg
)("RM: %s [%04x] (%04x -> %04x)\n",
919 SPY_GetMsgName(smsg
->msg
), smsg
->msg
, smsg
->hSrcQueue
, smsg
->hDstQueue
);
921 if (IsWindow( smsg
->hWnd
))
923 WND
*wndPtr
= WIN_FindWndPtr( smsg
->hWnd
);
924 DWORD extraInfo
= queue
->GetMessageExtraInfoVal
; /* save ExtraInfo */
926 /* use sender queue extra info value while calling the window proc */
927 senderQ
= (MESSAGEQUEUE
*)QUEUE_Lock( smsg
->hSrcQueue
);
930 queue
->GetMessageExtraInfoVal
= senderQ
->GetMessageExtraInfoVal
;
931 QUEUE_Unlock( senderQ
);
934 /* call the right version of CallWindowProcXX */
935 if (smsg
->flags
& SMSG_WIN32
)
937 TRACE_(sendmsg
)("\trcm: msg is Win32\n" );
938 if (smsg
->flags
& SMSG_UNICODE
)
939 result
= CallWindowProcW( wndPtr
->winproc
,
940 smsg
->hWnd
, smsg
->msg
,
941 smsg
->wParam
, smsg
->lParam
);
943 result
= CallWindowProcA( wndPtr
->winproc
,
944 smsg
->hWnd
, smsg
->msg
,
945 smsg
->wParam
, smsg
->lParam
);
947 else /* Win16 message */
948 result
= CallWindowProc16( (WNDPROC16
)wndPtr
->winproc
,
951 LOWORD (smsg
->wParam
),
954 queue
->GetMessageExtraInfoVal
= extraInfo
; /* Restore extra info */
955 WIN_ReleaseWndPtr(wndPtr
);
956 TRACE_(sendmsg
)("result = %08x\n", (unsigned)result
);
958 else WARN_(sendmsg
)("\trcm: bad hWnd\n");
961 /* set SMSG_SENDING_REPLY flag to tell ReplyMessage16, it's not
963 smsg
->flags
|= SMSG_SENDING_REPLY
;
964 ReplyMessage( result
);
966 TRACE_(sendmsg
)("done! \n" );
971 /***********************************************************************
974 * Add a message to the queue. Return FALSE if queue is full.
976 BOOL
QUEUE_AddMsg( HQUEUE16 hQueue
, MSG
*msg
, DWORD extraInfo
)
978 MESSAGEQUEUE
*msgQueue
;
982 if (!(msgQueue
= (MESSAGEQUEUE
*)QUEUE_Lock( hQueue
))) return FALSE
;
984 /* allocate new message in global heap for now */
985 if (!(qmsg
= (QMSG
*) HeapAlloc( SystemHeap
, 0, sizeof(QMSG
) ) ))
987 QUEUE_Unlock( msgQueue
);
991 EnterCriticalSection( &msgQueue
->cSection
);
995 qmsg
->extraInfo
= extraInfo
;
997 /* insert the message in the link list */
999 qmsg
->prevMsg
= msgQueue
->lastMsg
;
1001 if (msgQueue
->lastMsg
)
1002 msgQueue
->lastMsg
->nextMsg
= qmsg
;
1004 /* update first and last anchor in message queue */
1005 msgQueue
->lastMsg
= qmsg
;
1006 if (!msgQueue
->firstMsg
)
1007 msgQueue
->firstMsg
= qmsg
;
1009 msgQueue
->msgCount
++;
1011 LeaveCriticalSection( &msgQueue
->cSection
);
1013 QUEUE_SetWakeBit( msgQueue
, QS_POSTMESSAGE
);
1014 QUEUE_Unlock( msgQueue
);
1021 /***********************************************************************
1024 * Find a message matching the given parameters. Return -1 if none available.
1026 QMSG
* QUEUE_FindMsg( MESSAGEQUEUE
* msgQueue
, HWND hwnd
, int first
, int last
)
1030 EnterCriticalSection( &msgQueue
->cSection
);
1032 if (!msgQueue
->msgCount
)
1034 else if (!hwnd
&& !first
&& !last
)
1035 qmsg
= msgQueue
->firstMsg
;
1038 /* look in linked list for message matching first and last criteria */
1039 for (qmsg
= msgQueue
->firstMsg
; qmsg
; qmsg
= qmsg
->nextMsg
)
1041 MSG
*msg
= &(qmsg
->msg
);
1043 if (!hwnd
|| (msg
->hwnd
== hwnd
))
1045 if (!first
&& !last
)
1046 break; /* found it */
1048 if ((msg
->message
>= first
) && (!last
|| (msg
->message
<= last
)))
1049 break; /* found it */
1054 LeaveCriticalSection( &msgQueue
->cSection
);
1061 /***********************************************************************
1064 * Remove a message from the queue (pos must be a valid position).
1066 void QUEUE_RemoveMsg( MESSAGEQUEUE
* msgQueue
, QMSG
*qmsg
)
1068 EnterCriticalSection( &msgQueue
->cSection
);
1070 /* set the linked list */
1072 qmsg
->prevMsg
->nextMsg
= qmsg
->nextMsg
;
1075 qmsg
->nextMsg
->prevMsg
= qmsg
->prevMsg
;
1077 if (msgQueue
->firstMsg
== qmsg
)
1078 msgQueue
->firstMsg
= qmsg
->nextMsg
;
1080 if (msgQueue
->lastMsg
== qmsg
)
1081 msgQueue
->lastMsg
= qmsg
->prevMsg
;
1083 /* deallocate the memory for the message */
1084 HeapFree( SystemHeap
, 0, qmsg
);
1086 msgQueue
->msgCount
--;
1087 if (!msgQueue
->msgCount
) msgQueue
->wakeBits
&= ~QS_POSTMESSAGE
;
1089 LeaveCriticalSection( &msgQueue
->cSection
);
1093 /***********************************************************************
1096 * Wake a queue upon reception of a hardware event.
1098 static void QUEUE_WakeSomeone( UINT message
)
1103 HQUEUE16 hQueue
= 0;
1104 MESSAGEQUEUE
*queue
= NULL
;
1107 hQueue
= hCursorQueue
;
1109 if( (message
>= WM_KEYFIRST
) && (message
<= WM_KEYLAST
) )
1113 hQueue
= hActiveQueue
;
1117 wakeBit
= (message
== WM_MOUSEMOVE
) ? QS_MOUSEMOVE
: QS_MOUSEBUTTON
;
1118 if( (hwnd
= GetCapture()) )
1119 if( (wndPtr
= WIN_FindWndPtr( hwnd
)) )
1121 hQueue
= wndPtr
->hmemTaskQ
;
1122 WIN_ReleaseWndPtr(wndPtr
);
1126 if( (hwnd
= GetSysModalWindow16()) )
1128 if( (wndPtr
= WIN_FindWndPtr( hwnd
)) )
1130 hQueue
= wndPtr
->hmemTaskQ
;
1131 WIN_ReleaseWndPtr(wndPtr
);
1136 queue
= QUEUE_Lock( hQueue
);
1140 queue
= QUEUE_Lock( hFirstQueue
);
1143 if (queue
->wakeMask
& wakeBit
) break;
1145 QUEUE_Unlock(queue
);
1146 queue
= QUEUE_Lock( queue
->next
);
1150 WARN_(msg
)("couldn't find queue\n");
1155 QUEUE_SetWakeBit( queue
, wakeBit
);
1157 QUEUE_Unlock( queue
);
1161 /***********************************************************************
1164 * Add an event to the system message queue.
1165 * Note: the position is relative to the desktop window.
1167 void hardware_event( UINT message
, WPARAM wParam
, LPARAM lParam
,
1168 int xPos
, int yPos
, DWORD time
, DWORD extraInfo
)
1174 if (!sysMsgQueue
) return;
1176 EnterCriticalSection( &sysMsgQueue
->cSection
);
1178 /* Merge with previous event if possible */
1179 qmsg
= sysMsgQueue
->lastMsg
;
1181 if ((message
== WM_MOUSEMOVE
) && sysMsgQueue
->lastMsg
)
1183 msg
= &(sysMsgQueue
->lastMsg
->msg
);
1185 if ((msg
->message
== message
) && (msg
->wParam
== wParam
))
1188 qmsg
= sysMsgQueue
->lastMsg
;
1195 /* Should I limit the number of message in
1196 the system message queue??? */
1198 /* Don't merge allocate a new msg in the global heap */
1200 if (!(qmsg
= (QMSG
*) HeapAlloc( SystemHeap
, 0, sizeof(QMSG
) ) ))
1202 LeaveCriticalSection( &sysMsgQueue
->cSection
);
1206 /* put message at the end of the linked list */
1208 qmsg
->prevMsg
= sysMsgQueue
->lastMsg
;
1210 if (sysMsgQueue
->lastMsg
)
1211 sysMsgQueue
->lastMsg
->nextMsg
= qmsg
;
1213 /* set last and first anchor index in system message queue */
1214 sysMsgQueue
->lastMsg
= qmsg
;
1215 if (!sysMsgQueue
->firstMsg
)
1216 sysMsgQueue
->firstMsg
= qmsg
;
1218 sysMsgQueue
->msgCount
++;
1224 msg
->message
= message
;
1225 msg
->wParam
= wParam
;
1226 msg
->lParam
= lParam
;
1230 qmsg
->extraInfo
= extraInfo
;
1232 LeaveCriticalSection( &sysMsgQueue
->cSection
);
1234 QUEUE_WakeSomeone( message
);
1238 /***********************************************************************
1239 * QUEUE_GetQueueTask
1241 HTASK16
QUEUE_GetQueueTask( HQUEUE16 hQueue
)
1245 MESSAGEQUEUE
*queue
= QUEUE_Lock( hQueue
);
1249 hTask
= queue
->teb
->process
->task
;
1250 QUEUE_Unlock( queue
);
1258 /***********************************************************************
1259 * QUEUE_IncPaintCount
1261 void QUEUE_IncPaintCount( HQUEUE16 hQueue
)
1263 MESSAGEQUEUE
*queue
;
1265 if (!(queue
= (MESSAGEQUEUE
*)QUEUE_Lock( hQueue
))) return;
1266 queue
->wPaintCount
++;
1267 QUEUE_SetWakeBit( queue
, QS_PAINT
);
1268 QUEUE_Unlock( queue
);
1272 /***********************************************************************
1273 * QUEUE_DecPaintCount
1275 void QUEUE_DecPaintCount( HQUEUE16 hQueue
)
1277 MESSAGEQUEUE
*queue
;
1279 if (!(queue
= (MESSAGEQUEUE
*)QUEUE_Lock( hQueue
))) return;
1280 queue
->wPaintCount
--;
1281 if (!queue
->wPaintCount
) queue
->wakeBits
&= ~QS_PAINT
;
1282 QUEUE_Unlock( queue
);
1286 /***********************************************************************
1287 * QUEUE_IncTimerCount
1289 void QUEUE_IncTimerCount( HQUEUE16 hQueue
)
1291 MESSAGEQUEUE
*queue
;
1293 if (!(queue
= (MESSAGEQUEUE
*)QUEUE_Lock( hQueue
))) return;
1294 queue
->wTimerCount
++;
1295 QUEUE_SetWakeBit( queue
, QS_TIMER
);
1296 QUEUE_Unlock( queue
);
1300 /***********************************************************************
1301 * QUEUE_DecTimerCount
1303 void QUEUE_DecTimerCount( HQUEUE16 hQueue
)
1305 MESSAGEQUEUE
*queue
;
1307 if (!(queue
= (MESSAGEQUEUE
*)QUEUE_Lock( hQueue
))) return;
1308 queue
->wTimerCount
--;
1309 if (!queue
->wTimerCount
) queue
->wakeBits
&= ~QS_TIMER
;
1310 QUEUE_Unlock( queue
);
1314 /***********************************************************************
1315 * PostQuitMessage16 (USER.6)
1317 void WINAPI
PostQuitMessage16( INT16 exitCode
)
1319 PostQuitMessage( exitCode
);
1323 /***********************************************************************
1324 * PostQuitMessage32 (USER32.421)
1326 * PostQuitMessage() posts a message to the system requesting an
1327 * application to terminate execution. As a result of this function,
1328 * the WM_QUIT message is posted to the application, and
1329 * PostQuitMessage() returns immediately. The exitCode parameter
1330 * specifies an application-defined exit code, which appears in the
1331 * _wParam_ parameter of the WM_QUIT message posted to the application.
1337 void WINAPI
PostQuitMessage( INT exitCode
)
1339 MESSAGEQUEUE
*queue
;
1341 if (!(queue
= (MESSAGEQUEUE
*)QUEUE_Lock( GetFastQueue16() ))) return;
1342 queue
->wPostQMsg
= TRUE
;
1343 queue
->wExitCode
= (WORD
)exitCode
;
1344 QUEUE_Unlock( queue
);
1348 /***********************************************************************
1349 * GetWindowTask16 (USER.224)
1351 HTASK16 WINAPI
GetWindowTask16( HWND16 hwnd
)
1354 WND
*wndPtr
= WIN_FindWndPtr( hwnd
);
1356 if (!wndPtr
) return 0;
1357 retvalue
= QUEUE_GetQueueTask( wndPtr
->hmemTaskQ
);
1358 WIN_ReleaseWndPtr(wndPtr
);
1362 /***********************************************************************
1363 * GetWindowThreadProcessId (USER32.313)
1365 DWORD WINAPI
GetWindowThreadProcessId( HWND hwnd
, LPDWORD process
)
1368 MESSAGEQUEUE
*queue
;
1370 WND
*wndPtr
= WIN_FindWndPtr( hwnd
);
1371 if (!wndPtr
) return 0;
1373 queue
= QUEUE_Lock( wndPtr
->hmemTaskQ
);
1374 WIN_ReleaseWndPtr(wndPtr
);
1376 if (!queue
) return 0;
1378 if ( process
) *process
= (DWORD
)queue
->teb
->process
->server_pid
;
1379 retvalue
= (DWORD
)queue
->teb
->tid
;
1381 QUEUE_Unlock( queue
);
1386 /***********************************************************************
1387 * SetMessageQueue16 (USER.266)
1389 BOOL16 WINAPI
SetMessageQueue16( INT16 size
)
1391 return SetMessageQueue( size
);
1395 /***********************************************************************
1396 * SetMessageQueue32 (USER32.494)
1398 BOOL WINAPI
SetMessageQueue( INT size
)
1400 /* now obsolete the message queue will be expanded dynamically
1403 /* access the queue to create it if it's not existing */
1409 /***********************************************************************
1410 * InitThreadInput (USER.409)
1412 HQUEUE16 WINAPI
InitThreadInput16( WORD unknown
, WORD flags
)
1415 MESSAGEQUEUE
*queuePtr
;
1417 TEB
*teb
= NtCurrentTeb();
1422 hQueue
= teb
->queue
;
1426 /* Create thread message queue */
1427 if( !(hQueue
= QUEUE_CreateMsgQueue( TRUE
)))
1429 WARN_(msg
)("failed!\n");
1433 /* Link new queue into list */
1434 queuePtr
= (MESSAGEQUEUE
*)QUEUE_Lock( hQueue
);
1435 queuePtr
->teb
= NtCurrentTeb();
1437 HeapLock( SystemHeap
); /* FIXME: a bit overkill */
1438 SetThreadQueue16( 0, hQueue
);
1439 teb
->queue
= hQueue
;
1441 queuePtr
->next
= hFirstQueue
;
1442 hFirstQueue
= hQueue
;
1443 HeapUnlock( SystemHeap
);
1445 QUEUE_Unlock( queuePtr
);
1451 /***********************************************************************
1452 * GetQueueStatus16 (USER.334)
1454 DWORD WINAPI
GetQueueStatus16( UINT16 flags
)
1456 MESSAGEQUEUE
*queue
;
1459 if (!(queue
= (MESSAGEQUEUE
*)QUEUE_Lock( GetFastQueue16() ))) return 0;
1460 ret
= MAKELONG( queue
->changeBits
, queue
->wakeBits
);
1461 queue
->changeBits
= 0;
1462 QUEUE_Unlock( queue
);
1464 return ret
& MAKELONG( flags
, flags
);
1467 /***********************************************************************
1468 * GetQueueStatus32 (USER32.283)
1470 DWORD WINAPI
GetQueueStatus( UINT flags
)
1472 MESSAGEQUEUE
*queue
;
1475 if (!(queue
= (MESSAGEQUEUE
*)QUEUE_Lock( GetFastQueue16() ))) return 0;
1476 ret
= MAKELONG( queue
->changeBits
, queue
->wakeBits
);
1477 queue
->changeBits
= 0;
1478 QUEUE_Unlock( queue
);
1480 return ret
& MAKELONG( flags
, flags
);
1484 /***********************************************************************
1485 * GetInputState16 (USER.335)
1487 BOOL16 WINAPI
GetInputState16(void)
1489 return GetInputState();
1492 /***********************************************************************
1493 * WaitForInputIdle (USER32.577)
1495 DWORD WINAPI
WaitForInputIdle (HANDLE hProcess
, DWORD dwTimeOut
)
1497 FIXME_(msg
)("(hProcess=%d, dwTimeOut=%ld): stub\n", hProcess
, dwTimeOut
);
1499 return WAIT_TIMEOUT
;
1503 /***********************************************************************
1504 * GetInputState32 (USER32.244)
1506 BOOL WINAPI
GetInputState(void)
1508 MESSAGEQUEUE
*queue
;
1511 if (!(queue
= (MESSAGEQUEUE
*)QUEUE_Lock( GetFastQueue16() )))
1513 ret
= queue
->wakeBits
& (QS_KEY
| QS_MOUSEBUTTON
);
1514 QUEUE_Unlock( queue
);
1519 /***********************************************************************
1520 * UserYield (USER.332)
1522 void WINAPI
UserYield16(void)
1524 MESSAGEQUEUE
*queue
;
1526 /* Handle sent messages */
1527 queue
= (MESSAGEQUEUE
*)QUEUE_Lock( GetFastQueue16() );
1529 while (queue
&& (queue
->wakeBits
& QS_SENDMESSAGE
))
1530 QUEUE_ReceiveMessage( queue
);
1532 QUEUE_Unlock( queue
);
1535 if ( THREAD_IsWin16( NtCurrentTeb() ) )
1540 /* Handle sent messages again */
1541 queue
= (MESSAGEQUEUE
*)QUEUE_Lock( GetFastQueue16() );
1543 while (queue
&& (queue
->wakeBits
& QS_SENDMESSAGE
))
1544 QUEUE_ReceiveMessage( queue
);
1546 QUEUE_Unlock( queue
);
1549 /***********************************************************************
1550 * GetMessagePos (USER.119) (USER32.272)
1552 * The GetMessagePos() function returns a long value representing a
1553 * cursor position, in screen coordinates, when the last message
1554 * retrieved by the GetMessage() function occurs. The x-coordinate is
1555 * in the low-order word of the return value, the y-coordinate is in
1556 * the high-order word. The application can use the MAKEPOINT()
1557 * macro to obtain a POINT structure from the return value.
1559 * For the current cursor position, use GetCursorPos().
1563 * Cursor position of last message on success, zero on failure.
1570 DWORD WINAPI
GetMessagePos(void)
1572 MESSAGEQUEUE
*queue
;
1575 if (!(queue
= (MESSAGEQUEUE
*)QUEUE_Lock( GetFastQueue16() ))) return 0;
1576 ret
= queue
->GetMessagePosVal
;
1577 QUEUE_Unlock( queue
);
1583 /***********************************************************************
1584 * GetMessageTime (USER.120) (USER32.273)
1586 * GetMessageTime() returns the message time for the last message
1587 * retrieved by the function. The time is measured in milliseconds with
1588 * the same offset as GetTickCount().
1590 * Since the tick count wraps, this is only useful for moderately short
1591 * relative time comparisons.
1595 * Time of last message on success, zero on failure.
1602 LONG WINAPI
GetMessageTime(void)
1604 MESSAGEQUEUE
*queue
;
1607 if (!(queue
= (MESSAGEQUEUE
*)QUEUE_Lock( GetFastQueue16() ))) return 0;
1608 ret
= queue
->GetMessageTimeVal
;
1609 QUEUE_Unlock( queue
);
1615 /***********************************************************************
1616 * GetMessageExtraInfo (USER.288) (USER32.271)
1618 LONG WINAPI
GetMessageExtraInfo(void)
1620 MESSAGEQUEUE
*queue
;
1623 if (!(queue
= (MESSAGEQUEUE
*)QUEUE_Lock( GetFastQueue16() ))) return 0;
1624 ret
= queue
->GetMessageExtraInfoVal
;
1625 QUEUE_Unlock( queue
);