winex11.drv: Extract function to check devmode equality.
[wine.git] / dlls / winex11.drv / display.c
blob5fb027da680b53c97ccc01d352da27377c6a6dc3
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, BOOL is_primary, ULONG_PTR *id)
71 *id = is_primary ? 1 : 0;
72 return TRUE;
75 static BOOL nores_get_modes(ULONG_PTR id, DWORD flags, DEVMODEW **new_modes, UINT *mode_count)
77 RECT primary = get_host_primary_monitor_rect();
78 DEVMODEW *modes;
80 modes = calloc(1, sizeof(*modes));
81 if (!modes)
83 RtlSetLastWin32Error( ERROR_NOT_ENOUGH_MEMORY );
84 return FALSE;
87 modes[0].dmSize = sizeof(*modes);
88 modes[0].dmDriverExtra = 0;
89 modes[0].dmFields = DM_DISPLAYORIENTATION | DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT |
90 DM_DISPLAYFLAGS | DM_DISPLAYFREQUENCY;
91 modes[0].dmDisplayOrientation = DMDO_DEFAULT;
92 modes[0].dmBitsPerPel = screen_bpp;
93 modes[0].dmPelsWidth = primary.right;
94 modes[0].dmPelsHeight = primary.bottom;
95 modes[0].dmDisplayFlags = 0;
96 modes[0].dmDisplayFrequency = 60;
98 *new_modes = modes;
99 *mode_count = 1;
100 return TRUE;
103 static void nores_free_modes(DEVMODEW *modes)
105 free(modes);
108 static BOOL nores_get_current_mode(ULONG_PTR id, DEVMODEW *mode)
110 RECT primary = get_host_primary_monitor_rect();
112 mode->dmFields = DM_DISPLAYORIENTATION | DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT |
113 DM_DISPLAYFLAGS | DM_DISPLAYFREQUENCY | DM_POSITION;
114 mode->dmDisplayOrientation = DMDO_DEFAULT;
115 mode->dmDisplayFlags = 0;
116 mode->dmPosition.x = 0;
117 mode->dmPosition.y = 0;
119 if (id != 1)
121 FIXME("Non-primary adapters are unsupported.\n");
122 mode->dmBitsPerPel = 0;
123 mode->dmPelsWidth = 0;
124 mode->dmPelsHeight = 0;
125 mode->dmDisplayFrequency = 0;
126 return TRUE;
129 mode->dmBitsPerPel = screen_bpp;
130 mode->dmPelsWidth = primary.right;
131 mode->dmPelsHeight = primary.bottom;
132 mode->dmDisplayFrequency = 60;
133 return TRUE;
136 static LONG nores_set_current_mode(ULONG_PTR id, const DEVMODEW *mode)
138 WARN("NoRes settings handler, ignoring mode change request.\n");
139 return DISP_CHANGE_SUCCESSFUL;
142 /* default handler only gets the current X desktop resolution */
143 void X11DRV_Settings_Init(void)
145 struct x11drv_settings_handler nores_handler;
147 depths = screen_bpp == 32 ? depths_32 : depths_24;
149 nores_handler.name = "NoRes";
150 nores_handler.priority = 1;
151 nores_handler.get_id = nores_get_id;
152 nores_handler.get_modes = nores_get_modes;
153 nores_handler.free_modes = nores_free_modes;
154 nores_handler.get_current_mode = nores_get_current_mode;
155 nores_handler.set_current_mode = nores_set_current_mode;
156 X11DRV_Settings_SetHandler(&nores_handler);
159 /* Initialize registry display settings when new display devices are added */
160 void init_registry_display_settings(void)
162 DEVMODEW dm = {.dmSize = sizeof(dm)};
163 DISPLAY_DEVICEW dd = {sizeof(dd)};
164 UNICODE_STRING device_name;
165 DWORD i = 0;
166 int ret;
168 while (!NtUserEnumDisplayDevices( NULL, i++, &dd, 0 ))
170 RtlInitUnicodeString( &device_name, dd.DeviceName );
172 /* Skip if the device already has registry display settings */
173 if (NtUserEnumDisplaySettings( &device_name, ENUM_REGISTRY_SETTINGS, &dm, 0 ))
174 continue;
176 if (!NtUserEnumDisplaySettings( &device_name, ENUM_CURRENT_SETTINGS, &dm, 0 ))
178 ERR("Failed to query current display settings for %s.\n", wine_dbgstr_w(dd.DeviceName));
179 continue;
182 TRACE("Device %s current display mode %ux%u %ubits %uHz at %d,%d.\n",
183 wine_dbgstr_w(dd.DeviceName), (int)dm.dmPelsWidth, (int)dm.dmPelsHeight,
184 (int)dm.dmBitsPerPel, (int)dm.dmDisplayFrequency, (int)dm.dmPosition.x, (int)dm.dmPosition.y);
186 ret = NtUserChangeDisplaySettings( &device_name, &dm, NULL,
187 CDS_GLOBAL | CDS_NORESET | CDS_UPDATEREGISTRY, NULL );
188 if (ret != DISP_CHANGE_SUCCESSFUL)
189 ERR("Failed to save registry display settings for %s, returned %d.\n",
190 wine_dbgstr_w(dd.DeviceName), ret);
194 static void set_display_depth(ULONG_PTR display_id, DWORD depth)
196 struct x11drv_display_depth *display_depth;
198 pthread_mutex_lock( &settings_mutex );
199 LIST_FOR_EACH_ENTRY(display_depth, &x11drv_display_depth_list, struct x11drv_display_depth, entry)
201 if (display_depth->display_id == display_id)
203 display_depth->depth = depth;
204 pthread_mutex_unlock( &settings_mutex );
205 return;
209 display_depth = malloc(sizeof(*display_depth));
210 if (!display_depth)
212 ERR("Failed to allocate memory.\n");
213 pthread_mutex_unlock( &settings_mutex );
214 return;
217 display_depth->display_id = display_id;
218 display_depth->depth = depth;
219 list_add_head(&x11drv_display_depth_list, &display_depth->entry);
220 pthread_mutex_unlock( &settings_mutex );
223 static DWORD get_display_depth(ULONG_PTR display_id)
225 struct x11drv_display_depth *display_depth;
226 DWORD depth;
228 pthread_mutex_lock( &settings_mutex );
229 LIST_FOR_EACH_ENTRY(display_depth, &x11drv_display_depth_list, struct x11drv_display_depth, entry)
231 if (display_depth->display_id == display_id)
233 depth = display_depth->depth;
234 pthread_mutex_unlock( &settings_mutex );
235 return depth;
238 pthread_mutex_unlock( &settings_mutex );
239 return screen_bpp;
242 INT X11DRV_GetDisplayDepth(LPCWSTR name, BOOL is_primary)
244 ULONG_PTR id;
246 if (settings_handler.get_id( name, is_primary, &id ))
247 return get_display_depth( id );
249 return screen_bpp;
252 /***********************************************************************
253 * GetCurrentDisplaySettings (X11DRV.@)
256 BOOL X11DRV_GetCurrentDisplaySettings( LPCWSTR name, BOOL is_primary, LPDEVMODEW devmode )
258 DEVMODEW mode;
259 ULONG_PTR id;
261 if (!settings_handler.get_id( name, is_primary, &id ) || !settings_handler.get_current_mode( id, &mode ))
263 ERR("Failed to get %s current display settings.\n", wine_dbgstr_w(name));
264 return FALSE;
267 memcpy( &devmode->dmFields, &mode.dmFields, devmode->dmSize - offsetof(DEVMODEW, dmFields) );
268 if (!is_detached_mode( devmode )) devmode->dmBitsPerPel = get_display_depth( id );
269 return TRUE;
272 BOOL is_detached_mode(const DEVMODEW *mode)
274 return mode->dmFields & DM_POSITION &&
275 mode->dmFields & DM_PELSWIDTH &&
276 mode->dmFields & DM_PELSHEIGHT &&
277 mode->dmPelsWidth == 0 &&
278 mode->dmPelsHeight == 0;
281 static BOOL is_same_devmode( const DEVMODEW *a, const DEVMODEW *b )
283 return a->dmDisplayOrientation == b->dmDisplayOrientation &&
284 a->dmBitsPerPel == b->dmBitsPerPel &&
285 a->dmPelsWidth == b->dmPelsWidth &&
286 a->dmPelsHeight == b->dmPelsHeight &&
287 a->dmDisplayFrequency == b->dmDisplayFrequency;
290 /* Get the full display mode with all the necessary fields set.
291 * Return NULL on failure. Caller should call free_full_mode() to free the returned mode. */
292 static DEVMODEW *get_full_mode(ULONG_PTR id, DEVMODEW *dev_mode)
294 DEVMODEW *modes, *full_mode, *found_mode = NULL;
295 UINT mode_count, mode_idx;
297 if (is_detached_mode(dev_mode))
298 return dev_mode;
300 if (!settings_handler.get_modes(id, EDS_ROTATEDMODE, &modes, &mode_count))
301 return NULL;
303 for (mode_idx = 0; mode_idx < mode_count; ++mode_idx)
305 found_mode = (DEVMODEW *)((BYTE *)modes + (sizeof(*modes) + modes[0].dmDriverExtra) * mode_idx);
306 if (is_same_devmode( found_mode, dev_mode )) break;
309 if (!found_mode || mode_idx == mode_count)
311 settings_handler.free_modes(modes);
312 return NULL;
315 if (!(full_mode = malloc(sizeof(*found_mode) + found_mode->dmDriverExtra)))
317 settings_handler.free_modes(modes);
318 return NULL;
321 memcpy(full_mode, found_mode, sizeof(*found_mode) + found_mode->dmDriverExtra);
322 settings_handler.free_modes(modes);
324 full_mode->dmFields |= DM_POSITION;
325 full_mode->dmPosition = dev_mode->dmPosition;
326 return full_mode;
329 static void free_full_mode(DEVMODEW *mode)
331 if (!is_detached_mode(mode))
332 free(mode);
335 static LONG apply_display_settings( DEVMODEW *displays, ULONG_PTR *ids, BOOL do_attach )
337 DEVMODEW *full_mode;
338 BOOL attached_mode;
339 LONG count, ret;
340 DEVMODEW *mode;
342 for (count = 0, mode = displays; mode->dmSize; mode = NEXT_DEVMODEW(mode), count++)
344 ULONG_PTR *id = ids + count;
346 attached_mode = !is_detached_mode(mode);
347 if ((attached_mode && !do_attach) || (!attached_mode && do_attach))
348 continue;
350 /* FIXME: get a full mode again because X11 driver extra data isn't portable */
351 full_mode = get_full_mode(*id, mode);
352 if (!full_mode)
353 return DISP_CHANGE_BADMODE;
355 TRACE("handler:%s changing %s to position:(%d,%d) resolution:%ux%u frequency:%uHz "
356 "depth:%ubits orientation:%#x.\n", settings_handler.name,
357 wine_dbgstr_w(mode->dmDeviceName),
358 (int)full_mode->dmPosition.x, (int)full_mode->dmPosition.y, (int)full_mode->dmPelsWidth,
359 (int)full_mode->dmPelsHeight, (int)full_mode->dmDisplayFrequency,
360 (int)full_mode->dmBitsPerPel, (int)full_mode->dmDisplayOrientation);
362 ret = settings_handler.set_current_mode(*id, full_mode);
363 if (attached_mode && ret == DISP_CHANGE_SUCCESSFUL)
364 set_display_depth(*id, full_mode->dmBitsPerPel);
365 free_full_mode(full_mode);
366 if (ret != DISP_CHANGE_SUCCESSFUL)
367 return ret;
370 return DISP_CHANGE_SUCCESSFUL;
373 /***********************************************************************
374 * ChangeDisplaySettings (X11DRV.@)
377 LONG X11DRV_ChangeDisplaySettings( LPDEVMODEW displays, LPCWSTR primary_name, HWND hwnd, DWORD flags, LPVOID lpvoid )
379 INT left_most = INT_MAX, top_most = INT_MAX;
380 LONG count, ret = DISP_CHANGE_BADPARAM;
381 ULONG_PTR *ids;
382 DEVMODEW *mode;
384 /* Convert virtual screen coordinates to root coordinates, and find display ids.
385 * We cannot safely get the ids while changing modes, as the backend state may be invalidated.
387 for (count = 0, mode = displays; mode->dmSize; mode = NEXT_DEVMODEW(mode), count++)
389 left_most = min( left_most, mode->dmPosition.x );
390 top_most = min( top_most, mode->dmPosition.y );
393 if (!(ids = calloc( count, sizeof(*ids) ))) return DISP_CHANGE_FAILED;
394 for (count = 0, mode = displays; mode->dmSize; mode = NEXT_DEVMODEW(mode), count++)
396 if (!settings_handler.get_id( mode->dmDeviceName, !wcsicmp( mode->dmDeviceName, primary_name ), ids + count )) goto done;
397 mode->dmPosition.x -= left_most;
398 mode->dmPosition.y -= top_most;
401 /* Detach displays first to free up CRTCs */
402 ret = apply_display_settings( displays, ids, FALSE );
403 if (ret == DISP_CHANGE_SUCCESSFUL)
404 ret = apply_display_settings( displays, ids, TRUE );
406 done:
407 free( ids );
408 return ret;
411 POINT virtual_screen_to_root(INT x, INT y)
413 RECT virtual = NtUserGetVirtualScreenRect();
414 POINT pt;
416 pt.x = x - virtual.left;
417 pt.y = y - virtual.top;
418 return pt;
421 POINT root_to_virtual_screen(INT x, INT y)
423 RECT virtual = NtUserGetVirtualScreenRect();
424 POINT pt;
426 pt.x = x + virtual.left;
427 pt.y = y + virtual.top;
428 return pt;
431 /* Get the primary monitor rect from the host system */
432 RECT get_host_primary_monitor_rect(void)
434 INT gpu_count, adapter_count, monitor_count;
435 struct gdi_gpu *gpus = NULL;
436 struct gdi_adapter *adapters = NULL;
437 struct gdi_monitor *monitors = NULL;
438 RECT rect = {0};
440 /* The first monitor is always primary */
441 if (host_handler.get_gpus(&gpus, &gpu_count) && gpu_count &&
442 host_handler.get_adapters(gpus[0].id, &adapters, &adapter_count) && adapter_count &&
443 host_handler.get_monitors(adapters[0].id, &monitors, &monitor_count) && monitor_count)
444 rect = monitors[0].rc_monitor;
446 if (gpus) host_handler.free_gpus(gpus);
447 if (adapters) host_handler.free_adapters(adapters);
448 if (monitors) host_handler.free_monitors(monitors, monitor_count);
449 return rect;
452 BOOL get_host_primary_gpu(struct gdi_gpu *gpu)
454 struct gdi_gpu *gpus;
455 INT gpu_count;
457 if (host_handler.get_gpus(&gpus, &gpu_count) && gpu_count)
459 *gpu = gpus[0];
460 host_handler.free_gpus(gpus);
461 return TRUE;
464 return FALSE;
467 RECT get_work_area(const RECT *monitor_rect)
469 Atom type;
470 int format;
471 unsigned long count, remaining, i;
472 long *work_area;
473 RECT work_rect;
475 /* Try _GTK_WORKAREAS first as _NET_WORKAREA may be incorrect on multi-monitor systems */
476 if (!XGetWindowProperty(gdi_display, DefaultRootWindow(gdi_display),
477 x11drv_atom(_GTK_WORKAREAS_D0), 0, ~0, False, XA_CARDINAL, &type,
478 &format, &count, &remaining, (unsigned char **)&work_area))
480 if (type == XA_CARDINAL && format == 32)
482 for (i = 0; i < count / 4; ++i)
484 work_rect.left = work_area[i * 4];
485 work_rect.top = work_area[i * 4 + 1];
486 work_rect.right = work_rect.left + work_area[i * 4 + 2];
487 work_rect.bottom = work_rect.top + work_area[i * 4 + 3];
489 if (intersect_rect( &work_rect, &work_rect, monitor_rect ))
491 TRACE("work_rect:%s.\n", wine_dbgstr_rect(&work_rect));
492 XFree(work_area);
493 return work_rect;
497 XFree(work_area);
500 WARN("_GTK_WORKAREAS is not supported, fallback to _NET_WORKAREA. "
501 "Work areas may be incorrect on multi-monitor systems.\n");
502 if (!XGetWindowProperty(gdi_display, DefaultRootWindow(gdi_display), x11drv_atom(_NET_WORKAREA),
503 0, ~0, False, XA_CARDINAL, &type, &format, &count, &remaining,
504 (unsigned char **)&work_area))
506 if (type == XA_CARDINAL && format == 32 && count >= 4)
508 SetRect(&work_rect, work_area[0], work_area[1], work_area[0] + work_area[2],
509 work_area[1] + work_area[3]);
511 if (intersect_rect( &work_rect, &work_rect, monitor_rect ))
513 TRACE("work_rect:%s.\n", wine_dbgstr_rect(&work_rect));
514 XFree(work_area);
515 return work_rect;
518 XFree(work_area);
521 WARN("_NET_WORKAREA is not supported, Work areas may be incorrect.\n");
522 TRACE("work_rect:%s.\n", wine_dbgstr_rect(monitor_rect));
523 return *monitor_rect;
526 void X11DRV_DisplayDevices_SetHandler(const struct x11drv_display_device_handler *new_handler)
528 if (new_handler->priority > host_handler.priority)
530 host_handler = *new_handler;
531 TRACE("Display device functions are now handled by: %s\n", host_handler.name);
535 void X11DRV_DisplayDevices_RegisterEventHandlers(void)
537 struct x11drv_display_device_handler *handler = is_virtual_desktop() ? &desktop_handler : &host_handler;
539 if (handler->register_event_handlers)
540 handler->register_event_handlers();
543 /* Report whether a display device handler supports detecting dynamic device changes */
544 BOOL X11DRV_DisplayDevices_SupportEventHandlers(void)
546 return !!host_handler.register_event_handlers;
549 static BOOL force_display_devices_refresh;
551 BOOL X11DRV_UpdateDisplayDevices( const struct gdi_device_manager *device_manager, BOOL force, void *param )
553 struct x11drv_display_device_handler *handler;
554 struct gdi_adapter *adapters;
555 struct gdi_monitor *monitors;
556 struct gdi_gpu *gpus;
557 INT gpu_count, adapter_count, monitor_count;
558 INT gpu, adapter, monitor;
559 DEVMODEW *modes, *mode;
560 UINT mode_count;
562 if (!force && !force_display_devices_refresh) return TRUE;
563 force_display_devices_refresh = FALSE;
564 handler = is_virtual_desktop() ? &desktop_handler : &host_handler;
566 TRACE("via %s\n", wine_dbgstr_a(handler->name));
568 /* Initialize GPUs */
569 if (!handler->get_gpus( &gpus, &gpu_count )) return FALSE;
570 TRACE("GPU count: %d\n", gpu_count);
572 for (gpu = 0; gpu < gpu_count; gpu++)
574 device_manager->add_gpu( &gpus[gpu], param );
576 /* Initialize adapters */
577 if (!handler->get_adapters(gpus[gpu].id, &adapters, &adapter_count)) break;
578 TRACE("GPU: %#lx %s, adapter count: %d\n", gpus[gpu].id, wine_dbgstr_w(gpus[gpu].name), adapter_count);
580 for (adapter = 0; adapter < adapter_count; adapter++)
582 device_manager->add_adapter( &adapters[adapter], param );
584 if (!handler->get_monitors(adapters[adapter].id, &monitors, &monitor_count)) break;
585 TRACE("adapter: %#lx, monitor count: %d\n", adapters[adapter].id, monitor_count);
587 /* Initialize monitors */
588 for (monitor = 0; monitor < monitor_count; monitor++)
589 device_manager->add_monitor( &monitors[monitor], param );
591 handler->free_monitors(monitors, monitor_count);
593 if (!settings_handler.get_modes( adapters[adapter].id, EDS_ROTATEDMODE, &modes, &mode_count ))
594 continue;
596 for (mode = modes; mode_count; mode_count--)
598 TRACE( "mode: %p\n", mode );
599 device_manager->add_mode( mode, FALSE, param );
600 mode = (DEVMODEW *)((char *)mode + sizeof(*modes) + modes[0].dmDriverExtra);
603 settings_handler.free_modes( modes );
606 handler->free_adapters(adapters);
609 handler->free_gpus(gpus);
610 return TRUE;
613 void X11DRV_DisplayDevices_Init(BOOL force)
615 UINT32 num_path, num_mode;
617 if (force) force_display_devices_refresh = TRUE;
618 /* trigger refresh in win32u */
619 NtUserGetDisplayConfigBufferSizes( QDC_ONLY_ACTIVE_PATHS, &num_path, &num_mode );