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 DECLARE_DEBUG_CHANNEL(sendmsg
);
25 DEFAULT_DEBUG_CHANNEL(msg
);
27 #define MAX_QUEUE_SIZE 120 /* Max. size of a message queue */
29 static HQUEUE16 hExitingQueue
= 0;
30 static HQUEUE16 hmemSysMsgQueue
= 0;
31 static MESSAGEQUEUE
*sysMsgQueue
= NULL
;
32 static PERQUEUEDATA
*pQDataWin16
= NULL
; /* Global perQData for Win16 tasks */
34 static MESSAGEQUEUE
*pMouseQueue
= NULL
; /* Queue for last mouse message */
35 static MESSAGEQUEUE
*pKbdQueue
= NULL
; /* Queue for last kbd message */
37 HQUEUE16 hCursorQueue
= 0;
38 HQUEUE16 hActiveQueue
= 0;
41 /***********************************************************************
42 * PERQDATA_CreateInstance
44 * Creates an instance of a reference counted PERQUEUEDATA element
45 * for the message queue. perQData is stored globally for 16 bit tasks.
47 * Note: We don't implement perQdata exactly the same way Windows does.
48 * Each perQData element is reference counted since it may be potentially
49 * shared by multiple message Queues (via AttachThreadInput).
50 * We only store the current values for Active, Capture and focus windows
53 PERQUEUEDATA
* PERQDATA_CreateInstance( )
61 /* Share a single instance of perQData for all 16 bit tasks */
62 if ( ( bIsWin16
= !(NtCurrentTeb()->tibflags
& TEBF_WIN32
) ) )
64 /* If previously allocated, just bump up ref count */
67 PERQDATA_Addref( pQDataWin16
);
72 /* Allocate PERQUEUEDATA from the system heap */
73 if (!( pQData
= (PERQUEUEDATA
*) HeapAlloc( GetProcessHeap(), 0,
74 sizeof(PERQUEUEDATA
) ) ))
78 pQData
->hWndCapture
= pQData
->hWndFocus
= pQData
->hWndActive
= 0;
79 pQData
->ulRefCount
= 1;
80 pQData
->nCaptureHT
= HTCLIENT
;
82 /* Note: We have an independent critical section for the per queue data
83 * since this may be shared by different threads. see AttachThreadInput()
85 InitializeCriticalSection( &pQData
->cSection
);
86 /* FIXME: not all per queue data critical sections should be global */
87 MakeCriticalSectionGlobal( &pQData
->cSection
);
89 /* Save perQData globally for 16 bit tasks */
97 /***********************************************************************
100 * Increment reference count for the PERQUEUEDATA instance
101 * Returns reference count for debugging purposes
103 ULONG
PERQDATA_Addref( PERQUEUEDATA
*pQData
)
105 assert(pQData
!= 0 );
106 TRACE_(msg
)("(): current refcount %lu ...\n", pQData
->ulRefCount
);
108 EnterCriticalSection( &pQData
->cSection
);
109 ++pQData
->ulRefCount
;
110 LeaveCriticalSection( &pQData
->cSection
);
112 return pQData
->ulRefCount
;
116 /***********************************************************************
119 * Release a reference to a PERQUEUEDATA instance.
120 * Destroy the instance if no more references exist
121 * Returns reference count for debugging purposes
123 ULONG
PERQDATA_Release( PERQUEUEDATA
*pQData
)
125 assert(pQData
!= 0 );
126 TRACE_(msg
)("(): current refcount %lu ...\n",
127 (LONG
)pQData
->ulRefCount
);
129 EnterCriticalSection( &pQData
->cSection
);
130 if ( --pQData
->ulRefCount
== 0 )
132 LeaveCriticalSection( &pQData
->cSection
);
133 DeleteCriticalSection( &pQData
->cSection
);
135 TRACE_(msg
)("(): deleting PERQUEUEDATA instance ...\n" );
137 /* Deleting our global 16 bit perQData? */
138 if ( pQData
== pQDataWin16
)
141 /* Free the PERQUEUEDATA instance */
142 HeapFree( GetProcessHeap(), 0, pQData
);
146 LeaveCriticalSection( &pQData
->cSection
);
148 return pQData
->ulRefCount
;
152 /***********************************************************************
153 * PERQDATA_GetFocusWnd
155 * Get the focus hwnd member in a threadsafe manner
157 HWND
PERQDATA_GetFocusWnd( PERQUEUEDATA
*pQData
)
160 assert(pQData
!= 0 );
162 EnterCriticalSection( &pQData
->cSection
);
163 hWndFocus
= pQData
->hWndFocus
;
164 LeaveCriticalSection( &pQData
->cSection
);
170 /***********************************************************************
171 * PERQDATA_SetFocusWnd
173 * Set the focus hwnd member in a threadsafe manner
175 HWND
PERQDATA_SetFocusWnd( PERQUEUEDATA
*pQData
, HWND hWndFocus
)
178 assert(pQData
!= 0 );
180 EnterCriticalSection( &pQData
->cSection
);
181 hWndFocusPrv
= pQData
->hWndFocus
;
182 pQData
->hWndFocus
= hWndFocus
;
183 LeaveCriticalSection( &pQData
->cSection
);
189 /***********************************************************************
190 * PERQDATA_GetActiveWnd
192 * Get the active hwnd member in a threadsafe manner
194 HWND
PERQDATA_GetActiveWnd( PERQUEUEDATA
*pQData
)
197 assert(pQData
!= 0 );
199 EnterCriticalSection( &pQData
->cSection
);
200 hWndActive
= pQData
->hWndActive
;
201 LeaveCriticalSection( &pQData
->cSection
);
207 /***********************************************************************
208 * PERQDATA_SetActiveWnd
210 * Set the active focus hwnd member in a threadsafe manner
212 HWND
PERQDATA_SetActiveWnd( PERQUEUEDATA
*pQData
, HWND hWndActive
)
215 assert(pQData
!= 0 );
217 EnterCriticalSection( &pQData
->cSection
);
218 hWndActivePrv
= pQData
->hWndActive
;
219 pQData
->hWndActive
= hWndActive
;
220 LeaveCriticalSection( &pQData
->cSection
);
222 return hWndActivePrv
;
226 /***********************************************************************
227 * PERQDATA_GetCaptureWnd
229 * Get the capture hwnd member in a threadsafe manner
231 HWND
PERQDATA_GetCaptureWnd( PERQUEUEDATA
*pQData
)
234 assert(pQData
!= 0 );
236 EnterCriticalSection( &pQData
->cSection
);
237 hWndCapture
= pQData
->hWndCapture
;
238 LeaveCriticalSection( &pQData
->cSection
);
244 /***********************************************************************
245 * PERQDATA_SetCaptureWnd
247 * Set the capture hwnd member in a threadsafe manner
249 HWND
PERQDATA_SetCaptureWnd( PERQUEUEDATA
*pQData
, HWND hWndCapture
)
252 assert(pQData
!= 0 );
254 EnterCriticalSection( &pQData
->cSection
);
255 hWndCapturePrv
= pQData
->hWndCapture
;
256 pQData
->hWndCapture
= hWndCapture
;
257 LeaveCriticalSection( &pQData
->cSection
);
259 return hWndCapturePrv
;
263 /***********************************************************************
264 * PERQDATA_GetCaptureInfo
266 * Get the capture info member in a threadsafe manner
268 INT16
PERQDATA_GetCaptureInfo( PERQUEUEDATA
*pQData
)
271 assert(pQData
!= 0 );
273 EnterCriticalSection( &pQData
->cSection
);
274 nCaptureHT
= pQData
->nCaptureHT
;
275 LeaveCriticalSection( &pQData
->cSection
);
281 /***********************************************************************
282 * PERQDATA_SetCaptureInfo
284 * Set the capture info member in a threadsafe manner
286 INT16
PERQDATA_SetCaptureInfo( PERQUEUEDATA
*pQData
, INT16 nCaptureHT
)
289 assert(pQData
!= 0 );
291 EnterCriticalSection( &pQData
->cSection
);
292 nCaptureHTPrv
= pQData
->nCaptureHT
;
293 pQData
->nCaptureHT
= nCaptureHT
;
294 LeaveCriticalSection( &pQData
->cSection
);
296 return nCaptureHTPrv
;
300 /***********************************************************************
303 * Function for getting a 32 bit pointer on queue structure. For thread
304 * safeness programmers should use this function instead of GlobalLock to
305 * retrieve a pointer on the structure. QUEUE_Unlock should also be called
306 * when access to the queue structure is not required anymore.
308 MESSAGEQUEUE
*QUEUE_Lock( HQUEUE16 hQueue
)
312 HeapLock( GetProcessHeap() ); /* FIXME: a bit overkill */
313 queue
= GlobalLock16( hQueue
);
314 if ( !queue
|| (queue
->magic
!= QUEUE_MAGIC
) )
316 HeapUnlock( GetProcessHeap() );
321 HeapUnlock( GetProcessHeap() );
326 /***********************************************************************
329 * Use with QUEUE_Lock to get a thread safe access to message queue
332 void QUEUE_Unlock( MESSAGEQUEUE
*queue
)
336 HeapLock( GetProcessHeap() ); /* FIXME: a bit overkill */
338 if ( --queue
->lockCount
== 0 )
340 DeleteCriticalSection ( &queue
->cSection
);
341 if (queue
->server_queue
)
342 CloseHandle( queue
->server_queue
);
343 GlobalFree16( queue
->self
);
346 HeapUnlock( GetProcessHeap() );
351 /***********************************************************************
354 void QUEUE_DumpQueue( HQUEUE16 hQueue
)
358 if (!(pq
= QUEUE_Lock( hQueue
)) )
360 WARN_(msg
)("%04x is not a queue handle\n", hQueue
);
364 EnterCriticalSection( &pq
->cSection
);
366 DPRINTF( "thread: %10p Intertask SendMessage:\n"
367 "firstMsg: %8p lastMsg: %8p\n"
371 pq
->teb
, pq
->firstMsg
, pq
->lastMsg
,
372 (unsigned)pq
->lockCount
, pq
->wPaintCount
,
375 LeaveCriticalSection( &pq
->cSection
);
381 /***********************************************************************
382 * QUEUE_IsExitingQueue
384 BOOL
QUEUE_IsExitingQueue( HQUEUE16 hQueue
)
386 return (hExitingQueue
&& (hQueue
== hExitingQueue
));
390 /***********************************************************************
391 * QUEUE_SetExitingQueue
393 void QUEUE_SetExitingQueue( HQUEUE16 hQueue
)
395 hExitingQueue
= hQueue
;
399 /***********************************************************************
400 * QUEUE_CreateMsgQueue
402 * Creates a message queue. Doesn't link it into queue list!
404 static HQUEUE16
QUEUE_CreateMsgQueue( BOOL16 bCreatePerQData
)
408 MESSAGEQUEUE
* msgQueue
;
410 TRACE_(msg
)("(): Creating message queue...\n");
412 if (!(hQueue
= GlobalAlloc16( GMEM_FIXED
| GMEM_ZEROINIT
,
413 sizeof(MESSAGEQUEUE
) )))
416 msgQueue
= (MESSAGEQUEUE
*) GlobalLock16( hQueue
);
422 SERVER_START_REQ( get_msg_queue
)
425 handle
= req
->handle
;
430 ERR_(msg
)("Cannot get thread queue");
431 GlobalFree16( hQueue
);
434 msgQueue
->server_queue
= handle
;
437 msgQueue
->self
= hQueue
;
439 InitializeCriticalSection( &msgQueue
->cSection
);
440 MakeCriticalSectionGlobal( &msgQueue
->cSection
);
442 msgQueue
->lockCount
= 1;
443 msgQueue
->magic
= QUEUE_MAGIC
;
445 /* Create and initialize our per queue data */
446 msgQueue
->pQData
= bCreatePerQData
? PERQDATA_CreateInstance() : NULL
;
452 /***********************************************************************
453 * QUEUE_DeleteMsgQueue
455 * Unlinks and deletes a message queue.
457 * Note: We need to mask asynchronous events to make sure PostMessage works
458 * even in the signal handler.
460 BOOL
QUEUE_DeleteMsgQueue( HQUEUE16 hQueue
)
462 MESSAGEQUEUE
* msgQueue
= QUEUE_Lock(hQueue
);
464 TRACE_(msg
)("(): Deleting message queue %04x\n", hQueue
);
466 if (!hQueue
|| !msgQueue
)
468 ERR_(msg
)("invalid argument.\n");
474 if( hCursorQueue
== hQueue
) hCursorQueue
= 0;
475 if( hActiveQueue
== hQueue
) hActiveQueue
= 0;
477 HeapLock( GetProcessHeap() ); /* FIXME: a bit overkill */
479 /* Release per queue data if present */
480 if ( msgQueue
->pQData
)
482 PERQDATA_Release( msgQueue
->pQData
);
483 msgQueue
->pQData
= 0;
488 HeapUnlock( GetProcessHeap() );
490 /* free up resource used by MESSAGEQUEUE structure */
491 msgQueue
->lockCount
--;
492 QUEUE_Unlock( msgQueue
);
498 /***********************************************************************
499 * QUEUE_CreateSysMsgQueue
501 * Create the system message queue, and set the double-click speed.
502 * Must be called only once.
504 BOOL
QUEUE_CreateSysMsgQueue( int size
)
506 /* Note: We dont need perQ data for the system message queue */
507 if (!(hmemSysMsgQueue
= QUEUE_CreateMsgQueue( FALSE
)))
509 FarSetOwner16( hmemSysMsgQueue
, 0 );
510 sysMsgQueue
= (MESSAGEQUEUE
*) GlobalLock16( hmemSysMsgQueue
);
515 /***********************************************************************
518 MESSAGEQUEUE
*QUEUE_GetSysQueue(void)
524 /***********************************************************************
527 * See "Windows Internals", p.449
529 static BOOL
QUEUE_TrySetWakeBit( MESSAGEQUEUE
*queue
, WORD set
, WORD clear
, BOOL always
)
533 TRACE_(msg
)("queue = %04x, set = %04x, clear = %04x, always = %d\n",
534 queue
->self
, set
, clear
, always
);
535 if (!queue
->server_queue
) return FALSE
;
537 SERVER_START_REQ( set_queue_bits
)
539 req
->handle
= queue
->server_queue
;
542 req
->mask_cond
= always
? 0 : set
;
543 if (!SERVER_CALL()) wake
= (req
->changed_mask
& set
) != 0;
549 if (set
& QS_MOUSE
) pMouseQueue
= queue
;
550 if (set
& QS_KEY
) pKbdQueue
= queue
;
554 void QUEUE_SetWakeBit( MESSAGEQUEUE
*queue
, WORD set
, WORD clear
)
556 QUEUE_TrySetWakeBit( queue
, set
, clear
, TRUE
);
560 /***********************************************************************
563 void QUEUE_ClearWakeBit( MESSAGEQUEUE
*queue
, WORD bit
)
565 QUEUE_SetWakeBit( queue
, 0, bit
);
568 /***********************************************************************
571 * See "Windows Internals", p.447
574 * 0 if exit with timeout
577 int QUEUE_WaitBits( WORD bits
, DWORD timeout
)
582 TRACE_(msg
)("q %04x waiting for %04x\n", GetFastQueue16(), bits
);
584 hQueue
= GetFastQueue16();
585 if (!(queue
= QUEUE_Lock( hQueue
))) return 0;
589 unsigned int wake_bits
= 0, changed_bits
= 0;
592 SERVER_START_REQ( set_queue_mask
)
594 req
->wake_mask
= QS_SENDMESSAGE
;
595 req
->changed_mask
= bits
| QS_SENDMESSAGE
;
599 wake_bits
= req
->wake_bits
;
600 changed_bits
= req
->changed_bits
;
605 if (changed_bits
& bits
)
607 /* One of the bits is set; we can return */
608 QUEUE_Unlock( queue
);
611 if (wake_bits
& QS_SENDMESSAGE
)
613 /* Process the sent message immediately */
615 QUEUE_FindMsg( 0, 0, 0, TRUE
, TRUE
, &msg
);
616 continue; /* nested sm crux */
619 TRACE_(msg
)("(%04x) mask=%08x, bits=%08x, changed=%08x, waiting\n",
620 queue
->self
, bits
, wake_bits
, changed_bits
);
622 ReleaseThunkLock( &dwlc
);
623 if (dwlc
) TRACE_(msg
)("had win16 lock\n");
625 if (USER_Driver
.pMsgWaitForMultipleObjectsEx
)
626 USER_Driver
.pMsgWaitForMultipleObjectsEx( 1, &queue
->server_queue
, timeout
, 0, 0 );
628 WaitForSingleObject( queue
->server_queue
, timeout
);
629 if (dwlc
) RestoreThunkLock( dwlc
);
634 /* handle the reception of a sent message by calling the corresponding window proc */
635 static void handle_sent_message( QMSG
*msg
)
638 MESSAGEQUEUE
*queue
= QUEUE_Lock( GetFastQueue16() );
639 DWORD extraInfo
= queue
->GetMessageExtraInfoVal
; /* save ExtraInfo */
640 WND
*wndPtr
= WIN_FindWndPtr( msg
->msg
.hwnd
);
642 TRACE( "got hwnd %x msg %x (%s) wp %x lp %lx\n",
643 msg
->msg
.hwnd
, msg
->msg
.message
, SPY_GetMsgName(msg
->msg
.message
),
644 msg
->msg
.wParam
, msg
->msg
.lParam
);
646 queue
->GetMessageExtraInfoVal
= msg
->extraInfo
;
648 /* call the right version of CallWindowProcXX */
652 result
= CallWindowProc16( (WNDPROC16
)wndPtr
->winproc
,
653 (HWND16
) msg
->msg
.hwnd
,
654 (UINT16
) msg
->msg
.message
,
655 LOWORD(msg
->msg
.wParam
),
659 result
= CallWindowProcA( wndPtr
->winproc
, msg
->msg
.hwnd
, msg
->msg
.message
,
660 msg
->msg
.wParam
, msg
->msg
.lParam
);
663 result
= CallWindowProcW( wndPtr
->winproc
, msg
->msg
.hwnd
, msg
->msg
.message
,
664 msg
->msg
.wParam
, msg
->msg
.lParam
);
668 queue
->GetMessageExtraInfoVal
= extraInfo
; /* Restore extra info */
669 WIN_ReleaseWndPtr(wndPtr
);
670 QUEUE_Unlock( queue
);
672 SERVER_START_REQ( reply_message
)
674 req
->result
= result
;
682 /***********************************************************************
685 * Find a message matching the given parameters. Return -1 if none available.
687 BOOL
QUEUE_FindMsg( HWND hwnd
, UINT first
, UINT last
, BOOL remove
, BOOL sent_only
, QMSG
*msg
)
689 BOOL ret
= FALSE
, sent
= FALSE
;
691 if (!first
&& !last
) last
= ~0;
695 SERVER_START_REQ( get_message
)
697 req
->remove
= remove
;
698 req
->posted
= !sent_only
;
700 req
->get_first
= first
;
701 req
->get_last
= last
;
702 if ((ret
= !SERVER_CALL()))
705 msg
->type
= req
->type
;
706 msg
->msg
.hwnd
= req
->win
;
707 msg
->msg
.message
= req
->msg
;
708 msg
->msg
.wParam
= req
->wparam
;
709 msg
->msg
.lParam
= req
->lparam
;
710 msg
->msg
.time
= 0; /* FIXME */
711 msg
->msg
.pt
.x
= 0; /* FIXME */
712 msg
->msg
.pt
.y
= 0; /* FIXME */
713 msg
->extraInfo
= req
->info
;
718 if (!ret
|| !sent
) break;
719 handle_sent_message( msg
);
722 if (ret
) TRACE( "got hwnd %x msg %x (%s) wp %x lp %lx\n",
723 msg
->msg
.hwnd
, msg
->msg
.message
, SPY_GetMsgName(msg
->msg
.message
),
724 msg
->msg
.wParam
, msg
->msg
.lParam
);
730 /***********************************************************************
733 * Remove a message from the queue (pos must be a valid position).
735 void QUEUE_RemoveMsg( MESSAGEQUEUE
* msgQueue
, QMSG
*qmsg
)
737 EnterCriticalSection( &msgQueue
->cSection
);
739 /* set the linked list */
741 qmsg
->prevMsg
->nextMsg
= qmsg
->nextMsg
;
744 qmsg
->nextMsg
->prevMsg
= qmsg
->prevMsg
;
746 if (msgQueue
->firstMsg
== qmsg
)
747 msgQueue
->firstMsg
= qmsg
->nextMsg
;
749 if (msgQueue
->lastMsg
== qmsg
)
750 msgQueue
->lastMsg
= qmsg
->prevMsg
;
752 /* deallocate the memory for the message */
753 HeapFree( GetProcessHeap(), 0, qmsg
);
755 LeaveCriticalSection( &msgQueue
->cSection
);
759 /***********************************************************************
760 * QUEUE_CleanupWindow
762 * Cleanup the queue to account for a window being deleted.
764 void QUEUE_CleanupWindow( HWND hwnd
)
766 SERVER_START_REQ( cleanup_window_queue
)
775 /***********************************************************************
778 * Add an event to the system message queue.
779 * Note: the position is relative to the desktop window.
781 void hardware_event( UINT message
, WPARAM wParam
, LPARAM lParam
,
782 int xPos
, int yPos
, DWORD time
, DWORD extraInfo
)
789 if (!sysMsgQueue
) return;
791 EnterCriticalSection( &sysMsgQueue
->cSection
);
793 /* Merge with previous event if possible */
794 qmsg
= sysMsgQueue
->lastMsg
;
796 if ((message
== WM_MOUSEMOVE
) && sysMsgQueue
->lastMsg
)
798 msg
= &(sysMsgQueue
->lastMsg
->msg
);
800 if ((msg
->message
== message
) && (msg
->wParam
== wParam
))
803 qmsg
= sysMsgQueue
->lastMsg
;
810 /* Should I limit the number of messages in
811 the system message queue??? */
813 /* Don't merge allocate a new msg in the global heap */
815 if (!(qmsg
= (QMSG
*) HeapAlloc( GetProcessHeap(), 0, sizeof(QMSG
) ) ))
817 LeaveCriticalSection( &sysMsgQueue
->cSection
);
821 /* put message at the end of the linked list */
823 qmsg
->prevMsg
= sysMsgQueue
->lastMsg
;
825 if (sysMsgQueue
->lastMsg
)
826 sysMsgQueue
->lastMsg
->nextMsg
= qmsg
;
828 /* set last and first anchor index in system message queue */
829 sysMsgQueue
->lastMsg
= qmsg
;
830 if (!sysMsgQueue
->firstMsg
)
831 sysMsgQueue
->firstMsg
= qmsg
;
837 msg
->message
= message
;
838 msg
->wParam
= wParam
;
839 msg
->lParam
= lParam
;
843 qmsg
->extraInfo
= extraInfo
;
844 qmsg
->type
= QMSG_HARDWARE
;
846 LeaveCriticalSection( &sysMsgQueue
->cSection
);
848 if ((queue
= QUEUE_Lock( GetFastQueue16() )))
852 if ((message
>= WM_KEYFIRST
) && (message
<= WM_KEYLAST
)) wakeBit
= QS_KEY
;
853 else wakeBit
= (message
== WM_MOUSEMOVE
) ? QS_MOUSEMOVE
: QS_MOUSEBUTTON
;
855 QUEUE_SetWakeBit( queue
, wakeBit
, 0 );
856 QUEUE_Unlock( queue
);
861 /***********************************************************************
864 HTASK16
QUEUE_GetQueueTask( HQUEUE16 hQueue
)
868 MESSAGEQUEUE
*queue
= QUEUE_Lock( hQueue
);
872 hTask
= queue
->teb
->htask16
;
873 QUEUE_Unlock( queue
);
881 /***********************************************************************
882 * QUEUE_IncPaintCount
884 void QUEUE_IncPaintCount( HQUEUE16 hQueue
)
888 if (!(queue
= QUEUE_Lock( hQueue
))) return;
889 EnterCriticalSection( &queue
->cSection
);
890 queue
->wPaintCount
++;
891 LeaveCriticalSection( &queue
->cSection
);
892 QUEUE_SetWakeBit( queue
, QS_PAINT
, 0 );
893 QUEUE_Unlock( queue
);
897 /***********************************************************************
898 * QUEUE_DecPaintCount
900 void QUEUE_DecPaintCount( HQUEUE16 hQueue
)
904 if (!(queue
= QUEUE_Lock( hQueue
))) return;
905 EnterCriticalSection( &queue
->cSection
);
906 queue
->wPaintCount
--;
907 if (!queue
->wPaintCount
) QUEUE_ClearWakeBit( queue
, QS_PAINT
);
908 LeaveCriticalSection( &queue
->cSection
);
909 QUEUE_Unlock( queue
);
913 /***********************************************************************
914 * PostQuitMessage (USER.6)
916 void WINAPI
PostQuitMessage16( INT16 exitCode
)
918 PostQuitMessage( exitCode
);
922 /***********************************************************************
923 * PostQuitMessage (USER32.@)
925 * PostQuitMessage() posts a message to the system requesting an
926 * application to terminate execution. As a result of this function,
927 * the WM_QUIT message is posted to the application, and
928 * PostQuitMessage() returns immediately. The exitCode parameter
929 * specifies an application-defined exit code, which appears in the
930 * _wParam_ parameter of the WM_QUIT message posted to the application.
936 void WINAPI
PostQuitMessage( INT exitCode
)
938 PostThreadMessageW( GetCurrentThreadId(), WM_QUIT
, exitCode
, 0 );
942 /***********************************************************************
943 * GetWindowTask (USER.224)
945 HTASK16 WINAPI
GetWindowTask16( HWND16 hwnd
)
948 WND
*wndPtr
= WIN_FindWndPtr( hwnd
);
950 if (!wndPtr
) return 0;
951 retvalue
= QUEUE_GetQueueTask( wndPtr
->hmemTaskQ
);
952 WIN_ReleaseWndPtr(wndPtr
);
956 /***********************************************************************
957 * GetWindowThreadProcessId (USER32.@)
959 DWORD WINAPI
GetWindowThreadProcessId( HWND hwnd
, LPDWORD process
)
964 WND
*wndPtr
= WIN_FindWndPtr( hwnd
);
965 if (!wndPtr
) return 0;
967 queue
= QUEUE_Lock( wndPtr
->hmemTaskQ
);
968 WIN_ReleaseWndPtr(wndPtr
);
970 if (!queue
) return 0;
972 if ( process
) *process
= (DWORD
)queue
->teb
->pid
;
973 retvalue
= (DWORD
)queue
->teb
->tid
;
975 QUEUE_Unlock( queue
);
980 /***********************************************************************
981 * SetMessageQueue (USER.266)
983 BOOL16 WINAPI
SetMessageQueue16( INT16 size
)
985 return SetMessageQueue( size
);
989 /***********************************************************************
990 * SetMessageQueue (USER32.@)
992 BOOL WINAPI
SetMessageQueue( INT size
)
994 /* now obsolete the message queue will be expanded dynamically
997 /* access the queue to create it if it's not existing */
1003 /***********************************************************************
1004 * InitThreadInput (USER.409)
1006 HQUEUE16 WINAPI
InitThreadInput16( WORD unknown
, WORD flags
)
1009 MESSAGEQUEUE
*queuePtr
;
1011 TEB
*teb
= NtCurrentTeb();
1016 hQueue
= teb
->queue
;
1020 /* Create thread message queue */
1021 if( !(hQueue
= QUEUE_CreateMsgQueue( TRUE
)))
1023 ERR_(msg
)("failed!\n");
1027 /* Link new queue into list */
1028 queuePtr
= QUEUE_Lock( hQueue
);
1029 queuePtr
->teb
= NtCurrentTeb();
1031 HeapLock( GetProcessHeap() ); /* FIXME: a bit overkill */
1032 SetThreadQueue16( 0, hQueue
);
1033 teb
->queue
= hQueue
;
1034 HeapUnlock( GetProcessHeap() );
1036 QUEUE_Unlock( queuePtr
);
1042 /***********************************************************************
1043 * GetQueueStatus (USER.334)
1045 DWORD WINAPI
GetQueueStatus16( UINT16 flags
)
1047 return GetQueueStatus( flags
);
1050 /***********************************************************************
1051 * GetQueueStatus (USER32.@)
1053 DWORD WINAPI
GetQueueStatus( UINT flags
)
1057 SERVER_START_REQ( get_queue_status
)
1061 ret
= MAKELONG( req
->changed_bits
& flags
, req
->wake_bits
& flags
);
1068 /***********************************************************************
1069 * GetInputState (USER.335)
1071 BOOL16 WINAPI
GetInputState16(void)
1073 return GetInputState();
1076 /***********************************************************************
1077 * GetInputState (USER32.@)
1079 BOOL WINAPI
GetInputState(void)
1083 SERVER_START_REQ( get_queue_status
)
1087 ret
= req
->wake_bits
& (QS_KEY
| QS_MOUSEBUTTON
);
1093 /***********************************************************************
1094 * WaitForInputIdle (USER32.@)
1096 DWORD WINAPI
WaitForInputIdle (HANDLE hProcess
, DWORD dwTimeOut
)
1098 DWORD cur_time
, ret
;
1099 HANDLE idle_event
= -1;
1101 SERVER_START_REQ( wait_input_idle
)
1103 req
->handle
= hProcess
;
1104 req
->timeout
= dwTimeOut
;
1105 if (!(ret
= SERVER_CALL_ERR())) idle_event
= req
->event
;
1108 if (ret
) return 0xffffffff; /* error */
1109 if (!idle_event
) return 0; /* no event to wait on */
1111 cur_time
= GetTickCount();
1113 TRACE_(msg
)("waiting for %x\n", idle_event
);
1114 while ( dwTimeOut
> GetTickCount() - cur_time
|| dwTimeOut
== INFINITE
)
1116 ret
= MsgWaitForMultipleObjects ( 1, &idle_event
, FALSE
, dwTimeOut
, QS_SENDMESSAGE
);
1117 if ( ret
== ( WAIT_OBJECT_0
+ 1 ))
1120 QUEUE_FindMsg( 0, 0, 0, TRUE
, TRUE
, &msg
);
1123 if ( ret
== WAIT_TIMEOUT
|| ret
== 0xFFFFFFFF )
1125 TRACE_(msg
)("timeout or error\n");
1130 TRACE_(msg
)("finished\n");
1135 return WAIT_TIMEOUT
;
1138 /***********************************************************************
1139 * UserYield (USER.332)
1140 * UserYield16 (USER32.@)
1142 void WINAPI
UserYield16(void)
1146 /* Handle sent messages */
1147 while (QUEUE_FindMsg( 0, 0, 0, TRUE
, TRUE
, &msg
))
1153 /* Handle sent messages again */
1154 while (QUEUE_FindMsg( 0, 0, 0, TRUE
, TRUE
, &msg
))
1158 /***********************************************************************
1159 * GetMessagePos (USER.119) (USER32.@)
1161 * The GetMessagePos() function returns a long value representing a
1162 * cursor position, in screen coordinates, when the last message
1163 * retrieved by the GetMessage() function occurs. The x-coordinate is
1164 * in the low-order word of the return value, the y-coordinate is in
1165 * the high-order word. The application can use the MAKEPOINT()
1166 * macro to obtain a POINT structure from the return value.
1168 * For the current cursor position, use GetCursorPos().
1172 * Cursor position of last message on success, zero on failure.
1179 DWORD WINAPI
GetMessagePos(void)
1181 MESSAGEQUEUE
*queue
;
1184 if (!(queue
= QUEUE_Lock( GetFastQueue16() ))) return 0;
1185 ret
= queue
->GetMessagePosVal
;
1186 QUEUE_Unlock( queue
);
1192 /***********************************************************************
1193 * GetMessageTime (USER.120) (USER32.@)
1195 * GetMessageTime() returns the message time for the last message
1196 * retrieved by the function. The time is measured in milliseconds with
1197 * the same offset as GetTickCount().
1199 * Since the tick count wraps, this is only useful for moderately short
1200 * relative time comparisons.
1204 * Time of last message on success, zero on failure.
1211 LONG WINAPI
GetMessageTime(void)
1213 MESSAGEQUEUE
*queue
;
1216 if (!(queue
= QUEUE_Lock( GetFastQueue16() ))) return 0;
1217 ret
= queue
->GetMessageTimeVal
;
1218 QUEUE_Unlock( queue
);
1224 /***********************************************************************
1225 * GetMessageExtraInfo (USER.288) (USER32.@)
1227 LONG WINAPI
GetMessageExtraInfo(void)
1229 MESSAGEQUEUE
*queue
;
1232 if (!(queue
= QUEUE_Lock( GetFastQueue16() ))) return 0;
1233 ret
= queue
->GetMessageExtraInfoVal
;
1234 QUEUE_Unlock( queue
);
1240 /**********************************************************************
1241 * AttachThreadInput (USER32.@) Attaches input of 1 thread to other
1243 * Attaches the input processing mechanism of one thread to that of
1251 * 1. Reset the Key State (currenly per thread key state is not maintained)
1253 BOOL WINAPI
AttachThreadInput(
1254 DWORD idAttach
, /* [in] Thread to attach */
1255 DWORD idAttachTo
, /* [in] Thread to attach to */
1256 BOOL fAttach
) /* [in] Attach or detach */
1258 MESSAGEQUEUE
*pSrcMsgQ
= 0, *pTgtMsgQ
= 0;
1261 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1263 /* A thread cannot attach to itself */
1264 if ( idAttach
== idAttachTo
)
1267 /* According to the docs this method should fail if a
1268 * "Journal record" hook is installed. (attaches all input queues together)
1270 if ( HOOK_IsHooked( WH_JOURNALRECORD
) )
1273 /* Retrieve message queues corresponding to the thread id's */
1274 pTgtMsgQ
= QUEUE_Lock( GetThreadQueue16( idAttach
) );
1275 pSrcMsgQ
= QUEUE_Lock( GetThreadQueue16( idAttachTo
) );
1277 /* Ensure we have message queues and that Src and Tgt threads
1278 * are not system threads.
1280 if ( !pSrcMsgQ
|| !pTgtMsgQ
|| !pSrcMsgQ
->pQData
|| !pTgtMsgQ
->pQData
)
1283 if (fAttach
) /* Attach threads */
1285 /* Only attach if currently detached */
1286 if ( pTgtMsgQ
->pQData
!= pSrcMsgQ
->pQData
)
1288 /* First release the target threads perQData */
1289 PERQDATA_Release( pTgtMsgQ
->pQData
);
1291 /* Share a reference to the source threads perQDATA */
1292 PERQDATA_Addref( pSrcMsgQ
->pQData
);
1293 pTgtMsgQ
->pQData
= pSrcMsgQ
->pQData
;
1296 else /* Detach threads */
1298 /* Only detach if currently attached */
1299 if ( pTgtMsgQ
->pQData
== pSrcMsgQ
->pQData
)
1301 /* First release the target threads perQData */
1302 PERQDATA_Release( pTgtMsgQ
->pQData
);
1304 /* Give the target thread its own private perQDATA once more */
1305 pTgtMsgQ
->pQData
= PERQDATA_CreateInstance();
1309 /* TODO: Reset the Key State */
1311 bRet
= 1; /* Success */
1315 /* Unlock the queues before returning */
1317 QUEUE_Unlock( pSrcMsgQ
);
1319 QUEUE_Unlock( pTgtMsgQ
);