Release 970305
[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 "module.h"
10 #include "queue.h"
11 #include "task.h"
12 #include "win.h"
13 #include "hook.h"
14 #include "thread.h"
15 #include "process.h"
16 #include "stddebug.h"
17 #include "debug.h"
18
19 #define MAX_QUEUE_SIZE   120  /* Max. size of a message queue */
20
21 static HQUEUE16 hFirstQueue = 0;
22 static HQUEUE16 hDoomedQueue = 0;
23 static HQUEUE16 hmemSysMsgQueue = 0;
24 static MESSAGEQUEUE *sysMsgQueue = NULL;
25
26 static MESSAGEQUEUE *pMouseQueue = NULL;  /* Queue for last mouse message */
27 static MESSAGEQUEUE *pKbdQueue = NULL;    /* Queue for last kbd message */
28
29 extern void SIGNAL_MaskAsyncEvents(BOOL32);
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         fprintf( stderr, "%04x is not a queue handle\n", hQueue );
45         return;
46     }
47
48     fprintf( stderr,
49              "next: %12.4x  Intertask SendMessage:\n"
50              "hTask: %11.4x  ----------------------\n"
51              "msgSize: %9.4x  hWnd: %10.4x\n"
52              "msgCount: %8.4x  msg: %11.4x\n"
53              "msgNext: %9.4x  wParam: %8.4x\n"
54              "msgFree: %9.4x  lParam: %8.8x\n"
55              "qSize: %11.4x  lRet: %10.8x\n"
56              "wWinVer: %9.4x  ISMH: %10.4x\n"
57              "paints: %10.4x  hSendTask: %5.4x\n"
58              "timers: %10.4x  hPrevSend: %5.4x\n"
59              "wakeBits: %8.4x\n"
60              "wakeMask: %8.4x\n"
61              "hCurHook: %8.4x\n",
62              pq->next, pq->hTask, pq->msgSize, pq->hWnd, 
63              pq->msgCount, pq->msg, pq->nextMessage, pq->wParam,
64              pq->nextFreeMessage, (unsigned)pq->lParam, pq->queueSize,
65              (unsigned)pq->SendMessageReturn, pq->wWinVersion, pq->InSendMessageHandle,
66              pq->wPaintCount, pq->hSendingTask, pq->wTimerCount,
67              pq->hPrevSendingTask, pq->wakeBits, pq->wakeMask, pq->hCurHook);
68 }
69
70
71 /***********************************************************************
72  *           QUEUE_WalkQueues
73  */
74 void QUEUE_WalkQueues(void)
75 {
76     HQUEUE16 hQueue = hFirstQueue;
77
78     fprintf( stderr, "Queue Size Msgs Task\n" );
79     while (hQueue)
80     {
81         MESSAGEQUEUE *queue = (MESSAGEQUEUE *)GlobalLock16( hQueue );
82         if (!queue)
83         {
84             fprintf( stderr, "*** Bad queue handle %04x\n", hQueue );
85             return;
86         }
87         fprintf( stderr, "%04x %5d %4d %04x %s\n",
88                  hQueue, queue->msgSize, queue->msgCount, queue->hTask,
89                  MODULE_GetModuleName( GetExePtr(queue->hTask) ) );
90         hQueue = queue->next;
91     }
92     fprintf( stderr, "\n" );
93 }
94
95
96 /***********************************************************************
97  *           QUEUE_IsDoomedQueue
98  */
99 BOOL32 QUEUE_IsDoomedQueue( HQUEUE16 hQueue )
100 {
101     return (hDoomedQueue && (hQueue == hDoomedQueue));
102 }
103
104
105 /***********************************************************************
106  *           QUEUE_SetDoomedQueue
107  */
108 void QUEUE_SetDoomedQueue( HQUEUE16 hQueue )
109 {
110     hDoomedQueue = hQueue;
111 }
112
113
114 /***********************************************************************
115  *           QUEUE_CreateMsgQueue
116  *
117  * Creates a message queue. Doesn't link it into queue list!
118  */
119 static HQUEUE16 QUEUE_CreateMsgQueue( int size )
120 {
121     HQUEUE16 hQueue;
122     MESSAGEQUEUE * msgQueue;
123     int queueSize;
124     TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
125
126     dprintf_msg(stddeb,"Creating message queue...\n");
127
128     queueSize = sizeof(MESSAGEQUEUE) + size * sizeof(QMSG);
129     if (!(hQueue = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, queueSize )))
130         return 0;
131     msgQueue = (MESSAGEQUEUE *) GlobalLock16( hQueue );
132     msgQueue->self        = hQueue;
133     msgQueue->msgSize     = sizeof(QMSG);
134     msgQueue->queueSize   = size;
135     msgQueue->wakeBits    = msgQueue->changeBits = QS_SMPARAMSFREE;
136     msgQueue->wWinVersion = pTask ? pTask->version : 0;
137     GlobalUnlock16( hQueue );
138     return hQueue;
139 }
140
141
142 /***********************************************************************
143  *           QUEUE_DeleteMsgQueue
144  *
145  * Unlinks and deletes a message queue.
146  *
147  * Note: We need to mask asynchronous events to make sure PostMessage works
148  * even in the signal handler.
149  */
150 BOOL32 QUEUE_DeleteMsgQueue( HQUEUE16 hQueue )
151 {
152     MESSAGEQUEUE * msgQueue = (MESSAGEQUEUE*)GlobalLock16(hQueue);
153     HQUEUE16  senderQ;
154     HQUEUE16 *pPrev;
155
156     dprintf_msg(stddeb,"Deleting message queue %04x\n", hQueue);
157
158     if (!hQueue || !msgQueue)
159     {
160         dprintf_msg(stddeb,"DeleteMsgQueue: invalid argument.\n");
161         return 0;
162     }
163     if( pCursorQueue == msgQueue ) pCursorQueue = NULL;
164     if( pActiveQueue == msgQueue ) pActiveQueue = NULL;
165
166     /* flush sent messages */
167     senderQ = msgQueue->hSendingTask;
168     while( senderQ )
169     {
170       MESSAGEQUEUE* sq = (MESSAGEQUEUE*)GlobalLock16(senderQ);
171       if( !sq ) break;
172       sq->SendMessageReturn = 0L;
173       QUEUE_SetWakeBit( sq, QS_SMRESULT );
174       senderQ = sq->hPrevSendingTask;
175     }
176
177     SIGNAL_MaskAsyncEvents( TRUE );
178
179     pPrev = &hFirstQueue;
180     while (*pPrev && (*pPrev != hQueue))
181     {
182         MESSAGEQUEUE *msgQ = (MESSAGEQUEUE*)GlobalLock16(*pPrev);
183         pPrev = &msgQ->next;
184     }
185     if (*pPrev) *pPrev = msgQueue->next;
186     msgQueue->self = 0;
187
188     SIGNAL_MaskAsyncEvents( FALSE );
189
190     GlobalFree16( hQueue );
191     return 1;
192 }
193
194
195 /***********************************************************************
196  *           QUEUE_CreateSysMsgQueue
197  *
198  * Create the system message queue, and set the double-click speed.
199  * Must be called only once.
200  */
201 BOOL32 QUEUE_CreateSysMsgQueue( int size )
202 {
203     if (size > MAX_QUEUE_SIZE) size = MAX_QUEUE_SIZE;
204     else if (size <= 0) size = 1;
205     if (!(hmemSysMsgQueue = QUEUE_CreateMsgQueue( size ))) return FALSE;
206     sysMsgQueue = (MESSAGEQUEUE *) GlobalLock16( hmemSysMsgQueue );
207     return TRUE;
208 }
209
210
211 /***********************************************************************
212  *           QUEUE_GetSysQueue
213  */
214 MESSAGEQUEUE *QUEUE_GetSysQueue(void)
215 {
216     return sysMsgQueue;
217 }
218
219
220 /***********************************************************************
221  *           QUEUE_SetWakeBit
222  *
223  * See "Windows Internals", p.449
224  */
225 void QUEUE_SetWakeBit( MESSAGEQUEUE *queue, WORD bit )
226 {
227     dprintf_msg(stddeb,"SetWakeBit: queue = %04x (wm=%04x), bit = %04x\n", 
228                         queue->self, queue->wakeMask, bit );
229
230     if (bit & QS_MOUSE) pMouseQueue = queue;
231     if (bit & QS_KEY) pKbdQueue = queue;
232     queue->changeBits |= bit;
233     queue->wakeBits   |= bit;
234     if (queue->wakeMask & bit)
235     {
236         queue->wakeMask = 0;
237         PostEvent( queue->hTask );
238     }
239 }
240
241
242 /***********************************************************************
243  *           QUEUE_ClearWakeBit
244  */
245 void QUEUE_ClearWakeBit( MESSAGEQUEUE *queue, WORD bit )
246 {
247     queue->changeBits &= ~bit;
248     queue->wakeBits   &= ~bit;
249 }
250
251
252 /***********************************************************************
253  *           QUEUE_WaitBits
254  *
255  * See "Windows Internals", p.447
256  */
257 void QUEUE_WaitBits( WORD bits )
258 {
259     MESSAGEQUEUE *queue;
260
261     dprintf_msg(stddeb,"WaitBits: q %04x waiting for %04x\n", GetTaskQueue(0), bits);
262
263     for (;;)
264     {
265         if (!(queue = (MESSAGEQUEUE *)GlobalLock16( GetTaskQueue(0) ))) return;
266
267         if (queue->changeBits & bits)
268         {
269             /* One of the bits is set; we can return */
270             queue->wakeMask = 0;
271             return;
272         }
273         if (queue->wakeBits & QS_SENDMESSAGE)
274         {
275             /* Process the sent message immediately */
276
277             queue->wakeMask = 0;
278             QUEUE_ReceiveMessage( queue );
279             continue;                           /* nested sm crux */
280         }
281
282         queue->wakeMask = bits | QS_SENDMESSAGE;
283         if(queue->changeBits & bits) continue;
284         
285         dprintf_msg(stddeb,"wb: (%04x) wakeMask is %04x, waiting\n", queue->self, queue->wakeMask);
286
287         WaitEvent( 0 );
288     }
289 }
290
291
292 /***********************************************************************
293  *           QUEUE_ReceiveMessage
294  *
295  * This routine is called when a sent message is waiting for the queue.
296  */
297 void QUEUE_ReceiveMessage( MESSAGEQUEUE *queue )
298 {
299     MESSAGEQUEUE *senderQ = NULL;
300     HQUEUE16      prevSender = 0;
301     QSMCTRL*      prevCtrlPtr = NULL;
302     LRESULT       result = 0;
303
304     dprintf_msg(stddeb, "ReceiveMessage, queue %04x\n", queue->self );
305     if (!(queue->wakeBits & QS_SENDMESSAGE) ||
306         !(senderQ = (MESSAGEQUEUE*)GlobalLock16( queue->hSendingTask))) 
307         { dprintf_msg(stddeb,"\trcm: nothing to do\n"); return; }
308
309     if( !senderQ->hPrevSendingTask )
310     {
311       queue->wakeBits &= ~QS_SENDMESSAGE;       /* no more sent messages */
312       queue->changeBits &= ~QS_SENDMESSAGE;
313     }
314
315     /* Save current state on stack */
316     prevSender                 = queue->InSendMessageHandle;
317     prevCtrlPtr                = queue->smResultCurrent;
318
319     /* Remove sending queue from the list */
320     queue->InSendMessageHandle = queue->hSendingTask;
321     queue->smResultCurrent     = senderQ->smResultInit;
322     queue->hSendingTask        = senderQ->hPrevSendingTask;
323
324     dprintf_msg(stddeb, "\trcm: smResultCurrent = %08x, prevCtrl = %08x\n", 
325                                 (unsigned)queue->smResultCurrent, (unsigned)prevCtrlPtr );
326     QUEUE_SetWakeBit( senderQ, QS_SMPARAMSFREE );
327
328     dprintf_msg(stddeb, "\trcm: calling wndproc - %04x %04x %04x %08x\n",
329             senderQ->hWnd, senderQ->msg, senderQ->wParam, (unsigned)senderQ->lParam );
330
331     if (IsWindow32( senderQ->hWnd ))
332     {
333         DWORD extraInfo = queue->GetMessageExtraInfoVal;
334         queue->GetMessageExtraInfoVal = senderQ->GetMessageExtraInfoVal;
335
336         result = CallWindowProc16( (WNDPROC16)GetWindowLong16(senderQ->hWnd, GWL_WNDPROC),
337                                    senderQ->hWnd, senderQ->msg, senderQ->wParam, senderQ->lParam );
338
339         queue->GetMessageExtraInfoVal = extraInfo;  /* Restore extra info */
340         dprintf_msg(stddeb,"\trcm: result =  %08x\n", (unsigned)result );
341     }
342     else dprintf_msg(stddeb,"\trcm: bad hWnd\n");
343
344     /* Return the result to the sender task */
345     ReplyMessage16( result );
346
347     queue->InSendMessageHandle = prevSender;
348     queue->smResultCurrent     = prevCtrlPtr;
349
350     dprintf_msg(stddeb,"ReceiveMessage: done!\n");
351 }
352
353 /***********************************************************************
354  *           QUEUE_FlushMessage
355  * 
356  * Try to reply to all pending sent messages on exit.
357  */
358 void QUEUE_FlushMessages( HQUEUE16 hQueue )
359 {
360   MESSAGEQUEUE *queue = (MESSAGEQUEUE*)GlobalLock16( hQueue );
361
362   if( queue )
363   {
364     MESSAGEQUEUE *senderQ = (MESSAGEQUEUE*)GlobalLock16( queue->hSendingTask);
365     QSMCTRL*      CtrlPtr = queue->smResultCurrent;
366
367     while( senderQ )
368     {
369       if( !(queue->hSendingTask = senderQ->hPrevSendingTask) )
370             queue->wakeBits &= ~QS_SENDMESSAGE;
371       QUEUE_SetWakeBit( senderQ, QS_SMPARAMSFREE );
372       
373       queue->smResultCurrent = CtrlPtr;
374       while( senderQ->wakeBits & QS_SMRESULT ) OldYield();
375
376       senderQ->SendMessageReturn = 0;
377       senderQ->smResult = queue->smResultCurrent;
378       QUEUE_SetWakeBit( senderQ, QS_SMRESULT);
379
380       if( (senderQ = (MESSAGEQUEUE*)GlobalLock16( queue->hSendingTask)) )
381            CtrlPtr = senderQ->smResultInit;
382     }
383     queue->InSendMessageHandle = 0;
384   }  
385 }
386
387 /***********************************************************************
388  *           QUEUE_AddMsg
389  *
390  * Add a message to the queue. Return FALSE if queue is full.
391  */
392 BOOL32 QUEUE_AddMsg( HQUEUE16 hQueue, MSG16 * msg, DWORD extraInfo )
393 {
394     int pos;
395     MESSAGEQUEUE *msgQueue;
396
397     SIGNAL_MaskAsyncEvents( TRUE );
398
399     if (!(msgQueue = (MESSAGEQUEUE *)GlobalLock16( hQueue ))) return FALSE;
400     pos = msgQueue->nextFreeMessage;
401
402       /* Check if queue is full */
403     if ((pos == msgQueue->nextMessage) && (msgQueue->msgCount > 0))
404     {
405         SIGNAL_MaskAsyncEvents( FALSE );
406         fprintf(stderr,"MSG_AddMsg // queue is full !\n");
407         return FALSE;
408     }
409
410       /* Store message */
411     msgQueue->messages[pos].msg = *msg;
412     msgQueue->messages[pos].extraInfo = extraInfo;
413     if (pos < msgQueue->queueSize-1) pos++;
414     else pos = 0;
415     msgQueue->nextFreeMessage = pos;
416     msgQueue->msgCount++;
417
418     SIGNAL_MaskAsyncEvents( FALSE );
419
420     QUEUE_SetWakeBit( msgQueue, QS_POSTMESSAGE );
421     return TRUE;
422 }
423
424
425 /***********************************************************************
426  *           QUEUE_FindMsg
427  *
428  * Find a message matching the given parameters. Return -1 if none available.
429  */
430 int QUEUE_FindMsg( MESSAGEQUEUE * msgQueue, HWND32 hwnd, int first, int last )
431 {
432     int i, pos = msgQueue->nextMessage;
433
434     dprintf_msg(stddeb,"QUEUE_FindMsg: hwnd=%04x pos=%d\n", hwnd, pos );
435
436     if (!msgQueue->msgCount) return -1;
437     if (!hwnd && !first && !last) return pos;
438         
439     for (i = 0; i < msgQueue->msgCount; i++)
440     {
441         MSG16 * msg = &msgQueue->messages[pos].msg;
442
443         if (!hwnd || (msg->hwnd == hwnd))
444         {
445             if (!first && !last) return pos;
446             if ((msg->message >= first) && (msg->message <= last)) return pos;
447         }
448         if (pos < msgQueue->queueSize-1) pos++;
449         else pos = 0;
450     }
451     return -1;
452 }
453
454
455 /***********************************************************************
456  *           QUEUE_RemoveMsg
457  *
458  * Remove a message from the queue (pos must be a valid position).
459  */
460 void QUEUE_RemoveMsg( MESSAGEQUEUE * msgQueue, int pos )
461 {
462     SIGNAL_MaskAsyncEvents( TRUE );
463
464     if (pos >= msgQueue->nextMessage)
465     {
466         for ( ; pos > msgQueue->nextMessage; pos--)
467             msgQueue->messages[pos] = msgQueue->messages[pos-1];
468         msgQueue->nextMessage++;
469         if (msgQueue->nextMessage >= msgQueue->queueSize)
470             msgQueue->nextMessage = 0;
471     }
472     else
473     {
474         for ( ; pos < msgQueue->nextFreeMessage; pos++)
475             msgQueue->messages[pos] = msgQueue->messages[pos+1];
476         if (msgQueue->nextFreeMessage) msgQueue->nextFreeMessage--;
477         else msgQueue->nextFreeMessage = msgQueue->queueSize-1;
478     }
479     msgQueue->msgCount--;
480     if (!msgQueue->msgCount) msgQueue->wakeBits &= ~QS_POSTMESSAGE;
481
482     SIGNAL_MaskAsyncEvents( FALSE );
483 }
484
485
486 /***********************************************************************
487  *           QUEUE_WakeSomeone
488  *
489  * Wake a queue upon reception of a hardware event.
490  */
491 static void QUEUE_WakeSomeone( UINT32 message )
492 {
493     WND*          wndPtr = NULL;
494     WORD          wakeBit;
495     HWND32 hwnd;
496     MESSAGEQUEUE *queue = pCursorQueue;
497
498     if( (message >= WM_KEYFIRST) && (message <= WM_KEYLAST) )
499     {
500        wakeBit = QS_KEY;
501        if( pActiveQueue ) queue = pActiveQueue;
502     }
503     else 
504     {
505        wakeBit = (message == WM_MOUSEMOVE) ? QS_MOUSEMOVE : QS_MOUSEBUTTON;
506        if( (hwnd = GetCapture32()) )
507          if( (wndPtr = WIN_FindWndPtr( hwnd )) ) 
508            queue = (MESSAGEQUEUE *)GlobalLock16( wndPtr->hmemTaskQ );
509     }
510
511     if( (hwnd = GetSysModalWindow16()) )
512       if( (wndPtr = WIN_FindWndPtr( hwnd )) )
513         queue = (MESSAGEQUEUE *)GlobalLock16( wndPtr->hmemTaskQ );
514
515     if( !queue ) 
516     {
517       queue = GlobalLock16( hFirstQueue );
518       while( queue )
519       {
520         if (queue->wakeMask & wakeBit) break;
521         queue = GlobalLock16( queue->next );
522       }
523       if( !queue )
524       { 
525         dprintf_msg(stddeb,"WakeSomeone: couldn't find queue\n"); 
526         return; 
527       }
528     }
529
530     QUEUE_SetWakeBit( queue, wakeBit );
531 }
532
533
534 /***********************************************************************
535  *           hardware_event
536  *
537  * Add an event to the system message queue.
538  * Note: the position is relative to the desktop window.
539  */
540 void hardware_event( WORD message, WORD wParam, LONG lParam,
541                      int xPos, int yPos, DWORD time, DWORD extraInfo )
542 {
543     MSG16 *msg;
544     int pos;
545
546     if (!sysMsgQueue) return;
547     pos = sysMsgQueue->nextFreeMessage;
548
549       /* Merge with previous event if possible */
550
551     if ((message == WM_MOUSEMOVE) && sysMsgQueue->msgCount)
552     {
553         if (pos > 0) pos--;
554         else pos = sysMsgQueue->queueSize - 1;
555         msg = &sysMsgQueue->messages[pos].msg;
556         if ((msg->message == message) && (msg->wParam == wParam))
557             sysMsgQueue->msgCount--;  /* Merge events */
558         else
559             pos = sysMsgQueue->nextFreeMessage;  /* Don't merge */
560     }
561
562       /* Check if queue is full */
563
564     if ((pos == sysMsgQueue->nextMessage) && sysMsgQueue->msgCount)
565     {
566         /* Queue is full, beep (but not on every mouse motion...) */
567         if (message != WM_MOUSEMOVE) MessageBeep32(0);
568         return;
569     }
570
571       /* Store message */
572
573     msg = &sysMsgQueue->messages[pos].msg;
574     msg->hwnd    = 0;
575     msg->message = message;
576     msg->wParam  = wParam;
577     msg->lParam  = lParam;
578     msg->time    = time;
579     msg->pt.x    = xPos & 0xffff;
580     msg->pt.y    = yPos & 0xffff;
581     sysMsgQueue->messages[pos].extraInfo = extraInfo;
582     if (pos < sysMsgQueue->queueSize - 1) pos++;
583     else pos = 0;
584     sysMsgQueue->nextFreeMessage = pos;
585     sysMsgQueue->msgCount++;
586     QUEUE_WakeSomeone( message );
587 }
588
589                     
590 /***********************************************************************
591  *           QUEUE_GetQueueTask
592  */
593 HTASK16 QUEUE_GetQueueTask( HQUEUE16 hQueue )
594 {
595     MESSAGEQUEUE *queue = GlobalLock16( hQueue );
596     return (queue) ? queue->hTask : 0 ;
597 }
598
599
600 /***********************************************************************
601  *           QUEUE_IncPaintCount
602  */
603 void QUEUE_IncPaintCount( HQUEUE16 hQueue )
604 {
605     MESSAGEQUEUE *queue;
606
607     if (!(queue = (MESSAGEQUEUE *)GlobalLock16( hQueue ))) return;
608     queue->wPaintCount++;
609     QUEUE_SetWakeBit( queue, QS_PAINT );
610 }
611
612
613 /***********************************************************************
614  *           QUEUE_DecPaintCount
615  */
616 void QUEUE_DecPaintCount( HQUEUE16 hQueue )
617 {
618     MESSAGEQUEUE *queue;
619
620     if (!(queue = (MESSAGEQUEUE *)GlobalLock16( hQueue ))) return;
621     queue->wPaintCount--;
622     if (!queue->wPaintCount) queue->wakeBits &= ~QS_PAINT;
623 }
624
625
626 /***********************************************************************
627  *           QUEUE_IncTimerCount
628  */
629 void QUEUE_IncTimerCount( HQUEUE16 hQueue )
630 {
631     MESSAGEQUEUE *queue;
632
633     if (!(queue = (MESSAGEQUEUE *)GlobalLock16( hQueue ))) return;
634     queue->wTimerCount++;
635     QUEUE_SetWakeBit( queue, QS_TIMER );
636 }
637
638
639 /***********************************************************************
640  *           QUEUE_DecTimerCount
641  */
642 void QUEUE_DecTimerCount( HQUEUE16 hQueue )
643 {
644     MESSAGEQUEUE *queue;
645
646     if (!(queue = (MESSAGEQUEUE *)GlobalLock16( hQueue ))) return;
647     queue->wTimerCount--;
648     if (!queue->wTimerCount) queue->wakeBits &= ~QS_TIMER;
649 }
650
651
652 /***********************************************************************
653  *           PostQuitMessage16   (USER.6)
654  */
655 void PostQuitMessage16( INT16 exitCode )
656 {
657     PostQuitMessage32( exitCode );
658 }
659
660
661 /***********************************************************************
662  *           PostQuitMessage32   (USER32.420)
663  */
664 void PostQuitMessage32( INT32 exitCode )
665 {
666     MESSAGEQUEUE *queue;
667
668     if (!(queue = (MESSAGEQUEUE *)GlobalLock16( GetTaskQueue(0) ))) return;
669     queue->wPostQMsg = TRUE;
670     queue->wExitCode = (WORD)exitCode;
671 }
672
673
674 /***********************************************************************
675  *           GetWindowTask16   (USER.224)
676  */
677 HTASK16 GetWindowTask16( HWND16 hwnd )
678 {
679     WND *wndPtr = WIN_FindWndPtr( hwnd );
680
681     if (!wndPtr) return 0;
682     return QUEUE_GetQueueTask( wndPtr->hmemTaskQ );
683 }
684
685 /***********************************************************************
686  *           GetWindowThreadProcessId   (USER32.312)
687  */
688 DWORD GetWindowThreadProcessId( HWND32 hwnd, LPDWORD process )
689 {
690     HTASK16 htask;
691     TDB *tdb;
692
693     WND *wndPtr = WIN_FindWndPtr( hwnd );
694
695     if (!wndPtr) return 0;
696     htask=QUEUE_GetQueueTask( wndPtr->hmemTaskQ );
697     tdb = (TDB*)GlobalLock16(htask);
698     if (!tdb) return 0;
699     if (tdb->thdb) {
700         if (process)
701                 *process = (DWORD)tdb->thdb->process;
702         return (DWORD)tdb->thdb;
703     }
704     return 0;
705
706 }
707
708
709 /***********************************************************************
710  *           SetMessageQueue16   (USER.266)
711  */
712 BOOL16 SetMessageQueue16( INT16 size )
713 {
714     return SetMessageQueue32( size );
715 }
716
717
718 /***********************************************************************
719  *           SetMessageQueue32   (USER32.493)
720  */
721 BOOL32 SetMessageQueue32( INT32 size )
722 {
723     HQUEUE16 hQueue, hNewQueue;
724     MESSAGEQUEUE *queuePtr;
725
726     dprintf_msg(stddeb,"SetMessageQueue: task %04x size %i\n", GetCurrentTask(), size); 
727
728     if ((size > MAX_QUEUE_SIZE) || (size <= 0)) return TRUE;
729
730     if( !(hNewQueue = QUEUE_CreateMsgQueue( size ))) 
731     {
732         dprintf_msg(stddeb,"SetMessageQueue: failed!\n");
733         return FALSE;
734     }
735     queuePtr = (MESSAGEQUEUE *)GlobalLock16( hNewQueue );
736
737     SIGNAL_MaskAsyncEvents( TRUE );
738
739     /* Copy data and free the old message queue */
740     if ((hQueue = GetTaskQueue(0)) != 0) 
741     {
742        MESSAGEQUEUE *oldQ = (MESSAGEQUEUE *)GlobalLock16( hQueue );
743        memcpy( &queuePtr->reserved2, &oldQ->reserved2, 
744                         (int)oldQ->messages - (int)(&oldQ->reserved2) );
745        HOOK_ResetQueueHooks( hNewQueue );
746        if( WIN_GetDesktop()->hmemTaskQ == hQueue )
747            WIN_GetDesktop()->hmemTaskQ = hNewQueue;
748        WIN_ResetQueueWindows( WIN_GetDesktop()->child, hQueue, hNewQueue );
749        QUEUE_DeleteMsgQueue( hQueue );
750     }
751
752     /* Link new queue into list */
753     queuePtr->hTask = GetCurrentTask();
754     queuePtr->next  = hFirstQueue;
755     hFirstQueue = hNewQueue;
756     
757     if( !queuePtr->next ) pCursorQueue = queuePtr;
758     SetTaskQueue( 0, hNewQueue );
759     
760     SIGNAL_MaskAsyncEvents( FALSE );
761     return TRUE;
762 }
763
764
765 /***********************************************************************
766  *           GetQueueStatus16   (USER.334)
767  */
768 DWORD GetQueueStatus16( UINT16 flags )
769 {
770     MESSAGEQUEUE *queue;
771     DWORD ret;
772
773     if (!(queue = (MESSAGEQUEUE *)GlobalLock16( GetTaskQueue(0) ))) return 0;
774     ret = MAKELONG( queue->changeBits, queue->wakeBits );
775     queue->changeBits = 0;
776     return ret & MAKELONG( flags, flags );
777 }
778
779
780 /***********************************************************************
781  *           GetInputState16   (USER.335)
782  */
783 BOOL16 GetInputState16(void)
784 {
785     return GetInputState32();
786 }
787
788
789 /***********************************************************************
790  *           GetInputState32   (USER32.243)
791  */
792 BOOL32 GetInputState32(void)
793 {
794     MESSAGEQUEUE *queue;
795
796     if (!(queue = (MESSAGEQUEUE *)GlobalLock16( GetTaskQueue(0) )))
797         return FALSE;
798     return queue->wakeBits & (QS_KEY | QS_MOUSEBUTTON);
799 }
800
801
802 /***********************************************************************
803  *           GetMessagePos   (USER.119) (USER32.271)
804  */
805 DWORD GetMessagePos(void)
806 {
807     MESSAGEQUEUE *queue;
808
809     if (!(queue = (MESSAGEQUEUE *)GlobalLock16( GetTaskQueue(0) ))) return 0;
810     return queue->GetMessagePosVal;
811 }
812
813
814 /***********************************************************************
815  *           GetMessageTime   (USER.120) (USER32.272)
816  */
817 LONG GetMessageTime(void)
818 {
819     MESSAGEQUEUE *queue;
820
821     if (!(queue = (MESSAGEQUEUE *)GlobalLock16( GetTaskQueue(0) ))) return 0;
822     return queue->GetMessageTimeVal;
823 }
824
825
826 /***********************************************************************
827  *           GetMessageExtraInfo   (USER.288) (USER32.270)
828  */
829 LONG GetMessageExtraInfo(void)
830 {
831     MESSAGEQUEUE *queue;
832
833     if (!(queue = (MESSAGEQUEUE *)GlobalLock16( GetTaskQueue(0) ))) return 0;
834     return queue->GetMessageExtraInfoVal;
835 }