summaryrefslogtreecommitdiffstats
path: root/xrdp/xrdp_painter.c
blob: f9bf883957f6816723896cf23a491000f7aebc0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
/*
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

   xrdp: A Remote Desktop Protocol server.
   Copyright (C) Jay Sorg 2004-2007

   painter, gc

*/

#include "xrdp.h"

/*****************************************************************************/
struct xrdp_painter* APP_CC
xrdp_painter_create(struct xrdp_wm* wm, struct xrdp_session* session)
{
  struct xrdp_painter* self;

  self = (struct xrdp_painter*)g_malloc(sizeof(struct xrdp_painter), 1);
  self->wm = wm;
  self->session = session;
  self->rop = 0xcc; /* copy gota use 0xcc*/
  self->clip_children = 1;
  return self;
}

/*****************************************************************************/
void APP_CC
xrdp_painter_delete(struct xrdp_painter* self)
{
  if (self == 0)
  {
    return;
  }
  g_free(self);
}

/*****************************************************************************/
int APP_CC
xrdp_painter_begin_update(struct xrdp_painter* self)
{
  libxrdp_orders_init(self->session);
  return 0;
}

/*****************************************************************************/
int APP_CC
xrdp_painter_end_update(struct xrdp_painter* self)
{
  libxrdp_orders_send(self->session);
  return 0;
}

/*****************************************************************************/
int APP_CC
xrdp_painter_font_needed(struct xrdp_painter* self)
{
  if (self->font == 0)
  {
    self->font = self->wm->default_font;
  }
  return 0;
}

/*****************************************************************************/
/* returns boolean, true if there is something to draw */
static int APP_CC
xrdp_painter_clip_adj(struct xrdp_painter* self, int* x, int* y,
                      int* cx, int* cy)
{
  int dx;
  int dy;

  if (!self->use_clip)
  {
    return 1;
  }
  if (self->clip.left > *x)
  {
    dx = self->clip.left - *x;
  }
  else
  {
    dx = 0;
  }
  if (self->clip.top > *y)
  {
    dy = self->clip.top - *y;
  }
  else
  {
    dy = 0;
  }
  if (*x + *cx > self->clip.right)
  {
    *cx = *cx - ((*x + *cx) - self->clip.right);
  }
  if (*y + *cy > self->clip.bottom)
  {
    *cy = *cy - ((*y + *cy) - self->clip.bottom);
  }
  *cx = *cx - dx;
  *cy = *cy - dy;
  if (*cx <= 0)
  {
    return 0;
  }
  if (*cy <= 0)
  {
    return 0;
  }
  *x = *x + dx;
  *y = *y + dy;
  return 1;
}

/*****************************************************************************/
int APP_CC
xrdp_painter_set_clip(struct xrdp_painter* self,
                      int x, int y, int cx, int cy)
{
  self->use_clip = &self->clip;
  self->clip.left = x;
  self->clip.top = y;
  self->clip.right = x + cx;
  self->clip.bottom = y + cy;
  return 0;
}

/*****************************************************************************/
int APP_CC
xrdp_painter_clr_clip(struct xrdp_painter* self)
{
  self->use_clip = 0;
  return 0;
}

/*****************************************************************************/
static int APP_CC
xrdp_painter_rop(int rop, int src, int dst)
{
  switch (rop & 0x0f)
  {
    case 0x0: return 0;
    case 0x1: return ~(src | dst);
    case 0x2: return (~src) & dst;
    case 0x3: return ~src;
    case 0x4: return src & (~dst);
    case 0x5: return ~(dst);
    case 0x6: return src ^ dst;
    case 0x7: return ~(src & dst);
    case 0x8: return src & dst;
    case 0x9: return ~(src) ^ dst;
    case 0xa: return dst;
    case 0xb: return (~src) | dst;
    case 0xc: return src;
    case 0xd: return src | (~dst);
    case 0xe: return src | dst;
    case 0xf: return ~0;
  }
  return dst;
}

/*****************************************************************************/
int APP_CC
xrdp_painter_text_width(struct xrdp_painter* self, char* text)
{
  int index;
  int rv;
  int len;
  struct xrdp_font_char* font_item;
  twchar* wstr;

  xrdp_painter_font_needed(self);
  if (text == 0)
  {
    return 0;
  }
  rv = 0;
  len = g_mbstowcs(0, text, 0);
  wstr = (twchar*)g_malloc((len + 2) * sizeof(twchar), 0);
  g_mbstowcs(wstr, text, len + 1);
  for (index = 0; index < len; index++)
  {
    font_item = self->font->font_items + wstr[index];
    rv = rv + font_item->incby;
  }
  g_free(wstr);
  return rv;
}

