winex11.drv: Avoid copying window bits if the window didn't move.
[wine] / dlls / winex11.drv / window.c
1 /*
2  * Window related functions
3  *
4  * Copyright 1993, 1994, 1995, 1996, 2001 Alexandre Julliard
5  * Copyright 1993 David Metcalfe
6  * Copyright 1995, 1996 Alex Korobka
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #ifdef HAVE_UNISTD_H
29 # include <unistd.h>
30 #endif
31
32 #include <X11/Xlib.h>
33 #include <X11/Xresource.h>
34 #include <X11/Xutil.h>
35 #ifdef HAVE_LIBXSHAPE
36 #include <X11/extensions/shape.h>
37 #endif /* HAVE_LIBXSHAPE */
38
39 #include "windef.h"
40 #include "winbase.h"
41 #include "wingdi.h"
42 #include "winuser.h"
43 #include "wine/unicode.h"
44
45 #include "x11drv.h"
46 #include "xcomposite.h"
47 #include "wine/debug.h"
48 #include "wine/server.h"
49 #include "mwm.h"
50
51 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
52
53 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT      0
54 #define _NET_WM_MOVERESIZE_SIZE_TOP          1
55 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT     2
56 #define _NET_WM_MOVERESIZE_SIZE_RIGHT        3
57 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT  4
58 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM       5
59 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT   6
60 #define _NET_WM_MOVERESIZE_SIZE_LEFT         7
61 #define _NET_WM_MOVERESIZE_MOVE              8
62 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD     9
63 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD    10
64
65 #define _NET_WM_STATE_REMOVE  0
66 #define _NET_WM_STATE_ADD     1
67 #define _NET_WM_STATE_TOGGLE  2
68
69 #define SWP_AGG_NOPOSCHANGE (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_NOZORDER)
70
71 /* X context to associate a hwnd to an X window */
72 XContext winContext = 0;
73
74 /* X context to associate a struct x11drv_win_data to an hwnd */
75 static XContext win_data_context;
76
77 /* time of last user event and window where it's stored */
78 static Time last_user_time;
79 static Window user_time_window;
80
81 static const char whole_window_prop[] = "__wine_x11_whole_window";
82 static const char client_window_prop[]= "__wine_x11_client_window";
83 static const char icon_window_prop[]  = "__wine_x11_icon_window";
84 static const char fbconfig_id_prop[]  = "__wine_x11_fbconfig_id";
85 static const char gl_drawable_prop[]  = "__wine_x11_gl_drawable";
86 static const char pixmap_prop[]       = "__wine_x11_pixmap";
87 static const char managed_prop[]      = "__wine_x11_managed";
88
89
90 /***********************************************************************
91  * http://standards.freedesktop.org/startup-notification-spec
92  */
93 static void remove_startup_notification(Display *display, Window window)
94 {
95     static LONG startup_notification_removed = 0;
96     char id[1024];
97     char message[1024];
98     int i;
99     int pos;
100     XEvent xevent;
101     const char *src;
102     int srclen;
103
104     if (InterlockedCompareExchange(&startup_notification_removed, 1, 0) != 0)
105         return;
106
107     if (GetEnvironmentVariableA("DESKTOP_STARTUP_ID", id, sizeof(id)) == 0)
108         return;
109     SetEnvironmentVariableA("DESKTOP_STARTUP_ID", NULL);
110
111     if ((src = strstr( id, "_TIME" ))) update_user_time( atol( src + 5 ));
112
113     pos = snprintf(message, sizeof(message), "remove: ID=");
114     message[pos++] = '"';
115     for (i = 0; id[i] && pos < sizeof(message) - 2; i++)
116     {
117         if (id[i] == '"' || id[i] == '\\')
118             message[pos++] = '\\';
119         message[pos++] = id[i];
120     }
121     message[pos++] = '"';
122     message[pos++] = '\0';
123
124     xevent.xclient.type = ClientMessage;
125     xevent.xclient.message_type = x11drv_atom(_NET_STARTUP_INFO_BEGIN);
126     xevent.xclient.display = display;
127     xevent.xclient.window = window;
128     xevent.xclient.format = 8;
129
130     src = message;
131     srclen = strlen(src) + 1;
132
133     wine_tsx11_lock();
134     while (srclen > 0)
135     {
136         int msglen = srclen;
137         if (msglen > 20)
138             msglen = 20;
139         memset(&xevent.xclient.data.b[0], 0, 20);
140         memcpy(&xevent.xclient.data.b[0], src, msglen);
141         src += msglen;
142         srclen -= msglen;
143
144         XSendEvent( display, DefaultRootWindow( display ), False, PropertyChangeMask, &xevent );
145         xevent.xclient.message_type = x11drv_atom(_NET_STARTUP_INFO);
146     }
147     wine_tsx11_unlock();
148 }
149
150
151 struct has_popup_result
152 {
153     HWND hwnd;
154     BOOL found;
155 };
156
157 static BOOL CALLBACK has_popup( HWND hwnd, LPARAM lparam )
158 {
159     struct has_popup_result *result = (struct has_popup_result *)lparam;
160
161     if (hwnd == result->hwnd) return FALSE;  /* popups are always above owner */
162     result->found = (GetWindow( hwnd, GW_OWNER ) == result->hwnd);
163     return !result->found;
164 }
165
166 static BOOL has_owned_popups( HWND hwnd )
167 {
168     struct has_popup_result result;
169
170     result.hwnd = hwnd;
171     result.found = FALSE;
172     EnumWindows( has_popup, (LPARAM)&result );
173     return result.found;
174 }
175
176 /***********************************************************************
177  *              is_window_managed
178  *
179  * Check if a given window should be managed
180  */
181 static BOOL is_window_managed( HWND hwnd, UINT swp_flags, const RECT *window_rect )
182 {
183     DWORD style, ex_style;
184
185     if (!managed_mode) return FALSE;
186
187     /* child windows are not managed */
188     style = GetWindowLongW( hwnd, GWL_STYLE );
189     if ((style & (WS_CHILD|WS_POPUP)) == WS_CHILD) return FALSE;
190     /* activated windows are managed */
191     if (!(swp_flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW))) return TRUE;
192     if (hwnd == GetActiveWindow()) return TRUE;
193     /* windows with caption are managed */
194     if ((style & WS_CAPTION) == WS_CAPTION) return TRUE;
195     /* windows with thick frame are managed */
196     if (style & WS_THICKFRAME) return TRUE;
197     if (style & WS_POPUP)
198     {
199         HMONITOR hmon;
200         MONITORINFO mi;
201
202         /* popup with sysmenu == caption are managed */
203         if (style & WS_SYSMENU) return TRUE;
204         /* full-screen popup windows are managed */
205         hmon = MonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY );
206         mi.cbSize = sizeof( mi );
207         GetMonitorInfoW( hmon, &mi );
208         if (window_rect->left <= mi.rcWork.left && window_rect->right >= mi.rcWork.right &&
209             window_rect->top <= mi.rcWork.top && window_rect->bottom >= mi.rcWork.bottom)
210             return TRUE;
211     }
212     /* application windows are managed */
213     ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
214     if (ex_style & WS_EX_APPWINDOW) return TRUE;
215     /* windows that own popups are managed */
216     if (has_owned_popups( hwnd )) return TRUE;
217     /* default: not managed */
218     return FALSE;
219 }
220
221
222 /***********************************************************************
223  *              is_window_rect_mapped
224  *
225  * Check if the X whole window should be mapped based on its rectangle
226  */
227 static BOOL is_window_rect_mapped( const RECT *rect )
228 {
229     /* don't map if rect is off-screen */
230     if (rect->left >= virtual_screen_rect.right ||
231         rect->top >= virtual_screen_rect.bottom ||
232         rect->right <= virtual_screen_rect.left ||
233         rect->bottom <= virtual_screen_rect.top)
234         return FALSE;
235
236     return TRUE;
237 }
238
239
240 /***********************************************************************
241  *              is_window_resizable
242  *
243  * Check if window should be made resizable by the window manager
244  */
245 static inline BOOL is_window_resizable( struct x11drv_win_data *data, DWORD style )
246 {
247     if (style & WS_THICKFRAME) return TRUE;
248     /* Metacity needs the window to be resizable to make it fullscreen */
249     return (data->whole_rect.left <= 0 && data->whole_rect.right >= screen_width &&
250             data->whole_rect.top <= 0 && data->whole_rect.bottom >= screen_height);
251 }
252
253
254 /***********************************************************************
255  *              get_mwm_decorations
256  */
257 static unsigned long get_mwm_decorations( struct x11drv_win_data *data,
258                                           DWORD style, DWORD ex_style )
259 {
260     unsigned long ret = 0;
261
262     if (!decorated_mode) return 0;
263
264     if (IsRectEmpty( &data->window_rect )) return 0;
265     if (data->shaped) return 0;
266
267     if (ex_style & WS_EX_TOOLWINDOW) return 0;
268
269     if ((style & WS_CAPTION) == WS_CAPTION)
270     {
271         ret |= MWM_DECOR_TITLE | MWM_DECOR_BORDER;
272         if (style & WS_SYSMENU) ret |= MWM_DECOR_MENU;
273         if (style & WS_MINIMIZEBOX) ret |= MWM_DECOR_MINIMIZE;
274         if (style & WS_MAXIMIZEBOX) ret |= MWM_DECOR_MAXIMIZE;
275     }
276     if (ex_style & WS_EX_DLGMODALFRAME) ret |= MWM_DECOR_BORDER;
277     else if (style & WS_THICKFRAME) ret |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
278     else if ((style & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) ret |= MWM_DECOR_BORDER;
279     return ret;
280 }
281
282
283 /***********************************************************************
284  *              get_x11_rect_offset
285  *
286  * Helper for X11DRV_window_to_X_rect and X11DRV_X_to_window_rect.
287  */
288 static void get_x11_rect_offset( struct x11drv_win_data *data, RECT *rect )
289 {
290     DWORD style, ex_style, style_mask = 0, ex_style_mask = 0;
291     unsigned long decor;
292
293     rect->top = rect->bottom = rect->left = rect->right = 0;
294
295     style = GetWindowLongW( data->hwnd, GWL_STYLE );
296     ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
297     decor = get_mwm_decorations( data, style, ex_style );
298
299     if (decor & MWM_DECOR_TITLE) style_mask |= WS_CAPTION;
300     if (decor & MWM_DECOR_BORDER)
301     {
302         style_mask |= WS_DLGFRAME | WS_THICKFRAME;
303         ex_style_mask |= WS_EX_DLGMODALFRAME;
304     }
305
306     AdjustWindowRectEx( rect, style & style_mask, FALSE, ex_style & ex_style_mask );
307 }
308
309
310 /***********************************************************************
311  *              get_window_attributes
312  *
313  * Fill the window attributes structure for an X window.
314  */
315 static int get_window_attributes( Display *display, struct x11drv_win_data *data,
316                                   XSetWindowAttributes *attr )
317 {
318     attr->override_redirect = !data->managed;
319     attr->colormap          = X11DRV_PALETTE_PaletteXColormap;
320     attr->save_under        = ((GetClassLongW( data->hwnd, GCL_STYLE ) & CS_SAVEBITS) != 0);
321     attr->bit_gravity       = NorthWestGravity;
322     attr->win_gravity       = StaticGravity;
323     attr->backing_store     = NotUseful;
324     attr->event_mask        = (ExposureMask | PointerMotionMask |
325                                ButtonPressMask | ButtonReleaseMask | EnterWindowMask |
326                                KeyPressMask | KeyReleaseMask | FocusChangeMask |
327                                KeymapStateMask | StructureNotifyMask);
328     if (data->managed) attr->event_mask |= PropertyChangeMask;
329
330     return (CWOverrideRedirect | CWSaveUnder | CWColormap |
331             CWEventMask | CWBitGravity | CWBackingStore);
332 }
333
334
335 /***********************************************************************
336  *              create_client_window
337  */
338 static Window create_client_window( Display *display, struct x11drv_win_data *data, XVisualInfo *vis )
339 {
340     int cx, cy, mask;
341     XSetWindowAttributes attr;
342     Window client;
343     Visual *client_visual = vis ? vis->visual : visual;
344
345     attr.bit_gravity = NorthWestGravity;
346     attr.win_gravity = NorthWestGravity;
347     attr.backing_store = NotUseful;
348     attr.event_mask = (ExposureMask | PointerMotionMask |
349                        ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
350     mask = CWEventMask | CWBitGravity | CWWinGravity | CWBackingStore;
351
352     if ((cx = data->client_rect.right - data->client_rect.left) <= 0) cx = 1;
353     else if (cx > 65535) cx = 65535;
354     if ((cy = data->client_rect.bottom - data->client_rect.top) <= 0) cy = 1;
355     else if (cy > 65535) cy = 65535;
356
357     wine_tsx11_lock();
358
359     if (vis)
360     {
361         attr.colormap = XCreateColormap( display, root_window, vis->visual,
362                                          (vis->class == PseudoColor || vis->class == GrayScale ||
363                                           vis->class == DirectColor) ? AllocAll : AllocNone );
364         mask |= CWColormap;
365     }
366
367     client = XCreateWindow( display, data->whole_window,
368                             data->client_rect.left - data->whole_rect.left,
369                             data->client_rect.top - data->whole_rect.top,
370                             cx, cy, 0, screen_depth, InputOutput,
371                             client_visual, mask, &attr );
372     if (!client)
373     {
374         wine_tsx11_unlock();
375         return 0;
376     }
377
378     if (data->client_window)
379     {
380         XDeleteContext( display, data->client_window, winContext );
381         XDestroyWindow( display, data->client_window );
382     }
383     data->client_window = client;
384     data->visualid = XVisualIDFromVisual( client_visual );
385
386     if (data->colormap) XFreeColormap( display, data->colormap );
387     data->colormap = vis ? attr.colormap : 0;
388
389     XMapWindow( display, data->client_window );
390     XSaveContext( display, data->client_window, winContext, (char *)data->hwnd );
391     wine_tsx11_unlock();
392
393     SetPropA( data->hwnd, client_window_prop, (HANDLE)data->client_window );
394     return data->client_window;
395 }
396
397
398 /***********************************************************************
399  *              sync_window_style
400  *
401  * Change the X window attributes when the window style has changed.
402  */
403 static void sync_window_style( Display *display, struct x11drv_win_data *data )
404 {
405     if (data->whole_window != root_window)
406     {
407         XSetWindowAttributes attr;
408         int mask = get_window_attributes( display, data, &attr );
409
410         wine_tsx11_lock();
411         XChangeWindowAttributes( display, data->whole_window, mask, &attr );
412         wine_tsx11_unlock();
413     }
414 }
415
416
417 /***********************************************************************
418  *              sync_window_cursor
419  */
420 static void sync_window_cursor( struct x11drv_win_data *data )
421 {
422     HCURSOR cursor;
423
424     SERVER_START_REQ( set_cursor )
425     {
426         req->flags = 0;
427         wine_server_call( req );
428         cursor = reply->prev_count >= 0 ? wine_server_ptr_handle( reply->prev_handle ) : 0;
429     }
430     SERVER_END_REQ;
431
432     set_window_cursor( data->hwnd, cursor );
433 }
434
435
436 /***********************************************************************
437  *              sync_window_region
438  *
439  * Update the X11 window region.
440  */
441 static void sync_window_region( Display *display, struct x11drv_win_data *data, HRGN win_region )
442 {
443 #ifdef HAVE_LIBXSHAPE
444     HRGN hrgn = win_region;
445
446     if (!data->whole_window) return;
447     data->shaped = FALSE;
448
449     if (IsRectEmpty( &data->window_rect ))  /* set an empty shape */
450     {
451         static XRectangle empty_rect;
452         wine_tsx11_lock();
453         XShapeCombineRectangles( display, data->whole_window, ShapeBounding, 0, 0,
454                                  &empty_rect, 1, ShapeSet, YXBanded );
455         wine_tsx11_unlock();
456         return;
457     }
458
459     if (hrgn == (HRGN)1)  /* hack: win_region == 1 means retrieve region from server */
460     {
461         if (!(hrgn = CreateRectRgn( 0, 0, 0, 0 ))) return;
462         if (GetWindowRgn( data->hwnd, hrgn ) == ERROR)
463         {
464             DeleteObject( hrgn );
465             hrgn = 0;
466         }
467     }
468
469     if (!hrgn)
470     {
471         wine_tsx11_lock();
472         XShapeCombineMask( display, data->whole_window, ShapeBounding, 0, 0, None, ShapeSet );
473         wine_tsx11_unlock();
474     }
475     else
476     {
477         RGNDATA *pRegionData = X11DRV_GetRegionData( hrgn, 0 );
478         if (pRegionData)
479         {
480             wine_tsx11_lock();
481             XShapeCombineRectangles( display, data->whole_window, ShapeBounding,
482                                      data->window_rect.left - data->whole_rect.left,
483                                      data->window_rect.top - data->whole_rect.top,
484                                      (XRectangle *)pRegionData->Buffer,
485                                      pRegionData->rdh.nCount, ShapeSet, YXBanded );
486             wine_tsx11_unlock();
487             HeapFree(GetProcessHeap(), 0, pRegionData);
488             data->shaped = TRUE;
489         }
490     }
491     if (hrgn && hrgn != win_region) DeleteObject( hrgn );
492 #endif  /* HAVE_LIBXSHAPE */
493 }
494
495
496 /***********************************************************************
497  *              sync_window_opacity
498  */
499 static void sync_window_opacity( Display *display, Window win,
500                                  COLORREF key, BYTE alpha, DWORD flags )
501 {
502     unsigned long opacity = 0xffffffff;
503
504     if (flags & LWA_ALPHA) opacity = (0xffffffff / 0xff) * alpha;
505
506     if (flags & LWA_COLORKEY) FIXME("LWA_COLORKEY not supported\n");
507
508     wine_tsx11_lock();
509     if (opacity == 0xffffffff)
510         XDeleteProperty( display, win, x11drv_atom(_NET_WM_WINDOW_OPACITY) );
511     else
512         XChangeProperty( display, win, x11drv_atom(_NET_WM_WINDOW_OPACITY),
513                          XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&opacity, 1 );
514     wine_tsx11_unlock();
515 }
516
517
518 /***********************************************************************
519  *              sync_window_text
520  */
521 static void sync_window_text( Display *display, Window win, const WCHAR *text )
522 {
523     UINT count;
524     char *buffer, *utf8_buffer;
525     XTextProperty prop;
526
527     /* allocate new buffer for window text */
528     count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
529     if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count ))) return;
530     WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
531
532     count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
533     if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
534     {
535         HeapFree( GetProcessHeap(), 0, buffer );
536         return;
537     }
538     WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
539
540     wine_tsx11_lock();
541     if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
542     {
543         XSetWMName( display, win, &prop );
544         XSetWMIconName( display, win, &prop );
545         XFree( prop.value );
546     }
547     /*
548       Implements a NET_WM UTF-8 title. It should be without a trailing \0,
549       according to the standard
550       ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
551     */
552     XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
553                      8, PropModeReplace, (unsigned char *) utf8_buffer, count);
554     wine_tsx11_unlock();
555
556     HeapFree( GetProcessHeap(), 0, utf8_buffer );
557     HeapFree( GetProcessHeap(), 0, buffer );
558 }
559
560
561 /***********************************************************************
562  *              set_win_format
563  */
564 static BOOL set_win_format( HWND hwnd, XID fbconfig_id )
565 {
566     struct x11drv_win_data *data;
567     XVisualInfo *vis;
568     int w, h;
569
570     if (!(data = X11DRV_get_win_data(hwnd)) &&
571         !(data = X11DRV_create_win_data(hwnd))) return FALSE;
572
573     if (!(vis = visual_from_fbconfig_id(fbconfig_id))) return FALSE;
574
575     if (data->whole_window)
576     {
577         Display *display = thread_display();
578         Window client = data->client_window;
579
580         if (vis->visualid != data->visualid)
581         {
582             client = create_client_window( display, data, vis );
583             TRACE( "re-created client window %lx for %p fbconfig %lx\n", client, data->hwnd, fbconfig_id );
584         }
585         wine_tsx11_lock();
586         XFree(vis);
587         XFlush( display );
588         wine_tsx11_unlock();
589         if (client) goto done;
590         return FALSE;
591     }
592
593     w = data->client_rect.right - data->client_rect.left;
594     h = data->client_rect.bottom - data->client_rect.top;
595
596     if(w <= 0) w = 1;
597     if(h <= 0) h = 1;
598
599 #ifdef SONAME_LIBXCOMPOSITE
600     if(usexcomposite)
601     {
602         XSetWindowAttributes attrib;
603         static Window dummy_parent;
604
605         wine_tsx11_lock();
606         attrib.override_redirect = True;
607         if (!dummy_parent)
608         {
609             dummy_parent = XCreateWindow( gdi_display, root_window, -1, -1, 1, 1, 0, screen_depth,
610                                          InputOutput, visual, CWOverrideRedirect, &attrib );
611             XMapWindow( gdi_display, dummy_parent );
612         }
613         data->colormap = XCreateColormap(gdi_display, dummy_parent, vis->visual,
614                                          (vis->class == PseudoColor ||
615                                           vis->class == GrayScale ||
616                                           vis->class == DirectColor) ?
617                                          AllocAll : AllocNone);
618         attrib.colormap = data->colormap;
619         XInstallColormap(gdi_display, attrib.colormap);
620
621         if(data->gl_drawable) XDestroyWindow(gdi_display, data->gl_drawable);
622         data->gl_drawable = XCreateWindow(gdi_display, dummy_parent, -w, 0, w, h, 0,
623                                           vis->depth, InputOutput, vis->visual,
624                                           CWColormap | CWOverrideRedirect,
625                                           &attrib);
626         if(data->gl_drawable)
627         {
628             pXCompositeRedirectWindow(gdi_display, data->gl_drawable,
629                                       CompositeRedirectManual);
630             XMapWindow(gdi_display, data->gl_drawable);
631         }
632         XFree(vis);
633         XFlush( gdi_display );
634         wine_tsx11_unlock();
635     }
636     else
637 #endif
638     {
639         WARN("XComposite is not available, using GLXPixmap hack\n");
640
641         wine_tsx11_lock();
642
643         if(data->pixmap) XFreePixmap(gdi_display, data->pixmap);
644         data->pixmap = XCreatePixmap(gdi_display, root_window, w, h, vis->depth);
645         if(!data->pixmap)
646         {
647             XFree(vis);
648             wine_tsx11_unlock();
649             return FALSE;
650         }
651
652         if(data->gl_drawable) destroy_glxpixmap(gdi_display, data->gl_drawable);
653         data->gl_drawable = create_glxpixmap(gdi_display, vis, data->pixmap);
654         if(!data->gl_drawable)
655         {
656             XFreePixmap(gdi_display, data->pixmap);
657             data->pixmap = 0;
658         }
659         XFree(vis);
660         XFlush( gdi_display );
661         wine_tsx11_unlock();
662         if (data->pixmap) SetPropA(hwnd, pixmap_prop, (HANDLE)data->pixmap);
663     }
664
665     if (!data->gl_drawable) return FALSE;
666
667     TRACE("Created GL drawable 0x%lx, using FBConfigID 0x%lx\n",
668           data->gl_drawable, fbconfig_id);
669     SetPropA(hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
670
671 done:
672     data->fbconfig_id = fbconfig_id;
673     SetPropA(hwnd, fbconfig_id_prop, (HANDLE)data->fbconfig_id);
674     /* force DCE invalidation */
675     SetWindowPos( hwnd, 0, 0, 0, 0, 0,
676                   SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE |
677                   SWP_NOREDRAW | SWP_DEFERERASE | SWP_NOSENDCHANGING | SWP_STATECHANGED);
678     return TRUE;
679 }
680
681 /***********************************************************************
682  *              sync_gl_drawable
683  */
684 static void sync_gl_drawable(struct x11drv_win_data *data)
685 {
686     int w = data->client_rect.right - data->client_rect.left;
687     int h = data->client_rect.bottom - data->client_rect.top;
688     XVisualInfo *vis;
689     Drawable glxp;
690     Pixmap pix;
691
692     if (w <= 0) w = 1;
693     if (h <= 0) h = 1;
694
695     TRACE("Resizing GL drawable 0x%lx to %dx%d\n", data->gl_drawable, w, h);
696 #ifdef SONAME_LIBXCOMPOSITE
697     if(usexcomposite)
698     {
699         wine_tsx11_lock();
700         XMoveResizeWindow(gdi_display, data->gl_drawable, -w, 0, w, h);
701         wine_tsx11_unlock();
702         return;
703     }
704 #endif
705
706     if (!(vis = visual_from_fbconfig_id(data->fbconfig_id))) return;
707
708     wine_tsx11_lock();
709     pix = XCreatePixmap(gdi_display, root_window, w, h, vis->depth);
710     if(!pix)
711     {
712         ERR("Failed to create pixmap for offscreen rendering\n");
713         XFree(vis);
714         wine_tsx11_unlock();
715         return;
716     }
717
718     glxp = create_glxpixmap(gdi_display, vis, pix);
719     if(!glxp)
720     {
721         ERR("Failed to create drawable for offscreen rendering\n");
722         XFreePixmap(gdi_display, pix);
723         XFree(vis);
724         wine_tsx11_unlock();
725         return;
726     }
727
728     XFree(vis);
729
730     mark_drawable_dirty(data->gl_drawable, glxp);
731
732     XFreePixmap(gdi_display, data->pixmap);
733     destroy_glxpixmap(gdi_display, data->gl_drawable);
734     TRACE( "Recreated GL drawable %lx to replace %lx\n", glxp, data->gl_drawable );
735
736     data->pixmap = pix;
737     data->gl_drawable = glxp;
738
739     XFlush( gdi_display );
740     wine_tsx11_unlock();
741
742     SetPropA(data->hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
743     SetPropA(data->hwnd, pixmap_prop, (HANDLE)data->pixmap);
744 }
745
746
747 /***********************************************************************
748  *              get_window_changes
749  *
750  * fill the window changes structure
751  */
752 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
753 {
754     int mask = 0;
755
756     if (old->right - old->left != new->right - new->left )
757     {
758         if ((changes->width = new->right - new->left) <= 0) changes->width = 1;
759         else if (changes->width > 65535) changes->width = 65535;
760         mask |= CWWidth;
761     }
762     if (old->bottom - old->top != new->bottom - new->top)
763     {
764         if ((changes->height = new->bottom - new->top) <= 0) changes->height = 1;
765         else if (changes->height > 65535) changes->height = 65535;
766         mask |= CWHeight;
767     }
768     if (old->left != new->left)
769     {
770         changes->x = new->left;
771         mask |= CWX;
772     }
773     if (old->top != new->top)
774     {
775         changes->y = new->top;
776         mask |= CWY;
777     }
778     return mask;
779 }
780
781
782 /***********************************************************************
783  *              create_icon_window
784  */
785 static Window create_icon_window( Display *display, struct x11drv_win_data *data )
786 {
787     XSetWindowAttributes attr;
788
789     attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
790                        ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
791     attr.bit_gravity = NorthWestGravity;
792     attr.backing_store = NotUseful/*WhenMapped*/;
793     attr.colormap      = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
794
795     wine_tsx11_lock();
796     data->icon_window = XCreateWindow( display, root_window, 0, 0,
797                                        GetSystemMetrics( SM_CXICON ),
798                                        GetSystemMetrics( SM_CYICON ),
799                                        0, screen_depth,
800                                        InputOutput, visual,
801                                        CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
802     XSaveContext( display, data->icon_window, winContext, (char *)data->hwnd );
803     XFlush( display );  /* make sure the window exists before we start painting to it */
804     wine_tsx11_unlock();
805
806     TRACE( "created %lx\n", data->icon_window );
807     SetPropA( data->hwnd, icon_window_prop, (HANDLE)data->icon_window );
808     return data->icon_window;
809 }
810
811
812
813 /***********************************************************************
814  *              destroy_icon_window
815  */
816 static void destroy_icon_window( Display *display, struct x11drv_win_data *data )
817 {
818     if (!data->icon_window) return;
819     wine_tsx11_lock();
820     XDeleteContext( display, data->icon_window, winContext );
821     XDestroyWindow( display, data->icon_window );
822     data->icon_window = 0;
823     wine_tsx11_unlock();
824     RemovePropA( data->hwnd, icon_window_prop );
825 }
826
827
828 /***********************************************************************
829  *              get_bitmap_argb
830  *
831  * Return the bitmap bits in ARGB format. Helper for setting icon hints.
832  */
833 static unsigned long *get_bitmap_argb( HDC hdc, HBITMAP color, HBITMAP mask, unsigned int *size )
834 {
835     BITMAP bm;
836     BITMAPINFO *info;
837     unsigned int *ptr, *bits = NULL;
838     unsigned char *mask_bits = NULL;
839     int i, j, has_alpha = 0;
840
841     if (!GetObjectW( color, sizeof(bm), &bm )) return NULL;
842     if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) return NULL;
843     info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
844     info->bmiHeader.biWidth = bm.bmWidth;
845     info->bmiHeader.biHeight = -bm.bmHeight;
846     info->bmiHeader.biPlanes = 1;
847     info->bmiHeader.biBitCount = 32;
848     info->bmiHeader.biCompression = BI_RGB;
849     info->bmiHeader.biSizeImage = bm.bmWidth * bm.bmHeight * 4;
850     info->bmiHeader.biXPelsPerMeter = 0;
851     info->bmiHeader.biYPelsPerMeter = 0;
852     info->bmiHeader.biClrUsed = 0;
853     info->bmiHeader.biClrImportant = 0;
854     *size = bm.bmWidth * bm.bmHeight + 2;
855     if (!(bits = HeapAlloc( GetProcessHeap(), 0, *size * sizeof(long) ))) goto failed;
856     if (!GetDIBits( hdc, color, 0, bm.bmHeight, bits + 2, info, DIB_RGB_COLORS )) goto failed;
857
858     bits[0] = bm.bmWidth;
859     bits[1] = bm.bmHeight;
860
861     for (i = 0; i < bm.bmWidth * bm.bmHeight; i++)
862         if ((has_alpha = (bits[i + 2] & 0xff000000) != 0)) break;
863
864     if (!has_alpha)
865     {
866         unsigned int width_bytes = (bm.bmWidth + 31) / 32 * 4;
867         /* generate alpha channel from the mask */
868         info->bmiHeader.biBitCount = 1;
869         info->bmiHeader.biSizeImage = width_bytes * bm.bmHeight;
870         if (!(mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto failed;
871         if (!GetDIBits( hdc, mask, 0, bm.bmHeight, mask_bits, info, DIB_RGB_COLORS )) goto failed;
872         ptr = bits + 2;
873         for (i = 0; i < bm.bmHeight; i++)
874             for (j = 0; j < bm.bmWidth; j++, ptr++)
875                 if (!((mask_bits[i * width_bytes + j / 8] << (j % 8)) & 0x80)) *ptr |= 0xff000000;
876         HeapFree( GetProcessHeap(), 0, mask_bits );
877     }
878     HeapFree( GetProcessHeap(), 0, info );
879
880     /* convert to array of longs */
881     if (bits && sizeof(long) > sizeof(int))
882         for (i = *size - 1; i >= 0; i--) ((unsigned long *)bits)[i] = bits[i];
883
884     return (unsigned long *)bits;
885
886 failed:
887     HeapFree( GetProcessHeap(), 0, info );
888     HeapFree( GetProcessHeap(), 0, bits );
889     HeapFree( GetProcessHeap(), 0, mask_bits );
890     return NULL;
891 }
892
893
894 /***********************************************************************
895  *              set_icon_hints
896  *
897  * Set the icon wm hints
898  */
899 static void set_icon_hints( Display *display, struct x11drv_win_data *data,
900                             HICON icon_big, HICON icon_small )
901 {
902     XWMHints *hints = data->wm_hints;
903
904     if (!icon_big)
905     {
906         icon_big = (HICON)SendMessageW( data->hwnd, WM_GETICON, ICON_BIG, 0 );
907         if (!icon_big) icon_big = (HICON)GetClassLongPtrW( data->hwnd, GCLP_HICON );
908     }
909     if (!icon_small)
910     {
911         icon_small = (HICON)SendMessageW( data->hwnd, WM_GETICON, ICON_SMALL, 0 );
912         if (!icon_small) icon_small = (HICON)GetClassLongPtrW( data->hwnd, GCLP_HICONSM );
913     }
914
915     if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
916     if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
917     data->hWMIconBitmap = 0;
918     data->hWMIconMask = 0;
919
920     if (!icon_big)
921     {
922         if (!data->icon_window) create_icon_window( display, data );
923         hints->icon_window = data->icon_window;
924         hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
925     }
926     else
927     {
928         HBITMAP hbmOrig;
929         RECT rcMask;
930         BITMAP bm;
931         ICONINFO ii, ii_small;
932         HDC hDC;
933         unsigned int size;
934         unsigned long *bits;
935
936         if (!GetIconInfo(icon_big, &ii)) return;
937
938         GetObjectW(ii.hbmMask, sizeof(bm), &bm);
939         rcMask.top    = 0;
940         rcMask.left   = 0;
941         rcMask.right  = bm.bmWidth;
942         rcMask.bottom = bm.bmHeight;
943
944         hDC = CreateCompatibleDC(0);
945         bits = get_bitmap_argb( hDC, ii.hbmColor, ii.hbmMask, &size );
946         if (bits && GetIconInfo( icon_small, &ii_small ))
947         {
948             unsigned int size_small;
949             unsigned long *bits_small, *new;
950
951             if ((bits_small = get_bitmap_argb( hDC, ii_small.hbmColor, ii_small.hbmMask, &size_small )) &&
952                 (bits_small[0] != bits[0] || bits_small[1] != bits[1]))  /* size must be different */
953             {
954                 if ((new = HeapReAlloc( GetProcessHeap(), 0, bits,
955                                         (size + size_small) * sizeof(unsigned long) )))
956                 {
957                     bits = new;
958                     memcpy( bits + size, bits_small, size_small * sizeof(unsigned long) );
959                     size += size_small;
960                 }
961             }
962             HeapFree( GetProcessHeap(), 0, bits_small );
963             DeleteObject( ii_small.hbmColor );
964             DeleteObject( ii_small.hbmMask );
965         }
966         wine_tsx11_lock();
967         if (bits)
968             XChangeProperty( display, data->whole_window, x11drv_atom(_NET_WM_ICON),
969                              XA_CARDINAL, 32, PropModeReplace, (unsigned char *)bits, size );
970         else
971             XDeleteProperty( display, data->whole_window, x11drv_atom(_NET_WM_ICON) );
972         wine_tsx11_unlock();
973         HeapFree( GetProcessHeap(), 0, bits );
974
975         hbmOrig = SelectObject(hDC, ii.hbmMask);
976         InvertRect(hDC, &rcMask);
977         SelectObject(hDC, ii.hbmColor);  /* force the color bitmap to x11drv mode too */
978         SelectObject(hDC, hbmOrig);
979
980         data->hWMIconBitmap = ii.hbmColor;
981         data->hWMIconMask = ii.hbmMask;
982
983         hints->icon_pixmap = X11DRV_get_pixmap(data->hWMIconBitmap);
984         hints->icon_mask = X11DRV_get_pixmap(data->hWMIconMask);
985         destroy_icon_window( display, data );
986         hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
987
988         DeleteDC(hDC);
989     }
990 }
991
992
993 /***********************************************************************
994  *              set_size_hints
995  *
996  * set the window size hints
997  */
998 static void set_size_hints( Display *display, struct x11drv_win_data *data, DWORD style )
999 {
1000     XSizeHints* size_hints;
1001
1002     if (!(size_hints = XAllocSizeHints())) return;
1003
1004     size_hints->win_gravity = StaticGravity;
1005     size_hints->flags |= PWinGravity;
1006
1007     /* don't update size hints if window is not in normal state */
1008     if (!(style & (WS_MINIMIZE | WS_MAXIMIZE)))
1009     {
1010         if (data->hwnd != GetDesktopWindow())  /* don't force position of desktop */
1011         {
1012             size_hints->x = data->whole_rect.left;
1013             size_hints->y = data->whole_rect.top;
1014             size_hints->flags |= PPosition;
1015         }
1016
1017         if (!is_window_resizable( data, style ))
1018         {
1019             size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
1020             size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
1021             if (size_hints->max_width <= 0 ||size_hints->max_height <= 0)
1022                 size_hints->max_width = size_hints->max_height = 1;
1023             size_hints->min_width = size_hints->max_width;
1024             size_hints->min_height = size_hints->max_height;
1025             size_hints->flags |= PMinSize | PMaxSize;
1026         }
1027     }
1028     XSetWMNormalHints( display, data->whole_window, size_hints );
1029     XFree( size_hints );
1030 }
1031
1032
1033 /***********************************************************************
1034  *              get_process_name
1035  *
1036  * get the name of the current process for setting class hints
1037  */
1038 static char *get_process_name(void)
1039 {
1040     static char *name;
1041
1042     if (!name)
1043     {
1044         WCHAR module[MAX_PATH];
1045         DWORD len = GetModuleFileNameW( 0, module, MAX_PATH );
1046         if (len && len < MAX_PATH)
1047         {
1048             char *ptr;
1049             WCHAR *p, *appname = module;
1050
1051             if ((p = strrchrW( appname, '/' ))) appname = p + 1;
1052             if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
1053             len = WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, NULL, 0, NULL, NULL );
1054             if ((ptr = HeapAlloc( GetProcessHeap(), 0, len )))
1055             {
1056                 WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, ptr, len, NULL, NULL );
1057                 name = ptr;
1058             }
1059         }
1060     }
1061     return name;
1062 }
1063
1064
1065 /***********************************************************************
1066  *              set_initial_wm_hints
1067  *
1068  * Set the window manager hints that don't change over the lifetime of a window.
1069  */
1070 static void set_initial_wm_hints( Display *display, struct x11drv_win_data *data )
1071 {
1072     long i;
1073     Atom protocols[3];
1074     Atom dndVersion = WINE_XDND_VERSION;
1075     XClassHint *class_hints;
1076     char *process_name = get_process_name();
1077
1078     wine_tsx11_lock();
1079
1080     /* wm protocols */
1081     i = 0;
1082     protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
1083     protocols[i++] = x11drv_atom(_NET_WM_PING);
1084     if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
1085     XChangeProperty( display, data->whole_window, x11drv_atom(WM_PROTOCOLS),
1086                      XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
1087
1088     /* class hints */
1089     if ((class_hints = XAllocClassHint()))
1090     {
1091         static char wine[] = "Wine";
1092
1093         class_hints->res_name = process_name;
1094         class_hints->res_class = wine;
1095         XSetClassHint( display, data->whole_window, class_hints );
1096         XFree( class_hints );
1097     }
1098
1099     /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
1100     XSetWMProperties(display, data->whole_window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
1101     /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
1102     i = getpid();
1103     XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_PID),
1104                     XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
1105
1106     XChangeProperty( display, data->whole_window, x11drv_atom(XdndAware),
1107                      XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
1108
1109     update_user_time( 0 );  /* make sure that the user time window exists */
1110     if (user_time_window)
1111         XChangeProperty( display, data->whole_window, x11drv_atom(_NET_WM_USER_TIME_WINDOW),
1112                          XA_WINDOW, 32, PropModeReplace, (unsigned char *)&user_time_window, 1 );
1113
1114     data->wm_hints = XAllocWMHints();
1115     wine_tsx11_unlock();
1116
1117     if (data->wm_hints)
1118     {
1119         data->wm_hints->flags = 0;
1120         set_icon_hints( display, data, 0, 0 );
1121     }
1122 }
1123
1124
1125 /***********************************************************************
1126  *              get_owner_whole_window
1127  *
1128  * Retrieve an owner's window, creating it if necessary.
1129  */
1130 static Window get_owner_whole_window( HWND owner, BOOL force_managed )
1131 {
1132     struct x11drv_win_data *data;
1133
1134     if (!owner) return 0;
1135
1136     if (!(data = X11DRV_get_win_data( owner )))
1137     {
1138         if (GetWindowThreadProcessId( owner, NULL ) != GetCurrentThreadId() ||
1139             !(data = X11DRV_create_win_data( owner )))
1140             return (Window)GetPropA( owner, whole_window_prop );
1141     }
1142     else if (!data->managed && force_managed)  /* make it managed */
1143     {
1144         SetWindowPos( owner, 0, 0, 0, 0, 0,
1145                       SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE |
1146                       SWP_NOREDRAW | SWP_DEFERERASE | SWP_NOSENDCHANGING | SWP_STATECHANGED );
1147     }
1148     return data->whole_window;
1149 }
1150
1151
1152 /***********************************************************************
1153  *              set_wm_hints
1154  *
1155  * Set the window manager hints for a newly-created window
1156  */
1157 static void set_wm_hints( Display *display, struct x11drv_win_data *data )
1158 {
1159     Window group_leader = data->whole_window;
1160     Window owner_win = 0;
1161     Atom window_type;
1162     MwmHints mwm_hints;
1163     DWORD style, ex_style;
1164     HWND owner;
1165
1166     if (data->hwnd == GetDesktopWindow())
1167     {
1168         /* force some styles for the desktop to get the correct decorations */
1169         style = WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
1170         ex_style = WS_EX_APPWINDOW;
1171         owner = 0;
1172     }
1173     else
1174     {
1175         style = GetWindowLongW( data->hwnd, GWL_STYLE );
1176         ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
1177         owner = GetWindow( data->hwnd, GW_OWNER );
1178         if ((owner_win = get_owner_whole_window( owner, data->managed ))) group_leader = owner_win;
1179     }
1180
1181     wine_tsx11_lock();
1182
1183     if (owner_win) XSetTransientForHint( display, data->whole_window, owner_win );
1184
1185     /* size hints */
1186     set_size_hints( display, data, style );
1187
1188     /* set the WM_WINDOW_TYPE */
1189     if (style & WS_THICKFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
1190     else if (ex_style & WS_EX_APPWINDOW) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
1191     else if (style & WS_MINIMIZEBOX) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
1192     else if (style & WS_DLGFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
1193     else if (ex_style & WS_EX_DLGMODALFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
1194     else if ((style & WS_POPUP) && owner) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
1195 #if 0  /* many window managers don't handle utility windows very well */
1196     else if (ex_style & WS_EX_TOOLWINDOW) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY);
1197 #endif
1198     else window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
1199
1200     XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
1201                     XA_ATOM, 32, PropModeReplace, (unsigned char*)&window_type, 1);
1202
1203     mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
1204     mwm_hints.decorations = get_mwm_decorations( data, style, ex_style );
1205     mwm_hints.functions = MWM_FUNC_MOVE;
1206     if (is_window_resizable( data, style )) mwm_hints.functions |= MWM_FUNC_RESIZE;
1207     if (!(style & WS_DISABLED))
1208     {
1209         if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
1210         if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
1211         if (style & WS_SYSMENU)     mwm_hints.functions |= MWM_FUNC_CLOSE;
1212     }
1213
1214     XChangeProperty( display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
1215                      x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
1216                      (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
1217
1218     /* wm hints */
1219     if (data->wm_hints)
1220     {
1221         data->wm_hints->flags |= InputHint | StateHint | WindowGroupHint;
1222         data->wm_hints->input = !use_take_focus && !(style & WS_DISABLED);
1223         data->wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
1224         data->wm_hints->window_group = group_leader;
1225         XSetWMHints( display, data->whole_window, data->wm_hints );
1226     }
1227
1228     wine_tsx11_unlock();
1229 }
1230
1231
1232 /***********************************************************************
1233  *     update_user_time
1234  */
1235 void update_user_time( Time time )
1236 {
1237     wine_tsx11_lock();
1238     if (!user_time_window)
1239     {
1240         user_time_window = XCreateWindow( gdi_display, root_window, -1, -1, 1, 1, 0, 0, InputOnly,
1241                                           DefaultVisual(gdi_display,DefaultScreen(gdi_display)), 0, NULL );
1242         TRACE( "user time window %lx\n", user_time_window );
1243     }
1244     if (time && (!last_user_time || (long)(time - last_user_time) > 0))
1245     {
1246         last_user_time = time;
1247         XChangeProperty( gdi_display, user_time_window, x11drv_atom(_NET_WM_USER_TIME),
1248                          XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&time, 1 );
1249     }
1250     wine_tsx11_unlock();
1251 }
1252
1253 /***********************************************************************
1254  *     update_net_wm_states
1255  */
1256 void update_net_wm_states( Display *display, struct x11drv_win_data *data )
1257 {
1258     static const unsigned int state_atoms[NB_NET_WM_STATES] =
1259     {
1260         XATOM__NET_WM_STATE_FULLSCREEN,
1261         XATOM__NET_WM_STATE_ABOVE,
1262         XATOM__NET_WM_STATE_MAXIMIZED_VERT,
1263         XATOM__NET_WM_STATE_SKIP_PAGER,
1264         XATOM__NET_WM_STATE_SKIP_TASKBAR
1265     };
1266
1267     DWORD i, style, ex_style, new_state = 0;
1268
1269     if (!data->managed) return;
1270     if (data->whole_window == root_window) return;
1271
1272     style = GetWindowLongW( data->hwnd, GWL_STYLE );
1273     if (data->whole_rect.left <= 0 && data->whole_rect.right >= screen_width &&
1274         data->whole_rect.top <= 0 && data->whole_rect.bottom >= screen_height)
1275     {
1276         if ((style & WS_MAXIMIZE) && (style & WS_CAPTION) == WS_CAPTION)
1277             new_state |= (1 << NET_WM_STATE_MAXIMIZED);
1278         else if (!(style & WS_MINIMIZE))
1279             new_state |= (1 << NET_WM_STATE_FULLSCREEN);
1280     }
1281     else if (style & WS_MAXIMIZE)
1282         new_state |= (1 << NET_WM_STATE_MAXIMIZED);
1283
1284     ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
1285     if (ex_style & WS_EX_TOPMOST)
1286         new_state |= (1 << NET_WM_STATE_ABOVE);
1287     if (ex_style & (WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE))
1288         new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR) | (1 << NET_WM_STATE_SKIP_PAGER);
1289     if (!(ex_style & WS_EX_APPWINDOW) && GetWindow( data->hwnd, GW_OWNER ))
1290         new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR);
1291
1292     if (!data->mapped)  /* set the _NET_WM_STATE atom directly */
1293     {
1294         Atom atoms[NB_NET_WM_STATES+1];
1295         DWORD count;
1296
1297         for (i = count = 0; i < NB_NET_WM_STATES; i++)
1298         {
1299             if (!(new_state & (1 << i))) continue;
1300             TRACE( "setting wm state %u for unmapped window %p/%lx\n",
1301                    i, data->hwnd, data->whole_window );
1302             atoms[count++] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
1303             if (state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT)
1304                 atoms[count++] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ);
1305         }
1306         wine_tsx11_lock();
1307         XChangeProperty( display, data->whole_window, x11drv_atom(_NET_WM_STATE), XA_ATOM,
1308                          32, PropModeReplace, (unsigned char *)atoms, count );
1309         wine_tsx11_unlock();
1310     }
1311     else  /* ask the window manager to do it for us */
1312     {
1313         XEvent xev;
1314
1315         xev.xclient.type = ClientMessage;
1316         xev.xclient.window = data->whole_window;
1317         xev.xclient.message_type = x11drv_atom(_NET_WM_STATE);
1318         xev.xclient.serial = 0;
1319         xev.xclient.display = display;
1320         xev.xclient.send_event = True;
1321         xev.xclient.format = 32;
1322         xev.xclient.data.l[3] = 1;
1323
1324         for (i = 0; i < NB_NET_WM_STATES; i++)
1325         {
1326             if (!((data->net_wm_state ^ new_state) & (1 << i))) continue;  /* unchanged */
1327
1328             TRACE( "setting wm state %u for window %p/%lx to %u prev %u\n",
1329                    i, data->hwnd, data->whole_window,
1330                    (new_state & (1 << i)) != 0, (data->net_wm_state & (1 << i)) != 0 );
1331
1332             xev.xclient.data.l[0] = (new_state & (1 << i)) ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
1333             xev.xclient.data.l[1] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
1334             xev.xclient.data.l[2] = ((state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT) ?
1335                                      x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ) : 0);
1336             wine_tsx11_lock();
1337             XSendEvent( display, root_window, False,
1338                         SubstructureRedirectMask | SubstructureNotifyMask, &xev );
1339             wine_tsx11_unlock();
1340         }
1341     }
1342     data->net_wm_state = new_state;
1343 }
1344
1345
1346 /***********************************************************************
1347  *     set_xembed_flags
1348  */
1349 static void set_xembed_flags( Display *display, struct x11drv_win_data *data, unsigned long flags )
1350 {
1351     unsigned long info[2];
1352
1353     info[0] = 0; /* protocol version */
1354     info[1] = flags;
1355     wine_tsx11_lock();
1356     XChangeProperty( display, data->whole_window, x11drv_atom(_XEMBED_INFO),
1357                      x11drv_atom(_XEMBED_INFO), 32, PropModeReplace, (unsigned char*)info, 2 );
1358     wine_tsx11_unlock();
1359 }
1360
1361
1362 /***********************************************************************
1363  *     map_window
1364  */
1365 static void map_window( Display *display, struct x11drv_win_data *data, DWORD new_style )
1366 {
1367     TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1368
1369     remove_startup_notification( display, data->whole_window );
1370
1371     wait_for_withdrawn_state( display, data, TRUE );
1372
1373     if (!data->embedded)
1374     {
1375         update_net_wm_states( display, data );
1376         sync_window_style( display, data );
1377         wine_tsx11_lock();
1378         XMapWindow( display, data->whole_window );
1379         wine_tsx11_unlock();
1380     }
1381     else set_xembed_flags( display, data, XEMBED_MAPPED );
1382
1383     data->mapped = TRUE;
1384     data->iconic = (new_style & WS_MINIMIZE) != 0;
1385 }
1386
1387
1388 /***********************************************************************
1389  *     unmap_window
1390  */
1391 static void unmap_window( Display *display, struct x11drv_win_data *data )
1392 {
1393     TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1394
1395     if (!data->embedded)
1396     {
1397         wait_for_withdrawn_state( display, data, FALSE );
1398         wine_tsx11_lock();
1399         if (data->managed) XWithdrawWindow( display, data->whole_window, DefaultScreen(display) );
1400         else XUnmapWindow( display, data->whole_window );
1401         wine_tsx11_unlock();
1402     }
1403     else set_xembed_flags( display, data, 0 );
1404
1405     data->mapped = FALSE;
1406     data->net_wm_state = 0;
1407 }
1408
1409
1410 /***********************************************************************
1411  *     make_window_embedded
1412  */
1413 void make_window_embedded( Display *display, struct x11drv_win_data *data )
1414 {
1415     if (data->mapped)
1416     {
1417         /* the window cannot be mapped before being embedded */
1418         unmap_window( display, data );
1419         data->embedded = TRUE;
1420         map_window( display, data, 0 );
1421     }
1422     else
1423     {
1424         data->embedded = TRUE;
1425         set_xembed_flags( display, data, 0 );
1426     }
1427 }
1428
1429
1430 /***********************************************************************
1431  *              X11DRV_window_to_X_rect
1432  *
1433  * Convert a rect from client to X window coordinates
1434  */
1435 static void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
1436 {
1437     RECT rc;
1438
1439     if (!data->managed) return;
1440     if (IsRectEmpty( rect )) return;
1441
1442     get_x11_rect_offset( data, &rc );
1443
1444     rect->left   -= rc.left;
1445     rect->right  -= rc.right;
1446     rect->top    -= rc.top;
1447     rect->bottom -= rc.bottom;
1448     if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1449     if (rect->left >= rect->right) rect->right = rect->left + 1;
1450 }
1451
1452
1453 /***********************************************************************
1454  *              X11DRV_X_to_window_rect
1455  *
1456  * Opposite of X11DRV_window_to_X_rect
1457  */
1458 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
1459 {
1460     RECT rc;
1461
1462     if (!data->managed) return;
1463     if (IsRectEmpty( rect )) return;
1464
1465     get_x11_rect_offset( data, &rc );
1466
1467     rect->left   += rc.left;
1468     rect->right  += rc.right;
1469     rect->top    += rc.top;
1470     rect->bottom += rc.bottom;
1471     if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1472     if (rect->left >= rect->right) rect->right = rect->left + 1;
1473 }
1474
1475
1476 /***********************************************************************
1477  *              sync_window_position
1478  *
1479  * Synchronize the X window position with the Windows one
1480  */
1481 static void sync_window_position( Display *display, struct x11drv_win_data *data,
1482                                   UINT swp_flags, const RECT *old_window_rect,
1483                                   const RECT *old_whole_rect, const RECT *old_client_rect )
1484 {
1485     DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
1486     XWindowChanges changes;
1487     unsigned int mask = 0;
1488
1489     if (data->managed && data->iconic) return;
1490
1491     /* resizing a managed maximized window is not allowed */
1492     if (!(style & WS_MAXIMIZE) || !data->managed)
1493     {
1494         changes.width = data->whole_rect.right - data->whole_rect.left;
1495         changes.height = data->whole_rect.bottom - data->whole_rect.top;
1496         /* if window rect is empty force size to 1x1 */
1497         if (changes.width <= 0 || changes.height <= 0) changes.width = changes.height = 1;
1498         if (changes.width > 65535) changes.width = 65535;
1499         if (changes.height > 65535) changes.height = 65535;
1500         mask |= CWWidth | CWHeight;
1501     }
1502
1503     /* only the size is allowed to change for the desktop window */
1504     if (data->whole_window != root_window)
1505     {
1506         changes.x = data->whole_rect.left - virtual_screen_rect.left;
1507         changes.y = data->whole_rect.top - virtual_screen_rect.top;
1508         mask |= CWX | CWY;
1509     }
1510
1511     if (!(swp_flags & SWP_NOZORDER) || (swp_flags & SWP_SHOWWINDOW))
1512     {
1513         /* find window that this one must be after */
1514         HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
1515         while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
1516             prev = GetWindow( prev, GW_HWNDPREV );
1517         if (!prev)  /* top child */
1518         {
1519             changes.stack_mode = Above;
1520             mask |= CWStackMode;
1521         }
1522         /* should use stack_mode Below but most window managers don't get it right */
1523         /* and Above with a sibling doesn't work so well either, so we ignore it */
1524     }
1525
1526     wine_tsx11_lock();
1527     set_size_hints( display, data, style );
1528     data->configure_serial = NextRequest( display );
1529     XReconfigureWMWindow( display, data->whole_window,
1530                           DefaultScreen(display), mask, &changes );
1531 #ifdef HAVE_LIBXSHAPE
1532     if (IsRectEmpty( old_window_rect ) != IsRectEmpty( &data->window_rect ))
1533         sync_window_region( display, data, (HRGN)1 );
1534     if (data->shaped)
1535     {
1536         int old_x_offset = old_window_rect->left - old_whole_rect->left;
1537         int old_y_offset = old_window_rect->top - old_whole_rect->top;
1538         int new_x_offset = data->window_rect.left - data->whole_rect.left;
1539         int new_y_offset = data->window_rect.top - data->whole_rect.top;
1540         if (old_x_offset != new_x_offset || old_y_offset != new_y_offset)
1541             XShapeOffsetShape( display, data->whole_window, ShapeBounding,
1542                                new_x_offset - old_x_offset, new_y_offset - old_y_offset );
1543     }
1544 #endif
1545     wine_tsx11_unlock();
1546
1547     TRACE( "win %p/%lx pos %d,%d,%dx%d after %lx changes=%x serial=%lu\n",
1548            data->hwnd, data->whole_window, data->whole_rect.left, data->whole_rect.top,
1549            data->whole_rect.right - data->whole_rect.left,
1550            data->whole_rect.bottom - data->whole_rect.top,
1551            changes.sibling, mask, data->configure_serial );
1552 }
1553
1554
1555 /***********************************************************************
1556  *              sync_client_position
1557  *
1558  * Synchronize the X client window position with the Windows one
1559  */
1560 static void sync_client_position( Display *display, struct x11drv_win_data *data,
1561                                   UINT swp_flags, const RECT *old_client_rect,
1562                                   const RECT *old_whole_rect )
1563 {
1564     int mask;
1565     XWindowChanges changes;
1566     RECT old = *old_client_rect;
1567     RECT new = data->client_rect;
1568
1569     OffsetRect( &old, -old_whole_rect->left, -old_whole_rect->top );
1570     OffsetRect( &new, -data->whole_rect.left, -data->whole_rect.top );
1571     if (!(mask = get_window_changes( &changes, &old, &new ))) return;
1572
1573     if (data->client_window)
1574     {
1575         TRACE( "setting client win %lx pos %d,%d,%dx%d changes=%x\n",
1576                data->client_window, new.left, new.top,
1577                new.right - new.left, new.bottom - new.top, mask );
1578         wine_tsx11_lock();
1579         XConfigureWindow( display, data->client_window, mask, &changes );
1580         wine_tsx11_unlock();
1581     }
1582
1583     if (data->gl_drawable && (mask & (CWWidth|CWHeight))) sync_gl_drawable( data );
1584 }
1585
1586
1587 /***********************************************************************
1588  *              move_window_bits
1589  *
1590  * Move the window bits when a window is moved.
1591  */
1592 static void move_window_bits( struct x11drv_win_data *data, const RECT *old_rect, const RECT *new_rect,
1593                               const RECT *old_client_rect )
1594 {
1595     RECT src_rect = *old_rect;
1596     RECT dst_rect = *new_rect;
1597     HDC hdc_src, hdc_dst;
1598     INT code;
1599     HRGN rgn = 0;
1600     HWND parent = 0;
1601
1602     if (!data->whole_window)
1603     {
1604         OffsetRect( &dst_rect, -data->window_rect.left, -data->window_rect.top );
1605         parent = GetAncestor( data->hwnd, GA_PARENT );
1606         hdc_src = GetDCEx( parent, 0, DCX_CACHE );
1607         hdc_dst = GetDCEx( data->hwnd, 0, DCX_CACHE | DCX_WINDOW );
1608     }
1609     else
1610     {
1611         OffsetRect( &dst_rect, -data->client_rect.left, -data->client_rect.top );
1612         /* make src rect relative to the old position of the window */
1613         OffsetRect( &src_rect, -old_client_rect->left, -old_client_rect->top );
1614         if (dst_rect.left == src_rect.left && dst_rect.top == src_rect.top) return;
1615         hdc_src = hdc_dst = GetDCEx( data->hwnd, 0, DCX_CACHE );
1616     }
1617
1618     code = X11DRV_START_EXPOSURES;
1619     ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
1620
1621     TRACE( "copying bits for win %p/%lx/%lx %s -> %s\n",
1622            data->hwnd, data->whole_window, data->client_window,
1623            wine_dbgstr_rect(&src_rect), wine_dbgstr_rect(&dst_rect) );
1624     BitBlt( hdc_dst, dst_rect.left, dst_rect.top,
1625             dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top,
1626             hdc_src, src_rect.left, src_rect.top, SRCCOPY );
1627
1628     code = X11DRV_END_EXPOSURES;
1629     ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, sizeof(rgn), (LPSTR)&rgn );
1630
1631     ReleaseDC( data->hwnd, hdc_dst );
1632     if (hdc_src != hdc_dst) ReleaseDC( parent, hdc_src );
1633
1634     if (rgn)
1635     {
1636         if (!data->whole_window)
1637         {
1638             /* map region to client rect since we are using DCX_WINDOW */
1639             OffsetRgn( rgn, data->window_rect.left - data->client_rect.left,
1640                        data->window_rect.top - data->client_rect.top );
1641             RedrawWindow( data->hwnd, NULL, rgn,
1642                           RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ALLCHILDREN );
1643         }
1644         else RedrawWindow( data->hwnd, NULL, rgn, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1645         DeleteObject( rgn );
1646     }
1647 }
1648
1649
1650 /**********************************************************************
1651  *              create_whole_window
1652  *
1653  * Create the whole X window for a given window
1654  */
1655 static Window create_whole_window( Display *display, struct x11drv_win_data *data )
1656 {
1657     int cx, cy, mask;
1658     XSetWindowAttributes attr;
1659     WCHAR text[1024];
1660     COLORREF key;
1661     BYTE alpha;
1662     DWORD layered_flags;
1663     HRGN win_rgn;
1664
1665     if (!data->managed && is_window_managed( data->hwnd, SWP_NOACTIVATE, &data->window_rect ))
1666     {
1667         TRACE( "making win %p/%lx managed\n", data->hwnd, data->whole_window );
1668         data->managed = TRUE;
1669         SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1670     }
1671
1672     if ((win_rgn = CreateRectRgn( 0, 0, 0, 0 )) &&
1673         GetWindowRgn( data->hwnd, win_rgn ) == ERROR)
1674     {
1675         DeleteObject( win_rgn );
1676         win_rgn = 0;
1677     }
1678     data->shaped = (win_rgn != 0);
1679
1680     mask = get_window_attributes( display, data, &attr );
1681
1682     data->whole_rect = data->window_rect;
1683     X11DRV_window_to_X_rect( data, &data->whole_rect );
1684     if (!(cx = data->whole_rect.right - data->whole_rect.left)) cx = 1;
1685     else if (cx > 65535) cx = 65535;
1686     if (!(cy = data->whole_rect.bottom - data->whole_rect.top)) cy = 1;
1687     else if (cy > 65535) cy = 65535;
1688
1689     wine_tsx11_lock();
1690     data->whole_window = XCreateWindow( display, root_window,
1691                                         data->whole_rect.left - virtual_screen_rect.left,
1692                                         data->whole_rect.top - virtual_screen_rect.top,
1693                                         cx, cy, 0, screen_depth, InputOutput,
1694                                         visual, mask, &attr );
1695
1696     if (data->whole_window) XSaveContext( display, data->whole_window, winContext, (char *)data->hwnd );
1697     wine_tsx11_unlock();
1698
1699     if (!data->whole_window) goto done;
1700
1701     if (!create_client_window( display, data, NULL ))
1702     {
1703         wine_tsx11_lock();
1704         XDeleteContext( display, data->whole_window, winContext );
1705         XDestroyWindow( display, data->whole_window );
1706         data->whole_window = 0;
1707         wine_tsx11_unlock();
1708         goto done;
1709     }
1710
1711     set_initial_wm_hints( display, data );
1712     set_wm_hints( display, data );
1713
1714     SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
1715
1716     /* set the window text */
1717     if (!InternalGetWindowText( data->hwnd, text, sizeof(text)/sizeof(WCHAR) )) text[0] = 0;
1718     sync_window_text( display, data->whole_window, text );
1719
1720     /* set the window region */
1721     if (win_rgn || IsRectEmpty( &data->window_rect )) sync_window_region( display, data, win_rgn );
1722
1723     /* set the window opacity */
1724     if (!GetLayeredWindowAttributes( data->hwnd, &key, &alpha, &layered_flags )) layered_flags = 0;
1725     sync_window_opacity( display, data->whole_window, key, alpha, layered_flags );
1726
1727     wine_tsx11_lock();
1728     XFlush( display );  /* make sure the window exists before we start painting to it */
1729     wine_tsx11_unlock();
1730
1731     sync_window_cursor( data );
1732 done:
1733     if (win_rgn) DeleteObject( win_rgn );
1734     return data->whole_window;
1735 }
1736
1737
1738 /**********************************************************************
1739  *              destroy_whole_window
1740  *
1741  * Destroy the whole X window for a given window.
1742  */
1743 static void destroy_whole_window( Display *display, struct x11drv_win_data *data, BOOL already_destroyed )
1744 {
1745     if (!data->whole_window) return;
1746
1747     TRACE( "win %p xwin %lx/%lx\n", data->hwnd, data->whole_window, data->client_window );
1748     wine_tsx11_lock();
1749     XDeleteContext( display, data->whole_window, winContext );
1750     XDeleteContext( display, data->client_window, winContext );
1751     if (!already_destroyed) XDestroyWindow( display, data->whole_window );
1752     data->whole_window = data->client_window = 0;
1753     data->wm_state = WithdrawnState;
1754     data->net_wm_state = 0;
1755     data->mapped = FALSE;
1756     if (data->xic)
1757     {
1758         XUnsetICFocus( data->xic );
1759         XDestroyIC( data->xic );
1760         data->xic = 0;
1761     }
1762     /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
1763     XFlush( display );
1764     XFree( data->wm_hints );
1765     data->wm_hints = NULL;
1766     wine_tsx11_unlock();
1767     RemovePropA( data->hwnd, whole_window_prop );
1768     RemovePropA( data->hwnd, client_window_prop );
1769 }
1770
1771
1772 /*****************************************************************
1773  *              SetWindowText   (X11DRV.@)
1774  */
1775 void CDECL X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
1776 {
1777     Window win;
1778
1779     if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(gdi_display))
1780     {
1781         Display *display = thread_init_display();
1782         sync_window_text( display, win, text );
1783     }
1784 }
1785
1786
1787 /***********************************************************************
1788  *              SetWindowStyle   (X11DRV.@)
1789  *
1790  * Update the X state of a window to reflect a style change
1791  */
1792 void CDECL X11DRV_SetWindowStyle( HWND hwnd, INT offset, STYLESTRUCT *style )
1793 {
1794     struct x11drv_win_data *data;
1795     DWORD changed;
1796
1797     if (hwnd == GetDesktopWindow()) return;
1798     changed = style->styleNew ^ style->styleOld;
1799
1800     if (offset == GWL_STYLE && (changed & WS_VISIBLE) && (style->styleNew & WS_VISIBLE))
1801     {
1802         /* we don't unmap windows, that causes trouble with the window manager */
1803         if (!(data = X11DRV_get_win_data( hwnd )) &&
1804             !(data = X11DRV_create_win_data( hwnd ))) return;
1805
1806         if (data->whole_window && is_window_rect_mapped( &data->window_rect ))
1807         {
1808             Display *display = thread_display();
1809             set_wm_hints( display, data );
1810             if (!data->mapped) map_window( display, data, style->styleNew );
1811         }
1812     }
1813
1814     if (offset == GWL_STYLE && (changed & WS_DISABLED))
1815     {
1816         data = X11DRV_get_win_data( hwnd );
1817         if (data && data->whole_window)
1818             set_wm_hints( thread_display(), data );
1819     }
1820
1821     if (offset == GWL_EXSTYLE && (changed & WS_EX_LAYERED))
1822     {
1823         /* changing WS_EX_LAYERED resets attributes */
1824         if ((data = X11DRV_get_win_data( hwnd )) && data->whole_window)
1825             sync_window_opacity( thread_display(), data->whole_window, 0, 0, 0 );
1826     }
1827 }
1828
1829
1830 /***********************************************************************
1831  *              DestroyWindow   (X11DRV.@)
1832  */
1833 void CDECL X11DRV_DestroyWindow( HWND hwnd )
1834 {
1835     struct x11drv_thread_data *thread_data = x11drv_thread_data();
1836     struct x11drv_win_data *data;
1837
1838     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1839
1840     if (data->pixmap)
1841     {
1842         wine_tsx11_lock();
1843         destroy_glxpixmap(gdi_display, data->gl_drawable);
1844         XFreePixmap(gdi_display, data->pixmap);
1845         wine_tsx11_unlock();
1846     }
1847     else if (data->gl_drawable)
1848     {
1849         wine_tsx11_lock();
1850         XDestroyWindow(gdi_display, data->gl_drawable);
1851         wine_tsx11_unlock();
1852     }
1853
1854     destroy_whole_window( thread_data->display, data, FALSE );
1855     destroy_icon_window( thread_data->display, data );
1856
1857     if (data->colormap)
1858     {
1859         wine_tsx11_lock();
1860         XFreeColormap( thread_data->display, data->colormap );
1861         wine_tsx11_unlock();
1862     }
1863
1864     if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
1865     if (thread_data->last_xic_hwnd == hwnd) thread_data->last_xic_hwnd = 0;
1866     if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
1867     if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
1868     wine_tsx11_lock();
1869     XDeleteContext( thread_data->display, (XID)hwnd, win_data_context );
1870     wine_tsx11_unlock();
1871     HeapFree( GetProcessHeap(), 0, data );
1872 }
1873
1874
1875 /***********************************************************************
1876  *              X11DRV_DestroyNotify
1877  */
1878 void X11DRV_DestroyNotify( HWND hwnd, XEvent *event )
1879 {
1880     Display *display = event->xdestroywindow.display;
1881     struct x11drv_win_data *data;
1882
1883     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1884
1885     FIXME( "window %p/%lx destroyed from the outside\n", hwnd, data->whole_window );
1886     destroy_whole_window( display, data, TRUE );
1887 }
1888
1889
1890 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
1891 {
1892     struct x11drv_win_data *data;
1893
1894     if ((data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
1895     {
1896         data->hwnd = hwnd;
1897         wine_tsx11_lock();
1898         if (!winContext) winContext = XUniqueContext();
1899         if (!win_data_context) win_data_context = XUniqueContext();
1900         XSaveContext( display, (XID)hwnd, win_data_context, (char *)data );
1901         wine_tsx11_unlock();
1902     }
1903     return data;
1904 }
1905
1906
1907 /* initialize the desktop window id in the desktop manager process */
1908 static struct x11drv_win_data *create_desktop_win_data( Display *display, HWND hwnd )
1909 {
1910     struct x11drv_win_data *data;
1911
1912     if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1913     data->whole_window = data->client_window = root_window;
1914     data->managed = TRUE;
1915     SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1916     SetPropA( data->hwnd, whole_window_prop, (HANDLE)root_window );
1917     SetPropA( data->hwnd, client_window_prop, (HANDLE)root_window );
1918     set_initial_wm_hints( display, data );
1919     return data;
1920 }
1921
1922 /**********************************************************************
1923  *              CreateDesktopWindow   (X11DRV.@)
1924  */
1925 BOOL CDECL X11DRV_CreateDesktopWindow( HWND hwnd )
1926 {
1927     unsigned int width, height;
1928
1929     /* retrieve the real size of the desktop */
1930     SERVER_START_REQ( get_window_rectangles )
1931     {
1932         req->handle = wine_server_user_handle( hwnd );
1933         req->relative = COORDS_CLIENT;
1934         wine_server_call( req );
1935         width  = reply->window.right;
1936         height = reply->window.bottom;
1937     }
1938     SERVER_END_REQ;
1939
1940     if (!width && !height)  /* not initialized yet */
1941     {
1942         SERVER_START_REQ( set_window_pos )
1943         {
1944             req->handle        = wine_server_user_handle( hwnd );
1945             req->previous      = 0;
1946             req->flags         = SWP_NOZORDER;
1947             req->window.left   = virtual_screen_rect.left;
1948             req->window.top    = virtual_screen_rect.top;
1949             req->window.right  = virtual_screen_rect.right;
1950             req->window.bottom = virtual_screen_rect.bottom;
1951             req->client        = req->window;
1952             wine_server_call( req );
1953         }
1954         SERVER_END_REQ;
1955     }
1956     else
1957     {
1958         Window win = (Window)GetPropA( hwnd, whole_window_prop );
1959         if (win && win != root_window) X11DRV_init_desktop( win, width, height );
1960     }
1961     return TRUE;
1962 }
1963
1964
1965 /**********************************************************************
1966  *              CreateWindow   (X11DRV.@)
1967  */
1968 BOOL CDECL X11DRV_CreateWindow( HWND hwnd )
1969 {
1970     if (hwnd == GetDesktopWindow() && root_window != DefaultRootWindow( gdi_display ))
1971     {
1972         Display *display = thread_init_display();
1973
1974         /* the desktop win data can't be created lazily */
1975         if (!create_desktop_win_data( display, hwnd )) return FALSE;
1976     }
1977     return TRUE;
1978 }
1979
1980
1981 /***********************************************************************
1982  *              X11DRV_get_win_data
1983  *
1984  * Return the X11 data structure associated with a window.
1985  */
1986 struct x11drv_win_data *X11DRV_get_win_data( HWND hwnd )
1987 {
1988     struct x11drv_thread_data *thread_data = x11drv_thread_data();
1989     char *data;
1990
1991     if (!thread_data) return NULL;
1992     if (!hwnd) return NULL;
1993     if (XFindContext( thread_data->display, (XID)hwnd, win_data_context, &data )) data = NULL;
1994     return (struct x11drv_win_data *)data;
1995 }
1996
1997
1998 /***********************************************************************
1999  *              X11DRV_create_win_data
2000  *
2001  * Create an X11 data window structure for an existing window.
2002  */
2003 struct x11drv_win_data *X11DRV_create_win_data( HWND hwnd )
2004 {
2005     Display *display;
2006     struct x11drv_win_data *data;
2007     HWND parent;
2008
2009     if (!(parent = GetAncestor( hwnd, GA_PARENT ))) return NULL;  /* desktop */
2010
2011     /* don't create win data for HWND_MESSAGE windows */
2012     if (parent != GetDesktopWindow() && !GetAncestor( parent, GA_PARENT )) return NULL;
2013
2014     display = thread_init_display();
2015     if (!(data = alloc_win_data( display, hwnd ))) return NULL;
2016
2017     GetWindowRect( hwnd, &data->window_rect );
2018     MapWindowPoints( 0, parent, (POINT *)&data->window_rect, 2 );
2019     data->whole_rect = data->window_rect;
2020     GetClientRect( hwnd, &data->client_rect );
2021     MapWindowPoints( hwnd, parent, (POINT *)&data->client_rect, 2 );
2022
2023     if (parent == GetDesktopWindow())
2024     {
2025         if (!create_whole_window( display, data ))
2026         {
2027             HeapFree( GetProcessHeap(), 0, data );
2028             return NULL;
2029         }
2030         TRACE( "win %p/%lx/%lx window %s whole %s client %s\n",
2031                hwnd, data->whole_window, data->client_window, wine_dbgstr_rect( &data->window_rect ),
2032                wine_dbgstr_rect( &data->whole_rect ), wine_dbgstr_rect( &data->client_rect ));
2033     }
2034     return data;
2035 }
2036
2037
2038 /***********************************************************************
2039  *              X11DRV_get_whole_window
2040  *
2041  * Return the X window associated with the full area of a window
2042  */
2043 Window X11DRV_get_whole_window( HWND hwnd )
2044 {
2045     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2046
2047     if (!data)
2048     {
2049         if (hwnd == GetDesktopWindow()) return root_window;
2050         return (Window)GetPropA( hwnd, whole_window_prop );
2051     }
2052     return data->whole_window;
2053 }
2054
2055
2056 /***********************************************************************
2057  *              X11DRV_get_client_window
2058  *
2059  * Return the X window associated with the client area of a window
2060  */
2061 static Window X11DRV_get_client_window( HWND hwnd )
2062 {
2063     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2064
2065     if (!data)
2066     {
2067         if (hwnd == GetDesktopWindow()) return root_window;
2068         return (Window)GetPropA( hwnd, client_window_prop );
2069     }
2070     return data->client_window;
2071 }
2072
2073
2074 /***********************************************************************
2075  *              X11DRV_get_ic
2076  *
2077  * Return the X input context associated with a window
2078  */
2079 XIC X11DRV_get_ic( HWND hwnd )
2080 {
2081     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2082     XIM xim;
2083
2084     if (!data) return 0;
2085
2086     x11drv_thread_data()->last_xic_hwnd = hwnd;
2087     if (data->xic) return data->xic;
2088     if (!(xim = x11drv_thread_data()->xim)) return 0;
2089     return X11DRV_CreateIC( xim, data );
2090 }
2091
2092
2093 /***********************************************************************
2094  *              X11DRV_GetDC   (X11DRV.@)
2095  */
2096 void CDECL X11DRV_GetDC( HDC hdc, HWND hwnd, HWND top, const RECT *win_rect,
2097                          const RECT *top_rect, DWORD flags )
2098 {
2099     struct x11drv_escape_set_drawable escape;
2100     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2101
2102     escape.code        = X11DRV_SET_DRAWABLE;
2103     escape.mode        = IncludeInferiors;
2104     escape.fbconfig_id = 0;
2105     escape.gl_drawable = 0;
2106     escape.pixmap      = 0;
2107     escape.gl_copy     = FALSE;
2108
2109     if (top == hwnd)
2110     {
2111         escape.fbconfig_id = data ? data->fbconfig_id : (XID)GetPropA( hwnd, fbconfig_id_prop );
2112         /* GL draws to the client area even for window DCs */
2113         escape.gl_drawable = data ? data->client_window : X11DRV_get_client_window( hwnd );
2114         if (data && IsIconic( hwnd ) && data->icon_window)
2115         {
2116             escape.drawable = data->icon_window;
2117         }
2118         else if (flags & DCX_WINDOW)
2119             escape.drawable = data ? data->whole_window : X11DRV_get_whole_window( hwnd );
2120         else
2121             escape.drawable = escape.gl_drawable;
2122     }
2123     else
2124     {
2125         escape.drawable    = X11DRV_get_client_window( top );
2126         escape.fbconfig_id = data ? data->fbconfig_id : (XID)GetPropA( hwnd, fbconfig_id_prop );
2127         escape.gl_drawable = data ? data->gl_drawable : (Drawable)GetPropA( hwnd, gl_drawable_prop );
2128         escape.pixmap      = data ? data->pixmap : (Pixmap)GetPropA( hwnd, pixmap_prop );
2129         escape.gl_copy     = (escape.gl_drawable != 0);
2130         if (flags & DCX_CLIPCHILDREN) escape.mode = ClipByChildren;
2131     }
2132
2133     escape.dc_rect.left         = win_rect->left - top_rect->left;
2134     escape.dc_rect.top          = win_rect->top - top_rect->top;
2135     escape.dc_rect.right        = win_rect->right - top_rect->left;
2136     escape.dc_rect.bottom       = win_rect->bottom - top_rect->top;
2137     escape.drawable_rect.left   = top_rect->left;
2138     escape.drawable_rect.top    = top_rect->top;
2139     escape.drawable_rect.right  = top_rect->right;
2140     escape.drawable_rect.bottom = top_rect->bottom;
2141
2142     ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2143 }
2144
2145
2146 /***********************************************************************
2147  *              X11DRV_ReleaseDC  (X11DRV.@)
2148  */
2149 void CDECL X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
2150 {
2151     struct x11drv_escape_set_drawable escape;
2152
2153     escape.code = X11DRV_SET_DRAWABLE;
2154     escape.drawable = root_window;
2155     escape.mode = IncludeInferiors;
2156     escape.drawable_rect = virtual_screen_rect;
2157     SetRect( &escape.dc_rect, 0, 0, virtual_screen_rect.right - virtual_screen_rect.left,
2158              virtual_screen_rect.bottom - virtual_screen_rect.top );
2159     OffsetRect( &escape.dc_rect, -escape.drawable_rect.left, -escape.drawable_rect.top );
2160     escape.fbconfig_id = 0;
2161     escape.gl_drawable = 0;
2162     escape.pixmap = 0;
2163     ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2164 }
2165
2166
2167 /***********************************************************************
2168  *              SetCapture  (X11DRV.@)
2169  */
2170 void CDECL X11DRV_SetCapture( HWND hwnd, UINT flags )
2171 {
2172     struct x11drv_thread_data *thread_data = x11drv_thread_data();
2173
2174     if (!thread_data) return;
2175     if (!(flags & (GUI_INMOVESIZE | GUI_INMENUMODE))) return;
2176
2177     if (hwnd)
2178     {
2179         Window grab_win = X11DRV_get_client_window( GetAncestor( hwnd, GA_ROOT ) );
2180
2181         if (!grab_win) return;
2182         wine_tsx11_lock();
2183         XFlush( gdi_display );
2184         XGrabPointer( thread_data->display, grab_win, False,
2185                       PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
2186                       GrabModeAsync, GrabModeAsync, None, None, CurrentTime );
2187         wine_tsx11_unlock();
2188         thread_data->grab_window = grab_win;
2189     }
2190     else  /* release capture */
2191     {
2192         wine_tsx11_lock();
2193         XFlush( gdi_display );
2194         XUngrabPointer( thread_data->display, CurrentTime );
2195         XFlush( thread_data->display );
2196         wine_tsx11_unlock();
2197         thread_data->grab_window = None;
2198     }
2199 }
2200
2201
2202 /*****************************************************************
2203  *              SetParent   (X11DRV.@)
2204  */
2205 void CDECL X11DRV_SetParent( HWND hwnd, HWND parent, HWND old_parent )
2206 {
2207     Display *display = thread_display();
2208     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2209
2210     if (!data) return;
2211     if (parent == old_parent) return;
2212
2213     if (parent != GetDesktopWindow()) /* a child window */
2214     {
2215         if (old_parent == GetDesktopWindow())
2216         {
2217             /* destroy the old X windows */
2218             destroy_whole_window( display, data, FALSE );
2219             destroy_icon_window( display, data );
2220             if (data->managed)
2221             {
2222                 data->managed = FALSE;
2223                 RemovePropA( data->hwnd, managed_prop );
2224             }
2225         }
2226     }
2227     else  /* new top level window */
2228     {
2229         /* FIXME: we ignore errors since we can't really recover anyway */
2230         create_whole_window( display, data );
2231     }
2232 }
2233
2234
2235 /*****************************************************************
2236  *              SetFocus   (X11DRV.@)
2237  *
2238  * Set the X focus.
2239  */
2240 void CDECL X11DRV_SetFocus( HWND hwnd )
2241 {
2242     Display *display = thread_display();
2243     struct x11drv_win_data *data;
2244     XWindowChanges changes;
2245     DWORD timestamp;
2246
2247     if (!(hwnd = GetAncestor( hwnd, GA_ROOT ))) return;
2248     if (!(data = X11DRV_get_win_data( hwnd ))) return;
2249     if (data->managed || !data->whole_window) return;
2250
2251     if (EVENT_x11_time_to_win32_time(0))
2252         /* ICCCM says don't use CurrentTime, so try to use last message time if possible */
2253         /* FIXME: this is not entirely correct */
2254         timestamp = GetMessageTime() - EVENT_x11_time_to_win32_time(0);
2255     else
2256         timestamp = CurrentTime;
2257
2258     /* Set X focus and install colormap */
2259     wine_tsx11_lock();
2260     changes.stack_mode = Above;
2261     XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
2262     XSetInputFocus( display, data->whole_window, RevertToParent, timestamp );
2263     wine_tsx11_unlock();
2264 }
2265
2266
2267 /***********************************************************************
2268  *              WindowPosChanging   (X11DRV.@)
2269  */
2270 void CDECL X11DRV_WindowPosChanging( HWND hwnd, HWND insert_after, UINT swp_flags,
2271                                      const RECT *window_rect, const RECT *client_rect, RECT *visible_rect )
2272 {
2273     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2274     DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
2275
2276     if (!data)
2277     {
2278         /* create the win data if the window is being made visible */
2279         if (!(style & WS_VISIBLE) && !(swp_flags & SWP_SHOWWINDOW)) return;
2280         if (!(data = X11DRV_create_win_data( hwnd ))) return;
2281     }
2282
2283     /* check if we need to switch the window to managed */
2284     if (!data->managed && data->whole_window && is_window_managed( hwnd, swp_flags, window_rect ))
2285     {
2286         TRACE( "making win %p/%lx managed\n", hwnd, data->whole_window );
2287         if (data->mapped) unmap_window( thread_display(), data );
2288         data->managed = TRUE;
2289         SetPropA( hwnd, managed_prop, (HANDLE)1 );
2290     }
2291
2292     *visible_rect = *window_rect;
2293     X11DRV_window_to_X_rect( data, visible_rect );
2294 }
2295
2296
2297 /***********************************************************************
2298  *              WindowPosChanged   (X11DRV.@)
2299  */
2300 void CDECL X11DRV_WindowPosChanged( HWND hwnd, HWND insert_after, UINT swp_flags,
2301                                     const RECT *rectWindow, const RECT *rectClient,
2302                                     const RECT *visible_rect, const RECT *valid_rects )
2303 {
2304     struct x11drv_thread_data *thread_data;
2305     Display *display;
2306     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2307     DWORD new_style = GetWindowLongW( hwnd, GWL_STYLE );
2308     RECT old_window_rect, old_whole_rect, old_client_rect;
2309     int event_type;
2310
2311     if (!data) return;
2312
2313     thread_data = x11drv_thread_data();
2314     display = thread_data->display;
2315
2316     old_window_rect = data->window_rect;
2317     old_whole_rect  = data->whole_rect;
2318     old_client_rect = data->client_rect;
2319     data->window_rect = *rectWindow;
2320     data->whole_rect  = *visible_rect;
2321     data->client_rect = *rectClient;
2322
2323     TRACE( "win %p window %s client %s style %08x flags %08x\n",
2324            hwnd, wine_dbgstr_rect(rectWindow), wine_dbgstr_rect(rectClient), new_style, swp_flags );
2325
2326     if (!IsRectEmpty( &valid_rects[0] ))
2327     {
2328         int x_offset = old_whole_rect.left - data->whole_rect.left;
2329         int y_offset = old_whole_rect.top - data->whole_rect.top;
2330
2331         /* if all that happened is that the whole window moved, copy everything */
2332         if (!(swp_flags & SWP_FRAMECHANGED) &&
2333             old_whole_rect.right   - data->whole_rect.right   == x_offset &&
2334             old_whole_rect.bottom  - data->whole_rect.bottom  == y_offset &&
2335             old_client_rect.left   - data->client_rect.left   == x_offset &&
2336             old_client_rect.right  - data->client_rect.right  == x_offset &&
2337             old_client_rect.top    - data->client_rect.top    == y_offset &&
2338             old_client_rect.bottom - data->client_rect.bottom == y_offset &&
2339             !memcmp( &valid_rects[0], &data->client_rect, sizeof(RECT) ))
2340         {
2341             /* if we have an X window the bits will be moved by the X server */
2342             if (!data->whole_window && (x_offset != 0 || y_offset != 0))
2343                 move_window_bits( data, &old_whole_rect, &data->whole_rect, &old_client_rect );
2344         }
2345         else
2346             move_window_bits( data, &valid_rects[1], &valid_rects[0], &old_client_rect );
2347     }
2348
2349     wine_tsx11_lock();
2350     XFlush( gdi_display );  /* make sure painting is done before we move the window */
2351     wine_tsx11_unlock();
2352
2353     sync_client_position( display, data, swp_flags, &old_client_rect, &old_whole_rect );
2354
2355     if (!data->whole_window) return;
2356
2357     /* check if we are currently processing an event relevant to this window */
2358     event_type = 0;
2359     if (thread_data->current_event && thread_data->current_event->xany.window == data->whole_window)
2360         event_type = thread_data->current_event->type;
2361
2362     if (event_type != ConfigureNotify && event_type != PropertyNotify)
2363         event_type = 0;  /* ignore other events */
2364
2365     if (data->mapped)
2366     {
2367         if (((swp_flags & SWP_HIDEWINDOW) && !(new_style & WS_VISIBLE)) ||
2368             (event_type != ConfigureNotify &&
2369              !is_window_rect_mapped( rectWindow ) && is_window_rect_mapped( &old_window_rect )))
2370             unmap_window( display, data );
2371     }
2372
2373     /* don't change position if we are about to minimize or maximize a managed window */
2374     if (!event_type &&
2375         !(data->managed && (swp_flags & SWP_STATECHANGED) && (new_style & (WS_MINIMIZE|WS_MAXIMIZE))))
2376         sync_window_position( display, data, swp_flags,
2377                               &old_window_rect, &old_whole_rect, &old_client_rect );
2378
2379     if ((new_style & WS_VISIBLE) &&
2380         ((new_style & WS_MINIMIZE) || is_window_rect_mapped( rectWindow )))
2381     {
2382         if (!data->mapped || (swp_flags & (SWP_FRAMECHANGED|SWP_STATECHANGED)))
2383             set_wm_hints( display, data );
2384
2385         if (!data->mapped)
2386         {
2387             map_window( display, data, new_style );
2388         }
2389         else if ((swp_flags & SWP_STATECHANGED) && (!data->iconic != !(new_style & WS_MINIMIZE)))
2390         {
2391             data->iconic = (new_style & WS_MINIMIZE) != 0;
2392             TRACE( "changing win %p iconic state to %u\n", data->hwnd, data->iconic );
2393             wine_tsx11_lock();
2394             if (data->iconic)
2395                 XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
2396             else if (is_window_rect_mapped( rectWindow ))
2397                 XMapWindow( display, data->whole_window );
2398             wine_tsx11_unlock();
2399             update_net_wm_states( display, data );
2400         }
2401         else if (!event_type)
2402         {
2403             update_net_wm_states( display, data );
2404         }
2405     }
2406
2407     wine_tsx11_lock();
2408     XFlush( display );  /* make sure changes are done before we start painting again */
2409     wine_tsx11_unlock();
2410 }
2411
2412
2413 /***********************************************************************
2414  *           ShowWindow   (X11DRV.@)
2415  */
2416 UINT CDECL X11DRV_ShowWindow( HWND hwnd, INT cmd, RECT *rect, UINT swp )
2417 {
2418     int x, y;
2419     unsigned int width, height, border, depth;
2420     Window root, top;
2421     DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
2422     struct x11drv_thread_data *thread_data = x11drv_thread_data();
2423     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2424
2425     if (!data || !data->whole_window || !data->managed || !data->mapped || data->iconic) return swp;
2426     if (style & WS_MINIMIZE) return swp;
2427     if (IsRectEmpty( rect )) return swp;
2428
2429     /* only fetch the new rectangle if the ShowWindow was a result of a window manager event */
2430
2431     if (!thread_data->current_event || thread_data->current_event->xany.window != data->whole_window)
2432         return swp;
2433
2434     if (thread_data->current_event->type != ConfigureNotify &&
2435         thread_data->current_event->type != PropertyNotify)
2436         return swp;
2437
2438     TRACE( "win %p/%lx cmd %d at %s flags %08x\n",
2439            hwnd, data->whole_window, cmd, wine_dbgstr_rect(rect), swp );
2440
2441     wine_tsx11_lock();
2442     XGetGeometry( thread_data->display, data->whole_window,
2443                   &root, &x, &y, &width, &height, &border, &depth );
2444     XTranslateCoordinates( thread_data->display, data->whole_window, root, 0, 0, &x, &y, &top );
2445     wine_tsx11_unlock();
2446     rect->left   = x;
2447     rect->top    = y;
2448     rect->right  = x + width;
2449     rect->bottom = y + height;
2450     OffsetRect( rect, virtual_screen_rect.left, virtual_screen_rect.top );
2451     X11DRV_X_to_window_rect( data, rect );
2452     return swp & ~(SWP_NOMOVE | SWP_NOCLIENTMOVE | SWP_NOSIZE | SWP_NOCLIENTSIZE);
2453 }
2454
2455
2456 /**********************************************************************
2457  *              SetWindowIcon (X11DRV.@)
2458  *
2459  * hIcon or hIconSm has changed (or is being initialised for the
2460  * first time). Complete the X11 driver-specific initialisation
2461  * and set the window hints.
2462  *
2463  * This is not entirely correct, may need to create
2464  * an icon window and set the pixmap as a background
2465  */
2466 void CDECL X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
2467 {
2468     Display *display = thread_display();
2469     struct x11drv_win_data *data;
2470
2471
2472     if (!(data = X11DRV_get_win_data( hwnd ))) return;
2473     if (!data->whole_window) return;
2474     if (!data->managed) return;
2475
2476     if (data->wm_hints)
2477     {
2478         if (type == ICON_BIG) set_icon_hints( display, data, icon, 0 );
2479         else set_icon_hints( display, data, 0, icon );
2480         wine_tsx11_lock();
2481         XSetWMHints( display, data->whole_window, data->wm_hints );
2482         wine_tsx11_unlock();
2483     }
2484 }
2485
2486
2487 /***********************************************************************
2488  *              SetWindowRgn  (X11DRV.@)
2489  *
2490  * Assign specified region to window (for non-rectangular windows)
2491  */
2492 int CDECL X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
2493 {
2494     struct x11drv_win_data *data;
2495
2496     if ((data = X11DRV_get_win_data( hwnd )))
2497     {
2498         sync_window_region( thread_display(), data, hrgn );
2499     }
2500     else if (X11DRV_get_whole_window( hwnd ))
2501     {
2502         SendMessageW( hwnd, WM_X11DRV_SET_WIN_REGION, 0, 0 );
2503     }
2504     return TRUE;
2505 }
2506
2507
2508 /***********************************************************************
2509  *              SetLayeredWindowAttributes  (X11DRV.@)
2510  *
2511  * Set transparency attributes for a layered window.
2512  */
2513 void CDECL X11DRV_SetLayeredWindowAttributes( HWND hwnd, COLORREF key, BYTE alpha, DWORD flags )
2514 {
2515     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2516
2517     if (data)
2518     {
2519         if (data->whole_window)
2520             sync_window_opacity( thread_display(), data->whole_window, key, alpha, flags );
2521     }
2522     else
2523     {
2524         Window win = X11DRV_get_whole_window( hwnd );
2525         if (win) sync_window_opacity( gdi_display, win, key, alpha, flags );
2526     }
2527 }
2528
2529
2530 /**********************************************************************
2531  *           X11DRV_WindowMessage   (X11DRV.@)
2532  */
2533 LRESULT CDECL X11DRV_WindowMessage( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
2534 {
2535     struct x11drv_win_data *data;
2536
2537     switch(msg)
2538     {
2539     case WM_X11DRV_ACQUIRE_SELECTION:
2540         return X11DRV_AcquireClipboard( hwnd );
2541     case WM_X11DRV_SET_WIN_FORMAT:
2542         return set_win_format( hwnd, (XID)wp );
2543     case WM_X11DRV_SET_WIN_REGION:
2544         if ((data = X11DRV_get_win_data( hwnd ))) sync_window_region( thread_display(), data, (HRGN)1 );
2545         return 0;
2546     case WM_X11DRV_RESIZE_DESKTOP:
2547         X11DRV_resize_desktop( LOWORD(lp), HIWORD(lp) );
2548         return 0;
2549     case WM_X11DRV_SET_CURSOR:
2550         set_window_cursor( hwnd, (HCURSOR)lp );
2551         return 0;
2552     default:
2553         FIXME( "got window msg %x hwnd %p wp %lx lp %lx\n", msg, hwnd, wp, lp );
2554         return 0;
2555     }
2556 }
2557
2558
2559 /***********************************************************************
2560  *              is_netwm_supported
2561  */
2562 static BOOL is_netwm_supported( Display *display, Atom atom )
2563 {
2564     static Atom *net_supported;
2565     static int net_supported_count = -1;
2566     int i;
2567
2568     wine_tsx11_lock();
2569     if (net_supported_count == -1)
2570     {
2571         Atom type;
2572         int format;
2573         unsigned long count, remaining;
2574
2575         if (!XGetWindowProperty( display, DefaultRootWindow(display), x11drv_atom(_NET_SUPPORTED), 0,
2576                                  ~0UL, False, XA_ATOM, &type, &format, &count,
2577                                  &remaining, (unsigned char **)&net_supported ))
2578             net_supported_count = get_property_size( format, count ) / sizeof(Atom);
2579         else
2580             net_supported_count = 0;
2581     }
2582     wine_tsx11_unlock();
2583
2584     for (i = 0; i < net_supported_count; i++)
2585         if (net_supported[i] == atom) return TRUE;
2586     return FALSE;
2587 }
2588
2589
2590 /***********************************************************************
2591  *           SysCommand   (X11DRV.@)
2592  *
2593  * Perform WM_SYSCOMMAND handling.
2594  */
2595 LRESULT CDECL X11DRV_SysCommand( HWND hwnd, WPARAM wparam, LPARAM lparam )
2596 {
2597     WPARAM hittest = wparam & 0x0f;
2598     DWORD dwPoint;
2599     int x, y, dir;
2600     XEvent xev;
2601     Display *display = thread_display();
2602     struct x11drv_win_data *data;
2603
2604     if (!(data = X11DRV_get_win_data( hwnd ))) return -1;
2605     if (!data->whole_window || !data->managed || !data->mapped) return -1;
2606
2607     switch (wparam & 0xfff0)
2608     {
2609     case SC_MOVE:
2610         if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
2611         else dir = _NET_WM_MOVERESIZE_MOVE;
2612         break;
2613     case SC_SIZE:
2614         /* windows without WS_THICKFRAME are not resizable through the window manager */
2615         if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_THICKFRAME)) return -1;
2616
2617         switch (hittest)
2618         {
2619         case WMSZ_LEFT:        dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
2620         case WMSZ_RIGHT:       dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
2621         case WMSZ_TOP:         dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
2622         case WMSZ_TOPLEFT:     dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
2623         case WMSZ_TOPRIGHT:    dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
2624         case WMSZ_BOTTOM:      dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
2625         case WMSZ_BOTTOMLEFT:  dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
2626         case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
2627         default:               dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD; break;
2628         }
2629         break;
2630
2631     case SC_KEYMENU:
2632         /* prevent a simple ALT press+release from activating the system menu,
2633          * as that can get confusing on managed windows */
2634         if ((WCHAR)lparam) return -1;  /* got an explicit char */
2635         if (GetMenu( hwnd )) return -1;  /* window has a real menu */
2636         if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_SYSMENU)) return -1;  /* no system menu */
2637         TRACE( "ignoring SC_KEYMENU wp %lx lp %lx\n", wparam, lparam );
2638         return 0;
2639
2640     default:
2641         return -1;
2642     }
2643
2644     if (IsZoomed(hwnd)) return -1;
2645
2646     if (!is_netwm_supported( display, x11drv_atom(_NET_WM_MOVERESIZE) ))
2647     {
2648         TRACE( "_NET_WM_MOVERESIZE not supported\n" );
2649         return -1;
2650     }
2651
2652     dwPoint = GetMessagePos();
2653     x = (short)LOWORD(dwPoint);
2654     y = (short)HIWORD(dwPoint);
2655
2656     TRACE("hwnd %p, x %d, y %d, dir %d\n", hwnd, x, y, dir);
2657
2658     xev.xclient.type = ClientMessage;
2659     xev.xclient.window = X11DRV_get_whole_window(hwnd);
2660     xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
2661     xev.xclient.serial = 0;
2662     xev.xclient.display = display;
2663     xev.xclient.send_event = True;
2664     xev.xclient.format = 32;
2665     xev.xclient.data.l[0] = x - virtual_screen_rect.left; /* x coord */
2666     xev.xclient.data.l[1] = y - virtual_screen_rect.top;  /* y coord */
2667     xev.xclient.data.l[2] = dir; /* direction */
2668     xev.xclient.data.l[3] = 1; /* button */
2669     xev.xclient.data.l[4] = 0; /* unused */
2670
2671     /* need to ungrab the pointer that may have been automatically grabbed
2672      * with a ButtonPress event */
2673     wine_tsx11_lock();
2674     XUngrabPointer( display, CurrentTime );
2675     XSendEvent(display, root_window, False, SubstructureNotifyMask | SubstructureRedirectMask, &xev);
2676     wine_tsx11_unlock();
2677     return 0;
2678 }