Made HEAP_strdup* functions inline (temporary).
[wine/multimedia.git] / windows / queue.c
blob98e655d6c27cb03694c33c273a195efab9269525
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 "module.h"
15 #include "queue.h"
16 #include "task.h"
17 #include "win.h"
18 #include "clipboard.h"
19 #include "hook.h"
20 #include "heap.h"
21 #include "thread.h"
22 #include "debugtools.h"
23 #include "server.h"
24 #include "spy.h"
26 DECLARE_DEBUG_CHANNEL(msg);
27 DECLARE_DEBUG_CHANNEL(sendmsg);
29 #define MAX_QUEUE_SIZE 120 /* Max. size of a message queue */
31 static HQUEUE16 hFirstQueue = 0;
32 static HQUEUE16 hExitingQueue = 0;
33 static HQUEUE16 hmemSysMsgQueue = 0;
34 static MESSAGEQUEUE *sysMsgQueue = NULL;
35 static PERQUEUEDATA *pQDataWin16 = NULL; /* Global perQData for Win16 tasks */
37 static MESSAGEQUEUE *pMouseQueue = NULL; /* Queue for last mouse message */
38 static MESSAGEQUEUE *pKbdQueue = NULL; /* Queue for last kbd message */
40 HQUEUE16 hCursorQueue = 0;
41 HQUEUE16 hActiveQueue = 0;
44 /***********************************************************************
45 * PERQDATA_CreateInstance
47 * Creates an instance of a reference counted PERQUEUEDATA element
48 * for the message queue. perQData is stored globally for 16 bit tasks.
50 * Note: We don't implement perQdata exactly the same way Windows does.
51 * Each perQData element is reference counted since it may be potentially
52 * shared by multiple message Queues (via AttachThreadInput).
53 * We only store the current values for Active, Capture and focus windows
54 * currently.
56 PERQUEUEDATA * PERQDATA_CreateInstance( )
58 PERQUEUEDATA *pQData;
60 BOOL16 bIsWin16 = 0;
62 TRACE_(msg)("()\n");
64 /* Share a single instance of perQData for all 16 bit tasks */
65 if ( ( bIsWin16 = THREAD_IsWin16( NtCurrentTeb() ) ) )
67 /* If previously allocated, just bump up ref count */
68 if ( pQDataWin16 )
70 PERQDATA_Addref( pQDataWin16 );
71 return pQDataWin16;
75 /* Allocate PERQUEUEDATA from the system heap */
76 if (!( pQData = (PERQUEUEDATA *) HeapAlloc( SystemHeap, 0,
77 sizeof(PERQUEUEDATA) ) ))
78 return 0;
80 /* Initialize */
81 pQData->hWndCapture = pQData->hWndFocus = pQData->hWndActive = 0;
82 pQData->ulRefCount = 1;
83 pQData->nCaptureHT = HTCLIENT;
85 /* Note: We have an independent critical section for the per queue data
86 * since this may be shared by different threads. see AttachThreadInput()
88 InitializeCriticalSection( &pQData->cSection );
89 /* FIXME: not all per queue data critical sections should be global */
90 MakeCriticalSectionGlobal( &pQData->cSection );
92 /* Save perQData globally for 16 bit tasks */
93 if ( bIsWin16 )
94 pQDataWin16 = pQData;
96 return pQData;
100 /***********************************************************************
101 * PERQDATA_Addref
103 * Increment reference count for the PERQUEUEDATA instance
104 * Returns reference count for debugging purposes
106 ULONG PERQDATA_Addref( PERQUEUEDATA *pQData )
108 assert(pQData != 0 );
109 TRACE_(msg)("(): current refcount %lu ...\n", pQData->ulRefCount);
111 EnterCriticalSection( &pQData->cSection );
112 ++pQData->ulRefCount;
113 LeaveCriticalSection( &pQData->cSection );
115 return pQData->ulRefCount;
119 /***********************************************************************
120 * PERQDATA_Release
122 * Release a reference to a PERQUEUEDATA instance.
123 * Destroy the instance if no more references exist
124 * Returns reference count for debugging purposes
126 ULONG PERQDATA_Release( PERQUEUEDATA *pQData )
128 assert(pQData != 0 );
129 TRACE_(msg)("(): current refcount %lu ...\n",
130 (LONG)pQData->ulRefCount );
132 EnterCriticalSection( &pQData->cSection );
133 if ( --pQData->ulRefCount == 0 )
135 LeaveCriticalSection( &pQData->cSection );
136 DeleteCriticalSection( &pQData->cSection );
138 TRACE_(msg)("(): deleting PERQUEUEDATA instance ...\n" );
140 /* Deleting our global 16 bit perQData? */
141 if ( pQData == pQDataWin16 )
142 pQDataWin16 = 0;
144 /* Free the PERQUEUEDATA instance */
145 HeapFree( SystemHeap, 0, pQData );
147 return 0;
149 LeaveCriticalSection( &pQData->cSection );
151 return pQData->ulRefCount;
155 /***********************************************************************
156 * PERQDATA_GetFocusWnd
158 * Get the focus hwnd member in a threadsafe manner
160 HWND PERQDATA_GetFocusWnd( PERQUEUEDATA *pQData )
162 HWND hWndFocus;
163 assert(pQData != 0 );
165 EnterCriticalSection( &pQData->cSection );
166 hWndFocus = pQData->hWndFocus;
167 LeaveCriticalSection( &pQData->cSection );
169 return hWndFocus;
173 /***********************************************************************
174 * PERQDATA_SetFocusWnd
176 * Set the focus hwnd member in a threadsafe manner
178 HWND PERQDATA_SetFocusWnd( PERQUEUEDATA *pQData, HWND hWndFocus )
180 HWND hWndFocusPrv;
181 assert(pQData != 0 );
183 EnterCriticalSection( &pQData->cSection );
184 hWndFocusPrv = pQData->hWndFocus;
185 pQData->hWndFocus = hWndFocus;
186 LeaveCriticalSection( &pQData->cSection );
188 return hWndFocusPrv;
192 /***********************************************************************
193 * PERQDATA_GetActiveWnd
195 * Get the active hwnd member in a threadsafe manner
197 HWND PERQDATA_GetActiveWnd( PERQUEUEDATA *pQData )
199 HWND hWndActive;
200 assert(pQData != 0 );
202 EnterCriticalSection( &pQData->cSection );
203 hWndActive = pQData->hWndActive;
204 LeaveCriticalSection( &pQData->cSection );
206 return hWndActive;
210 /***********************************************************************
211 * PERQDATA_SetActiveWnd
213 * Set the active focus hwnd member in a threadsafe manner
215 HWND PERQDATA_SetActiveWnd( PERQUEUEDATA *pQData, HWND hWndActive )
217 HWND hWndActivePrv;
218 assert(pQData != 0 );
220 EnterCriticalSection( &pQData->cSection );
221 hWndActivePrv = pQData->hWndActive;
222 pQData->hWndActive = hWndActive;
223 LeaveCriticalSection( &pQData->cSection );
225 return hWndActivePrv;
229 /***********************************************************************
230 * PERQDATA_GetCaptureWnd
232 * Get the capture hwnd member in a threadsafe manner
234 HWND PERQDATA_GetCaptureWnd( PERQUEUEDATA *pQData )
236 HWND hWndCapture;
237 assert(pQData != 0 );
239 EnterCriticalSection( &pQData->cSection );
240 hWndCapture = pQData->hWndCapture;
241 LeaveCriticalSection( &pQData->cSection );
243 return hWndCapture;
247 /***********************************************************************
248 * PERQDATA_SetCaptureWnd
250 * Set the capture hwnd member in a threadsafe manner
252 HWND PERQDATA_SetCaptureWnd( PERQUEUEDATA *pQData, HWND hWndCapture )
254 HWND hWndCapturePrv;
255 assert(pQData != 0 );
257 EnterCriticalSection( &pQData->cSection );
258 hWndCapturePrv = pQData->hWndCapture;
259 pQData->hWndCapture = hWndCapture;
260 LeaveCriticalSection( &pQData->cSection );
262 return hWndCapturePrv;
266 /***********************************************************************
267 * PERQDATA_GetCaptureInfo
269 * Get the capture info member in a threadsafe manner
271 INT16 PERQDATA_GetCaptureInfo( PERQUEUEDATA *pQData )
273 INT16 nCaptureHT;
274 assert(pQData != 0 );
276 EnterCriticalSection( &pQData->cSection );
277 nCaptureHT = pQData->nCaptureHT;
278 LeaveCriticalSection( &pQData->cSection );
280 return nCaptureHT;
284 /***********************************************************************
285 * PERQDATA_SetCaptureInfo
287 * Set the capture info member in a threadsafe manner
289 INT16 PERQDATA_SetCaptureInfo( PERQUEUEDATA *pQData, INT16 nCaptureHT )
291 INT16 nCaptureHTPrv;
292 assert(pQData != 0 );
294 EnterCriticalSection( &pQData->cSection );
295 nCaptureHTPrv = pQData->nCaptureHT;
296 pQData->nCaptureHT = nCaptureHT;
297 LeaveCriticalSection( &pQData->cSection );
299 return nCaptureHTPrv;
303 /***********************************************************************
304 * QUEUE_Lock
306 * Function for getting a 32 bit pointer on queue structure. For thread
307 * safeness programmers should use this function instead of GlobalLock to
308 * retrieve a pointer on the structure. QUEUE_Unlock should also be called
309 * when access to the queue structure is not required anymore.
311 MESSAGEQUEUE *QUEUE_Lock( HQUEUE16 hQueue )
313 MESSAGEQUEUE *queue;
315 HeapLock( SystemHeap ); /* FIXME: a bit overkill */
316 queue = GlobalLock16( hQueue );
317 if ( !queue || (queue->magic != QUEUE_MAGIC) )
319 HeapUnlock( SystemHeap );
320 return NULL;
323 queue->lockCount++;
324 HeapUnlock( SystemHeap );
325 return queue;
329 /***********************************************************************
330 * QUEUE_Unlock
332 * Use with QUEUE_Lock to get a thread safe access to message queue
333 * structure
335 void QUEUE_Unlock( MESSAGEQUEUE *queue )
337 if (queue)
339 HeapLock( SystemHeap ); /* FIXME: a bit overkill */
341 if ( --queue->lockCount == 0 )
343 DeleteCriticalSection ( &queue->cSection );
344 if (queue->server_queue)
345 CloseHandle( queue->server_queue );
346 GlobalFree16( queue->self );
349 HeapUnlock( SystemHeap );
354 /***********************************************************************
355 * QUEUE_DumpQueue
357 void QUEUE_DumpQueue( HQUEUE16 hQueue )
359 MESSAGEQUEUE *pq;
361 if (!(pq = (MESSAGEQUEUE*) QUEUE_Lock( hQueue )) )
363 WARN_(msg)("%04x is not a queue handle\n", hQueue );
364 return;
367 DPRINTF( "next: %12.4x Intertask SendMessage:\n"
368 "thread: %10p ----------------------\n"
369 "firstMsg: %8p smWaiting: %10p\n"
370 "lastMsg: %8p smPending: %10p\n"
371 "msgCount: %8.4x smProcessing: %10p\n"
372 "lockCount: %7.4x\n"
373 "wWinVer: %9.4x\n"
374 "paints: %10.4x\n"
375 "timers: %10.4x\n"
376 "wakeBits: %8.4x\n"
377 "wakeMask: %8.4x\n"
378 "hCurHook: %8.4x\n",
379 pq->next, pq->teb, pq->firstMsg, pq->smWaiting, pq->lastMsg,
380 pq->smPending, pq->msgCount, pq->smProcessing,
381 (unsigned)pq->lockCount, pq->wWinVersion,
382 pq->wPaintCount, pq->wTimerCount,
383 pq->wakeBits, pq->wakeMask, pq->hCurHook);
385 QUEUE_Unlock( pq );
389 /***********************************************************************
390 * QUEUE_WalkQueues
392 void QUEUE_WalkQueues(void)
394 char module[10];
395 HQUEUE16 hQueue = hFirstQueue;
397 DPRINTF( "Queue Msgs Thread Task Module\n" );
398 while (hQueue)
400 MESSAGEQUEUE *queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue );
401 if (!queue)
403 WARN_(msg)("Bad queue handle %04x\n", hQueue );
404 return;
406 if (!GetModuleName16( queue->teb->htask16, module, sizeof(module )))
407 strcpy( module, "???" );
408 DPRINTF( "%04x %4d %p %04x %s\n", hQueue,queue->msgCount,
409 queue->teb, queue->teb->htask16, module );
410 hQueue = queue->next;
411 QUEUE_Unlock( queue );
413 DPRINTF( "\n" );
417 /***********************************************************************
418 * QUEUE_IsExitingQueue
420 BOOL QUEUE_IsExitingQueue( HQUEUE16 hQueue )
422 return (hExitingQueue && (hQueue == hExitingQueue));
426 /***********************************************************************
427 * QUEUE_SetExitingQueue
429 void QUEUE_SetExitingQueue( HQUEUE16 hQueue )
431 hExitingQueue = hQueue;
435 /***********************************************************************
436 * QUEUE_CreateMsgQueue
438 * Creates a message queue. Doesn't link it into queue list!
440 static HQUEUE16 QUEUE_CreateMsgQueue( BOOL16 bCreatePerQData )
442 HQUEUE16 hQueue;
443 HANDLE handle = -1;
444 MESSAGEQUEUE * msgQueue;
445 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
447 TRACE_(msg)("(): Creating message queue...\n");
449 if (!(hQueue = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT,
450 sizeof(MESSAGEQUEUE) )))
451 return 0;
453 msgQueue = (MESSAGEQUEUE *) GlobalLock16( hQueue );
454 if ( !msgQueue )
455 return 0;
457 SERVER_START_REQ
459 struct get_msg_queue_request *req = server_alloc_req( sizeof(*req), 0 );
460 if (!server_call( REQ_GET_MSG_QUEUE )) handle = req->handle;
462 SERVER_END_REQ;
463 if (handle == -1)
465 ERR_(msg)("Cannot get thread queue");
466 GlobalFree16( hQueue );
467 return 0;
469 msgQueue->server_queue = handle;
470 msgQueue->server_queue = ConvertToGlobalHandle( msgQueue->server_queue );
472 msgQueue->self = hQueue;
473 msgQueue->wakeBits = msgQueue->changeBits = 0;
474 msgQueue->wWinVersion = pTask ? pTask->version : 0;
476 InitializeCriticalSection( &msgQueue->cSection );
477 MakeCriticalSectionGlobal( &msgQueue->cSection );
479 msgQueue->lockCount = 1;
480 msgQueue->magic = QUEUE_MAGIC;
482 /* Create and initialize our per queue data */
483 msgQueue->pQData = bCreatePerQData ? PERQDATA_CreateInstance() : NULL;
485 return hQueue;
489 /***********************************************************************
490 * QUEUE_FlushMessage
492 * Try to reply to all pending sent messages on exit.
494 static void QUEUE_FlushMessages( MESSAGEQUEUE *queue )
496 SMSG *smsg;
497 MESSAGEQUEUE *senderQ = 0;
499 if( queue )
501 EnterCriticalSection( &queue->cSection );
503 /* empty the list of pending SendMessage waiting to be received */
504 while (queue->smPending)
506 smsg = QUEUE_RemoveSMSG( queue, SM_PENDING_LIST, 0);
508 senderQ = (MESSAGEQUEUE*)QUEUE_Lock( smsg->hSrcQueue );
509 if ( !senderQ )
510 continue;
512 /* return 0, to unblock other thread */
513 smsg->lResult = 0;
514 smsg->flags |= SMSG_HAVE_RESULT;
515 QUEUE_SetWakeBit( senderQ, QS_SMRESULT);
517 QUEUE_Unlock( senderQ );
520 QUEUE_ClearWakeBit( queue, QS_SENDMESSAGE );
522 LeaveCriticalSection( &queue->cSection );
527 /***********************************************************************
528 * QUEUE_DeleteMsgQueue
530 * Unlinks and deletes a message queue.
532 * Note: We need to mask asynchronous events to make sure PostMessage works
533 * even in the signal handler.
535 BOOL QUEUE_DeleteMsgQueue( HQUEUE16 hQueue )
537 MESSAGEQUEUE * msgQueue = (MESSAGEQUEUE*)QUEUE_Lock(hQueue);
538 HQUEUE16 *pPrev;
540 TRACE_(msg)("(): Deleting message queue %04x\n", hQueue);
542 if (!hQueue || !msgQueue)
544 ERR_(msg)("invalid argument.\n");
545 return 0;
548 msgQueue->magic = 0;
550 if( hCursorQueue == hQueue ) hCursorQueue = 0;
551 if( hActiveQueue == hQueue ) hActiveQueue = 0;
553 /* flush sent messages */
554 QUEUE_FlushMessages( msgQueue );
556 HeapLock( SystemHeap ); /* FIXME: a bit overkill */
558 /* Release per queue data if present */
559 if ( msgQueue->pQData )
561 PERQDATA_Release( msgQueue->pQData );
562 msgQueue->pQData = 0;
565 /* remove the message queue from the global link list */
566 pPrev = &hFirstQueue;
567 while (*pPrev && (*pPrev != hQueue))
569 MESSAGEQUEUE *msgQ = (MESSAGEQUEUE*)GlobalLock16(*pPrev);
571 /* sanity check */
572 if ( !msgQ || (msgQ->magic != QUEUE_MAGIC) )
574 /* HQUEUE link list is corrupted, try to exit gracefully */
575 ERR_(msg)("HQUEUE link list corrupted!\n");
576 pPrev = 0;
577 break;
579 pPrev = &msgQ->next;
581 if (pPrev && *pPrev) *pPrev = msgQueue->next;
582 msgQueue->self = 0;
584 HeapUnlock( SystemHeap );
586 /* free up resource used by MESSAGEQUEUE structure */
587 msgQueue->lockCount--;
588 QUEUE_Unlock( msgQueue );
590 return 1;
594 /***********************************************************************
595 * QUEUE_CreateSysMsgQueue
597 * Create the system message queue, and set the double-click speed.
598 * Must be called only once.
600 BOOL QUEUE_CreateSysMsgQueue( int size )
602 /* Note: We dont need perQ data for the system message queue */
603 if (!(hmemSysMsgQueue = QUEUE_CreateMsgQueue( FALSE )))
604 return FALSE;
606 sysMsgQueue = (MESSAGEQUEUE *) GlobalLock16( hmemSysMsgQueue );
607 return TRUE;
611 /***********************************************************************
612 * QUEUE_GetSysQueue
614 MESSAGEQUEUE *QUEUE_GetSysQueue(void)
616 return sysMsgQueue;
620 /***********************************************************************
621 * QUEUE_SetWakeBit
623 * See "Windows Internals", p.449
625 void QUEUE_SetWakeBit( MESSAGEQUEUE *queue, WORD bit )
627 TRACE_(msg)("queue = %04x (wm=%04x), bit = %04x\n",
628 queue->self, queue->wakeMask, bit );
630 if (bit & QS_MOUSE) pMouseQueue = queue;
631 if (bit & QS_KEY) pKbdQueue = queue;
632 queue->changeBits |= bit;
633 queue->wakeBits |= bit;
634 if (queue->wakeMask & bit)
636 queue->wakeMask = 0;
638 /* Wake up thread waiting for message */
639 if ( THREAD_IsWin16( queue->teb ) )
641 int iWndsLock = WIN_SuspendWndsLock();
642 PostEvent16( queue->teb->htask16 );
643 WIN_RestoreWndsLock( iWndsLock );
645 else
647 SERVER_START_REQ
649 struct wake_queue_request *req = server_alloc_req( sizeof(*req), 0 );
650 req->handle = queue->server_queue;
651 req->bits = bit;
652 server_call( REQ_WAKE_QUEUE );
654 SERVER_END_REQ;
660 /***********************************************************************
661 * QUEUE_ClearWakeBit
663 void QUEUE_ClearWakeBit( MESSAGEQUEUE *queue, WORD bit )
665 queue->changeBits &= ~bit;
666 queue->wakeBits &= ~bit;
670 /***********************************************************************
671 * QUEUE_WaitBits
673 * See "Windows Internals", p.447
675 * return values:
676 * 0 if exit with timeout
677 * 1 otherwise
679 int QUEUE_WaitBits( WORD bits, DWORD timeout )
681 MESSAGEQUEUE *queue;
682 DWORD curTime = 0;
683 HQUEUE16 hQueue;
685 TRACE_(msg)("q %04x waiting for %04x\n", GetFastQueue16(), bits);
687 if ( THREAD_IsWin16( NtCurrentTeb() ) && (timeout != INFINITE) )
688 curTime = GetTickCount();
690 hQueue = GetFastQueue16();
691 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return 0;
693 for (;;)
695 if (queue->changeBits & bits)
697 /* One of the bits is set; we can return */
698 queue->wakeMask = 0;
699 QUEUE_Unlock( queue );
700 return 1;
702 if (queue->wakeBits & QS_SENDMESSAGE)
704 /* Process the sent message immediately */
706 queue->wakeMask = 0;
707 QUEUE_ReceiveMessage( queue );
708 continue; /* nested sm crux */
711 queue->wakeMask = bits | QS_SENDMESSAGE;
712 if(queue->changeBits & bits)
714 continue;
717 TRACE_(msg)("%04x) wakeMask is %04x, waiting\n", queue->self, queue->wakeMask);
719 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
721 BOOL bHasWin16Lock;
722 DWORD dwlc;
724 if ( (bHasWin16Lock = _ConfirmWin16Lock()) )
726 TRACE_(msg)("bHasWin16Lock=TRUE\n");
727 ReleaseThunkLock( &dwlc );
730 WaitForSingleObject( queue->server_queue, timeout );
732 if ( bHasWin16Lock )
734 RestoreThunkLock( dwlc );
737 else
739 if ( timeout == INFINITE )
740 WaitEvent16( 0 ); /* win 16 thread, use WaitEvent */
741 else
743 /* check for timeout, then give control to other tasks */
744 if (GetTickCount() - curTime > timeout)
747 QUEUE_Unlock( queue );
748 return 0; /* exit with timeout */
750 Yield16();
757 /***********************************************************************
758 * QUEUE_AddSMSG
760 * This routine is called when a SMSG need to be added to one of the three
761 * SM list. (SM_PROCESSING_LIST, SM_PENDING_LIST, SM_WAITING_LIST)
763 BOOL QUEUE_AddSMSG( MESSAGEQUEUE *queue, int list, SMSG *smsg )
765 TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
766 smsg, SPY_GetMsgName(smsg->msg));
768 switch (list)
770 case SM_PROCESSING_LIST:
771 /* don't need to be thread safe, only accessed by the
772 thread associated with the sender queue */
773 smsg->nextProcessing = queue->smProcessing;
774 queue->smProcessing = smsg;
775 break;
777 case SM_WAITING_LIST:
778 /* don't need to be thread safe, only accessed by the
779 thread associated with the receiver queue */
780 smsg->nextWaiting = queue->smWaiting;
781 queue->smWaiting = smsg;
782 break;
784 case SM_PENDING_LIST:
786 /* make it thread safe, could be accessed by the sender and
787 receiver thread */
788 SMSG **prev;
790 EnterCriticalSection( &queue->cSection );
791 smsg->nextPending = NULL;
792 prev = &queue->smPending;
793 while ( *prev )
794 prev = &(*prev)->nextPending;
795 *prev = smsg;
796 LeaveCriticalSection( &queue->cSection );
798 QUEUE_SetWakeBit( queue, QS_SENDMESSAGE );
799 break;
802 default:
803 ERR_(sendmsg)("Invalid list: %d", list);
804 break;
807 return TRUE;
811 /***********************************************************************
812 * QUEUE_RemoveSMSG
814 * This routine is called when a SMSG needs to be removed from one of the three
815 * SM lists (SM_PROCESSING_LIST, SM_PENDING_LIST, SM_WAITING_LIST).
816 * If smsg == 0, remove the first smsg from the specified list
818 SMSG *QUEUE_RemoveSMSG( MESSAGEQUEUE *queue, int list, SMSG *smsg )
821 switch (list)
823 case SM_PROCESSING_LIST:
824 /* don't need to be thread safe, only accessed by the
825 thread associated with the sender queue */
827 /* if smsg is equal to null, it means the first in the list */
828 if (!smsg)
829 smsg = queue->smProcessing;
831 TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
832 smsg, SPY_GetMsgName(smsg->msg));
833 /* In fact SM_PROCESSING_LIST is a stack, and smsg
834 should be always at the top of the list */
835 if ( (smsg != queue->smProcessing) || !queue->smProcessing )
837 ERR_(sendmsg)("smsg not at the top of Processing list, smsg=0x%p queue=0x%p\n", smsg, queue);
838 return 0;
840 else
842 queue->smProcessing = smsg->nextProcessing;
843 smsg->nextProcessing = 0;
845 return smsg;
847 case SM_WAITING_LIST:
848 /* don't need to be thread safe, only accessed by the
849 thread associated with the receiver queue */
851 /* if smsg is equal to null, it means the first in the list */
852 if (!smsg)
853 smsg = queue->smWaiting;
855 TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
856 smsg, SPY_GetMsgName(smsg->msg));
857 /* In fact SM_WAITING_LIST is a stack, and smsg
858 should be always at the top of the list */
859 if ( (smsg != queue->smWaiting) || !queue->smWaiting )
861 ERR_(sendmsg)("smsg not at the top of Waiting list, smsg=0x%p queue=0x%p\n", smsg, queue);
862 return 0;
864 else
866 queue->smWaiting = smsg->nextWaiting;
867 smsg->nextWaiting = 0;
869 return smsg;
871 case SM_PENDING_LIST:
872 /* make it thread safe, could be accessed by the sender and
873 receiver thread */
874 EnterCriticalSection( &queue->cSection );
876 if (!smsg)
877 smsg = queue->smPending;
878 if ( (smsg != queue->smPending) || !queue->smPending )
880 ERR_(sendmsg)("should always remove the top one in Pending list, smsg=0x%p queue=0x%p\n", smsg, queue);
881 LeaveCriticalSection( &queue->cSection );
882 return 0;
885 TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
886 smsg, SPY_GetMsgName(smsg->msg));
888 queue->smPending = smsg->nextPending;
889 smsg->nextPending = 0;
891 /* if no more SMSG in Pending list, clear QS_SENDMESSAGE flag */
892 if (!queue->smPending)
893 QUEUE_ClearWakeBit( queue, QS_SENDMESSAGE );
895 LeaveCriticalSection( &queue->cSection );
896 return smsg;
898 default:
899 ERR_(sendmsg)("Invalid list: %d\n", list);
900 break;
903 return 0;
907 /***********************************************************************
908 * QUEUE_ReceiveMessage
910 * This routine is called when a sent message is waiting for the queue.
912 void QUEUE_ReceiveMessage( MESSAGEQUEUE *queue )
914 LRESULT result = 0;
915 SMSG *smsg;
916 MESSAGEQUEUE *senderQ;
918 TRACE_(sendmsg)("queue %04x\n", queue->self );
920 if ( !((queue->wakeBits & QS_SENDMESSAGE) && queue->smPending) )
922 TRACE_(sendmsg)("\trcm: nothing to do\n");
923 return;
926 /* remove smsg on the top of the pending list and put it in the processing list */
927 smsg = QUEUE_RemoveSMSG(queue, SM_PENDING_LIST, 0);
928 QUEUE_AddSMSG(queue, SM_WAITING_LIST, smsg);
930 TRACE_(sendmsg)("RM: %s [%04x] (%04x -> %04x)\n",
931 SPY_GetMsgName(smsg->msg), smsg->msg, smsg->hSrcQueue, smsg->hDstQueue );
933 if (IsWindow( smsg->hWnd ))
935 WND *wndPtr = WIN_FindWndPtr( smsg->hWnd );
936 DWORD extraInfo = queue->GetMessageExtraInfoVal; /* save ExtraInfo */
938 /* use sender queue extra info value while calling the window proc */
939 senderQ = (MESSAGEQUEUE*)QUEUE_Lock( smsg->hSrcQueue );
940 if (senderQ)
942 queue->GetMessageExtraInfoVal = senderQ->GetMessageExtraInfoVal;
943 QUEUE_Unlock( senderQ );
946 /* call the right version of CallWindowProcXX */
947 if (smsg->flags & SMSG_WIN32)
949 TRACE_(sendmsg)("\trcm: msg is Win32\n" );
950 if (smsg->flags & SMSG_UNICODE)
951 result = CallWindowProcW( wndPtr->winproc,
952 smsg->hWnd, smsg->msg,
953 smsg->wParam, smsg->lParam );
954 else
955 result = CallWindowProcA( wndPtr->winproc,
956 smsg->hWnd, smsg->msg,
957 smsg->wParam, smsg->lParam );
959 else /* Win16 message */
960 result = CallWindowProc16( (WNDPROC16)wndPtr->winproc,
961 (HWND16) smsg->hWnd,
962 (UINT16) smsg->msg,
963 LOWORD (smsg->wParam),
964 smsg->lParam );
966 queue->GetMessageExtraInfoVal = extraInfo; /* Restore extra info */
967 WIN_ReleaseWndPtr(wndPtr);
968 TRACE_(sendmsg)("result = %08x\n", (unsigned)result );
970 else WARN_(sendmsg)("\trcm: bad hWnd\n");
973 /* set SMSG_SENDING_REPLY flag to tell ReplyMessage16, it's not
974 an early reply */
975 smsg->flags |= SMSG_SENDING_REPLY;
976 ReplyMessage( result );
978 TRACE_(sendmsg)("done!\n" );
983 /***********************************************************************
984 * QUEUE_AddMsg
986 * Add a message to the queue. Return FALSE if queue is full.
988 BOOL QUEUE_AddMsg( HQUEUE16 hQueue, int type, MSG *msg, DWORD extraInfo )
990 MESSAGEQUEUE *msgQueue;
991 QMSG *qmsg;
994 if (!(msgQueue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return FALSE;
996 /* allocate new message in global heap for now */
997 if (!(qmsg = (QMSG *) HeapAlloc( SystemHeap, 0, sizeof(QMSG) ) ))
999 QUEUE_Unlock( msgQueue );
1000 return 0;
1003 EnterCriticalSection( &msgQueue->cSection );
1005 /* Store message */
1006 qmsg->type = type;
1007 qmsg->msg = *msg;
1008 qmsg->extraInfo = extraInfo;
1010 /* insert the message in the link list */
1011 qmsg->nextMsg = 0;
1012 qmsg->prevMsg = msgQueue->lastMsg;
1014 if (msgQueue->lastMsg)
1015 msgQueue->lastMsg->nextMsg = qmsg;
1017 /* update first and last anchor in message queue */
1018 msgQueue->lastMsg = qmsg;
1019 if (!msgQueue->firstMsg)
1020 msgQueue->firstMsg = qmsg;
1022 msgQueue->msgCount++;
1024 LeaveCriticalSection( &msgQueue->cSection );
1026 QUEUE_SetWakeBit( msgQueue, QS_POSTMESSAGE );
1027 QUEUE_Unlock( msgQueue );
1029 return TRUE;
1034 /***********************************************************************
1035 * QUEUE_FindMsg
1037 * Find a message matching the given parameters. Return -1 if none available.
1039 QMSG* QUEUE_FindMsg( MESSAGEQUEUE * msgQueue, HWND hwnd, int first, int last )
1041 QMSG* qmsg;
1043 EnterCriticalSection( &msgQueue->cSection );
1045 if (!msgQueue->msgCount)
1046 qmsg = 0;
1047 else if (!hwnd && !first && !last)
1048 qmsg = msgQueue->firstMsg;
1049 else
1051 /* look in linked list for message matching first and last criteria */
1052 for (qmsg = msgQueue->firstMsg; qmsg; qmsg = qmsg->nextMsg)
1054 MSG *msg = &(qmsg->msg);
1056 if (!hwnd || (msg->hwnd == hwnd))
1058 if (!first && !last)
1059 break; /* found it */
1061 if ((msg->message >= first) && (!last || (msg->message <= last)))
1062 break; /* found it */
1067 LeaveCriticalSection( &msgQueue->cSection );
1069 return qmsg;
1074 /***********************************************************************
1075 * QUEUE_RemoveMsg
1077 * Remove a message from the queue (pos must be a valid position).
1079 void QUEUE_RemoveMsg( MESSAGEQUEUE * msgQueue, QMSG *qmsg )
1081 EnterCriticalSection( &msgQueue->cSection );
1083 /* set the linked list */
1084 if (qmsg->prevMsg)
1085 qmsg->prevMsg->nextMsg = qmsg->nextMsg;
1087 if (qmsg->nextMsg)
1088 qmsg->nextMsg->prevMsg = qmsg->prevMsg;
1090 if (msgQueue->firstMsg == qmsg)
1091 msgQueue->firstMsg = qmsg->nextMsg;
1093 if (msgQueue->lastMsg == qmsg)
1094 msgQueue->lastMsg = qmsg->prevMsg;
1096 /* deallocate the memory for the message */
1097 HeapFree( SystemHeap, 0, qmsg );
1099 msgQueue->msgCount--;
1100 if (!msgQueue->msgCount) msgQueue->wakeBits &= ~QS_POSTMESSAGE;
1102 LeaveCriticalSection( &msgQueue->cSection );
1106 /***********************************************************************
1107 * QUEUE_WakeSomeone
1109 * Wake a queue upon reception of a hardware event.
1111 static void QUEUE_WakeSomeone( UINT message )
1113 WND* wndPtr = NULL;
1114 WORD wakeBit;
1115 HWND hwnd;
1116 HQUEUE16 hQueue = 0;
1117 MESSAGEQUEUE *queue = NULL;
1119 if (hCursorQueue)
1120 hQueue = hCursorQueue;
1122 if( (message >= WM_KEYFIRST) && (message <= WM_KEYLAST) )
1124 wakeBit = QS_KEY;
1125 if( hActiveQueue )
1126 hQueue = hActiveQueue;
1128 else
1130 wakeBit = (message == WM_MOUSEMOVE) ? QS_MOUSEMOVE : QS_MOUSEBUTTON;
1131 if( (hwnd = GetCapture()) )
1132 if( (wndPtr = WIN_FindWndPtr( hwnd )) )
1134 hQueue = wndPtr->hmemTaskQ;
1135 WIN_ReleaseWndPtr(wndPtr);
1139 if( (hwnd = GetSysModalWindow16()) )
1141 if( (wndPtr = WIN_FindWndPtr( hwnd )) )
1143 hQueue = wndPtr->hmemTaskQ;
1144 WIN_ReleaseWndPtr(wndPtr);
1148 if (hQueue)
1149 queue = QUEUE_Lock( hQueue );
1151 if( !queue )
1153 queue = QUEUE_Lock( hFirstQueue );
1154 while( queue )
1156 if (queue->wakeMask & wakeBit) break;
1158 QUEUE_Unlock(queue);
1159 queue = QUEUE_Lock( queue->next );
1161 if( !queue )
1163 WARN_(msg)("couldn't find queue\n");
1164 return;
1168 QUEUE_SetWakeBit( queue, wakeBit );
1170 QUEUE_Unlock( queue );
1174 /***********************************************************************
1175 * hardware_event
1177 * Add an event to the system message queue.
1178 * Note: the position is relative to the desktop window.
1180 void hardware_event( UINT message, WPARAM wParam, LPARAM lParam,
1181 int xPos, int yPos, DWORD time, DWORD extraInfo )
1183 MSG *msg;
1184 QMSG *qmsg;
1185 int mergeMsg = 0;
1187 if (!sysMsgQueue) return;
1189 EnterCriticalSection( &sysMsgQueue->cSection );
1191 /* Merge with previous event if possible */
1192 qmsg = sysMsgQueue->lastMsg;
1194 if ((message == WM_MOUSEMOVE) && sysMsgQueue->lastMsg)
1196 msg = &(sysMsgQueue->lastMsg->msg);
1198 if ((msg->message == message) && (msg->wParam == wParam))
1200 /* Merge events */
1201 qmsg = sysMsgQueue->lastMsg;
1202 mergeMsg = 1;
1206 if (!mergeMsg)
1208 /* Should I limit the number of messages in
1209 the system message queue??? */
1211 /* Don't merge allocate a new msg in the global heap */
1213 if (!(qmsg = (QMSG *) HeapAlloc( SystemHeap, 0, sizeof(QMSG) ) ))
1215 LeaveCriticalSection( &sysMsgQueue->cSection );
1216 return;
1219 /* put message at the end of the linked list */
1220 qmsg->nextMsg = 0;
1221 qmsg->prevMsg = sysMsgQueue->lastMsg;
1223 if (sysMsgQueue->lastMsg)
1224 sysMsgQueue->lastMsg->nextMsg = qmsg;
1226 /* set last and first anchor index in system message queue */
1227 sysMsgQueue->lastMsg = qmsg;
1228 if (!sysMsgQueue->firstMsg)
1229 sysMsgQueue->firstMsg = qmsg;
1231 sysMsgQueue->msgCount++;
1234 /* Store message */
1235 msg = &(qmsg->msg);
1236 msg->hwnd = 0;
1237 msg->message = message;
1238 msg->wParam = wParam;
1239 msg->lParam = lParam;
1240 msg->time = time;
1241 msg->pt.x = xPos;
1242 msg->pt.y = yPos;
1243 qmsg->extraInfo = extraInfo;
1244 qmsg->type = QMSG_HARDWARE;
1246 LeaveCriticalSection( &sysMsgQueue->cSection );
1248 QUEUE_WakeSomeone( message );
1252 /***********************************************************************
1253 * QUEUE_GetQueueTask
1255 HTASK16 QUEUE_GetQueueTask( HQUEUE16 hQueue )
1257 HTASK16 hTask = 0;
1259 MESSAGEQUEUE *queue = QUEUE_Lock( hQueue );
1261 if (queue)
1263 hTask = queue->teb->htask16;
1264 QUEUE_Unlock( queue );
1267 return hTask;
1272 /***********************************************************************
1273 * QUEUE_IncPaintCount
1275 void QUEUE_IncPaintCount( HQUEUE16 hQueue )
1277 MESSAGEQUEUE *queue;
1279 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1280 queue->wPaintCount++;
1281 QUEUE_SetWakeBit( queue, QS_PAINT );
1282 QUEUE_Unlock( queue );
1286 /***********************************************************************
1287 * QUEUE_DecPaintCount
1289 void QUEUE_DecPaintCount( HQUEUE16 hQueue )
1291 MESSAGEQUEUE *queue;
1293 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1294 queue->wPaintCount--;
1295 if (!queue->wPaintCount) queue->wakeBits &= ~QS_PAINT;
1296 QUEUE_Unlock( queue );
1300 /***********************************************************************
1301 * QUEUE_IncTimerCount
1303 void QUEUE_IncTimerCount( HQUEUE16 hQueue )
1305 MESSAGEQUEUE *queue;
1307 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1308 queue->wTimerCount++;
1309 QUEUE_SetWakeBit( queue, QS_TIMER );
1310 QUEUE_Unlock( queue );
1314 /***********************************************************************
1315 * QUEUE_DecTimerCount
1317 void QUEUE_DecTimerCount( HQUEUE16 hQueue )
1319 MESSAGEQUEUE *queue;
1321 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1322 queue->wTimerCount--;
1323 if (!queue->wTimerCount) queue->wakeBits &= ~QS_TIMER;
1324 QUEUE_Unlock( queue );
1328 /***********************************************************************
1329 * PostQuitMessage16 (USER.6)
1331 void WINAPI PostQuitMessage16( INT16 exitCode )
1333 PostQuitMessage( exitCode );
1337 /***********************************************************************
1338 * PostQuitMessage (USER32.421)
1340 * PostQuitMessage() posts a message to the system requesting an
1341 * application to terminate execution. As a result of this function,
1342 * the WM_QUIT message is posted to the application, and
1343 * PostQuitMessage() returns immediately. The exitCode parameter
1344 * specifies an application-defined exit code, which appears in the
1345 * _wParam_ parameter of the WM_QUIT message posted to the application.
1347 * CONFORMANCE
1349 * ECMA-234, Win32
1351 void WINAPI PostQuitMessage( INT exitCode )
1353 MESSAGEQUEUE *queue;
1355 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return;
1356 queue->wPostQMsg = TRUE;
1357 queue->wExitCode = (WORD)exitCode;
1358 QUEUE_Unlock( queue );
1362 /***********************************************************************
1363 * GetWindowTask16 (USER.224)
1365 HTASK16 WINAPI GetWindowTask16( HWND16 hwnd )
1367 HTASK16 retvalue;
1368 WND *wndPtr = WIN_FindWndPtr( hwnd );
1370 if (!wndPtr) return 0;
1371 retvalue = QUEUE_GetQueueTask( wndPtr->hmemTaskQ );
1372 WIN_ReleaseWndPtr(wndPtr);
1373 return retvalue;
1376 /***********************************************************************
1377 * GetWindowThreadProcessId (USER32.313)
1379 DWORD WINAPI GetWindowThreadProcessId( HWND hwnd, LPDWORD process )
1381 DWORD retvalue;
1382 MESSAGEQUEUE *queue;
1384 WND *wndPtr = WIN_FindWndPtr( hwnd );
1385 if (!wndPtr) return 0;
1387 queue = QUEUE_Lock( wndPtr->hmemTaskQ );
1388 WIN_ReleaseWndPtr(wndPtr);
1390 if (!queue) return 0;
1392 if ( process ) *process = (DWORD)queue->teb->pid;
1393 retvalue = (DWORD)queue->teb->tid;
1395 QUEUE_Unlock( queue );
1396 return retvalue;
1400 /***********************************************************************
1401 * SetMessageQueue16 (USER.266)
1403 BOOL16 WINAPI SetMessageQueue16( INT16 size )
1405 return SetMessageQueue( size );
1409 /***********************************************************************
1410 * SetMessageQueue (USER32.494)
1412 BOOL WINAPI SetMessageQueue( INT size )
1414 /* now obsolete the message queue will be expanded dynamically
1415 as necessary */
1417 /* access the queue to create it if it's not existing */
1418 GetFastQueue16();
1420 return TRUE;
1423 /***********************************************************************
1424 * InitThreadInput16 (USER.409)
1426 HQUEUE16 WINAPI InitThreadInput16( WORD unknown, WORD flags )
1428 HQUEUE16 hQueue;
1429 MESSAGEQUEUE *queuePtr;
1431 TEB *teb = NtCurrentTeb();
1433 if (!teb)
1434 return 0;
1436 hQueue = teb->queue;
1438 if ( !hQueue )
1440 /* Create thread message queue */
1441 if( !(hQueue = QUEUE_CreateMsgQueue( TRUE )))
1443 ERR_(msg)("failed!\n");
1444 return FALSE;
1447 /* Link new queue into list */
1448 queuePtr = (MESSAGEQUEUE *)QUEUE_Lock( hQueue );
1449 queuePtr->teb = NtCurrentTeb();
1451 HeapLock( SystemHeap ); /* FIXME: a bit overkill */
1452 SetThreadQueue16( 0, hQueue );
1453 teb->queue = hQueue;
1455 queuePtr->next = hFirstQueue;
1456 hFirstQueue = hQueue;
1457 HeapUnlock( SystemHeap );
1459 QUEUE_Unlock( queuePtr );
1462 return hQueue;
1465 /***********************************************************************
1466 * GetQueueStatus16 (USER.334)
1468 DWORD WINAPI GetQueueStatus16( UINT16 flags )
1470 MESSAGEQUEUE *queue;
1471 DWORD ret;
1473 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1474 ret = MAKELONG( queue->changeBits, queue->wakeBits );
1475 queue->changeBits = 0;
1476 QUEUE_Unlock( queue );
1478 return ret & MAKELONG( flags, flags );
1481 /***********************************************************************
1482 * GetQueueStatus (USER32.283)
1484 DWORD WINAPI GetQueueStatus( UINT flags )
1486 MESSAGEQUEUE *queue;
1487 DWORD ret;
1489 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1490 ret = MAKELONG( queue->changeBits, queue->wakeBits );
1491 queue->changeBits = 0;
1492 QUEUE_Unlock( queue );
1494 return ret & MAKELONG( flags, flags );
1498 /***********************************************************************
1499 * GetInputState16 (USER.335)
1501 BOOL16 WINAPI GetInputState16(void)
1503 return GetInputState();
1506 /***********************************************************************
1507 * WaitForInputIdle (USER32.577)
1509 DWORD WINAPI WaitForInputIdle (HANDLE hProcess, DWORD dwTimeOut)
1511 DWORD cur_time, ret;
1512 HANDLE idle_event = -1;
1514 SERVER_START_REQ
1516 struct wait_input_idle_request *req = server_alloc_req( sizeof(*req), 0 );
1517 req->handle = hProcess;
1518 req->timeout = dwTimeOut;
1519 if (!(ret = server_call( REQ_WAIT_INPUT_IDLE ))) idle_event = req->event;
1521 SERVER_END_REQ;
1522 if (ret) return 0xffffffff; /* error */
1523 if (idle_event == -1) return 0; /* no event to wait on */
1525 cur_time = GetTickCount();
1527 TRACE_(msg)("waiting for %x\n", idle_event );
1528 while ( dwTimeOut > GetTickCount() - cur_time || dwTimeOut == INFINITE ) {
1530 ret = MsgWaitForMultipleObjects ( 1, &idle_event, FALSE, dwTimeOut, QS_SENDMESSAGE );
1531 if ( ret == ( WAIT_OBJECT_0 + 1 )) {
1532 MESSAGEQUEUE * queue;
1533 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0xFFFFFFFF;
1534 QUEUE_ReceiveMessage ( queue );
1535 QUEUE_Unlock ( queue );
1536 continue;
1538 if ( ret == WAIT_TIMEOUT || ret == 0xFFFFFFFF ) {
1539 TRACE_(msg)("timeout or error\n");
1540 return ret;
1542 else {
1543 TRACE_(msg)("finished\n");
1544 return 0;
1548 return WAIT_TIMEOUT;
1551 /***********************************************************************
1552 * GetInputState (USER32.244)
1554 BOOL WINAPI GetInputState(void)
1556 MESSAGEQUEUE *queue;
1557 BOOL ret;
1559 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() )))
1560 return FALSE;
1561 ret = queue->wakeBits & (QS_KEY | QS_MOUSEBUTTON);
1562 QUEUE_Unlock( queue );
1564 return ret;
1567 /***********************************************************************
1568 * UserYield (USER.332)
1570 void WINAPI UserYield16(void)
1572 MESSAGEQUEUE *queue;
1574 /* Handle sent messages */
1575 queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() );
1577 while (queue && (queue->wakeBits & QS_SENDMESSAGE))
1578 QUEUE_ReceiveMessage( queue );
1580 QUEUE_Unlock( queue );
1582 /* Yield */
1583 if ( THREAD_IsWin16( NtCurrentTeb() ) )
1584 OldYield16();
1585 else
1586 WIN32_OldYield16();
1588 /* Handle sent messages again */
1589 queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() );
1591 while (queue && (queue->wakeBits & QS_SENDMESSAGE))
1592 QUEUE_ReceiveMessage( queue );
1594 QUEUE_Unlock( queue );
1597 /***********************************************************************
1598 * GetMessagePos (USER.119) (USER32.272)
1600 * The GetMessagePos() function returns a long value representing a
1601 * cursor position, in screen coordinates, when the last message
1602 * retrieved by the GetMessage() function occurs. The x-coordinate is
1603 * in the low-order word of the return value, the y-coordinate is in
1604 * the high-order word. The application can use the MAKEPOINT()
1605 * macro to obtain a POINT structure from the return value.
1607 * For the current cursor position, use GetCursorPos().
1609 * RETURNS
1611 * Cursor position of last message on success, zero on failure.
1613 * CONFORMANCE
1615 * ECMA-234, Win32
1618 DWORD WINAPI GetMessagePos(void)
1620 MESSAGEQUEUE *queue;
1621 DWORD ret;
1623 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1624 ret = queue->GetMessagePosVal;
1625 QUEUE_Unlock( queue );
1627 return ret;
1631 /***********************************************************************
1632 * GetMessageTime (USER.120) (USER32.273)
1634 * GetMessageTime() returns the message time for the last message
1635 * retrieved by the function. The time is measured in milliseconds with
1636 * the same offset as GetTickCount().
1638 * Since the tick count wraps, this is only useful for moderately short
1639 * relative time comparisons.
1641 * RETURNS
1643 * Time of last message on success, zero on failure.
1645 * CONFORMANCE
1647 * ECMA-234, Win32
1650 LONG WINAPI GetMessageTime(void)
1652 MESSAGEQUEUE *queue;
1653 LONG ret;
1655 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1656 ret = queue->GetMessageTimeVal;
1657 QUEUE_Unlock( queue );
1659 return ret;
1663 /***********************************************************************
1664 * GetMessageExtraInfo (USER.288) (USER32.271)
1666 LONG WINAPI GetMessageExtraInfo(void)
1668 MESSAGEQUEUE *queue;
1669 LONG ret;
1671 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1672 ret = queue->GetMessageExtraInfoVal;
1673 QUEUE_Unlock( queue );
1675 return ret;
1679 /**********************************************************************
1680 * AttachThreadInput [USER32.8] Attaches input of 1 thread to other
1682 * Attaches the input processing mechanism of one thread to that of
1683 * another thread.
1685 * RETURNS
1686 * Success: TRUE
1687 * Failure: FALSE
1689 * TODO:
1690 * 1. Reset the Key State (currenly per thread key state is not maintained)
1692 BOOL WINAPI AttachThreadInput(
1693 DWORD idAttach, /* [in] Thread to attach */
1694 DWORD idAttachTo, /* [in] Thread to attach to */
1695 BOOL fAttach) /* [in] Attach or detach */
1697 MESSAGEQUEUE *pSrcMsgQ = 0, *pTgtMsgQ = 0;
1698 BOOL16 bRet = 0;
1700 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1702 /* A thread cannot attach to itself */
1703 if ( idAttach == idAttachTo )
1704 goto CLEANUP;
1706 /* According to the docs this method should fail if a
1707 * "Journal record" hook is installed. (attaches all input queues together)
1709 if ( HOOK_IsHooked( WH_JOURNALRECORD ) )
1710 goto CLEANUP;
1712 /* Retrieve message queues corresponding to the thread id's */
1713 pTgtMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetThreadQueue16( idAttach ) );
1714 pSrcMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetThreadQueue16( idAttachTo ) );
1716 /* Ensure we have message queues and that Src and Tgt threads
1717 * are not system threads.
1719 if ( !pSrcMsgQ || !pTgtMsgQ || !pSrcMsgQ->pQData || !pTgtMsgQ->pQData )
1720 goto CLEANUP;
1722 if (fAttach) /* Attach threads */
1724 /* Only attach if currently detached */
1725 if ( pTgtMsgQ->pQData != pSrcMsgQ->pQData )
1727 /* First release the target threads perQData */
1728 PERQDATA_Release( pTgtMsgQ->pQData );
1730 /* Share a reference to the source threads perQDATA */
1731 PERQDATA_Addref( pSrcMsgQ->pQData );
1732 pTgtMsgQ->pQData = pSrcMsgQ->pQData;
1735 else /* Detach threads */
1737 /* Only detach if currently attached */
1738 if ( pTgtMsgQ->pQData == pSrcMsgQ->pQData )
1740 /* First release the target threads perQData */
1741 PERQDATA_Release( pTgtMsgQ->pQData );
1743 /* Give the target thread its own private perQDATA once more */
1744 pTgtMsgQ->pQData = PERQDATA_CreateInstance();
1748 /* TODO: Reset the Key State */
1750 bRet = 1; /* Success */
1752 CLEANUP:
1754 /* Unlock the queues before returning */
1755 if ( pSrcMsgQ )
1756 QUEUE_Unlock( pSrcMsgQ );
1757 if ( pTgtMsgQ )
1758 QUEUE_Unlock( pTgtMsgQ );
1760 return bRet;