Set the class hints for managed windows.
[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", GetTaskQueue(0), bits);
316
317     for (;;)
318     {
319         if (!(queue = (MESSAGEQUEUE *)GlobalLock16( GetTaskQueue(0) ))) 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     {
365       queue->wakeBits &= ~QS_SENDMESSAGE;       /* no more sent messages */
366       queue->changeBits &= ~QS_SENDMESSAGE;
367     }
368
369     /* Save current state on stack */
370     prevSender                 = queue->InSendMessageHandle;
371     prevCtrlPtr                = queue->smResultCurrent;
372
373     /* Remove sending queue from the list */
374     queue->InSendMessageHandle = queue->hSendingTask;
375     queue->smResultCurrent     = senderQ->smResultInit;
376     queue->hSendingTask        = senderQ->hPrevSendingTask;
377
378     TRACE(msg, "\trcm: smResultCurrent = %08x, prevCtrl = %08x\n", 
379                                 (unsigned)queue->smResultCurrent, (unsigned)prevCtrlPtr );
380     QUEUE_SetWakeBit( senderQ, QS_SMPARAMSFREE );
381
382     TRACE(msg, "\trcm: calling wndproc - %04x %04x %04x%04x %08x\n",
383                 senderQ->hWnd, senderQ->msg, senderQ->wParamHigh,
384                 senderQ->wParam, (unsigned)senderQ->lParam );
385
386     if (IsWindow32( senderQ->hWnd ))
387     {
388         WND *wndPtr = WIN_FindWndPtr( senderQ->hWnd );
389         DWORD extraInfo = queue->GetMessageExtraInfoVal;
390         queue->GetMessageExtraInfoVal = senderQ->GetMessageExtraInfoVal;
391
392         if (senderQ->flags & QUEUE_SM_WIN32)
393         {
394             WPARAM32 wParam = MAKELONG( senderQ->wParam, senderQ->wParamHigh );
395             TRACE(msg, "\trcm: msg is Win32\n" );
396             if (senderQ->flags & QUEUE_SM_UNICODE)
397                 result = CallWindowProc32W( wndPtr->winproc,
398                                             senderQ->hWnd, senderQ->msg,
399                                             wParam, senderQ->lParam );
400             else
401                 result = CallWindowProc32A( wndPtr->winproc,
402                                             senderQ->hWnd, senderQ->msg,
403                                             wParam, senderQ->lParam );
404         }
405         else  /* Win16 message */
406             result = CallWindowProc16( (WNDPROC16)wndPtr->winproc,
407                                        senderQ->hWnd, senderQ->msg,
408                                        senderQ->wParam, senderQ->lParam );
409
410         queue->GetMessageExtraInfoVal = extraInfo;  /* Restore extra info */
411         TRACE(msg,"\trcm: result =  %08x\n", (unsigned)result );
412     }
413     else WARN(msg, "\trcm: bad hWnd\n");
414
415     /* Return the result to the sender task */
416     ReplyMessage16( result );
417
418     queue->InSendMessageHandle = prevSender;
419     queue->smResultCurrent     = prevCtrlPtr;
420
421     TRACE(msg,"done!\n");
422 }
423
424 /***********************************************************************
425  *           QUEUE_FlushMessage
426  * 
427  * Try to reply to all pending sent messages on exit.
428  */
429 void QUEUE_FlushMessages( HQUEUE16 hQueue )
430 {
431   MESSAGEQUEUE *queue = (MESSAGEQUEUE*)GlobalLock16( hQueue );
432
433   if( queue )
434   {
435     MESSAGEQUEUE *senderQ = (MESSAGEQUEUE*)GlobalLock16( queue->hSendingTask);
436     QSMCTRL*      CtrlPtr = queue->smResultCurrent;
437
438     TRACE(msg,"Flushing queue %04x:\n", hQueue );
439
440     while( senderQ )
441     {
442       if( !CtrlPtr )
443            CtrlPtr = senderQ->smResultInit;
444
445       TRACE(msg,"\tfrom queue %04x, smResult %08x\n", queue->hSendingTask, (unsigned)CtrlPtr );
446
447       if( !(queue->hSendingTask = senderQ->hPrevSendingTask) )
448             queue->wakeBits &= ~QS_SENDMESSAGE;
449       QUEUE_SetWakeBit( senderQ, QS_SMPARAMSFREE );
450       
451       queue->smResultCurrent = CtrlPtr;
452       while( senderQ->wakeBits & QS_SMRESULT ) OldYield();
453
454       senderQ->SendMessageReturn = 0;
455       senderQ->smResult = queue->smResultCurrent;
456       QUEUE_SetWakeBit( senderQ, QS_SMRESULT);
457
458       senderQ = (MESSAGEQUEUE*)GlobalLock16( queue->hSendingTask);
459       CtrlPtr = NULL;
460     }
461     queue->InSendMessageHandle = 0;
462   }  
463 }
464
465 /***********************************************************************
466  *           QUEUE_AddMsg
467  *
468  * Add a message to the queue. Return FALSE if queue is full.
469  */
470 BOOL32 QUEUE_AddMsg( HQUEUE16 hQueue, MSG16 * msg, DWORD extraInfo )
471 {
472     int pos;
473     MESSAGEQUEUE *msgQueue;
474
475     SIGNAL_MaskAsyncEvents( TRUE );
476
477     if (!(msgQueue = (MESSAGEQUEUE *)GlobalLock16( hQueue ))) return FALSE;
478     pos = msgQueue->nextFreeMessage;
479
480       /* Check if queue is full */
481     if ((pos == msgQueue->nextMessage) && (msgQueue->msgCount > 0))
482     {
483         SIGNAL_MaskAsyncEvents( FALSE );
484         WARN( msg,"Queue is full!\n" );
485         return FALSE;
486     }
487
488       /* Store message */
489     msgQueue->messages[pos].msg = *msg;
490     msgQueue->messages[pos].extraInfo = extraInfo;
491     if (pos < msgQueue->queueSize-1) pos++;
492     else pos = 0;
493     msgQueue->nextFreeMessage = pos;
494     msgQueue->msgCount++;
495
496     SIGNAL_MaskAsyncEvents( FALSE );
497
498     QUEUE_SetWakeBit( msgQueue, QS_POSTMESSAGE );
499     return TRUE;
500 }
501
502
503 /***********************************************************************
504  *           QUEUE_FindMsg
505  *
506  * Find a message matching the given parameters. Return -1 if none available.
507  */
508 int QUEUE_FindMsg( MESSAGEQUEUE * msgQueue, HWND32 hwnd, int first, int last )
509 {
510     int i, pos = msgQueue->nextMessage;
511
512     TRACE(msg,"hwnd=%04x pos=%d\n", hwnd, pos );
513
514     if (!msgQueue->msgCount) return -1;
515     if (!hwnd && !first && !last) return pos;
516         
517     for (i = 0; i < msgQueue->msgCount; i++)
518     {
519         MSG16 * msg = &msgQueue->messages[pos].msg;
520
521         if (!hwnd || (msg->hwnd == hwnd))
522         {
523             if (!first && !last) return pos;
524             if ((msg->message >= first) && (msg->message <= last)) return pos;
525         }
526         if (pos < msgQueue->queueSize-1) pos++;
527         else pos = 0;
528     }
529     return -1;
530 }
531
532
533 /***********************************************************************
534  *           QUEUE_RemoveMsg
535  *
536  * Remove a message from the queue (pos must be a valid position).
537  */
538 void QUEUE_RemoveMsg( MESSAGEQUEUE * msgQueue, int pos )
539 {
540     SIGNAL_MaskAsyncEvents( TRUE );
541
542     if (pos >= msgQueue->nextMessage)
543     {
544         for ( ; pos > msgQueue->nextMessage; pos--)
545             msgQueue->messages[pos] = msgQueue->messages[pos-1];
546         msgQueue->nextMessage++;
547         if (msgQueue->nextMessage >= msgQueue->queueSize)
548             msgQueue->nextMessage = 0;
549     }
550     else
551     {
552         for ( ; pos < msgQueue->nextFreeMessage; pos++)
553             msgQueue->messages[pos] = msgQueue->messages[pos+1];
554         if (msgQueue->nextFreeMessage) msgQueue->nextFreeMessage--;
555         else msgQueue->nextFreeMessage = msgQueue->queueSize-1;
556     }
557     msgQueue->msgCount--;
558     if (!msgQueue->msgCount) msgQueue->wakeBits &= ~QS_POSTMESSAGE;
559
560     SIGNAL_MaskAsyncEvents( FALSE );
561 }
562
563
564 /***********************************************************************
565  *           QUEUE_WakeSomeone
566  *
567  * Wake a queue upon reception of a hardware event.
568  */
569 static void QUEUE_WakeSomeone( UINT32 message )
570 {
571     WND*          wndPtr = NULL;
572     WORD          wakeBit;
573     HWND32 hwnd;
574     MESSAGEQUEUE *queue = pCursorQueue;
575
576     if( (message >= WM_KEYFIRST) && (message <= WM_KEYLAST) )
577     {
578        wakeBit = QS_KEY;
579        if( pActiveQueue ) queue = pActiveQueue;
580     }
581     else 
582     {
583        wakeBit = (message == WM_MOUSEMOVE) ? QS_MOUSEMOVE : QS_MOUSEBUTTON;
584        if( (hwnd = GetCapture32()) )
585          if( (wndPtr = WIN_FindWndPtr( hwnd )) ) 
586            queue = (MESSAGEQUEUE *)GlobalLock16( wndPtr->hmemTaskQ );
587     }
588
589     if( (hwnd = GetSysModalWindow16()) )
590       if( (wndPtr = WIN_FindWndPtr( hwnd )) )
591         queue = (MESSAGEQUEUE *)GlobalLock16( wndPtr->hmemTaskQ );
592
593     if( !queue ) 
594     {
595       queue = GlobalLock16( hFirstQueue );
596       while( queue )
597       {
598         if (queue->wakeMask & wakeBit) break;
599         queue = GlobalLock16( queue->next );
600       }
601       if( !queue )
602       { 
603         WARN(msg, "couldn't find queue\n"); 
604         return; 
605       }
606     }
607
608     QUEUE_SetWakeBit( queue, wakeBit );
609 }
610
611
612 /***********************************************************************
613  *           hardware_event
614  *
615  * Add an event to the system message queue.
616  * Note: the position is relative to the desktop window.
617  */
618 void hardware_event( WORD message, WORD wParam, LONG lParam,
619                      int xPos, int yPos, DWORD time, DWORD extraInfo )
620 {
621     MSG16 *msg;
622     int pos;
623
624     if (!sysMsgQueue) return;
625     pos = sysMsgQueue->nextFreeMessage;
626
627       /* Merge with previous event if possible */
628
629     if ((message == WM_MOUSEMOVE) && sysMsgQueue->msgCount)
630     {
631         if (pos > 0) pos--;
632         else pos = sysMsgQueue->queueSize - 1;
633         msg = &sysMsgQueue->messages[pos].msg;
634         if ((msg->message == message) && (msg->wParam == wParam))
635             sysMsgQueue->msgCount--;  /* Merge events */
636         else
637             pos = sysMsgQueue->nextFreeMessage;  /* Don't merge */
638     }
639
640       /* Check if queue is full */
641
642     if ((pos == sysMsgQueue->nextMessage) && sysMsgQueue->msgCount)
643     {
644         /* Queue is full, beep (but not on every mouse motion...) */
645         if (message != WM_MOUSEMOVE) MessageBeep32(0);
646         return;
647     }
648
649       /* Store message */
650
651     msg = &sysMsgQueue->messages[pos].msg;
652     msg->hwnd    = 0;
653     msg->message = message;
654     msg->wParam  = wParam;
655     msg->lParam  = lParam;
656     msg->time    = time;
657     msg->pt.x    = xPos & 0xffff;
658     msg->pt.y    = yPos & 0xffff;
659     sysMsgQueue->messages[pos].extraInfo = extraInfo;
660     if (pos < sysMsgQueue->queueSize - 1) pos++;
661     else pos = 0;
662     sysMsgQueue->nextFreeMessage = pos;
663     sysMsgQueue->msgCount++;
664     QUEUE_WakeSomeone( message );
665 }
666
667                     
668 /***********************************************************************
669  *           QUEUE_GetQueueTask
670  */
671 HTASK16 QUEUE_GetQueueTask( HQUEUE16 hQueue )
672 {
673     MESSAGEQUEUE *queue = GlobalLock16( hQueue );
674     return (queue) ? queue->hTask : 0 ;
675 }
676
677
678 /***********************************************************************
679  *           QUEUE_IncPaintCount
680  */
681 void QUEUE_IncPaintCount( HQUEUE16 hQueue )
682 {
683     MESSAGEQUEUE *queue;
684
685     if (!(queue = (MESSAGEQUEUE *)GlobalLock16( hQueue ))) return;
686     queue->wPaintCount++;
687     QUEUE_SetWakeBit( queue, QS_PAINT );
688 }
689
690
691 /***********************************************************************
692  *           QUEUE_DecPaintCount
693  */
694 void QUEUE_DecPaintCount( HQUEUE16 hQueue )
695 {
696     MESSAGEQUEUE *queue;
697
698     if (!(queue = (MESSAGEQUEUE *)GlobalLock16( hQueue ))) return;
699     queue->wPaintCount--;
700     if (!queue->wPaintCount) queue->wakeBits &= ~QS_PAINT;
701 }
702
703
704 /***********************************************************************
705  *           QUEUE_IncTimerCount
706  */
707 void QUEUE_IncTimerCount( HQUEUE16 hQueue )
708 {
709     MESSAGEQUEUE *queue;
710
711     if (!(queue = (MESSAGEQUEUE *)GlobalLock16( hQueue ))) return;
712     queue->wTimerCount++;
713     QUEUE_SetWakeBit( queue, QS_TIMER );
714 }
715
716
717 /***********************************************************************
718  *           QUEUE_DecTimerCount
719  */
720 void QUEUE_DecTimerCount( HQUEUE16 hQueue )
721 {
722     MESSAGEQUEUE *queue;
723
724     if (!(queue = (MESSAGEQUEUE *)GlobalLock16( hQueue ))) return;
725     queue->wTimerCount--;
726     if (!queue->wTimerCount) queue->wakeBits &= ~QS_TIMER;
727 }
728
729
730 /***********************************************************************
731  *           PostQuitMessage16   (USER.6)
732  */
733 void WINAPI PostQuitMessage16( INT16 exitCode )
734 {
735     PostQuitMessage32( exitCode );
736 }
737
738
739 /***********************************************************************
740  *           PostQuitMessage32   (USER32.421)
741  *
742  * PostQuitMessage() posts a message to the system requesting an
743  * application to terminate execution. As a result of this function,
744  * the WM_QUIT message is posted to the application, and
745  * PostQuitMessage() returns immediately.  The exitCode parameter
746  * specifies an application-defined exit code, which appears in the
747  * _wParam_ parameter of the WM_QUIT message posted to the application.  
748  *
749  * CONFORMANCE
750  *
751  *  ECMA-234, Win32
752  */
753 void WINAPI PostQuitMessage32( INT32 exitCode )
754 {
755     MESSAGEQUEUE *queue;
756
757     if (!(queue = (MESSAGEQUEUE *)GlobalLock16( GetTaskQueue(0) ))) return;
758     queue->wPostQMsg = TRUE;
759     queue->wExitCode = (WORD)exitCode;
760 }
761
762
763 /***********************************************************************
764  *           GetWindowTask16   (USER.224)
765  */
766 HTASK16 WINAPI GetWindowTask16( HWND16 hwnd )
767 {
768     WND *wndPtr = WIN_FindWndPtr( hwnd );
769
770     if (!wndPtr) return 0;
771     return QUEUE_GetQueueTask( wndPtr->hmemTaskQ );
772 }
773
774 /***********************************************************************
775  *           GetWindowThreadProcessId   (USER32.313)
776  */
777 DWORD WINAPI GetWindowThreadProcessId( HWND32 hwnd, LPDWORD process )
778 {
779     HTASK16 htask;
780     TDB *tdb;
781
782     WND *wndPtr = WIN_FindWndPtr( hwnd );
783
784     if (!wndPtr) return 0;
785     htask=QUEUE_GetQueueTask( wndPtr->hmemTaskQ );
786     tdb = (TDB*)GlobalLock16(htask);
787     if (!tdb || !tdb->thdb) return 0;
788     if (process) *process = PDB_TO_PROCESS_ID( tdb->thdb->process );
789     return THDB_TO_THREAD_ID( tdb->thdb );
790 }
791
792
793 /***********************************************************************
794  *           SetMessageQueue16   (USER.266)
795  */
796 BOOL16 WINAPI SetMessageQueue16( INT16 size )
797 {
798     return SetMessageQueue32( size );
799 }
800
801
802 /***********************************************************************
803  *           SetMessageQueue32   (USER32.494)
804  */
805 BOOL32 WINAPI SetMessageQueue32( INT32 size )
806 {
807     HQUEUE16 hQueue, hNewQueue;
808     MESSAGEQUEUE *queuePtr;
809
810     TRACE(msg,"task %04x size %i\n", GetCurrentTask(), size); 
811
812     if ((size > MAX_QUEUE_SIZE) || (size <= 0)) return FALSE;
813
814     if( !(hNewQueue = QUEUE_CreateMsgQueue( size ))) 
815     {
816         WARN(msg, "failed!\n");
817         return FALSE;
818     }
819     queuePtr = (MESSAGEQUEUE *)GlobalLock16( hNewQueue );
820
821     SIGNAL_MaskAsyncEvents( TRUE );
822
823     /* Copy data and free the old message queue */
824     if ((hQueue = GetTaskQueue(0)) != 0) 
825     {
826        MESSAGEQUEUE *oldQ = (MESSAGEQUEUE *)GlobalLock16( hQueue );
827        memcpy( &queuePtr->wParamHigh, &oldQ->wParamHigh,
828                         (int)oldQ->messages - (int)(&oldQ->wParamHigh) );
829        HOOK_ResetQueueHooks( hNewQueue );
830        if( WIN_GetDesktop()->hmemTaskQ == hQueue )
831            WIN_GetDesktop()->hmemTaskQ = hNewQueue;
832        WIN_ResetQueueWindows( WIN_GetDesktop(), hQueue, hNewQueue );
833        CLIPBOARD_ResetLock( hQueue, hNewQueue );
834        QUEUE_DeleteMsgQueue( hQueue );
835     }
836
837     /* Link new queue into list */
838     queuePtr->hTask = GetCurrentTask();
839     queuePtr->next  = hFirstQueue;
840     hFirstQueue = hNewQueue;
841     
842     if( !queuePtr->next ) pCursorQueue = queuePtr;
843     SetTaskQueue( 0, hNewQueue );
844     
845     SIGNAL_MaskAsyncEvents( FALSE );
846     return TRUE;
847 }
848
849
850 /***********************************************************************
851  *           GetQueueStatus16   (USER.334)
852  */
853 DWORD WINAPI GetQueueStatus16( UINT16 flags )
854 {
855     MESSAGEQUEUE *queue;
856     DWORD ret;
857
858     if (!(queue = (MESSAGEQUEUE *)GlobalLock16( GetTaskQueue(0) ))) return 0;
859     ret = MAKELONG( queue->changeBits, queue->wakeBits );
860     queue->changeBits = 0;
861     return ret & MAKELONG( flags, flags );
862 }
863
864 /***********************************************************************
865  *           GetQueueStatus32   (USER32.283)
866  */
867 DWORD WINAPI GetQueueStatus32( UINT32 flags )
868 {
869     MESSAGEQUEUE *queue;
870     DWORD ret;
871
872     if (!(queue = (MESSAGEQUEUE *)GlobalLock16( GetTaskQueue(0) ))) return 0;
873     ret = MAKELONG( queue->changeBits, queue->wakeBits );
874     queue->changeBits = 0;
875     return ret & MAKELONG( flags, flags );
876 }
877
878
879 /***********************************************************************
880  *           GetInputState16   (USER.335)
881  */
882 BOOL16 WINAPI GetInputState16(void)
883 {
884     return GetInputState32();
885 }
886
887 /***********************************************************************
888  *           WaitForInputIdle   (USER32.577)
889  */
890 DWORD WINAPI WaitForInputIdle (HANDLE32 hProcess, DWORD dwTimeOut)
891 {
892   FIXME (msg, "(hProcess=%d, dwTimeOut=%ld): stub\n", hProcess, dwTimeOut);
893
894   return WAIT_TIMEOUT;
895 }
896
897
898 /***********************************************************************
899  *           GetInputState32   (USER32.244)
900  */
901 BOOL32 WINAPI GetInputState32(void)
902 {
903     MESSAGEQUEUE *queue;
904
905     if (!(queue = (MESSAGEQUEUE *)GlobalLock16( GetTaskQueue(0) )))
906         return FALSE;
907     return queue->wakeBits & (QS_KEY | QS_MOUSEBUTTON);
908 }
909
910
911 /***********************************************************************
912  *           GetMessagePos   (USER.119) (USER32.272)
913  * 
914  * The GetMessagePos() function returns a long value representing a
915  * cursor position, in screen coordinates, when the last message
916  * retrieved by the GetMessage() function occurs. The x-coordinate is
917  * in the low-order word of the return value, the y-coordinate is in
918  * the high-order word. The application can use the MAKEPOINT()
919  * macro to obtain a POINT structure from the return value. 
920  *
921  * For the current cursor position, use GetCursorPos().
922  *
923  * RETURNS
924  *
925  * Cursor position of last message on success, zero on failure.
926  *
927  * CONFORMANCE
928  *
929  * ECMA-234, Win32
930  *
931  */
932 DWORD WINAPI GetMessagePos(void)
933 {
934     MESSAGEQUEUE *queue;
935
936     if (!(queue = (MESSAGEQUEUE *)GlobalLock16( GetTaskQueue(0) ))) return 0;
937     return queue->GetMessagePosVal;
938 }
939
940
941 /***********************************************************************
942  *           GetMessageTime   (USER.120) (USER32.273)
943  *
944  * GetMessageTime() returns the message time for the last message
945  * retrieved by the function. The time is measured in milliseconds with
946  * the same offset as GetTickCount().
947  *
948  * Since the tick count wraps, this is only useful for moderately short
949  * relative time comparisons.
950  *
951  * RETURNS
952  *
953  * Time of last message on success, zero on failure.
954  *
955  * CONFORMANCE
956  *
957  * ECMA-234, Win32
958  *  
959  */
960 LONG WINAPI GetMessageTime(void)
961 {
962     MESSAGEQUEUE *queue;
963
964     if (!(queue = (MESSAGEQUEUE *)GlobalLock16( GetTaskQueue(0) ))) return 0;
965     return queue->GetMessageTimeVal;
966 }
967
968
969 /***********************************************************************
970  *           GetMessageExtraInfo   (USER.288) (USER32.271)
971  */
972 LONG WINAPI GetMessageExtraInfo(void)
973 {
974     MESSAGEQUEUE *queue;
975
976     if (!(queue = (MESSAGEQUEUE *)GlobalLock16( GetTaskQueue(0) ))) return 0;
977     return queue->GetMessageExtraInfoVal;
978 }