shell32/tests: Add PathResolve tests.
[wine.git] / dlls / winex11.drv / desktop.c
blobd70b38b2b30a3b1dc92859b1c16a410401c6e085
1 /*
2 * X11DRV desktop window handling
4 * Copyright 2001 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include <X11/cursorfont.h>
23 #include <X11/Xlib.h>
25 #include "x11drv.h"
27 /* avoid conflict with field names in included win32 headers */
28 #undef Status
29 #include "wine/debug.h"
30 #include "wine/heap.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
34 /* data for resolution changing */
35 static struct x11drv_mode_info *dd_modes;
36 static unsigned int dd_mode_count;
38 static unsigned int max_width;
39 static unsigned int max_height;
40 static unsigned int desktop_width;
41 static unsigned int desktop_height;
43 static struct screen_size {
44 unsigned int width;
45 unsigned int height;
46 } screen_sizes[] = {
47 /* 4:3 */
48 { 320, 240},
49 { 400, 300},
50 { 512, 384},
51 { 640, 480},
52 { 768, 576},
53 { 800, 600},
54 {1024, 768},
55 {1152, 864},
56 {1280, 960},
57 {1400, 1050},
58 {1600, 1200},
59 {2048, 1536},
60 /* 5:4 */
61 {1280, 1024},
62 {2560, 2048},
63 /* 16:9 */
64 {1280, 720},
65 {1366, 768},
66 {1600, 900},
67 {1920, 1080},
68 {2560, 1440},
69 {3840, 2160},
70 /* 16:10 */
71 { 320, 200},
72 { 640, 400},
73 {1280, 800},
74 {1440, 900},
75 {1680, 1050},
76 {1920, 1200},
77 {2560, 1600}
80 #define _NET_WM_STATE_REMOVE 0
81 #define _NET_WM_STATE_ADD 1
83 /* Return TRUE if Wine is currently in virtual desktop mode */
84 BOOL is_virtual_desktop(void)
86 return root_window != DefaultRootWindow( gdi_display );
89 /* create the mode structures */
90 static void make_modes(void)
92 RECT primary_rect = get_primary_monitor_rect();
93 unsigned int i;
94 unsigned int screen_width = primary_rect.right - primary_rect.left;
95 unsigned int screen_height = primary_rect.bottom - primary_rect.top;
97 /* original specified desktop size */
98 X11DRV_Settings_AddOneMode(screen_width, screen_height, 0, 60);
99 for (i=0; i<ARRAY_SIZE(screen_sizes); i++)
101 if ( (screen_sizes[i].width <= max_width) && (screen_sizes[i].height <= max_height) )
103 if ( ( (screen_sizes[i].width != max_width) || (screen_sizes[i].height != max_height) ) &&
104 ( (screen_sizes[i].width != screen_width) || (screen_sizes[i].height != screen_height) ) )
106 /* only add them if they are smaller than the root window and unique */
107 X11DRV_Settings_AddOneMode(screen_sizes[i].width, screen_sizes[i].height, 0, 60);
111 if ((max_width != screen_width) && (max_height != screen_height))
113 /* root window size (if different from desktop window) */
114 X11DRV_Settings_AddOneMode(max_width, max_height, 0, 60);
118 static int X11DRV_desktop_GetCurrentMode(void)
120 unsigned int i;
121 DWORD dwBpp = screen_bpp;
122 RECT primary_rect = get_primary_monitor_rect();
124 for (i=0; i<dd_mode_count; i++)
126 if ( (primary_rect.right - primary_rect.left == dd_modes[i].width) &&
127 (primary_rect.bottom - primary_rect.top == dd_modes[i].height) &&
128 (dwBpp == dd_modes[i].bpp))
129 return i;
131 ERR("In unknown mode, returning default\n");
132 return 0;
135 static LONG X11DRV_desktop_SetCurrentMode(int mode)
137 DWORD dwBpp = screen_bpp;
138 if (dwBpp != dd_modes[mode].bpp)
140 FIXME("Cannot change screen BPP from %d to %d\n", dwBpp, dd_modes[mode].bpp);
141 /* Ignore the depth mismatch
143 * Some (older) applications require a specific bit depth, this will allow them
144 * to run. X11drv performs a color depth conversion if needed.
147 TRACE("Resizing Wine desktop window to %dx%d\n", dd_modes[mode].width, dd_modes[mode].height);
148 X11DRV_resize_desktop(dd_modes[mode].width, dd_modes[mode].height);
149 return DISP_CHANGE_SUCCESSFUL;
152 static void query_desktop_work_area( RECT *rc_work )
154 static const WCHAR trayW[] = {'S','h','e','l','l','_','T','r','a','y','W','n','d',0};
155 RECT rect;
156 HWND hwnd = FindWindowW( trayW, NULL );
158 if (!hwnd || !IsWindowVisible( hwnd )) return;
159 if (!GetWindowRect( hwnd, &rect )) return;
160 if (rect.top) rc_work->bottom = rect.top;
161 else rc_work->top = rect.bottom;
162 TRACE( "found tray %p %s work area %s\n", hwnd, wine_dbgstr_rect( &rect ), wine_dbgstr_rect( rc_work ) );
165 static BOOL X11DRV_desktop_get_gpus( struct x11drv_gpu **new_gpus, int *count )
167 static const WCHAR wine_adapterW[] = {'W','i','n','e',' ','A','d','a','p','t','e','r',0};
168 struct x11drv_gpu *gpu;
170 gpu = heap_calloc( 1, sizeof(*gpu) );
171 if (!gpu) return FALSE;
173 lstrcpyW( gpu->name, wine_adapterW );
174 *new_gpus = gpu;
175 *count = 1;
176 return TRUE;
179 static void X11DRV_desktop_free_gpus( struct x11drv_gpu *gpus )
181 heap_free( gpus );
184 /* TODO: Support multi-head virtual desktop */
185 static BOOL X11DRV_desktop_get_adapters( ULONG_PTR gpu_id, struct x11drv_adapter **new_adapters, int *count )
187 struct x11drv_adapter *adapter;
189 adapter = heap_calloc( 1, sizeof(*adapter) );
190 if (!adapter) return FALSE;
192 adapter->state_flags = DISPLAY_DEVICE_PRIMARY_DEVICE;
193 if (desktop_width && desktop_height)
194 adapter->state_flags |= DISPLAY_DEVICE_ATTACHED_TO_DESKTOP;
196 *new_adapters = adapter;
197 *count = 1;
198 return TRUE;
201 static void X11DRV_desktop_free_adapters( struct x11drv_adapter *adapters )
203 heap_free( adapters );
206 static BOOL X11DRV_desktop_get_monitors( ULONG_PTR adapter_id, struct x11drv_monitor **new_monitors, int *count )
208 static const WCHAR generic_nonpnp_monitorW[] = {
209 'G','e','n','e','r','i','c',' ',
210 'N','o','n','-','P','n','P',' ','M','o','n','i','t','o','r',0};
211 struct x11drv_monitor *monitor;
213 monitor = heap_calloc( 1, sizeof(*monitor) );
214 if (!monitor) return FALSE;
216 lstrcpyW( monitor->name, generic_nonpnp_monitorW );
217 SetRect( &monitor->rc_monitor, 0, 0, desktop_width, desktop_height );
218 SetRect( &monitor->rc_work, 0, 0, desktop_width, desktop_height );
219 query_desktop_work_area( &monitor->rc_work );
220 monitor->state_flags = DISPLAY_DEVICE_ATTACHED;
221 if (desktop_width && desktop_height)
222 monitor->state_flags |= DISPLAY_DEVICE_ACTIVE;
224 *new_monitors = monitor;
225 *count = 1;
226 return TRUE;
229 static void X11DRV_desktop_free_monitors( struct x11drv_monitor *monitors )
231 heap_free( monitors );
234 /***********************************************************************
235 * X11DRV_init_desktop
237 * Setup the desktop when not using the root window.
239 void X11DRV_init_desktop( Window win, unsigned int width, unsigned int height )
241 RECT primary_rect = get_host_primary_monitor_rect();
243 root_window = win;
244 managed_mode = FALSE; /* no managed windows in desktop mode */
245 desktop_width = width;
246 desktop_height = height;
247 max_width = primary_rect.right;
248 max_height = primary_rect.bottom;
250 /* Initialize virtual desktop mode display device handler */
251 desktop_handler.name = "Virtual Desktop";
252 desktop_handler.get_gpus = X11DRV_desktop_get_gpus;
253 desktop_handler.get_adapters = X11DRV_desktop_get_adapters;
254 desktop_handler.get_monitors = X11DRV_desktop_get_monitors;
255 desktop_handler.free_gpus = X11DRV_desktop_free_gpus;
256 desktop_handler.free_adapters = X11DRV_desktop_free_adapters;
257 desktop_handler.free_monitors = X11DRV_desktop_free_monitors;
258 desktop_handler.register_event_handlers = NULL;
259 TRACE("Display device functions are now handled by: Virtual Desktop\n");
260 X11DRV_DisplayDevices_Init( TRUE );
262 /* initialize the available resolutions */
263 dd_modes = X11DRV_Settings_SetHandlers("desktop",
264 X11DRV_desktop_GetCurrentMode,
265 X11DRV_desktop_SetCurrentMode,
266 ARRAY_SIZE(screen_sizes)+2, 1);
267 make_modes();
268 X11DRV_Settings_AddDepthModes();
269 dd_mode_count = X11DRV_Settings_GetModeCount();
273 /***********************************************************************
274 * X11DRV_create_desktop
276 * Create the X11 desktop window for the desktop mode.
278 BOOL CDECL X11DRV_create_desktop( UINT width, UINT height )
280 static const WCHAR rootW[] = {'r','o','o','t',0};
281 XSetWindowAttributes win_attr;
282 Window win;
283 Display *display = thread_init_display();
284 WCHAR name[MAX_PATH];
286 if (!GetUserObjectInformationW( GetThreadDesktop( GetCurrentThreadId() ),
287 UOI_NAME, name, sizeof(name), NULL ))
288 name[0] = 0;
290 TRACE( "%s %ux%u\n", debugstr_w(name), width, height );
292 /* magic: desktop "root" means use the root window */
293 if (!lstrcmpiW( name, rootW )) return FALSE;
295 /* Create window */
296 win_attr.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask | EnterWindowMask |
297 PointerMotionMask | ButtonPressMask | ButtonReleaseMask | FocusChangeMask;
298 win_attr.cursor = XCreateFontCursor( display, XC_top_left_arrow );
300 if (default_visual.visual != DefaultVisual( display, DefaultScreen(display) ))
301 win_attr.colormap = XCreateColormap( display, DefaultRootWindow(display),
302 default_visual.visual, AllocNone );
303 else
304 win_attr.colormap = None;
306 win = XCreateWindow( display, DefaultRootWindow(display),
307 0, 0, width, height, 0, default_visual.depth, InputOutput, default_visual.visual,
308 CWEventMask | CWCursor | CWColormap, &win_attr );
309 if (!win) return FALSE;
310 if (!create_desktop_win_data( win )) return FALSE;
312 X11DRV_init_desktop( win, width, height );
313 if (is_desktop_fullscreen())
315 TRACE("setting desktop to fullscreen\n");
316 XChangeProperty( display, win, x11drv_atom(_NET_WM_STATE), XA_ATOM, 32,
317 PropModeReplace, (unsigned char*)&x11drv_atom(_NET_WM_STATE_FULLSCREEN),
320 XFlush( display );
321 return TRUE;
325 struct desktop_resize_data
327 RECT old_virtual_rect;
328 RECT new_virtual_rect;
331 static BOOL CALLBACK update_windows_on_desktop_resize( HWND hwnd, LPARAM lparam )
333 struct x11drv_win_data *data;
334 struct desktop_resize_data *resize_data = (struct desktop_resize_data *)lparam;
335 int mask = 0;
337 if (!(data = get_win_data( hwnd ))) return TRUE;
339 /* update the full screen state */
340 update_net_wm_states( data );
342 if (resize_data->old_virtual_rect.left != resize_data->new_virtual_rect.left) mask |= CWX;
343 if (resize_data->old_virtual_rect.top != resize_data->new_virtual_rect.top) mask |= CWY;
344 if (mask && data->whole_window)
346 POINT pos = virtual_screen_to_root( data->whole_rect.left, data->whole_rect.top );
347 XWindowChanges changes;
348 changes.x = pos.x;
349 changes.y = pos.y;
350 XReconfigureWMWindow( data->display, data->whole_window, data->vis.screen, mask, &changes );
352 release_win_data( data );
353 if (hwnd == GetForegroundWindow()) clip_fullscreen_window( hwnd, TRUE );
354 return TRUE;
357 BOOL is_desktop_fullscreen(void)
359 RECT primary_rect = get_primary_monitor_rect();
360 return (primary_rect.right - primary_rect.left == max_width &&
361 primary_rect.bottom - primary_rect.top == max_height);
364 static void update_desktop_fullscreen( unsigned int width, unsigned int height)
366 Display *display = thread_display();
367 XEvent xev;
369 if (!display || !is_virtual_desktop()) return;
371 xev.xclient.type = ClientMessage;
372 xev.xclient.window = root_window;
373 xev.xclient.message_type = x11drv_atom(_NET_WM_STATE);
374 xev.xclient.serial = 0;
375 xev.xclient.display = display;
376 xev.xclient.send_event = True;
377 xev.xclient.format = 32;
378 if (width == max_width && height == max_height)
379 xev.xclient.data.l[0] = _NET_WM_STATE_ADD;
380 else
381 xev.xclient.data.l[0] = _NET_WM_STATE_REMOVE;
382 xev.xclient.data.l[1] = x11drv_atom(_NET_WM_STATE_FULLSCREEN);
383 xev.xclient.data.l[2] = 0;
384 xev.xclient.data.l[3] = 1;
386 TRACE("action=%li\n", xev.xclient.data.l[0]);
388 XSendEvent( display, DefaultRootWindow(display), False,
389 SubstructureRedirectMask | SubstructureNotifyMask, &xev );
391 xev.xclient.data.l[1] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_VERT);
392 xev.xclient.data.l[2] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ);
393 XSendEvent( display, DefaultRootWindow(display), False,
394 SubstructureRedirectMask | SubstructureNotifyMask, &xev );
397 /***********************************************************************
398 * X11DRV_resize_desktop
400 void X11DRV_resize_desktop( unsigned int width, unsigned int height )
402 HWND hwnd = GetDesktopWindow();
403 struct desktop_resize_data resize_data;
405 resize_data.old_virtual_rect = get_virtual_screen_rect();
406 desktop_width = width;
407 desktop_height = height;
408 X11DRV_DisplayDevices_Init( TRUE );
409 resize_data.new_virtual_rect = get_virtual_screen_rect();
411 if (GetWindowThreadProcessId( hwnd, NULL ) != GetCurrentThreadId())
413 SendMessageW( hwnd, WM_X11DRV_RESIZE_DESKTOP, 0, MAKELPARAM( width, height ) );
415 else
417 TRACE( "desktop %p change to (%dx%d)\n", hwnd, width, height );
418 update_desktop_fullscreen( width, height );
419 SetWindowPos( hwnd, 0, resize_data.new_virtual_rect.left, resize_data.new_virtual_rect.top,
420 resize_data.new_virtual_rect.right - resize_data.new_virtual_rect.left,
421 resize_data.new_virtual_rect.bottom - resize_data.new_virtual_rect.top,
422 SWP_NOZORDER | SWP_NOACTIVATE | SWP_DEFERERASE );
423 ungrab_clipping_window();
424 SendMessageTimeoutW( HWND_BROADCAST, WM_DISPLAYCHANGE, screen_bpp,
425 MAKELPARAM( width, height ), SMTO_ABORTIFHUNG, 2000, NULL );
428 EnumWindows( update_windows_on_desktop_resize, (LPARAM)&resize_data );