wined3d: Get rid of the "render_to_fbo" field from the wined3d_swapchain structure.
[wine.git] / dlls / winex11.drv / window.c
blob4e856a418654971e3f72e5f252da81f902907d01
1 /*
2 * Window related functions
4 * Copyright 1993, 1994, 1995, 1996, 2001 Alexandre Julliard
5 * Copyright 1993 David Metcalfe
6 * Copyright 1995, 1996 Alex Korobka
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.
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.
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
23 #include "config.h"
24 #include "wine/port.h"
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
33 #include <X11/Xlib.h>
34 #include <X11/Xresource.h>
35 #include <X11/Xutil.h>
36 #ifdef HAVE_LIBXSHAPE
37 #include <X11/extensions/shape.h>
38 #endif /* HAVE_LIBXSHAPE */
40 /* avoid conflict with field names in included win32 headers */
41 #undef Status
42 #include "windef.h"
43 #include "winbase.h"
44 #include "wingdi.h"
45 #include "winuser.h"
46 #include "wine/unicode.h"
48 #include "x11drv.h"
49 #include "wine/debug.h"
50 #include "wine/server.h"
51 #include "mwm.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
55 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
56 #define _NET_WM_MOVERESIZE_SIZE_TOP 1
57 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
58 #define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
59 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
60 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
61 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
62 #define _NET_WM_MOVERESIZE_SIZE_LEFT 7
63 #define _NET_WM_MOVERESIZE_MOVE 8
64 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9
65 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10
67 #define _NET_WM_STATE_REMOVE 0
68 #define _NET_WM_STATE_ADD 1
69 #define _NET_WM_STATE_TOGGLE 2
71 static const unsigned int net_wm_state_atoms[NB_NET_WM_STATES] =
73 XATOM__NET_WM_STATE_FULLSCREEN,
74 XATOM__NET_WM_STATE_ABOVE,
75 XATOM__NET_WM_STATE_MAXIMIZED_VERT,
76 XATOM__NET_WM_STATE_SKIP_PAGER,
77 XATOM__NET_WM_STATE_SKIP_TASKBAR
80 #define SWP_AGG_NOPOSCHANGE (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_NOZORDER)
82 /* is cursor clipping active? */
83 BOOL clipping_cursor = FALSE;
85 /* X context to associate a hwnd to an X window */
86 XContext winContext = 0;
88 /* X context to associate a struct x11drv_win_data to an hwnd */
89 XContext win_data_context = 0;
91 /* time of last user event and window where it's stored */
92 static Time last_user_time;
93 static Window user_time_window;
95 static const char foreign_window_prop[] = "__wine_x11_foreign_window";
96 static const char whole_window_prop[] = "__wine_x11_whole_window";
97 static const char clip_window_prop[] = "__wine_x11_clip_window";
99 static CRITICAL_SECTION win_data_section;
100 static CRITICAL_SECTION_DEBUG critsect_debug =
102 0, 0, &win_data_section,
103 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
104 0, 0, { (DWORD_PTR)(__FILE__ ": win_data_section") }
106 static CRITICAL_SECTION win_data_section = { &critsect_debug, -1, 0, 0, 0, 0 };
109 /***********************************************************************
110 * http://standards.freedesktop.org/startup-notification-spec
112 static void remove_startup_notification(Display *display, Window window)
114 static LONG startup_notification_removed = 0;
115 char id[1024];
116 char message[1024];
117 int i;
118 int pos;
119 XEvent xevent;
120 const char *src;
121 int srclen;
123 if (InterlockedCompareExchange(&startup_notification_removed, 1, 0) != 0)
124 return;
126 if (GetEnvironmentVariableA("DESKTOP_STARTUP_ID", id, sizeof(id)) == 0)
127 return;
128 SetEnvironmentVariableA("DESKTOP_STARTUP_ID", NULL);
130 if ((src = strstr( id, "_TIME" ))) update_user_time( atol( src + 5 ));
132 pos = snprintf(message, sizeof(message), "remove: ID=");
133 message[pos++] = '"';
134 for (i = 0; id[i] && pos < sizeof(message) - 3; i++)
136 if (id[i] == '"' || id[i] == '\\')
137 message[pos++] = '\\';
138 message[pos++] = id[i];
140 message[pos++] = '"';
141 message[pos++] = '\0';
143 xevent.xclient.type = ClientMessage;
144 xevent.xclient.message_type = x11drv_atom(_NET_STARTUP_INFO_BEGIN);
145 xevent.xclient.display = display;
146 xevent.xclient.window = window;
147 xevent.xclient.format = 8;
149 src = message;
150 srclen = strlen(src) + 1;
152 while (srclen > 0)
154 int msglen = srclen;
155 if (msglen > 20)
156 msglen = 20;
157 memset(&xevent.xclient.data.b[0], 0, 20);
158 memcpy(&xevent.xclient.data.b[0], src, msglen);
159 src += msglen;
160 srclen -= msglen;
162 XSendEvent( display, DefaultRootWindow( display ), False, PropertyChangeMask, &xevent );
163 xevent.xclient.message_type = x11drv_atom(_NET_STARTUP_INFO);
168 struct has_popup_result
170 HWND hwnd;
171 BOOL found;
174 static BOOL is_managed( HWND hwnd )
176 struct x11drv_win_data *data = get_win_data( hwnd );
177 BOOL ret = data && data->managed;
178 release_win_data( data );
179 return ret;
182 static BOOL CALLBACK has_managed_popup( HWND hwnd, LPARAM lparam )
184 struct has_popup_result *result = (struct has_popup_result *)lparam;
186 if (hwnd == result->hwnd) return FALSE; /* popups are always above owner */
187 if (GetWindow( hwnd, GW_OWNER ) != result->hwnd) return TRUE;
188 result->found = is_managed( hwnd );
189 return !result->found;
192 static BOOL has_owned_popups( HWND hwnd )
194 struct has_popup_result result;
196 result.hwnd = hwnd;
197 result.found = FALSE;
198 EnumWindows( has_managed_popup, (LPARAM)&result );
199 return result.found;
203 /***********************************************************************
204 * alloc_win_data
206 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
208 struct x11drv_win_data *data;
210 if ((data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
212 data->display = display;
213 data->vis = default_visual;
214 data->hwnd = hwnd;
215 EnterCriticalSection( &win_data_section );
216 XSaveContext( gdi_display, (XID)hwnd, win_data_context, (char *)data );
218 return data;
222 /***********************************************************************
223 * is_window_managed
225 * Check if a given window should be managed
227 static BOOL is_window_managed( HWND hwnd, UINT swp_flags, const RECT *window_rect )
229 DWORD style, ex_style;
231 if (!managed_mode) return FALSE;
233 /* child windows are not managed */
234 style = GetWindowLongW( hwnd, GWL_STYLE );
235 if ((style & (WS_CHILD|WS_POPUP)) == WS_CHILD) return FALSE;
236 /* activated windows are managed */
237 if (!(swp_flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW))) return TRUE;
238 if (hwnd == GetActiveWindow()) return TRUE;
239 /* windows with caption are managed */
240 if ((style & WS_CAPTION) == WS_CAPTION) return TRUE;
241 /* windows with thick frame are managed */
242 if (style & WS_THICKFRAME) return TRUE;
243 if (style & WS_POPUP)
245 HMONITOR hmon;
246 MONITORINFO mi;
248 /* popup with sysmenu == caption are managed */
249 if (style & WS_SYSMENU) return TRUE;
250 /* full-screen popup windows are managed */
251 hmon = MonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY );
252 mi.cbSize = sizeof( mi );
253 GetMonitorInfoW( hmon, &mi );
254 if (window_rect->left <= mi.rcWork.left && window_rect->right >= mi.rcWork.right &&
255 window_rect->top <= mi.rcWork.top && window_rect->bottom >= mi.rcWork.bottom)
256 return TRUE;
258 /* application windows are managed */
259 ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
260 if (ex_style & WS_EX_APPWINDOW) return TRUE;
261 /* windows that own popups are managed */
262 if (has_owned_popups( hwnd )) return TRUE;
263 /* default: not managed */
264 return FALSE;
268 /***********************************************************************
269 * is_window_resizable
271 * Check if window should be made resizable by the window manager
273 static inline BOOL is_window_resizable( struct x11drv_win_data *data, DWORD style )
275 if (style & WS_THICKFRAME) return TRUE;
276 /* Metacity needs the window to be resizable to make it fullscreen */
277 return is_window_rect_full_screen( &data->whole_rect );
280 struct monitor_info
282 const RECT *rect;
283 BOOL full_screen;
286 static BOOL CALLBACK enum_monitor_proc( HMONITOR monitor, HDC hdc, RECT *monitor_rect, LPARAM lparam )
288 struct monitor_info *info = (struct monitor_info *)lparam;
290 if (info->rect->left <= monitor_rect->left && info->rect->right >= monitor_rect->right &&
291 info->rect->top <= monitor_rect->top && info->rect->bottom >= monitor_rect->bottom)
293 info->full_screen = TRUE;
294 return FALSE;
297 return TRUE;
300 BOOL is_window_rect_full_screen( const RECT *rect )
302 struct monitor_info info = {rect, FALSE};
304 EnumDisplayMonitors( NULL, NULL, enum_monitor_proc, (LPARAM)&info );
305 return info.full_screen;
308 /***********************************************************************
309 * get_mwm_decorations
311 static unsigned long get_mwm_decorations( struct x11drv_win_data *data,
312 DWORD style, DWORD ex_style,
313 const RECT *window_rect,
314 const RECT *client_rect )
316 unsigned long ret = 0;
318 if (!decorated_mode) return 0;
320 if (EqualRect( window_rect, client_rect )) return 0;
321 if (IsRectEmpty( window_rect )) return 0;
322 if (data->shaped) return 0;
324 if (ex_style & WS_EX_TOOLWINDOW) return 0;
325 if (ex_style & WS_EX_LAYERED) return 0;
327 if ((style & WS_CAPTION) == WS_CAPTION)
329 ret |= MWM_DECOR_TITLE | MWM_DECOR_BORDER;
330 if (style & WS_SYSMENU) ret |= MWM_DECOR_MENU;
331 if (style & WS_MINIMIZEBOX) ret |= MWM_DECOR_MINIMIZE;
332 if (style & WS_MAXIMIZEBOX) ret |= MWM_DECOR_MAXIMIZE;
334 if (ex_style & WS_EX_DLGMODALFRAME) ret |= MWM_DECOR_BORDER;
335 else if (style & WS_THICKFRAME) ret |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
336 else if ((style & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) ret |= MWM_DECOR_BORDER;
337 return ret;
341 /***********************************************************************
342 * get_window_attributes
344 * Fill the window attributes structure for an X window.
346 static int get_window_attributes( struct x11drv_win_data *data, XSetWindowAttributes *attr )
348 attr->override_redirect = !data->managed;
349 attr->colormap = data->whole_colormap ? data->whole_colormap : default_colormap;
350 attr->save_under = ((GetClassLongW( data->hwnd, GCL_STYLE ) & CS_SAVEBITS) != 0);
351 attr->bit_gravity = NorthWestGravity;
352 attr->backing_store = NotUseful;
353 attr->border_pixel = 0;
354 attr->event_mask = (ExposureMask | PointerMotionMask |
355 ButtonPressMask | ButtonReleaseMask | EnterWindowMask |
356 KeyPressMask | KeyReleaseMask | FocusChangeMask |
357 KeymapStateMask | StructureNotifyMask);
358 if (data->managed) attr->event_mask |= PropertyChangeMask;
360 return (CWOverrideRedirect | CWSaveUnder | CWColormap | CWBorderPixel |
361 CWEventMask | CWBitGravity | CWBackingStore);
365 /***********************************************************************
366 * sync_window_style
368 * Change the X window attributes when the window style has changed.
370 static void sync_window_style( struct x11drv_win_data *data )
372 if (data->whole_window != root_window)
374 XSetWindowAttributes attr;
375 int mask = get_window_attributes( data, &attr );
377 XChangeWindowAttributes( data->display, data->whole_window, mask, &attr );
382 /***********************************************************************
383 * sync_window_region
385 * Update the X11 window region.
387 static void sync_window_region( struct x11drv_win_data *data, HRGN win_region )
389 #ifdef HAVE_LIBXSHAPE
390 HRGN hrgn = win_region;
392 if (!data->whole_window) return;
393 data->shaped = FALSE;
395 if (IsRectEmpty( &data->window_rect )) /* set an empty shape */
397 static XRectangle empty_rect;
398 XShapeCombineRectangles( data->display, data->whole_window, ShapeBounding, 0, 0,
399 &empty_rect, 1, ShapeSet, YXBanded );
400 return;
403 if (hrgn == (HRGN)1) /* hack: win_region == 1 means retrieve region from server */
405 if (!(hrgn = CreateRectRgn( 0, 0, 0, 0 ))) return;
406 if (GetWindowRgn( data->hwnd, hrgn ) == ERROR)
408 DeleteObject( hrgn );
409 hrgn = 0;
413 if (!hrgn)
415 XShapeCombineMask( data->display, data->whole_window, ShapeBounding, 0, 0, None, ShapeSet );
417 else
419 RGNDATA *pRegionData;
421 if (GetWindowLongW( data->hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL) MirrorRgn( data->hwnd, hrgn );
422 if ((pRegionData = X11DRV_GetRegionData( hrgn, 0 )))
424 XShapeCombineRectangles( data->display, data->whole_window, ShapeBounding,
425 data->window_rect.left - data->whole_rect.left,
426 data->window_rect.top - data->whole_rect.top,
427 (XRectangle *)pRegionData->Buffer,
428 pRegionData->rdh.nCount, ShapeSet, YXBanded );
429 HeapFree(GetProcessHeap(), 0, pRegionData);
430 data->shaped = TRUE;
433 if (hrgn && hrgn != win_region) DeleteObject( hrgn );
434 #endif /* HAVE_LIBXSHAPE */
438 /***********************************************************************
439 * sync_window_opacity
441 static void sync_window_opacity( Display *display, Window win,
442 COLORREF key, BYTE alpha, DWORD flags )
444 unsigned long opacity = 0xffffffff;
446 if (flags & LWA_ALPHA) opacity = (0xffffffff / 0xff) * alpha;
448 if (opacity == 0xffffffff)
449 XDeleteProperty( display, win, x11drv_atom(_NET_WM_WINDOW_OPACITY) );
450 else
451 XChangeProperty( display, win, x11drv_atom(_NET_WM_WINDOW_OPACITY),
452 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&opacity, 1 );
456 /***********************************************************************
457 * sync_window_text
459 static void sync_window_text( Display *display, Window win, const WCHAR *text )
461 UINT count;
462 char *buffer, *utf8_buffer;
463 XTextProperty prop;
465 /* allocate new buffer for window text */
466 count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
467 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count ))) return;
468 WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
470 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
471 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
473 HeapFree( GetProcessHeap(), 0, buffer );
474 return;
476 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
478 if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
480 XSetWMName( display, win, &prop );
481 XSetWMIconName( display, win, &prop );
482 XFree( prop.value );
485 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
486 according to the standard
487 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
489 XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
490 8, PropModeReplace, (unsigned char *) utf8_buffer, count);
492 HeapFree( GetProcessHeap(), 0, utf8_buffer );
493 HeapFree( GetProcessHeap(), 0, buffer );
497 /***********************************************************************
498 * get_bitmap_argb
500 * Return the bitmap bits in ARGB format. Helper for setting icon hints.
502 static unsigned long *get_bitmap_argb( HDC hdc, HBITMAP color, HBITMAP mask, unsigned int *size )
504 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
505 BITMAPINFO *info = (BITMAPINFO *)buffer;
506 BITMAP bm;
507 unsigned int *ptr, *bits = NULL;
508 unsigned char *mask_bits = NULL;
509 int i, j;
510 BOOL has_alpha = FALSE;
512 if (!GetObjectW( color, sizeof(bm), &bm )) return NULL;
513 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
514 info->bmiHeader.biWidth = bm.bmWidth;
515 info->bmiHeader.biHeight = -bm.bmHeight;
516 info->bmiHeader.biPlanes = 1;
517 info->bmiHeader.biBitCount = 32;
518 info->bmiHeader.biCompression = BI_RGB;
519 info->bmiHeader.biSizeImage = bm.bmWidth * bm.bmHeight * 4;
520 info->bmiHeader.biXPelsPerMeter = 0;
521 info->bmiHeader.biYPelsPerMeter = 0;
522 info->bmiHeader.biClrUsed = 0;
523 info->bmiHeader.biClrImportant = 0;
524 *size = bm.bmWidth * bm.bmHeight + 2;
525 if (!(bits = HeapAlloc( GetProcessHeap(), 0, *size * sizeof(long) ))) goto failed;
526 if (!GetDIBits( hdc, color, 0, bm.bmHeight, bits + 2, info, DIB_RGB_COLORS )) goto failed;
528 bits[0] = bm.bmWidth;
529 bits[1] = bm.bmHeight;
531 for (i = 0; i < bm.bmWidth * bm.bmHeight; i++)
532 if ((has_alpha = (bits[i + 2] & 0xff000000) != 0)) break;
534 if (!has_alpha)
536 unsigned int width_bytes = (bm.bmWidth + 31) / 32 * 4;
537 /* generate alpha channel from the mask */
538 info->bmiHeader.biBitCount = 1;
539 info->bmiHeader.biSizeImage = width_bytes * bm.bmHeight;
540 if (!(mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto failed;
541 if (!GetDIBits( hdc, mask, 0, bm.bmHeight, mask_bits, info, DIB_RGB_COLORS )) goto failed;
542 ptr = bits + 2;
543 for (i = 0; i < bm.bmHeight; i++)
544 for (j = 0; j < bm.bmWidth; j++, ptr++)
545 if (!((mask_bits[i * width_bytes + j / 8] << (j % 8)) & 0x80)) *ptr |= 0xff000000;
546 HeapFree( GetProcessHeap(), 0, mask_bits );
549 /* convert to array of longs */
550 if (bits && sizeof(long) > sizeof(int))
551 for (i = *size - 1; i >= 0; i--) ((unsigned long *)bits)[i] = bits[i];
553 return (unsigned long *)bits;
555 failed:
556 HeapFree( GetProcessHeap(), 0, bits );
557 HeapFree( GetProcessHeap(), 0, mask_bits );
558 return NULL;
562 /***********************************************************************
563 * create_icon_pixmaps
565 static BOOL create_icon_pixmaps( HDC hdc, const ICONINFO *icon, Pixmap *icon_ret, Pixmap *mask_ret )
567 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
568 BITMAPINFO *info = (BITMAPINFO *)buffer;
569 XVisualInfo vis = default_visual;
570 struct gdi_image_bits bits;
571 Pixmap color_pixmap = 0, mask_pixmap = 0;
572 int lines;
573 unsigned int i;
575 bits.ptr = NULL;
576 bits.free = NULL;
577 bits.is_copy = TRUE;
579 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
580 info->bmiHeader.biBitCount = 0;
581 if (!(lines = GetDIBits( hdc, icon->hbmColor, 0, 0, NULL, info, DIB_RGB_COLORS ))) goto failed;
582 if (!(bits.ptr = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto failed;
583 if (!GetDIBits( hdc, icon->hbmColor, 0, lines, bits.ptr, info, DIB_RGB_COLORS )) goto failed;
585 color_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS );
586 HeapFree( GetProcessHeap(), 0, bits.ptr );
587 bits.ptr = NULL;
588 if (!color_pixmap) goto failed;
590 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
591 info->bmiHeader.biBitCount = 0;
592 if (!(lines = GetDIBits( hdc, icon->hbmMask, 0, 0, NULL, info, DIB_RGB_COLORS ))) goto failed;
593 if (!(bits.ptr = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto failed;
594 if (!GetDIBits( hdc, icon->hbmMask, 0, lines, bits.ptr, info, DIB_RGB_COLORS )) goto failed;
596 /* invert the mask */
597 for (i = 0; i < info->bmiHeader.biSizeImage / sizeof(DWORD); i++) ((DWORD *)bits.ptr)[i] ^= ~0u;
599 vis.depth = 1;
600 mask_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS );
601 HeapFree( GetProcessHeap(), 0, bits.ptr );
602 bits.ptr = NULL;
603 if (!mask_pixmap) goto failed;
605 *icon_ret = color_pixmap;
606 *mask_ret = mask_pixmap;
607 return TRUE;
609 failed:
610 if (color_pixmap) XFreePixmap( gdi_display, color_pixmap );
611 HeapFree( GetProcessHeap(), 0, bits.ptr );
612 return FALSE;
616 /***********************************************************************
617 * fetch_icon_data
619 static void fetch_icon_data( HWND hwnd, HICON icon_big, HICON icon_small )
621 struct x11drv_win_data *data;
622 ICONINFO ii, ii_small;
623 HDC hDC;
624 unsigned int size;
625 unsigned long *bits;
626 Pixmap icon_pixmap, mask_pixmap;
628 if (!icon_big)
630 icon_big = (HICON)SendMessageW( hwnd, WM_GETICON, ICON_BIG, 0 );
631 if (!icon_big) icon_big = (HICON)GetClassLongPtrW( hwnd, GCLP_HICON );
632 if (!icon_big) icon_big = LoadIconW( 0, (LPWSTR)IDI_WINLOGO );
634 if (!icon_small)
636 icon_small = (HICON)SendMessageW( hwnd, WM_GETICON, ICON_SMALL, 0 );
637 if (!icon_small) icon_small = (HICON)GetClassLongPtrW( hwnd, GCLP_HICONSM );
640 if (!GetIconInfo(icon_big, &ii)) return;
642 hDC = CreateCompatibleDC(0);
643 bits = get_bitmap_argb( hDC, ii.hbmColor, ii.hbmMask, &size );
644 if (bits && GetIconInfo( icon_small, &ii_small ))
646 unsigned int size_small;
647 unsigned long *bits_small, *new;
649 if ((bits_small = get_bitmap_argb( hDC, ii_small.hbmColor, ii_small.hbmMask, &size_small )) &&
650 (bits_small[0] != bits[0] || bits_small[1] != bits[1])) /* size must be different */
652 if ((new = HeapReAlloc( GetProcessHeap(), 0, bits,
653 (size + size_small) * sizeof(unsigned long) )))
655 bits = new;
656 memcpy( bits + size, bits_small, size_small * sizeof(unsigned long) );
657 size += size_small;
660 HeapFree( GetProcessHeap(), 0, bits_small );
661 DeleteObject( ii_small.hbmColor );
662 DeleteObject( ii_small.hbmMask );
665 if (!create_icon_pixmaps( hDC, &ii, &icon_pixmap, &mask_pixmap )) icon_pixmap = mask_pixmap = 0;
667 DeleteObject( ii.hbmColor );
668 DeleteObject( ii.hbmMask );
669 DeleteDC(hDC);
671 if ((data = get_win_data( hwnd )))
673 if (data->icon_pixmap) XFreePixmap( gdi_display, data->icon_pixmap );
674 if (data->icon_mask) XFreePixmap( gdi_display, data->icon_mask );
675 HeapFree( GetProcessHeap(), 0, data->icon_bits );
676 data->icon_pixmap = icon_pixmap;
677 data->icon_mask = mask_pixmap;
678 data->icon_bits = bits;
679 data->icon_size = size;
680 release_win_data( data );
682 else
684 if (icon_pixmap) XFreePixmap( gdi_display, icon_pixmap );
685 if (mask_pixmap) XFreePixmap( gdi_display, mask_pixmap );
686 HeapFree( GetProcessHeap(), 0, bits );
691 /***********************************************************************
692 * set_size_hints
694 * set the window size hints
696 static void set_size_hints( struct x11drv_win_data *data, DWORD style )
698 XSizeHints* size_hints;
700 if (!(size_hints = XAllocSizeHints())) return;
702 size_hints->win_gravity = StaticGravity;
703 size_hints->flags |= PWinGravity;
705 /* don't update size hints if window is not in normal state */
706 if (!(style & (WS_MINIMIZE | WS_MAXIMIZE)))
708 if (data->hwnd != GetDesktopWindow()) /* don't force position of desktop */
710 size_hints->x = data->whole_rect.left;
711 size_hints->y = data->whole_rect.top;
712 size_hints->flags |= PPosition;
714 else size_hints->win_gravity = NorthWestGravity;
716 if (!is_window_resizable( data, style ))
718 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
719 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
720 if (size_hints->max_width <= 0 ||size_hints->max_height <= 0)
721 size_hints->max_width = size_hints->max_height = 1;
722 size_hints->min_width = size_hints->max_width;
723 size_hints->min_height = size_hints->max_height;
724 size_hints->flags |= PMinSize | PMaxSize;
727 XSetWMNormalHints( data->display, data->whole_window, size_hints );
728 XFree( size_hints );
732 /***********************************************************************
733 * set_mwm_hints
735 static void set_mwm_hints( struct x11drv_win_data *data, DWORD style, DWORD ex_style )
737 MwmHints mwm_hints;
739 if (data->hwnd == GetDesktopWindow())
741 if (is_desktop_fullscreen()) mwm_hints.decorations = 0;
742 else mwm_hints.decorations = MWM_DECOR_TITLE | MWM_DECOR_BORDER | MWM_DECOR_MENU | MWM_DECOR_MINIMIZE;
743 mwm_hints.functions = MWM_FUNC_MOVE | MWM_FUNC_MINIMIZE | MWM_FUNC_CLOSE;
745 else
747 mwm_hints.decorations = get_mwm_decorations( data, style, ex_style, &data->window_rect, &data->client_rect );
748 mwm_hints.functions = MWM_FUNC_MOVE;
749 if (is_window_resizable( data, style )) mwm_hints.functions |= MWM_FUNC_RESIZE;
750 if (!(style & WS_DISABLED))
752 mwm_hints.functions |= MWM_FUNC_CLOSE;
753 if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
754 if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
756 /* The window can be programmatically minimized even without
757 a minimize box button. Allow the WM to restore it. */
758 if (style & WS_MINIMIZE) mwm_hints.functions |= MWM_FUNC_MINIMIZE | MWM_FUNC_MAXIMIZE;
762 TRACE( "%p setting mwm hints to %lx,%lx (style %x exstyle %x)\n",
763 data->hwnd, mwm_hints.decorations, mwm_hints.functions, style, ex_style );
765 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
766 mwm_hints.input_mode = 0;
767 mwm_hints.status = 0;
768 XChangeProperty( data->display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
769 x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
770 (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
774 /***********************************************************************
775 * set_style_hints
777 static void set_style_hints( struct x11drv_win_data *data, DWORD style, DWORD ex_style )
779 Window group_leader = data->whole_window;
780 HWND owner = GetWindow( data->hwnd, GW_OWNER );
781 Window owner_win = 0;
782 XWMHints *wm_hints;
783 Atom window_type;
785 if (owner)
787 owner = GetAncestor( owner, GA_ROOT );
788 owner_win = X11DRV_get_whole_window( owner );
791 if (owner_win)
793 XSetTransientForHint( data->display, data->whole_window, owner_win );
794 group_leader = owner_win;
797 /* Only use dialog type for owned popups. Metacity allows making fullscreen
798 * only normal windows, and doesn't handle correctly TRANSIENT_FOR hint for
799 * dialogs owned by fullscreen windows.
801 if (((style & WS_POPUP) || (ex_style & WS_EX_DLGMODALFRAME)) && owner)
802 window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
803 else
804 window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
806 XChangeProperty(data->display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
807 XA_ATOM, 32, PropModeReplace, (unsigned char*)&window_type, 1);
809 if ((wm_hints = XAllocWMHints()))
811 wm_hints->flags = InputHint | StateHint | WindowGroupHint;
812 wm_hints->input = !use_take_focus && !(style & WS_DISABLED);
813 wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
814 wm_hints->window_group = group_leader;
815 if (data->icon_pixmap)
817 wm_hints->icon_pixmap = data->icon_pixmap;
818 wm_hints->icon_mask = data->icon_mask;
819 wm_hints->flags |= IconPixmapHint | IconMaskHint;
821 XSetWMHints( data->display, data->whole_window, wm_hints );
822 XFree( wm_hints );
825 if (data->icon_bits)
826 XChangeProperty( data->display, data->whole_window, x11drv_atom(_NET_WM_ICON),
827 XA_CARDINAL, 32, PropModeReplace,
828 (unsigned char *)data->icon_bits, data->icon_size );
829 else
830 XDeleteProperty( data->display, data->whole_window, x11drv_atom(_NET_WM_ICON) );
835 /***********************************************************************
836 * set_initial_wm_hints
838 * Set the window manager hints that don't change over the lifetime of a window.
840 static void set_initial_wm_hints( Display *display, Window window )
842 long i;
843 Atom protocols[3];
844 Atom dndVersion = WINE_XDND_VERSION;
845 XClassHint *class_hints;
847 /* wm protocols */
848 i = 0;
849 protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
850 protocols[i++] = x11drv_atom(_NET_WM_PING);
851 if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
852 XChangeProperty( display, window, x11drv_atom(WM_PROTOCOLS),
853 XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
855 /* class hints */
856 if ((class_hints = XAllocClassHint()))
858 class_hints->res_name = process_name;
859 class_hints->res_class = process_name;
860 XSetClassHint( display, window, class_hints );
861 XFree( class_hints );
864 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
865 XSetWMProperties(display, window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
866 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
867 i = getpid();
868 XChangeProperty(display, window, x11drv_atom(_NET_WM_PID),
869 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
871 XChangeProperty( display, window, x11drv_atom(XdndAware),
872 XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
874 update_user_time( 0 ); /* make sure that the user time window exists */
875 if (user_time_window)
876 XChangeProperty( display, window, x11drv_atom(_NET_WM_USER_TIME_WINDOW),
877 XA_WINDOW, 32, PropModeReplace, (unsigned char *)&user_time_window, 1 );
881 /***********************************************************************
882 * make_owner_managed
884 * If the window is managed, make sure its owner window is too.
886 static void make_owner_managed( HWND hwnd )
888 HWND owner;
890 if (!(owner = GetWindow( hwnd, GW_OWNER ))) return;
891 if (is_managed( owner )) return;
892 if (!is_managed( hwnd )) return;
894 SetWindowPos( owner, 0, 0, 0, 0, 0,
895 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE |
896 SWP_NOREDRAW | SWP_DEFERERASE | SWP_NOSENDCHANGING | SWP_STATECHANGED );
900 /***********************************************************************
901 * set_wm_hints
903 * Set all the window manager hints for a window.
905 static void set_wm_hints( struct x11drv_win_data *data )
907 DWORD style, ex_style;
909 if (data->hwnd == GetDesktopWindow())
911 /* force some styles for the desktop to get the correct decorations */
912 style = WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
913 ex_style = WS_EX_APPWINDOW;
915 else
917 style = GetWindowLongW( data->hwnd, GWL_STYLE );
918 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
921 set_size_hints( data, style );
922 set_mwm_hints( data, style, ex_style );
923 set_style_hints( data, style, ex_style );
927 /***********************************************************************
928 * init_clip_window
930 Window init_clip_window(void)
932 struct x11drv_thread_data *data = x11drv_init_thread_data();
934 if (!data->clip_window &&
935 (data->clip_window = (Window)GetPropA( GetDesktopWindow(), clip_window_prop )))
937 XSelectInput( data->display, data->clip_window, StructureNotifyMask );
939 return data->clip_window;
943 /***********************************************************************
944 * update_user_time
946 void update_user_time( Time time )
948 if (!user_time_window)
950 Window win = XCreateWindow( gdi_display, root_window, -1, -1, 1, 1, 0, CopyFromParent,
951 InputOnly, CopyFromParent, 0, NULL );
952 if (InterlockedCompareExchangePointer( (void **)&user_time_window, (void *)win, 0 ))
953 XDestroyWindow( gdi_display, win );
954 TRACE( "user time window %lx\n", user_time_window );
957 if (!time) return;
958 XLockDisplay( gdi_display );
959 if (!last_user_time || (long)(time - last_user_time) > 0)
961 last_user_time = time;
962 XChangeProperty( gdi_display, user_time_window, x11drv_atom(_NET_WM_USER_TIME),
963 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&time, 1 );
965 XUnlockDisplay( gdi_display );
968 /***********************************************************************
969 * update_net_wm_states
971 void update_net_wm_states( struct x11drv_win_data *data )
973 DWORD i, style, ex_style, new_state = 0;
975 if (!data->managed) return;
976 if (data->whole_window == root_window) return;
978 style = GetWindowLongW( data->hwnd, GWL_STYLE );
979 if (style & WS_MINIMIZE)
980 new_state |= data->net_wm_state & ((1 << NET_WM_STATE_FULLSCREEN)|(1 << NET_WM_STATE_MAXIMIZED));
981 if (is_window_rect_full_screen( &data->whole_rect ))
983 if ((style & WS_MAXIMIZE) && (style & WS_CAPTION) == WS_CAPTION)
984 new_state |= (1 << NET_WM_STATE_MAXIMIZED);
985 else if (!(style & WS_MINIMIZE))
986 new_state |= (1 << NET_WM_STATE_FULLSCREEN);
988 else if (style & WS_MAXIMIZE)
989 new_state |= (1 << NET_WM_STATE_MAXIMIZED);
991 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
992 if (ex_style & WS_EX_TOPMOST)
993 new_state |= (1 << NET_WM_STATE_ABOVE);
994 if (ex_style & (WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE))
995 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR) | (1 << NET_WM_STATE_SKIP_PAGER);
996 if (!(ex_style & WS_EX_APPWINDOW) && GetWindow( data->hwnd, GW_OWNER ))
997 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR);
999 if (!data->mapped) /* set the _NET_WM_STATE atom directly */
1001 Atom atoms[NB_NET_WM_STATES+1];
1002 DWORD count;
1004 for (i = count = 0; i < NB_NET_WM_STATES; i++)
1006 if (!(new_state & (1 << i))) continue;
1007 TRACE( "setting wm state %u for unmapped window %p/%lx\n",
1008 i, data->hwnd, data->whole_window );
1009 atoms[count++] = X11DRV_Atoms[net_wm_state_atoms[i] - FIRST_XATOM];
1010 if (net_wm_state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT)
1011 atoms[count++] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ);
1013 XChangeProperty( data->display, data->whole_window, x11drv_atom(_NET_WM_STATE), XA_ATOM,
1014 32, PropModeReplace, (unsigned char *)atoms, count );
1016 else /* ask the window manager to do it for us */
1018 XEvent xev;
1020 xev.xclient.type = ClientMessage;
1021 xev.xclient.window = data->whole_window;
1022 xev.xclient.message_type = x11drv_atom(_NET_WM_STATE);
1023 xev.xclient.serial = 0;
1024 xev.xclient.display = data->display;
1025 xev.xclient.send_event = True;
1026 xev.xclient.format = 32;
1027 xev.xclient.data.l[3] = 1;
1028 xev.xclient.data.l[4] = 0;
1030 for (i = 0; i < NB_NET_WM_STATES; i++)
1032 TRACE( "setting wm state %u for window %p/%lx to %u prev %u\n",
1033 i, data->hwnd, data->whole_window,
1034 (new_state & (1 << i)) != 0, (data->net_wm_state & (1 << i)) != 0 );
1036 xev.xclient.data.l[0] = (new_state & (1 << i)) ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
1037 xev.xclient.data.l[1] = X11DRV_Atoms[net_wm_state_atoms[i] - FIRST_XATOM];
1038 xev.xclient.data.l[2] = ((net_wm_state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT) ?
1039 x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ) : 0);
1040 XSendEvent( data->display, root_window, False,
1041 SubstructureRedirectMask | SubstructureNotifyMask, &xev );
1044 data->net_wm_state = new_state;
1047 /***********************************************************************
1048 * read_net_wm_states
1050 void read_net_wm_states( Display* display, struct x11drv_win_data *data )
1052 Atom type, *state;
1053 int format;
1054 unsigned long i, j, count, remaining;
1055 DWORD new_state = 0;
1056 BOOL maximized_horz = FALSE;
1058 if (!data->whole_window) return;
1060 if (!XGetWindowProperty( display, data->whole_window, x11drv_atom(_NET_WM_STATE), 0,
1061 65536/sizeof(CARD32), False, XA_ATOM, &type, &format, &count,
1062 &remaining, (unsigned char **)&state ))
1064 if (type == XA_ATOM && format == 32)
1066 for (i = 0; i < count; i++)
1068 if (state[i] == x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ))
1069 maximized_horz = TRUE;
1070 for (j=0; j < NB_NET_WM_STATES; j++)
1072 if (state[i] == X11DRV_Atoms[net_wm_state_atoms[j] - FIRST_XATOM])
1074 new_state |= 1 << j;
1079 XFree( state );
1082 if (!maximized_horz)
1083 new_state &= ~(1 << NET_WM_STATE_MAXIMIZED);
1085 data->net_wm_state = new_state;
1089 /***********************************************************************
1090 * set_xembed_flags
1092 static void set_xembed_flags( struct x11drv_win_data *data, unsigned long flags )
1094 unsigned long info[2];
1096 if (!data->whole_window) return;
1098 info[0] = 0; /* protocol version */
1099 info[1] = flags;
1100 XChangeProperty( data->display, data->whole_window, x11drv_atom(_XEMBED_INFO),
1101 x11drv_atom(_XEMBED_INFO), 32, PropModeReplace, (unsigned char*)info, 2 );
1105 /***********************************************************************
1106 * map_window
1108 static void map_window( HWND hwnd, DWORD new_style )
1110 struct x11drv_win_data *data;
1112 make_owner_managed( hwnd );
1113 wait_for_withdrawn_state( hwnd, TRUE );
1115 if (!(data = get_win_data( hwnd ))) return;
1117 if (data->whole_window && !data->mapped)
1119 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1121 remove_startup_notification( data->display, data->whole_window );
1122 set_wm_hints( data );
1124 if (!data->embedded)
1126 update_net_wm_states( data );
1127 sync_window_style( data );
1128 XMapWindow( data->display, data->whole_window );
1129 XFlush( data->display );
1130 if (data->surface && data->vis.visualid != default_visual.visualid)
1131 data->surface->funcs->flush( data->surface );
1133 else set_xembed_flags( data, XEMBED_MAPPED );
1135 data->mapped = TRUE;
1136 data->iconic = (new_style & WS_MINIMIZE) != 0;
1138 release_win_data( data );
1142 /***********************************************************************
1143 * unmap_window
1145 static void unmap_window( HWND hwnd )
1147 struct x11drv_win_data *data;
1149 wait_for_withdrawn_state( hwnd, FALSE );
1151 if (!(data = get_win_data( hwnd ))) return;
1153 if (data->mapped)
1155 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1157 if (data->embedded) set_xembed_flags( data, 0 );
1158 else if (!data->managed) XUnmapWindow( data->display, data->whole_window );
1159 else XWithdrawWindow( data->display, data->whole_window, data->vis.screen );
1161 data->mapped = FALSE;
1162 data->net_wm_state = 0;
1164 release_win_data( data );
1168 /***********************************************************************
1169 * make_window_embedded
1171 void make_window_embedded( struct x11drv_win_data *data )
1173 /* the window cannot be mapped before being embedded */
1174 if (data->mapped)
1176 if (!data->managed) XUnmapWindow( data->display, data->whole_window );
1177 else XWithdrawWindow( data->display, data->whole_window, data->vis.screen );
1178 data->net_wm_state = 0;
1180 data->embedded = TRUE;
1181 data->managed = TRUE;
1182 sync_window_style( data );
1183 set_xembed_flags( data, (data->mapped || data->embedder) ? XEMBED_MAPPED : 0 );
1187 /***********************************************************************
1188 * get_decoration_rect
1190 static void get_decoration_rect( struct x11drv_win_data *data, RECT *rect,
1191 const RECT *window_rect, const RECT *client_rect )
1193 DWORD style, ex_style, style_mask = 0, ex_style_mask = 0;
1194 unsigned long decor;
1196 SetRectEmpty( rect );
1197 if (!data->managed) return;
1199 style = GetWindowLongW( data->hwnd, GWL_STYLE );
1200 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
1201 decor = get_mwm_decorations( data, style, ex_style, window_rect, client_rect );
1203 if (decor & MWM_DECOR_TITLE) style_mask |= WS_CAPTION;
1204 if (decor & MWM_DECOR_BORDER)
1206 style_mask |= WS_DLGFRAME | WS_THICKFRAME;
1207 ex_style_mask |= WS_EX_DLGMODALFRAME;
1210 AdjustWindowRectEx( rect, style & style_mask, FALSE, ex_style & ex_style_mask );
1214 /***********************************************************************
1215 * X11DRV_window_to_X_rect
1217 * Convert a rect from client to X window coordinates
1219 static void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect,
1220 const RECT *window_rect, const RECT *client_rect )
1222 RECT rc;
1224 if (IsRectEmpty( rect )) return;
1226 get_decoration_rect( data, &rc, window_rect, client_rect );
1227 rect->left -= rc.left;
1228 rect->right -= rc.right;
1229 rect->top -= rc.top;
1230 rect->bottom -= rc.bottom;
1231 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1232 if (rect->left >= rect->right) rect->right = rect->left + 1;
1236 /***********************************************************************
1237 * X11DRV_X_to_window_rect
1239 * Opposite of X11DRV_window_to_X_rect
1241 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect, int x, int y, int cx, int cy )
1243 RECT rc;
1245 get_decoration_rect( data, &rc, &data->window_rect, &data->client_rect );
1247 x += min( data->window_rect.left - data->whole_rect.left, rc.left );
1248 y += min( data->window_rect.top - data->whole_rect.top, rc.top );
1249 cx += max( (data->window_rect.right - data->window_rect.left) -
1250 (data->whole_rect.right - data->whole_rect.left), rc.right - rc.left );
1251 cy += max( (data->window_rect.bottom - data->window_rect.top) -
1252 (data->whole_rect.bottom - data->whole_rect.top), rc.bottom - rc.top );
1253 SetRect( rect, x, y, x + cx, y + cy );
1257 /***********************************************************************
1258 * sync_window_position
1260 * Synchronize the X window position with the Windows one
1262 static void sync_window_position( struct x11drv_win_data *data,
1263 UINT swp_flags, const RECT *old_window_rect,
1264 const RECT *old_whole_rect, const RECT *old_client_rect )
1266 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
1267 DWORD ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
1268 XWindowChanges changes;
1269 unsigned int mask = 0;
1271 if (data->managed && data->iconic) return;
1273 /* resizing a managed maximized window is not allowed */
1274 if (!(style & WS_MAXIMIZE) || !data->managed)
1276 changes.width = data->whole_rect.right - data->whole_rect.left;
1277 changes.height = data->whole_rect.bottom - data->whole_rect.top;
1278 /* if window rect is empty force size to 1x1 */
1279 if (changes.width <= 0 || changes.height <= 0) changes.width = changes.height = 1;
1280 if (changes.width > 65535) changes.width = 65535;
1281 if (changes.height > 65535) changes.height = 65535;
1282 mask |= CWWidth | CWHeight;
1285 /* only the size is allowed to change for the desktop window */
1286 if (data->whole_window != root_window)
1288 POINT pt = virtual_screen_to_root( data->whole_rect.left, data->whole_rect.top );
1289 changes.x = pt.x;
1290 changes.y = pt.y;
1291 mask |= CWX | CWY;
1294 if (!(swp_flags & SWP_NOZORDER) || (swp_flags & SWP_SHOWWINDOW))
1296 /* find window that this one must be after */
1297 HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
1298 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
1299 prev = GetWindow( prev, GW_HWNDPREV );
1300 if (!prev) /* top child */
1302 changes.stack_mode = Above;
1303 mask |= CWStackMode;
1305 /* should use stack_mode Below but most window managers don't get it right */
1306 /* and Above with a sibling doesn't work so well either, so we ignore it */
1309 set_size_hints( data, style );
1310 set_mwm_hints( data, style, ex_style );
1311 update_net_wm_states( data );
1312 data->configure_serial = NextRequest( data->display );
1313 XReconfigureWMWindow( data->display, data->whole_window, data->vis.screen, mask, &changes );
1314 #ifdef HAVE_LIBXSHAPE
1315 if (IsRectEmpty( old_window_rect ) != IsRectEmpty( &data->window_rect ))
1316 sync_window_region( data, (HRGN)1 );
1317 if (data->shaped)
1319 int old_x_offset = old_window_rect->left - old_whole_rect->left;
1320 int old_y_offset = old_window_rect->top - old_whole_rect->top;
1321 int new_x_offset = data->window_rect.left - data->whole_rect.left;
1322 int new_y_offset = data->window_rect.top - data->whole_rect.top;
1323 if (old_x_offset != new_x_offset || old_y_offset != new_y_offset)
1324 XShapeOffsetShape( data->display, data->whole_window, ShapeBounding,
1325 new_x_offset - old_x_offset, new_y_offset - old_y_offset );
1327 #endif
1329 TRACE( "win %p/%lx pos %d,%d,%dx%d after %lx changes=%x serial=%lu\n",
1330 data->hwnd, data->whole_window, data->whole_rect.left, data->whole_rect.top,
1331 data->whole_rect.right - data->whole_rect.left,
1332 data->whole_rect.bottom - data->whole_rect.top,
1333 changes.sibling, mask, data->configure_serial );
1337 /***********************************************************************
1338 * sync_client_position
1340 * Synchronize the X client window position with the Windows one
1342 static void sync_client_position( struct x11drv_win_data *data,
1343 const RECT *old_client_rect, const RECT *old_whole_rect )
1345 int mask = 0;
1346 XWindowChanges changes;
1348 if (!data->client_window) return;
1350 changes.x = data->client_rect.left - data->whole_rect.left;
1351 changes.y = data->client_rect.top - data->whole_rect.top;
1352 changes.width = min( max( 1, data->client_rect.right - data->client_rect.left ), 65535 );
1353 changes.height = min( max( 1, data->client_rect.bottom - data->client_rect.top ), 65535 );
1355 if (changes.x != old_client_rect->left - old_whole_rect->left) mask |= CWX;
1356 if (changes.y != old_client_rect->top - old_whole_rect->top) mask |= CWY;
1357 if (changes.width != old_client_rect->right - old_client_rect->left) mask |= CWWidth;
1358 if (changes.height != old_client_rect->bottom - old_client_rect->top) mask |= CWHeight;
1360 if (mask)
1362 TRACE( "setting client win %lx pos %d,%d,%dx%d changes=%x\n",
1363 data->client_window, changes.x, changes.y, changes.width, changes.height, mask );
1364 XConfigureWindow( data->display, data->client_window, mask, &changes );
1369 /***********************************************************************
1370 * move_window_bits
1372 * Move the window bits when a window is moved.
1374 static void move_window_bits( HWND hwnd, Window window, const RECT *old_rect, const RECT *new_rect,
1375 const RECT *old_client_rect, const RECT *new_client_rect,
1376 const RECT *new_window_rect )
1378 RECT src_rect = *old_rect;
1379 RECT dst_rect = *new_rect;
1380 HDC hdc_src, hdc_dst;
1381 INT code;
1382 HRGN rgn;
1383 HWND parent = 0;
1385 if (!window)
1387 OffsetRect( &dst_rect, -new_window_rect->left, -new_window_rect->top );
1388 parent = GetAncestor( hwnd, GA_PARENT );
1389 hdc_src = GetDCEx( parent, 0, DCX_CACHE );
1390 hdc_dst = GetDCEx( hwnd, 0, DCX_CACHE | DCX_WINDOW );
1392 else
1394 OffsetRect( &dst_rect, -new_client_rect->left, -new_client_rect->top );
1395 /* make src rect relative to the old position of the window */
1396 OffsetRect( &src_rect, -old_client_rect->left, -old_client_rect->top );
1397 if (dst_rect.left == src_rect.left && dst_rect.top == src_rect.top) return;
1398 hdc_src = hdc_dst = GetDCEx( hwnd, 0, DCX_CACHE );
1401 rgn = CreateRectRgnIndirect( &dst_rect );
1402 SelectClipRgn( hdc_dst, rgn );
1403 DeleteObject( rgn );
1404 /* WS_CLIPCHILDREN doesn't exclude children from the window update
1405 * region, and ExcludeUpdateRgn call may inappropriately clip valid
1406 * child window contents from the copied parent window bits, but we
1407 * still want to avoid copying invalid window bits when possible.
1409 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_CLIPCHILDREN ))
1410 ExcludeUpdateRgn( hdc_dst, hwnd );
1412 code = X11DRV_START_EXPOSURES;
1413 ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
1415 TRACE( "copying bits for win %p/%lx %s -> %s\n",
1416 hwnd, window, wine_dbgstr_rect(&src_rect), wine_dbgstr_rect(&dst_rect) );
1417 BitBlt( hdc_dst, dst_rect.left, dst_rect.top,
1418 dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top,
1419 hdc_src, src_rect.left, src_rect.top, SRCCOPY );
1421 rgn = 0;
1422 code = X11DRV_END_EXPOSURES;
1423 ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, sizeof(rgn), (LPSTR)&rgn );
1425 ReleaseDC( hwnd, hdc_dst );
1426 if (hdc_src != hdc_dst) ReleaseDC( parent, hdc_src );
1428 if (rgn)
1430 if (!window)
1432 /* map region to client rect since we are using DCX_WINDOW */
1433 OffsetRgn( rgn, new_window_rect->left - new_client_rect->left,
1434 new_window_rect->top - new_client_rect->top );
1435 RedrawWindow( hwnd, NULL, rgn,
1436 RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ALLCHILDREN );
1438 else RedrawWindow( hwnd, NULL, rgn, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1439 DeleteObject( rgn );
1444 /***********************************************************************
1445 * get_dummy_parent
1447 * Create a dummy parent window for child windows that don't have a true X11 parent.
1449 static Window get_dummy_parent(void)
1451 static Window dummy_parent;
1453 if (!dummy_parent)
1455 XSetWindowAttributes attrib;
1457 attrib.override_redirect = True;
1458 attrib.border_pixel = 0;
1459 attrib.colormap = default_colormap;
1460 dummy_parent = XCreateWindow( gdi_display, root_window, -1, -1, 1, 1, 0, default_visual.depth,
1461 InputOutput, default_visual.visual,
1462 CWColormap | CWBorderPixel | CWOverrideRedirect, &attrib );
1463 XMapWindow( gdi_display, dummy_parent );
1465 return dummy_parent;
1469 /**********************************************************************
1470 * create_dummy_client_window
1472 Window create_dummy_client_window(void)
1474 XSetWindowAttributes attr;
1476 attr.colormap = default_colormap;
1477 attr.bit_gravity = NorthWestGravity;
1478 attr.win_gravity = NorthWestGravity;
1479 attr.backing_store = NotUseful;
1480 attr.border_pixel = 0;
1482 return XCreateWindow( gdi_display, get_dummy_parent(), 0, 0, 1, 1, 0,
1483 default_visual.depth, InputOutput, default_visual.visual,
1484 CWBitGravity | CWWinGravity | CWBackingStore | CWColormap | CWBorderPixel, &attr );
1487 /**********************************************************************
1488 * create_client_window
1490 Window create_client_window( HWND hwnd, const XVisualInfo *visual )
1492 Window dummy_parent = get_dummy_parent();
1493 struct x11drv_win_data *data = get_win_data( hwnd );
1494 XSetWindowAttributes attr;
1495 Window ret;
1496 int x, y, cx, cy;
1498 if (!data)
1500 /* explicitly create data for HWND_MESSAGE windows since they can be used for OpenGL */
1501 HWND parent = GetAncestor( hwnd, GA_PARENT );
1502 if (parent == GetDesktopWindow() || GetAncestor( parent, GA_PARENT )) return 0;
1503 if (!(data = alloc_win_data( thread_init_display(), hwnd ))) return 0;
1504 GetClientRect( hwnd, &data->client_rect );
1505 data->window_rect = data->whole_rect = data->client_rect;
1508 if (data->client_window)
1510 XDeleteContext( data->display, data->client_window, winContext );
1511 XReparentWindow( gdi_display, data->client_window, dummy_parent, 0, 0 );
1512 TRACE( "%p reparent xwin %lx/%lx\n", data->hwnd, data->whole_window, data->client_window );
1515 if (data->client_colormap) XFreeColormap( gdi_display, data->client_colormap );
1516 data->client_colormap = XCreateColormap( gdi_display, dummy_parent, visual->visual,
1517 (visual->class == PseudoColor ||
1518 visual->class == GrayScale ||
1519 visual->class == DirectColor) ? AllocAll : AllocNone );
1520 attr.colormap = data->client_colormap;
1521 attr.bit_gravity = NorthWestGravity;
1522 attr.win_gravity = NorthWestGravity;
1523 attr.backing_store = NotUseful;
1524 attr.border_pixel = 0;
1526 x = data->client_rect.left - data->whole_rect.left;
1527 y = data->client_rect.top - data->whole_rect.top;
1528 cx = min( max( 1, data->client_rect.right - data->client_rect.left ), 65535 );
1529 cy = min( max( 1, data->client_rect.bottom - data->client_rect.top ), 65535 );
1531 ret = data->client_window = XCreateWindow( gdi_display,
1532 data->whole_window ? data->whole_window : dummy_parent,
1533 x, y, cx, cy, 0, default_visual.depth, InputOutput,
1534 visual->visual, CWBitGravity | CWWinGravity |
1535 CWBackingStore | CWColormap | CWBorderPixel, &attr );
1536 if (data->client_window)
1538 XSaveContext( data->display, data->client_window, winContext, (char *)data->hwnd );
1539 XMapWindow( gdi_display, data->client_window );
1540 XSync( gdi_display, False );
1541 if (data->whole_window) XSelectInput( data->display, data->client_window, ExposureMask );
1542 TRACE( "%p xwin %lx/%lx\n", data->hwnd, data->whole_window, data->client_window );
1544 release_win_data( data );
1545 return ret;
1549 /**********************************************************************
1550 * create_whole_window
1552 * Create the whole X window for a given window
1554 static void create_whole_window( struct x11drv_win_data *data )
1556 int cx, cy, mask;
1557 XSetWindowAttributes attr;
1558 WCHAR text[1024];
1559 COLORREF key;
1560 BYTE alpha;
1561 DWORD layered_flags;
1562 HRGN win_rgn;
1563 POINT pos;
1565 if (!data->managed && is_window_managed( data->hwnd, SWP_NOACTIVATE, &data->window_rect ))
1567 TRACE( "making win %p/%lx managed\n", data->hwnd, data->whole_window );
1568 data->managed = TRUE;
1571 if ((win_rgn = CreateRectRgn( 0, 0, 0, 0 )) && GetWindowRgn( data->hwnd, win_rgn ) == ERROR)
1573 DeleteObject( win_rgn );
1574 win_rgn = 0;
1576 data->shaped = (win_rgn != 0);
1578 if (data->vis.visualid != default_visual.visualid)
1579 data->whole_colormap = XCreateColormap( data->display, root_window, data->vis.visual, AllocNone );
1581 mask = get_window_attributes( data, &attr );
1583 if (!(cx = data->whole_rect.right - data->whole_rect.left)) cx = 1;
1584 else if (cx > 65535) cx = 65535;
1585 if (!(cy = data->whole_rect.bottom - data->whole_rect.top)) cy = 1;
1586 else if (cy > 65535) cy = 65535;
1588 pos = virtual_screen_to_root( data->whole_rect.left, data->whole_rect.top );
1589 data->whole_window = XCreateWindow( data->display, root_window, pos.x, pos.y,
1590 cx, cy, 0, data->vis.depth, InputOutput,
1591 data->vis.visual, mask, &attr );
1592 if (!data->whole_window) goto done;
1594 set_initial_wm_hints( data->display, data->whole_window );
1595 set_wm_hints( data );
1597 XSaveContext( data->display, data->whole_window, winContext, (char *)data->hwnd );
1598 SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
1600 /* set the window text */
1601 if (!InternalGetWindowText( data->hwnd, text, ARRAY_SIZE( text ))) text[0] = 0;
1602 sync_window_text( data->display, data->whole_window, text );
1604 /* set the window region */
1605 if (win_rgn || IsRectEmpty( &data->window_rect )) sync_window_region( data, win_rgn );
1607 /* set the window opacity */
1608 if (!GetLayeredWindowAttributes( data->hwnd, &key, &alpha, &layered_flags )) layered_flags = 0;
1609 sync_window_opacity( data->display, data->whole_window, key, alpha, layered_flags );
1611 XFlush( data->display ); /* make sure the window exists before we start painting to it */
1613 sync_window_cursor( data->whole_window );
1615 done:
1616 if (win_rgn) DeleteObject( win_rgn );
1620 /**********************************************************************
1621 * destroy_whole_window
1623 * Destroy the whole X window for a given window.
1625 static void destroy_whole_window( struct x11drv_win_data *data, BOOL already_destroyed )
1627 TRACE( "win %p xwin %lx/%lx\n", data->hwnd, data->whole_window, data->client_window );
1629 if (data->client_window) XDeleteContext( data->display, data->client_window, winContext );
1631 if (!data->whole_window)
1633 if (data->embedded)
1635 Window xwin = (Window)GetPropA( data->hwnd, foreign_window_prop );
1636 if (xwin)
1638 if (!already_destroyed) XSelectInput( data->display, xwin, 0 );
1639 XDeleteContext( data->display, xwin, winContext );
1640 RemovePropA( data->hwnd, foreign_window_prop );
1642 return;
1645 else
1647 if (data->client_window && !already_destroyed)
1649 XSelectInput( data->display, data->client_window, 0 );
1650 XReparentWindow( data->display, data->client_window, get_dummy_parent(), 0, 0 );
1651 XSync( data->display, False );
1653 XDeleteContext( data->display, data->whole_window, winContext );
1654 if (!already_destroyed) XDestroyWindow( data->display, data->whole_window );
1656 if (data->whole_colormap) XFreeColormap( data->display, data->whole_colormap );
1657 data->whole_window = data->client_window = 0;
1658 data->whole_colormap = 0;
1659 data->wm_state = WithdrawnState;
1660 data->net_wm_state = 0;
1661 data->mapped = FALSE;
1662 if (data->xic)
1664 XUnsetICFocus( data->xic );
1665 XDestroyIC( data->xic );
1666 data->xic = 0;
1668 /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
1669 XFlush( data->display );
1670 if (data->surface) window_surface_release( data->surface );
1671 data->surface = NULL;
1672 RemovePropA( data->hwnd, whole_window_prop );
1676 /**********************************************************************
1677 * set_window_visual
1679 * Change the visual by destroying and recreating the X window if needed.
1681 void set_window_visual( struct x11drv_win_data *data, const XVisualInfo *vis, BOOL use_alpha )
1683 Window client_window = data->client_window;
1684 Window whole_window = data->whole_window;
1686 if (!data->use_alpha == !use_alpha) return;
1687 if (data->surface) window_surface_release( data->surface );
1688 data->surface = NULL;
1689 data->use_alpha = use_alpha;
1691 if (data->vis.visualid == vis->visualid) return;
1692 data->client_window = 0;
1693 destroy_whole_window( data, client_window != 0 /* don't destroy whole_window until reparented */ );
1694 data->vis = *vis;
1695 create_whole_window( data );
1696 if (!client_window) return;
1697 /* move the client to the new parent */
1698 XReparentWindow( data->display, client_window, data->whole_window,
1699 data->client_rect.left - data->whole_rect.left,
1700 data->client_rect.top - data->whole_rect.top );
1701 data->client_window = client_window;
1702 XDestroyWindow( data->display, whole_window );
1706 /*****************************************************************
1707 * SetWindowText (X11DRV.@)
1709 void CDECL X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
1711 Window win;
1713 if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(gdi_display))
1715 Display *display = thread_init_display();
1716 sync_window_text( display, win, text );
1721 /***********************************************************************
1722 * SetWindowStyle (X11DRV.@)
1724 * Update the X state of a window to reflect a style change
1726 void CDECL X11DRV_SetWindowStyle( HWND hwnd, INT offset, STYLESTRUCT *style )
1728 struct x11drv_win_data *data;
1729 DWORD changed = style->styleNew ^ style->styleOld;
1731 if (hwnd == GetDesktopWindow()) return;
1732 if (!(data = get_win_data( hwnd ))) return;
1733 if (!data->whole_window) goto done;
1735 if (offset == GWL_STYLE && (changed & WS_DISABLED)) set_wm_hints( data );
1737 if (offset == GWL_EXSTYLE && (changed & WS_EX_LAYERED)) /* changing WS_EX_LAYERED resets attributes */
1739 data->layered = FALSE;
1740 set_window_visual( data, &default_visual, FALSE );
1741 sync_window_opacity( data->display, data->whole_window, 0, 0, 0 );
1742 if (data->surface) set_surface_color_key( data->surface, CLR_INVALID );
1744 done:
1745 release_win_data( data );
1749 /***********************************************************************
1750 * DestroyWindow (X11DRV.@)
1752 void CDECL X11DRV_DestroyWindow( HWND hwnd )
1754 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1755 struct x11drv_win_data *data;
1757 if (!(data = get_win_data( hwnd ))) return;
1759 destroy_whole_window( data, FALSE );
1760 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
1761 if (thread_data->last_xic_hwnd == hwnd) thread_data->last_xic_hwnd = 0;
1762 if (data->icon_pixmap) XFreePixmap( gdi_display, data->icon_pixmap );
1763 if (data->icon_mask) XFreePixmap( gdi_display, data->icon_mask );
1764 if (data->client_colormap) XFreeColormap( data->display, data->client_colormap );
1765 HeapFree( GetProcessHeap(), 0, data->icon_bits );
1766 XDeleteContext( gdi_display, (XID)hwnd, win_data_context );
1767 release_win_data( data );
1768 HeapFree( GetProcessHeap(), 0, data );
1769 destroy_gl_drawable( hwnd );
1770 wine_vk_surface_destroy( hwnd );
1774 /***********************************************************************
1775 * X11DRV_DestroyNotify
1777 BOOL X11DRV_DestroyNotify( HWND hwnd, XEvent *event )
1779 struct x11drv_win_data *data;
1780 BOOL embedded;
1782 if (!(data = get_win_data( hwnd ))) return FALSE;
1783 embedded = data->embedded;
1784 if (!embedded) FIXME( "window %p/%lx destroyed from the outside\n", hwnd, data->whole_window );
1786 destroy_whole_window( data, TRUE );
1787 release_win_data( data );
1788 if (embedded) SendMessageW( hwnd, WM_CLOSE, 0, 0 );
1789 return TRUE;
1793 /* initialize the desktop window id in the desktop manager process */
1794 BOOL create_desktop_win_data( Window win )
1796 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1797 Display *display = thread_data->display;
1798 struct x11drv_win_data *data;
1800 if (!(data = alloc_win_data( display, GetDesktopWindow() ))) return FALSE;
1801 data->whole_window = win;
1802 data->managed = TRUE;
1803 SetPropA( data->hwnd, whole_window_prop, (HANDLE)win );
1804 set_initial_wm_hints( display, win );
1805 release_win_data( data );
1806 if (thread_data->clip_window) XReparentWindow( display, thread_data->clip_window, win, 0, 0 );
1807 return TRUE;
1810 /**********************************************************************
1811 * CreateDesktopWindow (X11DRV.@)
1813 BOOL CDECL X11DRV_CreateDesktopWindow( HWND hwnd )
1815 unsigned int width, height;
1817 /* retrieve the real size of the desktop */
1818 SERVER_START_REQ( get_window_rectangles )
1820 req->handle = wine_server_user_handle( hwnd );
1821 req->relative = COORDS_CLIENT;
1822 wine_server_call( req );
1823 width = reply->window.right;
1824 height = reply->window.bottom;
1826 SERVER_END_REQ;
1828 if (!width && !height) /* not initialized yet */
1830 RECT rect = get_virtual_screen_rect();
1832 SERVER_START_REQ( set_window_pos )
1834 req->handle = wine_server_user_handle( hwnd );
1835 req->previous = 0;
1836 req->swp_flags = SWP_NOZORDER;
1837 req->window.left = rect.left;
1838 req->window.top = rect.top;
1839 req->window.right = rect.right;
1840 req->window.bottom = rect.bottom;
1841 req->client = req->window;
1842 wine_server_call( req );
1844 SERVER_END_REQ;
1846 else
1848 Window win = (Window)GetPropA( hwnd, whole_window_prop );
1849 if (win && win != root_window) X11DRV_init_desktop( win, width, height );
1851 return TRUE;
1855 static WNDPROC desktop_orig_wndproc;
1857 #define WM_WINE_NOTIFY_ACTIVITY WM_USER
1859 static LRESULT CALLBACK desktop_wndproc_wrapper( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
1861 switch (msg)
1863 case WM_WINE_NOTIFY_ACTIVITY:
1865 static ULONGLONG last = 0;
1866 ULONGLONG now = GetTickCount64();
1867 /* calling XResetScreenSaver too often can cause performance
1868 * problems, so throttle it */
1869 if (now > last + 5000)
1871 XResetScreenSaver( gdi_display );
1872 XFlush( gdi_display );
1873 last = now;
1875 break;
1878 return desktop_orig_wndproc( hwnd, msg, wp, lp );
1881 /**********************************************************************
1882 * CreateWindow (X11DRV.@)
1884 BOOL CDECL X11DRV_CreateWindow( HWND hwnd )
1886 if (hwnd == GetDesktopWindow())
1888 struct x11drv_thread_data *data = x11drv_init_thread_data();
1889 XSetWindowAttributes attr;
1891 desktop_orig_wndproc = (WNDPROC)SetWindowLongPtrW( hwnd, GWLP_WNDPROC,
1892 (LONG_PTR)desktop_wndproc_wrapper );
1894 /* create the cursor clipping window */
1895 attr.override_redirect = TRUE;
1896 attr.event_mask = StructureNotifyMask | FocusChangeMask;
1897 data->clip_window = XCreateWindow( data->display, root_window, 0, 0, 1, 1, 0, 0,
1898 InputOnly, default_visual.visual,
1899 CWOverrideRedirect | CWEventMask, &attr );
1900 XFlush( data->display );
1901 SetPropA( hwnd, clip_window_prop, (HANDLE)data->clip_window );
1902 X11DRV_InitClipboard();
1903 X11DRV_DisplayDevices_RegisterEventHandlers();
1905 return TRUE;
1909 /***********************************************************************
1910 * get_win_data
1912 * Lock and return the X11 data structure associated with a window.
1914 struct x11drv_win_data *get_win_data( HWND hwnd )
1916 char *data;
1918 if (!hwnd) return NULL;
1919 EnterCriticalSection( &win_data_section );
1920 if (!XFindContext( gdi_display, (XID)hwnd, win_data_context, &data ))
1921 return (struct x11drv_win_data *)data;
1922 LeaveCriticalSection( &win_data_section );
1923 return NULL;
1927 /***********************************************************************
1928 * release_win_data
1930 * Release the data returned by get_win_data.
1932 void release_win_data( struct x11drv_win_data *data )
1934 if (data) LeaveCriticalSection( &win_data_section );
1938 /***********************************************************************
1939 * X11DRV_create_win_data
1941 * Create an X11 data window structure for an existing window.
1943 static struct x11drv_win_data *X11DRV_create_win_data( HWND hwnd, const RECT *window_rect,
1944 const RECT *client_rect )
1946 Display *display;
1947 struct x11drv_win_data *data;
1948 HWND parent;
1950 if (!(parent = GetAncestor( hwnd, GA_PARENT ))) return NULL; /* desktop */
1952 /* don't create win data for HWND_MESSAGE windows */
1953 if (parent != GetDesktopWindow() && !GetAncestor( parent, GA_PARENT )) return NULL;
1955 if (GetWindowThreadProcessId( hwnd, NULL ) != GetCurrentThreadId()) return NULL;
1957 /* Recreate the parent gl_drawable now that we know there are child windows
1958 * that will need clipping support.
1960 sync_gl_drawable( parent, TRUE );
1962 display = thread_init_display();
1963 init_clip_window(); /* make sure the clip window is initialized in this thread */
1965 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1967 data->whole_rect = data->window_rect = *window_rect;
1968 data->client_rect = *client_rect;
1969 if (parent == GetDesktopWindow())
1971 create_whole_window( data );
1972 TRACE( "win %p/%lx window %s whole %s client %s\n",
1973 hwnd, data->whole_window, wine_dbgstr_rect( &data->window_rect ),
1974 wine_dbgstr_rect( &data->whole_rect ), wine_dbgstr_rect( &data->client_rect ));
1976 return data;
1980 /* window procedure for foreign windows */
1981 static LRESULT WINAPI foreign_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1983 switch(msg)
1985 case WM_WINDOWPOSCHANGED:
1986 update_systray_balloon_position();
1987 break;
1988 case WM_PARENTNOTIFY:
1989 if (LOWORD(wparam) == WM_DESTROY)
1991 TRACE( "%p: got parent notify destroy for win %lx\n", hwnd, lparam );
1992 PostMessageW( hwnd, WM_CLOSE, 0, 0 ); /* so that we come back here once the child is gone */
1994 return 0;
1995 case WM_CLOSE:
1996 if (GetWindow( hwnd, GW_CHILD )) return 0; /* refuse to die if we still have children */
1997 break;
1999 return DefWindowProcW( hwnd, msg, wparam, lparam );
2003 /***********************************************************************
2004 * create_foreign_window
2006 * Create a foreign window for the specified X window and its ancestors
2008 HWND create_foreign_window( Display *display, Window xwin )
2010 static const WCHAR classW[] = {'_','_','w','i','n','e','_','x','1','1','_','f','o','r','e','i','g','n','_','w','i','n','d','o','w',0};
2011 static BOOL class_registered;
2012 struct x11drv_win_data *data;
2013 HWND hwnd, parent;
2014 POINT pos;
2015 Window xparent, xroot;
2016 Window *xchildren;
2017 unsigned int nchildren;
2018 XWindowAttributes attr;
2019 DWORD style = WS_CLIPCHILDREN;
2021 if (!class_registered)
2023 WNDCLASSEXW class;
2025 memset( &class, 0, sizeof(class) );
2026 class.cbSize = sizeof(class);
2027 class.lpfnWndProc = foreign_window_proc;
2028 class.lpszClassName = classW;
2029 if (!RegisterClassExW( &class ) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS)
2031 ERR( "Could not register foreign window class\n" );
2032 return FALSE;
2034 class_registered = TRUE;
2037 if (XFindContext( display, xwin, winContext, (char **)&hwnd )) hwnd = 0;
2038 if (hwnd) return hwnd; /* already created */
2040 XSelectInput( display, xwin, StructureNotifyMask );
2041 if (!XGetWindowAttributes( display, xwin, &attr ) ||
2042 !XQueryTree( display, xwin, &xroot, &xparent, &xchildren, &nchildren ))
2044 XSelectInput( display, xwin, 0 );
2045 return 0;
2047 XFree( xchildren );
2049 if (xparent == xroot)
2051 parent = GetDesktopWindow();
2052 style |= WS_POPUP;
2053 pos = root_to_virtual_screen( attr.x, attr.y );
2055 else
2057 parent = create_foreign_window( display, xparent );
2058 style |= WS_CHILD;
2059 pos.x = attr.x;
2060 pos.y = attr.y;
2063 hwnd = CreateWindowW( classW, NULL, style, pos.x, pos.y, attr.width, attr.height,
2064 parent, 0, 0, NULL );
2066 if (!(data = alloc_win_data( display, hwnd )))
2068 DestroyWindow( hwnd );
2069 return 0;
2071 SetRect( &data->window_rect, pos.x, pos.y, pos.x + attr.width, pos.y + attr.height );
2072 data->whole_rect = data->client_rect = data->window_rect;
2073 data->whole_window = data->client_window = 0;
2074 data->embedded = TRUE;
2075 data->mapped = TRUE;
2077 SetPropA( hwnd, foreign_window_prop, (HANDLE)xwin );
2078 XSaveContext( display, xwin, winContext, (char *)data->hwnd );
2080 TRACE( "win %lx parent %p style %08x %s -> hwnd %p\n",
2081 xwin, parent, style, wine_dbgstr_rect(&data->window_rect), hwnd );
2083 release_win_data( data );
2085 ShowWindow( hwnd, SW_SHOW );
2086 return hwnd;
2090 /***********************************************************************
2091 * X11DRV_get_whole_window
2093 * Return the X window associated with the full area of a window
2095 Window X11DRV_get_whole_window( HWND hwnd )
2097 struct x11drv_win_data *data = get_win_data( hwnd );
2098 Window ret;
2100 if (!data)
2102 if (hwnd == GetDesktopWindow()) return root_window;
2103 return (Window)GetPropA( hwnd, whole_window_prop );
2105 ret = data->whole_window;
2106 release_win_data( data );
2107 return ret;
2111 /***********************************************************************
2112 * X11DRV_get_ic
2114 * Return the X input context associated with a window
2116 XIC X11DRV_get_ic( HWND hwnd )
2118 struct x11drv_win_data *data = get_win_data( hwnd );
2119 XIM xim;
2120 XIC ret = 0;
2122 if (data)
2124 x11drv_thread_data()->last_xic_hwnd = hwnd;
2125 ret = data->xic;
2126 if (!ret && (xim = x11drv_thread_data()->xim)) ret = X11DRV_CreateIC( xim, data );
2127 release_win_data( data );
2129 return ret;
2133 /***********************************************************************
2134 * X11DRV_GetDC (X11DRV.@)
2136 void CDECL X11DRV_GetDC( HDC hdc, HWND hwnd, HWND top, const RECT *win_rect,
2137 const RECT *top_rect, DWORD flags )
2139 struct x11drv_escape_set_drawable escape;
2140 HWND parent;
2142 escape.code = X11DRV_SET_DRAWABLE;
2143 escape.mode = IncludeInferiors;
2145 escape.dc_rect.left = win_rect->left - top_rect->left;
2146 escape.dc_rect.top = win_rect->top - top_rect->top;
2147 escape.dc_rect.right = win_rect->right - top_rect->left;
2148 escape.dc_rect.bottom = win_rect->bottom - top_rect->top;
2150 if (top == hwnd)
2152 struct x11drv_win_data *data = get_win_data( hwnd );
2154 escape.drawable = data ? data->whole_window : X11DRV_get_whole_window( hwnd );
2156 /* special case: when repainting the root window, clip out top-level windows */
2157 if (data && data->whole_window == root_window) escape.mode = ClipByChildren;
2158 release_win_data( data );
2160 else
2162 /* find the first ancestor that has a drawable */
2163 for (parent = hwnd; parent && parent != top; parent = GetAncestor( parent, GA_PARENT ))
2164 if ((escape.drawable = X11DRV_get_whole_window( parent ))) break;
2166 if (escape.drawable)
2168 POINT pt = { 0, 0 };
2169 MapWindowPoints( 0, parent, &pt, 1 );
2170 escape.dc_rect = *win_rect;
2171 OffsetRect( &escape.dc_rect, pt.x, pt.y );
2172 if (flags & DCX_CLIPCHILDREN) escape.mode = ClipByChildren;
2174 else escape.drawable = X11DRV_get_whole_window( top );
2177 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2181 /***********************************************************************
2182 * X11DRV_ReleaseDC (X11DRV.@)
2184 void CDECL X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
2186 struct x11drv_escape_set_drawable escape;
2188 escape.code = X11DRV_SET_DRAWABLE;
2189 escape.drawable = root_window;
2190 escape.mode = IncludeInferiors;
2191 escape.dc_rect = get_virtual_screen_rect();
2192 OffsetRect( &escape.dc_rect, -2 * escape.dc_rect.left, -2 * escape.dc_rect.top );
2193 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2197 /*************************************************************************
2198 * ScrollDC (X11DRV.@)
2200 BOOL CDECL X11DRV_ScrollDC( HDC hdc, INT dx, INT dy, HRGN update )
2202 RECT rect;
2203 BOOL ret;
2204 HRGN expose_rgn = 0;
2206 GetClipBox( hdc, &rect );
2208 if (update)
2210 INT code = X11DRV_START_EXPOSURES;
2211 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
2213 ret = BitBlt( hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
2214 hdc, rect.left - dx, rect.top - dy, SRCCOPY );
2216 code = X11DRV_END_EXPOSURES;
2217 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code,
2218 sizeof(expose_rgn), (LPSTR)&expose_rgn );
2219 if (expose_rgn)
2221 CombineRgn( update, update, expose_rgn, RGN_OR );
2222 DeleteObject( expose_rgn );
2225 else ret = BitBlt( hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
2226 hdc, rect.left - dx, rect.top - dy, SRCCOPY );
2228 return ret;
2232 /***********************************************************************
2233 * SetCapture (X11DRV.@)
2235 void CDECL X11DRV_SetCapture( HWND hwnd, UINT flags )
2237 struct x11drv_thread_data *thread_data = x11drv_thread_data();
2238 struct x11drv_win_data *data;
2240 if (!(flags & (GUI_INMOVESIZE | GUI_INMENUMODE))) return;
2242 if (hwnd)
2244 if (!(data = get_win_data( GetAncestor( hwnd, GA_ROOT )))) return;
2245 if (data->whole_window)
2247 XFlush( gdi_display );
2248 XGrabPointer( data->display, data->whole_window, False,
2249 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
2250 GrabModeAsync, GrabModeAsync, None, None, CurrentTime );
2251 thread_data->grab_hwnd = data->hwnd;
2253 release_win_data( data );
2255 else /* release capture */
2257 if (!(data = get_win_data( thread_data->grab_hwnd ))) return;
2258 XFlush( gdi_display );
2259 XUngrabPointer( data->display, CurrentTime );
2260 XFlush( data->display );
2261 thread_data->grab_hwnd = NULL;
2262 release_win_data( data );
2267 /*****************************************************************
2268 * SetParent (X11DRV.@)
2270 void CDECL X11DRV_SetParent( HWND hwnd, HWND parent, HWND old_parent )
2272 struct x11drv_win_data *data;
2274 if (parent == old_parent) return;
2275 if (!(data = get_win_data( hwnd ))) return;
2276 if (data->embedded) goto done;
2278 if (parent != GetDesktopWindow()) /* a child window */
2280 if (old_parent == GetDesktopWindow())
2282 /* destroy the old X windows */
2283 destroy_whole_window( data, FALSE );
2284 data->managed = FALSE;
2287 else /* new top level window */
2289 create_whole_window( data );
2291 done:
2292 release_win_data( data );
2293 set_gl_drawable_parent( hwnd, parent );
2295 /* Recreate the parent gl_drawable now that we know there are child windows
2296 * that will need clipping support.
2298 sync_gl_drawable( parent, TRUE );
2300 fetch_icon_data( hwnd, 0, 0 );
2304 static inline BOOL get_surface_rect( const RECT *visible_rect, RECT *surface_rect )
2306 *surface_rect = get_virtual_screen_rect();
2308 if (!IntersectRect( surface_rect, surface_rect, visible_rect )) return FALSE;
2309 OffsetRect( surface_rect, -visible_rect->left, -visible_rect->top );
2310 surface_rect->left &= ~31;
2311 surface_rect->top &= ~31;
2312 surface_rect->right = max( surface_rect->left + 32, (surface_rect->right + 31) & ~31 );
2313 surface_rect->bottom = max( surface_rect->top + 32, (surface_rect->bottom + 31) & ~31 );
2314 return TRUE;
2318 /***********************************************************************
2319 * WindowPosChanging (X11DRV.@)
2321 BOOL CDECL X11DRV_WindowPosChanging( HWND hwnd, HWND insert_after, UINT swp_flags,
2322 const RECT *window_rect, const RECT *client_rect, RECT *visible_rect,
2323 struct window_surface **surface )
2325 struct x11drv_win_data *data = get_win_data( hwnd );
2326 RECT surface_rect;
2327 DWORD flags;
2328 COLORREF key;
2329 BOOL layered = GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYERED;
2331 if (!data && !(data = X11DRV_create_win_data( hwnd, window_rect, client_rect ))) return TRUE;
2333 /* check if we need to switch the window to managed */
2334 if (!data->managed && data->whole_window && is_window_managed( hwnd, swp_flags, window_rect ))
2336 TRACE( "making win %p/%lx managed\n", hwnd, data->whole_window );
2337 release_win_data( data );
2338 unmap_window( hwnd );
2339 if (!(data = get_win_data( hwnd ))) return TRUE;
2340 data->managed = TRUE;
2343 *visible_rect = *window_rect;
2344 X11DRV_window_to_X_rect( data, visible_rect, window_rect, client_rect );
2346 /* create the window surface if necessary */
2348 if (!data->whole_window && !data->embedded) goto done;
2349 if (swp_flags & SWP_HIDEWINDOW) goto done;
2350 if (data->use_alpha) goto done;
2351 if (!get_surface_rect( visible_rect, &surface_rect )) goto done;
2353 if (*surface) window_surface_release( *surface );
2354 *surface = NULL; /* indicate that we want to draw directly to the window */
2356 if (data->embedded) goto done;
2357 if (data->whole_window == root_window) goto done;
2358 if (data->client_window) goto done;
2359 if (!client_side_graphics && !layered) goto done;
2361 if (data->surface)
2363 if (EqualRect( &data->surface->rect, &surface_rect ))
2365 /* existing surface is good enough */
2366 window_surface_add_ref( data->surface );
2367 *surface = data->surface;
2368 goto done;
2371 else if (!(swp_flags & SWP_SHOWWINDOW) && !(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)) goto done;
2373 if (!layered || !GetLayeredWindowAttributes( hwnd, &key, NULL, &flags ) || !(flags & LWA_COLORKEY))
2374 key = CLR_INVALID;
2376 *surface = create_surface( data->whole_window, &data->vis, &surface_rect, key, FALSE );
2378 done:
2379 release_win_data( data );
2380 return TRUE;
2384 /***********************************************************************
2385 * WindowPosChanged (X11DRV.@)
2387 void CDECL X11DRV_WindowPosChanged( HWND hwnd, HWND insert_after, UINT swp_flags,
2388 const RECT *rectWindow, const RECT *rectClient,
2389 const RECT *visible_rect, const RECT *valid_rects,
2390 struct window_surface *surface )
2392 struct x11drv_thread_data *thread_data;
2393 struct x11drv_win_data *data;
2394 DWORD new_style = GetWindowLongW( hwnd, GWL_STYLE );
2395 RECT old_window_rect, old_whole_rect, old_client_rect;
2396 int event_type;
2398 if (!(data = get_win_data( hwnd ))) return;
2400 thread_data = x11drv_thread_data();
2402 old_window_rect = data->window_rect;
2403 old_whole_rect = data->whole_rect;
2404 old_client_rect = data->client_rect;
2405 data->window_rect = *rectWindow;
2406 data->whole_rect = *visible_rect;
2407 data->client_rect = *rectClient;
2408 if (data->vis.visualid == default_visual.visualid)
2410 if (surface) window_surface_add_ref( surface );
2411 if (data->surface) window_surface_release( data->surface );
2412 data->surface = surface;
2415 TRACE( "win %p window %s client %s style %08x flags %08x\n",
2416 hwnd, wine_dbgstr_rect(rectWindow), wine_dbgstr_rect(rectClient), new_style, swp_flags );
2418 if (!IsRectEmpty( &valid_rects[0] ))
2420 Window window = data->whole_window;
2421 int x_offset = old_whole_rect.left - data->whole_rect.left;
2422 int y_offset = old_whole_rect.top - data->whole_rect.top;
2424 /* if all that happened is that the whole window moved, copy everything */
2425 if (!(swp_flags & SWP_FRAMECHANGED) &&
2426 old_whole_rect.right - data->whole_rect.right == x_offset &&
2427 old_whole_rect.bottom - data->whole_rect.bottom == y_offset &&
2428 old_client_rect.left - data->client_rect.left == x_offset &&
2429 old_client_rect.right - data->client_rect.right == x_offset &&
2430 old_client_rect.top - data->client_rect.top == y_offset &&
2431 old_client_rect.bottom - data->client_rect.bottom == y_offset &&
2432 EqualRect( &valid_rects[0], &data->client_rect ))
2434 /* if we have an X window the bits will be moved by the X server */
2435 if (!window && (x_offset != 0 || y_offset != 0))
2437 release_win_data( data );
2438 move_window_bits( hwnd, window, &old_whole_rect, visible_rect,
2439 &old_client_rect, rectClient, rectWindow );
2440 if (!(data = get_win_data( hwnd ))) return;
2443 else
2445 release_win_data( data );
2446 move_window_bits( hwnd, window, &valid_rects[1], &valid_rects[0],
2447 &old_client_rect, rectClient, rectWindow );
2448 if (!(data = get_win_data( hwnd ))) return;
2452 XFlush( gdi_display ); /* make sure painting is done before we move the window */
2454 sync_client_position( data, &old_client_rect, &old_whole_rect );
2456 if (!data->whole_window)
2458 BOOL needs_resize = (!data->client_window &&
2459 (data->client_rect.right - data->client_rect.left !=
2460 old_client_rect.right - old_client_rect.left ||
2461 data->client_rect.bottom - data->client_rect.top !=
2462 old_client_rect.bottom - old_client_rect.top));
2463 release_win_data( data );
2464 if (needs_resize) sync_gl_drawable( hwnd, FALSE );
2465 return;
2468 /* check if we are currently processing an event relevant to this window */
2469 event_type = 0;
2470 if (thread_data &&
2471 thread_data->current_event &&
2472 thread_data->current_event->xany.window == data->whole_window)
2474 event_type = thread_data->current_event->type;
2475 if (event_type != ConfigureNotify && event_type != PropertyNotify &&
2476 event_type != GravityNotify && event_type != ReparentNotify)
2477 event_type = 0; /* ignore other events */
2480 if (data->mapped && event_type != ReparentNotify)
2482 if (((swp_flags & SWP_HIDEWINDOW) && !(new_style & WS_VISIBLE)) ||
2483 (!event_type && !(new_style & WS_MINIMIZE) &&
2484 !is_window_rect_mapped( rectWindow ) && is_window_rect_mapped( &old_window_rect )))
2486 release_win_data( data );
2487 unmap_window( hwnd );
2488 if (is_window_rect_full_screen( &old_window_rect )) reset_clipping_window();
2489 if (!(data = get_win_data( hwnd ))) return;
2493 /* don't change position if we are about to minimize or maximize a managed window */
2494 if (!event_type &&
2495 !(data->managed && (swp_flags & SWP_STATECHANGED) && (new_style & (WS_MINIMIZE|WS_MAXIMIZE))))
2496 sync_window_position( data, swp_flags, &old_window_rect, &old_whole_rect, &old_client_rect );
2498 if ((new_style & WS_VISIBLE) &&
2499 ((new_style & WS_MINIMIZE) || is_window_rect_mapped( rectWindow )))
2501 if (!data->mapped)
2503 BOOL needs_icon = !data->icon_pixmap;
2504 BOOL needs_map = TRUE;
2506 /* layered windows are mapped only once their attributes are set */
2507 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYERED)
2508 needs_map = data->layered || IsRectEmpty( rectWindow );
2509 release_win_data( data );
2510 if (needs_icon) fetch_icon_data( hwnd, 0, 0 );
2511 if (needs_map) map_window( hwnd, new_style );
2512 return;
2514 else if ((swp_flags & SWP_STATECHANGED) && (!data->iconic != !(new_style & WS_MINIMIZE)))
2516 set_wm_hints( data );
2517 data->iconic = (new_style & WS_MINIMIZE) != 0;
2518 TRACE( "changing win %p iconic state to %u\n", data->hwnd, data->iconic );
2519 if (data->iconic)
2520 XIconifyWindow( data->display, data->whole_window, data->vis.screen );
2521 else if (is_window_rect_mapped( rectWindow ))
2522 XMapWindow( data->display, data->whole_window );
2523 update_net_wm_states( data );
2525 else
2527 if (swp_flags & (SWP_FRAMECHANGED|SWP_STATECHANGED)) set_wm_hints( data );
2528 if (!event_type) update_net_wm_states( data );
2532 XFlush( data->display ); /* make sure changes are done before we start painting again */
2533 if (data->surface && data->vis.visualid != default_visual.visualid)
2534 data->surface->funcs->flush( data->surface );
2536 release_win_data( data );
2539 /* check if the window icon should be hidden (i.e. moved off-screen) */
2540 static BOOL hide_icon( struct x11drv_win_data *data )
2542 static const WCHAR trayW[] = {'S','h','e','l','l','_','T','r','a','y','W','n','d',0};
2544 if (data->managed) return TRUE;
2545 /* hide icons in desktop mode when the taskbar is active */
2546 if (!is_virtual_desktop()) return FALSE;
2547 return IsWindowVisible( FindWindowW( trayW, NULL ));
2550 /***********************************************************************
2551 * ShowWindow (X11DRV.@)
2553 UINT CDECL X11DRV_ShowWindow( HWND hwnd, INT cmd, RECT *rect, UINT swp )
2555 int x, y;
2556 unsigned int width, height, border, depth;
2557 Window root, top;
2558 POINT pos;
2559 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
2560 struct x11drv_thread_data *thread_data = x11drv_thread_data();
2561 struct x11drv_win_data *data = get_win_data( hwnd );
2563 if (!data || !data->whole_window) goto done;
2564 if (style & WS_MINIMIZE)
2566 if (((rect->left != -32000 || rect->top != -32000)) && hide_icon( data ))
2568 OffsetRect( rect, -32000 - rect->left, -32000 - rect->top );
2569 swp &= ~(SWP_NOMOVE | SWP_NOCLIENTMOVE);
2571 goto done;
2573 if (!data->managed || !data->mapped || data->iconic) goto done;
2575 /* only fetch the new rectangle if the ShowWindow was a result of a window manager event */
2577 if (!thread_data->current_event || thread_data->current_event->xany.window != data->whole_window)
2578 goto done;
2580 if (thread_data->current_event->type != ConfigureNotify &&
2581 thread_data->current_event->type != PropertyNotify)
2582 goto done;
2584 TRACE( "win %p/%lx cmd %d at %s flags %08x\n",
2585 hwnd, data->whole_window, cmd, wine_dbgstr_rect(rect), swp );
2587 XGetGeometry( thread_data->display, data->whole_window,
2588 &root, &x, &y, &width, &height, &border, &depth );
2589 XTranslateCoordinates( thread_data->display, data->whole_window, root, 0, 0, &x, &y, &top );
2590 pos = root_to_virtual_screen( x, y );
2591 X11DRV_X_to_window_rect( data, rect, pos.x, pos.y, width, height );
2592 swp &= ~(SWP_NOMOVE | SWP_NOCLIENTMOVE | SWP_NOSIZE | SWP_NOCLIENTSIZE);
2594 done:
2595 release_win_data( data );
2596 return swp;
2600 /**********************************************************************
2601 * SetWindowIcon (X11DRV.@)
2603 * hIcon or hIconSm has changed (or is being initialised for the
2604 * first time). Complete the X11 driver-specific initialisation
2605 * and set the window hints.
2607 void CDECL X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
2609 struct x11drv_win_data *data;
2611 if (!(data = get_win_data( hwnd ))) return;
2612 if (!data->whole_window) goto done;
2613 release_win_data( data ); /* release the lock, fetching the icon requires sending messages */
2615 if (type == ICON_BIG) fetch_icon_data( hwnd, icon, 0 );
2616 else fetch_icon_data( hwnd, 0, icon );
2618 if (!(data = get_win_data( hwnd ))) return;
2619 set_wm_hints( data );
2620 done:
2621 release_win_data( data );
2625 /***********************************************************************
2626 * SetWindowRgn (X11DRV.@)
2628 * Assign specified region to window (for non-rectangular windows)
2630 void CDECL X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
2632 struct x11drv_win_data *data;
2634 if ((data = get_win_data( hwnd )))
2636 sync_window_region( data, hrgn );
2637 release_win_data( data );
2639 else if (X11DRV_get_whole_window( hwnd ))
2641 SendMessageW( hwnd, WM_X11DRV_SET_WIN_REGION, 0, 0 );
2646 /***********************************************************************
2647 * SetLayeredWindowAttributes (X11DRV.@)
2649 * Set transparency attributes for a layered window.
2651 void CDECL X11DRV_SetLayeredWindowAttributes( HWND hwnd, COLORREF key, BYTE alpha, DWORD flags )
2653 struct x11drv_win_data *data = get_win_data( hwnd );
2655 if (data)
2657 set_window_visual( data, &default_visual, FALSE );
2659 if (data->whole_window)
2660 sync_window_opacity( data->display, data->whole_window, key, alpha, flags );
2661 if (data->surface)
2662 set_surface_color_key( data->surface, (flags & LWA_COLORKEY) ? key : CLR_INVALID );
2664 data->layered = TRUE;
2665 if (!data->mapped) /* mapping is delayed until attributes are set */
2667 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
2669 if ((style & WS_VISIBLE) &&
2670 ((style & WS_MINIMIZE) || is_window_rect_mapped( &data->window_rect )))
2672 release_win_data( data );
2673 map_window( hwnd, style );
2674 return;
2677 release_win_data( data );
2679 else
2681 Window win = X11DRV_get_whole_window( hwnd );
2682 if (win)
2684 sync_window_opacity( gdi_display, win, key, alpha, flags );
2685 if (flags & LWA_COLORKEY)
2686 FIXME( "LWA_COLORKEY not supported on foreign process window %p\n", hwnd );
2692 /*****************************************************************************
2693 * UpdateLayeredWindow (X11DRV.@)
2695 BOOL CDECL X11DRV_UpdateLayeredWindow( HWND hwnd, const UPDATELAYEREDWINDOWINFO *info,
2696 const RECT *window_rect )
2698 struct window_surface *surface;
2699 struct x11drv_win_data *data;
2700 BLENDFUNCTION blend = { AC_SRC_OVER, 0, 255, 0 };
2701 COLORREF color_key = (info->dwFlags & ULW_COLORKEY) ? info->crKey : CLR_INVALID;
2702 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
2703 BITMAPINFO *bmi = (BITMAPINFO *)buffer;
2704 void *src_bits, *dst_bits;
2705 RECT rect, src_rect;
2706 HDC hdc = 0;
2707 HBITMAP dib;
2708 BOOL ret = FALSE;
2710 if (!(data = get_win_data( hwnd ))) return FALSE;
2712 data->layered = TRUE;
2713 if (!data->embedded && argb_visual.visualid) set_window_visual( data, &argb_visual, TRUE );
2715 rect = *window_rect;
2716 OffsetRect( &rect, -window_rect->left, -window_rect->top );
2718 surface = data->surface;
2719 if (!surface || !EqualRect( &surface->rect, &rect ))
2721 data->surface = create_surface( data->whole_window, &data->vis, &rect,
2722 color_key, data->use_alpha );
2723 if (surface) window_surface_release( surface );
2724 surface = data->surface;
2726 else set_surface_color_key( surface, color_key );
2728 if (surface) window_surface_add_ref( surface );
2729 release_win_data( data );
2731 if (!surface) return FALSE;
2732 if (!info->hdcSrc)
2734 window_surface_release( surface );
2735 return TRUE;
2738 dst_bits = surface->funcs->get_info( surface, bmi );
2740 if (!(dib = CreateDIBSection( info->hdcDst, bmi, DIB_RGB_COLORS, &src_bits, NULL, 0 ))) goto done;
2741 if (!(hdc = CreateCompatibleDC( 0 ))) goto done;
2743 SelectObject( hdc, dib );
2745 surface->funcs->lock( surface );
2747 if (info->prcDirty)
2749 IntersectRect( &rect, &rect, info->prcDirty );
2750 memcpy( src_bits, dst_bits, bmi->bmiHeader.biSizeImage );
2751 PatBlt( hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, BLACKNESS );
2753 src_rect = rect;
2754 if (info->pptSrc) OffsetRect( &src_rect, info->pptSrc->x, info->pptSrc->y );
2755 DPtoLP( info->hdcSrc, (POINT *)&src_rect, 2 );
2757 ret = GdiAlphaBlend( hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
2758 info->hdcSrc, src_rect.left, src_rect.top,
2759 src_rect.right - src_rect.left, src_rect.bottom - src_rect.top,
2760 (info->dwFlags & ULW_ALPHA) ? *info->pblend : blend );
2761 if (ret)
2763 memcpy( dst_bits, src_bits, bmi->bmiHeader.biSizeImage );
2764 add_bounds_rect( surface->funcs->get_bounds( surface ), &rect );
2767 surface->funcs->unlock( surface );
2768 surface->funcs->flush( surface );
2770 done:
2771 window_surface_release( surface );
2772 if (hdc) DeleteDC( hdc );
2773 if (dib) DeleteObject( dib );
2774 return ret;
2778 /**********************************************************************
2779 * X11DRV_WindowMessage (X11DRV.@)
2781 LRESULT CDECL X11DRV_WindowMessage( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
2783 struct x11drv_win_data *data;
2785 switch(msg)
2787 case WM_X11DRV_UPDATE_CLIPBOARD:
2788 return update_clipboard( hwnd );
2789 case WM_X11DRV_SET_WIN_REGION:
2790 if ((data = get_win_data( hwnd )))
2792 sync_window_region( data, (HRGN)1 );
2793 release_win_data( data );
2795 return 0;
2796 case WM_X11DRV_RESIZE_DESKTOP:
2797 X11DRV_resize_desktop( (BOOL)lp );
2798 return 0;
2799 case WM_X11DRV_SET_CURSOR:
2800 if ((data = get_win_data( hwnd )))
2802 Window win = data->whole_window;
2803 release_win_data( data );
2804 if (win) set_window_cursor( win, (HCURSOR)lp );
2806 else if (hwnd == x11drv_thread_data()->clip_hwnd)
2807 set_window_cursor( x11drv_thread_data()->clip_window, (HCURSOR)lp );
2808 return 0;
2809 case WM_X11DRV_CLIP_CURSOR_NOTIFY:
2810 return clip_cursor_notify( hwnd, (HWND)wp, (HWND)lp );
2811 case WM_X11DRV_CLIP_CURSOR_REQUEST:
2812 return clip_cursor_request( hwnd, (BOOL)wp, (BOOL)lp );
2813 default:
2814 FIXME( "got window msg %x hwnd %p wp %lx lp %lx\n", msg, hwnd, wp, lp );
2815 return 0;
2820 /***********************************************************************
2821 * is_netwm_supported
2823 static BOOL is_netwm_supported( Display *display, Atom atom )
2825 static Atom *net_supported;
2826 static int net_supported_count = -1;
2827 int i;
2829 if (net_supported_count == -1)
2831 Atom type;
2832 int format;
2833 unsigned long count, remaining;
2835 if (!XGetWindowProperty( display, DefaultRootWindow(display), x11drv_atom(_NET_SUPPORTED), 0,
2836 ~0UL, False, XA_ATOM, &type, &format, &count,
2837 &remaining, (unsigned char **)&net_supported ))
2838 net_supported_count = get_property_size( format, count ) / sizeof(Atom);
2839 else
2840 net_supported_count = 0;
2843 for (i = 0; i < net_supported_count; i++)
2844 if (net_supported[i] == atom) return TRUE;
2845 return FALSE;
2849 /***********************************************************************
2850 * start_screensaver
2852 static LRESULT start_screensaver(void)
2854 if (!is_virtual_desktop())
2856 const char *argv[3] = { "xdg-screensaver", "activate", NULL };
2857 int pid = _spawnvp( _P_DETACH, argv[0], argv );
2858 if (pid > 0)
2860 TRACE( "started process %d\n", pid );
2861 return 0;
2864 return -1;
2868 /***********************************************************************
2869 * SysCommand (X11DRV.@)
2871 * Perform WM_SYSCOMMAND handling.
2873 LRESULT CDECL X11DRV_SysCommand( HWND hwnd, WPARAM wparam, LPARAM lparam )
2875 WPARAM hittest = wparam & 0x0f;
2876 int dir;
2877 struct x11drv_win_data *data;
2879 if (!(data = get_win_data( hwnd )))
2881 if (wparam == SC_SCREENSAVE && hwnd == GetDesktopWindow()) return start_screensaver();
2882 return -1;
2884 if (!data->whole_window || !data->managed || !data->mapped) goto failed;
2886 switch (wparam & 0xfff0)
2888 case SC_MOVE:
2889 if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
2890 else dir = _NET_WM_MOVERESIZE_MOVE;
2891 break;
2892 case SC_SIZE:
2893 /* windows without WS_THICKFRAME are not resizable through the window manager */
2894 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_THICKFRAME)) goto failed;
2896 switch (hittest)
2898 case WMSZ_LEFT: dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
2899 case WMSZ_RIGHT: dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
2900 case WMSZ_TOP: dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
2901 case WMSZ_TOPLEFT: dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
2902 case WMSZ_TOPRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
2903 case WMSZ_BOTTOM: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
2904 case WMSZ_BOTTOMLEFT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
2905 case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
2906 case 9: dir = _NET_WM_MOVERESIZE_MOVE; break;
2907 default: dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD; break;
2909 break;
2911 case SC_KEYMENU:
2912 /* prevent a simple ALT press+release from activating the system menu,
2913 * as that can get confusing on managed windows */
2914 if ((WCHAR)lparam) goto failed; /* got an explicit char */
2915 if (GetMenu( hwnd )) goto failed; /* window has a real menu */
2916 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_SYSMENU)) goto failed; /* no system menu */
2917 TRACE( "ignoring SC_KEYMENU wp %lx lp %lx\n", wparam, lparam );
2918 release_win_data( data );
2919 return 0;
2921 default:
2922 goto failed;
2925 if (IsZoomed(hwnd)) goto failed;
2927 if (!is_netwm_supported( data->display, x11drv_atom(_NET_WM_MOVERESIZE) ))
2929 TRACE( "_NET_WM_MOVERESIZE not supported\n" );
2930 goto failed;
2933 release_win_data( data );
2934 move_resize_window( hwnd, dir );
2935 return 0;
2937 failed:
2938 release_win_data( data );
2939 return -1;
2942 void CDECL X11DRV_FlashWindowEx( PFLASHWINFO pfinfo )
2944 struct x11drv_win_data *data = get_win_data( pfinfo->hwnd );
2945 XEvent xev;
2947 if (!data)
2948 return;
2950 if (data->mapped)
2952 xev.type = ClientMessage;
2953 xev.xclient.window = data->whole_window;
2954 xev.xclient.message_type = x11drv_atom( _NET_WM_STATE );
2955 xev.xclient.serial = 0;
2956 xev.xclient.display = data->display;
2957 xev.xclient.send_event = True;
2958 xev.xclient.format = 32;
2959 xev.xclient.data.l[0] = pfinfo->dwFlags ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
2960 xev.xclient.data.l[1] = x11drv_atom( _NET_WM_STATE_DEMANDS_ATTENTION );
2961 xev.xclient.data.l[2] = 0;
2962 xev.xclient.data.l[3] = 1;
2963 xev.xclient.data.l[4] = 0;
2965 XSendEvent( data->display, DefaultRootWindow( data->display ), False,
2966 SubstructureNotifyMask, &xev );
2968 release_win_data( data );