22#include <freerdp/config.h>
30#include <winpr/assert.h>
31#include <winpr/cast.h>
34#include <freerdp/log.h>
36#define TAG CLIENT_TAG("x11")
39#include <X11/extensions/Xinerama.h>
43#include <X11/extensions/Xrandr.h>
44#include <X11/extensions/randr.h>
46#if (RANDR_MAJOR * 100 + RANDR_MINOR) >= 105
52#include "xf_monitor.h"
58int xf_list_monitors(xfContext* xfc)
65 Display* display = XOpenDisplay(NULL);
69 WLog_ERR(TAG,
"failed to open X display");
73#if defined(USABLE_XRANDR)
75 if (XRRQueryExtension(display, &major, &minor) &&
76 (XRRQueryVersion(display, &major, &minor) == True) && (major * 100 + minor >= 105))
78 XRRMonitorInfo* monitors =
79 XRRGetMonitors(display, DefaultRootWindow(display), 1, &nmonitors);
81 for (
int i = 0; i < nmonitors; i++)
83 printf(
" %s [%d] %dx%d\t+%d+%d\n", monitors[i].primary ?
"*" :
" ", i,
84 monitors[i].width, monitors[i].height, monitors[i].x, monitors[i].y);
87 XRRFreeMonitors(monitors);
92 if (XineramaQueryExtension(display, &major, &minor))
94 if (XineramaIsActive(display))
96 XineramaScreenInfo* screen = XineramaQueryScreens(display, &nmonitors);
98 for (
int i = 0; i < nmonitors; i++)
100 printf(
" %s [%d] %hdx%hd\t+%hd+%hd\n", (i == 0) ?
"*" :
" ", i,
101 screen[i].width, screen[i].height, screen[i].x_org, screen[i].y_org);
110 Screen* screen = ScreenOfDisplay(display, DefaultScreen(display));
111 printf(
" * [0] %dx%d\t+0+0\n", WidthOfScreen(screen), HeightOfScreen(screen));
115 LogDynAndXCloseDisplay(xfc->log, display);
119static BOOL xf_is_monitor_id_active(xfContext* xfc, UINT32
id)
121 const rdpSettings* settings = NULL;
125 settings = xfc->common.context.settings;
126 WINPR_ASSERT(settings);
129 if (NumMonitorIds == 0)
132 for (UINT32 index = 0; index < NumMonitorIds; index++)
134 const UINT32* cur = freerdp_settings_get_pointer_array(settings, FreeRDP_MonitorIds, index);
135 if (cur && (*cur ==
id))
142BOOL xf_detect_monitors(xfContext* xfc, UINT32* pMaxWidth, UINT32* pMaxHeight)
145 UINT32 monitor_index = 0;
146 BOOL primaryMonitorFound = FALSE;
151 UINT32 current_monitor = 0;
152 Screen* screen = NULL;
153#if defined WITH_XINERAMA || defined WITH_XRANDR
157#if defined(USABLE_XRANDR)
158 XRRMonitorInfo* rrmonitors = NULL;
159 BOOL useXRandr = FALSE;
162 if (!xfc || !pMaxWidth || !pMaxHeight || !xfc->common.context.settings)
165 rdpSettings* settings = xfc->common.context.settings;
166 VIRTUAL_SCREEN* vscreen = &xfc->vscreen;
181 if (!XQueryPointer(xfc->display, DefaultRootWindow(xfc->display), &_dummy_w, &_dummy_w,
182 &mouse_x, &mouse_y, &_dummy_i, &_dummy_i, (
void*)&_dummy_i))
183 mouse_x = mouse_y = 0;
185#if defined(USABLE_XRANDR)
187 if (XRRQueryExtension(xfc->display, &major, &minor) &&
188 (XRRQueryVersion(xfc->display, &major, &minor) == True) && (major * 100 + minor >= 105))
191 rrmonitors = XRRGetMonitors(xfc->display, DefaultRootWindow(xfc->display), 1, &nmonitors);
193 if ((nmonitors < 0) || (nmonitors > 16))
194 vscreen->nmonitors = 0;
196 vscreen->nmonitors = (UINT32)nmonitors;
198 if (vscreen->nmonitors)
200 for (UINT32 i = 0; i < vscreen->nmonitors; i++)
202 MONITOR_INFO* cur_vscreen = &vscreen->monitors[i];
203 const XRRMonitorInfo* cur_monitor = &rrmonitors[i];
205 cur_vscreen->area.left = WINPR_ASSERTING_INT_CAST(UINT16, cur_monitor->x);
206 cur_vscreen->area.top = WINPR_ASSERTING_INT_CAST(UINT16, cur_monitor->y);
207 cur_vscreen->area.right =
208 WINPR_ASSERTING_INT_CAST(UINT16, cur_monitor->x + cur_monitor->width - 1);
209 cur_vscreen->area.bottom =
210 WINPR_ASSERTING_INT_CAST(UINT16, cur_monitor->y + cur_monitor->height - 1);
211 cur_vscreen->primary = cur_monitor->primary > 0;
220 if (XineramaQueryExtension(xfc->display, &major, &minor) && XineramaIsActive(xfc->display))
223 XineramaScreenInfo* screenInfo = XineramaQueryScreens(xfc->display, &nmonitors);
225 if ((nmonitors < 0) || (nmonitors > 16))
226 vscreen->nmonitors = 0;
228 vscreen->nmonitors = (UINT32)nmonitors;
230 if (vscreen->nmonitors)
232 for (UINT32 i = 0; i < vscreen->nmonitors; i++)
234 MONITOR_INFO* monitor = &vscreen->monitors[i];
235 monitor->area.left = WINPR_ASSERTING_INT_CAST(uint16_t, screenInfo[i].x_org);
236 monitor->area.top = WINPR_ASSERTING_INT_CAST(uint16_t, screenInfo[i].y_org);
237 monitor->area.right = WINPR_ASSERTING_INT_CAST(
238 uint16_t, screenInfo[i].x_org + screenInfo[i].width - 1);
239 monitor->area.bottom = WINPR_ASSERTING_INT_CAST(
240 uint16_t, screenInfo[i].y_org + screenInfo[i].height - 1);
257 xfc->fullscreenMonitors.top = 0;
258 xfc->fullscreenMonitors.bottom = 0;
259 xfc->fullscreenMonitors.left = 0;
260 xfc->fullscreenMonitors.right = 0;
263 if (vscreen->monitors)
265 for (UINT32 i = 0; i < vscreen->nmonitors; i++)
267 const MONITOR_INFO* monitor = &vscreen->monitors[i];
269 if ((mouse_x >= monitor->area.left) && (mouse_x <= monitor->area.right) &&
270 (mouse_y >= monitor->area.top) && (mouse_y <= monitor->area.bottom))
295 UINT32
id = current_monitor;
321 (vscreen->nmonitors > current_monitor))
323 MONITOR_INFO* monitor = vscreen->monitors + current_monitor;
328 xfc->workArea.x = monitor->area.left;
329 xfc->workArea.y = monitor->area.top;
330 xfc->workArea.width = monitor->area.right - monitor->area.left + 1;
331 xfc->workArea.height = monitor->area.bottom - monitor->area.top + 1;
337 xfc->workArea.width = WINPR_ASSERTING_INT_CAST(uint32_t, WidthOfScreen(xfc->screen));
338 xfc->workArea.height = WINPR_ASSERTING_INT_CAST(uint32_t, HeightOfScreen(xfc->screen));
344 *pMaxWidth = WINPR_ASSERTING_INT_CAST(uint32_t, WidthOfScreen(xfc->screen));
345 *pMaxHeight = WINPR_ASSERTING_INT_CAST(uint32_t, HeightOfScreen(xfc->screen));
349 *pMaxWidth = xfc->workArea.width;
350 *pMaxHeight = xfc->workArea.height;
357 if (vscreen->nmonitors > 0)
359 if (!vscreen->monitors)
362 const MONITOR_INFO* vmonitor = &vscreen->monitors[current_monitor];
365 *pMaxWidth = area->right - area->left + 1;
366 *pMaxHeight = area->bottom - area->top + 1;
369 *pMaxWidth = ((area->right - area->left + 1) *
374 *pMaxHeight = ((area->bottom - area->top + 1) *
380 *pMaxWidth = xfc->workArea.width;
381 *pMaxHeight = xfc->workArea.height;
384 *pMaxWidth = (xfc->workArea.width *
389 *pMaxHeight = (xfc->workArea.height *
404 size_t nmonitors = 0;
413 for (UINT32 i = 0; i < vscreen->nmonitors; i++)
417 if (!xf_is_monitor_id_active(xfc, i))
420 if (!vscreen->monitors)
423 rdpMonitor* monitor = &rdpmonitors[nmonitors];
425 WINPR_ASSERTING_INT_CAST(
427 vscreen->monitors[i].area.left*(
433 WINPR_ASSERTING_INT_CAST(
435 vscreen->monitors[i].area.top*(
441 WINPR_ASSERTING_INT_CAST(
443 (vscreen->monitors[i].area.right - vscreen->monitors[i].area.left + 1) *
449 WINPR_ASSERTING_INT_CAST(
451 (vscreen->monitors[i].area.bottom - vscreen->monitors[i].area.top + 1) *
456 monitor->orig_screen = i;
459 if (useXRandr && rrmonitors)
463 attrs = &monitor->attributes;
464 attrs->physicalWidth = WINPR_ASSERTING_INT_CAST(uint32_t, rrmonitors[i].mwidth);
465 attrs->physicalHeight =
466 WINPR_ASSERTING_INT_CAST(uint32_t, rrmonitors[i].mheight);
467 ret = XRRRotations(xfc->display, WINPR_ASSERTING_INT_CAST(
int, i), &rot);
468 attrs->orientation = ret;
475 monitor->is_primary = TRUE;
476 primaryMonitorFound = TRUE;
485 if ((nmonitors == 0) && (vscreen->nmonitors > 0))
487 if (!vscreen->monitors)
490 const MONITOR_INFO* vmonitor = &vscreen->monitors[current_monitor];
493 const INT32 width = area->right - area->left + 1;
494 const INT32 height = area->bottom - area->top + 1;
496 ((width < 0) || ((UINT32)width < *pMaxWidth)) ? width : (INT32)*pMaxWidth;
498 ((height < 0) || ((UINT32)height < *pMaxHeight)) ? width : (INT32)*pMaxHeight;
504 monitor->x = area->left;
505 monitor->y = area->top;
506 monitor->width = maxw;
507 monitor->height = maxh;
508 monitor->orig_screen = current_monitor;
513 WINPR_ASSERTING_INT_CAST(uint32_t, nmonitors)))
524 int vX = cmonitor->x;
525 int vY = cmonitor->y;
526 int vR = vX + cmonitor->width;
527 int vB = vY + cmonitor->height;
528 const int32_t corig = WINPR_ASSERTING_INT_CAST(int32_t, cmonitor->orig_screen);
529 xfc->fullscreenMonitors.top = corig;
530 xfc->fullscreenMonitors.bottom = corig;
531 xfc->fullscreenMonitors.left = corig;
532 xfc->fullscreenMonitors.right = corig;
542 const int destX = MIN(vX, monitor->x);
543 const int destY = MIN(vY, monitor->y);
544 const int destR = MAX(vR, monitor->x + monitor->width);
545 const int destB = MAX(vB, monitor->y + monitor->height);
546 const int32_t orig = WINPR_ASSERTING_INT_CAST(int32_t, monitor->orig_screen);
549 xfc->fullscreenMonitors.left = orig;
552 xfc->fullscreenMonitors.top = orig;
555 xfc->fullscreenMonitors.right = orig;
558 xfc->fullscreenMonitors.bottom = orig;
561 WINPR_ASSERT(ps <= 100);
569 vX = (destX * psuw) / 100;
570 vY = (destY * psuh) / 100;
571 vR = (destR * psuw) / 100;
572 vB = (destB * psuh) / 100;
575 vscreen->area.left = 0;
576 const int r = vR - vX - 1;
577 vscreen->area.right = WINPR_ASSERTING_INT_CAST(UINT16, r);
578 vscreen->area.top = 0;
579 const int b = vB - vY - 1;
580 vscreen->area.bottom = WINPR_ASSERTING_INT_CAST(UINT16, b);
584 INT64 bottom = 1LL * xfc->workArea.height + xfc->workArea.y - 1LL;
585 vscreen->area.top = WINPR_ASSERTING_INT_CAST(UINT16, xfc->workArea.y);
586 vscreen->area.bottom = WINPR_ASSERTING_INT_CAST(UINT16, bottom);
589 if (!primaryMonitorFound)
595 UINT32* ids = freerdp_settings_get_pointer_array_writable(
596 settings, FreeRDP_MonitorIds, 0);
598 monitor_index = *ids;
606 screen = DefaultScreenOfDisplay(xfc->display);
608 WINPR_ASSERTING_INT_CAST(uint32_t, XScreenNumberOfScreen(screen));
611 UINT32 j = monitor_index;
615 if ((pmonitor->x != 0) || (pmonitor->y != 0))
617 pmonitor->is_primary = TRUE;
627 if (!primaryMonitorFound && monitor->x == 0 && monitor->y == 0)
629 monitor->is_primary = TRUE;
630 primaryMonitorFound = TRUE;
638 *pMaxWidth = MIN(*pMaxWidth, (UINT32)vscreen->area.right - vscreen->area.left + 1);
639 *pMaxHeight = MIN(*pMaxHeight, (UINT32)vscreen->area.bottom - vscreen->area.top + 1);
659 XRRFreeMonitors(rrmonitors);
FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.
FREERDP_API UINT64 freerdp_settings_get_uint64(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt64 id)
Returns a UINT64 settings value.
FREERDP_API BOOL freerdp_settings_set_pointer_len(rdpSettings *settings, FreeRDP_Settings_Keys_Pointer id, const void *data, size_t len)
Set a pointer to value data.
FREERDP_API BOOL freerdp_settings_set_monitor_def_array_sorted(rdpSettings *settings, const rdpMonitor *monitors, size_t count)
Sort monitor array according to:
FREERDP_API const void * freerdp_settings_get_pointer(const rdpSettings *settings, FreeRDP_Settings_Keys_Pointer id)
Returns a immutable pointer settings value.
FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 param)
Sets a UINT32 settings value.
FREERDP_API BOOL freerdp_settings_set_bool(rdpSettings *settings, FreeRDP_Settings_Keys_Bool id, BOOL param)
Sets a BOOL settings value.