Fix for deadlock when using private colormap.
[wine] / dlls / user / message.c
index 1c5afa6..ed07469 100644 (file)
@@ -2,8 +2,30 @@
  * Window messaging support
  *
  * Copyright 2001 Alexandre Julliard
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
+#include "config.h"
+#include "wine/port.h"
+
+#include <assert.h>
+#include <stdarg.h>
+
+#include "ntstatus.h"
+#include "windef.h"
 #include "winbase.h"
 #include "wingdi.h"
 #include "winuser.h"
 #include "dde.h"
 #include "wine/unicode.h"
 #include "wine/server.h"
-#include "queue.h"
-#include "input.h"
 #include "message.h"
-#include "hook.h"
-#include "spy.h"
 #include "user.h"
 #include "win.h"
-#include "debugtools.h"
+#include "winproc.h"
+#include "wine/debug.h"
 
-DEFAULT_DEBUG_CHANNEL(msg);
+WINE_DEFAULT_DEBUG_CHANNEL(msg);
+WINE_DECLARE_DEBUG_CHANNEL(relay);
 
 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
 #define WM_NCMOUSELAST  WM_NCMBUTTONDBLCLK
@@ -173,7 +193,7 @@ static const unsigned int message_unicode_flags[] =
     /* 0x260 - 0x27f */
     0,
     /* 0x280 - 0x29f */
-    0,
+    SET(WM_IME_CHAR),
     /* 0x2a0 - 0x2bf */
     0,
     /* 0x2c0 - 0x2df */
@@ -200,26 +220,6 @@ inline static int is_unicode_message( UINT message )
 
 #undef SET
 
-/* compute the total size of the packed data */
-inline static size_t get_data_total_size( const struct packed_message *data )
-{
-    int i;
-    size_t total = 0;
-    for (i = 0; i < data->count; i++) total += data->size[i];
-    return total;
-}
-
-/* copy all the data of a packed message into a single dest buffer */
-inline static void copy_all_data( void *dest, const struct packed_message *data )
-{
-    int i;
-    for (i = 0; i < data->count; i++)
-    {
-        memcpy( dest, data->data[i], data->size[i] );
-        dest = (char *)dest + data->size[i];
-    }
-}
-
 /* add a data field to a packed message */
 inline static void push_data( struct packed_message *data, const void *ptr, size_t size )
 {
@@ -280,12 +280,6 @@ inline static BOOL listbox_has_strings( HWND hwnd )
     return (!(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)) || (style & LBS_HASSTRINGS));
 }
 
-/* check if hwnd is a broadcast magic handle */
-inline static BOOL is_broadcast( HWND hwnd )
-{
-    return (hwnd == HWND_BROADCAST || hwnd == HWND_TOPMOST);
-}
-
 
 /***********************************************************************
  *             broadcast_message_callback
@@ -331,18 +325,33 @@ static BOOL CALLBACK broadcast_message_callback( HWND hwnd, LPARAM lparam )
  */
 static WPARAM map_wparam_AtoW( UINT message, WPARAM wparam )
 {
-    if (message == WM_CHARTOITEM ||
-        message == EM_SETPASSWORDCHAR ||
-        message == WM_CHAR ||
-        message == WM_DEADCHAR ||
-        message == WM_SYSCHAR ||
-        message == WM_SYSDEADCHAR ||
-        message == WM_MENUCHAR)
+    switch(message)
     {
-        char ch = LOWORD(wparam);
-        WCHAR wch;
-        MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
-        wparam = MAKEWPARAM( wch, HIWORD(wparam) );
+    case WM_CHARTOITEM:
+    case EM_SETPASSWORDCHAR:
+    case WM_CHAR:
+    case WM_DEADCHAR:
+    case WM_SYSCHAR:
+    case WM_SYSDEADCHAR:
+    case WM_MENUCHAR:
+        {
+            char ch = LOWORD(wparam);
+            WCHAR wch;
+            MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
+            wparam = MAKEWPARAM( wch, HIWORD(wparam) );
+        }
+        break;
+    case WM_IME_CHAR:
+        {
+            char ch[2];
+            WCHAR wch;
+            ch[0] = (wparam >> 8);
+            ch[1] = (wparam & 0xff);
+            if (ch[0]) MultiByteToWideChar(CP_ACP, 0, ch, 2, &wch, 1);
+            else MultiByteToWideChar(CP_ACP, 0, &ch[1], 1, &wch, 1);
+            wparam = MAKEWPARAM( wch, HIWORD(wparam) );
+        }
+        break;
     }
     return wparam;
 }
@@ -355,18 +364,33 @@ static WPARAM map_wparam_AtoW( UINT message, WPARAM wparam )
  */
 static WPARAM map_wparam_WtoA( UINT message, WPARAM wparam )
 {
-    if (message == WM_CHARTOITEM ||
-        message == EM_SETPASSWORDCHAR ||
-        message == WM_CHAR ||
-        message == WM_DEADCHAR ||
-        message == WM_SYSCHAR ||
-        message == WM_SYSDEADCHAR ||
-        message == WM_MENUCHAR)
+    switch(message)
     {
-        WCHAR wch = LOWORD(wparam);
-        char ch;
-        WideCharToMultiByte( CP_ACP, 0, &wch, 1, &ch, 1, NULL, NULL );
-        wparam = MAKEWPARAM( ch, HIWORD(wparam) );
+    case WM_CHARTOITEM:
+    case EM_SETPASSWORDCHAR:
+    case WM_CHAR:
+    case WM_DEADCHAR:
+    case WM_SYSCHAR:
+    case WM_SYSDEADCHAR:
+    case WM_MENUCHAR:
+        {
+            WCHAR wch = LOWORD(wparam);
+            BYTE ch;
+            WideCharToMultiByte( CP_ACP, 0, &wch, 1, &ch, 1, NULL, NULL );
+            wparam = MAKEWPARAM( ch, HIWORD(wparam) );
+        }
+        break;
+    case WM_IME_CHAR:
+        {
+            WCHAR wch = LOWORD(wparam);
+            BYTE ch[2];
+
+            if (WideCharToMultiByte( CP_ACP, 0, &wch, 1, ch, 2, NULL, NULL ) == 2)
+                wparam = MAKEWPARAM( (ch[0] << 8) | ch[1], HIWORD(wparam) );
+            else
+                wparam = MAKEWPARAM( ch[0], HIWORD(wparam) );
+        }
+        break;
     }
     return wparam;
 }
