2 * Message queues related functions
4 * Copyright 1993, 1994 Alexandre Julliard
13 #include "wine/winbase16.h"
14 #include "wine/winuser16.h"
20 #include "debugtools.h"
21 #include "wine/server.h"
24 DEFAULT_DEBUG_CHANNEL(msg);
27 static HQUEUE16 hExitingQueue = 0;
28 static PERQUEUEDATA *pQDataWin16 = NULL; /* Global perQData for Win16 tasks */
30 HQUEUE16 hActiveQueue = 0;
33 /***********************************************************************
36 * Increment reference count for the PERQUEUEDATA instance
37 * Returns reference count for debugging purposes
39 static void PERQDATA_Addref( PERQUEUEDATA *pQData )
42 TRACE_(msg)("(): current refcount %lu ...\n", pQData->ulRefCount);
44 InterlockedIncrement( &pQData->ulRefCount );
48 /***********************************************************************
51 * Release a reference to a PERQUEUEDATA instance.
52 * Destroy the instance if no more references exist
53 * Returns reference count for debugging purposes
55 static void PERQDATA_Release( PERQUEUEDATA *pQData )
58 TRACE_(msg)("(): current refcount %lu ...\n",
59 (LONG)pQData->ulRefCount );
61 if (!InterlockedDecrement( &pQData->ulRefCount ))
63 DeleteCriticalSection( &pQData->cSection );
65 TRACE_(msg)("(): deleting PERQUEUEDATA instance ...\n" );
67 /* Deleting our global 16 bit perQData? */
68 if ( pQData == pQDataWin16 ) pQDataWin16 = 0;
70 /* Free the PERQUEUEDATA instance */
71 HeapFree( GetProcessHeap(), 0, pQData );
76 /***********************************************************************
77 * PERQDATA_CreateInstance
79 * Creates an instance of a reference counted PERQUEUEDATA element
80 * for the message queue. perQData is stored globally for 16 bit tasks.
82 * Note: We don't implement perQdata exactly the same way Windows does.
83 * Each perQData element is reference counted since it may be potentially
84 * shared by multiple message Queues (via AttachThreadInput).
85 * We only store the current values for Active, Capture and focus windows
88 static PERQUEUEDATA * PERQDATA_CreateInstance(void)
96 /* Share a single instance of perQData for all 16 bit tasks */
97 if ( ( bIsWin16 = !(NtCurrentTeb()->tibflags & TEBF_WIN32) ) )
99 /* If previously allocated, just bump up ref count */
102 PERQDATA_Addref( pQDataWin16 );
107 /* Allocate PERQUEUEDATA from the system heap */
108 if (!( pQData = (PERQUEUEDATA *) HeapAlloc( GetProcessHeap(), 0,
109 sizeof(PERQUEUEDATA) ) ))
113 pQData->hWndCapture = pQData->hWndFocus = pQData->hWndActive = 0;
114 pQData->ulRefCount = 1;
115 pQData->nCaptureHT = HTCLIENT;
117 /* Note: We have an independent critical section for the per queue data
118 * since this may be shared by different threads. see AttachThreadInput()
120 InitializeCriticalSection( &pQData->cSection );
121 /* FIXME: not all per queue data critical sections should be global */
122 MakeCriticalSectionGlobal( &pQData->cSection );
124 /* Save perQData globally for 16 bit tasks */
126 pQDataWin16 = pQData;
132 /***********************************************************************
133 * PERQDATA_GetFocusWnd
135 * Get the focus hwnd member in a threadsafe manner
137 HWND PERQDATA_GetFocusWnd( PERQUEUEDATA *pQData )
140 assert(pQData != 0 );
142 EnterCriticalSection( &pQData->cSection );
143 hWndFocus = pQData->hWndFocus;
144 LeaveCriticalSection( &pQData->cSection );
150 /***********************************************************************
151 * PERQDATA_SetFocusWnd
153 * Set the focus hwnd member in a threadsafe manner
155 HWND PERQDATA_SetFocusWnd( PERQUEUEDATA *pQData, HWND hWndFocus )
158 assert(pQData != 0 );
160 EnterCriticalSection( &pQData->cSection );
161 hWndFocusPrv = pQData->hWndFocus;
162 pQData->hWndFocus = hWndFocus;
163 LeaveCriticalSection( &pQData->cSection );
169 /***********************************************************************
170 * PERQDATA_GetActiveWnd
172 * Get the active hwnd member in a threadsafe manner
174 HWND PERQDATA_GetActiveWnd( PERQUEUEDATA *pQData )
177 assert(pQData != 0 );
179 EnterCriticalSection( &pQData->cSection );
180 hWndActive = pQData->hWndActive;
181 LeaveCriticalSection( &pQData->cSection );
187 /***********************************************************************
188 * PERQDATA_SetActiveWnd
190 * Set the active focus hwnd member in a threadsafe manner
192 HWND PERQDATA_SetActiveWnd( PERQUEUEDATA *pQData, HWND hWndActive )
195 assert(pQData != 0 );
197 EnterCriticalSection( &pQData->cSection );
198 hWndActivePrv = pQData->hWndActive;
199 pQData->hWndActive = hWndActive;
200 LeaveCriticalSection( &pQData->cSection );
202 return hWndActivePrv;
206 /***********************************************************************
207 * PERQDATA_GetCaptureWnd
209 * Get the capture hwnd member in a threadsafe manner
211 HWND PERQDATA_GetCaptureWnd( INT *hittest )
214 PERQUEUEDATA *pQData;
217 if (!(queue = QUEUE_Current())) return 0;
218 pQData = queue->pQData;
220 EnterCriticalSection( &pQData->cSection );
221 hWndCapture = pQData->hWndCapture;
222 *hittest = pQData->nCaptureHT;
223 LeaveCriticalSection( &pQData->cSection );
228 /***********************************************************************
229 * PERQDATA_SetCaptureWnd
231 * Set the capture hwnd member in a threadsafe manner
233 HWND PERQDATA_SetCaptureWnd( HWND hWndCapture, INT hittest )
236 PERQUEUEDATA *pQData;
239 if (!(queue = QUEUE_Current())) return 0;
240 pQData = queue->pQData;
242 EnterCriticalSection( &pQData->cSection );
243 hWndCapturePrv = pQData->hWndCapture;
244 pQData->hWndCapture = hWndCapture;
245 pQData->nCaptureHT = hittest;
246 LeaveCriticalSection( &pQData->cSection );
247 return hWndCapturePrv;
252 /***********************************************************************
255 * Function for getting a 32 bit pointer on queue structure. For thread
256 * safeness programmers should use this function instead of GlobalLock to
257 * retrieve a pointer on the structure. QUEUE_Unlock should also be called
258 * when access to the queue structure is not required anymore.
260 MESSAGEQUEUE *QUEUE_Lock( HQUEUE16 hQueue )
264 HeapLock( GetProcessHeap() ); /* FIXME: a bit overkill */
265 queue = GlobalLock16( hQueue );
266 if ( !queue || (queue->magic != QUEUE_MAGIC) )
268 HeapUnlock( GetProcessHeap() );
273 HeapUnlock( GetProcessHeap() );
278 /***********************************************************************
281 * Get the current thread queue, creating it if required.
282 * QUEUE_Unlock is not needed since the queue can only be deleted by
283 * the current thread anyway.
285 MESSAGEQUEUE *QUEUE_Current(void)
290 if (!(hQueue = GetThreadQueue16(0)))
292 if (!(hQueue = InitThreadInput16( 0, 0 ))) return NULL;
295 if ((queue = GlobalLock16( hQueue )))
297 if (queue->magic != QUEUE_MAGIC) queue = NULL;
303 /***********************************************************************
306 * Use with QUEUE_Lock to get a thread safe access to message queue
309 void QUEUE_Unlock( MESSAGEQUEUE *queue )
313 HeapLock( GetProcessHeap() ); /* FIXME: a bit overkill */
315 if ( --queue->lockCount == 0 )
317 if (queue->server_queue)
318 CloseHandle( queue->server_queue );
319 GlobalFree16( queue->self );
322 HeapUnlock( GetProcessHeap() );
327 /***********************************************************************
328 * QUEUE_IsExitingQueue
330 BOOL QUEUE_IsExitingQueue( HQUEUE16 hQueue )
332 return (hExitingQueue && (hQueue == hExitingQueue));
336 /***********************************************************************
337 * QUEUE_SetExitingQueue
339 void QUEUE_SetExitingQueue( HQUEUE16 hQueue )
341 hExitingQueue = hQueue;
345 /***********************************************************************
346 * QUEUE_CreateMsgQueue
348 * Creates a message queue. Doesn't link it into queue list!
350 static HQUEUE16 QUEUE_CreateMsgQueue( BOOL16 bCreatePerQData )
354 MESSAGEQUEUE * msgQueue;
356 TRACE_(msg)("(): Creating message queue...\n");
358 if (!(hQueue = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT,
359 sizeof(MESSAGEQUEUE) )))
362 msgQueue = (MESSAGEQUEUE *) GlobalLock16( hQueue );
368 SERVER_START_REQ( get_msg_queue )
371 handle = req->handle;
376 ERR_(msg)("Cannot get thread queue");
377 GlobalFree16( hQueue );
380 msgQueue->server_queue = handle;
383 msgQueue->self = hQueue;
384 msgQueue->lockCount = 1;
385 msgQueue->magic = QUEUE_MAGIC;
387 /* Create and initialize our per queue data */
388 msgQueue->pQData = bCreatePerQData ? PERQDATA_CreateInstance() : NULL;
394 /***********************************************************************
395 * QUEUE_DeleteMsgQueue
397 * Unlinks and deletes a message queue.
399 * Note: We need to mask asynchronous events to make sure PostMessage works
400 * even in the signal handler.
402 void QUEUE_DeleteMsgQueue(void)
404 HQUEUE16 hQueue = GetThreadQueue16(0);
405 MESSAGEQUEUE * msgQueue;
407 if (!hQueue) return; /* thread doesn't have a queue */
409 TRACE("(): Deleting message queue %04x\n", hQueue);
411 if (!(msgQueue = QUEUE_Lock(hQueue)))
413 ERR("invalid thread queue\n");
419 if( hActiveQueue == hQueue ) hActiveQueue = 0;
420 if (hExitingQueue == hQueue) hExitingQueue = 0;
422 HeapLock( GetProcessHeap() ); /* FIXME: a bit overkill */
424 /* Release per queue data if present */
425 if ( msgQueue->pQData )
427 PERQDATA_Release( msgQueue->pQData );
428 msgQueue->pQData = 0;
433 HeapUnlock( GetProcessHeap() );
434 SetThreadQueue16( 0, 0 );
436 /* free up resource used by MESSAGEQUEUE structure */
437 msgQueue->lockCount--;
438 QUEUE_Unlock( msgQueue );
442 /***********************************************************************
443 * GetWindowTask (USER.224)
445 HTASK16 WINAPI GetWindowTask16( HWND16 hwnd )
450 WND *wndPtr = WIN_FindWndPtr16( hwnd );
451 if (!wndPtr) return 0;
453 queue = QUEUE_Lock( wndPtr->hmemTaskQ );
454 WIN_ReleaseWndPtr(wndPtr);
456 if (!queue) return 0;
458 retvalue = queue->teb->htask16;
459 QUEUE_Unlock( queue );
464 /***********************************************************************
465 * InitThreadInput (USER.409)
467 HQUEUE16 WINAPI InitThreadInput16( WORD unknown, WORD flags )
469 MESSAGEQUEUE *queuePtr;
470 HQUEUE16 hQueue = NtCurrentTeb()->queue;
474 /* Create thread message queue */
475 if( !(hQueue = QUEUE_CreateMsgQueue( TRUE )))
477 ERR_(msg)("failed!\n");
481 /* Link new queue into list */
482 queuePtr = QUEUE_Lock( hQueue );
483 queuePtr->teb = NtCurrentTeb();
485 HeapLock( GetProcessHeap() ); /* FIXME: a bit overkill */
486 SetThreadQueue16( 0, hQueue );
487 NtCurrentTeb()->queue = hQueue;
488 HeapUnlock( GetProcessHeap() );
490 QUEUE_Unlock( queuePtr );
496 /***********************************************************************
497 * GetQueueStatus (USER32.@)
499 DWORD WINAPI GetQueueStatus( UINT flags )
503 SERVER_START_REQ( get_queue_status )
507 ret = MAKELONG( req->changed_bits & flags, req->wake_bits & flags );
514 /***********************************************************************
515 * GetInputState (USER32.@)
517 BOOL WINAPI GetInputState(void)
521 SERVER_START_REQ( get_queue_status )
525 ret = req->wake_bits & (QS_KEY | QS_MOUSEBUTTON);
531 /***********************************************************************
532 * GetMessagePos (USER.119)
533 * GetMessagePos (USER32.@)
535 * The GetMessagePos() function returns a long value representing a
536 * cursor position, in screen coordinates, when the last message
537 * retrieved by the GetMessage() function occurs. The x-coordinate is
538 * in the low-order word of the return value, the y-coordinate is in
539 * the high-order word. The application can use the MAKEPOINT()
540 * macro to obtain a POINT structure from the return value.
542 * For the current cursor position, use GetCursorPos().
546 * Cursor position of last message on success, zero on failure.
553 DWORD WINAPI GetMessagePos(void)
557 if (!(queue = QUEUE_Current())) return 0;
558 return queue->GetMessagePosVal;
562 /***********************************************************************
563 * GetMessageTime (USER.120)
564 * GetMessageTime (USER32.@)
566 * GetMessageTime() returns the message time for the last message
567 * retrieved by the function. The time is measured in milliseconds with
568 * the same offset as GetTickCount().
570 * Since the tick count wraps, this is only useful for moderately short
571 * relative time comparisons.
575 * Time of last message on success, zero on failure.
582 LONG WINAPI GetMessageTime(void)
586 if (!(queue = QUEUE_Current())) return 0;
587 return queue->GetMessageTimeVal;
591 /***********************************************************************
592 * GetMessageExtraInfo (USER.288)
593 * GetMessageExtraInfo (USER32.@)
595 LONG WINAPI GetMessageExtraInfo(void)
599 if (!(queue = QUEUE_Current())) return 0;
600 return queue->GetMessageExtraInfoVal;
604 /**********************************************************************
605 * AttachThreadInput (USER32.@) Attaches input of 1 thread to other
607 * Attaches the input processing mechanism of one thread to that of
615 * 1. Reset the Key State (currenly per thread key state is not maintained)
617 BOOL WINAPI AttachThreadInput(
618 DWORD idAttach, /* [in] Thread to attach */
619 DWORD idAttachTo, /* [in] Thread to attach to */
620 BOOL fAttach) /* [in] Attach or detach */
622 MESSAGEQUEUE *pSrcMsgQ = 0, *pTgtMsgQ = 0;
625 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
627 /* A thread cannot attach to itself */
628 if ( idAttach == idAttachTo )
631 /* According to the docs this method should fail if a
632 * "Journal record" hook is installed. (attaches all input queues together)
634 if ( HOOK_IsHooked( WH_JOURNALRECORD ) )
637 /* Retrieve message queues corresponding to the thread id's */
638 pTgtMsgQ = QUEUE_Lock( GetThreadQueue16( idAttach ) );
639 pSrcMsgQ = QUEUE_Lock( GetThreadQueue16( idAttachTo ) );
641 /* Ensure we have message queues and that Src and Tgt threads
642 * are not system threads.
644 if ( !pSrcMsgQ || !pTgtMsgQ || !pSrcMsgQ->pQData || !pTgtMsgQ->pQData )
647 if (fAttach) /* Attach threads */
649 /* Only attach if currently detached */
650 if ( pTgtMsgQ->pQData != pSrcMsgQ->pQData )
652 /* First release the target threads perQData */
653 PERQDATA_Release( pTgtMsgQ->pQData );
655 /* Share a reference to the source threads perQDATA */
656 PERQDATA_Addref( pSrcMsgQ->pQData );
657 pTgtMsgQ->pQData = pSrcMsgQ->pQData;
660 else /* Detach threads */
662 /* Only detach if currently attached */
663 if ( pTgtMsgQ->pQData == pSrcMsgQ->pQData )
665 /* First release the target threads perQData */
666 PERQDATA_Release( pTgtMsgQ->pQData );
668 /* Give the target thread its own private perQDATA once more */
669 pTgtMsgQ->pQData = PERQDATA_CreateInstance();
673 /* TODO: Reset the Key State */
675 bRet = 1; /* Success */
679 /* Unlock the queues before returning */
681 QUEUE_Unlock( pSrcMsgQ );
683 QUEUE_Unlock( pTgtMsgQ );