1 /* * Message queues related functions
3 * Copyright 1993, 1994 Alexandre Julliard
11 #include "wine/winbase16.h"
12 #include "wine/winuser16.h"
19 #include "clipboard.h"
24 #include "debugtools.h"
27 DECLARE_DEBUG_CHANNEL(msg
)
28 DECLARE_DEBUG_CHANNEL(sendmsg
)
30 #define MAX_QUEUE_SIZE 120 /* Max. size of a message queue */
32 static HQUEUE16 hFirstQueue
= 0;
33 static HQUEUE16 hExitingQueue
= 0;
34 static HQUEUE16 hmemSysMsgQueue
= 0;
35 static MESSAGEQUEUE
*sysMsgQueue
= NULL
;
36 static PERQUEUEDATA
*pQDataWin16
= NULL
; /* Global perQData for Win16 tasks */
38 static MESSAGEQUEUE
*pMouseQueue
= NULL
; /* Queue for last mouse message */
39 static MESSAGEQUEUE
*pKbdQueue
= NULL
; /* Queue for last kbd message */
41 HQUEUE16 hCursorQueue
= 0;
42 HQUEUE16 hActiveQueue
= 0;
45 /***********************************************************************
46 * PERQDATA_CreateInstance
48 * Creates an instance of a reference counted PERQUEUEDATA element
49 * for the message queue. perQData is stored globally for 16 bit tasks.
51 * Note: We don't implement perQdata exactly the same way Windows does.
52 * Each perQData element is reference counted since it may be potentially
53 * shared by multiple message Queues (via AttachThreadInput).
54 * We only store the current values for Active, Capture and focus windows
57 PERQUEUEDATA
* PERQDATA_CreateInstance( )
65 /* Share a single instance of perQData for all 16 bit tasks */
66 if ( ( bIsWin16
= THREAD_IsWin16( NtCurrentTeb() ) ) )
68 /* If previously allocated, just bump up ref count */
71 PERQDATA_Addref( pQDataWin16
);
76 /* Allocate PERQUEUEDATA from the system heap */
77 if (!( pQData
= (PERQUEUEDATA
*) HeapAlloc( SystemHeap
, 0,
78 sizeof(PERQUEUEDATA
) ) ))
82 pQData
->hWndCapture
= pQData
->hWndFocus
= pQData
->hWndActive
= 0;
83 pQData
->ulRefCount
= 1;
84 pQData
->nCaptureHT
= HTCLIENT
;
86 /* Note: We have an independent critical section for the per queue data
87 * since this may be shared by different threads. see AttachThreadInput()
89 InitializeCriticalSection( &pQData
->cSection
);
90 /* FIXME: not all per queue data critical sections should be global */
91 MakeCriticalSectionGlobal( &pQData
->cSection
);
93 /* Save perQData globally for 16 bit tasks */
101 /***********************************************************************
104 * Increment reference count for the PERQUEUEDATA instance
105 * Returns reference count for debugging purposes
107 ULONG
PERQDATA_Addref( PERQUEUEDATA
*pQData
)
109 assert(pQData
!= 0 );
110 TRACE_(msg
)("(): current refcount %lu ...\n", pQData
->ulRefCount
);
112 EnterCriticalSection( &pQData
->cSection
);
113 ++pQData
->ulRefCount
;
114 LeaveCriticalSection( &pQData
->cSection
);
116 return pQData
->ulRefCount
;
120 /***********************************************************************
123 * Release a reference to a PERQUEUEDATA instance.
124 * Destroy the instance if no more references exist
125 * Returns reference count for debugging purposes
127 ULONG
PERQDATA_Release( PERQUEUEDATA
*pQData
)
129 assert(pQData
!= 0 );
130 TRACE_(msg
)("(): current refcount %lu ...\n",
131 (LONG
)pQData
->ulRefCount
);
133 EnterCriticalSection( &pQData
->cSection
);
134 if ( --pQData
->ulRefCount
== 0 )
136 LeaveCriticalSection( &pQData
->cSection
);
137 DeleteCriticalSection( &pQData
->cSection
);
139 TRACE_(msg
)("(): deleting PERQUEUEDATA instance ...\n" );
141 /* Deleting our global 16 bit perQData? */
142 if ( pQData
== pQDataWin16
)
145 /* Free the PERQUEUEDATA instance */
146 HeapFree( SystemHeap
, 0, pQData
);
150 LeaveCriticalSection( &pQData
->cSection
);
152 return pQData
->ulRefCount
;
156 /***********************************************************************
157 * PERQDATA_GetFocusWnd
159 * Get the focus hwnd member in a threadsafe manner
161 HWND
PERQDATA_GetFocusWnd( PERQUEUEDATA
*pQData
)
164 assert(pQData
!= 0 );
166 EnterCriticalSection( &pQData
->cSection
);
167 hWndFocus
= pQData
->hWndFocus
;
168 LeaveCriticalSection( &pQData
->cSection
);
174 /***********************************************************************
175 * PERQDATA_SetFocusWnd
177 * Set the focus hwnd member in a threadsafe manner
179 HWND
PERQDATA_SetFocusWnd( PERQUEUEDATA
*pQData
, HWND hWndFocus
)
182 assert(pQData
!= 0 );
184 EnterCriticalSection( &pQData
->cSection
);
185 hWndFocusPrv
= pQData
->hWndFocus
;
186 pQData
->hWndFocus
= hWndFocus
;
187 LeaveCriticalSection( &pQData
->cSection
);
193 /***********************************************************************
194 * PERQDATA_GetActiveWnd
196 * Get the active hwnd member in a threadsafe manner
198 HWND
PERQDATA_GetActiveWnd( PERQUEUEDATA
*pQData
)
201 assert(pQData
!= 0 );
203 EnterCriticalSection( &pQData
->cSection
);
204 hWndActive
= pQData
->hWndActive
;
205 LeaveCriticalSection( &pQData
->cSection
);
211 /***********************************************************************
212 * PERQDATA_SetActiveWnd
214 * Set the active focus hwnd member in a threadsafe manner
216 HWND
PERQDATA_SetActiveWnd( PERQUEUEDATA
*pQData
, HWND hWndActive
)
219 assert(pQData
!= 0 );
221 EnterCriticalSection( &pQData
->cSection
);
222 hWndActivePrv
= pQData
->hWndActive
;
223 pQData
->hWndActive
= hWndActive
;
224 LeaveCriticalSection( &pQData
->cSection
);
226 return hWndActivePrv
;
230 /***********************************************************************
231 * PERQDATA_GetCaptureWnd
233 * Get the capture hwnd member in a threadsafe manner
235 HWND
PERQDATA_GetCaptureWnd( PERQUEUEDATA
*pQData
)
238 assert(pQData
!= 0 );
240 EnterCriticalSection( &pQData
->cSection
);
241 hWndCapture
= pQData
->hWndCapture
;
242 LeaveCriticalSection( &pQData
->cSection
);
248 /***********************************************************************
249 * PERQDATA_SetCaptureWnd
251 * Set the capture hwnd member in a threadsafe manner
253 HWND
PERQDATA_SetCaptureWnd( PERQUEUEDATA
*pQData
, HWND hWndCapture
)
256 assert(pQData
!= 0 );
258 EnterCriticalSection( &pQData
->cSection
);
259 hWndCapturePrv
= pQData
->hWndCapture
;
260 pQData
->hWndCapture
= hWndCapture
;
261 LeaveCriticalSection( &pQData
->cSection
);
263 return hWndCapturePrv
;
267 /***********************************************************************
268 * PERQDATA_GetCaptureInfo
270 * Get the capture info member in a threadsafe manner
272 INT16
PERQDATA_GetCaptureInfo( PERQUEUEDATA
*pQData
)
275 assert(pQData
!= 0 );
277 EnterCriticalSection( &pQData
->cSection
);
278 nCaptureHT
= pQData
->nCaptureHT
;
279 LeaveCriticalSection( &pQData
->cSection
);
285 /***********************************************************************
286 * PERQDATA_SetCaptureInfo
288 * Set the capture info member in a threadsafe manner
290 INT16
PERQDATA_SetCaptureInfo( PERQUEUEDATA
*pQData
, INT16 nCaptureHT
)
293 assert(pQData
!= 0 );
295 EnterCriticalSection( &pQData
->cSection
);
296 nCaptureHTPrv
= pQData
->nCaptureHT
;
297 pQData
->nCaptureHT
= nCaptureHT
;
298 LeaveCriticalSection( &pQData
->cSection
);
300 return nCaptureHTPrv
;
304 /***********************************************************************
307 * Function for getting a 32 bit pointer on queue strcture. For thread
308 * safeness programmers should use this function instead of GlobalLock to
309 * retrieve a pointer on the structure. QUEUE_Unlock should also be called
310 * when access to the queue structure is not required anymore.
312 MESSAGEQUEUE
*QUEUE_Lock( HQUEUE16 hQueue
)
316 HeapLock( SystemHeap
); /* FIXME: a bit overkill */
317 queue
= GlobalLock16( hQueue
);
318 if ( !queue
|| (queue
->magic
!= QUEUE_MAGIC
) )
320 HeapUnlock( SystemHeap
);
325 HeapUnlock( SystemHeap
);
330 /***********************************************************************
333 * Use with QUEUE_Lock to get a thread safe access to message queue
336 void QUEUE_Unlock( MESSAGEQUEUE
*queue
)
340 HeapLock( SystemHeap
); /* FIXME: a bit overkill */
342 if ( --queue
->lockCount
== 0 )
344 DeleteCriticalSection ( &queue
->cSection
);
346 CloseHandle( queue
->hEvent
);
347 GlobalFree16( queue
->self
);
350 HeapUnlock( SystemHeap
);
355 /***********************************************************************
358 void QUEUE_DumpQueue( HQUEUE16 hQueue
)
362 if (!(pq
= (MESSAGEQUEUE
*) QUEUE_Lock( hQueue
)) )
364 WARN_(msg
)("%04x is not a queue handle\n", hQueue
);
368 DPRINTF( "next: %12.4x Intertask SendMessage:\n"
369 "thread: %10p ----------------------\n"
370 "firstMsg: %8p smWaiting: %10p\n"
371 "lastMsg: %8p smPending: %10p\n"
372 "msgCount: %8.4x smProcessing: %10p\n"
380 pq
->next
, pq
->teb
, pq
->firstMsg
, pq
->smWaiting
, pq
->lastMsg
,
381 pq
->smPending
, pq
->msgCount
, pq
->smProcessing
,
382 (unsigned)pq
->lockCount
, pq
->wWinVersion
,
383 pq
->wPaintCount
, pq
->wTimerCount
,
384 pq
->wakeBits
, pq
->wakeMask
, pq
->hCurHook
);
390 /***********************************************************************
393 void QUEUE_WalkQueues(void)
396 HQUEUE16 hQueue
= hFirstQueue
;
398 DPRINTF( "Queue Msgs Thread Task Module\n" );
401 MESSAGEQUEUE
*queue
= (MESSAGEQUEUE
*)QUEUE_Lock( hQueue
);
404 WARN_(msg
)("Bad queue handle %04x\n", hQueue
);
407 if (!GetModuleName16( queue
->teb
->process
->task
, module
, sizeof(module
)))
408 strcpy( module
, "???" );
409 DPRINTF( "%04x %4d %p %04x %s\n", hQueue
,queue
->msgCount
,
410 queue
->teb
, queue
->teb
->process
->task
, module
);
411 hQueue
= queue
->next
;
412 QUEUE_Unlock( queue
);
418 /***********************************************************************
419 * QUEUE_IsExitingQueue
421 BOOL
QUEUE_IsExitingQueue( HQUEUE16 hQueue
)
423 return (hExitingQueue
&& (hQueue
== hExitingQueue
));
427 /***********************************************************************
428 * QUEUE_SetExitingQueue
430 void QUEUE_SetExitingQueue( HQUEUE16 hQueue
)
432 hExitingQueue
= hQueue
;
436 /***********************************************************************
437 * QUEUE_CreateMsgQueue
439 * Creates a message queue. Doesn't link it into queue list!
441 static HQUEUE16
QUEUE_CreateMsgQueue( BOOL16 bCreatePerQData
)
444 MESSAGEQUEUE
* msgQueue
;
445 TDB
*pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
447 TRACE_(msg
)("(): Creating message queue...\n");
449 if (!(hQueue
= GlobalAlloc16( GMEM_FIXED
| GMEM_ZEROINIT
,
450 sizeof(MESSAGEQUEUE
) )))
453 msgQueue
= (MESSAGEQUEUE
*) GlobalLock16( hQueue
);
457 msgQueue
->self
= hQueue
;
458 msgQueue
->wakeBits
= msgQueue
->changeBits
= 0;
459 msgQueue
->wWinVersion
= pTask
? pTask
->version
: 0;
461 InitializeCriticalSection( &msgQueue
->cSection
);
462 MakeCriticalSectionGlobal( &msgQueue
->cSection
);
464 /* Create an Event object for waiting on message, used by win32 thread
466 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
468 msgQueue
->hEvent
= CreateEventA( NULL
, FALSE
, FALSE
, NULL
);
470 if (msgQueue
->hEvent
== 0)
472 WARN_(msg
)("CreateEventA is not able to create an event object");
475 msgQueue
->hEvent
= ConvertToGlobalHandle( msgQueue
->hEvent
);
478 msgQueue
->hEvent
= 0;
480 msgQueue
->lockCount
= 1;
481 msgQueue
->magic
= QUEUE_MAGIC
;
483 /* Create and initialize our per queue data */
484 msgQueue
->pQData
= bCreatePerQData
? PERQDATA_CreateInstance() : NULL
;
490 /***********************************************************************
493 * Try to reply to all pending sent messages on exit.
495 static void QUEUE_FlushMessages( MESSAGEQUEUE
*queue
)
498 MESSAGEQUEUE
*senderQ
= 0;
502 EnterCriticalSection( &queue
->cSection
);
504 /* empty the list of pending SendMessage waiting to be received */
505 while (queue
->smPending
)
507 smsg
= QUEUE_RemoveSMSG( queue
, SM_PENDING_LIST
, 0);
509 senderQ
= (MESSAGEQUEUE
*)QUEUE_Lock( smsg
->hSrcQueue
);
513 /* return 0, to unblock other thread */
515 smsg
->flags
|= SMSG_HAVE_RESULT
;
516 QUEUE_SetWakeBit( senderQ
, QS_SMRESULT
);
518 QUEUE_Unlock( senderQ
);
521 QUEUE_ClearWakeBit( queue
, QS_SENDMESSAGE
);
523 LeaveCriticalSection( &queue
->cSection
);
528 /***********************************************************************
529 * QUEUE_DeleteMsgQueue
531 * Unlinks and deletes a message queue.
533 * Note: We need to mask asynchronous events to make sure PostMessage works
534 * even in the signal handler.
536 BOOL
QUEUE_DeleteMsgQueue( HQUEUE16 hQueue
)
538 MESSAGEQUEUE
* msgQueue
= (MESSAGEQUEUE
*)QUEUE_Lock(hQueue
);
541 TRACE_(msg
)("(): Deleting message queue %04x\n", hQueue
);
543 if (!hQueue
|| !msgQueue
)
545 WARN_(msg
)("invalid argument.\n");
551 if( hCursorQueue
== hQueue
) hCursorQueue
= 0;
552 if( hActiveQueue
== hQueue
) hActiveQueue
= 0;
554 /* flush sent messages */
555 QUEUE_FlushMessages( msgQueue
);
557 HeapLock( SystemHeap
); /* FIXME: a bit overkill */
559 /* Release per queue data if present */
560 if ( msgQueue
->pQData
)
562 PERQDATA_Release( msgQueue
->pQData
);
563 msgQueue
->pQData
= 0;
566 /* remove the message queue from the global link list */
567 pPrev
= &hFirstQueue
;
568 while (*pPrev
&& (*pPrev
!= hQueue
))
570 MESSAGEQUEUE
*msgQ
= (MESSAGEQUEUE
*)GlobalLock16(*pPrev
);
573 if ( !msgQ
|| (msgQ
->magic
!= QUEUE_MAGIC
) )
575 /* HQUEUE link list is corrupted, try to exit gracefully */
576 WARN_(msg
)("HQUEUE link list corrupted!\n");
582 if (pPrev
&& *pPrev
) *pPrev
= msgQueue
->next
;
585 HeapUnlock( SystemHeap
);
587 /* free up resource used by MESSAGEQUEUE strcture */
588 msgQueue
->lockCount
--;
589 QUEUE_Unlock( msgQueue
);
595 /***********************************************************************
596 * QUEUE_CreateSysMsgQueue
598 * Create the system message queue, and set the double-click speed.
599 * Must be called only once.
601 BOOL
QUEUE_CreateSysMsgQueue( int size
)
603 /* Note: We dont need perQ data for the system message queue */
604 if (!(hmemSysMsgQueue
= QUEUE_CreateMsgQueue( FALSE
)))
607 sysMsgQueue
= (MESSAGEQUEUE
*) GlobalLock16( hmemSysMsgQueue
);
612 /***********************************************************************
615 MESSAGEQUEUE
*QUEUE_GetSysQueue(void)
621 /***********************************************************************
624 * See "Windows Internals", p.449
626 void QUEUE_SetWakeBit( MESSAGEQUEUE
*queue
, WORD bit
)
628 TRACE_(msg
)("queue = %04x (wm=%04x), bit = %04x\n",
629 queue
->self
, queue
->wakeMask
, bit
);
631 if (bit
& QS_MOUSE
) pMouseQueue
= queue
;
632 if (bit
& QS_KEY
) pKbdQueue
= queue
;
633 queue
->changeBits
|= bit
;
634 queue
->wakeBits
|= bit
;
635 if (queue
->wakeMask
& bit
)
639 /* Wake up thread waiting for message */
640 if ( THREAD_IsWin16( queue
->teb
) )
642 int iWndsLock
= WIN_SuspendWndsLock();
643 PostEvent16( queue
->teb
->process
->task
);
644 WIN_RestoreWndsLock( iWndsLock
);
648 SetEvent( queue
->hEvent
);
654 /***********************************************************************
657 void QUEUE_ClearWakeBit( MESSAGEQUEUE
*queue
, WORD bit
)
659 queue
->changeBits
&= ~bit
;
660 queue
->wakeBits
&= ~bit
;
664 /***********************************************************************
667 * See "Windows Internals", p.447
670 * 0 if exit with timeout
673 int QUEUE_WaitBits( WORD bits
, DWORD timeout
)
680 TRACE_(msg
)("q %04x waiting for %04x\n", GetFastQueue16(), bits
);
682 if ( THREAD_IsWin16( NtCurrentTeb() ) && (timeout
!= INFINITE
) )
683 curTime
= GetTickCount();
685 hQueue
= GetFastQueue16();
686 if (!(queue
= (MESSAGEQUEUE
*)QUEUE_Lock( hQueue
))) return 0;
688 pdb
= PROCESS_Current();
692 if (queue
->changeBits
& bits
)
694 /* One of the bits is set; we can return */
696 QUEUE_Unlock( queue
);
699 if (queue
->wakeBits
& QS_SENDMESSAGE
)
701 /* Process the sent message immediately */
704 QUEUE_ReceiveMessage( queue
);
705 continue; /* nested sm crux */
708 queue
->wakeMask
= bits
| QS_SENDMESSAGE
;
709 if(queue
->changeBits
& bits
)
714 TRACE_(msg
)("%04x) wakeMask is %04x, waiting\n", queue
->self
, queue
->wakeMask
);
716 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
721 if ( (bHasWin16Lock
= _ConfirmWin16Lock()) )
723 TRACE_(msg
)("bHasWin16Lock=TRUE\n");
724 ReleaseThunkLock( &dwlc
);
728 if ( pdb
->main_queue
== INVALID_HANDLE_VALUE16
)
730 pdb
->main_queue
= hQueue
;
732 if ( pdb
->main_queue
== hQueue
)
734 SetEvent ( pdb
->idle_event
);
737 WaitForSingleObject( queue
->hEvent
, timeout
);
739 if ( pdb
->main_queue
== hQueue
)
741 ResetEvent ( pdb
->idle_event
);
746 RestoreThunkLock( dwlc
);
751 SetEvent ( pdb
->idle_event
);
753 if ( timeout
== INFINITE
)
754 WaitEvent16( 0 ); /* win 16 thread, use WaitEvent */
757 /* check for timeout, then give control to other tasks */
758 if (GetTickCount() - curTime
> timeout
)
761 QUEUE_Unlock( queue
);
762 return 0; /* exit with timeout */
771 /***********************************************************************
774 * This routine is called when a SMSG need to be added to one of the three
775 * SM list. (SM_PROCESSING_LIST, SM_PENDING_LIST, SM_WAITING_LIST)
777 BOOL
QUEUE_AddSMSG( MESSAGEQUEUE
*queue
, int list
, SMSG
*smsg
)
779 TRACE_(sendmsg
)("queue=%x, list=%d, smsg=%p msg=%s\n", queue
->self
, list
,
780 smsg
, SPY_GetMsgName(smsg
->msg
));
784 case SM_PROCESSING_LIST
:
785 /* don't need to be thread safe, only accessed by the
786 thread associated with the sender queue */
787 smsg
->nextProcessing
= queue
->smProcessing
;
788 queue
->smProcessing
= smsg
;
791 case SM_WAITING_LIST
:
792 /* don't need to be thread safe, only accessed by the
793 thread associated with the receiver queue */
794 smsg
->nextWaiting
= queue
->smWaiting
;
795 queue
->smWaiting
= smsg
;
798 case SM_PENDING_LIST
:
800 /* make it thread safe, could be accessed by the sender and
804 EnterCriticalSection( &queue
->cSection
);
805 smsg
->nextPending
= NULL
;
806 prev
= &queue
->smPending
;
808 prev
= &(*prev
)->nextPending
;
810 LeaveCriticalSection( &queue
->cSection
);
812 QUEUE_SetWakeBit( queue
, QS_SENDMESSAGE
);
817 WARN_(sendmsg
)("Invalid list: %d", list
);
825 /***********************************************************************
828 * This routine is called when a SMSG need to be remove from one of the three
829 * SM list. (SM_PROCESSING_LIST, SM_PENDING_LIST, SM_WAITING_LIST)
830 * If smsg == 0, remove the first smsg from the specified list
832 SMSG
*QUEUE_RemoveSMSG( MESSAGEQUEUE
*queue
, int list
, SMSG
*smsg
)
837 case SM_PROCESSING_LIST
:
838 /* don't need to be thread safe, only accessed by the
839 thread associated with the sender queue */
841 /* if smsg is equal to null, it means the first in the list */
843 smsg
= queue
->smProcessing
;
845 TRACE_(sendmsg
)("queue=%x, list=%d, smsg=%p msg=%s\n", queue
->self
, list
,
846 smsg
, SPY_GetMsgName(smsg
->msg
));
847 /* In fact SM_PROCESSING_LIST is a stack, and smsg
848 should be always at the top of the list */
849 if ( (smsg
!= queue
->smProcessing
) || !queue
->smProcessing
)
851 ERR_(sendmsg
)("smsg not at the top of Processing list, smsg=0x%p queue=0x%p", smsg
, queue
);
856 queue
->smProcessing
= smsg
->nextProcessing
;
857 smsg
->nextProcessing
= 0;
861 case SM_WAITING_LIST
:
862 /* don't need to be thread safe, only accessed by the
863 thread associated with the receiver queue */
865 /* if smsg is equal to null, it means the first in the list */
867 smsg
= queue
->smWaiting
;
869 TRACE_(sendmsg
)("queue=%x, list=%d, smsg=%p msg=%s\n", queue
->self
, list
,
870 smsg
, SPY_GetMsgName(smsg
->msg
));
871 /* In fact SM_WAITING_LIST is a stack, and smsg
872 should be always at the top of the list */
873 if ( (smsg
!= queue
->smWaiting
) || !queue
->smWaiting
)
875 ERR_(sendmsg
)("smsg not at the top of Waiting list, smsg=0x%p queue=0x%p", smsg
, queue
);
880 queue
->smWaiting
= smsg
->nextWaiting
;
881 smsg
->nextWaiting
= 0;
885 case SM_PENDING_LIST
:
886 /* make it thread safe, could be accessed by the sender and
888 EnterCriticalSection( &queue
->cSection
);
890 if (!smsg
|| !queue
->smPending
)
891 smsg
= queue
->smPending
;
894 ERR_(sendmsg
)("should always remove the top one in Pending list, smsg=0x%p queue=0x%p", smsg
, queue
);
895 LeaveCriticalSection( &queue
->cSection
);
899 TRACE_(sendmsg
)("queue=%x, list=%d, smsg=%p msg=%s\n", queue
->self
, list
,
900 smsg
, SPY_GetMsgName(smsg
->msg
));
902 queue
->smPending
= smsg
->nextPending
;
903 smsg
->nextPending
= 0;
905 /* if no more SMSG in Pending list, clear QS_SENDMESSAGE flag */
906 if (!queue
->smPending
)
907 QUEUE_ClearWakeBit( queue
, QS_SENDMESSAGE
);
909 LeaveCriticalSection( &queue
->cSection
);
913 WARN_(sendmsg
)("Invalid list: %d", list
);
921 /***********************************************************************
922 * QUEUE_ReceiveMessage
924 * This routine is called when a sent message is waiting for the queue.
926 void QUEUE_ReceiveMessage( MESSAGEQUEUE
*queue
)
930 MESSAGEQUEUE
*senderQ
;
932 TRACE_(sendmsg
)("queue %04x\n", queue
->self
);
934 if ( !(queue
->wakeBits
& QS_SENDMESSAGE
) && queue
->smPending
)
936 TRACE_(sendmsg
)("\trcm: nothing to do\n");
940 /* remove smsg on the top of the pending list and put it in the processing list */
941 smsg
= QUEUE_RemoveSMSG(queue
, SM_PENDING_LIST
, 0);
942 QUEUE_AddSMSG(queue
, SM_WAITING_LIST
, smsg
);
944 TRACE_(sendmsg
)("RM: %s [%04x] (%04x -> %04x)\n",
945 SPY_GetMsgName(smsg
->msg
), smsg
->msg
, smsg
->hSrcQueue
, smsg
->hDstQueue
);
947 if (IsWindow( smsg
->hWnd
))
949 WND
*wndPtr
= WIN_FindWndPtr( smsg
->hWnd
);
950 DWORD extraInfo
= queue
->GetMessageExtraInfoVal
; /* save ExtraInfo */
952 /* use sender queue extra info value while calling the window proc */
953 senderQ
= (MESSAGEQUEUE
*)QUEUE_Lock( smsg
->hSrcQueue
);
956 queue
->GetMessageExtraInfoVal
= senderQ
->GetMessageExtraInfoVal
;
957 QUEUE_Unlock( senderQ
);
960 /* call the right version of CallWindowProcXX */
961 if (smsg
->flags
& SMSG_WIN32
)
963 TRACE_(sendmsg
)("\trcm: msg is Win32\n" );
964 if (smsg
->flags
& SMSG_UNICODE
)
965 result
= CallWindowProcW( wndPtr
->winproc
,
966 smsg
->hWnd
, smsg
->msg
,
967 smsg
->wParam
, smsg
->lParam
);
969 result
= CallWindowProcA( wndPtr
->winproc
,
970 smsg
->hWnd
, smsg
->msg
,
971 smsg
->wParam
, smsg
->lParam
);
973 else /* Win16 message */
974 result
= CallWindowProc16( (WNDPROC16
)wndPtr
->winproc
,
977 LOWORD (smsg
->wParam
),
980 queue
->GetMessageExtraInfoVal
= extraInfo
; /* Restore extra info */
981 WIN_ReleaseWndPtr(wndPtr
);
982 TRACE_(sendmsg
)("result = %08x\n", (unsigned)result
);
984 else WARN_(sendmsg
)("\trcm: bad hWnd\n");
987 /* set SMSG_SENDING_REPLY flag to tell ReplyMessage16, it's not
989 smsg
->flags
|= SMSG_SENDING_REPLY
;
990 ReplyMessage( result
);
992 TRACE_(sendmsg
)("done! \n" );
997 /***********************************************************************
1000 * Add a message to the queue. Return FALSE if queue is full.
1002 BOOL
QUEUE_AddMsg( HQUEUE16 hQueue
, int type
, MSG
*msg
, DWORD extraInfo
)
1004 MESSAGEQUEUE
*msgQueue
;
1008 if (!(msgQueue
= (MESSAGEQUEUE
*)QUEUE_Lock( hQueue
))) return FALSE
;
1010 /* allocate new message in global heap for now */
1011 if (!(qmsg
= (QMSG
*) HeapAlloc( SystemHeap
, 0, sizeof(QMSG
) ) ))
1013 QUEUE_Unlock( msgQueue
);
1017 EnterCriticalSection( &msgQueue
->cSection
);
1022 qmsg
->extraInfo
= extraInfo
;
1024 /* insert the message in the link list */
1026 qmsg
->prevMsg
= msgQueue
->lastMsg
;
1028 if (msgQueue
->lastMsg
)
1029 msgQueue
->lastMsg
->nextMsg
= qmsg
;
1031 /* update first and last anchor in message queue */
1032 msgQueue
->lastMsg
= qmsg
;
1033 if (!msgQueue
->firstMsg
)
1034 msgQueue
->firstMsg
= qmsg
;
1036 msgQueue
->msgCount
++;
1038 LeaveCriticalSection( &msgQueue
->cSection
);
1040 QUEUE_SetWakeBit( msgQueue
, QS_POSTMESSAGE
);
1041 QUEUE_Unlock( msgQueue
);
1048 /***********************************************************************
1051 * Find a message matching the given parameters. Return -1 if none available.
1053 QMSG
* QUEUE_FindMsg( MESSAGEQUEUE
* msgQueue
, HWND hwnd
, int first
, int last
)
1057 EnterCriticalSection( &msgQueue
->cSection
);
1059 if (!msgQueue
->msgCount
)
1061 else if (!hwnd
&& !first
&& !last
)
1062 qmsg
= msgQueue
->firstMsg
;
1065 /* look in linked list for message matching first and last criteria */
1066 for (qmsg
= msgQueue
->firstMsg
; qmsg
; qmsg
= qmsg
->nextMsg
)
1068 MSG
*msg
= &(qmsg
->msg
);
1070 if (!hwnd
|| (msg
->hwnd
== hwnd
))
1072 if (!first
&& !last
)
1073 break; /* found it */
1075 if ((msg
->message
>= first
) && (!last
|| (msg
->message
<= last
)))
1076 break; /* found it */
1081 LeaveCriticalSection( &msgQueue
->cSection
);
1088 /***********************************************************************
1091 * Remove a message from the queue (pos must be a valid position).
1093 void QUEUE_RemoveMsg( MESSAGEQUEUE
* msgQueue
, QMSG
*qmsg
)
1095 EnterCriticalSection( &msgQueue
->cSection
);
1097 /* set the linked list */
1099 qmsg
->prevMsg
->nextMsg
= qmsg
->nextMsg
;
1102 qmsg
->nextMsg
->prevMsg
= qmsg
->prevMsg
;
1104 if (msgQueue
->firstMsg
== qmsg
)
1105 msgQueue
->firstMsg
= qmsg
->nextMsg
;
1107 if (msgQueue
->lastMsg
== qmsg
)
1108 msgQueue
->lastMsg
= qmsg
->prevMsg
;
1110 /* deallocate the memory for the message */
1111 HeapFree( SystemHeap
, 0, qmsg
);
1113 msgQueue
->msgCount
--;
1114 if (!msgQueue
->msgCount
) msgQueue
->wakeBits
&= ~QS_POSTMESSAGE
;
1116 LeaveCriticalSection( &msgQueue
->cSection
);
1120 /***********************************************************************
1123 * Wake a queue upon reception of a hardware event.
1125 static void QUEUE_WakeSomeone( UINT message
)
1130 HQUEUE16 hQueue
= 0;
1131 MESSAGEQUEUE
*queue
= NULL
;
1134 hQueue
= hCursorQueue
;
1136 if( (message
>= WM_KEYFIRST
) && (message
<= WM_KEYLAST
) )
1140 hQueue
= hActiveQueue
;
1144 wakeBit
= (message
== WM_MOUSEMOVE
) ? QS_MOUSEMOVE
: QS_MOUSEBUTTON
;
1145 if( (hwnd
= GetCapture()) )
1146 if( (wndPtr
= WIN_FindWndPtr( hwnd
)) )
1148 hQueue
= wndPtr
->hmemTaskQ
;
1149 WIN_ReleaseWndPtr(wndPtr
);
1153 if( (hwnd
= GetSysModalWindow16()) )
1155 if( (wndPtr
= WIN_FindWndPtr( hwnd
)) )
1157 hQueue
= wndPtr
->hmemTaskQ
;
1158 WIN_ReleaseWndPtr(wndPtr
);
1163 queue
= QUEUE_Lock( hQueue
);
1167 queue
= QUEUE_Lock( hFirstQueue
);
1170 if (queue
->wakeMask
& wakeBit
) break;
1172 QUEUE_Unlock(queue
);
1173 queue
= QUEUE_Lock( queue
->next
);
1177 WARN_(msg
)("couldn't find queue\n");
1182 QUEUE_SetWakeBit( queue
, wakeBit
);
1184 QUEUE_Unlock( queue
);
1188 /***********************************************************************
1191 * Add an event to the system message queue.
1192 * Note: the position is relative to the desktop window.
1194 void hardware_event( UINT message
, WPARAM wParam
, LPARAM lParam
,
1195 int xPos
, int yPos
, DWORD time
, DWORD extraInfo
)
1201 if (!sysMsgQueue
) return;
1203 EnterCriticalSection( &sysMsgQueue
->cSection
);
1205 /* Merge with previous event if possible */
1206 qmsg
= sysMsgQueue
->lastMsg
;
1208 if ((message
== WM_MOUSEMOVE
) && sysMsgQueue
->lastMsg
)
1210 msg
= &(sysMsgQueue
->lastMsg
->msg
);
1212 if ((msg
->message
== message
) && (msg
->wParam
== wParam
))
1215 qmsg
= sysMsgQueue
->lastMsg
;
1222 /* Should I limit the number of message in
1223 the system message queue??? */
1225 /* Don't merge allocate a new msg in the global heap */
1227 if (!(qmsg
= (QMSG
*) HeapAlloc( SystemHeap
, 0, sizeof(QMSG
) ) ))
1229 LeaveCriticalSection( &sysMsgQueue
->cSection
);
1233 /* put message at the end of the linked list */
1235 qmsg
->prevMsg
= sysMsgQueue
->lastMsg
;
1237 if (sysMsgQueue
->lastMsg
)
1238 sysMsgQueue
->lastMsg
->nextMsg
= qmsg
;
1240 /* set last and first anchor index in system message queue */
1241 sysMsgQueue
->lastMsg
= qmsg
;
1242 if (!sysMsgQueue
->firstMsg
)
1243 sysMsgQueue
->firstMsg
= qmsg
;
1245 sysMsgQueue
->msgCount
++;
1251 msg
->message
= message
;
1252 msg
->wParam
= wParam
;
1253 msg
->lParam
= lParam
;
1257 qmsg
->extraInfo
= extraInfo
;
1258 qmsg
->type
= QMSG_HARDWARE
;
1260 LeaveCriticalSection( &sysMsgQueue
->cSection
);
1262 QUEUE_WakeSomeone( message
);
1266 /***********************************************************************
1267 * QUEUE_GetQueueTask
1269 HTASK16
QUEUE_GetQueueTask( HQUEUE16 hQueue
)
1273 MESSAGEQUEUE
*queue
= QUEUE_Lock( hQueue
);
1277 hTask
= queue
->teb
->process
->task
;
1278 QUEUE_Unlock( queue
);
1286 /***********************************************************************
1287 * QUEUE_IncPaintCount
1289 void QUEUE_IncPaintCount( HQUEUE16 hQueue
)
1291 MESSAGEQUEUE
*queue
;
1293 if (!(queue
= (MESSAGEQUEUE
*)QUEUE_Lock( hQueue
))) return;
1294 queue
->wPaintCount
++;
1295 QUEUE_SetWakeBit( queue
, QS_PAINT
);
1296 QUEUE_Unlock( queue
);
1300 /***********************************************************************
1301 * QUEUE_DecPaintCount
1303 void QUEUE_DecPaintCount( HQUEUE16 hQueue
)
1305 MESSAGEQUEUE
*queue
;
1307 if (!(queue
= (MESSAGEQUEUE
*)QUEUE_Lock( hQueue
))) return;
1308 queue
->wPaintCount
--;
1309 if (!queue
->wPaintCount
) queue
->wakeBits
&= ~QS_PAINT
;
1310 QUEUE_Unlock( queue
);
1314 /***********************************************************************
1315 * QUEUE_IncTimerCount
1317 void QUEUE_IncTimerCount( HQUEUE16 hQueue
)
1319 MESSAGEQUEUE
*queue
;
1321 if (!(queue
= (MESSAGEQUEUE
*)QUEUE_Lock( hQueue
))) return;
1322 queue
->wTimerCount
++;
1323 QUEUE_SetWakeBit( queue
, QS_TIMER
);
1324 QUEUE_Unlock( queue
);
1328 /***********************************************************************
1329 * QUEUE_DecTimerCount
1331 void QUEUE_DecTimerCount( HQUEUE16 hQueue
)
1333 MESSAGEQUEUE
*queue
;
1335 if (!(queue
= (MESSAGEQUEUE
*)QUEUE_Lock( hQueue
))) return;
1336 queue
->wTimerCount
--;
1337 if (!queue
->wTimerCount
) queue
->wakeBits
&= ~QS_TIMER
;
1338 QUEUE_Unlock( queue
);
1342 /***********************************************************************
1343 * PostQuitMessage16 (USER.6)
1345 void WINAPI
PostQuitMessage16( INT16 exitCode
)
1347 PostQuitMessage( exitCode
);
1351 /***********************************************************************
1352 * PostQuitMessage (USER32.421)
1354 * PostQuitMessage() posts a message to the system requesting an
1355 * application to terminate execution. As a result of this function,
1356 * the WM_QUIT message is posted to the application, and
1357 * PostQuitMessage() returns immediately. The exitCode parameter
1358 * specifies an application-defined exit code, which appears in the
1359 * _wParam_ parameter of the WM_QUIT message posted to the application.
1365 void WINAPI
PostQuitMessage( INT exitCode
)
1367 MESSAGEQUEUE
*queue
;
1369 if (!(queue
= (MESSAGEQUEUE
*)QUEUE_Lock( GetFastQueue16() ))) return;
1370 queue
->wPostQMsg
= TRUE
;
1371 queue
->wExitCode
= (WORD
)exitCode
;
1372 QUEUE_Unlock( queue
);
1376 /***********************************************************************
1377 * GetWindowTask16 (USER.224)
1379 HTASK16 WINAPI
GetWindowTask16( HWND16 hwnd
)
1382 WND
*wndPtr
= WIN_FindWndPtr( hwnd
);
1384 if (!wndPtr
) return 0;
1385 retvalue
= QUEUE_GetQueueTask( wndPtr
->hmemTaskQ
);
1386 WIN_ReleaseWndPtr(wndPtr
);
1390 /***********************************************************************
1391 * GetWindowThreadProcessId (USER32.313)
1393 DWORD WINAPI
GetWindowThreadProcessId( HWND hwnd
, LPDWORD process
)
1396 MESSAGEQUEUE
*queue
;
1398 WND
*wndPtr
= WIN_FindWndPtr( hwnd
);
1399 if (!wndPtr
) return 0;
1401 queue
= QUEUE_Lock( wndPtr
->hmemTaskQ
);
1402 WIN_ReleaseWndPtr(wndPtr
);
1404 if (!queue
) return 0;
1406 if ( process
) *process
= (DWORD
)queue
->teb
->pid
;
1407 retvalue
= (DWORD
)queue
->teb
->tid
;
1409 QUEUE_Unlock( queue
);
1414 /***********************************************************************
1415 * SetMessageQueue16 (USER.266)
1417 BOOL16 WINAPI
SetMessageQueue16( INT16 size
)
1419 return SetMessageQueue( size
);
1423 /***********************************************************************
1424 * SetMessageQueue (USER32.494)
1426 BOOL WINAPI
SetMessageQueue( INT size
)
1428 /* now obsolete the message queue will be expanded dynamically
1431 /* access the queue to create it if it's not existing */
1437 /***********************************************************************
1438 * InitThreadInput16 (USER.409)
1440 HQUEUE16 WINAPI
InitThreadInput16( WORD unknown
, WORD flags
)
1443 MESSAGEQUEUE
*queuePtr
;
1445 TEB
*teb
= NtCurrentTeb();
1450 hQueue
= teb
->queue
;
1454 /* Create thread message queue */
1455 if( !(hQueue
= QUEUE_CreateMsgQueue( TRUE
)))
1457 WARN_(msg
)("failed!\n");
1461 /* Link new queue into list */
1462 queuePtr
= (MESSAGEQUEUE
*)QUEUE_Lock( hQueue
);
1463 queuePtr
->teb
= NtCurrentTeb();
1465 HeapLock( SystemHeap
); /* FIXME: a bit overkill */
1466 SetThreadQueue16( 0, hQueue
);
1467 teb
->queue
= hQueue
;
1469 queuePtr
->next
= hFirstQueue
;
1470 hFirstQueue
= hQueue
;
1471 HeapUnlock( SystemHeap
);
1473 QUEUE_Unlock( queuePtr
);
1479 /***********************************************************************
1480 * GetQueueStatus16 (USER.334)
1482 DWORD WINAPI
GetQueueStatus16( UINT16 flags
)
1484 MESSAGEQUEUE
*queue
;
1487 if (!(queue
= (MESSAGEQUEUE
*)QUEUE_Lock( GetFastQueue16() ))) return 0;
1488 ret
= MAKELONG( queue
->changeBits
, queue
->wakeBits
);
1489 queue
->changeBits
= 0;
1490 QUEUE_Unlock( queue
);
1492 return ret
& MAKELONG( flags
, flags
);
1495 /***********************************************************************
1496 * GetQueueStatus (USER32.283)
1498 DWORD WINAPI
GetQueueStatus( UINT flags
)
1500 MESSAGEQUEUE
*queue
;
1503 if (!(queue
= (MESSAGEQUEUE
*)QUEUE_Lock( GetFastQueue16() ))) return 0;
1504 ret
= MAKELONG( queue
->changeBits
, queue
->wakeBits
);
1505 queue
->changeBits
= 0;
1506 QUEUE_Unlock( queue
);
1508 return ret
& MAKELONG( flags
, flags
);
1512 /***********************************************************************
1513 * GetInputState16 (USER.335)
1515 BOOL16 WINAPI
GetInputState16(void)
1517 return GetInputState();
1520 /***********************************************************************
1521 * WaitForInputIdle (USER32.577)
1523 DWORD WINAPI
WaitForInputIdle (HANDLE hProcess
, DWORD dwTimeOut
)
1526 DWORD cur_time
, ret
, pid
= MapProcessHandle ( hProcess
);
1528 /* Check whether the calling process is a command line application */
1529 if (!THREAD_IsWin16(NtCurrentTeb() ) &&
1530 (PROCESS_Current()->flags
& PDB32_CONSOLE_PROC
))
1532 TRACE_(msg
)("not a win32 GUI application!\n" );
1536 pdb
= PROCESS_IdToPDB( pid
);
1538 /* check whether we are waiting for a win32 process or the win16 subsystem */
1539 if ( pdb
->flags
& PDB32_WIN16_PROC
) {
1540 if ( THREAD_IsWin16(NtCurrentTeb()) ) return 0;
1542 else { /* target is win32 */
1543 if ( pdb
->flags
& PDB32_CONSOLE_PROC
) return 0;
1544 if ( GetFastQueue16() == pdb
->main_queue
) return 0;
1547 cur_time
= GetTickCount();
1549 TRACE_(msg
)("waiting for %x\n", pdb
->idle_event
);
1550 while ( dwTimeOut
> GetTickCount() - cur_time
|| dwTimeOut
== INFINITE
) {
1552 ret
= MsgWaitForMultipleObjects ( 1, &pdb
->idle_event
, FALSE
, dwTimeOut
, QS_SENDMESSAGE
);
1553 if ( ret
== ( WAIT_OBJECT_0
+ 1 )) {
1554 MESSAGEQUEUE
* queue
;
1555 if (!(queue
= (MESSAGEQUEUE
*)QUEUE_Lock( GetFastQueue16() ))) return 0xFFFFFFFF;
1556 QUEUE_ReceiveMessage ( queue
);
1557 QUEUE_Unlock ( queue
);
1560 if ( ret
== WAIT_TIMEOUT
|| ret
== 0xFFFFFFFF ) {
1561 TRACE_(msg
)("timeout or error\n");
1565 TRACE_(msg
)("finished\n");
1570 return WAIT_TIMEOUT
;
1573 /***********************************************************************
1574 * GetInputState (USER32.244)
1576 BOOL WINAPI
GetInputState(void)
1578 MESSAGEQUEUE
*queue
;
1581 if (!(queue
= (MESSAGEQUEUE
*)QUEUE_Lock( GetFastQueue16() )))
1583 ret
= queue
->wakeBits
& (QS_KEY
| QS_MOUSEBUTTON
);
1584 QUEUE_Unlock( queue
);
1589 /***********************************************************************
1590 * UserYield (USER.332)
1592 void WINAPI
UserYield16(void)
1594 MESSAGEQUEUE
*queue
;
1596 /* Handle sent messages */
1597 queue
= (MESSAGEQUEUE
*)QUEUE_Lock( GetFastQueue16() );
1599 while (queue
&& (queue
->wakeBits
& QS_SENDMESSAGE
))
1600 QUEUE_ReceiveMessage( queue
);
1602 QUEUE_Unlock( queue
);
1605 if ( THREAD_IsWin16( NtCurrentTeb() ) )
1610 /* Handle sent messages again */
1611 queue
= (MESSAGEQUEUE
*)QUEUE_Lock( GetFastQueue16() );
1613 while (queue
&& (queue
->wakeBits
& QS_SENDMESSAGE
))
1614 QUEUE_ReceiveMessage( queue
);
1616 QUEUE_Unlock( queue
);
1619 /***********************************************************************
1620 * GetMessagePos (USER.119) (USER32.272)
1622 * The GetMessagePos() function returns a long value representing a
1623 * cursor position, in screen coordinates, when the last message
1624 * retrieved by the GetMessage() function occurs. The x-coordinate is
1625 * in the low-order word of the return value, the y-coordinate is in
1626 * the high-order word. The application can use the MAKEPOINT()
1627 * macro to obtain a POINT structure from the return value.
1629 * For the current cursor position, use GetCursorPos().
1633 * Cursor position of last message on success, zero on failure.
1640 DWORD WINAPI
GetMessagePos(void)
1642 MESSAGEQUEUE
*queue
;
1645 if (!(queue
= (MESSAGEQUEUE
*)QUEUE_Lock( GetFastQueue16() ))) return 0;
1646 ret
= queue
->GetMessagePosVal
;
1647 QUEUE_Unlock( queue
);
1653 /***********************************************************************
1654 * GetMessageTime (USER.120) (USER32.273)
1656 * GetMessageTime() returns the message time for the last message
1657 * retrieved by the function. The time is measured in milliseconds with
1658 * the same offset as GetTickCount().
1660 * Since the tick count wraps, this is only useful for moderately short
1661 * relative time comparisons.
1665 * Time of last message on success, zero on failure.
1672 LONG WINAPI
GetMessageTime(void)
1674 MESSAGEQUEUE
*queue
;
1677 if (!(queue
= (MESSAGEQUEUE
*)QUEUE_Lock( GetFastQueue16() ))) return 0;
1678 ret
= queue
->GetMessageTimeVal
;
1679 QUEUE_Unlock( queue
);
1685 /***********************************************************************
1686 * GetMessageExtraInfo (USER.288) (USER32.271)
1688 LONG WINAPI
GetMessageExtraInfo(void)
1690 MESSAGEQUEUE
*queue
;
1693 if (!(queue
= (MESSAGEQUEUE
*)QUEUE_Lock( GetFastQueue16() ))) return 0;
1694 ret
= queue
->GetMessageExtraInfoVal
;
1695 QUEUE_Unlock( queue
);