@@ -397,18 +421,14 @@ static size_t pack_message( HWND hwnd, UINT message, WPARAM wparam, LPARAM lpara
     case WM_GETTEXT:
     case WM_ASKCBFORMATNAME:
         return wparam * sizeof(WCHAR);
-    case WM_SETTEXT:
     case WM_WININICHANGE:
+        if (lparam) push_string(data, (LPWSTR)lparam );
+        return 0;
+    case WM_SETTEXT:
     case WM_DEVMODECHANGE:
     case CB_DIR:
-    case CB_FINDSTRING:
-    case CB_FINDSTRINGEXACT:
-    case CB_SELECTSTRING:
     case LB_DIR:
     case LB_ADDFILE:
-    case LB_FINDSTRING:
-    case LB_FINDSTRINGEXACT:
-    case LB_SELECTSTRING:
     case EM_REPLACESEL:
         push_string( data, (LPWSTR)lparam );
         return 0;
@@ -500,6 +520,9 @@ static size_t pack_message( HWND hwnd, UINT message, WPARAM wparam, LPARAM lpara
         return 0;
     case CB_ADDSTRING:
     case CB_INSERTSTRING:
+    case CB_FINDSTRING:
+    case CB_FINDSTRINGEXACT:
+    case CB_SELECTSTRING:
         if (combobox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
         return 0;
     case CB_GETLBTEXT:
@@ -507,6 +530,9 @@ static size_t pack_message( HWND hwnd, UINT message, WPARAM wparam, LPARAM lpara
         return (SendMessageW( hwnd, CB_GETLBTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
     case LB_ADDSTRING:
     case LB_INSERTSTRING:
+    case LB_FINDSTRING:
+    case LB_FINDSTRINGEXACT:
+    case LB_SELECTSTRING:
         if (listbox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
         return 0;
     case LB_GETTEXT:
@@ -532,6 +558,15 @@ static size_t pack_message( HWND hwnd, UINT message, WPARAM wparam, LPARAM lpara
     case WM_MDIGETACTIVE:
         if (lparam) return sizeof(BOOL);
         return 0;
+    case WM_WINE_SETWINDOWPOS:
+        push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
+        return 0;
+    case WM_WINE_KEYBOARD_LL_HOOK:
+        push_data( data, (KBDLLHOOKSTRUCT *)lparam, sizeof(KBDLLHOOKSTRUCT) );
+        return 0;
+    case WM_WINE_MOUSE_LL_HOOK:
+        push_data( data, (MSLLHOOKSTRUCT *)lparam, sizeof(MSLLHOOKSTRUCT) );
+        return 0;
 
     /* these contain an HFONT */
     case WM_SETFONT:
@@ -553,13 +588,11 @@ static size_t pack_message( HWND hwnd, UINT message, WPARAM wparam, LPARAM lpara
     /* these contain an HGLOBAL */
     case WM_PAINTCLIPBOARD:
     case WM_SIZECLIPBOARD:
-    case WM_DDE_INITIATE:
-    case WM_DDE_ADVISE:
-    case WM_DDE_UNADVISE:
-    case WM_DDE_DATA:
-    case WM_DDE_REQUEST:
-    case WM_DDE_POKE:
-    case WM_DDE_EXECUTE:
+    /* these contain HICON */
+    case WM_GETICON:
+    case WM_SETICON:
+    case WM_QUERYDRAGICON:
+    case WM_QUERYPARKICON:
     /* these contain pointers */
     case WM_DROPOBJECT:
     case WM_QUERYDROPOBJECT:
@@ -612,18 +645,14 @@ static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lpa
     case WM_ASKCBFORMATNAME:
         if (!get_buffer_space( buffer, (*wparam * sizeof(WCHAR)) )) return FALSE;
         break;
-    case WM_SETTEXT:
     case WM_WININICHANGE:
+        if (!*lparam) return TRUE;
+        /* fall through */
+    case WM_SETTEXT:
     case WM_DEVMODECHANGE:
     case CB_DIR:
-    case CB_FINDSTRING:
-    case CB_FINDSTRINGEXACT:
-    case CB_SELECTSTRING:
     case LB_DIR:
     case LB_ADDFILE:
-    case LB_FINDSTRING:
-    case LB_FINDSTRINGEXACT:
-    case LB_SELECTSTRING:
     case EM_REPLACESEL:
         if (!check_string( *buffer, size )) return FALSE;
         break;
@@ -644,6 +673,7 @@ static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lpa
         break;
     case WM_WINDOWPOSCHANGING:
     case WM_WINDOWPOSCHANGED:
+    case WM_WINE_SETWINDOWPOS:
         minsize = sizeof(WINDOWPOS);
         break;
     case WM_COPYDATA:
@@ -721,8 +751,14 @@ static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lpa
         break;
     case CB_ADDSTRING:
     case CB_INSERTSTRING:
+    case CB_FINDSTRING:
+    case CB_FINDSTRINGEXACT:
+    case CB_SELECTSTRING:
     case LB_ADDSTRING:
     case LB_INSERTSTRING:
+    case LB_FINDSTRING:
+    case LB_FINDSTRINGEXACT:
+    case LB_SELECTSTRING:
         if (!*buffer) return TRUE;
         if (!check_string( *buffer, size )) return FALSE;
         break;
@@ -778,6 +814,12 @@ static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lpa
         if (!*lparam) return TRUE;
         if (!get_buffer_space( buffer, sizeof(BOOL) )) return FALSE;
         break;
+    case WM_WINE_KEYBOARD_LL_HOOK:
+        minsize = sizeof(KBDLLHOOKSTRUCT);
+        break;
+    case WM_WINE_MOUSE_LL_HOOK:
+        minsize = sizeof(MSLLHOOKSTRUCT);
+        break;
 
     /* these contain an HFONT */
     case WM_SETFONT:
@@ -799,13 +841,11 @@ static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lpa
     /* these contain an HGLOBAL */
     case WM_PAINTCLIPBOARD:
     case WM_SIZECLIPBOARD:
-    case WM_DDE_INITIATE:
-    case WM_DDE_ADVISE:
-    case WM_DDE_UNADVISE:
-    case WM_DDE_DATA:
-    case WM_DDE_REQUEST:
-    case WM_DDE_POKE:
-    case WM_DDE_EXECUTE:
+    /* these contain HICON */
+    case WM_GETICON:
+    case WM_SETICON:
+    case WM_QUERYDRAGICON:
+    case WM_QUERYPARKICON:
     /* these contain pointers */
     case WM_DROPOBJECT:
     case WM_QUERYDROPOBJECT:
@@ -959,7 +999,7 @@ static void unpack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
         memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
         break;
     case EM_GETLINE:
-        size = min( size, *(WORD *)lparam );
+        size = min( size, (size_t)*(WORD *)lparam );
         memcpy( (WCHAR *)lparam, buffer, size );
         break;
     case LB_GETSELITEMS:
@@ -1027,7 +1067,7 @@ static void unpack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
 static void reply_message( struct received_message_info *info, LRESULT result, BOOL remove )
 {
     struct packed_message data;
-    int replied = info->flags & ISMEX_REPLIED;
+    int i, replied = info->flags & ISMEX_REPLIED;
 
     if (info->flags & ISMEX_NOTIFY) return;  /* notify messages don't get replies */
     if (!remove && replied) return;  /* replied already */
@@ -1039,92 +1079,442 @@ static void reply_message( struct received_message_info *info, LRESULT result, B
     {
         pack_reply( info->msg.hwnd, info->msg.message, info->msg.wParam,
                     info->msg.lParam, result, &data );
-        if (data.count)
+    }
+
+    SERVER_START_REQ( reply_message )
+    {
+        req->type   = info->type;
+        req->result = result;
+        req->remove = remove;
+        for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
+        wine_server_call( req );
+    }
+    SERVER_END_REQ;
+}
+
+
+/***********************************************************************
+ *           handle_internal_message
+ *
+ * Handle an internal Wine message instead of calling the window proc.
+ */
+static LRESULT handle_internal_message( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
+{
+    if (hwnd == GetDesktopWindow()) return 0;
+    switch(msg)
+    {
+    case WM_WINE_DESTROYWINDOW:
+        return WIN_DestroyWindow( hwnd );
+    case WM_WINE_SETWINDOWPOS:
+        return USER_Driver.pSetWindowPos( (WINDOWPOS *)lparam );
+    case WM_WINE_SHOWWINDOW:
+        return ShowWindow( hwnd, wparam );
+    case WM_WINE_SETPARENT:
+        return (LRESULT)SetParent( hwnd, (HWND)wparam );
+    case WM_WINE_SETWINDOWLONG:
+        return (LRESULT)SetWindowLongW( hwnd, wparam, lparam );
+    case WM_WINE_ENABLEWINDOW:
+        return EnableWindow( hwnd, wparam );
+    case WM_WINE_SETACTIVEWINDOW:
+        return (LRESULT)SetActiveWindow( (HWND)wparam );
+    case WM_WINE_KEYBOARD_LL_HOOK:
+        return HOOK_CallHooks( WH_KEYBOARD_LL, HC_ACTION, wparam, lparam, TRUE );
+    case WM_WINE_MOUSE_LL_HOOK:
+        return HOOK_CallHooks( WH_MOUSE_LL, HC_ACTION, wparam, lparam, TRUE );
+    default:
+        FIXME( "unknown internal message %x\n", msg );
+        return 0;
+    }
+}
+
+/* since the WM_DDE_ACK response to a WM_DDE_EXECUTE message should contain the handle
+ * to the memory handle, we keep track (in the server side) of all pairs of handle
+ * used (the client passes its value and the content of the memory handle), and
+ * the server stored both values (the client, and the local one, created after the
+ * content). When a ACK message is generated, the list of pair is searched for a
+ * matching pair, so that the client memory handle can be returned.
+ */
+struct DDE_pair {
+    HGLOBAL     client_hMem;
+    HGLOBAL     server_hMem;
+};
+
+static      struct DDE_pair*    dde_pairs;
+static      int                 dde_num_alloc;
+static      int                 dde_num_used;
+
+static CRITICAL_SECTION dde_crst;
+static CRITICAL_SECTION_DEBUG critsect_debug =
+{
+    0, 0, &dde_crst,
+    { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
+      0, 0, { 0, (DWORD)(__FILE__ ": dde_crst") }
+};
+static CRITICAL_SECTION dde_crst = { &critsect_debug, -1, 0, 0, 0, 0 };
+
+static BOOL dde_add_pair(HGLOBAL chm, HGLOBAL shm)
+{
+    int  i;
+#define GROWBY  4
+
+    EnterCriticalSection(&dde_crst);
+
+    /* now remember the pair of hMem on both sides */
+    if (dde_num_used == dde_num_alloc)
+    {
+        struct DDE_pair* tmp;
+       if (dde_pairs)
+           tmp  = HeapReAlloc( GetProcessHeap(), 0, dde_pairs,
+                                            (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
+       else
+           tmp  = HeapAlloc( GetProcessHeap(), 0, 
+                                            (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
+
+        if (!tmp)
+        {
+            LeaveCriticalSection(&dde_crst);
+            return FALSE;
+        }
+        dde_pairs = tmp;
+        /* zero out newly allocated part */
+        memset(&dde_pairs[dde_num_alloc], 0, GROWBY * sizeof(struct DDE_pair));
+        dde_num_alloc += GROWBY;
+    }
+#undef GROWBY
+    for (i = 0; i < dde_num_alloc; i++)
+    {
+        if (dde_pairs[i].server_hMem == 0)
+        {
+            dde_pairs[i].client_hMem = chm;
+            dde_pairs[i].server_hMem = shm;
+            dde_num_used++;
+            break;
+        }
+    }
+    LeaveCriticalSection(&dde_crst);
+    return TRUE;
+}
+
+static HGLOBAL dde_get_pair(HGLOBAL shm)
+{
+    int  i;
+    HGLOBAL     ret = 0;
+
+    EnterCriticalSection(&dde_crst);
+    for (i = 0; i < dde_num_alloc; i++)
+    {
+        if (dde_pairs[i].server_hMem == shm)
         {
-            size_t total = get_data_total_size( &data );
+            /* free this pair */
+            dde_pairs[i].server_hMem = 0;
+            dde_num_used--;
+            ret = dde_pairs[i].client_hMem;
+            break;
+        }
+    }
+    LeaveCriticalSection(&dde_crst);
+    return ret;
+}
 
-            if (total > REQUEST_MAX_VAR_SIZE)
+/***********************************************************************
+ *             post_dde_message
+ *
+ * Post a DDE message
+ */
+static BOOL post_dde_message( DWORD dest_tid, struct packed_message *data, const struct send_message_info *info )
+{
+    void*       ptr = NULL;
+    int         size = 0;
+    UINT        uiLo, uiHi;
+    LPARAM      lp = 0;
+    HGLOBAL     hunlock = 0;
+    int         i;
+    DWORD       res;
+
+    if (!UnpackDDElParam( info->msg, info->lparam, &uiLo, &uiHi ))
+        return FALSE;
+
+    lp = info->lparam;
+    switch (info->msg)
+    {
+        /* DDE messages which don't require packing are:
+         * WM_DDE_INITIATE
+         * WM_DDE_TERMINATE
+         * WM_DDE_REQUEST
+         * WM_DDE_UNADVISE
+         */
+    case WM_DDE_ACK:
+        if (HIWORD(uiHi))
+        {
+            /* uiHi should contain a hMem from WM_DDE_EXECUTE */
+            HGLOBAL h = dde_get_pair( (HANDLE)uiHi );
+            if (h)
             {
-                FIXME( "inter-process msg data size %d not supported yet, expect trouble\n",
-                       total );
-                total = REQUEST_MAX_VAR_SIZE;
+                /* send back the value of h on the other side */
+                push_data( data, &h, sizeof(HGLOBAL) );
+                lp = uiLo;
+                TRACE( "send dde-ack %x %08x => %08lx\n", uiLo, uiHi, (DWORD)h );
             }
+        }
+        else
+        {
+            /* uiHi should contain either an atom or 0 */
+            TRACE( "send dde-ack %x atom=%x\n", uiLo, uiHi );
+            lp = MAKELONG( uiLo, uiHi );
+        }
+        break;
+    case WM_DDE_ADVISE:
+    case WM_DDE_DATA:
+    case WM_DDE_POKE:
+        size = 0;
+        if (uiLo)
+        {
+            size = GlobalSize( (HGLOBAL)uiLo ) ;
+            if ((info->msg == WM_DDE_ADVISE && size < sizeof(DDEADVISE)) ||
+                (info->msg == WM_DDE_DATA   && size < sizeof(DDEDATA))   ||
+                (info->msg == WM_DDE_POKE   && size < sizeof(DDEPOKE))
+                )
+            return FALSE;
+        }
+        else if (info->msg != WM_DDE_DATA) return FALSE;
 
-            SERVER_START_VAR_REQ( reply_message, total )
+        lp = uiHi;
+        if (uiLo)
+        {
+            if ((ptr = GlobalLock( (HGLOBAL)uiLo) ))
             {
-                req->result = result;
-                req->remove = remove;
-                copy_all_data( server_data_ptr(req), &data );
-                SERVER_CALL();
+                DDEDATA *dde_data = (DDEDATA *)ptr;
+                TRACE("unused %d, fResponse %d, fRelease %d, fDeferUpd %d, fAckReq %d, cfFormat %d\n",
+                       dde_data->unused, dde_data->fResponse, dde_data->fRelease,
+                       dde_data->reserved, dde_data->fAckReq, dde_data->cfFormat);
+                push_data( data, ptr, size );
+                hunlock = (HGLOBAL)uiLo;
             }
-            SERVER_END_VAR_REQ;
-            return;
         }
+        TRACE( "send ddepack %u %x\n", size, uiHi );
+        break;
+    case WM_DDE_EXECUTE:
+        if (info->lparam)
+        {
+            if ((ptr = GlobalLock( (HGLOBAL)info->lparam) ))
+            {
+                push_data(data, ptr, GlobalSize( (HGLOBAL)info->lparam ));
+                /* so that the other side can send it back on ACK */
+                lp = info->lparam;
+                hunlock = (HGLOBAL)info->lparam;
+            }
+        }
+        break;
     }
-
-    SERVER_START_REQ( reply_message )
+    SERVER_START_REQ( send_message )
     {
-        req->result = result;
-        req->remove = remove;
-        SERVER_CALL();
+        req->id      = dest_tid;
+        req->type    = info->type;
+        req->flags   = 0;
+        req->win     = info->hwnd;
+        req->msg     = info->msg;
+        req->wparam  = info->wparam;
+        req->lparam  = lp;
+        req->time    = GetCurrentTime();
+        req->timeout = -1;
+        for (i = 0; i < data->count; i++)
+            wine_server_add_data( req, data->data[i], data->size[i] );
+        if ((res = wine_server_call( req )))
+        {
+            if (res == STATUS_INVALID_PARAMETER)
+                /* FIXME: find a STATUS_ value for this one */
+                SetLastError( ERROR_INVALID_THREAD_ID );
+            else
+                SetLastError( RtlNtStatusToDosError(res) );
+        }
+        else
+            FreeDDElParam(info->msg, info->lparam);
     }
     SERVER_END_REQ;
+    if (hunlock) GlobalUnlock(hunlock);
+
+    return !res;
 }
 
+/***********************************************************************
+ *             unpack_dde_message
+ *
+ * Unpack a posted DDE message received from another process.
+ */
+static BOOL unpack_dde_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
+                                void **buffer, size_t size )
+{
+    UINT       uiLo, uiHi;
+    HGLOBAL    hMem = 0;
+    void*      ptr;
+
+    switch (message)
+    {
+    case WM_DDE_ACK:
+        if (size)
+        {
+            /* hMem is being passed */
+            if (size != sizeof(HGLOBAL)) return FALSE;
+            if (!buffer || !*buffer) return FALSE;
+            uiLo = *lparam;
+            memcpy( &hMem, *buffer, size );
+            uiHi = (UINT)hMem;
+            TRACE("recv dde-ack %x mem=%x[%lx]\n", uiLo, uiHi, GlobalSize( hMem ));
+        }
+        else
+        {
+            uiLo = LOWORD( *lparam );
+            uiHi = HIWORD( *lparam );
+            TRACE("recv dde-ack %x atom=%x\n", uiLo, uiHi);
+        }
+       *lparam = PackDDElParam( WM_DDE_ACK, uiLo, uiHi );
+       break;
+    case WM_DDE_ADVISE:
+    case WM_DDE_DATA:
+    case WM_DDE_POKE:
+       if ((!buffer || !*buffer) && message != WM_DDE_DATA) return FALSE;
+       uiHi = *lparam;
+       TRACE( "recv ddepack %u %x\n", size, uiHi );
+        if (size)
+        {
+            hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size );
+            if (hMem && (ptr = GlobalLock( hMem )))
+            {
+                memcpy( ptr, *buffer, size );
+                GlobalUnlock( hMem );
+            }
+            else return FALSE;
+        }
+        uiLo = (UINT)hMem;
+
+       *lparam = PackDDElParam( message, uiLo, uiHi );
+       break;
+    case WM_DDE_EXECUTE:
+       if (size)
+       {
+           if (!buffer || !*buffer) return FALSE;
+           hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size );
+           if (hMem && (ptr = GlobalLock( hMem )))
+           {
+               memcpy( ptr, *buffer, size );
+               GlobalUnlock( hMem );
+                TRACE( "exec: pairing c=%08lx s=%08lx\n", *lparam, (DWORD)hMem );
+                if (!dde_add_pair( (HGLOBAL)*lparam, hMem ))
+                {
+                    GlobalFree( hMem );
+                    return FALSE;
+                }
+           }
+       } else return FALSE;
+        *lparam = (LPARAM)hMem;
+        break;
+    }
+    return TRUE;
+}
 
 /***********************************************************************
  *           call_window_proc
  *
  * Call a window procedure and the corresponding hooks.
  */
-static LRESULT call_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam, BOOL unicode )
+static LRESULT call_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
+                                 BOOL unicode, BOOL same_thread )
 {
-    LRESULT result;
+    LRESULT result = 0;
     WNDPROC winproc;
+    CWPSTRUCT cwp;
+    CWPRETSTRUCT cwpret;
+    MESSAGEQUEUE *queue = QUEUE_Current();
 
-    /* FIXME: should check for exiting queue */
+    if (queue->recursion_count > MAX_SENDMSG_RECURSION) return 0;
+    queue->recursion_count++;
 
-    /* first the WH_CALLWNDPROC hook */
-    if (HOOK_IsHooked( WH_CALLWNDPROC ))
+    if (msg & 0x80000000)
     {
-        CWPSTRUCT cwp;
-        cwp.lParam  = lparam;
-        cwp.wParam  = wparam;
-        cwp.message = msg;
-        cwp.hwnd    = WIN_GetFullHandle( hwnd );
-        if (unicode) HOOK_CallHooksW( WH_CALLWNDPROC, HC_ACTION, 1, (LPARAM)&cwp );
-        else HOOK_CallHooksA( WH_CALLWNDPROC, HC_ACTION, 1, (LPARAM)&cwp );
-        lparam = cwp.lParam;
-        wparam = cwp.wParam;
-        msg    = cwp.message;
-        hwnd   = cwp.hwnd;
+        result = handle_internal_message( hwnd, msg, wparam, lparam );
+        goto done;
     }
 
+    /* first the WH_CALLWNDPROC hook */
+    hwnd = WIN_GetFullHandle( hwnd );
+    cwp.lParam  = lparam;
+    cwp.wParam  = wparam;
+    cwp.message = msg;
+    cwp.hwnd    = hwnd;
+    HOOK_CallHooks( WH_CALLWNDPROC, HC_ACTION, same_thread, (LPARAM)&cwp, unicode );
+
     /* now call the window procedure */
     if (unicode)
     {
-        if (!(winproc = (WNDPROC)GetWindowLongW( hwnd, GWL_WNDPROC ))) return 0;
+        if (!(winproc = (WNDPROC)GetWindowLongW( hwnd, GWL_WNDPROC ))) goto done;
         result = CallWindowProcW( winproc, hwnd, msg, wparam, lparam );
     }
     else
     {
-        if (!(winproc = (WNDPROC)GetWindowLongA( hwnd, GWL_WNDPROC ))) return 0;
+        if (!(winproc = (WNDPROC)GetWindowLongA( hwnd, GWL_WNDPROC ))) goto done;
         result = CallWindowProcA( winproc, hwnd, msg, wparam, lparam );
     }
 
     /* and finally the WH_CALLWNDPROCRET hook */
-    if (HOOK_IsHooked( WH_CALLWNDPROCRET ))
+    cwpret.lResult = result;
+    cwpret.lParam  = lparam;
+    cwpret.wParam  = wparam;
+    cwpret.message = msg;
+    cwpret.hwnd    = hwnd;
+    HOOK_CallHooks( WH_CALLWNDPROCRET, HC_ACTION, same_thread, (LPARAM)&cwpret, unicode );
+ done:
+    queue->recursion_count--;
+    return result;
+}
+
+
+/***********************************************************************
+ *           process_hardware_message
+ *
+ * Process a hardware message; return TRUE if message should be passed on to the app
+ */
+static BOOL process_hardware_message( MSG *msg, ULONG_PTR extra_info, HWND hwnd,
+                                      UINT first, UINT last, BOOL remove )
+{
+    BOOL ret;
+
+    if (!MSG_process_raw_hardware_message( msg, extra_info, hwnd, first, last, remove ))
+        return FALSE;
+
+    ret = MSG_process_cooked_hardware_message( msg, extra_info, remove );
+
+    /* tell the server we have passed it to the app
+     * (even though we may end up dropping it later on)
+     */
+    SERVER_START_REQ( reply_message )
     {
-        CWPRETSTRUCT cwp;
-        cwp.lResult = result;
-        cwp.lParam  = lparam;
-        cwp.wParam  = wparam;
-        cwp.message = msg;
-        cwp.hwnd    = WIN_GetFullHandle( hwnd );
-        if (unicode) HOOK_CallHooksW( WH_CALLWNDPROCRET, HC_ACTION, 1, (LPARAM)&cwp );
-        else HOOK_CallHooksA( WH_CALLWNDPROCRET, HC_ACTION, 1, (LPARAM)&cwp );
+        req->type   = MSG_HARDWARE;
+        req->result = 0;
+        req->remove = remove || !ret;
+        wine_server_call( req );
     }
-    return result;
+    SERVER_END_REQ;
+    return ret;
+}
+
+
+/***********************************************************************
+ *           call_sendmsg_callback
+ *
+ * Call the callback function of SendMessageCallback.
+ */
+static inline void call_sendmsg_callback( SENDASYNCPROC callback, HWND hwnd, UINT msg,
+                                          ULONG_PTR data, LRESULT result )
+{
+    if (TRACE_ON(relay))
+        DPRINTF( "%04lx:Call message callback %p (hwnd=%p,msg=%s,data=%08lx,result=%08lx)\n",
+                 GetCurrentThreadId(), callback, hwnd, SPY_GetMsgName( msg, hwnd ),
+                 data, result );
+    callback( hwnd, msg, data, result );
+    if (TRACE_ON(relay))
+        DPRINTF( "%04lx:Ret  message callback %p (hwnd=%p,msg=%s,data=%08lx,result=%08lx)\n",
+                 GetCurrentThreadId(), callback, hwnd, SPY_GetMsgName( msg, hwnd ),
+                 data, result );
 }
 
 
@@ -1136,7 +1526,6 @@ static LRESULT call_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lpar
  */
 BOOL MSG_peek_message( MSG *msg, HWND hwnd, UINT first, UINT last, int flags )
 {
-    BOOL ret;
     LRESULT result;
     ULONG_PTR extra_info = 0;
     MESSAGEQUEUE *queue = QUEUE_Current();
@@ -1146,44 +1535,48 @@ BOOL MSG_peek_message( MSG *msg, HWND hwnd, UINT first, UINT last, int flags )
 
     for (;;)
     {
+        NTSTATUS res;
         void *buffer = NULL;
-        size_t size = 0;
+        size_t size = 0, buffer_size = 0;
 
-        SERVER_START_VAR_REQ( get_message, REQUEST_MAX_VAR_SIZE )
+        do  /* loop while buffer is too small */
         {
-            req->flags     = flags;
-            req->get_win   = hwnd;
-            req->get_first = first;
-            req->get_last  = last;
-            if ((ret = !SERVER_CALL()))
+            if (buffer_size && !(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size )))
+                return FALSE;
+            SERVER_START_REQ( get_message )
             {
-                info.type        = req->type;
-                info.msg.hwnd    = req->win;
-                info.msg.message = req->msg;
-                info.msg.wParam  = req->wparam;
-                info.msg.lParam  = req->lparam;
-                info.msg.time    = req->time;
-                info.msg.pt.x    = req->x;
-                info.msg.pt.y    = req->y;
-                extra_info       = req->info;
-
-                if ((size = server_data_size(req)))
+                req->flags     = flags;
+                req->get_win   = hwnd;
+                req->get_first = first;
+                req->get_last  = last;
+                if (buffer_size) wine_server_set_reply( req, buffer, buffer_size );
+                if (!(res = wine_server_call( req )))
                 {
-                    if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size )))
-                    {
-                        ERR("out of memory for message data\n");
-                        ret = FALSE;
-                    }
-                    else memcpy( buffer, server_data_ptr(req), size );
+                    size = wine_server_reply_size( reply );
+                    info.type        = reply->type;
+                    info.msg.hwnd    = reply->win;
+                    info.msg.message = reply->msg;
+                    info.msg.wParam  = reply->wparam;
+                    info.msg.lParam  = reply->lparam;
+                    info.msg.time    = reply->time;
+                    info.msg.pt.x    = reply->x;
+                    info.msg.pt.y    = reply->y;
+                    extra_info       = reply->info;
+                }
+                else
+                {
+                    if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
+                    buffer_size = reply->total;
                 }
             }
-        }
-        SERVER_END_VAR_REQ;
+            SERVER_END_REQ;
+        } while (res == STATUS_BUFFER_OVERFLOW);
 
-        if (!ret) return FALSE;  /* no message available */
+        if (res) return FALSE;
 
-        TRACE( "got type %d msg %x hwnd %x wp %x lp %lx\n",
-               info.type, info.msg.message, info.msg.hwnd, info.msg.wParam, info.msg.lParam );
+        TRACE( "got type %d msg %x (%s) hwnd %p wp %x lp %lx\n",
+               info.type, info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd),
+               info.msg.hwnd, info.msg.wParam, info.msg.lParam );
 
         switch(info.type)
         {
@@ -1197,50 +1590,60 @@ BOOL MSG_peek_message( MSG *msg, HWND hwnd, UINT first, UINT last, int flags )
         case MSG_CALLBACK:
             info.flags = ISMEX_CALLBACK;
             break;
+        case MSG_CALLBACK_RESULT:
+            call_sendmsg_callback( (SENDASYNCPROC)info.msg.wParam, info.msg.hwnd,
+                                   info.msg.message, extra_info, info.msg.lParam );
+            goto next;
         case MSG_OTHER_PROCESS:
             info.flags = ISMEX_SEND;
             if (!unpack_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
                                  &info.msg.lParam, &buffer, size ))
             {
-                ERR( "invalid packed message %x (%s) hwnd %x wp %x lp %lx size %d\n",
+                ERR( "invalid packed message %x (%s) hwnd %p wp %x lp %lx size %d\n",
                      info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd), info.msg.hwnd,
                      info.msg.wParam, info.msg.lParam, size );
                 /* ignore it */
                 reply_message( &info, 0, TRUE );
-                continue;
+                goto next;
             }
             break;
-        case MSG_POSTED:
-            goto got_one;
-        case MSG_HARDWARE_RAW:
-            if (!MSG_process_raw_hardware_message( &info.msg, extra_info,
-                                                   hwnd, first, last, flags & GET_MSG_REMOVE ))
-                continue;
-            /* fall through */
-        case MSG_HARDWARE_COOKED:
-            if (!MSG_process_cooked_hardware_message( &info.msg, flags & GET_MSG_REMOVE ))
+        case MSG_HARDWARE:
+            if (!process_hardware_message( &info.msg, extra_info,
+                                           hwnd, first, last, flags & GET_MSG_REMOVE ))
             {
-                flags |= GET_MSG_REMOVE_LAST;
-                continue;
+                TRACE("dropping msg %x\n", info.msg.message );
+                goto next;  /* ignore it */
             }
+            queue->GetMessagePosVal = MAKELONG( info.msg.pt.x, info.msg.pt.y );
+            /* fall through */
+        case MSG_POSTED:
             queue->GetMessageExtraInfoVal = extra_info;
-            goto got_one;
+           if (info.msg.message >= WM_DDE_FIRST && info.msg.message <= WM_DDE_LAST)
+           {
+               if (!unpack_dde_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
+                                         &info.msg.lParam, &buffer, size ))
+               {
+                   ERR( "invalid packed dde-message %x (%s) hwnd %p wp %x lp %lx size %d\n",
+                        info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd),
+                        info.msg.hwnd, info.msg.wParam, info.msg.lParam, size );
+                    goto next;  /* ignore it */
+               }
+           }
+            *msg = info.msg;
+            if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
+            return TRUE;
         }
 
         /* if we get here, we have a sent message; call the window procedure */
         old_info = queue->receive_info;
         queue->receive_info = &info;
         result = call_window_proc( info.msg.hwnd, info.msg.message, info.msg.wParam,
-                                   info.msg.lParam, (info.type != MSG_ASCII) );
+                                   info.msg.lParam, (info.type != MSG_ASCII), FALSE );
         reply_message( &info, result, TRUE );
         queue->receive_info = old_info;
-
+    next:
         if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
     }
-
- got_one:
-    *msg = info.msg;
-    return TRUE;
 }
 
 
@@ -1262,18 +1665,18 @@ static void wait_message_reply( UINT flags )
 
         SERVER_START_REQ( set_queue_mask )
         {
-            req->wake_mask    = (flags & SMTO_BLOCK) ? 0 : QS_SENDMESSAGE;
-            req->changed_mask = QS_SMRESULT | req->wake_mask;
+            req->wake_mask    = QS_SMRESULT | ((flags & SMTO_BLOCK) ? 0 : QS_SENDMESSAGE);
+            req->changed_mask = req->wake_mask;
             req->skip_wait    = 1;
-            if (!SERVER_CALL())
+            if (!wine_server_call( req ))
             {
-                wake_bits    = req->wake_bits;
-                changed_bits = req->changed_bits;
+                wake_bits    = reply->wake_bits;
+                changed_bits = reply->changed_bits;
             }
         }
         SERVER_END_REQ;
 
-        if (changed_bits & QS_SMRESULT) return;  /* got a result */
+        if (wake_bits & QS_SMRESULT) return;  /* got a result */
         if (wake_bits & QS_SENDMESSAGE)
         {
             /* Process the sent message immediately */
@@ -1305,10 +1708,9 @@ static void wait_message_reply( UINT flags )
 static BOOL put_message_in_queue( DWORD dest_tid, const struct send_message_info *info,
                                   size_t *reply_size )
 {
+    struct packed_message data;
     unsigned int res;
-    int timeout = -1;
-
-    /* FIXME: should check for exiting queue */
+    int i, timeout = -1;
 
     if (info->type != MSG_NOTIFY &&
         info->type != MSG_CALLBACK &&
@@ -1316,70 +1718,51 @@ static BOOL put_message_in_queue( DWORD dest_tid, const struct send_message_info
         info->timeout != INFINITE)
         timeout = info->timeout;
 
+    data.count = 0;
     if (info->type == MSG_OTHER_PROCESS)
     {
-        struct packed_message data;
         *reply_size = pack_message( info->hwnd, info->msg, info->wparam, info->lparam, &data );
-
         if (data.count == -1)
         {
             WARN( "cannot pack message %x\n", info->msg );
             return FALSE;
         }
-
-        if (data.size[0]) /* need to send extra data along with the message */
-        {
-            size_t total = get_data_total_size( &data );
-
-            if (total > REQUEST_MAX_VAR_SIZE)
-            {
-                FIXME( "inter-process msg data size %d not supported yet, expect trouble\n",
-                       total );
-                total = REQUEST_MAX_VAR_SIZE;
-            }
-
-            SERVER_START_VAR_REQ( send_message, total )
-            {
-                req->id      = (void *)dest_tid;
-                req->type    = MSG_OTHER_PROCESS;
-                req->win     = info->hwnd;
-                req->msg     = info->msg;
-                req->wparam  = info->wparam;
-                req->lparam  = info->lparam;
-                req->time    = GetCurrentTime();
-                req->timeout = timeout;
-                copy_all_data( server_data_ptr(req), &data );
-                res = SERVER_CALL();
-            }
-            SERVER_END_VAR_REQ;
-            goto done;
-        }
+    }
+    else if (info->type == MSG_POSTED && info->msg >= WM_DDE_FIRST && info->msg <= WM_DDE_LAST)
+    {
+        return post_dde_message( dest_tid, &data, info );
     }
 
-    /* no extra data, or not inter-process message */
     SERVER_START_REQ( send_message )
     {
-        req->id      = (void *)dest_tid;
+        req->id      = dest_tid;
         req->type    = info->type;
+        req->flags   = 0;
         req->win     = info->hwnd;
         req->msg     = info->msg;
         req->wparam  = info->wparam;
         req->lparam  = info->lparam;
         req->time    = GetCurrentTime();
         req->timeout = timeout;
-        res = SERVER_CALL();
-    }
-    SERVER_END_REQ;
 
- done:
-    if (res)
-    {
-        if (res == STATUS_INVALID_PARAMETER)
-            /* FIXME: find a STATUS_ value for this one */
-            SetLastError( ERROR_INVALID_THREAD_ID );
-        else
-            SetLastError( RtlNtStatusToDosError(res) );
+        if (info->type == MSG_CALLBACK)
+        {
+            req->callback = info->callback;
+            req->info     = info->data;
+        }
+
+        if (info->flags & SMTO_ABORTIFHUNG) req->flags |= SEND_MSG_ABORT_IF_HUNG;
+        for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
+        if ((res = wine_server_call( req )))
+        {
+            if (res == STATUS_INVALID_PARAMETER)
+                /* FIXME: find a STATUS_ value for this one */
+                SetLastError( ERROR_INVALID_THREAD_ID );
+            else
+                SetLastError( RtlNtStatusToDosError(res) );
+        }
     }
+    SERVER_END_REQ;
     return !res;
 }
 
@@ -1393,44 +1776,36 @@ static LRESULT retrieve_reply( const struct send_message_info *info,
                                size_t reply_size, LRESULT *result )
 {
     NTSTATUS status;
+    void *reply_data = NULL;
 
     if (reply_size)
     {
-        if (reply_size > REQUEST_MAX_VAR_SIZE)
+        if (!(reply_data = HeapAlloc( GetProcessHeap(), 0, reply_size )))
         {
-            WARN( "reply_size %d too large, reply may be truncated\n", reply_size );
-            reply_size = REQUEST_MAX_VAR_SIZE;
+            WARN( "no memory for reply %d bytes, will be truncated\n", reply_size );
+            reply_size = 0;
         }
-        SERVER_START_VAR_REQ( get_message_reply, reply_size )
-        {
-            req->cancel = 1;
-            if (!(status = SERVER_CALL()))
-            {
-                *result = req->result;
-                unpack_reply( info->hwnd, info->msg, info->wparam, info->lparam,
-                              server_data_ptr(req), server_data_size(req) );
-            }
-        }
-        SERVER_END_VAR_REQ;
     }
-    else
+    SERVER_START_REQ( get_message_reply )
     {
-        SERVER_START_REQ( get_message_reply )
-        {
-            req->cancel = 1;
-            if (!(status = SERVER_CALL())) *result = req->result;
-        }
-        SERVER_END_REQ;
+        req->cancel = 1;
+        if (reply_size) wine_server_set_reply( req, reply_data, reply_size );
+        if (!(status = wine_server_call( req ))) *result = reply->result;
+        reply_size = wine_server_reply_size( reply );
     }
+    SERVER_END_REQ;
+    if (!status && reply_size)
+        unpack_reply( info->hwnd, info->msg, info->wparam, info->lparam, reply_data, reply_size );
+
+    if (reply_data) HeapFree( GetProcessHeap(), 0, reply_data );
 
-    TRACE( "hwnd %x msg %x (%s) wp %x lp %lx got reply %lx (err=%ld)\n",
+    TRACE( "hwnd %p msg %x (%s) wp %x lp %lx got reply %lx (err=%ld)\n",
            info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam,
            info->lparam, *result, status );
 
-    if (!status) return 1;
-    if (status == STATUS_TIMEOUT) SetLastError(0);  /* timeout */
-    else SetLastError( RtlNtStatusToDosError(status) );
-    return 0;
+    /* MSDN states that last error is 0 on timeout, but at least NT4 returns ERROR_TIMEOUT */
+    if (status) SetLastError( RtlNtStatusToDosError(status) );
+    return !status;
 }
 
 
@@ -1444,7 +1819,7 @@ static LRESULT send_inter_thread_message( DWORD dest_tid, const struct send_mess
     int locks;
     size_t reply_size = 0;
 
-    TRACE( "hwnd %x msg %x (%s) wp %x lp %lx\n",
+    TRACE( "hwnd %p msg %x (%s) wp %x lp %lx\n",
            info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam, info->lparam );
 
     if (!put_message_in_queue( dest_tid, info, &reply_size )) return 0;
@@ -1462,11 +1837,51 @@ static LRESULT send_inter_thread_message( DWORD dest_tid, const struct send_mess
 }
 
 
+/***********************************************************************
+ *             MSG_SendInternalMessageTimeout
+ *
+ * Same as SendMessageTimeoutW but sends the message to a specific thread
+ * without requiring a window handle. Only works for internal Wine messages.
+ */
+LRESULT MSG_SendInternalMessageTimeout( DWORD dest_pid, DWORD dest_tid,
+                                        UINT msg, WPARAM wparam, LPARAM lparam,
+                                        UINT flags, UINT timeout, PDWORD_PTR res_ptr )
+{
+    struct send_message_info info;
+    LRESULT ret, result;
+
+    assert( msg & 0x80000000 );  /* must be an internal Wine message */
+
+    info.type    = MSG_UNICODE;
+    info.hwnd    = 0;
+    info.msg     = msg;
+    info.wparam  = wparam;
+    info.lparam  = lparam;
+    info.flags   = flags;
+    info.timeout = timeout;
+
+    if (USER_IsExitingThread( dest_tid )) return 0;
+
+    if (dest_tid == GetCurrentThreadId())
+    {
+        result = handle_internal_message( 0, msg, wparam, lparam );
+        ret = 1;
+    }
+    else
+    {
+        if (dest_pid != GetCurrentProcessId()) info.type = MSG_OTHER_PROCESS;
+        ret = send_inter_thread_message( dest_tid, &info, &result );
+    }
+    if (ret && res_ptr) *res_ptr = result;
+    return ret;
+}
+
+
 /***********************************************************************
  *             SendMessageTimeoutW  (USER32.@)
  */
 LRESULT WINAPI SendMessageTimeoutW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
-                                    UINT flags, UINT timeout, LPDWORD res_ptr )
+                                    UINT flags, UINT timeout, PDWORD_PTR res_ptr )
 {
     struct send_message_info info;
     DWORD dest_tid, dest_pid;
@@ -1487,13 +1902,15 @@ LRESULT WINAPI SendMessageTimeoutW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM l
         return 1;
     }
 
-    SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
+    if (!(dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid ))) return 0;
 
-    dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid );
+    if (USER_IsExitingThread( dest_tid )) return 0;
+
+    SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
 
     if (dest_tid == GetCurrentThreadId())
     {
-        result = call_window_proc( hwnd, msg, wparam, lparam, TRUE );
+        result = call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
         ret = 1;
     }
     else
@@ -1512,7 +1929,7 @@ LRESULT WINAPI SendMessageTimeoutW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM l
  *             SendMessageTimeoutA  (USER32.@)
  */
 LRESULT WINAPI SendMessageTimeoutA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
-                                    UINT flags, UINT timeout, LPDWORD res_ptr )
+                                    UINT flags, UINT timeout, PDWORD_PTR res_ptr )
 {
     struct send_message_info info;
     DWORD dest_tid, dest_pid;
@@ -1533,13 +1950,15 @@ LRESULT WINAPI SendMessageTimeoutA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM l
         return 1;
     }
 
