Implemented stub for InitThreadInput.
[wine] / windows / queue.c
1 /*
2  * Message queues related functions
3  *
4  * Copyright 1993, 1994 Alexandre Julliard
5  */
6
7 #include <stdlib.h>
8 #include <signal.h>
9 #include "miscemu.h"
10 #include "module.h"
11 #include "queue.h"
12 #include "task.h"
13 #include "win.h"
14 #include "clipboard.h"
15 #include "hook.h"
16 #include "heap.h"
17 #include "thread.h"
18 #include "process.h"
19 #include "debug.h"
20
21 #define MAX_QUEUE_SIZE   120  /* Max. size of a message queue */
22
23 static HQUEUE16 hFirstQueue = 0;
24 static HQUEUE16 hExitingQueue = 0;
25 static HQUEUE16 hmemSysMsgQueue = 0;
26 static MESSAGEQUEUE *sysMsgQueue = NULL;
27
28 static MESSAGEQUEUE *pMouseQueue = NULL;  /* Queue for last mouse message */
29 static MESSAGEQUEUE *pKbdQueue = NULL;    /* Queue for last kbd message */
30
31 MESSAGEQUEUE *pCursorQueue = NULL; 
32 MESSAGEQUEUE *pActiveQueue = NULL;
33
34 /***********************************************************************
35  *           QUEUE_DumpQueue
36  */
37 void QUEUE_DumpQueue( HQUEUE16 hQueue )
38 {
39     MESSAGEQUEUE *pq; 
40
41     if (!(pq = (MESSAGEQUEUE*) GlobalLock16( hQueue )) ||
42         GlobalSize16(hQueue) < sizeof(MESSAGEQUEUE)+pq->queueSize*sizeof(QMSG))
43     {
44         WARN(msg, "%04x is not a queue handle\n", hQueue );
45         return;
46     }
47
48     DUMP(    "next: %12.4x  Intertask SendMessage:\n"
49              "hTask: %11.4x  ----------------------\n"
50              "msgSize: %9.4x  hWnd: %10.4x\n"
51              "msgCount: %8.4x  msg: %11.4x\n"
52              "msgNext: %9.4x  wParam: %8.4x\n"
53              "msgFree: %9.4x  lParam: %8.8x\n"
54              "qSize: %11.4x  lRet: %10.8x\n"
55              "wWinVer: %9.4x  ISMH: %10.4x\n"
56              "paints: %10.4x  hSendTask: %5.4x\n"
57              "timers: %10.4x  hPrevSend: %5.4x\n"
58              "wakeBits: %8.4x\n"
59              "wakeMask: %8.4x\n"
60              "hCurHook: %8.4x\n",
61              pq->next, pq->hTask, pq->msgSize, pq->hWnd, 
62              pq->msgCount, pq->msg, pq->nextMessage, pq->wParam,
63              pq->nextFreeMessage, (unsigned)pq->lParam, pq->queueSize,
64              (unsigned)pq->SendMessageReturn, pq->wWinVersion, pq->InSendMessageHandle,
65              pq->wPaintCount, pq->hSendingTask, pq->wTimerCount,
66              pq->hPrevSendingTask, pq->wakeBits, pq->wakeMask, pq->hCurHook);
67 }
68
69
70 /***********************************************************************
71  *           QUEUE_WalkQueues
72  */
73 void QUEUE_WalkQueues(void)
74 {
75     char module[10];
76     HQUEUE16 hQueue = hFirstQueue;
77
78     DUMP( "Queue Size Msgs Task\n" );
79     while (hQueue)
80     {
81         MESSAGEQUEUE *queue = (MESSAGEQUEUE *)GlobalLock16( hQueue );
82         if (!queue)
83         {
84             WARN( msg, "Bad queue handle %04x\n", hQueue );
85             return;
86         }
87         if (!GetModuleName( queue->hTask, module, sizeof(module )))
88             strcpy( module, "???" );
89         DUMP( "%04x %5d %4d %04x %s\n", hQueue, queue->msgSize,
90                  queue->msgCount, queue->hTask, module );
91         hQueue = queue->next;
92     }
93     DUMP( "\n" );
94 }
95
96
97 /***********************************************************************
98  *           QUEUE_IsExitingQueue
99  */
100 BOOL32 QUEUE_IsExitingQueue( HQUEUE16 hQueue )
101 {
102     return (hExitingQueue && (hQueue == hExitingQueue));
103 }
104
105
106 /***********************************************************************
107  *           QUEUE_SetExitingQueue
108  */
109 void QUEUE_SetExitingQueue( HQUEUE16 hQueue )
110 {
111     hExitingQueue = hQueue;
112 }
113
114
115 /***********************************************************************
116  *           QUEUE_CreateMsgQueue
117  *
118  * Creates a message queue. Doesn't link it into queue list!
119  */
120 static HQUEUE16 QUEUE_CreateMsgQueue( int size )
121 {
122     HQUEUE16 hQueue;
123     MESSAGEQUEUE * msgQueue;
124     int queueSize;
125     TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
126
127     TRACE(msg,"Creating message queue...\n");
128
129     queueSize = sizeof(MESSAGEQUEUE) + size * sizeof(QMSG);
130     if (!(hQueue = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, queueSize )))
131         return 0;
132     msgQueue = (MESSAGEQUEUE *) GlobalLock16( hQueue );
133     msgQueue->self        = hQueue;
134     msgQueue->msgSize     = sizeof(QMSG);
135     msgQueue->queueSize   = size;
136     msgQueue->wakeBits    = msgQueue->changeBits = QS_SMPARAMSFREE;
137     msgQueue->wWinVersion = pTask ? pTask->version : 0;
138     GlobalUnlock16( hQueue );
139     return hQueue;
140 }
141
142
143 /***********************************************************************
144  *           QUEUE_DeleteMsgQueue
145  *
146  * Unlinks and deletes a message queue.
147  *
148  * Note: We need to mask asynchronous events to make sure PostMessage works
149  * even in the signal handler.
150  */
151 BOOL32 QUEUE_DeleteMsgQueue( HQUEUE16 hQueue )
152 {
153     MESSAGEQUEUE * msgQueue = (MESSAGEQUEUE*)GlobalLock16(hQueue);
154     HQUEUE16  senderQ;
155     HQUEUE16 *pPrev;
156
157     TRACE(msg,"Deleting message queue %04x\n", hQueue);
158
159     if (!hQueue || !msgQueue)
160     {
161         WARN(msg, "invalid argument.\n");
162         return 0;
163     }
164     if( pCursorQueue == msgQueue ) pCursorQueue = NULL;
165     if( pActiveQueue == msgQueue ) pActiveQueue = NULL;
166
167     /* flush sent messages */
168     senderQ = msgQueue->hSendingTask;
169     while( senderQ )
170     {
171       MESSAGEQUEUE* sq = (MESSAGEQUEUE*)GlobalLock16(senderQ);
172       if( !sq ) break;
173       sq->SendMessageReturn = 0L;
174       QUEUE_SetWakeBit( sq, QS_SMRESULT );
175       senderQ = sq->hPrevSendingTask;
176     }
177
178     SIGNAL_MaskAsyncEvents( TRUE );
179
180     pPrev = &hFirstQueue;
181     while (*pPrev && (*pPrev != hQueue))
182     {
183         MESSAGEQUEUE *msgQ = (MESSAGEQUEUE*)GlobalLock16(*pPrev);
184         pPrev = &msgQ->next;
185     }
186     if (*pPrev) *pPrev = msgQueue->next;
187     msgQueue->self = 0;
188
189     SIGNAL_MaskAsyncEvents( FALSE );
190
191     GlobalFree16( hQueue );
192     return 1;
193 }
194
195
196 /***********************************************************************
197  *           QUEUE_CreateSysMsgQueue
198  *
199  * Create the system message queue, and set the double-click speed.
200  * Must be called only once.
201  */
202 BOOL32 QUEUE_CreateSysMsgQueue( int size )
203 {
204     if (size > MAX_QUEUE_SIZE) size = MAX_QUEUE_SIZE;
205     else if (size <= 0) size = 1;
206     if (!(hmemSysMsgQueue = QUEUE_CreateMsgQueue( size ))) return FALSE;
207     sysMsgQueue = (MESSAGEQUEUE *) GlobalLock16( hmemSysMsgQueue );
208     return TRUE;
209 }
210
211
212 /***********************************************************************
213  *           QUEUE_GetSysQueue
214  */
215 MESSAGEQUEUE *QUEUE_GetSysQueue(void)
216 {
217     return sysMsgQueue;
218 }
219
220 /***********************************************************************
221  *           QUEUE_Signal
222  */
223 void QUEUE_Signal( HTASK16 hTask )
224 {
225     PDB32 *pdb;
226     THREAD_ENTRY *entry;
227     int wakeup = FALSE;
228
229     TDB *pTask = (TDB *)GlobalLock16( hTask );
230     if ( !pTask ) return;
231
232     TRACE(msg, "calling SYNC_MsgWakeUp\n");
233
234     /* Wake up thread waiting for message */
235     /* NOTE: This should really wake up *the* thread that owns
236              the queue. Since we dont't have thread-local message
237              queues yet, we wake up all waiting threads ... */
238     SYSTEM_LOCK();
239     pdb = pTask->thdb->process;
240     entry = pdb? pdb->thread_list->next : NULL;
241
242     if (entry)
243         for (;;)
244         {
245             if (entry->thread->wait_struct.wait_msg)
246             {
247                 SYNC_MsgWakeUp( entry->thread );
248                 wakeup = TRUE;
249             }
250             if (entry == pdb->thread_list) break;
251             entry = entry->next;
252         }
253     SYSTEM_UNLOCK();
254
255 /*    if ( !wakeup )*/
256         PostEvent( hTask );
257 }
258
259 /***********************************************************************
260  *           QUEUE_Wait
261  */
262 void QUEUE_Wait( void )
263 {
264     if ( THREAD_IsWin16( THREAD_Current() ) )
265         WaitEvent( 0 );
266     else
267     {
268         TRACE(msg, "current task is 32-bit, calling SYNC_DoWait\n");
269         SYNC_DoWait( 0, NULL, FALSE, INFINITE32, FALSE, TRUE );
270     }
271 }
272
273
274 /***********************************************************************
275  *           QUEUE_SetWakeBit
276  *
277  * See "Windows Internals", p.449
278  */
279 void QUEUE_SetWakeBit( MESSAGEQUEUE *queue, WORD bit )
280 {
281     TRACE(msg,"queue = %04x (wm=%04x), bit = %04x\n", 
282                         queue->self, queue->wakeMask, bit );
283
284     if (bit & QS_MOUSE) pMouseQueue = queue;
285     if (bit & QS_KEY) pKbdQueue = queue;
286     queue->changeBits |= bit;
287     queue->wakeBits   |= bit;
288     if (queue->wakeMask & bit)
289     {
290         queue->wakeMask = 0;
291         QUEUE_Signal( queue->hTask );
292     }
293 }
294
295
296 /***********************************************************************
297  *           QUEUE_ClearWakeBit
298  */
299 void QUEUE_ClearWakeBit( MESSAGEQUEUE *queue, WORD bit )
300 {
301     queue->changeBits &= ~bit;
302     queue->wakeBits   &= ~bit;
303 }
304
305
306 /***********************************************************************
307  *           QUEUE_WaitBits
308  *
309  * See "Windows Internals", p.447
310  */
311 void QUEUE_WaitBits( WORD bits )
312 {
313     MESSAGEQUEUE *queue;
314
315     TRACE(msg,"q %04x waiting for %04x\n", GetFastQueue(), bits);
316
317     for (;;)
318     {
319         if (!(queue = (MESSAGEQUEUE *)GlobalLock16( GetFastQueue() ))) return;
320
321         if (queue->changeBits & bits)
322         {
323             /* One of the bits is set; we can return */
324             queue->wakeMask = 0;
325             return;
326         }
327         if (queue->wakeBits & QS_SENDMESSAGE)
328         {
329             /* Process the sent message immediately */
330
331             queue->wakeMask = 0;
332             QUEUE_ReceiveMessage( queue );
333             continue;                           /* nested sm crux */
334         }
335
336         queue->wakeMask = bits | QS_SENDMESSAGE;
337         if(queue->changeBits & bits) continue;
338         
339         TRACE(msg,"%04x) wakeMask is %04x, waiting\n", queue->self, queue->wakeMask);
340
341         QUEUE_Wait();
342     }
343 }
344
345
346 /***********************************************************************
347  *           QUEUE_ReceiveMessage
348  *
349  * This routine is called when a sent message is waiting for the queue.
350  */
351 void QUEUE_ReceiveMessage( MESSAGEQUEUE *queue )
352 {
353     MESSAGEQUEUE *senderQ = NULL;
354     HQUEUE16      prevSender = 0;
355     QSMCTRL*      prevCtrlPtr = NULL;
356     LRESULT       result = 0;
357
358     TRACE(msg, "ReceiveMessage, queue %04x\n", queue->self );
359     if (!(queue->wakeBits & QS_SENDMESSAGE) ||
360         !(senderQ = (MESSAGEQUEUE*)GlobalLock16( queue->hSendingTask))) 
361         { TRACE(msg,"\trcm: nothing to do\n"); return; }
362
363     if( !senderQ->hPrevSendingTask )
364         QUEUE_ClearWakeBit( queue, QS_SENDMESSAGE );   /* no more sent messages */
365
366     /* Save current state on stack */
367     prevSender                 = queue->InSendMessageHandle;
368     prevCtrlPtr                = queue->smResultCurrent;
369
370     /* Remove sending queue from the list */
371     queue->InSendMessageHandle = queue->hSendingTask;
372     queue->smResultCurrent     = senderQ->smResultInit;
373     queue->hSendingTask        = senderQ->hPrevSendingTask;
374
375     TRACE(msg, "\trcm: smResultCurrent = %08x, prevCtrl = %08x\n", 
376                                 (unsigned)queue->smResultCurrent, (unsigned)prevCtrlPtr );
377     QUEUE_SetWakeBit( senderQ, QS_SMPARAMSFREE );
378
379     TRACE(msg, "\trcm: calling wndproc - %04x %04x %04x%04x %08x\n",
380                 senderQ->hWnd, senderQ->msg, senderQ->wParamHigh,
381                 senderQ->wParam, (unsigned)senderQ->lParam );
382
383     if (IsWindow32( senderQ->hWnd ))
384     {
385         WND *wndPtr = WIN_FindWndPtr( senderQ->hWnd );
386         DWORD extraInfo = queue->GetMessageExtraInfoVal;
387         queue->GetMessageExtraInfoVal = senderQ->GetMessageExtraInfoVal;
388
389         if (senderQ->flags & QUEUE_SM_WIN32)
390         {
391             WPARAM32 wParam = MAKELONG( senderQ->wParam, senderQ->wParamHigh );
392             TRACE(msg, "\trcm: msg is Win32\n" );
393             if (senderQ->flags & QUEUE_SM_UNICODE)
394                 result = CallWindowProc32W( wndPtr->winproc,
395                                             senderQ->hWnd, senderQ->msg,
396                                             wParam, senderQ->lParam );
397             else
398                 result = CallWindowProc32A( wndPtr->winproc,
399                                             senderQ->hWnd, senderQ->msg,
400                                             wParam, senderQ->lParam );
401         }
402         else  /* Win16 message */
403             result = CallWindowProc16( (WNDPROC16)wndPtr->winproc,
404                                        senderQ->hWnd, senderQ->msg,
405                                        senderQ->wParam, senderQ->lParam );
406
407         queue->GetMessageExtraInfoVal = extraInfo;  /* Restore extra info */
408         TRACE(msg,"\trcm: result =  %08x\n", (unsigned)result );
409     }
410     else WARN(msg, "\trcm: bad hWnd\n");
411
412     /* Return the result to the sender task */
413     ReplyMessage16( result );
414
415     queue->InSendMessageHandle = prevSender;
416     queue->smResultCurrent     = prevCtrlPtr;
417
418     TRACE(msg,"done!\n");
419 }
420
421 /***********************************************************************
422  *           QUEUE_FlushMessage
423  * 
424  * Try to reply to all pending sent messages on exit.
425  */
426 void QUEUE_FlushMessages( HQUEUE16 hQueue )
427 {
428   MESSAGEQUEUE *queue = (MESSAGEQUEUE*)GlobalLock16( hQueue );
429
430   if( queue )
431   {
432     MESSAGEQUEUE *senderQ = (MESSAGEQUEUE*)GlobalLock16( queue->hSendingTask);
433     QSMCTRL*      CtrlPtr = queue->smResultCurrent;
434
435     TRACE(msg,"Flushing queue %04x:\n", hQueue );
436
437     while( senderQ )
438     {
439       if( !CtrlPtr )
440            CtrlPtr = senderQ->smResultInit;
441
442       TRACE(msg,"\tfrom queue %04x, smResult %08x\n", queue->hSendingTask, (unsigned)CtrlPtr );
443
444       if( !(queue->hSendingTask = senderQ->hPrevSendingTask) )
445         QUEUE_ClearWakeBit( queue, QS_SENDMESSAGE );
446
447       QUEUE_SetWakeBit( senderQ, QS_SMPARAMSFREE );
448       
449       queue->smResultCurrent = CtrlPtr;
450       while( senderQ->wakeBits & QS_SMRESULT ) OldYield();
451
452       senderQ->SendMessageReturn = 0;
453       senderQ->smResult = queue->smResultCurrent;
454       QUEUE_SetWakeBit( senderQ, QS_SMRESULT);
455
456       senderQ = (MESSAGEQUEUE*)GlobalLock16( queue->hSendingTask);
457       CtrlPtr = NULL;
458     }
459     queue->InSendMessageHandle = 0;
460   }  
461 }
462
463 /***********************************************************************
464  *           QUEUE_AddMsg
465  *
466  * Add a message to the queue. Return FALSE if queue is full.
467  */
468 BOOL32 QUEUE_AddMsg( HQUEUE16 hQueue, MSG16 * msg, DWORD extraInfo )
469 {
470     int pos;
471     MESSAGEQUEUE *msgQueue;
472
473     SIGNAL_MaskAsyncEvents( TRUE );
474
475     if (!(msgQueue = (MESSAGEQUEUE *)GlobalLock16( hQueue ))) return FALSE;
476     pos = msgQueue->nextFreeMessage;
477
478       /* Check if queue is full */
479     if ((pos == msgQueue->nextMessage) && (msgQueue->msgCount > 0))
480     {
481         SIGNAL_MaskAsyncEvents( FALSE );
482         WARN( msg,"Queue is full!\n" );
483         return FALSE;
484     }
485
486       /* Store message */
487     msgQueue->messages[pos].msg = *msg;
488     msgQueue->messages[pos].extraInfo = extraInfo;
489     if (pos < msgQueue->queueSize-1) pos++;
490     else pos = 0;
491     msgQueue->nextFreeMessage = pos;
492     msgQueue->msgCount++;
493
494     SIGNAL_MaskAsyncEvents( FALSE );
495
496     QUEUE_SetWakeBit( msgQueue, QS_POSTMESSAGE );
497     return TRUE;
498 }
499
500
501 /***********************************************************************
502  *           QUEUE_FindMsg
503  *
504  * Find a message matching the given parameters. Return -1 if none available.
505  */
506 int QUEUE_FindMsg( MESSAGEQUEUE * msgQueue, HWND32 hwnd, int first, int last )
507 {
508     int i, pos = msgQueue->nextMessage;
509
510     TRACE(msg,"hwnd=%04x pos=%d\n", hwnd, pos );
511
512     if (!msgQueue->msgCount) return -1;
513     if (!hwnd && !first && !last) return pos;
514         
515     for (i = 0; i < msgQueue->msgCount; i++)
516     {
517         MSG16 * msg = &msgQueue->messages[pos].msg;
518
519         if (!hwnd || (msg->hwnd == hwnd))
520         {
521             if (!first && !last) return pos;
522             if ((msg->message >= first) && (msg->message <= last)) return pos;
523         }
524         if (pos < msgQueue->queueSize-1) pos++;
525         else pos = 0;
526     }
527     return -1;
528 }
529
530
531 /***********************************************************************
532  *           QUEUE_RemoveMsg
533  *
534  * Remove a message from the queue (pos must be a valid position).
535  */
536 void QUEUE_RemoveMsg( MESSAGEQUEUE * msgQueue, int pos )
537 {
538     SIGNAL_MaskAsyncEvents( TRUE );
539
540     if (pos >= msgQueue->nextMessage)
541     {
542         for ( ; pos > msgQueue->nextMessage; pos--)
543             msgQueue->messages[pos] = msgQueue->messages[pos-1];
544         msgQueue->nextMessage++;
545         if (msgQueue->nextMessage >= msgQueue->queueSize)
546             msgQueue->nextMessage = 0;
547     }
548     else
549     {
550         for ( ; pos < msgQueue->nextFreeMessage; pos++)
551             msgQueue->messages[pos] = msgQueue->messages[pos+1];
552         if (msgQueue->nextFreeMessage) msgQueue->nextFreeMessage--;
553         else msgQueue->nextFreeMessage = msgQueue->queueSize-1;
554     }
555     msgQueue->msgCount--;
556     if (!msgQueue->msgCount) msgQueue->wakeBits &= ~QS_POSTMESSAGE;
557
558     SIGNAL_MaskAsyncEvents( FALSE );
559 }
560
561
562 /***********************************************************************
563  *           QUEUE_WakeSomeone
564  *
565  * Wake a queue upon reception of a hardware event.
566  */
567 static void QUEUE_WakeSomeone( UINT32 message )
568 {
569     WND*          wndPtr = NULL;
570     WORD          wakeBit;
571     HWND32 hwnd;
572     MESSAGEQUEUE *queue = pCursorQueue;
573
574     if( (message >= WM_KEYFIRST) && (message <= WM_KEYLAST) )
575     {
576        wakeBit = QS_KEY;
577        if( pActiveQueue ) queue = pActiveQueue;
578     }
579     else 
580     {
581        wakeBit = (message == WM_MOUSEMOVE) ? QS_MOUSEMOVE : QS_MOUSEBUTTON;
582        if( (hwnd = GetCapture32()) )
583          if( (wndPtr = WIN_FindWndPtr( hwnd )) ) 
584            queue = (MESSAGEQUEUE *)GlobalLock16( wndPtr->hmemTaskQ );
585     }
586
587     if( (hwnd = GetSysModalWindow16()) )
588       if( (wndPtr = WIN_FindWndPtr( hwnd )) )
589         queue = (MESSAGEQUEUE *)GlobalLock16( wndPtr->hmemTaskQ );
590
591     if( !queue ) 
592     {
593       queue = GlobalLock16( hFirstQueue );
594       while( queue )
595       {
596         if (queue->wakeMask & wakeBit) break;
597         queue = GlobalLock16( queue->next );
598       }
599       if( !queue )
600       { 
601         WARN(msg, "couldn't find queue\n"); 
602         return; 
603       }
604     }
605
606     QUEUE_SetWakeBit( queue, wakeBit );
607 }
608
609
610 /***********************************************************************
611  *           hardware_event
612  *
613  * Add an event to the system message queue.
614  * Note: the position is relative to the desktop window.
615  */
616 void hardware_event( WORD message, WORD wParam, LONG lParam,
617                      int xPos, int yPos, DWORD time, DWORD extraInfo )
618 {
619     MSG16 *msg;
620     int pos;
621
622     if (!sysMsgQueue) return;
623     pos = sysMsgQueue->nextFreeMessage;
624
625       /* Merge with previous event if possible */
626
627     if ((message == WM_MOUSEMOVE) && sysMsgQueue->msgCount)
628     {
629         if (pos > 0) pos--;
630         else pos = sysMsgQueue->queueSize - 1;
631         msg = &sysMsgQueue->messages[pos].msg;
632         if ((msg->message == message) && (msg->wParam == wParam))
633             sysMsgQueue->msgCount--;  /* Merge events */
634         else
635             pos = sysMsgQueue->nextFreeMessage;  /* Don't merge */
636     }
637
638       /* Check if queue is full */
639
640     if ((pos == sysMsgQueue->nextMessage) && sysMsgQueue->msgCount)
641     {
642         /* Queue is full, beep (but not on every mouse motion...) */
643         if (message != WM_MOUSEMOVE) MessageBeep32(0);
644         return;
645     }
646
647       /* Store message */
648
649     msg = &sysMsgQueue->messages[pos].msg;
650     msg->hwnd    = 0;
651     msg->message = message;
652     msg->wParam  = wParam;
653     msg->lParam  = lParam;
654     msg->time    = time;
655     msg->pt.x    = xPos & 0xffff;
656     msg->pt.y    = yPos & 0xffff;
657     sysMsgQueue->messages[pos].extraInfo = extraInfo;
658     if (pos < sysMsgQueue->queueSize - 1) pos++;
659     else pos = 0;
660     sysMsgQueue->nextFreeMessage = pos;
661     sysMsgQueue->msgCount++;
662     QUEUE_WakeSomeone( message );
663 }
664
665                     
666 /***********************************************************************
667  *           QUEUE_GetQueueTask
668  */
669 HTASK16 QUEUE_GetQueueTask( HQUEUE16 hQueue )
670 {
671     MESSAGEQUEUE *queue = GlobalLock16( hQueue );
672     return (queue) ? queue->hTask : 0 ;
673 }
674
675
676 /***********************************************************************
677  *           QUEUE_IncPaintCount
678  */
679 void QUEUE_IncPaintCount( HQUEUE16 hQueue )
680 {
681     MESSAGEQUEUE *queue;
682
683     if (!(queue = (MESSAGEQUEUE *)GlobalLock16( hQueue ))) return;
684     queue->wPaintCount++;
685     QUEUE_SetWakeBit( queue, QS_PAINT );
686 }
687
688
689 /***********************************************************************
690  *           QUEUE_DecPaintCount
691  */
692 void QUEUE_DecPaintCount( HQUEUE16 hQueue )
693 {
694     MESSAGEQUEUE *queue;
695
696     if (!(queue = (MESSAGEQUEUE *)GlobalLock16( hQueue ))) return;
697     queue->wPaintCount--;
698     if (!queue->wPaintCount) queue->wakeBits &= ~QS_PAINT;
699 }
700
701
702 /***********************************************************************
703  *           QUEUE_IncTimerCount
704  */
705 void QUEUE_IncTimerCount( HQUEUE16 hQueue )
706 {
707     MESSAGEQUEUE *queue;
708
709     if (!(queue = (MESSAGEQUEUE *)GlobalLock16( hQueue ))) return;
710     queue->wTimerCount++;
711     QUEUE_SetWakeBit( queue, QS_TIMER );
712 }
713
714
715 /***********************************************************************
716  *           QUEUE_DecTimerCount
717  */
718 void QUEUE_DecTimerCount( HQUEUE16 hQueue )
719 {
720     MESSAGEQUEUE *queue;
721
722     if (!(queue = (MESSAGEQUEUE *)GlobalLock16( hQueue ))) return;
723     queue->wTimerCount--;
724     if (!queue->wTimerCount) queue->wakeBits &= ~QS_TIMER;
725 }
726
727
728 /***********************************************************************
729  *           PostQuitMessage16   (USER.6)
730  */
731 void WINAPI PostQuitMessage16( INT16 exitCode )
732 {
733     PostQuitMessage32( exitCode );
734 }
735
736
737 /***********************************************************************
738  *           PostQuitMessage32   (USER32.421)
739  *
740  * PostQuitMessage() posts a message to the system requesting an
741  * application to terminate execution. As a result of this function,
742  * the WM_QUIT message is posted to the application, and
743  * PostQuitMessage() returns immediately.  The exitCode parameter
744  * specifies an application-defined exit code, which appears in the
745  * _wParam_ parameter of the WM_QUIT message posted to the application.  
746  *
747  * CONFORMANCE
748  *
749  *  ECMA-234, Win32
750  */
751 void WINAPI PostQuitMessage32( INT32 exitCode )
752 {
753     MESSAGEQUEUE *queue;
754
755     if (!(queue = (MESSAGEQUEUE *)GlobalLock16( GetFastQueue() ))) return;
756     queue->wPostQMsg = TRUE;
757     queue->wExitCode = (WORD)exitCode;
758 }
759
760
761 /***********************************************************************
762  *           GetWindowTask16   (USER.224)
763  */
764 HTASK16 WINAPI GetWindowTask16( HWND16 hwnd )
765 {
766     WND *wndPtr = WIN_FindWndPtr( hwnd );
767
768     if (!wndPtr) return 0;
769     return QUEUE_GetQueueTask( wndPtr->hmemTaskQ );
770 }
771
772 /***********************************************************************
773  *           GetWindowThreadProcessId   (USER32.313)
774  */
775 DWORD WINAPI GetWindowThreadProcessId( HWND32 hwnd, LPDWORD process )
776 {
777     HTASK16 htask;
778     TDB *tdb;
779
780     WND *wndPtr = WIN_FindWndPtr( hwnd );
781
782     if (!wndPtr) return 0;
783     htask=QUEUE_GetQueueTask( wndPtr->hmemTaskQ );
784     tdb = (TDB*)GlobalLock16(htask);
785     if (!tdb || !tdb->thdb) return 0;
786     if (process) *process = PDB_TO_PROCESS_ID( tdb->thdb->process );
787     return THDB_TO_THREAD_ID( tdb->thdb );
788 }
789
790
791 /***********************************************************************
792  *           SetMessageQueue16   (USER.266)
793  */
794 BOOL16 WINAPI SetMessageQueue16( INT16 size )
795 {
796     return SetMessageQueue32( size );
797 }
798
799
800 /***********************************************************************
801  *           SetMessageQueue32   (USER32.494)
802  */
803 BOOL32 WINAPI SetMessageQueue32( INT32 size )
804 {
805     HQUEUE16 hQueue, hNewQueue;
806     MESSAGEQUEUE *queuePtr;
807
808     TRACE(msg,"task %04x size %i\n", GetCurrentTask(), size); 
809
810     if ((size > MAX_QUEUE_SIZE) || (size <= 0)) return FALSE;
811
812     if( !(hNewQueue = QUEUE_CreateMsgQueue( size ))) 
813     {
814         WARN(msg, "failed!\n");
815         return FALSE;
816     }
817     queuePtr = (MESSAGEQUEUE *)GlobalLock16( hNewQueue );
818
819     SIGNAL_MaskAsyncEvents( TRUE );
820
821     /* Copy data and free the old message queue */
822     if ((hQueue = GetThreadQueue(0)) != 0) 
823     {
824        MESSAGEQUEUE *oldQ = (MESSAGEQUEUE *)GlobalLock16( hQueue );
825        memcpy( &queuePtr->wParamHigh, &oldQ->wParamHigh,
826                         (int)oldQ->messages - (int)(&oldQ->wParamHigh) );
827        HOOK_ResetQueueHooks( hNewQueue );
828        if( WIN_GetDesktop()->hmemTaskQ == hQueue )
829            WIN_GetDesktop()->hmemTaskQ = hNewQueue;
830        WIN_ResetQueueWindows( WIN_GetDesktop(), hQueue, hNewQueue );
831        CLIPBOARD_ResetLock( hQueue, hNewQueue );
832        QUEUE_DeleteMsgQueue( hQueue );
833     }
834
835     /* Link new queue into list */
836     queuePtr->hTask = GetCurrentTask();
837     queuePtr->next  = hFirstQueue;
838     hFirstQueue = hNewQueue;
839     
840     if( !queuePtr->next ) pCursorQueue = queuePtr;
841     SetThreadQueue( 0, hNewQueue );
842     
843     SIGNAL_MaskAsyncEvents( FALSE );
844     return TRUE;
845 }
846
847 /***********************************************************************
848  *           InitThreadInput   (USER.409)
849  */
850 HQUEUE16 WINAPI InitThreadInput( WORD unknown, WORD flags )
851 {
852     HQUEUE16 hQueue = GetTaskQueue( 0 );
853
854     FIXME( msg, "(%04X,%04X): should create thread-local message queue!\n",
855                 unknown, flags );
856
857     SetFastQueue( 0, hQueue );    
858     return hQueue;
859 }
860
861 /***********************************************************************
862  *           GetQueueStatus16   (USER.334)
863  */
864 DWORD WINAPI GetQueueStatus16( UINT16 flags )
865 {
866     MESSAGEQUEUE *queue;
867     DWORD ret;
868
869     if (!(queue = (MESSAGEQUEUE *)GlobalLock16( GetFastQueue() ))) return 0;
870     ret = MAKELONG( queue->changeBits, queue->wakeBits );
871     queue->changeBits = 0;
872     return ret & MAKELONG( flags, flags );
873 }
874
875 /***********************************************************************
876  *           GetQueueStatus32   (USER32.283)
877  */
878 DWORD WINAPI GetQueueStatus32( UINT32 flags )
879 {
880     MESSAGEQUEUE *queue;
881     DWORD ret;
882
883     if (!(queue = (MESSAGEQUEUE *)GlobalLock16( GetFastQueue() ))) return 0;
884     ret = MAKELONG( queue->changeBits, queue->wakeBits );
885     queue->changeBits = 0;
886     return ret & MAKELONG( flags, flags );
887 }
888
889
890 /***********************************************************************
891  *           GetInputState16   (USER.335)
892  */
893 BOOL16 WINAPI GetInputState16(void)
894 {
895     return GetInputState32();
896 }
897
898 /***********************************************************************
899  *           WaitForInputIdle   (USER32.577)
900  */
901 DWORD WINAPI WaitForInputIdle (HANDLE32 hProcess, DWORD dwTimeOut)
902 {
903   FIXME (msg, "(hProcess=%d, dwTimeOut=%ld): stub\n", hProcess, dwTimeOut);
904
905   return WAIT_TIMEOUT;
906 }
907
908
909 /***********************************************************************
910  *           GetInputState32   (USER32.244)
911  */
912 BOOL32 WINAPI GetInputState32(void)
913 {
914     MESSAGEQUEUE *queue;
915
916     if (!(queue = (MESSAGEQUEUE *)GlobalLock16( GetFastQueue() )))
917         return FALSE;
918     return queue->wakeBits & (QS_KEY | QS_MOUSEBUTTON);
919 }
920
921 /***********************************************************************
922  *           UserYield  (USER.332)
923  */
924 void WINAPI UserYield(void)
925 {
926     TDB *pCurTask = (TDB *)GlobalLock16( GetCurrentTask() );
927     MESSAGEQUEUE *queue = (MESSAGEQUEUE *)GlobalLock16( pCurTask->hQueue );
928
929     if ( !THREAD_IsWin16( THREAD_Current() ) )
930     {
931         FIXME(task, "called for Win32 thread (%04x)!\n", THREAD_Current()->teb_sel);
932         return;
933     }
934
935     /* Handle sent messages */
936     while (queue && (queue->wakeBits & QS_SENDMESSAGE))
937         QUEUE_ReceiveMessage( queue );
938
939     OldYield();
940
941     queue = (MESSAGEQUEUE *)GlobalLock16( pCurTask->hQueue );
942     while (queue && (queue->wakeBits & QS_SENDMESSAGE))
943         QUEUE_ReceiveMessage( queue );
944 }
945
946 /***********************************************************************
947  *           GetMessagePos   (USER.119) (USER32.272)
948  * 
949  * The GetMessagePos() function returns a long value representing a
950  * cursor position, in screen coordinates, when the last message
951  * retrieved by the GetMessage() function occurs. The x-coordinate is
952  * in the low-order word of the return value, the y-coordinate is in
953  * the high-order word. The application can use the MAKEPOINT()
954  * macro to obtain a POINT structure from the return value. 
955  *
956  * For the current cursor position, use GetCursorPos().
957  *
958  * RETURNS
959  *
960  * Cursor position of last message on success, zero on failure.
961  *
962  * CONFORMANCE
963  *
964  * ECMA-234, Win32
965  *
966  */
967 DWORD WINAPI GetMessagePos(void)
968 {
969     MESSAGEQUEUE *queue;
970
971     if (!(queue = (MESSAGEQUEUE *)GlobalLock16( GetFastQueue() ))) return 0;
972     return queue->GetMessagePosVal;
973 }
974
975
976 /***********************************************************************
977  *           GetMessageTime   (USER.120) (USER32.273)
978  *
979  * GetMessageTime() returns the message time for the last message
980  * retrieved by the function. The time is measured in milliseconds with
981  * the same offset as GetTickCount().
982  *
983  * Since the tick count wraps, this is only useful for moderately short
984  * relative time comparisons.
985  *
986  * RETURNS
987  *
988  * Time of last message on success, zero on failure.
989  *
990  * CONFORMANCE
991  *
992  * ECMA-234, Win32
993  *  
994  */
995 LONG WINAPI GetMessageTime(void)
996 {
997     MESSAGEQUEUE *queue;
998
999     if (!(queue = (MESSAGEQUEUE *)GlobalLock16( GetFastQueue() ))) return 0;
1000     return queue->GetMessageTimeVal;
1001 }
1002
1003
1004 /***********************************************************************
1005  *           GetMessageExtraInfo   (USER.288) (USER32.271)
1006  */
1007 LONG WINAPI GetMessageExtraInfo(void)
1008 {
1009     MESSAGEQUEUE *queue;
1010
1011     if (!(queue = (MESSAGEQUEUE *)GlobalLock16( GetFastQueue() ))) return 0;
1012     return queue->GetMessageExtraInfoVal;
1013 }