winex11.drv: Send WM_X11DRV_CLIP_CURSOR_REQUEST message from the deskop.
[wine.git] / dlls / winex11.drv / display.c
blob87f7adb76d22a1c67b0dee574ad606a2f599de76
1 /*
2 * X11DRV display device functions
4 * Copyright 2019 Zhiyi Zhang for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #if 0
22 #pragma makedep unix
23 #endif
25 #include "config.h"
26 #include "x11drv.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
31 static struct x11drv_display_device_handler host_handler;
32 struct x11drv_display_device_handler desktop_handler;
33 static struct x11drv_settings_handler settings_handler;
35 #define NEXT_DEVMODEW(mode) ((DEVMODEW *)((char *)((mode) + 1) + (mode)->dmDriverExtra))
37 struct x11drv_display_depth
39 struct list entry;
40 ULONG_PTR display_id;
41 DWORD depth;
44 /* Display device emulated depth list, protected by modes_section */
45 static struct list x11drv_display_depth_list = LIST_INIT(x11drv_display_depth_list);
47 /* All Windows drivers seen so far either support 32 bit depths, or 24 bit depths, but never both. So if we have
48 * a 32 bit framebuffer, report 32 bit bpps, otherwise 24 bit ones.
50 static const unsigned int depths_24[] = {8, 16, 24};
51 static const unsigned int depths_32[] = {8, 16, 32};
52 const unsigned int *depths;
54 static pthread_mutex_t settings_mutex = PTHREAD_MUTEX_INITIALIZER;
56 void X11DRV_Settings_SetHandler(const struct x11drv_settings_handler *new_handler)
58 if (new_handler->priority > settings_handler.priority)
60 settings_handler = *new_handler;
61 TRACE("Display settings are now handled by: %s.\n", settings_handler.name);
65 /***********************************************************************
66 * Default handlers if resolution switching is not enabled
69 static BOOL nores_get_id(const WCHAR *device_name, ULONG_PTR *id)
71 WCHAR primary_adapter[CCHDEVICENAME];
73 if (!get_primary_adapter( primary_adapter ))
74 return FALSE;
76 *id = !wcsicmp( device_name, primary_adapter ) ? 1 : 0;
77 return TRUE;
80 static BOOL nores_get_modes(ULONG_PTR id, DWORD flags, DEVMODEW **new_modes, UINT *mode_count)
82 RECT primary = get_host_primary_monitor_rect();
83 DEVMODEW *modes;
85 modes = calloc(1, sizeof(*modes));
86 if (!modes)
88 RtlSetLastWin32Error( ERROR_NOT_ENOUGH_MEMORY );
89 return FALSE;
92 modes[0].dmSize = sizeof(*modes);
93 modes[0].dmDriverExtra = 0;
94 modes[0].dmFields = DM_DISPLAYORIENTATION | DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT |
95 DM_DISPLAYFLAGS | DM_DISPLAYFREQUENCY;
96 modes[0].dmDisplayOrientation = DMDO_DEFAULT;
97 modes[0].dmBitsPerPel = screen_bpp;
98 modes[0].dmPelsWidth = primary.right;
99 modes[0].dmPelsHeight = primary.bottom;
100 modes[0].dmDisplayFlags = 0;
101 modes[0].dmDisplayFrequency = 60;
103 *new_modes = modes;
104 *mode_count = 1;
105 return TRUE;
108 static void nores_free_modes(DEVMODEW *modes)
110 free(modes);
113 static BOOL nores_get_current_mode(ULONG_PTR id, DEVMODEW *mode)
115 RECT primary = get_host_primary_monitor_rect();
117 mode->dmFields = DM_DISPLAYORIENTATION | DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT |
118 DM_DISPLAYFLAGS | DM_DISPLAYFREQUENCY | DM_POSITION;
119 mode->dmDisplayOrientation = DMDO_DEFAULT;
120 mode->dmDisplayFlags = 0;
121 mode->dmPosition.x = 0;
122 mode->dmPosition.y = 0;
124 if (id != 1)
126 FIXME("Non-primary adapters are unsupported.\n");
127 mode->dmBitsPerPel = 0;
128 mode->dmPelsWidth = 0;
129 mode->dmPelsHeight = 0;
130 mode->dmDisplayFrequency = 0;
131 return TRUE;
134 mode->dmBitsPerPel = screen_bpp;
135 mode->dmPelsWidth = primary.right;
136 mode->dmPelsHeight = primary.bottom;
137 mode->dmDisplayFrequency = 60;
138 return TRUE;
141 static LONG nores_set_current_mode(ULONG_PTR id, const DEVMODEW *mode)
143 WARN("NoRes settings handler, ignoring mode change request.\n");
144 return DISP_CHANGE_SUCCESSFUL;
147 /* default handler only gets the current X desktop resolution */
148 void X11DRV_Settings_Init(void)
150 struct x11drv_settings_handler nores_handler;
152 depths = screen_bpp == 32 ? depths_32 : depths_24;
154 nores_handler.name = "NoRes";
155 nores_handler.priority = 1;
156 nores_handler.get_id = nores_get_id;
157 nores_handler.get_modes = nores_get_modes;
158 nores_handler.free_modes = nores_free_modes;
159 nores_handler.get_current_mode = nores_get_current_mode;
160 nores_handler.set_current_mode = nores_set_current_mode;
161 X11DRV_Settings_SetHandler(&nores_handler);
164 /* Initialize registry display settings when new display devices are added */
165 void init_registry_display_settings(void)
167 DEVMODEW dm = {.dmSize = sizeof(dm)};
168 DISPLAY_DEVICEW dd = {sizeof(dd)};
169 UNICODE_STRING device_name;
170 DWORD i = 0;
171 LONG ret;
173 while (!NtUserEnumDisplayDevices( NULL, i++, &dd, 0 ))
175 RtlInitUnicodeString( &device_name, dd.DeviceName );
177 /* Skip if the device already has registry display settings */
178 if (NtUserEnumDisplaySettings( &device_name, ENUM_REGISTRY_SETTINGS, &dm, 0 ))
179 continue;
181 if (!NtUserEnumDisplaySettings( &device_name, ENUM_CURRENT_SETTINGS, &dm, 0 ))
183 ERR("Failed to query current display settings for %s.\n", wine_dbgstr_w(dd.DeviceName));
184 continue;
187 TRACE("Device %s current display mode %ux%u %ubits %uHz at %d,%d.\n",
188 wine_dbgstr_w(dd.DeviceName), dm.dmPelsWidth, dm.dmPelsHeight, dm.dmBitsPerPel,
189 dm.dmDisplayFrequency, dm.dmPosition.x, dm.dmPosition.y);
191 ret = NtUserChangeDisplaySettings( &device_name, &dm, NULL,
192 CDS_GLOBAL | CDS_NORESET | CDS_UPDATEREGISTRY, NULL );
193 if (ret != DISP_CHANGE_SUCCESSFUL)
194 ERR("Failed to save registry display settings for %s, returned %d.\n",
195 wine_dbgstr_w(dd.DeviceName), ret);
199 BOOL get_primary_adapter(WCHAR *name)
201 DISPLAY_DEVICEW dd;
202 DWORD i;
204 dd.cb = sizeof(dd);
205 for (i = 0; !NtUserEnumDisplayDevices( NULL, i, &dd, 0 ); ++i)
207 if (dd.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
209 lstrcpyW(name, dd.DeviceName);
210 return TRUE;
214 return FALSE;
217 static void set_display_depth(ULONG_PTR display_id, DWORD depth)
219 struct x11drv_display_depth *display_depth;
221 pthread_mutex_lock( &settings_mutex );
222 LIST_FOR_EACH_ENTRY(display_depth, &x11drv_display_depth_list, struct x11drv_display_depth, entry)
224 if (display_depth->display_id == display_id)
226 display_depth->depth = depth;
227 pthread_mutex_unlock( &settings_mutex );
228 return;
232 display_depth = malloc(sizeof(*display_depth));
233 if (!display_depth)
235 ERR("Failed to allocate memory.\n");
236 pthread_mutex_unlock( &settings_mutex );
237 return;
240 display_depth->display_id = display_id;
241 display_depth->depth = depth;
242 list_add_head(&x11drv_display_depth_list, &display_depth->entry);
243 pthread_mutex_unlock( &settings_mutex );
246 static DWORD get_display_depth(ULONG_PTR display_id)
248 struct x11drv_display_depth *display_depth;
249 DWORD depth;
251 pthread_mutex_lock( &settings_mutex );
252 LIST_FOR_EACH_ENTRY(display_depth, &x11drv_display_depth_list, struct x11drv_display_depth, entry)
254 if (display_depth->display_id == display_id)
256 depth = display_depth->depth;
257 pthread_mutex_unlock( &settings_mutex );
258 return depth;
261 pthread_mutex_unlock( &settings_mutex );
262 return screen_bpp;
265 /***********************************************************************
266 * GetCurrentDisplaySettings (X11DRV.@)
269 BOOL X11DRV_GetCurrentDisplaySettings( LPCWSTR name, LPDEVMODEW devmode )
271 DEVMODEW mode;
272 ULONG_PTR id;
274 if (!settings_handler.get_id( name, &id ) || !settings_handler.get_current_mode( id, &mode ))
276 ERR("Failed to get %s current display settings.\n", wine_dbgstr_w(name));
277 return FALSE;
280 memcpy( &devmode->dmFields, &mode.dmFields, devmode->dmSize - offsetof(DEVMODEW, dmFields) );
281 if (!is_detached_mode( devmode )) devmode->dmBitsPerPel = get_display_depth( id );
282 return TRUE;
285 BOOL is_detached_mode(const DEVMODEW *mode)
287 return mode->dmFields & DM_POSITION &&
288 mode->dmFields & DM_PELSWIDTH &&
289 mode->dmFields & DM_PELSHEIGHT &&
290 mode->dmPelsWidth == 0 &&
291 mode->dmPelsHeight == 0;
294 /* Get the full display mode with all the necessary fields set.
295 * Return NULL on failure. Caller should call free_full_mode() to free the returned mode. */
296 static DEVMODEW *get_full_mode(ULONG_PTR id, DEVMODEW *dev_mode)
298 DEVMODEW *modes, *full_mode, *found_mode = NULL;
299 UINT mode_count, mode_idx;
301 if (is_detached_mode(dev_mode))
302 return dev_mode;
304 if (!settings_handler.get_modes(id, EDS_ROTATEDMODE, &modes, &mode_count))
305 return NULL;
307 for (mode_idx = 0; mode_idx < mode_count; ++mode_idx)
309 found_mode = (DEVMODEW *)((BYTE *)modes + (sizeof(*modes) + modes[0].dmDriverExtra) * mode_idx);
311 if (found_mode->dmBitsPerPel != dev_mode->dmBitsPerPel)
312 continue;
313 if (found_mode->dmPelsWidth != dev_mode->dmPelsWidth)
314 continue;
315 if (found_mode->dmPelsHeight != dev_mode->dmPelsHeight)
316 continue;
317 if (found_mode->dmDisplayFrequency != dev_mode->dmDisplayFrequency)
318 continue;
319 if (found_mode->dmDisplayOrientation != dev_mode->dmDisplayOrientation)
320 continue;
322 break;
325 if (!found_mode || mode_idx == mode_count)
327 settings_handler.free_modes(modes);
328 return NULL;
331 if (!(full_mode = malloc(sizeof(*found_mode) + found_mode->dmDriverExtra)))
333 settings_handler.free_modes(modes);
334 return NULL;
337 memcpy(full_mode, found_mode, sizeof(*found_mode) + found_mode->dmDriverExtra);
338 settings_handler.free_modes(modes);
340 full_mode->dmFields |= DM_POSITION;
341 full_mode->dmPosition = dev_mode->dmPosition;
342 return full_mode;
345 static void free_full_mode(DEVMODEW *mode)
347 if (!is_detached_mode(mode))
348 free(mode);
351 static LONG apply_display_settings( DEVMODEW *displays, ULONG_PTR *ids, BOOL do_attach )
353 DEVMODEW *full_mode;
354 BOOL attached_mode;
355 LONG count, ret;
356 DEVMODEW *mode;
358 for (count = 0, mode = displays; mode->dmSize; mode = NEXT_DEVMODEW(mode), count++)
360 ULONG_PTR *id = ids + count;
362 attached_mode = !is_detached_mode(mode);
363 if ((attached_mode && !do_attach) || (!attached_mode && do_attach))
364 continue;
366 /* FIXME: get a full mode again because X11 driver extra data isn't portable */
367 full_mode = get_full_mode(*id, mode);
368 if (!full_mode)
369 return DISP_CHANGE_BADMODE;
371 TRACE("handler:%s changing %s to position:(%d,%d) resolution:%ux%u frequency:%uHz "
372 "depth:%ubits orientation:%#x.\n", settings_handler.name,
373 wine_dbgstr_w(mode->dmDeviceName),
374 full_mode->dmPosition.x, full_mode->dmPosition.y, full_mode->dmPelsWidth,
375 full_mode->dmPelsHeight, full_mode->dmDisplayFrequency, full_mode->dmBitsPerPel,
376 full_mode->dmDisplayOrientation);
378 ret = settings_handler.set_current_mode(*id, full_mode);
379 if (attached_mode && ret == DISP_CHANGE_SUCCESSFUL)
380 set_display_depth(*id, full_mode->dmBitsPerPel);
381 free_full_mode(full_mode);
382 if (ret != DISP_CHANGE_SUCCESSFUL)
383 return ret;
386 return DISP_CHANGE_SUCCESSFUL;
389 /***********************************************************************
390 * ChangeDisplaySettings (X11DRV.@)
393 LONG X11DRV_ChangeDisplaySettings( LPDEVMODEW displays, HWND hwnd, DWORD flags, LPVOID lpvoid )
395 INT left_most = INT_MAX, top_most = INT_MAX;
396 LONG count, ret = DISP_CHANGE_BADPARAM;
397 ULONG_PTR *ids;
398 DEVMODEW *mode;
400 /* Convert virtual screen coordinates to root coordinates, and find display ids.
401 * We cannot safely get the ids while changing modes, as the backend state may be invalidated.
403 for (count = 0, mode = displays; mode->dmSize; mode = NEXT_DEVMODEW(mode), count++)
405 left_most = min( left_most, mode->dmPosition.x );
406 top_most = min( top_most, mode->dmPosition.y );
409 if (!(ids = calloc( count, sizeof(*ids) ))) return DISP_CHANGE_FAILED;
410 for (count = 0, mode = displays; mode->dmSize; mode = NEXT_DEVMODEW(mode), count++)
412 if (!settings_handler.get_id( mode->dmDeviceName, ids + count )) goto done;
413 mode->dmPosition.x -= left_most;
414 mode->dmPosition.y -= top_most;
417 /* Detach displays first to free up CRTCs */
418 ret = apply_display_settings( displays, ids, FALSE );
419 if (ret == DISP_CHANGE_SUCCESSFUL)
420 ret = apply_display_settings( displays, ids, TRUE );
421 if (ret == DISP_CHANGE_SUCCESSFUL)
422 X11DRV_DisplayDevices_Update();
424 done:
425 free( ids );
426 return ret;
429 POINT virtual_screen_to_root(INT x, INT y)
431 RECT virtual = NtUserGetVirtualScreenRect();
432 POINT pt;
434 pt.x = x - virtual.left;
435 pt.y = y - virtual.top;
436 return pt;
439 POINT root_to_virtual_screen(INT x, INT y)
441 RECT virtual = NtUserGetVirtualScreenRect();
442 POINT pt;
444 pt.x = x + virtual.left;
445 pt.y = y + virtual.top;
446 return pt;
449 /* Get the primary monitor rect from the host system */
450 RECT get_host_primary_monitor_rect(void)
452 INT gpu_count, adapter_count, monitor_count;
453 struct gdi_gpu *gpus = NULL;
454 struct gdi_adapter *adapters = NULL;
455 struct gdi_monitor *monitors = NULL;
456 RECT rect = {0};
458 /* The first monitor is always primary */
459 if (host_handler.get_gpus(&gpus, &gpu_count) && gpu_count &&
460 host_handler.get_adapters(gpus[0].id, &adapters, &adapter_count) && adapter_count &&
461 host_handler.get_monitors(adapters[0].id, &monitors, &monitor_count) && monitor_count)
462 rect = monitors[0].rc_monitor;
464 if (gpus) host_handler.free_gpus(gpus);
465 if (adapters) host_handler.free_adapters(adapters);
466 if (monitors) host_handler.free_monitors(monitors, monitor_count);
467 return rect;
470 BOOL get_host_primary_gpu(struct gdi_gpu *gpu)
472 struct gdi_gpu *gpus;
473 INT gpu_count;
475 if (host_handler.get_gpus(&gpus, &gpu_count) && gpu_count)
477 *gpu = gpus[0];
478 host_handler.free_gpus(gpus);
479 return TRUE;
482 return FALSE;
485 RECT get_work_area(const RECT *monitor_rect)
487 Atom type;
488 int format;
489 unsigned long count, remaining, i;
490 long *work_area;
491 RECT work_rect;
493 /* Try _GTK_WORKAREAS first as _NET_WORKAREA may be incorrect on multi-monitor systems */
494 if (!XGetWindowProperty(gdi_display, DefaultRootWindow(gdi_display),
495 x11drv_atom(_GTK_WORKAREAS_D0), 0, ~0, False, XA_CARDINAL, &type,
496 &format, &count, &remaining, (unsigned char **)&work_area))
498 if (type == XA_CARDINAL && format == 32)
500 for (i = 0; i < count / 4; ++i)
502 work_rect.left = work_area[i * 4];
503 work_rect.top = work_area[i * 4 + 1];
504 work_rect.right = work_rect.left + work_area[i * 4 + 2];
505 work_rect.bottom = work_rect.top + work_area[i * 4 + 3];
507 if (intersect_rect( &work_rect, &work_rect, monitor_rect ))
509 TRACE("work_rect:%s.\n", wine_dbgstr_rect(&work_rect));
510 XFree(work_area);
511 return work_rect;
515 XFree(work_area);
518 WARN("_GTK_WORKAREAS is not supported, fallback to _NET_WORKAREA. "
519 "Work areas may be incorrect on multi-monitor systems.\n");
520 if (!XGetWindowProperty(gdi_display, DefaultRootWindow(gdi_display), x11drv_atom(_NET_WORKAREA),
521 0, ~0, False, XA_CARDINAL, &type, &format, &count, &remaining,
522 (unsigned char **)&work_area))
524 if (type == XA_CARDINAL && format == 32 && count >= 4)
526 SetRect(&work_rect, work_area[0], work_area[1], work_area[0] + work_area[2],
527 work_area[1] + work_area[3]);
529 if (intersect_rect( &work_rect, &work_rect, monitor_rect ))
531 TRACE("work_rect:%s.\n", wine_dbgstr_rect(&work_rect));
532 XFree(work_area);
533 return work_rect;
536 XFree(work_area);
539 WARN("_NET_WORKAREA is not supported, Work areas may be incorrect.\n");
540 TRACE("work_rect:%s.\n", wine_dbgstr_rect(monitor_rect));
541 return *monitor_rect;
544 void X11DRV_DisplayDevices_SetHandler(const struct x11drv_display_device_handler *new_handler)
546 if (new_handler->priority > host_handler.priority)
548 host_handler = *new_handler;
549 TRACE("Display device functions are now handled by: %s\n", host_handler.name);
553 void X11DRV_DisplayDevices_RegisterEventHandlers(void)
555 struct x11drv_display_device_handler *handler = is_virtual_desktop() ? &desktop_handler : &host_handler;
557 if (handler->register_event_handlers)
558 handler->register_event_handlers();
561 void X11DRV_DisplayDevices_Update(void)
563 X11DRV_DisplayDevices_Init(TRUE);
565 X11DRV_resize_desktop();
568 static BOOL force_display_devices_refresh;
570 BOOL X11DRV_UpdateDisplayDevices( const struct gdi_device_manager *device_manager, BOOL force, void *param )
572 struct x11drv_display_device_handler *handler;
573 struct gdi_adapter *adapters;
574 struct gdi_monitor *monitors;
575 struct gdi_gpu *gpus;
576 INT gpu_count, adapter_count, monitor_count;
577 INT gpu, adapter, monitor;
578 DEVMODEW *modes, *mode;
579 DWORD mode_count;
581 if (!force && !force_display_devices_refresh) return TRUE;
582 force_display_devices_refresh = FALSE;
583 handler = is_virtual_desktop() ? &desktop_handler : &host_handler;
585 TRACE("via %s\n", wine_dbgstr_a(handler->name));
587 /* Initialize GPUs */
588 if (!handler->get_gpus( &gpus, &gpu_count )) return FALSE;
589 TRACE("GPU count: %d\n", gpu_count);
591 for (gpu = 0; gpu < gpu_count; gpu++)
593 device_manager->add_gpu( &gpus[gpu], param );
595 /* Initialize adapters */
596 if (!handler->get_adapters(gpus[gpu].id, &adapters, &adapter_count)) break;
597 TRACE("GPU: %#lx %s, adapter count: %d\n", gpus[gpu].id, wine_dbgstr_w(gpus[gpu].name), adapter_count);
599 for (adapter = 0; adapter < adapter_count; adapter++)
601 device_manager->add_adapter( &adapters[adapter], param );
603 if (!handler->get_monitors(adapters[adapter].id, &monitors, &monitor_count)) break;
604 TRACE("adapter: %#lx, monitor count: %d\n", adapters[adapter].id, monitor_count);
606 /* Initialize monitors */
607 for (monitor = 0; monitor < monitor_count; monitor++)
609 TRACE("monitor: %#x %s\n", monitor, wine_dbgstr_w(monitors[monitor].name));
610 device_manager->add_monitor( &monitors[monitor], param );
613 handler->free_monitors(monitors, monitor_count);
615 if (!settings_handler.get_modes( adapters[adapter].id, EDS_ROTATEDMODE, &modes, &mode_count ))
616 continue;
618 for (mode = modes; mode_count; mode_count--)
620 TRACE( "mode: %p\n", mode );
621 device_manager->add_mode( mode, param );
622 mode = (DEVMODEW *)((char *)mode + sizeof(*modes) + modes[0].dmDriverExtra);
625 settings_handler.free_modes( modes );
628 handler->free_adapters(adapters);
631 handler->free_gpus(gpus);
632 return TRUE;
635 void X11DRV_DisplayDevices_Init(BOOL force)
637 UINT32 num_path, num_mode;
639 if (force) force_display_devices_refresh = TRUE;
640 /* trigger refresh in win32u */
641 NtUserGetDisplayConfigBufferSizes( QDC_ONLY_ACTIVE_PATHS, &num_path, &num_mode );