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"
57int xf_list_monitors(xfContext* xfc)
64 Display* display = XOpenDisplay(NULL);
68 WLog_ERR(TAG,
"failed to open X display");
72#if defined(USABLE_XRANDR)
74 if (XRRQueryExtension(display, &major, &minor) &&
75 (XRRQueryVersion(display, &major, &minor) == True) && (major * 100 + minor >= 105))
77 XRRMonitorInfo* monitors =
78 XRRGetMonitors(display, DefaultRootWindow(display), 1, &nmonitors);
80 for (
int i = 0; i < nmonitors; i++)
82 printf(
" %s [%d] %dx%d\t+%d+%d\n", monitors[i].primary ?
"*" :
" ", i,
83 monitors[i].width, monitors[i].height, monitors[i].x, monitors[i].y);
86 XRRFreeMonitors(monitors);
91 if (XineramaQueryExtension(display, &major, &minor))
93 if (XineramaIsActive(display))
95 XineramaScreenInfo* screen = XineramaQueryScreens(display, &nmonitors);
97 for (
int i = 0; i < nmonitors; i++)
99 printf(
" %s [%d] %hdx%hd\t+%hd+%hd\n", (i == 0) ?
"*" :
" ", i,
100 screen[i].width, screen[i].height, screen[i].x_org, screen[i].y_org);
109 Screen* screen = ScreenOfDisplay(display, DefaultScreen(display));
110 printf(
" * [0] %dx%d\t+0+0\n", WidthOfScreen(screen), HeightOfScreen(screen));
114 XCloseDisplay(display);
118static BOOL xf_is_monitor_id_active(xfContext* xfc, UINT32
id)
120 const rdpSettings* settings = NULL;
124 settings = xfc->common.context.settings;
125 WINPR_ASSERT(settings);
128 if (NumMonitorIds == 0)
131 for (UINT32 index = 0; index < NumMonitorIds; index++)
133 const UINT32* cur = freerdp_settings_get_pointer_array(settings, FreeRDP_MonitorIds, index);
134 if (cur && (*cur ==
id))
141BOOL xf_detect_monitors(xfContext* xfc, UINT32* pMaxWidth, UINT32* pMaxHeight)
144 UINT32 monitor_index = 0;
145 BOOL primaryMonitorFound = FALSE;
150 UINT32 current_monitor = 0;
151 Screen* screen = NULL;
152#if defined WITH_XINERAMA || defined WITH_XRANDR
156#if defined(USABLE_XRANDR)
157 XRRMonitorInfo* rrmonitors = NULL;
158 BOOL useXRandr = FALSE;
161 if (!xfc || !pMaxWidth || !pMaxHeight || !xfc->common.context.settings)
164 rdpSettings* settings = xfc->common.context.settings;
165 VIRTUAL_SCREEN* vscreen = &xfc->vscreen;
180 if (!XQueryPointer(xfc->display, DefaultRootWindow(xfc->display), &_dummy_w, &_dummy_w,
181 &mouse_x, &mouse_y, &_dummy_i, &_dummy_i, (
void*)&_dummy_i))
182 mouse_x = mouse_y = 0;
184#if defined(USABLE_XRANDR)
186 if (XRRQueryExtension(xfc->display, &major, &minor) &&
187 (XRRQueryVersion(xfc->display, &major, &minor) == True) && (major * 100 + minor >= 105))
190 rrmonitors = XRRGetMonitors(xfc->display, DefaultRootWindow(xfc->display), 1, &nmonitors);
192 if ((nmonitors < 0) || (nmonitors > 16))
193 vscreen->nmonitors = 0;
195 vscreen->nmonitors = (UINT32)nmonitors;
197 if (vscreen->nmonitors)
199 for (UINT32 i = 0; i < vscreen->nmonitors; i++)
201 MONITOR_INFO* cur_vscreen = &vscreen->monitors[i];
202 const XRRMonitorInfo* cur_monitor = &rrmonitors[i];
204 cur_vscreen->area.left = WINPR_ASSERTING_INT_CAST(UINT16, cur_monitor->x);
205 cur_vscreen->area.top = WINPR_ASSERTING_INT_CAST(UINT16, cur_monitor->y);
206 cur_vscreen->area.right =
207 WINPR_ASSERTING_INT_CAST(UINT16, cur_monitor->x + cur_monitor->width - 1);
208 cur_vscreen->area.bottom =
209 WINPR_ASSERTING_INT_CAST(UINT16, cur_monitor->y + cur_monitor->height - 1);
210 cur_vscreen->primary = cur_monitor->primary > 0;
219 if (XineramaQueryExtension(xfc->display, &major, &minor) && XineramaIsActive(xfc->display))
222 XineramaScreenInfo* screenInfo = XineramaQueryScreens(xfc->display, &nmonitors);
224 if ((nmonitors < 0) || (nmonitors > 16))
225 vscreen->nmonitors = 0;
227 vscreen->nmonitors = (UINT32)nmonitors;
229 if (vscreen->nmonitors)
231 for (UINT32 i = 0; i < vscreen->nmonitors; i++)
233 MONITOR_INFO* monitor = &vscreen->monitors[i];
234 monitor->area.left = WINPR_ASSERTING_INT_CAST(uint16_t, screenInfo[i].x_org);
235 monitor->area.top = WINPR_ASSERTING_INT_CAST(uint16_t, screenInfo[i].y_org);
236 monitor->area.right = WINPR_ASSERTING_INT_CAST(
237 uint16_t, screenInfo[i].x_org + screenInfo[i].width - 1);
238 monitor->area.bottom = WINPR_ASSERTING_INT_CAST(
239 uint16_t, screenInfo[i].y_org + screenInfo[i].height - 1);
256 xfc->fullscreenMonitors.top = 0;
257 xfc->fullscreenMonitors.bottom = 0;
258 xfc->fullscreenMonitors.left = 0;
259 xfc->fullscreenMonitors.right = 0;
262 if (vscreen->monitors)
264 for (UINT32 i = 0; i < vscreen->nmonitors; i++)
266 const MONITOR_INFO* monitor = &vscreen->monitors[i];
268 if ((mouse_x >= monitor->area.left) && (mouse_x <= monitor->area.right) &&
269 (mouse_y >= monitor->area.top) && (mouse_y <= monitor->area.bottom))
294 UINT32
id = current_monitor;
320 (vscreen->nmonitors > current_monitor))
322 MONITOR_INFO* monitor = vscreen->monitors + current_monitor;
327 xfc->workArea.x = monitor->area.left;
328 xfc->workArea.y = monitor->area.top;
329 xfc->workArea.width = monitor->area.right - monitor->area.left + 1;
330 xfc->workArea.height = monitor->area.bottom - monitor->area.top + 1;
336 xfc->workArea.width = WINPR_ASSERTING_INT_CAST(uint32_t, WidthOfScreen(xfc->screen));
337 xfc->workArea.height = WINPR_ASSERTING_INT_CAST(uint32_t, HeightOfScreen(xfc->screen));
343 *pMaxWidth = WINPR_ASSERTING_INT_CAST(uint32_t, WidthOfScreen(xfc->screen));
344 *pMaxHeight = WINPR_ASSERTING_INT_CAST(uint32_t, HeightOfScreen(xfc->screen));
348 *pMaxWidth = xfc->workArea.width;
349 *pMaxHeight = xfc->workArea.height;
356 if (vscreen->nmonitors > 0)
358 if (!vscreen->monitors)
361 const MONITOR_INFO* vmonitor = &vscreen->monitors[current_monitor];
364 *pMaxWidth = area->right - area->left + 1;
365 *pMaxHeight = area->bottom - area->top + 1;
368 *pMaxWidth = ((area->right - area->left + 1) *
373 *pMaxHeight = ((area->bottom - area->top + 1) *
379 *pMaxWidth = xfc->workArea.width;
380 *pMaxHeight = xfc->workArea.height;
383 *pMaxWidth = (xfc->workArea.width *
388 *pMaxHeight = (xfc->workArea.height *
402 size_t nmonitors = 0;
411 for (UINT32 i = 0; i < vscreen->nmonitors; i++)
415 if (!xf_is_monitor_id_active(xfc, i))
418 if (!vscreen->monitors)
421 rdpMonitor* monitor = &rdpmonitors[nmonitors];
423 WINPR_ASSERTING_INT_CAST(
424 int32_t, vscreen->monitors[i].area.left*(
430 WINPR_ASSERTING_INT_CAST(
431 int32_t, vscreen->monitors[i].area.top*(
437 WINPR_ASSERTING_INT_CAST(
439 (vscreen->monitors[i].area.right - vscreen->monitors[i].area.left + 1) *
445 WINPR_ASSERTING_INT_CAST(
447 (vscreen->monitors[i].area.bottom - vscreen->monitors[i].area.top + 1) *
452 monitor->orig_screen = i;
455 if (useXRandr && rrmonitors)
459 attrs = &monitor->attributes;
460 attrs->physicalWidth = WINPR_ASSERTING_INT_CAST(uint32_t, rrmonitors[i].mwidth);
461 attrs->physicalHeight = WINPR_ASSERTING_INT_CAST(uint32_t, rrmonitors[i].mheight);
462 ret = XRRRotations(xfc->display, WINPR_ASSERTING_INT_CAST(
int, i), &rot);
463 attrs->orientation = ret;
470 monitor->is_primary = TRUE;
471 primaryMonitorFound = TRUE;
480 if ((nmonitors == 0) && (vscreen->nmonitors > 0))
482 if (!vscreen->monitors)
485 const MONITOR_INFO* vmonitor = &vscreen->monitors[current_monitor];
488 const INT32 width = area->right - area->left + 1;
489 const INT32 height = area->bottom - area->top + 1;
491 ((width < 0) || ((UINT32)width < *pMaxWidth)) ? width : (INT32)*pMaxWidth;
493 ((height < 0) || ((UINT32)height < *pMaxHeight)) ? width : (INT32)*pMaxHeight;
499 monitor->x = area->left;
500 monitor->y = area->top;
501 monitor->width = maxw;
502 monitor->height = maxh;
503 monitor->orig_screen = current_monitor;
508 WINPR_ASSERTING_INT_CAST(uint32_t, nmonitors)))
519 int vX = cmonitor->x;
520 int vY = cmonitor->y;
521 int vR = vX + cmonitor->width;
522 int vB = vY + cmonitor->height;
523 const int32_t corig = WINPR_ASSERTING_INT_CAST(int32_t, cmonitor->orig_screen);
524 xfc->fullscreenMonitors.top = corig;
525 xfc->fullscreenMonitors.bottom = corig;
526 xfc->fullscreenMonitors.left = corig;
527 xfc->fullscreenMonitors.right = corig;
537 const int destX = MIN(vX, monitor->x);
538 const int destY = MIN(vY, monitor->y);
539 const int destR = MAX(vR, monitor->x + monitor->width);
540 const int destB = MAX(vB, monitor->y + monitor->height);
541 const int32_t orig = WINPR_ASSERTING_INT_CAST(int32_t, monitor->orig_screen);
544 xfc->fullscreenMonitors.left = orig;
547 xfc->fullscreenMonitors.top = orig;
550 xfc->fullscreenMonitors.right = orig;
553 xfc->fullscreenMonitors.bottom = orig;
556 WINPR_ASSERT(ps <= 100);
562 vX = (destX * psuw) / 100;
563 vY = (destY * psuh) / 100;
564 vR = (destR * psuw) / 100;
565 vB = (destB * psuh) / 100;
568 vscreen->area.left = 0;
569 const int r = vR - vX - 1;
570 vscreen->area.right = WINPR_ASSERTING_INT_CAST(UINT16, r);
571 vscreen->area.top = 0;
572 const int b = vB - vY - 1;
573 vscreen->area.bottom = WINPR_ASSERTING_INT_CAST(UINT16, b);
577 INT64 bottom = 1LL * xfc->workArea.height + xfc->workArea.y - 1LL;
578 vscreen->area.top = WINPR_ASSERTING_INT_CAST(UINT16, xfc->workArea.y);
579 vscreen->area.bottom = WINPR_ASSERTING_INT_CAST(UINT16, bottom);
582 if (!primaryMonitorFound)
589 freerdp_settings_get_pointer_array_writable(settings, FreeRDP_MonitorIds, 0);
591 monitor_index = *ids;
599 screen = DefaultScreenOfDisplay(xfc->display);
600 monitor_index = WINPR_ASSERTING_INT_CAST(uint32_t, XScreenNumberOfScreen(screen));
603 UINT32 j = monitor_index;
607 if ((pmonitor->x != 0) || (pmonitor->y != 0))
609 pmonitor->is_primary = TRUE;
619 if (!primaryMonitorFound && monitor->x == 0 && monitor->y == 0)
621 monitor->is_primary = TRUE;
622 primaryMonitorFound = TRUE;
630 *pMaxWidth = MIN(*pMaxWidth, (UINT32)vscreen->area.right - vscreen->area.left + 1);
631 *pMaxHeight = MIN(*pMaxHeight, (UINT32)vscreen->area.bottom - vscreen->area.top + 1);
650 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.