explorer: Don't call driver create_desktop if desktop name is "root".
[wine.git] / dlls / winex11.drv / desktop.c
blob17431f8215eb705ad94707f9aa430f649937d473
1 /*
2 * X11DRV desktop window handling
4 * Copyright 2001 Alexandre Julliard
5 * Copyright 2020 Zhiyi Zhang for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #if 0
23 #pragma makedep unix
24 #endif
26 #include "config.h"
27 #include <X11/cursorfont.h>
28 #include <X11/Xlib.h>
30 #define NONAMELESSSTRUCT
31 #define NONAMELESSUNION
33 #include "x11drv.h"
35 /* avoid conflict with field names in included win32 headers */
36 #undef Status
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
41 static unsigned int max_width;
42 static unsigned int max_height;
43 static unsigned int desktop_width;
44 static unsigned int desktop_height;
46 static struct screen_size {
47 unsigned int width;
48 unsigned int height;
49 } screen_sizes[] = {
50 /* 4:3 */
51 { 320, 240},
52 { 400, 300},
53 { 512, 384},
54 { 640, 480},
55 { 768, 576},
56 { 800, 600},
57 {1024, 768},
58 {1152, 864},
59 {1280, 960},
60 {1400, 1050},
61 {1600, 1200},
62 {2048, 1536},
63 /* 5:4 */
64 {1280, 1024},
65 {2560, 2048},
66 /* 16:9 */
67 {1280, 720},
68 {1366, 768},
69 {1600, 900},
70 {1920, 1080},
71 {2560, 1440},
72 {3840, 2160},
73 /* 16:10 */
74 { 320, 200},
75 { 640, 400},
76 {1280, 800},
77 {1440, 900},
78 {1680, 1050},
79 {1920, 1200},
80 {2560, 1600}
83 #define _NET_WM_STATE_REMOVE 0
84 #define _NET_WM_STATE_ADD 1
86 /* parse the desktop size specification */
87 static BOOL parse_size( const WCHAR *size, unsigned int *width, unsigned int *height )
89 WCHAR *end;
91 *width = wcstoul( size, &end, 10 );
92 if (end == size) return FALSE;
93 if (*end != 'x') return FALSE;
94 size = end + 1;
95 *height = wcstoul( size, &end, 10 );
96 return !*end;
99 /* retrieve the default desktop size from the registry */
100 static BOOL get_default_desktop_size( unsigned int *width, unsigned int *height )
102 static const WCHAR defaultW[] = {'D','e','f','a','u','l','t',0};
103 WCHAR buffer[4096];
104 KEY_VALUE_PARTIAL_INFORMATION *value = (void *)buffer;
105 DWORD size;
106 HKEY hkey;
108 /* @@ Wine registry key: HKCU\Software\Wine\Explorer\Desktops */
109 if (!(hkey = open_hkcu_key( "Software\\Wine\\Explorer\\Desktops" ))) return FALSE;
111 size = query_reg_value( hkey, defaultW, value, sizeof(buffer) );
112 NtClose( hkey );
113 if (!size || value->Type != REG_SZ) return FALSE;
115 if (!parse_size( (const WCHAR *)value->Data, width, height )) return FALSE;
116 return TRUE;
119 /* Return TRUE if Wine is currently in virtual desktop mode */
120 BOOL is_virtual_desktop(void)
122 return root_window != DefaultRootWindow( gdi_display );
125 /* Virtual desktop display settings handler */
126 static BOOL X11DRV_desktop_get_id( const WCHAR *device_name, BOOL is_primary, x11drv_settings_id *id )
128 if (!is_primary) return FALSE;
129 id->id = 0;
130 return TRUE;
133 static void add_desktop_mode( DEVMODEW *mode, DWORD depth, DWORD width, DWORD height )
135 mode->dmSize = sizeof(*mode);
136 mode->dmFields = DM_DISPLAYORIENTATION | DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT |
137 DM_DISPLAYFLAGS | DM_DISPLAYFREQUENCY;
138 mode->u1.s2.dmDisplayOrientation = DMDO_DEFAULT;
139 mode->dmBitsPerPel = depth;
140 mode->dmPelsWidth = width;
141 mode->dmPelsHeight = height;
142 mode->u2.dmDisplayFlags = 0;
143 mode->dmDisplayFrequency = 60;
146 static BOOL X11DRV_desktop_get_modes( x11drv_settings_id id, DWORD flags, DEVMODEW **new_modes, UINT *mode_count )
148 UINT depth_idx, size_idx, mode_idx = 0;
149 UINT screen_width, screen_height;
150 DEVMODEW *modes;
152 if (!get_default_desktop_size( &screen_width, &screen_height ))
154 screen_width = max_width;
155 screen_height = max_height;
158 /* Allocate memory for modes in different color depths */
159 if (!(modes = calloc( (ARRAY_SIZE(screen_sizes) + 2) * DEPTH_COUNT, sizeof(*modes))) )
161 RtlSetLastWin32Error( ERROR_NOT_ENOUGH_MEMORY );
162 return FALSE;
165 for (depth_idx = 0; depth_idx < DEPTH_COUNT; ++depth_idx)
167 for (size_idx = 0; size_idx < ARRAY_SIZE(screen_sizes); ++size_idx)
169 if (screen_sizes[size_idx].width > max_width ||
170 screen_sizes[size_idx].height > max_height)
171 continue;
173 if (screen_sizes[size_idx].width == max_width &&
174 screen_sizes[size_idx].height == max_height)
175 continue;
177 if (screen_sizes[size_idx].width == screen_width &&
178 screen_sizes[size_idx].height == screen_height)
179 continue;
181 add_desktop_mode( &modes[mode_idx++], depths[depth_idx], screen_sizes[size_idx].width,
182 screen_sizes[size_idx].height );
185 add_desktop_mode( &modes[mode_idx++], depths[depth_idx], screen_width, screen_height );
186 if (max_width != screen_width || max_height != screen_height)
187 add_desktop_mode( &modes[mode_idx++], depths[depth_idx], max_width, max_height );
190 *new_modes = modes;
191 *mode_count = mode_idx;
192 return TRUE;
195 static void X11DRV_desktop_free_modes( DEVMODEW *modes )
197 free( modes );
200 static BOOL X11DRV_desktop_get_current_mode( x11drv_settings_id id, DEVMODEW *mode )
202 RECT primary_rect = NtUserGetPrimaryMonitorRect();
204 mode->dmFields = DM_DISPLAYORIENTATION | DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT |
205 DM_DISPLAYFLAGS | DM_DISPLAYFREQUENCY | DM_POSITION;
206 mode->u1.s2.dmDisplayOrientation = DMDO_DEFAULT;
207 mode->dmBitsPerPel = screen_bpp;
208 mode->dmPelsWidth = primary_rect.right - primary_rect.left;
209 mode->dmPelsHeight = primary_rect.bottom - primary_rect.top;
210 mode->u2.dmDisplayFlags = 0;
211 mode->dmDisplayFrequency = 60;
212 mode->u1.s2.dmPosition.x = 0;
213 mode->u1.s2.dmPosition.y = 0;
214 return TRUE;
217 static LONG X11DRV_desktop_set_current_mode( x11drv_settings_id id, const DEVMODEW *mode )
219 if (mode->dmFields & DM_BITSPERPEL && mode->dmBitsPerPel != screen_bpp)
220 WARN("Cannot change screen color depth from %dbits to %dbits!\n",
221 screen_bpp, (int)mode->dmBitsPerPel);
223 desktop_width = mode->dmPelsWidth;
224 desktop_height = mode->dmPelsHeight;
225 return DISP_CHANGE_SUCCESSFUL;
228 static void query_desktop_work_area( RECT *rc_work )
230 static const WCHAR trayW[] = {'S','h','e','l','l','_','T','r','a','y','W','n','d',0};
231 UNICODE_STRING str = RTL_CONSTANT_STRING( trayW );
232 RECT rect;
233 HWND hwnd = NtUserFindWindowEx( 0, 0, &str, NULL, 0 );
235 if (!hwnd || !NtUserIsWindowVisible( hwnd )) return;
236 if (!NtUserGetWindowRect( hwnd, &rect )) return;
237 if (rect.top) rc_work->bottom = rect.top;
238 else rc_work->top = rect.bottom;
239 TRACE( "found tray %p %s work area %s\n", hwnd, wine_dbgstr_rect( &rect ), wine_dbgstr_rect( rc_work ) );
242 static BOOL X11DRV_desktop_get_gpus( struct gdi_gpu **new_gpus, int *count )
244 static const WCHAR wine_adapterW[] = {'W','i','n','e',' ','A','d','a','p','t','e','r',0};
245 struct gdi_gpu *gpu;
247 gpu = calloc( 1, sizeof(*gpu) );
248 if (!gpu) return FALSE;
250 if (!get_host_primary_gpu( gpu ))
252 WARN( "Failed to get host primary gpu.\n" );
253 lstrcpyW( gpu->name, wine_adapterW );
256 *new_gpus = gpu;
257 *count = 1;
258 return TRUE;
261 static void X11DRV_desktop_free_gpus( struct gdi_gpu *gpus )
263 free( gpus );
266 /* TODO: Support multi-head virtual desktop */
267 static BOOL X11DRV_desktop_get_adapters( ULONG_PTR gpu_id, struct gdi_adapter **new_adapters, int *count )
269 struct gdi_adapter *adapter;
271 adapter = calloc( 1, sizeof(*adapter) );
272 if (!adapter) return FALSE;
274 adapter->state_flags = DISPLAY_DEVICE_PRIMARY_DEVICE;
275 if (desktop_width && desktop_height)
276 adapter->state_flags |= DISPLAY_DEVICE_ATTACHED_TO_DESKTOP;
278 *new_adapters = adapter;
279 *count = 1;
280 return TRUE;
283 static void X11DRV_desktop_free_adapters( struct gdi_adapter *adapters )
285 free( adapters );
288 static BOOL X11DRV_desktop_get_monitors( ULONG_PTR adapter_id, struct gdi_monitor **new_monitors, int *count )
290 struct gdi_monitor *monitor;
292 monitor = calloc( 1, sizeof(*monitor) );
293 if (!monitor) return FALSE;
295 SetRect( &monitor->rc_monitor, 0, 0, desktop_width, desktop_height );
296 SetRect( &monitor->rc_work, 0, 0, desktop_width, desktop_height );
297 query_desktop_work_area( &monitor->rc_work );
298 monitor->state_flags = DISPLAY_DEVICE_ATTACHED;
299 monitor->edid_len = 0;
300 monitor->edid = NULL;
301 if (desktop_width && desktop_height)
302 monitor->state_flags |= DISPLAY_DEVICE_ACTIVE;
304 *new_monitors = monitor;
305 *count = 1;
306 return TRUE;
309 static void X11DRV_desktop_free_monitors( struct gdi_monitor *monitors, int count )
311 free( monitors );
314 /***********************************************************************
315 * X11DRV_init_desktop
317 * Setup the desktop when not using the root window.
319 void X11DRV_init_desktop( Window win, unsigned int width, unsigned int height )
321 RECT primary_rect = get_host_primary_monitor_rect();
322 struct x11drv_settings_handler settings_handler;
324 root_window = win;
325 managed_mode = FALSE; /* no managed windows in desktop mode */
326 desktop_width = width;
327 desktop_height = height;
328 max_width = primary_rect.right;
329 max_height = primary_rect.bottom;
331 /* Initialize virtual desktop display settings handler */
332 settings_handler.name = "Virtual Desktop";
333 settings_handler.priority = 1000;
334 settings_handler.get_id = X11DRV_desktop_get_id;
335 settings_handler.get_modes = X11DRV_desktop_get_modes;
336 settings_handler.free_modes = X11DRV_desktop_free_modes;
337 settings_handler.get_current_mode = X11DRV_desktop_get_current_mode;
338 settings_handler.set_current_mode = X11DRV_desktop_set_current_mode;
339 X11DRV_Settings_SetHandler( &settings_handler );
341 /* Initialize virtual desktop mode display device handler */
342 desktop_handler.name = "Virtual Desktop";
343 desktop_handler.get_gpus = X11DRV_desktop_get_gpus;
344 desktop_handler.get_adapters = X11DRV_desktop_get_adapters;
345 desktop_handler.get_monitors = X11DRV_desktop_get_monitors;
346 desktop_handler.free_gpus = X11DRV_desktop_free_gpus;
347 desktop_handler.free_adapters = X11DRV_desktop_free_adapters;
348 desktop_handler.free_monitors = X11DRV_desktop_free_monitors;
349 desktop_handler.register_event_handlers = NULL;
350 TRACE("Display device functions are now handled by: Virtual Desktop\n");
351 X11DRV_DisplayDevices_Init( TRUE );
355 /***********************************************************************
356 * x11drv_create_desktop
358 * Create the X11 desktop window for the desktop mode.
360 NTSTATUS x11drv_create_desktop( void *arg )
362 const struct create_desktop_params *params = arg;
363 XSetWindowAttributes win_attr;
364 Window win;
365 Display *display = thread_init_display();
367 /* Create window */
368 win_attr.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask | EnterWindowMask |
369 PointerMotionMask | ButtonPressMask | ButtonReleaseMask | FocusChangeMask;
370 win_attr.cursor = XCreateFontCursor( display, XC_top_left_arrow );
372 if (default_visual.visual != DefaultVisual( display, DefaultScreen(display) ))
373 win_attr.colormap = XCreateColormap( display, DefaultRootWindow(display),
374 default_visual.visual, AllocNone );
375 else
376 win_attr.colormap = None;
378 win = XCreateWindow( display, DefaultRootWindow(display),
379 0, 0, params->width, params->height, 0, default_visual.depth, InputOutput,
380 default_visual.visual, CWEventMask | CWCursor | CWColormap, &win_attr );
381 if (!win) return FALSE;
382 if (!create_desktop_win_data( win )) return FALSE;
384 X11DRV_init_desktop( win, params->width, params->height );
385 if (is_desktop_fullscreen())
387 TRACE("setting desktop to fullscreen\n");
388 XChangeProperty( display, win, x11drv_atom(_NET_WM_STATE), XA_ATOM, 32,
389 PropModeReplace, (unsigned char*)&x11drv_atom(_NET_WM_STATE_FULLSCREEN),
392 XFlush( display );
393 return TRUE;
396 BOOL is_desktop_fullscreen(void)
398 RECT primary_rect = NtUserGetPrimaryMonitorRect();
399 return (primary_rect.right - primary_rect.left == max_width &&
400 primary_rect.bottom - primary_rect.top == max_height);
403 static void update_desktop_fullscreen( unsigned int width, unsigned int height)
405 Display *display = thread_display();
406 XEvent xev;
408 if (!display || !is_virtual_desktop()) return;
410 xev.xclient.type = ClientMessage;
411 xev.xclient.window = root_window;
412 xev.xclient.message_type = x11drv_atom(_NET_WM_STATE);
413 xev.xclient.serial = 0;
414 xev.xclient.display = display;
415 xev.xclient.send_event = True;
416 xev.xclient.format = 32;
417 if (width == max_width && height == max_height)
418 xev.xclient.data.l[0] = _NET_WM_STATE_ADD;
419 else
420 xev.xclient.data.l[0] = _NET_WM_STATE_REMOVE;
421 xev.xclient.data.l[1] = x11drv_atom(_NET_WM_STATE_FULLSCREEN);
422 xev.xclient.data.l[2] = 0;
423 xev.xclient.data.l[3] = 1;
425 TRACE("action=%li\n", xev.xclient.data.l[0]);
427 XSendEvent( display, DefaultRootWindow(display), False,
428 SubstructureRedirectMask | SubstructureNotifyMask, &xev );
430 xev.xclient.data.l[1] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_VERT);
431 xev.xclient.data.l[2] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ);
432 XSendEvent( display, DefaultRootWindow(display), False,
433 SubstructureRedirectMask | SubstructureNotifyMask, &xev );
436 /***********************************************************************
437 * X11DRV_resize_desktop
439 void X11DRV_resize_desktop(void)
441 static RECT old_virtual_rect;
443 RECT primary_rect, virtual_rect;
444 HWND hwnd = NtUserGetDesktopWindow();
445 INT width, height;
447 virtual_rect = NtUserGetVirtualScreenRect();
448 primary_rect = NtUserGetPrimaryMonitorRect();
449 width = primary_rect.right;
450 height = primary_rect.bottom;
452 TRACE( "desktop %p change to (%dx%d)\n", hwnd, width, height );
453 update_desktop_fullscreen( width, height );
454 NtUserSetWindowPos( hwnd, 0, virtual_rect.left, virtual_rect.top,
455 virtual_rect.right - virtual_rect.left, virtual_rect.bottom - virtual_rect.top,
456 SWP_NOZORDER | SWP_NOACTIVATE | SWP_DEFERERASE );
457 ungrab_clipping_window();
459 if (old_virtual_rect.left != virtual_rect.left || old_virtual_rect.top != virtual_rect.top)
460 send_message_timeout( HWND_BROADCAST, WM_X11DRV_DESKTOP_RESIZED, old_virtual_rect.left,
461 old_virtual_rect.top, SMTO_ABORTIFHUNG, 2000, FALSE );
463 /* forward clip_fullscreen_window request to the foreground window */
464 send_notify_message( NtUserGetForegroundWindow(), WM_X11DRV_CLIP_CURSOR_REQUEST, TRUE, TRUE );
466 old_virtual_rect = virtual_rect;