wined3d: Pass a wined3d_device_context to wined3d_cs_emit_blt_sub_resource().
[wine/zf.git] / dlls / winex11.drv / window.c
blobbaaa30d74e3d90556fdec3ec2cf69e221a467325
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 if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
753 if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
754 if (style & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
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_client_window
1472 Window create_client_window( HWND hwnd, const XVisualInfo *visual )
1474 Window dummy_parent = get_dummy_parent();
1475 struct x11drv_win_data *data = get_win_data( hwnd );
1476 XSetWindowAttributes attr;
1477 Window ret;
1478 int x, y, cx, cy;
1480 if (!data)
1482 /* explicitly create data for HWND_MESSAGE windows since they can be used for OpenGL */
1483 HWND parent = GetAncestor( hwnd, GA_PARENT );
1484 if (parent == GetDesktopWindow() || GetAncestor( parent, GA_PARENT )) return 0;
1485 if (!(data = alloc_win_data( thread_init_display(), hwnd ))) return 0;
1486 GetClientRect( hwnd, &data->client_rect );
1487 data->window_rect = data->whole_rect = data->client_rect;
1490 if (data->client_window)
1492 XDeleteContext( data->display, data->client_window, winContext );
1493 XReparentWindow( gdi_display, data->client_window, dummy_parent, 0, 0 );
1494 TRACE( "%p reparent xwin %lx/%lx\n", data->hwnd, data->whole_window, data->client_window );
1497 if (data->client_colormap) XFreeColormap( gdi_display, data->client_colormap );
1498 data->client_colormap = XCreateColormap( gdi_display, dummy_parent, visual->visual,
1499 (visual->class == PseudoColor ||
1500 visual->class == GrayScale ||
1501 visual->class == DirectColor) ? AllocAll : AllocNone );
1502 attr.colormap = data->client_colormap;
1503 attr.bit_gravity = NorthWestGravity;
1504 attr.win_gravity = NorthWestGravity;
1505 attr.backing_store = NotUseful;
1506 attr.border_pixel = 0;
1508 x = data->client_rect.left - data->whole_rect.left;
1509 y = data->client_rect.top - data->whole_rect.top;
1510 cx = min( max( 1, data->client_rect.right - data->client_rect.left ), 65535 );
1511 cy = min( max( 1, data->client_rect.bottom - data->client_rect.top ), 65535 );
1513 ret = data->client_window = XCreateWindow( gdi_display,
1514 data->whole_window ? data->whole_window : dummy_parent,
1515 x, y, cx, cy, 0, default_visual.depth, InputOutput,
1516 visual->visual, CWBitGravity | CWWinGravity |
1517 CWBackingStore | CWColormap | CWBorderPixel, &attr );
1518 if (data->client_window)
1520 XSaveContext( data->display, data->client_window, winContext, (char *)data->hwnd );
1521 XMapWindow( gdi_display, data->client_window );
1522 XSync( gdi_display, False );
1523 if (data->whole_window) XSelectInput( data->display, data->client_window, ExposureMask );
1524 TRACE( "%p xwin %lx/%lx\n", data->hwnd, data->whole_window, data->client_window );
1526 release_win_data( data );
1527 return ret;
1531 /**********************************************************************
1532 * create_whole_window
1534 * Create the whole X window for a given window
1536 static void create_whole_window( struct x11drv_win_data *data )
1538 int cx, cy, mask;
1539 XSetWindowAttributes attr;
1540 WCHAR text[1024];
1541 COLORREF key;
1542 BYTE alpha;
1543 DWORD layered_flags;
1544 HRGN win_rgn;
1545 POINT pos;
1547 if (!data->managed && is_window_managed( data->hwnd, SWP_NOACTIVATE, &data->window_rect ))
1549 TRACE( "making win %p/%lx managed\n", data->hwnd, data->whole_window );
1550 data->managed = TRUE;
1553 if ((win_rgn = CreateRectRgn( 0, 0, 0, 0 )) && GetWindowRgn( data->hwnd, win_rgn ) == ERROR)
1555 DeleteObject( win_rgn );
1556 win_rgn = 0;
1558 data->shaped = (win_rgn != 0);
1560 if (data->vis.visualid != default_visual.visualid)
1561 data->whole_colormap = XCreateColormap( data->display, root_window, data->vis.visual, AllocNone );
1563 mask = get_window_attributes( data, &attr );
1565 if (!(cx = data->whole_rect.right - data->whole_rect.left)) cx = 1;
1566 else if (cx > 65535) cx = 65535;
1567 if (!(cy = data->whole_rect.bottom - data->whole_rect.top)) cy = 1;
1568 else if (cy > 65535) cy = 65535;
1570 pos = virtual_screen_to_root( data->whole_rect.left, data->whole_rect.top );
1571 data->whole_window = XCreateWindow( data->display, root_window, pos.x, pos.y,
1572 cx, cy, 0, data->vis.depth, InputOutput,
1573 data->vis.visual, mask, &attr );
1574 if (!data->whole_window) goto done;
1576 set_initial_wm_hints( data->display, data->whole_window );
1577 set_wm_hints( data );
1579 XSaveContext( data->display, data->whole_window, winContext, (char *)data->hwnd );
1580 SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
1582 /* set the window text */
1583 if (!InternalGetWindowText( data->hwnd, text, ARRAY_SIZE( text ))) text[0] = 0;
1584 sync_window_text( data->display, data->whole_window, text );
1586 /* set the window region */
1587 if (win_rgn || IsRectEmpty( &data->window_rect )) sync_window_region( data, win_rgn );
1589 /* set the window opacity */
1590 if (!GetLayeredWindowAttributes( data->hwnd, &key, &alpha, &layered_flags )) layered_flags = 0;
1591 sync_window_opacity( data->display, data->whole_window, key, alpha, layered_flags );
1593 XFlush( data->display ); /* make sure the window exists before we start painting to it */
1595 sync_window_cursor( data->whole_window );
1597 done:
1598 if (win_rgn) DeleteObject( win_rgn );
1602 /**********************************************************************
1603 * destroy_whole_window
1605 * Destroy the whole X window for a given window.
1607 static void destroy_whole_window( struct x11drv_win_data *data, BOOL already_destroyed )
1609 TRACE( "win %p xwin %lx/%lx\n", data->hwnd, data->whole_window, data->client_window );
1611 if (data->client_window) XDeleteContext( data->display, data->client_window, winContext );
1613 if (!data->whole_window)
1615 if (data->embedded)
1617 Window xwin = (Window)GetPropA( data->hwnd, foreign_window_prop );
1618 if (xwin)
1620 if (!already_destroyed) XSelectInput( data->display, xwin, 0 );
1621 XDeleteContext( data->display, xwin, winContext );
1622 RemovePropA( data->hwnd, foreign_window_prop );
1624 return;
1627 else
1629 if (data->client_window && !already_destroyed)
1631 XSelectInput( data->display, data->client_window, 0 );
1632 XReparentWindow( data->display, data->client_window, get_dummy_parent(), 0, 0 );
1633 XSync( data->display, False );
1635 XDeleteContext( data->display, data->whole_window, winContext );
1636 if (!already_destroyed) XDestroyWindow( data->display, data->whole_window );
1638 if (data->whole_colormap) XFreeColormap( data->display, data->whole_colormap );
1639 data->whole_window = data->client_window = 0;
1640 data->whole_colormap = 0;
1641 data->wm_state = WithdrawnState;
1642 data->net_wm_state = 0;
1643 data->mapped = FALSE;
1644 if (data->xic)
1646 XUnsetICFocus( data->xic );
1647 XDestroyIC( data->xic );
1648 data->xic = 0;
1650 /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
1651 XFlush( data->display );
1652 if (data->surface) window_surface_release( data->surface );
1653 data->surface = NULL;
1654 RemovePropA( data->hwnd, whole_window_prop );
1658 /**********************************************************************
1659 * set_window_visual
1661 * Change the visual by destroying and recreating the X window if needed.
1663 void set_window_visual( struct x11drv_win_data *data, const XVisualInfo *vis, BOOL use_alpha )
1665 Window client_window = data->client_window;
1666 Window whole_window = data->whole_window;
1668 if (!data->use_alpha == !use_alpha) return;
1669 if (data->surface) window_surface_release( data->surface );
1670 data->surface = NULL;
1671 data->use_alpha = use_alpha;
1673 if (data->vis.visualid == vis->visualid) return;
1674 data->client_window = 0;
1675 destroy_whole_window( data, client_window != 0 /* don't destroy whole_window until reparented */ );
1676 data->vis = *vis;
1677 create_whole_window( data );
1678 if (!client_window) return;
1679 /* move the client to the new parent */
1680 XReparentWindow( data->display, client_window, data->whole_window,
1681 data->client_rect.left - data->whole_rect.left,
1682 data->client_rect.top - data->whole_rect.top );
1683 data->client_window = client_window;
1684 XDestroyWindow( data->display, whole_window );
1688 /*****************************************************************
1689 * SetWindowText (X11DRV.@)
1691 void CDECL X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
1693 Window win;
1695 if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(gdi_display))
1697 Display *display = thread_init_display();
1698 sync_window_text( display, win, text );
1703 /***********************************************************************
1704 * SetWindowStyle (X11DRV.@)
1706 * Update the X state of a window to reflect a style change
1708 void CDECL X11DRV_SetWindowStyle( HWND hwnd, INT offset, STYLESTRUCT *style )
1710 struct x11drv_win_data *data;
1711 DWORD changed = style->styleNew ^ style->styleOld;
1713 if (hwnd == GetDesktopWindow()) return;
1714 if (!(data = get_win_data( hwnd ))) return;
1715 if (!data->whole_window) goto done;
1717 if (offset == GWL_STYLE && (changed & WS_DISABLED)) set_wm_hints( data );
1719 if (offset == GWL_EXSTYLE && (changed & WS_EX_LAYERED)) /* changing WS_EX_LAYERED resets attributes */
1721 data->layered = FALSE;
1722 set_window_visual( data, &default_visual, FALSE );
1723 sync_window_opacity( data->display, data->whole_window, 0, 0, 0 );
1724 if (data->surface) set_surface_color_key( data->surface, CLR_INVALID );
1726 done:
1727 release_win_data( data );
1731 /***********************************************************************
1732 * DestroyWindow (X11DRV.@)
1734 void CDECL X11DRV_DestroyWindow( HWND hwnd )
1736 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1737 struct x11drv_win_data *data;
1739 if (!(data = get_win_data( hwnd ))) return;
1741 destroy_whole_window( data, FALSE );
1742 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
1743 if (thread_data->last_xic_hwnd == hwnd) thread_data->last_xic_hwnd = 0;
1744 if (data->icon_pixmap) XFreePixmap( gdi_display, data->icon_pixmap );
1745 if (data->icon_mask) XFreePixmap( gdi_display, data->icon_mask );
1746 if (data->client_colormap) XFreeColormap( data->display, data->client_colormap );
1747 HeapFree( GetProcessHeap(), 0, data->icon_bits );
1748 XDeleteContext( gdi_display, (XID)hwnd, win_data_context );
1749 release_win_data( data );
1750 HeapFree( GetProcessHeap(), 0, data );
1751 destroy_gl_drawable( hwnd );
1752 wine_vk_surface_destroy( hwnd );
1756 /***********************************************************************
1757 * X11DRV_DestroyNotify
1759 BOOL X11DRV_DestroyNotify( HWND hwnd, XEvent *event )
1761 struct x11drv_win_data *data;
1762 BOOL embedded;
1764 if (!(data = get_win_data( hwnd ))) return FALSE;
1765 embedded = data->embedded;
1766 if (!embedded) FIXME( "window %p/%lx destroyed from the outside\n", hwnd, data->whole_window );
1768 destroy_whole_window( data, TRUE );
1769 release_win_data( data );
1770 if (embedded) SendMessageW( hwnd, WM_CLOSE, 0, 0 );
1771 return TRUE;
1775 /* initialize the desktop window id in the desktop manager process */
1776 BOOL create_desktop_win_data( Window win )
1778 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1779 Display *display = thread_data->display;
1780 struct x11drv_win_data *data;
1782 if (!(data = alloc_win_data( display, GetDesktopWindow() ))) return FALSE;
1783 data->whole_window = win;
1784 data->managed = TRUE;
1785 SetPropA( data->hwnd, whole_window_prop, (HANDLE)win );
1786 set_initial_wm_hints( display, win );
1787 release_win_data( data );
1788 if (thread_data->clip_window) XReparentWindow( display, thread_data->clip_window, win, 0, 0 );
1789 return TRUE;
1792 /**********************************************************************
1793 * CreateDesktopWindow (X11DRV.@)
1795 BOOL CDECL X11DRV_CreateDesktopWindow( HWND hwnd )
1797 unsigned int width, height;
1799 /* retrieve the real size of the desktop */
1800 SERVER_START_REQ( get_window_rectangles )
1802 req->handle = wine_server_user_handle( hwnd );
1803 req->relative = COORDS_CLIENT;
1804 wine_server_call( req );
1805 width = reply->window.right;
1806 height = reply->window.bottom;
1808 SERVER_END_REQ;
1810 if (!width && !height) /* not initialized yet */
1812 RECT rect = get_virtual_screen_rect();
1814 SERVER_START_REQ( set_window_pos )
1816 req->handle = wine_server_user_handle( hwnd );
1817 req->previous = 0;
1818 req->swp_flags = SWP_NOZORDER;
1819 req->window.left = rect.left;
1820 req->window.top = rect.top;
1821 req->window.right = rect.right;
1822 req->window.bottom = rect.bottom;
1823 req->client = req->window;
1824 wine_server_call( req );
1826 SERVER_END_REQ;
1828 else
1830 Window win = (Window)GetPropA( hwnd, whole_window_prop );
1831 if (win && win != root_window) X11DRV_init_desktop( win, width, height );
1833 return TRUE;
1837 static WNDPROC desktop_orig_wndproc;
1839 #define WM_WINE_NOTIFY_ACTIVITY WM_USER
1841 static LRESULT CALLBACK desktop_wndproc_wrapper( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
1843 switch (msg)
1845 case WM_WINE_NOTIFY_ACTIVITY:
1847 static ULONGLONG last = 0;
1848 ULONGLONG now = GetTickCount64();
1849 /* calling XResetScreenSaver too often can cause performance
1850 * problems, so throttle it */
1851 if (now > last + 5000)
1853 XResetScreenSaver( gdi_display );
1854 XFlush( gdi_display );
1855 last = now;
1857 break;
1860 return desktop_orig_wndproc( hwnd, msg, wp, lp );
1863 /**********************************************************************
1864 * CreateWindow (X11DRV.@)
1866 BOOL CDECL X11DRV_CreateWindow( HWND hwnd )
1868 if (hwnd == GetDesktopWindow())
1870 struct x11drv_thread_data *data = x11drv_init_thread_data();
1871 XSetWindowAttributes attr;
1873 desktop_orig_wndproc = (WNDPROC)SetWindowLongPtrW( hwnd, GWLP_WNDPROC,
1874 (LONG_PTR)desktop_wndproc_wrapper );
1876 /* create the cursor clipping window */
1877 attr.override_redirect = TRUE;
1878 attr.event_mask = StructureNotifyMask | FocusChangeMask;
1879 data->clip_window = XCreateWindow( data->display, root_window, 0, 0, 1, 1, 0, 0,
1880 InputOnly, default_visual.visual,
1881 CWOverrideRedirect | CWEventMask, &attr );
1882 XFlush( data->display );
1883 SetPropA( hwnd, clip_window_prop, (HANDLE)data->clip_window );
1884 X11DRV_InitClipboard();
1885 X11DRV_DisplayDevices_RegisterEventHandlers();
1887 return TRUE;
1891 /***********************************************************************
1892 * get_win_data
1894 * Lock and return the X11 data structure associated with a window.
1896 struct x11drv_win_data *get_win_data( HWND hwnd )
1898 char *data;
1900 if (!hwnd) return NULL;
1901 EnterCriticalSection( &win_data_section );
1902 if (!XFindContext( gdi_display, (XID)hwnd, win_data_context, &data ))
1903 return (struct x11drv_win_data *)data;
1904 LeaveCriticalSection( &win_data_section );
1905 return NULL;
1909 /***********************************************************************
1910 * release_win_data
1912 * Release the data returned by get_win_data.
1914 void release_win_data( struct x11drv_win_data *data )
1916 if (data) LeaveCriticalSection( &win_data_section );
1920 /***********************************************************************
1921 * X11DRV_create_win_data
1923 * Create an X11 data window structure for an existing window.
1925 static struct x11drv_win_data *X11DRV_create_win_data( HWND hwnd, const RECT *window_rect,
1926 const RECT *client_rect )
1928 Display *display;
1929 struct x11drv_win_data *data;
1930 HWND parent;
1932 if (!(parent = GetAncestor( hwnd, GA_PARENT ))) return NULL; /* desktop */
1934 /* don't create win data for HWND_MESSAGE windows */
1935 if (parent != GetDesktopWindow() && !GetAncestor( parent, GA_PARENT )) return NULL;
1937 if (GetWindowThreadProcessId( hwnd, NULL ) != GetCurrentThreadId()) return NULL;
1939 /* Recreate the parent gl_drawable now that we know there are child windows
1940 * that will need clipping support.
1942 sync_gl_drawable( parent, TRUE );
1944 display = thread_init_display();
1945 init_clip_window(); /* make sure the clip window is initialized in this thread */
1947 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1949 data->whole_rect = data->window_rect = *window_rect;
1950 data->client_rect = *client_rect;
1951 if (parent == GetDesktopWindow())
1953 create_whole_window( data );
1954 TRACE( "win %p/%lx window %s whole %s client %s\n",
1955 hwnd, data->whole_window, wine_dbgstr_rect( &data->window_rect ),
1956 wine_dbgstr_rect( &data->whole_rect ), wine_dbgstr_rect( &data->client_rect ));
1958 return data;
1962 /* window procedure for foreign windows */
1963 static LRESULT WINAPI foreign_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1965 switch(msg)
1967 case WM_WINDOWPOSCHANGED:
1968 update_systray_balloon_position();
1969 break;
1970 case WM_PARENTNOTIFY:
1971 if (LOWORD(wparam) == WM_DESTROY)
1973 TRACE( "%p: got parent notify destroy for win %lx\n", hwnd, lparam );
1974 PostMessageW( hwnd, WM_CLOSE, 0, 0 ); /* so that we come back here once the child is gone */
1976 return 0;
1977 case WM_CLOSE:
1978 if (GetWindow( hwnd, GW_CHILD )) return 0; /* refuse to die if we still have children */
1979 break;
1981 return DefWindowProcW( hwnd, msg, wparam, lparam );
1985 /***********************************************************************
1986 * create_foreign_window
1988 * Create a foreign window for the specified X window and its ancestors
1990 HWND create_foreign_window( Display *display, Window xwin )
1992 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};
1993 static BOOL class_registered;
1994 struct x11drv_win_data *data;
1995 HWND hwnd, parent;
1996 POINT pos;
1997 Window xparent, xroot;
1998 Window *xchildren;
1999 unsigned int nchildren;
2000 XWindowAttributes attr;
2001 DWORD style = WS_CLIPCHILDREN;
2003 if (!class_registered)
2005 WNDCLASSEXW class;
2007 memset( &class, 0, sizeof(class) );
2008 class.cbSize = sizeof(class);
2009 class.lpfnWndProc = foreign_window_proc;
2010 class.lpszClassName = classW;
2011 if (!RegisterClassExW( &class ) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS)
2013 ERR( "Could not register foreign window class\n" );
2014 return FALSE;
2016 class_registered = TRUE;
2019 if (XFindContext( display, xwin, winContext, (char **)&hwnd )) hwnd = 0;
2020 if (hwnd) return hwnd; /* already created */
2022 XSelectInput( display, xwin, StructureNotifyMask );
2023 if (!XGetWindowAttributes( display, xwin, &attr ) ||
2024 !XQueryTree( display, xwin, &xroot, &xparent, &xchildren, &nchildren ))
2026 XSelectInput( display, xwin, 0 );
2027 return 0;
2029 XFree( xchildren );
2031 if (xparent == xroot)
2033 parent = GetDesktopWindow();
2034 style |= WS_POPUP;
2035 pos = root_to_virtual_screen( attr.x, attr.y );
2037 else
2039 parent = create_foreign_window( display, xparent );
2040 style |= WS_CHILD;
2041 pos.x = attr.x;
2042 pos.y = attr.y;
2045 hwnd = CreateWindowW( classW, NULL, style, pos.x, pos.y, attr.width, attr.height,
2046 parent, 0, 0, NULL );
2048 if (!(data = alloc_win_data( display, hwnd )))
2050 DestroyWindow( hwnd );
2051 return 0;
2053 SetRect( &data->window_rect, pos.x, pos.y, pos.x + attr.width, pos.y + attr.height );
2054 data->whole_rect = data->client_rect = data->window_rect;
2055 data->whole_window = data->client_window = 0;
2056 data->embedded = TRUE;
2057 data->mapped = TRUE;
2059 SetPropA( hwnd, foreign_window_prop, (HANDLE)xwin );
2060 XSaveContext( display, xwin, winContext, (char *)data->hwnd );
2062 TRACE( "win %lx parent %p style %08x %s -> hwnd %p\n",
2063 xwin, parent, style, wine_dbgstr_rect(&data->window_rect), hwnd );
2065 release_win_data( data );
2067 ShowWindow( hwnd, SW_SHOW );
2068 return hwnd;
2072 /***********************************************************************
2073 * X11DRV_get_whole_window
2075 * Return the X window associated with the full area of a window
2077 Window X11DRV_get_whole_window( HWND hwnd )
2079 struct x11drv_win_data *data = get_win_data( hwnd );
2080 Window ret;
2082 if (!data)
2084 if (hwnd == GetDesktopWindow()) return root_window;
2085 return (Window)GetPropA( hwnd, whole_window_prop );
2087 ret = data->whole_window;
2088 release_win_data( data );
2089 return ret;
2093 /***********************************************************************
2094 * X11DRV_get_ic
2096 * Return the X input context associated with a window
2098 XIC X11DRV_get_ic( HWND hwnd )
2100 struct x11drv_win_data *data = get_win_data( hwnd );
2101 XIM xim;
2102 XIC ret = 0;
2104 if (data)
2106 x11drv_thread_data()->last_xic_hwnd = hwnd;
2107 ret = data->xic;
2108 if (!ret && (xim = x11drv_thread_data()->xim)) ret = X11DRV_CreateIC( xim, data );
2109 release_win_data( data );
2111 return ret;
2115 /***********************************************************************
2116 * X11DRV_GetDC (X11DRV.@)
2118 void CDECL X11DRV_GetDC( HDC hdc, HWND hwnd, HWND top, const RECT *win_rect,
2119 const RECT *top_rect, DWORD flags )
2121 struct x11drv_escape_set_drawable escape;
2122 HWND parent;
2124 escape.code = X11DRV_SET_DRAWABLE;
2125 escape.mode = IncludeInferiors;
2127 escape.dc_rect.left = win_rect->left - top_rect->left;
2128 escape.dc_rect.top = win_rect->top - top_rect->top;
2129 escape.dc_rect.right = win_rect->right - top_rect->left;
2130 escape.dc_rect.bottom = win_rect->bottom - top_rect->top;
2132 if (top == hwnd)
2134 struct x11drv_win_data *data = get_win_data( hwnd );
2136 escape.drawable = data ? data->whole_window : X11DRV_get_whole_window( hwnd );
2138 /* special case: when repainting the root window, clip out top-level windows */
2139 if (data && data->whole_window == root_window) escape.mode = ClipByChildren;
2140 release_win_data( data );
2142 else
2144 /* find the first ancestor that has a drawable */
2145 for (parent = hwnd; parent && parent != top; parent = GetAncestor( parent, GA_PARENT ))
2146 if ((escape.drawable = X11DRV_get_whole_window( parent ))) break;
2148 if (escape.drawable)
2150 POINT pt = { 0, 0 };
2151 MapWindowPoints( 0, parent, &pt, 1 );
2152 escape.dc_rect = *win_rect;
2153 OffsetRect( &escape.dc_rect, pt.x, pt.y );
2154 if (flags & DCX_CLIPCHILDREN) escape.mode = ClipByChildren;
2156 else escape.drawable = X11DRV_get_whole_window( top );
2159 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2163 /***********************************************************************
2164 * X11DRV_ReleaseDC (X11DRV.@)
2166 void CDECL X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
2168 struct x11drv_escape_set_drawable escape;
2170 escape.code = X11DRV_SET_DRAWABLE;
2171 escape.drawable = root_window;
2172 escape.mode = IncludeInferiors;
2173 escape.dc_rect = get_virtual_screen_rect();
2174 OffsetRect( &escape.dc_rect, -2 * escape.dc_rect.left, -2 * escape.dc_rect.top );
2175 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2179 /*************************************************************************
2180 * ScrollDC (X11DRV.@)
2182 BOOL CDECL X11DRV_ScrollDC( HDC hdc, INT dx, INT dy, HRGN update )
2184 RECT rect;
2185 BOOL ret;
2186 HRGN expose_rgn = 0;
2188 GetClipBox( hdc, &rect );
2190 if (update)
2192 INT code = X11DRV_START_EXPOSURES;
2193 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
2195 ret = BitBlt( hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
2196 hdc, rect.left - dx, rect.top - dy, SRCCOPY );
2198 code = X11DRV_END_EXPOSURES;
2199 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code,
2200 sizeof(expose_rgn), (LPSTR)&expose_rgn );
2201 if (expose_rgn)
2203 CombineRgn( update, update, expose_rgn, RGN_OR );
2204 DeleteObject( expose_rgn );
2207 else ret = BitBlt( hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
2208 hdc, rect.left - dx, rect.top - dy, SRCCOPY );
2210 return ret;
2214 /***********************************************************************
2215 * SetCapture (X11DRV.@)
2217 void CDECL X11DRV_SetCapture( HWND hwnd, UINT flags )
2219 struct x11drv_thread_data *thread_data = x11drv_thread_data();
2220 struct x11drv_win_data *data;
2222 if (!(flags & (GUI_INMOVESIZE | GUI_INMENUMODE))) return;
2224 if (hwnd)
2226 if (!(data = get_win_data( GetAncestor( hwnd, GA_ROOT )))) return;
2227 if (data->whole_window)
2229 XFlush( gdi_display );
2230 XGrabPointer( data->display, data->whole_window, False,
2231 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
2232 GrabModeAsync, GrabModeAsync, None, None, CurrentTime );
2233 thread_data->grab_hwnd = data->hwnd;
2235 release_win_data( data );
2237 else /* release capture */
2239 if (!(data = get_win_data( thread_data->grab_hwnd ))) return;
2240 XFlush( gdi_display );
2241 XUngrabPointer( data->display, CurrentTime );
2242 XFlush( data->display );
2243 thread_data->grab_hwnd = NULL;
2244 release_win_data( data );
2249 /*****************************************************************
2250 * SetParent (X11DRV.@)
2252 void CDECL X11DRV_SetParent( HWND hwnd, HWND parent, HWND old_parent )
2254 struct x11drv_win_data *data;
2256 if (parent == old_parent) return;
2257 if (!(data = get_win_data( hwnd ))) return;
2258 if (data->embedded) goto done;
2260 if (parent != GetDesktopWindow()) /* a child window */
2262 if (old_parent == GetDesktopWindow())
2264 /* destroy the old X windows */
2265 destroy_whole_window( data, FALSE );
2266 data->managed = FALSE;
2269 else /* new top level window */
2271 create_whole_window( data );
2273 done:
2274 release_win_data( data );
2275 set_gl_drawable_parent( hwnd, parent );
2277 /* Recreate the parent gl_drawable now that we know there are child windows
2278 * that will need clipping support.
2280 sync_gl_drawable( parent, TRUE );
2282 fetch_icon_data( hwnd, 0, 0 );
2286 static inline BOOL get_surface_rect( const RECT *visible_rect, RECT *surface_rect )
2288 *surface_rect = get_virtual_screen_rect();
2290 if (!IntersectRect( surface_rect, surface_rect, visible_rect )) return FALSE;
2291 OffsetRect( surface_rect, -visible_rect->left, -visible_rect->top );
2292 surface_rect->left &= ~31;
2293 surface_rect->top &= ~31;
2294 surface_rect->right = max( surface_rect->left + 32, (surface_rect->right + 31) & ~31 );
2295 surface_rect->bottom = max( surface_rect->top + 32, (surface_rect->bottom + 31) & ~31 );
2296 return TRUE;
2300 /***********************************************************************
2301 * WindowPosChanging (X11DRV.@)
2303 void CDECL X11DRV_WindowPosChanging( HWND hwnd, HWND insert_after, UINT swp_flags,
2304 const RECT *window_rect, const RECT *client_rect, RECT *visible_rect,
2305 struct window_surface **surface )
2307 struct x11drv_win_data *data = get_win_data( hwnd );
2308 RECT surface_rect;
2309 DWORD flags;
2310 COLORREF key;
2311 BOOL layered = GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYERED;
2313 if (!data && !(data = X11DRV_create_win_data( hwnd, window_rect, client_rect ))) return;
2315 /* check if we need to switch the window to managed */
2316 if (!data->managed && data->whole_window && is_window_managed( hwnd, swp_flags, window_rect ))
2318 TRACE( "making win %p/%lx managed\n", hwnd, data->whole_window );
2319 release_win_data( data );
2320 unmap_window( hwnd );
2321 if (!(data = get_win_data( hwnd ))) return;
2322 data->managed = TRUE;
2325 *visible_rect = *window_rect;
2326 X11DRV_window_to_X_rect( data, visible_rect, window_rect, client_rect );
2328 /* create the window surface if necessary */
2330 if (!data->whole_window && !data->embedded) goto done;
2331 if (swp_flags & SWP_HIDEWINDOW) goto done;
2332 if (data->use_alpha) goto done;
2333 if (!get_surface_rect( visible_rect, &surface_rect )) goto done;
2335 if (*surface) window_surface_release( *surface );
2336 *surface = NULL; /* indicate that we want to draw directly to the window */
2338 if (data->embedded) goto done;
2339 if (data->whole_window == root_window) goto done;
2340 if (data->client_window) goto done;
2341 if (!client_side_graphics && !layered) goto done;
2343 if (data->surface)
2345 if (EqualRect( &data->surface->rect, &surface_rect ))
2347 /* existing surface is good enough */
2348 window_surface_add_ref( data->surface );
2349 *surface = data->surface;
2350 goto done;
2353 else if (!(swp_flags & SWP_SHOWWINDOW) && !(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)) goto done;
2355 if (!layered || !GetLayeredWindowAttributes( hwnd, &key, NULL, &flags ) || !(flags & LWA_COLORKEY))
2356 key = CLR_INVALID;
2358 *surface = create_surface( data->whole_window, &data->vis, &surface_rect, key, FALSE );
2360 done:
2361 release_win_data( data );
2365 /***********************************************************************
2366 * WindowPosChanged (X11DRV.@)
2368 void CDECL X11DRV_WindowPosChanged( HWND hwnd, HWND insert_after, UINT swp_flags,
2369 const RECT *rectWindow, const RECT *rectClient,
2370 const RECT *visible_rect, const RECT *valid_rects,
2371 struct window_surface *surface )
2373 struct x11drv_thread_data *thread_data;
2374 struct x11drv_win_data *data;
2375 DWORD new_style = GetWindowLongW( hwnd, GWL_STYLE );
2376 RECT old_window_rect, old_whole_rect, old_client_rect;
2377 int event_type;
2379 if (!(data = get_win_data( hwnd ))) return;
2381 thread_data = x11drv_thread_data();
2383 old_window_rect = data->window_rect;
2384 old_whole_rect = data->whole_rect;
2385 old_client_rect = data->client_rect;
2386 data->window_rect = *rectWindow;
2387 data->whole_rect = *visible_rect;
2388 data->client_rect = *rectClient;
2389 if (data->vis.visualid == default_visual.visualid)
2391 if (surface) window_surface_add_ref( surface );
2392 if (data->surface) window_surface_release( data->surface );
2393 data->surface = surface;
2396 TRACE( "win %p window %s client %s style %08x flags %08x\n",
2397 hwnd, wine_dbgstr_rect(rectWindow), wine_dbgstr_rect(rectClient), new_style, swp_flags );
2399 if (!IsRectEmpty( &valid_rects[0] ))
2401 Window window = data->whole_window;
2402 int x_offset = old_whole_rect.left - data->whole_rect.left;
2403 int y_offset = old_whole_rect.top - data->whole_rect.top;
2405 /* if all that happened is that the whole window moved, copy everything */
2406 if (!(swp_flags & SWP_FRAMECHANGED) &&
2407 old_whole_rect.right - data->whole_rect.right == x_offset &&
2408 old_whole_rect.bottom - data->whole_rect.bottom == y_offset &&
2409 old_client_rect.left - data->client_rect.left == x_offset &&
2410 old_client_rect.right - data->client_rect.right == x_offset &&
2411 old_client_rect.top - data->client_rect.top == y_offset &&
2412 old_client_rect.bottom - data->client_rect.bottom == y_offset &&
2413 EqualRect( &valid_rects[0], &data->client_rect ))
2415 /* if we have an X window the bits will be moved by the X server */
2416 if (!window && (x_offset != 0 || y_offset != 0))
2418 release_win_data( data );
2419 move_window_bits( hwnd, window, &old_whole_rect, visible_rect,
2420 &old_client_rect, rectClient, rectWindow );
2421 if (!(data = get_win_data( hwnd ))) return;
2424 else
2426 release_win_data( data );
2427 move_window_bits( hwnd, window, &valid_rects[1], &valid_rects[0],
2428 &old_client_rect, rectClient, rectWindow );
2429 if (!(data = get_win_data( hwnd ))) return;
2433 XFlush( gdi_display ); /* make sure painting is done before we move the window */
2435 sync_client_position( data, &old_client_rect, &old_whole_rect );
2437 if (!data->whole_window)
2439 BOOL needs_resize = (!data->client_window &&
2440 (data->client_rect.right - data->client_rect.left !=
2441 old_client_rect.right - old_client_rect.left ||
2442 data->client_rect.bottom - data->client_rect.top !=
2443 old_client_rect.bottom - old_client_rect.top));
2444 release_win_data( data );
2445 if (needs_resize) sync_gl_drawable( hwnd, FALSE );
2446 return;
2449 /* check if we are currently processing an event relevant to this window */
2450 event_type = 0;
2451 if (thread_data &&
2452 thread_data->current_event &&
2453 thread_data->current_event->xany.window == data->whole_window)
2455 event_type = thread_data->current_event->type;
2456 if (event_type != ConfigureNotify && event_type != PropertyNotify &&
2457 event_type != GravityNotify && event_type != ReparentNotify)
2458 event_type = 0; /* ignore other events */
2461 if (data->mapped && event_type != ReparentNotify)
2463 if (((swp_flags & SWP_HIDEWINDOW) && !(new_style & WS_VISIBLE)) ||
2464 (!event_type && !(new_style & WS_MINIMIZE) &&
2465 !is_window_rect_mapped( rectWindow ) && is_window_rect_mapped( &old_window_rect )))
2467 release_win_data( data );
2468 unmap_window( hwnd );
2469 if (is_window_rect_full_screen( &old_window_rect )) reset_clipping_window();
2470 if (!(data = get_win_data( hwnd ))) return;
2474 /* don't change position if we are about to minimize or maximize a managed window */
2475 if (!event_type &&
2476 !(data->managed && (swp_flags & SWP_STATECHANGED) && (new_style & (WS_MINIMIZE|WS_MAXIMIZE))))
2477 sync_window_position( data, swp_flags, &old_window_rect, &old_whole_rect, &old_client_rect );
2479 if ((new_style & WS_VISIBLE) &&
2480 ((new_style & WS_MINIMIZE) || is_window_rect_mapped( rectWindow )))
2482 if (!data->mapped)
2484 BOOL needs_icon = !data->icon_pixmap;
2485 BOOL needs_map = TRUE;
2487 /* layered windows are mapped only once their attributes are set */
2488 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYERED)
2489 needs_map = data->layered || IsRectEmpty( rectWindow );
2490 release_win_data( data );
2491 if (needs_icon) fetch_icon_data( hwnd, 0, 0 );
2492 if (needs_map) map_window( hwnd, new_style );
2493 return;
2495 else if ((swp_flags & SWP_STATECHANGED) && (!data->iconic != !(new_style & WS_MINIMIZE)))
2497 set_wm_hints( data );
2498 data->iconic = (new_style & WS_MINIMIZE) != 0;
2499 TRACE( "changing win %p iconic state to %u\n", data->hwnd, data->iconic );
2500 if (data->iconic)
2501 XIconifyWindow( data->display, data->whole_window, data->vis.screen );
2502 else if (is_window_rect_mapped( rectWindow ))
2503 XMapWindow( data->display, data->whole_window );
2504 update_net_wm_states( data );
2506 else
2508 if (swp_flags & (SWP_FRAMECHANGED|SWP_STATECHANGED)) set_wm_hints( data );
2509 if (!event_type) update_net_wm_states( data );
2513 XFlush( data->display ); /* make sure changes are done before we start painting again */
2514 if (data->surface && data->vis.visualid != default_visual.visualid)
2515 data->surface->funcs->flush( data->surface );
2517 release_win_data( data );
2520 /* check if the window icon should be hidden (i.e. moved off-screen) */
2521 static BOOL hide_icon( struct x11drv_win_data *data )
2523 static const WCHAR trayW[] = {'S','h','e','l','l','_','T','r','a','y','W','n','d',0};
2525 if (data->managed) return TRUE;
2526 /* hide icons in desktop mode when the taskbar is active */
2527 if (!is_virtual_desktop()) return FALSE;
2528 return IsWindowVisible( FindWindowW( trayW, NULL ));
2531 /***********************************************************************
2532 * ShowWindow (X11DRV.@)
2534 UINT CDECL X11DRV_ShowWindow( HWND hwnd, INT cmd, RECT *rect, UINT swp )
2536 int x, y;
2537 unsigned int width, height, border, depth;
2538 Window root, top;
2539 POINT pos;
2540 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
2541 struct x11drv_thread_data *thread_data = x11drv_thread_data();
2542 struct x11drv_win_data *data = get_win_data( hwnd );
2544 if (!data || !data->whole_window) goto done;
2545 if (IsRectEmpty( rect )) goto done;
2546 if (style & WS_MINIMIZE)
2548 if (((rect->left != -32000 || rect->top != -32000)) && hide_icon( data ))
2550 OffsetRect( rect, -32000 - rect->left, -32000 - rect->top );
2551 swp &= ~(SWP_NOMOVE | SWP_NOCLIENTMOVE);
2553 goto done;
2555 if (!data->managed || !data->mapped || data->iconic) goto done;
2557 /* only fetch the new rectangle if the ShowWindow was a result of a window manager event */
2559 if (!thread_data->current_event || thread_data->current_event->xany.window != data->whole_window)
2560 goto done;
2562 if (thread_data->current_event->type != ConfigureNotify &&
2563 thread_data->current_event->type != PropertyNotify)
2564 goto done;
2566 TRACE( "win %p/%lx cmd %d at %s flags %08x\n",
2567 hwnd, data->whole_window, cmd, wine_dbgstr_rect(rect), swp );
2569 XGetGeometry( thread_data->display, data->whole_window,
2570 &root, &x, &y, &width, &height, &border, &depth );
2571 XTranslateCoordinates( thread_data->display, data->whole_window, root, 0, 0, &x, &y, &top );
2572 pos = root_to_virtual_screen( x, y );
2573 X11DRV_X_to_window_rect( data, rect, pos.x, pos.y, width, height );
2574 swp &= ~(SWP_NOMOVE | SWP_NOCLIENTMOVE | SWP_NOSIZE | SWP_NOCLIENTSIZE);
2576 done:
2577 release_win_data( data );
2578 return swp;
2582 /**********************************************************************
2583 * SetWindowIcon (X11DRV.@)
2585 * hIcon or hIconSm has changed (or is being initialised for the
2586 * first time). Complete the X11 driver-specific initialisation
2587 * and set the window hints.
2589 void CDECL X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
2591 struct x11drv_win_data *data;
2593 if (!(data = get_win_data( hwnd ))) return;
2594 if (!data->whole_window) goto done;
2595 release_win_data( data ); /* release the lock, fetching the icon requires sending messages */
2597 if (type == ICON_BIG) fetch_icon_data( hwnd, icon, 0 );
2598 else fetch_icon_data( hwnd, 0, icon );
2600 if (!(data = get_win_data( hwnd ))) return;
2601 set_wm_hints( data );
2602 done:
2603 release_win_data( data );
2607 /***********************************************************************
2608 * SetWindowRgn (X11DRV.@)
2610 * Assign specified region to window (for non-rectangular windows)
2612 void CDECL X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
2614 struct x11drv_win_data *data;
2616 if ((data = get_win_data( hwnd )))
2618 sync_window_region( data, hrgn );
2619 release_win_data( data );
2621 else if (X11DRV_get_whole_window( hwnd ))
2623 SendMessageW( hwnd, WM_X11DRV_SET_WIN_REGION, 0, 0 );
2628 /***********************************************************************
2629 * SetLayeredWindowAttributes (X11DRV.@)
2631 * Set transparency attributes for a layered window.
2633 void CDECL X11DRV_SetLayeredWindowAttributes( HWND hwnd, COLORREF key, BYTE alpha, DWORD flags )
2635 struct x11drv_win_data *data = get_win_data( hwnd );
2637 if (data)
2639 set_window_visual( data, &default_visual, FALSE );
2641 if (data->whole_window)
2642 sync_window_opacity( data->display, data->whole_window, key, alpha, flags );
2643 if (data->surface)
2644 set_surface_color_key( data->surface, (flags & LWA_COLORKEY) ? key : CLR_INVALID );
2646 data->layered = TRUE;
2647 if (!data->mapped) /* mapping is delayed until attributes are set */
2649 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
2651 if ((style & WS_VISIBLE) &&
2652 ((style & WS_MINIMIZE) || is_window_rect_mapped( &data->window_rect )))
2654 release_win_data( data );
2655 map_window( hwnd, style );
2656 return;
2659 release_win_data( data );
2661 else
2663 Window win = X11DRV_get_whole_window( hwnd );
2664 if (win)
2666 sync_window_opacity( gdi_display, win, key, alpha, flags );
2667 if (flags & LWA_COLORKEY)
2668 FIXME( "LWA_COLORKEY not supported on foreign process window %p\n", hwnd );
2674 /*****************************************************************************
2675 * UpdateLayeredWindow (X11DRV.@)
2677 BOOL CDECL X11DRV_UpdateLayeredWindow( HWND hwnd, const UPDATELAYEREDWINDOWINFO *info,
2678 const RECT *window_rect )
2680 struct window_surface *surface;
2681 struct x11drv_win_data *data;
2682 BLENDFUNCTION blend = { AC_SRC_OVER, 0, 255, 0 };
2683 COLORREF color_key = (info->dwFlags & ULW_COLORKEY) ? info->crKey : CLR_INVALID;
2684 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
2685 BITMAPINFO *bmi = (BITMAPINFO *)buffer;
2686 void *src_bits, *dst_bits;
2687 RECT rect, src_rect;
2688 HDC hdc = 0;
2689 HBITMAP dib;
2690 BOOL ret = FALSE;
2692 if (!(data = get_win_data( hwnd ))) return FALSE;
2694 data->layered = TRUE;
2695 if (!data->embedded && argb_visual.visualid) set_window_visual( data, &argb_visual, TRUE );
2697 rect = *window_rect;
2698 OffsetRect( &rect, -window_rect->left, -window_rect->top );
2700 surface = data->surface;
2701 if (!surface || !EqualRect( &surface->rect, &rect ))
2703 data->surface = create_surface( data->whole_window, &data->vis, &rect,
2704 color_key, data->use_alpha );
2705 if (surface) window_surface_release( surface );
2706 surface = data->surface;
2708 else set_surface_color_key( surface, color_key );
2710 if (surface) window_surface_add_ref( surface );
2711 release_win_data( data );
2713 if (!surface) return FALSE;
2714 if (!info->hdcSrc)
2716 window_surface_release( surface );
2717 return TRUE;
2720 dst_bits = surface->funcs->get_info( surface, bmi );
2722 if (!(dib = CreateDIBSection( info->hdcDst, bmi, DIB_RGB_COLORS, &src_bits, NULL, 0 ))) goto done;
2723 if (!(hdc = CreateCompatibleDC( 0 ))) goto done;
2725 SelectObject( hdc, dib );
2727 surface->funcs->lock( surface );
2729 if (info->prcDirty)
2731 IntersectRect( &rect, &rect, info->prcDirty );
2732 memcpy( src_bits, dst_bits, bmi->bmiHeader.biSizeImage );
2733 PatBlt( hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, BLACKNESS );
2735 src_rect = rect;
2736 if (info->pptSrc) OffsetRect( &src_rect, info->pptSrc->x, info->pptSrc->y );
2737 DPtoLP( info->hdcSrc, (POINT *)&src_rect, 2 );
2739 ret = GdiAlphaBlend( hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
2740 info->hdcSrc, src_rect.left, src_rect.top,
2741 src_rect.right - src_rect.left, src_rect.bottom - src_rect.top,
2742 (info->dwFlags & ULW_ALPHA) ? *info->pblend : blend );
2743 if (ret)
2745 memcpy( dst_bits, src_bits, bmi->bmiHeader.biSizeImage );
2746 add_bounds_rect( surface->funcs->get_bounds( surface ), &rect );
2749 surface->funcs->unlock( surface );
2750 surface->funcs->flush( surface );
2752 done:
2753 window_surface_release( surface );
2754 if (hdc) DeleteDC( hdc );
2755 if (dib) DeleteObject( dib );
2756 return ret;
2760 /**********************************************************************
2761 * X11DRV_WindowMessage (X11DRV.@)
2763 LRESULT CDECL X11DRV_WindowMessage( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
2765 struct x11drv_win_data *data;
2767 switch(msg)
2769 case WM_X11DRV_UPDATE_CLIPBOARD:
2770 return update_clipboard( hwnd );
2771 case WM_X11DRV_SET_WIN_REGION:
2772 if ((data = get_win_data( hwnd )))
2774 sync_window_region( data, (HRGN)1 );
2775 release_win_data( data );
2777 return 0;
2778 case WM_X11DRV_RESIZE_DESKTOP:
2779 X11DRV_resize_desktop( (BOOL)lp );
2780 return 0;
2781 case WM_X11DRV_SET_CURSOR:
2782 if ((data = get_win_data( hwnd )))
2784 Window win = data->whole_window;
2785 release_win_data( data );
2786 if (win) set_window_cursor( win, (HCURSOR)lp );
2788 else if (hwnd == x11drv_thread_data()->clip_hwnd)
2789 set_window_cursor( x11drv_thread_data()->clip_window, (HCURSOR)lp );
2790 return 0;
2791 case WM_X11DRV_CLIP_CURSOR_NOTIFY:
2792 return clip_cursor_notify( hwnd, (HWND)wp, (HWND)lp );
2793 case WM_X11DRV_CLIP_CURSOR_REQUEST:
2794 return clip_cursor_request( hwnd, (BOOL)wp, (BOOL)lp );
2795 default:
2796 FIXME( "got window msg %x hwnd %p wp %lx lp %lx\n", msg, hwnd, wp, lp );
2797 return 0;
2802 /***********************************************************************
2803 * is_netwm_supported
2805 static BOOL is_netwm_supported( Display *display, Atom atom )
2807 static Atom *net_supported;
2808 static int net_supported_count = -1;
2809 int i;
2811 if (net_supported_count == -1)
2813 Atom type;
2814 int format;
2815 unsigned long count, remaining;
2817 if (!XGetWindowProperty( display, DefaultRootWindow(display), x11drv_atom(_NET_SUPPORTED), 0,
2818 ~0UL, False, XA_ATOM, &type, &format, &count,
2819 &remaining, (unsigned char **)&net_supported ))
2820 net_supported_count = get_property_size( format, count ) / sizeof(Atom);
2821 else
2822 net_supported_count = 0;
2825 for (i = 0; i < net_supported_count; i++)
2826 if (net_supported[i] == atom) return TRUE;
2827 return FALSE;
2831 /***********************************************************************
2832 * start_screensaver
2834 static LRESULT start_screensaver(void)
2836 if (!is_virtual_desktop())
2838 const char *argv[3] = { "xdg-screensaver", "activate", NULL };
2839 int pid = _spawnvp( _P_DETACH, argv[0], argv );
2840 if (pid > 0)
2842 TRACE( "started process %d\n", pid );
2843 return 0;
2846 return -1;
2850 /***********************************************************************
2851 * SysCommand (X11DRV.@)
2853 * Perform WM_SYSCOMMAND handling.
2855 LRESULT CDECL X11DRV_SysCommand( HWND hwnd, WPARAM wparam, LPARAM lparam )
2857 WPARAM hittest = wparam & 0x0f;
2858 int dir;
2859 struct x11drv_win_data *data;
2861 if (!(data = get_win_data( hwnd )))
2863 if (wparam == SC_SCREENSAVE && hwnd == GetDesktopWindow()) return start_screensaver();
2864 return -1;
2866 if (!data->whole_window || !data->managed || !data->mapped) goto failed;
2868 switch (wparam & 0xfff0)
2870 case SC_MOVE:
2871 if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
2872 else dir = _NET_WM_MOVERESIZE_MOVE;
2873 break;
2874 case SC_SIZE:
2875 /* windows without WS_THICKFRAME are not resizable through the window manager */
2876 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_THICKFRAME)) goto failed;
2878 switch (hittest)
2880 case WMSZ_LEFT: dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
2881 case WMSZ_RIGHT: dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
2882 case WMSZ_TOP: dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
2883 case WMSZ_TOPLEFT: dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
2884 case WMSZ_TOPRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
2885 case WMSZ_BOTTOM: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
2886 case WMSZ_BOTTOMLEFT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
2887 case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
2888 case 9: dir = _NET_WM_MOVERESIZE_MOVE; break;
2889 default: dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD; break;
2891 break;
2893 case SC_KEYMENU:
2894 /* prevent a simple ALT press+release from activating the system menu,
2895 * as that can get confusing on managed windows */
2896 if ((WCHAR)lparam) goto failed; /* got an explicit char */
2897 if (GetMenu( hwnd )) goto failed; /* window has a real menu */
2898 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_SYSMENU)) goto failed; /* no system menu */
2899 TRACE( "ignoring SC_KEYMENU wp %lx lp %lx\n", wparam, lparam );
2900 release_win_data( data );
2901 return 0;
2903 default:
2904 goto failed;
2907 if (IsZoomed(hwnd)) goto failed;
2909 if (!is_netwm_supported( data->display, x11drv_atom(_NET_WM_MOVERESIZE) ))
2911 TRACE( "_NET_WM_MOVERESIZE not supported\n" );
2912 goto failed;
2915 release_win_data( data );
2916 move_resize_window( hwnd, dir );
2917 return 0;
2919 failed:
2920 release_win_data( data );
2921 return -1;
2924 void CDECL X11DRV_FlashWindowEx( PFLASHWINFO pfinfo )
2926 struct x11drv_win_data *data = get_win_data( pfinfo->hwnd );
2927 XEvent xev;
2929 if (!data)
2930 return;
2932 if (data->mapped)
2934 xev.type = ClientMessage;
2935 xev.xclient.window = data->whole_window;
2936 xev.xclient.message_type = x11drv_atom( _NET_WM_STATE );
2937 xev.xclient.serial = 0;
2938 xev.xclient.display = data->display;
2939 xev.xclient.send_event = True;
2940 xev.xclient.format = 32;
2941 xev.xclient.data.l[0] = pfinfo->dwFlags ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
2942 xev.xclient.data.l[1] = x11drv_atom( _NET_WM_STATE_DEMANDS_ATTENTION );
2943 xev.xclient.data.l[2] = 0;
2944 xev.xclient.data.l[3] = 1;
2945 xev.xclient.data.l[4] = 0;
2947 XSendEvent( data->display, DefaultRootWindow( data->display ), False,
2948 SubstructureNotifyMask, &xev );
2950 release_win_data( data );