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 PERQUEUEDATA *pQDataWin16 = NULL; /* Global perQData for Win16 tasks */
29 HQUEUE16 hActiveQueue = 0;
32 /***********************************************************************
35 * Increment reference count for the PERQUEUEDATA instance
36 * Returns reference count for debugging purposes
38 static void PERQDATA_Addref( PERQUEUEDATA *pQData )
41 TRACE_(msg)("(): current refcount %lu ...\n", pQData->ulRefCount);
43 InterlockedIncrement( &pQData->ulRefCount );
47 /***********************************************************************
50 * Release a reference to a PERQUEUEDATA instance.
51 * Destroy the instance if no more references exist
52 * Returns reference count for debugging purposes
54 static void PERQDATA_Release( PERQUEUEDATA *pQData )
57 TRACE_(msg)("(): current refcount %lu ...\n",
58 (LONG)pQData->ulRefCount );
60 if (!InterlockedDecrement( &pQData->ulRefCount ))
62 DeleteCriticalSection( &pQData->cSection );
64 TRACE_(msg)("(): deleting PERQUEUEDATA instance ...\n" );
66 /* Deleting our global 16 bit perQData? */
67 if ( pQData == pQDataWin16 ) pQDataWin16 = 0;
69 /* Free the PERQUEUEDATA instance */
70 HeapFree( GetProcessHeap(), 0, pQData );
75 /***********************************************************************
76 * PERQDATA_CreateInstance
78 * Creates an instance of a reference counted PERQUEUEDATA element
79 * for the message queue. perQData is stored globally for 16 bit tasks.
81 * Note: We don't implement perQdata exactly the same way Windows does.
82 * Each perQData element is reference counted since it may be potentially
83 * shared by multiple message Queues (via AttachThreadInput).
84 * We only store the current values for Active, Capture and focus windows
87 static PERQUEUEDATA * PERQDATA_CreateInstance(void)
95 /* Share a single instance of perQData for all 16 bit tasks */
96 if ( ( bIsWin16 = !(NtCurrentTeb()->tibflags & TEBF_WIN32) ) )
98 /* If previously allocated, just bump up ref count */
101 PERQDATA_Addref( pQDataWin16 );
106 /* Allocate PERQUEUEDATA from the system heap */
107 if (!( pQData = (PERQUEUEDATA *) HeapAlloc( GetProcessHeap(), 0,
108 sizeof(PERQUEUEDATA) ) ))
112 pQData->hWndCapture = pQData->hWndFocus = pQData->hWndActive = 0;
113 pQData->ulRefCount = 1;
114 pQData->nCaptureHT = HTCLIENT;
116 /* Note: We have an independent critical section for the per queue data
117 * since this may be shared by different threads. see AttachThreadInput()
119 InitializeCriticalSection( &pQData->cSection );
120 /* FIXME: not all per queue data critical sections should be global */
121 MakeCriticalSectionGlobal( &pQData->cSection );
123 /* Save perQData globally for 16 bit tasks */
125 pQDataWin16 = pQData;
131 /***********************************************************************
132 * PERQDATA_GetFocusWnd
134 * Get the focus hwnd member in a threadsafe manner
136 HWND PERQDATA_GetFocusWnd( PERQUEUEDATA *pQData )
139 assert(pQData != 0 );
141 EnterCriticalSection( &pQData->cSection );
142 hWndFocus = pQData->hWndFocus;
143 LeaveCriticalSection( &pQData->cSection );
149 /***********************************************************************
150 * PERQDATA_SetFocusWnd
152 * Set the focus hwnd member in a threadsafe manner
154 HWND PERQDATA_SetFocusWnd( PERQUEUEDATA *pQData, HWND hWndFocus )
157 assert(pQData != 0 );
159 EnterCriticalSection( &pQData->cSection );
160 hWndFocusPrv = pQData->hWndFocus;
161 pQData->hWndFocus = hWndFocus;
162 LeaveCriticalSection( &pQData->cSection );
168 /***********************************************************************
169 * PERQDATA_GetActiveWnd
171 * Get the active hwnd member in a threadsafe manner
173 HWND PERQDATA_GetActiveWnd( PERQUEUEDATA *pQData )
176 assert(pQData != 0 );
178 EnterCriticalSection( &pQData->cSection );
179 hWndActive = pQData->hWndActive;
180 LeaveCriticalSection( &pQData->cSection );
186 /***********************************************************************
187 * PERQDATA_SetActiveWnd
189 * Set the active focus hwnd member in a threadsafe manner
191 HWND PERQDATA_SetActiveWnd( PERQUEUEDATA *pQData, HWND hWndActive )
194 assert(pQData != 0 );
196 EnterCriticalSection( &pQData->cSection );
197 hWndActivePrv = pQData->hWndActive;
198 pQData->hWndActive = hWndActive;
199 LeaveCriticalSection( &pQData->cSection );
201 return hWndActivePrv;
205 /***********************************************************************
206 * PERQDATA_GetCaptureWnd
208 * Get the capture hwnd member in a threadsafe manner
210 HWND PERQDATA_GetCaptureWnd( INT *hittest )
213 PERQUEUEDATA *pQData;
216 if (!(queue = QUEUE_Current())) return 0;
217 pQData = queue->pQData;
219 EnterCriticalSection( &pQData->cSection );
220 hWndCapture = pQData->hWndCapture;
221 *hittest = pQData->nCaptureHT;
222 LeaveCriticalSection( &pQData->cSection );
227 /***********************************************************************
228 * PERQDATA_SetCaptureWnd
230 * Set the capture hwnd member in a threadsafe manner
232 HWND PERQDATA_SetCaptureWnd( HWND hWndCapture, INT hittest )
235 PERQUEUEDATA *pQData;
238 if (!(queue = QUEUE_Current())) return 0;
239 pQData = queue->pQData;
241 EnterCriticalSection( &pQData->cSection );
242 hWndCapturePrv = pQData->hWndCapture;
243 pQData->hWndCapture = hWndCapture;
244 pQData->nCaptureHT = hittest;
245 LeaveCriticalSection( &pQData->cSection );
246 return hWndCapturePrv;
251 /***********************************************************************
254 * Function for getting a 32 bit pointer on queue structure. For thread
255 * safeness programmers should use this function instead of GlobalLock to
256 * retrieve a pointer on the structure. QUEUE_Unlock should also be called
257 * when access to the queue structure is not required anymore.
259 MESSAGEQUEUE *QUEUE_Lock( HQUEUE16 hQueue )
263 HeapLock( GetProcessHeap() ); /* FIXME: a bit overkill */
264 queue = GlobalLock16( hQueue );
265 if ( !queue || (queue->magic != QUEUE_MAGIC) )
267 HeapUnlock( GetProcessHeap() );
272 HeapUnlock( GetProcessHeap() );
277 /***********************************************************************
280 * Get the current thread queue, creating it if required.
281 * QUEUE_Unlock is not needed since the queue can only be deleted by
282 * the current thread anyway.
284 MESSAGEQUEUE *QUEUE_Current(void)
289 if (!(hQueue = GetThreadQueue16(0)))
291 if (!(hQueue = InitThreadInput16( 0, 0 ))) return NULL;
294 if ((queue = GlobalLock16( hQueue )))
296 if (queue->magic != QUEUE_MAGIC) queue = NULL;
302 /***********************************************************************
305 * Use with QUEUE_Lock to get a thread safe access to message queue
308 void QUEUE_Unlock( MESSAGEQUEUE *queue )
312 HeapLock( GetProcessHeap() ); /* FIXME: a bit overkill */
314 if ( --queue->lockCount == 0 )
316 if (queue->server_queue)
317 CloseHandle( queue->server_queue );
318 GlobalFree16( queue->self );
321 HeapUnlock( GetProcessHeap() );
326 /***********************************************************************
327 * QUEUE_CreateMsgQueue
329 * Creates a message queue. Doesn't link it into queue list!
331 static HQUEUE16 QUEUE_CreateMsgQueue( BOOL16 bCreatePerQData )
335 MESSAGEQUEUE * msgQueue;
337 TRACE_(msg)("(): Creating message queue...\n");
339 if (!(hQueue = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT,
340 sizeof(MESSAGEQUEUE) )))
343 msgQueue = (MESSAGEQUEUE *) GlobalLock16( hQueue );
349 SERVER_START_REQ( get_msg_queue )
351 wine_server_call_err( req );
352 handle = reply->handle;
357 ERR_(msg)("Cannot get thread queue");
358 GlobalFree16( hQueue );
361 msgQueue->server_queue = handle;
364 msgQueue->self = hQueue;
365 msgQueue->lockCount = 1;
366 msgQueue->magic = QUEUE_MAGIC;
368 /* Create and initialize our per queue data */
369 msgQueue->pQData = bCreatePerQData ? PERQDATA_CreateInstance() : NULL;
375 /***********************************************************************
376 * QUEUE_DeleteMsgQueue
378 * Unlinks and deletes a message queue.
380 * Note: We need to mask asynchronous events to make sure PostMessage works
381 * even in the signal handler.
383 void QUEUE_DeleteMsgQueue(void)
385 HQUEUE16 hQueue = GetThreadQueue16(0);
386 MESSAGEQUEUE * msgQueue;
388 if (!hQueue) return; /* thread doesn't have a queue */
390 TRACE("(): Deleting message queue %04x\n", hQueue);
392 if (!(msgQueue = QUEUE_Lock(hQueue)))
394 ERR("invalid thread queue\n");
400 if( hActiveQueue == hQueue ) hActiveQueue = 0;
402 HeapLock( GetProcessHeap() ); /* FIXME: a bit overkill */
404 /* Release per queue data if present */
405 if ( msgQueue->pQData )
407 PERQDATA_Release( msgQueue->pQData );
408 msgQueue->pQData = 0;
413 HeapUnlock( GetProcessHeap() );
414 SetThreadQueue16( 0, 0 );
416 /* free up resource used by MESSAGEQUEUE structure */
417 msgQueue->lockCount--;
418 QUEUE_Unlock( msgQueue );
422 /***********************************************************************
423 * GetWindowTask (USER.224)
425 HTASK16 WINAPI GetWindowTask16( HWND16 hwnd )
430 WND *wndPtr = WIN_FindWndPtr16( hwnd );
431 if (!wndPtr) return 0;
433 queue = QUEUE_Lock( wndPtr->hmemTaskQ );
434 WIN_ReleaseWndPtr(wndPtr);
436 if (!queue) return 0;
438 retvalue = queue->teb->htask16;
439 QUEUE_Unlock( queue );
444 /***********************************************************************
445 * InitThreadInput (USER.409)
447 HQUEUE16 WINAPI InitThreadInput16( WORD unknown, WORD flags )
449 MESSAGEQUEUE *queuePtr;
450 HQUEUE16 hQueue = NtCurrentTeb()->queue;
454 /* Create thread message queue */
455 if( !(hQueue = QUEUE_CreateMsgQueue( TRUE )))
457 ERR_(msg)("failed!\n");
461 /* Link new queue into list */
462 queuePtr = QUEUE_Lock( hQueue );
463 queuePtr->teb = NtCurrentTeb();
465 HeapLock( GetProcessHeap() ); /* FIXME: a bit overkill */
466 SetThreadQueue16( 0, hQueue );
467 NtCurrentTeb()->queue = hQueue;
468 HeapUnlock( GetProcessHeap() );
470 QUEUE_Unlock( queuePtr );
476 /***********************************************************************
477 * GetQueueStatus (USER32.@)
479 DWORD WINAPI GetQueueStatus( UINT flags )
483 SERVER_START_REQ( get_queue_status )
486 wine_server_call( req );
487 ret = MAKELONG( reply->changed_bits & flags, reply->wake_bits & flags );
494 /***********************************************************************
495 * GetInputState (USER32.@)
497 BOOL WINAPI GetInputState(void)
501 SERVER_START_REQ( get_queue_status )
504 wine_server_call( req );
505 ret = reply->wake_bits & (QS_KEY | QS_MOUSEBUTTON);
511 /***********************************************************************
512 * GetMessagePos (USER.119)
513 * GetMessagePos (USER32.@)
515 * The GetMessagePos() function returns a long value representing a
516 * cursor position, in screen coordinates, when the last message
517 * retrieved by the GetMessage() function occurs. The x-coordinate is
518 * in the low-order word of the return value, the y-coordinate is in
519 * the high-order word. The application can use the MAKEPOINT()
520 * macro to obtain a POINT structure from the return value.
522 * For the current cursor position, use GetCursorPos().
526 * Cursor position of last message on success, zero on failure.
533 DWORD WINAPI GetMessagePos(void)
537 if (!(queue = QUEUE_Current())) return 0;
538 return queue->GetMessagePosVal;
542 /***********************************************************************
543 * GetMessageTime (USER.120)
544 * GetMessageTime (USER32.@)
546 * GetMessageTime() returns the message time for the last message
547 * retrieved by the function. The time is measured in milliseconds with
548 * the same offset as GetTickCount().
550 * Since the tick count wraps, this is only useful for moderately short
551 * relative time comparisons.
555 * Time of last message on success, zero on failure.
562 LONG WINAPI GetMessageTime(void)
566 if (!(queue = QUEUE_Current())) return 0;
567 return queue->GetMessageTimeVal;
571 /***********************************************************************
572 * GetMessageExtraInfo (USER.288)
573 * GetMessageExtraInfo (USER32.@)
575 LONG WINAPI GetMessageExtraInfo(void)
579 if (!(queue = QUEUE_Current())) return 0;
580 return queue->GetMessageExtraInfoVal;
584 /**********************************************************************
585 * AttachThreadInput (USER32.@) Attaches input of 1 thread to other
587 * Attaches the input processing mechanism of one thread to that of
595 * 1. Reset the Key State (currenly per thread key state is not maintained)
597 BOOL WINAPI AttachThreadInput(
598 DWORD idAttach, /* [in] Thread to attach */
599 DWORD idAttachTo, /* [in] Thread to attach to */
600 BOOL fAttach) /* [in] Attach or detach */
602 MESSAGEQUEUE *pSrcMsgQ = 0, *pTgtMsgQ = 0;
605 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
607 /* A thread cannot attach to itself */
608 if ( idAttach == idAttachTo )
611 /* According to the docs this method should fail if a
612 * "Journal record" hook is installed. (attaches all input queues together)
614 if ( HOOK_IsHooked( WH_JOURNALRECORD ) )
617 /* Retrieve message queues corresponding to the thread id's */
618 pTgtMsgQ = QUEUE_Lock( GetThreadQueue16( idAttach ) );
619 pSrcMsgQ = QUEUE_Lock( GetThreadQueue16( idAttachTo ) );
621 /* Ensure we have message queues and that Src and Tgt threads
622 * are not system threads.
624 if ( !pSrcMsgQ || !pTgtMsgQ || !pSrcMsgQ->pQData || !pTgtMsgQ->pQData )
627 if (fAttach) /* Attach threads */
629 /* Only attach if currently detached */
630 if ( pTgtMsgQ->pQData != pSrcMsgQ->pQData )
632 /* First release the target threads perQData */
633 PERQDATA_Release( pTgtMsgQ->pQData );
635 /* Share a reference to the source threads perQDATA */
636 PERQDATA_Addref( pSrcMsgQ->pQData );
637 pTgtMsgQ->pQData = pSrcMsgQ->pQData;
640 else /* Detach threads */
642 /* Only detach if currently attached */
643 if ( pTgtMsgQ->pQData == pSrcMsgQ->pQData )
645 /* First release the target threads perQData */
646 PERQDATA_Release( pTgtMsgQ->pQData );
648 /* Give the target thread its own private perQDATA once more */
649 pTgtMsgQ->pQData = PERQDATA_CreateInstance();
653 /* TODO: Reset the Key State */
655 bRet = 1; /* Success */
659 /* Unlock the queues before returning */
661 QUEUE_Unlock( pSrcMsgQ );
663 QUEUE_Unlock( pTgtMsgQ );