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"
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_Lock( GetFastQueue16() ))) return 0;
218 pQData
= queue
->pQData
;
220 EnterCriticalSection( &pQData
->cSection
);
221 hWndCapture
= pQData
->hWndCapture
;
222 *hittest
= pQData
->nCaptureHT
;
223 LeaveCriticalSection( &pQData
->cSection
);
225 QUEUE_Unlock( queue
);
230 /***********************************************************************
231 * PERQDATA_SetCaptureWnd
233 * Set the capture hwnd member in a threadsafe manner
235 HWND
PERQDATA_SetCaptureWnd( HWND hWndCapture
, INT hittest
)
238 PERQUEUEDATA
*pQData
;
241 if (!(queue
= QUEUE_Lock( GetFastQueue16() ))) return 0;
242 pQData
= queue
->pQData
;
244 EnterCriticalSection( &pQData
->cSection
);
245 hWndCapturePrv
= pQData
->hWndCapture
;
246 pQData
->hWndCapture
= hWndCapture
;
247 pQData
->nCaptureHT
= hittest
;
248 LeaveCriticalSection( &pQData
->cSection
);
250 QUEUE_Unlock( queue
);
251 return hWndCapturePrv
;
256 /***********************************************************************
259 * Function for getting a 32 bit pointer on queue structure. For thread
260 * safeness programmers should use this function instead of GlobalLock to
261 * retrieve a pointer on the structure. QUEUE_Unlock should also be called
262 * when access to the queue structure is not required anymore.
264 MESSAGEQUEUE
*QUEUE_Lock( HQUEUE16 hQueue
)
268 HeapLock( GetProcessHeap() ); /* FIXME: a bit overkill */
269 queue
= GlobalLock16( hQueue
);
270 if ( !queue
|| (queue
->magic
!= QUEUE_MAGIC
) )
272 HeapUnlock( GetProcessHeap() );
277 HeapUnlock( GetProcessHeap() );
282 /***********************************************************************
285 * Use with QUEUE_Lock to get a thread safe access to message queue
288 void QUEUE_Unlock( MESSAGEQUEUE
*queue
)
292 HeapLock( GetProcessHeap() ); /* FIXME: a bit overkill */
294 if ( --queue
->lockCount
== 0 )
296 if (queue
->server_queue
)
297 CloseHandle( queue
->server_queue
);
298 GlobalFree16( queue
->self
);
301 HeapUnlock( GetProcessHeap() );
306 /***********************************************************************
307 * QUEUE_IsExitingQueue
309 BOOL
QUEUE_IsExitingQueue( HQUEUE16 hQueue
)
311 return (hExitingQueue
&& (hQueue
== hExitingQueue
));
315 /***********************************************************************
316 * QUEUE_SetExitingQueue
318 void QUEUE_SetExitingQueue( HQUEUE16 hQueue
)
320 hExitingQueue
= hQueue
;
324 /***********************************************************************
325 * QUEUE_CreateMsgQueue
327 * Creates a message queue. Doesn't link it into queue list!
329 static HQUEUE16
QUEUE_CreateMsgQueue( BOOL16 bCreatePerQData
)
333 MESSAGEQUEUE
* msgQueue
;
335 TRACE_(msg
)("(): Creating message queue...\n");
337 if (!(hQueue
= GlobalAlloc16( GMEM_FIXED
| GMEM_ZEROINIT
,
338 sizeof(MESSAGEQUEUE
) )))
341 msgQueue
= (MESSAGEQUEUE
*) GlobalLock16( hQueue
);
347 SERVER_START_REQ( get_msg_queue
)
350 handle
= req
->handle
;
355 ERR_(msg
)("Cannot get thread queue");
356 GlobalFree16( hQueue
);
359 msgQueue
->server_queue
= handle
;
362 msgQueue
->self
= hQueue
;
363 msgQueue
->lockCount
= 1;
364 msgQueue
->magic
= QUEUE_MAGIC
;
366 /* Create and initialize our per queue data */
367 msgQueue
->pQData
= bCreatePerQData
? PERQDATA_CreateInstance() : NULL
;
373 /***********************************************************************
374 * QUEUE_DeleteMsgQueue
376 * Unlinks and deletes a message queue.
378 * Note: We need to mask asynchronous events to make sure PostMessage works
379 * even in the signal handler.
381 BOOL
QUEUE_DeleteMsgQueue( HQUEUE16 hQueue
)
383 MESSAGEQUEUE
* msgQueue
= QUEUE_Lock(hQueue
);
385 TRACE_(msg
)("(): Deleting message queue %04x\n", hQueue
);
387 if (!hQueue
|| !msgQueue
)
389 ERR_(msg
)("invalid argument.\n");
395 if( hActiveQueue
== hQueue
) hActiveQueue
= 0;
396 if (hExitingQueue
== hQueue
) hExitingQueue
= 0;
398 HeapLock( GetProcessHeap() ); /* FIXME: a bit overkill */
400 /* Release per queue data if present */
401 if ( msgQueue
->pQData
)
403 PERQDATA_Release( msgQueue
->pQData
);
404 msgQueue
->pQData
= 0;
409 HeapUnlock( GetProcessHeap() );
411 /* free up resource used by MESSAGEQUEUE structure */
412 msgQueue
->lockCount
--;
413 QUEUE_Unlock( msgQueue
);
419 /***********************************************************************
420 * QUEUE_CleanupWindow
422 * Cleanup the queue to account for a window being deleted.
424 void QUEUE_CleanupWindow( HWND hwnd
)
426 SERVER_START_REQ( cleanup_window_queue
)
435 /***********************************************************************
438 HTASK16
QUEUE_GetQueueTask( HQUEUE16 hQueue
)
442 MESSAGEQUEUE
*queue
= QUEUE_Lock( hQueue
);
446 hTask
= queue
->teb
->htask16
;
447 QUEUE_Unlock( queue
);
454 /***********************************************************************
455 * PostQuitMessage (USER.6)
457 void WINAPI
PostQuitMessage16( INT16 exitCode
)
459 PostQuitMessage( exitCode
);
463 /***********************************************************************
464 * PostQuitMessage (USER32.@)
466 * PostQuitMessage() posts a message to the system requesting an
467 * application to terminate execution. As a result of this function,
468 * the WM_QUIT message is posted to the application, and
469 * PostQuitMessage() returns immediately. The exitCode parameter
470 * specifies an application-defined exit code, which appears in the
471 * _wParam_ parameter of the WM_QUIT message posted to the application.
477 void WINAPI
PostQuitMessage( INT exitCode
)
479 PostThreadMessageW( GetCurrentThreadId(), WM_QUIT
, exitCode
, 0 );
483 /***********************************************************************
484 * GetWindowTask (USER.224)
486 HTASK16 WINAPI
GetWindowTask16( HWND16 hwnd
)
489 WND
*wndPtr
= WIN_FindWndPtr( hwnd
);
491 if (!wndPtr
) return 0;
492 retvalue
= QUEUE_GetQueueTask( wndPtr
->hmemTaskQ
);
493 WIN_ReleaseWndPtr(wndPtr
);
497 /***********************************************************************
498 * GetWindowThreadProcessId (USER32.@)
500 DWORD WINAPI
GetWindowThreadProcessId( HWND hwnd
, LPDWORD process
)
505 WND
*wndPtr
= WIN_FindWndPtr( hwnd
);
506 if (!wndPtr
) return 0;
508 queue
= QUEUE_Lock( wndPtr
->hmemTaskQ
);
509 WIN_ReleaseWndPtr(wndPtr
);
511 if (!queue
) return 0;
513 if ( process
) *process
= (DWORD
)queue
->teb
->pid
;
514 retvalue
= (DWORD
)queue
->teb
->tid
;
516 QUEUE_Unlock( queue
);
521 /***********************************************************************
522 * SetMessageQueue (USER.266)
524 BOOL16 WINAPI
SetMessageQueue16( INT16 size
)
526 return SetMessageQueue( size
);
530 /***********************************************************************
531 * SetMessageQueue (USER32.@)
533 BOOL WINAPI
SetMessageQueue( INT size
)
535 /* now obsolete the message queue will be expanded dynamically
538 /* access the queue to create it if it's not existing */
544 /***********************************************************************
545 * InitThreadInput (USER.409)
547 HQUEUE16 WINAPI
InitThreadInput16( WORD unknown
, WORD flags
)
550 MESSAGEQUEUE
*queuePtr
;
552 TEB
*teb
= NtCurrentTeb();
561 /* Create thread message queue */
562 if( !(hQueue
= QUEUE_CreateMsgQueue( TRUE
)))
564 ERR_(msg
)("failed!\n");
568 /* Link new queue into list */
569 queuePtr
= QUEUE_Lock( hQueue
);
570 queuePtr
->teb
= NtCurrentTeb();
572 HeapLock( GetProcessHeap() ); /* FIXME: a bit overkill */
573 SetThreadQueue16( 0, hQueue
);
575 HeapUnlock( GetProcessHeap() );
577 QUEUE_Unlock( queuePtr
);
583 /***********************************************************************
584 * GetQueueStatus (USER.334)
586 DWORD WINAPI
GetQueueStatus16( UINT16 flags
)
588 return GetQueueStatus( flags
);
591 /***********************************************************************
592 * GetQueueStatus (USER32.@)
594 DWORD WINAPI
GetQueueStatus( UINT flags
)
598 SERVER_START_REQ( get_queue_status
)
602 ret
= MAKELONG( req
->changed_bits
& flags
, req
->wake_bits
& flags
);
609 /***********************************************************************
610 * GetInputState (USER.335)
612 BOOL16 WINAPI
GetInputState16(void)
614 return GetInputState();
617 /***********************************************************************
618 * GetInputState (USER32.@)
620 BOOL WINAPI
GetInputState(void)
624 SERVER_START_REQ( get_queue_status
)
628 ret
= req
->wake_bits
& (QS_KEY
| QS_MOUSEBUTTON
);
634 /***********************************************************************
635 * GetMessagePos (USER.119)
636 * GetMessagePos (USER32.@)
638 * The GetMessagePos() function returns a long value representing a
639 * cursor position, in screen coordinates, when the last message
640 * retrieved by the GetMessage() function occurs. The x-coordinate is
641 * in the low-order word of the return value, the y-coordinate is in
642 * the high-order word. The application can use the MAKEPOINT()
643 * macro to obtain a POINT structure from the return value.
645 * For the current cursor position, use GetCursorPos().
649 * Cursor position of last message on success, zero on failure.
656 DWORD WINAPI
GetMessagePos(void)
661 if (!(queue
= QUEUE_Lock( GetFastQueue16() ))) return 0;
662 ret
= queue
->GetMessagePosVal
;
663 QUEUE_Unlock( queue
);
669 /***********************************************************************
670 * GetMessageTime (USER.120)
671 * GetMessageTime (USER32.@)
673 * GetMessageTime() returns the message time for the last message
674 * retrieved by the function. The time is measured in milliseconds with
675 * the same offset as GetTickCount().
677 * Since the tick count wraps, this is only useful for moderately short
678 * relative time comparisons.
682 * Time of last message on success, zero on failure.
689 LONG WINAPI
GetMessageTime(void)
694 if (!(queue
= QUEUE_Lock( GetFastQueue16() ))) return 0;
695 ret
= queue
->GetMessageTimeVal
;
696 QUEUE_Unlock( queue
);
702 /***********************************************************************
703 * GetMessageExtraInfo (USER.288)
704 * GetMessageExtraInfo (USER32.@)
706 LONG WINAPI
GetMessageExtraInfo(void)
711 if (!(queue
= QUEUE_Lock( GetFastQueue16() ))) return 0;
712 ret
= queue
->GetMessageExtraInfoVal
;
713 QUEUE_Unlock( queue
);
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
);