/*****************************************************************************/
int APP_CC
xrdp_painter_text_height(struct xrdp_painter* self, char* text)
{
  int index;
  int rv;
  int len;
  struct xrdp_font_char* font_item;
  twchar* wstr;

  xrdp_painter_font_needed(self);
  if (text == 0)
  {
    return 0;
  }
  rv = 0;
  len = g_mbstowcs(0, text, 0);
  wstr = (twchar*)g_malloc((len + 2) * sizeof(twchar), 0);
  g_mbstowcs(wstr, text, len + 1);
  for (index = 0; index < len; index++)
  {
    font_item = self->font->font_items + wstr[index];
    rv = MAX(rv, font_item->height);
  }
  g_free(wstr);
  return rv;
}

/*****************************************************************************/
/* fill in an area of the screen with one color */
int APP_CC
xrdp_painter_fill_rect(struct xrdp_painter* self,
                       struct xrdp_bitmap* bitmap,
                       int x, int y, int cx, int cy)
{
  struct xrdp_rect clip_rect;
  struct xrdp_rect draw_rect;
  struct xrdp_rect rect;
  struct xrdp_region* region;
  int k;
  int dx;
  int dy;
  int rop;

  if (self == 0)
  {
    return 0;
  }

  /* todo data */

  if (bitmap->type == WND_TYPE_BITMAP) /* 0 */
  {
    return 0;
  }
  xrdp_bitmap_get_screen_clip(bitmap, self, &clip_rect, &dx, &dy);
  region = xrdp_region_create(self->wm);
  xrdp_wm_get_vis_region(self->wm, bitmap, x, y, cx, cy, region,
                         self->clip_children);
  x += dx;
  y += dy;
  if (self->mix_mode == 0 && self->rop == 0xcc)
  {
    k = 0;
    while (xrdp_region_get_rect(region, k, &rect) == 0)
    {
      if (rect_intersect(&rect, &clip_rect, &draw_rect))
      {
        libxrdp_orders_rect(self->session, x, y, cx, cy,
                            self->fg_color, &draw_rect);
      }
      k++;
    }
  }
  else if (self->mix_mode == 0 &&
             ((self->rop & 0xf) == 0x0 || /* black */
              (self->rop & 0xf) == 0xf || /* white */
              (self->rop & 0xf) == 0x5))  /* DSTINVERT */
  {
    k = 0;
    while (xrdp_region_get_rect(region, k, &rect) == 0)
    {
      if (rect_intersect(&rect, &clip_rect, &draw_rect))
      {
        libxrdp_orders_dest_blt(self->session, x, y, cx, cy,
                                self->rop, &draw_rect);
      }
      k++;
    }
  }
  else
  {
    k = 0;
    rop = self->rop;
    /* if opcode is in the form 0x00, 0x11, 0x22, ... convert it */
    if (((rop & 0xf0) >> 4) == (rop & 0xf))
    {
      switch (rop)
      {
        case 0x66: /* xor */
          rop = 0x5a;
          break;
        case 0xaa: /* noop */
          rop = 0xfb;
          break;
        case 0xcc: /* copy */
          rop = 0xf0;
          break;
        case 0x88: /* and */
          rop = 0xc0;
          break;
      }
    }
    while (xrdp_region_get_rect(region, k, &rect) == 0)
    {
      if (rect_intersect(&rect, &clip_rect, &draw_rect))
      {
        libxrdp_orders_pat_blt(self->session, x, y, cx, cy,
                               rop, self->bg_color, self->fg_color,
                               &self->brush, &draw_rect);
      }
      k++;
    }
  }
  xrdp_region_delete(region);
  return 0;
}

