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