Moved idle event handling to the server.

diff --git a/windows/message.c b/windows/message.c
index d2c1a67..2c42ad6 100644
--- a/windows/message.c
+++ b/windows/message.c
@@ -23,7 +23,6 @@
 #include "queue.h"
 #include "winproc.h"
 #include "task.h"
-#include "process.h"
 #include "selectors.h"
 #include "thread.h"
 #include "options.h"
@@ -31,9 +30,9 @@
 #include "struct32.h"
 #include "debugtools.h"
 
-DEFAULT_DEBUG_CHANNEL(msg)
-DECLARE_DEBUG_CHANNEL(key)
-DECLARE_DEBUG_CHANNEL(sendmsg)
+DEFAULT_DEBUG_CHANNEL(msg);
+DECLARE_DEBUG_CHANNEL(key);
+DECLARE_DEBUG_CHANNEL(sendmsg);
 
 #define WM_NCMOUSEFIRST         WM_NCMOUSEMOVE
 #define WM_NCMOUSELAST          WM_NCMBUTTONDBLCLK
@@ -1952,7 +1951,6 @@
     DWORD i;
     HANDLE handles[MAXIMUM_WAIT_OBJECTS];
     DWORD ret;
-    PDB * pdb = PROCESS_Current();
 
     HQUEUE16 hQueue = GetFastQueue16();
     MESSAGEQUEUE *msgQueue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue );
@@ -1988,7 +1986,6 @@
 	/*
 	 * Check the handles in the list.
 	 */
-	SetEvent ( pdb->idle_event );
 	ret = WaitForMultipleObjects(nCount, pHandles, fWaitAll, 5L);
 
 	/*
@@ -2020,18 +2017,11 @@
     {
     /* Add the thread event to the handle list */
       for (i = 0; i < nCount; i++)
-	handles[i] = pHandles[i];
-      handles[nCount] = msgQueue->hEvent;
-
-      if ( pdb->main_queue == INVALID_HANDLE_VALUE16 ) pdb->main_queue = hQueue;
-      if ( pdb->main_queue == hQueue ) SetEvent ( pdb->idle_event );
+ 	handles[i] = pHandles[i];
+      handles[nCount] = msgQueue->server_queue;
       ret = WaitForMultipleObjects( nCount+1, handles, fWaitAll, dwMilliseconds );
-      if ( pdb->main_queue == hQueue ) ResetEvent ( pdb->idle_event );
-
     } 
-
     QUEUE_Unlock( msgQueue );
-    
     return ret;
 }
 
diff --git a/windows/queue.c b/windows/queue.c
index 67fabc5..f4705b2 100644
--- a/windows/queue.c
+++ b/windows/queue.c
@@ -20,12 +20,12 @@
 #include "hook.h"
 #include "heap.h"
 #include "thread.h"
-#include "process.h"
 #include "debugtools.h"
+#include "server.h"
 #include "spy.h"
 
-DECLARE_DEBUG_CHANNEL(msg)
-DECLARE_DEBUG_CHANNEL(sendmsg)
+DECLARE_DEBUG_CHANNEL(msg);
+DECLARE_DEBUG_CHANNEL(sendmsg);
 
 #define MAX_QUEUE_SIZE   120  /* Max. size of a message queue */
 
@@ -342,8 +342,8 @@
         if ( --queue->lockCount == 0 )
         {
             DeleteCriticalSection ( &queue->cSection );
-            if (queue->hEvent)
-                CloseHandle( queue->hEvent );
+            if (queue->server_queue)
+                CloseHandle( queue->server_queue );
             GlobalFree16( queue->self );
         }
     
@@ -443,6 +443,7 @@
     HQUEUE16 hQueue;
     MESSAGEQUEUE * msgQueue;
     TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
+    struct get_msg_queue_request *req = get_req_buffer();
 
     TRACE_(msg)("(): Creating message queue...\n");
 
@@ -454,6 +455,15 @@
     if ( !msgQueue )
         return 0;
 
