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"
24 #define MAX_QUEUE_SIZE 120 /* Max. size of a message queue */
26 static HQUEUE16 hFirstQueue = 0;
27 static HQUEUE16 hExitingQueue = 0;
28 static HQUEUE16 hmemSysMsgQueue = 0;
29 static MESSAGEQUEUE *sysMsgQueue = NULL;
30 static PERQUEUEDATA *pQDataWin16 = NULL; /* Global perQData for Win16 tasks */
32 static MESSAGEQUEUE *pMouseQueue = NULL; /* Queue for last mouse message */
33 static MESSAGEQUEUE *pKbdQueue = NULL; /* Queue for last kbd message */
35 HQUEUE16 hCursorQueue = 0;
36 HQUEUE16 hActiveQueue = 0;
39 /***********************************************************************
40 * PERQDATA_CreateInstance
42 * Creates an instance of a reference counted PERQUEUEDATA element
43 * for the message queue. perQData is stored globally for 16 bit tasks.
45 * Note: We don't implement perQdata exactly the same way Windows does.
46 * Each perQData element is reference counted since it may be potentially
47 * shared by multiple message Queues (via AttachThreadInput).
48 * We only store the current values for Active, Capture and focus windows
51 PERQUEUEDATA * PERQDATA_CreateInstance( )
59 /* Share a single instance of perQData for all 16 bit tasks */
60 if ( ( bIsWin16 = THREAD_IsWin16( THREAD_Current() ) ) )
62 /* If previously allocated, just bump up ref count */
65 PERQDATA_Addref( pQDataWin16 );
70 /* Allocate PERQUEUEDATA from the system heap */
71 if (!( pQData = (PERQUEUEDATA *) HeapAlloc( SystemHeap, 0,
72 sizeof(PERQUEUEDATA) ) ))
76 pQData->hWndCapture = pQData->hWndFocus = pQData->hWndActive = 0;
77 pQData->ulRefCount = 1;
78 pQData->nCaptureHT = HTCLIENT;
80 /* Note: We have an independent critical section for the per queue data
81 * since this may be shared by different threads. see AttachThreadInput()
83 InitializeCriticalSection( &pQData->cSection );
85 /* Save perQData globally for 16 bit tasks */
93 /***********************************************************************
96 * Increment reference count for the PERQUEUEDATA instance
97 * Returns reference count for debugging purposes
99 ULONG PERQDATA_Addref( PERQUEUEDATA *pQData )
101 assert(pQData != 0 );
102 TRACE(msg,"(): current refcount %lu ...\n", pQData->ulRefCount);
104 EnterCriticalSection( &pQData->cSection );
105 ++pQData->ulRefCount;
106 LeaveCriticalSection( &pQData->cSection );
108 return pQData->ulRefCount;
112 /***********************************************************************
115 * Release a reference to a PERQUEUEDATA instance.
116 * Destroy the instance if no more references exist
117 * Returns reference count for debugging purposes
119 ULONG PERQDATA_Release( PERQUEUEDATA *pQData )
121 assert(pQData != 0 );
122 TRACE(msg,"(): current refcount %lu ...\n",
123 (LONG)pQData->ulRefCount );
125 EnterCriticalSection( &pQData->cSection );
126 if ( --pQData->ulRefCount == 0 )
128 LeaveCriticalSection( &pQData->cSection );
129 DeleteCriticalSection( &pQData->cSection );
131 TRACE(msg,"(): deleting PERQUEUEDATA instance ...\n" );
133 /* Deleting our global 16 bit perQData? */
134 if ( pQData == pQDataWin16 )
137 /* Free the PERQUEUEDATA instance */
138 HeapFree( SystemHeap, 0, pQData );
142 LeaveCriticalSection( &pQData->cSection );
144 return pQData->ulRefCount;
148 /***********************************************************************
149 * PERQDATA_GetFocusWnd
151 * Get the focus hwnd member in a threadsafe manner
153 HWND PERQDATA_GetFocusWnd( PERQUEUEDATA *pQData )
156 assert(pQData != 0 );
158 EnterCriticalSection( &pQData->cSection );
159 hWndFocus = pQData->hWndFocus;
160 LeaveCriticalSection( &pQData->cSection );
166 /***********************************************************************
167 * PERQDATA_SetFocusWnd
169 * Set the focus hwnd member in a threadsafe manner
171 HWND PERQDATA_SetFocusWnd( PERQUEUEDATA *pQData, HWND hWndFocus )
174 assert(pQData != 0 );
176 EnterCriticalSection( &pQData->cSection );
177 hWndFocusPrv = pQData->hWndFocus;
178 pQData->hWndFocus = hWndFocus;
179 LeaveCriticalSection( &pQData->cSection );
185 /***********************************************************************
186 * PERQDATA_GetActiveWnd
188 * Get the active hwnd member in a threadsafe manner
190 HWND PERQDATA_GetActiveWnd( PERQUEUEDATA *pQData )
193 assert(pQData != 0 );
195 EnterCriticalSection( &pQData->cSection );
196 hWndActive = pQData->hWndActive;
197 LeaveCriticalSection( &pQData->cSection );
203 /***********************************************************************
204 * PERQDATA_SetActiveWnd
206 * Set the active focus hwnd member in a threadsafe manner
208 HWND PERQDATA_SetActiveWnd( PERQUEUEDATA *pQData, HWND hWndActive )
211 assert(pQData != 0 );
213 EnterCriticalSection( &pQData->cSection );
214 hWndActivePrv = pQData->hWndActive;
215 pQData->hWndActive = hWndActive;
216 LeaveCriticalSection( &pQData->cSection );
218 return hWndActivePrv;
222 /***********************************************************************
223 * PERQDATA_GetCaptureWnd
225 * Get the capture hwnd member in a threadsafe manner
227 HWND PERQDATA_GetCaptureWnd( PERQUEUEDATA *pQData )
230 assert(pQData != 0 );
232 EnterCriticalSection( &pQData->cSection );
233 hWndCapture = pQData->hWndCapture;
234 LeaveCriticalSection( &pQData->cSection );
240 /***********************************************************************
241 * PERQDATA_SetCaptureWnd
243 * Set the capture hwnd member in a threadsafe manner
245 HWND PERQDATA_SetCaptureWnd( PERQUEUEDATA *pQData, HWND hWndCapture )
248 assert(pQData != 0 );
250 EnterCriticalSection( &pQData->cSection );
251 hWndCapturePrv = pQData->hWndCapture;
252 pQData->hWndCapture = hWndCapture;
253 LeaveCriticalSection( &pQData->cSection );
255 return hWndCapturePrv;
259 /***********************************************************************
260 * PERQDATA_GetCaptureInfo
262 * Get the capture info member in a threadsafe manner
264 INT16 PERQDATA_GetCaptureInfo( PERQUEUEDATA *pQData )
267 assert(pQData != 0 );
269 EnterCriticalSection( &pQData->cSection );
270 nCaptureHT = pQData->nCaptureHT;
271 LeaveCriticalSection( &pQData->cSection );
277 /***********************************************************************
278 * PERQDATA_SetCaptureInfo
280 * Set the capture info member in a threadsafe manner
282 INT16 PERQDATA_SetCaptureInfo( PERQUEUEDATA *pQData, INT16 nCaptureHT )
285 assert(pQData != 0 );
287 EnterCriticalSection( &pQData->cSection );
288 nCaptureHTPrv = pQData->nCaptureHT;
289 pQData->nCaptureHT = nCaptureHT;
290 LeaveCriticalSection( &pQData->cSection );
292 return nCaptureHTPrv;
296 /***********************************************************************
299 * Function for getting a 32 bit pointer on queue strcture. For thread
300 * safeness programmers should use this function instead of GlobalLock to
301 * retrieve a pointer on the structure. QUEUE_Unlock should also be called
302 * when access to the queue structure is not required anymore.
304 MESSAGEQUEUE *QUEUE_Lock( HQUEUE16 hQueue )
308 HeapLock( SystemHeap ); /* FIXME: a bit overkill */
309 queue = GlobalLock16( hQueue );
310 if ( !queue || (queue->magic != QUEUE_MAGIC) )
312 HeapUnlock( SystemHeap );
317 HeapUnlock( SystemHeap );
322 /***********************************************************************
325 * Use with QUEUE_Lock to get a thread safe acces to message queue
328 void QUEUE_Unlock( MESSAGEQUEUE *queue )
332 HeapLock( SystemHeap ); /* FIXME: a bit overkill */
334 if ( --queue->lockCount == 0 )
336 DeleteCriticalSection ( &queue->cSection );
338 CloseHandle( queue->hEvent );
339 GlobalFree16( queue->self );
342 HeapUnlock( SystemHeap );
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"
362 "firstMsg: %8p smWaiting: %10p\n"
363 "lastMsg: %8p smPending: %10p\n"
364 "msgCount: %8.4x smProcessing: %10p\n"
372 pq->next, pq->thdb, pq->firstMsg, pq->smWaiting, pq->lastMsg,
373 pq->smPending, pq->msgCount, pq->smProcessing,
374 (unsigned)pq->lockCount, pq->wWinVersion,
375 pq->wPaintCount, pq->wTimerCount,
376 pq->wakeBits, pq->wakeMask, pq->hCurHook);
382 /***********************************************************************
385 void QUEUE_WalkQueues(void)
388 HQUEUE16 hQueue = hFirstQueue;
390 DUMP( "Queue Msgs Thread Task Module\n" );
393 MESSAGEQUEUE *queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue );
396 WARN( msg, "Bad queue handle %04x\n", hQueue );
399 if (!GetModuleName16( queue->thdb->process->task, module, sizeof(module )))
400 strcpy( module, "???" );
401 DUMP( "%04x %4d %p %04x %s\n", hQueue,queue->msgCount,
402 queue->thdb, queue->thdb->process->task, module );
403 hQueue = queue->next;
404 QUEUE_Unlock( queue );
410 /***********************************************************************
411 * QUEUE_IsExitingQueue
413 BOOL QUEUE_IsExitingQueue( HQUEUE16 hQueue )
415 return (hExitingQueue && (hQueue == hExitingQueue));
419 /***********************************************************************
420 * QUEUE_SetExitingQueue
422 void QUEUE_SetExitingQueue( HQUEUE16 hQueue )
424 hExitingQueue = hQueue;
428 /***********************************************************************
429 * QUEUE_CreateMsgQueue
431 * Creates a message queue. Doesn't link it into queue list!
433 static HQUEUE16 QUEUE_CreateMsgQueue( BOOL16 bCreatePerQData )
436 MESSAGEQUEUE * msgQueue;
437 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
439 TRACE(msg,"(): Creating message queue...\n");
441 if (!(hQueue = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT,
442 sizeof(MESSAGEQUEUE) )))
445 msgQueue = (MESSAGEQUEUE *) GlobalLock16( hQueue );
449 msgQueue->self = hQueue;
450 msgQueue->wakeBits = msgQueue->changeBits = 0;
451 msgQueue->wWinVersion = pTask ? pTask->version : 0;
453 InitializeCriticalSection( &msgQueue->cSection );
454 MakeCriticalSectionGlobal( &msgQueue->cSection );
456 /* Create an Event object for waiting on message, used by win32 thread
458 if ( !THREAD_IsWin16( THREAD_Current() ) )
460 msgQueue->hEvent = CreateEventA( NULL, FALSE, FALSE, NULL);
462 if (msgQueue->hEvent == 0)
464 WARN(msg, "CreateEvent32A is not able to create an event object");
467 msgQueue->hEvent = ConvertToGlobalHandle( msgQueue->hEvent );
470 msgQueue->hEvent = 0;
472 msgQueue->lockCount = 1;
473 msgQueue->magic = QUEUE_MAGIC;
475 /* Create and initialize our per queue data */
476 msgQueue->pQData = bCreatePerQData ? PERQDATA_CreateInstance() : NULL;
482 /***********************************************************************
485 * Try to reply to all pending sent messages on exit.
487 void QUEUE_FlushMessages( MESSAGEQUEUE *queue )
490 MESSAGEQUEUE *senderQ = 0;
494 EnterCriticalSection( &queue->cSection );
496 /* empty the list of pending SendMessage waiting to be received */
497 while (queue->smPending)
499 smsg = QUEUE_RemoveSMSG( queue, SM_PENDING_LIST, 0);
501 senderQ = (MESSAGEQUEUE*)QUEUE_Lock( smsg->hSrcQueue );
505 /* return 0, to unblock other thread */
507 smsg->flags |= SMSG_HAVE_RESULT;
508 QUEUE_SetWakeBit( senderQ, QS_SMRESULT);
510 QUEUE_Unlock( senderQ );
513 QUEUE_ClearWakeBit( queue, QS_SENDMESSAGE );
515 LeaveCriticalSection( &queue->cSection );
520 /***********************************************************************
521 * QUEUE_DeleteMsgQueue
523 * Unlinks and deletes a message queue.
525 * Note: We need to mask asynchronous events to make sure PostMessage works
526 * even in the signal handler.
528 BOOL QUEUE_DeleteMsgQueue( HQUEUE16 hQueue )
530 MESSAGEQUEUE * msgQueue = (MESSAGEQUEUE*)QUEUE_Lock(hQueue);
533 TRACE(msg,"(): Deleting message queue %04x\n", hQueue);
535 if (!hQueue || !msgQueue)
537 WARN(msg, "invalid argument.\n");
543 if( hCursorQueue == hQueue ) hCursorQueue = 0;
544 if( hActiveQueue == hQueue ) hActiveQueue = 0;
546 /* flush sent messages */
547 QUEUE_FlushMessages( msgQueue );
549 HeapLock( SystemHeap ); /* FIXME: a bit overkill */
551 /* Release per queue data if present */
552 if ( msgQueue->pQData )
554 PERQDATA_Release( msgQueue->pQData );
555 msgQueue->pQData = 0;
558 /* remove the message queue from the global link list */
559 pPrev = &hFirstQueue;
560 while (*pPrev && (*pPrev != hQueue))
562 MESSAGEQUEUE *msgQ = (MESSAGEQUEUE*)GlobalLock16(*pPrev);
565 if ( !msgQ || (msgQ->magic != QUEUE_MAGIC) )
567 /* HQUEUE link list is corrupted, try to exit gracefully */
568 WARN( msg, "HQUEUE link list corrupted!\n");
574 if (pPrev && *pPrev) *pPrev = msgQueue->next;
577 HeapUnlock( SystemHeap );
579 /* free up resource used by MESSAGEQUEUE strcture */
580 msgQueue->lockCount--;
581 QUEUE_Unlock( msgQueue );
587 /***********************************************************************
588 * QUEUE_CreateSysMsgQueue
590 * Create the system message queue, and set the double-click speed.
591 * Must be called only once.
593 BOOL QUEUE_CreateSysMsgQueue( int size )
595 /* Note: We dont need perQ data for the system message queue */
596 if (!(hmemSysMsgQueue = QUEUE_CreateMsgQueue( FALSE )))
599 sysMsgQueue = (MESSAGEQUEUE *) GlobalLock16( hmemSysMsgQueue );
604 /***********************************************************************
607 MESSAGEQUEUE *QUEUE_GetSysQueue(void)
613 /***********************************************************************
616 * See "Windows Internals", p.449
618 void QUEUE_SetWakeBit( MESSAGEQUEUE *queue, WORD bit )
620 TRACE(msg,"queue = %04x (wm=%04x), bit = %04x\n",
621 queue->self, queue->wakeMask, bit );
623 if (bit & QS_MOUSE) pMouseQueue = queue;
624 if (bit & QS_KEY) pKbdQueue = queue;
625 queue->changeBits |= bit;
626 queue->wakeBits |= bit;
627 if (queue->wakeMask & bit)
631 /* Wake up thread waiting for message */
632 if ( THREAD_IsWin16( queue->thdb ) )
633 PostEvent16( queue->thdb->process->task );
636 SetEvent( queue->hEvent );
642 /***********************************************************************
645 void QUEUE_ClearWakeBit( MESSAGEQUEUE *queue, WORD bit )
647 queue->changeBits &= ~bit;
648 queue->wakeBits &= ~bit;
652 /***********************************************************************
655 * See "Windows Internals", p.447
658 * 0 if exit with timeout
661 int QUEUE_WaitBits( WORD bits, DWORD timeout )
666 TRACE(msg,"q %04x waiting for %04x\n", GetFastQueue16(), bits);
668 if ( THREAD_IsWin16( THREAD_Current() ) && (timeout != INFINITE) )
669 curTime = GetTickCount();
671 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
675 if (queue->changeBits & bits)
677 /* One of the bits is set; we can return */
679 QUEUE_Unlock( queue );
682 if (queue->wakeBits & QS_SENDMESSAGE)
684 /* Process the sent message immediately */
687 QUEUE_ReceiveMessage( queue );
688 continue; /* nested sm crux */
691 queue->wakeMask = bits | QS_SENDMESSAGE;
692 if(queue->changeBits & bits)
697 TRACE(msg,"%04x) wakeMask is %04x, waiting\n", queue->self, queue->wakeMask);
699 if ( !THREAD_IsWin16( THREAD_Current() ) )
701 /* win32 thread, use WaitForMultipleObjects */
702 MsgWaitForMultipleObjects( 0, NULL, FALSE, timeout, queue->wakeMask );
706 if ( timeout == INFINITE )
707 WaitEvent16( 0 ); /* win 16 thread, use WaitEvent */
710 /* check for timeout, then give control to other tasks */
711 if (GetTickCount() - curTime > timeout)
714 QUEUE_Unlock( queue );
715 return 0; /* exit with timeout */
724 /***********************************************************************
727 * This routine is called when a SMSG need to be added to one of the three
728 * SM list. (SM_PROCESSING_LIST, SM_PENDING_LIST, SM_WAITING_LIST)
730 BOOL QUEUE_AddSMSG( MESSAGEQUEUE *queue, int list, SMSG *smsg )
732 TRACE(sendmsg,"queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
733 smsg, SPY_GetMsgName(smsg->msg));
737 case SM_PROCESSING_LIST:
738 /* don't need to be thread safe, only accessed by the
739 thread associated with the sender queue */
740 smsg->nextProcessing = queue->smProcessing;
741 queue->smProcessing = smsg;
744 case SM_WAITING_LIST:
745 /* don't need to be thread safe, only accessed by the
746 thread associated with the receiver queue */
747 smsg->nextWaiting = queue->smWaiting;
748 queue->smWaiting = smsg;
751 case SM_PENDING_LIST:
753 /* make it thread safe, could be accessed by the sender and
757 EnterCriticalSection( &queue->cSection );
758 smsg->nextPending = NULL;
759 prev = &queue->smPending;
761 prev = &(*prev)->nextPending;
763 LeaveCriticalSection( &queue->cSection );
765 QUEUE_SetWakeBit( queue, QS_SENDMESSAGE );
770 WARN(sendmsg, "Invalid list: %d", list);
778 /***********************************************************************
781 * This routine is called when a SMSG need to be remove from one of the three
782 * SM list. (SM_PROCESSING_LIST, SM_PENDING_LIST, SM_WAITING_LIST)
783 * If smsg == 0, remove the first smsg from the specified list
785 SMSG *QUEUE_RemoveSMSG( MESSAGEQUEUE *queue, int list, SMSG *smsg )
790 case SM_PROCESSING_LIST:
791 /* don't need to be thread safe, only accessed by the
792 thread associated with the sender queue */
794 /* if smsg is equal to null, it means the first in the list */
796 smsg = queue->smProcessing;
798 TRACE(sendmsg,"queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
799 smsg, SPY_GetMsgName(smsg->msg));
800 /* In fact SM_PROCESSING_LIST is a stack, and smsg
801 should be always at the top of the list */
802 if ( (smsg != queue->smProcessing) || !queue->smProcessing )
804 ERR( sendmsg, "smsg not at the top of Processing list, smsg=0x%p queue=0x%p", smsg, queue);
809 queue->smProcessing = smsg->nextProcessing;
810 smsg->nextProcessing = 0;
814 case SM_WAITING_LIST:
815 /* don't need to be thread safe, only accessed by the
816 thread associated with the receiver queue */
818 /* if smsg is equal to null, it means the first in the list */
820 smsg = queue->smWaiting;
822 TRACE(sendmsg,"queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
823 smsg, SPY_GetMsgName(smsg->msg));
824 /* In fact SM_WAITING_LIST is a stack, and smsg
825 should be always at the top of the list */
826 if ( (smsg != queue->smWaiting) || !queue->smWaiting )
828 ERR( sendmsg, "smsg not at the top of Waiting list, smsg=0x%p queue=0x%p", smsg, queue);
833 queue->smWaiting = smsg->nextWaiting;
834 smsg->nextWaiting = 0;
838 case SM_PENDING_LIST:
839 /* make it thread safe, could be accessed by the sender and
841 EnterCriticalSection( &queue->cSection );
843 if (!smsg || !queue->smPending)
844 smsg = queue->smPending;
847 ERR( sendmsg, "should always remove the top one in Pending list, smsg=0x%p queue=0x%p", smsg, queue);
851 TRACE(sendmsg,"queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
852 smsg, SPY_GetMsgName(smsg->msg));
854 queue->smPending = smsg->nextPending;
855 smsg->nextPending = 0;
857 /* if no more SMSG in Pending list, clear QS_SENDMESSAGE flag */
858 if (!queue->smPending)
859 QUEUE_ClearWakeBit( queue, QS_SENDMESSAGE );
861 LeaveCriticalSection( &queue->cSection );
865 WARN(sendmsg, "Invalid list: %d", list);
873 /***********************************************************************
874 * QUEUE_ReceiveMessage
876 * This routine is called when a sent message is waiting for the queue.
878 void QUEUE_ReceiveMessage( MESSAGEQUEUE *queue )
882 MESSAGEQUEUE *senderQ;
884 TRACE(sendmsg, "queue %04x\n", queue->self );
886 if ( !(queue->wakeBits & QS_SENDMESSAGE) && queue->smPending )
888 TRACE(sendmsg,"\trcm: nothing to do\n");
892 /* remove smsg on the top of the pending list and put it in the processing list */
893 smsg = QUEUE_RemoveSMSG(queue, SM_PENDING_LIST, 0);
894 QUEUE_AddSMSG(queue, SM_WAITING_LIST, smsg);
896 TRACE(sendmsg,"RM: %s [%04x] (%04x -> %04x)\n",
897 SPY_GetMsgName(smsg->msg), smsg->msg, smsg->hSrcQueue, smsg->hDstQueue );
899 if (IsWindow( smsg->hWnd ))
901 WND *wndPtr = WIN_FindWndPtr( smsg->hWnd );
902 DWORD extraInfo = queue->GetMessageExtraInfoVal; /* save ExtraInfo */
904 /* use sender queue extra info value while calling the window proc */
905 senderQ = (MESSAGEQUEUE*)QUEUE_Lock( smsg->hSrcQueue );
908 queue->GetMessageExtraInfoVal = senderQ->GetMessageExtraInfoVal;
909 QUEUE_Unlock( senderQ );
912 /* call the right version of CallWindowProcXX */
913 if (smsg->flags & SMSG_WIN32)
915 TRACE(sendmsg, "\trcm: msg is Win32\n" );
916 if (smsg->flags & SMSG_UNICODE)
917 result = CallWindowProcW( wndPtr->winproc,
918 smsg->hWnd, smsg->msg,
919 smsg->wParam, smsg->lParam );
921 result = CallWindowProcA( wndPtr->winproc,
922 smsg->hWnd, smsg->msg,
923 smsg->wParam, smsg->lParam );
925 else /* Win16 message */
926 result = CallWindowProc16( (WNDPROC16)wndPtr->winproc,
929 LOWORD (smsg->wParam),
932 queue->GetMessageExtraInfoVal = extraInfo; /* Restore extra info */
933 WIN_ReleaseWndPtr(wndPtr);
934 TRACE(sendmsg,"result = %08x\n", (unsigned)result );
936 else WARN(sendmsg, "\trcm: bad hWnd\n");
939 /* set SMSG_SENDING_REPLY flag to tell ReplyMessage16, it's not
941 smsg->flags |= SMSG_SENDING_REPLY;
942 ReplyMessage( result );
944 TRACE( sendmsg,"done! \n" );
949 /***********************************************************************
952 * Add a message to the queue. Return FALSE if queue is full.
954 BOOL QUEUE_AddMsg( HQUEUE16 hQueue, MSG *msg, DWORD extraInfo )
956 MESSAGEQUEUE *msgQueue;
960 if (!(msgQueue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return FALSE;
962 /* allocate new message in global heap for now */
963 if (!(qmsg = (QMSG *) HeapAlloc( SystemHeap, 0, sizeof(QMSG) ) ))
965 QUEUE_Unlock( msgQueue );
969 EnterCriticalSection( &msgQueue->cSection );
973 qmsg->extraInfo = extraInfo;
975 /* insert the message in the link list */
977 qmsg->prevMsg = msgQueue->lastMsg;
979 if (msgQueue->lastMsg)
980 msgQueue->lastMsg->nextMsg = qmsg;
982 /* update first and last anchor in message queue */
983 msgQueue->lastMsg = qmsg;
984 if (!msgQueue->firstMsg)
985 msgQueue->firstMsg = qmsg;
987 msgQueue->msgCount++;
989 LeaveCriticalSection( &msgQueue->cSection );
991 QUEUE_SetWakeBit( msgQueue, QS_POSTMESSAGE );
992 QUEUE_Unlock( msgQueue );
999 /***********************************************************************
1002 * Find a message matching the given parameters. Return -1 if none available.
1004 QMSG* QUEUE_FindMsg( MESSAGEQUEUE * msgQueue, HWND hwnd, int first, int last )
1008 EnterCriticalSection( &msgQueue->cSection );
1010 if (!msgQueue->msgCount)
1012 else if (!hwnd && !first && !last)
1013 qmsg = msgQueue->firstMsg;
1016 /* look in linked list for message matching first and last criteria */
1017 for (qmsg = msgQueue->firstMsg; qmsg; qmsg = qmsg->nextMsg)
1019 MSG *msg = &(qmsg->msg);
1021 if (!hwnd || (msg->hwnd == hwnd))
1023 if (!first && !last)
1024 break; /* found it */
1026 if ((msg->message >= first) && (!last || (msg->message <= last)))
1027 break; /* found it */
1032 LeaveCriticalSection( &msgQueue->cSection );
1039 /***********************************************************************
1042 * Remove a message from the queue (pos must be a valid position).
1044 void QUEUE_RemoveMsg( MESSAGEQUEUE * msgQueue, QMSG *qmsg )
1046 EnterCriticalSection( &msgQueue->cSection );
1048 /* set the linked list */
1050 qmsg->prevMsg->nextMsg = qmsg->nextMsg;
1053 qmsg->nextMsg->prevMsg = qmsg->prevMsg;
1055 if (msgQueue->firstMsg == qmsg)
1056 msgQueue->firstMsg = qmsg->nextMsg;
1058 if (msgQueue->lastMsg == qmsg)
1059 msgQueue->lastMsg = qmsg->prevMsg;
1061 /* deallocate the memory for the message */
1062 HeapFree( SystemHeap, 0, qmsg );
1064 msgQueue->msgCount--;
1065 if (!msgQueue->msgCount) msgQueue->wakeBits &= ~QS_POSTMESSAGE;
1067 LeaveCriticalSection( &msgQueue->cSection );
1071 /***********************************************************************
1074 * Wake a queue upon reception of a hardware event.
1076 static void QUEUE_WakeSomeone( UINT message )
1081 HQUEUE16 hQueue = 0;
1082 MESSAGEQUEUE *queue = NULL;
1085 hQueue = hCursorQueue;
1087 if( (message >= WM_KEYFIRST) && (message <= WM_KEYLAST) )
1091 hQueue = hActiveQueue;
1095 wakeBit = (message == WM_MOUSEMOVE) ? QS_MOUSEMOVE : QS_MOUSEBUTTON;
1096 if( (hwnd = GetCapture()) )
1097 if( (wndPtr = WIN_FindWndPtr( hwnd )) )
1099 hQueue = wndPtr->hmemTaskQ;
1100 WIN_ReleaseWndPtr(wndPtr);
1104 if( (hwnd = GetSysModalWindow16()) )
1106 if( (wndPtr = WIN_FindWndPtr( hwnd )) )
1108 hQueue = wndPtr->hmemTaskQ;
1109 WIN_ReleaseWndPtr(wndPtr);
1114 queue = QUEUE_Lock( hQueue );
1118 queue = QUEUE_Lock( hFirstQueue );
1121 if (queue->wakeMask & wakeBit) break;
1123 QUEUE_Unlock(queue);
1124 queue = QUEUE_Lock( queue->next );
1128 WARN(msg, "couldn't find queue\n");
1133 QUEUE_SetWakeBit( queue, wakeBit );
1135 QUEUE_Unlock( queue );
1139 /***********************************************************************
1142 * Add an event to the system message queue.
1143 * Note: the position is relative to the desktop window.
1145 void hardware_event( WORD message, WORD wParam, LONG lParam,
1146 int xPos, int yPos, DWORD time, DWORD extraInfo )
1152 if (!sysMsgQueue) return;
1154 EnterCriticalSection( &sysMsgQueue->cSection );
1156 /* Merge with previous event if possible */
1157 qmsg = sysMsgQueue->lastMsg;
1159 if ((message == WM_MOUSEMOVE) && sysMsgQueue->lastMsg)
1161 msg = &(sysMsgQueue->lastMsg->msg);
1163 if ((msg->message == message) && (msg->wParam == wParam))
1166 qmsg = sysMsgQueue->lastMsg;
1173 /* Should I limit the number of message in
1174 the system message queue??? */
1176 /* Don't merge allocate a new msg in the global heap */
1178 if (!(qmsg = (QMSG *) HeapAlloc( SystemHeap, 0, sizeof(QMSG) ) ))
1180 LeaveCriticalSection( &sysMsgQueue->cSection );
1184 /* put message at the end of the linked list */
1186 qmsg->prevMsg = sysMsgQueue->lastMsg;
1188 if (sysMsgQueue->lastMsg)
1189 sysMsgQueue->lastMsg->nextMsg = qmsg;
1191 /* set last and first anchor index in system message queue */
1192 sysMsgQueue->lastMsg = qmsg;
1193 if (!sysMsgQueue->firstMsg)
1194 sysMsgQueue->firstMsg = qmsg;
1196 sysMsgQueue->msgCount++;
1202 msg->message = message;
1203 msg->wParam = wParam;
1204 msg->lParam = lParam;
1208 qmsg->extraInfo = extraInfo;
1210 LeaveCriticalSection( &sysMsgQueue->cSection );
1212 QUEUE_WakeSomeone( message );
1216 /***********************************************************************
1217 * QUEUE_GetQueueTask
1219 HTASK16 QUEUE_GetQueueTask( HQUEUE16 hQueue )
1223 MESSAGEQUEUE *queue = QUEUE_Lock( hQueue );
1227 hTask = queue->thdb->process->task;
1228 QUEUE_Unlock( queue );
1236 /***********************************************************************
1237 * QUEUE_IncPaintCount
1239 void QUEUE_IncPaintCount( HQUEUE16 hQueue )
1241 MESSAGEQUEUE *queue;
1243 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1244 queue->wPaintCount++;
1245 QUEUE_SetWakeBit( queue, QS_PAINT );
1246 QUEUE_Unlock( queue );
1250 /***********************************************************************
1251 * QUEUE_DecPaintCount
1253 void QUEUE_DecPaintCount( HQUEUE16 hQueue )
1255 MESSAGEQUEUE *queue;
1257 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1258 queue->wPaintCount--;
1259 if (!queue->wPaintCount) queue->wakeBits &= ~QS_PAINT;
1260 QUEUE_Unlock( queue );
1264 /***********************************************************************
1265 * QUEUE_IncTimerCount
1267 void QUEUE_IncTimerCount( HQUEUE16 hQueue )
1269 MESSAGEQUEUE *queue;
1271 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1272 queue->wTimerCount++;
1273 QUEUE_SetWakeBit( queue, QS_TIMER );
1274 QUEUE_Unlock( queue );
1278 /***********************************************************************
1279 * QUEUE_DecTimerCount
1281 void QUEUE_DecTimerCount( HQUEUE16 hQueue )
1283 MESSAGEQUEUE *queue;
1285 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1286 queue->wTimerCount--;
1287 if (!queue->wTimerCount) queue->wakeBits &= ~QS_TIMER;
1288 QUEUE_Unlock( queue );
1292 /***********************************************************************
1293 * PostQuitMessage16 (USER.6)
1295 void WINAPI PostQuitMessage16( INT16 exitCode )
1297 PostQuitMessage( exitCode );
1301 /***********************************************************************
1302 * PostQuitMessage32 (USER32.421)
1304 * PostQuitMessage() posts a message to the system requesting an
1305 * application to terminate execution. As a result of this function,
1306 * the WM_QUIT message is posted to the application, and
1307 * PostQuitMessage() returns immediately. The exitCode parameter
1308 * specifies an application-defined exit code, which appears in the
1309 * _wParam_ parameter of the WM_QUIT message posted to the application.
1315 void WINAPI PostQuitMessage( INT exitCode )
1317 MESSAGEQUEUE *queue;
1319 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return;
1320 queue->wPostQMsg = TRUE;
1321 queue->wExitCode = (WORD)exitCode;
1322 QUEUE_Unlock( queue );
1326 /***********************************************************************
1327 * GetWindowTask16 (USER.224)
1329 HTASK16 WINAPI GetWindowTask16( HWND16 hwnd )
1332 WND *wndPtr = WIN_FindWndPtr( hwnd );
1334 if (!wndPtr) return 0;
1335 retvalue = QUEUE_GetQueueTask( wndPtr->hmemTaskQ );
1336 WIN_ReleaseWndPtr(wndPtr);
1340 /***********************************************************************
1341 * GetWindowThreadProcessId (USER32.313)
1343 DWORD WINAPI GetWindowThreadProcessId( HWND hwnd, LPDWORD process )
1348 WND *wndPtr = WIN_FindWndPtr( hwnd );
1350 if (!wndPtr) return 0;
1351 htask=QUEUE_GetQueueTask( wndPtr->hmemTaskQ );
1352 WIN_ReleaseWndPtr(wndPtr);
1353 tdb = (TDB*)GlobalLock16(htask);
1354 if (!tdb || !tdb->thdb) return 0;
1355 if (process) *process = (DWORD)tdb->thdb->process->server_pid;
1356 return (DWORD)tdb->thdb->server_tid;
1360 /***********************************************************************
1361 * SetMessageQueue16 (USER.266)
1363 BOOL16 WINAPI SetMessageQueue16( INT16 size )
1365 return SetMessageQueue( size );
1369 /***********************************************************************
1370 * SetMessageQueue32 (USER32.494)
1372 BOOL WINAPI SetMessageQueue( INT size )
1374 /* now obsolete the message queue will be expanded dynamically
1377 /* access the queue to create it if it's not existing */
1383 /***********************************************************************
1384 * InitThreadInput (USER.409)
1386 HQUEUE16 WINAPI InitThreadInput16( WORD unknown, WORD flags )
1389 MESSAGEQUEUE *queuePtr;
1391 THDB *thdb = THREAD_Current();
1396 hQueue = thdb->teb.queue;
1400 /* Create thread message queue */
1401 if( !(hQueue = QUEUE_CreateMsgQueue( TRUE )))
1403 WARN(msg, "failed!\n");
1407 /* Link new queue into list */
1408 queuePtr = (MESSAGEQUEUE *)QUEUE_Lock( hQueue );
1409 queuePtr->thdb = THREAD_Current();
1411 HeapLock( SystemHeap ); /* FIXME: a bit overkill */
1412 SetThreadQueue16( 0, hQueue );
1413 thdb->teb.queue = hQueue;
1415 queuePtr->next = hFirstQueue;
1416 hFirstQueue = hQueue;
1417 HeapUnlock( SystemHeap );
1419 QUEUE_Unlock( queuePtr );
1425 /***********************************************************************
1426 * GetQueueStatus16 (USER.334)
1428 DWORD WINAPI GetQueueStatus16( UINT16 flags )
1430 MESSAGEQUEUE *queue;
1433 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1434 ret = MAKELONG( queue->changeBits, queue->wakeBits );
1435 queue->changeBits = 0;
1436 QUEUE_Unlock( queue );
1438 return ret & MAKELONG( flags, flags );
1441 /***********************************************************************
1442 * GetQueueStatus32 (USER32.283)
1444 DWORD WINAPI GetQueueStatus( UINT flags )
1446 MESSAGEQUEUE *queue;
1449 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1450 ret = MAKELONG( queue->changeBits, queue->wakeBits );
1451 queue->changeBits = 0;
1452 QUEUE_Unlock( queue );
1454 return ret & MAKELONG( flags, flags );
1458 /***********************************************************************
1459 * GetInputState16 (USER.335)
1461 BOOL16 WINAPI GetInputState16(void)
1463 return GetInputState();
1466 /***********************************************************************
1467 * WaitForInputIdle (USER32.577)
1469 DWORD WINAPI WaitForInputIdle (HANDLE hProcess, DWORD dwTimeOut)
1471 FIXME (msg, "(hProcess=%d, dwTimeOut=%ld): stub\n", hProcess, dwTimeOut);
1473 return WAIT_TIMEOUT;
1477 /***********************************************************************
1478 * GetInputState32 (USER32.244)
1480 BOOL WINAPI GetInputState(void)
1482 MESSAGEQUEUE *queue;
1485 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() )))
1487 ret = queue->wakeBits & (QS_KEY | QS_MOUSEBUTTON);
1488 QUEUE_Unlock( queue );
1493 /***********************************************************************
1494 * UserYield (USER.332)
1496 void WINAPI UserYield16(void)
1498 MESSAGEQUEUE *queue;
1500 /* Handle sent messages */
1501 queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() );
1503 while (queue && (queue->wakeBits & QS_SENDMESSAGE))
1504 QUEUE_ReceiveMessage( queue );
1506 QUEUE_Unlock( queue );
1509 if ( THREAD_IsWin16( THREAD_Current() ) )
1515 ReleaseThunkLock(&count);
1516 RestoreThunkLock(count);
1519 /* Handle sent messages again */
1520 queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() );
1522 while (queue && (queue->wakeBits & QS_SENDMESSAGE))
1523 QUEUE_ReceiveMessage( queue );
1525 QUEUE_Unlock( queue );
1528 /***********************************************************************
1529 * GetMessagePos (USER.119) (USER32.272)
1531 * The GetMessagePos() function returns a long value representing a
1532 * cursor position, in screen coordinates, when the last message
1533 * retrieved by the GetMessage() function occurs. The x-coordinate is
1534 * in the low-order word of the return value, the y-coordinate is in
1535 * the high-order word. The application can use the MAKEPOINT()
1536 * macro to obtain a POINT structure from the return value.
1538 * For the current cursor position, use GetCursorPos().
1542 * Cursor position of last message on success, zero on failure.
1549 DWORD WINAPI GetMessagePos(void)
1551 MESSAGEQUEUE *queue;
1554 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1555 ret = queue->GetMessagePosVal;
1556 QUEUE_Unlock( queue );
1562 /***********************************************************************
1563 * GetMessageTime (USER.120) (USER32.273)
1565 * GetMessageTime() returns the message time for the last message
1566 * retrieved by the function. The time is measured in milliseconds with
1567 * the same offset as GetTickCount().
1569 * Since the tick count wraps, this is only useful for moderately short
1570 * relative time comparisons.
1574 * Time of last message on success, zero on failure.
1581 LONG WINAPI GetMessageTime(void)
1583 MESSAGEQUEUE *queue;
1586 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1587 ret = queue->GetMessageTimeVal;
1588 QUEUE_Unlock( queue );
1594 /***********************************************************************
1595 * GetMessageExtraInfo (USER.288) (USER32.271)
1597 LONG WINAPI GetMessageExtraInfo(void)
1599 MESSAGEQUEUE *queue;
1602 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1603 ret = queue->GetMessageExtraInfoVal;
1604 QUEUE_Unlock( queue );