Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2013 Hardening <rdp.effort@gmail.com> |
| 3 | * |
| 4 | * Permission to use, copy, modify, distribute, and sell this software and |
| 5 | * its documentation for any purpose is hereby granted without fee, provided |
| 6 | * that the above copyright notice appear in all copies and that both that |
| 7 | * copyright notice and this permission notice appear in supporting |
| 8 | * documentation, and that the name of the copyright holders not be used in |
| 9 | * advertising or publicity pertaining to distribution of the software |
| 10 | * without specific, written prior permission. The copyright holders make |
| 11 | * no representations about the suitability of this software for any |
| 12 | * purpose. It is provided "as is" without express or implied warranty. |
| 13 | * |
| 14 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS |
| 15 | * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 16 | * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 17 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 18 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 19 | * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 20 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 21 | */ |
| 22 | |
Daniel Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 23 | #include "config.h" |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 24 | |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <errno.h> |
| 28 | #include <linux/input.h> |
| 29 | |
Hardening | 04633b6 | 2014-01-10 11:33:06 +0100 | [diff] [blame^] | 30 | #if HAVE_FREERDP_VERSION_H |
| 31 | #include <freerdp/version.h> |
| 32 | #else |
| 33 | /* assume it's a early 1.1 version */ |
| 34 | #define FREERDP_VERSION_MAJOR 1 |
| 35 | #define FREERDP_VERSION_MINOR 1 |
| 36 | #endif |
| 37 | |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 38 | #include <freerdp/freerdp.h> |
| 39 | #include <freerdp/listener.h> |
| 40 | #include <freerdp/update.h> |
| 41 | #include <freerdp/input.h> |
| 42 | #include <freerdp/codec/color.h> |
| 43 | #include <freerdp/codec/rfx.h> |
| 44 | #include <freerdp/codec/nsc.h> |
Hardening | 4a3c150 | 2013-04-06 23:39:26 +0200 | [diff] [blame] | 45 | #include <winpr/input.h> |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 46 | |
| 47 | #include "compositor.h" |
| 48 | #include "pixman-renderer.h" |
| 49 | |
| 50 | #define MAX_FREERDP_FDS 32 |
Hardening | b60e46f | 2013-06-03 22:55:47 +0200 | [diff] [blame] | 51 | #define DEFAULT_AXIS_STEP_DISTANCE wl_fixed_from_int(10) |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 52 | |
| 53 | struct rdp_compositor_config { |
| 54 | int width; |
| 55 | int height; |
| 56 | char *bind_address; |
| 57 | int port; |
| 58 | char *rdp_key; |
| 59 | char *server_cert; |
| 60 | char *server_key; |
| 61 | char *extra_modes; |
| 62 | int env_socket; |
| 63 | }; |
| 64 | |
| 65 | struct rdp_output; |
| 66 | |
| 67 | struct rdp_compositor { |
| 68 | struct weston_compositor base; |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 69 | |
| 70 | freerdp_listener *listener; |
| 71 | struct wl_event_source *listener_events[MAX_FREERDP_FDS]; |
| 72 | struct rdp_output *output; |
| 73 | |
| 74 | char *server_cert; |
| 75 | char *server_key; |
| 76 | char *rdp_key; |
| 77 | int tls_enabled; |
| 78 | }; |
| 79 | |
| 80 | enum peer_item_flags { |
| 81 | RDP_PEER_ACTIVATED = (1 << 0), |
| 82 | RDP_PEER_OUTPUT_ENABLED = (1 << 1), |
| 83 | }; |
| 84 | |
| 85 | struct rdp_peers_item { |
| 86 | int flags; |
| 87 | freerdp_peer *peer; |
| 88 | struct weston_seat seat; |
| 89 | |
| 90 | struct wl_list link; |
| 91 | }; |
| 92 | |
| 93 | struct rdp_output { |
| 94 | struct weston_output base; |
| 95 | struct wl_event_source *finish_frame_timer; |
| 96 | pixman_image_t *shadow_surface; |
| 97 | |
| 98 | struct wl_list peers; |
| 99 | }; |
| 100 | |
| 101 | struct rdp_peer_context { |
| 102 | rdpContext _p; |
Hardening | b8f03aa | 2013-05-26 23:34:00 +0200 | [diff] [blame] | 103 | |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 104 | struct rdp_compositor *rdpCompositor; |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 105 | struct wl_event_source *events[MAX_FREERDP_FDS]; |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 106 | RFX_CONTEXT *rfx_context; |
| 107 | wStream *encode_stream; |
| 108 | RFX_RECT *rfx_rects; |
| 109 | NSC_CONTEXT *nsc_context; |
| 110 | |
| 111 | struct rdp_peers_item item; |
| 112 | }; |
| 113 | typedef struct rdp_peer_context RdpPeerContext; |
| 114 | |
| 115 | static void |
| 116 | rdp_compositor_config_init(struct rdp_compositor_config *config) { |
| 117 | config->width = 640; |
| 118 | config->height = 480; |
| 119 | config->bind_address = NULL; |
| 120 | config->port = 3389; |
| 121 | config->rdp_key = NULL; |
| 122 | config->server_cert = NULL; |
| 123 | config->server_key = NULL; |
| 124 | config->extra_modes = NULL; |
| 125 | config->env_socket = 0; |
| 126 | } |
| 127 | |
| 128 | static void |
| 129 | rdp_peer_refresh_rfx(pixman_region32_t *damage, pixman_image_t *image, freerdp_peer *peer) |
| 130 | { |
| 131 | int width, height, nrects, i; |
| 132 | pixman_box32_t *region, *rects; |
| 133 | uint32_t *ptr; |
| 134 | RFX_RECT *rfxRect; |
| 135 | rdpUpdate *update = peer->update; |
| 136 | SURFACE_BITS_COMMAND *cmd = &update->surface_bits_command; |
| 137 | RdpPeerContext *context = (RdpPeerContext *)peer->context; |
| 138 | |
Hardening | a1ce6cd | 2013-05-22 23:40:16 +0200 | [diff] [blame] | 139 | Stream_Clear(context->encode_stream); |
| 140 | Stream_SetPosition(context->encode_stream, 0); |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 141 | |
| 142 | width = (damage->extents.x2 - damage->extents.x1); |
| 143 | height = (damage->extents.y2 - damage->extents.y1); |
| 144 | |
| 145 | cmd->destLeft = damage->extents.x1; |
| 146 | cmd->destTop = damage->extents.y1; |
| 147 | cmd->destRight = damage->extents.x2; |
| 148 | cmd->destBottom = damage->extents.y2; |
| 149 | cmd->bpp = 32; |
| 150 | cmd->codecID = peer->settings->RemoteFxCodecId; |
| 151 | cmd->width = width; |
| 152 | cmd->height = height; |
| 153 | |
| 154 | ptr = pixman_image_get_data(image) + damage->extents.x1 + |
| 155 | damage->extents.y1 * (pixman_image_get_stride(image) / sizeof(uint32_t)); |
| 156 | |
| 157 | rects = pixman_region32_rectangles(damage, &nrects); |
| 158 | context->rfx_rects = realloc(context->rfx_rects, nrects * sizeof *rfxRect); |
| 159 | |
| 160 | for (i = 0; i < nrects; i++) { |
| 161 | region = &rects[i]; |
| 162 | rfxRect = &context->rfx_rects[i]; |
| 163 | |
| 164 | rfxRect->x = (region->x1 - damage->extents.x1); |
| 165 | rfxRect->y = (region->y1 - damage->extents.y1); |
| 166 | rfxRect->width = (region->x2 - region->x1); |
| 167 | rfxRect->height = (region->y2 - region->y1); |
| 168 | } |
| 169 | |
| 170 | rfx_compose_message(context->rfx_context, context->encode_stream, context->rfx_rects, nrects, |
| 171 | (BYTE *)ptr, width, height, |
| 172 | pixman_image_get_stride(image) |
| 173 | ); |
| 174 | |
Hardening | a1ce6cd | 2013-05-22 23:40:16 +0200 | [diff] [blame] | 175 | cmd->bitmapDataLength = Stream_GetPosition(context->encode_stream); |
| 176 | cmd->bitmapData = Stream_Buffer(context->encode_stream); |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 177 | |
| 178 | update->SurfaceBits(update->context, cmd); |
| 179 | } |
| 180 | |
| 181 | |
| 182 | static void |
| 183 | rdp_peer_refresh_nsc(pixman_region32_t *damage, pixman_image_t *image, freerdp_peer *peer) |
| 184 | { |
| 185 | int width, height; |
| 186 | uint32_t *ptr; |
| 187 | rdpUpdate *update = peer->update; |
| 188 | SURFACE_BITS_COMMAND *cmd = &update->surface_bits_command; |
| 189 | RdpPeerContext *context = (RdpPeerContext *)peer->context; |
| 190 | |
Hardening | a1ce6cd | 2013-05-22 23:40:16 +0200 | [diff] [blame] | 191 | Stream_Clear(context->encode_stream); |
| 192 | Stream_SetPosition(context->encode_stream, 0); |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 193 | |
| 194 | width = (damage->extents.x2 - damage->extents.x1); |
| 195 | height = (damage->extents.y2 - damage->extents.y1); |
| 196 | |
| 197 | cmd->destLeft = damage->extents.x1; |
| 198 | cmd->destTop = damage->extents.y1; |
| 199 | cmd->destRight = damage->extents.x2; |
| 200 | cmd->destBottom = damage->extents.y2; |
| 201 | cmd->bpp = 32; |
| 202 | cmd->codecID = peer->settings->NSCodecId; |
| 203 | cmd->width = width; |
| 204 | cmd->height = height; |
| 205 | |
| 206 | ptr = pixman_image_get_data(image) + damage->extents.x1 + |
| 207 | damage->extents.y1 * (pixman_image_get_stride(image) / sizeof(uint32_t)); |
| 208 | |
| 209 | nsc_compose_message(context->nsc_context, context->encode_stream, (BYTE *)ptr, |
| 210 | cmd->width, cmd->height, |
| 211 | pixman_image_get_stride(image)); |
Hardening | a1ce6cd | 2013-05-22 23:40:16 +0200 | [diff] [blame] | 212 | cmd->bitmapDataLength = Stream_GetPosition(context->encode_stream); |
| 213 | cmd->bitmapData = Stream_Buffer(context->encode_stream); |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 214 | update->SurfaceBits(update->context, cmd); |
| 215 | } |
| 216 | |
| 217 | static void |
Hardening | fe36a13 | 2013-05-22 23:40:20 +0200 | [diff] [blame] | 218 | pixman_image_flipped_subrect(const pixman_box32_t *rect, pixman_image_t *img, BYTE *dest) { |
| 219 | int stride = pixman_image_get_stride(img); |
| 220 | int h; |
| 221 | int toCopy = (rect->x2 - rect->x1) * 4; |
| 222 | int height = (rect->y2 - rect->y1); |
| 223 | const BYTE *src = (const BYTE *)pixman_image_get_data(img); |
| 224 | src += ((rect->y2-1) * stride) + (rect->x1 * 4); |
| 225 | |
| 226 | for (h = 0; h < height; h++, src -= stride, dest += toCopy) |
| 227 | memcpy(dest, src, toCopy); |
| 228 | } |
| 229 | |
| 230 | static void |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 231 | rdp_peer_refresh_raw(pixman_region32_t *region, pixman_image_t *image, freerdp_peer *peer) |
| 232 | { |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 233 | rdpUpdate *update = peer->update; |
| 234 | SURFACE_BITS_COMMAND *cmd = &update->surface_bits_command; |
Hardening | fe36a13 | 2013-05-22 23:40:20 +0200 | [diff] [blame] | 235 | SURFACE_FRAME_MARKER *marker = &update->surface_frame_marker; |
| 236 | pixman_box32_t *rect, subrect; |
| 237 | int nrects, i; |
| 238 | int heightIncrement, remainingHeight, top; |
| 239 | |
| 240 | rect = pixman_region32_rectangles(region, &nrects); |
| 241 | if (!nrects) |
| 242 | return; |
| 243 | |
| 244 | marker->frameId++; |
| 245 | marker->frameAction = SURFACECMD_FRAMEACTION_BEGIN; |
| 246 | update->SurfaceFrameMarker(peer->context, marker); |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 247 | |
| 248 | cmd->bpp = 32; |
| 249 | cmd->codecID = 0; |
Hardening | fe36a13 | 2013-05-22 23:40:20 +0200 | [diff] [blame] | 250 | |
| 251 | for (i = 0; i < nrects; i++, rect++) { |
| 252 | /*weston_log("rect(%d,%d, %d,%d)\n", rect->x1, rect->y1, rect->x2, rect->y2);*/ |
| 253 | cmd->destLeft = rect->x1; |
| 254 | cmd->destRight = rect->x2; |
| 255 | cmd->width = rect->x2 - rect->x1; |
| 256 | |
| 257 | heightIncrement = peer->settings->MultifragMaxRequestSize / (16 + cmd->width * 4); |
| 258 | remainingHeight = rect->y2 - rect->y1; |
| 259 | top = rect->y1; |
| 260 | |
| 261 | subrect.x1 = rect->x1; |
| 262 | subrect.x2 = rect->x2; |
| 263 | |
| 264 | while (remainingHeight) { |
| 265 | cmd->height = (remainingHeight > heightIncrement) ? heightIncrement : remainingHeight; |
| 266 | cmd->destTop = top; |
| 267 | cmd->destBottom = top + cmd->height; |
| 268 | cmd->bitmapDataLength = cmd->width * cmd->height * 4; |
| 269 | cmd->bitmapData = (BYTE *)realloc(cmd->bitmapData, cmd->bitmapDataLength); |
| 270 | |
| 271 | subrect.y1 = top; |
| 272 | subrect.y2 = top + cmd->height; |
| 273 | pixman_image_flipped_subrect(&subrect, image, cmd->bitmapData); |
| 274 | |
| 275 | /*weston_log("* sending (%d,%d, %d,%d)\n", subrect.x1, subrect.y1, subrect.x2, subrect.y2); */ |
| 276 | update->SurfaceBits(peer->context, cmd); |
| 277 | |
| 278 | remainingHeight -= cmd->height; |
| 279 | top += cmd->height; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | marker->frameAction = SURFACECMD_FRAMEACTION_END; |
| 284 | update->SurfaceFrameMarker(peer->context, marker); |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | static void |
| 288 | rdp_peer_refresh_region(pixman_region32_t *region, freerdp_peer *peer) |
| 289 | { |
| 290 | RdpPeerContext *context = (RdpPeerContext *)peer->context; |
| 291 | struct rdp_output *output = context->rdpCompositor->output; |
| 292 | rdpSettings *settings = peer->settings; |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 293 | |
Hardening | fe36a13 | 2013-05-22 23:40:20 +0200 | [diff] [blame] | 294 | if (settings->RemoteFxCodec) |
| 295 | rdp_peer_refresh_rfx(region, output->shadow_surface, peer); |
| 296 | else if (settings->NSCodec) |
| 297 | rdp_peer_refresh_nsc(region, output->shadow_surface, peer); |
| 298 | else |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 299 | rdp_peer_refresh_raw(region, output->shadow_surface, peer); |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 300 | } |
| 301 | |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 302 | static void |
| 303 | rdp_output_start_repaint_loop(struct weston_output *output) |
| 304 | { |
| 305 | uint32_t msec; |
| 306 | struct timeval tv; |
| 307 | |
| 308 | gettimeofday(&tv, NULL); |
| 309 | msec = tv.tv_sec * 1000 + tv.tv_usec / 1000; |
| 310 | weston_output_finish_frame(output, msec); |
| 311 | } |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 312 | |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 313 | static int |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 314 | rdp_output_repaint(struct weston_output *output_base, pixman_region32_t *damage) |
| 315 | { |
| 316 | struct rdp_output *output = container_of(output_base, struct rdp_output, base); |
| 317 | struct weston_compositor *ec = output->base.compositor; |
| 318 | struct rdp_peers_item *outputPeer; |
| 319 | |
| 320 | pixman_renderer_output_set_buffer(output_base, output->shadow_surface); |
| 321 | ec->renderer->repaint_output(&output->base, damage); |
| 322 | |
| 323 | wl_list_for_each(outputPeer, &output->peers, link) { |
| 324 | if ((outputPeer->flags & RDP_PEER_ACTIVATED) && |
| 325 | (outputPeer->flags & RDP_PEER_OUTPUT_ENABLED)) |
| 326 | { |
| 327 | rdp_peer_refresh_region(damage, outputPeer->peer); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | pixman_region32_subtract(&ec->primary_plane.damage, |
| 332 | &ec->primary_plane.damage, damage); |
| 333 | |
| 334 | wl_event_source_timer_update(output->finish_frame_timer, 16); |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 335 | return 0; |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | static void |
| 339 | rdp_output_destroy(struct weston_output *output_base) |
| 340 | { |
| 341 | struct rdp_output *output = (struct rdp_output *)output_base; |
| 342 | |
| 343 | wl_event_source_remove(output->finish_frame_timer); |
| 344 | free(output); |
| 345 | } |
| 346 | |
| 347 | static int |
| 348 | finish_frame_handler(void *data) |
| 349 | { |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 350 | rdp_output_start_repaint_loop(data); |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 351 | |
| 352 | return 1; |
| 353 | } |
| 354 | |
| 355 | |
| 356 | static struct weston_mode * |
| 357 | find_matching_mode(struct weston_output *output, struct weston_mode *target) { |
| 358 | struct weston_mode *local; |
| 359 | |
| 360 | wl_list_for_each(local, &output->mode_list, link) { |
| 361 | if((local->width == target->width) && (local->height == target->height)) |
| 362 | return local; |
| 363 | } |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | static int |
| 368 | rdp_switch_mode(struct weston_output *output, struct weston_mode *target_mode) { |
| 369 | struct rdp_output *rdpOutput = container_of(output, struct rdp_output, base); |
| 370 | struct rdp_peers_item *rdpPeer; |
| 371 | rdpSettings *settings; |
| 372 | pixman_image_t *new_shadow_buffer; |
| 373 | struct weston_mode *local_mode; |
| 374 | |
| 375 | local_mode = find_matching_mode(output, target_mode); |
| 376 | if(!local_mode) { |
| 377 | weston_log("mode %dx%d not available\n", target_mode->width, target_mode->height); |
| 378 | return -ENOENT; |
| 379 | } |
| 380 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 381 | if(local_mode == output->current_mode) |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 382 | return 0; |
| 383 | |
Hardening | 57388e4 | 2013-09-18 23:56:36 +0200 | [diff] [blame] | 384 | output->current_mode->flags &= ~WL_OUTPUT_MODE_CURRENT; |
| 385 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 386 | output->current_mode = local_mode; |
Hardening | 57388e4 | 2013-09-18 23:56:36 +0200 | [diff] [blame] | 387 | output->current_mode->flags |= WL_OUTPUT_MODE_CURRENT; |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 388 | |
| 389 | pixman_renderer_output_destroy(output); |
| 390 | pixman_renderer_output_create(output); |
| 391 | |
| 392 | new_shadow_buffer = pixman_image_create_bits(PIXMAN_x8r8g8b8, target_mode->width, |
| 393 | target_mode->height, 0, target_mode->width * 4); |
| 394 | pixman_image_composite32(PIXMAN_OP_SRC, rdpOutput->shadow_surface, 0, new_shadow_buffer, |
| 395 | 0, 0, 0, 0, 0, 0, target_mode->width, target_mode->height); |
| 396 | pixman_image_unref(rdpOutput->shadow_surface); |
| 397 | rdpOutput->shadow_surface = new_shadow_buffer; |
| 398 | |
| 399 | wl_list_for_each(rdpPeer, &rdpOutput->peers, link) { |
| 400 | settings = rdpPeer->peer->settings; |
| 401 | if(!settings->DesktopResize) { |
| 402 | /* too bad this peer does not support desktop resize */ |
| 403 | rdpPeer->peer->Close(rdpPeer->peer); |
| 404 | } else { |
| 405 | settings->DesktopWidth = target_mode->width; |
| 406 | settings->DesktopHeight = target_mode->height; |
| 407 | rdpPeer->peer->update->DesktopResize(rdpPeer->peer->context); |
| 408 | } |
| 409 | } |
| 410 | return 0; |
| 411 | } |
| 412 | |
| 413 | static int |
| 414 | parse_extra_modes(const char *modes_str, struct rdp_output *output) { |
| 415 | const char *startAt = modes_str; |
| 416 | const char *nextPos; |
| 417 | int w, h; |
| 418 | struct weston_mode *mode; |
| 419 | |
| 420 | while(startAt && *startAt) { |
| 421 | nextPos = strchr(startAt, 'x'); |
| 422 | if(!nextPos) |
| 423 | return -1; |
| 424 | |
| 425 | w = strtoul(startAt, NULL, 0); |
| 426 | startAt = nextPos + 1; |
| 427 | if(!*startAt) |
| 428 | return -1; |
| 429 | |
| 430 | h = strtoul(startAt, NULL, 0); |
| 431 | |
| 432 | if(!w || (w > 3000) || !h || (h > 3000)) |
| 433 | return -1; |
| 434 | mode = malloc(sizeof *mode); |
| 435 | if(!mode) |
| 436 | return -1; |
| 437 | |
| 438 | mode->width = w; |
| 439 | mode->height = h; |
| 440 | mode->refresh = 5; |
| 441 | mode->flags = 0; |
| 442 | wl_list_insert(&output->base.mode_list, &mode->link); |
| 443 | |
| 444 | startAt = strchr(startAt, ','); |
| 445 | if(startAt && *startAt == ',') |
| 446 | startAt++; |
| 447 | } |
| 448 | return 0; |
| 449 | } |
| 450 | static int |
| 451 | rdp_compositor_create_output(struct rdp_compositor *c, int width, int height, |
| 452 | const char *extraModes) |
| 453 | { |
| 454 | struct rdp_output *output; |
| 455 | struct wl_event_loop *loop; |
| 456 | struct weston_mode *currentMode, *next; |
| 457 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 458 | output = zalloc(sizeof *output); |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 459 | if (output == NULL) |
| 460 | return -1; |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 461 | |
| 462 | wl_list_init(&output->peers); |
| 463 | wl_list_init(&output->base.mode_list); |
| 464 | |
| 465 | currentMode = malloc(sizeof *currentMode); |
| 466 | if(!currentMode) |
| 467 | goto out_free_output; |
| 468 | currentMode->flags = WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 469 | currentMode->width = width; |
| 470 | currentMode->height = height; |
| 471 | currentMode->refresh = 5; |
| 472 | wl_list_insert(&output->base.mode_list, ¤tMode->link); |
| 473 | |
| 474 | if(parse_extra_modes(extraModes, output) < 0) { |
| 475 | weston_log("invalid extra modes\n"); |
| 476 | goto out_free_output_and_modes; |
| 477 | } |
| 478 | |
Hardening | 57388e4 | 2013-09-18 23:56:36 +0200 | [diff] [blame] | 479 | output->base.current_mode = output->base.native_mode = currentMode; |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 480 | weston_output_init(&output->base, &c->base, 0, 0, width, height, |
Kristian Høgsberg | 785e1f3 | 2013-05-22 21:53:09 -0400 | [diff] [blame] | 481 | WL_OUTPUT_TRANSFORM_NORMAL, 1); |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 482 | |
| 483 | output->base.make = "weston"; |
| 484 | output->base.model = "rdp"; |
| 485 | output->shadow_surface = pixman_image_create_bits(PIXMAN_x8r8g8b8, |
| 486 | width, height, |
| 487 | NULL, |
| 488 | width * 4); |
| 489 | if (output->shadow_surface == NULL) { |
| 490 | weston_log("Failed to create surface for frame buffer.\n"); |
| 491 | goto out_output; |
| 492 | } |
| 493 | |
| 494 | if (pixman_renderer_output_create(&output->base) < 0) |
| 495 | goto out_shadow_surface; |
| 496 | |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 497 | loop = wl_display_get_event_loop(c->base.wl_display); |
| 498 | output->finish_frame_timer = wl_event_loop_add_timer(loop, finish_frame_handler, output); |
| 499 | |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 500 | output->base.start_repaint_loop = rdp_output_start_repaint_loop; |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 501 | output->base.repaint = rdp_output_repaint; |
| 502 | output->base.destroy = rdp_output_destroy; |
| 503 | output->base.assign_planes = NULL; |
| 504 | output->base.set_backlight = NULL; |
| 505 | output->base.set_dpms = NULL; |
| 506 | output->base.switch_mode = rdp_switch_mode; |
| 507 | c->output = output; |
| 508 | |
| 509 | wl_list_insert(c->base.output_list.prev, &output->base.link); |
| 510 | return 0; |
| 511 | |
| 512 | out_shadow_surface: |
| 513 | pixman_image_unref(output->shadow_surface); |
| 514 | out_output: |
| 515 | weston_output_destroy(&output->base); |
| 516 | out_free_output_and_modes: |
| 517 | wl_list_for_each_safe(currentMode, next, &output->base.mode_list, link) |
| 518 | free(currentMode); |
| 519 | out_free_output: |
| 520 | free(output); |
| 521 | return -1; |
| 522 | } |
| 523 | |
| 524 | static void |
| 525 | rdp_restore(struct weston_compositor *ec) |
| 526 | { |
| 527 | } |
| 528 | |
| 529 | static void |
| 530 | rdp_destroy(struct weston_compositor *ec) |
| 531 | { |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 532 | weston_compositor_shutdown(ec); |
| 533 | |
| 534 | free(ec); |
| 535 | } |
| 536 | |
| 537 | static |
| 538 | int rdp_listener_activity(int fd, uint32_t mask, void *data) { |
| 539 | freerdp_listener* instance = (freerdp_listener *)data; |
| 540 | |
| 541 | if (!(mask & WL_EVENT_READABLE)) |
| 542 | return 0; |
| 543 | if (!instance->CheckFileDescriptor(instance)) |
| 544 | { |
| 545 | weston_log("failed to check FreeRDP file descriptor\n"); |
| 546 | return -1; |
| 547 | } |
| 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | static |
| 552 | int rdp_implant_listener(struct rdp_compositor *c, freerdp_listener* instance) { |
| 553 | int i, fd; |
| 554 | int rcount = 0; |
| 555 | void* rfds[MAX_FREERDP_FDS]; |
| 556 | struct wl_event_loop *loop; |
| 557 | |
| 558 | if (!instance->GetFileDescriptor(instance, rfds, &rcount)) { |
| 559 | weston_log("Failed to get FreeRDP file descriptor\n"); |
| 560 | return -1; |
| 561 | } |
| 562 | |
| 563 | loop = wl_display_get_event_loop(c->base.wl_display); |
| 564 | for (i = 0; i < rcount; i++) { |
| 565 | fd = (int)(long)(rfds[i]); |
| 566 | c->listener_events[i] = wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE, |
| 567 | rdp_listener_activity, instance); |
| 568 | } |
| 569 | |
| 570 | for( ; i < MAX_FREERDP_FDS; i++) |
| 571 | c->listener_events[i] = 0; |
| 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | |
| 576 | static void |
| 577 | rdp_peer_context_new(freerdp_peer* client, RdpPeerContext* context) |
| 578 | { |
| 579 | context->item.peer = client; |
Hardening | 58bcc36 | 2013-08-17 00:30:31 +0200 | [diff] [blame] | 580 | context->item.flags = RDP_PEER_OUTPUT_ENABLED; |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 581 | |
Hardening | 04633b6 | 2014-01-10 11:33:06 +0100 | [diff] [blame^] | 582 | #if FREERDP_VERSION_MAJOR == 1 && FREERDP_VERSION_MINOR == 1 |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 583 | context->rfx_context = rfx_context_new(); |
Hardening | 04633b6 | 2014-01-10 11:33:06 +0100 | [diff] [blame^] | 584 | #else |
| 585 | context->rfx_context = rfx_context_new(TRUE); |
| 586 | #endif |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 587 | context->rfx_context->mode = RLGR3; |
| 588 | context->rfx_context->width = client->settings->DesktopWidth; |
| 589 | context->rfx_context->height = client->settings->DesktopHeight; |
| 590 | rfx_context_set_pixel_format(context->rfx_context, RDP_PIXEL_FORMAT_B8G8R8A8); |
| 591 | |
| 592 | context->nsc_context = nsc_context_new(); |
Hardening | 827358e | 2013-05-22 23:40:18 +0200 | [diff] [blame] | 593 | nsc_context_set_pixel_format(context->nsc_context, RDP_PIXEL_FORMAT_B8G8R8A8); |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 594 | |
Hardening | a1ce6cd | 2013-05-22 23:40:16 +0200 | [diff] [blame] | 595 | context->encode_stream = Stream_New(NULL, 65536); |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | static void |
| 599 | rdp_peer_context_free(freerdp_peer* client, RdpPeerContext* context) |
| 600 | { |
| 601 | int i; |
| 602 | if(!context) |
| 603 | return; |
| 604 | |
| 605 | wl_list_remove(&context->item.link); |
| 606 | for(i = 0; i < MAX_FREERDP_FDS; i++) { |
Hardening | b8f03aa | 2013-05-26 23:34:00 +0200 | [diff] [blame] | 607 | if (context->events[i]) |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 608 | wl_event_source_remove(context->events[i]); |
| 609 | } |
| 610 | |
Hardening | fb8546e | 2013-12-21 23:19:11 +0100 | [diff] [blame] | 611 | if(context->item.flags & RDP_PEER_ACTIVATED) { |
| 612 | weston_seat_release_keyboard(&context->item.seat); |
| 613 | weston_seat_release_pointer(&context->item.seat); |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 614 | weston_seat_release(&context->item.seat); |
Hardening | fb8546e | 2013-12-21 23:19:11 +0100 | [diff] [blame] | 615 | } |
Hardening | a1ce6cd | 2013-05-22 23:40:16 +0200 | [diff] [blame] | 616 | Stream_Free(context->encode_stream, TRUE); |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 617 | nsc_context_free(context->nsc_context); |
| 618 | rfx_context_free(context->rfx_context); |
| 619 | free(context->rfx_rects); |
| 620 | } |
| 621 | |
| 622 | |
| 623 | static int |
| 624 | rdp_client_activity(int fd, uint32_t mask, void *data) { |
| 625 | freerdp_peer* client = (freerdp_peer *)data; |
| 626 | |
| 627 | if (!client->CheckFileDescriptor(client)) { |
| 628 | weston_log("unable to checkDescriptor for %p\n", client); |
| 629 | goto out_clean; |
| 630 | } |
| 631 | return 0; |
| 632 | |
| 633 | out_clean: |
| 634 | freerdp_peer_context_free(client); |
| 635 | freerdp_peer_free(client); |
| 636 | return 0; |
| 637 | } |
| 638 | |
| 639 | static BOOL |
| 640 | xf_peer_capabilities(freerdp_peer* client) |
| 641 | { |
| 642 | return TRUE; |
| 643 | } |
| 644 | |
Hardening | 4a3c150 | 2013-04-06 23:39:26 +0200 | [diff] [blame] | 645 | |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 646 | struct rdp_to_xkb_keyboard_layout { |
Hardening | 4a3c150 | 2013-04-06 23:39:26 +0200 | [diff] [blame] | 647 | UINT32 rdpLayoutCode; |
| 648 | char *xkbLayout; |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 649 | }; |
| 650 | |
| 651 | /* picked from http://technet.microsoft.com/en-us/library/cc766503(WS.10).aspx */ |
| 652 | static struct rdp_to_xkb_keyboard_layout rdp_keyboards[] = { |
| 653 | {0x00000406, "dk"}, |
| 654 | {0x00000407, "de"}, |
| 655 | {0x00000409, "us"}, |
| 656 | {0x0000040c, "fr"}, |
| 657 | {0x00000410, "it"}, |
| 658 | {0x00000813, "be"}, |
| 659 | {0x00000000, 0}, |
| 660 | }; |
| 661 | |
| 662 | /* taken from 2.2.7.1.6 Input Capability Set (TS_INPUT_CAPABILITYSET) */ |
| 663 | static char *rdp_keyboard_types[] = { |
Hardening | 4a3c150 | 2013-04-06 23:39:26 +0200 | [diff] [blame] | 664 | "", /* 0: unused */ |
| 665 | "", /* 1: IBM PC/XT or compatible (83-key) keyboard */ |
| 666 | "", /* 2: Olivetti "ICO" (102-key) keyboard */ |
| 667 | "", /* 3: IBM PC/AT (84-key) or similar keyboard */ |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 668 | "pc102",/* 4: IBM enhanced (101- or 102-key) keyboard */ |
Hardening | 4a3c150 | 2013-04-06 23:39:26 +0200 | [diff] [blame] | 669 | "", /* 5: Nokia 1050 and similar keyboards */ |
| 670 | "", /* 6: Nokia 9140 and similar keyboards */ |
| 671 | "" /* 7: Japanese keyboard */ |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 672 | }; |
| 673 | |
| 674 | static BOOL |
| 675 | xf_peer_post_connect(freerdp_peer* client) |
| 676 | { |
| 677 | RdpPeerContext *peerCtx; |
| 678 | struct rdp_compositor *c; |
| 679 | struct rdp_output *output; |
| 680 | rdpSettings *settings; |
Hardening | c39118b | 2013-05-22 23:40:19 +0200 | [diff] [blame] | 681 | rdpPointerUpdate *pointer; |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 682 | struct xkb_context *xkbContext; |
| 683 | struct xkb_rule_names xkbRuleNames; |
| 684 | struct xkb_keymap *keymap; |
| 685 | int i; |
Hardening | c39118b | 2013-05-22 23:40:19 +0200 | [diff] [blame] | 686 | pixman_box32_t box; |
| 687 | pixman_region32_t damage; |
| 688 | |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 689 | |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 690 | peerCtx = (RdpPeerContext *)client->context; |
| 691 | c = peerCtx->rdpCompositor; |
| 692 | output = c->output; |
| 693 | settings = client->settings; |
| 694 | |
Hardening | 57388e4 | 2013-09-18 23:56:36 +0200 | [diff] [blame] | 695 | if (!settings->SurfaceCommandsEnabled) { |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 696 | weston_log("client doesn't support required SurfaceCommands\n"); |
| 697 | return FALSE; |
| 698 | } |
| 699 | |
Hardening | 57388e4 | 2013-09-18 23:56:36 +0200 | [diff] [blame] | 700 | if (output->base.width != (int)settings->DesktopWidth || |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 701 | output->base.height != (int)settings->DesktopHeight) |
| 702 | { |
Hardening | 57388e4 | 2013-09-18 23:56:36 +0200 | [diff] [blame] | 703 | struct weston_mode new_mode; |
| 704 | struct weston_mode *target_mode; |
| 705 | new_mode.width = (int)settings->DesktopWidth; |
| 706 | new_mode.height = (int)settings->DesktopHeight; |
| 707 | target_mode = find_matching_mode(&output->base, &new_mode); |
| 708 | if (!target_mode) { |
| 709 | weston_log("client mode not found\n"); |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 710 | return FALSE; |
| 711 | } |
Hardening | 57388e4 | 2013-09-18 23:56:36 +0200 | [diff] [blame] | 712 | weston_output_switch_mode(&output->base, target_mode, 1, WESTON_MODE_SWITCH_SET_NATIVE); |
| 713 | output->base.width = new_mode.width; |
| 714 | output->base.height = new_mode.height; |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | weston_log("kbd_layout:%x kbd_type:%x kbd_subType:%x kbd_functionKeys:%x\n", |
| 718 | settings->KeyboardLayout, settings->KeyboardType, settings->KeyboardSubType, |
| 719 | settings->KeyboardFunctionKey); |
| 720 | |
| 721 | memset(&xkbRuleNames, 0, sizeof(xkbRuleNames)); |
| 722 | if(settings->KeyboardType <= 7) |
| 723 | xkbRuleNames.model = rdp_keyboard_types[settings->KeyboardType]; |
| 724 | for(i = 0; rdp_keyboards[i].xkbLayout; i++) { |
| 725 | if(rdp_keyboards[i].rdpLayoutCode == settings->KeyboardLayout) { |
| 726 | xkbRuleNames.layout = rdp_keyboards[i].xkbLayout; |
| 727 | break; |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | keymap = NULL; |
| 732 | if(xkbRuleNames.layout) { |
| 733 | xkbContext = xkb_context_new(0); |
| 734 | if(!xkbContext) { |
| 735 | weston_log("unable to create a xkb_context\n"); |
| 736 | return FALSE; |
| 737 | } |
| 738 | |
| 739 | keymap = xkb_keymap_new_from_names(xkbContext, &xkbRuleNames, 0); |
| 740 | } |
| 741 | weston_seat_init_keyboard(&peerCtx->item.seat, keymap); |
| 742 | weston_seat_init_pointer(&peerCtx->item.seat); |
| 743 | |
| 744 | peerCtx->item.flags |= RDP_PEER_ACTIVATED; |
Hardening | c39118b | 2013-05-22 23:40:19 +0200 | [diff] [blame] | 745 | |
| 746 | /* disable pointer on the client side */ |
| 747 | pointer = client->update->pointer; |
| 748 | pointer->pointer_system.type = SYSPTR_NULL; |
| 749 | pointer->PointerSystem(client->context, &pointer->pointer_system); |
| 750 | |
| 751 | /* sends a full refresh */ |
| 752 | box.x1 = 0; |
| 753 | box.y1 = 0; |
| 754 | box.x2 = output->base.width; |
| 755 | box.y2 = output->base.height; |
| 756 | pixman_region32_init_with_extents(&damage, &box); |
| 757 | |
| 758 | rdp_peer_refresh_region(&damage, client); |
| 759 | |
| 760 | pixman_region32_fini(&damage); |
| 761 | |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 762 | return TRUE; |
| 763 | } |
| 764 | |
| 765 | static BOOL |
| 766 | xf_peer_activate(freerdp_peer *client) |
| 767 | { |
Hardening | 827358e | 2013-05-22 23:40:18 +0200 | [diff] [blame] | 768 | RdpPeerContext *context = (RdpPeerContext *)client->context; |
| 769 | rfx_context_reset(context->rfx_context); |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 770 | return TRUE; |
| 771 | } |
| 772 | |
| 773 | static void |
| 774 | xf_mouseEvent(rdpInput *input, UINT16 flags, UINT16 x, UINT16 y) { |
Hardening | b60e46f | 2013-06-03 22:55:47 +0200 | [diff] [blame] | 775 | wl_fixed_t wl_x, wl_y, axis; |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 776 | RdpPeerContext *peerContext = (RdpPeerContext *)input->context; |
| 777 | struct rdp_output *output; |
| 778 | uint32_t button = 0; |
| 779 | |
| 780 | if (flags & PTR_FLAGS_MOVE) { |
| 781 | output = peerContext->rdpCompositor->output; |
| 782 | if(x < output->base.width && y < output->base.height) { |
| 783 | wl_x = wl_fixed_from_int((int)x); |
| 784 | wl_y = wl_fixed_from_int((int)y); |
| 785 | notify_motion_absolute(&peerContext->item.seat, weston_compositor_get_time(), |
| 786 | wl_x, wl_y); |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | if (flags & PTR_FLAGS_BUTTON1) |
| 791 | button = BTN_LEFT; |
| 792 | else if (flags & PTR_FLAGS_BUTTON2) |
| 793 | button = BTN_RIGHT; |
| 794 | else if (flags & PTR_FLAGS_BUTTON3) |
| 795 | button = BTN_MIDDLE; |
| 796 | |
| 797 | if(button) { |
| 798 | notify_button(&peerContext->item.seat, weston_compositor_get_time(), button, |
| 799 | (flags & PTR_FLAGS_DOWN) ? WL_POINTER_BUTTON_STATE_PRESSED : WL_POINTER_BUTTON_STATE_RELEASED |
| 800 | ); |
| 801 | } |
Hardening | b60e46f | 2013-06-03 22:55:47 +0200 | [diff] [blame] | 802 | |
| 803 | if (flags & PTR_FLAGS_WHEEL) { |
| 804 | /* DEFAULT_AXIS_STEP_DISTANCE is stolen from compositor-x11.c |
| 805 | * The RDP specs says the lower bits of flags contains the "the number of rotation |
| 806 | * units the mouse wheel was rotated". |
| 807 | * |
| 808 | * http://blogs.msdn.com/b/oldnewthing/archive/2013/01/23/10387366.aspx explains the 120 value |
| 809 | */ |
| 810 | axis = (DEFAULT_AXIS_STEP_DISTANCE * (flags & 0xff)) / 120; |
| 811 | if (flags & PTR_FLAGS_WHEEL_NEGATIVE) |
| 812 | axis = -axis; |
| 813 | |
| 814 | notify_axis(&peerContext->item.seat, weston_compositor_get_time(), |
| 815 | WL_POINTER_AXIS_VERTICAL_SCROLL, |
| 816 | axis); |
| 817 | } |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | static void |
| 821 | xf_extendedMouseEvent(rdpInput *input, UINT16 flags, UINT16 x, UINT16 y) { |
| 822 | wl_fixed_t wl_x, wl_y; |
| 823 | RdpPeerContext *peerContext = (RdpPeerContext *)input->context; |
| 824 | struct rdp_output *output; |
| 825 | |
| 826 | output = peerContext->rdpCompositor->output; |
| 827 | if(x < output->base.width && y < output->base.height) { |
| 828 | wl_x = wl_fixed_from_int((int)x); |
| 829 | wl_y = wl_fixed_from_int((int)y); |
| 830 | notify_motion_absolute(&peerContext->item.seat, weston_compositor_get_time(), |
| 831 | wl_x, wl_y); |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | |
| 836 | static void |
| 837 | xf_input_synchronize_event(rdpInput *input, UINT32 flags) |
| 838 | { |
| 839 | freerdp_peer *client = input->context->peer; |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 840 | RdpPeerContext *peerCtx = (RdpPeerContext *)input->context; |
| 841 | struct rdp_output *output = peerCtx->rdpCompositor->output; |
| 842 | pixman_box32_t box; |
| 843 | pixman_region32_t damage; |
| 844 | |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 845 | /* sends a full refresh */ |
| 846 | box.x1 = 0; |
| 847 | box.y1 = 0; |
| 848 | box.x2 = output->base.width; |
| 849 | box.y2 = output->base.height; |
| 850 | pixman_region32_init_with_extents(&damage, &box); |
| 851 | |
| 852 | rdp_peer_refresh_region(&damage, client); |
| 853 | |
| 854 | pixman_region32_fini(&damage); |
| 855 | } |
| 856 | |
Hardening | 4a3c150 | 2013-04-06 23:39:26 +0200 | [diff] [blame] | 857 | extern DWORD KEYCODE_TO_VKCODE_EVDEV[]; |
| 858 | static uint32_t vk_to_keycode[256]; |
| 859 | static void |
| 860 | init_vk_translator(void) |
| 861 | { |
| 862 | int i; |
| 863 | |
| 864 | memset(vk_to_keycode, 0, sizeof(vk_to_keycode)); |
| 865 | for(i = 0; i < 256; i++) |
| 866 | vk_to_keycode[KEYCODE_TO_VKCODE_EVDEV[i] & 0xff] = i-8; |
| 867 | } |
| 868 | |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 869 | static void |
| 870 | xf_input_keyboard_event(rdpInput *input, UINT16 flags, UINT16 code) |
| 871 | { |
Hardening | 4a3c150 | 2013-04-06 23:39:26 +0200 | [diff] [blame] | 872 | uint32_t scan_code, vk_code, full_code; |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 873 | enum wl_keyboard_key_state keyState; |
| 874 | RdpPeerContext *peerContext = (RdpPeerContext *)input->context; |
| 875 | int notify = 0; |
| 876 | |
| 877 | if (flags & KBD_FLAGS_DOWN) { |
| 878 | keyState = WL_KEYBOARD_KEY_STATE_PRESSED; |
| 879 | notify = 1; |
| 880 | } else if (flags & KBD_FLAGS_RELEASE) { |
| 881 | keyState = WL_KEYBOARD_KEY_STATE_RELEASED; |
| 882 | notify = 1; |
| 883 | } |
| 884 | |
Hardening | 4a3c150 | 2013-04-06 23:39:26 +0200 | [diff] [blame] | 885 | if(notify) { |
| 886 | full_code = code; |
| 887 | if(flags & KBD_FLAGS_EXTENDED) |
| 888 | full_code |= KBD_FLAGS_EXTENDED; |
| 889 | |
| 890 | vk_code = GetVirtualKeyCodeFromVirtualScanCode(full_code, 4); |
| 891 | if(vk_code > 0xff) { |
| 892 | weston_log("invalid vk_code %x", vk_code); |
| 893 | return; |
| 894 | } |
| 895 | scan_code = vk_to_keycode[vk_code]; |
| 896 | |
| 897 | |
| 898 | /*weston_log("code=%x ext=%d vk_code=%x scan_code=%x\n", code, (flags & KBD_FLAGS_EXTENDED) ? 1 : 0, |
| 899 | vk_code, scan_code);*/ |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 900 | notify_key(&peerContext->item.seat, weston_compositor_get_time(), |
Hardening | 4a3c150 | 2013-04-06 23:39:26 +0200 | [diff] [blame] | 901 | scan_code, keyState, STATE_UPDATE_AUTOMATIC); |
| 902 | } |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | static void |
| 906 | xf_input_unicode_keyboard_event(rdpInput *input, UINT16 flags, UINT16 code) |
| 907 | { |
| 908 | weston_log("Client sent a unicode keyboard event (flags:0x%X code:0x%X)\n", flags, code); |
| 909 | } |
| 910 | |
| 911 | |
| 912 | static void |
| 913 | xf_suppress_output(rdpContext *context, BYTE allow, RECTANGLE_16 *area) { |
| 914 | RdpPeerContext *peerContext = (RdpPeerContext *)context; |
| 915 | if(allow) |
| 916 | peerContext->item.flags |= RDP_PEER_OUTPUT_ENABLED; |
| 917 | else |
| 918 | peerContext->item.flags &= (~RDP_PEER_OUTPUT_ENABLED); |
| 919 | } |
| 920 | |
| 921 | static int |
| 922 | rdp_peer_init(freerdp_peer *client, struct rdp_compositor *c) |
| 923 | { |
| 924 | int rcount = 0; |
| 925 | void *rfds[MAX_FREERDP_FDS]; |
| 926 | int i, fd; |
| 927 | struct wl_event_loop *loop; |
| 928 | rdpSettings *settings; |
| 929 | rdpInput *input; |
| 930 | RdpPeerContext *peerCtx; |
Rob Bradford | 9af5f9e | 2013-05-31 18:09:50 +0100 | [diff] [blame] | 931 | char seat_name[32]; |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 932 | |
Hardening | fe107f3 | 2013-07-08 00:51:34 +0200 | [diff] [blame] | 933 | client->ContextSize = sizeof(RdpPeerContext); |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 934 | client->ContextNew = (psPeerContextNew)rdp_peer_context_new; |
| 935 | client->ContextFree = (psPeerContextFree)rdp_peer_context_free; |
| 936 | freerdp_peer_context_new(client); |
| 937 | |
| 938 | peerCtx = (RdpPeerContext *) client->context; |
| 939 | peerCtx->rdpCompositor = c; |
| 940 | |
| 941 | settings = client->settings; |
| 942 | settings->RdpKeyFile = c->rdp_key; |
| 943 | if(c->tls_enabled) { |
| 944 | settings->CertificateFile = c->server_cert; |
| 945 | settings->PrivateKeyFile = c->server_key; |
| 946 | } else { |
| 947 | settings->TlsSecurity = FALSE; |
| 948 | } |
| 949 | |
| 950 | settings->NlaSecurity = FALSE; |
| 951 | |
| 952 | client->Capabilities = xf_peer_capabilities; |
| 953 | client->PostConnect = xf_peer_post_connect; |
| 954 | client->Activate = xf_peer_activate; |
| 955 | |
| 956 | client->update->SuppressOutput = xf_suppress_output; |
| 957 | |
| 958 | input = client->input; |
| 959 | input->SynchronizeEvent = xf_input_synchronize_event; |
| 960 | input->MouseEvent = xf_mouseEvent; |
| 961 | input->ExtendedMouseEvent = xf_extendedMouseEvent; |
| 962 | input->KeyboardEvent = xf_input_keyboard_event; |
| 963 | input->UnicodeKeyboardEvent = xf_input_unicode_keyboard_event; |
Rob Bradford | 9af5f9e | 2013-05-31 18:09:50 +0100 | [diff] [blame] | 964 | |
| 965 | if (snprintf(seat_name, 32, "rdp:%d:%s", client->sockfd, client->hostname) >= 32) |
| 966 | seat_name[31] = '\0'; |
| 967 | |
| 968 | weston_seat_init(&peerCtx->item.seat, &c->base, seat_name); |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 969 | |
| 970 | client->Initialize(client); |
| 971 | |
| 972 | if (!client->GetFileDescriptor(client, rfds, &rcount)) { |
| 973 | weston_log("unable to retrieve client fds\n"); |
| 974 | return -1; |
| 975 | } |
| 976 | |
| 977 | loop = wl_display_get_event_loop(c->base.wl_display); |
| 978 | for(i = 0; i < rcount; i++) { |
| 979 | fd = (int)(long)(rfds[i]); |
| 980 | |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 981 | peerCtx->events[i] = wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE, |
| 982 | rdp_client_activity, client); |
| 983 | } |
Hardening | b8f03aa | 2013-05-26 23:34:00 +0200 | [diff] [blame] | 984 | for ( ; i < MAX_FREERDP_FDS; i++) |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 985 | peerCtx->events[i] = 0; |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 986 | |
| 987 | wl_list_insert(&c->output->peers, &peerCtx->item.link); |
| 988 | return 0; |
| 989 | } |
| 990 | |
| 991 | |
| 992 | static void |
| 993 | rdp_incoming_peer(freerdp_listener *instance, freerdp_peer *client) |
| 994 | { |
| 995 | struct rdp_compositor *c = (struct rdp_compositor *)instance->param4; |
| 996 | if (rdp_peer_init(client, c) < 0) |
| 997 | return; |
| 998 | } |
| 999 | |
| 1000 | static struct weston_compositor * |
| 1001 | rdp_compositor_create(struct wl_display *display, |
| 1002 | struct rdp_compositor_config *config, |
Hardening | 6a4e8c6 | 2013-05-27 23:13:45 +0200 | [diff] [blame] | 1003 | int *argc, char *argv[], struct weston_config *wconfig) |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 1004 | { |
| 1005 | struct rdp_compositor *c; |
| 1006 | char *fd_str; |
| 1007 | int fd; |
| 1008 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 1009 | c = zalloc(sizeof *c); |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 1010 | if (c == NULL) |
| 1011 | return NULL; |
| 1012 | |
Hardening | 6a4e8c6 | 2013-05-27 23:13:45 +0200 | [diff] [blame] | 1013 | if (weston_compositor_init(&c->base, display, argc, argv, wconfig) < 0) |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 1014 | goto err_free; |
| 1015 | |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 1016 | c->base.destroy = rdp_destroy; |
| 1017 | c->base.restore = rdp_restore; |
| 1018 | c->rdp_key = config->rdp_key ? strdup(config->rdp_key) : NULL; |
| 1019 | |
| 1020 | /* activate TLS only if certificate/key are available */ |
| 1021 | if(config->server_cert && config->server_key) { |
| 1022 | weston_log("TLS support activated\n"); |
| 1023 | c->server_cert = strdup(config->server_cert); |
| 1024 | c->server_key = strdup(config->server_key); |
| 1025 | if(!c->server_cert || !c->server_key) |
| 1026 | goto err_free_strings; |
| 1027 | c->tls_enabled = 1; |
| 1028 | } |
| 1029 | |
| 1030 | if (pixman_renderer_init(&c->base) < 0) |
| 1031 | goto err_compositor; |
| 1032 | |
| 1033 | if (rdp_compositor_create_output(c, config->width, config->height, config->extra_modes) < 0) |
| 1034 | goto err_compositor; |
| 1035 | |
| 1036 | if(!config->env_socket) { |
| 1037 | c->listener = freerdp_listener_new(); |
| 1038 | c->listener->PeerAccepted = rdp_incoming_peer; |
| 1039 | c->listener->param4 = c; |
| 1040 | if(!c->listener->Open(c->listener, config->bind_address, config->port)) { |
| 1041 | weston_log("unable to bind rdp socket\n"); |
| 1042 | goto err_listener; |
| 1043 | } |
| 1044 | |
| 1045 | if (rdp_implant_listener(c, c->listener) < 0) |
| 1046 | goto err_compositor; |
| 1047 | } else { |
| 1048 | /* get the socket from RDP_FD var */ |
| 1049 | fd_str = getenv("RDP_FD"); |
| 1050 | if(!fd_str) { |
| 1051 | weston_log("RDP_FD env variable not set"); |
| 1052 | goto err_output; |
| 1053 | } |
| 1054 | |
| 1055 | fd = strtoul(fd_str, NULL, 10); |
| 1056 | if(rdp_peer_init(freerdp_peer_new(fd), c)) |
| 1057 | goto err_output; |
| 1058 | } |
| 1059 | |
| 1060 | return &c->base; |
| 1061 | |
| 1062 | err_listener: |
| 1063 | freerdp_listener_free(c->listener); |
| 1064 | err_output: |
| 1065 | weston_output_destroy(&c->output->base); |
| 1066 | err_compositor: |
| 1067 | weston_compositor_shutdown(&c->base); |
| 1068 | err_free_strings: |
| 1069 | if(c->rdp_key) |
| 1070 | free(c->rdp_key); |
| 1071 | if(c->server_cert) |
| 1072 | free(c->server_cert); |
| 1073 | if(c->server_key) |
| 1074 | free(c->server_key); |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 1075 | err_free: |
| 1076 | free(c); |
| 1077 | return NULL; |
| 1078 | } |
| 1079 | |
| 1080 | WL_EXPORT struct weston_compositor * |
| 1081 | backend_init(struct wl_display *display, int *argc, char *argv[], |
Hardening | 6a4e8c6 | 2013-05-27 23:13:45 +0200 | [diff] [blame] | 1082 | struct weston_config *wconfig) |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 1083 | { |
| 1084 | struct rdp_compositor_config config; |
| 1085 | rdp_compositor_config_init(&config); |
| 1086 | int major, minor, revision; |
| 1087 | |
| 1088 | freerdp_get_version(&major, &minor, &revision); |
| 1089 | weston_log("using FreeRDP version %d.%d.%d\n", major, minor, revision); |
Hardening | 4a3c150 | 2013-04-06 23:39:26 +0200 | [diff] [blame] | 1090 | init_vk_translator(); |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 1091 | |
| 1092 | const struct weston_option rdp_options[] = { |
| 1093 | { WESTON_OPTION_BOOLEAN, "env-socket", 0, &config.env_socket }, |
| 1094 | { WESTON_OPTION_INTEGER, "width", 0, &config.width }, |
| 1095 | { WESTON_OPTION_INTEGER, "height", 0, &config.height }, |
| 1096 | { WESTON_OPTION_STRING, "extra-modes", 0, &config.extra_modes }, |
| 1097 | { WESTON_OPTION_STRING, "address", 0, &config.bind_address }, |
| 1098 | { WESTON_OPTION_INTEGER, "port", 0, &config.port }, |
| 1099 | { WESTON_OPTION_STRING, "rdp4-key", 0, &config.rdp_key }, |
| 1100 | { WESTON_OPTION_STRING, "rdp-tls-cert", 0, &config.server_cert }, |
| 1101 | { WESTON_OPTION_STRING, "rdp-tls-key", 0, &config.server_key } |
| 1102 | }; |
| 1103 | |
| 1104 | parse_options(rdp_options, ARRAY_LENGTH(rdp_options), argc, argv); |
Hardening | 6a4e8c6 | 2013-05-27 23:13:45 +0200 | [diff] [blame] | 1105 | return rdp_compositor_create(display, &config, argc, argv, wconfig); |
Hardening | a83409c | 2013-04-01 23:43:58 +0200 | [diff] [blame] | 1106 | } |