Use libwine and libwine_unicode directly from their build directory
[wine/multimedia.git] / windows / queue.c
blob9f90f58408b7f8673b14be1f9d9074416caed0c7
1 /* * Message queues related functions
3 * Copyright 1993, 1994 Alexandre Julliard
4 */
6 #include <string.h>
7 #include <signal.h>
8 #include <assert.h>
9 #include "windef.h"
10 #include "wingdi.h"
11 #include "winerror.h"
12 #include "wine/winbase16.h"
13 #include "wine/winuser16.h"
14 #include "queue.h"
15 #include "task.h"
16 #include "win.h"
17 #include "clipboard.h"
18 #include "hook.h"
19 #include "heap.h"
20 #include "thread.h"
21 #include "debugtools.h"
22 #include "server.h"
23 #include "spy.h"
25 DECLARE_DEBUG_CHANNEL(msg);
26 DECLARE_DEBUG_CHANNEL(sendmsg);
28 #define MAX_QUEUE_SIZE 120 /* Max. size of a message queue */
30 static HQUEUE16 hFirstQueue = 0;
31 static HQUEUE16 hExitingQueue = 0;
32 static HQUEUE16 hmemSysMsgQueue = 0;
33 static MESSAGEQUEUE *sysMsgQueue = NULL;
34 static PERQUEUEDATA *pQDataWin16 = NULL; /* Global perQData for Win16 tasks */
36 static MESSAGEQUEUE *pMouseQueue = NULL; /* Queue for last mouse message */
37 static MESSAGEQUEUE *pKbdQueue = NULL; /* Queue for last kbd message */
39 HQUEUE16 hCursorQueue = 0;
40 HQUEUE16 hActiveQueue = 0;
43 /***********************************************************************
44 * PERQDATA_CreateInstance
46 * Creates an instance of a reference counted PERQUEUEDATA element
47 * for the message queue. perQData is stored globally for 16 bit tasks.
49 * Note: We don't implement perQdata exactly the same way Windows does.
50 * Each perQData element is reference counted since it may be potentially
51 * shared by multiple message Queues (via AttachThreadInput).
52 * We only store the current values for Active, Capture and focus windows
53 * currently.
55 PERQUEUEDATA * PERQDATA_CreateInstance( )
57 PERQUEUEDATA *pQData;
59 BOOL16 bIsWin16 = 0;
61 TRACE_(msg)("()\n");
63 /* Share a single instance of perQData for all 16 bit tasks */
64 if ( ( bIsWin16 = THREAD_IsWin16( NtCurrentTeb() ) ) )
66 /* If previously allocated, just bump up ref count */
67 if ( pQDataWin16 )
69 PERQDATA_Addref( pQDataWin16 );
70 return pQDataWin16;
74 /* Allocate PERQUEUEDATA from the system heap */
75 if (!( pQData = (PERQUEUEDATA *) HeapAlloc( SystemHeap, 0,
76 sizeof(PERQUEUEDATA) ) ))
77 return 0;
79 /* Initialize */
80 pQData->hWndCapture = pQData->hWndFocus = pQData->hWndActive = 0;
81 pQData->ulRefCount = 1;
82 pQData->nCaptureHT = HTCLIENT;
84 /* Note: We have an independent critical section for the per queue data
85 * since this may be shared by different threads. see AttachThreadInput()
87 InitializeCriticalSection( &pQData->cSection );
88 /* FIXME: not all per queue data critical sections should be global */
89 MakeCriticalSectionGlobal( &pQData->cSection );
91 /* Save perQData globally for 16 bit tasks */
92 if ( bIsWin16 )
93 pQDataWin16 = pQData;
95 return pQData;
99 /***********************************************************************
100 * PERQDATA_Addref
102 * Increment reference count for the PERQUEUEDATA instance
103 * Returns reference count for debugging purposes
105 ULONG PERQDATA_Addref( PERQUEUEDATA *pQData )
107 assert(pQData != 0 );
108 TRACE_(msg)("(): current refcount %lu ...\n", pQData->ulRefCount);
110 EnterCriticalSection( &pQData->cSection );
111 ++pQData->ulRefCount;
112 LeaveCriticalSection( &pQData->cSection );
114 return pQData->ulRefCount;
118 /***********************************************************************
119 * PERQDATA_Release
121 * Release a reference to a PERQUEUEDATA instance.
122 * Destroy the instance if no more references exist
123 * Returns reference count for debugging purposes
125 ULONG PERQDATA_Release( PERQUEUEDATA *pQData )
127 assert(pQData != 0 );
128 TRACE_(msg)("(): current refcount %lu ...\n",
129 (LONG)pQData->ulRefCount );
131 EnterCriticalSection( &pQData->cSection );
132 if ( --pQData->ulRefCount == 0 )
134 LeaveCriticalSection( &pQData->cSection );
135 DeleteCriticalSection( &pQData->cSection );
137 TRACE_(msg)("(): deleting PERQUEUEDATA instance ...\n" );
139 /* Deleting our global 16 bit perQData? */
140 if ( pQData == pQDataWin16 )
141 pQDataWin16 = 0;
143 /* Free the PERQUEUEDATA instance */
144 HeapFree( SystemHeap, 0, pQData );
146 return 0;
148 LeaveCriticalSection( &pQData->cSection );
150 return pQData->ulRefCount;
154 /***********************************************************************
155 * PERQDATA_GetFocusWnd
157 * Get the focus hwnd member in a threadsafe manner
159 HWND PERQDATA_GetFocusWnd( PERQUEUEDATA *pQData )
161 HWND hWndFocus;
162 assert(pQData != 0 );
164 EnterCriticalSection( &pQData->cSection );
165 hWndFocus = pQData->hWndFocus;
166 LeaveCriticalSection( &pQData->cSection );
168 return hWndFocus;
172 /***********************************************************************
173 * PERQDATA_SetFocusWnd
175 * Set the focus hwnd member in a threadsafe manner
177 HWND PERQDATA_SetFocusWnd( PERQUEUEDATA *pQData, HWND hWndFocus )
179 HWND hWndFocusPrv;
180 assert(pQData != 0 );
182 EnterCriticalSection( &pQData->cSection );
183 hWndFocusPrv = pQData->hWndFocus;
184 pQData->hWndFocus = hWndFocus;
185 LeaveCriticalSection( &pQData->cSection );
187 return hWndFocusPrv;
191 /***********************************************************************
192 * PERQDATA_GetActiveWnd
194 * Get the active hwnd member in a threadsafe manner
196 HWND PERQDATA_GetActiveWnd( PERQUEUEDATA *pQData )
198 HWND hWndActive;
199 assert(pQData != 0 );
201 EnterCriticalSection( &pQData->cSection );
202 hWndActive = pQData->hWndActive;
203 LeaveCriticalSection( &pQData->cSection );
205 return hWndActive;
209 /***********************************************************************
210 * PERQDATA_SetActiveWnd
212 * Set the active focus hwnd member in a threadsafe manner
214 HWND PERQDATA_SetActiveWnd( PERQUEUEDATA *pQData, HWND hWndActive )
216 HWND hWndActivePrv;
217 assert(pQData != 0 );
219 EnterCriticalSection( &pQData->cSection );
220 hWndActivePrv = pQData->hWndActive;
221 pQData->hWndActive = hWndActive;
222 LeaveCriticalSection( &pQData->cSection );
224 return hWndActivePrv;
228 /***********************************************************************
229 * PERQDATA_GetCaptureWnd
231 * Get the capture hwnd member in a threadsafe manner
233 HWND PERQDATA_GetCaptureWnd( PERQUEUEDATA *pQData )
235 HWND hWndCapture;
236 assert(pQData != 0 );
238 EnterCriticalSection( &pQData->cSection );
239 hWndCapture = pQData->hWndCapture;
240 LeaveCriticalSection( &pQData->cSection );
242 return hWndCapture;
246 /***********************************************************************
247 * PERQDATA_SetCaptureWnd
249 * Set the capture hwnd member in a threadsafe manner
251 HWND PERQDATA_SetCaptureWnd( PERQUEUEDATA *pQData, HWND hWndCapture )
253 HWND hWndCapturePrv;
254 assert(pQData != 0 );
256 EnterCriticalSection( &pQData->cSection );
257 hWndCapturePrv = pQData->hWndCapture;
258 pQData->hWndCapture = hWndCapture;
259 LeaveCriticalSection( &pQData->cSection );
261 return hWndCapturePrv;
265 /***********************************************************************
266 * PERQDATA_GetCaptureInfo
268 * Get the capture info member in a threadsafe manner
270 INT16 PERQDATA_GetCaptureInfo( PERQUEUEDATA *pQData )
272 INT16 nCaptureHT;
273 assert(pQData != 0 );
275 EnterCriticalSection( &pQData->cSection );
276 nCaptureHT = pQData->nCaptureHT;
277 LeaveCriticalSection( &pQData->cSection );
279 return nCaptureHT;
283 /***********************************************************************
284 * PERQDATA_SetCaptureInfo
286 * Set the capture info member in a threadsafe manner
288 INT16 PERQDATA_SetCaptureInfo( PERQUEUEDATA *pQData, INT16 nCaptureHT )
290 INT16 nCaptureHTPrv;
291 assert(pQData != 0 );
293 EnterCriticalSection( &pQData->cSection );
294 nCaptureHTPrv = pQData->nCaptureHT;
295 pQData->nCaptureHT = nCaptureHT;
296 LeaveCriticalSection( &pQData->cSection );
298 return nCaptureHTPrv;
302 /***********************************************************************
303 * QUEUE_Lock
305 * Function for getting a 32 bit pointer on queue structure. For thread
306 * safeness programmers should use this function instead of GlobalLock to
307 * retrieve a pointer on the structure. QUEUE_Unlock should also be called
308 * when access to the queue structure is not required anymore.
310 MESSAGEQUEUE *QUEUE_Lock( HQUEUE16 hQueue )
312 MESSAGEQUEUE *queue;
314 HeapLock( SystemHeap ); /* FIXME: a bit overkill */
315 queue = GlobalLock16( hQueue );
316 if ( !queue || (queue->magic != QUEUE_MAGIC) )
318 HeapUnlock( SystemHeap );
319 return NULL;
322 queue->lockCount++;
323 HeapUnlock( SystemHeap );
324 return queue;
328 /***********************************************************************
329 * QUEUE_Unlock
331 * Use with QUEUE_Lock to get a thread safe access to message queue
332 * structure
334 void QUEUE_Unlock( MESSAGEQUEUE *queue )
336 if (queue)
338 HeapLock( SystemHeap ); /* FIXME: a bit overkill */
340 if ( --queue->lockCount == 0 )
342 DeleteCriticalSection ( &queue->cSection );
343 if (queue->server_queue)
344 CloseHandle( queue->server_queue );
345 GlobalFree16( queue->self );
348 HeapUnlock( SystemHeap );
353 /***********************************************************************
354 * QUEUE_DumpQueue
356 void QUEUE_DumpQueue( HQUEUE16 hQueue )
358 MESSAGEQUEUE *pq;
360 if (!(pq = (MESSAGEQUEUE*) QUEUE_Lock( hQueue )) )
362 WARN_(msg)("%04x is not a queue handle\n", hQueue );
363 return;
366 DPRINTF( "next: %12.4x Intertask SendMessage:\n"
367 "thread: %10p ----------------------\n"
368 "firstMsg: %8p smWaiting: %10p\n"
369 "lastMsg: %8p smPending: %10p\n"
370 "msgCount: %8.4x smProcessing: %10p\n"
371 "lockCount: %7.4x\n"
372 "wWinVer: %9.4x\n"
373 "paints: %10.4x\n"
374 "timers: %10.4x\n"
375 "wakeBits: %8.4x\n"
376 "wakeMask: %8.4x\n"
377 "hCurHook: %8.4x\n",
378 pq->next, pq->teb, pq->firstMsg, pq->smWaiting, pq->lastMsg,
379 pq->smPending, pq->msgCount, pq->smProcessing,
380 (unsigned)pq->lockCount, pq->wWinVersion,
381 pq->wPaintCount, pq->wTimerCount,
382 pq->wakeBits, pq->wakeMask, pq->hCurHook);
384 QUEUE_Unlock( pq );
388 /***********************************************************************
389 * QUEUE_WalkQueues
391 void QUEUE_WalkQueues(void)
393 char module[10];
394 HQUEUE16 hQueue = hFirstQueue;
396 DPRINTF( "Queue Msgs Thread Task Module\n" );
397 while (hQueue)
399 MESSAGEQUEUE *queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue );
400 if (!queue)
402 WARN_(msg)("Bad queue handle %04x\n", hQueue );
403 return;
405 if (!GetModuleName16( queue->teb->htask16, module, sizeof(module )))
406 strcpy( module, "???" );
407 DPRINTF( "%04x %4d %p %04x %s\n", hQueue,queue->msgCount,
408 queue->teb, queue->teb->htask16, module );
409 hQueue = queue->next;
410 QUEUE_Unlock( queue );
412 DPRINTF( "\n" );
416 /***********************************************************************
417 * QUEUE_IsExitingQueue
419 BOOL QUEUE_IsExitingQueue( HQUEUE16 hQueue )
421 return (hExitingQueue && (hQueue == hExitingQueue));
425 /***********************************************************************
426 * QUEUE_SetExitingQueue
428 void QUEUE_SetExitingQueue( HQUEUE16 hQueue )
430 hExitingQueue = hQueue;
434 /***********************************************************************
435 * QUEUE_CreateMsgQueue
437 * Creates a message queue. Doesn't link it into queue list!
439 static HQUEUE16 QUEUE_CreateMsgQueue( BOOL16 bCreatePerQData )
441 HQUEUE16 hQueue;
442 HANDLE handle = -1;
443 MESSAGEQUEUE * msgQueue;
444 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
446 TRACE_(msg)("(): Creating message queue...\n");
448 if (!(hQueue = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT,
449 sizeof(MESSAGEQUEUE) )))
450 return 0;
452 msgQueue = (MESSAGEQUEUE *) GlobalLock16( hQueue );
453 if ( !msgQueue )
454 return 0;
456 SERVER_START_REQ
458 struct get_msg_queue_request *req = server_alloc_req( sizeof(*req), 0 );
459 if (!server_call( REQ_GET_MSG_QUEUE )) handle = req->handle;
461 SERVER_END_REQ;
462 if (handle == -1)
464 ERR_(msg)("Cannot get thread queue");
465 GlobalFree16( hQueue );
466 return 0;
468 msgQueue->server_queue = handle;
469 msgQueue->server_queue = ConvertToGlobalHandle( msgQueue->server_queue );
471 msgQueue->self = hQueue;
472 msgQueue->wakeBits = msgQueue->changeBits = 0;
473 msgQueue->wWinVersion = pTask ? pTask->version : 0;
475 InitializeCriticalSection( &msgQueue->cSection );
476 MakeCriticalSectionGlobal( &msgQueue->cSection );
478 msgQueue->lockCount = 1;
479 msgQueue->magic = QUEUE_MAGIC;
481 /* Create and initialize our per queue data */
482 msgQueue->pQData = bCreatePerQData ? PERQDATA_CreateInstance() : NULL;
484 return hQueue;
488 /***********************************************************************
489 * QUEUE_FlushMessage
491 * Try to reply to all pending sent messages on exit.
493 static void QUEUE_FlushMessages( MESSAGEQUEUE *queue )
495 SMSG *smsg;
496 MESSAGEQUEUE *senderQ = 0;
498 if( queue )
500 EnterCriticalSection( &queue->cSection );
502 /* empty the list of pending SendMessage waiting to be received */
503 while (queue->smPending)
505 smsg = QUEUE_RemoveSMSG( queue, SM_PENDING_LIST, 0);
507 senderQ = (MESSAGEQUEUE*)QUEUE_Lock( smsg->hSrcQueue );
508 if ( !senderQ )
509 continue;
511 /* return 0, to unblock other thread */
512 smsg->lResult = 0;
513 smsg->flags |= SMSG_HAVE_RESULT;
514 QUEUE_SetWakeBit( senderQ, QS_SMRESULT);
516 QUEUE_Unlock( senderQ );
519 QUEUE_ClearWakeBit( queue, QS_SENDMESSAGE );
521 LeaveCriticalSection( &queue->cSection );
526 /***********************************************************************
527 * QUEUE_DeleteMsgQueue
529 * Unlinks and deletes a message queue.
531 * Note: We need to mask asynchronous events to make sure PostMessage works
532 * even in the signal handler.
534 BOOL QUEUE_DeleteMsgQueue( HQUEUE16 hQueue )
536 MESSAGEQUEUE * msgQueue = (MESSAGEQUEUE*)QUEUE_Lock(hQueue);
537 HQUEUE16 *pPrev;
539 TRACE_(msg)("(): Deleting message queue %04x\n", hQueue);
541 if (!hQueue || !msgQueue)
543 ERR_(msg)("invalid argument.\n");
544 return 0;
547 msgQueue->magic = 0;
549 if( hCursorQueue == hQueue ) hCursorQueue = 0;
550 if( hActiveQueue == hQueue ) hActiveQueue = 0;
552 /* flush sent messages */
553 QUEUE_FlushMessages( msgQueue );
555 HeapLock( SystemHeap ); /* FIXME: a bit overkill */
557 /* Release per queue data if present */
558 if ( msgQueue->pQData )
560 PERQDATA_Release( msgQueue->pQData );
561 msgQueue->pQData = 0;
564 /* remove the message queue from the global link list */
565 pPrev = &hFirstQueue;
566 while (*pPrev && (*pPrev != hQueue))
568 MESSAGEQUEUE *msgQ = (MESSAGEQUEUE*)GlobalLock16(*pPrev);
570 /* sanity check */
571 if ( !msgQ || (msgQ->magic != QUEUE_MAGIC) )
573 /* HQUEUE link list is corrupted, try to exit gracefully */
574 ERR_(msg)("HQUEUE link list corrupted!\n");
575 pPrev = 0;
576 break;
578 pPrev = &msgQ->next;
580 if (pPrev && *pPrev) *pPrev = msgQueue->next;
581 msgQueue->self = 0;
583 HeapUnlock( SystemHeap );
585 /* free up resource used by MESSAGEQUEUE structure */
586 msgQueue->lockCount--;
587 QUEUE_Unlock( msgQueue );
589 return 1;
593 /***********************************************************************
594 * QUEUE_CreateSysMsgQueue
596 * Create the system message queue, and set the double-click speed.
597 * Must be called only once.
599 BOOL QUEUE_CreateSysMsgQueue( int size )
601 /* Note: We dont need perQ data for the system message queue */
602 if (!(hmemSysMsgQueue = QUEUE_CreateMsgQueue( FALSE )))
603 return FALSE;
605 sysMsgQueue = (MESSAGEQUEUE *) GlobalLock16( hmemSysMsgQueue );
606 return TRUE;
610 /***********************************************************************
611 * QUEUE_GetSysQueue
613 MESSAGEQUEUE *QUEUE_GetSysQueue(void)
615 return sysMsgQueue;
619 /***********************************************************************
620 * QUEUE_SetWakeBit
622 * See "Windows Internals", p.449
624 void QUEUE_SetWakeBit( MESSAGEQUEUE *queue, WORD bit )
626 TRACE_(msg)("queue = %04x (wm=%04x), bit = %04x\n",
627 queue->self, queue->wakeMask, bit );
629 if (bit & QS_MOUSE) pMouseQueue = queue;
630 if (bit & QS_KEY) pKbdQueue = queue;
631 queue->changeBits |= bit;
632 queue->wakeBits |= bit;
633 if (queue->wakeMask & bit)
635 queue->wakeMask = 0;
637 /* Wake up thread waiting for message */
638 if ( THREAD_IsWin16( queue->teb ) )
640 int iWndsLock = WIN_SuspendWndsLock();
641 PostEvent16( queue->teb->htask16 );
642 WIN_RestoreWndsLock( iWndsLock );
644 else
646 SERVER_START_REQ
648 struct wake_queue_request *req = server_alloc_req( sizeof(*req), 0 );
649 req->handle = queue->server_queue;
650 req->bits = bit;
651 server_call( REQ_WAKE_QUEUE );
653 SERVER_END_REQ;
659 /***********************************************************************
660 * QUEUE_ClearWakeBit
662 void QUEUE_ClearWakeBit( MESSAGEQUEUE *queue, WORD bit )
664 queue->changeBits &= ~bit;
665 queue->wakeBits &= ~bit;
669 /***********************************************************************
670 * QUEUE_WaitBits
672 * See "Windows Internals", p.447
674 * return values:
675 * 0 if exit with timeout
676 * 1 otherwise
678 int QUEUE_WaitBits( WORD bits, DWORD timeout )
680 MESSAGEQUEUE *queue;
681 DWORD curTime = 0;
682 HQUEUE16 hQueue;
684 TRACE_(msg)("q %04x waiting for %04x\n", GetFastQueue16(), bits);
686 if ( THREAD_IsWin16( NtCurrentTeb() ) && (timeout != INFINITE) )
687 curTime = GetTickCount();
689 hQueue = GetFastQueue16();
690 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return 0;
692 for (;;)
694 if (queue->changeBits & bits)
696 /* One of the bits is set; we can return */
697 queue->wakeMask = 0;
698 QUEUE_Unlock( queue );
699 return 1;
701 if (queue->wakeBits & QS_SENDMESSAGE)
703 /* Process the sent message immediately */
705 queue->wakeMask = 0;
706 QUEUE_ReceiveMessage( queue );
707 continue; /* nested sm crux */
710 queue->wakeMask = bits | QS_SENDMESSAGE;
711 if(queue->changeBits & bits)
713 continue;
716 TRACE_(msg)("%04x) wakeMask is %04x, waiting\n", queue->self, queue->wakeMask);
718 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
720 BOOL bHasWin16Lock;
721 DWORD dwlc;
723 if ( (bHasWin16Lock = _ConfirmWin16Lock()) )
725 TRACE_(msg)("bHasWin16Lock=TRUE\n");
726 ReleaseThunkLock( &dwlc );
729 WaitForSingleObject( queue->server_queue, timeout );
731 if ( bHasWin16Lock )
733 RestoreThunkLock( dwlc );
736 else
738 if ( timeout == INFINITE )
739 WaitEvent16( 0 ); /* win 16 thread, use WaitEvent */
740 else
742 /* check for timeout, then give control to other tasks */
743 if (GetTickCount() - curTime > timeout)
746 QUEUE_Unlock( queue );
747 return 0; /* exit with timeout */
749 Yield16();
756 /***********************************************************************
757 * QUEUE_AddSMSG
759 * This routine is called when a SMSG need to be added to one of the three
760 * SM list. (SM_PROCESSING_LIST, SM_PENDING_LIST, SM_WAITING_LIST)
762 BOOL QUEUE_AddSMSG( MESSAGEQUEUE *queue, int list, SMSG *smsg )
764 TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
765 smsg, SPY_GetMsgName(smsg->msg));
767 switch (list)
769 case SM_PROCESSING_LIST:
770 /* don't need to be thread safe, only accessed by the
771 thread associated with the sender queue */
772 smsg->nextProcessing = queue->smProcessing;
773 queue->smProcessing = smsg;
774 break;
776 case SM_WAITING_LIST:
777 /* don't need to be thread safe, only accessed by the
778 thread associated with the receiver queue */
779 smsg->nextWaiting = queue->smWaiting;
780 queue->smWaiting = smsg;
781 break;
783 case SM_PENDING_LIST:
785 /* make it thread safe, could be accessed by the sender and
786 receiver thread */
787 SMSG **prev;
789 EnterCriticalSection( &queue->cSection );
790 smsg->nextPending = NULL;
791 prev = &queue->smPending;
792 while ( *prev )
793 prev = &(*prev)->nextPending;
794 *prev = smsg;
795 LeaveCriticalSection( &queue->cSection );
797 QUEUE_SetWakeBit( queue, QS_SENDMESSAGE );
798 break;
801 default:
802 ERR_(sendmsg)("Invalid list: %d", list);
803 break;
806 return TRUE;
810 /***********************************************************************
811 * QUEUE_RemoveSMSG
813 * This routine is called when a SMSG needs to be removed from one of the three
814 * SM lists (SM_PROCESSING_LIST, SM_PENDING_LIST, SM_WAITING_LIST).
815 * If smsg == 0, remove the first smsg from the specified list
817 SMSG *QUEUE_RemoveSMSG( MESSAGEQUEUE *queue, int list, SMSG *smsg )
820 switch (list)
822 case SM_PROCESSING_LIST:
823 /* don't need to be thread safe, only accessed by the
824 thread associated with the sender queue */
826 /* if smsg is equal to null, it means the first in the list */
827 if (!smsg)
828 smsg = queue->smProcessing;
830 TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
831 smsg, SPY_GetMsgName(smsg->msg));
832 /* In fact SM_PROCESSING_LIST is a stack, and smsg
833 should be always at the top of the list */
834 if ( (smsg != queue->smProcessing) || !queue->smProcessing )
836 ERR_(sendmsg)("smsg not at the top of Processing list, smsg=0x%p queue=0x%p\n", smsg, queue);
837 return 0;
839 else
841 queue->smProcessing = smsg->nextProcessing;
842 smsg->nextProcessing = 0;
844 return smsg;
846 case SM_WAITING_LIST:
847 /* don't need to be thread safe, only accessed by the
848 thread associated with the receiver queue */
850 /* if smsg is equal to null, it means the first in the list */
851 if (!smsg)
852 smsg = queue->smWaiting;
854 TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
855 smsg, SPY_GetMsgName(smsg->msg));
856 /* In fact SM_WAITING_LIST is a stack, and smsg
857 should be always at the top of the list */
858 if ( (smsg != queue->smWaiting) || !queue->smWaiting )
860 ERR_(sendmsg)("smsg not at the top of Waiting list, smsg=0x%p queue=0x%p\n", smsg, queue);
861 return 0;
863 else
865 queue->smWaiting = smsg->nextWaiting;
866 smsg->nextWaiting = 0;
868 return smsg;
870 case SM_PENDING_LIST:
871 /* make it thread safe, could be accessed by the sender and
872 receiver thread */
873 EnterCriticalSection( &queue->cSection );
875 if (!smsg)
876 smsg = queue->smPending;
877 if ( (smsg != queue->smPending) || !queue->smPending )
879 ERR_(sendmsg)("should always remove the top one in Pending list, smsg=0x%p queue=0x%p\n", smsg, queue);
880 LeaveCriticalSection( &queue->cSection );
881 return 0;
884 TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
885 smsg, SPY_GetMsgName(smsg->msg));
887 queue->smPending = smsg->nextPending;
888 smsg->nextPending = 0;
890 /* if no more SMSG in Pending list, clear QS_SENDMESSAGE flag */
891 if (!queue->smPending)
892 QUEUE_ClearWakeBit( queue, QS_SENDMESSAGE );
894 LeaveCriticalSection( &queue->cSection );
895 return smsg;
897 default:
898 ERR_(sendmsg)("Invalid list: %d\n", list);
899 break;
902 return 0;
906 /***********************************************************************
907 * QUEUE_ReceiveMessage
909 * This routine is called when a sent message is waiting for the queue.
911 void QUEUE_ReceiveMessage( MESSAGEQUEUE *queue )
913 LRESULT result = 0;
914 SMSG *smsg;
915 MESSAGEQUEUE *senderQ;
917 TRACE_(sendmsg)("queue %04x\n", queue->self );
919 if ( !((queue->wakeBits & QS_SENDMESSAGE) && queue->smPending) )
921 TRACE_(sendmsg)("\trcm: nothing to do\n");
922 return;
925 /* remove smsg on the top of the pending list and put it in the processing list */
926 smsg = QUEUE_RemoveSMSG(queue, SM_PENDING_LIST, 0);
927 QUEUE_AddSMSG(queue, SM_WAITING_LIST, smsg);
929 TRACE_(sendmsg)("RM: %s [%04x] (%04x -> %04x)\n",
930 SPY_GetMsgName(smsg->msg), smsg->msg, smsg->hSrcQueue, smsg->hDstQueue );
932 if (IsWindow( smsg->hWnd ))
934 WND *wndPtr = WIN_FindWndPtr( smsg->hWnd );
935 DWORD extraInfo = queue->GetMessageExtraInfoVal; /* save ExtraInfo */
937 /* use sender queue extra info value while calling the window proc */
938 senderQ = (MESSAGEQUEUE*)QUEUE_Lock( smsg->hSrcQueue );
939 if (senderQ)
941 queue->GetMessageExtraInfoVal = senderQ->GetMessageExtraInfoVal;
942 QUEUE_Unlock( senderQ );
945 /* call the right version of CallWindowProcXX */
946 if (smsg->flags & SMSG_WIN32)
948 TRACE_(sendmsg)("\trcm: msg is Win32\n" );
949 if (smsg->flags & SMSG_UNICODE)
950 result = CallWindowProcW( wndPtr->winproc,
951 smsg->hWnd, smsg->msg,
952 smsg->wParam, smsg->lParam );
953 else
954 result = CallWindowProcA( wndPtr->winproc,
955 smsg->hWnd, smsg->msg,
956 smsg->wParam, smsg->lParam );
958 else /* Win16 message */
959 result = CallWindowProc16( (WNDPROC16)wndPtr->winproc,
960 (HWND16) smsg->hWnd,
961 (UINT16) smsg->msg,
962 LOWORD (smsg->wParam),
963 smsg->lParam );
965 queue->GetMessageExtraInfoVal = extraInfo; /* Restore extra info */
966 WIN_ReleaseWndPtr(wndPtr);
967 TRACE_(sendmsg)("result = %08x\n", (unsigned)result );
969 else WARN_(sendmsg)("\trcm: bad hWnd\n");
972 /* set SMSG_SENDING_REPLY flag to tell ReplyMessage16, it's not
973 an early reply */
974 smsg->flags |= SMSG_SENDING_REPLY;
975 ReplyMessage( result );
977 TRACE_(sendmsg)("done!\n" );
982 /***********************************************************************
983 * QUEUE_AddMsg
985 * Add a message to the queue. Return FALSE if queue is full.
987 BOOL QUEUE_AddMsg( HQUEUE16 hQueue, int type, MSG *msg, DWORD extraInfo )
989 MESSAGEQUEUE *msgQueue;
990 QMSG *qmsg;
993 if (!(msgQueue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return FALSE;
995 /* allocate new message in global heap for now */
996 if (!(qmsg = (QMSG *) HeapAlloc( SystemHeap, 0, sizeof(QMSG) ) ))
998 QUEUE_Unlock( msgQueue );
999 return 0;
1002 EnterCriticalSection( &msgQueue->cSection );
1004 /* Store message */
1005 qmsg->type = type;
1006 qmsg->msg = *msg;
1007 qmsg->extraInfo = extraInfo;
1009 /* insert the message in the link list */
1010 qmsg->nextMsg = 0;
1011 qmsg->prevMsg = msgQueue->lastMsg;
1013 if (msgQueue->lastMsg)
1014 msgQueue->lastMsg->nextMsg = qmsg;
1016 /* update first and last anchor in message queue */
1017 msgQueue->lastMsg = qmsg;
1018 if (!msgQueue->firstMsg)
1019 msgQueue->firstMsg = qmsg;
1021 msgQueue->msgCount++;
1023 LeaveCriticalSection( &msgQueue->cSection );
1025 QUEUE_SetWakeBit( msgQueue, QS_POSTMESSAGE );
1026 QUEUE_Unlock( msgQueue );
1028 return TRUE;
1033 /***********************************************************************
1034 * QUEUE_FindMsg
1036 * Find a message matching the given parameters. Return -1 if none available.
1038 QMSG* QUEUE_FindMsg( MESSAGEQUEUE * msgQueue, HWND hwnd, int first, int last )
1040 QMSG* qmsg;
1042 EnterCriticalSection( &msgQueue->cSection );
1044 if (!msgQueue->msgCount)
1045 qmsg = 0;
1046 else if (!hwnd && !first && !last)
1047 qmsg = msgQueue->firstMsg;
1048 else
1050 /* look in linked list for message matching first and last criteria */
1051 for (qmsg = msgQueue->firstMsg; qmsg; qmsg = qmsg->nextMsg)
1053 MSG *msg = &(qmsg->msg);
1055 if (!hwnd || (msg->hwnd == hwnd))
1057 if (!first && !last)
1058 break; /* found it */
1060 if ((msg->message >= first) && (!last || (msg->message <= last)))
1061 break; /* found it */
1066 LeaveCriticalSection( &msgQueue->cSection );
1068 return qmsg;
1073 /***********************************************************************
1074 * QUEUE_RemoveMsg
1076 * Remove a message from the queue (pos must be a valid position).
1078 void QUEUE_RemoveMsg( MESSAGEQUEUE * msgQueue, QMSG *qmsg )
1080 EnterCriticalSection( &msgQueue->cSection );
1082 /* set the linked list */
1083 if (qmsg->prevMsg)
1084 qmsg->prevMsg->nextMsg = qmsg->nextMsg;
1086 if (qmsg->nextMsg)
1087 qmsg->nextMsg->prevMsg = qmsg->prevMsg;
1089 if (msgQueue->firstMsg == qmsg)
1090 msgQueue->firstMsg = qmsg->nextMsg;
1092 if (msgQueue->lastMsg == qmsg)
1093 msgQueue->lastMsg = qmsg->prevMsg;
1095 /* deallocate the memory for the message */
1096 HeapFree( SystemHeap, 0, qmsg );
1098 msgQueue->msgCount--;
1099 if (!msgQueue->msgCount) msgQueue->wakeBits &= ~QS_POSTMESSAGE;
1101 LeaveCriticalSection( &msgQueue->cSection );
1105 /***********************************************************************
1106 * QUEUE_WakeSomeone
1108 * Wake a queue upon reception of a hardware event.
1110 static void QUEUE_WakeSomeone( UINT message )
1112 WND* wndPtr = NULL;
1113 WORD wakeBit;
1114 HWND hwnd;
1115 HQUEUE16 hQueue = 0;
1116 MESSAGEQUEUE *queue = NULL;
1118 if (hCursorQueue)
1119 hQueue = hCursorQueue;
1121 if( (message >= WM_KEYFIRST) && (message <= WM_KEYLAST) )
1123 wakeBit = QS_KEY;
1124 if( hActiveQueue )
1125 hQueue = hActiveQueue;
1127 else
1129 wakeBit = (message == WM_MOUSEMOVE) ? QS_MOUSEMOVE : QS_MOUSEBUTTON;
1130 if( (hwnd = GetCapture()) )
1131 if( (wndPtr = WIN_FindWndPtr( hwnd )) )
1133 hQueue = wndPtr->hmemTaskQ;
1134 WIN_ReleaseWndPtr(wndPtr);
1138 if( (hwnd = GetSysModalWindow16()) )
1140 if( (wndPtr = WIN_FindWndPtr( hwnd )) )
1142 hQueue = wndPtr->hmemTaskQ;
1143 WIN_ReleaseWndPtr(wndPtr);
1147 if (hQueue)
1148 queue = QUEUE_Lock( hQueue );
1150 if( !queue )
1152 queue = QUEUE_Lock( hFirstQueue );
1153 while( queue )
1155 if (queue->wakeMask & wakeBit) break;
1157 QUEUE_Unlock(queue);
1158 queue = QUEUE_Lock( queue->next );
1160 if( !queue )
1162 WARN_(msg)("couldn't find queue\n");
1163 return;
1167 QUEUE_SetWakeBit( queue, wakeBit );
1169 QUEUE_Unlock( queue );
1173 /***********************************************************************
1174 * hardware_event
1176 * Add an event to the system message queue.
1177 * Note: the position is relative to the desktop window.
1179 void hardware_event( UINT message, WPARAM wParam, LPARAM lParam,
1180 int xPos, int yPos, DWORD time, DWORD extraInfo )
1182 MSG *msg;
1183 QMSG *qmsg;
1184 int mergeMsg = 0;
1186 if (!sysMsgQueue) return;
1188 EnterCriticalSection( &sysMsgQueue->cSection );
1190 /* Merge with previous event if possible */
1191 qmsg = sysMsgQueue->lastMsg;
1193 if ((message == WM_MOUSEMOVE) && sysMsgQueue->lastMsg)
1195 msg = &(sysMsgQueue->lastMsg->msg);
1197 if ((msg->message == message) && (msg->wParam == wParam))
1199 /* Merge events */
1200 qmsg = sysMsgQueue->lastMsg;
1201 mergeMsg = 1;
1205 if (!mergeMsg)
1207 /* Should I limit the number of messages in
1208 the system message queue??? */
1210 /* Don't merge allocate a new msg in the global heap */
1212 if (!(qmsg = (QMSG *) HeapAlloc( SystemHeap, 0, sizeof(QMSG) ) ))
1214 LeaveCriticalSection( &sysMsgQueue->cSection );
1215 return;
1218 /* put message at the end of the linked list */
1219 qmsg->nextMsg = 0;
1220 qmsg->prevMsg = sysMsgQueue->lastMsg;
1222 if (sysMsgQueue->lastMsg)
1223 sysMsgQueue->lastMsg->nextMsg = qmsg;
1225 /* set last and first anchor index in system message queue */
1226 sysMsgQueue->lastMsg = qmsg;
1227 if (!sysMsgQueue->firstMsg)
1228 sysMsgQueue->firstMsg = qmsg;
1230 sysMsgQueue->msgCount++;
1233 /* Store message */
1234 msg = &(qmsg->msg);
1235 msg->hwnd = 0;
1236 msg->message = message;
1237 msg->wParam = wParam;
1238 msg->lParam = lParam;
1239 msg->time = time;
1240 msg->pt.x = xPos;
1241 msg->pt.y = yPos;
1242 qmsg->extraInfo = extraInfo;
1243 qmsg->type = QMSG_HARDWARE;
1245 LeaveCriticalSection( &sysMsgQueue->cSection );
1247 QUEUE_WakeSomeone( message );
1251 /***********************************************************************
1252 * QUEUE_GetQueueTask
1254 HTASK16 QUEUE_GetQueueTask( HQUEUE16 hQueue )
1256 HTASK16 hTask = 0;
1258 MESSAGEQUEUE *queue = QUEUE_Lock( hQueue );
1260 if (queue)
1262 hTask = queue->teb->htask16;
1263 QUEUE_Unlock( queue );
1266 return hTask;
1271 /***********************************************************************
1272 * QUEUE_IncPaintCount
1274 void QUEUE_IncPaintCount( HQUEUE16 hQueue )
1276 MESSAGEQUEUE *queue;
1278 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1279 queue->wPaintCount++;
1280 QUEUE_SetWakeBit( queue, QS_PAINT );
1281 QUEUE_Unlock( queue );
1285 /***********************************************************************
1286 * QUEUE_DecPaintCount
1288 void QUEUE_DecPaintCount( HQUEUE16 hQueue )
1290 MESSAGEQUEUE *queue;
1292 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1293 queue->wPaintCount--;
1294 if (!queue->wPaintCount) queue->wakeBits &= ~QS_PAINT;
1295 QUEUE_Unlock( queue );
1299 /***********************************************************************
1300 * QUEUE_IncTimerCount
1302 void QUEUE_IncTimerCount( HQUEUE16 hQueue )
1304 MESSAGEQUEUE *queue;
1306 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1307 queue->wTimerCount++;
1308 QUEUE_SetWakeBit( queue, QS_TIMER );
1309 QUEUE_Unlock( queue );
1313 /***********************************************************************
1314 * QUEUE_DecTimerCount
1316 void QUEUE_DecTimerCount( HQUEUE16 hQueue )
1318 MESSAGEQUEUE *queue;
1320 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1321 queue->wTimerCount--;
1322 if (!queue->wTimerCount) queue->wakeBits &= ~QS_TIMER;
1323 QUEUE_Unlock( queue );
1327 /***********************************************************************
1328 * PostQuitMessage16 (USER.6)
1330 void WINAPI PostQuitMessage16( INT16 exitCode )
1332 PostQuitMessage( exitCode );
1336 /***********************************************************************
1337 * PostQuitMessage (USER32.421)
1339 * PostQuitMessage() posts a message to the system requesting an
1340 * application to terminate execution. As a result of this function,
1341 * the WM_QUIT message is posted to the application, and
1342 * PostQuitMessage() returns immediately. The exitCode parameter
1343 * specifies an application-defined exit code, which appears in the
1344 * _wParam_ parameter of the WM_QUIT message posted to the application.
1346 * CONFORMANCE
1348 * ECMA-234, Win32
1350 void WINAPI PostQuitMessage( INT exitCode )
1352 MESSAGEQUEUE *queue;
1354 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return;
1355 queue->wPostQMsg = TRUE;
1356 queue->wExitCode = (WORD)exitCode;
1357 QUEUE_Unlock( queue );
1361 /***********************************************************************
1362 * GetWindowTask16 (USER.224)
1364 HTASK16 WINAPI GetWindowTask16( HWND16 hwnd )
1366 HTASK16 retvalue;
1367 WND *wndPtr = WIN_FindWndPtr( hwnd );
1369 if (!wndPtr) return 0;
1370 retvalue = QUEUE_GetQueueTask( wndPtr->hmemTaskQ );
1371 WIN_ReleaseWndPtr(wndPtr);
1372 return retvalue;
1375 /***********************************************************************
1376 * GetWindowThreadProcessId (USER32.313)
1378 DWORD WINAPI GetWindowThreadProcessId( HWND hwnd, LPDWORD process )
1380 DWORD retvalue;
1381 MESSAGEQUEUE *queue;
1383 WND *wndPtr = WIN_FindWndPtr( hwnd );
1384 if (!wndPtr) return 0;
1386 queue = QUEUE_Lock( wndPtr->hmemTaskQ );
1387 WIN_ReleaseWndPtr(wndPtr);
1389 if (!queue) return 0;
1391 if ( process ) *process = (DWORD)queue->teb->pid;
1392 retvalue = (DWORD)queue->teb->tid;
1394 QUEUE_Unlock( queue );
1395 return retvalue;
1399 /***********************************************************************
1400 * SetMessageQueue16 (USER.266)
1402 BOOL16 WINAPI SetMessageQueue16( INT16 size )
1404 return SetMessageQueue( size );
1408 /***********************************************************************
1409 * SetMessageQueue (USER32.494)
1411 BOOL WINAPI SetMessageQueue( INT size )
1413 /* now obsolete the message queue will be expanded dynamically
1414 as necessary */
1416 /* access the queue to create it if it's not existing */
1417 GetFastQueue16();
1419 return TRUE;
1422 /***********************************************************************
1423 * InitThreadInput16 (USER.409)
1425 HQUEUE16 WINAPI InitThreadInput16( WORD unknown, WORD flags )
1427 HQUEUE16 hQueue;
1428 MESSAGEQUEUE *queuePtr;
1430 TEB *teb = NtCurrentTeb();
1432 if (!teb)
1433 return 0;
1435 hQueue = teb->queue;
1437 if ( !hQueue )
1439 /* Create thread message queue */
1440 if( !(hQueue = QUEUE_CreateMsgQueue( TRUE )))
1442 ERR_(msg)("failed!\n");
1443 return FALSE;
1446 /* Link new queue into list */
1447 queuePtr = (MESSAGEQUEUE *)QUEUE_Lock( hQueue );
1448 queuePtr->teb = NtCurrentTeb();
1450 HeapLock( SystemHeap ); /* FIXME: a bit overkill */
1451 SetThreadQueue16( 0, hQueue );
1452 teb->queue = hQueue;
1454 queuePtr->next = hFirstQueue;
1455 hFirstQueue = hQueue;
1456 HeapUnlock( SystemHeap );
1458 QUEUE_Unlock( queuePtr );
1461 return hQueue;
1464 /***********************************************************************
1465 * GetQueueStatus16 (USER.334)
1467 DWORD WINAPI GetQueueStatus16( UINT16 flags )
1469 MESSAGEQUEUE *queue;
1470 DWORD ret;
1472 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1473 ret = MAKELONG( queue->changeBits, queue->wakeBits );
1474 queue->changeBits = 0;
1475 QUEUE_Unlock( queue );
1477 return ret & MAKELONG( flags, flags );
1480 /***********************************************************************
1481 * GetQueueStatus (USER32.283)
1483 DWORD WINAPI GetQueueStatus( UINT flags )
1485 MESSAGEQUEUE *queue;
1486 DWORD ret;
1488 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1489 ret = MAKELONG( queue->changeBits, queue->wakeBits );
1490 queue->changeBits = 0;
1491 QUEUE_Unlock( queue );
1493 return ret & MAKELONG( flags, flags );
1497 /***********************************************************************
1498 * GetInputState16 (USER.335)
1500 BOOL16 WINAPI GetInputState16(void)
1502 return GetInputState();
1505 /***********************************************************************
1506 * WaitForInputIdle (USER32.577)
1508 DWORD WINAPI WaitForInputIdle (HANDLE hProcess, DWORD dwTimeOut)
1510 DWORD cur_time, ret;
1511 HANDLE idle_event = -1;
1513 SERVER_START_REQ
1515 struct wait_input_idle_request *req = server_alloc_req( sizeof(*req), 0 );
1516 req->handle = hProcess;
1517 req->timeout = dwTimeOut;
1518 if (!(ret = server_call( REQ_WAIT_INPUT_IDLE ))) idle_event = req->event;
1520 SERVER_END_REQ;
1521 if (ret) return 0xffffffff; /* error */
1522 if (idle_event == -1) return 0; /* no event to wait on */
1524 cur_time = GetTickCount();
1526 TRACE_(msg)("waiting for %x\n", idle_event );
1527 while ( dwTimeOut > GetTickCount() - cur_time || dwTimeOut == INFINITE ) {
1529 ret = MsgWaitForMultipleObjects ( 1, &idle_event, FALSE, dwTimeOut, QS_SENDMESSAGE );
1530 if ( ret == ( WAIT_OBJECT_0 + 1 )) {
1531 MESSAGEQUEUE * queue;
1532 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0xFFFFFFFF;
1533 QUEUE_ReceiveMessage ( queue );
1534 QUEUE_Unlock ( queue );
1535 continue;
1537 if ( ret == WAIT_TIMEOUT || ret == 0xFFFFFFFF ) {
1538 TRACE_(msg)("timeout or error\n");
1539 return ret;
1541 else {
1542 TRACE_(msg)("finished\n");
1543 return 0;
1547 return WAIT_TIMEOUT;
1550 /***********************************************************************
1551 * GetInputState (USER32.244)
1553 BOOL WINAPI GetInputState(void)
1555 MESSAGEQUEUE *queue;
1556 BOOL ret;
1558 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() )))
1559 return FALSE;
1560 ret = queue->wakeBits & (QS_KEY | QS_MOUSEBUTTON);
1561 QUEUE_Unlock( queue );
1563 return ret;
1566 /***********************************************************************
1567 * UserYield (USER.332)
1569 void WINAPI UserYield16(void)
1571 MESSAGEQUEUE *queue;
1573 /* Handle sent messages */
1574 queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() );
1576 while (queue && (queue->wakeBits & QS_SENDMESSAGE))
1577 QUEUE_ReceiveMessage( queue );
1579 QUEUE_Unlock( queue );
1581 /* Yield */
1582 if ( THREAD_IsWin16( NtCurrentTeb() ) )
1583 OldYield16();
1584 else
1585 WIN32_OldYield16();
1587 /* Handle sent messages again */
1588 queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() );
1590 while (queue && (queue->wakeBits & QS_SENDMESSAGE))
1591 QUEUE_ReceiveMessage( queue );
1593 QUEUE_Unlock( queue );
1596 /***********************************************************************
1597 * GetMessagePos (USER.119) (USER32.272)
1599 * The GetMessagePos() function returns a long value representing a
1600 * cursor position, in screen coordinates, when the last message
1601 * retrieved by the GetMessage() function occurs. The x-coordinate is
1602 * in the low-order word of the return value, the y-coordinate is in
1603 * the high-order word. The application can use the MAKEPOINT()
1604 * macro to obtain a POINT structure from the return value.
1606 * For the current cursor position, use GetCursorPos().
1608 * RETURNS
1610 * Cursor position of last message on success, zero on failure.
1612 * CONFORMANCE
1614 * ECMA-234, Win32
1617 DWORD WINAPI GetMessagePos(void)
1619 MESSAGEQUEUE *queue;
1620 DWORD ret;
1622 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1623 ret = queue->GetMessagePosVal;
1624 QUEUE_Unlock( queue );
1626 return ret;
1630 /***********************************************************************
1631 * GetMessageTime (USER.120) (USER32.273)
1633 * GetMessageTime() returns the message time for the last message
1634 * retrieved by the function. The time is measured in milliseconds with
1635 * the same offset as GetTickCount().
1637 * Since the tick count wraps, this is only useful for moderately short
1638 * relative time comparisons.
1640 * RETURNS
1642 * Time of last message on success, zero on failure.
1644 * CONFORMANCE
1646 * ECMA-234, Win32
1649 LONG WINAPI GetMessageTime(void)
1651 MESSAGEQUEUE *queue;
1652 LONG ret;
1654 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1655 ret = queue->GetMessageTimeVal;
1656 QUEUE_Unlock( queue );
1658 return ret;
1662 /***********************************************************************
1663 * GetMessageExtraInfo (USER.288) (USER32.271)
1665 LONG WINAPI GetMessageExtraInfo(void)
1667 MESSAGEQUEUE *queue;
1668 LONG ret;
1670 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1671 ret = queue->GetMessageExtraInfoVal;
1672 QUEUE_Unlock( queue );
1674 return ret;
1678 /**********************************************************************
1679 * AttachThreadInput [USER32.8] Attaches input of 1 thread to other
1681 * Attaches the input processing mechanism of one thread to that of
1682 * another thread.
1684 * RETURNS
1685 * Success: TRUE
1686 * Failure: FALSE
1688 * TODO:
1689 * 1. Reset the Key State (currenly per thread key state is not maintained)
1691 BOOL WINAPI AttachThreadInput(
1692 DWORD idAttach, /* [in] Thread to attach */
1693 DWORD idAttachTo, /* [in] Thread to attach to */
1694 BOOL fAttach) /* [in] Attach or detach */
1696 MESSAGEQUEUE *pSrcMsgQ = 0, *pTgtMsgQ = 0;
1697 BOOL16 bRet = 0;
1699 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1701 /* A thread cannot attach to itself */
1702 if ( idAttach == idAttachTo )
1703 goto CLEANUP;
1705 /* According to the docs this method should fail if a
1706 * "Journal record" hook is installed. (attaches all input queues together)
1708 if ( HOOK_IsHooked( WH_JOURNALRECORD ) )
1709 goto CLEANUP;
1711 /* Retrieve message queues corresponding to the thread id's */
1712 pTgtMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetThreadQueue16( idAttach ) );
1713 pSrcMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetThreadQueue16( idAttachTo ) );
1715 /* Ensure we have message queues and that Src and Tgt threads
1716 * are not system threads.
1718 if ( !pSrcMsgQ || !pTgtMsgQ || !pSrcMsgQ->pQData || !pTgtMsgQ->pQData )
1719 goto CLEANUP;
1721 if (fAttach) /* Attach threads */
1723 /* Only attach if currently detached */
1724 if ( pTgtMsgQ->pQData != pSrcMsgQ->pQData )
1726 /* First release the target threads perQData */
1727 PERQDATA_Release( pTgtMsgQ->pQData );
1729 /* Share a reference to the source threads perQDATA */
1730 PERQDATA_Addref( pSrcMsgQ->pQData );
1731 pTgtMsgQ->pQData = pSrcMsgQ->pQData;
1734 else /* Detach threads */
1736 /* Only detach if currently attached */
1737 if ( pTgtMsgQ->pQData == pSrcMsgQ->pQData )
1739 /* First release the target threads perQData */
1740 PERQDATA_Release( pTgtMsgQ->pQData );
1742 /* Give the target thread its own private perQDATA once more */
1743 pTgtMsgQ->pQData = PERQDATA_CreateInstance();
1747 /* TODO: Reset the Key State */
1749 bRet = 1; /* Success */
1751 CLEANUP:
1753 /* Unlock the queues before returning */
1754 if ( pSrcMsgQ )
1755 QUEUE_Unlock( pSrcMsgQ );
1756 if ( pTgtMsgQ )
1757 QUEUE_Unlock( pTgtMsgQ );
1759 return bRet;