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
27 #include <X11/cursorfont.h>
30 #define NONAMELESSSTRUCT
31 #define NONAMELESSUNION
35 /* avoid conflict with field names in included win32 headers */
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
{
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
)
91 *width
= wcstoul( size
, &end
, 10 );
92 if (end
== size
) return FALSE
;
93 if (*end
!= 'x') return FALSE
;
95 *height
= wcstoul( size
, &end
, 10 );
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};
104 KEY_VALUE_PARTIAL_INFORMATION
*value
= (void *)buffer
;
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
) );
113 if (!size
|| value
->Type
!= REG_SZ
) return FALSE
;
115 if (!parse_size( (const WCHAR
*)value
->Data
, width
, height
)) return FALSE
;
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
, ULONG_PTR
*id
)
128 if (!is_primary
) return FALSE
;
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( ULONG_PTR id
, DWORD flags
, DEVMODEW
**new_modes
, UINT
*mode_count
)
148 UINT depth_idx
, size_idx
, mode_idx
= 0;
149 UINT screen_width
, screen_height
;
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
);
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
)
173 if (screen_sizes
[size_idx
].width
== max_width
&&
174 screen_sizes
[size_idx
].height
== max_height
)
177 if (screen_sizes
[size_idx
].width
== screen_width
&&
178 screen_sizes
[size_idx
].height
== screen_height
)
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
);
191 *mode_count
= mode_idx
;
195 static void X11DRV_desktop_free_modes( DEVMODEW
*modes
)
200 static BOOL
X11DRV_desktop_get_current_mode( ULONG_PTR 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;
217 static LONG
X11DRV_desktop_set_current_mode( ULONG_PTR 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'};
231 UNICODE_STRING str
= { sizeof(trayW
), sizeof(trayW
), (WCHAR
*)trayW
};
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};
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
);
261 static void X11DRV_desktop_free_gpus( struct gdi_gpu
*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
;
283 static void X11DRV_desktop_free_adapters( struct gdi_adapter
*adapters
)
288 static BOOL
X11DRV_desktop_get_monitors( ULONG_PTR adapter_id
, struct gdi_monitor
**new_monitors
, int *count
)
290 static const WCHAR generic_nonpnp_monitorW
[] = {
291 'G','e','n','e','r','i','c',' ',
292 'N','o','n','-','P','n','P',' ','M','o','n','i','t','o','r',0};
293 struct gdi_monitor
*monitor
;
295 monitor
= calloc( 1, sizeof(*monitor
) );
296 if (!monitor
) return FALSE
;
298 lstrcpyW( monitor
->name
, generic_nonpnp_monitorW
);
299 SetRect( &monitor
->rc_monitor
, 0, 0, desktop_width
, desktop_height
);
300 SetRect( &monitor
->rc_work
, 0, 0, desktop_width
, desktop_height
);
301 query_desktop_work_area( &monitor
->rc_work
);
302 monitor
->state_flags
= DISPLAY_DEVICE_ATTACHED
;
303 monitor
->edid_len
= 0;
304 monitor
->edid
= NULL
;
305 if (desktop_width
&& desktop_height
)
306 monitor
->state_flags
|= DISPLAY_DEVICE_ACTIVE
;
308 *new_monitors
= monitor
;
313 static void X11DRV_desktop_free_monitors( struct gdi_monitor
*monitors
, int count
)
318 /***********************************************************************
319 * X11DRV_init_desktop
321 * Setup the desktop when not using the root window.
323 void X11DRV_init_desktop( Window win
, unsigned int width
, unsigned int height
)
325 RECT primary_rect
= get_host_primary_monitor_rect();
326 struct x11drv_settings_handler settings_handler
;
329 managed_mode
= FALSE
; /* no managed windows in desktop mode */
330 desktop_width
= width
;
331 desktop_height
= height
;
332 max_width
= primary_rect
.right
;
333 max_height
= primary_rect
.bottom
;
335 /* Initialize virtual desktop display settings handler */
336 settings_handler
.name
= "Virtual Desktop";
337 settings_handler
.priority
= 1000;
338 settings_handler
.get_id
= X11DRV_desktop_get_id
;
339 settings_handler
.get_modes
= X11DRV_desktop_get_modes
;
340 settings_handler
.free_modes
= X11DRV_desktop_free_modes
;
341 settings_handler
.get_current_mode
= X11DRV_desktop_get_current_mode
;
342 settings_handler
.set_current_mode
= X11DRV_desktop_set_current_mode
;
343 X11DRV_Settings_SetHandler( &settings_handler
);
345 /* Initialize virtual desktop mode display device handler */
346 desktop_handler
.name
= "Virtual Desktop";
347 desktop_handler
.get_gpus
= X11DRV_desktop_get_gpus
;
348 desktop_handler
.get_adapters
= X11DRV_desktop_get_adapters
;
349 desktop_handler
.get_monitors
= X11DRV_desktop_get_monitors
;
350 desktop_handler
.free_gpus
= X11DRV_desktop_free_gpus
;
351 desktop_handler
.free_adapters
= X11DRV_desktop_free_adapters
;
352 desktop_handler
.free_monitors
= X11DRV_desktop_free_monitors
;
353 desktop_handler
.register_event_handlers
= NULL
;
354 TRACE("Display device functions are now handled by: Virtual Desktop\n");
355 X11DRV_DisplayDevices_Init( TRUE
);
359 /***********************************************************************
360 * x11drv_create_desktop
362 * Create the X11 desktop window for the desktop mode.
364 NTSTATUS
x11drv_create_desktop( void *arg
)
366 static const WCHAR rootW
[] = {'r','o','o','t',0};
367 const struct create_desktop_params
*params
= arg
;
368 XSetWindowAttributes win_attr
;
370 Display
*display
= thread_init_display();
371 WCHAR name
[MAX_PATH
];
373 if (!NtUserGetObjectInformation( NtUserGetThreadDesktop( GetCurrentThreadId() ),
374 UOI_NAME
, name
, sizeof(name
), NULL
))
377 TRACE( "%s %ux%u\n", debugstr_w(name
), params
->width
, params
->height
);
379 /* magic: desktop "root" means use the root window */
380 if (!wcsicmp( name
, rootW
)) return FALSE
;
383 win_attr
.event_mask
= ExposureMask
| KeyPressMask
| KeyReleaseMask
| EnterWindowMask
|
384 PointerMotionMask
| ButtonPressMask
| ButtonReleaseMask
| FocusChangeMask
;
385 win_attr
.cursor
= XCreateFontCursor( display
, XC_top_left_arrow
);
387 if (default_visual
.visual
!= DefaultVisual( display
, DefaultScreen(display
) ))
388 win_attr
.colormap
= XCreateColormap( display
, DefaultRootWindow(display
),
389 default_visual
.visual
, AllocNone
);
391 win_attr
.colormap
= None
;
393 win
= XCreateWindow( display
, DefaultRootWindow(display
),
394 0, 0, params
->width
, params
->height
, 0, default_visual
.depth
, InputOutput
,
395 default_visual
.visual
, CWEventMask
| CWCursor
| CWColormap
, &win_attr
);
396 if (!win
) return FALSE
;
397 if (!create_desktop_win_data( win
)) return FALSE
;
399 X11DRV_init_desktop( win
, params
->width
, params
->height
);
400 if (is_desktop_fullscreen())
402 TRACE("setting desktop to fullscreen\n");
403 XChangeProperty( display
, win
, x11drv_atom(_NET_WM_STATE
), XA_ATOM
, 32,
404 PropModeReplace
, (unsigned char*)&x11drv_atom(_NET_WM_STATE_FULLSCREEN
),
411 BOOL
is_desktop_fullscreen(void)
413 RECT primary_rect
= NtUserGetPrimaryMonitorRect();
414 return (primary_rect
.right
- primary_rect
.left
== max_width
&&
415 primary_rect
.bottom
- primary_rect
.top
== max_height
);
418 static void update_desktop_fullscreen( unsigned int width
, unsigned int height
)
420 Display
*display
= thread_display();
423 if (!display
|| !is_virtual_desktop()) return;
425 xev
.xclient
.type
= ClientMessage
;
426 xev
.xclient
.window
= root_window
;
427 xev
.xclient
.message_type
= x11drv_atom(_NET_WM_STATE
);
428 xev
.xclient
.serial
= 0;
429 xev
.xclient
.display
= display
;
430 xev
.xclient
.send_event
= True
;
431 xev
.xclient
.format
= 32;
432 if (width
== max_width
&& height
== max_height
)
433 xev
.xclient
.data
.l
[0] = _NET_WM_STATE_ADD
;
435 xev
.xclient
.data
.l
[0] = _NET_WM_STATE_REMOVE
;
436 xev
.xclient
.data
.l
[1] = x11drv_atom(_NET_WM_STATE_FULLSCREEN
);
437 xev
.xclient
.data
.l
[2] = 0;
438 xev
.xclient
.data
.l
[3] = 1;
440 TRACE("action=%li\n", xev
.xclient
.data
.l
[0]);
442 XSendEvent( display
, DefaultRootWindow(display
), False
,
443 SubstructureRedirectMask
| SubstructureNotifyMask
, &xev
);
445 xev
.xclient
.data
.l
[1] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_VERT
);
446 xev
.xclient
.data
.l
[2] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ
);
447 XSendEvent( display
, DefaultRootWindow(display
), False
,
448 SubstructureRedirectMask
| SubstructureNotifyMask
, &xev
);
451 /***********************************************************************
452 * X11DRV_resize_desktop
454 void X11DRV_resize_desktop(void)
456 static RECT old_virtual_rect
;
458 RECT primary_rect
, virtual_rect
;
459 HWND hwnd
= NtUserGetDesktopWindow();
462 virtual_rect
= NtUserGetVirtualScreenRect();
463 primary_rect
= NtUserGetPrimaryMonitorRect();
464 width
= primary_rect
.right
;
465 height
= primary_rect
.bottom
;
467 TRACE( "desktop %p change to (%dx%d)\n", hwnd
, width
, height
);
468 update_desktop_fullscreen( width
, height
);
469 NtUserSetWindowPos( hwnd
, 0, virtual_rect
.left
, virtual_rect
.top
,
470 virtual_rect
.right
- virtual_rect
.left
, virtual_rect
.bottom
- virtual_rect
.top
,
471 SWP_NOZORDER
| SWP_NOACTIVATE
| SWP_DEFERERASE
);
472 ungrab_clipping_window();
474 if (old_virtual_rect
.left
!= virtual_rect
.left
|| old_virtual_rect
.top
!= virtual_rect
.top
)
475 send_message_timeout( HWND_BROADCAST
, WM_X11DRV_DESKTOP_RESIZED
, old_virtual_rect
.left
,
476 old_virtual_rect
.top
, SMTO_ABORTIFHUNG
, 2000, FALSE
);
478 /* forward clip_fullscreen_window request to the foreground window */
479 send_notify_message( NtUserGetForegroundWindow(), WM_X11DRV_CLIP_CURSOR_REQUEST
, TRUE
, TRUE
);
481 old_virtual_rect
= virtual_rect
;