wow64: In wow64_NtSetInformationToken forward TokenIntegrityLevel.
[wine.git] / dlls / winex11.drv / display.c
blob6b92e046fb8d51539f7f152a1957b52c000b8c6a
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"
27 #include "ntstatus.h"
28 #define WIN32_NO_STATUS
29 #include "x11drv.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
34 static struct x11drv_display_device_handler host_handler;
35 static struct x11drv_settings_handler settings_handler;
37 #define NEXT_DEVMODEW(mode) ((DEVMODEW *)((char *)((mode) + 1) + (mode)->dmDriverExtra))
39 struct x11drv_display_depth
41 struct list entry;
42 x11drv_settings_id display_id;
43 DWORD depth;
46 /* Display device emulated depth list, protected by modes_section */
47 static struct list x11drv_display_depth_list = LIST_INIT(x11drv_display_depth_list);
49 /* All Windows drivers seen so far either support 32 bit depths, or 24 bit depths, but never both. So if we have
50 * a 32 bit framebuffer, report 32 bit bpps, otherwise 24 bit ones.
52 static const unsigned int depths_24[] = {8, 16, 24};
53 static const unsigned int depths_32[] = {8, 16, 32};
54 const unsigned int *depths;
56 static pthread_mutex_t settings_mutex = PTHREAD_MUTEX_INITIALIZER;
58 void X11DRV_Settings_SetHandler(const struct x11drv_settings_handler *new_handler)
60 if (new_handler->priority > settings_handler.priority)
62 settings_handler = *new_handler;
63 TRACE("Display settings are now handled by: %s.\n", settings_handler.name);
67 /***********************************************************************
68 * Default handlers if resolution switching is not enabled
71 static BOOL nores_get_id(const WCHAR *device_name, BOOL is_primary, x11drv_settings_id *id)
73 id->id = is_primary ? 1 : 0;
74 return TRUE;
77 static BOOL nores_get_modes( x11drv_settings_id id, DWORD flags, DEVMODEW **new_modes, UINT *mode_count, BOOL full )
79 RECT primary = get_host_primary_monitor_rect();
80 DEVMODEW *modes;
82 modes = calloc(1, sizeof(*modes));
83 if (!modes)
85 RtlSetLastWin32Error( ERROR_NOT_ENOUGH_MEMORY );
86 return FALSE;
89 modes[0].dmSize = sizeof(*modes);
90 modes[0].dmDriverExtra = 0;
91 modes[0].dmFields = DM_DISPLAYORIENTATION | DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT |
92 DM_DISPLAYFLAGS | DM_DISPLAYFREQUENCY;
93 modes[0].dmDisplayOrientation = DMDO_DEFAULT;
94 modes[0].dmBitsPerPel = screen_bpp;
95 modes[0].dmPelsWidth = primary.right;
96 modes[0].dmPelsHeight = primary.bottom;
97 modes[0].dmDisplayFlags = 0;
98 modes[0].dmDisplayFrequency = 60;
100 *new_modes = modes;
101 *mode_count = 1;
102 return TRUE;
105 static void nores_free_modes(DEVMODEW *modes)
107 free(modes);
110 static BOOL nores_get_current_mode(x11drv_settings_id id, DEVMODEW *mode)
112 RECT primary = get_host_primary_monitor_rect();
114 mode->dmFields = DM_DISPLAYORIENTATION | DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT |
115 DM_DISPLAYFLAGS | DM_DISPLAYFREQUENCY | DM_POSITION;
116 mode->dmDisplayOrientation = DMDO_DEFAULT;
117 mode->dmDisplayFlags = 0;
118 mode->dmPosition.x = 0;
119 mode->dmPosition.y = 0;
121 if (id.id != 1)
123 FIXME("Non-primary adapters are unsupported.\n");
124 mode->dmBitsPerPel = 0;
125 mode->dmPelsWidth = 0;
126 mode->dmPelsHeight = 0;
127 mode->dmDisplayFrequency = 0;
128 return TRUE;
131 mode->dmBitsPerPel = screen_bpp;
132 mode->dmPelsWidth = primary.right;
133 mode->dmPelsHeight = primary.bottom;
134 mode->dmDisplayFrequency = 60;
135 return TRUE;
138 static LONG nores_set_current_mode(x11drv_settings_id id, const DEVMODEW *mode)
140 WARN("NoRes settings handler, ignoring mode change request.\n");
141 return DISP_CHANGE_SUCCESSFUL;
144 /* default handler only gets the current X desktop resolution */
145 void X11DRV_Settings_Init(void)
147 struct x11drv_settings_handler nores_handler;
149 depths = screen_bpp == 32 ? depths_32 : depths_24;
151 nores_handler.name = "NoRes";
152 nores_handler.priority = 1;
153 nores_handler.get_id = nores_get_id;
154 nores_handler.get_modes = nores_get_modes;
155 nores_handler.free_modes = nores_free_modes;
156 nores_handler.get_current_mode = nores_get_current_mode;
157 nores_handler.set_current_mode = nores_set_current_mode;
158 X11DRV_Settings_SetHandler(&nores_handler);
161 static void set_display_depth(x11drv_settings_id display_id, DWORD depth)
163 struct x11drv_display_depth *display_depth;
165 pthread_mutex_lock( &settings_mutex );
166 LIST_FOR_EACH_ENTRY(display_depth, &x11drv_display_depth_list, struct x11drv_display_depth, entry)
168 if (display_depth->display_id.id == display_id.id)
170 display_depth->depth = depth;
171 pthread_mutex_unlock( &settings_mutex );
172 return;
176 display_depth = malloc(sizeof(*display_depth));
177 if (!display_depth)
179 ERR("Failed to allocate memory.\n");
180 pthread_mutex_unlock( &settings_mutex );
181 return;
184 display_depth->display_id = display_id;
185 display_depth->depth = depth;
186 list_add_head(&x11drv_display_depth_list, &display_depth->entry);
187 pthread_mutex_unlock( &settings_mutex );
190 static DWORD get_display_depth(x11drv_settings_id display_id)
192 struct x11drv_display_depth *display_depth;
193 DWORD depth;
195 pthread_mutex_lock( &settings_mutex );
196 LIST_FOR_EACH_ENTRY(display_depth, &x11drv_display_depth_list, struct x11drv_display_depth, entry)
198 if (display_depth->display_id.id == display_id.id)
200 depth = display_depth->depth;
201 pthread_mutex_unlock( &settings_mutex );
202 return depth;
205 pthread_mutex_unlock( &settings_mutex );
206 return screen_bpp;
209 INT X11DRV_GetDisplayDepth(LPCWSTR name, BOOL is_primary)
211 x11drv_settings_id id;
213 if (settings_handler.get_id( name, is_primary, &id ))
214 return get_display_depth( id );
216 return screen_bpp;
219 /***********************************************************************
220 * GetCurrentDisplaySettings (X11DRV.@)
223 BOOL X11DRV_GetCurrentDisplaySettings( LPCWSTR name, BOOL is_primary, LPDEVMODEW devmode )
225 DEVMODEW mode;
226 x11drv_settings_id id;
228 if (!settings_handler.get_id( name, is_primary, &id ) || !settings_handler.get_current_mode( id, &mode ))
230 ERR("Failed to get %s current display settings.\n", wine_dbgstr_w(name));
231 return FALSE;
234 memcpy( &devmode->dmFields, &mode.dmFields, devmode->dmSize - offsetof(DEVMODEW, dmFields) );
235 if (!is_detached_mode( devmode )) devmode->dmBitsPerPel = get_display_depth( id );
236 return TRUE;
239 BOOL is_detached_mode(const DEVMODEW *mode)
241 return mode->dmFields & DM_POSITION &&
242 mode->dmFields & DM_PELSWIDTH &&
243 mode->dmFields & DM_PELSHEIGHT &&
244 mode->dmPelsWidth == 0 &&
245 mode->dmPelsHeight == 0;
248 static BOOL is_same_devmode( const DEVMODEW *a, const DEVMODEW *b )
250 return a->dmDisplayOrientation == b->dmDisplayOrientation &&
251 a->dmBitsPerPel == b->dmBitsPerPel &&
252 a->dmPelsWidth == b->dmPelsWidth &&
253 a->dmPelsHeight == b->dmPelsHeight &&
254 a->dmDisplayFrequency == b->dmDisplayFrequency;
257 /* Get the full display mode with all the necessary fields set.
258 * Return NULL on failure. Caller should call free_full_mode() to free the returned mode. */
259 static DEVMODEW *get_full_mode(x11drv_settings_id id, DEVMODEW *dev_mode)
261 DEVMODEW *modes, *full_mode, *found_mode = NULL;
262 UINT mode_count, mode_idx;
264 if (is_detached_mode(dev_mode))
265 return dev_mode;
267 if (!settings_handler.get_modes( id, EDS_ROTATEDMODE, &modes, &mode_count, TRUE )) return NULL;
269 for (mode_idx = 0; mode_idx < mode_count; ++mode_idx)
271 found_mode = (DEVMODEW *)((BYTE *)modes + (sizeof(*modes) + modes[0].dmDriverExtra) * mode_idx);
272 if (is_same_devmode( found_mode, dev_mode )) break;
275 if (!found_mode || mode_idx == mode_count)
277 settings_handler.free_modes(modes);
278 return NULL;
281 if (!(full_mode = malloc(sizeof(*found_mode) + found_mode->dmDriverExtra)))
283 settings_handler.free_modes(modes);
284 return NULL;
287 memcpy(full_mode, found_mode, sizeof(*found_mode) + found_mode->dmDriverExtra);
288 settings_handler.free_modes(modes);
290 full_mode->dmFields |= DM_POSITION;
291 full_mode->dmPosition = dev_mode->dmPosition;
292 return full_mode;
295 static void free_full_mode(DEVMODEW *mode)
297 if (!is_detached_mode(mode))
298 free(mode);
301 static LONG apply_display_settings( DEVMODEW *displays, x11drv_settings_id *ids, BOOL do_attach )
303 DEVMODEW *full_mode;
304 BOOL attached_mode;
305 LONG count, ret;
306 DEVMODEW *mode;
308 for (count = 0, mode = displays; mode->dmSize; mode = NEXT_DEVMODEW(mode), count++)
310 x11drv_settings_id *id = ids + count;
312 attached_mode = !is_detached_mode(mode);
313 if ((attached_mode && !do_attach) || (!attached_mode && do_attach))
314 continue;
316 /* FIXME: get a full mode again because X11 driver extra data isn't portable */
317 full_mode = get_full_mode(*id, mode);
318 if (!full_mode)
319 return DISP_CHANGE_BADMODE;
321 TRACE("handler:%s changing %s to position:(%d,%d) resolution:%ux%u frequency:%uHz "
322 "depth:%ubits orientation:%#x.\n", settings_handler.name,
323 wine_dbgstr_w(mode->dmDeviceName),
324 (int)full_mode->dmPosition.x, (int)full_mode->dmPosition.y, (int)full_mode->dmPelsWidth,
325 (int)full_mode->dmPelsHeight, (int)full_mode->dmDisplayFrequency,
326 (int)full_mode->dmBitsPerPel, (int)full_mode->dmDisplayOrientation);
328 ret = settings_handler.set_current_mode(*id, full_mode);
329 if (attached_mode && ret == DISP_CHANGE_SUCCESSFUL)
330 set_display_depth(*id, full_mode->dmBitsPerPel);
331 free_full_mode(full_mode);
332 if (ret != DISP_CHANGE_SUCCESSFUL)
333 return ret;
336 return DISP_CHANGE_SUCCESSFUL;
339 /***********************************************************************
340 * ChangeDisplaySettings (X11DRV.@)
343 LONG X11DRV_ChangeDisplaySettings( LPDEVMODEW displays, LPCWSTR primary_name, HWND hwnd, DWORD flags, LPVOID lpvoid )
345 INT left_most = INT_MAX, top_most = INT_MAX;
346 LONG count, ret = DISP_CHANGE_BADPARAM;
347 x11drv_settings_id *ids;
348 DEVMODEW *mode;
350 /* Convert virtual screen coordinates to root coordinates, and find display ids.
351 * We cannot safely get the ids while changing modes, as the backend state may be invalidated.
353 for (count = 0, mode = displays; mode->dmSize; mode = NEXT_DEVMODEW(mode), count++)
355 left_most = min( left_most, mode->dmPosition.x );
356 top_most = min( top_most, mode->dmPosition.y );
359 if (!(ids = calloc( count, sizeof(*ids) ))) return DISP_CHANGE_FAILED;
360 for (count = 0, mode = displays; mode->dmSize; mode = NEXT_DEVMODEW(mode), count++)
362 if (!settings_handler.get_id( mode->dmDeviceName, !wcsicmp( mode->dmDeviceName, primary_name ), ids + count )) goto done;
363 mode->dmPosition.x -= left_most;
364 mode->dmPosition.y -= top_most;
367 /* Detach displays first to free up CRTCs */
368 ret = apply_display_settings( displays, ids, FALSE );
369 if (ret == DISP_CHANGE_SUCCESSFUL)
370 ret = apply_display_settings( displays, ids, TRUE );
372 done:
373 free( ids );
374 return ret;
377 POINT virtual_screen_to_root(INT x, INT y)
379 RECT virtual = NtUserGetVirtualScreenRect();
380 POINT pt;
382 pt.x = x - virtual.left;
383 pt.y = y - virtual.top;
384 return pt;
387 POINT root_to_virtual_screen(INT x, INT y)
389 RECT virtual = NtUserGetVirtualScreenRect();
390 POINT pt;
392 pt.x = x + virtual.left;
393 pt.y = y + virtual.top;
394 return pt;
397 /* Get the primary monitor rect from the host system */
398 RECT get_host_primary_monitor_rect(void)
400 INT gpu_count, adapter_count, monitor_count;
401 struct x11drv_gpu *gpus = NULL;
402 struct x11drv_adapter *adapters = NULL;
403 struct gdi_monitor *monitors = NULL;
404 RECT rect = {0};
406 /* The first monitor is always primary */
407 if (host_handler.get_gpus(&gpus, &gpu_count, FALSE) && gpu_count &&
408 host_handler.get_adapters(gpus[0].id, &adapters, &adapter_count) && adapter_count &&
409 host_handler.get_monitors(adapters[0].id, &monitors, &monitor_count) && monitor_count)
410 rect = monitors[0].rc_monitor;
412 if (gpus) host_handler.free_gpus( gpus, gpu_count );
413 if (adapters) host_handler.free_adapters(adapters);
414 if (monitors) host_handler.free_monitors(monitors, monitor_count);
415 return rect;
418 RECT get_work_area(const RECT *monitor_rect)
420 Atom type;
421 int format;
422 unsigned long count, remaining, i;
423 long *work_area;
424 RECT work_rect;
426 /* Try _GTK_WORKAREAS first as _NET_WORKAREA may be incorrect on multi-monitor systems */
427 if (!XGetWindowProperty(gdi_display, DefaultRootWindow(gdi_display),
428 x11drv_atom(_GTK_WORKAREAS_D0), 0, ~0, False, XA_CARDINAL, &type,
429 &format, &count, &remaining, (unsigned char **)&work_area))
431 if (type == XA_CARDINAL && format == 32)
433 for (i = 0; i < count / 4; ++i)
435 work_rect.left = work_area[i * 4];
436 work_rect.top = work_area[i * 4 + 1];
437 work_rect.right = work_rect.left + work_area[i * 4 + 2];
438 work_rect.bottom = work_rect.top + work_area[i * 4 + 3];
440 if (intersect_rect( &work_rect, &work_rect, monitor_rect ))
442 TRACE("work_rect:%s.\n", wine_dbgstr_rect(&work_rect));
443 XFree(work_area);
444 return work_rect;
448 XFree(work_area);
451 WARN("_GTK_WORKAREAS is not supported, fallback to _NET_WORKAREA. "
452 "Work areas may be incorrect on multi-monitor systems.\n");
453 if (!XGetWindowProperty(gdi_display, DefaultRootWindow(gdi_display), x11drv_atom(_NET_WORKAREA),
454 0, ~0, False, XA_CARDINAL, &type, &format, &count, &remaining,
455 (unsigned char **)&work_area))
457 if (type == XA_CARDINAL && format == 32 && count >= 4)
459 SetRect(&work_rect, work_area[0], work_area[1], work_area[0] + work_area[2],
460 work_area[1] + work_area[3]);
462 if (intersect_rect( &work_rect, &work_rect, monitor_rect ))
464 TRACE("work_rect:%s.\n", wine_dbgstr_rect(&work_rect));
465 XFree(work_area);
466 return work_rect;
469 XFree(work_area);
472 WARN("_NET_WORKAREA is not supported, Work areas may be incorrect.\n");
473 TRACE("work_rect:%s.\n", wine_dbgstr_rect(monitor_rect));
474 return *monitor_rect;
477 void X11DRV_DisplayDevices_SetHandler(const struct x11drv_display_device_handler *new_handler)
479 if (new_handler->priority > host_handler.priority)
481 host_handler = *new_handler;
482 TRACE("Display device functions are now handled by: %s\n", host_handler.name);
486 void X11DRV_DisplayDevices_RegisterEventHandlers(void)
488 if (host_handler.register_event_handlers) host_handler.register_event_handlers();
491 /* Report whether a display device handler supports detecting dynamic device changes */
492 BOOL X11DRV_DisplayDevices_SupportEventHandlers(void)
494 return !!host_handler.register_event_handlers;
497 UINT X11DRV_UpdateDisplayDevices( const struct gdi_device_manager *device_manager, void *param )
499 struct x11drv_adapter *adapters;
500 struct gdi_monitor *monitors;
501 struct x11drv_gpu *gpus;
502 INT gpu_count, adapter_count, monitor_count;
503 INT gpu, adapter, monitor;
504 DEVMODEW *modes;
505 UINT mode_count;
507 TRACE( "via %s\n", debugstr_a(host_handler.name) );
509 /* Initialize GPUs */
510 if (!host_handler.get_gpus( &gpus, &gpu_count, TRUE )) return STATUS_UNSUCCESSFUL;
511 TRACE("GPU count: %d\n", gpu_count);
513 for (gpu = 0; gpu < gpu_count; gpu++)
515 device_manager->add_gpu( gpus[gpu].name, &gpus[gpu].pci_id, &gpus[gpu].vulkan_uuid, param );
517 /* Initialize adapters */
518 if (!host_handler.get_adapters( gpus[gpu].id, &adapters, &adapter_count )) break;
519 TRACE( "GPU: %#lx %s, adapter count: %d\n", gpus[gpu].id, debugstr_a( gpus[gpu].name ), adapter_count );
521 for (adapter = 0; adapter < adapter_count; adapter++)
523 DEVMODEW current_mode = {.dmSize = sizeof(current_mode)};
524 WCHAR devname[32];
525 char buffer[32];
526 x11drv_settings_id settings_id;
527 BOOL is_primary = adapters[adapter].state_flags & DISPLAY_DEVICE_PRIMARY_DEVICE;
529 sprintf( buffer, "%04lx", adapters[adapter].id );
530 device_manager->add_source( buffer, adapters[adapter].state_flags, param );
532 if (!host_handler.get_monitors( adapters[adapter].id, &monitors, &monitor_count )) break;
533 TRACE("adapter: %#lx, monitor count: %d\n", adapters[adapter].id, monitor_count);
535 /* Initialize monitors */
536 for (monitor = 0; monitor < monitor_count; monitor++)
537 device_manager->add_monitor( &monitors[monitor], param );
539 host_handler.free_monitors( monitors, monitor_count );
541 /* Get the settings handler id for the adapter */
542 snprintf( buffer, sizeof(buffer), "\\\\.\\DISPLAY%d", adapter + 1 );
543 asciiz_to_unicode( devname, buffer );
544 if (!settings_handler.get_id( devname, is_primary, &settings_id )) break;
546 settings_handler.get_current_mode( settings_id, &current_mode );
547 if (settings_handler.get_modes( settings_id, EDS_ROTATEDMODE, &modes, &mode_count, FALSE ))
549 device_manager->add_modes( &current_mode, mode_count, modes, param );
550 settings_handler.free_modes( modes );
554 host_handler.free_adapters( adapters );
557 host_handler.free_gpus( gpus, gpu_count );
558 return STATUS_SUCCESS;