+    if (server_call( REQ_GET_MSG_QUEUE ))
+    {
+        ERR_(msg)("Cannot get thread queue");
+        GlobalFree16( hQueue );
+        return 0;
+    }
+    msgQueue->server_queue = req->handle;
+    msgQueue->server_queue = ConvertToGlobalHandle( msgQueue->server_queue );
+
     msgQueue->self        = hQueue;
     msgQueue->wakeBits    = msgQueue->changeBits = 0;
     msgQueue->wWinVersion = pTask ? pTask->version : 0;
@@ -461,22 +471,6 @@
     InitializeCriticalSection( &msgQueue->cSection );
     MakeCriticalSectionGlobal( &msgQueue->cSection );
 
-    /* Create an Event object for waiting on message, used by win32 thread
-       only */
-    if ( !THREAD_IsWin16( NtCurrentTeb() ) )
-    {
-        msgQueue->hEvent = CreateEventA( NULL, FALSE, FALSE, NULL);
-
-        if (msgQueue->hEvent == 0)
-        {
-            WARN_(msg)("CreateEventA is not able to create an event object");
-            return 0;
-        }
-        msgQueue->hEvent = ConvertToGlobalHandle( msgQueue->hEvent );
-    }
-    else
-        msgQueue->hEvent = 0;
-         
     msgQueue->lockCount = 1;
     msgQueue->magic = QUEUE_MAGIC;
     
@@ -645,7 +639,10 @@
         }
         else
         {
-            SetEvent( queue->hEvent );
+            struct wake_queue_request *req = get_req_buffer();
+            req->handle = queue->server_queue;
+            req->bits   = bit;
+            server_call( REQ_WAKE_QUEUE );
         }
     }
 }
@@ -675,7 +672,6 @@
     MESSAGEQUEUE *queue;
     DWORD curTime = 0;
     HQUEUE16 hQueue;
-    PDB * pdb;
 
     TRACE_(msg)("q %04x waiting for %04x\n", GetFastQueue16(), bits);
 
@@ -685,8 +681,6 @@
     hQueue = GetFastQueue16();
     if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue ))) return 0;
     
-    pdb = PROCESS_Current();
-
     for (;;)
     {
         if (queue->changeBits & bits)
@@ -724,22 +718,7 @@
 	        ReleaseThunkLock( &dwlc );
 	    }
 