/*****************************************************************************/
int APP_CC
xrdp_painter_draw_text(struct xrdp_painter* self,
                       struct xrdp_bitmap* bitmap,
                       int x, int y, const char* text)
{
  int i;
  int f;
  int c;
  int k;
  int x1;
  int y1;
  int flags;
  int len;
  int index;
  int total_width;
  int total_height;
  int dx;
  int dy;
  char* data;
  struct xrdp_region* region;
  struct xrdp_rect rect;
  struct xrdp_rect clip_rect;
  struct xrdp_rect draw_rect;
  struct xrdp_font* font;
  struct xrdp_font_char* font_item;
  twchar* wstr;

  if (self == 0)
  {
    return 0;
  }
  len = g_mbstowcs(0, text, 0);
  if (len < 1)
  {
    return 0;
  }

  /* todo data */

  if (bitmap->type == 0)
  {
    return 0;
  }
  /* convert to wide char */
  wstr = (twchar*)g_malloc((len + 2) * sizeof(twchar), 0);
  g_mbstowcs(wstr, text, len + 1);
  xrdp_painter_font_needed(self);
  font = self->font;
  f = 0;
  k = 0;
  total_width = 0;
  total_height = 0;
  data = (char*)g_malloc(len * 4, 1);
  for (index = 0; index < len; index++)
  {
    font_item = font->font_items + wstr[index];
    i = xrdp_cache_add_char(self->wm->cache, font_item);
    f = HIWORD(i);
    c = LOWORD(i);
    data[index * 2] = c;
    data[index * 2 + 1] = k;
    k = font_item->incby;
    total_width += k;
    total_height = MAX(total_height, font_item->height);
  }
  xrdp_bitmap_get_screen_clip(bitmap, self, &clip_rect, &dx, &dy);
  region = xrdp_region_create(self->wm);
  xrdp_wm_get_vis_region(self->wm, bitmap, x, y, total_width, total_height,
                         region, self->clip_children);
  x += dx;
  y += dy;
  k = 0;
  while (xrdp_region_get_rect(region, k, &rect) == 0)
  {
    if (rect_intersect(&rect, &clip_rect, &draw_rect))
    {
      x1 = x;
      y1 = y + total_height;
      flags = 0x03; /* 0x03 0x73; TEXT2_IMPLICIT_X and something else */
      libxrdp_orders_text(self->session, f, flags, 0,
                          self->fg_color, 0,
                          x - 1, y - 1, x + total_width, y + total_height,
                          0, 0, 0, 0,
                          x1, y1, data, len * 2, &draw_rect);
    }
    k++;
  }
  xrdp_region_delete(region);
  g_free(data);
  g_free(wstr);
  return 0;
}

/*****************************************************************************/
int APP_CC
xrdp_painter_draw_text2(struct xrdp_painter* self,
                        struct xrdp_bitmap* bitmap,
                        int font, int flags, int mixmode,
                        int clip_left, int clip_top,
                        int clip_right, int clip_bottom,
                        int box_left, int box_top,
                        int box_right, int box_bottom,
                        int x, int y, char* data, int data_len)
{
  struct xrdp_rect clip_rect;
  struct xrdp_rect draw_rect;
  struct xrdp_rect rect;
  struct xrdp_region* region;
  int k;
  int dx;
  int dy;

  if (self == 0)
  {
    return 0;
  }

  /* todo data */

  if (bitmap->type == WND_TYPE_BITMAP)
  {
    return 0;
  }
  xrdp_bitmap_get_screen_clip(bitmap, self, &clip_rect, &dx, &dy);
  region = xrdp_region_create(self->wm);
  if (box_right - box_left > 1)
  {
    xrdp_wm_get_vis_region(self->wm, bitmap, box_left, box_top,
                           box_right - box_left, box_bottom - box_top,
                           region, self->clip_children);
  }
  else
  {
    xrdp_wm_get_vis_region(self->wm, bitmap, clip_left, clip_top,
                           clip_right - clip_left, clip_bottom - clip_top,
                           region, self->clip_children);
  }
  clip_left += dx;
  clip_top += dy;
  clip_right += dx;
  clip_bottom += dy;
  box_left += dx;
  box_top += dy;
  box_right += dx;
  box_bottom += dy;
  x += dx;
  y += dy;
  k = 0;
  while (xrdp_region_get_rect(region, k, &rect) == 0)
  {
    if (rect_intersect(&rect, &clip_rect, &draw_rect))
    {
      libxrdp_orders_text(self->session, font, flags, mixmode,
                          self->fg_color, self->bg_color,
                          clip_left, clip_top, clip_right, clip_bottom,
                          box_left, box_top, box_right, box_bottom,
                          x, y, data, data_len, &draw_rect);
    }
    k++;
  }
  xrdp_region_delete(region);
  return 0;
}

