Removed process argument to PROCESS_CallUserSignalProc.
[wine/multimedia.git] / windows / queue.c
blobd27ec951163cb8315481e16e5a8f5e2c173830bf
1 /* * Message queues related functions
3 * Copyright 1993, 1994 Alexandre Julliard
4 */
6 #include <string.h>
7 #include <signal.h>
8 #include "wine/winbase16.h"
9 #include "wine/winuser16.h"
10 #include "miscemu.h"
11 #include "syslevel.h"
12 #include "module.h"
13 #include "queue.h"
14 #include "task.h"
15 #include "win.h"
16 #include "clipboard.h"
17 #include "hook.h"
18 #include "heap.h"
19 #include "thread.h"
20 #include "process.h"
21 #include <assert.h>
22 #include "debugtools.h"
23 #include "spy.h"
25 DECLARE_DEBUG_CHANNEL(msg)
26 DECLARE_DEBUG_CHANNEL(sendmsg)
28 #define MAX_QUEUE_SIZE 120 /* Max. size of a message queue */
30 static HQUEUE16 hFirstQueue = 0;
31 static HQUEUE16 hExitingQueue = 0;
32 static HQUEUE16 hmemSysMsgQueue = 0;
33 static MESSAGEQUEUE *sysMsgQueue = NULL;
34 static PERQUEUEDATA *pQDataWin16 = NULL; /* Global perQData for Win16 tasks */
36 static MESSAGEQUEUE *pMouseQueue = NULL; /* Queue for last mouse message */
37 static MESSAGEQUEUE *pKbdQueue = NULL; /* Queue for last kbd message */
39 HQUEUE16 hCursorQueue = 0;
40 HQUEUE16 hActiveQueue = 0;
43 /***********************************************************************
44 * PERQDATA_CreateInstance
46 * Creates an instance of a reference counted PERQUEUEDATA element
47 * for the message queue. perQData is stored globally for 16 bit tasks.
49 * Note: We don't implement perQdata exactly the same way Windows does.
50 * Each perQData element is reference counted since it may be potentially
51 * shared by multiple message Queues (via AttachThreadInput).
52 * We only store the current values for Active, Capture and focus windows
53 * currently.
55 PERQUEUEDATA * PERQDATA_CreateInstance( )
57 PERQUEUEDATA *pQData;
59 BOOL16 bIsWin16 = 0;
61 TRACE_(msg)("()\n");
63 /* Share a single instance of perQData for all 16 bit tasks */
64 if ( ( bIsWin16 = THREAD_IsWin16( THREAD_Current() ) ) )
66 /* If previously allocated, just bump up ref count */
67 if ( pQDataWin16 )
69 PERQDATA_Addref( pQDataWin16 );
70 return pQDataWin16;
74 /* Allocate PERQUEUEDATA from the system heap */
75 if (!( pQData = (PERQUEUEDATA *) HeapAlloc( SystemHeap, 0,
76 sizeof(PERQUEUEDATA) ) ))
77 return 0;
79 /* Initialize */
80 pQData->hWndCapture = pQData->hWndFocus = pQData->hWndActive = 0;
81 pQData->ulRefCount = 1;
82 pQData->nCaptureHT = HTCLIENT;
84 /* Note: We have an independent critical section for the per queue data
85 * since this may be shared by different threads. see AttachThreadInput()
87 InitializeCriticalSection( &pQData->cSection );
89 /* Save perQData globally for 16 bit tasks */
90 if ( bIsWin16 )
91 pQDataWin16 = pQData;
93 return pQData;
97 /***********************************************************************
98 * PERQDATA_Addref
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 /***********************************************************************
117 * PERQDATA_Release
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 )
139 pQDataWin16 = 0;
141 /* Free the PERQUEUEDATA instance */
142 HeapFree( SystemHeap, 0, pQData );
144 return 0;
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 )
159 HWND hWndFocus;
160 assert(pQData != 0 );
162 EnterCriticalSection( &pQData->cSection );
163 hWndFocus = pQData->hWndFocus;
164 LeaveCriticalSection( &pQData->cSection );
166 return hWndFocus;
170 /***********************************************************************
171 * PERQDATA_SetFocusWnd
173 * Set the focus hwnd member in a threadsafe manner
175 HWND PERQDATA_SetFocusWnd( PERQUEUEDATA *pQData, HWND hWndFocus )
177 HWND hWndFocusPrv;
178 assert(pQData != 0 );
180 EnterCriticalSection( &pQData->cSection );
181 hWndFocusPrv = pQData->hWndFocus;
182 pQData->hWndFocus = hWndFocus;
183 LeaveCriticalSection( &pQData->cSection );
185 return hWndFocusPrv;
189 /***********************************************************************
190 * PERQDATA_GetActiveWnd
192 * Get the active hwnd member in a threadsafe manner
194 HWND PERQDATA_GetActiveWnd( PERQUEUEDATA *pQData )
196 HWND hWndActive;
197 assert(pQData != 0 );
199 EnterCriticalSection( &pQData->cSection );
200 hWndActive = pQData->hWndActive;
201 LeaveCriticalSection( &pQData->cSection );
203 return hWndActive;
207 /***********************************************************************
208 * PERQDATA_SetActiveWnd
210 * Set the active focus hwnd member in a threadsafe manner
212 HWND PERQDATA_SetActiveWnd( PERQUEUEDATA *pQData, HWND hWndActive )
214 HWND hWndActivePrv;
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 )
233 HWND hWndCapture;
234 assert(pQData != 0 );
236 EnterCriticalSection( &pQData->cSection );
237 hWndCapture = pQData->hWndCapture;
238 LeaveCriticalSection( &pQData->cSection );
240 return hWndCapture;
244 /***********************************************************************
245 * PERQDATA_SetCaptureWnd
247 * Set the capture hwnd member in a threadsafe manner
249 HWND PERQDATA_SetCaptureWnd( PERQUEUEDATA *pQData, HWND hWndCapture )
251 HWND hWndCapturePrv;
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 )
270 INT16 nCaptureHT;
271 assert(pQData != 0 );
273 EnterCriticalSection( &pQData->cSection );
274 nCaptureHT = pQData->nCaptureHT;
275 LeaveCriticalSection( &pQData->cSection );
277 return nCaptureHT;
281 /***********************************************************************
282 * PERQDATA_SetCaptureInfo
284 * Set the capture info member in a threadsafe manner
286 INT16 PERQDATA_SetCaptureInfo( PERQUEUEDATA *pQData, INT16 nCaptureHT )
288 INT16 nCaptureHTPrv;
289 assert(pQData != 0 );
291 EnterCriticalSection( &pQData->cSection );
292 nCaptureHTPrv = pQData->nCaptureHT;
293 pQData->nCaptureHT = nCaptureHT;
294 LeaveCriticalSection( &pQData->cSection );
296 return nCaptureHTPrv;
300 /***********************************************************************
301 * QUEUE_Lock
303 * Function for getting a 32 bit pointer on queue strcture. 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 )
310 MESSAGEQUEUE *queue;
312 HeapLock( SystemHeap ); /* FIXME: a bit overkill */
313 queue = GlobalLock16( hQueue );
314 if ( !queue || (queue->magic != QUEUE_MAGIC) )
316 HeapUnlock( SystemHeap );
317 return NULL;
320 queue->lockCount++;
321 HeapUnlock( SystemHeap );
322 return queue;
326 /***********************************************************************
327 * QUEUE_Unlock
329 * Use with QUEUE_Lock to get a thread safe access to message queue
330 * structure
332 void QUEUE_Unlock( MESSAGEQUEUE *queue )
334 if (queue)
336 HeapLock( SystemHeap ); /* FIXME: a bit overkill */
338 if ( --queue->lockCount == 0 )
340 DeleteCriticalSection ( &queue->cSection );
341 if (queue->hEvent)
342 CloseHandle( queue->hEvent );
343 GlobalFree16( queue->self );
346 HeapUnlock( SystemHeap );
351 /***********************************************************************
352 * QUEUE_DumpQueue
354 void QUEUE_DumpQueue( HQUEUE16 hQueue )
356 MESSAGEQUEUE *pq;
358 if (!(pq = (MESSAGEQUEUE*) QUEUE_Lock( hQueue )) )
360 WARN_(msg)("%04x is not a queue handle\n", hQueue );
361 return;
364 DPRINTF( "next: %12.4x Intertask SendMessage:\n"
365 "thread: %10p ----------------------\n"
366 "firstMsg: %8p smWaiting: %10p\n"
367 "lastMsg: %8p smPending: %10p\n"
368 "msgCount: %8.4x smProcessing: %10p\n"
369 "lockCount: %7.4x\n"
370 "wWinVer: %9.4x\n"
371 "paints: %10.4x\n"
372 "timers: %10.4x\n"
373 "wakeBits: %8.4x\n"
374 "wakeMask: %8.4x\n"
375 "hCurHook: %8.4x\n",
376 pq->next, pq->thdb, pq->firstMsg, pq->smWaiting, pq->lastMsg,
377 pq->smPending, pq->msgCount, pq->smProcessing,
378 (unsigned)pq->lockCount, pq->wWinVersion,
379 pq->wPaintCount, pq->wTimerCount,
380 pq->wakeBits, pq->wakeMask, pq->hCurHook);
382 QUEUE_Unlock( pq );
386 /***********************************************************************
387 * QUEUE_WalkQueues
389 void QUEUE_WalkQueues(void)
391 char module[10];
392 HQUEUE16 hQueue = hFirstQueue;
394 DPRINTF( "Queue Msgs Thread Task Module\n" );
395 while (hQueue)
397 MESSAGEQUEUE *queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue );
398 if (!queue)
400 WARN_(msg)("Bad queue handle %04x\n", hQueue );
401 return;
403 if (!GetModuleName16( queue->thdb->process->task, module, sizeof(module )))
404 strcpy( module, "???" );
405 DPRINTF( "%04x %4d %p %04x %s\n", hQueue,queue->msgCount,
406 queue->thdb, queue->thdb->process->task, module );
407 hQueue = queue->next;
408 QUEUE_Unlock( queue );
410 DPRINTF( "\n" );
414 /***********************************************************************
415 * QUEUE_IsExitingQueue
417 BOOL QUEUE_IsExitingQueue( HQUEUE16 hQueue )
419 return (hExitingQueue && (hQueue == hExitingQueue));
423 /***********************************************************************
424 * QUEUE_SetExitingQueue
426 void QUEUE_SetExitingQueue( HQUEUE16 hQueue )
428 hExitingQueue = hQueue;
432 /***********************************************************************
433 * QUEUE_CreateMsgQueue
435 * Creates a message queue. Doesn't link it into queue list!
437 static HQUEUE16 QUEUE_CreateMsgQueue( BOOL16 bCreatePerQData )
439 HQUEUE16 hQueue;
440 MESSAGEQUEUE * msgQueue;
441 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
443 TRACE_(msg)("(): Creating message queue...\n");
445 if (!(hQueue = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT,
446 sizeof(MESSAGEQUEUE) )))
447 return 0;
449 msgQueue = (MESSAGEQUEUE *) GlobalLock16( hQueue );
450 if ( !msgQueue )
451 return 0;
453 msgQueue->self = hQueue;
454 msgQueue->wakeBits = msgQueue->changeBits = 0;
455 msgQueue->wWinVersion = pTask ? pTask->version : 0;
457 InitializeCriticalSection( &msgQueue->cSection );
458 MakeCriticalSectionGlobal( &msgQueue->cSection );
460 /* Create an Event object for waiting on message, used by win32 thread
461 only */
462 if ( !THREAD_IsWin16( THREAD_Current() ) )
464 msgQueue->hEvent = CreateEventA( NULL, FALSE, FALSE, NULL);
466 if (msgQueue->hEvent == 0)
468 WARN_(msg)("CreateEvent32A is not able to create an event object");
469 return 0;
471 msgQueue->hEvent = ConvertToGlobalHandle( msgQueue->hEvent );
473 else
474 msgQueue->hEvent = 0;
476 msgQueue->lockCount = 1;
477 msgQueue->magic = QUEUE_MAGIC;
479 /* Create and initialize our per queue data */
480 msgQueue->pQData = bCreatePerQData ? PERQDATA_CreateInstance() : NULL;
482 return hQueue;
486 /***********************************************************************
487 * QUEUE_FlushMessage
489 * Try to reply to all pending sent messages on exit.
491 void QUEUE_FlushMessages( MESSAGEQUEUE *queue )
493 SMSG *smsg;
494 MESSAGEQUEUE *senderQ = 0;
496 if( queue )
498 EnterCriticalSection( &queue->cSection );
500 /* empty the list of pending SendMessage waiting to be received */
501 while (queue->smPending)
503 smsg = QUEUE_RemoveSMSG( queue, SM_PENDING_LIST, 0);
505 senderQ = (MESSAGEQUEUE*)QUEUE_Lock( smsg->hSrcQueue );
506 if ( !senderQ )
507 continue;
509 /* return 0, to unblock other thread */
510 smsg->lResult = 0;
511 smsg->flags |= SMSG_HAVE_RESULT;
512 QUEUE_SetWakeBit( senderQ, QS_SMRESULT);
514 QUEUE_Unlock( senderQ );
517 QUEUE_ClearWakeBit( queue, QS_SENDMESSAGE );
519 LeaveCriticalSection( &queue->cSection );
524 /***********************************************************************
525 * QUEUE_DeleteMsgQueue
527 * Unlinks and deletes a message queue.
529 * Note: We need to mask asynchronous events to make sure PostMessage works
530 * even in the signal handler.
532 BOOL QUEUE_DeleteMsgQueue( HQUEUE16 hQueue )
534 MESSAGEQUEUE * msgQueue = (MESSAGEQUEUE*)QUEUE_Lock(hQueue);
535 HQUEUE16 *pPrev;
537 TRACE_(msg)("(): Deleting message queue %04x\n", hQueue);
539 if (!hQueue || !msgQueue)
541 WARN_(msg)("invalid argument.\n");
542 return 0;
545 msgQueue->magic = 0;
547 if( hCursorQueue == hQueue ) hCursorQueue = 0;
548 if( hActiveQueue == hQueue ) hActiveQueue = 0;
550 /* flush sent messages */
551 QUEUE_FlushMessages( msgQueue );
553 HeapLock( SystemHeap ); /* FIXME: a bit overkill */
555 /* Release per queue data if present */
556 if ( msgQueue->pQData )
558 PERQDATA_Release( msgQueue->pQData );
559 msgQueue->pQData = 0;
562 /* remove the message queue from the global link list */
563 pPrev = &hFirstQueue;
564 while (*pPrev && (*pPrev != hQueue))
566 MESSAGEQUEUE *msgQ = (MESSAGEQUEUE*)GlobalLock16(*pPrev);
568 /* sanity check */
569 if ( !msgQ || (msgQ->magic != QUEUE_MAGIC) )
571 /* HQUEUE link list is corrupted, try to exit gracefully */
572 WARN_(msg)("HQUEUE link list corrupted!\n");
573 pPrev = 0;
574 break;
576 pPrev = &msgQ->next;
578 if (pPrev && *pPrev) *pPrev = msgQueue->next;
579 msgQueue->self = 0;
581 HeapUnlock( SystemHeap );
583 /* free up resource used by MESSAGEQUEUE strcture */
584 msgQueue->lockCount--;
585 QUEUE_Unlock( msgQueue );
587 return 1;
591 /***********************************************************************
592 * QUEUE_CreateSysMsgQueue
594 * Create the system message queue, and set the double-click speed.
595 * Must be called only once.
597 BOOL QUEUE_CreateSysMsgQueue( int size )
599 /* Note: We dont need perQ data for the system message queue */
600 if (!(hmemSysMsgQueue = QUEUE_CreateMsgQueue( FALSE )))
601 return FALSE;
603 sysMsgQueue = (MESSAGEQUEUE *) GlobalLock16( hmemSysMsgQueue );
604 return TRUE;
608 /***********************************************************************
609 * QUEUE_GetSysQueue
611 MESSAGEQUEUE *QUEUE_GetSysQueue(void)
613 return sysMsgQueue;
617 /***********************************************************************
618 * QUEUE_SetWakeBit
620 * See "Windows Internals", p.449
622 void QUEUE_SetWakeBit( MESSAGEQUEUE *queue, WORD bit )
624 TRACE_(msg)("queue = %04x (wm=%04x), bit = %04x\n",
625 queue->self, queue->wakeMask, bit );
627 if (bit & QS_MOUSE) pMouseQueue = queue;
628 if (bit & QS_KEY) pKbdQueue = queue;
629 queue->changeBits |= bit;
630 queue->wakeBits |= bit;
631 if (queue->wakeMask & bit)
633 queue->wakeMask = 0;
635 /* Wake up thread waiting for message */
636 if ( THREAD_IsWin16( queue->thdb ) )
637 PostEvent16( queue->thdb->process->task );
638 else
640 SetEvent( queue->hEvent );
646 /***********************************************************************
647 * QUEUE_ClearWakeBit
649 void QUEUE_ClearWakeBit( MESSAGEQUEUE *queue, WORD bit )
651 queue->changeBits &= ~bit;
652 queue->wakeBits &= ~bit;
656 /***********************************************************************
657 * QUEUE_WaitBits
659 * See "Windows Internals", p.447
661 * return values:
662 * 0 if exit with timeout
663 * 1 otherwise
665 int QUEUE_WaitBits( WORD bits, DWORD timeout )
667 MESSAGEQUEUE *queue;
668 DWORD curTime = 0;
670 TRACE_(msg)("q %04x waiting for %04x\n", GetFastQueue16(), bits);
672 if ( THREAD_IsWin16( THREAD_Current() ) && (timeout != INFINITE) )
673 curTime = GetTickCount();
675 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
677 for (;;)
679 if (queue->changeBits & bits)
681 /* One of the bits is set; we can return */
682 queue->wakeMask = 0;
683 QUEUE_Unlock( queue );
684 return 1;
686 if (queue->wakeBits & QS_SENDMESSAGE)
688 /* Process the sent message immediately */
690 queue->wakeMask = 0;
691 QUEUE_ReceiveMessage( queue );
692 continue; /* nested sm crux */
695 queue->wakeMask = bits | QS_SENDMESSAGE;
696 if(queue->changeBits & bits)
698 continue;
701 TRACE_(msg)("%04x) wakeMask is %04x, waiting\n", queue->self, queue->wakeMask);
703 if ( !THREAD_IsWin16( THREAD_Current() ) )
705 BOOL bHasWin16Lock;
706 DWORD dwlc;
708 if ( (bHasWin16Lock = _ConfirmWin16Lock()) )
710 TRACE_(msg)("bHasWin16Lock=TRUE\n");
711 ReleaseThunkLock( &dwlc );
713 WaitForSingleObject( queue->hEvent, timeout );
714 if ( bHasWin16Lock )
716 RestoreThunkLock( dwlc );
719 else
721 if ( timeout == INFINITE )
722 WaitEvent16( 0 ); /* win 16 thread, use WaitEvent */
723 else
725 /* check for timeout, then give control to other tasks */
726 if (GetTickCount() - curTime > timeout)
729 QUEUE_Unlock( queue );
730 return 0; /* exit with timeout */
732 Yield16();
739 /***********************************************************************
740 * QUEUE_AddSMSG
742 * This routine is called when a SMSG need to be added to one of the three
743 * SM list. (SM_PROCESSING_LIST, SM_PENDING_LIST, SM_WAITING_LIST)
745 BOOL QUEUE_AddSMSG( MESSAGEQUEUE *queue, int list, SMSG *smsg )
747 TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
748 smsg, SPY_GetMsgName(smsg->msg));
750 switch (list)
752 case SM_PROCESSING_LIST:
753 /* don't need to be thread safe, only accessed by the
754 thread associated with the sender queue */
755 smsg->nextProcessing = queue->smProcessing;
756 queue->smProcessing = smsg;
757 break;
759 case SM_WAITING_LIST:
760 /* don't need to be thread safe, only accessed by the
761 thread associated with the receiver queue */
762 smsg->nextWaiting = queue->smWaiting;
763 queue->smWaiting = smsg;
764 break;
766 case SM_PENDING_LIST:
768 /* make it thread safe, could be accessed by the sender and
769 receiver thread */
770 SMSG **prev;
772 EnterCriticalSection( &queue->cSection );
773 smsg->nextPending = NULL;
774 prev = &queue->smPending;
775 while ( *prev )
776 prev = &(*prev)->nextPending;
777 *prev = smsg;
778 LeaveCriticalSection( &queue->cSection );
780 QUEUE_SetWakeBit( queue, QS_SENDMESSAGE );
781 break;
784 default:
785 WARN_(sendmsg)("Invalid list: %d", list);
786 break;
789 return TRUE;
793 /***********************************************************************
794 * QUEUE_RemoveSMSG
796 * This routine is called when a SMSG need to be remove from one of the three
797 * SM list. (SM_PROCESSING_LIST, SM_PENDING_LIST, SM_WAITING_LIST)
798 * If smsg == 0, remove the first smsg from the specified list
800 SMSG *QUEUE_RemoveSMSG( MESSAGEQUEUE *queue, int list, SMSG *smsg )
803 switch (list)
805 case SM_PROCESSING_LIST:
806 /* don't need to be thread safe, only accessed by the
807 thread associated with the sender queue */
809 /* if smsg is equal to null, it means the first in the list */
810 if (!smsg)
811 smsg = queue->smProcessing;
813 TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
814 smsg, SPY_GetMsgName(smsg->msg));
815 /* In fact SM_PROCESSING_LIST is a stack, and smsg
816 should be always at the top of the list */
817 if ( (smsg != queue->smProcessing) || !queue->smProcessing )
819 ERR_(sendmsg)("smsg not at the top of Processing list, smsg=0x%p queue=0x%p", smsg, queue);
820 return 0;
822 else
824 queue->smProcessing = smsg->nextProcessing;
825 smsg->nextProcessing = 0;
827 return smsg;
829 case SM_WAITING_LIST:
830 /* don't need to be thread safe, only accessed by the
831 thread associated with the receiver queue */
833 /* if smsg is equal to null, it means the first in the list */
834 if (!smsg)
835 smsg = queue->smWaiting;
837 TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
838 smsg, SPY_GetMsgName(smsg->msg));
839 /* In fact SM_WAITING_LIST is a stack, and smsg
840 should be always at the top of the list */
841 if ( (smsg != queue->smWaiting) || !queue->smWaiting )
843 ERR_(sendmsg)("smsg not at the top of Waiting list, smsg=0x%p queue=0x%p", smsg, queue);
844 return 0;
846 else
848 queue->smWaiting = smsg->nextWaiting;
849 smsg->nextWaiting = 0;
851 return smsg;
853 case SM_PENDING_LIST:
854 /* make it thread safe, could be accessed by the sender and
855 receiver thread */
856 EnterCriticalSection( &queue->cSection );
858 if (!smsg || !queue->smPending)
859 smsg = queue->smPending;
860 else
862 ERR_(sendmsg)("should always remove the top one in Pending list, smsg=0x%p queue=0x%p", smsg, queue);
863 LeaveCriticalSection( &queue->cSection );
864 return 0;
867 TRACE_(sendmsg)("queue=%x, list=%d, smsg=%p msg=%s\n", queue->self, list,
868 smsg, SPY_GetMsgName(smsg->msg));
870 queue->smPending = smsg->nextPending;
871 smsg->nextPending = 0;
873 /* if no more SMSG in Pending list, clear QS_SENDMESSAGE flag */
874 if (!queue->smPending)
875 QUEUE_ClearWakeBit( queue, QS_SENDMESSAGE );
877 LeaveCriticalSection( &queue->cSection );
878 return smsg;
880 default:
881 WARN_(sendmsg)("Invalid list: %d", list);
882 break;
885 return 0;
889 /***********************************************************************
890 * QUEUE_ReceiveMessage
892 * This routine is called when a sent message is waiting for the queue.
894 void QUEUE_ReceiveMessage( MESSAGEQUEUE *queue )
896 LRESULT result = 0;
897 SMSG *smsg;
898 MESSAGEQUEUE *senderQ;
900 TRACE_(sendmsg)("queue %04x\n", queue->self );
902 if ( !(queue->wakeBits & QS_SENDMESSAGE) && queue->smPending )
904 TRACE_(sendmsg)("\trcm: nothing to do\n");
905 return;
908 /* remove smsg on the top of the pending list and put it in the processing list */
909 smsg = QUEUE_RemoveSMSG(queue, SM_PENDING_LIST, 0);
910 QUEUE_AddSMSG(queue, SM_WAITING_LIST, smsg);
912 TRACE_(sendmsg)("RM: %s [%04x] (%04x -> %04x)\n",
913 SPY_GetMsgName(smsg->msg), smsg->msg, smsg->hSrcQueue, smsg->hDstQueue );
915 if (IsWindow( smsg->hWnd ))
917 WND *wndPtr = WIN_FindWndPtr( smsg->hWnd );
918 DWORD extraInfo = queue->GetMessageExtraInfoVal; /* save ExtraInfo */
920 /* use sender queue extra info value while calling the window proc */
921 senderQ = (MESSAGEQUEUE*)QUEUE_Lock( smsg->hSrcQueue );
922 if (senderQ)
924 queue->GetMessageExtraInfoVal = senderQ->GetMessageExtraInfoVal;
925 QUEUE_Unlock( senderQ );
928 /* call the right version of CallWindowProcXX */
929 if (smsg->flags & SMSG_WIN32)
931 TRACE_(sendmsg)("\trcm: msg is Win32\n" );
932 if (smsg->flags & SMSG_UNICODE)
933 result = CallWindowProcW( wndPtr->winproc,
934 smsg->hWnd, smsg->msg,
935 smsg->wParam, smsg->lParam );
936 else
937 result = CallWindowProcA( wndPtr->winproc,
938 smsg->hWnd, smsg->msg,
939 smsg->wParam, smsg->lParam );
941 else /* Win16 message */
942 result = CallWindowProc16( (WNDPROC16)wndPtr->winproc,
943 (HWND16) smsg->hWnd,
944 (UINT16) smsg->msg,
945 LOWORD (smsg->wParam),
946 smsg->lParam );
948 queue->GetMessageExtraInfoVal = extraInfo; /* Restore extra info */
949 WIN_ReleaseWndPtr(wndPtr);
950 TRACE_(sendmsg)("result = %08x\n", (unsigned)result );
952 else WARN_(sendmsg)("\trcm: bad hWnd\n");
955 /* set SMSG_SENDING_REPLY flag to tell ReplyMessage16, it's not
956 an early reply */
957 smsg->flags |= SMSG_SENDING_REPLY;
958 ReplyMessage( result );
960 TRACE_(sendmsg)("done! \n" );
965 /***********************************************************************
966 * QUEUE_AddMsg
968 * Add a message to the queue. Return FALSE if queue is full.
970 BOOL QUEUE_AddMsg( HQUEUE16 hQueue, MSG *msg, DWORD extraInfo )
972 MESSAGEQUEUE *msgQueue;
973 QMSG *qmsg;
976 if (!(msgQueue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return FALSE;
978 /* allocate new message in global heap for now */
979 if (!(qmsg = (QMSG *) HeapAlloc( SystemHeap, 0, sizeof(QMSG) ) ))
981 QUEUE_Unlock( msgQueue );
982 return 0;
985 EnterCriticalSection( &msgQueue->cSection );
987 /* Store message */
988 qmsg->msg = *msg;
989 qmsg->extraInfo = extraInfo;
991 /* insert the message in the link list */
992 qmsg->nextMsg = 0;
993 qmsg->prevMsg = msgQueue->lastMsg;
995 if (msgQueue->lastMsg)
996 msgQueue->lastMsg->nextMsg = qmsg;
998 /* update first and last anchor in message queue */
999 msgQueue->lastMsg = qmsg;
1000 if (!msgQueue->firstMsg)
1001 msgQueue->firstMsg = qmsg;
1003 msgQueue->msgCount++;
1005 LeaveCriticalSection( &msgQueue->cSection );
1007 QUEUE_SetWakeBit( msgQueue, QS_POSTMESSAGE );
1008 QUEUE_Unlock( msgQueue );
1010 return TRUE;
1015 /***********************************************************************
1016 * QUEUE_FindMsg
1018 * Find a message matching the given parameters. Return -1 if none available.
1020 QMSG* QUEUE_FindMsg( MESSAGEQUEUE * msgQueue, HWND hwnd, int first, int last )
1022 QMSG* qmsg;
1024 EnterCriticalSection( &msgQueue->cSection );
1026 if (!msgQueue->msgCount)
1027 qmsg = 0;
1028 else if (!hwnd && !first && !last)
1029 qmsg = msgQueue->firstMsg;
1030 else
1032 /* look in linked list for message matching first and last criteria */
1033 for (qmsg = msgQueue->firstMsg; qmsg; qmsg = qmsg->nextMsg)
1035 MSG *msg = &(qmsg->msg);
1037 if (!hwnd || (msg->hwnd == hwnd))
1039 if (!first && !last)
1040 break; /* found it */
1042 if ((msg->message >= first) && (!last || (msg->message <= last)))
1043 break; /* found it */
1048 LeaveCriticalSection( &msgQueue->cSection );
1050 return qmsg;
1055 /***********************************************************************
1056 * QUEUE_RemoveMsg
1058 * Remove a message from the queue (pos must be a valid position).
1060 void QUEUE_RemoveMsg( MESSAGEQUEUE * msgQueue, QMSG *qmsg )
1062 EnterCriticalSection( &msgQueue->cSection );
1064 /* set the linked list */
1065 if (qmsg->prevMsg)
1066 qmsg->prevMsg->nextMsg = qmsg->nextMsg;
1068 if (qmsg->nextMsg)
1069 qmsg->nextMsg->prevMsg = qmsg->prevMsg;
1071 if (msgQueue->firstMsg == qmsg)
1072 msgQueue->firstMsg = qmsg->nextMsg;
1074 if (msgQueue->lastMsg == qmsg)
1075 msgQueue->lastMsg = qmsg->prevMsg;
1077 /* deallocate the memory for the message */
1078 HeapFree( SystemHeap, 0, qmsg );
1080 msgQueue->msgCount--;
1081 if (!msgQueue->msgCount) msgQueue->wakeBits &= ~QS_POSTMESSAGE;
1083 LeaveCriticalSection( &msgQueue->cSection );
1087 /***********************************************************************
1088 * QUEUE_WakeSomeone
1090 * Wake a queue upon reception of a hardware event.
1092 static void QUEUE_WakeSomeone( UINT message )
1094 WND* wndPtr = NULL;
1095 WORD wakeBit;
1096 HWND hwnd;
1097 HQUEUE16 hQueue = 0;
1098 MESSAGEQUEUE *queue = NULL;
1100 if (hCursorQueue)
1101 hQueue = hCursorQueue;
1103 if( (message >= WM_KEYFIRST) && (message <= WM_KEYLAST) )
1105 wakeBit = QS_KEY;
1106 if( hActiveQueue )
1107 hQueue = hActiveQueue;
1109 else
1111 wakeBit = (message == WM_MOUSEMOVE) ? QS_MOUSEMOVE : QS_MOUSEBUTTON;
1112 if( (hwnd = GetCapture()) )
1113 if( (wndPtr = WIN_FindWndPtr( hwnd )) )
1115 hQueue = wndPtr->hmemTaskQ;
1116 WIN_ReleaseWndPtr(wndPtr);
1120 if( (hwnd = GetSysModalWindow16()) )
1122 if( (wndPtr = WIN_FindWndPtr( hwnd )) )
1124 hQueue = wndPtr->hmemTaskQ;
1125 WIN_ReleaseWndPtr(wndPtr);
1129 if (hQueue)
1130 queue = QUEUE_Lock( hQueue );
1132 if( !queue )
1134 queue = QUEUE_Lock( hFirstQueue );
1135 while( queue )
1137 if (queue->wakeMask & wakeBit) break;
1139 QUEUE_Unlock(queue);
1140 queue = QUEUE_Lock( queue->next );
1142 if( !queue )
1144 WARN_(msg)("couldn't find queue\n");
1145 return;
1149 QUEUE_SetWakeBit( queue, wakeBit );
1151 QUEUE_Unlock( queue );
1155 /***********************************************************************
1156 * hardware_event
1158 * Add an event to the system message queue.
1159 * Note: the position is relative to the desktop window.
1161 void hardware_event( WORD message, WORD wParam, LONG lParam,
1162 int xPos, int yPos, DWORD time, DWORD extraInfo )
1164 MSG *msg;
1165 QMSG *qmsg;
1166 int mergeMsg = 0;
1168 if (!sysMsgQueue) return;
1170 EnterCriticalSection( &sysMsgQueue->cSection );
1172 /* Merge with previous event if possible */
1173 qmsg = sysMsgQueue->lastMsg;
1175 if ((message == WM_MOUSEMOVE) && sysMsgQueue->lastMsg)
1177 msg = &(sysMsgQueue->lastMsg->msg);
1179 if ((msg->message == message) && (msg->wParam == wParam))
1181 /* Merge events */
1182 qmsg = sysMsgQueue->lastMsg;
1183 mergeMsg = 1;
1187 if (!mergeMsg)
1189 /* Should I limit the number of message in
1190 the system message queue??? */
1192 /* Don't merge allocate a new msg in the global heap */
1194 if (!(qmsg = (QMSG *) HeapAlloc( SystemHeap, 0, sizeof(QMSG) ) ))
1196 LeaveCriticalSection( &sysMsgQueue->cSection );
1197 return;
1200 /* put message at the end of the linked list */
1201 qmsg->nextMsg = 0;
1202 qmsg->prevMsg = sysMsgQueue->lastMsg;
1204 if (sysMsgQueue->lastMsg)
1205 sysMsgQueue->lastMsg->nextMsg = qmsg;
1207 /* set last and first anchor index in system message queue */
1208 sysMsgQueue->lastMsg = qmsg;
1209 if (!sysMsgQueue->firstMsg)
1210 sysMsgQueue->firstMsg = qmsg;
1212 sysMsgQueue->msgCount++;
1215 /* Store message */
1216 msg = &(qmsg->msg);
1217 msg->hwnd = 0;
1218 msg->message = message;
1219 msg->wParam = wParam;
1220 msg->lParam = lParam;
1221 msg->time = time;
1222 msg->pt.x = xPos;
1223 msg->pt.y = yPos;
1224 qmsg->extraInfo = extraInfo;
1226 LeaveCriticalSection( &sysMsgQueue->cSection );
1228 QUEUE_WakeSomeone( message );
1232 /***********************************************************************
1233 * QUEUE_GetQueueTask
1235 HTASK16 QUEUE_GetQueueTask( HQUEUE16 hQueue )
1237 HTASK16 hTask = 0;
1239 MESSAGEQUEUE *queue = QUEUE_Lock( hQueue );
1241 if (queue)
1243 hTask = queue->thdb->process->task;
1244 QUEUE_Unlock( queue );
1247 return hTask;
1252 /***********************************************************************
1253 * QUEUE_IncPaintCount
1255 void QUEUE_IncPaintCount( HQUEUE16 hQueue )
1257 MESSAGEQUEUE *queue;
1259 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1260 queue->wPaintCount++;
1261 QUEUE_SetWakeBit( queue, QS_PAINT );
1262 QUEUE_Unlock( queue );
1266 /***********************************************************************
1267 * QUEUE_DecPaintCount
1269 void QUEUE_DecPaintCount( HQUEUE16 hQueue )
1271 MESSAGEQUEUE *queue;
1273 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1274 queue->wPaintCount--;
1275 if (!queue->wPaintCount) queue->wakeBits &= ~QS_PAINT;
1276 QUEUE_Unlock( queue );
1280 /***********************************************************************
1281 * QUEUE_IncTimerCount
1283 void QUEUE_IncTimerCount( HQUEUE16 hQueue )
1285 MESSAGEQUEUE *queue;
1287 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1288 queue->wTimerCount++;
1289 QUEUE_SetWakeBit( queue, QS_TIMER );
1290 QUEUE_Unlock( queue );
1294 /***********************************************************************
1295 * QUEUE_DecTimerCount
1297 void QUEUE_DecTimerCount( HQUEUE16 hQueue )
1299 MESSAGEQUEUE *queue;
1301 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return;
1302 queue->wTimerCount--;
1303 if (!queue->wTimerCount) queue->wakeBits &= ~QS_TIMER;
1304 QUEUE_Unlock( queue );
1308 /***********************************************************************
1309 * PostQuitMessage16 (USER.6)
1311 void WINAPI PostQuitMessage16( INT16 exitCode )
1313 PostQuitMessage( exitCode );
1317 /***********************************************************************
1318 * PostQuitMessage32 (USER32.421)
1320 * PostQuitMessage() posts a message to the system requesting an
1321 * application to terminate execution. As a result of this function,
1322 * the WM_QUIT message is posted to the application, and
1323 * PostQuitMessage() returns immediately. The exitCode parameter
1324 * specifies an application-defined exit code, which appears in the
1325 * _wParam_ parameter of the WM_QUIT message posted to the application.
1327 * CONFORMANCE
1329 * ECMA-234, Win32
1331 void WINAPI PostQuitMessage( INT exitCode )
1333 MESSAGEQUEUE *queue;
1335 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return;
1336 queue->wPostQMsg = TRUE;
1337 queue->wExitCode = (WORD)exitCode;
1338 QUEUE_Unlock( queue );
1342 /***********************************************************************
1343 * GetWindowTask16 (USER.224)
1345 HTASK16 WINAPI GetWindowTask16( HWND16 hwnd )
1347 HTASK16 retvalue;
1348 WND *wndPtr = WIN_FindWndPtr( hwnd );
1350 if (!wndPtr) return 0;
1351 retvalue = QUEUE_GetQueueTask( wndPtr->hmemTaskQ );
1352 WIN_ReleaseWndPtr(wndPtr);
1353 return retvalue;
1356 /***********************************************************************
1357 * GetWindowThreadProcessId (USER32.313)
1359 DWORD WINAPI GetWindowThreadProcessId( HWND hwnd, LPDWORD process )
1361 DWORD retvalue;
1362 MESSAGEQUEUE *queue;
1364 WND *wndPtr = WIN_FindWndPtr( hwnd );
1365 if (!wndPtr) return 0;
1367 queue = QUEUE_Lock( wndPtr->hmemTaskQ );
1368 WIN_ReleaseWndPtr(wndPtr);
1370 if (!queue) return 0;
1372 if ( process ) *process = (DWORD)queue->thdb->process->server_pid;
1373 retvalue = (DWORD)queue->thdb->server_tid;
1375 QUEUE_Unlock( queue );
1376 return retvalue;
1380 /***********************************************************************
1381 * SetMessageQueue16 (USER.266)
1383 BOOL16 WINAPI SetMessageQueue16( INT16 size )
1385 return SetMessageQueue( size );
1389 /***********************************************************************
1390 * SetMessageQueue32 (USER32.494)
1392 BOOL WINAPI SetMessageQueue( INT size )
1394 /* now obsolete the message queue will be expanded dynamically
1395 as necessary */
1397 /* access the queue to create it if it's not existing */
1398 GetFastQueue16();
1400 return TRUE;
1403 /***********************************************************************
1404 * InitThreadInput (USER.409)
1406 HQUEUE16 WINAPI InitThreadInput16( WORD unknown, WORD flags )
1408 HQUEUE16 hQueue;
1409 MESSAGEQUEUE *queuePtr;
1411 THDB *thdb = THREAD_Current();
1413 if (!thdb)
1414 return 0;
1416 hQueue = thdb->teb.queue;
1418 if ( !hQueue )
1420 /* Create thread message queue */
1421 if( !(hQueue = QUEUE_CreateMsgQueue( TRUE )))
1423 WARN_(msg)("failed!\n");
1424 return FALSE;
1427 /* Link new queue into list */
1428 queuePtr = (MESSAGEQUEUE *)QUEUE_Lock( hQueue );
1429 queuePtr->thdb = THREAD_Current();
1431 HeapLock( SystemHeap ); /* FIXME: a bit overkill */
1432 SetThreadQueue16( 0, hQueue );
1433 thdb->teb.queue = hQueue;
1435 queuePtr->next = hFirstQueue;
1436 hFirstQueue = hQueue;
1437 HeapUnlock( SystemHeap );
1439 QUEUE_Unlock( queuePtr );
1442 return hQueue;
1445 /***********************************************************************
1446 * GetQueueStatus16 (USER.334)
1448 DWORD WINAPI GetQueueStatus16( UINT16 flags )
1450 MESSAGEQUEUE *queue;
1451 DWORD ret;
1453 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1454 ret = MAKELONG( queue->changeBits, queue->wakeBits );
1455 queue->changeBits = 0;
1456 QUEUE_Unlock( queue );
1458 return ret & MAKELONG( flags, flags );
1461 /***********************************************************************
1462 * GetQueueStatus32 (USER32.283)
1464 DWORD WINAPI GetQueueStatus( UINT flags )
1466 MESSAGEQUEUE *queue;
1467 DWORD ret;
1469 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1470 ret = MAKELONG( queue->changeBits, queue->wakeBits );
1471 queue->changeBits = 0;
1472 QUEUE_Unlock( queue );
1474 return ret & MAKELONG( flags, flags );
1478 /***********************************************************************
1479 * GetInputState16 (USER.335)
1481 BOOL16 WINAPI GetInputState16(void)
1483 return GetInputState();
1486 /***********************************************************************
1487 * WaitForInputIdle (USER32.577)
1489 DWORD WINAPI WaitForInputIdle (HANDLE hProcess, DWORD dwTimeOut)
1491 FIXME_(msg)("(hProcess=%d, dwTimeOut=%ld): stub\n", hProcess, dwTimeOut);
1493 return WAIT_TIMEOUT;
1497 /***********************************************************************
1498 * GetInputState32 (USER32.244)
1500 BOOL WINAPI GetInputState(void)
1502 MESSAGEQUEUE *queue;
1503 BOOL ret;
1505 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() )))
1506 return FALSE;
1507 ret = queue->wakeBits & (QS_KEY | QS_MOUSEBUTTON);
1508 QUEUE_Unlock( queue );
1510 return ret;
1513 /***********************************************************************
1514 * UserYield (USER.332)
1516 void WINAPI UserYield16(void)
1518 MESSAGEQUEUE *queue;
1520 /* Handle sent messages */
1521 queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() );
1523 while (queue && (queue->wakeBits & QS_SENDMESSAGE))
1524 QUEUE_ReceiveMessage( queue );
1526 QUEUE_Unlock( queue );
1528 /* Yield */
1529 if ( THREAD_IsWin16( THREAD_Current() ) )
1530 OldYield16();
1531 else
1533 DWORD count;
1535 ReleaseThunkLock(&count);
1536 RestoreThunkLock(count);
1539 /* Handle sent messages again */
1540 queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() );
1542 while (queue && (queue->wakeBits & QS_SENDMESSAGE))
1543 QUEUE_ReceiveMessage( queue );
1545 QUEUE_Unlock( queue );
1548 /***********************************************************************
1549 * GetMessagePos (USER.119) (USER32.272)
1551 * The GetMessagePos() function returns a long value representing a
1552 * cursor position, in screen coordinates, when the last message
1553 * retrieved by the GetMessage() function occurs. The x-coordinate is
1554 * in the low-order word of the return value, the y-coordinate is in
1555 * the high-order word. The application can use the MAKEPOINT()
1556 * macro to obtain a POINT structure from the return value.
1558 * For the current cursor position, use GetCursorPos().
1560 * RETURNS
1562 * Cursor position of last message on success, zero on failure.
1564 * CONFORMANCE
1566 * ECMA-234, Win32
1569 DWORD WINAPI GetMessagePos(void)
1571 MESSAGEQUEUE *queue;
1572 DWORD ret;
1574 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1575 ret = queue->GetMessagePosVal;
1576 QUEUE_Unlock( queue );
1578 return ret;
1582 /***********************************************************************
1583 * GetMessageTime (USER.120) (USER32.273)
1585 * GetMessageTime() returns the message time for the last message
1586 * retrieved by the function. The time is measured in milliseconds with
1587 * the same offset as GetTickCount().
1589 * Since the tick count wraps, this is only useful for moderately short
1590 * relative time comparisons.
1592 * RETURNS
1594 * Time of last message on success, zero on failure.
1596 * CONFORMANCE
1598 * ECMA-234, Win32
1601 LONG WINAPI GetMessageTime(void)
1603 MESSAGEQUEUE *queue;
1604 LONG ret;
1606 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1607 ret = queue->GetMessageTimeVal;
1608 QUEUE_Unlock( queue );
1610 return ret;
1614 /***********************************************************************
1615 * GetMessageExtraInfo (USER.288) (USER32.271)
1617 LONG WINAPI GetMessageExtraInfo(void)
1619 MESSAGEQUEUE *queue;
1620 LONG ret;
1622 if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0;
1623 ret = queue->GetMessageExtraInfoVal;
1624 QUEUE_Unlock( queue );
1626 return ret;