de060ec33c7c78964d02a34e2ccdf79292efbc2f
[platform/core/uifw/e-mod-tizen-rdp.git] / src / e_mod_rdp.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include <unistd.h>
8 #include <fcntl.h>
9 #include <errno.h>
10
11 #include <assert.h>
12 #include <stdint.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <errno.h>
16 #include <linux/input.h>
17
18 #include <freerdp/version.h>
19 #include <freerdp/freerdp.h>
20 #include <freerdp/listener.h>
21 #include <freerdp/update.h>
22 #include <freerdp/input.h>
23 #include <freerdp/codec/color.h>
24 #include <freerdp/codec/rfx.h>
25 #include <freerdp/codec/nsc.h>
26 #include <freerdp/locale/keyboard.h>
27 #include <winpr/input.h>
28 #include <winpr/ssl.h>
29
30 #include <pixman.h>
31 #include <tbm_surface.h>
32 #include <tbm_surface_internal.h>
33
34 #include "e_mod_main.h"
35 #include "e_mod_rdp.h"
36
37 #define E_RDP_WIDTH 1280
38 #define E_RDP_HEIGHT 720
39 #define E_MOD_RDP_CONFIG_VERSION  2
40 #define E_MOD_RDP_NAME_LEN        64
41 #define MAX_FREERDP_FDS           10
42 #define DEFAULT_PIXEL_FORMAT PIXEL_FORMAT_BGRA32
43
44 enum peer_item_flags
45 {
46    RDP_PEER_ACTIVATED      = (1 << 0),
47    RDP_PEER_OUTPUT_ENABLED = (1 << 1),
48 };
49
50 typedef struct _E_Rdp_Output E_Rdp_Output;
51 typedef struct _E_Rdp_Backend E_Rdp_Backend;
52 typedef struct _E_Rdp_Peer_Context E_Rdp_Peer_Context;
53 typedef struct _E_Rdp_Peer_Item E_Rdp_Peer_Item;
54 typedef struct _E_Rdp_Peer_Context E_Rdp_Peer_Context;
55
56 static E_Rdp_Config_Data *g_rdp_config;
57 static E_Rdp_Backend *g_rdp_backend;
58 static unsigned int refresh_raw_frame_id;
59
60 struct _E_Rdp_Output
61 {
62    E_Output *primary_output;
63    Ecore_Timer *frame_timer;
64    pixman_image_t *pix_surface;
65    tbm_surface_h tbm_surface;
66
67    struct wl_list peers;
68 };
69
70 struct _E_Rdp_Backend
71 {
72    freerdp_listener *listener;
73    struct wl_event_source *listener_events[MAX_FREERDP_FDS];
74    E_Rdp_Output *output;
75
76    char *server_cert;
77    char *server_key;
78    char *rdp_key;
79    int tls_enabled;
80    int no_clients_resize;
81    int force_no_compression;
82 };
83
84 struct _E_Rdp_Peer_Item
85 {
86    int flags;
87    freerdp_peer *peer;
88
89    struct wl_list link;
90 };
91
92 struct _E_Rdp_Peer_Context
93 {
94    rdpContext _p;
95
96    E_Rdp_Backend *rdpBackend;
97    struct wl_event_source *events[MAX_FREERDP_FDS];
98    RFX_CONTEXT *rfx_context;
99    wStream *encode_stream;
100    RFX_RECT *rfx_rects;
101    NSC_CONTEXT *nsc_context;
102
103    E_Rdp_Peer_Item item;
104 };
105
106 struct rdp_to_xkb_keyboard_layout
107 {
108    UINT32 rdpLayoutCode;
109    const char *xkbLayout;
110    const char *xkbVariant;
111 };
112
113 static tbm_surface_h
114 _e_rdp_tbm_image_create(E_Rdp_Output *output, int w, int h, int color)
115 {
116    tbm_surface_h tbm_surface = NULL;
117
118    tbm_surface = tbm_surface_internal_create_with_flags(w, h, TBM_FORMAT_XRGB8888, TBM_BO_SCANOUT);
119    if (!tbm_surface)
120      return NULL;
121
122    return tbm_surface;
123 }
124
125 static pixman_format_code_t
126 _e_rdp_pixman_format_get(tbm_format format)
127 {
128    switch (format)
129      {
130       case TBM_FORMAT_ARGB8888:
131         return PIXMAN_a8r8g8b8;
132
133       case TBM_FORMAT_XRGB8888:
134         return PIXMAN_x8r8g8b8;
135
136       default:
137         return 0;
138      }
139
140    return 0;
141 }
142
143 static pixman_image_t *
144 _e_rdp_pixman_image_create(tbm_surface_h tbm_surface)
145 {
146    int err;
147    tbm_surface_info_s info;
148    pixman_image_t *pix_surface = NULL;
149    pixman_format_code_t pix_format = 0;
150
151    EINA_SAFETY_ON_NULL_RETURN_VAL(tbm_surface, NULL);
152
153    err = tbm_surface_map(tbm_surface, TBM_SURF_OPTION_WRITE, &info);
154    if (err)
155      return NULL;
156
157    pix_format = _e_rdp_pixman_format_get(info.format);
158    if (pix_format == 0)
159      {
160         ERR("not supported format");
161         goto out;
162      }
163
164    pix_surface = pixman_image_create_bits(pix_format, info.width, info.height, (uint32_t *)info.planes[0].ptr, info.planes[0].stride);
165    if (pix_surface == NULL)
166      ERR("create pixman image failed");
167
168 out:
169    tbm_surface_unmap(tbm_surface);
170    return pix_surface;
171 }
172
173 static void
174 e_rdp_peer_refresh_rfx(pixman_region32_t *damage, pixman_image_t *image, freerdp_peer *peer)
175 {
176    int width, height, nrects, i;
177    pixman_box32_t *region, *rects;
178    uint32_t *ptr;
179    RFX_RECT *rfxRect;
180    rdpUpdate *update = peer->update;
181    SURFACE_BITS_COMMAND cmd = { 0 };
182    E_Rdp_Peer_Context *context = (E_Rdp_Peer_Context *)peer->context;
183
184    Stream_Clear(context->encode_stream);
185    Stream_SetPosition(context->encode_stream, 0);
186
187    width = (damage->extents.x2 - damage->extents.x1);
188    height = (damage->extents.y2 - damage->extents.y1);
189
190    cmd.skipCompression = TRUE;
191    cmd.cmdType = CMDTYPE_STREAM_SURFACE_BITS;
192    cmd.destLeft = damage->extents.x1;
193    cmd.destTop = damage->extents.y1;
194    cmd.destRight = damage->extents.x2;
195    cmd.destBottom = damage->extents.y2;
196    cmd.bmp.bpp = 32;
197    cmd.bmp.codecID = peer->settings->RemoteFxCodecId;
198    cmd.bmp.width = width;
199    cmd.bmp.height = height;
200
201    ptr = pixman_image_get_data(image) + damage->extents.x1 + damage->extents.y1 * (pixman_image_get_stride(image) / sizeof(uint32_t));
202
203    rects = pixman_region32_rectangles(damage, &nrects);
204    context->rfx_rects = realloc(context->rfx_rects, nrects * sizeof *rfxRect);
205    if (context->rfx_rects == NULL)
206      {
207         ERR("realloc failed");
208         return;
209      }
210
211    for (i = 0; i < nrects; i++)
212      {
213         region = &rects[i];
214         rfxRect = &context->rfx_rects[i];
215
216         rfxRect->x = (region->x1 - damage->extents.x1);
217         rfxRect->y = (region->y1 - damage->extents.y1);
218         rfxRect->width = (region->x2 - region->x1);
219         rfxRect->height = (region->y2 - region->y1);
220      }
221
222    rfx_compose_message(context->rfx_context, context->encode_stream, context->rfx_rects, nrects, (BYTE *)ptr, width, height, pixman_image_get_stride(image));
223
224    cmd.bmp.bitmapDataLength = Stream_GetPosition(context->encode_stream);
225    cmd.bmp.bitmapData = Stream_Buffer(context->encode_stream);
226
227    update->SurfaceBits(update->context, &cmd);
228 }
229
230
231 static void
232 e_rdp_peer_refresh_nsc(pixman_region32_t *damage, pixman_image_t *image, freerdp_peer *peer)
233 {
234    int width, height;
235    uint32_t *ptr;
236    rdpUpdate *update = peer->update;
237    SURFACE_BITS_COMMAND cmd = { 0 };
238    E_Rdp_Peer_Context *context = (E_Rdp_Peer_Context *)peer->context;
239
240    Stream_Clear(context->encode_stream);
241    Stream_SetPosition(context->encode_stream, 0);
242
243    width = (damage->extents.x2 - damage->extents.x1);
244    height = (damage->extents.y2 - damage->extents.y1);
245
246    cmd.cmdType = CMDTYPE_SET_SURFACE_BITS;
247    cmd.skipCompression = TRUE;
248    cmd.destLeft = damage->extents.x1;
249    cmd.destTop = damage->extents.y1;
250    cmd.destRight = damage->extents.x2;
251    cmd.destBottom = damage->extents.y2;
252    cmd.bmp.bpp = 32;
253    cmd.bmp.codecID = peer->settings->NSCodecId;
254    cmd.bmp.width = width;
255    cmd.bmp.height = height;
256
257    ptr = pixman_image_get_data(image) + damage->extents.x1 + damage->extents.y1 * (pixman_image_get_stride(image) / sizeof(uint32_t));
258
259    nsc_compose_message(context->nsc_context, context->encode_stream, (BYTE *)ptr, width, height, pixman_image_get_stride(image));
260
261    cmd.bmp.bitmapDataLength = Stream_GetPosition(context->encode_stream);
262    cmd.bmp.bitmapData = Stream_Buffer(context->encode_stream);
263
264    update->SurfaceBits(update->context, &cmd);
265 }
266
267 static void
268 pixman_image_flipped_subrect(const pixman_box32_t *rect, pixman_image_t *img, BYTE *dest)
269 {
270    int stride = pixman_image_get_stride(img);
271    int h;
272    int toCopy = (rect->x2 - rect->x1) * 4;
273    int height = (rect->y2 - rect->y1);
274    const BYTE *src = (const BYTE *)pixman_image_get_data(img);
275    src += ((rect->y2-1) * stride) + (rect->x1 * 4);
276
277    for (h = 0; h < height; h++, src -= stride, dest += toCopy)
278      memcpy(dest, src, toCopy);
279 }
280
281 static void
282 e_rdp_peer_refresh_raw(pixman_region32_t *region, pixman_image_t *image, freerdp_peer *peer)
283 {
284    rdpUpdate *update = peer->update;
285    SURFACE_BITS_COMMAND cmd = { 0 };
286    SURFACE_FRAME_MARKER marker;
287    pixman_box32_t *rect, subrect;
288    int nrects, i;
289    int heightIncrement, remainingHeight, top;
290
291    rect = pixman_region32_rectangles(region, &nrects);
292    if (!nrects)
293      return;
294
295    if (refresh_raw_frame_id == 1024)
296      refresh_raw_frame_id = 0;
297    refresh_raw_frame_id++;
298
299    marker.frameId = refresh_raw_frame_id;
300    marker.frameAction = SURFACECMD_FRAMEACTION_BEGIN;
301    update->SurfaceFrameMarker(peer->context, &marker);
302
303    cmd.cmdType = CMDTYPE_SET_SURFACE_BITS;
304    cmd.bmp.bpp = 32;
305    cmd.bmp.codecID = 0;
306
307    for (i = 0; i < nrects; i++, rect++)
308      {
309         /*INF("rect(%d,%d, %d,%d)\n", rect->x1, rect->y1, rect->x2, rect->y2);*/
310         cmd.destLeft = rect->x1;
311         cmd.destRight = rect->x2;
312         cmd.bmp.width = (rect->x2 - rect->x1);
313
314         heightIncrement = peer->settings->MultifragMaxRequestSize / (16 + cmd.bmp.width * 4);
315         remainingHeight = rect->y2 - rect->y1;
316         top = rect->y1;
317
318         subrect.x1 = rect->x1;
319         subrect.x2 = rect->x2;
320
321         while (remainingHeight)
322           {
323              cmd.bmp.height = (remainingHeight > heightIncrement) ? heightIncrement : remainingHeight;
324              cmd.destTop = top;
325              cmd.destBottom = top + cmd.bmp.height;
326              cmd.bmp.bitmapDataLength = cmd.bmp.width * cmd.bmp.height * 4;
327              cmd.bmp.bitmapData = (BYTE *)realloc(cmd.bmp.bitmapData, cmd.bmp.bitmapDataLength);
328
329              subrect.y1 = top;
330              subrect.y2 = top + cmd.bmp.height;
331              pixman_image_flipped_subrect(&subrect, image, cmd.bmp.bitmapData);
332
333              /*INF("*  sending (%d,%d, %d,%d)\n", subrect.x1, subrect.y1, subrect.x2, subrect.y2); */
334              update->SurfaceBits(peer->context, &cmd);
335
336              remainingHeight -= cmd.bmp.height;
337              top += cmd.bmp.height;
338           }
339      }
340
341    free(cmd.bmp.bitmapData);
342
343    marker.frameAction = SURFACECMD_FRAMEACTION_END;
344    update->SurfaceFrameMarker(peer->context, &marker);
345 }
346
347 static void
348 e_rdp_peer_refresh_region(pixman_region32_t *region, freerdp_peer *peer, tbm_surface_h tbm_surface, pixman_image_t *pix_surface)
349 {
350    E_Rdp_Peer_Context *context = (E_Rdp_Peer_Context *)peer->context;
351    E_Rdp_Output *output = context->rdpBackend->output;
352    rdpSettings *settings = peer->settings;
353
354    if (settings->RemoteFxCodec)
355      e_rdp_peer_refresh_rfx(region, pix_surface, peer);
356    else if (settings->NSCodec)
357      e_rdp_peer_refresh_nsc(region, pix_surface, peer);
358    else
359      e_rdp_peer_refresh_raw(region, pix_surface, peer);
360
361    if (output->pix_surface)
362      pixman_image_unref(output->pix_surface);
363    output->pix_surface = pix_surface;
364
365    if (output->tbm_surface)
366      tbm_surface_destroy(output->tbm_surface);
367    output->tbm_surface = tbm_surface;
368 }
369
370 static void
371 _e_rdp_output_image_showing_rect_get(Eina_Rectangle *out_rect, Eina_Rectangle *dst_rect, Eina_Rectangle *showing_rect)
372 {
373    showing_rect->x = dst_rect->x;
374    showing_rect->y = dst_rect->y;
375
376    if (dst_rect->x >= out_rect->w)
377      showing_rect->w = 0;
378    else if (dst_rect->x + dst_rect->w > out_rect->w)
379      showing_rect->w = out_rect->w - dst_rect->x;
380    else
381      showing_rect->w = dst_rect->w;
382
383    if (dst_rect->y >= out_rect->h)
384      showing_rect->h = 0;
385    else if (dst_rect->y + dst_rect->h > out_rect->h)
386      showing_rect->h = out_rect->h - dst_rect->y;
387    else
388      showing_rect->h = dst_rect->h;
389 }
390
391 static void
392 _e_rdp_output_image_src_crop_get(E_Hwc_Window *hwc_window, Eina_Rectangle *fit, Eina_Rectangle *showing_rect, int primary_w, int primary_h)
393 {
394    float ratio_x, ratio_y;
395    Eina_Rectangle out_rect;
396    Eina_Rectangle dst_rect;
397
398    fit->x = 0;
399    fit->y = 0;
400    fit->w = 0;
401    fit->h = 0;
402
403    out_rect.x = 0;
404    out_rect.y = 0;
405    out_rect.w = primary_w;
406    out_rect.h = primary_h;
407
408    dst_rect.x = hwc_window->current.info.dst_pos.x;
409    dst_rect.y = hwc_window->current.info.dst_pos.y;
410    dst_rect.w = hwc_window->current.info.dst_pos.w;
411    dst_rect.h = hwc_window->current.info.dst_pos.h;
412
413    _e_rdp_output_image_showing_rect_get(&out_rect, &dst_rect, showing_rect);
414
415    fit->x = hwc_window->current.info.src_config.pos.x;
416    fit->y = hwc_window->current.info.src_config.pos.y;
417
418    if (hwc_window->current.info.transform % 2 == 0)
419      {
420         ratio_x = (float)hwc_window->current.info.src_config.pos.w / dst_rect.w;
421         ratio_y = (float)hwc_window->current.info.src_config.pos.h / dst_rect.h;
422
423         fit->w = showing_rect->w * ratio_x;
424         fit->h = showing_rect->h * ratio_y;
425      }
426    else
427      {
428         ratio_x = (float)hwc_window->current.info.src_config.pos.w / dst_rect.h;
429         ratio_y = (float)hwc_window->current.info.src_config.pos.h / dst_rect.w;
430
431         fit->w = showing_rect->h * ratio_x;
432         fit->h = showing_rect->w * ratio_y;
433      }
434 }
435
436 static void
437 _e_rdp_output_center_rect_get (int src_w, int src_h, int dst_w, int dst_h, Eina_Rectangle *fit)
438 {
439    float rw, rh;
440
441    if (src_w <= 0 || src_h <= 0 || dst_w <= 0 || dst_h <= 0 || !fit)
442      return;
443
444    rw = (float)src_w / dst_w;
445    rh = (float)src_h / dst_h;
446
447    if (rw > rh)
448      {
449         fit->w = dst_w;
450         fit->h = src_h / rw;
451         fit->x = 0;
452         fit->y = (dst_h - fit->h) / 2;
453      }
454    else if (rw < rh)
455      {
456         fit->w = src_w / rh;
457         fit->h = dst_h;
458         fit->x = (dst_w - fit->w) / 2;
459         fit->y = 0;
460      }
461    else
462      {
463         fit->w = dst_w;
464         fit->h = dst_h;
465         fit->x = 0;
466         fit->y = 0;
467      }
468
469    if (fit->x % 2)
470      fit->x = fit->x - 1;
471 }
472
473 static void
474 _e_rdp_output_image_dst_crop_get(E_Hwc_Window *hwc_window, int src_w, int src_h, int w, int h,
475                                  Eina_Rectangle *pos, Eina_Rectangle *showing_pos, Eina_Rectangle *dst_crop, int rotate)
476 {
477    dst_crop->x = 0;
478    dst_crop->y = 0;
479    dst_crop->w = 0;
480    dst_crop->h = 0;
481
482    if (hwc_window->current.info.src_config.pos.w == w && hwc_window->current.info.src_config.pos.h == h &&
483        pos->x == 0 && pos->y == 0 && pos->w == src_w && pos->h == src_h)
484      {
485         dst_crop->x = pos->x;
486         dst_crop->y = pos->y;
487         dst_crop->w = pos->w;
488         dst_crop->h = pos->h;
489      }
490    else if ((w == pos->w) && (h == pos->h) && (showing_pos->w == pos->w) && (showing_pos->h == pos->h))
491      {
492         dst_crop->x = hwc_window->current.info.dst_pos.x + pos->x;
493         dst_crop->y = hwc_window->current.info.dst_pos.y + pos->y;
494         dst_crop->w = hwc_window->current.info.dst_pos.w;
495         dst_crop->h = hwc_window->current.info.dst_pos.h;
496      }
497    else if (rotate == 0)
498      {
499         dst_crop->x = showing_pos->x * pos->w / w + pos->x;
500         dst_crop->y = showing_pos->y * pos->h / h + pos->y;
501         dst_crop->w = showing_pos->w * pos->w / w;
502         dst_crop->h = showing_pos->h * pos->h / h;
503      }
504    else
505      {
506         dst_crop->x = pos->x;
507         dst_crop->y = pos->y;
508         dst_crop->w = pos->w;
509         dst_crop->h = pos->h;
510      }
511 }
512
513 static void
514 _e_rdp_output_image_composite(pixman_image_t *src_img, pixman_image_t *dst_img,
515                           int sx, int sy, int sw, int sh,
516                           int dx, int dy, int dw, int dh,
517                           Eina_Bool over, int rotate, int hflip, int vflip)
518 {
519    double scale_x, scale_y;
520    int rotate_step;
521    pixman_transform_t t;
522    struct pixman_f_transform ft;
523    pixman_op_t op;
524
525    pixman_f_transform_init_identity(&ft);
526
527    if (hflip)
528      {
529         pixman_f_transform_scale(&ft, NULL, -1, 1);
530         pixman_f_transform_translate(&ft, NULL, dw, 0);
531      }
532
533    if (vflip)
534      {
535         pixman_f_transform_scale(&ft, NULL, 1, -1);
536         pixman_f_transform_translate(&ft, NULL, 0, dh);
537      }
538
539    rotate_step = (rotate + 360) / 90 % 4;
540    if (rotate_step > 0)
541      {
542         int c, s, tx = 0, ty = 0;
543         switch (rotate_step)
544           {
545            case 1:
546               c = 0, s = -1, tx = -dw;
547               break;
548            case 2:
549               c = -1, s = 0, tx = -dw, ty = -dh;
550               break;
551            case 3:
552               c = 0, s = 1, ty = -dh;
553               break;
554           }
555         pixman_f_transform_translate(&ft, NULL, tx, ty);
556         pixman_f_transform_rotate(&ft, NULL, c, s);
557      }
558
559    if (rotate_step % 2 == 0)
560      {
561         scale_x = (double)sw / dw;
562         scale_y = (double)sh / dh;
563      }
564    else
565      {
566         scale_x = (double)sw / dh;
567         scale_y = (double)sh / dw;
568      }
569
570    pixman_f_transform_scale(&ft, NULL, scale_x, scale_y);
571    pixman_f_transform_translate(&ft, NULL, sx, sy);
572    pixman_transform_from_pixman_f_transform(&t, &ft);
573    pixman_image_set_transform(src_img, &t);
574
575    if (!over) op = PIXMAN_OP_SRC;
576    else op = PIXMAN_OP_OVER;
577
578    pixman_image_composite(op, src_img, NULL, dst_img, 0, 0, 0, 0,
579                           dx, dy, dw, dh);
580 }
581
582 static Eina_Bool
583 _e_rdp_pixman_output_image_composite(E_Rdp_Output *output, E_Hwc_Window *hwc_window, pixman_image_t *pix_surface, int pix_w, int pix_h, int primary_w, int primary_h)
584 {
585    Eina_Rectangle showing_pos = {0, };
586    Eina_Rectangle dst_pos = {0, };
587    Eina_Rectangle src_crop = {0, };
588    Eina_Rectangle dst_crop = {0, };
589    tbm_surface_h tbm_surface = NULL;
590    tbm_surface_info_s info;
591    pixman_image_t *pix_surface_src = NULL;
592    pixman_format_code_t pix_format = 0;
593    int err = 0;
594
595    tbm_surface = hwc_window->display.buffer.tsurface;
596    if (!tbm_surface)
597      return EINA_FALSE;
598
599    tbm_surface_internal_ref(tbm_surface);
600
601    err = tbm_surface_map(tbm_surface, TBM_SURF_OPTION_READ, &info);
602    if (err)
603      return EINA_FALSE;
604
605    pix_format = _e_rdp_pixman_format_get(info.format);
606    if (pix_format == 0)
607      {
608         ERR("not supported format");
609         tbm_surface_unmap(tbm_surface);
610         return EINA_FALSE;
611      }
612
613    pix_surface_src = pixman_image_create_bits(pix_format, info.width, info.height, (uint32_t *)info.planes[0].ptr, info.planes[0].stride);
614    if (pix_surface_src == NULL)
615      {
616         ERR("create pixman image failed");
617         tbm_surface_unmap(tbm_surface);
618         return EINA_FALSE;
619      }
620
621    _e_rdp_output_image_src_crop_get(hwc_window, &src_crop, &showing_pos, primary_w, primary_h);
622    _e_rdp_output_center_rect_get(primary_w, primary_h, pix_w, pix_h, &dst_pos);
623    _e_rdp_output_image_dst_crop_get(hwc_window, info.width, info.height, primary_w, primary_h, &dst_pos, &showing_pos, &dst_crop, 0);
624
625    _e_rdp_output_image_composite(pix_surface_src, pix_surface,
626                                  src_crop.x, src_crop.y, src_crop.w, src_crop.h,
627                                  dst_crop.x, dst_crop.y, dst_crop.w, dst_crop.h,
628                                  EINA_TRUE, 0, 0, 0);
629
630    pixman_image_unref(pix_surface_src);
631    tbm_surface_unmap(tbm_surface);
632    tbm_surface_internal_unref(tbm_surface);
633
634    return EINA_TRUE;
635 }
636
637 static int
638 _e_rdp_cb_hwc_window_sort(const void *d1, const void *d2)
639 {
640    E_Hwc_Window *hwc_window1 = (E_Hwc_Window *)d1;
641    E_Hwc_Window *hwc_window2 = (E_Hwc_Window *)d2;
642    int zpos1 = 0, zpos2 = 0;
643
644    if (!hwc_window1) return(1);
645    if (!hwc_window2) return(-1);
646
647    if (hwc_window1->state == E_HWC_WINDOW_STATE_NONE)
648      zpos1 = -999;
649    else
650      zpos1 = hwc_window1->zpos;
651
652    if (hwc_window2->state == E_HWC_WINDOW_STATE_NONE)
653      zpos2 = -999;
654    else
655      zpos2 = hwc_window2->zpos;
656
657    return (zpos1 - zpos2);
658 }
659
660 static pixman_image_t *
661 _e_rdp_pixman_output_image_get(E_Rdp_Output *output, tbm_surface_h tbm_surface)
662 {
663    E_Output *e_output = NULL;
664    E_Hwc *hwc = NULL;
665    E_Hwc_Window *hwc_window = NULL;
666    Eina_List *l;
667    Eina_List *visible_list = NULL;
668    Eina_Bool target_window = EINA_FALSE;
669    int err;
670    tbm_surface_info_s info;
671    pixman_image_t *pix_surface = NULL;
672    pixman_format_code_t pix_format = 0;
673    int e_output_w, e_output_h;
674
675    EINA_SAFETY_ON_NULL_RETURN_VAL(output, NULL);
676    EINA_SAFETY_ON_NULL_RETURN_VAL(tbm_surface, NULL);
677
678    e_output = output->primary_output;
679    EINA_SAFETY_ON_NULL_RETURN_VAL(e_output, NULL);
680
681    hwc = e_output->hwc;
682    EINA_SAFETY_ON_NULL_RETURN_VAL(hwc, EINA_FALSE);
683
684    e_output_w = e_output->config.mode.w;
685    e_output_h = e_output->config.mode.h;
686
687    err = tbm_surface_map(tbm_surface, TBM_SURF_OPTION_WRITE, &info);
688    if (err)
689      return NULL;
690
691    pix_format = _e_rdp_pixman_format_get(info.format);
692    if (pix_format == 0)
693      {
694         ERR("not supported format");
695         goto out;
696      }
697
698    pix_surface = pixman_image_create_bits(pix_format, info.width, info.height, (uint32_t *)info.planes[0].ptr, info.planes[0].stride);
699    if (pix_surface == NULL)
700      {
701         ERR("create pixman image failed");
702         goto out;
703      }
704
705    EINA_LIST_FOREACH(hwc->hwc_windows, l, hwc_window)
706      {
707         if (!hwc_window) continue;
708         if (hwc_window->is_target) continue;
709         if (hwc_window->is_video) continue;
710         if (hwc_window->is_cursor) continue;
711         if (hwc_window->state == E_HWC_WINDOW_STATE_NONE || hwc_window->zpos == -999) continue;
712
713         if (hwc_window->accepted_state == E_HWC_WINDOW_STATE_CLIENT)
714           {
715              target_window = EINA_TRUE;
716              visible_list = eina_list_append(visible_list, hwc_window);
717              continue;
718           }
719
720         if (hwc_window->accepted_state == E_HWC_WINDOW_STATE_DEVICE)
721           visible_list = eina_list_append(visible_list, hwc_window);
722      }
723
724    if (eina_list_count(visible_list) == 0)
725      {
726         ERR("no visible hwc_window for capture");
727         _e_rdp_pixman_output_image_composite(output, (E_Hwc_Window *)hwc->target_hwc_window, pix_surface, info.width, info.height, e_output_w, e_output_h);
728         goto out;
729      }
730
731    visible_list = eina_list_sort(visible_list, eina_list_count(visible_list), _e_rdp_cb_hwc_window_sort);
732
733    EINA_LIST_FOREACH(visible_list, l, hwc_window)
734      {
735         if (hwc_window->accepted_state == E_HWC_WINDOW_STATE_CLIENT)
736           {
737              if (target_window)
738                {
739                   target_window = EINA_FALSE;
740                   _e_rdp_pixman_output_image_composite(output, (E_Hwc_Window *)hwc->target_hwc_window, pix_surface, info.width, info.height, e_output_w, e_output_h);
741                }
742              continue;
743           }
744
745         _e_rdp_pixman_output_image_composite(output, hwc_window, pix_surface, info.width, info.height, e_output_w, e_output_h);
746      }
747
748 out:
749    tbm_surface_unmap(tbm_surface);
750
751    return pix_surface;
752 }
753
754 static Eina_Bool
755 _e_rdp_frame_timer(void *data)
756 {
757    E_Rdp_Output *output;
758    tbm_surface_h tbm_surface = NULL;
759    pixman_image_t *pix_surface = NULL;
760    pixman_box32_t box;
761    pixman_region32_t damage;
762    E_Rdp_Peer_Item *rdp_peer, *tmp;
763    freerdp_peer *client = NULL;
764
765    EINA_SAFETY_ON_NULL_RETURN_VAL(data, ECORE_CALLBACK_CANCEL);
766
767    output = (E_Rdp_Output *)data;
768
769    tbm_surface = _e_rdp_tbm_image_create(output, E_RDP_WIDTH, E_RDP_HEIGHT, 0x00000000);
770    if (tbm_surface == NULL)
771      {
772         ERR("create tbm surface failed");
773         return ECORE_CALLBACK_RENEW;
774      }
775
776    pix_surface = _e_rdp_pixman_output_image_get(output, tbm_surface);
777    if (pix_surface == NULL)
778      {
779         ERR("create output pixman surface failed");
780         tbm_surface_destroy(tbm_surface);
781         return ECORE_CALLBACK_RENEW;
782      }
783
784    /* sends a full refresh */
785    box.x1 = 0;
786    box.y1 = 0;
787    box.x2 = E_RDP_WIDTH;
788    box.y2 = E_RDP_HEIGHT;
789    pixman_region32_init_with_extents(&damage, &box);
790
791    wl_list_for_each_safe(rdp_peer, tmp, &output->peers, link)
792      {
793         client = rdp_peer->peer;
794         if (!client)
795           continue;
796
797         e_rdp_peer_refresh_region(&damage, client, tbm_surface, pix_surface);
798      }
799    pixman_region32_fini(&damage);
800
801    return ECORE_CALLBACK_RENEW;
802 }
803
804
805 static BOOL
806 e_rdp_peer_capabilities(freerdp_peer *client)
807 {
808    return TRUE;
809 }
810
811 static BOOL
812 e_rdp_peer_post_connect(freerdp_peer *client)
813 {
814    return TRUE;
815 }
816
817 static BOOL
818 e_rdp_peer_activate(freerdp_peer *client)
819 {
820    E_Rdp_Peer_Context *peerCtx;
821    E_Rdp_Backend *b;
822    E_Rdp_Output *output;
823    rdpSettings *settings;
824    rdpPointerUpdate *pointer;
825    E_Rdp_Peer_Item *peersItem;
826    pixman_box32_t box;
827    pixman_region32_t damage;
828    POINTER_SYSTEM_UPDATE pointer_system;
829    tbm_surface_h tbm_surface = NULL;
830    pixman_image_t *pix_surface = NULL;
831
832    EINA_SAFETY_ON_NULL_RETURN_VAL(client, FALSE);
833    EINA_SAFETY_ON_NULL_RETURN_VAL(client->context, FALSE);
834    EINA_SAFETY_ON_NULL_RETURN_VAL(client->settings, FALSE);
835
836    peerCtx = (E_Rdp_Peer_Context *)client->context;
837    EINA_SAFETY_ON_NULL_RETURN_VAL(peerCtx, FALSE);
838
839    b = peerCtx->rdpBackend;
840    EINA_SAFETY_ON_NULL_RETURN_VAL(b, FALSE);
841
842    peersItem = &peerCtx->item;
843    EINA_SAFETY_ON_NULL_RETURN_VAL(peersItem, FALSE);
844
845    output = b->output;
846    settings = client->settings;
847
848    if (!settings->SurfaceCommandsEnabled)
849      {
850         ERR("client doesn't support required SurfaceCommands\n");
851         return FALSE;
852      }
853
854    if (b->force_no_compression && settings->CompressionEnabled)
855      {
856         ERR("Forcing compression off\n");
857         settings->CompressionEnabled = FALSE;
858      }
859
860    if (E_RDP_WIDTH != (int)settings->DesktopWidth || E_RDP_HEIGHT != (int)settings->DesktopHeight)
861      {
862         if (b->no_clients_resize)
863           {
864              /* RDP peers don't dictate their resolution to e */
865              if (!settings->DesktopResize)
866                {
867                   /* peer does not support desktop resize */
868                   ERR("%s: client doesn't support resizing, closing connection\n", __FUNCTION__);
869                   return FALSE;
870                }
871              else
872                {
873                   settings->DesktopWidth = E_RDP_WIDTH;
874                   settings->DesktopHeight = E_RDP_HEIGHT;
875                   client->update->DesktopResize(client->context);
876                }
877           }
878         else
879           {
880              ;
881              /* not supported */
882           }
883      }
884
885    rfx_context_reset(peerCtx->rfx_context, E_RDP_WIDTH, E_RDP_HEIGHT);
886    nsc_context_reset(peerCtx->nsc_context, E_RDP_WIDTH, E_RDP_HEIGHT);
887    if (peersItem->flags & RDP_PEER_ACTIVATED)
888      return TRUE;
889
890    /* when here it's the first reactivation, we need to setup a little more */
891    DBG("kbd_layout:0x%x kbd_type:0x%x kbd_subType:0x%x kbd_functionKeys:0x%x\n",
892        settings->KeyboardLayout, settings->KeyboardType, settings->KeyboardSubType, settings->KeyboardFunctionKey);
893
894    peersItem->flags |= RDP_PEER_ACTIVATED;
895
896    /* disable pointer on the client side */
897    pointer = client->update->pointer;
898    pointer_system.type = SYSPTR_NULL;
899    pointer->PointerSystem(client->context, &pointer_system);
900
901    tbm_surface = _e_rdp_tbm_image_create(output, E_RDP_WIDTH, E_RDP_HEIGHT, 0xFFFFCCFF);
902    if (tbm_surface == NULL)
903      {
904         ERR("create sample tbm surface failed");
905         return TRUE;
906      }
907    pix_surface = _e_rdp_pixman_image_create(tbm_surface);
908    if (pix_surface == NULL)
909      {
910         ERR("create sample pixman failed");
911         tbm_surface_destroy(tbm_surface);
912         return TRUE;
913      }
914
915    /* sends a full refresh */
916    box.x1 = 0;
917    box.y1 = 0;
918    box.x2 = E_RDP_WIDTH;
919    box.y2 = E_RDP_HEIGHT;
920    pixman_region32_init_with_extents(&damage, &box);
921
922    e_rdp_peer_refresh_region(&damage, client, tbm_surface, pix_surface);
923
924    pixman_region32_fini(&damage);
925
926    if (output->frame_timer != NULL)
927      ecore_timer_del(output->frame_timer);
928    output->frame_timer = ecore_timer_add(0.2, _e_rdp_frame_timer, output);
929
930    return TRUE;
931 }
932
933 static BOOL
934 e_rdp_suppress_output(rdpContext *context, BYTE allow, const RECTANGLE_16 *area)
935 {
936    E_Rdp_Peer_Context *peerContext = (E_Rdp_Peer_Context *)context;
937
938    if (allow)
939      peerContext->item.flags |= RDP_PEER_OUTPUT_ENABLED;
940    else
941      peerContext->item.flags &= (~RDP_PEER_OUTPUT_ENABLED);
942
943    return TRUE;
944 }
945
946 static BOOL
947 e_rdp_input_synchronize_event(rdpInput *input, UINT32 flags)
948 {
949    //To do
950    return TRUE;
951 }
952
953 static BOOL
954 e_rdp_mouse_event(rdpInput *input, UINT16 flags, UINT16 x, UINT16 y)
955 {
956    //To do
957    return TRUE;
958 }
959
960 static BOOL
961 e_rdp_extended_mouse_event(rdpInput *input, UINT16 flags, UINT16 x, UINT16 y)
962 {
963    //To do
964    return TRUE;
965 }
966
967 static BOOL
968 e_rdp_input_keyboard_event(rdpInput *input, UINT16 flags, UINT16 code)
969 {
970    //To do
971    return TRUE;
972 }
973
974 static BOOL
975 e_rdp_input_unicode_keyboard_event(rdpInput *input, UINT16 flags, UINT16 code)
976 {
977    DBG("Client sent a unicode keyboard event (flags:0x%X code:0x%X)\n", flags, code);
978    return TRUE;
979 }
980
981 static int
982 e_rdp_client_activity(int fd, uint32_t mask, void *data)
983 {
984    freerdp_peer *client = (freerdp_peer *)data;
985    E_Rdp_Peer_Context *peerCtx;
986    E_Rdp_Backend *b;
987    E_Rdp_Output *output;
988
989    EINA_SAFETY_ON_NULL_RETURN_VAL(client, 0);
990    EINA_SAFETY_ON_NULL_RETURN_VAL(client->context, 0);
991
992    if (client->CheckFileDescriptor(client))
993      {
994         DBG("enable client activity %p", client);
995         return 0;
996      }
997
998    INF("unable to checkDescriptor for %p\n", client);
999    peerCtx = (E_Rdp_Peer_Context *)client->context;
1000    EINA_SAFETY_ON_NULL_GOTO(peerCtx, out_clean);
1001
1002    b = peerCtx->rdpBackend;
1003    EINA_SAFETY_ON_NULL_GOTO(b, out_clean);
1004
1005    output = b->output;
1006    if (output->frame_timer)
1007      {
1008         ecore_timer_del(output->frame_timer);
1009         output->frame_timer = NULL;
1010      }
1011 out_clean:
1012    freerdp_peer_context_free(client);
1013    freerdp_peer_free(client);
1014    return 0;
1015 }
1016
1017 static BOOL
1018 e_rdp_peer_context_new(freerdp_peer *client, E_Rdp_Peer_Context *context)
1019 {
1020    context->item.peer = client;
1021    context->item.flags = RDP_PEER_OUTPUT_ENABLED;
1022
1023    context->rfx_context = rfx_context_new(TRUE);
1024    if (!context->rfx_context)
1025      return FALSE;
1026
1027    context->rfx_context->mode = RLGR3;
1028    context->rfx_context->width = client->settings->DesktopWidth;
1029    context->rfx_context->height = client->settings->DesktopHeight;
1030    rfx_context_set_pixel_format(context->rfx_context, DEFAULT_PIXEL_FORMAT);
1031
1032    context->nsc_context = nsc_context_new();
1033    if (!context->nsc_context)
1034      goto out_error_nsc;
1035
1036    nsc_context_set_parameters(context->nsc_context, NSC_COLOR_FORMAT, DEFAULT_PIXEL_FORMAT);
1037    context->encode_stream = Stream_New(NULL, 65536);
1038    if (!context->encode_stream)
1039      goto out_error_stream;
1040
1041    return TRUE;
1042
1043 out_error_nsc:
1044    rfx_context_free(context->rfx_context);
1045 out_error_stream:
1046    nsc_context_free(context->nsc_context);
1047    return FALSE;
1048 }
1049
1050 static void
1051 e_rdp_peer_context_free(freerdp_peer *client, E_Rdp_Peer_Context *context)
1052 {
1053    int i;
1054    if (!context)
1055      return;
1056
1057    wl_list_remove(&context->item.link);
1058    for (i = 0; i < MAX_FREERDP_FDS; i++)
1059      {
1060         if (context->events[i])
1061           wl_event_source_remove(context->events[i]);
1062      }
1063
1064    Stream_Free(context->encode_stream, TRUE);
1065    nsc_context_free(context->nsc_context);
1066    rfx_context_free(context->rfx_context);
1067    free(context->rfx_rects);
1068 }
1069
1070
1071 static int
1072 e_rdp_peer_init(freerdp_peer *client, E_Rdp_Backend *b)
1073 {
1074    int rcount = 0;
1075    void *rfds[MAX_FREERDP_FDS];
1076    int i, fd;
1077    struct wl_event_loop *loop;
1078    rdpSettings *settings;
1079    rdpInput *input;
1080    E_Rdp_Peer_Context *peerCtx;
1081
1082    client->ContextSize = sizeof(E_Rdp_Peer_Context);
1083    client->ContextNew = (psPeerContextNew)e_rdp_peer_context_new;
1084    client->ContextFree = (psPeerContextFree)e_rdp_peer_context_free;
1085    freerdp_peer_context_new(client);
1086
1087    peerCtx = (E_Rdp_Peer_Context *)client->context;
1088    peerCtx->rdpBackend = b;
1089
1090    settings = client->settings;
1091
1092    if (b->tls_enabled)
1093      {
1094         settings->CertificateFile = strdup(b->server_cert);
1095         settings->PrivateKeyFile = strdup(b->server_key);
1096      }
1097    else
1098      {
1099         settings->TlsSecurity = FALSE;
1100      }
1101    settings->NlaSecurity = FALSE;
1102
1103    if (!client->Initialize(client))
1104      {
1105         ERR("peer initialization failed\n");
1106         return -1;
1107      }
1108
1109    settings->OsMajorType = OSMAJORTYPE_UNIX;
1110    settings->OsMinorType = OSMINORTYPE_PSEUDO_XSERVER;
1111    settings->ColorDepth = 32;
1112    settings->RefreshRect = TRUE;
1113    settings->RemoteFxCodec = TRUE;
1114    settings->NSCodec = TRUE;
1115    settings->FrameMarkerCommandEnabled = TRUE;
1116    settings->SurfaceFrameMarkerEnabled = TRUE;
1117
1118    client->Capabilities = e_rdp_peer_capabilities;
1119    client->PostConnect = e_rdp_peer_post_connect;
1120    client->Activate = e_rdp_peer_activate;
1121
1122    client->update->SuppressOutput = (pSuppressOutput)e_rdp_suppress_output;
1123
1124    input = client->input;
1125    input->SynchronizeEvent = e_rdp_input_synchronize_event;
1126    input->MouseEvent = e_rdp_mouse_event;
1127    input->ExtendedMouseEvent = e_rdp_extended_mouse_event;
1128    input->KeyboardEvent = e_rdp_input_keyboard_event;
1129    input->UnicodeKeyboardEvent = e_rdp_input_unicode_keyboard_event;
1130
1131    if (!client->GetFileDescriptor(client, rfds, &rcount))
1132      {
1133         ERR("unable to retrieve client fds");
1134         goto error_initialize;
1135      }
1136
1137    if (rcount > MAX_FREERDP_FDS)
1138      {
1139         ERR("check fds max count. count:%d, max:%d", rcount, MAX_FREERDP_FDS);
1140         goto error_initialize;
1141      }
1142
1143    loop = wl_display_get_event_loop(e_comp_wl->wl.disp);
1144    for (i = 0; i < rcount; i++)
1145      {
1146         fd = (int)(long)(rfds[i]);
1147
1148         peerCtx->events[i] = wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE, e_rdp_client_activity, client);
1149      }
1150    for ( ; i < MAX_FREERDP_FDS; i++)
1151      peerCtx->events[i] = 0;
1152
1153    wl_list_insert(&b->output->peers, &peerCtx->item.link);
1154    return 0;
1155
1156 error_initialize:
1157    client->Close(client);
1158    return -1;
1159 }
1160
1161 static BOOL
1162 e_rdp_incoming_peer(freerdp_listener *instance, freerdp_peer *client)
1163 {
1164    E_Rdp_Backend *b = (E_Rdp_Backend *)instance->param4;
1165
1166    if (e_rdp_peer_init(client, b) < 0)
1167      {
1168         ERR("error when treating incoming peer\n");
1169         return FALSE;
1170      }
1171
1172    return TRUE;
1173 }
1174
1175 static int
1176 e_rdp_listener_activity(int fd, uint32_t mask, void *data)
1177 {
1178    freerdp_listener* instance = (freerdp_listener *)data;
1179
1180    if (!(mask & WL_EVENT_READABLE))
1181      return 0;
1182
1183    if (!instance->CheckFileDescriptor(instance))
1184      {
1185         ERR("failed to check FreeRDP file descriptor\n");
1186         return -1;
1187      }
1188
1189    return 0;
1190 }
1191
1192 static Eina_Bool
1193 e_rdp_implant_listener(E_Rdp_Backend *b, freerdp_listener *instance)
1194 {
1195    int i, fd;
1196    int rcount = 0;
1197    void* rfds[MAX_FREERDP_FDS];
1198    struct wl_event_loop *loop;
1199
1200    if (!instance->GetFileDescriptor(instance, rfds, &rcount))
1201      {
1202         ERR("Failed to get FreeRDP file descriptor\n");
1203         return EINA_FALSE;
1204      }
1205
1206    loop = wl_display_get_event_loop(e_comp_wl->wl.disp);
1207    for (i = 0; i < rcount; i++)
1208      {
1209         fd = (int)(long)(rfds[i]);
1210         b->listener_events[i] = wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE, e_rdp_listener_activity, instance);
1211      }
1212
1213    for ( ; i < MAX_FREERDP_FDS; i++)
1214      b->listener_events[i] = 0;
1215
1216    return EINA_TRUE;
1217 }
1218
1219 static void
1220 e_rdp_output_destroy(E_Rdp_Output *output)
1221 {
1222    EINA_SAFETY_ON_NULL_RETURN(output);
1223
1224    if (output->pix_surface)
1225      pixman_image_unref(output->pix_surface);
1226    if (output->tbm_surface)
1227      tbm_surface_destroy(output->tbm_surface);
1228
1229    free(output);
1230 }
1231
1232 E_Rdp_Output *
1233 e_rdp_output_create(void)
1234 {
1235    E_Rdp_Output *output = NULL;
1236
1237    output = E_NEW(E_Rdp_Output, 1);
1238    EINA_SAFETY_ON_NULL_RETURN_VAL(output, NULL);
1239
1240    output->primary_output = e_output_find_by_index(0);
1241
1242    wl_list_init(&output->peers);
1243
1244    return output;
1245 }
1246
1247 static void
1248 e_rdp_backend_destroy(void)
1249 {
1250    E_Rdp_Backend *b = NULL;
1251    E_Rdp_Peer_Item *rdp_peer, *tmp;
1252    int i;
1253
1254    b = g_rdp_backend;
1255    EINA_SAFETY_ON_NULL_RETURN(b);
1256
1257    if (b->output->frame_timer)
1258      {
1259         ecore_timer_del(b->output->frame_timer);
1260         b->output->frame_timer = NULL;
1261      }
1262
1263    wl_list_for_each_safe(rdp_peer, tmp, &b->output->peers, link)
1264      {
1265         freerdp_peer* client = rdp_peer->peer;
1266
1267         client->Disconnect(client);
1268         freerdp_peer_context_free(client);
1269         freerdp_peer_free(client);
1270      }
1271
1272    for (i = 0; i < MAX_FREERDP_FDS; i++)
1273      if (b->listener_events[i])
1274        wl_event_source_remove(b->listener_events[i]);
1275
1276    freerdp_listener_free(b->listener);
1277
1278    e_rdp_output_destroy(b->output);
1279
1280    free(b->server_cert);
1281    free(b->server_key);
1282    free(b);
1283 }
1284
1285
1286 Eina_Bool
1287 e_rdp_backend_create(E_Rdp_Conf_Edd *config)
1288 {
1289    E_Rdp_Backend *b;
1290
1291    b = E_NEW(E_Rdp_Backend, 1);
1292    EINA_SAFETY_ON_NULL_RETURN_VAL(b, EINA_FALSE);
1293
1294    b->rdp_key = NULL;
1295    b->no_clients_resize = config->no_clients_resize;
1296    b->force_no_compression = config->force_no_compression;
1297
1298    /* activate TLS only if certificate/key are available */
1299    if (config->server_cert && config->server_key)
1300      {
1301         DBG("TLS support activated\n");
1302         b->server_cert = strdup(config->server_cert);
1303         b->server_key = strdup(config->server_key);
1304         if (!b->server_cert || !b->server_key)
1305           goto err_free_strings;
1306         b->tls_enabled = 1;
1307      }
1308
1309    b->output = e_rdp_output_create();
1310    if (!b->output)
1311      {
1312         ERR("output create failed");
1313         goto err_free_strings;
1314      }
1315
1316    b->listener = freerdp_listener_new();
1317    b->listener->PeerAccepted = e_rdp_incoming_peer;
1318    b->listener->param4 = b;
1319    if (!b->listener->Open(b->listener, NULL, config->port))
1320      {
1321         ERR("unable to bind rdp socket\n");
1322         goto err_listener;
1323      }
1324
1325    if (e_rdp_implant_listener(b, b->listener) == EINA_FALSE)
1326      goto err_listener;
1327
1328    g_rdp_backend = b;
1329
1330    return EINA_TRUE;
1331
1332    err_listener:
1333    freerdp_listener_free(b->listener);
1334
1335    e_rdp_output_destroy(b->output);
1336    b->output = NULL;
1337
1338    err_free_strings:
1339    free(b->server_cert);
1340    free(b->server_key);
1341    free(b);
1342
1343    return EINA_FALSE;
1344 }
1345
1346 Eina_Bool
1347 e_mod_rdp_init(void)
1348 {
1349    int major, minor, revision;
1350    Eina_Bool ret = EINA_FALSE;
1351
1352    EINA_SAFETY_ON_NULL_RETURN_VAL(e_comp_wl, EINA_FALSE);
1353    EINA_SAFETY_ON_NULL_RETURN_VAL(e_comp_wl->wl.disp, EINA_FALSE);
1354
1355 #if FREERDP_VERSION_MAJOR >= 2
1356    winpr_InitializeSSL(0);
1357 #endif
1358
1359    freerdp_get_version(&major, &minor, &revision);
1360    if (major < E_MOD_RDP_CONFIG_VERSION)
1361      {
1362         ERR("Not supported version");
1363         return EINA_FALSE;
1364      }
1365    INF("freerdp version : %d.%d.%d", major, minor, revision);
1366
1367
1368    /* Init config */
1369    g_rdp_config = E_NEW(E_Rdp_Config_Data, 1);
1370    EINA_SAFETY_ON_NULL_RETURN_VAL(g_rdp_config, EINA_FALSE);
1371
1372    ret = e_rdp_conf_init(g_rdp_config);
1373    EINA_SAFETY_ON_FALSE_GOTO(ret, conf_err);
1374
1375    ret = e_rdp_backend_create(g_rdp_config->conf);
1376    EINA_SAFETY_ON_FALSE_GOTO(ret, create_err);
1377
1378    return ret;
1379
1380 create_err:
1381    e_rdp_conf_deinit(g_rdp_config);
1382 conf_err:
1383    E_FREE(g_rdp_config);
1384
1385    return ret;
1386 }
1387
1388 void
1389 e_mod_rdp_deinit(void)
1390 {
1391    e_rdp_backend_destroy();
1392    if (g_rdp_config)
1393      {
1394         e_rdp_conf_deinit(g_rdp_config);
1395         E_FREE(g_rdp_config);
1396      }
1397 }