Removed some unnecessary #includes and dll dependencies.
[wine/multimedia.git] / windows / queue.c
blob34e95ae30a4f6e75928e45211bb5d6214cf13966
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 "syslevel.h"
15 #include "module.h"
16 #include "queue.h"
17 #include "task.h"
18 #include "win.h"
19 #include "clipboard.h"
20 #include "hook.h"
21 #include "heap.h"
22 #include "thread.h"
23 #include "debugtools.h"
24 #include "server.h"
25 #include "spy.h"
27 DECLARE_DEBUG_CHANNEL(msg);
28 DECLARE_DEBUG_CHANNEL(sendmsg);
30 #define MAX_QUEUE_SIZE 120 /* Max. size of a message queue */
32 static HQUEUE16 hFirstQueue = 0;
33 static HQUEUE16 hExitingQueue = 0;
34 static HQUEUE16 hmemSysMsgQueue = 0;
35 static MESSAGEQUEUE *sysMsgQueue = NULL;
36 static PERQUEUEDATA *pQDataWin16 = NULL; /* Global perQData for Win16 tasks */
38 static MESSAGEQUEUE *pMouseQueue = NULL; /* Queue for last mouse message */
39 static MESSAGEQUEUE *pKbdQueue = NULL; /* Queue for last kbd message */
41 HQUEUE16 hCursorQueue = 0;
42 HQUEUE16 hActiveQueue = 0;
45 /***********************************************************************
46 * PERQDATA_CreateInstance
48 * Creates an instance of a reference counted PERQUEUEDATA element
49 * for the message queue. perQData is stored globally for 16 bit tasks.
51 * Note: We don't implement perQdata exactly the same way Windows does.
52 * Each perQData element is reference counted since it may be potentially
53 * shared by multiple message Queues (via AttachThreadInput).
54 * We only store the current values for Active, Capture and focus windows
55 * currently.
57 PERQUEUEDATA * PERQDATA_CreateInstance( )
59 PERQUEUEDATA *pQData;
61 BOOL16 bIsWin16 = 0;
63 TRACE_(msg)("()\n");
65 /* Share a single instance of perQData for all 16 bit tasks */
66 if ( ( bIsWin16 = THREAD_IsWin16( NtCurrentTeb() ) ) )
68 /* If previously allocated, just bump up ref count */
69 if ( pQDataWin16 )
71 PERQDATA_Addref( pQDataWin16 );
72 return pQDataWin16;
76 /* Allocate PERQUEUEDATA from the system heap */
77 if (!( pQData = (PERQUEUEDATA *) HeapAlloc( SystemHeap, 0,
78 sizeof(PERQUEUEDATA) ) ))
79 return 0;
81 /* Initialize */
82 pQData->hWndCapture = pQData->hWndFocus = pQData->hWndActive = 0;
83 pQData->ulRefCount = 1;
84 pQData->nCaptureHT = HTCLIENT;
86 /* Note: We have an independent critical section for the per queue data
87 * since this may be shared by different threads. see AttachThreadInput()
89 InitializeCriticalSection( &pQData->cSection );
90 /* FIXME: not all per queue data critical sections should be global */
91 MakeCriticalSectionGlobal( &pQData->cSection );
93 /* Save perQData globally for 16 bit tasks */
94 if ( bIsWin16 )
95 pQDataWin16 = pQData;
97 return pQData;
101 /***********************************************************************
102 * PERQDATA_Addref
104 * Increment reference count for the PERQUEUEDATA instance
105 * Returns reference count for debugging purposes
107 ULONG PERQDATA_Addref( PERQUEUEDATA *pQData )
109 assert(pQData != 0 );
110 TRACE_(msg)("(): current refcount %lu ...\n", pQData->ulRefCount);
112 EnterCriticalSection( &pQData->cSection );
113 ++pQData->ulRefCount;
114 LeaveCriticalSection( &pQData->cSection );
116 return pQData->ulRefCount;
120 /***********************************************************************
121 * PERQDATA_Release
123 * Release a reference to a PERQUEUEDATA instance.
124 * Destroy the instance if no more references exist
125 * Returns reference count for debugging purposes
127 ULONG PERQDATA_Release( PERQUEUEDATA *pQData )
129 assert(pQData != 0 );
130 TRACE_(msg)("(): current refcount %lu ...\n",
131 (LONG)pQData->ulRefCount );
133 EnterCriticalSection( &pQData->cSection );
134 if ( --pQData->ulRefCount == 0 )
136 LeaveCriticalSection( &pQData->cSection );
137 DeleteCriticalSection( &pQData->cSection );
139 TRACE_(msg)("(): deleting PERQUEUEDATA instance ...\n" );
141 /* Deleting our global 16 bit perQData? */
142 if ( pQData == pQDataWin16 )
143 pQDataWin16 = 0;
145 /* Free the PERQUEUEDATA instance */
146 HeapFree( SystemHeap, 0, pQData );
148 return 0;
150 LeaveCriticalSection( &pQData->cSection );
152 return pQData->ulRefCount;
156 /***********************************************************************
157 * PERQDATA_GetFocusWnd
159 * Get the focus hwnd member in a threadsafe manner
161 HWND PERQDATA_GetFocusWnd( PERQUEUEDATA *pQData )
163 HWND hWndFocus;
164 assert(pQData != 0 );
166 EnterCriticalSection( &pQData->cSection );
167 hWndFocus = pQData->hWndFocus;
168 LeaveCriticalSection( &pQData->cSection );
170 return hWndFocus;
174 /***********************************************************************
175 * PERQDATA_SetFocusWnd
177 * Set the focus hwnd member in a threadsafe manner
179 HWND PERQDATA_SetFocusWnd( PERQUEUEDATA *pQData, HWND hWndFocus )
181 HWND hWndFocusPrv;
182 assert(pQData != 0 );
184 EnterCriticalSection( &pQData->cSection );
185 hWndFocusPrv = pQData->hWndFocus;
186 pQData->hWndFocus = hWndFocus;
187 LeaveCriticalSection( &pQData->cSection );
189 return hWndFocusPrv;
193 /***********************************************************************
194 * PERQDATA_GetActiveWnd
196 * Get the active hwnd member in a threadsafe manner
198 HWND PERQDATA_GetActiveWnd( PERQUEUEDATA *pQData )
200 HWND hWndActive;
201 assert(pQData != 0 );
203 EnterCriticalSection( &pQData->cSection );
204 hWndActive = pQData->hWndActive;
205 LeaveCriticalSection( &pQData->cSection );
207 return hWndActive;
211 /***********************************************************************
212 * PERQDATA_SetActiveWnd
214 * Set the active focus hwnd member in a threadsafe manner
216 HWND PERQDATA_SetActiveWnd( PERQUEUEDATA *pQData, HWND hWndActive )
218 HWND hWndActivePrv;
219 assert(pQData != 0 );
221 EnterCriticalSection( &pQData->cSection );
222 hWndActivePrv = pQData->hWndActive;
223 pQData->hWndActive = hWndActive;
224 LeaveCriticalSection( &pQData->cSection );
226 return hWndActivePrv;
230 /***********************************************************************
231 * PERQDATA_GetCaptureWnd
233 * Get the capture hwnd member in a threadsafe manner
235 HWND PERQDATA_GetCaptureWnd( PERQUEUEDATA *pQData )
237 HWND hWndCapture;
238 assert(pQData != 0 );
240 EnterCriticalSection( &pQData->cSection );
241 hWndCapture = pQData->hWndCapture;
242 LeaveCriticalSection( &pQData->cSection );
244 return hWndCapture;
248 /***********************************************************************
249 * PERQDATA_SetCaptureWnd
251 * Set the capture hwnd member in a threadsafe manner
253 HWND PERQDATA_SetCaptureWnd( PERQUEUEDATA *pQData, HWND hWndCapture )
255 HWND hWndCapturePrv;
256 assert(pQData != 0 );
258 EnterCriticalSection( &pQData->cSection );
259 hWndCapturePrv = pQData->hWndCapture;
260 pQData->hWndCapture = hWndCapture;
261 LeaveCriticalSection( &pQData->cSection );
263 return hWndCapturePrv;
267 /***********************************************************************
268 * PERQDATA_GetCaptureInfo
270 * Get the capture info member in a threadsafe manner
272 INT16 PERQDATA_GetCaptureInfo( PERQUEUEDATA *pQData )
274 INT16 nCaptureHT;
275 assert(pQData != 0 );
277 EnterCriticalSection( &pQData->cSection );
278 nCaptureHT = pQData->nCaptureHT;
279 LeaveCriticalSection( &pQData->cSection );
281 return nCaptureHT;
285 /***********************************************************************
286 * PERQDATA_SetCaptureInfo
288 * Set the capture info member in a threadsafe manner
290 INT16 PERQDATA_SetCaptureInfo( PERQUEUEDATA *pQData, INT16 nCaptureHT )
292 INT16 nCaptureHTPrv;
293 assert(pQData != 0 );
295 EnterCriticalSection( &pQData->cSection );
296 nCaptureHTPrv = pQData->nCaptureHT;
297 pQData->nCaptureHT = nCaptureHT;
298 LeaveCriticalSection( &pQData->cSection );
300 return nCaptureHTPrv;
304 /***********************************************************************
305 * QUEUE_Lock
307 * Function for getting a 32 bit pointer on queue strcture. For thread
308 * safeness programmers should use this function instead of GlobalLock to
309 * retrieve a pointer on the structure. QUEUE_Unlock should also be called
310 * when access to the queue structure is not required anymore.
312 MESSAGEQUEUE *QUEUE_Lock( HQUEUE16 hQueue )
314 MESSAGEQUEUE *queue;
316 HeapLock( SystemHeap ); /* FIXME: a bit overkill */
317 queue = GlobalLock16( hQueue );
318 if ( !queue || (queue->magic != QUEUE_MAGIC) )
320 HeapUnlock( SystemHeap );
321 return NULL;
324 queue->lockCount++;
325 HeapUnlock( SystemHeap );
326 return queue;
330 /***********************************************************************
331 * QUEUE_Unlock
333 * Use with QUEUE_Lock to get a thread safe access to message queue
334 * structure
336 void QUEUE_Unlock( MESSAGEQUEUE *queue )
338 if (queue)
340 HeapLock( SystemHeap ); /* FIXME: a bit overkill */
342 if ( --queue->lockCount == 0 )
344 DeleteCriticalSection ( &queue->cSection );
345 if (queue->server_queue)
346 CloseHandle( queue->server_queue );
347 GlobalFree16( queue->self );
350 HeapUnlock( SystemHeap );
355 /***********************************************************************
356 * QUEUE_DumpQueue
358 void QUEUE_DumpQueue( HQUEUE16 hQueue )
360 MESSAGEQUEUE *pq;
362 if (!(pq = (MESSAGEQUEUE*) QUEUE_Lock( hQueue )) )
364 WARN_(msg)("%04x is not a queue handle\n", hQueue );
365 return;
368 DPRINTF( "next: %12.4x Intertask SendMessage:\n"
369 "thread: %10p ----------------------\n"
370 "firstMsg: %8p smWaiting: %10p\n"
371 "lastMsg: %8p smPending: %10p\n"
372 "msgCount: %8.4x smProcessing: %10p\n"
373 "lockCount: %7.4x\n"
374 "wWinVer: %9.4x\n"
375 "paints: %10.4x\n"
376 "timers: %10.4x\n"
377 "wakeBits: %8.4x\n"
378 "wakeMask: %8.4x\n"
379 "hCurHook: %8.4x\n",
380 pq->next, pq->teb, pq->firstMsg, pq->smWaiting, pq->lastMsg,
381 pq->smPending, pq->msgCount, pq->smProcessing,
382 (unsigned)pq->lockCount, pq->wWinVersion,
383 pq->wPaintCount, pq->wTimerCount,
384 pq->wakeBits, pq->wakeMask, pq->hCurHook);
386 QUEUE_Unlock( pq );
390 /***********************************************************************
391 * QUEUE_WalkQueues
393 void QUEUE_WalkQueues(void)
395 char module[10];
396 HQUEUE16 hQueue = hFirstQueue;
398 DPRINTF( "Queue Msgs Thread Task Module\n" );
399 while (hQueue)
401 MESSAGEQUEUE *queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue );
402 if (!queue)
404 WARN_(msg)("Bad queue handle %04x\n", hQueue );
405 return;
407 if (!GetModuleName16( queue->teb->htask16, module, sizeof(module )))
408 strcpy( module, "???" );
409 DPRINTF( "%04x %4d %p %04x %s\n", hQueue,queue->msgCount,
410 queue->teb, queue->teb->htask16, module );
411 hQueue = queue->next;
412 QUEUE_Unlock( queue );
414 DPRINTF( "\n" );
418 /***********************************************************************
419 * QUEUE_IsExitingQueue
421 BOOL QUEUE_IsExitingQueue( HQUEUE16 hQueue )
423 return (hExitingQueue && (hQueue == hExitingQueue));
427 /***********************************************************************
428 * QUEUE_SetExitingQueue
430 void QUEUE_SetExitingQueue( HQUEUE16 hQueue )
432 hExitingQueue = hQueue;
436 /***********************************************************************
437 * QUEUE_CreateMsgQueue
439 * Creates a message queue. Doesn't link it into queue list!
441 static HQUEUE16 QUEUE_CreateMsgQueue( BOOL16 bCreatePerQData )
443 HQUEUE16 hQueue;
444 MESSAGEQUEUE * msgQueue;
445 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
446 struct get_msg_queue_request *req = get_req_buffer();
448 TRACE_(msg)("(): Creating message queue...\n");
450 if (!(hQueue = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT,
451 sizeof(MESSAGEQUEUE) )))
452 return 0;
454 msgQueue = (MESSAGEQUEUE *) GlobalLock16( hQueue );
455 if ( !msgQueue )
456 return 0;
458 if (server_call( REQ_GET_MSG_QUEUE ))
460 ERR_(msg)("Cannot get thread queue");
461 GlobalFree16( hQueue );
462 return 0;
464 msgQueue->server_queue = req->handle;
465 msgQueue->server_queue = ConvertToGlobalHandle( msgQueue->server_queue );
467 msgQueue->self = hQueue;
468 msgQueue->wakeBits = msgQueue->changeBits = 0;
469 msgQueue->wWinVersion = pTask ? pTask->version : 0;
471 InitializeCriticalSection( &msgQueue->cSection );
472 MakeCriticalSectionGlobal( &msgQueue->cSection );
474 msgQueue->lockCount = 1;
475 msgQueue->magic = QUEUE_MAGIC;
477 /* Create and initialize our per queue data */
478 msgQueue->pQData = bCreatePerQData ? PERQDATA_CreateInstance() : NULL;
480 return hQueue;
484 /***********************************************************************
485 * QUEUE_FlushMessage
487 * Try to reply to all pending sent messages on exit.
489 static void QUEUE_FlushMessages( MESSAGEQUEUE *queue )
491 SMSG *smsg;
492 MESSAGEQUEUE *senderQ = 0;
494 if( queue )
496 EnterCriticalSection( &queue->cSection );
498 /* empty the list of pending SendMessage waiting to be received */
499 while (queue->smPending)
501 smsg = QUEUE_RemoveSMSG( queue, SM_PENDING_LIST, 0);
503 senderQ = (MESSAGEQUEUE*)QUEUE_Lock( smsg->hSrcQueue );
504 if ( !senderQ )
505 continue;
507 /* return 0, to unblock other thread */
508 smsg->lResult = 0;
509 smsg->flags |= SMSG_HAVE_RESULT;
510 QUEUE_SetWakeBit( senderQ, QS_SMRESULT);
512 QUEUE_Unlock( senderQ );
515 QUEUE_ClearWakeBit( queue, QS_SENDMESSAGE );
517 LeaveCriticalSection( &queue->cSection );
522 /***********************************************************************
523 * QUEUE_DeleteMsgQueue
525 * Unlinks and deletes a message queue.
527 * Note: We need to mask asynchronous events to make sure PostMessage works
528 * even in the signal handler.
530 BOOL QUEUE_DeleteMsgQueue( HQUEUE16 hQueue )
532 MESSAGEQUEUE * msgQueue = (MESSAGEQUEUE*)QUEUE_Lock(hQueue);
533 HQUEUE16 *pPrev;
535 TRACE_(msg)("(): Deleting message queue %04x\n", hQueue);
537 if (!hQueue || !msgQueue)
539 WARN_(msg)("invalid argument.\n");
540 return 0;
543 msgQueue->magic = 0;
545 if( hCursorQueue == hQueue ) hCursorQueue = 0;
546 if( hActiveQueue == hQueue ) hActiveQueue = 0;
548 /* flush sent messages */
549 QUEUE_FlushMessages( msgQueue );
551 HeapLock( SystemHeap ); /* FIXME: a bit overkill */
553 /* Release per queue data if present */
554 if ( msgQueue->pQData )
556 PERQDATA_Release( msgQueue->pQData );
557 msgQueue->pQData = 0;
560 /* remove the message queue from the global link list */
561 pPrev = &hFirstQueue;
562 while (*pPrev && (*pPrev != hQueue))
564 MESSAGEQUEUE *msgQ = (MESSAGEQUEUE*)GlobalLock16(*pPrev);
566 /* sanity check */
567 if ( !msgQ || (msgQ->magic != QUEUE_MAGIC) )
569 /* HQUEUE link list is corrupted, try to exit gracefully */
570 WARN_(msg)("HQUEUE link list corrupted!\n");
571 pPrev = 0;
572 break;
574 pPrev = &msgQ->next;
576 if (pPrev && *pPrev) *pPrev = msgQueue->next;
577 msgQueue->self = 0;
579 HeapUnlock( SystemHeap );
581 /* free up resource used by MESSAGEQUEUE strcture */
582 msgQueue->lockCount--;
583 QUEUE_Unlock( msgQueue );
585 return 1;
589 /***********************************************************************
590 * QUEUE_CreateSysMsgQueue
592 * Create the system message queue, and set the double-click speed.
593 * Must be called only once.
595 BOOL QUEUE_CreateSysMsgQueue( int size )
597 /* Note: We dont need perQ data for the system message queue */
598 if (!(hmemSysMsgQueue = QUEUE_CreateMsgQueue( FALSE )))
599 return FALSE;
601 sysMsgQueue = (MESSAGEQUEUE *) GlobalLock16( hmemSysMsgQueue );
602 return TRUE;
606 /***********************************************************************
607 * QUEUE_GetSysQueue
609 MESSAGEQUEUE *QUEUE_GetSysQueue(void)
611 return sysMsgQueue;
615 /***********************************************************************
616 * QUEUE_SetWakeBit
618 * See "Windows Internals", p.449
620 void QUEUE_SetWakeBit( MESSAGEQUEUE *queue, WORD bit )
622 TRACE_(msg)("queue = %04x (wm=%04x), bit = %04x\n",
623 queue->self, queue->wakeMask, bit );
625 if (bit & QS_MOUSE) pMouseQueue = queue;
626 if (bit & QS_KEY) pKbdQueue = queue;
627 queue->changeBits |= bit;
628 queue->wakeBits |= bit;
629 if (queue->wakeMask & bit)
631 queue->wakeMask = 0;
633 /* Wake up thread waiting for message */
634 if ( THREAD_IsWin16( queue->teb ) )
636 int iWndsLock = WIN_SuspendWndsLock();
637 PostEvent16( queue->teb->htask16 );
638 WIN_RestoreWndsLock( iWndsLock );
640 else
642 struct wake_queue_request *req = get_req_buffer();
643 req->handle = queue->server_queue;
644 req->bits = bit;
645 server_call( REQ_WAKE_QUEUE );
651 /***********************************************************************
652 * QUEUE_ClearWakeBit
654 void QUEUE_ClearWakeBit( MESSAGEQUEUE *queue, WORD bit )
656 queue->changeBits &= ~bit;
657 queue->wakeBits &= ~bit;
661 /***********************************************************************
662 * QUEUE_WaitBits
664 * See "Windows Internals", p.447
666 * return values:
667 * 0 if exit with timeout
668 * 1 otherwise
670 int QUEUE_WaitBits( WORD bits, DWORD timeout )
672 MESSAGEQUEUE *queue;
673 DWORD curTime = 0;
674 HQUEUE16 hQueue;
676 TRACE_(msg)("q %04x waiting for %04x\n", GetFastQueue16(), bits);
678 if ( THREAD_IsWin16( NtCurrentTeb() ) && (timeout != INFINITE) )
679 curTime = GetTickCount();
681 hQueue = GetFastQueue16();
682 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return 0;
684 for (;;)
686 if (queue->changeBits & bits)
688 /* One of the bits is set; we can return */
689 queue->wakeMask = 0;
690 QUEUE_Unlock( queue );
691 return 1;
693 if (queue->wakeBits & QS_SENDMESSAGE)
695 /* Process the sent message immediately */
697 queue->wakeMask = 0;
698 QUEUE_ReceiveMessage( queue );
699 continue; /* nested sm crux */
702 queue->wakeMask = bits | QS_SENDMESSAGE;
703 if(queue->changeBits & bits)
705 continue;
708 TRACE_(msg)("%04x) wakeMask is %04x, waiting\n", queue->self, queue->wakeMask);
710 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
712 BOOL bHasWin16Lock;
713 DWORD dwlc;
715 if ( (bHasWin16Lock = _ConfirmWin16Lock()) )
717 TRACE_(msg)("bHasWin16Lock=TRUE\n");
718 ReleaseThunkLock( &dwlc );
721 WaitForSingleObject( queue->server_queue, timeout );
723 if ( bHasWin16Lock )
725 RestoreThunkLock( dwlc );
728 else
730 if ( timeout == INFINITE )
731 WaitEvent16( 0 ); /* win 16 thread, use WaitEvent */
732 else
734 /* check for timeout, then give control to other tasks */
735 if (GetTickCount() - curTime > timeout)
738 QUEUE_Unlock( queue );
739 return 0; /* exit with timeout */
741 Yield16();
748 /***********************************************************************
749 * QUEUE_AddSMSG
751 * This routine is called when a SMSG need to be added to one of the three
752 * SM list. (SM_PROCESSING_LIST, SM_PENDING_LIST, SM_WAITING_LIST)
754 BOOL QUEUE_AddSMSG( MESSAGEQUEUE *queue, int list, SMSG *smsg )
756 TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
757 smsg, SPY_GetMsgName(smsg->msg));
759 switch (list)
761 case SM_PROCESSING_LIST:
762 /* don't need to be thread safe, only accessed by the
763 thread associated with the sender queue */
764 smsg->nextProcessing = queue->smProcessing;
765 queue->smProcessing = smsg;
766 break;
768 case SM_WAITING_LIST:
769 /* don't need to be thread safe, only accessed by the
770 thread associated with the receiver queue */
771 smsg->nextWaiting = queue->smWaiting;
772 queue->smWaiting = smsg;
773 break;
775 case SM_PENDING_LIST:
777 /* make it thread safe, could be accessed by the sender and
778 receiver thread */
779 SMSG **prev;
781 EnterCriticalSection( &queue->cSection );
782 smsg->nextPending = NULL;
783 prev = &queue->smPending;
784 while ( *prev )
785 prev = &(*prev)->nextPending;
786 *prev = smsg;
787 LeaveCriticalSection( &queue->cSection );
789 QUEUE_SetWakeBit( queue, QS_SENDMESSAGE );
790 break;
793 default:
794 WARN_(sendmsg)("Invalid list: %d", list);
795 break;
798 return TRUE;
802 /***********************************************************************
803 * QUEUE_RemoveSMSG
805 * This routine is called when a SMSG need to be remove from one of the three
806 * SM list. (SM_PROCESSING_LIST, SM_PENDING_LIST, SM_WAITING_LIST)
807 * If smsg == 0, remove the first smsg from the specified list
809 SMSG *QUEUE_RemoveSMSG( MESSAGEQUEUE *queue, int list, SMSG *smsg )
812 switch (list)
814 case SM_PROCESSING_LIST:
815 /* don't need to be thread safe, only accessed by the
816 thread associated with the sender queue */
818 /* if smsg is equal to null, it means the first in the list */
819 if (!smsg)
820 smsg = queue->smProcessing;
822 TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
823 smsg, SPY_GetMsgName(smsg->msg));
824 /* In fact SM_PROCESSING_LIST is a stack, and smsg
825 should be always at the top of the list */
826 if ( (smsg != queue->smProcessing) || !queue->smProcessing )
828 ERR_(sendmsg)("smsg not at the top of Processing list, smsg=0x%p queue=0x%p", smsg, queue);
829 return 0;
831 else
833 queue->smProcessing = smsg->nextProcessing;
834 smsg->nextProcessing = 0;
836 return smsg;
838 case SM_WAITING_LIST:
839 /* don't need to be thread safe, only accessed by the
840 thread associated with the receiver queue */
842 /* if smsg is equal to null, it means the first in the list */
843 if (!smsg)
844 smsg = queue->smWaiting;
846 TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
847 smsg, SPY_GetMsgName(smsg->msg));
848 /* In fact SM_WAITING_LIST is a stack, and smsg
849 should be always at the top of the list */
850 if ( (smsg != queue->smWaiting) || !queue->smWaiting )
852 ERR_(sendmsg)("smsg not at the top of Waiting list, smsg=0x%p queue=0x%p", smsg, queue);
853 return 0;
855 else
857 queue->smWaiting = smsg->nextWaiting;
858 smsg->nextWaiting = 0;
860 return smsg;
862 case SM_PENDING_LIST:
863 /* make it thread safe, could be accessed by the sender and
864 receiver thread */
865 EnterCriticalSection( &queue->cSection );
867 if (!smsg || !queue->smPending)
868 smsg = queue->smPending;
869 else
871 ERR_(sendmsg)("should always remove the top one in Pending list, smsg=0x%p queue=0x%p", smsg, queue);
872 LeaveCriticalSection( &queue->cSection );
873 return 0;
876 TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
877 smsg, SPY_GetMsgName(smsg->msg));
879 queue->smPending = smsg->nextPending;
880 smsg->nextPending = 0;
882 /* if no more SMSG in Pending list, clear QS_SENDMESSAGE flag */
883 if (!queue->smPending)
884 QUEUE_ClearWakeBit( queue, QS_SENDMESSAGE );
886 LeaveCriticalSection( &queue->cSection );
887 return smsg;
889 default:
890 WARN_(sendmsg)("Invalid list: %d", list);
891 break;
894 return 0;
898 /***********************************************************************
899 * QUEUE_ReceiveMessage
901 * This routine is called when a sent message is waiting for the queue.
903 void QUEUE_ReceiveMessage( MESSAGEQUEUE *queue )
905 LRESULT result = 0;
906 SMSG *smsg;
907 MESSAGEQUEUE *senderQ;
909 TRACE_(sendmsg)("queue %04x\n", queue->self );
911 if ( !(queue->wakeBits & QS_SENDMESSAGE) && queue->smPending )
913 TRACE_(sendmsg)("\trcm: nothing to do\n");
914 return;
917 /* remove smsg on the top of the pending list and put it in the processing list */
918 smsg = QUEUE_RemoveSMSG(queue, SM_PENDING_LIST, 0);
919 QUEUE_AddSMSG(queue, SM_WAITING_LIST, smsg);
921 TRACE_(sendmsg)("RM: %s [%04x] (%04x -> %04x)\n",
922 SPY_GetMsgName(smsg->msg), smsg->msg, smsg->hSrcQueue, smsg->hDstQueue );
924 if (IsWindow( smsg->hWnd ))
926 WND *wndPtr = WIN_FindWndPtr( smsg->hWnd );
927 DWORD extraInfo = queue->GetMessageExtraInfoVal; /* save ExtraInfo */
929 /* use sender queue extra info value while calling the window proc */
930 senderQ = (MESSAGEQUEUE*)QUEUE_Lock( smsg->hSrcQueue );
931 if (senderQ)
933 queue->GetMessageExtraInfoVal = senderQ->GetMessageExtraInfoVal;
934 QUEUE_Unlock( senderQ );
937 /* call the right version of CallWindowProcXX */
938 if (smsg->flags & SMSG_WIN32)
940 TRACE_(sendmsg)("\trcm: msg is Win32\n" );
941 if (smsg->flags & SMSG_UNICODE)
942 result = CallWindowProcW( wndPtr->winproc,
943 smsg->hWnd, smsg->msg,
944 smsg->wParam, smsg->lParam );
945 else
946 result = CallWindowProcA( wndPtr->winproc,
947 smsg->hWnd, smsg->msg,
948 smsg->wParam, smsg->lParam );
950 else /* Win16 message */
951 result = CallWindowProc16( (WNDPROC16)wndPtr->winproc,
952 (HWND16) smsg->hWnd,
953 (UINT16) smsg->msg,
954 LOWORD (smsg->wParam),
955 smsg->lParam );
957 queue->GetMessageExtraInfoVal = extraInfo; /* Restore extra info */
958 WIN_ReleaseWndPtr(wndPtr);
959 TRACE_(sendmsg)("result = %08x\n", (unsigned)result );
961 else WARN_(sendmsg)("\trcm: bad hWnd\n");
964 /* set SMSG_SENDING_REPLY flag to tell ReplyMessage16, it's not
965 an early reply */
966 smsg->flags |= SMSG_SENDING_REPLY;
967 ReplyMessage( result );
969 TRACE_(sendmsg)("done! \n" );
974 /***********************************************************************
975 * QUEUE_AddMsg
977 * Add a message to the queue. Return FALSE if queue is full.
979 BOOL QUEUE_AddMsg( HQUEUE16 hQueue, int type, MSG *msg, DWORD extraInfo )
981 MESSAGEQUEUE *msgQueue;
982 QMSG *qmsg;
985 if (!(msgQueue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return FALSE;
987 /* allocate new message in global heap for now */
988 if (!(qmsg = (QMSG *) HeapAlloc( SystemHeap, 0, sizeof(QMSG) ) ))
990 QUEUE_Unlock( msgQueue );
991 return 0;
994 EnterCriticalSection( &msgQueue->cSection );
996 /* Store message */
997 qmsg->type = type;
998 qmsg->msg = *msg;
999 qmsg->extraInfo = extraInfo;
1001 /* insert the message in the link list */
1002 qmsg->nextMsg = 0;
1003 qmsg->prevMsg = msgQueue->lastMsg;
1005 if (msgQueue->lastMsg)
1006 msgQueue->lastMsg->nextMsg = qmsg;
1008 /* update first and last anchor in message queue */
1009 msgQueue->lastMsg = qmsg;
1010 if (!msgQueue->firstMsg)
1011 msgQueue->firstMsg = qmsg;
1013 msgQueue->msgCount++;
1015 LeaveCriticalSection( &msgQueue->cSection );
1017 QUEUE_SetWakeBit( msgQueue, QS_POSTMESSAGE );
1018 QUEUE_Unlock( msgQueue );
1020 return TRUE;
1025 /***********************************************************************
1026 * QUEUE_FindMsg
1028 * Find a message matching the given parameters. Return -1 if none available.
1030 QMSG* QUEUE_FindMsg( MESSAGEQUEUE * msgQueue, HWND hwnd, int first, int last )
1032 QMSG* qmsg;
1034 EnterCriticalSection( &msgQueue->cSection );
1036 if (!msgQueue->msgCount)
1037 qmsg = 0;
1038 else if (!hwnd && !first && !last)
1039 qmsg = msgQueue->firstMsg;
1040 else
1042 /* look in linked list for message matching first and last criteria */
1043 for (qmsg = msgQueue->firstMsg; qmsg; qmsg = qmsg->nextMsg)
1045 MSG *msg = &(qmsg->msg);
1047 if (!hwnd || (msg->hwnd == hwnd))
1049 if (!first && !last)
1050 break; /* found it */
1052 if ((msg->message >= first) && (!last || (msg->message <= last)))
1053 break; /* found it */
1058 LeaveCriticalSection( &msgQueue->cSection );
1060 return qmsg;
1065 /***********************************************************************
1066 * QUEUE_RemoveMsg
1068 * Remove a message from the queue (pos must be a valid position).
1070 void QUEUE_RemoveMsg( MESSAGEQUEUE * msgQueue, QMSG *qmsg )
1072 EnterCriticalSection( &msgQueue->cSection );
1074 /* set the linked list */
1075 if (qmsg->prevMsg)
1076 qmsg->prevMsg->nextMsg = qmsg->nextMsg;
1078 if (qmsg->nextMsg)
1079 qmsg->nextMsg->prevMsg = qmsg->prevMsg;
1081 if (msgQueue->firstMsg == qmsg)
1082 msgQueue->firstMsg = qmsg->nextMsg;
1084 if (msgQueue->lastMsg == qmsg)
1085 msgQueue->lastMsg = qmsg->prevMsg;
1087 /* deallocate the memory for the message */
1088 HeapFree( SystemHeap, 0, qmsg );
1090 msgQueue->msgCount--;
1091 if (!msgQueue->msgCount) msgQueue->wakeBits &= ~QS_POSTMESSAGE;
1093 LeaveCriticalSection( &msgQueue->cSection );
1097 /***********************************************************************
1098 * QUEUE_WakeSomeone
1100 * Wake a queue upon reception of a hardware event.
1102 static void QUEUE_WakeSomeone( UINT message )
1104 WND* wndPtr = NULL;
1105 WORD wakeBit;
1106 HWND hwnd;
1107 HQUEUE16 hQueue = 0;
1108 MESSAGEQUEUE *queue = NULL;
1110 if (hCursorQueue)
1111 hQueue = hCursorQueue;
1113 if( (message >= WM_KEYFIRST) && (message <= WM_KEYLAST) )
1115 wakeBit = QS_KEY;
1116 if( hActiveQueue )
1117 hQueue = hActiveQueue;
1119 else
1121 wakeBit = (message == WM_MOUSEMOVE) ? QS_MOUSEMOVE : QS_MOUSEBUTTON;
1122 if( (hwnd = GetCapture()) )
1123 if( (wndPtr = WIN_FindWndPtr( hwnd )) )
1125 hQueue = wndPtr->hmemTaskQ;
1126 WIN_ReleaseWndPtr(wndPtr);
1130 if( (hwnd = GetSysModalWindow16()) )
1132 if( (wndPtr = WIN_FindWndPtr( hwnd )) )
1134 hQueue = wndPtr->hmemTaskQ;
1135 WIN_ReleaseWndPtr(wndPtr);
1139 if (hQueue)
1140 queue = QUEUE_Lock( hQueue );
1142 if( !queue )
1144 queue = QUEUE_Lock( hFirstQueue );
1145 while( queue )
1147 if (queue->wakeMask & wakeBit) break;
1149 QUEUE_Unlock(queue);
1150 queue = QUEUE_Lock( queue->next );
1152 if( !queue )
1154 WARN_(msg)("couldn't find queue\n");
1155 return;
1159 QUEUE_SetWakeBit( queue, wakeBit );
1161 QUEUE_Unlock( queue );
1165 /***********************************************************************
1166 * hardware_event
1168 * Add an event to the system message queue.
1169 * Note: the position is relative to the desktop window.
1171 void hardware_event( UINT message, WPARAM wParam, LPARAM lParam,
1172 int xPos, int yPos, DWORD time, DWORD extraInfo )
1174 MSG *msg;
1175 QMSG *qmsg;
1176 int mergeMsg = 0;
1178 if (!sysMsgQueue) return;
1180 EnterCriticalSection( &sysMsgQueue->cSection );
1182 /* Merge with previous event if possible */
1183 qmsg = sysMsgQueue->lastMsg;
1185 if ((message == WM_MOUSEMOVE) && sysMsgQueue->lastMsg)
1187 msg = &(sysMsgQueue->lastMsg->msg);
1189 if ((msg->message == message) && (msg->wParam == wParam))
1191 /* Merge events */
1192 qmsg = sysMsgQueue->lastMsg;
1193 mergeMsg = 1;
1197 if (!mergeMsg)
1199 /* Should I limit the number of message in
1200 the system message queue??? */
1202 /* Don't merge allocate a new msg in the global heap */
1204 if (!(qmsg = (QMSG *) HeapAlloc( SystemHeap, 0, sizeof(QMSG) ) ))
1206 LeaveCriticalSection( &sysMsgQueue->cSection );
1207 return;
1210 /* put message at the end of the linked list */
1211 qmsg->nextMsg = 0;
1212 qmsg->prevMsg = sysMsgQueue->lastMsg;
1214 if (sysMsgQueue->lastMsg)
1215 sysMsgQueue->lastMsg->nextMsg = qmsg;
1217 /* set last and first anchor index in system message queue */
1218 sysMsgQueue->lastMsg = qmsg;
1219 if (!sysMsgQueue->firstMsg)
1220 sysMsgQueue->firstMsg = qmsg;
1222 sysMsgQueue->msgCount++;
1225 /* Store message */
1226 msg = &(qmsg->msg);
1227 msg->hwnd = 0;
1228 msg->message = message;
1229 msg->wParam = wParam;
1230 msg->lParam = lParam;
1231 msg->time = time;
1232 msg->pt.x = xPos;
1233 msg->pt.y = yPos;
1234 qmsg->extraInfo = extraInfo;
1235 qmsg->type = QMSG_HARDWARE;
1237 LeaveCriticalSection( &sysMsgQueue->cSection );
1239 QUEUE_WakeSomeone( message );
1243 /***********************************************************************
1244 * QUEUE_GetQueueTask
1246 HTASK16 QUEUE_GetQueueTask( HQUEUE16 hQueue )
1248 HTASK16 hTask = 0;
1250 MESSAGEQUEUE *queue = QUEUE_Lock( hQueue );
1252 if (queue)
1254 hTask = queue->teb->htask16;
1255 QUEUE_Unlock( queue );
1258 return hTask;
1263 /***********************************************************************
1264 * QUEUE_IncPaintCount
1266 void QUEUE_IncPaintCount( HQUEUE16 hQueue )
1268 MESSAGEQUEUE *queue;
1270 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1271 queue->wPaintCount++;
1272 QUEUE_SetWakeBit( queue, QS_PAINT );
1273 QUEUE_Unlock( queue );
1277 /***********************************************************************
1278 * QUEUE_DecPaintCount
1280 void QUEUE_DecPaintCount( HQUEUE16 hQueue )
1282 MESSAGEQUEUE *queue;
1284 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1285 queue->wPaintCount--;
1286 if (!queue->wPaintCount) queue->wakeBits &= ~QS_PAINT;
1287 QUEUE_Unlock( queue );
1291 /***********************************************************************
1292 * QUEUE_IncTimerCount
1294 void QUEUE_IncTimerCount( HQUEUE16 hQueue )
1296 MESSAGEQUEUE *queue;
1298 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1299 queue->wTimerCount++;
1300 QUEUE_SetWakeBit( queue, QS_TIMER );
1301 QUEUE_Unlock( queue );
1305 /***********************************************************************
1306 * QUEUE_DecTimerCount
1308 void QUEUE_DecTimerCount( HQUEUE16 hQueue )
1310 MESSAGEQUEUE *queue;
1312 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1313 queue->wTimerCount--;
1314 if (!queue->wTimerCount) queue->wakeBits &= ~QS_TIMER;
1315 QUEUE_Unlock( queue );
1319 /***********************************************************************
1320 * PostQuitMessage16 (USER.6)
1322 void WINAPI PostQuitMessage16( INT16 exitCode )
1324 PostQuitMessage( exitCode );
1328 /***********************************************************************
1329 * PostQuitMessage (USER32.421)
1331 * PostQuitMessage() posts a message to the system requesting an
1332 * application to terminate execution. As a result of this function,
1333 * the WM_QUIT message is posted to the application, and
1334 * PostQuitMessage() returns immediately. The exitCode parameter
1335 * specifies an application-defined exit code, which appears in the
1336 * _wParam_ parameter of the WM_QUIT message posted to the application.
1338 * CONFORMANCE
1340 * ECMA-234, Win32
1342 void WINAPI PostQuitMessage( INT exitCode )
1344 MESSAGEQUEUE *queue;
1346 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return;
1347 queue->wPostQMsg = TRUE;
1348 queue->wExitCode = (WORD)exitCode;
1349 QUEUE_Unlock( queue );
1353 /***********************************************************************
1354 * GetWindowTask16 (USER.224)
1356 HTASK16 WINAPI GetWindowTask16( HWND16 hwnd )
1358 HTASK16 retvalue;
1359 WND *wndPtr = WIN_FindWndPtr( hwnd );
1361 if (!wndPtr) return 0;
1362 retvalue = QUEUE_GetQueueTask( wndPtr->hmemTaskQ );
1363 WIN_ReleaseWndPtr(wndPtr);
1364 return retvalue;
1367 /***********************************************************************
1368 * GetWindowThreadProcessId (USER32.313)
1370 DWORD WINAPI GetWindowThreadProcessId( HWND hwnd, LPDWORD process )
1372 DWORD retvalue;
1373 MESSAGEQUEUE *queue;
1375 WND *wndPtr = WIN_FindWndPtr( hwnd );
1376 if (!wndPtr) return 0;
1378 queue = QUEUE_Lock( wndPtr->hmemTaskQ );
1379 WIN_ReleaseWndPtr(wndPtr);
1381 if (!queue) return 0;
1383 if ( process ) *process = (DWORD)queue->teb->pid;
1384 retvalue = (DWORD)queue->teb->tid;
1386 QUEUE_Unlock( queue );
1387 return retvalue;
1391 /***********************************************************************
1392 * SetMessageQueue16 (USER.266)
1394 BOOL16 WINAPI SetMessageQueue16( INT16 size )
1396 return SetMessageQueue( size );
1400 /***********************************************************************
1401 * SetMessageQueue (USER32.494)
1403 BOOL WINAPI SetMessageQueue( INT size )
1405 /* now obsolete the message queue will be expanded dynamically
1406 as necessary */
1408 /* access the queue to create it if it's not existing */
1409 GetFastQueue16();
1411 return TRUE;
1414 /***********************************************************************
1415 * InitThreadInput16 (USER.409)
1417 HQUEUE16 WINAPI InitThreadInput16( WORD unknown, WORD flags )
1419 HQUEUE16 hQueue;
1420 MESSAGEQUEUE *queuePtr;
1422 TEB *teb = NtCurrentTeb();
1424 if (!teb)
1425 return 0;
1427 hQueue = teb->queue;
1429 if ( !hQueue )
1431 /* Create thread message queue */
1432 if( !(hQueue = QUEUE_CreateMsgQueue( TRUE )))
1434 WARN_(msg)("failed!\n");
1435 return FALSE;
1438 /* Link new queue into list */
1439 queuePtr = (MESSAGEQUEUE *)QUEUE_Lock( hQueue );
1440 queuePtr->teb = NtCurrentTeb();
1442 HeapLock( SystemHeap ); /* FIXME: a bit overkill */
1443 SetThreadQueue16( 0, hQueue );
1444 teb->queue = hQueue;
1446 queuePtr->next = hFirstQueue;
1447 hFirstQueue = hQueue;
1448 HeapUnlock( SystemHeap );
1450 QUEUE_Unlock( queuePtr );
1453 return hQueue;
1456 /***********************************************************************
1457 * GetQueueStatus16 (USER.334)
1459 DWORD WINAPI GetQueueStatus16( UINT16 flags )
1461 MESSAGEQUEUE *queue;
1462 DWORD ret;
1464 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1465 ret = MAKELONG( queue->changeBits, queue->wakeBits );
1466 queue->changeBits = 0;
1467 QUEUE_Unlock( queue );
1469 return ret & MAKELONG( flags, flags );
1472 /***********************************************************************
1473 * GetQueueStatus (USER32.283)
1475 DWORD WINAPI GetQueueStatus( UINT flags )
1477 MESSAGEQUEUE *queue;
1478 DWORD ret;
1480 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1481 ret = MAKELONG( queue->changeBits, queue->wakeBits );
1482 queue->changeBits = 0;
1483 QUEUE_Unlock( queue );
1485 return ret & MAKELONG( flags, flags );
1489 /***********************************************************************
1490 * GetInputState16 (USER.335)
1492 BOOL16 WINAPI GetInputState16(void)
1494 return GetInputState();
1497 /***********************************************************************
1498 * WaitForInputIdle (USER32.577)
1500 DWORD WINAPI WaitForInputIdle (HANDLE hProcess, DWORD dwTimeOut)
1502 DWORD cur_time, ret;
1503 HANDLE idle_event;
1504 struct wait_input_idle_request *req = get_req_buffer();
1506 req->handle = hProcess;
1507 req->timeout = dwTimeOut;
1508 if (server_call( REQ_WAIT_INPUT_IDLE )) return 0xffffffff;
1509 if ((idle_event = req->event) == -1) return 0; /* no event to wait on */
1511 cur_time = GetTickCount();
1513 TRACE_(msg)("waiting for %x\n", idle_event );
1514 while ( dwTimeOut > GetTickCount() - cur_time || dwTimeOut == INFINITE ) {
1516 ret = MsgWaitForMultipleObjects ( 1, &idle_event, FALSE, dwTimeOut, QS_SENDMESSAGE );
1517 if ( ret == ( WAIT_OBJECT_0 + 1 )) {
1518 MESSAGEQUEUE * queue;
1519 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0xFFFFFFFF;
1520 QUEUE_ReceiveMessage ( queue );
1521 QUEUE_Unlock ( queue );
1522 continue;
1524 if ( ret == WAIT_TIMEOUT || ret == 0xFFFFFFFF ) {
1525 TRACE_(msg)("timeout or error\n");
1526 return ret;
1528 else {
1529 TRACE_(msg)("finished\n");
1530 return 0;
1534 return WAIT_TIMEOUT;
1537 /***********************************************************************
1538 * GetInputState (USER32.244)
1540 BOOL WINAPI GetInputState(void)
1542 MESSAGEQUEUE *queue;
1543 BOOL ret;
1545 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() )))
1546 return FALSE;
1547 ret = queue->wakeBits & (QS_KEY | QS_MOUSEBUTTON);
1548 QUEUE_Unlock( queue );
1550 return ret;
1553 /***********************************************************************
1554 * UserYield (USER.332)
1556 void WINAPI UserYield16(void)
1558 MESSAGEQUEUE *queue;
1560 /* Handle sent messages */
1561 queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() );
1563 while (queue && (queue->wakeBits & QS_SENDMESSAGE))
1564 QUEUE_ReceiveMessage( queue );
1566 QUEUE_Unlock( queue );
1568 /* Yield */
1569 if ( THREAD_IsWin16( NtCurrentTeb() ) )
1570 OldYield16();
1571 else
1572 WIN32_OldYield16();
1574 /* Handle sent messages again */
1575 queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() );
1577 while (queue && (queue->wakeBits & QS_SENDMESSAGE))
1578 QUEUE_ReceiveMessage( queue );
1580 QUEUE_Unlock( queue );
1583 /***********************************************************************
1584 * GetMessagePos (USER.119) (USER32.272)
1586 * The GetMessagePos() function returns a long value representing a
1587 * cursor position, in screen coordinates, when the last message
1588 * retrieved by the GetMessage() function occurs. The x-coordinate is
1589 * in the low-order word of the return value, the y-coordinate is in
1590 * the high-order word. The application can use the MAKEPOINT()
1591 * macro to obtain a POINT structure from the return value.
1593 * For the current cursor position, use GetCursorPos().
1595 * RETURNS
1597 * Cursor position of last message on success, zero on failure.
1599 * CONFORMANCE
1601 * ECMA-234, Win32
1604 DWORD WINAPI GetMessagePos(void)
1606 MESSAGEQUEUE *queue;
1607 DWORD ret;
1609 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1610 ret = queue->GetMessagePosVal;
1611 QUEUE_Unlock( queue );
1613 return ret;
1617 /***********************************************************************
1618 * GetMessageTime (USER.120) (USER32.273)
1620 * GetMessageTime() returns the message time for the last message
1621 * retrieved by the function. The time is measured in milliseconds with
1622 * the same offset as GetTickCount().
1624 * Since the tick count wraps, this is only useful for moderately short
1625 * relative time comparisons.
1627 * RETURNS
1629 * Time of last message on success, zero on failure.
1631 * CONFORMANCE
1633 * ECMA-234, Win32
1636 LONG WINAPI GetMessageTime(void)
1638 MESSAGEQUEUE *queue;
1639 LONG ret;
1641 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1642 ret = queue->GetMessageTimeVal;
1643 QUEUE_Unlock( queue );
1645 return ret;
1649 /***********************************************************************
1650 * GetMessageExtraInfo (USER.288) (USER32.271)
1652 LONG WINAPI GetMessageExtraInfo(void)
1654 MESSAGEQUEUE *queue;
1655 LONG ret;
1657 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1658 ret = queue->GetMessageExtraInfoVal;
1659 QUEUE_Unlock( queue );
1661 return ret;
1665 /**********************************************************************
1666 * AttachThreadInput [USER32.8] Attaches input of 1 thread to other
1668 * Attaches the input processing mechanism of one thread to that of
1669 * another thread.
1671 * RETURNS
1672 * Success: TRUE
1673 * Failure: FALSE
1675 * TODO:
1676 * 1. Reset the Key State (currenly per thread key state is not maintained)
1678 BOOL WINAPI AttachThreadInput(
1679 DWORD idAttach, /* [in] Thread to attach */
1680 DWORD idAttachTo, /* [in] Thread to attach to */
1681 BOOL fAttach) /* [in] Attach or detach */
1683 MESSAGEQUEUE *pSrcMsgQ = 0, *pTgtMsgQ = 0;
1684 BOOL16 bRet = 0;
1686 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1688 /* A thread cannot attach to itself */
1689 if ( idAttach == idAttachTo )
1690 goto CLEANUP;
1692 /* According to the docs this method should fail if a
1693 * "Journal record" hook is installed. (attaches all input queues together)
1695 if ( HOOK_IsHooked( WH_JOURNALRECORD ) )
1696 goto CLEANUP;
1698 /* Retrieve message queues corresponding to the thread id's */
1699 pTgtMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetThreadQueue16( idAttach ) );
1700 pSrcMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetThreadQueue16( idAttachTo ) );
1702 /* Ensure we have message queues and that Src and Tgt threads
1703 * are not system threads.
1705 if ( !pSrcMsgQ || !pTgtMsgQ || !pSrcMsgQ->pQData || !pTgtMsgQ->pQData )
1706 goto CLEANUP;
1708 if (fAttach) /* Attach threads */
1710 /* Only attach if currently detached */
1711 if ( pTgtMsgQ->pQData != pSrcMsgQ->pQData )
1713 /* First release the target threads perQData */
1714 PERQDATA_Release( pTgtMsgQ->pQData );
1716 /* Share a reference to the source threads perQDATA */
1717 PERQDATA_Addref( pSrcMsgQ->pQData );
1718 pTgtMsgQ->pQData = pSrcMsgQ->pQData;
1721 else /* Detach threads */
1723 /* Only detach if currently attached */
1724 if ( pTgtMsgQ->pQData == pSrcMsgQ->pQData )
1726 /* First release the target threads perQData */
1727 PERQDATA_Release( pTgtMsgQ->pQData );
1729 /* Give the target thread its own private perQDATA once more */
1730 pTgtMsgQ->pQData = PERQDATA_CreateInstance();
1734 /* TODO: Reset the Key State */
1736 bRet = 1; /* Success */
1738 CLEANUP:
1740 /* Unlock the queues before returning */
1741 if ( pSrcMsgQ )
1742 QUEUE_Unlock( pSrcMsgQ );
1743 if ( pTgtMsgQ )
1744 QUEUE_Unlock( pTgtMsgQ );
1746 return bRet;