Added version info to 16-bit shell.dll.
[wine/multimedia.git] / windows / queue.c
blob88d2f745544a665948c2a123497049f1bb145c85
1 /*
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
21 #include <string.h>
22 #include <signal.h>
23 #include <assert.h>
24 #include "windef.h"
25 #include "wingdi.h"
26 #include "winerror.h"
27 #include "wine/winbase16.h"
28 #include "wine/winuser16.h"
29 #include "queue.h"
30 #include "win.h"
31 #include "user.h"
32 #include "hook.h"
33 #include "thread.h"
34 #include "wine/debug.h"
35 #include "wine/server.h"
36 #include "spy.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(msg);
41 static PERQUEUEDATA *pQDataWin16 = NULL; /* Global perQData for Win16 tasks */
43 HQUEUE16 hActiveQueue = 0;
46 /***********************************************************************
47 * PERQDATA_Addref
49 * Increment reference count for the PERQUEUEDATA instance
50 * Returns reference count for debugging purposes
52 static void PERQDATA_Addref( PERQUEUEDATA *pQData )
54 assert(pQData != 0 );
55 TRACE_(msg)("(): current refcount %lu ...\n", pQData->ulRefCount);
57 InterlockedIncrement( &pQData->ulRefCount );
61 /***********************************************************************
62 * PERQDATA_Release
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 )
70 assert(pQData != 0 );
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
99 * currently.
101 static PERQUEUEDATA * PERQDATA_CreateInstance(void)
103 PERQUEUEDATA *pQData;
105 BOOL16 bIsWin16 = 0;
107 TRACE_(msg)("()\n");
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 */
113 if ( pQDataWin16 )
115 PERQDATA_Addref( pQDataWin16 );
116 return pQDataWin16;
120 /* Allocate PERQUEUEDATA from the system heap */
121 if (!( pQData = (PERQUEUEDATA *) HeapAlloc( GetProcessHeap(), 0,
122 sizeof(PERQUEUEDATA) ) ))
123 return 0;
125 /* Initialize */
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 */
138 if ( bIsWin16 )
139 pQDataWin16 = pQData;
141 return pQData;
145 /***********************************************************************
146 * PERQDATA_GetFocusWnd
148 * Get the focus hwnd member in a threadsafe manner
150 HWND PERQDATA_GetFocusWnd( PERQUEUEDATA *pQData )
152 HWND hWndFocus;
153 assert(pQData != 0 );
155 EnterCriticalSection( &pQData->cSection );
156 hWndFocus = pQData->hWndFocus;
157 LeaveCriticalSection( &pQData->cSection );
159 return hWndFocus;
163 /***********************************************************************
164 * PERQDATA_SetFocusWnd
166 * Set the focus hwnd member in a threadsafe manner
168 HWND PERQDATA_SetFocusWnd( PERQUEUEDATA *pQData, HWND hWndFocus )
170 HWND hWndFocusPrv;
171 assert(pQData != 0 );
173 EnterCriticalSection( &pQData->cSection );
174 hWndFocusPrv = pQData->hWndFocus;
175 pQData->hWndFocus = hWndFocus;
176 LeaveCriticalSection( &pQData->cSection );
178 return hWndFocusPrv;
182 /***********************************************************************
183 * PERQDATA_GetActiveWnd
185 * Get the active hwnd member in a threadsafe manner
187 HWND PERQDATA_GetActiveWnd( PERQUEUEDATA *pQData )
189 HWND hWndActive;
190 assert(pQData != 0 );
192 EnterCriticalSection( &pQData->cSection );
193 hWndActive = pQData->hWndActive;
194 LeaveCriticalSection( &pQData->cSection );
196 return hWndActive;
200 /***********************************************************************
201 * PERQDATA_SetActiveWnd
203 * Set the active focus hwnd member in a threadsafe manner
205 HWND PERQDATA_SetActiveWnd( PERQUEUEDATA *pQData, HWND hWndActive )
207 HWND hWndActivePrv;
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 )
226 MESSAGEQUEUE *queue;
227 PERQUEUEDATA *pQData;
228 HWND hWndCapture;
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 );
237 return hWndCapture;
241 /***********************************************************************
242 * PERQDATA_SetCaptureWnd
244 * Set the capture hwnd member in a threadsafe manner
246 HWND PERQDATA_SetCaptureWnd( HWND hWndCapture, INT hittest )
248 MESSAGEQUEUE *queue;
249 PERQUEUEDATA *pQData;
250 HWND hWndCapturePrv;
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 /***********************************************************************
266 * QUEUE_Lock
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 )
275 MESSAGEQUEUE *queue;
277 HeapLock( GetProcessHeap() ); /* FIXME: a bit overkill */
278 queue = GlobalLock16( hQueue );
279 if ( !queue || (queue->magic != QUEUE_MAGIC) )
281 HeapUnlock( GetProcessHeap() );
282 return NULL;
285 queue->lockCount++;
286 HeapUnlock( GetProcessHeap() );
287 return queue;
291 /***********************************************************************
292 * QUEUE_Current
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)
300 MESSAGEQUEUE *queue;
301 HQUEUE16 hQueue;
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;
312 return queue;
316 /***********************************************************************
317 * QUEUE_Unlock
319 * Use with QUEUE_Lock to get a thread safe access to message queue
320 * structure
322 void QUEUE_Unlock( MESSAGEQUEUE *queue )
324 if (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 )
347 HQUEUE16 hQueue;
348 HANDLE handle;
349 MESSAGEQUEUE * msgQueue;
351 TRACE_(msg)("(): Creating message queue...\n");
353 if (!(hQueue = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT,
354 sizeof(MESSAGEQUEUE) )))
355 return 0;
357 msgQueue = (MESSAGEQUEUE *) GlobalLock16( hQueue );
358 if ( !msgQueue )
359 return 0;
361 if (bCreatePerQData)
363 SERVER_START_REQ( get_msg_queue )
365 wine_server_call_err( req );
366 handle = reply->handle;
368 SERVER_END_REQ;
369 if (!handle)
371 ERR_(msg)("Cannot get thread queue");
372 GlobalFree16( hQueue );
373 return 0;
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;
385 return hQueue;
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");
409 return;
412 msgQueue->magic = 0;
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;
425 msgQueue->self = 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 )
441 HTASK16 retvalue;
442 MESSAGEQUEUE *queue;
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 );
455 return retvalue;
458 /***********************************************************************
459 * InitThreadInput (USER.409)
461 HQUEUE16 WINAPI InitThreadInput16( WORD unknown, WORD flags )
463 MESSAGEQUEUE *queuePtr;
464 HQUEUE16 hQueue = NtCurrentTeb()->queue;
466 if ( !hQueue )
468 /* Create thread message queue */
469 if( !(hQueue = QUEUE_CreateMsgQueue( TRUE )))
471 ERR_(msg)("failed!\n");
472 return FALSE;
475 /* Link new queue into list */
476 queuePtr = QUEUE_Lock( hQueue );
477 queuePtr->teb = NtCurrentTeb();
479 HeapLock( GetProcessHeap() ); /* FIXME: a bit overkill */
480 SetThreadQueue16( 0, hQueue );
481 NtCurrentTeb()->queue = hQueue;
482 HeapUnlock( GetProcessHeap() );
484 QUEUE_Unlock( queuePtr );
487 return hQueue;
490 /***********************************************************************
491 * GetQueueStatus (USER32.@)
493 DWORD WINAPI GetQueueStatus( UINT flags )
495 DWORD ret = 0;
497 SERVER_START_REQ( get_queue_status )
499 req->clear = 1;
500 wine_server_call( req );
501 ret = MAKELONG( reply->changed_bits & flags, reply->wake_bits & flags );
503 SERVER_END_REQ;
504 return ret;
508 /***********************************************************************
509 * GetInputState (USER32.@)
511 BOOL WINAPI GetInputState(void)
513 DWORD ret = 0;
515 SERVER_START_REQ( get_queue_status )
517 req->clear = 0;
518 wine_server_call( req );
519 ret = reply->wake_bits & (QS_KEY | QS_MOUSEBUTTON);
521 SERVER_END_REQ;
522 return ret;
525 /***********************************************************************
526 * GetMessagePos (USER.119)
527 * GetMessagePos (USER32.@)
529 * The GetMessagePos() function returns a long value representing a
530 * cursor position, in screen coordinates, when the last message
531 * retrieved by the GetMessage() function occurs. The x-coordinate is
532 * in the low-order word of the return value, the y-coordinate is in
533 * the high-order word. The application can use the MAKEPOINT()
534 * macro to obtain a POINT structure from the return value.
536 * For the current cursor position, use GetCursorPos().
538 * RETURNS
540 * Cursor position of last message on success, zero on failure.
542 * CONFORMANCE
544 * ECMA-234, Win32
547 DWORD WINAPI GetMessagePos(void)
549 MESSAGEQUEUE *queue;
551 if (!(queue = QUEUE_Current())) return 0;
552 return queue->GetMessagePosVal;
556 /***********************************************************************
557 * GetMessageTime (USER.120)
558 * GetMessageTime (USER32.@)
560 * GetMessageTime() returns the message time for the last message
561 * retrieved by the function. The time is measured in milliseconds with
562 * the same offset as GetTickCount().
564 * Since the tick count wraps, this is only useful for moderately short
565 * relative time comparisons.
567 * RETURNS
569 * Time of last message on success, zero on failure.
571 * CONFORMANCE
573 * ECMA-234, Win32
576 LONG WINAPI GetMessageTime(void)
578 MESSAGEQUEUE *queue;
580 if (!(queue = QUEUE_Current())) return 0;
581 return queue->GetMessageTimeVal;
585 /***********************************************************************
586 * GetMessageExtraInfo (USER.288)
587 * GetMessageExtraInfo (USER32.@)
589 LONG WINAPI GetMessageExtraInfo(void)
591 MESSAGEQUEUE *queue;
593 if (!(queue = QUEUE_Current())) return 0;
594 return queue->GetMessageExtraInfoVal;
598 /**********************************************************************
599 * AttachThreadInput (USER32.@) Attaches input of 1 thread to other
601 * Attaches the input processing mechanism of one thread to that of
602 * another thread.
604 * RETURNS
605 * Success: TRUE
606 * Failure: FALSE
608 * TODO:
609 * 1. Reset the Key State (currenly per thread key state is not maintained)
611 BOOL WINAPI AttachThreadInput(
612 DWORD idAttach, /* [in] Thread to attach */
613 DWORD idAttachTo, /* [in] Thread to attach to */
614 BOOL fAttach) /* [in] Attach or detach */
616 MESSAGEQUEUE *pSrcMsgQ = 0, *pTgtMsgQ = 0;
617 BOOL16 bRet = 0;
619 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
621 /* A thread cannot attach to itself */
622 if ( idAttach == idAttachTo )
623 goto CLEANUP;
625 /* According to the docs this method should fail if a
626 * "Journal record" hook is installed. (attaches all input queues together)
628 if ( HOOK_IsHooked( WH_JOURNALRECORD ) )
629 goto CLEANUP;
631 /* Retrieve message queues corresponding to the thread id's */
632 pTgtMsgQ = QUEUE_Lock( GetThreadQueue16( idAttach ) );
633 pSrcMsgQ = QUEUE_Lock( GetThreadQueue16( idAttachTo ) );
635 /* Ensure we have message queues and that Src and Tgt threads
636 * are not system threads.
638 if ( !pSrcMsgQ || !pTgtMsgQ || !pSrcMsgQ->pQData || !pTgtMsgQ->pQData )
639 goto CLEANUP;
641 if (fAttach) /* Attach threads */
643 /* Only attach if currently detached */
644 if ( pTgtMsgQ->pQData != pSrcMsgQ->pQData )
646 /* First release the target threads perQData */
647 PERQDATA_Release( pTgtMsgQ->pQData );
649 /* Share a reference to the source threads perQDATA */
650 PERQDATA_Addref( pSrcMsgQ->pQData );
651 pTgtMsgQ->pQData = pSrcMsgQ->pQData;
654 else /* Detach threads */
656 /* Only detach if currently attached */
657 if ( pTgtMsgQ->pQData == pSrcMsgQ->pQData )
659 /* First release the target threads perQData */
660 PERQDATA_Release( pTgtMsgQ->pQData );
662 /* Give the target thread its own private perQDATA once more */
663 pTgtMsgQ->pQData = PERQDATA_CreateInstance();
667 /* TODO: Reset the Key State */
669 bRet = 1; /* Success */
671 CLEANUP:
673 /* Unlock the queues before returning */
674 if ( pSrcMsgQ )
675 QUEUE_Unlock( pSrcMsgQ );
676 if ( pTgtMsgQ )
677 QUEUE_Unlock( pTgtMsgQ );
679 return bRet;