Fixed some deadlock(s) in message sending.
[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             BOOL                bHasWin16Lock;
705             DWORD               dwlc;
706
707             if ( (bHasWin16Lock = _ConfirmWin16Lock()) )
708             {
709                 TRACE_(msg)("bHasWin16Lock=TRUE\n");
710                 ReleaseThunkLock( &dwlc );
711             }
712             WaitForSingleObject( queue->hEvent, timeout );
713             if ( bHasWin16Lock ) 
714             {
715                 RestoreThunkLock( dwlc );
716             }
717         }
718         else
719         {
720             if ( timeout == INFINITE )
721                 WaitEvent16( 0 );  /* win 16 thread, use WaitEvent */
722             else
723             {
724                 /* check for timeout, then give control to other tasks */
725                 if (GetTickCount() - curTime > timeout)
726                 {
727
728                     QUEUE_Unlock( queue );
729                     return 0;   /* exit with timeout */
730                 }
731                 Yield16();
732             }
733         }
734     }
735 }
736
737
738 /***********************************************************************
739  *           QUEUE_AddSMSG
740  *
741  * This routine is called when a SMSG need to be added to one of the three
742  * SM list.  (SM_PROCESSING_LIST, SM_PENDING_LIST, SM_WAITING_LIST)
743  */
744 BOOL QUEUE_AddSMSG( MESSAGEQUEUE *queue, int list, SMSG *smsg )
745 {
746     TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
747           smsg, SPY_GetMsgName(smsg->msg));
748     
749     switch (list)
750     {
751         case SM_PROCESSING_LIST:
752             /* don't need to be thread safe, only accessed by the
753              thread associated with the sender queue */
754             smsg->nextProcessing = queue->smProcessing;
755             queue->smProcessing = smsg;
756             break;
757             
758         case SM_WAITING_LIST:
759             /* don't need to be thread safe, only accessed by the
760              thread associated with the receiver queue */
761             smsg->nextWaiting = queue->smWaiting;
762             queue->smWaiting = smsg;
763             break;
764             
765         case SM_PENDING_LIST:
766         {
767             /* make it thread safe, could be accessed by the sender and
768              receiver thread */
769             SMSG **prev;
770
771             EnterCriticalSection( &queue->cSection );
772             smsg->nextPending = NULL;
773             prev = &queue->smPending;
774             while ( *prev )
775                 prev = &(*prev)->nextPending;
776             *prev = smsg;
777             LeaveCriticalSection( &queue->cSection );
778
779             QUEUE_SetWakeBit( queue, QS_SENDMESSAGE );
780             break;
781         }
782
783         default:
784             WARN_(sendmsg)("Invalid list: %d", list);
785             break;
786     }
787
788     return TRUE;
789 }
790
791
792 /***********************************************************************
793  *           QUEUE_RemoveSMSG
794  *
795  * This routine is called when a SMSG need to be remove from one of the three
796  * SM list.  (SM_PROCESSING_LIST, SM_PENDING_LIST, SM_WAITING_LIST)
797  * If smsg == 0, remove the first smsg from the specified list
798  */
799 SMSG *QUEUE_RemoveSMSG( MESSAGEQUEUE *queue, int list, SMSG *smsg )
800 {
801
802     switch (list)
803     {
804         case SM_PROCESSING_LIST:
805             /* don't need to be thread safe, only accessed by the
806              thread associated with the sender queue */
807
808             /* if smsg is equal to null, it means the first in the list */
809             if (!smsg)
810                 smsg = queue->smProcessing;
811
812             TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
813                   smsg, SPY_GetMsgName(smsg->msg));
814             /* In fact SM_PROCESSING_LIST is a stack, and smsg
815              should be always at the top of the list */
816             if ( (smsg != queue->smProcessing) || !queue->smProcessing )
817         {
818                 ERR_(sendmsg)("smsg not at the top of Processing list, smsg=0x%p queue=0x%p", smsg, queue);
819                 return 0;
820             }
821             else
822             {
823                 queue->smProcessing = smsg->nextProcessing;
824                 smsg->nextProcessing = 0;
825         }
826             return smsg;
827
828         case SM_WAITING_LIST:
829             /* don't need to be thread safe, only accessed by the
830              thread associated with the receiver queue */
831
832             /* if smsg is equal to null, it means the first in the list */
833             if (!smsg)
834                 smsg = queue->smWaiting;
835             
836             TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
837                   smsg, SPY_GetMsgName(smsg->msg));
838             /* In fact SM_WAITING_LIST is a stack, and smsg
839              should be always at the top of the list */
840             if ( (smsg != queue->smWaiting) || !queue->smWaiting )
841             {
842                 ERR_(sendmsg)("smsg not at the top of Waiting list, smsg=0x%p queue=0x%p", smsg, queue);
843                 return 0;
844             }
845             else
846             {
847                 queue->smWaiting = smsg->nextWaiting;
848                 smsg->nextWaiting = 0;
849     }
850             return smsg;
851
852         case SM_PENDING_LIST:
853             /* make it thread safe, could be accessed by the sender and
854              receiver thread */
855             EnterCriticalSection( &queue->cSection );
856     
857             if (!smsg || !queue->smPending)
858                 smsg = queue->smPending;
859             else
860             {
861                 ERR_(sendmsg)("should always remove the top one in Pending list, smsg=0x%p queue=0x%p", smsg, queue);
862                                 LeaveCriticalSection( &queue->cSection );
863                 return 0;
864             }
865             
866             TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
867                   smsg, SPY_GetMsgName(smsg->msg));
868
869             queue->smPending = smsg->nextPending;
870             smsg->nextPending = 0;
871
872             /* if no more SMSG in Pending list, clear QS_SENDMESSAGE flag */
873             if (!queue->smPending)
874                 QUEUE_ClearWakeBit( queue, QS_SENDMESSAGE );
875             
876             LeaveCriticalSection( &queue->cSection );
877             return smsg;
878
879         default:
880             WARN_(sendmsg)("Invalid list: %d", list);
881             break;
882     }
883
884     return 0;
885 }
886
887
888 /***********************************************************************
889  *           QUEUE_ReceiveMessage
890  * 
891  * This routine is called when a sent message is waiting for the queue.
892  */
893 void QUEUE_ReceiveMessage( MESSAGEQUEUE *queue )
894 {
895     LRESULT       result = 0;
896     SMSG          *smsg;
897     MESSAGEQUEUE  *senderQ;
898
899     TRACE_(sendmsg)("queue %04x\n", queue->self );
900
901     if ( !(queue->wakeBits & QS_SENDMESSAGE) && queue->smPending )
902     {
903         TRACE_(sendmsg)("\trcm: nothing to do\n");
904         return;
905     }
906
907     /* remove smsg on the top of the pending list and put it in the processing list */
908     smsg = QUEUE_RemoveSMSG(queue, SM_PENDING_LIST, 0);
909     QUEUE_AddSMSG(queue, SM_WAITING_LIST, smsg);
910
911     TRACE_(sendmsg)("RM: %s [%04x] (%04x -> %04x)\n",
912             SPY_GetMsgName(smsg->msg), smsg->msg, smsg->hSrcQueue, smsg->hDstQueue );
913
914     if (IsWindow( smsg->hWnd ))
915     {
916         WND *wndPtr = WIN_FindWndPtr( smsg->hWnd );
917         DWORD extraInfo = queue->GetMessageExtraInfoVal; /* save ExtraInfo */
918
919         /* use sender queue extra info value while calling the window proc */
920         senderQ = (MESSAGEQUEUE*)QUEUE_Lock( smsg->hSrcQueue );
921         if (senderQ)
922   {
923             queue->GetMessageExtraInfoVal = senderQ->GetMessageExtraInfoVal;
924             QUEUE_Unlock( senderQ );
925         }
926
927         /* call the right version of CallWindowProcXX */
928         if (smsg->flags & SMSG_WIN32)
929         {
930             TRACE_(sendmsg)("\trcm: msg is Win32\n" );
931             if (smsg->flags & SMSG_UNICODE)
932                 result = CallWindowProcW( wndPtr->winproc,
933                                             smsg->hWnd, smsg->msg,
934                                             smsg->wParam, smsg->lParam );
935             else
936                 result = CallWindowProcA( wndPtr->winproc,
937                                             smsg->hWnd, smsg->msg,
938                                             smsg->wParam, smsg->lParam );
939         }
940         else  /* Win16 message */
941             result = CallWindowProc16( (WNDPROC16)wndPtr->winproc,
942                                        (HWND16) smsg->hWnd,
943                                        (UINT16) smsg->msg,
944                                        LOWORD (smsg->wParam),
945                                        smsg->lParam );
946
947         queue->GetMessageExtraInfoVal = extraInfo;  /* Restore extra info */
948         WIN_ReleaseWndPtr(wndPtr);
949         TRACE_(sendmsg)("result =  %08x\n", (unsigned)result );
950     }
951     else WARN_(sendmsg)("\trcm: bad hWnd\n");
952
953     
954         /* set SMSG_SENDING_REPLY flag to tell ReplyMessage16, it's not
955          an early reply */
956         smsg->flags |= SMSG_SENDING_REPLY;
957         ReplyMessage( result );
958
959     TRACE_(sendmsg)("done! \n" );
960 }
961
962
963
964 /***********************************************************************
965  *           QUEUE_AddMsg
966  *
967  * Add a message to the queue. Return FALSE if queue is full.
968  */
969 BOOL QUEUE_AddMsg( HQUEUE16 hQueue, MSG *msg, DWORD extraInfo )
970 {
971     MESSAGEQUEUE *msgQueue;
972     QMSG         *qmsg;
973
974
975     if (!(msgQueue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return FALSE;
976
977     /* allocate new message in global heap for now */
978     if (!(qmsg = (QMSG *) HeapAlloc( SystemHeap, 0, sizeof(QMSG) ) ))
979     {
980         QUEUE_Unlock( msgQueue );
981         return 0;
982     }
983
984     EnterCriticalSection( &msgQueue->cSection );
985
986       /* Store message */
987     qmsg->msg = *msg;
988     qmsg->extraInfo = extraInfo;
989
990     /* insert the message in the link list */
991     qmsg->nextMsg = 0;
992     qmsg->prevMsg = msgQueue->lastMsg;
993
994     if (msgQueue->lastMsg)
995         msgQueue->lastMsg->nextMsg = qmsg;
996
997     /* update first and last anchor in message queue */
998     msgQueue->lastMsg = qmsg;
999     if (!msgQueue->firstMsg)
1000         msgQueue->firstMsg = qmsg;
1001     
1002     msgQueue->msgCount++;
1003
1004     LeaveCriticalSection( &msgQueue->cSection );
1005
1006     QUEUE_SetWakeBit( msgQueue, QS_POSTMESSAGE );
1007     QUEUE_Unlock( msgQueue );
1008     
1009     return TRUE;
1010 }
1011
1012
1013
1014 /***********************************************************************
1015  *           QUEUE_FindMsg
1016  *
1017  * Find a message matching the given parameters. Return -1 if none available.
1018  */
1019 QMSG* QUEUE_FindMsg( MESSAGEQUEUE * msgQueue, HWND hwnd, int first, int last )
1020 {
1021     QMSG* qmsg;
1022
1023     EnterCriticalSection( &msgQueue->cSection );
1024
1025     if (!msgQueue->msgCount)
1026         qmsg = 0;
1027     else if (!hwnd && !first && !last)
1028         qmsg = msgQueue->firstMsg;
1029     else
1030     {
1031         /* look in linked list for message matching first and last criteria */
1032         for (qmsg = msgQueue->firstMsg; qmsg; qmsg = qmsg->nextMsg)
1033     {
1034             MSG *msg = &(qmsg->msg);
1035
1036         if (!hwnd || (msg->hwnd == hwnd))
1037         {
1038                 if (!first && !last)
1039                     break;   /* found it */
1040                 
1041                 if ((msg->message >= first) && (!last || (msg->message <= last)))
1042                     break;   /* found it */
1043             }
1044         }
1045     }
1046     
1047     LeaveCriticalSection( &msgQueue->cSection );
1048
1049     return qmsg;
1050 }
1051
1052
1053
1054 /***********************************************************************
1055  *           QUEUE_RemoveMsg
1056  *
1057  * Remove a message from the queue (pos must be a valid position).
1058  */
1059 void QUEUE_RemoveMsg( MESSAGEQUEUE * msgQueue, QMSG *qmsg )
1060 {
1061     EnterCriticalSection( &msgQueue->cSection );
1062
1063     /* set the linked list */
1064     if (qmsg->prevMsg)
1065         qmsg->prevMsg->nextMsg = qmsg->nextMsg;
1066
1067     if (qmsg->nextMsg)
1068         qmsg->nextMsg->prevMsg = qmsg->prevMsg;
1069
1070     if (msgQueue->firstMsg == qmsg)
1071         msgQueue->firstMsg = qmsg->nextMsg;
1072
1073     if (msgQueue->lastMsg == qmsg)
1074         msgQueue->lastMsg = qmsg->prevMsg;
1075
1076     /* deallocate the memory for the message */
1077     HeapFree( SystemHeap, 0, qmsg );
1078     
1079     msgQueue->msgCount--;
1080     if (!msgQueue->msgCount) msgQueue->wakeBits &= ~QS_POSTMESSAGE;
1081
1082     LeaveCriticalSection( &msgQueue->cSection );
1083 }
1084
1085
1086 /***********************************************************************
1087  *           QUEUE_WakeSomeone
1088  *
1089  * Wake a queue upon reception of a hardware event.
1090  */
1091 static void QUEUE_WakeSomeone( UINT message )
1092 {
1093     WND*          wndPtr = NULL;
1094     WORD          wakeBit;
1095     HWND hwnd;
1096     HQUEUE16     hQueue = 0;
1097     MESSAGEQUEUE *queue = NULL;
1098
1099     if (hCursorQueue)
1100         hQueue = hCursorQueue;
1101
1102     if( (message >= WM_KEYFIRST) && (message <= WM_KEYLAST) )
1103     {
1104        wakeBit = QS_KEY;
1105        if( hActiveQueue )
1106            hQueue = hActiveQueue;
1107     }
1108     else 
1109     {
1110        wakeBit = (message == WM_MOUSEMOVE) ? QS_MOUSEMOVE : QS_MOUSEBUTTON;
1111        if( (hwnd = GetCapture()) )
1112          if( (wndPtr = WIN_FindWndPtr( hwnd )) ) 
1113            {
1114                hQueue = wndPtr->hmemTaskQ;
1115                WIN_ReleaseWndPtr(wndPtr);
1116            }
1117     }
1118
1119     if( (hwnd = GetSysModalWindow16()) )
1120     {
1121       if( (wndPtr = WIN_FindWndPtr( hwnd )) )
1122         {
1123             hQueue = wndPtr->hmemTaskQ;
1124             WIN_ReleaseWndPtr(wndPtr);
1125         }
1126     }
1127
1128     if (hQueue)
1129         queue = QUEUE_Lock( hQueue );
1130     
1131     if( !queue ) 
1132     {
1133         queue = QUEUE_Lock( hFirstQueue );
1134       while( queue )
1135       {
1136         if (queue->wakeMask & wakeBit) break;
1137           
1138             QUEUE_Unlock(queue);
1139             queue = QUEUE_Lock( queue->next );
1140       }
1141       if( !queue )
1142       { 
1143         WARN_(msg)("couldn't find queue\n"); 
1144         return; 
1145       }
1146     }
1147
1148     QUEUE_SetWakeBit( queue, wakeBit );
1149
1150     QUEUE_Unlock( queue );
1151 }
1152
1153
1154 /***********************************************************************
1155  *           hardware_event
1156  *
1157  * Add an event to the system message queue.
1158  * Note: the position is relative to the desktop window.
1159  */
1160 void hardware_event( WORD message, WORD wParam, LONG lParam,
1161                      int xPos, int yPos, DWORD time, DWORD extraInfo )
1162 {
1163     MSG *msg;
1164     QMSG  *qmsg;
1165     int  mergeMsg = 0;
1166
1167     if (!sysMsgQueue) return;
1168
1169     EnterCriticalSection( &sysMsgQueue->cSection );
1170
1171     /* Merge with previous event if possible */
1172     qmsg = sysMsgQueue->lastMsg;
1173
1174     if ((message == WM_MOUSEMOVE) && sysMsgQueue->lastMsg)
1175     {
1176         msg = &(sysMsgQueue->lastMsg->msg);
1177         
1178         if ((msg->message == message) && (msg->wParam == wParam))
1179         {
1180             /* Merge events */
1181             qmsg = sysMsgQueue->lastMsg;
1182             mergeMsg = 1;
1183     }
1184     }
1185
1186     if (!mergeMsg)
1187     {
1188         /* Should I limit the number of message in
1189           the system message queue??? */
1190
1191         /* Don't merge allocate a new msg in the global heap */
1192         
1193         if (!(qmsg = (QMSG *) HeapAlloc( SystemHeap, 0, sizeof(QMSG) ) ))
1194         {
1195             LeaveCriticalSection( &sysMsgQueue->cSection );
1196             return;
1197         }
1198         
1199         /* put message at the end of the linked list */
1200         qmsg->nextMsg = 0;
1201         qmsg->prevMsg = sysMsgQueue->lastMsg;
1202
1203         if (sysMsgQueue->lastMsg)
1204             sysMsgQueue->lastMsg->nextMsg = qmsg;
1205
1206         /* set last and first anchor index in system message queue */
1207         sysMsgQueue->lastMsg = qmsg;
1208         if (!sysMsgQueue->firstMsg)
1209             sysMsgQueue->firstMsg = qmsg;
1210         
1211         sysMsgQueue->msgCount++;
1212     }
1213
1214       /* Store message */
1215     msg = &(qmsg->msg);
1216     msg->hwnd    = 0;
1217     msg->message = message;
1218     msg->wParam  = wParam;
1219     msg->lParam  = lParam;
1220     msg->time    = time;
1221     msg->pt.x    = xPos;
1222     msg->pt.y    = yPos;
1223     qmsg->extraInfo = extraInfo;
1224
1225     LeaveCriticalSection( &sysMsgQueue->cSection );
1226
1227     QUEUE_WakeSomeone( message );
1228 }
1229
1230                     
1231 /***********************************************************************
1232  *           QUEUE_GetQueueTask
1233  */
1234 HTASK16 QUEUE_GetQueueTask( HQUEUE16 hQueue )
1235 {
1236     HTASK16 hTask = 0;
1237     
1238     MESSAGEQUEUE *queue = QUEUE_Lock( hQueue );
1239
1240     if (queue)
1241 {
1242         hTask = queue->thdb->process->task;
1243         QUEUE_Unlock( queue );
1244 }
1245
1246     return hTask;
1247 }
1248
1249
1250
1251 /***********************************************************************
1252  *           QUEUE_IncPaintCount
1253  */
1254 void QUEUE_IncPaintCount( HQUEUE16 hQueue )
1255 {
1256     MESSAGEQUEUE *queue;
1257
1258     if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1259     queue->wPaintCount++;
1260     QUEUE_SetWakeBit( queue, QS_PAINT );
1261     QUEUE_Unlock( queue );
1262 }
1263
1264
1265 /***********************************************************************
1266  *           QUEUE_DecPaintCount
1267  */
1268 void QUEUE_DecPaintCount( HQUEUE16 hQueue )
1269 {
1270     MESSAGEQUEUE *queue;
1271
1272     if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1273     queue->wPaintCount--;
1274     if (!queue->wPaintCount) queue->wakeBits &= ~QS_PAINT;
1275     QUEUE_Unlock( queue );
1276 }
1277
1278
1279 /***********************************************************************
1280  *           QUEUE_IncTimerCount
1281  */
1282 void QUEUE_IncTimerCount( HQUEUE16 hQueue )
1283 {
1284     MESSAGEQUEUE *queue;
1285
1286     if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1287     queue->wTimerCount++;
1288     QUEUE_SetWakeBit( queue, QS_TIMER );
1289     QUEUE_Unlock( queue );
1290 }
1291
1292
1293 /***********************************************************************
1294  *           QUEUE_DecTimerCount
1295  */
1296 void QUEUE_DecTimerCount( HQUEUE16 hQueue )
1297 {
1298     MESSAGEQUEUE *queue;
1299
1300     if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1301     queue->wTimerCount--;
1302     if (!queue->wTimerCount) queue->wakeBits &= ~QS_TIMER;
1303     QUEUE_Unlock( queue );
1304 }
1305
1306
1307 /***********************************************************************
1308  *           PostQuitMessage16   (USER.6)
1309  */
1310 void WINAPI PostQuitMessage16( INT16 exitCode )
1311 {
1312     PostQuitMessage( exitCode );
1313 }
1314
1315
1316 /***********************************************************************
1317  *           PostQuitMessage32   (USER32.421)
1318  *
1319  * PostQuitMessage() posts a message to the system requesting an
1320  * application to terminate execution. As a result of this function,
1321  * the WM_QUIT message is posted to the application, and
1322  * PostQuitMessage() returns immediately.  The exitCode parameter
1323  * specifies an application-defined exit code, which appears in the
1324  * _wParam_ parameter of the WM_QUIT message posted to the application.  
1325  *
1326  * CONFORMANCE
1327  *
1328  *  ECMA-234, Win32
1329  */
1330 void WINAPI PostQuitMessage( INT exitCode )
1331 {
1332     MESSAGEQUEUE *queue;
1333
1334     if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return;
1335     queue->wPostQMsg = TRUE;
1336     queue->wExitCode = (WORD)exitCode;
1337     QUEUE_Unlock( queue );
1338 }
1339
1340
1341 /***********************************************************************
1342  *           GetWindowTask16   (USER.224)
1343  */
1344 HTASK16 WINAPI GetWindowTask16( HWND16 hwnd )
1345 {
1346     HTASK16 retvalue;
1347     WND *wndPtr = WIN_FindWndPtr( hwnd );
1348
1349     if (!wndPtr) return 0;
1350     retvalue = QUEUE_GetQueueTask( wndPtr->hmemTaskQ );
1351     WIN_ReleaseWndPtr(wndPtr);
1352     return retvalue;
1353 }
1354
1355 /***********************************************************************
1356  *           GetWindowThreadProcessId   (USER32.313)
1357  */
1358 DWORD WINAPI GetWindowThreadProcessId( HWND hwnd, LPDWORD process )
1359 {
1360     HTASK16 htask;
1361     TDB *tdb;
1362
1363     WND *wndPtr = WIN_FindWndPtr( hwnd );
1364
1365     if (!wndPtr) return 0;
1366     htask=QUEUE_GetQueueTask( wndPtr->hmemTaskQ );
1367     WIN_ReleaseWndPtr(wndPtr);
1368     tdb = (TDB*)GlobalLock16(htask);
1369     if (!tdb || !tdb->thdb) return 0;
1370     if (process) *process = (DWORD)tdb->thdb->process->server_pid;
1371     return (DWORD)tdb->thdb->server_tid;
1372 }
1373
1374
1375 /***********************************************************************
1376  *           SetMessageQueue16   (USER.266)
1377  */
1378 BOOL16 WINAPI SetMessageQueue16( INT16 size )
1379 {
1380     return SetMessageQueue( size );
1381 }
1382
1383
1384 /***********************************************************************
1385  *           SetMessageQueue32   (USER32.494)
1386  */
1387 BOOL WINAPI SetMessageQueue( INT size )
1388 {
1389     /* now obsolete the message queue will be expanded dynamically
1390      as necessary */
1391
1392     /* access the queue to create it if it's not existing */
1393     GetFastQueue16();
1394
1395     return TRUE;
1396 }
1397
1398 /***********************************************************************
1399  *           InitThreadInput   (USER.409)
1400  */
1401 HQUEUE16 WINAPI InitThreadInput16( WORD unknown, WORD flags )
1402 {
1403     HQUEUE16 hQueue;
1404     MESSAGEQUEUE *queuePtr;
1405
1406     THDB *thdb = THREAD_Current();
1407
1408     if (!thdb)
1409         return 0;
1410
1411     hQueue = thdb->teb.queue;
1412     
1413     if ( !hQueue )
1414     {
1415         /* Create thread message queue */
1416         if( !(hQueue = QUEUE_CreateMsgQueue( TRUE )))
1417         {
1418             WARN_(msg)("failed!\n");
1419             return FALSE;
1420     }
1421         
1422         /* Link new queue into list */
1423         queuePtr = (MESSAGEQUEUE *)QUEUE_Lock( hQueue );
1424         queuePtr->thdb = THREAD_Current();
1425
1426         HeapLock( SystemHeap );  /* FIXME: a bit overkill */
1427         SetThreadQueue16( 0, hQueue );
1428         thdb->teb.queue = hQueue;
1429             
1430         queuePtr->next  = hFirstQueue;
1431         hFirstQueue = hQueue;
1432         HeapUnlock( SystemHeap );
1433         
1434         QUEUE_Unlock( queuePtr );
1435     }
1436
1437     return hQueue;
1438 }
1439
1440 /***********************************************************************
1441  *           GetQueueStatus16   (USER.334)
1442  */
1443 DWORD WINAPI GetQueueStatus16( UINT16 flags )
1444 {
1445     MESSAGEQUEUE *queue;
1446     DWORD ret;
1447
1448     if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1449     ret = MAKELONG( queue->changeBits, queue->wakeBits );
1450     queue->changeBits = 0;
1451     QUEUE_Unlock( queue );
1452     
1453     return ret & MAKELONG( flags, flags );
1454 }
1455
1456 /***********************************************************************
1457  *           GetQueueStatus32   (USER32.283)
1458  */
1459 DWORD WINAPI GetQueueStatus( UINT flags )
1460 {
1461     MESSAGEQUEUE *queue;
1462     DWORD ret;
1463
1464     if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1465     ret = MAKELONG( queue->changeBits, queue->wakeBits );
1466     queue->changeBits = 0;
1467     QUEUE_Unlock( queue );
1468     
1469     return ret & MAKELONG( flags, flags );
1470 }
1471
1472
1473 /***********************************************************************
1474  *           GetInputState16   (USER.335)
1475  */
1476 BOOL16 WINAPI GetInputState16(void)
1477 {
1478     return GetInputState();
1479 }
1480
1481 /***********************************************************************
1482  *           WaitForInputIdle   (USER32.577)
1483  */
1484 DWORD WINAPI WaitForInputIdle (HANDLE hProcess, DWORD dwTimeOut)
1485 {
1486   FIXME_(msg)("(hProcess=%d, dwTimeOut=%ld): stub\n", hProcess, dwTimeOut);
1487
1488   return WAIT_TIMEOUT;
1489 }
1490
1491
1492 /***********************************************************************
1493  *           GetInputState32   (USER32.244)
1494  */
1495 BOOL WINAPI GetInputState(void)
1496 {
1497     MESSAGEQUEUE *queue;
1498     BOOL ret;
1499
1500     if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() )))
1501         return FALSE;
1502     ret = queue->wakeBits & (QS_KEY | QS_MOUSEBUTTON);
1503     QUEUE_Unlock( queue );
1504
1505     return ret;
1506 }
1507
1508 /***********************************************************************
1509  *           UserYield  (USER.332)
1510  */
1511 void WINAPI UserYield16(void)
1512 {
1513     MESSAGEQUEUE *queue;
1514
1515     /* Handle sent messages */
1516     queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() );
1517
1518     while (queue && (queue->wakeBits & QS_SENDMESSAGE))
1519         QUEUE_ReceiveMessage( queue );
1520
1521     QUEUE_Unlock( queue );
1522     
1523     /* Yield */
1524     if ( THREAD_IsWin16( THREAD_Current() ) )
1525         OldYield16();
1526     else
1527     {
1528         DWORD  count;
1529
1530        ReleaseThunkLock(&count);
1531        RestoreThunkLock(count);
1532     }
1533
1534     /* Handle sent messages again */
1535     queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() );
1536
1537     while (queue && (queue->wakeBits & QS_SENDMESSAGE))
1538         QUEUE_ReceiveMessage( queue );
1539
1540     QUEUE_Unlock( queue );
1541 }
1542
1543 /***********************************************************************
1544  *           GetMessagePos   (USER.119) (USER32.272)
1545  * 
1546  * The GetMessagePos() function returns a long value representing a
1547  * cursor position, in screen coordinates, when the last message
1548  * retrieved by the GetMessage() function occurs. The x-coordinate is
1549  * in the low-order word of the return value, the y-coordinate is in
1550  * the high-order word. The application can use the MAKEPOINT()
1551  * macro to obtain a POINT structure from the return value. 
1552  *
1553  * For the current cursor position, use GetCursorPos().
1554  *
1555  * RETURNS
1556  *
1557  * Cursor position of last message on success, zero on failure.
1558  *
1559  * CONFORMANCE
1560  *
1561  * ECMA-234, Win32
1562  *
1563  */
1564 DWORD WINAPI GetMessagePos(void)
1565 {
1566     MESSAGEQUEUE *queue;
1567     DWORD ret;
1568
1569     if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1570     ret = queue->GetMessagePosVal;
1571     QUEUE_Unlock( queue );
1572
1573     return ret;
1574 }
1575
1576
1577 /***********************************************************************
1578  *           GetMessageTime   (USER.120) (USER32.273)
1579  *
1580  * GetMessageTime() returns the message time for the last message
1581  * retrieved by the function. The time is measured in milliseconds with
1582  * the same offset as GetTickCount().
1583  *
1584  * Since the tick count wraps, this is only useful for moderately short
1585  * relative time comparisons.
1586  *
1587  * RETURNS
1588  *
1589  * Time of last message on success, zero on failure.
1590  *
1591  * CONFORMANCE
1592  *
1593  * ECMA-234, Win32
1594  *  
1595  */
1596 LONG WINAPI GetMessageTime(void)
1597 {
1598     MESSAGEQUEUE *queue;
1599     LONG ret;
1600
1601     if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1602     ret = queue->GetMessageTimeVal;
1603     QUEUE_Unlock( queue );
1604     
1605     return ret;
1606 }
1607
1608
1609 /***********************************************************************
1610  *           GetMessageExtraInfo   (USER.288) (USER32.271)
1611  */
1612 LONG WINAPI GetMessageExtraInfo(void)
1613 {
1614     MESSAGEQUEUE *queue;
1615     LONG ret;
1616
1617     if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1618     ret = queue->GetMessageExtraInfoVal;
1619     QUEUE_Unlock( queue );
1620
1621     return ret;
1622 }