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 *
403 size_t nmonitors = 0;
412 for (UINT32 i = 0; i < vscreen->nmonitors; i++)
416 if (!xf_is_monitor_id_active(xfc, i))
419 if (!vscreen->monitors)
422 rdpMonitor* monitor = &rdpmonitors[nmonitors];
424 WINPR_ASSERTING_INT_CAST(
425 int32_t, vscreen->monitors[i].area.left*(
431 WINPR_ASSERTING_INT_CAST(
432 int32_t, vscreen->monitors[i].area.top*(
438 WINPR_ASSERTING_INT_CAST(
440 (vscreen->monitors[i].area.right - vscreen->monitors[i].area.left + 1) *
446 WINPR_ASSERTING_INT_CAST(
448 (vscreen->monitors[i].area.bottom - vscreen->monitors[i].area.top + 1) *
453 monitor->orig_screen = i;
456 if (useXRandr && rrmonitors)
460 attrs = &monitor->attributes;
461 attrs->physicalWidth = WINPR_ASSERTING_INT_CAST(uint32_t, rrmonitors[i].mwidth);
462 attrs->physicalHeight = WINPR_ASSERTING_INT_CAST(uint32_t, rrmonitors[i].mheight);
463 ret = XRRRotations(xfc->display, WINPR_ASSERTING_INT_CAST(
int, i), &rot);
464 attrs->orientation = ret;
471 monitor->is_primary = TRUE;
472 primaryMonitorFound = TRUE;
481 if ((nmonitors == 0) && (vscreen->nmonitors > 0))
483 if (!vscreen->monitors)
486 const MONITOR_INFO* vmonitor = &vscreen->monitors[current_monitor];
489 const INT32 width = area->right - area->left + 1;
490 const INT32 height = area->bottom - area->top + 1;
492 ((width < 0) || ((UINT32)width < *pMaxWidth)) ? width : (INT32)*pMaxWidth;
494 ((height < 0) || ((UINT32)height < *pMaxHeight)) ? width : (INT32)*pMaxHeight;
500 monitor->x = area->left;
501 monitor->y = area->top;
502 monitor->width = maxw;
503 monitor->height = maxh;
504 monitor->orig_screen = current_monitor;
509 WINPR_ASSERTING_INT_CAST(uint32_t, nmonitors)))
520 int vX = cmonitor->x;
521 int vY = cmonitor->y;
522 int vR = vX + cmonitor->width;
523 int vB = vY + cmonitor->height;
524 const int32_t corig = WINPR_ASSERTING_INT_CAST(int32_t, cmonitor->orig_screen);
525 xfc->fullscreenMonitors.top = corig;
526 xfc->fullscreenMonitors.bottom = corig;
527 xfc->fullscreenMonitors.left = corig;
528 xfc->fullscreenMonitors.right = corig;
538 const int destX = MIN(vX, monitor->x);
539 const int destY = MIN(vY, monitor->y);
540 const int destR = MAX(vR, monitor->x + monitor->width);
541 const int destB = MAX(vB, monitor->y + monitor->height);
542 const int32_t orig = WINPR_ASSERTING_INT_CAST(int32_t, monitor->orig_screen);
545 xfc->fullscreenMonitors.left = orig;
548 xfc->fullscreenMonitors.top = orig;
551 xfc->fullscreenMonitors.right = orig;
554 xfc->fullscreenMonitors.bottom = orig;
557 WINPR_ASSERT(ps <= 100);
563 vX = (destX * psuw) / 100;
564 vY = (destY * psuh) / 100;
565 vR = (destR * psuw) / 100;
566 vB = (destB * psuh) / 100;
569 vscreen->area.left = 0;
570 const int r = vR - vX - 1;
571 vscreen->area.right = WINPR_ASSERTING_INT_CAST(UINT16, r);
572 vscreen->area.top = 0;
573 const int b = vB - vY - 1;
574 vscreen->area.bottom = WINPR_ASSERTING_INT_CAST(UINT16, b);
578 INT64 bottom = 1LL * xfc->workArea.height + xfc->workArea.y - 1LL;
579 vscreen->area.top = WINPR_ASSERTING_INT_CAST(UINT16, xfc->workArea.y);
580 vscreen->area.bottom = WINPR_ASSERTING_INT_CAST(UINT16, bottom);
583 if (!primaryMonitorFound)
590 freerdp_settings_get_pointer_array_writable(settings, FreeRDP_MonitorIds, 0);
592 monitor_index = *ids;
600 screen = DefaultScreenOfDisplay(xfc->display);
601 monitor_index = WINPR_ASSERTING_INT_CAST(uint32_t, XScreenNumberOfScreen(screen));
604 UINT32 j = monitor_index;
608 if ((pmonitor->x != 0) || (pmonitor->y != 0))
610 pmonitor->is_primary = TRUE;
620 if (!primaryMonitorFound && monitor->x == 0 && monitor->y == 0)
622 monitor->is_primary = TRUE;
623 primaryMonitorFound = TRUE;
631 *pMaxWidth = MIN(*pMaxWidth, (UINT32)vscreen->area.right - vscreen->area.left + 1);
632 *pMaxHeight = MIN(*pMaxHeight, (UINT32)vscreen->area.bottom - vscreen->area.top + 1);
651 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.