Create an X input context for each top-level window.

diff --git a/dlls/x11drv/window.c b/dlls/x11drv/window.c
index 74cf323..78907ed 100644
--- a/dlls/x11drv/window.c
+++ b/dlls/x11drv/window.c
@@ -688,6 +688,15 @@
         return 0;
     }
 
+    if (is_top_level)
+    {
+        XIM xim = x11drv_thread_data()->xim;
+        if (xim) data->xic = XCreateIC( xim,
+                                        XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
+                                        XNClientWindow, data->whole_window,
+                                        0 );
+    }
+
     /* non-maximized child must be at bottom of Z order */
     if ((win->dwStyle & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
     {
@@ -829,6 +838,11 @@
         XDeleteContext( display, data->whole_window, winContext );
         XDeleteContext( display, data->client_window, winContext );
         XDestroyWindow( display, data->whole_window );  /* this destroys client too */
+        if (data->xic)
+        {
+            XUnsetICFocus( data->xic );
+            XDestroyIC( data->xic );
+        }
         destroy_icon_window( display, wndPtr );
         wine_tsx11_unlock();
     }
@@ -871,6 +885,7 @@
     data->whole_window  = 0;
     data->client_window = 0;
     data->icon_window   = 0;
+    data->xic           = 0;
     data->hWMIconBitmap = 0;
     data->hWMIconMask   = 0;
 
@@ -1080,6 +1095,26 @@
 }
 
 
+/***********************************************************************
+ *		X11DRV_get_ic
+ *
+ * Return the X input context associated with a window
+ */
+XIC X11DRV_get_ic( HWND hwnd )
+{
+    XIC ret = 0;
+    WND *win = WIN_GetPtr( hwnd );
+
+    if (win && win != WND_OTHER_PROCESS)
+    {
+        struct x11drv_win_data *data = win->pDriverData;
+        ret = data->xic;
+        WIN_ReleasePtr( win );
+    }
+    return ret;
+}
+
+
 /*****************************************************************
  *		SetParent   (X11DRV.@)
  */
diff --git a/dlls/x11drv/x11drv_main.c b/dlls/x11drv/x11drv_main.c
index 0dcc6dd..00caec8 100644
--- a/dlls/x11drv/x11drv_main.c
+++ b/dlls/x11drv/x11drv_main.c
@@ -328,14 +328,6 @@
         screen_depth = desktop_vi->depth;
     }
 
-    /* tell the libX11 that we will do input method handling ourselves
-     * that keep libX11 from doing anything whith dead keys, allowing Wine
-     * to have total control over dead keys, that is this line allows
-     * them to work in Wine, even whith a libX11 including the dead key
-     * patches from Th.Quinot (http://Web.FdN.FR/~tquinot/dead-keys.en.html)
-     */
-    TSXOpenIM( display, NULL, NULL, NULL);
-
     if (synchronous) XSynchronize( display, True );
 
     screen_width  = WidthOfScreen( screen );
@@ -377,6 +369,7 @@
         CloseHandle( data->display_fd );
         wine_tsx11_lock();
         XCloseDisplay( data->display );
+        if (data->xim) XCloseIM( data->xim );
         wine_tsx11_unlock();
         HeapFree( GetProcessHeap(), 0, data );
     }
@@ -429,6 +422,10 @@
         ExitProcess(1);
     }
     fcntl( ConnectionNumber(data->display), F_SETFD, 1 ); /* set close on exec flag */
+
+    if (!(data->xim = XOpenIM( data->display, NULL, NULL, NULL )))
+        WARN("Can't open input method\n");
+
     if (synchronous) XSynchronize( data->display, True );
     wine_tsx11_unlock();
     if (wine_server_fd_to_handle( ConnectionNumber(data->display), GENERIC_READ | SYNCHRONIZE,
diff --git a/include/x11drv.h b/include/x11drv.h
index d63bfbd..621a877 100644
--- a/include/x11drv.h
+++ b/include/x11drv.h
@@ -337,6 +337,7 @@
     Cursor   cursor;               /* current cursor */
     Window   cursor_window;        /* current window that contains the cursor */
     HWND     last_focus;           /* last window that had focus */
+    XIM      xim;                  /* input method */
 };
 
 extern struct x11drv_thread_data *x11drv_init_thread_data(void);
@@ -400,6 +401,7 @@
     Window  icon_window;    /* X window for the icon */
     RECT    whole_rect;     /* X window rectangle for the whole window relative to parent */
     RECT    client_rect;    /* client area relative to whole window */
+    XIC     xic;            /* X input context */
     HBITMAP hWMIconBitmap;
     HBITMAP hWMIconMask;
 };
@@ -408,6 +410,7 @@
 
 extern Window X11DRV_get_client_window( HWND hwnd );
 extern Window X11DRV_get_whole_window( HWND hwnd );
+extern XIC X11DRV_get_ic( HWND hwnd );
 
 inline static Window get_client_window( WND *wnd )
 {