-    SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
+    if (!(dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid ))) return 0;
 
-    dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid );
+    if (USER_IsExitingThread( dest_tid )) return 0;
+
+    SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
 
     if (dest_tid == GetCurrentThreadId())
     {
-        result = call_window_proc( hwnd, msg, wparam, lparam, FALSE );
+        result = call_window_proc( hwnd, msg, wparam, lparam, FALSE, TRUE );
         ret = 1;
     }
     else if (dest_pid == GetCurrentProcessId())
@@ -1617,6 +2036,7 @@ BOOL WINAPI SendNotifyMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lpara
     info.msg     = msg;
     info.wparam  = wparam;
     info.lparam  = lparam;
+    info.flags   = 0;
 
     if (is_broadcast(hwnd))
     {
@@ -1624,11 +2044,13 @@ BOOL WINAPI SendNotifyMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lpara
         return TRUE;
     }
 
-    dest_tid = GetWindowThreadProcessId( hwnd, NULL );
+    if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
+
+    if (USER_IsExitingThread( dest_tid )) return TRUE;
 
     if (dest_tid == GetCurrentThreadId())
     {
-        call_window_proc( hwnd, msg, wparam, lparam, TRUE );
+        call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
         return TRUE;
     }
     return send_inter_thread_message( dest_tid, &info, &result );
