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