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
23 #include <X11/cursorfont.h>
26 #define NONAMELESSSTRUCT
27 #define NONAMELESSUNION
31 /* avoid conflict with field names in included win32 headers */
33 #include "wine/debug.h"
34 #include "wine/heap.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(x11drv
);
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
{
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 /* Virtual desktop display settings handler */
90 static BOOL
X11DRV_desktop_get_id( const WCHAR
*device_name
, ULONG_PTR
*id
)
92 WCHAR primary_adapter
[CCHDEVICENAME
];
94 if (!get_primary_adapter( primary_adapter
) || lstrcmpiW( primary_adapter
, device_name
))
101 static void add_desktop_mode( DEVMODEW
*mode
, DWORD depth
, DWORD width
, DWORD height
)
103 mode
->dmSize
= sizeof(*mode
);
104 mode
->dmFields
= DM_DISPLAYORIENTATION
| DM_BITSPERPEL
| DM_PELSWIDTH
| DM_PELSHEIGHT
|
105 DM_DISPLAYFLAGS
| DM_DISPLAYFREQUENCY
;
106 mode
->u1
.s2
.dmDisplayOrientation
= DMDO_DEFAULT
;
107 mode
->dmBitsPerPel
= depth
;
108 mode
->dmPelsWidth
= width
;
109 mode
->dmPelsHeight
= height
;
110 mode
->u2
.dmDisplayFlags
= 0;
111 mode
->dmDisplayFrequency
= 60;
114 static BOOL
X11DRV_desktop_get_modes( ULONG_PTR id
, DWORD flags
, DEVMODEW
**new_modes
, UINT
*mode_count
)
116 UINT depth_idx
, size_idx
, mode_idx
= 0;
117 UINT screen_width
, screen_height
;
121 primary_rect
= get_primary_monitor_rect();
122 screen_width
= primary_rect
.right
- primary_rect
.left
;
123 screen_height
= primary_rect
.bottom
- primary_rect
.top
;
125 /* Allocate memory for modes in different color depths */
126 if (!(modes
= heap_calloc( (ARRAY_SIZE(screen_sizes
) + 2) * DEPTH_COUNT
, sizeof(*modes
))) )
128 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
132 for (depth_idx
= 0; depth_idx
< DEPTH_COUNT
; ++depth_idx
)
134 for (size_idx
= 0; size_idx
< ARRAY_SIZE(screen_sizes
); ++size_idx
)
136 if (screen_sizes
[size_idx
].width
> max_width
||
137 screen_sizes
[size_idx
].height
> max_height
)
140 if (screen_sizes
[size_idx
].width
== max_width
&&
141 screen_sizes
[size_idx
].height
== max_height
)
144 if (screen_sizes
[size_idx
].width
== screen_width
&&
145 screen_sizes
[size_idx
].height
== screen_height
)
148 add_desktop_mode( &modes
[mode_idx
++], depths
[depth_idx
], screen_sizes
[size_idx
].width
,
149 screen_sizes
[size_idx
].height
);
152 add_desktop_mode( &modes
[mode_idx
++], depths
[depth_idx
], screen_width
, screen_height
);
153 if (max_width
!= screen_width
|| max_height
!= screen_height
)
154 add_desktop_mode( &modes
[mode_idx
++], depths
[depth_idx
], max_width
, max_height
);
158 *mode_count
= mode_idx
;
162 static void X11DRV_desktop_free_modes( DEVMODEW
*modes
)
167 static BOOL
X11DRV_desktop_get_current_mode( ULONG_PTR id
, DEVMODEW
*mode
)
169 RECT primary_rect
= get_primary_monitor_rect();
171 mode
->dmFields
= DM_DISPLAYORIENTATION
| DM_BITSPERPEL
| DM_PELSWIDTH
| DM_PELSHEIGHT
|
172 DM_DISPLAYFLAGS
| DM_DISPLAYFREQUENCY
| DM_POSITION
;
173 mode
->u1
.s2
.dmDisplayOrientation
= DMDO_DEFAULT
;
174 mode
->dmBitsPerPel
= screen_bpp
;
175 mode
->dmPelsWidth
= primary_rect
.right
- primary_rect
.left
;
176 mode
->dmPelsHeight
= primary_rect
.bottom
- primary_rect
.top
;
177 mode
->u2
.dmDisplayFlags
= 0;
178 mode
->dmDisplayFrequency
= 60;
179 mode
->u1
.s2
.dmPosition
.x
= 0;
180 mode
->u1
.s2
.dmPosition
.y
= 0;
184 static LONG
X11DRV_desktop_set_current_mode( ULONG_PTR id
, DEVMODEW
*mode
)
186 if (mode
->dmFields
& DM_BITSPERPEL
&& mode
->dmBitsPerPel
!= screen_bpp
)
187 WARN("Cannot change screen color depth from %dbits to %dbits!\n", screen_bpp
, mode
->dmBitsPerPel
);
189 desktop_width
= mode
->dmPelsWidth
;
190 desktop_height
= mode
->dmPelsHeight
;
191 return DISP_CHANGE_SUCCESSFUL
;
194 static void query_desktop_work_area( RECT
*rc_work
)
196 static const WCHAR trayW
[] = {'S','h','e','l','l','_','T','r','a','y','W','n','d',0};
198 HWND hwnd
= FindWindowW( trayW
, NULL
);
200 if (!hwnd
|| !IsWindowVisible( hwnd
)) return;
201 if (!GetWindowRect( hwnd
, &rect
)) return;
202 if (rect
.top
) rc_work
->bottom
= rect
.top
;
203 else rc_work
->top
= rect
.bottom
;
204 TRACE( "found tray %p %s work area %s\n", hwnd
, wine_dbgstr_rect( &rect
), wine_dbgstr_rect( rc_work
) );
207 static BOOL
X11DRV_desktop_get_gpus( struct x11drv_gpu
**new_gpus
, int *count
)
209 static const WCHAR wine_adapterW
[] = {'W','i','n','e',' ','A','d','a','p','t','e','r',0};
210 struct x11drv_gpu
*gpu
;
212 gpu
= heap_calloc( 1, sizeof(*gpu
) );
213 if (!gpu
) return FALSE
;
215 if (!get_host_primary_gpu( gpu
))
217 WARN( "Failed to get host primary gpu.\n" );
218 lstrcpyW( gpu
->name
, wine_adapterW
);
226 static void X11DRV_desktop_free_gpus( struct x11drv_gpu
*gpus
)
231 /* TODO: Support multi-head virtual desktop */
232 static BOOL
X11DRV_desktop_get_adapters( ULONG_PTR gpu_id
, struct x11drv_adapter
**new_adapters
, int *count
)
234 struct x11drv_adapter
*adapter
;
236 adapter
= heap_calloc( 1, sizeof(*adapter
) );
237 if (!adapter
) return FALSE
;
239 adapter
->state_flags
= DISPLAY_DEVICE_PRIMARY_DEVICE
;
240 if (desktop_width
&& desktop_height
)
241 adapter
->state_flags
|= DISPLAY_DEVICE_ATTACHED_TO_DESKTOP
;
243 *new_adapters
= adapter
;
248 static void X11DRV_desktop_free_adapters( struct x11drv_adapter
*adapters
)
250 heap_free( adapters
);
253 static BOOL
X11DRV_desktop_get_monitors( ULONG_PTR adapter_id
, struct x11drv_monitor
**new_monitors
, int *count
)
255 static const WCHAR generic_nonpnp_monitorW
[] = {
256 'G','e','n','e','r','i','c',' ',
257 'N','o','n','-','P','n','P',' ','M','o','n','i','t','o','r',0};
258 struct x11drv_monitor
*monitor
;
260 monitor
= heap_calloc( 1, sizeof(*monitor
) );
261 if (!monitor
) return FALSE
;
263 lstrcpyW( monitor
->name
, generic_nonpnp_monitorW
);
264 SetRect( &monitor
->rc_monitor
, 0, 0, desktop_width
, desktop_height
);
265 SetRect( &monitor
->rc_work
, 0, 0, desktop_width
, desktop_height
);
266 query_desktop_work_area( &monitor
->rc_work
);
267 monitor
->state_flags
= DISPLAY_DEVICE_ATTACHED
;
268 if (desktop_width
&& desktop_height
)
269 monitor
->state_flags
|= DISPLAY_DEVICE_ACTIVE
;
271 *new_monitors
= monitor
;
276 static void X11DRV_desktop_free_monitors( struct x11drv_monitor
*monitors
)
278 heap_free( monitors
);
281 /***********************************************************************
282 * X11DRV_init_desktop
284 * Setup the desktop when not using the root window.
286 void X11DRV_init_desktop( Window win
, unsigned int width
, unsigned int height
)
288 RECT primary_rect
= get_host_primary_monitor_rect();
289 struct x11drv_settings_handler settings_handler
;
292 managed_mode
= FALSE
; /* no managed windows in desktop mode */
293 desktop_width
= width
;
294 desktop_height
= height
;
295 max_width
= primary_rect
.right
;
296 max_height
= primary_rect
.bottom
;
298 /* Initialize virtual desktop mode display device handler */
299 desktop_handler
.name
= "Virtual Desktop";
300 desktop_handler
.get_gpus
= X11DRV_desktop_get_gpus
;
301 desktop_handler
.get_adapters
= X11DRV_desktop_get_adapters
;
302 desktop_handler
.get_monitors
= X11DRV_desktop_get_monitors
;
303 desktop_handler
.free_gpus
= X11DRV_desktop_free_gpus
;
304 desktop_handler
.free_adapters
= X11DRV_desktop_free_adapters
;
305 desktop_handler
.free_monitors
= X11DRV_desktop_free_monitors
;
306 desktop_handler
.register_event_handlers
= NULL
;
307 TRACE("Display device functions are now handled by: Virtual Desktop\n");
308 X11DRV_DisplayDevices_Init( TRUE
);
310 /* Initialize virtual desktop display settings handler */
311 settings_handler
.name
= "Virtual Desktop";
312 settings_handler
.priority
= 1000;
313 settings_handler
.get_id
= X11DRV_desktop_get_id
;
314 settings_handler
.get_modes
= X11DRV_desktop_get_modes
;
315 settings_handler
.free_modes
= X11DRV_desktop_free_modes
;
316 settings_handler
.get_current_mode
= X11DRV_desktop_get_current_mode
;
317 settings_handler
.set_current_mode
= X11DRV_desktop_set_current_mode
;
318 X11DRV_Settings_SetHandler( &settings_handler
);
322 /***********************************************************************
323 * X11DRV_create_desktop
325 * Create the X11 desktop window for the desktop mode.
327 BOOL CDECL
X11DRV_create_desktop( UINT width
, UINT height
)
329 static const WCHAR rootW
[] = {'r','o','o','t',0};
330 XSetWindowAttributes win_attr
;
332 Display
*display
= thread_init_display();
333 WCHAR name
[MAX_PATH
];
335 if (!GetUserObjectInformationW( GetThreadDesktop( GetCurrentThreadId() ),
336 UOI_NAME
, name
, sizeof(name
), NULL
))
339 TRACE( "%s %ux%u\n", debugstr_w(name
), width
, height
);
341 /* magic: desktop "root" means use the root window */
342 if (!lstrcmpiW( name
, rootW
)) return FALSE
;
345 win_attr
.event_mask
= ExposureMask
| KeyPressMask
| KeyReleaseMask
| EnterWindowMask
|
346 PointerMotionMask
| ButtonPressMask
| ButtonReleaseMask
| FocusChangeMask
;
347 win_attr
.cursor
= XCreateFontCursor( display
, XC_top_left_arrow
);
349 if (default_visual
.visual
!= DefaultVisual( display
, DefaultScreen(display
) ))
350 win_attr
.colormap
= XCreateColormap( display
, DefaultRootWindow(display
),
351 default_visual
.visual
, AllocNone
);
353 win_attr
.colormap
= None
;
355 win
= XCreateWindow( display
, DefaultRootWindow(display
),
356 0, 0, width
, height
, 0, default_visual
.depth
, InputOutput
, default_visual
.visual
,
357 CWEventMask
| CWCursor
| CWColormap
, &win_attr
);
358 if (!win
) return FALSE
;
359 if (!create_desktop_win_data( win
)) return FALSE
;
361 X11DRV_init_desktop( win
, width
, height
);
362 if (is_desktop_fullscreen())
364 TRACE("setting desktop to fullscreen\n");
365 XChangeProperty( display
, win
, x11drv_atom(_NET_WM_STATE
), XA_ATOM
, 32,
366 PropModeReplace
, (unsigned char*)&x11drv_atom(_NET_WM_STATE_FULLSCREEN
),
373 BOOL
is_desktop_fullscreen(void)
375 RECT primary_rect
= get_primary_monitor_rect();
376 return (primary_rect
.right
- primary_rect
.left
== max_width
&&
377 primary_rect
.bottom
- primary_rect
.top
== max_height
);
380 static void update_desktop_fullscreen( unsigned int width
, unsigned int height
)
382 Display
*display
= thread_display();
385 if (!display
|| !is_virtual_desktop()) return;
387 xev
.xclient
.type
= ClientMessage
;
388 xev
.xclient
.window
= root_window
;
389 xev
.xclient
.message_type
= x11drv_atom(_NET_WM_STATE
);
390 xev
.xclient
.serial
= 0;
391 xev
.xclient
.display
= display
;
392 xev
.xclient
.send_event
= True
;
393 xev
.xclient
.format
= 32;
394 if (width
== max_width
&& height
== max_height
)
395 xev
.xclient
.data
.l
[0] = _NET_WM_STATE_ADD
;
397 xev
.xclient
.data
.l
[0] = _NET_WM_STATE_REMOVE
;
398 xev
.xclient
.data
.l
[1] = x11drv_atom(_NET_WM_STATE_FULLSCREEN
);
399 xev
.xclient
.data
.l
[2] = 0;
400 xev
.xclient
.data
.l
[3] = 1;
402 TRACE("action=%li\n", xev
.xclient
.data
.l
[0]);
404 XSendEvent( display
, DefaultRootWindow(display
), False
,
405 SubstructureRedirectMask
| SubstructureNotifyMask
, &xev
);
407 xev
.xclient
.data
.l
[1] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_VERT
);
408 xev
.xclient
.data
.l
[2] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ
);
409 XSendEvent( display
, DefaultRootWindow(display
), False
,
410 SubstructureRedirectMask
| SubstructureNotifyMask
, &xev
);
413 /***********************************************************************
414 * X11DRV_resize_desktop
416 void X11DRV_resize_desktop( BOOL send_display_change
)
418 RECT primary_rect
, virtual_rect
;
419 HWND hwnd
= GetDesktopWindow();
422 virtual_rect
= get_virtual_screen_rect();
423 primary_rect
= get_primary_monitor_rect();
424 width
= primary_rect
.right
;
425 height
= primary_rect
.bottom
;
427 if (GetWindowThreadProcessId( hwnd
, NULL
) != GetCurrentThreadId())
429 SendMessageW( hwnd
, WM_X11DRV_RESIZE_DESKTOP
, 0, (LPARAM
)send_display_change
);
433 TRACE( "desktop %p change to (%dx%d)\n", hwnd
, width
, height
);
434 update_desktop_fullscreen( width
, height
);
435 SetWindowPos( hwnd
, 0, virtual_rect
.left
, virtual_rect
.top
,
436 virtual_rect
.right
- virtual_rect
.left
, virtual_rect
.bottom
- virtual_rect
.top
,
437 SWP_NOZORDER
| SWP_NOACTIVATE
| SWP_DEFERERASE
);
438 ungrab_clipping_window();
440 if (send_display_change
)
442 SendMessageTimeoutW( HWND_BROADCAST
, WM_DISPLAYCHANGE
, screen_bpp
, MAKELPARAM( width
, height
),
443 SMTO_ABORTIFHUNG
, 2000, NULL
);