2 * Message queues related functions
4 * Copyright 1993, 1994 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "wine/winbase16.h"
28 #include "wine/winuser16.h"
34 #include "wine/debug.h"
35 #include "wine/server.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(msg
);
41 static PERQUEUEDATA
*pQDataWin16
= NULL
; /* Global perQData for Win16 tasks */
43 HQUEUE16 hActiveQueue
= 0;
46 /***********************************************************************
49 * Increment reference count for the PERQUEUEDATA instance
50 * Returns reference count for debugging purposes
52 static void PERQDATA_Addref( PERQUEUEDATA
*pQData
)
55 TRACE_(msg
)("(): current refcount %lu ...\n", pQData
->ulRefCount
);
57 InterlockedIncrement( &pQData
->ulRefCount
);
61 /***********************************************************************
64 * Release a reference to a PERQUEUEDATA instance.
65 * Destroy the instance if no more references exist
66 * Returns reference count for debugging purposes
68 static void PERQDATA_Release( PERQUEUEDATA
*pQData
)
71 TRACE_(msg
)("(): current refcount %lu ...\n",
72 (LONG
)pQData
->ulRefCount
);
74 if (!InterlockedDecrement( &pQData
->ulRefCount
))
76 DeleteCriticalSection( &pQData
->cSection
);
78 TRACE_(msg
)("(): deleting PERQUEUEDATA instance ...\n" );
80 /* Deleting our global 16 bit perQData? */
81 if ( pQData
== pQDataWin16
) pQDataWin16
= 0;
83 /* Free the PERQUEUEDATA instance */
84 HeapFree( GetProcessHeap(), 0, pQData
);
89 /***********************************************************************
90 * PERQDATA_CreateInstance
92 * Creates an instance of a reference counted PERQUEUEDATA element
93 * for the message queue. perQData is stored globally for 16 bit tasks.
95 * Note: We don't implement perQdata exactly the same way Windows does.
96 * Each perQData element is reference counted since it may be potentially
97 * shared by multiple message Queues (via AttachThreadInput).
98 * We only store the current values for Active, Capture and focus windows
101 static PERQUEUEDATA
* PERQDATA_CreateInstance(void)
103 PERQUEUEDATA
*pQData
;
109 /* Share a single instance of perQData for all 16 bit tasks */
110 if ( ( bIsWin16
= !(NtCurrentTeb()->tibflags
& TEBF_WIN32
) ) )
112 /* If previously allocated, just bump up ref count */
115 PERQDATA_Addref( pQDataWin16
);
120 /* Allocate PERQUEUEDATA from the system heap */
121 if (!( pQData
= (PERQUEUEDATA
*) HeapAlloc( GetProcessHeap(), 0,
122 sizeof(PERQUEUEDATA
) ) ))
126 pQData
->hWndCapture
= pQData
->hWndFocus
= pQData
->hWndActive
= 0;
127 pQData
->ulRefCount
= 1;
128 pQData
->nCaptureHT
= HTCLIENT
;
130 /* Note: We have an independent critical section for the per queue data
131 * since this may be shared by different threads. see AttachThreadInput()
133 InitializeCriticalSection( &pQData
->cSection
);
134 /* FIXME: not all per queue data critical sections should be global */
135 MakeCriticalSectionGlobal( &pQData
->cSection
);
137 /* Save perQData globally for 16 bit tasks */
139 pQDataWin16
= pQData
;
145 /***********************************************************************
146 * PERQDATA_GetFocusWnd
148 * Get the focus hwnd member in a threadsafe manner
150 HWND
PERQDATA_GetFocusWnd( PERQUEUEDATA
*pQData
)
153 assert(pQData
!= 0 );
155 EnterCriticalSection( &pQData
->cSection
);
156 hWndFocus
= pQData
->hWndFocus
;
157 LeaveCriticalSection( &pQData
->cSection
);
163 /***********************************************************************
164 * PERQDATA_SetFocusWnd
166 * Set the focus hwnd member in a threadsafe manner
168 HWND
PERQDATA_SetFocusWnd( PERQUEUEDATA
*pQData
, HWND hWndFocus
)
171 assert(pQData
!= 0 );
173 EnterCriticalSection( &pQData
->cSection
);
174 hWndFocusPrv
= pQData
->hWndFocus
;
175 pQData
->hWndFocus
= hWndFocus
;
176 LeaveCriticalSection( &pQData
->cSection
);
182 /***********************************************************************
183 * PERQDATA_GetActiveWnd
185 * Get the active hwnd member in a threadsafe manner
187 HWND
PERQDATA_GetActiveWnd( PERQUEUEDATA
*pQData
)
190 assert(pQData
!= 0 );
192 EnterCriticalSection( &pQData
->cSection
);
193 hWndActive
= pQData
->hWndActive
;
194 LeaveCriticalSection( &pQData
->cSection
);
200 /***********************************************************************
201 * PERQDATA_SetActiveWnd
203 * Set the active focus hwnd member in a threadsafe manner
205 HWND
PERQDATA_SetActiveWnd( PERQUEUEDATA
*pQData
, HWND hWndActive
)
208 assert(pQData
!= 0 );
210 EnterCriticalSection( &pQData
->cSection
);
211 hWndActivePrv
= pQData
->hWndActive
;
212 pQData
->hWndActive
= hWndActive
;
213 LeaveCriticalSection( &pQData
->cSection
);
215 return hWndActivePrv
;
219 /***********************************************************************
220 * PERQDATA_GetCaptureWnd
222 * Get the capture hwnd member in a threadsafe manner
224 HWND
PERQDATA_GetCaptureWnd( INT
*hittest
)
227 PERQUEUEDATA
*pQData
;
230 if (!(queue
= QUEUE_Current())) return 0;
231 pQData
= queue
->pQData
;
233 EnterCriticalSection( &pQData
->cSection
);
234 hWndCapture
= pQData
->hWndCapture
;
235 *hittest
= pQData
->nCaptureHT
;
236 LeaveCriticalSection( &pQData
->cSection
);
241 /***********************************************************************
242 * PERQDATA_SetCaptureWnd
244 * Set the capture hwnd member in a threadsafe manner
246 HWND
PERQDATA_SetCaptureWnd( HWND hWndCapture
, INT hittest
)
249 PERQUEUEDATA
*pQData
;
252 if (!(queue
= QUEUE_Current())) return 0;
253 pQData
= queue
->pQData
;
255 EnterCriticalSection( &pQData
->cSection
);
256 hWndCapturePrv
= pQData
->hWndCapture
;
257 pQData
->hWndCapture
= hWndCapture
;
258 pQData
->nCaptureHT
= hittest
;
259 LeaveCriticalSection( &pQData
->cSection
);
260 return hWndCapturePrv
;
265 /***********************************************************************
268 * Function for getting a 32 bit pointer on queue structure. For thread
269 * safeness programmers should use this function instead of GlobalLock to
270 * retrieve a pointer on the structure. QUEUE_Unlock should also be called
271 * when access to the queue structure is not required anymore.
273 MESSAGEQUEUE
*QUEUE_Lock( HQUEUE16 hQueue
)
277 HeapLock( GetProcessHeap() ); /* FIXME: a bit overkill */
278 queue
= GlobalLock16( hQueue
);
279 if ( !queue
|| (queue
->magic
!= QUEUE_MAGIC
) )
281 HeapUnlock( GetProcessHeap() );
286 HeapUnlock( GetProcessHeap() );
291 /***********************************************************************
294 * Get the current thread queue, creating it if required.
295 * QUEUE_Unlock is not needed since the queue can only be deleted by
296 * the current thread anyway.
298 MESSAGEQUEUE
*QUEUE_Current(void)
303 if (!(hQueue
= GetThreadQueue16(0)))
305 if (!(hQueue
= InitThreadInput16( 0, 0 ))) return NULL
;
308 if ((queue
= GlobalLock16( hQueue
)))
310 if (queue
->magic
!= QUEUE_MAGIC
) queue
= NULL
;
316 /***********************************************************************
319 * Use with QUEUE_Lock to get a thread safe access to message queue
322 void QUEUE_Unlock( MESSAGEQUEUE
*queue
)
326 HeapLock( GetProcessHeap() ); /* FIXME: a bit overkill */
328 if ( --queue
->lockCount
== 0 )
330 if (queue
->server_queue
)
331 CloseHandle( queue
->server_queue
);
332 GlobalFree16( queue
->self
);
335 HeapUnlock( GetProcessHeap() );
340 /***********************************************************************
341 * QUEUE_CreateMsgQueue
343 * Creates a message queue. Doesn't link it into queue list!
345 static HQUEUE16
QUEUE_CreateMsgQueue( BOOL16 bCreatePerQData
)
349 MESSAGEQUEUE
* msgQueue
;
351 TRACE_(msg
)("(): Creating message queue...\n");
353 if (!(hQueue
= GlobalAlloc16( GMEM_FIXED
| GMEM_ZEROINIT
,
354 sizeof(MESSAGEQUEUE
) )))
357 msgQueue
= (MESSAGEQUEUE
*) GlobalLock16( hQueue
);
363 SERVER_START_REQ( get_msg_queue
)
365 wine_server_call_err( req
);
366 handle
= reply
->handle
;
371 ERR_(msg
)("Cannot get thread queue");
372 GlobalFree16( hQueue
);
375 msgQueue
->server_queue
= handle
;
378 msgQueue
->self
= hQueue
;
379 msgQueue
->lockCount
= 1;
380 msgQueue
->magic
= QUEUE_MAGIC
;
382 /* Create and initialize our per queue data */
383 msgQueue
->pQData
= bCreatePerQData
? PERQDATA_CreateInstance() : NULL
;
389 /***********************************************************************
390 * QUEUE_DeleteMsgQueue
392 * Unlinks and deletes a message queue.
394 * Note: We need to mask asynchronous events to make sure PostMessage works
395 * even in the signal handler.
397 void QUEUE_DeleteMsgQueue(void)
399 HQUEUE16 hQueue
= GetThreadQueue16(0);
400 MESSAGEQUEUE
* msgQueue
;
402 if (!hQueue
) return; /* thread doesn't have a queue */
404 TRACE("(): Deleting message queue %04x\n", hQueue
);
406 if (!(msgQueue
= QUEUE_Lock(hQueue
)))
408 ERR("invalid thread queue\n");
414 if( hActiveQueue
== hQueue
) hActiveQueue
= 0;
416 HeapLock( GetProcessHeap() ); /* FIXME: a bit overkill */
418 /* Release per queue data if present */
419 if ( msgQueue
->pQData
)
421 PERQDATA_Release( msgQueue
->pQData
);
422 msgQueue
->pQData
= 0;
427 HeapUnlock( GetProcessHeap() );
428 SetThreadQueue16( 0, 0 );
430 /* free up resource used by MESSAGEQUEUE structure */
431 msgQueue
->lockCount
--;
432 QUEUE_Unlock( msgQueue
);
436 /***********************************************************************
437 * GetWindowTask (USER.224)
439 HTASK16 WINAPI
GetWindowTask16( HWND16 hwnd
)
444 WND
*wndPtr
= WIN_FindWndPtr16( hwnd
);
445 if (!wndPtr
) return 0;
447 queue
= QUEUE_Lock( wndPtr
->hmemTaskQ
);
448 WIN_ReleaseWndPtr(wndPtr
);
450 if (!queue
) return 0;
452 retvalue
= queue
->teb
->htask16
;
453 QUEUE_Unlock( queue
);
458 /***********************************************************************
459 * InitThreadInput (USER.409)
460 * InitThreadInput16 (USER32.@)
462 HQUEUE16 WINAPI
InitThreadInput16( WORD unknown
, WORD flags
)
464 MESSAGEQUEUE
*queuePtr
;
465 HQUEUE16 hQueue
= NtCurrentTeb()->queue
;
469 /* Create thread message queue */
470 if( !(hQueue
= QUEUE_CreateMsgQueue( TRUE
)))
472 ERR_(msg
)("failed!\n");
476 /* Link new queue into list */
477 queuePtr
= QUEUE_Lock( hQueue
);
478 queuePtr
->teb
= NtCurrentTeb();
480 HeapLock( GetProcessHeap() ); /* FIXME: a bit overkill */
481 SetThreadQueue16( 0, hQueue
);
482 NtCurrentTeb()->queue
= hQueue
;
483 HeapUnlock( GetProcessHeap() );
485 QUEUE_Unlock( queuePtr
);
491 /***********************************************************************
492 * GetQueueStatus (USER32.@)
494 DWORD WINAPI
GetQueueStatus( UINT flags
)
498 SERVER_START_REQ( get_queue_status
)
501 wine_server_call( req
);
502 ret
= MAKELONG( reply
->changed_bits
& flags
, reply
->wake_bits
& flags
);
509 /***********************************************************************
510 * GetInputState (USER32.@)
512 BOOL WINAPI
GetInputState(void)
516 SERVER_START_REQ( get_queue_status
)
519 wine_server_call( req
);
520 ret
= reply
->wake_bits
& (QS_KEY
| QS_MOUSEBUTTON
);
526 /***********************************************************************
527 * GetMessagePos (USER.119)
528 * GetMessagePos (USER32.@)
530 * The GetMessagePos() function returns a long value representing a
531 * cursor position, in screen coordinates, when the last message
532 * retrieved by the GetMessage() function occurs. The x-coordinate is
533 * in the low-order word of the return value, the y-coordinate is in
534 * the high-order word. The application can use the MAKEPOINT()
535 * macro to obtain a POINT structure from the return value.
537 * For the current cursor position, use GetCursorPos().
541 * Cursor position of last message on success, zero on failure.
548 DWORD WINAPI
GetMessagePos(void)
552 if (!(queue
= QUEUE_Current())) return 0;
553 return queue
->GetMessagePosVal
;
557 /***********************************************************************
558 * GetMessageTime (USER.120)
559 * GetMessageTime (USER32.@)
561 * GetMessageTime() returns the message time for the last message
562 * retrieved by the function. The time is measured in milliseconds with
563 * the same offset as GetTickCount().
565 * Since the tick count wraps, this is only useful for moderately short
566 * relative time comparisons.
570 * Time of last message on success, zero on failure.
577 LONG WINAPI
GetMessageTime(void)
581 if (!(queue
= QUEUE_Current())) return 0;
582 return queue
->GetMessageTimeVal
;
586 /***********************************************************************
587 * GetMessageExtraInfo (USER.288)
588 * GetMessageExtraInfo (USER32.@)
590 LONG WINAPI
GetMessageExtraInfo(void)
594 if (!(queue
= QUEUE_Current())) return 0;
595 return queue
->GetMessageExtraInfoVal
;
599 /**********************************************************************
600 * AttachThreadInput (USER32.@) Attaches input of 1 thread to other
602 * Attaches the input processing mechanism of one thread to that of
610 * 1. Reset the Key State (currenly per thread key state is not maintained)
612 BOOL WINAPI
AttachThreadInput(
613 DWORD idAttach
, /* [in] Thread to attach */
614 DWORD idAttachTo
, /* [in] Thread to attach to */
615 BOOL fAttach
) /* [in] Attach or detach */
617 MESSAGEQUEUE
*pSrcMsgQ
= 0, *pTgtMsgQ
= 0;
620 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
622 /* A thread cannot attach to itself */
623 if ( idAttach
== idAttachTo
)
626 /* According to the docs this method should fail if a
627 * "Journal record" hook is installed. (attaches all input queues together)
629 if ( HOOK_IsHooked( WH_JOURNALRECORD
) )
632 /* Retrieve message queues corresponding to the thread id's */
633 pTgtMsgQ
= QUEUE_Lock( GetThreadQueue16( idAttach
) );
634 pSrcMsgQ
= QUEUE_Lock( GetThreadQueue16( idAttachTo
) );
636 /* Ensure we have message queues and that Src and Tgt threads
637 * are not system threads.
639 if ( !pSrcMsgQ
|| !pTgtMsgQ
|| !pSrcMsgQ
->pQData
|| !pTgtMsgQ
->pQData
)
642 if (fAttach
) /* Attach threads */
644 /* Only attach if currently detached */
645 if ( pTgtMsgQ
->pQData
!= pSrcMsgQ
->pQData
)
647 /* First release the target threads perQData */
648 PERQDATA_Release( pTgtMsgQ
->pQData
);
650 /* Share a reference to the source threads perQDATA */
651 PERQDATA_Addref( pSrcMsgQ
->pQData
);
652 pTgtMsgQ
->pQData
= pSrcMsgQ
->pQData
;
655 else /* Detach threads */
657 /* Only detach if currently attached */
658 if ( pTgtMsgQ
->pQData
== pSrcMsgQ
->pQData
)
660 /* First release the target threads perQData */
661 PERQDATA_Release( pTgtMsgQ
->pQData
);
663 /* Give the target thread its own private perQDATA once more */
664 pTgtMsgQ
->pQData
= PERQDATA_CreateInstance();
668 /* TODO: Reset the Key State */
670 bRet
= 1; /* Success */
674 /* Unlock the queues before returning */
676 QUEUE_Unlock( pSrcMsgQ
);
678 QUEUE_Unlock( pTgtMsgQ
);