Added a missing LeaveCriticalSection.
[wine.git] / windows / queue.c
blob595be9c2929127c839e3e543c9106f429a78ee0b
1 /* * Message queues related functions
3 * Copyright 1993, 1994 Alexandre Julliard
4 */
6 #include <string.h>
7 #include <signal.h>
8 #include "wine/winbase16.h"
9 #include "wine/winuser16.h"
10 #include "miscemu.h"
11 #include "module.h"
12 #include "queue.h"
13 #include "task.h"
14 #include "win.h"
15 #include "clipboard.h"
16 #include "hook.h"
17 #include "heap.h"
18 #include "thread.h"
19 #include "process.h"
20 #include <assert.h>
21 #include "debug.h"
22 #include "spy.h"
24 DECLARE_DEBUG_CHANNEL(msg)
25 DECLARE_DEBUG_CHANNEL(sendmsg)
27 #define MAX_QUEUE_SIZE 120 /* Max. size of a message queue */
29 static HQUEUE16 hFirstQueue = 0;
30 static HQUEUE16 hExitingQueue = 0;
31 static HQUEUE16 hmemSysMsgQueue = 0;
32 static MESSAGEQUEUE *sysMsgQueue = NULL;
33 static PERQUEUEDATA *pQDataWin16 = NULL; /* Global perQData for Win16 tasks */
35 static MESSAGEQUEUE *pMouseQueue = NULL; /* Queue for last mouse message */
36 static MESSAGEQUEUE *pKbdQueue = NULL; /* Queue for last kbd message */
38 HQUEUE16 hCursorQueue = 0;
39 HQUEUE16 hActiveQueue = 0;
42 /***********************************************************************
43 * PERQDATA_CreateInstance
45 * Creates an instance of a reference counted PERQUEUEDATA element
46 * for the message queue. perQData is stored globally for 16 bit tasks.
48 * Note: We don't implement perQdata exactly the same way Windows does.
49 * Each perQData element is reference counted since it may be potentially
50 * shared by multiple message Queues (via AttachThreadInput).
51 * We only store the current values for Active, Capture and focus windows
52 * currently.
54 PERQUEUEDATA * PERQDATA_CreateInstance( )
56 PERQUEUEDATA *pQData;
58 BOOL16 bIsWin16 = 0;
60 TRACE(msg,"()\n");
62 /* Share a single instance of perQData for all 16 bit tasks */
63 if ( ( bIsWin16 = THREAD_IsWin16( THREAD_Current() ) ) )
65 /* If previously allocated, just bump up ref count */
66 if ( pQDataWin16 )
68 PERQDATA_Addref( pQDataWin16 );
69 return pQDataWin16;
73 /* Allocate PERQUEUEDATA from the system heap */
74 if (!( pQData = (PERQUEUEDATA *) HeapAlloc( SystemHeap, 0,
75 sizeof(PERQUEUEDATA) ) ))
76 return 0;
78 /* Initialize */
79 pQData->hWndCapture = pQData->hWndFocus = pQData->hWndActive = 0;
80 pQData->ulRefCount = 1;
81 pQData->nCaptureHT = HTCLIENT;
83 /* Note: We have an independent critical section for the per queue data
84 * since this may be shared by different threads. see AttachThreadInput()
86 InitializeCriticalSection( &pQData->cSection );
88 /* Save perQData globally for 16 bit tasks */
89 if ( bIsWin16 )
90 pQDataWin16 = pQData;
92 return pQData;
96 /***********************************************************************
97 * PERQDATA_Addref
99 * Increment reference count for the PERQUEUEDATA instance
100 * Returns reference count for debugging purposes
102 ULONG PERQDATA_Addref( PERQUEUEDATA *pQData )
104 assert(pQData != 0 );
105 TRACE(msg,"(): current refcount %lu ...\n", pQData->ulRefCount);
107 EnterCriticalSection( &pQData->cSection );
108 ++pQData->ulRefCount;
109 LeaveCriticalSection( &pQData->cSection );
111 return pQData->ulRefCount;
115 /***********************************************************************
116 * PERQDATA_Release
118 * Release a reference to a PERQUEUEDATA instance.
119 * Destroy the instance if no more references exist
120 * Returns reference count for debugging purposes
122 ULONG PERQDATA_Release( PERQUEUEDATA *pQData )
124 assert(pQData != 0 );
125 TRACE(msg,"(): current refcount %lu ...\n",
126 (LONG)pQData->ulRefCount );
128 EnterCriticalSection( &pQData->cSection );
129 if ( --pQData->ulRefCount == 0 )
131 LeaveCriticalSection( &pQData->cSection );
132 DeleteCriticalSection( &pQData->cSection );
134 TRACE(msg,"(): deleting PERQUEUEDATA instance ...\n" );
136 /* Deleting our global 16 bit perQData? */
137 if ( pQData == pQDataWin16 )
138 pQDataWin16 = 0;
140 /* Free the PERQUEUEDATA instance */
141 HeapFree( SystemHeap, 0, pQData );
143 return 0;
145 LeaveCriticalSection( &pQData->cSection );
147 return pQData->ulRefCount;
151 /***********************************************************************
152 * PERQDATA_GetFocusWnd
154 * Get the focus hwnd member in a threadsafe manner
156 HWND PERQDATA_GetFocusWnd( PERQUEUEDATA *pQData )
158 HWND hWndFocus;
159 assert(pQData != 0 );
161 EnterCriticalSection( &pQData->cSection );
162 hWndFocus = pQData->hWndFocus;
163 LeaveCriticalSection( &pQData->cSection );
165 return hWndFocus;
169 /***********************************************************************
170 * PERQDATA_SetFocusWnd
172 * Set the focus hwnd member in a threadsafe manner
174 HWND PERQDATA_SetFocusWnd( PERQUEUEDATA *pQData, HWND hWndFocus )
176 HWND hWndFocusPrv;
177 assert(pQData != 0 );
179 EnterCriticalSection( &pQData->cSection );
180 hWndFocusPrv = pQData->hWndFocus;
181 pQData->hWndFocus = hWndFocus;
182 LeaveCriticalSection( &pQData->cSection );
184 return hWndFocusPrv;
188 /***********************************************************************
189 * PERQDATA_GetActiveWnd
191 * Get the active hwnd member in a threadsafe manner
193 HWND PERQDATA_GetActiveWnd( PERQUEUEDATA *pQData )
195 HWND hWndActive;
196 assert(pQData != 0 );
198 EnterCriticalSection( &pQData->cSection );
199 hWndActive = pQData->hWndActive;
200 LeaveCriticalSection( &pQData->cSection );
202 return hWndActive;
206 /***********************************************************************
207 * PERQDATA_SetActiveWnd
209 * Set the active focus hwnd member in a threadsafe manner
211 HWND PERQDATA_SetActiveWnd( PERQUEUEDATA *pQData, HWND hWndActive )
213 HWND hWndActivePrv;
214 assert(pQData != 0 );
216 EnterCriticalSection( &pQData->cSection );
217 hWndActivePrv = pQData->hWndActive;
218 pQData->hWndActive = hWndActive;
219 LeaveCriticalSection( &pQData->cSection );
221 return hWndActivePrv;
225 /***********************************************************************
226 * PERQDATA_GetCaptureWnd
228 * Get the capture hwnd member in a threadsafe manner
230 HWND PERQDATA_GetCaptureWnd( PERQUEUEDATA *pQData )
232 HWND hWndCapture;
233 assert(pQData != 0 );
235 EnterCriticalSection( &pQData->cSection );
236 hWndCapture = pQData->hWndCapture;
237 LeaveCriticalSection( &pQData->cSection );
239 return hWndCapture;
243 /***********************************************************************
244 * PERQDATA_SetCaptureWnd
246 * Set the capture hwnd member in a threadsafe manner
248 HWND PERQDATA_SetCaptureWnd( PERQUEUEDATA *pQData, HWND hWndCapture )
250 HWND hWndCapturePrv;
251 assert(pQData != 0 );
253 EnterCriticalSection( &pQData->cSection );
254 hWndCapturePrv = pQData->hWndCapture;
255 pQData->hWndCapture = hWndCapture;
256 LeaveCriticalSection( &pQData->cSection );
258 return hWndCapturePrv;
262 /***********************************************************************
263 * PERQDATA_GetCaptureInfo
265 * Get the capture info member in a threadsafe manner
267 INT16 PERQDATA_GetCaptureInfo( PERQUEUEDATA *pQData )
269 INT16 nCaptureHT;
270 assert(pQData != 0 );
272 EnterCriticalSection( &pQData->cSection );
273 nCaptureHT = pQData->nCaptureHT;
274 LeaveCriticalSection( &pQData->cSection );
276 return nCaptureHT;
280 /***********************************************************************
281 * PERQDATA_SetCaptureInfo
283 * Set the capture info member in a threadsafe manner
285 INT16 PERQDATA_SetCaptureInfo( PERQUEUEDATA *pQData, INT16 nCaptureHT )
287 INT16 nCaptureHTPrv;
288 assert(pQData != 0 );
290 EnterCriticalSection( &pQData->cSection );
291 nCaptureHTPrv = pQData->nCaptureHT;
292 pQData->nCaptureHT = nCaptureHT;
293 LeaveCriticalSection( &pQData->cSection );
295 return nCaptureHTPrv;
299 /***********************************************************************
300 * QUEUE_Lock
302 * Function for getting a 32 bit pointer on queue strcture. For thread
303 * safeness programmers should use this function instead of GlobalLock to
304 * retrieve a pointer on the structure. QUEUE_Unlock should also be called
305 * when access to the queue structure is not required anymore.
307 MESSAGEQUEUE *QUEUE_Lock( HQUEUE16 hQueue )
309 MESSAGEQUEUE *queue;
311 HeapLock( SystemHeap ); /* FIXME: a bit overkill */
312 queue = GlobalLock16( hQueue );
313 if ( !queue || (queue->magic != QUEUE_MAGIC) )
315 HeapUnlock( SystemHeap );
316 return NULL;
319 queue->lockCount++;
320 HeapUnlock( SystemHeap );
321 return queue;
325 /***********************************************************************
326 * QUEUE_Unlock
328 * Use with QUEUE_Lock to get a thread safe access to message queue
329 * structure
331 void QUEUE_Unlock( MESSAGEQUEUE *queue )
333 if (queue)
335 HeapLock( SystemHeap ); /* FIXME: a bit overkill */
337 if ( --queue->lockCount == 0 )
339 DeleteCriticalSection ( &queue->cSection );
340 if (queue->hEvent)
341 CloseHandle( queue->hEvent );
342 GlobalFree16( queue->self );
345 HeapUnlock( SystemHeap );
350 /***********************************************************************
351 * QUEUE_DumpQueue
353 void QUEUE_DumpQueue( HQUEUE16 hQueue )
355 MESSAGEQUEUE *pq;
357 if (!(pq = (MESSAGEQUEUE*) QUEUE_Lock( hQueue )) )
359 WARN(msg, "%04x is not a queue handle\n", hQueue );
360 return;
363 DUMP( "next: %12.4x Intertask SendMessage:\n"
364 "thread: %10p ----------------------\n"
365 "firstMsg: %8p smWaiting: %10p\n"
366 "lastMsg: %8p smPending: %10p\n"
367 "msgCount: %8.4x smProcessing: %10p\n"
368 "lockCount: %7.4x\n"
369 "wWinVer: %9.4x\n"
370 "paints: %10.4x\n"
371 "timers: %10.4x\n"
372 "wakeBits: %8.4x\n"
373 "wakeMask: %8.4x\n"
374 "hCurHook: %8.4x\n",
375 pq->next, pq->thdb, pq->firstMsg, pq->smWaiting, pq->lastMsg,
376 pq->smPending, pq->msgCount, pq->smProcessing,
377 (unsigned)pq->lockCount, pq->wWinVersion,
378 pq->wPaintCount, pq->wTimerCount,
379 pq->wakeBits, pq->wakeMask, pq->hCurHook);
381 QUEUE_Unlock( pq );
385 /***********************************************************************
386 * QUEUE_WalkQueues
388 void QUEUE_WalkQueues(void)
390 char module[10];
391 HQUEUE16 hQueue = hFirstQueue;
393 DUMP( "Queue Msgs Thread Task Module\n" );
394 while (hQueue)
396 MESSAGEQUEUE *queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue );
397 if (!queue)
399 WARN( msg, "Bad queue handle %04x\n", hQueue );
400 return;
402 if (!GetModuleName16( queue->thdb->process->task, module, sizeof(module )))
403 strcpy( module, "???" );
404 DUMP( "%04x %4d %p %04x %s\n", hQueue,queue->msgCount,
405 queue->thdb, queue->thdb->process->task, module );
406 hQueue = queue->next;
407 QUEUE_Unlock( queue );
409 DUMP( "\n" );
413 /***********************************************************************
414 * QUEUE_IsExitingQueue
416 BOOL QUEUE_IsExitingQueue( HQUEUE16 hQueue )
418 return (hExitingQueue && (hQueue == hExitingQueue));
422 /***********************************************************************
423 * QUEUE_SetExitingQueue
425 void QUEUE_SetExitingQueue( HQUEUE16 hQueue )
427 hExitingQueue = hQueue;
431 /***********************************************************************
432 * QUEUE_CreateMsgQueue
434 * Creates a message queue. Doesn't link it into queue list!
436 static HQUEUE16 QUEUE_CreateMsgQueue( BOOL16 bCreatePerQData )
438 HQUEUE16 hQueue;
439 MESSAGEQUEUE * msgQueue;
440 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
442 TRACE(msg,"(): Creating message queue...\n");
444 if (!(hQueue = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT,
445 sizeof(MESSAGEQUEUE) )))
446 return 0;
448 msgQueue = (MESSAGEQUEUE *) GlobalLock16( hQueue );
449 if ( !msgQueue )
450 return 0;
452 msgQueue->self = hQueue;
453 msgQueue->wakeBits = msgQueue->changeBits = 0;
454 msgQueue->wWinVersion = pTask ? pTask->version : 0;
456 InitializeCriticalSection( &msgQueue->cSection );
457 MakeCriticalSectionGlobal( &msgQueue->cSection );
459 /* Create an Event object for waiting on message, used by win32 thread
460 only */
461 if ( !THREAD_IsWin16( THREAD_Current() ) )
463 msgQueue->hEvent = CreateEventA( NULL, FALSE, FALSE, NULL);
465 if (msgQueue->hEvent == 0)
467 WARN(msg, "CreateEvent32A is not able to create an event object");
468 return 0;
470 msgQueue->hEvent = ConvertToGlobalHandle( msgQueue->hEvent );
472 else
473 msgQueue->hEvent = 0;
475 msgQueue->lockCount = 1;
476 msgQueue->magic = QUEUE_MAGIC;
478 /* Create and initialize our per queue data */
479 msgQueue->pQData = bCreatePerQData ? PERQDATA_CreateInstance() : NULL;
481 return hQueue;
485 /***********************************************************************
486 * QUEUE_FlushMessage
488 * Try to reply to all pending sent messages on exit.
490 void QUEUE_FlushMessages( MESSAGEQUEUE *queue )
492 SMSG *smsg;
493 MESSAGEQUEUE *senderQ = 0;
495 if( queue )
497 EnterCriticalSection( &queue->cSection );
499 /* empty the list of pending SendMessage waiting to be received */
500 while (queue->smPending)
502 smsg = QUEUE_RemoveSMSG( queue, SM_PENDING_LIST, 0);
504 senderQ = (MESSAGEQUEUE*)QUEUE_Lock( smsg->hSrcQueue );
505 if ( !senderQ )
506 continue;
508 /* return 0, to unblock other thread */
509 smsg->lResult = 0;
510 smsg->flags |= SMSG_HAVE_RESULT;
511 QUEUE_SetWakeBit( senderQ, QS_SMRESULT);
513 QUEUE_Unlock( senderQ );
516 QUEUE_ClearWakeBit( queue, QS_SENDMESSAGE );
518 LeaveCriticalSection( &queue->cSection );
523 /***********************************************************************
524 * QUEUE_DeleteMsgQueue
526 * Unlinks and deletes a message queue.
528 * Note: We need to mask asynchronous events to make sure PostMessage works
529 * even in the signal handler.
531 BOOL QUEUE_DeleteMsgQueue( HQUEUE16 hQueue )
533 MESSAGEQUEUE * msgQueue = (MESSAGEQUEUE*)QUEUE_Lock(hQueue);
534 HQUEUE16 *pPrev;
536 TRACE(msg,"(): Deleting message queue %04x\n", hQueue);
538 if (!hQueue || !msgQueue)
540 WARN(msg, "invalid argument.\n");
541 return 0;
544 msgQueue->magic = 0;
546 if( hCursorQueue == hQueue ) hCursorQueue = 0;
547 if( hActiveQueue == hQueue ) hActiveQueue = 0;
549 /* flush sent messages */
550 QUEUE_FlushMessages( msgQueue );
552 HeapLock( SystemHeap ); /* FIXME: a bit overkill */
554 /* Release per queue data if present */
555 if ( msgQueue->pQData )
557 PERQDATA_Release( msgQueue->pQData );
558 msgQueue->pQData = 0;
561 /* remove the message queue from the global link list */
562 pPrev = &hFirstQueue;
563 while (*pPrev && (*pPrev != hQueue))
565 MESSAGEQUEUE *msgQ = (MESSAGEQUEUE*)GlobalLock16(*pPrev);
567 /* sanity check */
568 if ( !msgQ || (msgQ->magic != QUEUE_MAGIC) )
570 /* HQUEUE link list is corrupted, try to exit gracefully */
571 WARN( msg, "HQUEUE link list corrupted!\n");
572 pPrev = 0;
573 break;
575 pPrev = &msgQ->next;
577 if (pPrev && *pPrev) *pPrev = msgQueue->next;
578 msgQueue->self = 0;
580 HeapUnlock( SystemHeap );
582 /* free up resource used by MESSAGEQUEUE strcture */
583 msgQueue->lockCount--;
584 QUEUE_Unlock( msgQueue );
586 return 1;
590 /***********************************************************************
591 * QUEUE_CreateSysMsgQueue
593 * Create the system message queue, and set the double-click speed.
594 * Must be called only once.
596 BOOL QUEUE_CreateSysMsgQueue( int size )
598 /* Note: We dont need perQ data for the system message queue */
599 if (!(hmemSysMsgQueue = QUEUE_CreateMsgQueue( FALSE )))
600 return FALSE;
602 sysMsgQueue = (MESSAGEQUEUE *) GlobalLock16( hmemSysMsgQueue );
603 return TRUE;
607 /***********************************************************************
608 * QUEUE_GetSysQueue
610 MESSAGEQUEUE *QUEUE_GetSysQueue(void)
612 return sysMsgQueue;
616 /***********************************************************************
617 * QUEUE_SetWakeBit
619 * See "Windows Internals", p.449
621 void QUEUE_SetWakeBit( MESSAGEQUEUE *queue, WORD bit )
623 TRACE(msg,"queue = %04x (wm=%04x), bit = %04x\n",
624 queue->self, queue->wakeMask, bit );
626 if (bit & QS_MOUSE) pMouseQueue = queue;
627 if (bit & QS_KEY) pKbdQueue = queue;
628 queue->changeBits |= bit;
629 queue->wakeBits |= bit;
630 if (queue->wakeMask & bit)
632 queue->wakeMask = 0;
634 /* Wake up thread waiting for message */
635 if ( THREAD_IsWin16( queue->thdb ) )
636 PostEvent16( queue->thdb->process->task );
637 else
639 SetEvent( queue->hEvent );
645 /***********************************************************************
646 * QUEUE_ClearWakeBit
648 void QUEUE_ClearWakeBit( MESSAGEQUEUE *queue, WORD bit )
650 queue->changeBits &= ~bit;
651 queue->wakeBits &= ~bit;
655 /***********************************************************************
656 * QUEUE_WaitBits
658 * See "Windows Internals", p.447
660 * return values:
661 * 0 if exit with timeout
662 * 1 otherwise
664 int QUEUE_WaitBits( WORD bits, DWORD timeout )
666 MESSAGEQUEUE *queue;
667 DWORD curTime = 0;
669 TRACE(msg,"q %04x waiting for %04x\n", GetFastQueue16(), bits);
671 if ( THREAD_IsWin16( THREAD_Current() ) && (timeout != INFINITE) )
672 curTime = GetTickCount();
674 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
676 for (;;)
678 if (queue->changeBits & bits)
680 /* One of the bits is set; we can return */
681 queue->wakeMask = 0;
682 QUEUE_Unlock( queue );
683 return 1;
685 if (queue->wakeBits & QS_SENDMESSAGE)
687 /* Process the sent message immediately */
689 queue->wakeMask = 0;
690 QUEUE_ReceiveMessage( queue );
691 continue; /* nested sm crux */
694 queue->wakeMask = bits | QS_SENDMESSAGE;
695 if(queue->changeBits & bits)
697 continue;
700 TRACE(msg,"%04x) wakeMask is %04x, waiting\n", queue->self, queue->wakeMask);
702 if ( !THREAD_IsWin16( THREAD_Current() ) )
704 /* win32 thread, use WaitForMultipleObjects */
705 MsgWaitForMultipleObjects( 0, NULL, FALSE, timeout, queue->wakeMask );
707 else
709 if ( timeout == INFINITE )
710 WaitEvent16( 0 ); /* win 16 thread, use WaitEvent */
711 else
713 /* check for timeout, then give control to other tasks */
714 if (GetTickCount() - curTime > timeout)
717 QUEUE_Unlock( queue );
718 return 0; /* exit with timeout */
720 Yield16();
727 /***********************************************************************
728 * QUEUE_AddSMSG
730 * This routine is called when a SMSG need to be added to one of the three
731 * SM list. (SM_PROCESSING_LIST, SM_PENDING_LIST, SM_WAITING_LIST)
733 BOOL QUEUE_AddSMSG( MESSAGEQUEUE *queue, int list, SMSG *smsg )
735 TRACE(sendmsg,"queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
736 smsg, SPY_GetMsgName(smsg->msg));
738 switch (list)
740 case SM_PROCESSING_LIST:
741 /* don't need to be thread safe, only accessed by the
742 thread associated with the sender queue */
743 smsg->nextProcessing = queue->smProcessing;
744 queue->smProcessing = smsg;
745 break;
747 case SM_WAITING_LIST:
748 /* don't need to be thread safe, only accessed by the
749 thread associated with the receiver queue */
750 smsg->nextWaiting = queue->smWaiting;
751 queue->smWaiting = smsg;
752 break;
754 case SM_PENDING_LIST:
756 /* make it thread safe, could be accessed by the sender and
757 receiver thread */
758 SMSG **prev;
760 EnterCriticalSection( &queue->cSection );
761 smsg->nextPending = NULL;
762 prev = &queue->smPending;
763 while ( *prev )
764 prev = &(*prev)->nextPending;
765 *prev = smsg;
766 LeaveCriticalSection( &queue->cSection );
768 QUEUE_SetWakeBit( queue, QS_SENDMESSAGE );
769 break;
772 default:
773 WARN(sendmsg, "Invalid list: %d", list);
774 break;
777 return TRUE;
781 /***********************************************************************
782 * QUEUE_RemoveSMSG
784 * This routine is called when a SMSG need to be remove from one of the three
785 * SM list. (SM_PROCESSING_LIST, SM_PENDING_LIST, SM_WAITING_LIST)
786 * If smsg == 0, remove the first smsg from the specified list
788 SMSG *QUEUE_RemoveSMSG( MESSAGEQUEUE *queue, int list, SMSG *smsg )
791 switch (list)
793 case SM_PROCESSING_LIST:
794 /* don't need to be thread safe, only accessed by the
795 thread associated with the sender queue */
797 /* if smsg is equal to null, it means the first in the list */
798 if (!smsg)
799 smsg = queue->smProcessing;
801 TRACE(sendmsg,"queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
802 smsg, SPY_GetMsgName(smsg->msg));
803 /* In fact SM_PROCESSING_LIST is a stack, and smsg
804 should be always at the top of the list */
805 if ( (smsg != queue->smProcessing) || !queue->smProcessing )
807 ERR( sendmsg, "smsg not at the top of Processing list, smsg=0x%p queue=0x%p", smsg, queue);
808 return 0;
810 else
812 queue->smProcessing = smsg->nextProcessing;
813 smsg->nextProcessing = 0;
815 return smsg;
817 case SM_WAITING_LIST:
818 /* don't need to be thread safe, only accessed by the
819 thread associated with the receiver queue */
821 /* if smsg is equal to null, it means the first in the list */
822 if (!smsg)
823 smsg = queue->smWaiting;
825 TRACE(sendmsg,"queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
826 smsg, SPY_GetMsgName(smsg->msg));
827 /* In fact SM_WAITING_LIST is a stack, and smsg
828 should be always at the top of the list */
829 if ( (smsg != queue->smWaiting) || !queue->smWaiting )
831 ERR( sendmsg, "smsg not at the top of Waiting list, smsg=0x%p queue=0x%p", smsg, queue);
832 return 0;
834 else
836 queue->smWaiting = smsg->nextWaiting;
837 smsg->nextWaiting = 0;
839 return smsg;
841 case SM_PENDING_LIST:
842 /* make it thread safe, could be accessed by the sender and
843 receiver thread */
844 EnterCriticalSection( &queue->cSection );
846 if (!smsg || !queue->smPending)
847 smsg = queue->smPending;
848 else
850 ERR( sendmsg, "should always remove the top one in Pending list, smsg=0x%p queue=0x%p", smsg, queue);
851 LeaveCriticalSection( &queue->cSection );
852 return 0;
855 TRACE(sendmsg,"queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
856 smsg, SPY_GetMsgName(smsg->msg));
858 queue->smPending = smsg->nextPending;
859 smsg->nextPending = 0;
861 /* if no more SMSG in Pending list, clear QS_SENDMESSAGE flag */
862 if (!queue->smPending)
863 QUEUE_ClearWakeBit( queue, QS_SENDMESSAGE );
865 LeaveCriticalSection( &queue->cSection );
866 return smsg;
868 default:
869 WARN(sendmsg, "Invalid list: %d", list);
870 break;
873 return 0;
877 /***********************************************************************
878 * QUEUE_ReceiveMessage
880 * This routine is called when a sent message is waiting for the queue.
882 void QUEUE_ReceiveMessage( MESSAGEQUEUE *queue )
884 LRESULT result = 0;
885 SMSG *smsg;
886 MESSAGEQUEUE *senderQ;
888 TRACE(sendmsg, "queue %04x\n", queue->self );
890 if ( !(queue->wakeBits & QS_SENDMESSAGE) && queue->smPending )
892 TRACE(sendmsg,"\trcm: nothing to do\n");
893 return;
896 /* remove smsg on the top of the pending list and put it in the processing list */
897 smsg = QUEUE_RemoveSMSG(queue, SM_PENDING_LIST, 0);
898 QUEUE_AddSMSG(queue, SM_WAITING_LIST, smsg);
900 TRACE(sendmsg,"RM: %s [%04x] (%04x -> %04x)\n",
901 SPY_GetMsgName(smsg->msg), smsg->msg, smsg->hSrcQueue, smsg->hDstQueue );
903 if (IsWindow( smsg->hWnd ))
905 WND *wndPtr = WIN_FindWndPtr( smsg->hWnd );
906 DWORD extraInfo = queue->GetMessageExtraInfoVal; /* save ExtraInfo */
908 /* use sender queue extra info value while calling the window proc */
909 senderQ = (MESSAGEQUEUE*)QUEUE_Lock( smsg->hSrcQueue );
910 if (senderQ)
912 queue->GetMessageExtraInfoVal = senderQ->GetMessageExtraInfoVal;
913 QUEUE_Unlock( senderQ );
916 /* call the right version of CallWindowProcXX */
917 if (smsg->flags & SMSG_WIN32)
919 TRACE(sendmsg, "\trcm: msg is Win32\n" );
920 if (smsg->flags & SMSG_UNICODE)
921 result = CallWindowProcW( wndPtr->winproc,
922 smsg->hWnd, smsg->msg,
923 smsg->wParam, smsg->lParam );
924 else
925 result = CallWindowProcA( wndPtr->winproc,
926 smsg->hWnd, smsg->msg,
927 smsg->wParam, smsg->lParam );
929 else /* Win16 message */
930 result = CallWindowProc16( (WNDPROC16)wndPtr->winproc,
931 (HWND16) smsg->hWnd,
932 (UINT16) smsg->msg,
933 LOWORD (smsg->wParam),
934 smsg->lParam );
936 queue->GetMessageExtraInfoVal = extraInfo; /* Restore extra info */
937 WIN_ReleaseWndPtr(wndPtr);
938 TRACE(sendmsg,"result = %08x\n", (unsigned)result );
940 else WARN(sendmsg, "\trcm: bad hWnd\n");
943 /* set SMSG_SENDING_REPLY flag to tell ReplyMessage16, it's not
944 an early reply */
945 smsg->flags |= SMSG_SENDING_REPLY;
946 ReplyMessage( result );
948 TRACE( sendmsg,"done! \n" );
953 /***********************************************************************
954 * QUEUE_AddMsg
956 * Add a message to the queue. Return FALSE if queue is full.
958 BOOL QUEUE_AddMsg( HQUEUE16 hQueue, MSG *msg, DWORD extraInfo )
960 MESSAGEQUEUE *msgQueue;
961 QMSG *qmsg;
964 if (!(msgQueue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return FALSE;
966 /* allocate new message in global heap for now */
967 if (!(qmsg = (QMSG *) HeapAlloc( SystemHeap, 0, sizeof(QMSG) ) ))
969 QUEUE_Unlock( msgQueue );
970 return 0;
973 EnterCriticalSection( &msgQueue->cSection );
975 /* Store message */
976 qmsg->msg = *msg;
977 qmsg->extraInfo = extraInfo;
979 /* insert the message in the link list */
980 qmsg->nextMsg = 0;
981 qmsg->prevMsg = msgQueue->lastMsg;
983 if (msgQueue->lastMsg)
984 msgQueue->lastMsg->nextMsg = qmsg;
986 /* update first and last anchor in message queue */
987 msgQueue->lastMsg = qmsg;
988 if (!msgQueue->firstMsg)
989 msgQueue->firstMsg = qmsg;
991 msgQueue->msgCount++;
993 LeaveCriticalSection( &msgQueue->cSection );
995 QUEUE_SetWakeBit( msgQueue, QS_POSTMESSAGE );
996 QUEUE_Unlock( msgQueue );
998 return TRUE;
1003 /***********************************************************************
1004 * QUEUE_FindMsg
1006 * Find a message matching the given parameters. Return -1 if none available.
1008 QMSG* QUEUE_FindMsg( MESSAGEQUEUE * msgQueue, HWND hwnd, int first, int last )
1010 QMSG* qmsg;
1012 EnterCriticalSection( &msgQueue->cSection );
1014 if (!msgQueue->msgCount)
1015 qmsg = 0;
1016 else if (!hwnd && !first && !last)
1017 qmsg = msgQueue->firstMsg;
1018 else
1020 /* look in linked list for message matching first and last criteria */
1021 for (qmsg = msgQueue->firstMsg; qmsg; qmsg = qmsg->nextMsg)
1023 MSG *msg = &(qmsg->msg);
1025 if (!hwnd || (msg->hwnd == hwnd))
1027 if (!first && !last)
1028 break; /* found it */
1030 if ((msg->message >= first) && (!last || (msg->message <= last)))
1031 break; /* found it */
1036 LeaveCriticalSection( &msgQueue->cSection );
1038 return qmsg;
1043 /***********************************************************************
1044 * QUEUE_RemoveMsg
1046 * Remove a message from the queue (pos must be a valid position).
1048 void QUEUE_RemoveMsg( MESSAGEQUEUE * msgQueue, QMSG *qmsg )
1050 EnterCriticalSection( &msgQueue->cSection );
1052 /* set the linked list */
1053 if (qmsg->prevMsg)
1054 qmsg->prevMsg->nextMsg = qmsg->nextMsg;
1056 if (qmsg->nextMsg)
1057 qmsg->nextMsg->prevMsg = qmsg->prevMsg;
1059 if (msgQueue->firstMsg == qmsg)
1060 msgQueue->firstMsg = qmsg->nextMsg;
1062 if (msgQueue->lastMsg == qmsg)
1063 msgQueue->lastMsg = qmsg->prevMsg;
1065 /* deallocate the memory for the message */
1066 HeapFree( SystemHeap, 0, qmsg );
1068 msgQueue->msgCount--;
1069 if (!msgQueue->msgCount) msgQueue->wakeBits &= ~QS_POSTMESSAGE;
1071 LeaveCriticalSection( &msgQueue->cSection );
1075 /***********************************************************************
1076 * QUEUE_WakeSomeone
1078 * Wake a queue upon reception of a hardware event.
1080 static void QUEUE_WakeSomeone( UINT message )
1082 WND* wndPtr = NULL;
1083 WORD wakeBit;
1084 HWND hwnd;
1085 HQUEUE16 hQueue = 0;
1086 MESSAGEQUEUE *queue = NULL;
1088 if (hCursorQueue)
1089 hQueue = hCursorQueue;
1091 if( (message >= WM_KEYFIRST) && (message <= WM_KEYLAST) )
1093 wakeBit = QS_KEY;
1094 if( hActiveQueue )
1095 hQueue = hActiveQueue;
1097 else
1099 wakeBit = (message == WM_MOUSEMOVE) ? QS_MOUSEMOVE : QS_MOUSEBUTTON;
1100 if( (hwnd = GetCapture()) )
1101 if( (wndPtr = WIN_FindWndPtr( hwnd )) )
1103 hQueue = wndPtr->hmemTaskQ;
1104 WIN_ReleaseWndPtr(wndPtr);
1108 if( (hwnd = GetSysModalWindow16()) )
1110 if( (wndPtr = WIN_FindWndPtr( hwnd )) )
1112 hQueue = wndPtr->hmemTaskQ;
1113 WIN_ReleaseWndPtr(wndPtr);
1117 if (hQueue)
1118 queue = QUEUE_Lock( hQueue );
1120 if( !queue )
1122 queue = QUEUE_Lock( hFirstQueue );
1123 while( queue )
1125 if (queue->wakeMask & wakeBit) break;
1127 QUEUE_Unlock(queue);
1128 queue = QUEUE_Lock( queue->next );
1130 if( !queue )
1132 WARN(msg, "couldn't find queue\n");
1133 return;
1137 QUEUE_SetWakeBit( queue, wakeBit );
1139 QUEUE_Unlock( queue );
1143 /***********************************************************************
1144 * hardware_event
1146 * Add an event to the system message queue.
1147 * Note: the position is relative to the desktop window.
1149 void hardware_event( WORD message, WORD wParam, LONG lParam,
1150 int xPos, int yPos, DWORD time, DWORD extraInfo )
1152 MSG *msg;
1153 QMSG *qmsg;
1154 int mergeMsg = 0;
1156 if (!sysMsgQueue) return;
1158 EnterCriticalSection( &sysMsgQueue->cSection );
1160 /* Merge with previous event if possible */
1161 qmsg = sysMsgQueue->lastMsg;
1163 if ((message == WM_MOUSEMOVE) && sysMsgQueue->lastMsg)
1165 msg = &(sysMsgQueue->lastMsg->msg);
1167 if ((msg->message == message) && (msg->wParam == wParam))
1169 /* Merge events */
1170 qmsg = sysMsgQueue->lastMsg;
1171 mergeMsg = 1;
1175 if (!mergeMsg)
1177 /* Should I limit the number of message in
1178 the system message queue??? */
1180 /* Don't merge allocate a new msg in the global heap */
1182 if (!(qmsg = (QMSG *) HeapAlloc( SystemHeap, 0, sizeof(QMSG) ) ))
1184 LeaveCriticalSection( &sysMsgQueue->cSection );
1185 return;
1188 /* put message at the end of the linked list */
1189 qmsg->nextMsg = 0;
1190 qmsg->prevMsg = sysMsgQueue->lastMsg;
1192 if (sysMsgQueue->lastMsg)
1193 sysMsgQueue->lastMsg->nextMsg = qmsg;
1195 /* set last and first anchor index in system message queue */
1196 sysMsgQueue->lastMsg = qmsg;
1197 if (!sysMsgQueue->firstMsg)
1198 sysMsgQueue->firstMsg = qmsg;
1200 sysMsgQueue->msgCount++;
1203 /* Store message */
1204 msg = &(qmsg->msg);
1205 msg->hwnd = 0;
1206 msg->message = message;
1207 msg->wParam = wParam;
1208 msg->lParam = lParam;
1209 msg->time = time;
1210 msg->pt.x = xPos;
1211 msg->pt.y = yPos;
1212 qmsg->extraInfo = extraInfo;
1214 LeaveCriticalSection( &sysMsgQueue->cSection );
1216 QUEUE_WakeSomeone( message );
1220 /***********************************************************************
1221 * QUEUE_GetQueueTask
1223 HTASK16 QUEUE_GetQueueTask( HQUEUE16 hQueue )
1225 HTASK16 hTask = 0;
1227 MESSAGEQUEUE *queue = QUEUE_Lock( hQueue );
1229 if (queue)
1231 hTask = queue->thdb->process->task;
1232 QUEUE_Unlock( queue );
1235 return hTask;
1240 /***********************************************************************
1241 * QUEUE_IncPaintCount
1243 void QUEUE_IncPaintCount( HQUEUE16 hQueue )
1245 MESSAGEQUEUE *queue;
1247 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1248 queue->wPaintCount++;
1249 QUEUE_SetWakeBit( queue, QS_PAINT );
1250 QUEUE_Unlock( queue );
1254 /***********************************************************************
1255 * QUEUE_DecPaintCount
1257 void QUEUE_DecPaintCount( HQUEUE16 hQueue )
1259 MESSAGEQUEUE *queue;
1261 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1262 queue->wPaintCount--;
1263 if (!queue->wPaintCount) queue->wakeBits &= ~QS_PAINT;
1264 QUEUE_Unlock( queue );
1268 /***********************************************************************
1269 * QUEUE_IncTimerCount
1271 void QUEUE_IncTimerCount( HQUEUE16 hQueue )
1273 MESSAGEQUEUE *queue;
1275 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1276 queue->wTimerCount++;
1277 QUEUE_SetWakeBit( queue, QS_TIMER );
1278 QUEUE_Unlock( queue );
1282 /***********************************************************************
1283 * QUEUE_DecTimerCount
1285 void QUEUE_DecTimerCount( HQUEUE16 hQueue )
1287 MESSAGEQUEUE *queue;
1289 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1290 queue->wTimerCount--;
1291 if (!queue->wTimerCount) queue->wakeBits &= ~QS_TIMER;
1292 QUEUE_Unlock( queue );
1296 /***********************************************************************
1297 * PostQuitMessage16 (USER.6)
1299 void WINAPI PostQuitMessage16( INT16 exitCode )
1301 PostQuitMessage( exitCode );
1305 /***********************************************************************
1306 * PostQuitMessage32 (USER32.421)
1308 * PostQuitMessage() posts a message to the system requesting an
1309 * application to terminate execution. As a result of this function,
1310 * the WM_QUIT message is posted to the application, and
1311 * PostQuitMessage() returns immediately. The exitCode parameter
1312 * specifies an application-defined exit code, which appears in the
1313 * _wParam_ parameter of the WM_QUIT message posted to the application.
1315 * CONFORMANCE
1317 * ECMA-234, Win32
1319 void WINAPI PostQuitMessage( INT exitCode )
1321 MESSAGEQUEUE *queue;
1323 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return;
1324 queue->wPostQMsg = TRUE;
1325 queue->wExitCode = (WORD)exitCode;
1326 QUEUE_Unlock( queue );
1330 /***********************************************************************
1331 * GetWindowTask16 (USER.224)
1333 HTASK16 WINAPI GetWindowTask16( HWND16 hwnd )
1335 HTASK16 retvalue;
1336 WND *wndPtr = WIN_FindWndPtr( hwnd );
1338 if (!wndPtr) return 0;
1339 retvalue = QUEUE_GetQueueTask( wndPtr->hmemTaskQ );
1340 WIN_ReleaseWndPtr(wndPtr);
1341 return retvalue;
1344 /***********************************************************************
1345 * GetWindowThreadProcessId (USER32.313)
1347 DWORD WINAPI GetWindowThreadProcessId( HWND hwnd, LPDWORD process )
1349 HTASK16 htask;
1350 TDB *tdb;
1352 WND *wndPtr = WIN_FindWndPtr( hwnd );
1354 if (!wndPtr) return 0;
1355 htask=QUEUE_GetQueueTask( wndPtr->hmemTaskQ );
1356 WIN_ReleaseWndPtr(wndPtr);
1357 tdb = (TDB*)GlobalLock16(htask);
1358 if (!tdb || !tdb->thdb) return 0;
1359 if (process) *process = (DWORD)tdb->thdb->process->server_pid;
1360 return (DWORD)tdb->thdb->server_tid;
1364 /***********************************************************************
1365 * SetMessageQueue16 (USER.266)
1367 BOOL16 WINAPI SetMessageQueue16( INT16 size )
1369 return SetMessageQueue( size );
1373 /***********************************************************************
1374 * SetMessageQueue32 (USER32.494)
1376 BOOL WINAPI SetMessageQueue( INT size )
1378 /* now obsolete the message queue will be expanded dynamically
1379 as necessary */
1381 /* access the queue to create it if it's not existing */
1382 GetFastQueue16();
1384 return TRUE;
1387 /***********************************************************************
1388 * InitThreadInput (USER.409)
1390 HQUEUE16 WINAPI InitThreadInput16( WORD unknown, WORD flags )
1392 HQUEUE16 hQueue;
1393 MESSAGEQUEUE *queuePtr;
1395 THDB *thdb = THREAD_Current();
1397 if (!thdb)
1398 return 0;
1400 hQueue = thdb->teb.queue;
1402 if ( !hQueue )
1404 /* Create thread message queue */
1405 if( !(hQueue = QUEUE_CreateMsgQueue( TRUE )))
1407 WARN(msg, "failed!\n");
1408 return FALSE;
1411 /* Link new queue into list */
1412 queuePtr = (MESSAGEQUEUE *)QUEUE_Lock( hQueue );
1413 queuePtr->thdb = THREAD_Current();
1415 HeapLock( SystemHeap ); /* FIXME: a bit overkill */
1416 SetThreadQueue16( 0, hQueue );
1417 thdb->teb.queue = hQueue;
1419 queuePtr->next = hFirstQueue;
1420 hFirstQueue = hQueue;
1421 HeapUnlock( SystemHeap );
1423 QUEUE_Unlock( queuePtr );
1426 return hQueue;
1429 /***********************************************************************
1430 * GetQueueStatus16 (USER.334)
1432 DWORD WINAPI GetQueueStatus16( UINT16 flags )
1434 MESSAGEQUEUE *queue;
1435 DWORD ret;
1437 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1438 ret = MAKELONG( queue->changeBits, queue->wakeBits );
1439 queue->changeBits = 0;
1440 QUEUE_Unlock( queue );
1442 return ret & MAKELONG( flags, flags );
1445 /***********************************************************************
1446 * GetQueueStatus32 (USER32.283)
1448 DWORD WINAPI GetQueueStatus( UINT flags )
1450 MESSAGEQUEUE *queue;
1451 DWORD ret;
1453 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1454 ret = MAKELONG( queue->changeBits, queue->wakeBits );
1455 queue->changeBits = 0;
1456 QUEUE_Unlock( queue );
1458 return ret & MAKELONG( flags, flags );
1462 /***********************************************************************
1463 * GetInputState16 (USER.335)
1465 BOOL16 WINAPI GetInputState16(void)
1467 return GetInputState();
1470 /***********************************************************************
1471 * WaitForInputIdle (USER32.577)
1473 DWORD WINAPI WaitForInputIdle (HANDLE hProcess, DWORD dwTimeOut)
1475 FIXME (msg, "(hProcess=%d, dwTimeOut=%ld): stub\n", hProcess, dwTimeOut);
1477 return WAIT_TIMEOUT;
1481 /***********************************************************************
1482 * GetInputState32 (USER32.244)
1484 BOOL WINAPI GetInputState(void)
1486 MESSAGEQUEUE *queue;
1487 BOOL ret;
1489 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() )))
1490 return FALSE;
1491 ret = queue->wakeBits & (QS_KEY | QS_MOUSEBUTTON);
1492 QUEUE_Unlock( queue );
1494 return ret;
1497 /***********************************************************************
1498 * UserYield (USER.332)
1500 void WINAPI UserYield16(void)
1502 MESSAGEQUEUE *queue;
1504 /* Handle sent messages */
1505 queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() );
1507 while (queue && (queue->wakeBits & QS_SENDMESSAGE))
1508 QUEUE_ReceiveMessage( queue );
1510 QUEUE_Unlock( queue );
1512 /* Yield */
1513 if ( THREAD_IsWin16( THREAD_Current() ) )
1514 OldYield16();
1515 else
1517 DWORD count;
1519 ReleaseThunkLock(&count);
1520 RestoreThunkLock(count);
1523 /* Handle sent messages again */
1524 queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() );
1526 while (queue && (queue->wakeBits & QS_SENDMESSAGE))
1527 QUEUE_ReceiveMessage( queue );
1529 QUEUE_Unlock( queue );
1532 /***********************************************************************
1533 * GetMessagePos (USER.119) (USER32.272)
1535 * The GetMessagePos() function returns a long value representing a
1536 * cursor position, in screen coordinates, when the last message
1537 * retrieved by the GetMessage() function occurs. The x-coordinate is
1538 * in the low-order word of the return value, the y-coordinate is in
1539 * the high-order word. The application can use the MAKEPOINT()
1540 * macro to obtain a POINT structure from the return value.
1542 * For the current cursor position, use GetCursorPos().
1544 * RETURNS
1546 * Cursor position of last message on success, zero on failure.
1548 * CONFORMANCE
1550 * ECMA-234, Win32
1553 DWORD WINAPI GetMessagePos(void)
1555 MESSAGEQUEUE *queue;
1556 DWORD ret;
1558 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1559 ret = queue->GetMessagePosVal;
1560 QUEUE_Unlock( queue );
1562 return ret;
1566 /***********************************************************************
1567 * GetMessageTime (USER.120) (USER32.273)
1569 * GetMessageTime() returns the message time for the last message
1570 * retrieved by the function. The time is measured in milliseconds with
1571 * the same offset as GetTickCount().
1573 * Since the tick count wraps, this is only useful for moderately short
1574 * relative time comparisons.
1576 * RETURNS
1578 * Time of last message on success, zero on failure.
1580 * CONFORMANCE
1582 * ECMA-234, Win32
1585 LONG WINAPI GetMessageTime(void)
1587 MESSAGEQUEUE *queue;
1588 LONG ret;
1590 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1591 ret = queue->GetMessageTimeVal;
1592 QUEUE_Unlock( queue );
1594 return ret;
1598 /***********************************************************************
1599 * GetMessageExtraInfo (USER.288) (USER32.271)
1601 LONG WINAPI GetMessageExtraInfo(void)
1603 MESSAGEQUEUE *queue;
1604 LONG ret;
1606 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1607 ret = queue->GetMessageExtraInfoVal;
1608 QUEUE_Unlock( queue );
1610 return ret;