FreeRDP/client/X11/xf_disp.c

461 lines
11 KiB
C
Raw Normal View History

/**
* FreeRDP: A Remote Desktop Protocol Implementation
* X11 Display Control channel
*
* Copyright 2017 David Fort <contact@hardening-consulting.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <winpr/sysinfo.h>
#include <X11/Xutil.h>
#ifdef WITH_XRANDR
#include <X11/extensions/Xrandr.h>
#include <X11/extensions/randr.h>
#if (RANDR_MAJOR * 100 + RANDR_MINOR) >= 105
2019-11-06 17:24:51 +03:00
#define USABLE_XRANDR
#endif
#endif
#include "xf_disp.h"
#include "xf_monitor.h"
#define TAG CLIENT_TAG("x11disp")
#define RESIZE_MIN_DELAY 200 /* minimum delay in ms between two resizes */
struct _xfDispContext
{
xfContext* xfc;
2018-09-24 17:44:25 +03:00
DispClientContext* disp;
BOOL haveXRandr;
int eventBase, errorBase;
int lastSentWidth, lastSentHeight;
UINT64 lastSentDate;
int targetWidth, targetHeight;
BOOL activated;
BOOL waitingResize;
BOOL fullscreen;
UINT16 lastSentDesktopOrientation;
UINT32 lastSentDesktopScaleFactor;
UINT32 lastSentDeviceScaleFactor;
};
static UINT xf_disp_sendLayout(DispClientContext* disp, rdpMonitor* monitors, int nmonitors);
static BOOL xf_disp_settings_changed(xfDispContext* xfDisp)
{
2018-10-02 13:33:52 +03:00
rdpSettings* settings = xfDisp->xfc->context.settings;
if (xfDisp->lastSentWidth != xfDisp->targetWidth)
return TRUE;
if (xfDisp->lastSentHeight != xfDisp->targetHeight)
return TRUE;
if (xfDisp->lastSentDesktopOrientation != settings->DesktopOrientation)
return TRUE;
if (xfDisp->lastSentDesktopScaleFactor != settings->DesktopScaleFactor)
return TRUE;
if (xfDisp->lastSentDeviceScaleFactor != settings->DeviceScaleFactor)
return TRUE;
if (xfDisp->fullscreen != xfDisp->xfc->fullscreen)
return TRUE;
return FALSE;
}
static BOOL xf_update_last_sent(xfDispContext* xfDisp)
{
2018-10-02 13:33:52 +03:00
rdpSettings* settings = xfDisp->xfc->context.settings;
xfDisp->lastSentWidth = xfDisp->targetWidth;
xfDisp->lastSentHeight = xfDisp->targetHeight;
xfDisp->lastSentDesktopOrientation = settings->DesktopOrientation;
xfDisp->lastSentDesktopScaleFactor = settings->DesktopScaleFactor;
xfDisp->lastSentDeviceScaleFactor = settings->DeviceScaleFactor;
xfDisp->fullscreen = xfDisp->xfc->fullscreen;
return TRUE;
}
static BOOL xf_disp_sendResize(xfDispContext* xfDisp)
{
DISPLAY_CONTROL_MONITOR_LAYOUT layout;
xfContext* xfc;
rdpSettings* settings;
if (!xfDisp || !xfDisp->xfc)
return FALSE;
xfc = xfDisp->xfc;
settings = xfc->context.settings;
if (!settings)
return FALSE;
2018-09-24 17:44:25 +03:00
if (!xfDisp->activated || !xfDisp->disp)
return TRUE;
if (GetTickCount64() - xfDisp->lastSentDate < RESIZE_MIN_DELAY)
return TRUE;
xfDisp->lastSentDate = GetTickCount64();
if (!xf_disp_settings_changed(xfDisp))
return TRUE;
if (xfc->fullscreen && (settings->MonitorCount > 0))
{
2019-11-06 17:24:51 +03:00
if (xf_disp_sendLayout(xfDisp->disp, settings->MonitorDefArray, settings->MonitorCount) !=
CHANNEL_RC_OK)
return FALSE;
}
else
{
xfDisp->waitingResize = TRUE;
layout.Flags = DISPLAY_CONTROL_MONITOR_PRIMARY;
layout.Top = layout.Left = 0;
layout.Width = xfDisp->targetWidth;
layout.Height = xfDisp->targetHeight;
layout.Orientation = settings->DesktopOrientation;
layout.DesktopScaleFactor = settings->DesktopScaleFactor;
layout.DeviceScaleFactor = settings->DeviceScaleFactor;
layout.PhysicalWidth = xfDisp->targetWidth;
layout.PhysicalHeight = xfDisp->targetHeight;
2018-09-24 17:44:25 +03:00
if (IFCALLRESULT(CHANNEL_RC_OK, xfDisp->disp->SendMonitorLayout, xfDisp->disp, 1,
&layout) != CHANNEL_RC_OK)
return FALSE;
}
return xf_update_last_sent(xfDisp);
}
static BOOL xf_disp_set_window_resizable(xfDispContext* xfDisp)
{
XSizeHints* size_hints;
if (!(size_hints = XAllocSizeHints()))
return FALSE;
size_hints->flags = PMinSize | PMaxSize | PWinGravity;
size_hints->win_gravity = NorthWestGravity;
size_hints->min_width = size_hints->min_height = 320;
size_hints->max_width = size_hints->max_height = 8192;
if (xfDisp->xfc->window)
XSetWMNormalHints(xfDisp->xfc->display, xfDisp->xfc->window->handle, size_hints);
XFree(size_hints);
return TRUE;
}
2018-10-02 13:33:52 +03:00
static BOOL xf_disp_check_context(void* context, xfContext** ppXfc, xfDispContext** ppXfDisp,
rdpSettings** ppSettings)
{
xfContext* xfc;
if (!context)
return FALSE;
xfc = (xfContext*)context;
if (!(xfc->xfDisp))
return FALSE;
if (!xfc->context.settings)
return FALSE;
*ppXfc = xfc;
*ppXfDisp = xfc->xfDisp;
*ppSettings = xfc->context.settings;
return TRUE;
}
2018-02-14 12:14:33 +03:00
static void xf_disp_OnActivated(void* context, ActivatedEventArgs* e)
{
2018-10-02 13:33:52 +03:00
xfContext* xfc;
xfDispContext* xfDisp;
rdpSettings* settings;
if (!xf_disp_check_context(context, &xfc, &xfDisp, &settings))
return;
xfDisp->waitingResize = FALSE;
if (xfDisp->activated && !settings->Fullscreen)
{
xf_disp_set_window_resizable(xfDisp);
if (e->firstActivation)
return;
xf_disp_sendResize(xfDisp);
}
}
2018-02-14 12:14:33 +03:00
static void xf_disp_OnGraphicsReset(void* context, GraphicsResetEventArgs* e)
{
2018-10-02 13:33:52 +03:00
xfContext* xfc;
xfDispContext* xfDisp;
rdpSettings* settings;
2019-02-07 19:53:21 +03:00
WINPR_UNUSED(e);
2018-10-02 13:33:52 +03:00
if (!xf_disp_check_context(context, &xfc, &xfDisp, &settings))
return;
xfDisp->waitingResize = FALSE;
if (xfDisp->activated && !settings->Fullscreen)
{
xf_disp_set_window_resizable(xfDisp);
xf_disp_sendResize(xfDisp);
}
}
2018-02-14 12:14:33 +03:00
static void xf_disp_OnTimer(void* context, TimerEventArgs* e)
{
2018-10-02 13:33:52 +03:00
xfContext* xfc;
xfDispContext* xfDisp;
rdpSettings* settings;
2019-02-07 19:53:21 +03:00
WINPR_UNUSED(e);
2018-10-02 13:33:52 +03:00
if (!xf_disp_check_context(context, &xfc, &xfDisp, &settings))
return;
if (!xfDisp->activated || settings->Fullscreen)
return;
xf_disp_sendResize(xfDisp);
}
xfDispContext* xf_disp_new(xfContext* xfc)
{
2018-10-02 13:33:52 +03:00
xfDispContext* ret;
if (!xfc || !xfc->context.settings || !xfc->context.pubSub)
return NULL;
ret = calloc(1, sizeof(xfDispContext));
if (!ret)
return NULL;
ret->xfc = xfc;
#ifdef USABLE_XRANDR
if (XRRQueryExtension(xfc->display, &ret->eventBase, &ret->errorBase))
{
ret->haveXRandr = TRUE;
}
#endif
ret->lastSentWidth = ret->targetWidth = xfc->context.settings->DesktopWidth;
ret->lastSentHeight = ret->targetHeight = xfc->context.settings->DesktopHeight;
2018-02-14 12:14:33 +03:00
PubSub_SubscribeActivated(xfc->context.pubSub, xf_disp_OnActivated);
PubSub_SubscribeGraphicsReset(xfc->context.pubSub, xf_disp_OnGraphicsReset);
PubSub_SubscribeTimer(xfc->context.pubSub, xf_disp_OnTimer);
return ret;
}
void xf_disp_free(xfDispContext* disp)
{
2018-10-02 13:33:52 +03:00
if (!disp)
return;
if (disp->xfc)
{
PubSub_UnsubscribeActivated(disp->xfc->context.pubSub, xf_disp_OnActivated);
PubSub_UnsubscribeGraphicsReset(disp->xfc->context.pubSub, xf_disp_OnGraphicsReset);
PubSub_UnsubscribeTimer(disp->xfc->context.pubSub, xf_disp_OnTimer);
}
free(disp);
}
2018-10-02 13:33:52 +03:00
UINT xf_disp_sendLayout(DispClientContext* disp, rdpMonitor* monitors, int nmonitors)
{
UINT ret = CHANNEL_RC_OK;
DISPLAY_CONTROL_MONITOR_LAYOUT* layouts;
int i;
xfDispContext* xfDisp = (xfDispContext*)disp->custom;
rdpSettings* settings = xfDisp->xfc->context.settings;
layouts = calloc(nmonitors, sizeof(DISPLAY_CONTROL_MONITOR_LAYOUT));
if (!layouts)
return CHANNEL_RC_NO_MEMORY;
for (i = 0; i < nmonitors; i++)
{
layouts[i].Flags = (monitors[i].is_primary ? DISPLAY_CONTROL_MONITOR_PRIMARY : 0);
layouts[i].Left = monitors[i].x;
layouts[i].Top = monitors[i].y;
layouts[i].Width = monitors[i].width;
layouts[i].Height = monitors[i].height;
layouts[i].Orientation = ORIENTATION_LANDSCAPE;
layouts[i].PhysicalWidth = monitors[i].attributes.physicalWidth;
layouts[i].PhysicalHeight = monitors[i].attributes.physicalHeight;
switch (monitors[i].attributes.orientation)
{
case 90:
layouts[i].Orientation = ORIENTATION_PORTRAIT;
break;
case 180:
layouts[i].Orientation = ORIENTATION_LANDSCAPE_FLIPPED;
break;
case 270:
layouts[i].Orientation = ORIENTATION_PORTRAIT_FLIPPED;
break;
case 0:
default:
/* MS-RDPEDISP - 2.2.2.2.1:
* Orientation (4 bytes): A 32-bit unsigned integer that specifies the
* orientation of the monitor in degrees. Valid values are 0, 90, 180
* or 270
*
* So we default to ORIENTATION_LANDSCAPE
*/
layouts[i].Orientation = ORIENTATION_LANDSCAPE;
break;
}
layouts[i].DesktopScaleFactor = settings->DesktopScaleFactor;
layouts[i].DeviceScaleFactor = settings->DeviceScaleFactor;
}
ret = IFCALLRESULT(CHANNEL_RC_OK, disp->SendMonitorLayout, disp, nmonitors, layouts);
free(layouts);
return ret;
}
BOOL xf_disp_handle_xevent(xfContext* xfc, XEvent* event)
{
xfDispContext* xfDisp;
rdpSettings* settings;
UINT32 maxWidth, maxHeight;
if (!xfc || !event)
return FALSE;
xfDisp = xfc->xfDisp;
if (!xfDisp)
return FALSE;
settings = xfc->context.settings;
if (!settings)
return FALSE;
2018-09-24 17:44:25 +03:00
if (!xfDisp->haveXRandr || !xfDisp->disp)
return TRUE;
#ifdef USABLE_XRANDR
if (event->type != xfDisp->eventBase + RRScreenChangeNotify)
return TRUE;
#endif
xf_detect_monitors(xfc, &maxWidth, &maxHeight);
2019-11-06 17:24:51 +03:00
return xf_disp_sendLayout(xfDisp->disp, settings->MonitorDefArray, settings->MonitorCount) ==
CHANNEL_RC_OK;
}
BOOL xf_disp_handle_configureNotify(xfContext* xfc, int width, int height)
{
xfDispContext* xfDisp;
2018-09-24 17:44:25 +03:00
if (!xfc)
return FALSE;
xfDisp = xfc->xfDisp;
2018-10-02 13:33:52 +03:00
if (!xfDisp)
return FALSE;
xfDisp->targetWidth = width;
xfDisp->targetHeight = height;
return xf_disp_sendResize(xfDisp);
}
2018-10-02 13:33:52 +03:00
static UINT xf_DisplayControlCaps(DispClientContext* disp, UINT32 maxNumMonitors,
UINT32 maxMonitorAreaFactorA, UINT32 maxMonitorAreaFactorB)
{
/* we're called only if dynamic resolution update is activated */
xfDispContext* xfDisp = (xfDispContext*)disp->custom;
rdpSettings* settings = xfDisp->xfc->context.settings;
WLog_DBG(TAG,
2019-11-06 17:24:51 +03:00
"DisplayControlCapsPdu: MaxNumMonitors: %" PRIu32 " MaxMonitorAreaFactorA: %" PRIu32
" MaxMonitorAreaFactorB: %" PRIu32 "",
maxNumMonitors, maxMonitorAreaFactorA, maxMonitorAreaFactorB);
xfDisp->activated = TRUE;
if (settings->Fullscreen)
return CHANNEL_RC_OK;
WLog_DBG(TAG, "DisplayControlCapsPdu: setting the window as resizable");
return xf_disp_set_window_resizable(xfDisp) ? CHANNEL_RC_OK : CHANNEL_RC_NO_MEMORY;
}
2018-09-24 17:44:25 +03:00
BOOL xf_disp_init(xfDispContext* xfDisp, DispClientContext* disp)
{
2018-09-24 17:44:25 +03:00
rdpSettings* settings;
if (!xfDisp || !xfDisp->xfc || !disp)
return FALSE;
settings = xfDisp->xfc->context.settings;
2018-10-02 13:33:52 +03:00
if (!settings)
return FALSE;
2018-09-24 17:44:25 +03:00
xfDisp->disp = disp;
2019-11-06 17:24:51 +03:00
disp->custom = (void*)xfDisp;
if (settings->DynamicResolutionUpdate)
{
disp->DisplayControlCaps = xf_DisplayControlCaps;
#ifdef USABLE_XRANDR
if (settings->Fullscreen)
{
/* ask X11 to notify us of screen changes */
2018-09-24 17:44:25 +03:00
XRRSelectInput(xfDisp->xfc->display, DefaultRootWindow(xfDisp->xfc->display),
RRScreenChangeNotifyMask);
}
#endif
}
return TRUE;
}
2018-09-24 17:44:25 +03:00
BOOL xf_disp_uninit(xfDispContext* xfDisp, DispClientContext* disp)
{
if (!xfDisp || !disp)
return FALSE;
xfDisp->disp = NULL;
2018-10-03 16:16:59 +03:00
return TRUE;
2018-09-24 17:44:25 +03:00
}