1 /* * Message queues related functions
3 * Copyright 1993, 1994 Alexandre Julliard
8 #include "wine/winbase16.h"
9 #include "wine/winuser16.h"
15 #include "clipboard.h"
23 #define MAX_QUEUE_SIZE 120 /* Max. size of a message queue */
25 static HQUEUE16 hFirstQueue = 0;
26 static HQUEUE16 hExitingQueue = 0;
27 static HQUEUE16 hmemSysMsgQueue = 0;
28 static MESSAGEQUEUE *sysMsgQueue = NULL;
29 static PERQUEUEDATA *pQDataWin16 = NULL; /* Global perQData for Win16 tasks */
31 static MESSAGEQUEUE *pMouseQueue = NULL; /* Queue for last mouse message */
32 static MESSAGEQUEUE *pKbdQueue = NULL; /* Queue for last kbd message */
34 HQUEUE16 hCursorQueue = 0;
35 HQUEUE16 hActiveQueue = 0;
38 /***********************************************************************
39 * PERQDATA_CreateInstance
41 * Creates an instance of a reference counted PERQUEUEDATA element
42 * for the message queue. perQData is stored globally for 16 bit tasks.
44 * Note: We don't implement perQdata exactly the same way Windows does.
45 * Each perQData element is reference counted since it may be potentially
46 * shared by multiple message Queues (via AttachThreadInput).
47 * We only store the current values for Active, Capture and focus windows
50 PERQUEUEDATA * PERQDATA_CreateInstance( )
58 /* Share a single instance of perQData for all 16 bit tasks */
59 if ( ( bIsWin16 = THREAD_IsWin16( THREAD_Current() ) ) )
61 /* If previously allocated, just bump up ref count */
64 PERQDATA_Addref( pQDataWin16 );
69 /* Allocate PERQUEUEDATA from the system heap */
70 if (!( pQData = (PERQUEUEDATA *) HeapAlloc( SystemHeap, 0,
71 sizeof(PERQUEUEDATA) ) ))
75 pQData->hWndCapture = pQData->hWndFocus = pQData->hWndActive = 0;
76 pQData->ulRefCount = 1;
77 pQData->nCaptureHT = HTCLIENT;
79 /* Note: We have an independent critical section for the per queue data
80 * since this may be shared by different threads. see AttachThreadInput()
82 InitializeCriticalSection( &pQData->cSection );
84 /* Save perQData globally for 16 bit tasks */
92 /***********************************************************************
95 * Increment reference count for the PERQUEUEDATA instance
96 * Returns reference count for debugging purposes
98 ULONG PERQDATA_Addref( PERQUEUEDATA *pQData )
100 assert(pQData != 0 );
101 TRACE(msg,"(): current refcount %lu ...\n", pQData->ulRefCount);
103 EnterCriticalSection( &pQData->cSection );
104 ++pQData->ulRefCount;
105 LeaveCriticalSection( &pQData->cSection );
107 return pQData->ulRefCount;
111 /***********************************************************************
114 * Release a reference to a PERQUEUEDATA instance.
115 * Destroy the instance if no more references exist
116 * Returns reference count for debugging purposes
118 ULONG PERQDATA_Release( PERQUEUEDATA *pQData )
120 assert(pQData != 0 );
121 TRACE(msg,"(): current refcount %lu ...\n",
122 (LONG)pQData->ulRefCount );
124 EnterCriticalSection( &pQData->cSection );
125 if ( --pQData->ulRefCount == 0 )
127 LeaveCriticalSection( &pQData->cSection );
128 DeleteCriticalSection( &pQData->cSection );
130 TRACE(msg,"(): deleting PERQUEUEDATA instance ...\n" );
132 /* Deleting our global 16 bit perQData? */
133 if ( pQData == pQDataWin16 )
136 /* Free the PERQUEUEDATA instance */
137 HeapFree( SystemHeap, 0, pQData );
141 LeaveCriticalSection( &pQData->cSection );
143 return pQData->ulRefCount;
147 /***********************************************************************
148 * PERQDATA_GetFocusWnd
150 * Get the focus hwnd member in a threadsafe manner
152 HWND32 PERQDATA_GetFocusWnd( PERQUEUEDATA *pQData )
155 assert(pQData != 0 );
157 EnterCriticalSection( &pQData->cSection );
158 hWndFocus = pQData->hWndFocus;
159 LeaveCriticalSection( &pQData->cSection );
165 /***********************************************************************
166 * PERQDATA_SetFocusWnd
168 * Set the focus hwnd member in a threadsafe manner
170 HWND32 PERQDATA_SetFocusWnd( PERQUEUEDATA *pQData, HWND32 hWndFocus )
173 assert(pQData != 0 );
175 EnterCriticalSection( &pQData->cSection );
176 hWndFocusPrv = pQData->hWndFocus;
177 pQData->hWndFocus = hWndFocus;
178 LeaveCriticalSection( &pQData->cSection );
184 /***********************************************************************
185 * PERQDATA_GetActiveWnd
187 * Get the active hwnd member in a threadsafe manner
189 HWND32 PERQDATA_GetActiveWnd( PERQUEUEDATA *pQData )
192 assert(pQData != 0 );
194 EnterCriticalSection( &pQData->cSection );
195 hWndActive = pQData->hWndActive;
196 LeaveCriticalSection( &pQData->cSection );
202 /***********************************************************************
203 * PERQDATA_SetActiveWnd
205 * Set the active focus hwnd member in a threadsafe manner
207 HWND32 PERQDATA_SetActiveWnd( PERQUEUEDATA *pQData, HWND32 hWndActive )
209 HWND32 hWndActivePrv;
210 assert(pQData != 0 );
212 EnterCriticalSection( &pQData->cSection );
213 hWndActivePrv = pQData->hWndActive;
214 pQData->hWndActive = hWndActive;
215 LeaveCriticalSection( &pQData->cSection );
217 return hWndActivePrv;
221 /***********************************************************************
222 * PERQDATA_GetCaptureWnd
224 * Get the capture hwnd member in a threadsafe manner
226 HWND32 PERQDATA_GetCaptureWnd( PERQUEUEDATA *pQData )
229 assert(pQData != 0 );
231 EnterCriticalSection( &pQData->cSection );
232 hWndCapture = pQData->hWndCapture;
233 LeaveCriticalSection( &pQData->cSection );
239 /***********************************************************************
240 * PERQDATA_SetCaptureWnd
242 * Set the capture hwnd member in a threadsafe manner
244 HWND32 PERQDATA_SetCaptureWnd( PERQUEUEDATA *pQData, HWND32 hWndCapture )
246 HWND32 hWndCapturePrv;
247 assert(pQData != 0 );
249 EnterCriticalSection( &pQData->cSection );
250 hWndCapturePrv = pQData->hWndCapture;
251 pQData->hWndCapture = hWndCapture;
252 LeaveCriticalSection( &pQData->cSection );
254 return hWndCapturePrv;
258 /***********************************************************************
259 * PERQDATA_GetCaptureInfo
261 * Get the capture info member in a threadsafe manner
263 INT16 PERQDATA_GetCaptureInfo( PERQUEUEDATA *pQData )
266 assert(pQData != 0 );
268 EnterCriticalSection( &pQData->cSection );
269 nCaptureHT = pQData->nCaptureHT;
270 LeaveCriticalSection( &pQData->cSection );
276 /***********************************************************************
277 * PERQDATA_SetCaptureInfo
279 * Set the capture info member in a threadsafe manner
281 INT16 PERQDATA_SetCaptureInfo( PERQUEUEDATA *pQData, INT16 nCaptureHT )
284 assert(pQData != 0 );
286 EnterCriticalSection( &pQData->cSection );
287 nCaptureHTPrv = pQData->nCaptureHT;
288 pQData->nCaptureHT = nCaptureHT;
289 LeaveCriticalSection( &pQData->cSection );
291 return nCaptureHTPrv;
295 /***********************************************************************
298 * Function for getting a 32 bit pointer on queue strcture. For thread
299 * safeness programmers should use this function instead of GlobalLock to
300 * retrieve a pointer on the structure. QUEUE_Unlock should also be called
301 * when access to the queue structure is not required anymore.
303 MESSAGEQUEUE *QUEUE_Lock( HQUEUE16 hQueue )
308 queue = GlobalLock16( hQueue );
309 if ( !queue || (queue->magic != QUEUE_MAGIC) )
322 /***********************************************************************
325 * Use with QUEUE_Lock to get a thread safe acces to message queue
328 void QUEUE_Unlock( MESSAGEQUEUE *queue )
334 if ( --queue->lockCount == 0 )
336 DeleteCriticalSection ( &queue->cSection );
338 CloseHandle( queue->hEvent );
339 GlobalFree16( queue->self );
347 /***********************************************************************
350 void QUEUE_DumpQueue( HQUEUE16 hQueue )
354 if (!(pq = (MESSAGEQUEUE*) QUEUE_Lock( hQueue )) )
356 WARN(msg, "%04x is not a queue handle\n", hQueue );
360 DUMP( "next: %12.4x Intertask SendMessage:\n"
361 "thread: %10p ----------------------\n"
363 "firstMsg: %8p msg: %11.8x\n"
364 "lastMsg: %8p wParam: %10.8x\n"
365 "msgCount: %8.4x lParam: %10.8x\n"
366 "lockCount: %7.4x lRet: %12.8x\n"
367 "wWinVer: %9.4x ISMH: %10.4x\n"
368 "paints: %10.4x hSendTask: %5.4x\n"
369 "timers: %10.4x hPrevSend: %5.4x\n"
373 pq->next, pq->thdb, pq->hWnd32, pq->firstMsg, pq->msg32,
374 pq->lastMsg, pq->wParam32, pq->msgCount, (unsigned)pq->lParam,
375 (unsigned)pq->lockCount, (unsigned)pq->SendMessageReturn,
376 pq->wWinVersion, pq->InSendMessageHandle,
377 pq->wPaintCount, pq->hSendingTask, pq->wTimerCount,
378 pq->hPrevSendingTask, pq->wakeBits, pq->wakeMask, pq->hCurHook);
384 /***********************************************************************
387 void QUEUE_WalkQueues(void)
390 HQUEUE16 hQueue = hFirstQueue;
392 DUMP( "Queue Msgs Thread Task Module\n" );
395 MESSAGEQUEUE *queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue );
398 WARN( msg, "Bad queue handle %04x\n", hQueue );
401 if (!GetModuleName( queue->thdb->process->task, module, sizeof(module )))
402 strcpy( module, "???" );
403 DUMP( "%04x %4d %p %04x %s\n", hQueue,queue->msgCount,
404 queue->thdb, queue->thdb->process->task, module );
405 hQueue = queue->next;
406 QUEUE_Unlock( queue );
412 /***********************************************************************
413 * QUEUE_IsExitingQueue
415 BOOL32 QUEUE_IsExitingQueue( HQUEUE16 hQueue )
417 return (hExitingQueue && (hQueue == hExitingQueue));
421 /***********************************************************************
422 * QUEUE_SetExitingQueue
424 void QUEUE_SetExitingQueue( HQUEUE16 hQueue )
426 hExitingQueue = hQueue;
430 /***********************************************************************
431 * QUEUE_CreateMsgQueue
433 * Creates a message queue. Doesn't link it into queue list!
435 static HQUEUE16 QUEUE_CreateMsgQueue( BOOL16 bCreatePerQData )
438 MESSAGEQUEUE * msgQueue;
439 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
441 TRACE(msg,"(): Creating message queue...\n");
443 if (!(hQueue = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT,
444 sizeof(MESSAGEQUEUE) )))
447 msgQueue = (MESSAGEQUEUE *) GlobalLock16( hQueue );
451 msgQueue->self = hQueue;
452 msgQueue->wakeBits = msgQueue->changeBits = QS_SMPARAMSFREE;
453 msgQueue->wWinVersion = pTask ? pTask->version : 0;
455 InitializeCriticalSection( &msgQueue->cSection );
456 MakeCriticalSectionGlobal( &msgQueue->cSection );
458 /* Create an Event object for waiting on message, used by win32 thread
460 if ( !THREAD_IsWin16( THREAD_Current() ) )
462 msgQueue->hEvent = CreateEvent32A( NULL, FALSE, FALSE, NULL);
464 if (msgQueue->hEvent == 0)
466 WARN(msg, "CreateEvent32A is not able to create an event object");
469 msgQueue->hEvent = ConvertToGlobalHandle( msgQueue->hEvent );
472 msgQueue->hEvent = 0;
474 msgQueue->lockCount = 1;
475 msgQueue->magic = QUEUE_MAGIC;
477 /* Create and initialize our per queue data */
478 msgQueue->pQData = bCreatePerQData ? PERQDATA_CreateInstance() : NULL;
484 /***********************************************************************
485 * QUEUE_DeleteMsgQueue
487 * Unlinks and deletes a message queue.
489 * Note: We need to mask asynchronous events to make sure PostMessage works
490 * even in the signal handler.
492 BOOL32 QUEUE_DeleteMsgQueue( HQUEUE16 hQueue )
494 MESSAGEQUEUE * msgQueue = (MESSAGEQUEUE*)QUEUE_Lock(hQueue);
498 TRACE(msg,"(): Deleting message queue %04x\n", hQueue);
500 if (!hQueue || !msgQueue)
502 WARN(msg, "invalid argument.\n");
508 if( hCursorQueue == hQueue ) hCursorQueue = 0;
509 if( hActiveQueue == hQueue ) hActiveQueue = 0;
511 /* flush sent messages */
512 senderQ = msgQueue->hSendingTask;
515 MESSAGEQUEUE* sq = (MESSAGEQUEUE*)QUEUE_Lock(senderQ);
517 sq->SendMessageReturn = 0L;
518 QUEUE_SetWakeBit( sq, QS_SMRESULT );
519 senderQ = sq->hPrevSendingTask;
525 /* Release per queue data if present */
526 if ( msgQueue->pQData )
528 PERQDATA_Release( msgQueue->pQData );
529 msgQueue->pQData = 0;
532 /* remove the message queue from the global link list */
533 pPrev = &hFirstQueue;
534 while (*pPrev && (*pPrev != hQueue))
536 MESSAGEQUEUE *msgQ = (MESSAGEQUEUE*)GlobalLock16(*pPrev);
539 if ( !msgQ || (msgQ->magic != QUEUE_MAGIC) )
541 /* HQUEUE link list is corrupted, try to exit gracefully */
542 WARN( msg, "HQUEUE link list corrupted!\n");
548 if (pPrev && *pPrev) *pPrev = msgQueue->next;
553 /* free up resource used by MESSAGEQUEUE strcture */
554 msgQueue->lockCount--;
555 QUEUE_Unlock( msgQueue );
561 /***********************************************************************
562 * QUEUE_CreateSysMsgQueue
564 * Create the system message queue, and set the double-click speed.
565 * Must be called only once.
567 BOOL32 QUEUE_CreateSysMsgQueue( int size )
569 /* Note: We dont need perQ data for the system message queue */
570 if (!(hmemSysMsgQueue = QUEUE_CreateMsgQueue( FALSE )))
573 sysMsgQueue = (MESSAGEQUEUE *) GlobalLock16( hmemSysMsgQueue );
578 /***********************************************************************
581 MESSAGEQUEUE *QUEUE_GetSysQueue(void)
587 /***********************************************************************
590 static void QUEUE_Wait( DWORD wait_mask )
592 if ( THREAD_IsWin16( THREAD_Current() ) )
596 TRACE(msg, "current task is 32-bit, calling SYNC_DoWait\n");
597 MsgWaitForMultipleObjects( 0, NULL, FALSE, INFINITE32, wait_mask );
602 /***********************************************************************
605 * See "Windows Internals", p.449
607 void QUEUE_SetWakeBit( MESSAGEQUEUE *queue, WORD bit )
609 TRACE(msg,"queue = %04x (wm=%04x), bit = %04x\n",
610 queue->self, queue->wakeMask, bit );
612 if (bit & QS_MOUSE) pMouseQueue = queue;
613 if (bit & QS_KEY) pKbdQueue = queue;
614 queue->changeBits |= bit;
615 queue->wakeBits |= bit;
616 if (queue->wakeMask & bit)
620 /* Wake up thread waiting for message */
621 if ( THREAD_IsWin16( queue->thdb ) )
622 PostEvent( queue->thdb->process->task );
625 SetEvent( queue->hEvent );
631 /***********************************************************************
634 void QUEUE_ClearWakeBit( MESSAGEQUEUE *queue, WORD bit )
636 queue->changeBits &= ~bit;
637 queue->wakeBits &= ~bit;
641 /***********************************************************************
644 * See "Windows Internals", p.447
646 void QUEUE_WaitBits( WORD bits )
650 TRACE(msg,"q %04x waiting for %04x\n", GetFastQueue(), bits);
654 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue() ))) return;
656 if (queue->changeBits & bits)
658 /* One of the bits is set; we can return */
660 QUEUE_Unlock( queue );
663 if (queue->wakeBits & QS_SENDMESSAGE)
665 /* Process the sent message immediately */
668 QUEUE_ReceiveMessage( queue );
669 QUEUE_Unlock( queue );
670 continue; /* nested sm crux */
673 queue->wakeMask = bits | QS_SENDMESSAGE;
674 if(queue->changeBits & bits)
676 QUEUE_Unlock( queue );
680 TRACE(msg,"%04x) wakeMask is %04x, waiting\n", queue->self, queue->wakeMask);
682 QUEUE_Wait( queue->wakeMask );
684 QUEUE_Unlock( queue );
689 /***********************************************************************
690 * QUEUE_ReceiveMessage
692 * This routine is called when a sent message is waiting for the queue.
694 void QUEUE_ReceiveMessage( MESSAGEQUEUE *queue )
696 MESSAGEQUEUE *senderQ = NULL;
697 HQUEUE16 prevSender = 0;
698 QSMCTRL* prevCtrlPtr = NULL;
701 TRACE(msg, "ReceiveMessage, queue %04x\n", queue->self );
702 if (!(queue->wakeBits & QS_SENDMESSAGE) ||
703 !(senderQ = (MESSAGEQUEUE*)QUEUE_Lock( queue->hSendingTask)))
704 { TRACE(msg,"\trcm: nothing to do\n"); return; }
706 if( !senderQ->hPrevSendingTask )
707 QUEUE_ClearWakeBit( queue, QS_SENDMESSAGE ); /* no more sent messages */
709 /* Save current state on stack */
710 prevSender = queue->InSendMessageHandle;
711 prevCtrlPtr = queue->smResultCurrent;
713 /* Remove sending queue from the list */
714 queue->InSendMessageHandle = queue->hSendingTask;
715 queue->smResultCurrent = senderQ->smResultInit;
716 queue->hSendingTask = senderQ->hPrevSendingTask;
718 TRACE(msg, "\trcm: smResultCurrent = %08x, prevCtrl = %08x\n",
719 (unsigned)queue->smResultCurrent, (unsigned)prevCtrlPtr );
720 QUEUE_SetWakeBit( senderQ, QS_SMPARAMSFREE );
722 TRACE(msg, "\trcm: calling wndproc - %08x %08x %08x %08x\n",
723 senderQ->hWnd32, senderQ->msg32,
724 senderQ->wParam32, (unsigned)senderQ->lParam );
726 if (IsWindow32( senderQ->hWnd32 ))
728 WND *wndPtr = WIN_FindWndPtr( senderQ->hWnd32 );
729 DWORD extraInfo = queue->GetMessageExtraInfoVal;
730 queue->GetMessageExtraInfoVal = senderQ->GetMessageExtraInfoVal;
732 if (senderQ->flags & QUEUE_SM_WIN32)
734 TRACE(msg, "\trcm: msg is Win32\n" );
735 if (senderQ->flags & QUEUE_SM_UNICODE)
736 result = CallWindowProc32W( wndPtr->winproc,
737 senderQ->hWnd32, senderQ->msg32,
738 senderQ->wParam32, senderQ->lParam );
740 result = CallWindowProc32A( wndPtr->winproc,
741 senderQ->hWnd32, senderQ->msg32,
742 senderQ->wParam32, senderQ->lParam );
744 else /* Win16 message */
745 result = CallWindowProc16( (WNDPROC16)wndPtr->winproc,
746 (HWND16) senderQ->hWnd32,
747 (UINT16) senderQ->msg32,
748 LOWORD (senderQ->wParam32),
751 queue->GetMessageExtraInfoVal = extraInfo; /* Restore extra info */
752 TRACE(msg,"\trcm: result = %08x\n", (unsigned)result );
754 else WARN(msg, "\trcm: bad hWnd\n");
756 QUEUE_Unlock( senderQ );
758 /* Return the result to the sender task */
759 ReplyMessage16( result );
761 queue->InSendMessageHandle = prevSender;
762 queue->smResultCurrent = prevCtrlPtr;
764 TRACE(msg,"done!\n");
767 /***********************************************************************
770 * Try to reply to all pending sent messages on exit.
772 void QUEUE_FlushMessages( HQUEUE16 hQueue )
774 MESSAGEQUEUE *queue = (MESSAGEQUEUE*)QUEUE_Lock( hQueue );
778 MESSAGEQUEUE *senderQ = (MESSAGEQUEUE*)QUEUE_Lock( queue->hSendingTask );
779 QSMCTRL* CtrlPtr = queue->smResultCurrent;
781 TRACE(msg,"Flushing queue %04x:\n", hQueue );
786 CtrlPtr = senderQ->smResultInit;
788 TRACE(msg,"\tfrom queue %04x, smResult %08x\n", queue->hSendingTask, (unsigned)CtrlPtr );
790 if( !(queue->hSendingTask = senderQ->hPrevSendingTask) )
791 QUEUE_ClearWakeBit( queue, QS_SENDMESSAGE );
793 QUEUE_SetWakeBit( senderQ, QS_SMPARAMSFREE );
795 queue->smResultCurrent = CtrlPtr;
796 while( senderQ->wakeBits & QS_SMRESULT ) OldYield();
798 senderQ->SendMessageReturn = 0;
799 senderQ->smResult = queue->smResultCurrent;
800 QUEUE_SetWakeBit( senderQ, QS_SMRESULT);
802 QUEUE_Unlock( senderQ );
804 senderQ = (MESSAGEQUEUE*)QUEUE_Lock( queue->hSendingTask );
807 queue->InSendMessageHandle = 0;
809 QUEUE_Unlock( queue );
814 /***********************************************************************
817 * Add a message to the queue. Return FALSE if queue is full.
819 BOOL32 QUEUE_AddMsg( HQUEUE16 hQueue, MSG32 *msg, DWORD extraInfo )
821 MESSAGEQUEUE *msgQueue;
825 if (!(msgQueue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return FALSE;
827 /* allocate new message in global heap for now */
828 if (!(qmsg = (QMSG *) HeapAlloc( SystemHeap, 0, sizeof(QMSG) ) ))
830 QUEUE_Unlock( msgQueue );
834 EnterCriticalSection( &msgQueue->cSection );
838 qmsg->extraInfo = extraInfo;
840 /* insert the message in the link list */
842 qmsg->prevMsg = msgQueue->lastMsg;
844 if (msgQueue->lastMsg)
845 msgQueue->lastMsg->nextMsg = qmsg;
847 /* update first and last anchor in message queue */
848 msgQueue->lastMsg = qmsg;
849 if (!msgQueue->firstMsg)
850 msgQueue->firstMsg = qmsg;
852 msgQueue->msgCount++;
854 LeaveCriticalSection( &msgQueue->cSection );
856 QUEUE_SetWakeBit( msgQueue, QS_POSTMESSAGE );
857 QUEUE_Unlock( msgQueue );
864 /***********************************************************************
867 * Find a message matching the given parameters. Return -1 if none available.
869 QMSG* QUEUE_FindMsg( MESSAGEQUEUE * msgQueue, HWND32 hwnd, int first, int last )
873 EnterCriticalSection( &msgQueue->cSection );
875 if (!msgQueue->msgCount)
877 else if (!hwnd && !first && !last)
878 qmsg = msgQueue->firstMsg;
881 /* look in linked list for message matching first and last criteria */
882 for (qmsg = msgQueue->firstMsg; qmsg; qmsg = qmsg->nextMsg)
884 MSG32 *msg = &(qmsg->msg);
886 if (!hwnd || (msg->hwnd == hwnd))
889 break; /* found it */
891 if ((msg->message >= first) && (msg->message <= last))
892 break; /* found it */
897 LeaveCriticalSection( &msgQueue->cSection );
904 /***********************************************************************
907 * Remove a message from the queue (pos must be a valid position).
909 void QUEUE_RemoveMsg( MESSAGEQUEUE * msgQueue, QMSG *qmsg )
911 EnterCriticalSection( &msgQueue->cSection );
913 /* set the linked list */
915 qmsg->prevMsg->nextMsg = qmsg->nextMsg;
918 qmsg->nextMsg->prevMsg = qmsg->prevMsg;
920 if (msgQueue->firstMsg == qmsg)
921 msgQueue->firstMsg = qmsg->nextMsg;
923 if (msgQueue->lastMsg == qmsg)
924 msgQueue->lastMsg = qmsg->prevMsg;
926 /* deallocate the memory for the message */
927 HeapFree( SystemHeap, 0, qmsg );
929 msgQueue->msgCount--;
930 if (!msgQueue->msgCount) msgQueue->wakeBits &= ~QS_POSTMESSAGE;
932 LeaveCriticalSection( &msgQueue->cSection );
936 /***********************************************************************
939 * Wake a queue upon reception of a hardware event.
941 static void QUEUE_WakeSomeone( UINT32 message )
947 MESSAGEQUEUE *queue = NULL;
950 hQueue = hCursorQueue;
952 if( (message >= WM_KEYFIRST) && (message <= WM_KEYLAST) )
956 hQueue = hActiveQueue;
960 wakeBit = (message == WM_MOUSEMOVE) ? QS_MOUSEMOVE : QS_MOUSEBUTTON;
961 if( (hwnd = GetCapture32()) )
962 if( (wndPtr = WIN_FindWndPtr( hwnd )) )
964 hQueue = wndPtr->hmemTaskQ;
968 if( (hwnd = GetSysModalWindow16()) )
970 if( (wndPtr = WIN_FindWndPtr( hwnd )) )
971 hQueue = wndPtr->hmemTaskQ;
975 queue = QUEUE_Lock( hQueue );
979 queue = QUEUE_Lock( hFirstQueue );
982 if (queue->wakeMask & wakeBit) break;
985 queue = QUEUE_Lock( queue->next );
989 WARN(msg, "couldn't find queue\n");
994 QUEUE_SetWakeBit( queue, wakeBit );
996 QUEUE_Unlock( queue );
1000 /***********************************************************************
1003 * Add an event to the system message queue.
1004 * Note: the position is relative to the desktop window.
1006 void hardware_event( WORD message, WORD wParam, LONG lParam,
1007 int xPos, int yPos, DWORD time, DWORD extraInfo )
1010 QMSG *qmsg = sysMsgQueue->lastMsg;
1013 if (!sysMsgQueue) return;
1015 /* Merge with previous event if possible */
1017 if ((message == WM_MOUSEMOVE) && sysMsgQueue->lastMsg)
1019 msg = &(sysMsgQueue->lastMsg->msg);
1021 if ((msg->message == message) && (msg->wParam == wParam))
1024 qmsg = sysMsgQueue->lastMsg;
1031 /* Should I limit the number of message in
1032 the system message queue??? */
1034 /* Don't merge allocate a new msg in the global heap */
1036 if (!(qmsg = (QMSG *) HeapAlloc( SystemHeap, 0, sizeof(QMSG) ) ))
1039 /* put message at the end of the linked list */
1041 qmsg->prevMsg = sysMsgQueue->lastMsg;
1043 if (sysMsgQueue->lastMsg)
1044 sysMsgQueue->lastMsg->nextMsg = qmsg;
1046 /* set last and first anchor index in system message queue */
1047 sysMsgQueue->lastMsg = qmsg;
1048 if (!sysMsgQueue->firstMsg)
1049 sysMsgQueue->firstMsg = qmsg;
1051 sysMsgQueue->msgCount++;
1057 msg->message = message;
1058 msg->wParam = wParam;
1059 msg->lParam = lParam;
1063 qmsg->extraInfo = extraInfo;
1065 QUEUE_WakeSomeone( message );
1069 /***********************************************************************
1070 * QUEUE_GetQueueTask
1072 HTASK16 QUEUE_GetQueueTask( HQUEUE16 hQueue )
1076 MESSAGEQUEUE *queue = QUEUE_Lock( hQueue );
1080 hTask = queue->thdb->process->task;
1081 QUEUE_Unlock( queue );
1089 /***********************************************************************
1090 * QUEUE_IncPaintCount
1092 void QUEUE_IncPaintCount( HQUEUE16 hQueue )
1094 MESSAGEQUEUE *queue;
1096 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1097 queue->wPaintCount++;
1098 QUEUE_SetWakeBit( queue, QS_PAINT );
1099 QUEUE_Unlock( queue );
1103 /***********************************************************************
1104 * QUEUE_DecPaintCount
1106 void QUEUE_DecPaintCount( HQUEUE16 hQueue )
1108 MESSAGEQUEUE *queue;
1110 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1111 queue->wPaintCount--;
1112 if (!queue->wPaintCount) queue->wakeBits &= ~QS_PAINT;
1113 QUEUE_Unlock( queue );
1117 /***********************************************************************
1118 * QUEUE_IncTimerCount
1120 void QUEUE_IncTimerCount( HQUEUE16 hQueue )
1122 MESSAGEQUEUE *queue;
1124 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1125 queue->wTimerCount++;
1126 QUEUE_SetWakeBit( queue, QS_TIMER );
1127 QUEUE_Unlock( queue );
1131 /***********************************************************************
1132 * QUEUE_DecTimerCount
1134 void QUEUE_DecTimerCount( HQUEUE16 hQueue )
1136 MESSAGEQUEUE *queue;
1138 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1139 queue->wTimerCount--;
1140 if (!queue->wTimerCount) queue->wakeBits &= ~QS_TIMER;
1141 QUEUE_Unlock( queue );
1145 /***********************************************************************
1146 * PostQuitMessage16 (USER.6)
1148 void WINAPI PostQuitMessage16( INT16 exitCode )
1150 PostQuitMessage32( exitCode );
1154 /***********************************************************************
1155 * PostQuitMessage32 (USER32.421)
1157 * PostQuitMessage() posts a message to the system requesting an
1158 * application to terminate execution. As a result of this function,
1159 * the WM_QUIT message is posted to the application, and
1160 * PostQuitMessage() returns immediately. The exitCode parameter
1161 * specifies an application-defined exit code, which appears in the
1162 * _wParam_ parameter of the WM_QUIT message posted to the application.
1168 void WINAPI PostQuitMessage32( INT32 exitCode )
1170 MESSAGEQUEUE *queue;
1172 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue() ))) return;
1173 queue->wPostQMsg = TRUE;
1174 queue->wExitCode = (WORD)exitCode;
1175 QUEUE_Unlock( queue );
1179 /***********************************************************************
1180 * GetWindowTask16 (USER.224)
1182 HTASK16 WINAPI GetWindowTask16( HWND16 hwnd )
1184 WND *wndPtr = WIN_FindWndPtr( hwnd );
1186 if (!wndPtr) return 0;
1187 return QUEUE_GetQueueTask( wndPtr->hmemTaskQ );
1190 /***********************************************************************
1191 * GetWindowThreadProcessId (USER32.313)
1193 DWORD WINAPI GetWindowThreadProcessId( HWND32 hwnd, LPDWORD process )
1198 WND *wndPtr = WIN_FindWndPtr( hwnd );
1200 if (!wndPtr) return 0;
1201 htask=QUEUE_GetQueueTask( wndPtr->hmemTaskQ );
1202 tdb = (TDB*)GlobalLock16(htask);
1203 if (!tdb || !tdb->thdb) return 0;
1204 if (process) *process = PDB_TO_PROCESS_ID( tdb->thdb->process );
1205 return THDB_TO_THREAD_ID( tdb->thdb );
1209 /***********************************************************************
1210 * SetMessageQueue16 (USER.266)
1212 BOOL16 WINAPI SetMessageQueue16( INT16 size )
1214 return SetMessageQueue32( size );
1218 /***********************************************************************
1219 * SetMessageQueue32 (USER32.494)
1221 BOOL32 WINAPI SetMessageQueue32( INT32 size )
1223 /* now obsolete the message queue will be expanded dynamically
1226 /* access the queue to create it if it's not existing */
1232 /***********************************************************************
1233 * InitThreadInput (USER.409)
1235 HQUEUE16 WINAPI InitThreadInput( WORD unknown, WORD flags )
1238 MESSAGEQUEUE *queuePtr;
1240 THDB *thdb = THREAD_Current();
1245 hQueue = thdb->teb.queue;
1249 /* Create thread message queue */
1250 if( !(hQueue = QUEUE_CreateMsgQueue( TRUE )))
1252 WARN(msg, "failed!\n");
1256 /* Link new queue into list */
1257 queuePtr = (MESSAGEQUEUE *)QUEUE_Lock( hQueue );
1258 queuePtr->thdb = THREAD_Current();
1261 SetThreadQueue( 0, hQueue );
1262 thdb->teb.queue = hQueue;
1264 queuePtr->next = hFirstQueue;
1265 hFirstQueue = hQueue;
1268 QUEUE_Unlock( queuePtr );
1274 /***********************************************************************
1275 * GetQueueStatus16 (USER.334)
1277 DWORD WINAPI GetQueueStatus16( UINT16 flags )
1279 MESSAGEQUEUE *queue;
1282 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue() ))) return 0;
1283 ret = MAKELONG( queue->changeBits, queue->wakeBits );
1284 queue->changeBits = 0;
1285 QUEUE_Unlock( queue );
1287 return ret & MAKELONG( flags, flags );
1290 /***********************************************************************
1291 * GetQueueStatus32 (USER32.283)
1293 DWORD WINAPI GetQueueStatus32( UINT32 flags )
1295 MESSAGEQUEUE *queue;
1298 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue() ))) return 0;
1299 ret = MAKELONG( queue->changeBits, queue->wakeBits );
1300 queue->changeBits = 0;
1301 QUEUE_Unlock( queue );
1303 return ret & MAKELONG( flags, flags );
1307 /***********************************************************************
1308 * GetInputState16 (USER.335)
1310 BOOL16 WINAPI GetInputState16(void)
1312 return GetInputState32();
1315 /***********************************************************************
1316 * WaitForInputIdle (USER32.577)
1318 DWORD WINAPI WaitForInputIdle (HANDLE32 hProcess, DWORD dwTimeOut)
1320 FIXME (msg, "(hProcess=%d, dwTimeOut=%ld): stub\n", hProcess, dwTimeOut);
1322 return WAIT_TIMEOUT;
1326 /***********************************************************************
1327 * GetInputState32 (USER32.244)
1329 BOOL32 WINAPI GetInputState32(void)
1331 MESSAGEQUEUE *queue;
1334 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue() )))
1336 ret = queue->wakeBits & (QS_KEY | QS_MOUSEBUTTON);
1337 QUEUE_Unlock( queue );
1342 /***********************************************************************
1343 * UserYield (USER.332)
1345 void WINAPI UserYield(void)
1347 TDB *pCurTask = (TDB *)GlobalLock16( GetCurrentTask() );
1348 MESSAGEQUEUE *queue = (MESSAGEQUEUE *)QUEUE_Lock( pCurTask->hQueue );
1350 if ( !THREAD_IsWin16( THREAD_Current() ) )
1352 FIXME(task, "called for Win32 thread (%04x)!\n", THREAD_Current()->teb_sel);
1353 QUEUE_Unlock( queue );
1357 /* Handle sent messages */
1358 while (queue && (queue->wakeBits & QS_SENDMESSAGE))
1359 QUEUE_ReceiveMessage( queue );
1361 QUEUE_Unlock( queue );
1365 queue = (MESSAGEQUEUE *)QUEUE_Lock( pCurTask->hQueue );
1366 while (queue && (queue->wakeBits & QS_SENDMESSAGE))
1367 QUEUE_ReceiveMessage( queue );
1369 QUEUE_Unlock( queue );
1372 /***********************************************************************
1373 * GetMessagePos (USER.119) (USER32.272)
1375 * The GetMessagePos() function returns a long value representing a
1376 * cursor position, in screen coordinates, when the last message
1377 * retrieved by the GetMessage() function occurs. The x-coordinate is
1378 * in the low-order word of the return value, the y-coordinate is in
1379 * the high-order word. The application can use the MAKEPOINT()
1380 * macro to obtain a POINT structure from the return value.
1382 * For the current cursor position, use GetCursorPos().
1386 * Cursor position of last message on success, zero on failure.
1393 DWORD WINAPI GetMessagePos(void)
1395 MESSAGEQUEUE *queue;
1398 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue() ))) return 0;
1399 ret = queue->GetMessagePosVal;
1400 QUEUE_Unlock( queue );
1406 /***********************************************************************
1407 * GetMessageTime (USER.120) (USER32.273)
1409 * GetMessageTime() returns the message time for the last message
1410 * retrieved by the function. The time is measured in milliseconds with
1411 * the same offset as GetTickCount().
1413 * Since the tick count wraps, this is only useful for moderately short
1414 * relative time comparisons.
1418 * Time of last message on success, zero on failure.
1425 LONG WINAPI GetMessageTime(void)
1427 MESSAGEQUEUE *queue;
1430 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue() ))) return 0;
1431 ret = queue->GetMessageTimeVal;
1432 QUEUE_Unlock( queue );
1438 /***********************************************************************
1439 * GetMessageExtraInfo (USER.288) (USER32.271)
1441 LONG WINAPI GetMessageExtraInfo(void)
1443 MESSAGEQUEUE *queue;
1446 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue() ))) return 0;
1447 ret = queue->GetMessageExtraInfoVal;
1448 QUEUE_Unlock( queue );