[PATCH] FreeRds backend, simplifications and improvements take 2

Mariusz Ceier mceier+wayland at gmail.com
Wed Feb 26 10:14:29 PST 2014


Hi,
  I did some basic testing of this version, and it seems to be as
stable as x11-backend :)
Issues that still aren't resolved:
 - when trying to enter polish letter they send either ^C or
characters from different keymap(?), depending on client used.
   But this may not be issue in freerds backend of weston, but in
FreeRDS or FreeRDP.
 - focus never changes (at least it's not indicated visually which
window has focus), to reproduce:
   run 2 instances of weston-terminal and see that cursor block isn't
filled in, and title bar doesn't change,
   when switching between windows.

Other than that, it works great :)

Mariusz Ceier


On 26 February 2014 17:57, Hardening <rdp.effort at gmail.com> wrote:
> Sorry for the last mail that was badly rebased with an incorrect src/Makefile.am
> file (thanks to Mariusz Ceier who told me about that).
>
> FreeRDS is a FreeRDP based RDP server, the server handles incoming connections and
> talks RDP with the peers. FreeRds cooperates with an "out-service": the out-service creates
> the content to display, and FreeRds will take care of encoding the content in the
> appropriate codec (raw, planar, remoteFx or NsCodec).
> To communicate, they use a unix socket for passing commands, and a shared buffer for
> the screen content. A vblank signal sent by FreeRds via the command channel allows
> to share the framebuffer nicely.
> This patch adds a backend to create a FreeRDS compositor and have weston being an out-service
> for FreeRds.
>
> This new revision simplifies packet extraction from the local socket. It also adds support for
> xkb variants: RDP keyboards that requires a xkb variant should work correctly now. The keyboards
> table has been filled with all know matches.
> ---
>  Makefile.am              |   15 +
>  configure.ac             |   12 +-
>  src/compositor-freerds.c | 1237 ++++++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 1263 insertions(+), 1 deletion(-)
>  create mode 100644 src/compositor-freerds.c
>
> diff --git a/Makefile.am b/Makefile.am
> index 64d0743..9d39d22 100644
> --- a/Makefile.am
> +++ b/Makefile.am
> @@ -308,6 +308,21 @@ rdp_backend_la_CFLAGS =                            \
>  rdp_backend_la_SOURCES = src/compositor-rdp.c
>  endif
>
> +if ENABLE_FREERDS_COMPOSITOR
> +module_LTLIBRARIES += freerds-backend.la
> +freerds_backend = freerds-backend.la
> +freerds_backend_la_LDFLAGS = -module -avoid-version
> +freerds_backend_la_LIBADD = $(COMPOSITOR_LIBS) \
> +       $(FREERDS_COMPOSITOR_LIBS) \
> +       libshared.la -lwinpr-input -lfreerdp-core
> +freerds_backend_la_CFLAGS =    \
> +       $(COMPOSITOR_CFLAGS)                    \
> +       $(FREERDS_COMPOSITOR_CFLAGS) \
> +       $(GCC_CFLAGS)
> +freerds_backend_la_SOURCES = src/compositor-freerds.c
> +endif
> +
> +
>  if HAVE_LCMS
>  module_LTLIBRARIES += cms-static.la
>  cms_static_la_LDFLAGS = -module -avoid-version
> diff --git a/configure.ac b/configure.ac
> index 00d7123..27d6d0a 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -56,7 +56,7 @@ AC_CHECK_DECL(CLOCK_MONOTONIC,[],
>               [[#include <time.h>]])
>  AC_CHECK_HEADERS([execinfo.h])
>
> -AC_CHECK_FUNCS([mkostemp strchrnul initgroups posix_fallocate])
> +AC_CHECK_FUNCS([accept4 mkostemp strchrnul initgroups posix_fallocate])
>
>  COMPOSITOR_MODULES="wayland-server >= 1.3.90 pixman-1"
>
> @@ -212,6 +212,15 @@ if test x$enable_rdp_compositor = xyes; then
>    CPPFLAGS="$SAVED_CPPFLAGS"
>  fi
>
> +AC_ARG_ENABLE([freerds-compositor], [  --enable-freerds-compositor],,
> +              enable_freerds_compositor=no)
> +AM_CONDITIONAL([ENABLE_FREERDS_COMPOSITOR],
> +               [test x$enable_freerds_compositor = xyes])
> +if test x$enable_freerds_compositor = xyes; then
> +  AC_DEFINE([BUILD_FREERDS_COMPOSITOR], [1], [Build the FreeRDS compositor])
> +  PKG_CHECK_MODULES(FREERDS_COMPOSITOR, [freerds-backend])
> +fi
> +
>  AC_ARG_WITH(cairo,
>             AS_HELP_STRING([--with-cairo=@<:@image|gl|glesv2@:>@]
>                            [Which Cairo renderer to use for the clients]),
> @@ -505,6 +514,7 @@ AC_MSG_RESULT([
>         RPI Compositor                  ${enable_rpi_compositor}
>         FBDEV Compositor                ${enable_fbdev_compositor}
>         RDP Compositor                  ${enable_rdp_compositor}
> +       FreeRDS Compositor              ${enable_freerds_compositor}
>
>         Raspberry Pi BCM headers        ${have_bcm_host}
>
> diff --git a/src/compositor-freerds.c b/src/compositor-freerds.c
> new file mode 100644
> index 0000000..d78b42c
> --- /dev/null
> +++ b/src/compositor-freerds.c
> @@ -0,0 +1,1237 @@
> +/**
> + * Copyright © 2013 Hardening <contact at hardening-consulting.com>
> + *
> + * Permission to use, copy, modify, distribute, and sell this software and
> + * its documentation for any purpose is hereby granted without fee, provided
> + * that the above copyright notice appear in all copies and that both that
> + * copyright notice and this permission notice appear in supporting
> + * documentation, and that the name of the copyright holders not be used in
> + * advertising or publicity pertaining to distribution of the software
> + * without specific, written prior permission.  The copyright holders make
> + * no representations about the suitability of this software for any
> + * purpose.  It is provided "as is" without express or implied warranty.
> + *
> + * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
> + * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
> + * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
> + * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
> + * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
> + * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
> + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
> + */
> +
> +#include "config.h"
> +
> +#include <stdlib.h>
> +#include <stdbool.h>
> +#include <unistd.h>
> +#include <string.h>
> +#include <errno.h>
> +#include <fcntl.h>
> +#include <sys/socket.h>
> +#include <sys/un.h>
> +#include <sys/shm.h>
> +#include <sys/mman.h>
> +#include <linux/input.h>
> +
> +#include <freerds/backend.h>
> +
> +#include <freerdp/freerdp.h>
> +#include <freerdp/update.h>
> +#include <freerdp/input.h>
> +#include <freerdp/locale/keyboard.h>
> +
> +#include <winpr/input.h>
> +#include <winpr/stream.h>
> +
> +#include "compositor.h"
> +#include "pixman-renderer.h"
> +
> +
> +#define DEFAULT_AXIS_STEP_DISTANCE wl_fixed_from_int(10)
> +#define FREERDS_COMMON_LENGTH 10
> +#define FREERDS_MODE_FPS 60 * 1000
> +
> +/* ======================== stolen from wayland-os.c  =======================*/
> +static int
> +set_cloexec_or_close(int fd)
> +{
> +       long flags;
> +
> +       if (fd == -1)
> +               return -1;
> +
> +       flags = fcntl(fd, F_GETFD);
> +       if (flags == -1)
> +               goto err;
> +
> +       if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
> +               goto err;
> +
> +       return fd;
> +
> +err:
> +       close(fd);
> +       return -1;
> +}
> +
> +static int
> +wl_os_socket_cloexec(int domain, int type, int protocol)
> +{
> +       int fd;
> +
> +       fd = socket(domain, type | SOCK_CLOEXEC, protocol);
> +       if (fd >= 0)
> +               return fd;
> +       if (errno != EINVAL)
> +               return -1;
> +
> +       fd = socket(domain, type, protocol);
> +       return set_cloexec_or_close(fd);
> +}
> +
> +static int
> +wl_os_accept_cloexec(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
> +{
> +       int fd;
> +
> +#ifdef HAVE_ACCEPT4
> +       fd = accept4(sockfd, addr, addrlen, SOCK_CLOEXEC);
> +       if (fd >= 0)
> +               return fd;
> +       if (errno != ENOSYS)
> +               return -1;
> +#endif
> +
> +       fd = accept(sockfd, addr, addrlen);
> +       return set_cloexec_or_close(fd);
> +}
> +
> +/* =========================================================================== */
> +
> +
> +struct freerds_compositor_config {
> +       int width;
> +       int height;
> +       char *freerds_named_pipe;
> +};
> +
> +struct freerds_compositor;
> +struct freerds_output;
> +
> +/** @brief state of the module protocol automata */
> +enum StreamState {
> +       STREAM_WAITING_COMMON_HEADER,
> +       STREAM_WAITING_DATA
> +};
> +
> +struct freerds_compositor {
> +       struct weston_compositor base;
> +
> +       struct freerds_output *output;
> +       struct weston_seat seat;
> +
> +       bool have_seat;
> +       int listening_fd;
> +       struct wl_event_source *server_event_source;
> +       int     client_fd;
> +       struct wl_event_source *client_event_source;
> +       enum StreamState streamState;
> +       wStream *in_stream;
> +       wStream *out_stream;
> +       int expected_bytes;
> +       UINT32 keyboard_layout;
> +       UINT32 keyboard_type;
> +       xkb_mod_index_t capslock_mod_index;
> +       xkb_mod_index_t numlock_mod_index;
> +       xkb_mod_index_t scrolllock_mod_index;
> +
> +       RDS_MSG_COMMON header_common;
> +       RDS_MSG_SHARED_FRAMEBUFFER rds_shared_fb;
> +       RDS_MSG_BEGIN_UPDATE rds_begin_update;
> +       RDS_MSG_END_UPDATE rds_end_update;
> +       RDS_MSG_PAINT_RECT rds_paint_rect;
> +       RDS_MSG_SET_SYSTEM_POINTER rds_set_system_pointer;
> +};
> +
> +struct freerds_output {
> +       struct weston_output base;
> +       struct freerds_compositor *compositor;
> +       struct wl_event_source *finish_frame_timer;
> +
> +       int shmid;
> +       void *buffer;
> +       pixman_image_t *shadow_surface;
> +       pixman_region32_t damagedRegion;
> +       bool waitingVBlank;
> +       bool outputActive;
> +};
> +
> +static void
> +freerds_compositor_config_init(struct freerds_compositor_config *config) {
> +       config->width = 640;
> +       config->height = 480;
> +       config->freerds_named_pipe = 0;
> +}
> +
> +static int
> +freerds_send_stream(struct freerds_compositor *c, wStream *s) {
> +       int toWrite, written;
> +       const char *ptr;
> +
> +       toWrite = Stream_Length(s);
> +       ptr = (const char *)Stream_Buffer(s);
> +       while (toWrite) {
> +               written = write(c->client_fd, ptr, toWrite);
> +               if (written <= 0)
> +                       return -1;
> +               toWrite -= written;
> +               ptr += written;
> +       }
> +       return 0;
> +}
> +
> +#define ROUND_DOWN(V, B) ((V) - ((V) % (B)))
> +#define ROUND_UP(V, B) ((V + (B - ((V) % B))))
> +
> +static void
> +freerds_update_framebuffer(struct freerds_compositor *c, pixman_box32_t *rect) {
> +       struct freerds_output *output = c->output;
> +       unsigned char *src = (unsigned char *)pixman_image_get_data(output->shadow_surface) +
> +                       (pixman_image_get_stride(output->shadow_surface) * rect->y1) +
> +                       (rect->x1 * 4);
> +       unsigned char *dst = (unsigned char *)output->buffer +
> +                       (c->rds_shared_fb.scanline * rect->y1) +
> +                       (rect->x1 * 4);
> +       int widthBytes = (rect->x2 - rect->x1) * 4;
> +       int y;
> +
> +       for (y = rect->y1; y < rect->y2; y++) {
> +               memcpy(dst, src, widthBytes);
> +               src += pixman_image_get_stride(output->shadow_surface);
> +               dst += c->rds_shared_fb.scanline;
> +       }
> +}
> +
> +static void
> +freerds_refresh_region(struct freerds_compositor *c, pixman_region32_t *region)
> +{
> +       int nrects, i;
> +       pixman_box32_t *rect;
> +       RDS_MSG_PAINT_RECT *paint = &c->rds_paint_rect;
> +
> +       Stream_SetPosition(c->out_stream, 0);
> +       freerds_server_message_write(c->out_stream, (RDS_MSG_COMMON *)&c->rds_begin_update);
> +
> +       rect = pixman_region32_rectangles(region, &nrects);
> +       for (i = 0; i < nrects; i++, rect++) {
> +               //weston_log("refresh_rect(%d,%d,%d,%d)\n", rect->x1, rect->y1, rect->x2, rect->y2);
> +               freerds_update_framebuffer(c, rect);
> +
> +               paint->nXSrc = paint->nLeftRect = ROUND_DOWN(rect->x1, 16);
> +               paint->nYSrc = paint->nTopRect = ROUND_DOWN(rect->y1, 16);
> +               paint->nWidth = ROUND_UP(rect->x2, 16) - paint->nLeftRect;
> +               paint->nHeight = ROUND_UP(rect->y2, 16) - paint->nTopRect;
> +               freerds_server_message_write(c->out_stream, (RDS_MSG_COMMON *)&c->rds_paint_rect);
> +       }
> +
> +       freerds_server_message_write(c->out_stream, (RDS_MSG_COMMON *)&c->rds_end_update);
> +
> +       Stream_SealLength(c->out_stream);
> +       freerds_send_stream(c, c->out_stream);
> +
> +       pixman_region32_clear(region);
> +
> +       c->output->waitingVBlank = true;
> +}
> +
> +
> +static void
> +freerds_output_start_repaint_loop(struct weston_output *output)
> +{
> +       uint32_t msec;
> +       struct timeval tv;
> +
> +       gettimeofday(&tv, NULL);
> +       msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
> +       weston_output_finish_frame(output, msec);
> +}
> +
> +static int
> +freerds_output_repaint(struct weston_output *output_base, pixman_region32_t *damage)
> +{
> +       struct freerds_output *output = container_of(output_base, struct freerds_output, base);
> +       struct weston_compositor *ec = output->base.compositor;
> +
> +       pixman_region32_union(&output->damagedRegion, &output->damagedRegion, damage);
> +
> +       pixman_renderer_output_set_buffer(output_base, output->shadow_surface);
> +       ec->renderer->repaint_output(&output->base, damage);
> +
> +       if (output->compositor->client_event_source && output->outputActive && !output->waitingVBlank)
> +               freerds_refresh_region(output->compositor, &output->damagedRegion);
> +
> +       pixman_region32_subtract(&ec->primary_plane.damage, &ec->primary_plane.damage, damage);
> +
> +       wl_event_source_timer_update(output->finish_frame_timer, 16);
> +       return 0;
> +}
> +
> +static void
> +freerds_kill_client(struct freerds_compositor *c) {
> +       wl_event_source_remove(c->client_event_source);
> +       c->client_event_source = 0;
> +       if (c->have_seat) {
> +               weston_seat_release_pointer(&c->seat);
> +               weston_seat_release_keyboard(&c->seat);
> +               weston_seat_release(&c->seat);
> +       }
> +
> +       close(c->client_fd);
> +       c->client_fd = -1;
> +
> +       c->expected_bytes = FREERDS_COMMON_LENGTH;
> +       c->streamState = STREAM_WAITING_COMMON_HEADER;
> +       c->keyboard_layout = 0;
> +       c->keyboard_type = 0;
> +       c->have_seat = false;
> +       c->output->waitingVBlank = true;
> +       c->output->outputActive = true;
> +}
> +
> +static void
> +freerds_output_destroy(struct weston_output *output_base)
> +{
> +       struct freerds_output *output = container_of(output_base, struct freerds_output, base);
> +
> +       if (output->shmid >= 0) {
> +               if (shmdt(output->buffer) < 0)
> +                       weston_log("error detaching segment 0x%x, error=%s", output->shmid, strerror(errno));
> +
> +               output->shmid = -1;
> +               output->buffer = 0;
> +       }
> +
> +       pixman_image_unref(output->shadow_surface);
> +       wl_event_source_remove(output->finish_frame_timer);
> +       pixman_region32_fini(&output->damagedRegion);
> +
> +       pixman_renderer_output_destroy(output_base);
> +       free(output);
> +}
> +
> +static int
> +finish_frame_handler(void *data)
> +{
> +       freerds_output_start_repaint_loop(data);
> +       return 1;
> +}
> +
> +static bool
> +freerds_update_shm(struct freerds_output *output, int width, int height) {
> +       struct freerds_compositor *c = output->compositor;
> +       if (output->shmid >= 0) {
> +               /* cleanup the previous shared memory if there were one */
> +               if (shmdt(output->buffer) < 0)
> +                       weston_log("error detaching segment 0x%x, error=%s", output->shmid, strerror(errno));
> +
> +               output->shmid = -1;
> +               output->buffer = 0;
> +               c->rds_shared_fb.segmentId = -1;
> +       }
> +
> +       c->rds_shared_fb.segmentId = output->shmid = shmget(IPC_PRIVATE, width * height * 4,
> +                       IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
> +       if (output->shmid < 0) {
> +               weston_log("unable to create a SHM, error=%s\n", strerror(errno));
> +               return false;
> +       }
> +
> +       output->buffer = shmat(output->shmid, 0, 0);
> +       if (output->buffer == (void *)-1) {
> +               weston_log("unable to attach to SHM, error=%s\n", strerror(errno));
> +       }
> +
> +       if (shmctl(output->shmid, IPC_RMID, 0) < 0)
> +               weston_log("error setting delete flag for shmId %d, error=%s", output->shmid, strerror(errno));
> +
> +       return (output->buffer != (void *)-1);
> +}
> +
> +
> +static struct weston_mode *
> +freerds_insert_new_mode(struct weston_output *output, int width, int height, int rate) {
> +       struct weston_mode *ret;
> +       ret = zalloc(sizeof *ret);
> +       if(!ret)
> +               return ret;
> +       ret->width = width;
> +       ret->height = height;
> +       ret->refresh = rate;
> +       wl_list_insert(&output->mode_list, &ret->link);
> +       return ret;
> +}
> +
> +static struct weston_mode *
> +ensure_matching_mode(struct weston_output *output, struct weston_mode *target) {
> +       struct weston_mode *local;
> +
> +       wl_list_for_each(local, &output->mode_list, link) {
> +               if((local->width == target->width) && (local->height == target->height))
> +                       return local;
> +       }
> +
> +       return freerds_insert_new_mode(output, target->width, target->height, FREERDS_MODE_FPS);
> +}
> +
> +static int
> +freerds_send_shared_framebuffer(struct freerds_compositor *c) {
> +       Stream_SetPosition(c->out_stream, 0);
> +       freerds_server_message_write(c->out_stream, (RDS_MSG_COMMON *)&c->rds_shared_fb);
> +       Stream_SealLength(c->out_stream);
> +
> +       return freerds_send_stream(c, c->out_stream);
> +}
> +
> +
> +static int
> +freerds_switch_mode(struct weston_output *output, struct weston_mode *mode)
> +{
> +       struct weston_mode *localMode;
> +       pixman_image_t *new_shadow_buffer;
> +       struct freerds_output *rdsOutput = container_of(output, struct freerds_output, base);
> +       struct freerds_compositor *c = rdsOutput->compositor;
> +
> +       localMode = ensure_matching_mode(output, mode);
> +       if (!localMode) {
> +               weston_log("unable to ensure the requested mode\n");
> +               return -ENOMEM;
> +       }
> +
> +       if(localMode == output->current_mode)
> +               return 0;
> +
> +       output->current_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
> +
> +       output->current_mode = localMode;
> +       output->current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
> +
> +       pixman_renderer_output_destroy(output);
> +       pixman_renderer_output_create(output);
> +
> +       new_shadow_buffer = pixman_image_create_bits(PIXMAN_x8r8g8b8, mode->width,
> +                       mode->height, 0, mode->width * 4);
> +       pixman_image_composite32(PIXMAN_OP_SRC, rdsOutput->shadow_surface, 0,
> +                       new_shadow_buffer, 0, 0,
> +                       0, 0,
> +                       0, 0, mode->width, mode->height
> +       );
> +
> +       pixman_image_unref(rdsOutput->shadow_surface);
> +       rdsOutput->shadow_surface = new_shadow_buffer;
> +
> +       pixman_region32_clear(&rdsOutput->damagedRegion);
> +       pixman_region32_union_rect(&rdsOutput->damagedRegion, &rdsOutput->damagedRegion,
> +                       0, 0, mode->width, mode->height);
> +
> +       c->rds_shared_fb.width = mode->width;
> +       c->rds_shared_fb.height = mode->height;
> +       c->rds_shared_fb.scanline = mode->width * 4;
> +
> +       if (c->client_fd >= 0) {
> +               // we have a connected peer so we have to inform it of the new configuration
> +               if (!freerds_update_shm(rdsOutput, mode->width, mode->height)) {
> +                       weston_log("unable to change the SHM configuration\n");
> +                       return -1;
> +               }
> +
> +               freerds_send_shared_framebuffer(c);
> +       }
> +       return 0;
> +}
> +
> +struct freerds_simple_mode {
> +       int width;
> +       int height;
> +};
> +static struct freerds_simple_mode standard_modes[] = {
> +               {640, 480},
> +               {800, 600},
> +               {1024, 768},
> +               {1280, 1024},
> +
> +               {0, 0}, /* /!\ the last one /!\ */
> +};
> +
> +
> +static int
> +freerds_compositor_create_output(struct freerds_compositor *c, int width, int height)
> +{
> +       int i;
> +       struct freerds_output *output;
> +       struct wl_event_loop *loop;
> +       struct weston_mode *currentMode, *next, *extraMode;
> +       RDS_MSG_SHARED_FRAMEBUFFER *shared_fb;
> +       RDS_MSG_PAINT_RECT *rect;
> +       RDS_MSG_SET_SYSTEM_POINTER *system_pointer;
> +
> +       output = zalloc(sizeof *output);
> +       if (output == NULL)
> +               return -1;
> +
> +       wl_list_init(&output->base.mode_list);
> +
> +       currentMode = freerds_insert_new_mode(&output->base, width, height, FREERDS_MODE_FPS);
> +       if(!currentMode)
> +               goto out_free_output;
> +       currentMode->flags = WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
> +
> +       for (i = 0; standard_modes[i].width; i++) {
> +               if (standard_modes[i].width == width && standard_modes[i].height == height)
> +                       continue;
> +
> +               extraMode = freerds_insert_new_mode(&output->base,
> +                               standard_modes[i].width,
> +                               standard_modes[i].height, FREERDS_MODE_FPS
> +               );
> +               if(!extraMode)
> +                       goto out_output;
> +       }
> +
> +       output->base.current_mode = output->base.native_mode = currentMode;
> +       weston_output_init(&output->base, &c->base, 0, 0, width, height,
> +                          WL_OUTPUT_TRANSFORM_NORMAL, 1);
> +
> +       output->base.make = "weston";
> +       output->base.model = "freerds";
> +
> +       output->shmid = -1;
> +       output->buffer = 0;
> +
> +       shared_fb = &c->rds_shared_fb;
> +       shared_fb->type = RDS_SERVER_SHARED_FRAMEBUFFER;
> +       shared_fb->width = width;
> +       shared_fb->height = height;
> +       shared_fb->bitsPerPixel = 32;
> +       shared_fb->bytesPerPixel = 4;
> +       shared_fb->flags = RDS_FRAMEBUFFER_FLAG_ATTACH;
> +       shared_fb->scanline = width * 4;
> +
> +       rect = &c->rds_paint_rect;
> +       rect->type = RDS_SERVER_PAINT_RECT;
> +       rect->fbSegmentId = output->shmid;
> +       rect->bitmapData = NULL;
> +       rect->bitmapDataLength = 0;
> +       rect->framebuffer = 0;
> +
> +       system_pointer = &c->rds_set_system_pointer;
> +       system_pointer->type = RDS_SERVER_SET_SYSTEM_POINTER;
> +       system_pointer->ptrType = SYSPTR_NULL;
> +
> +       c->rds_begin_update.type = RDS_SERVER_BEGIN_UPDATE;
> +       c->rds_end_update.type = RDS_SERVER_END_UPDATE;
> +
> +       output->waitingVBlank = true;
> +       output->outputActive = true;
> +
> +       pixman_region32_init(&output->damagedRegion);
> +       output->shadow_surface = pixman_image_create_bits(PIXMAN_a8r8g8b8,
> +                       width, height,
> +                   NULL,
> +                   width * 4);
> +       if (output->shadow_surface == NULL) {
> +               weston_log("Failed to create surface for frame buffer.\n");
> +               goto out_output;
> +       }
> +
> +       if (pixman_renderer_output_create(&output->base) < 0)
> +               goto out_shadow_surface;
> +
> +       loop = wl_display_get_event_loop(c->base.wl_display);
> +       output->finish_frame_timer = wl_event_loop_add_timer(loop, finish_frame_handler, output);
> +
> +       output->base.start_repaint_loop = freerds_output_start_repaint_loop;
> +       output->base.repaint = freerds_output_repaint;
> +       output->base.destroy = freerds_output_destroy;
> +       output->base.assign_planes = NULL;
> +       output->base.set_backlight = NULL;
> +       output->base.set_dpms = NULL;
> +       output->base.switch_mode = freerds_switch_mode;
> +       output->compositor = c;
> +       c->output = output;
> +
> +       wl_list_insert(c->base.output_list.prev, &output->base.link);
> +       return 0;
> +
> +out_shadow_surface:
> +       pixman_image_unref(output->shadow_surface);
> +out_output:
> +       weston_output_destroy(&output->base);
> +
> +       wl_list_for_each_safe(currentMode, next, &output->base.mode_list, link)
> +               free(currentMode);
> +out_free_output:
> +       free(output);
> +       return -1;
> +}
> +
> +static void
> +freerds_restore(struct weston_compositor *ec)
> +{
> +}
> +
> +static void
> +freerds_destroy(struct weston_compositor *ec)
> +{
> +       struct freerds_compositor *c = container_of(ec, struct freerds_compositor, base);
> +
> +       wl_event_source_remove(c->server_event_source);
> +       c->server_event_source = 0;
> +       close(c->listening_fd);
> +
> +       if (c->client_event_source)
> +               freerds_kill_client(c);
> +
> +       Stream_Free(c->in_stream, TRUE);
> +       Stream_Free(c->out_stream, TRUE);
> +       weston_compositor_shutdown(ec);
> +
> +       free(ec);
> +}
> +
> +
> +static void
> +freerds_mouse_event(struct freerds_compositor *c, DWORD x, DWORD y, DWORD flags) {
> +       wl_fixed_t wl_x, wl_y, axis;
> +       uint32_t button = 0;
> +
> +       //weston_log("mouse event: x=%ld y=%ld flags=0x%x\n", x, y, flags);
> +       if (flags & PTR_FLAGS_MOVE) {
> +               if((int)x < c->output->base.width && (int)y < c->output->base.height) {
> +                       wl_x = wl_fixed_from_int((int)x);
> +                       wl_y = wl_fixed_from_int((int)y);
> +                       notify_motion_absolute(&c->seat, weston_compositor_get_time(), wl_x, wl_y);
> +               }
> +       }
> +
> +       if (flags & PTR_FLAGS_BUTTON1)
> +               button = BTN_LEFT;
> +       else if (flags & PTR_FLAGS_BUTTON2)
> +               button = BTN_RIGHT;
> +       else if (flags & PTR_FLAGS_BUTTON3)
> +               button = BTN_MIDDLE;
> +
> +       if(button) {
> +               notify_button(&c->seat, weston_compositor_get_time(), button,
> +                       (flags & PTR_FLAGS_DOWN) ? WL_POINTER_BUTTON_STATE_PRESSED : WL_POINTER_BUTTON_STATE_RELEASED
> +               );
> +       }
> +
> +       if (flags & PTR_FLAGS_WHEEL) {
> +               /* DEFAULT_AXIS_STEP_DISTANCE is stolen from compositor-x11.c
> +                * The RDP specs says the lower bits of flags contains the "the number of rotation
> +                * units the mouse wheel was rotated".
> +                *
> +                * http://blogs.msdn.com/b/oldnewthing/archive/2013/01/23/10387366.aspx explains the 120 value
> +                */
> +               axis = (DEFAULT_AXIS_STEP_DISTANCE * (flags & 0xff)) / 120;
> +               if (flags & PTR_FLAGS_WHEEL_NEGATIVE)
> +                       axis = -axis;
> +
> +               notify_axis(&c->seat, weston_compositor_get_time(),
> +                                           WL_POINTER_AXIS_VERTICAL_SCROLL,
> +                                           axis);
> +       }
> +}
> +
> +static void
> +freerds_scancode_keyboard_event(struct freerds_compositor *c, UINT32 flags, UINT32 code,
> +               UINT32 keyboardType)
> +{
> +       uint32_t vk_code, full_code, key_code;
> +       enum wl_keyboard_key_state keyState;
> +       int notify = 0;
> +
> +       //weston_log("code=%d flags=0x%x keyb=%d\n", code, flags, keyboardType);
> +       if (flags & KBD_FLAGS_DOWN) {
> +               keyState = WL_KEYBOARD_KEY_STATE_PRESSED;
> +               notify = 1;
> +       } else if (flags & KBD_FLAGS_RELEASE) {
> +               keyState = WL_KEYBOARD_KEY_STATE_RELEASED;
> +               notify = 1;
> +       }
> +
> +       if(notify) {
> +               full_code = code;
> +               if(flags & KBD_FLAGS_EXTENDED)
> +                       full_code |= KBD_FLAGS_EXTENDED;
> +
> +               vk_code = GetVirtualKeyCodeFromVirtualScanCode(full_code, keyboardType);
> +               if(flags & KBD_FLAGS_EXTENDED)
> +                       vk_code |= KBDEXT;
> +
> +               key_code = GetKeycodeFromVirtualKeyCode(vk_code, KEYCODE_TYPE_EVDEV);
> +
> +               /*weston_log("code=%x ext=%d vk_code=%x scan_code=%x\n", code, (flags & KBD_FLAGS_EXTENDED) ? 1 : 0,
> +                               vk_code, scan_code);*/
> +               notify_key(&c->seat, weston_compositor_get_time(), key_code-8, keyState,
> +                               STATE_UPDATE_AUTOMATIC);
> +       }
> +}
> +
> +static void
> +freerds_virtual_keyboard_event(struct freerds_compositor *c, UINT32 flags, UINT32 vk_code)
> +{
> +       uint32_t key_code;
> +       enum wl_keyboard_key_state keyState;
> +       int notify = 0;
> +
> +       //weston_log("vk_code=%d flags=0x%x\n", vk_code, flags);
> +       if (flags & KBD_FLAGS_DOWN) {
> +               keyState = WL_KEYBOARD_KEY_STATE_PRESSED;
> +               notify = 1;
> +       } else if (flags & KBD_FLAGS_RELEASE) {
> +               keyState = WL_KEYBOARD_KEY_STATE_RELEASED;
> +               notify = 1;
> +       }
> +
> +       if(notify) {
> +               if(flags & KBD_FLAGS_EXTENDED)
> +                       vk_code |= KBDEXT;
> +
> +               key_code = GetKeycodeFromVirtualKeyCode(vk_code, KEYCODE_TYPE_EVDEV);
> +
> +               /*weston_log("code=%x ext=%d vk_code=%x scan_code=%x\n", code, (flags & KBD_FLAGS_EXTENDED) ? 1 : 0,
> +                               vk_code, scan_code);*/
> +               notify_key(&c->seat, weston_compositor_get_time(), key_code, keyState,
> +                               STATE_UPDATE_AUTOMATIC);
> +       }
> +}
> +
> +
> +struct rdp_to_xkb_keyboard_layout {
> +       UINT32 rdpLayoutCode;
> +       const char *xkbLayout;
> +       const char *xkbVariant;
> +};
> +
> +
> +/* table reversed from
> +        https://github.com/awakecoding/FreeRDP/blob/master/libfreerdp/locale/xkb_layout_ids.c#L811 */
> +static
> +struct rdp_to_xkb_keyboard_layout rdp_keyboards[] = {
> +               {KBD_ARABIC_101, "ara", 0},
> +               {KBD_BULGARIAN, 0, 0},
> +               {KBD_CHINESE_TRADITIONAL_US, 0},
> +               {KBD_CZECH, "cz", 0},
> +               {KBD_CZECH_PROGRAMMERS, "cz", "bksl"},
> +               {KBD_CZECH_QWERTY, "cz", "qwerty"},
> +               {KBD_DANISH, "dk", 0},
> +               {KBD_GERMAN, "de", 0},
> +               {KBD_GERMAN_NEO, "de", "neo"},
> +               {KBD_GERMAN_IBM, "de", "qwerty"},
> +               {KBD_GREEK, "gr", 0},
> +               {KBD_GREEK_220, "gr", "simple"},
> +               {KBD_GREEK_319, "gr", "extended"},
> +               {KBD_GREEK_POLYTONIC, "gr", "polytonic"},
> +               {KBD_US, "us", 0},
> +               {KBD_US_ENGLISH_TABLE_FOR_IBM_ARABIC_238_L, "ara", "buckwalter"},
> +               {KBD_SPANISH, "es", 0},
> +               {KBD_SPANISH_VARIATION, "es", "nodeadkeys"},
> +               {KBD_FINNISH, "fi", 0},
> +               {KBD_FRENCH, "fr", 0},
> +               {KBD_HEBREW, "il", 0},
> +               {KBD_HUNGARIAN, "hu", 0},
> +               {KBD_HUNGARIAN_101_KEY, "hu", "standard"},
> +               {KBD_ICELANDIC, "is", 0},
> +               {KBD_ITALIAN, "it", 0},
> +               {KBD_ITALIAN_142, "it", "nodeadkeys"},
> +               {KBD_JAPANESE, "jp", 0},
> +               {KBD_JAPANESE_INPUT_SYSTEM_MS_IME2002, "jp", "kana"},
> +               {KBD_KOREAN, "kr", 0},
> +               {KBD_KOREAN_INPUT_SYSTEM_IME_2000, "kr", "kr104"},
> +               {KBD_DUTCH, "nl", 0},
> +               {KBD_NORWEGIAN, "no", 0},
> +               {KBD_POLISH_PROGRAMMERS, "pl", 0},
> +               {KBD_POLISH_214, "pl", "qwertz"},
> +//             {KBD_PORTUGUESE_BRAZILIAN_ABN0416, 0},
> +               {KBD_ROMANIAN, "ro", 0},
> +               {KBD_RUSSIAN, "ru", 0},
> +               {KBD_RUSSIAN_TYPEWRITER, "ru", "typewriter"},
> +               {KBD_CROATIAN, "hr", 0},
> +               {KBD_SLOVAK, "sk", 0},
> +               {KBD_SLOVAK_QWERTY, "sk", "qwerty"},
> +               {KBD_ALBANIAN, 0, 0},
> +               {KBD_SWEDISH, "se", 0},
> +               {KBD_THAI_KEDMANEE, "th", 0},
> +               {KBD_THAI_KEDMANEE_NON_SHIFTLOCK, "th", "tis"},
> +               {KBD_TURKISH_Q, "tr", 0},
> +               {KBD_TURKISH_F, "tr", "f"},
> +               {KBD_URDU, "in", "urd-phonetic3"},
> +               {KBD_UKRAINIAN, "ua", 0},
> +               {KBD_BELARUSIAN, "by", 0},
> +               {KBD_SLOVENIAN, "si", 0},
> +               {KBD_ESTONIAN, "ee", 0},
> +               {KBD_LATVIAN, "lv", 0},
> +               {KBD_LITHUANIAN_IBM, "lt", "ibm"},
> +               {KBD_FARSI, "af", 0},
> +               {KBD_VIETNAMESE, "vn", 0},
> +               {KBD_ARMENIAN_EASTERN, "am", 0},
> +               {KBD_AZERI_LATIN, 0, 0},
> +               {KBD_FYRO_MACEDONIAN, "mk", 0},
> +               {KBD_GEORGIAN, "ge", 0},
> +               {KBD_FAEROESE, 0, 0},
> +               {KBD_DEVANAGARI_INSCRIPT, 0, 0},
> +               {KBD_MALTESE_47_KEY, 0, 0},
> +               {KBD_NORWEGIAN_WITH_SAMI, "no", "smi"},
> +               {KBD_KAZAKH, "kz", 0},
> +               {KBD_KYRGYZ_CYRILLIC, "kg", "phonetic"},
> +               {KBD_TATAR, "ru", "tt"},
> +               {KBD_BENGALI, "bd", 0},
> +               {KBD_BENGALI_INSCRIPT, "bd", "probhat"},
> +               {KBD_PUNJABI, 0, 0},
> +               {KBD_GUJARATI, "in", "guj"},
> +               {KBD_TAMIL, "in", "tam"},
> +               {KBD_TELUGU, "in", "tel"},
> +               {KBD_KANNADA, "in", "kan"},
> +               {KBD_MALAYALAM, "in", "mal"},
> +               {KBD_HINDI_TRADITIONAL, "in", 0},
> +               {KBD_MARATHI, 0, 0},
> +               {KBD_MONGOLIAN_CYRILLIC, "mn", 0},
> +               {KBD_UNITED_KINGDOM_EXTENDED, "gb", "intl"},
> +               {KBD_SYRIAC, "syc", 0},
> +               {KBD_SYRIAC_PHONETIC, "syc", "syc_phonetic"},
> +               {KBD_NEPALI, "np", 0},
> +               {KBD_PASHTO, "af", "ps"},
> +               {KBD_DIVEHI_PHONETIC, 0, 0},
> +               {KBD_LUXEMBOURGISH, 0, 0},
> +               {KBD_MAORI, "mao", 0},
> +               {KBD_CHINESE_SIMPLIFIED_US, 0, 0},
> +               {KBD_SWISS_GERMAN, "ch", "de_nodeadkeys"},
> +               {KBD_UNITED_KINGDOM, "gb", 0},
> +               {KBD_LATIN_AMERICAN, "latam", 0},
> +               {KBD_BELGIAN_FRENCH, "be", 0},
> +               {KBD_BELGIAN_PERIOD, "be", "oss_sundeadkeys"},
> +               {KBD_PORTUGUESE, "pt", 0},
> +               {KBD_SERBIAN_LATIN, "rs", 0},
> +               {KBD_AZERI_CYRILLIC, "az", "cyrillic"},
> +               {KBD_SWEDISH_WITH_SAMI, "se", "smi"},
> +               {KBD_UZBEK_CYRILLIC, "af", "uz"},
> +               {KBD_INUKTITUT_LATIN, "ca", "ike"},
> +               {KBD_CANADIAN_FRENCH_LEGACY, "ca", "fr-legacy"},
> +               {KBD_SERBIAN_CYRILLIC, "rs", 0},
> +               {KBD_CANADIAN_FRENCH, "ca", "fr-legacy"},
> +               {KBD_SWISS_FRENCH, "ch", "fr"},
> +               {KBD_BOSNIAN, "ba", 0},
> +               {KBD_IRISH, 0, 0},
> +               {KBD_BOSNIAN_CYRILLIC, "ba", "us"},
> +               {KBD_UNITED_STATES_DVORAK, "us", "dvorak"},
> +               {KBD_PORTUGUESE_BRAZILIAN_ABNT2, "br", "nativo"},
> +               {KBD_CANADIAN_MULTILINGUAL_STANDARD, "ca", "multix"},
> +               {KBD_GAELIC, "ie", "CloGaelach"},
> +
> +               {0x00000000, 0, 0},
> +};
> +
> +/* taken from 2.2.7.1.6 Input Capability Set (TS_INPUT_CAPABILITYSET) */
> +static char *rdp_keyboard_types[] = {
> +       "",     /* 0: unused */
> +       "", /* 1: IBM PC/XT or compatible (83-key) keyboard */
> +       "", /* 2: Olivetti "ICO" (102-key) keyboard */
> +       "", /* 3: IBM PC/AT (84-key) or similar keyboard */
> +       "pc105",/* 4: IBM enhanced (101- or 102-key) keyboard */
> +       "", /* 5: Nokia 1050 and similar keyboards */
> +       "",     /* 6: Nokia 9140 and similar keyboards */
> +       "jp106" /* 7: Japanese keyboard */
> +};
> +
> +static struct xkb_keymap *
> +freerds_retrieve_keymap(UINT32 rdpKbLayout, UINT32 rdpKbType) {
> +       struct xkb_context *xkbContext;
> +       struct xkb_rule_names xkbRuleNames;
> +       struct xkb_keymap *keymap;
> +       int i;
> +
> +       memset(&xkbRuleNames, 0, sizeof(xkbRuleNames));
> +       if(rdpKbType <= 7 && rdpKbType > 0)
> +               xkbRuleNames.model = rdp_keyboard_types[rdpKbType];
> +       else
> +               xkbRuleNames.model = "pc105";
> +
> +       for(i = 0; rdp_keyboards[i].rdpLayoutCode; i++) {
> +               if(rdp_keyboards[i].rdpLayoutCode == rdpKbLayout) {
> +                       xkbRuleNames.layout = rdp_keyboards[i].xkbLayout;
> +                       xkbRuleNames.variant = rdp_keyboards[i].xkbVariant;
> +                       break;
> +               }
> +       }
> +
> +       keymap = NULL;
> +       if(xkbRuleNames.layout) {
> +               xkbContext = xkb_context_new(0);
> +               if(!xkbContext) {
> +                       weston_log("unable to create a xkb_context\n");
> +                       return NULL;
> +               }
> +
> +               weston_log("looking for keymap %s\n", xkbRuleNames.layout);
> +               keymap = xkb_keymap_new_from_names(xkbContext, &xkbRuleNames, 0);
> +       }
> +       return keymap;
> +}
> +
> +static void
> +freerds_configure_keyboard(struct freerds_compositor *c, UINT32 layout, UINT32 keyboard_type) {
> +       //weston_log("%s: layout=0x%x keyboard_type=%d\n", __FUNCTION__, layout, keyboard_type);
> +       if (c->keyboard_layout == layout && c->keyboard_type == keyboard_type)
> +               return;
> +
> +       weston_seat_init_keyboard(&c->seat,
> +                       freerds_retrieve_keymap(layout, keyboard_type)
> +       );
> +
> +       c->keyboard_layout = layout;
> +       c->keyboard_type = keyboard_type;
> +}
> +
> +static void
> +freerds_update_keyboard_modifiers(struct freerds_compositor *c, bool capsLock,
> +               bool numLock, bool scrollLock, bool kanaLock)
> +{
> +       uint32_t mods_depressed, mods_latched, mods_locked, group;
> +       uint32_t serial;
> +       int numMask, capsMask, scrollMask;
> +
> +       struct weston_keyboard *keyboard = c->seat.keyboard;
> +       struct xkb_state *state = keyboard->xkb_state.state;
> +       struct weston_xkb_info *xkb_info = keyboard->xkb_info;
> +
> +       mods_depressed = xkb_state_serialize_mods(state, XKB_STATE_DEPRESSED);
> +       mods_latched = xkb_state_serialize_mods(state, XKB_STATE_LATCHED);
> +       mods_locked = xkb_state_serialize_mods(state, XKB_STATE_LOCKED);
> +       group = xkb_state_serialize_group(state, XKB_STATE_EFFECTIVE);
> +
> +       numMask = (1 << xkb_info->mod2_mod);
> +       capsMask = (1 << xkb_info->caps_mod);
> +       scrollMask = (1 << xkb_info->scroll_led); // TODO: don't rely on the led status
> +
> +       mods_locked = capsLock ? (mods_locked | capsMask) : (mods_locked & ~capsMask);
> +       mods_locked = numLock ? (mods_locked | numMask) : (mods_locked & ~numLock);
> +       mods_locked = scrollLock ? (mods_locked | scrollMask) : (mods_locked & ~scrollMask);
> +
> +       xkb_state_update_mask(state, mods_depressed, mods_latched, mods_locked, 0, 0, group);
> +
> +       serial = wl_display_next_serial(c->base.wl_display);
> +       notify_modifiers(&c->seat, serial);
> +}
> +
> +static int
> +freerds_send_disable_pointer(struct freerds_compositor *c) {
> +       Stream_SetPosition(c->out_stream, 0);
> +       freerds_server_message_write(c->out_stream, (RDS_MSG_COMMON *)&c->rds_set_system_pointer);
> +       Stream_SealLength(c->out_stream);
> +       return freerds_send_stream(c, c->out_stream);
> +}
> +
> +
> +static int
> +freerds_treat_message(struct freerds_compositor *c, wStream *s) {
> +       RDS_MSG_CAPABILITIES capabilities;
> +       RDS_MSG_MOUSE_EVENT mouse_event;
> +       RDS_MSG_SUPPRESS_OUTPUT suppress_output;
> +       RDS_MSG_SCANCODE_KEYBOARD_EVENT scancode_event;
> +       RDS_MSG_VIRTUAL_KEYBOARD_EVENT vk_event;
> +       RDS_MSG_SYNCHRONIZE_KEYBOARD_EVENT sync_keyboard_event;
> +       RDS_MSG_REFRESH_RECT refresh_rect_event;
> +
> +       RECTANGLE_16 *rect16;
> +       unsigned int i;
> +       struct weston_mode *currentMode, targetMode;
> +
> +       //weston_log("message type %d\n", c->header_common.type);
> +       switch (c->header_common.type) {
> +       case RDS_CLIENT_CAPABILITIES:
> +               if (freerds_read_capabilities(s, &capabilities) < 0) {
> +                       weston_log("invalid capabilities message\n");
> +                       return -1;
> +               }
> +               weston_seat_init(&c->seat, &c->base, "freerds");
> +               weston_seat_init_pointer(&c->seat);
> +
> +               freerds_configure_keyboard(c, capabilities.KeyboardLayout, capabilities.KeyboardType);
> +               c->have_seat = true;
> +
> +               currentMode = c->output->base.current_mode;
> +               if (capabilities.DesktopWidth != currentMode->width || capabilities.DesktopHeight != currentMode->height) {
> +                       // mode switching will send the shared framebuffer
> +                       targetMode.width = capabilities.DesktopWidth;
> +                       targetMode.height = capabilities.DesktopHeight;
> +                       weston_output_switch_mode(&c->output->base, &targetMode, 1, WESTON_MODE_SWITCH_SET_NATIVE);
> +               } else {
> +                       if (c->output->shmid < 0)
> +                               freerds_update_shm(c->output, capabilities.DesktopWidth, capabilities.DesktopHeight);
> +                       if (freerds_send_shared_framebuffer(c) < 0)
> +                               weston_log("unable to send shared framebuffer, errno=%d\n", errno);
> +               }
> +
> +               if (freerds_send_disable_pointer(c) < 0)
> +                       weston_log("unable to disable client-side pointer, errno=%d\n", errno);
> +               break;
> +
> +       case RDS_CLIENT_MOUSE_EVENT:
> +               if (freerds_read_mouse_event(s, &mouse_event) < 0) {
> +                       weston_log("invalid mouse event message\n");
> +                       return -1;
> +               }
> +               freerds_mouse_event(c, mouse_event.x, mouse_event.y, mouse_event.flags);
> +               break;
> +
> +       case RDS_CLIENT_SCANCODE_KEYBOARD_EVENT:
> +               if (freerds_read_scancode_keyboard_event(s, &scancode_event) < 0) {
> +                       weston_log("invalid scancode keyboard event message\n");
> +                       return -1;
> +               }
> +               freerds_scancode_keyboard_event(c, scancode_event.flags, scancode_event.code,
> +                               scancode_event.keyboardType);
> +               break;
> +
> +       case RDS_CLIENT_VIRTUAL_KEYBOARD_EVENT:
> +               if (freerds_read_virtual_keyboard_event(s, &vk_event) < 0) {
> +                       weston_log("invalid virtual keycode event message\n");
> +                       return -1;
> +               }
> +               freerds_virtual_keyboard_event(c, vk_event.flags, vk_event.code);
> +               break;
> +
> +       case RDS_CLIENT_VBLANK_EVENT:
> +               c->output->waitingVBlank = false;
> +               if (c->output->outputActive && pixman_region32_not_empty(&c->output->damagedRegion))
> +                       freerds_refresh_region(c, &c->output->damagedRegion);
> +               break;
> +
> +       case RDS_CLIENT_REFRESH_RECT:
> +               if (freerds_read_refresh_rect(s, &refresh_rect_event) < 0) {
> +                       weston_log("invalid refresh_rect message\n");
> +                       return -1;
> +               }
> +
> +               rect16 = refresh_rect_event.areasToRefresh;
> +               for (i = 0; i < refresh_rect_event.numberOfAreas; i++, rect16++) {
> +                       pixman_region32_union_rect(&c->output->damagedRegion, &c->output->damagedRegion,
> +                                       rect16->left, rect16->top,
> +                                       (rect16->right - rect16->left),
> +                                       (rect16->bottom - rect16->top));
> +               }
> +
> +               if (!c->output->waitingVBlank && c->output->outputActive)
> +                       freerds_refresh_region(c, &c->output->damagedRegion);
> +               break;
> +
> +       case RDS_CLIENT_SUPPRESS_OUTPUT:
> +               if (freerds_read_suppress_output(s, &suppress_output) < 0) {
> +                       weston_log("invalid suppress_output message\n");
> +                       return -1;
> +               }
> +
> +               c->output->outputActive = suppress_output.activeOutput;
> +               // TODO: freeze the compositor when output is suppressed ?
> +               break;
> +
> +       case RDS_CLIENT_SYNCHRONIZE_KEYBOARD_EVENT:
> +               if (freerds_read_synchronize_keyboard_event(s, &sync_keyboard_event) < 0) {
> +                       weston_log("invalid synchronize keyboard message\n");
> +                       return -1;
> +               }
> +
> +               freerds_update_keyboard_modifiers(c,
> +                               sync_keyboard_event.flags & KBD_SYNC_CAPS_LOCK,
> +                               sync_keyboard_event.flags & KBD_SYNC_NUM_LOCK,
> +                               sync_keyboard_event.flags & KBD_SYNC_SCROLL_LOCK,
> +                               sync_keyboard_event.flags & KBD_SYNC_KANA_LOCK
> +               );
> +               break;
> +
> +       case RDS_CLIENT_UNICODE_KEYBOARD_EVENT:
> +       case RDS_CLIENT_EXTENDED_MOUSE_EVENT:
> +       default:
> +               weston_log("not handled yet, %d\n", c->header_common.type);
> +               break;
> +       }
> +       return 0;
> +}
> +
> +
> +static int
> +freerds_client_activity(int fd, uint32_t mask, void *data) {
> +       struct freerds_compositor *c = (struct freerds_compositor *)data;
> +       int ret, len;
> +
> +       if (!(mask & WL_EVENT_READABLE))
> +               return 0;
> +
> +       ret = read(fd, Stream_Pointer(c->in_stream), c->expected_bytes);
> +       if (ret <= 0) {
> +               weston_log("connection closed fd=%d client_fd=%d\n", fd, c->client_fd);
> +               freerds_kill_client(c);
> +               return 0;
> +       }
> +
> +       Stream_Seek(c->in_stream, ret);
> +       c->expected_bytes -= ret;
> +
> +       if (c->expected_bytes)
> +               return 0;
> +
> +       if (c->streamState == STREAM_WAITING_COMMON_HEADER) {
> +               len = freerds_peek_common_header_length(Stream_Buffer(c->in_stream));
> +               if (len < FREERDS_COMMON_LENGTH) {
> +                       weston_log("packet invalid length %d\n", len);
> +                       freerds_kill_client(c);
> +                       return 0;
> +               }
> +
> +               c->expected_bytes = len - FREERDS_COMMON_LENGTH;
> +               if(c->expected_bytes) {
> +                       Stream_EnsureCapacity(c->in_stream, c->expected_bytes);
> +                       c->streamState = STREAM_WAITING_DATA;
> +                       return 0;
> +               }
> +       }
> +
> +       Stream_SealLength(c->in_stream);
> +       Stream_SetPosition(c->in_stream, 0);
> +
> +       if (freerds_read_common_header(c->in_stream, &c->header_common) < 0) {
> +               weston_log("invalid common header\n");
> +               freerds_kill_client(c);
> +               return 0;
> +       }
> +
> +       if (freerds_treat_message(c, c->in_stream) < 0) {
> +               weston_log("error treating message type %d\n", c->header_common.type);
> +               freerds_kill_client(c);
> +               return 0;
> +       }
> +
> +       Stream_SetPosition(c->in_stream, 0);
> +       c->streamState = STREAM_WAITING_COMMON_HEADER;
> +       c->expected_bytes = FREERDS_COMMON_LENGTH;
> +
> +       return 0;
> +}
> +
> +static int
> +freerds_named_pipe_activity(int fd, uint32_t mask, void *data) {
> +       struct freerds_compositor *c = (struct freerds_compositor *)data;
> +       struct wl_event_loop *loop;
> +
> +       if (c->client_fd != -1) {
> +               weston_log("dropping existing client");
> +               freerds_kill_client(c);
> +       }
> +
> +       c->client_fd = wl_os_accept_cloexec(c->listening_fd, 0, 0);
> +       if (c->client_fd >= 0) {
> +               loop = wl_display_get_event_loop(c->base.wl_display);
> +               c->client_event_source = wl_event_loop_add_fd(loop, c->client_fd, WL_EVENT_READABLE,
> +                               freerds_client_activity, c);
> +       }
> +       return 0;
> +}
> +
> +
> +static struct weston_compositor *
> +freerds_compositor_create(struct wl_display *display,
> +               struct freerds_compositor_config *config,
> +               int *argc, char *argv[], struct weston_config *wconfig)
> +{
> +       struct freerds_compositor *c;
> +       struct wl_event_loop *loop;
> +       struct sockaddr_un remote;
> +       int len;
> +
> +       c = zalloc(sizeof *c);
> +       if (c == NULL)
> +               return NULL;
> +
> +       if (weston_compositor_init(&c->base, display, argc, argv, wconfig) < 0)
> +               goto err_free;
> +
> +       c->base.destroy = freerds_destroy;
> +       c->base.restore = freerds_restore;
> +       c->client_fd = -1;
> +
> +       if (pixman_renderer_init(&c->base) < 0)
> +               goto err_compositor;
> +
> +       if (freerds_compositor_create_output(c, config->width, config->height) < 0)
> +               goto err_compositor;
> +
> +       if (!config->freerds_named_pipe) {
> +               weston_log("no socket path given");
> +               goto err_output;
> +       }
> +
> +       c->listening_fd = wl_os_socket_cloexec(AF_UNIX, SOCK_STREAM, 0);
> +       if (c->listening_fd < 0) {
> +               weston_log("unable to create the listening socket\n");
> +               goto err_output;
> +       }
> +
> +       memset(&remote, 0, sizeof(remote));
> +       remote.sun_family = AF_UNIX;
> +       strcpy(remote.sun_path, config->freerds_named_pipe);
> +       len = strlen(remote.sun_path) + sizeof(remote.sun_family);
> +       if (bind(c->listening_fd, (struct sockaddr *)&remote, len) < 0) {
> +               weston_log("unable to bind the named pipe, error=%s path=%s\n",
> +                               strerror(errno), config->freerds_named_pipe);
> +               goto err_socket;
> +       }
> +
> +       if (!listen(c->listening_fd, 1) < 0) {
> +               weston_log("unable to listen on the named pipe, errno=%d path=%s\n",
> +                               errno, config->freerds_named_pipe);
> +               goto err_socket;
> +       }
> +
> +       c->in_stream = Stream_New(NULL, FREERDS_COMMON_LENGTH);
> +       c->out_stream = Stream_New(NULL, 65536);
> +       c->expected_bytes = FREERDS_COMMON_LENGTH;
> +       c->streamState = STREAM_WAITING_COMMON_HEADER;
> +       loop = wl_display_get_event_loop(c->base.wl_display);
> +       c->server_event_source = wl_event_loop_add_fd(loop, c->listening_fd, WL_EVENT_READABLE,
> +                                                                                                       freerds_named_pipe_activity, c);
> +
> +       return &c->base;
> +
> +err_socket:
> +       close(c->listening_fd);
> +err_output:
> +       weston_output_destroy(&c->output->base);
> +err_compositor:
> +       weston_compositor_shutdown(&c->base);
> +err_free:
> +       free(c);
> +       return NULL;
> +}
> +
> +WL_EXPORT struct weston_compositor *
> +backend_init(struct wl_display *display, int *argc, char *argv[],
> +            struct weston_config *wconfig)
> +{
> +       struct freerds_compositor_config config;
> +       freerds_compositor_config_init(&config);
> +       int major, minor, revision;
> +
> +       freerdp_get_version(&major, &minor, &revision);
> +       weston_log("using FreeRDP version %d.%d.%d\n", major, minor, revision);
> +
> +       const struct weston_option freerds_options[] = {
> +               { WESTON_OPTION_STRING, "freerds-pipe", 0, &config.freerds_named_pipe },
> +               { WESTON_OPTION_UNSIGNED_INTEGER, "width", 0, &config.width },
> +               { WESTON_OPTION_UNSIGNED_INTEGER, "height", 0, &config.height },
> +       };
> +
> +       parse_options(freerds_options, ARRAY_LENGTH(freerds_options), argc, argv);
> +       return freerds_compositor_create(display, &config, argc, argv, wconfig);
> +}
> --
> 1.8.1.2
>
> _______________________________________________
> wayland-devel mailing list
> wayland-devel at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/wayland-devel


More information about the wayland-devel mailing list