@@ -1669,6 +2091,7 @@ BOOL WINAPI SendMessageCallbackW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lpa
     info.lparam   = lparam;
     info.callback = callback;
     info.data     = data;
+    info.flags    = 0;
 
     if (is_broadcast(hwnd))
     {
@@ -1676,12 +2099,14 @@ BOOL WINAPI SendMessageCallbackW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lpa
         return TRUE;
     }
 
-    dest_tid = GetWindowThreadProcessId( hwnd, NULL );
+    if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
+
+    if (USER_IsExitingThread( dest_tid )) return TRUE;
 
     if (dest_tid == GetCurrentThreadId())
     {
-        result = call_window_proc( hwnd, msg, wparam, lparam, TRUE );
-        callback( hwnd, msg, data, result );
+        result = call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
+        call_sendmsg_callback( callback, hwnd, msg, data, result );
         return TRUE;
     }
     FIXME( "callback will not be called\n" );
@@ -1740,6 +2165,7 @@ BOOL WINAPI PostMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
 BOOL WINAPI PostMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
 {
     struct send_message_info info;
+    DWORD dest_tid;
 
     if (is_pointer_message( msg ))
     {
@@ -1747,18 +2173,29 @@ BOOL WINAPI PostMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
         return FALSE;
     }
 
+    TRACE( "hwnd %p msg %x (%s) wp %x lp %lx\n",
+           hwnd, msg, SPY_GetMsgName(msg, hwnd), wparam, lparam );
+
     info.type   = MSG_POSTED;
     info.hwnd   = hwnd;
     info.msg    = msg;
     info.wparam = wparam;
     info.lparam = lparam;
+    info.flags  = 0;
 
     if (is_broadcast(hwnd))
     {
         EnumWindows( broadcast_message_callback, (LPARAM)&info );
         return TRUE;
     }
-    return put_message_in_queue( GetWindowThreadProcessId( hwnd, NULL ), &info, NULL );
+
+    if (!hwnd) return PostThreadMessageW( GetCurrentThreadId(), msg, wparam, lparam );
+
+    if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
+
+    if (USER_IsExitingThread( dest_tid )) return TRUE;
+
+    return put_message_in_queue( dest_tid, &info, NULL );
 }
 
 