/*****************************************************************************/
int APP_CC
xrdp_painter_copy(struct xrdp_painter* self,
                  struct xrdp_bitmap* src,
                  struct xrdp_bitmap* dst,
                  int x, int y, int cx, int cy,
                  int srcx, int srcy)
{
  struct xrdp_rect clip_rect;
  struct xrdp_rect draw_rect;
  struct xrdp_rect rect1;
  struct xrdp_rect rect2;
  struct xrdp_region* region;
  struct xrdp_bitmap* b;
  int i;
  int j;
  int k;
  int dx;
  int dy;
  int palette_id;
  int bitmap_id;
  int cache_id;
  int cache_idx;
  int dstx;
  int dsty;
  int w;
  int h;

  if (self == 0 || src == 0 || dst == 0)
  {
    return 0;
  }

  /* todo data */

  if (dst->type == WND_TYPE_BITMAP)
  {
    return 0;
  }
  if (src == dst && src->wm->screen == src)
  {
    xrdp_bitmap_get_screen_clip(dst, self, &clip_rect, &dx, &dy);
    region = xrdp_region_create(self->wm);
    xrdp_wm_get_vis_region(self->wm, dst, x, y, cx, cy,
                           region, self->clip_children);
    x += dx;
    y += dy;
    srcx += dx;
    srcy += dy;
    k = 0;
    while (xrdp_region_get_rect(region, k, &rect1) == 0)
    {
      if (rect_intersect(&rect1, &clip_rect, &draw_rect))
      {
        libxrdp_orders_screen_blt(self->session, x, y, cx, cy,
                                  srcx, srcy, self->rop, &draw_rect);
      }
      k++;
    }
    xrdp_region_delete(region);
  }
  else if (src->data != 0)
  /* todo, the non bitmap cache part is gone, it should be put back */
  {
    xrdp_bitmap_get_screen_clip(dst, self, &clip_rect, &dx, &dy);
    region = xrdp_region_create(self->wm);
    xrdp_wm_get_vis_region(self->wm, dst, x, y, cx, cy,
                           region, self->clip_children);
    x += dx;
    y += dy;
    palette_id = 0;
    j = srcy;
    while (j < src->height)
    {
      i = srcx;
      while (i < src->width)
      {
        w = MIN(64, src->width - i);
        h = MIN(64, src->height - j);
        b = xrdp_bitmap_create(w, h, self->wm->screen->bpp, 0, self->wm);
        xrdp_bitmap_copy_box_with_crc(src, b, i, j, w, h);
        bitmap_id = xrdp_cache_add_bitmap(self->wm->cache, b);
        cache_id = HIWORD(bitmap_id);
        cache_idx = LOWORD(bitmap_id);
        dstx = (x + i) - srcx;
        dsty = (y + j) - srcy;
        k = 0;
        while (xrdp_region_get_rect(region, k, &rect1) == 0)
        {
          if (rect_intersect(&rect1, &clip_rect, &rect2))
          {
            MAKERECT(rect1, dstx, dsty, w, h);
            if (rect_intersect(&rect2, &rect1, &draw_rect))
            {
              libxrdp_orders_mem_blt(self->session, cache_id, palette_id,
                                     dstx, dsty, w, h, self->rop, 0, 0,
                                     cache_idx, &draw_rect);
            }
          }
          k++;
        }
        i += 64;
      }
      j += 64;
    }
    xrdp_region_delete(region);
  }
  return 0;
}

/*****************************************************************************/
int APP_CC
xrdp_painter_line(struct xrdp_painter* self,
                  struct xrdp_bitmap* bitmap,
                  int x1, int y1, int x2, int y2)
{
  struct xrdp_rect clip_rect;
  struct xrdp_rect draw_rect;
  struct xrdp_rect rect;
  struct xrdp_region* region;
  int k;
  int dx;
  int dy;
  int rop;

  if (self == 0)
  {
    return 0;
  }

  /* todo data */

  if (bitmap->type == WND_TYPE_BITMAP)
  {
    return 0;
  }
  xrdp_bitmap_get_screen_clip(bitmap, self, &clip_rect, &dx, &dy);
  region = xrdp_region_create(self->wm);
  xrdp_wm_get_vis_region(self->wm, bitmap, MIN(x1, x2), MIN(y1, y2),
                         g_abs(x1 - x2) + 1, g_abs(y1 - y2) + 1,
                         region, self->clip_children);
  x1 += dx;
  y1 += dy;
  x2 += dx;
  y2 += dy;
  k = 0;
  rop = self->rop;
  if (rop < 0x01 || rop > 0x10)
  {
    rop = (rop & 0xf) + 1;
  }
  while (xrdp_region_get_rect(region, k, &rect) == 0)
  {
    if (rect_intersect(&rect, &clip_rect, &draw_rect))
    {
      libxrdp_orders_line(self->session, 0, x1, y1, x2, y2,
                          rop, self->bg_color,
                          &self->pen, &draw_rect);
    }
    k++;
  }
  xrdp_region_delete(region);
  return 0;
}