If the window is already the topmost window in ShowWindow(SW_SHOW), it
[wine] / windows / queue.c
1 /* * Message queues related functions
2  *
3  * Copyright 1993, 1994 Alexandre Julliard
4  */
5
6 #include <string.h>
7 #include <signal.h>
8 #include "wine/winbase16.h"
9 #include "wine/winuser16.h"
10 #include "miscemu.h"
11 #include "syslevel.h"
12 #include "module.h"
13 #include "queue.h"
14 #include "task.h"
15 #include "win.h"
16 #include "clipboard.h"
17 #include "hook.h"
18 #include "heap.h"
19 #include "thread.h"
20 #include "process.h"
21 #include <assert.h>
22 #include "debugtools.h"
23 #include "spy.h"
24
25 DECLARE_DEBUG_CHANNEL(msg)
26 DECLARE_DEBUG_CHANNEL(sendmsg)
27
28 #define MAX_QUEUE_SIZE   120  /* Max. size of a message queue */
29
30 static HQUEUE16 hFirstQueue = 0;
31 static HQUEUE16 hExitingQueue = 0;
32 static HQUEUE16 hmemSysMsgQueue = 0;
33 static MESSAGEQUEUE *sysMsgQueue = NULL;
34 static PERQUEUEDATA *pQDataWin16 = NULL;  /* Global perQData for Win16 tasks */
35
36 static MESSAGEQUEUE *pMouseQueue = NULL;  /* Queue for last mouse message */
37 static MESSAGEQUEUE *pKbdQueue = NULL;    /* Queue for last kbd message */
38
39 HQUEUE16 hCursorQueue = 0;
40 HQUEUE16 hActiveQueue = 0;
41
42
43 /***********************************************************************
44  *           PERQDATA_CreateInstance
45  *
46  * Creates an instance of a reference counted PERQUEUEDATA element
47  * for the message queue. perQData is stored globally for 16 bit tasks.
48  *
49  * Note: We don't implement perQdata exactly the same way Windows does.
50  * Each perQData element is reference counted since it may be potentially
51  * shared by multiple message Queues (via AttachThreadInput).
52  * We only store the current values for Active, Capture and focus windows
53  * currently.
54  */
55 PERQUEUEDATA * PERQDATA_CreateInstance( )
56 {
57     PERQUEUEDATA *pQData;
58     
59     BOOL16 bIsWin16 = 0;
60     
61     TRACE_(msg)("()\n");
62
63     /* Share a single instance of perQData for all 16 bit tasks */
64     if ( ( bIsWin16 = THREAD_IsWin16( NtCurrentTeb() ) ) )
65     {
66         /* If previously allocated, just bump up ref count */
67         if ( pQDataWin16 )
68         {
69             PERQDATA_Addref( pQDataWin16 );
70             return pQDataWin16;
71         }
72     }
73
74     /* Allocate PERQUEUEDATA from the system heap */
75     if (!( pQData = (PERQUEUEDATA *) HeapAlloc( SystemHeap, 0,
76                                                     sizeof(PERQUEUEDATA) ) ))
77         return 0;
78
79     /* Initialize */
80     pQData->hWndCapture = pQData->hWndFocus = pQData->hWndActive = 0;
81     pQData->ulRefCount = 1;
82     pQData->nCaptureHT = HTCLIENT;
83
84     /* Note: We have an independent critical section for the per queue data
85      * since this may be shared by different threads. see AttachThreadInput()
86      */
87     InitializeCriticalSection( &pQData->cSection );
88
89     /* Save perQData globally for 16 bit tasks */
90     if ( bIsWin16 )
91         pQDataWin16 = pQData;
92         
93     return pQData;
94 }
95
96
97 /***********************************************************************
98  *           PERQDATA_Addref
99  *
100  * Increment reference count for the PERQUEUEDATA instance
101  * Returns reference count for debugging purposes
102  */
103 ULONG PERQDATA_Addref( PERQUEUEDATA *pQData )
104 {
105     assert(pQData != 0 );
106     TRACE_(msg)("(): current refcount %lu ...\n", pQData->ulRefCount);
107
108     EnterCriticalSection( &pQData->cSection );
109     ++pQData->ulRefCount;
110     LeaveCriticalSection( &pQData->cSection );
111
112     return pQData->ulRefCount;
113 }
114
115
116 /***********************************************************************
117  *           PERQDATA_Release
118  *
119  * Release a reference to a PERQUEUEDATA instance.
120  * Destroy the instance if no more references exist
121  * Returns reference count for debugging purposes
122  */
123 ULONG PERQDATA_Release( PERQUEUEDATA *pQData )
124 {
125     assert(pQData != 0 );
126     TRACE_(msg)("(): current refcount %lu ...\n",
127           (LONG)pQData->ulRefCount );
128
129     EnterCriticalSection( &pQData->cSection );
130     if ( --pQData->ulRefCount == 0 )
131     {
132         LeaveCriticalSection( &pQData->cSection );
133         DeleteCriticalSection( &pQData->cSection );
134
135         TRACE_(msg)("(): deleting PERQUEUEDATA instance ...\n" );
136
137         /* Deleting our global 16 bit perQData? */
138         if ( pQData == pQDataWin16 )
139             pQDataWin16 = 0;
140             
141         /* Free the PERQUEUEDATA instance */
142         HeapFree( SystemHeap, 0, pQData );
143
144         return 0;
145     }
146     LeaveCriticalSection( &pQData->cSection );
147
148     return pQData->ulRefCount;
149 }
150
151
152 /***********************************************************************
153  *           PERQDATA_GetFocusWnd
154  *
155  * Get the focus hwnd member in a threadsafe manner
156  */
157 HWND PERQDATA_GetFocusWnd( PERQUEUEDATA *pQData )
158 {
159     HWND hWndFocus;
160     assert(pQData != 0 );
161
162     EnterCriticalSection( &pQData->cSection );
163     hWndFocus = pQData->hWndFocus;
164     LeaveCriticalSection( &pQData->cSection );
165
166     return hWndFocus;
167 }
168
169
170 /***********************************************************************
171  *           PERQDATA_SetFocusWnd
172  *
173  * Set the focus hwnd member in a threadsafe manner
174  */
175 HWND PERQDATA_SetFocusWnd( PERQUEUEDATA *pQData, HWND hWndFocus )
176 {
177     HWND hWndFocusPrv;
178     assert(pQData != 0 );
179
180     EnterCriticalSection( &pQData->cSection );
181     hWndFocusPrv = pQData->hWndFocus;
182     pQData->hWndFocus = hWndFocus;
183     LeaveCriticalSection( &pQData->cSection );
184
185     return hWndFocusPrv;
186 }
187
188
189 /***********************************************************************
190  *           PERQDATA_GetActiveWnd
191  *
192  * Get the active hwnd member in a threadsafe manner
193  */
194 HWND PERQDATA_GetActiveWnd( PERQUEUEDATA *pQData )
195 {
196     HWND hWndActive;
197     assert(pQData != 0 );
198
199     EnterCriticalSection( &pQData->cSection );
200     hWndActive = pQData->hWndActive;
201     LeaveCriticalSection( &pQData->cSection );
202
203     return hWndActive;
204 }
205
206
207 /***********************************************************************
208  *           PERQDATA_SetActiveWnd
209  *
210  * Set the active focus hwnd member in a threadsafe manner
211  */
212 HWND PERQDATA_SetActiveWnd( PERQUEUEDATA *pQData, HWND hWndActive )
213 {
214     HWND hWndActivePrv;
215     assert(pQData != 0 );
216
217     EnterCriticalSection( &pQData->cSection );
218     hWndActivePrv = pQData->hWndActive;
219     pQData->hWndActive = hWndActive;
220     LeaveCriticalSection( &pQData->cSection );
221
222     return hWndActivePrv;
223 }
224
225
226 /***********************************************************************
227  *           PERQDATA_GetCaptureWnd
228  *
229  * Get the capture hwnd member in a threadsafe manner
230  */
231 HWND PERQDATA_GetCaptureWnd( PERQUEUEDATA *pQData )
232 {
233     HWND hWndCapture;
234     assert(pQData != 0 );
235
236     EnterCriticalSection( &pQData->cSection );
237     hWndCapture = pQData->hWndCapture;
238     LeaveCriticalSection( &pQData->cSection );
239
240     return hWndCapture;
241 }
242
243
244 /***********************************************************************
245  *           PERQDATA_SetCaptureWnd
246  *
247  * Set the capture hwnd member in a threadsafe manner
248  */
249 HWND PERQDATA_SetCaptureWnd( PERQUEUEDATA *pQData, HWND hWndCapture )
250 {
251     HWND hWndCapturePrv;
252     assert(pQData != 0 );
253
254     EnterCriticalSection( &pQData->cSection );
255     hWndCapturePrv = pQData->hWndCapture;
256     pQData->hWndCapture = hWndCapture;
257     LeaveCriticalSection( &pQData->cSection );
258
259     return hWndCapturePrv;
260 }
261
262
263 /***********************************************************************
264  *           PERQDATA_GetCaptureInfo
265  *
266  * Get the capture info member in a threadsafe manner
267  */
268 INT16 PERQDATA_GetCaptureInfo( PERQUEUEDATA *pQData )
269 {
270     INT16 nCaptureHT;
271     assert(pQData != 0 );
272
273     EnterCriticalSection( &pQData->cSection );
274     nCaptureHT = pQData->nCaptureHT;
275     LeaveCriticalSection( &pQData->cSection );
276
277     return nCaptureHT;
278 }
279
280
281 /***********************************************************************
282  *           PERQDATA_SetCaptureInfo
283  *
284  * Set the capture info member in a threadsafe manner
285  */
286 INT16 PERQDATA_SetCaptureInfo( PERQUEUEDATA *pQData, INT16 nCaptureHT )
287 {
288     INT16 nCaptureHTPrv;
289     assert(pQData != 0 );
290
291     EnterCriticalSection( &pQData->cSection );
292     nCaptureHTPrv = pQData->nCaptureHT;
293     pQData->nCaptureHT = nCaptureHT;
294     LeaveCriticalSection( &pQData->cSection );
295
296     return nCaptureHTPrv;
297 }
298
299
300 /***********************************************************************
301  *           QUEUE_Lock
302  *
303  * Function for getting a 32 bit pointer on queue strcture. For thread
304  * safeness programmers should use this function instead of GlobalLock to
305  * retrieve a pointer on the structure. QUEUE_Unlock should also be called
306  * when access to the queue structure is not required anymore.
307  */
308 MESSAGEQUEUE *QUEUE_Lock( HQUEUE16 hQueue )
309 {
310     MESSAGEQUEUE *queue;
311
312     HeapLock( SystemHeap );  /* FIXME: a bit overkill */
313     queue = GlobalLock16( hQueue );
314     if ( !queue || (queue->magic != QUEUE_MAGIC) )
315     {
316         HeapUnlock( SystemHeap );
317         return NULL;
318     }
319
320     queue->lockCount++;
321     HeapUnlock( SystemHeap );
322     return queue;
323 }
324
325
326 /***********************************************************************
327  *           QUEUE_Unlock
328  *
329  * Use with QUEUE_Lock to get a thread safe access to message queue
330  * structure
331  */
332 void QUEUE_Unlock( MESSAGEQUEUE *queue )
333 {
334     if (queue)
335     {
336         HeapLock( SystemHeap );  /* FIXME: a bit overkill */
337
338         if ( --queue->lockCount == 0 )
339         {
340             DeleteCriticalSection ( &queue->cSection );
341             if (queue->hEvent)
342                 CloseHandle( queue->hEvent );
343             GlobalFree16( queue->self );
344         }
345     
346         HeapUnlock( SystemHeap );
347     }
348 }
349
350
351 /***********************************************************************
352  *           QUEUE_DumpQueue
353  */
354 void QUEUE_DumpQueue( HQUEUE16 hQueue )
355 {
356     MESSAGEQUEUE *pq; 
357
358     if (!(pq = (MESSAGEQUEUE*) QUEUE_Lock( hQueue )) )
359     {
360         WARN_(msg)("%04x is not a queue handle\n", hQueue );
361         return;
362     }
363
364     DPRINTF( "next: %12.4x  Intertask SendMessage:\n"
365              "thread: %10p  ----------------------\n"
366              "firstMsg: %8p   smWaiting:     %10p\n"
367              "lastMsg:  %8p   smPending:     %10p\n"
368              "msgCount: %8.4x   smProcessing:  %10p\n"
369              "lockCount: %7.4x\n"
370              "wWinVer: %9.4x\n"
371              "paints: %10.4x\n"
372              "timers: %10.4x\n"
373              "wakeBits: %8.4x\n"
374              "wakeMask: %8.4x\n"
375              "hCurHook: %8.4x\n",
376              pq->next, pq->teb, pq->firstMsg, pq->smWaiting, pq->lastMsg,
377              pq->smPending, pq->msgCount, pq->smProcessing,
378              (unsigned)pq->lockCount, pq->wWinVersion,
379              pq->wPaintCount, pq->wTimerCount,
380              pq->wakeBits, pq->wakeMask, pq->hCurHook);
381
382     QUEUE_Unlock( pq );
383 }
384
385
386 /***********************************************************************
387  *           QUEUE_WalkQueues
388  */
389 void QUEUE_WalkQueues(void)
390 {
391     char module[10];
392     HQUEUE16 hQueue = hFirstQueue;
393
394     DPRINTF( "Queue Msgs Thread   Task Module\n" );
395     while (hQueue)
396     {
397         MESSAGEQUEUE *queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue );
398         if (!queue)
399         {
400             WARN_(msg)("Bad queue handle %04x\n", hQueue );
401             return;
402         }
403         if (!GetModuleName16( queue->teb->process->task, module, sizeof(module )))
404             strcpy( module, "???" );
405         DPRINTF( "%04x %4d %p %04x %s\n", hQueue,queue->msgCount,
406                  queue->teb, queue->teb->process->task, module );
407         hQueue = queue->next;
408         QUEUE_Unlock( queue );
409     }
410     DPRINTF( "\n" );
411 }
412
413
414 /***********************************************************************
415  *           QUEUE_IsExitingQueue
416  */
417 BOOL QUEUE_IsExitingQueue( HQUEUE16 hQueue )
418 {
419     return (hExitingQueue && (hQueue == hExitingQueue));
420 }
421
422
423 /***********************************************************************
424  *           QUEUE_SetExitingQueue
425  */
426 void QUEUE_SetExitingQueue( HQUEUE16 hQueue )
427 {
428     hExitingQueue = hQueue;
429 }
430
431
432 /***********************************************************************
433  *           QUEUE_CreateMsgQueue
434  *
435  * Creates a message queue. Doesn't link it into queue list!
436  */
437 static HQUEUE16 QUEUE_CreateMsgQueue( BOOL16 bCreatePerQData )
438 {
439     HQUEUE16 hQueue;
440     MESSAGEQUEUE * msgQueue;
441     TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
442
443     TRACE_(msg)("(): Creating message queue...\n");
444
445     if (!(hQueue = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT,
446                                   sizeof(MESSAGEQUEUE) )))
447         return 0;
448
449     msgQueue = (MESSAGEQUEUE *) GlobalLock16( hQueue );
450     if ( !msgQueue )
451         return 0;
452
453     msgQueue->self        = hQueue;
454     msgQueue->wakeBits    = msgQueue->changeBits = 0;
455     msgQueue->wWinVersion = pTask ? pTask->version : 0;
456     
457     InitializeCriticalSection( &msgQueue->cSection );
458     MakeCriticalSectionGlobal( &msgQueue->cSection );
459
460     /* Create an Event object for waiting on message, used by win32 thread
461        only */
462     if ( !THREAD_IsWin16( NtCurrentTeb() ) )
463     {
464         msgQueue->hEvent = CreateEventA( NULL, FALSE, FALSE, NULL);
465
466         if (msgQueue->hEvent == 0)
467         {
468             WARN_(msg)("CreateEvent32A is not able to create an event object");
469             return 0;
470         }
471         msgQueue->hEvent = ConvertToGlobalHandle( msgQueue->hEvent );
472     }
473     else
474         msgQueue->hEvent = 0;
475          
476     msgQueue->lockCount = 1;
477     msgQueue->magic = QUEUE_MAGIC;
478     
479     /* Create and initialize our per queue data */
480     msgQueue->pQData = bCreatePerQData ? PERQDATA_CreateInstance() : NULL;
481     
482     return hQueue;
483 }
484
485
486 /***********************************************************************
487  *           QUEUE_FlushMessage
488  * 
489  * Try to reply to all pending sent messages on exit.
490  */
491 static void QUEUE_FlushMessages( MESSAGEQUEUE *queue )
492 {
493     SMSG *smsg;
494     MESSAGEQUEUE *senderQ = 0;
495
496     if( queue )
497     {
498         EnterCriticalSection( &queue->cSection );
499
500         /* empty the list of pending SendMessage waiting to be received */
501         while (queue->smPending)
502         {
503             smsg = QUEUE_RemoveSMSG( queue, SM_PENDING_LIST, 0);
504
505             senderQ = (MESSAGEQUEUE*)QUEUE_Lock( smsg->hSrcQueue );
506             if ( !senderQ )
507                 continue;
508
509             /* return 0, to unblock other thread */
510             smsg->lResult = 0;
511             smsg->flags |= SMSG_HAVE_RESULT;
512             QUEUE_SetWakeBit( senderQ, QS_SMRESULT);
513             
514             QUEUE_Unlock( senderQ );
515         }
516
517         QUEUE_ClearWakeBit( queue, QS_SENDMESSAGE );
518         
519         LeaveCriticalSection( &queue->cSection );
520     }
521 }
522
523
524 /***********************************************************************
525  *           QUEUE_DeleteMsgQueue
526  *
527  * Unlinks and deletes a message queue.
528  *
529  * Note: We need to mask asynchronous events to make sure PostMessage works
530  * even in the signal handler.
531  */
532 BOOL QUEUE_DeleteMsgQueue( HQUEUE16 hQueue )
533 {
534     MESSAGEQUEUE * msgQueue = (MESSAGEQUEUE*)QUEUE_Lock(hQueue);
535     HQUEUE16 *pPrev;
536
537     TRACE_(msg)("(): Deleting message queue %04x\n", hQueue);
538
539     if (!hQueue || !msgQueue)
540     {
541         WARN_(msg)("invalid argument.\n");
542         return 0;
543     }
544
545     msgQueue->magic = 0;
546     
547     if( hCursorQueue == hQueue ) hCursorQueue = 0;
548     if( hActiveQueue == hQueue ) hActiveQueue = 0;
549
550     /* flush sent messages */
551     QUEUE_FlushMessages( msgQueue );
552
553     HeapLock( SystemHeap );  /* FIXME: a bit overkill */
554
555     /* Release per queue data if present */
556     if ( msgQueue->pQData )
557     {
558         PERQDATA_Release( msgQueue->pQData );
559         msgQueue->pQData = 0;
560     }
561     
562     /* remove the message queue from the global link list */
563     pPrev = &hFirstQueue;
564     while (*pPrev && (*pPrev != hQueue))
565     {
566         MESSAGEQUEUE *msgQ = (MESSAGEQUEUE*)GlobalLock16(*pPrev);
567
568         /* sanity check */
569         if ( !msgQ || (msgQ->magic != QUEUE_MAGIC) )
570         {
571             /* HQUEUE link list is corrupted, try to exit gracefully */
572             WARN_(msg)("HQUEUE link list corrupted!\n");
573             pPrev = 0;
574             break;
575         }
576         pPrev = &msgQ->next;
577     }
578     if (pPrev && *pPrev) *pPrev = msgQueue->next;
579     msgQueue->self = 0;
580
581     HeapUnlock( SystemHeap );
582
583     /* free up resource used by MESSAGEQUEUE strcture */
584     msgQueue->lockCount--;
585     QUEUE_Unlock( msgQueue );
586     
587     return 1;
588 }
589
590
591 /***********************************************************************
592  *           QUEUE_CreateSysMsgQueue
593  *
594  * Create the system message queue, and set the double-click speed.
595  * Must be called only once.
596  */
597 BOOL QUEUE_CreateSysMsgQueue( int size )
598 {
599     /* Note: We dont need perQ data for the system message queue */
600     if (!(hmemSysMsgQueue = QUEUE_CreateMsgQueue( FALSE )))
601         return FALSE;
602     
603     sysMsgQueue = (MESSAGEQUEUE *) GlobalLock16( hmemSysMsgQueue );
604     return TRUE;
605 }
606
607
608 /***********************************************************************
609  *           QUEUE_GetSysQueue
610  */
611 MESSAGEQUEUE *QUEUE_GetSysQueue(void)
612 {
613     return sysMsgQueue;
614 }
615
616
617 /***********************************************************************
618  *           QUEUE_SetWakeBit
619  *
620  * See "Windows Internals", p.449
621  */
622 void QUEUE_SetWakeBit( MESSAGEQUEUE *queue, WORD bit )
623 {
624     TRACE_(msg)("queue = %04x (wm=%04x), bit = %04x\n", 
625                         queue->self, queue->wakeMask, bit );
626
627     if (bit & QS_MOUSE) pMouseQueue = queue;
628     if (bit & QS_KEY) pKbdQueue = queue;
629     queue->changeBits |= bit;
630     queue->wakeBits   |= bit;
631     if (queue->wakeMask & bit)
632     {
633         queue->wakeMask = 0;
634         
635         /* Wake up thread waiting for message */
636         if ( THREAD_IsWin16( queue->teb ) )
637         {
638             int iWndsLock = WIN_SuspendWndsLock();
639             PostEvent16( queue->teb->process->task );
640             WIN_RestoreWndsLock( iWndsLock );
641         }
642         else
643         {
644             SetEvent( queue->hEvent );
645         }
646     }
647 }
648
649
650 /***********************************************************************
651  *           QUEUE_ClearWakeBit
652  */
653 void QUEUE_ClearWakeBit( MESSAGEQUEUE *queue, WORD bit )
654 {
655     queue->changeBits &= ~bit;
656     queue->wakeBits   &= ~bit;
657 }
658
659
660 /***********************************************************************
661  *           QUEUE_WaitBits
662  *
663  * See "Windows Internals", p.447
664  *
665  * return values:
666  *    0 if exit with timeout
667  *    1 otherwise
668  */
669 int QUEUE_WaitBits( WORD bits, DWORD timeout )
670 {
671     MESSAGEQUEUE *queue;
672     DWORD curTime = 0;
673
674     TRACE_(msg)("q %04x waiting for %04x\n", GetFastQueue16(), bits);
675
676     if ( THREAD_IsWin16( NtCurrentTeb() ) && (timeout != INFINITE) )
677         curTime = GetTickCount();
678
679     if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
680     
681     for (;;)
682     {
683         if (queue->changeBits & bits)
684         {
685             /* One of the bits is set; we can return */
686             queue->wakeMask = 0;
687             QUEUE_Unlock( queue );
688             return 1;
689         }
690         if (queue->wakeBits & QS_SENDMESSAGE)
691         {
692             /* Process the sent message immediately */
693
694             queue->wakeMask = 0;
695             QUEUE_ReceiveMessage( queue );
696             continue;                           /* nested sm crux */
697         }
698
699         queue->wakeMask = bits | QS_SENDMESSAGE;
700         if(queue->changeBits & bits)
701         {
702             continue;
703         }
704         
705         TRACE_(msg)("%04x) wakeMask is %04x, waiting\n", queue->self, queue->wakeMask);
706
707         if ( !THREAD_IsWin16( NtCurrentTeb() ) )
708         {
709             BOOL                bHasWin16Lock;
710             DWORD               dwlc;
711
712             if ( (bHasWin16Lock = _ConfirmWin16Lock()) )
713             {
714                 TRACE_(msg)("bHasWin16Lock=TRUE\n");
715                 ReleaseThunkLock( &dwlc );
716             }
717             WaitForSingleObject( queue->hEvent, timeout );
718             if ( bHasWin16Lock ) 
719             {
720                 RestoreThunkLock( dwlc );
721             }
722         }
723         else
724         {
725             if ( timeout == INFINITE )
726                 WaitEvent16( 0 );  /* win 16 thread, use WaitEvent */
727             else
728             {
729                 /* check for timeout, then give control to other tasks */
730                 if (GetTickCount() - curTime > timeout)
731                 {
732
733                     QUEUE_Unlock( queue );
734                     return 0;   /* exit with timeout */
735                 }
736                 Yield16();
737             }
738         }
739     }
740 }
741
742
743 /***********************************************************************
744  *           QUEUE_AddSMSG
745  *
746  * This routine is called when a SMSG need to be added to one of the three
747  * SM list.  (SM_PROCESSING_LIST, SM_PENDING_LIST, SM_WAITING_LIST)
748  */
749 BOOL QUEUE_AddSMSG( MESSAGEQUEUE *queue, int list, SMSG *smsg )
750 {
751     TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
752           smsg, SPY_GetMsgName(smsg->msg));
753     
754     switch (list)
755     {
756         case SM_PROCESSING_LIST:
757             /* don't need to be thread safe, only accessed by the
758              thread associated with the sender queue */
759             smsg->nextProcessing = queue->smProcessing;
760             queue->smProcessing = smsg;
761             break;
762             
763         case SM_WAITING_LIST:
764             /* don't need to be thread safe, only accessed by the
765              thread associated with the receiver queue */
766             smsg->nextWaiting = queue->smWaiting;
767             queue->smWaiting = smsg;
768             break;
769             
770         case SM_PENDING_LIST:
771         {
772             /* make it thread safe, could be accessed by the sender and
773              receiver thread */
774             SMSG **prev;
775
776             EnterCriticalSection( &queue->cSection );
777             smsg->nextPending = NULL;
778             prev = &queue->smPending;
779             while ( *prev )
780                 prev = &(*prev)->nextPending;
781             *prev = smsg;
782             LeaveCriticalSection( &queue->cSection );
783
784             QUEUE_SetWakeBit( queue, QS_SENDMESSAGE );
785             break;
786         }
787
788         default:
789             WARN_(sendmsg)("Invalid list: %d", list);
790             break;
791     }
792
793     return TRUE;
794 }
795
796
797 /***********************************************************************
798  *           QUEUE_RemoveSMSG
799  *
800  * This routine is called when a SMSG need to be remove from one of the three
801  * SM list.  (SM_PROCESSING_LIST, SM_PENDING_LIST, SM_WAITING_LIST)
802  * If smsg == 0, remove the first smsg from the specified list
803  */
804 SMSG *QUEUE_RemoveSMSG( MESSAGEQUEUE *queue, int list, SMSG *smsg )
805 {
806
807     switch (list)
808     {
809         case SM_PROCESSING_LIST:
810             /* don't need to be thread safe, only accessed by the
811              thread associated with the sender queue */
812
813             /* if smsg is equal to null, it means the first in the list */
814             if (!smsg)
815                 smsg = queue->smProcessing;
816
817             TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
818                   smsg, SPY_GetMsgName(smsg->msg));
819             /* In fact SM_PROCESSING_LIST is a stack, and smsg
820              should be always at the top of the list */
821             if ( (smsg != queue->smProcessing) || !queue->smProcessing )
822         {
823                 ERR_(sendmsg)("smsg not at the top of Processing list, smsg=0x%p queue=0x%p", smsg, queue);
824                 return 0;
825             }
826             else
827             {
828                 queue->smProcessing = smsg->nextProcessing;
829                 smsg->nextProcessing = 0;
830         }
831             return smsg;
832
833         case SM_WAITING_LIST:
834             /* don't need to be thread safe, only accessed by the
835              thread associated with the receiver queue */
836
837             /* if smsg is equal to null, it means the first in the list */
838             if (!smsg)
839                 smsg = queue->smWaiting;
840             
841             TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
842                   smsg, SPY_GetMsgName(smsg->msg));
843             /* In fact SM_WAITING_LIST is a stack, and smsg
844              should be always at the top of the list */
845             if ( (smsg != queue->smWaiting) || !queue->smWaiting )
846             {
847                 ERR_(sendmsg)("smsg not at the top of Waiting list, smsg=0x%p queue=0x%p", smsg, queue);
848                 return 0;
849             }
850             else
851             {
852                 queue->smWaiting = smsg->nextWaiting;
853                 smsg->nextWaiting = 0;
854     }
855             return smsg;
856
857         case SM_PENDING_LIST:
858             /* make it thread safe, could be accessed by the sender and
859              receiver thread */
860             EnterCriticalSection( &queue->cSection );
861     
862             if (!smsg || !queue->smPending)
863                 smsg = queue->smPending;
864             else
865             {
866                 ERR_(sendmsg)("should always remove the top one in Pending list, smsg=0x%p queue=0x%p", smsg, queue);
867                                 LeaveCriticalSection( &queue->cSection );
868                 return 0;
869             }
870             
871             TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
872                   smsg, SPY_GetMsgName(smsg->msg));
873
874             queue->smPending = smsg->nextPending;
875             smsg->nextPending = 0;
876
877             /* if no more SMSG in Pending list, clear QS_SENDMESSAGE flag */
878             if (!queue->smPending)
879                 QUEUE_ClearWakeBit( queue, QS_SENDMESSAGE );
880             
881             LeaveCriticalSection( &queue->cSection );
882             return smsg;
883
884         default:
885             WARN_(sendmsg)("Invalid list: %d", list);
886             break;
887     }
888
889     return 0;
890 }
891
892
893 /***********************************************************************
894  *           QUEUE_ReceiveMessage
895  * 
896  * This routine is called when a sent message is waiting for the queue.
897  */
898 void QUEUE_ReceiveMessage( MESSAGEQUEUE *queue )
899 {
900     LRESULT       result = 0;
901     SMSG          *smsg;
902     MESSAGEQUEUE  *senderQ;
903
904     TRACE_(sendmsg)("queue %04x\n", queue->self );
905
906     if ( !(queue->wakeBits & QS_SENDMESSAGE) && queue->smPending )
907     {
908         TRACE_(sendmsg)("\trcm: nothing to do\n");
909         return;
910     }
911
912     /* remove smsg on the top of the pending list and put it in the processing list */
913     smsg = QUEUE_RemoveSMSG(queue, SM_PENDING_LIST, 0);
914     QUEUE_AddSMSG(queue, SM_WAITING_LIST, smsg);
915
916     TRACE_(sendmsg)("RM: %s [%04x] (%04x -> %04x)\n",
917             SPY_GetMsgName(smsg->msg), smsg->msg, smsg->hSrcQueue, smsg->hDstQueue );
918
919     if (IsWindow( smsg->hWnd ))
920     {
921         WND *wndPtr = WIN_FindWndPtr( smsg->hWnd );
922         DWORD extraInfo = queue->GetMessageExtraInfoVal; /* save ExtraInfo */
923
924         /* use sender queue extra info value while calling the window proc */
925         senderQ = (MESSAGEQUEUE*)QUEUE_Lock( smsg->hSrcQueue );
926         if (senderQ)
927   {
928             queue->GetMessageExtraInfoVal = senderQ->GetMessageExtraInfoVal;
929             QUEUE_Unlock( senderQ );
930         }
931
932         /* call the right version of CallWindowProcXX */
933         if (smsg->flags & SMSG_WIN32)
934         {
935             TRACE_(sendmsg)("\trcm: msg is Win32\n" );
936             if (smsg->flags & SMSG_UNICODE)
937                 result = CallWindowProcW( wndPtr->winproc,
938                                             smsg->hWnd, smsg->msg,
939                                             smsg->wParam, smsg->lParam );
940             else
941                 result = CallWindowProcA( wndPtr->winproc,
942                                             smsg->hWnd, smsg->msg,
943                                             smsg->wParam, smsg->lParam );
944         }
945         else  /* Win16 message */
946             result = CallWindowProc16( (WNDPROC16)wndPtr->winproc,
947                                        (HWND16) smsg->hWnd,
948                                        (UINT16) smsg->msg,
949                                        LOWORD (smsg->wParam),
950                                        smsg->lParam );
951
952         queue->GetMessageExtraInfoVal = extraInfo;  /* Restore extra info */
953         WIN_ReleaseWndPtr(wndPtr);
954         TRACE_(sendmsg)("result =  %08x\n", (unsigned)result );
955     }
956     else WARN_(sendmsg)("\trcm: bad hWnd\n");
957
958     
959         /* set SMSG_SENDING_REPLY flag to tell ReplyMessage16, it's not
960          an early reply */
961         smsg->flags |= SMSG_SENDING_REPLY;
962         ReplyMessage( result );
963
964     TRACE_(sendmsg)("done! \n" );
965 }
966
967
968
969 /***********************************************************************
970  *           QUEUE_AddMsg
971  *
972  * Add a message to the queue. Return FALSE if queue is full.
973  */
974 BOOL QUEUE_AddMsg( HQUEUE16 hQueue, MSG *msg, DWORD extraInfo )
975 {
976     MESSAGEQUEUE *msgQueue;
977     QMSG         *qmsg;
978
979
980     if (!(msgQueue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return FALSE;
981
982     /* allocate new message in global heap for now */
983     if (!(qmsg = (QMSG *) HeapAlloc( SystemHeap, 0, sizeof(QMSG) ) ))
984     {
985         QUEUE_Unlock( msgQueue );
986         return 0;
987     }
988
989     EnterCriticalSection( &msgQueue->cSection );
990
991       /* Store message */
992     qmsg->msg = *msg;
993     qmsg->extraInfo = extraInfo;
994
995     /* insert the message in the link list */
996     qmsg->nextMsg = 0;
997     qmsg->prevMsg = msgQueue->lastMsg;
998
999     if (msgQueue->lastMsg)
1000         msgQueue->lastMsg->nextMsg = qmsg;
1001
1002     /* update first and last anchor in message queue */
1003     msgQueue->lastMsg = qmsg;
1004     if (!msgQueue->firstMsg)
1005         msgQueue->firstMsg = qmsg;
1006     
1007     msgQueue->msgCount++;
1008
1009     LeaveCriticalSection( &msgQueue->cSection );
1010
1011     QUEUE_SetWakeBit( msgQueue, QS_POSTMESSAGE );
1012     QUEUE_Unlock( msgQueue );
1013     
1014     return TRUE;
1015 }
1016
1017
1018
1019 /***********************************************************************
1020  *           QUEUE_FindMsg
1021  *
1022  * Find a message matching the given parameters. Return -1 if none available.
1023  */
1024 QMSG* QUEUE_FindMsg( MESSAGEQUEUE * msgQueue, HWND hwnd, int first, int last )
1025 {
1026     QMSG* qmsg;
1027
1028     EnterCriticalSection( &msgQueue->cSection );
1029
1030     if (!msgQueue->msgCount)
1031         qmsg = 0;
1032     else if (!hwnd && !first && !last)
1033         qmsg = msgQueue->firstMsg;
1034     else
1035     {
1036         /* look in linked list for message matching first and last criteria */
1037         for (qmsg = msgQueue->firstMsg; qmsg; qmsg = qmsg->nextMsg)
1038     {
1039             MSG *msg = &(qmsg->msg);
1040
1041         if (!hwnd || (msg->hwnd == hwnd))
1042         {
1043                 if (!first && !last)
1044                     break;   /* found it */
1045                 
1046                 if ((msg->message >= first) && (!last || (msg->message <= last)))
1047                     break;   /* found it */
1048             }
1049         }
1050     }
1051     
1052     LeaveCriticalSection( &msgQueue->cSection );
1053
1054     return qmsg;
1055 }
1056
1057
1058
1059 /***********************************************************************
1060  *           QUEUE_RemoveMsg
1061  *
1062  * Remove a message from the queue (pos must be a valid position).
1063  */
1064 void QUEUE_RemoveMsg( MESSAGEQUEUE * msgQueue, QMSG *qmsg )
1065 {
1066     EnterCriticalSection( &msgQueue->cSection );
1067
1068     /* set the linked list */
1069     if (qmsg->prevMsg)
1070         qmsg->prevMsg->nextMsg = qmsg->nextMsg;
1071
1072     if (qmsg->nextMsg)
1073         qmsg->nextMsg->prevMsg = qmsg->prevMsg;
1074
1075     if (msgQueue->firstMsg == qmsg)
1076         msgQueue->firstMsg = qmsg->nextMsg;
1077
1078     if (msgQueue->lastMsg == qmsg)
1079         msgQueue->lastMsg = qmsg->prevMsg;
1080
1081     /* deallocate the memory for the message */
1082     HeapFree( SystemHeap, 0, qmsg );
1083     
1084     msgQueue->msgCount--;
1085     if (!msgQueue->msgCount) msgQueue->wakeBits &= ~QS_POSTMESSAGE;
1086
1087     LeaveCriticalSection( &msgQueue->cSection );
1088 }
1089
1090
1091 /***********************************************************************
1092  *           QUEUE_WakeSomeone
1093  *
1094  * Wake a queue upon reception of a hardware event.
1095  */
1096 static void QUEUE_WakeSomeone( UINT message )
1097 {
1098     WND*          wndPtr = NULL;
1099     WORD          wakeBit;
1100     HWND hwnd;
1101     HQUEUE16     hQueue = 0;
1102     MESSAGEQUEUE *queue = NULL;
1103
1104     if (hCursorQueue)
1105         hQueue = hCursorQueue;
1106
1107     if( (message >= WM_KEYFIRST) && (message <= WM_KEYLAST) )
1108     {
1109        wakeBit = QS_KEY;
1110        if( hActiveQueue )
1111            hQueue = hActiveQueue;
1112     }
1113     else 
1114     {
1115        wakeBit = (message == WM_MOUSEMOVE) ? QS_MOUSEMOVE : QS_MOUSEBUTTON;
1116        if( (hwnd = GetCapture()) )
1117          if( (wndPtr = WIN_FindWndPtr( hwnd )) ) 
1118            {
1119                hQueue = wndPtr->hmemTaskQ;
1120                WIN_ReleaseWndPtr(wndPtr);
1121            }
1122     }
1123
1124     if( (hwnd = GetSysModalWindow16()) )
1125     {
1126       if( (wndPtr = WIN_FindWndPtr( hwnd )) )
1127         {
1128             hQueue = wndPtr->hmemTaskQ;
1129             WIN_ReleaseWndPtr(wndPtr);
1130         }
1131     }
1132
1133     if (hQueue)
1134         queue = QUEUE_Lock( hQueue );
1135     
1136     if( !queue ) 
1137     {
1138         queue = QUEUE_Lock( hFirstQueue );
1139       while( queue )
1140       {
1141         if (queue->wakeMask & wakeBit) break;
1142           
1143             QUEUE_Unlock(queue);
1144             queue = QUEUE_Lock( queue->next );
1145       }
1146       if( !queue )
1147       { 
1148         WARN_(msg)("couldn't find queue\n"); 
1149         return; 
1150       }
1151     }
1152
1153     QUEUE_SetWakeBit( queue, wakeBit );
1154
1155     QUEUE_Unlock( queue );
1156 }
1157
1158
1159 /***********************************************************************
1160  *           hardware_event
1161  *
1162  * Add an event to the system message queue.
1163  * Note: the position is relative to the desktop window.
1164  */
1165 void hardware_event( UINT message, WPARAM wParam, LPARAM lParam,
1166                      int xPos, int yPos, DWORD time, DWORD extraInfo )
1167 {
1168     MSG *msg;
1169     QMSG  *qmsg;
1170     int  mergeMsg = 0;
1171
1172     if (!sysMsgQueue) return;
1173
1174     EnterCriticalSection( &sysMsgQueue->cSection );
1175
1176     /* Merge with previous event if possible */
1177     qmsg = sysMsgQueue->lastMsg;
1178
1179     if ((message == WM_MOUSEMOVE) && sysMsgQueue->lastMsg)
1180     {
1181         msg = &(sysMsgQueue->lastMsg->msg);
1182         
1183         if ((msg->message == message) && (msg->wParam == wParam))
1184         {
1185             /* Merge events */
1186             qmsg = sysMsgQueue->lastMsg;
1187             mergeMsg = 1;
1188     }
1189     }
1190
1191     if (!mergeMsg)
1192     {
1193         /* Should I limit the number of message in
1194           the system message queue??? */
1195
1196         /* Don't merge allocate a new msg in the global heap */
1197         
1198         if (!(qmsg = (QMSG *) HeapAlloc( SystemHeap, 0, sizeof(QMSG) ) ))
1199         {
1200             LeaveCriticalSection( &sysMsgQueue->cSection );
1201             return;
1202         }
1203         
1204         /* put message at the end of the linked list */
1205         qmsg->nextMsg = 0;
1206         qmsg->prevMsg = sysMsgQueue->lastMsg;
1207
1208         if (sysMsgQueue->lastMsg)
1209             sysMsgQueue->lastMsg->nextMsg = qmsg;
1210
1211         /* set last and first anchor index in system message queue */
1212         sysMsgQueue->lastMsg = qmsg;
1213         if (!sysMsgQueue->firstMsg)
1214             sysMsgQueue->firstMsg = qmsg;
1215         
1216         sysMsgQueue->msgCount++;
1217     }
1218
1219       /* Store message */
1220     msg = &(qmsg->msg);
1221     msg->hwnd    = 0;
1222     msg->message = message;
1223     msg->wParam  = wParam;
1224     msg->lParam  = lParam;
1225     msg->time    = time;
1226     msg->pt.x    = xPos;
1227     msg->pt.y    = yPos;
1228     qmsg->extraInfo = extraInfo;
1229
1230     LeaveCriticalSection( &sysMsgQueue->cSection );
1231
1232     QUEUE_WakeSomeone( message );
1233 }
1234
1235                     
1236 /***********************************************************************
1237  *           QUEUE_GetQueueTask
1238  */
1239 HTASK16 QUEUE_GetQueueTask( HQUEUE16 hQueue )
1240 {
1241     HTASK16 hTask = 0;
1242     
1243     MESSAGEQUEUE *queue = QUEUE_Lock( hQueue );
1244
1245     if (queue)
1246 {
1247         hTask = queue->teb->process->task;
1248         QUEUE_Unlock( queue );
1249 }
1250
1251     return hTask;
1252 }
1253
1254
1255
1256 /***********************************************************************
1257  *           QUEUE_IncPaintCount
1258  */
1259 void QUEUE_IncPaintCount( HQUEUE16 hQueue )
1260 {
1261     MESSAGEQUEUE *queue;
1262
1263     if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1264     queue->wPaintCount++;
1265     QUEUE_SetWakeBit( queue, QS_PAINT );
1266     QUEUE_Unlock( queue );
1267 }
1268
1269
1270 /***********************************************************************
1271  *           QUEUE_DecPaintCount
1272  */
1273 void QUEUE_DecPaintCount( HQUEUE16 hQueue )
1274 {
1275     MESSAGEQUEUE *queue;
1276
1277     if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1278     queue->wPaintCount--;
1279     if (!queue->wPaintCount) queue->wakeBits &= ~QS_PAINT;
1280     QUEUE_Unlock( queue );
1281 }
1282
1283
1284 /***********************************************************************
1285  *           QUEUE_IncTimerCount
1286  */
1287 void QUEUE_IncTimerCount( HQUEUE16 hQueue )
1288 {
1289     MESSAGEQUEUE *queue;
1290
1291     if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1292     queue->wTimerCount++;
1293     QUEUE_SetWakeBit( queue, QS_TIMER );
1294     QUEUE_Unlock( queue );
1295 }
1296
1297
1298 /***********************************************************************
1299  *           QUEUE_DecTimerCount
1300  */
1301 void QUEUE_DecTimerCount( HQUEUE16 hQueue )
1302 {
1303     MESSAGEQUEUE *queue;
1304
1305     if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1306     queue->wTimerCount--;
1307     if (!queue->wTimerCount) queue->wakeBits &= ~QS_TIMER;
1308     QUEUE_Unlock( queue );
1309 }
1310
1311
1312 /***********************************************************************
1313  *           PostQuitMessage16   (USER.6)
1314  */
1315 void WINAPI PostQuitMessage16( INT16 exitCode )
1316 {
1317     PostQuitMessage( exitCode );
1318 }
1319
1320
1321 /***********************************************************************
1322  *           PostQuitMessage32   (USER32.421)
1323  *
1324  * PostQuitMessage() posts a message to the system requesting an
1325  * application to terminate execution. As a result of this function,
1326  * the WM_QUIT message is posted to the application, and
1327  * PostQuitMessage() returns immediately.  The exitCode parameter
1328  * specifies an application-defined exit code, which appears in the
1329  * _wParam_ parameter of the WM_QUIT message posted to the application.  
1330  *
1331  * CONFORMANCE
1332  *
1333  *  ECMA-234, Win32
1334  */
1335 void WINAPI PostQuitMessage( INT exitCode )
1336 {
1337     MESSAGEQUEUE *queue;
1338
1339     if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return;
1340     queue->wPostQMsg = TRUE;
1341     queue->wExitCode = (WORD)exitCode;
1342     QUEUE_Unlock( queue );
1343 }
1344
1345
1346 /***********************************************************************
1347  *           GetWindowTask16   (USER.224)
1348  */
1349 HTASK16 WINAPI GetWindowTask16( HWND16 hwnd )
1350 {
1351     HTASK16 retvalue;
1352     WND *wndPtr = WIN_FindWndPtr( hwnd );
1353
1354     if (!wndPtr) return 0;
1355     retvalue = QUEUE_GetQueueTask( wndPtr->hmemTaskQ );
1356     WIN_ReleaseWndPtr(wndPtr);
1357     return retvalue;
1358 }
1359
1360 /***********************************************************************
1361  *           GetWindowThreadProcessId   (USER32.313)
1362  */
1363 DWORD WINAPI GetWindowThreadProcessId( HWND hwnd, LPDWORD process )
1364 {
1365     DWORD retvalue;
1366     MESSAGEQUEUE *queue;
1367
1368     WND *wndPtr = WIN_FindWndPtr( hwnd );
1369     if (!wndPtr) return 0;
1370
1371     queue = QUEUE_Lock( wndPtr->hmemTaskQ );
1372     WIN_ReleaseWndPtr(wndPtr);
1373
1374     if (!queue) return 0;
1375
1376     if ( process ) *process = (DWORD)queue->teb->process->server_pid;
1377     retvalue = (DWORD)queue->teb->tid;
1378
1379     QUEUE_Unlock( queue );
1380     return retvalue;
1381 }
1382
1383
1384 /***********************************************************************
1385  *           SetMessageQueue16   (USER.266)
1386  */
1387 BOOL16 WINAPI SetMessageQueue16( INT16 size )
1388 {
1389     return SetMessageQueue( size );
1390 }
1391
1392
1393 /***********************************************************************
1394  *           SetMessageQueue32   (USER32.494)
1395  */
1396 BOOL WINAPI SetMessageQueue( INT size )
1397 {
1398     /* now obsolete the message queue will be expanded dynamically
1399      as necessary */
1400
1401     /* access the queue to create it if it's not existing */
1402     GetFastQueue16();
1403
1404     return TRUE;
1405 }
1406
1407 /***********************************************************************
1408  *           InitThreadInput   (USER.409)
1409  */
1410 HQUEUE16 WINAPI InitThreadInput16( WORD unknown, WORD flags )
1411 {
1412     HQUEUE16 hQueue;
1413     MESSAGEQUEUE *queuePtr;
1414
1415     TEB *teb = NtCurrentTeb();
1416
1417     if (!teb)
1418         return 0;
1419
1420     hQueue = teb->queue;
1421     
1422     if ( !hQueue )
1423     {
1424         /* Create thread message queue */
1425         if( !(hQueue = QUEUE_CreateMsgQueue( TRUE )))
1426         {
1427             WARN_(msg)("failed!\n");
1428             return FALSE;
1429     }
1430         
1431         /* Link new queue into list */
1432         queuePtr = (MESSAGEQUEUE *)QUEUE_Lock( hQueue );
1433         queuePtr->teb = NtCurrentTeb();
1434
1435         HeapLock( SystemHeap );  /* FIXME: a bit overkill */
1436         SetThreadQueue16( 0, hQueue );
1437         teb->queue = hQueue;
1438             
1439         queuePtr->next  = hFirstQueue;
1440         hFirstQueue = hQueue;
1441         HeapUnlock( SystemHeap );
1442         
1443         QUEUE_Unlock( queuePtr );
1444     }
1445
1446     return hQueue;
1447 }
1448
1449 /***********************************************************************
1450  *           GetQueueStatus16   (USER.334)
1451  */
1452 DWORD WINAPI GetQueueStatus16( UINT16 flags )
1453 {
1454     MESSAGEQUEUE *queue;
1455     DWORD ret;
1456
1457     if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1458     ret = MAKELONG( queue->changeBits, queue->wakeBits );
1459     queue->changeBits = 0;
1460     QUEUE_Unlock( queue );
1461     
1462     return ret & MAKELONG( flags, flags );
1463 }
1464
1465 /***********************************************************************
1466  *           GetQueueStatus32   (USER32.283)
1467  */
1468 DWORD WINAPI GetQueueStatus( UINT flags )
1469 {
1470     MESSAGEQUEUE *queue;
1471     DWORD ret;
1472
1473     if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1474     ret = MAKELONG( queue->changeBits, queue->wakeBits );
1475     queue->changeBits = 0;
1476     QUEUE_Unlock( queue );
1477     
1478     return ret & MAKELONG( flags, flags );
1479 }
1480
1481
1482 /***********************************************************************
1483  *           GetInputState16   (USER.335)
1484  */
1485 BOOL16 WINAPI GetInputState16(void)
1486 {
1487     return GetInputState();
1488 }
1489
1490 /***********************************************************************
1491  *           WaitForInputIdle   (USER32.577)
1492  */
1493 DWORD WINAPI WaitForInputIdle (HANDLE hProcess, DWORD dwTimeOut)
1494 {
1495   FIXME_(msg)("(hProcess=%d, dwTimeOut=%ld): stub\n", hProcess, dwTimeOut);
1496
1497   return WAIT_TIMEOUT;
1498 }
1499
1500
1501 /***********************************************************************
1502  *           GetInputState32   (USER32.244)
1503  */
1504 BOOL WINAPI GetInputState(void)
1505 {
1506     MESSAGEQUEUE *queue;
1507     BOOL ret;
1508
1509     if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() )))
1510         return FALSE;
1511     ret = queue->wakeBits & (QS_KEY | QS_MOUSEBUTTON);
1512     QUEUE_Unlock( queue );
1513
1514     return ret;
1515 }
1516
1517 /***********************************************************************
1518  *           UserYield  (USER.332)
1519  */
1520 void WINAPI UserYield16(void)
1521 {
1522     MESSAGEQUEUE *queue;
1523
1524     /* Handle sent messages */
1525     queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() );
1526
1527     while (queue && (queue->wakeBits & QS_SENDMESSAGE))
1528         QUEUE_ReceiveMessage( queue );
1529
1530     QUEUE_Unlock( queue );
1531     
1532     /* Yield */
1533     if ( THREAD_IsWin16( NtCurrentTeb() ) )
1534         OldYield16();
1535     else
1536         WIN32_OldYield16();
1537
1538     /* Handle sent messages again */
1539     queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() );
1540
1541     while (queue && (queue->wakeBits & QS_SENDMESSAGE))
1542         QUEUE_ReceiveMessage( queue );
1543
1544     QUEUE_Unlock( queue );
1545 }
1546
1547 /***********************************************************************
1548  *           GetMessagePos   (USER.119) (USER32.272)
1549  * 
1550  * The GetMessagePos() function returns a long value representing a
1551  * cursor position, in screen coordinates, when the last message
1552  * retrieved by the GetMessage() function occurs. The x-coordinate is
1553  * in the low-order word of the return value, the y-coordinate is in
1554  * the high-order word. The application can use the MAKEPOINT()
1555  * macro to obtain a POINT structure from the return value. 
1556  *
1557  * For the current cursor position, use GetCursorPos().
1558  *
1559  * RETURNS
1560  *
1561  * Cursor position of last message on success, zero on failure.
1562  *
1563  * CONFORMANCE
1564  *
1565  * ECMA-234, Win32
1566  *
1567  */
1568 DWORD WINAPI GetMessagePos(void)
1569 {
1570     MESSAGEQUEUE *queue;
1571     DWORD ret;
1572
1573     if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1574     ret = queue->GetMessagePosVal;
1575     QUEUE_Unlock( queue );
1576
1577     return ret;
1578 }
1579
1580
1581 /***********************************************************************
1582  *           GetMessageTime   (USER.120) (USER32.273)
1583  *
1584  * GetMessageTime() returns the message time for the last message
1585  * retrieved by the function. The time is measured in milliseconds with
1586  * the same offset as GetTickCount().
1587  *
1588  * Since the tick count wraps, this is only useful for moderately short
1589  * relative time comparisons.
1590  *
1591  * RETURNS
1592  *
1593  * Time of last message on success, zero on failure.
1594  *
1595  * CONFORMANCE
1596  *
1597  * ECMA-234, Win32
1598  *  
1599  */
1600 LONG WINAPI GetMessageTime(void)
1601 {
1602     MESSAGEQUEUE *queue;
1603     LONG ret;
1604
1605     if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1606     ret = queue->GetMessageTimeVal;
1607     QUEUE_Unlock( queue );
1608     
1609     return ret;
1610 }
1611
1612
1613 /***********************************************************************
1614  *           GetMessageExtraInfo   (USER.288) (USER32.271)
1615  */
1616 LONG WINAPI GetMessageExtraInfo(void)
1617 {
1618     MESSAGEQUEUE *queue;
1619     LONG ret;
1620
1621     if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1622     ret = queue->GetMessageExtraInfoVal;
1623     QUEUE_Unlock( queue );
1624
1625     return ret;
1626 }