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 /***********************************************************************
461 HTASK16 QUEUE_GetQueueTask( HQUEUE16 hQueue )
465 MESSAGEQUEUE *queue = QUEUE_Lock( hQueue );
469 hTask = queue->teb->htask16;
470 QUEUE_Unlock( queue );
477 /***********************************************************************
478 * PostQuitMessage (USER.6)
480 void WINAPI PostQuitMessage16( INT16 exitCode )
482 PostQuitMessage( exitCode );
486 /***********************************************************************
487 * PostQuitMessage (USER32.@)
489 * PostQuitMessage() posts a message to the system requesting an
490 * application to terminate execution. As a result of this function,
491 * the WM_QUIT message is posted to the application, and
492 * PostQuitMessage() returns immediately. The exitCode parameter
493 * specifies an application-defined exit code, which appears in the
494 * _wParam_ parameter of the WM_QUIT message posted to the application.
500 void WINAPI PostQuitMessage( INT exitCode )
502 PostThreadMessageW( GetCurrentThreadId(), WM_QUIT, exitCode, 0 );
506 /***********************************************************************
507 * GetWindowTask (USER.224)
509 HTASK16 WINAPI GetWindowTask16( HWND16 hwnd )
512 WND *wndPtr = WIN_FindWndPtr( hwnd );
514 if (!wndPtr) return 0;
515 retvalue = QUEUE_GetQueueTask( wndPtr->hmemTaskQ );
516 WIN_ReleaseWndPtr(wndPtr);
520 /***********************************************************************
521 * GetWindowThreadProcessId (USER32.@)
523 DWORD WINAPI GetWindowThreadProcessId( HWND hwnd, LPDWORD process )
528 WND *wndPtr = WIN_FindWndPtr( hwnd );
529 if (!wndPtr) return 0;
531 queue = QUEUE_Lock( wndPtr->hmemTaskQ );
532 WIN_ReleaseWndPtr(wndPtr);
534 if (!queue) return 0;
536 if ( process ) *process = (DWORD)queue->teb->pid;
537 retvalue = (DWORD)queue->teb->tid;
539 QUEUE_Unlock( queue );
544 /***********************************************************************
545 * SetMessageQueue (USER.266)
547 BOOL16 WINAPI SetMessageQueue16( INT16 size )
549 return SetMessageQueue( size );
553 /***********************************************************************
554 * SetMessageQueue (USER32.@)
556 BOOL WINAPI SetMessageQueue( INT size )
558 /* now obsolete the message queue will be expanded dynamically
563 /***********************************************************************
564 * InitThreadInput (USER.409)
566 HQUEUE16 WINAPI InitThreadInput16( WORD unknown, WORD flags )
568 MESSAGEQUEUE *queuePtr;
569 HQUEUE16 hQueue = NtCurrentTeb()->queue;
573 /* Create thread message queue */
574 if( !(hQueue = QUEUE_CreateMsgQueue( TRUE )))
576 ERR_(msg)("failed!\n");
580 /* Link new queue into list */
581 queuePtr = QUEUE_Lock( hQueue );
582 queuePtr->teb = NtCurrentTeb();
584 HeapLock( GetProcessHeap() ); /* FIXME: a bit overkill */
585 SetThreadQueue16( 0, hQueue );
586 NtCurrentTeb()->queue = hQueue;
587 HeapUnlock( GetProcessHeap() );
589 QUEUE_Unlock( queuePtr );
595 /***********************************************************************
596 * GetQueueStatus (USER.334)
598 DWORD WINAPI GetQueueStatus16( UINT16 flags )
600 return GetQueueStatus( flags );
603 /***********************************************************************
604 * GetQueueStatus (USER32.@)
606 DWORD WINAPI GetQueueStatus( UINT flags )
610 SERVER_START_REQ( get_queue_status )
614 ret = MAKELONG( req->changed_bits & flags, req->wake_bits & flags );
621 /***********************************************************************
622 * GetInputState (USER.335)
624 BOOL16 WINAPI GetInputState16(void)
626 return GetInputState();
629 /***********************************************************************
630 * GetInputState (USER32.@)
632 BOOL WINAPI GetInputState(void)
636 SERVER_START_REQ( get_queue_status )
640 ret = req->wake_bits & (QS_KEY | QS_MOUSEBUTTON);
646 /***********************************************************************
647 * GetMessagePos (USER.119)
648 * GetMessagePos (USER32.@)
650 * The GetMessagePos() function returns a long value representing a
651 * cursor position, in screen coordinates, when the last message
652 * retrieved by the GetMessage() function occurs. The x-coordinate is
653 * in the low-order word of the return value, the y-coordinate is in
654 * the high-order word. The application can use the MAKEPOINT()
655 * macro to obtain a POINT structure from the return value.
657 * For the current cursor position, use GetCursorPos().
661 * Cursor position of last message on success, zero on failure.
668 DWORD WINAPI GetMessagePos(void)
672 if (!(queue = QUEUE_Current())) return 0;
673 return queue->GetMessagePosVal;
677 /***********************************************************************
678 * GetMessageTime (USER.120)
679 * GetMessageTime (USER32.@)
681 * GetMessageTime() returns the message time for the last message
682 * retrieved by the function. The time is measured in milliseconds with
683 * the same offset as GetTickCount().
685 * Since the tick count wraps, this is only useful for moderately short
686 * relative time comparisons.
690 * Time of last message on success, zero on failure.
697 LONG WINAPI GetMessageTime(void)
701 if (!(queue = QUEUE_Current())) return 0;
702 return queue->GetMessageTimeVal;
706 /***********************************************************************
707 * GetMessageExtraInfo (USER.288)
708 * GetMessageExtraInfo (USER32.@)
710 LONG WINAPI GetMessageExtraInfo(void)
714 if (!(queue = QUEUE_Current())) return 0;
715 return queue->GetMessageExtraInfoVal;
719 /**********************************************************************
720 * AttachThreadInput (USER32.@) Attaches input of 1 thread to other
722 * Attaches the input processing mechanism of one thread to that of
730 * 1. Reset the Key State (currenly per thread key state is not maintained)
732 BOOL WINAPI AttachThreadInput(
733 DWORD idAttach, /* [in] Thread to attach */
734 DWORD idAttachTo, /* [in] Thread to attach to */
735 BOOL fAttach) /* [in] Attach or detach */
737 MESSAGEQUEUE *pSrcMsgQ = 0, *pTgtMsgQ = 0;
740 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
742 /* A thread cannot attach to itself */
743 if ( idAttach == idAttachTo )
746 /* According to the docs this method should fail if a
747 * "Journal record" hook is installed. (attaches all input queues together)
749 if ( HOOK_IsHooked( WH_JOURNALRECORD ) )
752 /* Retrieve message queues corresponding to the thread id's */
753 pTgtMsgQ = QUEUE_Lock( GetThreadQueue16( idAttach ) );
754 pSrcMsgQ = QUEUE_Lock( GetThreadQueue16( idAttachTo ) );
756 /* Ensure we have message queues and that Src and Tgt threads
757 * are not system threads.
759 if ( !pSrcMsgQ || !pTgtMsgQ || !pSrcMsgQ->pQData || !pTgtMsgQ->pQData )
762 if (fAttach) /* Attach threads */
764 /* Only attach if currently detached */
765 if ( pTgtMsgQ->pQData != pSrcMsgQ->pQData )
767 /* First release the target threads perQData */
768 PERQDATA_Release( pTgtMsgQ->pQData );
770 /* Share a reference to the source threads perQDATA */
771 PERQDATA_Addref( pSrcMsgQ->pQData );
772 pTgtMsgQ->pQData = pSrcMsgQ->pQData;
775 else /* Detach threads */
777 /* Only detach if currently attached */
778 if ( pTgtMsgQ->pQData == pSrcMsgQ->pQData )
780 /* First release the target threads perQData */
781 PERQDATA_Release( pTgtMsgQ->pQData );
783 /* Give the target thread its own private perQDATA once more */
784 pTgtMsgQ->pQData = PERQDATA_CreateInstance();
788 /* TODO: Reset the Key State */
790 bRet = 1; /* Success */
794 /* Unlock the queues before returning */
796 QUEUE_Unlock( pSrcMsgQ );
798 QUEUE_Unlock( pTgtMsgQ );