-
-	    if ( pdb->main_queue == INVALID_HANDLE_VALUE16 ) 
-	    {
-	        pdb->main_queue = hQueue;
-	    }
-	    if ( pdb->main_queue == hQueue ) 
-	    {
-	        SetEvent ( pdb->idle_event );
-	    }
-
-	    WaitForSingleObject( queue->hEvent, timeout );
-
-	    if ( pdb->main_queue == hQueue ) 
-	    {
-	        ResetEvent ( pdb->idle_event );
-	    }
+	    WaitForSingleObject( queue->server_queue, timeout );
 
 	    if ( bHasWin16Lock ) 
 	    {
@@ -748,8 +727,6 @@
         }
         else
         {
-	    SetEvent ( pdb->idle_event );
-
             if ( timeout == INFINITE )
                 WaitEvent16( 0 );  /* win 16 thread, use WaitEvent */
             else
@@ -1522,34 +1499,21 @@
  */
 DWORD WINAPI WaitForInputIdle (HANDLE hProcess, DWORD dwTimeOut)
 {
-  PDB * pdb;
-  DWORD cur_time, ret, pid = MapProcessHandle ( hProcess );
+    DWORD cur_time, ret;
+    HANDLE idle_event;
+    struct wait_input_idle_request *req = get_req_buffer();
 
-  /* Check whether the calling process is a command line application */
-  if (!THREAD_IsWin16(NtCurrentTeb() ) &&
-      (PROCESS_Current()->flags & PDB32_CONSOLE_PROC))
-  {
-    TRACE_(msg)("not a win32 GUI application!\n" );
-    return 0; 
-  }
-  
-  if (!(pdb = PROCESS_IdToPDB( pid ))) return 0;
-
-  /* check whether we are waiting for a win32 process or the win16 subsystem */
-  if ( pdb->flags & PDB32_WIN16_PROC ) {
-    if ( THREAD_IsWin16(NtCurrentTeb()) ) return 0;
-  }
-    else { /* target is win32 */
-    if ( pdb->flags & PDB32_CONSOLE_PROC ) return 0;
-    if ( GetFastQueue16() == pdb->main_queue ) return 0;
-  }
+    req->handle = hProcess;
+    req->timeout = dwTimeOut;
+    if (server_call( REQ_WAIT_INPUT_IDLE )) return 0xffffffff;
+    if ((idle_event = req->event) == -1) return 0;  /* no event to wait on */
 
   cur_time = GetTickCount();
   
-  TRACE_(msg)("waiting for %x\n", pdb->idle_event );
+  TRACE_(msg)("waiting for %x\n", idle_event );
   while ( dwTimeOut > GetTickCount() - cur_time || dwTimeOut == INFINITE ) {
 
-    ret = MsgWaitForMultipleObjects ( 1, &pdb->idle_event, FALSE, dwTimeOut, QS_SENDMESSAGE );
+    ret = MsgWaitForMultipleObjects ( 1, &idle_event, FALSE, dwTimeOut, QS_SENDMESSAGE );
     if ( ret == ( WAIT_OBJECT_0 + 1 )) {
       MESSAGEQUEUE * queue;
       if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() ))) return 0xFFFFFFFF;
diff --git a/windows/user.c b/windows/user.c
index c427c17..e77f20c 100644
--- a/windows/user.c
+++ b/windows/user.c
@@ -210,9 +210,7 @@
 WORD WINAPI UserSignalProc( UINT uCode, DWORD dwThreadOrProcessID,
                             DWORD dwFlags, HMODULE16 hModule )
 {
-    static HANDLE win16_idle_event;
     HINSTANCE16 hInst;
-    PDB * pdb;
 
     /* FIXME: Proper reaction to most signals still missing. */
 
@@ -239,32 +237,12 @@
         break;
 
     case USIG_PROCESS_CREATE:
-      pdb = PROCESS_Current();
-
-      /* Create the idle event for the process. We have just one idle_event for all
-	 win16 processes, while each win32 process has its own */
-
-      if ( pdb->flags & PDB32_WIN16_PROC )
-      {
-          if (!win16_idle_event)
-          {
-              win16_idle_event = CreateEventA ( NULL, TRUE, FALSE, NULL );
-              win16_idle_event = ConvertToGlobalHandle ( win16_idle_event );
-          }
-          pdb->idle_event = win16_idle_event;
-      }
-      else { /* win32 process */
-	pdb->idle_event = CreateEventA ( NULL, TRUE, FALSE, NULL );
-        pdb->idle_event = ConvertToGlobalHandle ( pdb->idle_event );
-	TRACE_(win)("created win32 idle event: %x\n", pdb->idle_event );
-      }
       break;
 
     case USIG_PROCESS_INIT:
     case USIG_PROCESS_LOADED:
       break;
     case USIG_PROCESS_RUNNING:
-	SetEvent ( PROCESS_Current()->idle_event );
         break;
 
     case USIG_PROCESS_EXIT:
@@ -273,12 +251,6 @@
     case USIG_PROCESS_DESTROY:      
       hInst = ((TDB *)GlobalLock16( GetCurrentTask() ))->hInstance;
       USER_AppExit( hInst );
-
-      pdb = PROCESS_Current();
-      if ( ! (pdb->flags & PDB32_WIN16_PROC) ) {
-	TRACE_(win)("destroying win32 idle event: %x\n", pdb->idle_event );
-	CloseHandle ( pdb->idle_event );	
-      }
       break;
 
     default: