Made all 16<->32 HWND conversions use explicit functions instead of
[wine/multimedia.git] / windows / queue.c
blobb9585209cb532faad2a35be1b1046865dfbbd625
1 /*
2 * Message queues related functions
4 * Copyright 1993, 1994 Alexandre Julliard
5 */
7 #include <string.h>
8 #include <signal.h>
9 #include <assert.h>
10 #include "windef.h"
11 #include "wingdi.h"
12 #include "winerror.h"
13 #include "wine/winbase16.h"
14 #include "wine/winuser16.h"
15 #include "queue.h"
16 #include "win.h"
17 #include "user.h"
18 #include "hook.h"
19 #include "thread.h"
20 #include "debugtools.h"
21 #include "wine/server.h"
22 #include "spy.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 /***********************************************************************
34 * PERQDATA_Addref
36 * Increment reference count for the PERQUEUEDATA instance
37 * Returns reference count for debugging purposes
39 static void PERQDATA_Addref( PERQUEUEDATA *pQData )
41 assert(pQData != 0 );
42 TRACE_(msg)("(): current refcount %lu ...\n", pQData->ulRefCount);
44 InterlockedIncrement( &pQData->ulRefCount );
48 /***********************************************************************
49 * PERQDATA_Release
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 )
57 assert(pQData != 0 );
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
86 * currently.
88 static PERQUEUEDATA * PERQDATA_CreateInstance(void)
90 PERQUEUEDATA *pQData;
92 BOOL16 bIsWin16 = 0;
94 TRACE_(msg)("()\n");
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 */
100 if ( pQDataWin16 )
102 PERQDATA_Addref( pQDataWin16 );
103 return pQDataWin16;
107 /* Allocate PERQUEUEDATA from the system heap */
108 if (!( pQData = (PERQUEUEDATA *) HeapAlloc( GetProcessHeap(), 0,
109 sizeof(PERQUEUEDATA) ) ))
110 return 0;
112 /* Initialize */
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 */
125 if ( bIsWin16 )
126 pQDataWin16 = pQData;
128 return pQData;
132 /***********************************************************************
133 * PERQDATA_GetFocusWnd
135 * Get the focus hwnd member in a threadsafe manner
137 HWND PERQDATA_GetFocusWnd( PERQUEUEDATA *pQData )
139 HWND hWndFocus;
140 assert(pQData != 0 );
142 EnterCriticalSection( &pQData->cSection );
143 hWndFocus = pQData->hWndFocus;
144 LeaveCriticalSection( &pQData->cSection );
146 return hWndFocus;
150 /***********************************************************************
151 * PERQDATA_SetFocusWnd
153 * Set the focus hwnd member in a threadsafe manner
155 HWND PERQDATA_SetFocusWnd( PERQUEUEDATA *pQData, HWND hWndFocus )
157 HWND hWndFocusPrv;
158 assert(pQData != 0 );
160 EnterCriticalSection( &pQData->cSection );
161 hWndFocusPrv = pQData->hWndFocus;
162 pQData->hWndFocus = hWndFocus;
163 LeaveCriticalSection( &pQData->cSection );
165 return hWndFocusPrv;
169 /***********************************************************************
170 * PERQDATA_GetActiveWnd
172 * Get the active hwnd member in a threadsafe manner
174 HWND PERQDATA_GetActiveWnd( PERQUEUEDATA *pQData )
176 HWND hWndActive;
177 assert(pQData != 0 );
179 EnterCriticalSection( &pQData->cSection );
180 hWndActive = pQData->hWndActive;
181 LeaveCriticalSection( &pQData->cSection );
183 return hWndActive;
187 /***********************************************************************
188 * PERQDATA_SetActiveWnd
190 * Set the active focus hwnd member in a threadsafe manner
192 HWND PERQDATA_SetActiveWnd( PERQUEUEDATA *pQData, HWND hWndActive )
194 HWND hWndActivePrv;
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 )
213 MESSAGEQUEUE *queue;
214 PERQUEUEDATA *pQData;
215 HWND hWndCapture;
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 );
224 return hWndCapture;
228 /***********************************************************************
229 * PERQDATA_SetCaptureWnd
231 * Set the capture hwnd member in a threadsafe manner
233 HWND PERQDATA_SetCaptureWnd( HWND hWndCapture, INT hittest )
235 MESSAGEQUEUE *queue;
236 PERQUEUEDATA *pQData;
237 HWND hWndCapturePrv;
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 /***********************************************************************
253 * QUEUE_Lock
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 )
262 MESSAGEQUEUE *queue;
264 HeapLock( GetProcessHeap() ); /* FIXME: a bit overkill */
265 queue = GlobalLock16( hQueue );
266 if ( !queue || (queue->magic != QUEUE_MAGIC) )
268 HeapUnlock( GetProcessHeap() );
269 return NULL;
272 queue->lockCount++;
273 HeapUnlock( GetProcessHeap() );
274 return queue;
278 /***********************************************************************
279 * QUEUE_Current
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)
287 MESSAGEQUEUE *queue;
288 HQUEUE16 hQueue;
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;
299 return queue;
303 /***********************************************************************
304 * QUEUE_Unlock
306 * Use with QUEUE_Lock to get a thread safe access to message queue
307 * structure
309 void QUEUE_Unlock( MESSAGEQUEUE *queue )
311 if (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 )
352 HQUEUE16 hQueue;
353 HANDLE handle;
354 MESSAGEQUEUE * msgQueue;
356 TRACE_(msg)("(): Creating message queue...\n");
358 if (!(hQueue = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT,
359 sizeof(MESSAGEQUEUE) )))
360 return 0;
362 msgQueue = (MESSAGEQUEUE *) GlobalLock16( hQueue );
363 if ( !msgQueue )
364 return 0;
366 if (bCreatePerQData)
368 SERVER_START_REQ( get_msg_queue )
370 SERVER_CALL_ERR();
371 handle = req->handle;
373 SERVER_END_REQ;
374 if (!handle)
376 ERR_(msg)("Cannot get thread queue");
377 GlobalFree16( hQueue );
378 return 0;
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;
390 return hQueue;
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");
414 return;
417 msgQueue->magic = 0;
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;
431 msgQueue->self = 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 )
451 req->win = hwnd;
452 SERVER_CALL();
454 SERVER_END_REQ;
458 /***********************************************************************
459 * GetWindowTask (USER.224)
461 HTASK16 WINAPI GetWindowTask16( HWND16 hwnd )
463 HTASK16 retvalue;
464 MESSAGEQUEUE *queue;
466 WND *wndPtr = WIN_FindWndPtr16( 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 );
477 return retvalue;
480 /***********************************************************************
481 * InitThreadInput (USER.409)
483 HQUEUE16 WINAPI InitThreadInput16( WORD unknown, WORD flags )
485 MESSAGEQUEUE *queuePtr;
486 HQUEUE16 hQueue = NtCurrentTeb()->queue;
488 if ( !hQueue )
490 /* Create thread message queue */
491 if( !(hQueue = QUEUE_CreateMsgQueue( TRUE )))
493 ERR_(msg)("failed!\n");
494 return FALSE;
497 /* Link new queue into list */
498 queuePtr = QUEUE_Lock( hQueue );
499 queuePtr->teb = NtCurrentTeb();
501 HeapLock( GetProcessHeap() ); /* FIXME: a bit overkill */
502 SetThreadQueue16( 0, hQueue );
503 NtCurrentTeb()->queue = hQueue;
504 HeapUnlock( GetProcessHeap() );
506 QUEUE_Unlock( queuePtr );
509 return hQueue;
512 /***********************************************************************
513 * GetQueueStatus (USER32.@)
515 DWORD WINAPI GetQueueStatus( UINT flags )
517 DWORD ret = 0;
519 SERVER_START_REQ( get_queue_status )
521 req->clear = 1;
522 SERVER_CALL();
523 ret = MAKELONG( req->changed_bits & flags, req->wake_bits & flags );
525 SERVER_END_REQ;
526 return ret;
530 /***********************************************************************
531 * GetInputState (USER32.@)
533 BOOL WINAPI GetInputState(void)
535 DWORD ret = 0;
537 SERVER_START_REQ( get_queue_status )
539 req->clear = 0;
540 SERVER_CALL();
541 ret = req->wake_bits & (QS_KEY | QS_MOUSEBUTTON);
543 SERVER_END_REQ;
544 return ret;
547 /***********************************************************************
548 * GetMessagePos (USER.119)
549 * GetMessagePos (USER32.@)
551 * The GetMessagePos() function returns a long value representing a
552 * cursor position, in screen coordinates, when the last message
553 * retrieved by the GetMessage() function occurs. The x-coordinate is
554 * in the low-order word of the return value, the y-coordinate is in
555 * the high-order word. The application can use the MAKEPOINT()
556 * macro to obtain a POINT structure from the return value.
558 * For the current cursor position, use GetCursorPos().
560 * RETURNS
562 * Cursor position of last message on success, zero on failure.
564 * CONFORMANCE
566 * ECMA-234, Win32
569 DWORD WINAPI GetMessagePos(void)
571 MESSAGEQUEUE *queue;
573 if (!(queue = QUEUE_Current())) return 0;
574 return queue->GetMessagePosVal;
578 /***********************************************************************
579 * GetMessageTime (USER.120)
580 * GetMessageTime (USER32.@)
582 * GetMessageTime() returns the message time for the last message
583 * retrieved by the function. The time is measured in milliseconds with
584 * the same offset as GetTickCount().
586 * Since the tick count wraps, this is only useful for moderately short
587 * relative time comparisons.
589 * RETURNS
591 * Time of last message on success, zero on failure.
593 * CONFORMANCE
595 * ECMA-234, Win32
598 LONG WINAPI GetMessageTime(void)
600 MESSAGEQUEUE *queue;
602 if (!(queue = QUEUE_Current())) return 0;
603 return queue->GetMessageTimeVal;
607 /***********************************************************************
608 * GetMessageExtraInfo (USER.288)
609 * GetMessageExtraInfo (USER32.@)
611 LONG WINAPI GetMessageExtraInfo(void)
613 MESSAGEQUEUE *queue;
615 if (!(queue = QUEUE_Current())) return 0;
616 return queue->GetMessageExtraInfoVal;
620 /**********************************************************************
621 * AttachThreadInput (USER32.@) Attaches input of 1 thread to other
623 * Attaches the input processing mechanism of one thread to that of
624 * another thread.
626 * RETURNS
627 * Success: TRUE
628 * Failure: FALSE
630 * TODO:
631 * 1. Reset the Key State (currenly per thread key state is not maintained)
633 BOOL WINAPI AttachThreadInput(
634 DWORD idAttach, /* [in] Thread to attach */
635 DWORD idAttachTo, /* [in] Thread to attach to */
636 BOOL fAttach) /* [in] Attach or detach */
638 MESSAGEQUEUE *pSrcMsgQ = 0, *pTgtMsgQ = 0;
639 BOOL16 bRet = 0;
641 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
643 /* A thread cannot attach to itself */
644 if ( idAttach == idAttachTo )
645 goto CLEANUP;
647 /* According to the docs this method should fail if a
648 * "Journal record" hook is installed. (attaches all input queues together)
650 if ( HOOK_IsHooked( WH_JOURNALRECORD ) )
651 goto CLEANUP;
653 /* Retrieve message queues corresponding to the thread id's */
654 pTgtMsgQ = QUEUE_Lock( GetThreadQueue16( idAttach ) );
655 pSrcMsgQ = QUEUE_Lock( GetThreadQueue16( idAttachTo ) );
657 /* Ensure we have message queues and that Src and Tgt threads
658 * are not system threads.
660 if ( !pSrcMsgQ || !pTgtMsgQ || !pSrcMsgQ->pQData || !pTgtMsgQ->pQData )
661 goto CLEANUP;
663 if (fAttach) /* Attach threads */
665 /* Only attach if currently detached */
666 if ( pTgtMsgQ->pQData != pSrcMsgQ->pQData )
668 /* First release the target threads perQData */
669 PERQDATA_Release( pTgtMsgQ->pQData );
671 /* Share a reference to the source threads perQDATA */
672 PERQDATA_Addref( pSrcMsgQ->pQData );
673 pTgtMsgQ->pQData = pSrcMsgQ->pQData;
676 else /* Detach threads */
678 /* Only detach if currently attached */
679 if ( pTgtMsgQ->pQData == pSrcMsgQ->pQData )
681 /* First release the target threads perQData */
682 PERQDATA_Release( pTgtMsgQ->pQData );
684 /* Give the target thread its own private perQDATA once more */
685 pTgtMsgQ->pQData = PERQDATA_CreateInstance();
689 /* TODO: Reset the Key State */
691 bRet = 1; /* Success */
693 CLEANUP:
695 /* Unlock the queues before returning */
696 if ( pSrcMsgQ )
697 QUEUE_Unlock( pSrcMsgQ );
698 if ( pTgtMsgQ )
699 QUEUE_Unlock( pTgtMsgQ );
701 return bRet;