winevulkan: Use host Vulkan structures for private thunks arguments.
[wine.git] / dlls / winex11.drv / desktop.c
blob3da8f92978d165a7a3681f7b1d9e9edde0c5dfb8
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, ULONG_PTR *id )
128 WCHAR primary_adapter[CCHDEVICENAME];
130 if (!get_primary_adapter( primary_adapter ) || wcsicmp( primary_adapter, device_name ))
131 return FALSE;
133 *id = 0;
134 return TRUE;
137 static void add_desktop_mode( DEVMODEW *mode, DWORD depth, DWORD width, DWORD height )
139 mode->dmSize = sizeof(*mode);
140 mode->dmFields = DM_DISPLAYORIENTATION | DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT |
141 DM_DISPLAYFLAGS | DM_DISPLAYFREQUENCY;
142 mode->u1.s2.dmDisplayOrientation = DMDO_DEFAULT;
143 mode->dmBitsPerPel = depth;
144 mode->dmPelsWidth = width;
145 mode->dmPelsHeight = height;
146 mode->u2.dmDisplayFlags = 0;
147 mode->dmDisplayFrequency = 60;
150 static BOOL X11DRV_desktop_get_modes( ULONG_PTR id, DWORD flags, DEVMODEW **new_modes, UINT *mode_count )
152 UINT depth_idx, size_idx, mode_idx = 0;
153 UINT screen_width, screen_height;
154 DEVMODEW *modes;
156 if (!get_default_desktop_size( &screen_width, &screen_height ))
158 screen_width = max_width;
159 screen_height = max_height;
162 /* Allocate memory for modes in different color depths */
163 if (!(modes = calloc( (ARRAY_SIZE(screen_sizes) + 2) * DEPTH_COUNT, sizeof(*modes))) )
165 RtlSetLastWin32Error( ERROR_NOT_ENOUGH_MEMORY );
166 return FALSE;
169 for (depth_idx = 0; depth_idx < DEPTH_COUNT; ++depth_idx)
171 for (size_idx = 0; size_idx < ARRAY_SIZE(screen_sizes); ++size_idx)
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 == max_width &&
178 screen_sizes[size_idx].height == max_height)
179 continue;
181 if (screen_sizes[size_idx].width == screen_width &&
182 screen_sizes[size_idx].height == screen_height)
183 continue;
185 add_desktop_mode( &modes[mode_idx++], depths[depth_idx], screen_sizes[size_idx].width,
186 screen_sizes[size_idx].height );
189 add_desktop_mode( &modes[mode_idx++], depths[depth_idx], screen_width, screen_height );
190 if (max_width != screen_width || max_height != screen_height)
191 add_desktop_mode( &modes[mode_idx++], depths[depth_idx], max_width, max_height );
194 *new_modes = modes;
195 *mode_count = mode_idx;
196 return TRUE;
199 static void X11DRV_desktop_free_modes( DEVMODEW *modes )
201 free( modes );
204 static BOOL X11DRV_desktop_get_current_mode( ULONG_PTR id, DEVMODEW *mode )
206 RECT primary_rect = NtUserGetPrimaryMonitorRect();
208 mode->dmFields = DM_DISPLAYORIENTATION | DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT |
209 DM_DISPLAYFLAGS | DM_DISPLAYFREQUENCY | DM_POSITION;
210 mode->u1.s2.dmDisplayOrientation = DMDO_DEFAULT;
211 mode->dmBitsPerPel = screen_bpp;
212 mode->dmPelsWidth = primary_rect.right - primary_rect.left;
213 mode->dmPelsHeight = primary_rect.bottom - primary_rect.top;
214 mode->u2.dmDisplayFlags = 0;
215 mode->dmDisplayFrequency = 60;
216 mode->u1.s2.dmPosition.x = 0;
217 mode->u1.s2.dmPosition.y = 0;
218 return TRUE;
221 static LONG X11DRV_desktop_set_current_mode( ULONG_PTR id, const DEVMODEW *mode )
223 if (mode->dmFields & DM_BITSPERPEL && mode->dmBitsPerPel != screen_bpp)
224 WARN("Cannot change screen color depth from %dbits to %dbits!\n", screen_bpp, mode->dmBitsPerPel);
226 desktop_width = mode->dmPelsWidth;
227 desktop_height = mode->dmPelsHeight;
228 return DISP_CHANGE_SUCCESSFUL;
231 static void query_desktop_work_area( RECT *rc_work )
233 static const WCHAR trayW[] = {'S','h','e','l','l','_','T','r','a','y','W','n','d'};
234 UNICODE_STRING str = { sizeof(trayW), sizeof(trayW), (WCHAR *)trayW };
235 RECT rect;
236 HWND hwnd = NtUserFindWindowEx( 0, 0, &str, NULL, 0 );
238 if (!hwnd || !NtUserIsWindowVisible( hwnd )) return;
239 if (!NtUserGetWindowRect( hwnd, &rect )) return;
240 if (rect.top) rc_work->bottom = rect.top;
241 else rc_work->top = rect.bottom;
242 TRACE( "found tray %p %s work area %s\n", hwnd, wine_dbgstr_rect( &rect ), wine_dbgstr_rect( rc_work ) );
245 static BOOL X11DRV_desktop_get_gpus( struct gdi_gpu **new_gpus, int *count )
247 static const WCHAR wine_adapterW[] = {'W','i','n','e',' ','A','d','a','p','t','e','r',0};
248 struct gdi_gpu *gpu;
250 gpu = calloc( 1, sizeof(*gpu) );
251 if (!gpu) return FALSE;
253 if (!get_host_primary_gpu( gpu ))
255 WARN( "Failed to get host primary gpu.\n" );
256 lstrcpyW( gpu->name, wine_adapterW );
259 *new_gpus = gpu;
260 *count = 1;
261 return TRUE;
264 static void X11DRV_desktop_free_gpus( struct gdi_gpu *gpus )
266 free( gpus );
269 /* TODO: Support multi-head virtual desktop */
270 static BOOL X11DRV_desktop_get_adapters( ULONG_PTR gpu_id, struct gdi_adapter **new_adapters, int *count )
272 struct gdi_adapter *adapter;
274 adapter = calloc( 1, sizeof(*adapter) );
275 if (!adapter) return FALSE;
277 adapter->state_flags = DISPLAY_DEVICE_PRIMARY_DEVICE;
278 if (desktop_width && desktop_height)
279 adapter->state_flags |= DISPLAY_DEVICE_ATTACHED_TO_DESKTOP;
281 *new_adapters = adapter;
282 *count = 1;
283 return TRUE;
286 static void X11DRV_desktop_free_adapters( struct gdi_adapter *adapters )
288 free( adapters );
291 static BOOL X11DRV_desktop_get_monitors( ULONG_PTR adapter_id, struct gdi_monitor **new_monitors, int *count )
293 static const WCHAR generic_nonpnp_monitorW[] = {
294 'G','e','n','e','r','i','c',' ',
295 'N','o','n','-','P','n','P',' ','M','o','n','i','t','o','r',0};
296 struct gdi_monitor *monitor;
298 monitor = calloc( 1, sizeof(*monitor) );
299 if (!monitor) return FALSE;
301 lstrcpyW( monitor->name, generic_nonpnp_monitorW );
302 SetRect( &monitor->rc_monitor, 0, 0, desktop_width, desktop_height );
303 SetRect( &monitor->rc_work, 0, 0, desktop_width, desktop_height );
304 query_desktop_work_area( &monitor->rc_work );
305 monitor->state_flags = DISPLAY_DEVICE_ATTACHED;
306 monitor->edid_len = 0;
307 monitor->edid = NULL;
308 if (desktop_width && desktop_height)
309 monitor->state_flags |= DISPLAY_DEVICE_ACTIVE;
311 *new_monitors = monitor;
312 *count = 1;
313 return TRUE;
316 static void X11DRV_desktop_free_monitors( struct gdi_monitor *monitors, int count )
318 free( monitors );
321 /***********************************************************************
322 * X11DRV_init_desktop
324 * Setup the desktop when not using the root window.
326 void X11DRV_init_desktop( Window win, unsigned int width, unsigned int height )
328 RECT primary_rect = get_host_primary_monitor_rect();
329 struct x11drv_settings_handler settings_handler;
331 root_window = win;
332 managed_mode = FALSE; /* no managed windows in desktop mode */
333 desktop_width = width;
334 desktop_height = height;
335 max_width = primary_rect.right;
336 max_height = primary_rect.bottom;
338 /* Initialize virtual desktop display settings handler */
339 settings_handler.name = "Virtual Desktop";
340 settings_handler.priority = 1000;
341 settings_handler.get_id = X11DRV_desktop_get_id;
342 settings_handler.get_modes = X11DRV_desktop_get_modes;
343 settings_handler.free_modes = X11DRV_desktop_free_modes;
344 settings_handler.get_current_mode = X11DRV_desktop_get_current_mode;
345 settings_handler.set_current_mode = X11DRV_desktop_set_current_mode;
346 X11DRV_Settings_SetHandler( &settings_handler );
348 /* Initialize virtual desktop mode display device handler */
349 desktop_handler.name = "Virtual Desktop";
350 desktop_handler.get_gpus = X11DRV_desktop_get_gpus;
351 desktop_handler.get_adapters = X11DRV_desktop_get_adapters;
352 desktop_handler.get_monitors = X11DRV_desktop_get_monitors;
353 desktop_handler.free_gpus = X11DRV_desktop_free_gpus;
354 desktop_handler.free_adapters = X11DRV_desktop_free_adapters;
355 desktop_handler.free_monitors = X11DRV_desktop_free_monitors;
356 desktop_handler.register_event_handlers = NULL;
357 TRACE("Display device functions are now handled by: Virtual Desktop\n");
358 X11DRV_DisplayDevices_Init( TRUE );
362 /***********************************************************************
363 * x11drv_create_desktop
365 * Create the X11 desktop window for the desktop mode.
367 NTSTATUS x11drv_create_desktop( void *arg )
369 static const WCHAR rootW[] = {'r','o','o','t',0};
370 const struct create_desktop_params *params = arg;
371 XSetWindowAttributes win_attr;
372 Window win;
373 Display *display = thread_init_display();
374 WCHAR name[MAX_PATH];
376 if (!NtUserGetObjectInformation( NtUserGetThreadDesktop( GetCurrentThreadId() ),
377 UOI_NAME, name, sizeof(name), NULL ))
378 name[0] = 0;
380 TRACE( "%s %ux%u\n", debugstr_w(name), params->width, params->height );
382 /* magic: desktop "root" means use the root window */
383 if (!wcsicmp( name, rootW )) return FALSE;
385 /* Create window */
386 win_attr.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask | EnterWindowMask |
387 PointerMotionMask | ButtonPressMask | ButtonReleaseMask | FocusChangeMask;
388 win_attr.cursor = XCreateFontCursor( display, XC_top_left_arrow );
390 if (default_visual.visual != DefaultVisual( display, DefaultScreen(display) ))
391 win_attr.colormap = XCreateColormap( display, DefaultRootWindow(display),
392 default_visual.visual, AllocNone );
393 else
394 win_attr.colormap = None;
396 win = XCreateWindow( display, DefaultRootWindow(display),
397 0, 0, params->width, params->height, 0, default_visual.depth, InputOutput,
398 default_visual.visual, CWEventMask | CWCursor | CWColormap, &win_attr );
399 if (!win) return FALSE;
400 if (!create_desktop_win_data( win )) return FALSE;
402 X11DRV_init_desktop( win, params->width, params->height );
403 if (is_desktop_fullscreen())
405 TRACE("setting desktop to fullscreen\n");
406 XChangeProperty( display, win, x11drv_atom(_NET_WM_STATE), XA_ATOM, 32,
407 PropModeReplace, (unsigned char*)&x11drv_atom(_NET_WM_STATE_FULLSCREEN),
410 XFlush( display );
411 return TRUE;
414 BOOL is_desktop_fullscreen(void)
416 RECT primary_rect = NtUserGetPrimaryMonitorRect();
417 return (primary_rect.right - primary_rect.left == max_width &&
418 primary_rect.bottom - primary_rect.top == max_height);
421 static void update_desktop_fullscreen( unsigned int width, unsigned int height)
423 Display *display = thread_display();
424 XEvent xev;
426 if (!display || !is_virtual_desktop()) return;
428 xev.xclient.type = ClientMessage;
429 xev.xclient.window = root_window;
430 xev.xclient.message_type = x11drv_atom(_NET_WM_STATE);
431 xev.xclient.serial = 0;
432 xev.xclient.display = display;
433 xev.xclient.send_event = True;
434 xev.xclient.format = 32;
435 if (width == max_width && height == max_height)
436 xev.xclient.data.l[0] = _NET_WM_STATE_ADD;
437 else
438 xev.xclient.data.l[0] = _NET_WM_STATE_REMOVE;
439 xev.xclient.data.l[1] = x11drv_atom(_NET_WM_STATE_FULLSCREEN);
440 xev.xclient.data.l[2] = 0;
441 xev.xclient.data.l[3] = 1;
443 TRACE("action=%li\n", xev.xclient.data.l[0]);
445 XSendEvent( display, DefaultRootWindow(display), False,
446 SubstructureRedirectMask | SubstructureNotifyMask, &xev );
448 xev.xclient.data.l[1] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_VERT);
449 xev.xclient.data.l[2] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ);
450 XSendEvent( display, DefaultRootWindow(display), False,
451 SubstructureRedirectMask | SubstructureNotifyMask, &xev );
454 /***********************************************************************
455 * X11DRV_resize_desktop
457 void X11DRV_resize_desktop(void)
459 RECT primary_rect, virtual_rect;
460 HWND hwnd = NtUserGetDesktopWindow();
461 INT width, height;
463 virtual_rect = NtUserGetVirtualScreenRect();
464 primary_rect = NtUserGetPrimaryMonitorRect();
465 width = primary_rect.right;
466 height = primary_rect.bottom;
468 if (NtUserGetWindowThread( hwnd, NULL ) != GetCurrentThreadId())
470 send_message( hwnd, WM_X11DRV_RESIZE_DESKTOP, 0, 0 );
472 else
474 TRACE( "desktop %p change to (%dx%d)\n", hwnd, width, height );
475 update_desktop_fullscreen( width, height );
476 NtUserSetWindowPos( hwnd, 0, virtual_rect.left, virtual_rect.top,
477 virtual_rect.right - virtual_rect.left, virtual_rect.bottom - virtual_rect.top,
478 SWP_NOZORDER | SWP_NOACTIVATE | SWP_DEFERERASE );
479 ungrab_clipping_window();