FreeRDP/client/X11/xf_client.c

1859 lines
46 KiB
C
Raw Normal View History

2013-04-02 21:22:59 +04:00
/**
* FreeRDP: A Remote Desktop Protocol Implementation
* X11 Client Interface
*
* Copyright 2013 Marc-Andre Moreau <marcandre.moreau@gmail.com>
2013-04-26 02:30:39 +04:00
* Copyright 2013 Corey Clayton <can.of.tuna@gmail.com>
* Copyright 2014 Thincast Technologies GmbH
* Copyright 2014 Norbert Federa <norbert.federa@thincast.com>
2013-04-02 21:22:59 +04:00
*
* 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <assert.h>
2013-04-02 21:22:59 +04:00
#include <X11/Xlib.h>
#include <X11/Xutil.h>
2013-04-26 02:30:39 +04:00
#ifdef WITH_XRENDER
#include <X11/extensions/Xrender.h>
#include <math.h>
#endif
2013-04-26 02:30:39 +04:00
#ifdef WITH_XI
2013-04-24 05:28:05 +04:00
#include <X11/extensions/XInput2.h>
2013-04-26 02:30:39 +04:00
#endif
2013-04-02 21:22:59 +04:00
#ifdef WITH_XCURSOR
#include <X11/Xcursor/Xcursor.h>
#endif
#ifdef WITH_XINERAMA
#include <X11/extensions/Xinerama.h>
#endif
#ifdef WITH_XI
#include <X11/extensions/XInput2.h>
#endif
#include <X11/XKBlib.h>
2013-04-02 21:22:59 +04:00
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <unistd.h>
#include <string.h>
#include <termios.h>
#include <pthread.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/select.h>
#include <freerdp/freerdp.h>
2013-04-02 21:22:59 +04:00
#include <freerdp/constants.h>
#include <freerdp/codec/nsc.h>
#include <freerdp/codec/rfx.h>
#include <freerdp/codec/color.h>
#include <freerdp/codec/bitmap.h>
#include <freerdp/utils/signal.h>
#include <freerdp/utils/passphrase.h>
#include <freerdp/client/cliprdr.h>
#include <freerdp/client/channels.h>
#include <freerdp/client/file.h>
#include <freerdp/client/cmdline.h>
#include <winpr/crt.h>
#include <winpr/synch.h>
#include <winpr/file.h>
2013-05-02 22:30:43 +04:00
#include <winpr/print.h>
#include <X11/XKBlib.h>
2013-04-02 21:22:59 +04:00
#include "xf_gdi.h"
#include "xf_rail.h"
#include "xf_tsmf.h"
#include "xf_event.h"
#include "xf_input.h"
2013-04-02 21:22:59 +04:00
#include "xf_cliprdr.h"
#include "xf_monitor.h"
#include "xf_graphics.h"
#include "xf_keyboard.h"
2013-04-26 02:57:41 +04:00
#include "xf_input.h"
#include "xf_channels.h"
#include "xfreerdp.h"
2014-09-12 19:13:01 +04:00
#include <freerdp/log.h>
#define TAG CLIENT_TAG("x11")
2013-04-02 21:22:59 +04:00
static const size_t password_size = 512;
#ifdef WITH_XRENDER
static void xf_draw_screen_scaled(xfContext* xfc, int x, int y, int w, int h)
{
XTransform transform;
Picture windowPicture;
Picture primaryPicture;
XRenderPictureAttributes pa;
2014-05-23 15:08:22 +04:00
XRenderPictFormat *picFormat;
double xScalingFactor;
double yScalingFactor;
int x2;
int y2;
if (xfc->scaledWidth <= 0 || xfc->scaledHeight <= 0)
{
WLog_ERR(TAG, "the current window dimensions are invalid");
return;
}
if (xfc->width <= 0 || xfc->height <= 0)
{
WLog_ERR(TAG, "the window dimensions are invalid");
return;
}
xScalingFactor = xfc->width / (double)xfc->scaledWidth;
yScalingFactor = xfc->height / (double)xfc->scaledHeight;
XSetFillStyle(xfc->display, xfc->gc, FillSolid);
XSetForeground(xfc->display, xfc->gc, 0);
/* Black out possible space between desktop and window borders */
{
XRectangle box1 = { 0, 0, xfc->window->width, xfc->window->height };
XRectangle box2 = { xfc->offset_x, xfc->offset_y, xfc->scaledWidth, xfc->scaledHeight };
Region reg1 = XCreateRegion();
Region reg2 = XCreateRegion();
XUnionRectWithRegion( &box1, reg1, reg1);
XUnionRectWithRegion( &box2, reg2, reg2);
if (XSubtractRegion(reg1, reg2, reg1) && !XEmptyRegion(reg1))
{
XSetRegion( xfc->display, xfc->gc, reg1);
XFillRectangle(xfc->display, xfc->window->handle, xfc->gc, 0, 0, xfc->window->width, xfc->window->height);
XSetClipMask(xfc->display, xfc->gc, None);
}
XDestroyRegion(reg1);
XDestroyRegion(reg2);
}
picFormat = XRenderFindVisualFormat(xfc->display, xfc->visual);
pa.subwindow_mode = IncludeInferiors;
primaryPicture = XRenderCreatePicture(xfc->display, xfc->primary, picFormat, CPSubwindowMode, &pa);
windowPicture = XRenderCreatePicture(xfc->display, xfc->window->handle, picFormat, CPSubwindowMode, &pa);
XRenderSetPictureFilter(xfc->display, primaryPicture, FilterBilinear, 0, 0);
transform.matrix[0][0] = XDoubleToFixed(xScalingFactor);
transform.matrix[0][1] = XDoubleToFixed(0.0);
transform.matrix[0][2] = XDoubleToFixed(0.0);
transform.matrix[1][0] = XDoubleToFixed(0.0);
transform.matrix[1][1] = XDoubleToFixed(yScalingFactor);
transform.matrix[1][2] = XDoubleToFixed(0.0);
transform.matrix[2][0] = XDoubleToFixed(0.0);
transform.matrix[2][1] = XDoubleToFixed(0.0);
transform.matrix[2][2] = XDoubleToFixed(1.0);
/* calculate and fix up scaled coordinates */
x2 = x + w;
y2 = y + h;
x = floor(x / xScalingFactor) - 1;
y = floor(y / yScalingFactor) - 1;
w = ceil(x2 / xScalingFactor) + 1 - x;
h = ceil(y2 / yScalingFactor) + 1 - y;
XRenderSetPictureTransform(xfc->display, primaryPicture, &transform);
XRenderComposite(xfc->display, PictOpSrc, primaryPicture, 0, windowPicture, x, y, 0, 0, xfc->offset_x + x, xfc->offset_y + y, w, h);
XRenderFreePicture(xfc->display, primaryPicture);
XRenderFreePicture(xfc->display, windowPicture);
}
BOOL xf_picture_transform_required(xfContext* xfc)
{
if (xfc->offset_x || xfc->offset_y ||
xfc->scaledWidth != xfc->width ||
xfc->scaledHeight != xfc->height)
{
return TRUE;
}
return FALSE;
}
#endif /* WITH_XRENDER defined */
void xf_draw_screen(xfContext* xfc, int x, int y, int w, int h)
{
if (w == 0 || h == 0)
{
WLog_WARN(TAG, "invalid width and/or height specified: w=%d h=%d", w, h);
return;
}
#ifdef WITH_XRENDER
if (xf_picture_transform_required(xfc)) {
xf_draw_screen_scaled(xfc, x, y, w, h);
return;
}
#endif
XCopyArea(xfc->display, xfc->primary, xfc->window->handle, xfc->gc, x, y, w, h, x, y);
}
static void xf_desktop_resize(rdpContext* context)
{
rdpSettings* settings;
xfContext* xfc = (xfContext*) context;
settings = xfc->settings;
if (xfc->primary)
{
BOOL same = (xfc->primary == xfc->drawing) ? TRUE : FALSE;
XFreePixmap(xfc->display, xfc->primary);
xfc->primary = XCreatePixmap(xfc->display, xfc->drawable, xfc->width, xfc->height, xfc->depth);
if (same)
xfc->drawing = xfc->primary;
}
#ifdef WITH_XRENDER
2014-12-03 20:23:20 +03:00
if (!xfc->settings->SmartSizing)
{
xfc->scaledWidth = xfc->width;
xfc->scaledHeight = xfc->height;
}
#endif
if (!xfc->fullscreen)
{
if (xfc->window)
xf_ResizeDesktopWindow(xfc, xfc->window, settings->DesktopWidth, settings->DesktopHeight);
}
else
{
XSetFunction(xfc->display, xfc->gc, GXcopy);
XSetFillStyle(xfc->display, xfc->gc, FillSolid);
XSetForeground(xfc->display, xfc->gc, 0);
XFillRectangle(xfc->display, xfc->drawable, xfc->gc, 0, 0, xfc->window->width, xfc->window->height);
}
}
void xf_sw_begin_paint(rdpContext* context)
2013-04-02 21:22:59 +04:00
{
rdpGdi* gdi = context->gdi;
2013-04-02 21:22:59 +04:00
gdi->primary->hdc->hwnd->invalid->null = 1;
gdi->primary->hdc->hwnd->ninvalid = 0;
}
void xf_sw_end_paint(rdpContext* context)
2013-04-02 21:22:59 +04:00
{
int i;
2013-04-02 21:22:59 +04:00
INT32 x, y;
UINT32 w, h;
int ninvalid;
HGDI_RGN cinvalid;
xfContext* xfc = (xfContext*) context;
rdpGdi* gdi = context->gdi;
x = gdi->primary->hdc->hwnd->invalid->x;
y = gdi->primary->hdc->hwnd->invalid->y;
w = gdi->primary->hdc->hwnd->invalid->w;
h = gdi->primary->hdc->hwnd->invalid->h;
ninvalid = gdi->primary->hdc->hwnd->ninvalid;
cinvalid = gdi->primary->hdc->hwnd->cinvalid;
if (!xfc->remote_app)
2013-04-02 21:22:59 +04:00
{
if (!xfc->complex_regions)
2013-04-02 21:22:59 +04:00
{
if (gdi->primary->hdc->hwnd->invalid->null)
2013-04-02 21:22:59 +04:00
return;
xf_lock_x11(xfc, FALSE);
XPutImage(xfc->display, xfc->primary, xfc->gc, xfc->image, x, y, x, y, w, h);
xf_draw_screen(xfc, x, y, w, h);
xf_unlock_x11(xfc, FALSE);
2013-04-02 21:22:59 +04:00
}
else
{
if (gdi->primary->hdc->hwnd->ninvalid < 1)
2013-04-02 21:22:59 +04:00
return;
xf_lock_x11(xfc, FALSE);
for (i = 0; i < ninvalid; i++)
2013-04-02 21:22:59 +04:00
{
x = cinvalid[i].x;
y = cinvalid[i].y;
w = cinvalid[i].w;
h = cinvalid[i].h;
XPutImage(xfc->display, xfc->primary, xfc->gc, xfc->image, x, y, x, y, w, h);
xf_draw_screen(xfc, x, y, w, h);
2013-04-02 21:22:59 +04:00
}
XFlush(xfc->display);
xf_unlock_x11(xfc, FALSE);
2013-04-02 21:22:59 +04:00
}
}
else
{
if (gdi->primary->hdc->hwnd->invalid->null)
2013-04-02 21:22:59 +04:00
return;
xf_lock_x11(xfc, FALSE);
xf_rail_paint(xfc, x, y, x + w - 1, y + h - 1);
xf_unlock_x11(xfc, FALSE);
2013-04-02 21:22:59 +04:00
}
}
void xf_sw_desktop_resize(rdpContext* context)
2013-04-02 21:22:59 +04:00
{
rdpGdi* gdi = context->gdi;
xfContext* xfc = (xfContext*) context;
xf_lock_x11(xfc, TRUE);
xfc->width = context->settings->DesktopWidth;
xfc->height = context->settings->DesktopHeight;
gdi_resize(gdi, xfc->width, xfc->height);
if (xfc->image)
{
xfc->image->data = NULL;
XDestroyImage(xfc->image);
xfc->image = XCreateImage(xfc->display, xfc->visual, xfc->depth, ZPixmap, 0,
(char*) gdi->primary_buffer, gdi->width, gdi->height, xfc->scanline_pad, 0);
2013-04-02 21:22:59 +04:00
}
xf_desktop_resize(context);
xf_unlock_x11(xfc, TRUE);
2013-04-02 21:22:59 +04:00
}
void xf_hw_begin_paint(rdpContext* context)
2013-04-02 21:22:59 +04:00
{
xfContext* xfc = (xfContext*) context;
xfc->hdc->hwnd->invalid->null = 1;
xfc->hdc->hwnd->ninvalid = 0;
2013-04-02 21:22:59 +04:00
}
void xf_hw_end_paint(rdpContext* context)
2013-04-02 21:22:59 +04:00
{
INT32 x, y;
UINT32 w, h;
xfContext* xfc = (xfContext*) context;
if (!xfc->remote_app)
2013-04-02 21:22:59 +04:00
{
if (!xfc->complex_regions)
2013-04-02 21:22:59 +04:00
{
if (xfc->hdc->hwnd->invalid->null)
2013-04-02 21:22:59 +04:00
return;
x = xfc->hdc->hwnd->invalid->x;
y = xfc->hdc->hwnd->invalid->y;
w = xfc->hdc->hwnd->invalid->w;
h = xfc->hdc->hwnd->invalid->h;
xf_lock_x11(xfc, FALSE);
xf_draw_screen(xfc, x, y, w, h);
xf_unlock_x11(xfc, FALSE);
2013-04-02 21:22:59 +04:00
}
else
{
int i;
int ninvalid;
HGDI_RGN cinvalid;
if (xfc->hdc->hwnd->ninvalid < 1)
2013-04-02 21:22:59 +04:00
return;
ninvalid = xfc->hdc->hwnd->ninvalid;
cinvalid = xfc->hdc->hwnd->cinvalid;
xf_lock_x11(xfc, FALSE);
for (i = 0; i < ninvalid; i++)
2013-04-02 21:22:59 +04:00
{
x = cinvalid[i].x;
y = cinvalid[i].y;
w = cinvalid[i].w;
h = cinvalid[i].h;
xf_draw_screen(xfc, x, y, w, h);
2013-04-02 21:22:59 +04:00
}
XFlush(xfc->display);
xf_unlock_x11(xfc, FALSE);
2013-04-02 21:22:59 +04:00
}
}
else
{
if (xfc->hdc->hwnd->invalid->null)
2013-04-02 21:22:59 +04:00
return;
x = xfc->hdc->hwnd->invalid->x;
y = xfc->hdc->hwnd->invalid->y;
w = xfc->hdc->hwnd->invalid->w;
h = xfc->hdc->hwnd->invalid->h;
xf_lock_x11(xfc, FALSE);
xf_rail_paint(xfc, x, y, x + w - 1, y + h - 1);
xf_unlock_x11(xfc, FALSE);
2013-04-02 21:22:59 +04:00
}
}
void xf_hw_desktop_resize(rdpContext* context)
2013-04-02 21:22:59 +04:00
{
xfContext* xfc = (xfContext*) context;
rdpSettings* settings = xfc->settings;
xf_lock_x11(xfc, TRUE);
xfc->width = settings->DesktopWidth;
xfc->height = settings->DesktopHeight;
xf_desktop_resize(context);
xf_unlock_x11(xfc, TRUE);
2013-04-02 21:22:59 +04:00
}
2014-05-23 15:08:22 +04:00
BOOL xf_get_fds(freerdp *instance, void **rfds, int *rcount, void **wfds, int *wcount)
2013-04-02 21:22:59 +04:00
{
xfContext* xfc = (xfContext*) instance->context;
2014-05-23 15:08:22 +04:00
rfds[*rcount] = (void *)(long)(xfc->xfds);
2013-04-02 21:22:59 +04:00
(*rcount)++;
return TRUE;
}
BOOL xf_process_x_events(freerdp* instance)
2013-04-02 21:22:59 +04:00
{
BOOL status;
XEvent xevent;
int pending_status;
xfContext* xfc = (xfContext*) instance->context;
2013-04-02 21:22:59 +04:00
status = TRUE;
pending_status = TRUE;
while (pending_status)
2013-04-02 21:22:59 +04:00
{
xf_lock_x11(xfc, FALSE);
pending_status = XPending(xfc->display);
xf_unlock_x11(xfc, FALSE);
if (pending_status)
2013-04-02 21:22:59 +04:00
{
ZeroMemory(&xevent, sizeof(xevent));
XNextEvent(xfc->display, &xevent);
2013-04-02 21:22:59 +04:00
status = xf_event_process(instance, &xevent);
if (!status)
2013-04-02 21:22:59 +04:00
return status;
}
}
return status;
}
void xf_create_window(xfContext* xfc)
2013-04-02 21:22:59 +04:00
{
XEvent xevent;
int width, height;
char* windowTitle;
2013-04-02 21:22:59 +04:00
ZeroMemory(&xevent, sizeof(xevent));
width = xfc->width;
height = xfc->height;
if (!xfc->remote_app)
2013-04-02 21:22:59 +04:00
{
xfc->attribs.background_pixel = BlackPixelOfScreen(xfc->screen);
xfc->attribs.border_pixel = WhitePixelOfScreen(xfc->screen);
xfc->attribs.backing_store = xfc->primary ? NotUseful : Always;
xfc->attribs.override_redirect = xfc->grab_keyboard ? xfc->fullscreen : False;
xfc->attribs.colormap = xfc->colormap;
xfc->attribs.bit_gravity = NorthWestGravity;
xfc->attribs.win_gravity = NorthWestGravity;
#ifdef WITH_XRENDER
xfc->offset_x = 0;
xfc->offset_y = 0;
#endif
if (xfc->settings->WindowTitle)
2013-04-02 21:22:59 +04:00
{
windowTitle = _strdup(xfc->settings->WindowTitle);
}
else if (xfc->settings->ServerPort == 3389)
{
windowTitle = malloc(1 + sizeof("FreeRDP: ") + strlen(xfc->settings->ServerHostname));
sprintf(windowTitle, "FreeRDP: %s", xfc->settings->ServerHostname);
2013-04-02 21:22:59 +04:00
}
else
{
windowTitle = malloc(1 + sizeof("FreeRDP: ") + strlen(xfc->settings->ServerHostname) + sizeof(":00000"));
sprintf(windowTitle, "FreeRDP: %s:%i", xfc->settings->ServerHostname, xfc->settings->ServerPort);
}
if (xfc->fullscreen)
{
width = WidthOfScreen(xfc->screen);
height = HeightOfScreen(xfc->screen);
}
#ifdef WITH_XRENDER
if (xfc->settings->SmartSizing)
{
if (xfc->fullscreen)
{
if (xfc->window)
{
xfc->settings->SmartSizingWidth = xfc->window->width;
xfc->settings->SmartSizingHeight = xfc->window->height;
}
}
else
{
if (xfc->settings->SmartSizingWidth)
width = xfc->settings->SmartSizingWidth;
if (xfc->settings->SmartSizingHeight)
height = xfc->settings->SmartSizingHeight;
}
xfc->scaledWidth = width;
xfc->scaledHeight = height;
}
#endif
xfc->window = xf_CreateDesktopWindow(xfc, windowTitle, width, height, xfc->settings->Decorations);
free(windowTitle);
if (xfc->fullscreen)
xf_SetWindowFullscreen(xfc, xfc->window, xfc->fullscreen);
xfc->unobscured = (xevent.xvisibility.state == VisibilityUnobscured);
XSetWMProtocols(xfc->display, xfc->window->handle, &(xfc->WM_DELETE_WINDOW), 1);
xfc->drawable = xfc->window->handle;
2013-04-02 21:22:59 +04:00
}
else
{
xfc->drawable = DefaultRootWindow(xfc->display);
2013-04-02 21:22:59 +04:00
}
}
void xf_toggle_fullscreen(xfContext* xfc)
2013-04-02 21:22:59 +04:00
{
WindowStateChangeEventArgs e;
xf_lock_x11(xfc, TRUE);
XDestroyWindow(xfc->display, xfc->window->handle);
xfc->fullscreen = (xfc->fullscreen) ? FALSE : TRUE;
xf_create_window(xfc);
xf_unlock_x11(xfc, TRUE);
EventArgsInit(&e, "xfreerdp");
e.state = xfc->fullscreen ? FREERDP_WINDOW_STATE_FULLSCREEN : 0;
2014-05-23 15:08:22 +04:00
PubSub_OnWindowStateChange(((rdpContext *) xfc)->pubSub, xfc, &e);
2013-04-02 21:22:59 +04:00
}
void xf_toggle_control(xfContext* xfc)
{
EncomspClientContext* encomsp;
ENCOMSP_CHANGE_PARTICIPANT_CONTROL_LEVEL_PDU pdu;
encomsp = xfc->encomsp;
if (!encomsp)
return;
pdu.ParticipantId = 0;
pdu.Flags = ENCOMSP_REQUEST_VIEW;
if (!xfc->controlToggle)
pdu.Flags |= ENCOMSP_REQUEST_INTERACT;
encomsp->ChangeParticipantControlLevel(encomsp, &pdu);
xfc->controlToggle = !xfc->controlToggle;
}
int xf_encomsp_participant_created(EncomspClientContext* context, ENCOMSP_PARTICIPANT_CREATED_PDU* participantCreated)
{
return 1;
}
void xf_encomsp_init(xfContext* xfc, EncomspClientContext* encomsp)
{
xfc->encomsp = encomsp;
encomsp->custom = (void*) xfc;
encomsp->ParticipantCreated = xf_encomsp_participant_created;
}
void xf_encomsp_uninit(xfContext* xfc, EncomspClientContext* encomsp)
{
xfc->encomsp = NULL;
}
void xf_lock_x11(xfContext* xfc, BOOL display)
2013-04-02 21:22:59 +04:00
{
if (!xfc->UseXThreads)
2013-04-02 21:22:59 +04:00
{
WaitForSingleObject(xfc->mutex, INFINITE);
2013-04-02 21:22:59 +04:00
}
else
{
if (display)
XLockDisplay(xfc->display);
2013-04-02 21:22:59 +04:00
}
}
void xf_unlock_x11(xfContext* xfc, BOOL display)
2013-04-02 21:22:59 +04:00
{
if (!xfc->UseXThreads)
2013-04-02 21:22:59 +04:00
{
ReleaseMutex(xfc->mutex);
2013-04-02 21:22:59 +04:00
}
else
{
if (display)
XUnlockDisplay(xfc->display);
2013-04-02 21:22:59 +04:00
}
}
static void xf_calculate_color_shifts(UINT32 mask, UINT8* rsh, UINT8* lsh)
{
for (*lsh = 0; !(mask & 1); mask >>= 1)
(*lsh)++;
for (*rsh = 8; mask; mask >>= 1)
(*rsh)--;
}
BOOL xf_get_pixmap_info(xfContext* xfc)
2013-04-02 21:22:59 +04:00
{
int i;
int vi_count;
int pf_count;
2014-05-23 15:08:22 +04:00
XVisualInfo *vi;
XVisualInfo *vis;
2013-04-02 21:22:59 +04:00
XVisualInfo template;
2014-05-23 15:08:22 +04:00
XPixmapFormatValues *pf;
XPixmapFormatValues *pfs;
2013-04-02 21:22:59 +04:00
XWindowAttributes window_attributes;
pfs = XListPixmapFormats(xfc->display, &pf_count);
2014-10-22 06:19:11 +04:00
if (!pfs)
2013-04-02 21:22:59 +04:00
{
2014-09-12 19:13:01 +04:00
WLog_ERR(TAG, "XListPixmapFormats failed");
2013-04-02 21:22:59 +04:00
return 1;
}
2014-10-22 06:19:11 +04:00
for (i = 0; i < pf_count; i++)
2013-04-02 21:22:59 +04:00
{
pf = pfs + i;
2014-10-22 06:19:11 +04:00
if (pf->depth == xfc->depth)
2013-04-02 21:22:59 +04:00
{
xfc->bpp = pf->bits_per_pixel;
xfc->scanline_pad = pf->scanline_pad;
2013-04-02 21:22:59 +04:00
break;
}
}
2014-10-22 06:19:11 +04:00
2013-04-02 21:22:59 +04:00
XFree(pfs);
2014-10-22 06:19:11 +04:00
2013-04-02 21:22:59 +04:00
ZeroMemory(&template, sizeof(template));
template.class = TrueColor;
template.screen = xfc->screen_number;
2014-10-22 06:19:11 +04:00
if (XGetWindowAttributes(xfc->display, RootWindowOfScreen(xfc->screen), &window_attributes) == 0)
2013-04-02 21:22:59 +04:00
{
2014-09-12 19:13:01 +04:00
WLog_ERR(TAG, "XGetWindowAttributes failed");
2013-04-02 21:22:59 +04:00
return FALSE;
}
2014-10-22 06:19:11 +04:00
vis = XGetVisualInfo(xfc->display, VisualClassMask | VisualScreenMask, &template, &vi_count);
2014-10-22 06:19:11 +04:00
if (!vis)
2013-04-02 21:22:59 +04:00
{
2014-09-12 19:13:01 +04:00
WLog_ERR(TAG, "XGetVisualInfo failed");
2013-04-02 21:22:59 +04:00
return FALSE;
}
2014-10-22 06:19:11 +04:00
2013-04-02 21:22:59 +04:00
vi = NULL;
2014-10-22 06:19:11 +04:00
for (i = 0; i < vi_count; i++)
2013-04-02 21:22:59 +04:00
{
vi = vis + i;
if (vi->visual == window_attributes.visual)
2013-04-02 21:22:59 +04:00
{
xfc->visual = vi->visual;
2013-04-02 21:22:59 +04:00
break;
}
}
2014-10-22 06:19:11 +04:00
if (xfc->visual)
2013-04-02 21:22:59 +04:00
{
/*
* Detect if the server visual has an inverted colormap
* (BGR vs RGB, or red being the least significant byte)
*/
2014-10-22 06:19:11 +04:00
if (vi->red_mask & 0xFF)
2013-04-02 21:22:59 +04:00
{
2014-10-22 06:19:11 +04:00
xfc->invert = TRUE;
2013-04-02 21:22:59 +04:00
}
/* calculate color shifts required for rdp order color conversion */
xf_calculate_color_shifts(vi->red_mask, &xfc->red_shift_r, &xfc->red_shift_l);
xf_calculate_color_shifts(vi->green_mask, &xfc->green_shift_r, &xfc->green_shift_l);
xf_calculate_color_shifts(vi->blue_mask, &xfc->blue_shift_r, &xfc->blue_shift_l);
2013-04-02 21:22:59 +04:00
}
2014-10-22 06:19:11 +04:00
2013-04-02 21:22:59 +04:00
XFree(vis);
2014-10-22 06:19:11 +04:00
if ((xfc->visual == NULL) || (xfc->scanline_pad == 0))
2013-04-02 21:22:59 +04:00
{
return FALSE;
}
2014-10-22 06:19:11 +04:00
2013-04-02 21:22:59 +04:00
return TRUE;
}
2014-05-23 15:08:22 +04:00
static int (*_def_error_handler)(Display *, XErrorEvent *);
2013-04-02 21:22:59 +04:00
int xf_error_handler(Display* d, XErrorEvent* ev)
2013-04-02 21:22:59 +04:00
{
char buf[256];
2013-04-02 21:22:59 +04:00
int do_abort = TRUE;
XGetErrorText(d, ev->error_code, buf, sizeof(buf));
WLog_ERR(TAG, "%s", buf);
if (do_abort)
2013-04-02 21:22:59 +04:00
abort();
2013-04-02 21:22:59 +04:00
_def_error_handler(d, ev);
2013-04-02 21:22:59 +04:00
return FALSE;
}
int _xf_error_handler(Display* d, XErrorEvent* ev)
2013-04-02 21:22:59 +04:00
{
/*
2014-05-23 15:08:22 +04:00
* ungrab the keyboard, in case a debugger is running in
* another window. This make xf_error_handler() a potential
* debugger breakpoint.
*/
2013-04-02 21:22:59 +04:00
XUngrabKeyboard(d, CurrentTime);
return xf_error_handler(d, ev);
}
static void xf_post_disconnect(freerdp* instance)
{
xfContext* xfc;
if (!instance || !instance->context || !instance->settings)
return;
xfc = (xfContext*) instance->context;
if (xfc->mutex)
{
WaitForSingleObject(xfc->mutex, INFINITE);
CloseHandle(xfc->mutex);
xfc->mutex = NULL;
}
xf_monitors_free(xfc, instance->settings);
}
static void xf_play_sound(rdpContext* context, PLAY_SOUND_UPDATE* play_sound)
{
xfContext* xfc = (xfContext*) context;
XkbBell(xfc->display, None, 100, 0);
}
void xf_check_extensions(xfContext* context)
{
int xkb_opcode, xkb_event, xkb_error;
int xkb_major = XkbMajorVersion;
int xkb_minor = XkbMinorVersion;
if (XkbLibraryVersion( &xkb_major, &xkb_minor ) && XkbQueryExtension(context->display, &xkb_opcode, &xkb_event,
&xkb_error, &xkb_major, &xkb_minor))
{
context->xkbAvailable = TRUE;
}
#ifdef WITH_XRENDER
{
int xrender_event_base;
int xrender_error_base;
if (XRenderQueryExtension (context->display, &xrender_event_base,
&xrender_error_base))
{
context->xrenderAvailable = TRUE;
}
}
#endif
}
2013-04-02 21:22:59 +04:00
/**
* Callback given to freerdp_connect() to process the pre-connect operations.
* It will fill the rdp_freerdp structure (instance) with the appropriate options to use for the connection.
*
* @param instance - pointer to the rdp_freerdp structure that contains the connection's parameters, and will
* be filled with the appropriate informations.
*
* @return TRUE if successful. FALSE otherwise.
* Can exit with error code XF_EXIT_PARSE_ARGUMENTS if there is an error in the parameters.
*/
BOOL xf_pre_connect(freerdp* instance)
2013-04-02 21:22:59 +04:00
{
rdpChannels* channels;
rdpSettings* settings;
xfContext* xfc = (xfContext*) instance->context;
xfc->codecs = instance->context->codecs;
xfc->settings = instance->settings;
xfc->instance = instance;
2013-04-02 21:22:59 +04:00
settings = instance->settings;
channels = instance->context->channels;
settings->OsMajorType = OSMAJORTYPE_UNIX;
settings->OsMinorType = OSMINORTYPE_NATIVE_XSERVER;
ZeroMemory(settings->OrderSupport, 32);
settings->OrderSupport[NEG_DSTBLT_INDEX] = TRUE;
settings->OrderSupport[NEG_PATBLT_INDEX] = TRUE;
settings->OrderSupport[NEG_SCRBLT_INDEX] = TRUE;
settings->OrderSupport[NEG_OPAQUE_RECT_INDEX] = TRUE;
settings->OrderSupport[NEG_DRAWNINEGRID_INDEX] = FALSE;
settings->OrderSupport[NEG_MULTIDSTBLT_INDEX] = FALSE;
settings->OrderSupport[NEG_MULTIPATBLT_INDEX] = FALSE;
settings->OrderSupport[NEG_MULTISCRBLT_INDEX] = FALSE;
settings->OrderSupport[NEG_MULTIOPAQUERECT_INDEX] = TRUE;
settings->OrderSupport[NEG_MULTI_DRAWNINEGRID_INDEX] = FALSE;
settings->OrderSupport[NEG_LINETO_INDEX] = TRUE;
settings->OrderSupport[NEG_POLYLINE_INDEX] = TRUE;
settings->OrderSupport[NEG_MEMBLT_INDEX] = settings->BitmapCacheEnabled;
settings->OrderSupport[NEG_MEM3BLT_INDEX] = (settings->SoftwareGdi) ? TRUE : FALSE;
settings->OrderSupport[NEG_MEMBLT_V2_INDEX] = settings->BitmapCacheEnabled;
settings->OrderSupport[NEG_MEM3BLT_V2_INDEX] = FALSE;
settings->OrderSupport[NEG_SAVEBITMAP_INDEX] = FALSE;
settings->OrderSupport[NEG_GLYPH_INDEX_INDEX] = TRUE;
settings->OrderSupport[NEG_FAST_INDEX_INDEX] = TRUE;
settings->OrderSupport[NEG_FAST_GLYPH_INDEX] = TRUE;
settings->OrderSupport[NEG_POLYGON_SC_INDEX] = (settings->SoftwareGdi) ? FALSE : TRUE;
settings->OrderSupport[NEG_POLYGON_CB_INDEX] = (settings->SoftwareGdi) ? FALSE : TRUE;
settings->OrderSupport[NEG_ELLIPSE_SC_INDEX] = FALSE;
settings->OrderSupport[NEG_ELLIPSE_CB_INDEX] = FALSE;
xfc->UseXThreads = TRUE;
if (xfc->UseXThreads)
{
if (!XInitThreads())
{
2014-09-12 19:13:01 +04:00
WLog_WARN(TAG, "XInitThreads() failure");
xfc->UseXThreads = FALSE;
}
}
xfc->display = XOpenDisplay(NULL);
if (!xfc->display)
{
2014-09-12 19:13:01 +04:00
WLog_ERR(TAG, "failed to open display: %s", XDisplayName(NULL));
WLog_ERR(TAG, "Please check that the $DISPLAY environment variable is properly set.");
return FALSE;
}
if (xfc->debug)
{
2014-09-12 19:13:01 +04:00
WLog_INFO(TAG, "Enabling X11 debug mode.");
XSynchronize(xfc->display, TRUE);
_def_error_handler = XSetErrorHandler(_xf_error_handler);
}
xf_check_extensions(xfc);
xfc->mutex = CreateMutex(NULL, FALSE, NULL);
PubSub_SubscribeChannelConnected(instance->context->pubSub,
(pChannelConnectedEventHandler) xf_OnChannelConnectedEventHandler);
PubSub_SubscribeChannelDisconnected(instance->context->pubSub,
(pChannelDisconnectedEventHandler) xf_OnChannelDisconnectedEventHandler);
freerdp_client_load_addins(channels, instance->settings);
freerdp_channels_pre_connect(channels, instance);
if (!settings->Username)
{
char *login_name = getlogin();
if (login_name)
{
settings->Username = _strdup(login_name);
WLog_INFO(TAG, "No user name set. - Using login name: %s", settings->Username);
}
}
if (settings->AuthenticationOnly)
2013-04-02 21:22:59 +04:00
{
/* Check +auth-only has a username and password. */
if (!settings->Password)
2013-04-02 21:22:59 +04:00
{
WLog_INFO(TAG, "auth-only, but no password set. Please provide one.");
2013-09-05 16:27:34 +04:00
return FALSE;
2013-04-02 21:22:59 +04:00
}
WLog_INFO(TAG, "Authentication only. Don't connect to X.");
2013-04-02 21:22:59 +04:00
}
xfc->_NET_WM_ICON = XInternAtom(xfc->display, "_NET_WM_ICON", False);
xfc->_MOTIF_WM_HINTS = XInternAtom(xfc->display, "_MOTIF_WM_HINTS", False);
xfc->_NET_CURRENT_DESKTOP = XInternAtom(xfc->display, "_NET_CURRENT_DESKTOP", False);
xfc->_NET_WORKAREA = XInternAtom(xfc->display, "_NET_WORKAREA", False);
xfc->_NET_WM_STATE = XInternAtom(xfc->display, "_NET_WM_STATE", False);
xfc->_NET_WM_STATE_FULLSCREEN = XInternAtom(xfc->display, "_NET_WM_STATE_FULLSCREEN", False);
xfc->_NET_WM_WINDOW_TYPE = XInternAtom(xfc->display, "_NET_WM_WINDOW_TYPE", False);
xfc->_NET_WM_WINDOW_TYPE_NORMAL = XInternAtom(xfc->display, "_NET_WM_WINDOW_TYPE_NORMAL", False);
xfc->_NET_WM_WINDOW_TYPE_DIALOG = XInternAtom(xfc->display, "_NET_WM_WINDOW_TYPE_DIALOG", False);
xfc->_NET_WM_WINDOW_TYPE_POPUP = XInternAtom(xfc->display, "_NET_WM_WINDOW_TYPE_POPUP", False);
xfc->_NET_WM_WINDOW_TYPE_UTILITY = XInternAtom(xfc->display, "_NET_WM_WINDOW_TYPE_UTILITY", False);
xfc->_NET_WM_WINDOW_TYPE_DROPDOWN_MENU = XInternAtom(xfc->display, "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", False);
xfc->_NET_WM_STATE_SKIP_TASKBAR = XInternAtom(xfc->display, "_NET_WM_STATE_SKIP_TASKBAR", False);
xfc->_NET_WM_STATE_SKIP_PAGER = XInternAtom(xfc->display, "_NET_WM_STATE_SKIP_PAGER", False);
xfc->_NET_WM_MOVERESIZE = XInternAtom(xfc->display, "_NET_WM_MOVERESIZE", False);
xfc->_NET_MOVERESIZE_WINDOW = XInternAtom(xfc->display, "_NET_MOVERESIZE_WINDOW", False);
xfc->WM_PROTOCOLS = XInternAtom(xfc->display, "WM_PROTOCOLS", False);
xfc->WM_DELETE_WINDOW = XInternAtom(xfc->display, "WM_DELETE_WINDOW", False);
xfc->WM_STATE = XInternAtom(xfc->display, "WM_STATE", False);
2014-03-14 05:10:22 +04:00
xf_keyboard_init(xfc);
2013-04-02 21:22:59 +04:00
instance->context->cache = cache_new(instance->settings);
xfc->xfds = ConnectionNumber(xfc->display);
xfc->screen_number = DefaultScreen(xfc->display);
xfc->screen = ScreenOfDisplay(xfc->display, xfc->screen_number);
xfc->depth = DefaultDepthOfScreen(xfc->screen);
xfc->big_endian = (ImageByteOrder(xfc->display) == MSBFirst);
xfc->invert = (ImageByteOrder(xfc->display) == MSBFirst) ? TRUE : FALSE;
xfc->complex_regions = TRUE;
xfc->fullscreen = settings->Fullscreen;
xfc->grab_keyboard = settings->GrabKeyboard;
xfc->fullscreen_toggle = settings->ToggleFullscreen;
xf_detect_monitors(xfc, settings);
xfc->colormap = DefaultColormap(xfc->display, xfc->screen_number);
2014-09-07 05:13:37 +04:00
xfc->format = PIXEL_FORMAT_XRGB32;
if (xfc->depth == 32)
xfc->format = (!xfc->invert) ? PIXEL_FORMAT_XRGB32 : PIXEL_FORMAT_XBGR32;
else if (xfc->depth == 24)
xfc->format = (!xfc->invert) ? PIXEL_FORMAT_XRGB32 : PIXEL_FORMAT_XBGR32;
else if (xfc->depth == 16)
xfc->format = (!xfc->invert) ? PIXEL_FORMAT_RGB565 : PIXEL_FORMAT_BGR565;
else if (xfc->depth == 15)
xfc->format = (!xfc->invert) ? PIXEL_FORMAT_RGB555 : PIXEL_FORMAT_BGR555;
else
xfc->format = PIXEL_FORMAT_XRGB32;
2013-04-02 21:22:59 +04:00
return TRUE;
}
/**
* Callback given to freerdp_connect() to perform post-connection operations.
* It will be called only if the connection was initialized properly, and will continue the initialization based on the
* newly created connection.
*/
2014-05-23 15:08:22 +04:00
BOOL xf_post_connect(freerdp *instance)
2013-04-02 21:22:59 +04:00
{
2014-10-22 06:19:11 +04:00
UINT32 flags;
2013-04-02 21:22:59 +04:00
XGCValues gcv;
rdpCache* cache;
rdpChannels* channels;
rdpSettings* settings;
ResizeWindowEventArgs e;
xfContext* xfc = (xfContext*) instance->context;
2013-04-02 21:22:59 +04:00
cache = instance->context->cache;
channels = instance->context->channels;
2013-04-02 21:22:59 +04:00
settings = instance->settings;
if (!xf_get_pixmap_info(xfc))
2013-04-02 21:22:59 +04:00
return FALSE;
xf_register_graphics(instance->context->graphics);
2014-10-22 06:19:11 +04:00
flags = CLRCONV_ALPHA;
if (xfc->bpp > 16)
flags |= CLRBUF_32BPP;
else
flags |= CLRBUF_16BPP;
if (settings->SoftwareGdi)
2013-04-02 21:22:59 +04:00
{
rdpGdi* gdi;
2013-04-02 21:22:59 +04:00
gdi_init(instance, flags, NULL);
2013-04-02 21:22:59 +04:00
gdi = instance->context->gdi;
xfc->primary_buffer = gdi->primary_buffer;
2013-04-02 21:22:59 +04:00
}
else
{
xfc->srcBpp = settings->ColorDepth;
2013-04-02 21:22:59 +04:00
xf_gdi_register_update_callbacks(instance->update);
2014-10-22 06:19:11 +04:00
xfc->hdc = gdi_CreateDC(flags, xfc->bpp);
2013-04-02 21:22:59 +04:00
}
xfc->width = settings->DesktopWidth;
xfc->height = settings->DesktopHeight;
2013-04-02 21:22:59 +04:00
#ifdef WITH_XRENDER
xfc->scaledWidth = xfc->width;
xfc->scaledHeight = xfc->height;
xfc->offset_x = 0;
xfc->offset_y = 0;
#endif
if (!xfc->xrenderAvailable)
{
if (settings->SmartSizing)
{
WLog_ERR(TAG, "XRender not available: disabling smart-sizing");
settings->SmartSizing = FALSE;
}
if (settings->MultiTouchGestures)
{
WLog_ERR(TAG, "XRender not available: disabling local multi-touch gestures");
settings->MultiTouchGestures = FALSE;
}
}
2013-04-02 21:22:59 +04:00
if (settings->RemoteApplicationMode)
xfc->remote_app = TRUE;
2013-04-02 21:22:59 +04:00
xf_create_window(xfc);
2013-04-02 21:22:59 +04:00
ZeroMemory(&gcv, sizeof(gcv));
2014-09-12 06:29:09 +04:00
if (xfc->modifierMap)
2014-03-14 05:10:22 +04:00
XFreeModifiermap(xfc->modifierMap);
2014-03-14 05:10:22 +04:00
xfc->modifierMap = XGetModifierMapping(xfc->display);
xfc->gc = XCreateGC(xfc->display, xfc->drawable, GCGraphicsExposures, &gcv);
xfc->primary = XCreatePixmap(xfc->display, xfc->drawable, xfc->width, xfc->height, xfc->depth);
xfc->drawing = xfc->primary;
xfc->bitmap_mono = XCreatePixmap(xfc->display, xfc->drawable, 8, 8, 1);
xfc->gc_mono = XCreateGC(xfc->display, xfc->bitmap_mono, GCGraphicsExposures, &gcv);
XSetFunction(xfc->display, xfc->gc, GXcopy);
XSetFillStyle(xfc->display, xfc->gc, FillSolid);
XSetForeground(xfc->display, xfc->gc, BlackPixelOfScreen(xfc->screen));
XFillRectangle(xfc->display, xfc->primary, xfc->gc, 0, 0, xfc->width, xfc->height);
XFlush(xfc->display);
2014-09-17 22:29:56 +04:00
xfc->image = XCreateImage(xfc->display, xfc->visual, xfc->depth, ZPixmap, 0,
2014-07-15 02:01:29 +04:00
(char*) xfc->primary_buffer, xfc->width, xfc->height, xfc->scanline_pad, 0);
2014-09-12 06:29:09 +04:00
if (settings->SoftwareGdi)
2013-04-02 21:22:59 +04:00
{
instance->update->BeginPaint = xf_sw_begin_paint;
instance->update->EndPaint = xf_sw_end_paint;
instance->update->DesktopResize = xf_sw_desktop_resize;
}
else
{
instance->update->BeginPaint = xf_hw_begin_paint;
instance->update->EndPaint = xf_hw_end_paint;
instance->update->DesktopResize = xf_hw_desktop_resize;
}
pointer_cache_register_callbacks(instance->update);
2014-09-12 06:29:09 +04:00
if (!settings->SoftwareGdi)
2013-04-02 21:22:59 +04:00
{
glyph_cache_register_callbacks(instance->update);
brush_cache_register_callbacks(instance->update);
bitmap_cache_register_callbacks(instance->update);
offscreen_cache_register_callbacks(instance->update);
palette_cache_register_callbacks(instance->update);
2014-09-12 06:29:09 +04:00
instance->update->BitmapUpdate = xf_gdi_bitmap_update;
2013-04-02 21:22:59 +04:00
}
instance->update->PlaySound = xf_play_sound;
instance->update->SetKeyboardIndicators = xf_keyboard_set_indicators;
2013-04-02 21:22:59 +04:00
xfc->clipboard = xf_clipboard_new(xfc);
2013-04-02 21:22:59 +04:00
freerdp_channels_post_connect(channels, instance);
EventArgsInit(&e, "xfreerdp");
e.width = settings->DesktopWidth;
e.height = settings->DesktopHeight;
2014-05-23 15:08:22 +04:00
PubSub_OnResizeWindow(((rdpContext *) xfc)->pubSub, xfc, &e);
2013-04-06 03:08:37 +04:00
2013-04-02 21:22:59 +04:00
return TRUE;
}
/** Callback set in the rdp_freerdp structure, and used to get the user's password,
* if required to establish the connection.
* This function is actually called in credssp_ntlmssp_client_init()
* @see rdp_server_accept_nego() and rdp_check_fds()
* @param instance - pointer to the rdp_freerdp structure that contains the connection settings
* @param username - unused
* @param password - on return: pointer to a character string that will be filled by the password entered by the user.
* Note that this character string will be allocated inside the function, and needs to be deallocated by the caller
* using free(), even in case this function fails.
* @param domain - unused
* @return TRUE if a password was successfully entered. See freerdp_passphrase_read() for more details.
*/
2014-05-23 15:08:22 +04:00
BOOL xf_authenticate(freerdp *instance, char **username, char **password, char **domain)
2013-04-02 21:22:59 +04:00
{
// FIXME: seems this callback may be called when 'username' is not known.
// But it doesn't do anything to fix it...
*password = malloc(password_size * sizeof(char));
if (freerdp_passphrase_read("Password: ", *password, password_size, instance->settings->CredentialsFromStdin) == NULL)
2013-04-02 21:22:59 +04:00
return FALSE;
2013-04-02 21:22:59 +04:00
return TRUE;
}
/** Callback set in the rdp_freerdp structure, and used to make a certificate validation
* when the connection requires it.
* This function will actually be called by tls_verify_certificate().
* @see rdp_client_connect() and tls_connect()
* @param instance - pointer to the rdp_freerdp structure that contains the connection settings
* @param subject
* @param issuer
* @param fingerprint
* @return TRUE if the certificate is trusted. FALSE otherwise.
*/
BOOL xf_verify_certificate(freerdp* instance, char* subject, char* issuer, char* fingerprint)
2013-04-02 21:22:59 +04:00
{
char answer;
2014-09-12 19:13:01 +04:00
WLog_INFO(TAG, "Certificate details:");
WLog_INFO(TAG, "\tSubject: %s", subject);
WLog_INFO(TAG, "\tIssuer: %s", issuer);
WLog_INFO(TAG, "\tThumbprint: %s", fingerprint);
WLog_INFO(TAG, "The above X.509 certificate could not be verified, possibly because you do not have "
2014-05-23 15:08:22 +04:00
"the CA certificate in your certificate store, or the certificate has expired. "
2014-09-12 19:13:01 +04:00
"Please look at the documentation on how to create local certificate store for a private CA.");
while (1)
2013-04-02 21:22:59 +04:00
{
2014-09-12 19:13:01 +04:00
WLog_INFO(TAG, "Do you trust the above certificate? (Y/N) ");
2013-04-02 21:22:59 +04:00
answer = fgetc(stdin);
if (feof(stdin))
2013-04-02 21:22:59 +04:00
{
2014-09-12 19:13:01 +04:00
WLog_INFO(TAG, "Error: Could not read answer from stdin.");
if (instance->settings->CredentialsFromStdin)
2014-09-12 19:13:01 +04:00
WLog_INFO(TAG, " - Run without parameter \"--from-stdin\" to set trust.");
WLog_INFO(TAG, "");
2013-04-02 21:22:59 +04:00
return FALSE;
}
if (answer == 'y' || answer == 'Y')
2013-04-02 21:22:59 +04:00
{
return TRUE;
}
else if (answer == 'n' || answer == 'N')
{
break;
}
2014-09-12 19:13:01 +04:00
WLog_INFO(TAG, "");
2013-04-02 21:22:59 +04:00
}
2013-04-02 21:22:59 +04:00
return FALSE;
}
int xf_logon_error_info(freerdp* instance, UINT32 data, UINT32 type)
2013-04-02 21:22:59 +04:00
{
xfContext* xfc = (xfContext*) instance->context;
xf_rail_disable_remoteapp_mode(xfc);
2013-04-02 21:22:59 +04:00
return 1;
}
void xf_window_free(xfContext* xfc)
2013-04-02 21:22:59 +04:00
{
rdpContext* context = (rdpContext*) xfc;
xf_keyboard_free(xfc);
if (xfc->gc)
2013-04-02 21:22:59 +04:00
{
XFreeGC(xfc->display, xfc->gc);
xfc->gc = 0;
2013-04-02 21:22:59 +04:00
}
if (xfc->gc_mono)
2013-04-02 21:22:59 +04:00
{
XFreeGC(xfc->display, xfc->gc_mono);
xfc->gc_mono = 0;
2013-04-02 21:22:59 +04:00
}
if (xfc->window)
2013-04-02 21:22:59 +04:00
{
xf_DestroyDesktopWindow(xfc, xfc->window);
xfc->window = NULL;
2013-04-02 21:22:59 +04:00
}
if (xfc->primary)
2013-04-02 21:22:59 +04:00
{
XFreePixmap(xfc->display, xfc->primary);
xfc->primary = 0;
2013-04-02 21:22:59 +04:00
}
if (xfc->bitmap_mono)
2013-04-02 21:22:59 +04:00
{
XFreePixmap(xfc->display, xfc->bitmap_mono);
xfc->bitmap_mono = 0;
2013-04-02 21:22:59 +04:00
}
if (xfc->image)
2013-04-02 21:22:59 +04:00
{
xfc->image->data = NULL;
XDestroyImage(xfc->image);
xfc->image = NULL;
2013-04-02 21:22:59 +04:00
}
if (context->cache)
2013-04-02 21:22:59 +04:00
{
cache_free(context->cache);
context->cache = NULL;
2013-06-14 05:34:46 +04:00
}
if (xfc->hdc)
{
gdi_DeleteDC(xfc->hdc);
}
if (xfc->xv_context)
2013-06-14 05:34:46 +04:00
{
xf_tsmf_uninit(xfc, NULL);
2013-06-14 05:34:46 +04:00
xfc->xv_context = NULL;
}
2014-10-15 06:24:07 +04:00
if (xfc->clipboard)
2013-06-14 05:34:46 +04:00
{
2014-10-15 06:24:07 +04:00
xf_clipboard_free(xfc->clipboard);
xfc->clipboard = NULL;
2013-06-14 05:34:46 +04:00
}
2013-04-02 21:22:59 +04:00
}
2014-10-15 06:24:07 +04:00
void* xf_input_thread(void *arg)
2013-04-02 21:22:59 +04:00
{
DWORD status;
DWORD nCount;
HANDLE events[2];
2013-04-02 21:22:59 +04:00
XEvent xevent;
wMessage msg;
wMessageQueue *queue;
2013-04-02 21:22:59 +04:00
int pending_status = 1;
int process_status = 1;
freerdp* instance = (freerdp*) arg;
xfContext* xfc = (xfContext*) instance->context;
queue = freerdp_get_message_queue(instance, FREERDP_INPUT_MESSAGE_QUEUE);
nCount = 0;
events[nCount++] = MessageQueue_Event(queue);
events[nCount++] = CreateFileDescriptorEvent(NULL, FALSE, FALSE, xfc->xfds);
2014-10-15 06:24:07 +04:00
while(1)
2013-04-02 21:22:59 +04:00
{
status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
if (WaitForSingleObject(events[0], 0) == WAIT_OBJECT_0)
{
if (MessageQueue_Peek(queue, &msg, FALSE))
{
if (msg.id == WMQ_QUIT)
break;
}
}
if (WaitForSingleObject(events[1], 0) == WAIT_OBJECT_0)
{
do
2013-04-02 21:22:59 +04:00
{
xf_lock_x11(xfc, FALSE);
pending_status = XPending(xfc->display);
xf_unlock_x11(xfc, FALSE);
if (pending_status)
{
xf_lock_x11(xfc, FALSE);
ZeroMemory(&xevent, sizeof(xevent));
XNextEvent(xfc->display, &xevent);
process_status = xf_event_process(instance, &xevent);
2014-10-15 06:24:07 +04:00
xf_unlock_x11(xfc, FALSE);
if (!process_status)
break;
}
}
while (pending_status);
if (!process_status)
break;
}
2013-04-02 21:22:59 +04:00
}
2014-10-15 06:24:07 +04:00
CloseHandle(events[1]);
2013-04-02 21:22:59 +04:00
MessageQueue_PostQuit(queue, 0);
2013-09-05 16:27:34 +04:00
ExitThread(0);
2013-04-02 21:22:59 +04:00
return NULL;
}
2014-10-15 06:24:07 +04:00
void* xf_channels_thread(void *arg)
2013-04-02 21:22:59 +04:00
{
int status;
xfContext* xfc;
2013-04-02 21:22:59 +04:00
HANDLE event;
2014-05-23 15:08:22 +04:00
rdpChannels *channels;
freerdp *instance = (freerdp *) arg;
2013-09-05 16:27:34 +04:00
assert(NULL != instance);
2014-05-23 15:08:22 +04:00
xfc = (xfContext *) instance->context;
2013-09-05 16:27:34 +04:00
assert(NULL != xfc);
2013-04-02 21:22:59 +04:00
channels = instance->context->channels;
event = freerdp_channels_get_event_handle(instance);
2014-10-15 06:24:07 +04:00
while (WaitForSingleObject(event, INFINITE) == WAIT_OBJECT_0)
2013-04-02 21:22:59 +04:00
{
status = freerdp_channels_process_pending_messages(instance);
2014-10-15 06:24:07 +04:00
if (!status)
break;
2013-04-02 21:22:59 +04:00
}
2014-10-15 06:24:07 +04:00
2013-09-05 16:27:34 +04:00
ExitThread(0);
2013-04-02 21:22:59 +04:00
return NULL;
}
BOOL xf_auto_reconnect(freerdp* instance)
{
xfContext* xfc = (xfContext*) instance->context;
UINT32 num_retries = 0;
UINT32 max_retries = instance->settings->AutoReconnectMaxRetries;
/* Only auto reconnect on network disconnects. */
if (freerdp_error_info(instance) != 0)
return FALSE;
/* A network disconnect was detected */
2014-09-12 19:13:01 +04:00
WLog_INFO(TAG, "Network disconnect!");
if (!instance->settings->AutoReconnectionEnabled)
{
/* No auto-reconnect - just quit */
return FALSE;
}
/* Perform an auto-reconnect. */
for (;;)
{
/* Quit retrying if max retries has been exceeded */
if (num_retries++ >= max_retries)
{
return FALSE;
}
/* Attempt the next reconnect */
2014-09-12 19:13:01 +04:00
WLog_INFO(TAG, "Attempting reconnect (%u of %u)", num_retries, max_retries);
if (freerdp_reconnect(instance))
{
xfc->disconnect = FALSE;
return TRUE;
}
sleep(5);
}
2014-09-12 19:13:01 +04:00
WLog_ERR(TAG, "Maximum reconnect retries exceeded");
return FALSE;
}
2013-04-02 21:22:59 +04:00
/** Main loop for the rdp connection.
* It will be run from the thread's entry point (thread_func()).
* It initiates the connection, and will continue to run until the session ends,
* processing events as they are received.
* @param instance - pointer to the rdp_freerdp structure that contains the session's settings
* @return A code from the enum XF_EXIT_CODE (0 if successful)
*/
2014-10-15 06:24:07 +04:00
void* xf_thread(void *param)
2013-04-02 21:22:59 +04:00
{
int i;
int fds;
xfContext* xfc;
2013-04-02 21:22:59 +04:00
int max_fds;
int rcount;
int wcount;
BOOL status;
int exit_code;
2014-05-23 15:08:22 +04:00
void *rfds[32];
void *wfds[32];
2013-04-02 21:22:59 +04:00
fd_set rfds_set;
fd_set wfds_set;
2014-05-23 15:08:22 +04:00
freerdp *instance;
2013-04-02 21:22:59 +04:00
int fd_input_event;
HANDLE input_event;
2013-04-02 21:22:59 +04:00
int select_status;
BOOL async_input;
BOOL async_channels;
BOOL async_transport;
HANDLE input_thread;
HANDLE channels_thread;
2014-05-23 15:08:22 +04:00
rdpChannels *channels;
rdpSettings *settings;
2013-04-02 21:22:59 +04:00
struct timeval timeout;
exit_code = 0;
input_event = NULL;
2014-05-23 15:08:22 +04:00
instance = (freerdp *) param;
2013-09-05 16:27:34 +04:00
assert(NULL != instance);
2013-04-02 21:22:59 +04:00
ZeroMemory(rfds, sizeof(rfds));
ZeroMemory(wfds, sizeof(wfds));
ZeroMemory(&timeout, sizeof(struct timeval));
status = freerdp_connect(instance);
xfc = (xfContext*) instance->context;
2013-09-05 16:27:34 +04:00
assert(NULL != xfc);
2013-04-02 21:22:59 +04:00
/* Connection succeeded. --authonly ? */
if (instance->settings->AuthenticationOnly)
2013-04-02 21:22:59 +04:00
{
freerdp_disconnect(instance);
2014-09-12 19:13:01 +04:00
WLog_ERR(TAG, "Authentication only, exit status %d", !status);
ExitThread(exit_code);
2013-04-02 21:22:59 +04:00
}
if (!status)
2013-04-02 21:22:59 +04:00
{
if (xfc->mutex)
{
WaitForSingleObject(xfc->mutex, INFINITE);
CloseHandle(xfc->mutex);
xfc->mutex = NULL;
}
xf_monitors_free(xfc, instance->settings);
exit_code = XF_EXIT_CONN_FAILED;
ExitThread(exit_code);
2013-04-02 21:22:59 +04:00
}
2013-04-02 21:22:59 +04:00
channels = instance->context->channels;
settings = instance->context->settings;
async_input = settings->AsyncInput;
async_channels = settings->AsyncChannels;
async_transport = settings->AsyncTransport;
if (async_input)
2013-04-02 21:22:59 +04:00
{
input_thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) xf_input_thread, instance, 0, NULL);
}
if (async_channels)
2013-04-02 21:22:59 +04:00
{
channels_thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) xf_channels_thread, instance, 0, NULL);
}
while (!xfc->disconnect && !freerdp_shall_disconnect(instance))
2013-04-02 21:22:59 +04:00
{
rcount = 0;
wcount = 0;
/*
* win8 and server 2k12 seem to have some timing issue/race condition
* when a initial sync request is send to sync the keyboard inidcators
* sending the sync event twice fixed this problem
*/
if (freerdp_focus_required(instance))
{
2014-03-14 05:10:22 +04:00
xf_keyboard_focus_in(xfc);
xf_keyboard_focus_in(xfc);
}
if (!async_transport)
2013-04-02 21:22:59 +04:00
{
if (!freerdp_get_fds(instance, rfds, &rcount, wfds, &wcount))
2013-04-02 21:22:59 +04:00
{
2014-09-12 19:13:01 +04:00
WLog_ERR(TAG, "Failed to get FreeRDP file descriptor");
exit_code = XF_EXIT_CONN_FAILED;
2013-04-02 21:22:59 +04:00
break;
}
}
if (!async_channels)
2013-04-02 21:22:59 +04:00
{
if (!freerdp_channels_get_fds(channels, instance, rfds, &rcount, wfds, &wcount))
2013-04-02 21:22:59 +04:00
{
2014-09-12 19:13:01 +04:00
WLog_ERR(TAG, "Failed to get channel manager file descriptor");
exit_code = XF_EXIT_CONN_FAILED;
2013-04-02 21:22:59 +04:00
break;
}
}
if (!async_input)
2013-04-02 21:22:59 +04:00
{
if (!xf_get_fds(instance, rfds, &rcount, wfds, &wcount))
2013-04-02 21:22:59 +04:00
{
2014-09-12 19:13:01 +04:00
WLog_ERR(TAG, "Failed to get xfreerdp file descriptor");
exit_code = XF_EXIT_CONN_FAILED;
2013-04-02 21:22:59 +04:00
break;
}
}
else
{
input_event = freerdp_get_message_queue_event_handle(instance, FREERDP_INPUT_MESSAGE_QUEUE);
fd_input_event = GetEventFileDescriptor(input_event);
2014-05-23 15:08:22 +04:00
rfds[rcount++] = (void *)(long) fd_input_event;
2013-04-02 21:22:59 +04:00
}
2013-04-02 21:22:59 +04:00
max_fds = 0;
FD_ZERO(&rfds_set);
FD_ZERO(&wfds_set);
for (i = 0; i < rcount; i++)
2013-04-02 21:22:59 +04:00
{
fds = (int)(long)(rfds[i]);
if (fds > max_fds)
2013-04-02 21:22:59 +04:00
max_fds = fds;
2013-04-02 21:22:59 +04:00
FD_SET(fds, &rfds_set);
}
if (max_fds == 0)
2013-04-02 21:22:59 +04:00
break;
2013-04-02 21:22:59 +04:00
timeout.tv_sec = 1;
timeout.tv_usec = 0;
select_status = select(max_fds + 1, &rfds_set, NULL, NULL, &timeout);
if (select_status == 0)
2013-04-02 21:22:59 +04:00
{
continue; /* select timeout */
}
else if (select_status == -1)
{
/* these are not really errors */
if (!((errno == EAGAIN) || (errno == EWOULDBLOCK) ||
(errno == EINPROGRESS) || (errno == EINTR))) /* signal occurred */
2013-04-02 21:22:59 +04:00
{
WLog_ERR(TAG, "xfreerdp_run: select failed");
break;
2013-04-02 21:22:59 +04:00
}
}
if (!async_transport)
2013-04-02 21:22:59 +04:00
{
if (!freerdp_check_fds(instance))
2013-04-02 21:22:59 +04:00
{
if (xf_auto_reconnect(instance))
continue;
2014-09-12 19:13:01 +04:00
WLog_ERR(TAG, "Failed to check FreeRDP file descriptor");
2013-04-02 21:22:59 +04:00
break;
}
}
if (!async_channels)
2013-04-02 21:22:59 +04:00
{
if (!freerdp_channels_check_fds(channels, instance))
2013-04-02 21:22:59 +04:00
{
2014-09-12 19:13:01 +04:00
WLog_ERR(TAG, "Failed to check channel manager file descriptor");
2013-04-02 21:22:59 +04:00
break;
}
}
if (!async_input)
2013-04-02 21:22:59 +04:00
{
if (!xf_process_x_events(instance))
2013-04-02 21:22:59 +04:00
{
2014-09-12 19:13:01 +04:00
WLog_INFO(TAG, "Closed from X11");
2013-04-02 21:22:59 +04:00
break;
}
}
else
{
if (WaitForSingleObject(input_event, 0) == WAIT_OBJECT_0)
2013-04-02 21:22:59 +04:00
{
if (!freerdp_message_queue_process_pending_messages(instance, FREERDP_INPUT_MESSAGE_QUEUE))
2013-04-02 21:22:59 +04:00
{
2014-09-12 19:13:01 +04:00
WLog_INFO(TAG, "User Disconnect");
xfc->disconnect = TRUE;
2013-04-02 21:22:59 +04:00
break;
}
}
}
}
/* Close the channels first. This will signal the internal message pipes
* that the threads should quit. */
freerdp_channels_close(channels, instance);
if (async_input)
2013-09-05 16:27:34 +04:00
{
2014-05-23 15:08:22 +04:00
wMessageQueue *input_queue = freerdp_get_message_queue(instance, FREERDP_INPUT_MESSAGE_QUEUE);
MessageQueue_PostQuit(input_queue, 0);
2013-09-05 16:27:34 +04:00
WaitForSingleObject(input_thread, INFINITE);
CloseHandle(input_thread);
}
if (async_channels)
2013-09-05 16:27:34 +04:00
{
WaitForSingleObject(channels_thread, INFINITE);
CloseHandle(channels_thread);
}
if (!exit_code)
exit_code = freerdp_error_info(instance);
2013-04-02 21:22:59 +04:00
freerdp_disconnect(instance);
gdi_free(instance);
ExitThread(exit_code);
return NULL;
2013-04-02 21:22:59 +04:00
}
DWORD xf_exit_code_from_disconnect_reason(DWORD reason)
2013-04-02 21:22:59 +04:00
{
if (reason == 0 || (reason >= XF_EXIT_PARSE_ARGUMENTS && reason <= XF_EXIT_CONN_FAILED))
2014-05-23 15:08:22 +04:00
return reason;
2013-04-02 21:22:59 +04:00
/* License error set */
else if (reason >= 0x100 && reason <= 0x10A)
reason -= 0x100 + XF_EXIT_LICENSE_INTERNAL;
2013-04-02 21:22:59 +04:00
/* RDP protocol error set */
else if (reason >= 0x10c9 && reason <= 0x1193)
reason = XF_EXIT_RDP;
2013-04-02 21:22:59 +04:00
/* There's no need to test protocol-independent codes: they match */
else if (!(reason <= 0xB))
reason = XF_EXIT_UNKNOWN;
2013-04-02 21:22:59 +04:00
return reason;
}
void xf_TerminateEventHandler(rdpContext* context, TerminateEventArgs* e)
2013-06-19 00:55:23 +04:00
{
wMessageQueue* queue;
xfContext* xfc = (xfContext*) context;
if (context->settings->AsyncInput)
2013-06-19 00:55:23 +04:00
{
queue = freerdp_get_message_queue(context->instance, FREERDP_INPUT_MESSAGE_QUEUE);
if (queue)
2013-06-19 00:55:23 +04:00
MessageQueue_PostQuit(queue, 0);
}
else
{
xfc->disconnect = TRUE;
}
}
#ifdef WITH_XRENDER
static void xf_ZoomingChangeEventHandler(rdpContext* context, ZoomingChangeEventArgs* e)
{
2014-09-12 06:29:09 +04:00
xfContext* xfc = (xfContext*) context;
int w = xfc->scaledWidth + e->dx;
int h = xfc->scaledHeight + e->dy;
2014-09-12 06:29:09 +04:00
if (e->dx == 0 && e->dy == 0)
return;
2014-09-12 06:29:09 +04:00
if (w < 10)
w = 10;
2014-09-12 06:29:09 +04:00
if (h < 10)
h = 10;
2014-09-12 06:29:09 +04:00
if (w == xfc->scaledWidth && h == xfc->scaledHeight)
return;
2014-09-12 06:29:09 +04:00
xfc->scaledWidth = w;
xfc->scaledHeight = h;
2014-09-12 06:29:09 +04:00
xf_draw_screen(xfc, 0, 0, xfc->width, xfc->height);
}
static void xf_PanningChangeEventHandler(rdpContext* context, PanningChangeEventArgs* e)
{
xfContext* xfc = (xfContext*) context;
if (e->dx == 0 && e->dy == 0)
return;
xfc->offset_x += e->dx;
xfc->offset_y += e->dy;
2014-09-12 06:29:09 +04:00
xf_draw_screen(xfc, 0, 0, xfc->width, xfc->height);
}
#endif
/**
* Client Interface
*/
static void xfreerdp_client_global_init()
{
setlocale(LC_ALL, "");
freerdp_handle_signals();
}
static void xfreerdp_client_global_uninit()
{
}
static int xfreerdp_client_start(rdpContext* context)
{
2014-09-12 06:29:09 +04:00
xfContext* xfc = (xfContext *) context;
rdpSettings* settings = context->settings;
if (!settings->ServerHostname)
{
2014-09-12 19:13:01 +04:00
WLog_ERR(TAG, "error: server hostname was not specified with /v:<server>[:port]");
return -1;
}
2013-09-05 16:27:34 +04:00
xfc->thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) xf_thread,
2014-05-23 15:08:22 +04:00
context->instance, 0, NULL);
return 0;
}
2014-09-12 06:29:09 +04:00
static int xfreerdp_client_stop(rdpContext* context)
{
2014-09-12 06:29:09 +04:00
xfContext* xfc = (xfContext*) context;
if (context->settings->AsyncInput)
2013-04-08 01:46:57 +04:00
{
2014-05-23 15:08:22 +04:00
wMessageQueue *queue;
queue = freerdp_get_message_queue(context->instance, FREERDP_INPUT_MESSAGE_QUEUE);
if (queue)
2013-06-19 00:55:23 +04:00
MessageQueue_PostQuit(queue, 0);
2013-04-08 01:46:57 +04:00
}
else
{
xfc->disconnect = TRUE;
2013-04-08 01:46:57 +04:00
}
2014-09-12 06:29:09 +04:00
if (xfc->thread)
{
CloseHandle(xfc->thread);
xfc->thread = NULL;
}
2014-09-12 06:29:09 +04:00
return 0;
}
2014-09-12 06:29:09 +04:00
static int xfreerdp_client_new(freerdp* instance, rdpContext* context)
{
2014-09-12 06:29:09 +04:00
xfContext* xfc;
rdpSettings* settings;
xfc = (xfContext*) instance->context;
2013-04-02 23:45:48 +04:00
instance->PreConnect = xf_pre_connect;
instance->PostConnect = xf_post_connect;
instance->PostDisconnect = xf_post_disconnect;
2013-04-02 23:45:48 +04:00
instance->Authenticate = xf_authenticate;
instance->VerifyCertificate = xf_verify_certificate;
instance->LogonErrorInfo = xf_logon_error_info;
2013-06-14 05:34:46 +04:00
context->channels = freerdp_channels_new();
2014-09-12 06:29:09 +04:00
settings = instance->settings;
xfc->settings = instance->context->settings;
2014-09-12 06:29:09 +04:00
2013-06-19 00:55:23 +04:00
PubSub_SubscribeTerminate(context->pubSub, (pTerminateEventHandler) xf_TerminateEventHandler);
#ifdef WITH_XRENDER
PubSub_SubscribeZoomingChange(context->pubSub, (pZoomingChangeEventHandler) xf_ZoomingChangeEventHandler);
PubSub_SubscribePanningChange(context->pubSub, (pPanningChangeEventHandler) xf_PanningChangeEventHandler);
#endif
2014-09-12 06:29:09 +04:00
return 0;
}
2014-09-12 06:29:09 +04:00
static void xfreerdp_client_free(freerdp* instance, rdpContext* context)
{
2014-09-12 06:29:09 +04:00
xfContext* xfc = (xfContext*) context;
if (context)
{
xf_window_free(xfc);
2014-09-12 06:29:09 +04:00
if (xfc->bitmap_buffer)
_aligned_free(xfc->bitmap_buffer);
if (xfc->display)
2013-06-14 05:34:46 +04:00
XCloseDisplay(xfc->display);
if (context->channels)
{
freerdp_channels_free(context->channels);
context->channels = NULL;
}
2013-04-02 23:45:48 +04:00
}
}
2014-09-12 06:29:09 +04:00
int RdpClientEntry(RDP_CLIENT_ENTRY_POINTS* pEntryPoints)
{
pEntryPoints->Version = 1;
pEntryPoints->Size = sizeof(RDP_CLIENT_ENTRY_POINTS_V1);
2013-06-14 06:11:23 +04:00
pEntryPoints->GlobalInit = xfreerdp_client_global_init;
pEntryPoints->GlobalUninit = xfreerdp_client_global_uninit;
2013-06-14 05:34:46 +04:00
pEntryPoints->ContextSize = sizeof(xfContext);
2013-06-14 06:11:23 +04:00
pEntryPoints->ClientNew = xfreerdp_client_new;
pEntryPoints->ClientFree = xfreerdp_client_free;
pEntryPoints->ClientStart = xfreerdp_client_start;
pEntryPoints->ClientStop = xfreerdp_client_stop;
return 0;
}