@@ -1783,12 +2220,14 @@ BOOL WINAPI PostThreadMessageW( DWORD thread, UINT msg, WPARAM wparam, LPARAM lp
         SetLastError( ERROR_INVALID_PARAMETER );
         return FALSE;
     }
+    if (USER_IsExitingThread( thread )) return TRUE;
 
     info.type   = MSG_POSTED;
     info.hwnd   = 0;
     info.msg    = msg;
     info.wparam = wparam;
     info.lparam = lparam;
+    info.flags  = 0;
     return put_message_in_queue( thread, &info, NULL );
 }
 
@@ -1821,9 +2260,6 @@ BOOL WINAPI PeekMessageW( MSG *msg_out, HWND hwnd, UINT first, UINT last, UINT f
     if (!MSG_peek_message( &msg, hwnd, first, last,
                            (flags & PM_REMOVE) ? GET_MSG_REMOVE : 0 ))
     {
-        /* FIXME: should be done before checking for hw events */
-        MSG_JournalPlayBackMsg();
-
         if (!(flags & PM_NOYIELD))
         {
             DWORD count;
@@ -1839,17 +2275,11 @@ BOOL WINAPI PeekMessageW( MSG *msg_out, HWND hwnd, UINT first, UINT last, UINT f
     /* need to fill the window handle for WM_PAINT message */
     if (msg.message == WM_PAINT)
     {
-        if (!(msg.hwnd = WIN_FindWinToRepaint( hwnd ))) return FALSE;
-
         if (IsIconic( msg.hwnd ) && GetClassLongA( msg.hwnd, GCL_HICON ))
         {
             msg.message = WM_PAINTICON;
             msg.wParam = 1;
         }
-
-        /* check hwnd filter */
-        if (hwnd && msg.hwnd != hwnd && !IsChild( hwnd, msg.hwnd )) return FALSE;
-
         /* clear internal paint flag */
         RedrawWindow( msg.hwnd, NULL, 0, RDW_NOINTERNALPAINT | RDW_NOCHILDREN );
     }
@@ -1857,24 +2287,11 @@ BOOL WINAPI PeekMessageW( MSG *msg_out, HWND hwnd, UINT first, UINT last, UINT f
     if ((queue = QUEUE_Current()))
     {
         queue->GetMessageTimeVal = msg.time;
-        queue->GetMessagePosVal  = MAKELONG( msg.pt.x, msg.pt.y );
-    }
-
-      /* We got a message */
-    if (flags & PM_REMOVE)
-    {
-        if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
-        {
-            BYTE *p = &QueueKeyStateTable[msg.wParam & 0xff];
-
-            if (!(*p & 0x80)) *p ^= 0x01;
-            *p |= 0x80;
-        }
-        else if (msg.message == WM_KEYUP || msg.message == WM_SYSKEYUP)
-            QueueKeyStateTable[msg.wParam & 0xff] &= ~0x80;
+        msg.pt.x = LOWORD( queue->GetMessagePosVal );
+        msg.pt.y = HIWORD( queue->GetMessagePosVal );
     }
 
-    HOOK_CallHooksW( WH_GETMESSAGE, HC_ACTION, flags & PM_REMOVE, (LPARAM)&msg );
+    HOOK_CallHooks( WH_GETMESSAGE, HC_ACTION, flags & PM_REMOVE, (LPARAM)&msg, TRUE );
 
     /* copy back our internal safe copy of message data to msg_out.
      * msg_out is a variable from the *program*, so it can't be used
@@ -1929,10 +2346,10 @@ BOOL WINAPI GetMessageW( MSG *msg, HWND hwnd, UINT first, UINT last )
             req->wake_mask    = QS_SENDMESSAGE;
             req->changed_mask = mask;
             req->skip_wait    = 1;
-            if (!SERVER_CALL())
+            if (!wine_server_call( req ))
             {
-                wake_bits    = req->wake_bits;
-                changed_bits = req->changed_bits;
+                wake_bits    = reply->wake_bits;
+                changed_bits = reply->changed_bits;
             }
         }
         SERVER_END_REQ;
@@ -1968,6 +2385,18 @@ BOOL WINAPI GetMessageA( MSG *msg, HWND hwnd, UINT first, UINT last )
 }
 
 
+/***********************************************************************
+ *             IsDialogMessage  (USER32.@)
+ *             IsDialogMessageA (USER32.@)
+ */
+BOOL WINAPI IsDialogMessageA( HWND hwndDlg, LPMSG pmsg )
+{
+    MSG msg = *pmsg;
+    msg.wParam = map_wparam_AtoW( msg.message, msg.wParam );
+    return IsDialogMessageW( hwndDlg, &msg );
+}
+
+
 /***********************************************************************
  *             SetMessageQueue (USER32.@)
  */
@@ -1976,3 +2405,133 @@ BOOL WINAPI SetMessageQueue( INT size )
     /* now obsolete the message queue will be expanded dynamically as necessary */
     return TRUE;
 }
+
+
+/**********************************************************************
+ *             AttachThreadInput (USER32.@)
+ *
+ * Attaches the input processing mechanism of one thread to that of
+ * another thread.
+ */
+BOOL WINAPI AttachThreadInput( DWORD from, DWORD to, BOOL attach )
+{
+    BOOL ret;
+
+    SERVER_START_REQ( attach_thread_input )
+    {
+        req->tid_from = from;
+        req->tid_to   = to;
+        req->attach   = attach;
+        ret = !wine_server_call_err( req );
+    }
+    SERVER_END_REQ;
+    return ret;
+}
+
+
+/**********************************************************************
+ *             GetGUIThreadInfo  (USER32.@)
+ */
+BOOL WINAPI GetGUIThreadInfo( DWORD id, GUITHREADINFO *info )
+{
+    BOOL ret;
+
+    SERVER_START_REQ( get_thread_input )
+    {
+        req->tid = id;
+        if ((ret = !wine_server_call_err( req )))
+        {
+            info->flags          = 0;
+            info->hwndActive     = reply->active;
+            info->hwndFocus      = reply->focus;
+            info->hwndCapture    = reply->capture;
+            info->hwndMenuOwner  = reply->menu_owner;
+            info->hwndMoveSize   = reply->move_size;
+            info->hwndCaret      = reply->caret;
+            info->rcCaret.left   = reply->rect.left;
+            info->rcCaret.top    = reply->rect.top;
+            info->rcCaret.right  = reply->rect.right;
+            info->rcCaret.bottom = reply->rect.bottom;
+            if (reply->menu_owner) info->flags |= GUI_INMENUMODE;
+            if (reply->move_size) info->flags |= GUI_INMOVESIZE;
+            if (reply->caret) info->flags |= GUI_CARETBLINKING;
+        }
+    }
+    SERVER_END_REQ;
+    return ret;
+}
+
+
+/**********************************************************************
+ *             GetKeyState (USER32.@)
+ *
+ * An application calls the GetKeyState function in response to a
+ * keyboard-input message.  This function retrieves the state of the key
+ * at the time the input message was generated.
+ */
+SHORT WINAPI GetKeyState(INT vkey)
+{
+    SHORT retval = 0;
+
+    SERVER_START_REQ( get_key_state )
+    {
+        req->tid = GetCurrentThreadId();
+        req->key = vkey;
+        if (!wine_server_call( req )) retval = (signed char)reply->state;
+    }
+    SERVER_END_REQ;
+    TRACE("key (0x%x) -> %x\n", vkey, retval);
+    return retval;
+}
+
+
+/**********************************************************************
+ *             GetKeyboardState (USER32.@)
+ */
+BOOL WINAPI GetKeyboardState( LPBYTE state )
+{
+    BOOL ret;
+
+    TRACE("(%p)\n", state);
+
+    memset( state, 0, 256 );
+    SERVER_START_REQ( get_key_state )
+    {
+        req->tid = GetCurrentThreadId();
+        req->key = -1;
+        wine_server_set_reply( req, state, 256 );
+        ret = !wine_server_call_err( req );
+    }
+    SERVER_END_REQ;
+    return ret;
+}
+
+
+/**********************************************************************
+ *             SetKeyboardState (USER32.@)
+ */
+BOOL WINAPI SetKeyboardState( LPBYTE state )
+{
+    BOOL ret;
+
+    TRACE("(%p)\n", state);
+
+    SERVER_START_REQ( set_key_state )
+    {
+        req->tid = GetCurrentThreadId();
+        wine_server_add_data( req, state, 256 );
+        ret = !wine_server_call_err( req );
+    }
+    SERVER_END_REQ;
+    return ret;
+}
+
+/******************************************************************
+ *             IsHungAppWindow (USER32.@)
+ *
+ */
+BOOL WINAPI IsHungAppWindow( HWND hWnd )
+{
+    DWORD dwResult; 
+    return !SendMessageTimeoutA(hWnd, WM_NULL, 0, 0, SMTO_ABORTIFHUNG, 5000, &dwResult);       
+}