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
, x11drv_settings_id
*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( 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
;
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( 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;
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
);
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 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
;
309 static void X11DRV_desktop_free_monitors( struct gdi_monitor
*monitors
, int count
)
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
;
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");
354 /***********************************************************************
355 * X11DRV_CreateDesktop
357 * Create the X11 desktop window for the desktop mode.
359 BOOL
X11DRV_CreateDesktop( const WCHAR
*name
, UINT width
, UINT height
)
361 XSetWindowAttributes win_attr
;
363 Display
*display
= thread_init_display();
365 TRACE( "%s %ux%u\n", debugstr_w(name
), width
, height
);
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
);
376 win_attr
.colormap
= None
;
378 win
= XCreateWindow( display
, DefaultRootWindow(display
),
379 0, 0, width
, height
, 0, default_visual
.depth
, InputOutput
,
380 default_visual
.visual
, CWEventMask
| CWCursor
| CWColormap
, &win_attr
);
381 if (!win
) return FALSE
;
384 X11DRV_init_desktop( win
, width
, height
);
388 BOOL
is_desktop_fullscreen(void)
390 RECT primary_rect
= NtUserGetPrimaryMonitorRect();
391 return (primary_rect
.right
- primary_rect
.left
== max_width
&&
392 primary_rect
.bottom
- primary_rect
.top
== max_height
);
395 static void update_desktop_fullscreen( unsigned int width
, unsigned int height
)
397 Display
*display
= thread_display();
400 if (!display
|| !is_virtual_desktop()) return;
402 xev
.xclient
.type
= ClientMessage
;
403 xev
.xclient
.window
= root_window
;
404 xev
.xclient
.message_type
= x11drv_atom(_NET_WM_STATE
);
405 xev
.xclient
.serial
= 0;
406 xev
.xclient
.display
= display
;
407 xev
.xclient
.send_event
= True
;
408 xev
.xclient
.format
= 32;
409 if (width
== max_width
&& height
== max_height
)
410 xev
.xclient
.data
.l
[0] = _NET_WM_STATE_ADD
;
412 xev
.xclient
.data
.l
[0] = _NET_WM_STATE_REMOVE
;
413 xev
.xclient
.data
.l
[1] = x11drv_atom(_NET_WM_STATE_FULLSCREEN
);
414 xev
.xclient
.data
.l
[2] = 0;
415 xev
.xclient
.data
.l
[3] = 1;
417 TRACE("action=%li\n", xev
.xclient
.data
.l
[0]);
419 XSendEvent( display
, DefaultRootWindow(display
), False
,
420 SubstructureRedirectMask
| SubstructureNotifyMask
, &xev
);
422 xev
.xclient
.data
.l
[1] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_VERT
);
423 xev
.xclient
.data
.l
[2] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ
);
424 XSendEvent( display
, DefaultRootWindow(display
), False
,
425 SubstructureRedirectMask
| SubstructureNotifyMask
, &xev
);
428 /***********************************************************************
429 * X11DRV_resize_desktop
431 void X11DRV_resize_desktop(void)
433 static RECT old_virtual_rect
;
435 RECT primary_rect
, virtual_rect
;
436 HWND hwnd
= NtUserGetDesktopWindow();
439 virtual_rect
= NtUserGetVirtualScreenRect();
440 primary_rect
= NtUserGetPrimaryMonitorRect();
441 width
= primary_rect
.right
;
442 height
= primary_rect
.bottom
;
444 TRACE( "desktop %p change to (%dx%d)\n", hwnd
, width
, height
);
445 update_desktop_fullscreen( width
, height
);
446 NtUserSetWindowPos( hwnd
, 0, virtual_rect
.left
, virtual_rect
.top
,
447 virtual_rect
.right
- virtual_rect
.left
, virtual_rect
.bottom
- virtual_rect
.top
,
448 SWP_NOZORDER
| SWP_NOACTIVATE
| SWP_DEFERERASE
);
450 if (old_virtual_rect
.left
!= virtual_rect
.left
|| old_virtual_rect
.top
!= virtual_rect
.top
)
451 send_message_timeout( HWND_BROADCAST
, WM_X11DRV_DESKTOP_RESIZED
, old_virtual_rect
.left
,
452 old_virtual_rect
.top
, SMTO_ABORTIFHUNG
, 2000, FALSE
);
454 /* forward clip_fullscreen_window request to the foreground window */
455 send_notify_message( NtUserGetForegroundWindow(), WM_X11DRV_CLIP_CURSOR_REQUEST
, TRUE
, TRUE
);
457 old_virtual_rect
= virtual_rect
;