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 * QUEUE_CleanupWindow
445 * Cleanup the queue to account for a window being deleted.
447 void QUEUE_CleanupWindow( HWND hwnd )
449 SERVER_START_REQ( cleanup_window_queue )
458 /***********************************************************************
459 * GetWindowTask (USER.224)
461 HTASK16 WINAPI GetWindowTask16( HWND16 hwnd )
466 WND *wndPtr = WIN_FindWndPtr( hwnd );
467 if (!wndPtr) return 0;
469 queue = QUEUE_Lock( wndPtr->hmemTaskQ );
470 WIN_ReleaseWndPtr(wndPtr);
472 if (!queue) return 0;
474 retvalue = queue->teb->htask16;
475 QUEUE_Unlock( queue );
480 /***********************************************************************
481 * GetWindowThreadProcessId (USER32.@)
483 DWORD WINAPI GetWindowThreadProcessId( HWND hwnd, LPDWORD process )
488 WND *wndPtr = WIN_FindWndPtr( hwnd );
489 if (!wndPtr) return 0;
491 queue = QUEUE_Lock( wndPtr->hmemTaskQ );
492 WIN_ReleaseWndPtr(wndPtr);
494 if (!queue) return 0;
496 if ( process ) *process = (DWORD)queue->teb->pid;
497 retvalue = (DWORD)queue->teb->tid;
499 QUEUE_Unlock( queue );
504 /***********************************************************************
505 * InitThreadInput (USER.409)
507 HQUEUE16 WINAPI InitThreadInput16( WORD unknown, WORD flags )
509 MESSAGEQUEUE *queuePtr;
510 HQUEUE16 hQueue = NtCurrentTeb()->queue;
514 /* Create thread message queue */
515 if( !(hQueue = QUEUE_CreateMsgQueue( TRUE )))
517 ERR_(msg)("failed!\n");
521 /* Link new queue into list */
522 queuePtr = QUEUE_Lock( hQueue );
523 queuePtr->teb = NtCurrentTeb();
525 HeapLock( GetProcessHeap() ); /* FIXME: a bit overkill */
526 SetThreadQueue16( 0, hQueue );
527 NtCurrentTeb()->queue = hQueue;
528 HeapUnlock( GetProcessHeap() );
530 QUEUE_Unlock( queuePtr );
536 /***********************************************************************
537 * GetQueueStatus (USER32.@)
539 DWORD WINAPI GetQueueStatus( UINT flags )
543 SERVER_START_REQ( get_queue_status )
547 ret = MAKELONG( req->changed_bits & flags, req->wake_bits & flags );
554 /***********************************************************************
555 * GetInputState (USER32.@)
557 BOOL WINAPI GetInputState(void)
561 SERVER_START_REQ( get_queue_status )
565 ret = req->wake_bits & (QS_KEY | QS_MOUSEBUTTON);
571 /***********************************************************************
572 * GetMessagePos (USER.119)
573 * GetMessagePos (USER32.@)
575 * The GetMessagePos() function returns a long value representing a
576 * cursor position, in screen coordinates, when the last message
577 * retrieved by the GetMessage() function occurs. The x-coordinate is
578 * in the low-order word of the return value, the y-coordinate is in
579 * the high-order word. The application can use the MAKEPOINT()
580 * macro to obtain a POINT structure from the return value.
582 * For the current cursor position, use GetCursorPos().
586 * Cursor position of last message on success, zero on failure.
593 DWORD WINAPI GetMessagePos(void)
597 if (!(queue = QUEUE_Current())) return 0;
598 return queue->GetMessagePosVal;
602 /***********************************************************************
603 * GetMessageTime (USER.120)
604 * GetMessageTime (USER32.@)
606 * GetMessageTime() returns the message time for the last message
607 * retrieved by the function. The time is measured in milliseconds with
608 * the same offset as GetTickCount().
610 * Since the tick count wraps, this is only useful for moderately short
611 * relative time comparisons.
615 * Time of last message on success, zero on failure.
622 LONG WINAPI GetMessageTime(void)
626 if (!(queue = QUEUE_Current())) return 0;
627 return queue->GetMessageTimeVal;
631 /***********************************************************************
632 * GetMessageExtraInfo (USER.288)
633 * GetMessageExtraInfo (USER32.@)
635 LONG WINAPI GetMessageExtraInfo(void)
639 if (!(queue = QUEUE_Current())) return 0;
640 return queue->GetMessageExtraInfoVal;
644 /**********************************************************************
645 * AttachThreadInput (USER32.@) Attaches input of 1 thread to other
647 * Attaches the input processing mechanism of one thread to that of
655 * 1. Reset the Key State (currenly per thread key state is not maintained)
657 BOOL WINAPI AttachThreadInput(
658 DWORD idAttach, /* [in] Thread to attach */
659 DWORD idAttachTo, /* [in] Thread to attach to */
660 BOOL fAttach) /* [in] Attach or detach */
662 MESSAGEQUEUE *pSrcMsgQ = 0, *pTgtMsgQ = 0;
665 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
667 /* A thread cannot attach to itself */
668 if ( idAttach == idAttachTo )
671 /* According to the docs this method should fail if a
672 * "Journal record" hook is installed. (attaches all input queues together)
674 if ( HOOK_IsHooked( WH_JOURNALRECORD ) )
677 /* Retrieve message queues corresponding to the thread id's */
678 pTgtMsgQ = QUEUE_Lock( GetThreadQueue16( idAttach ) );
679 pSrcMsgQ = QUEUE_Lock( GetThreadQueue16( idAttachTo ) );
681 /* Ensure we have message queues and that Src and Tgt threads
682 * are not system threads.
684 if ( !pSrcMsgQ || !pTgtMsgQ || !pSrcMsgQ->pQData || !pTgtMsgQ->pQData )
687 if (fAttach) /* Attach threads */
689 /* Only attach if currently detached */
690 if ( pTgtMsgQ->pQData != pSrcMsgQ->pQData )
692 /* First release the target threads perQData */
693 PERQDATA_Release( pTgtMsgQ->pQData );
695 /* Share a reference to the source threads perQDATA */
696 PERQDATA_Addref( pSrcMsgQ->pQData );
697 pTgtMsgQ->pQData = pSrcMsgQ->pQData;
700 else /* Detach threads */
702 /* Only detach if currently attached */
703 if ( pTgtMsgQ->pQData == pSrcMsgQ->pQData )
705 /* First release the target threads perQData */
706 PERQDATA_Release( pTgtMsgQ->pQData );
708 /* Give the target thread its own private perQDATA once more */
709 pTgtMsgQ->pQData = PERQDATA_CreateInstance();
713 /* TODO: Reset the Key State */
715 bRet = 1; /* Success */
719 /* Unlock the queues before returning */
721 QUEUE_Unlock( pSrcMsgQ );
723 QUEUE_Unlock( pTgtMsgQ );