summaryrefslogtreecommitdiffstats
path: root/xrdp/xrdp_painter.c
blob: cc585b75bf42679705f5efb00781ef980ab90514 (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
/*
   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-2005

   painter, gc

*/

#include "xrdp.h"

/*****************************************************************************/
struct xrdp_painter* xrdp_painter_create(struct xrdp_wm* wm)
{
  struct xrdp_painter* self;

  self = (struct xrdp_painter*)g_malloc(sizeof(struct xrdp_painter), 1);
  self->wm = wm;
  self->orders = wm->orders;
  self->rop = 0xcc; /* copy */
  self->font = xrdp_font_create(wm);
  self->clip_children = 1;
  return self;
}

/*****************************************************************************/
void xrdp_painter_delete(struct xrdp_painter* self)
{
  if (self == 0)
  {
    return;
  }
  xrdp_font_delete(self->font);
  g_free(self);
}

/*****************************************************************************/
int xrdp_painter_begin_update(struct xrdp_painter* self)
{
  xrdp_orders_init(self->orders);
  return 0;
}

/*****************************************************************************/
int xrdp_painter_end_update(struct xrdp_painter* self)
{
  xrdp_orders_send(self->orders);
  return 0;
}

/*****************************************************************************/
/* returns boolean, true if there is something to draw */
int 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 xrdp_painter_set_clip(struct xrdp_painter* self,
                          int x, int y, int cx, int cy)
{
  self->use_clip = 1;
  self->clip.left = x;
  self->clip.top = y;
  self->clip.right = x + cx;
  self->clip.bottom = y + cy;
  return 0;
}

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

/*****************************************************************************/
int 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;
}

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

  if (!check_bounds(bitmap, &x, &y, &cx, &cy))
  {
    return 0;
  }
  if (!xrdp_painter_clip_adj(self, &x, &y, &cx, &cy))
  {
    return 0;
  }

  /* todo data */

  if (bitmap->type == WND_TYPE_BITMAP) /* 0 */
  {
    return 0;
  }
  region = xrdp_region_create(self->wm);
  xrdp_wm_get_vis_region(self->wm, bitmap, x, y, cx, cy, region,
                         self->clip_children);
  i = 0;
  while (xrdp_region_get_rect(region, i, &rect) == 0)
  {
    if (!ISRECTEMPTY(rect))
    {
      DEBUG(("sending rect order %d %d %d %d\n\r",
             rect.left, rect.top,
             rect.right, rect.bottom));
      xrdp_orders_rect(self->orders, rect.left, rect.top,
                       rect.right - rect.left,
                       rect.bottom - rect.top,
                       self->fg_color, 0);
    }
    i++;
  }
  xrdp_region_delete(region);
  return 0;
}

/*****************************************************************************/
/* fill in an area of the screen with opcodes and patterns */
/* todo, this needs work */
int xrdp_painter_fill_rect2(struct xrdp_painter* self,
                            struct xrdp_bitmap* bitmap,
                            int x, int y, int cx, int cy)
{
  int i;
  struct xrdp_region* region;
  struct xrdp_rect rect;

  if (!check_bounds(bitmap, &x, &y, &cx, &cy))
  {
    return 0;
  }
  if (!xrdp_painter_clip_adj(self, &x, &y, &cx, &cy))
  {
    return 0;
  }

  /* todo data */

  if (bitmap->type == WND_TYPE_BITMAP) /* 0 */
  {
    return 0;
  }
  region = xrdp_region_create(self->wm);
  xrdp_wm_get_vis_region(self->wm, bitmap, x, y, cx, cy, region,
                         self->clip_children);
  i = 0;
  while (xrdp_region_get_rect(region, i, &rect) == 0)
  {
    if (!ISRECTEMPTY(rect))
    {
      DEBUG(("sending rect2 order %d %d %d %d\n\r",
             rect.left, rect.top,
             rect.right, rect.bottom));
      xrdp_orders_pat_blt(self->orders, rect.left, rect.top,
                          rect.right - rect.left,
                          rect.bottom - rect.top,
                          self->rop, self->bg_color, self->fg_color,
                          &self->brush, 0);
    }
    i++;
  }
  xrdp_region_delete(region);
  return 0;
}

#define SSW 64
#define SSH 63

/*****************************************************************************/
int xrdp_painter_draw_bitmap(struct xrdp_painter* self,
                             struct xrdp_bitmap* bitmap,
                             struct xrdp_bitmap* to_draw,
                             int x, int y, int cx, int cy)
{
  int i;
  int j;
  int k;
  int w;
  int h;
  int x1;
  int y1;
  int ok;
  int srcx;
  int srcy;
  int bitmap_id;
  int cache_id;
  int cache_idx;
  int palette_id;
  struct xrdp_region* region;
  struct xrdp_rect rect;
  struct xrdp_rect rect1;
  struct xrdp_rect rect2;
  struct xrdp_bitmap* b;

  /* todo data */

  if (bitmap->type == WND_TYPE_BITMAP)
  {
    return 0;
  }
  region = xrdp_region_create(self->wm);
  xrdp_wm_get_vis_region(self->wm, bitmap, x, y, cx, cy, region,
                         self->clip_children);
  b = bitmap;
  while (b != 0)
  {
    x = x + b->left;
    y = y + b->top;
    b = b->parent;
  }
  if (self->wm->client_info->use_bitmap_cache)
  {
    /*palette_id = xrdp_cache_add_palette(self->wm->cache, self->wm->palette);*/
    palette_id = 0;
    j = 0;
    while (j < to_draw->height)
    {
      i = 0;
      while (i < to_draw->width)
      {
        x1 = x + i;
        y1 = y + j;
        w = MIN(SSW, to_draw->width - i);
        h = MIN(SSH, to_draw->height - j);
        b = xrdp_bitmap_create(w, h, self->wm->screen->bpp, 0, self->wm);
#ifdef USE_CRC
        xrdp_bitmap_copy_box_with_crc(to_draw, b, i, j, w, h);
#else
        xrdp_bitmap_copy_box(to_draw, b, i, j, w, h);
#endif
        bitmap_id = xrdp_cache_add_bitmap(self->wm->cache, b);
        cache_id = HIWORD(bitmap_id);
        cache_idx = LOWORD(bitmap_id);
        k = 0;
        while (xrdp_region_get_rect(region, k, &rect) == 0)
        {
          if (!ISRECTEMPTY(rect))
          {
            MAKERECT(rect1, x1, y1, w, h);
            if (rect_intersect(&rect, &rect1, &rect2))
            {
              ok = 1;
              if (self->use_clip)
              {
                rect = self->clip;
                RECTOFFSET(rect, x, y);
                if (!rect_intersect(&rect2, &rect, &rect1))
                {
                  ok = 0;
                }
              }
              else
              {
                rect1 = rect2;
              }
              if (ok)
              {
                rect1.right--;
                rect1.bottom--;
                /* check these so ms client don't crash */
                if (x1 + w >= self->wm->screen->width)
                {
                  w = self->wm->screen->width - x1;
                }
                if (y1 + h >= self->wm->screen->height)
                {
                  h = self->wm->screen->height - y1;
                }
                if (w > 0 && h > 0 && x1 + w > 0 && y1 + h > 0)
                {
                  srcx = 0;
                  srcy = 0;
                  if (x1 < 0)
                  {
                    w = w + x1;
                    srcx = srcx - x1;
                    x1 = 0;
                  }
                  if (y1 < 0)
                  {
                    h = h + y1;
                    srcy = srcy - y1;
                    y1 = 0;
                  }
                  xrdp_orders_mem_blt(self->orders, cache_id, palette_id,
                                      x1, y1, w, h, self->rop, srcx, srcy,
                                      cache_idx, &rect1);
                }
              }
            }
          }
          k++;
        }
        i += SSW;
      }
      j += SSH;
    }
  }
  else /* no bitmap cache */
  {
    /* make sure there is no waiting orders */
    xrdp_orders_force_send(self->orders);
    k = 0;
    while (xrdp_region_get_rect(region, k, &rect) == 0)
    {
      x1 = rect.left;
      y1 = rect.top;
      w = rect.right - rect.left;
      h = rect.bottom - rect.top;
      b = xrdp_bitmap_create(w, h, self->wm->screen->bpp, 0, self->wm);
      xrdp_bitmap_copy_box(to_draw, b, x1 - x, y1 - y, w, h);
      xrdp_wm_send_bitmap(self->wm, b, x1, y1, w, h);
      xrdp_bitmap_delete(b);
      k++;
    }
  }
  xrdp_region_delete(region);
  return 0;
}

/*****************************************************************************/
int xrdp_painter_text_width(struct xrdp_painter* self, char* text)
{
  int index;
  int rv;
  int len;
  struct xrdp_font_item* font_item;

  rv = 0;
  len = g_strlen(text);
  for (index = 0; index < len; index++)
  {
    font_item = self->font->font_items + (unsigned char)text[index];
    rv = rv + font_item->incby;
  }
  return rv;
}

/*****************************************************************************/
int xrdp_painter_text_height(struct xrdp_painter* self, char* text)
{
  int index;
  int rv;
  int len;
  struct xrdp_font_item* font_item;

  rv = 0;
  len = g_strlen(text);
  for (index = 0; index < len; index++)
  {
    font_item = self->font->font_items + (unsigned char)text[index];
    rv = MAX(rv, font_item->height);
  }
  return rv;
}

/*****************************************************************************/
int xrdp_painter_draw_text(struct xrdp_painter* self,
                           struct xrdp_bitmap* bitmap,
                           int x, int y, 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;
  char* data;
  struct xrdp_region* region;
  struct xrdp_rect rect;
  struct xrdp_rect clip_rect;
  struct xrdp_rect draw_rect;
  struct xrdp_bitmap* b;
  struct xrdp_font* font;
  struct xrdp_font_item* font_item;

  len = g_strlen(text);
  if (len < 1)
  {
    return 0;
  }

  /* todo data */

  if (bitmap->type == 0)
  {
    return 0;
  }
  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 + (unsigned char)text[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);
  }
  region = xrdp_region_create(self->wm);
  xrdp_wm_get_vis_region(self->wm, bitmap, x, y, total_width, total_height,
                         region, self->clip_children);
  b = bitmap;
  while (b != 0)
  {
    x = x + b->left;
    y = y + b->top;
    b = b->parent;
  }
  if (self->use_clip)
  {
    clip_rect = self->clip;
  }
  else
  {
    MAKERECT(clip_rect, 0, 0, bitmap->width, bitmap->height);
  }
  b = bitmap;
  while (b != 0)
  {
    RECTOFFSET(clip_rect, b->left, b->top);
    b = b->parent;
  }
  k = 0;
  while (xrdp_region_get_rect(region, k, &rect) == 0)
  {
    if (!ISRECTEMPTY(rect))
    {
      if (rect_intersect(&rect, &clip_rect, &draw_rect))
      {
        x1 = x;
        y1 = y + total_height;
        draw_rect.right--;
        draw_rect.bottom--;
        flags = 0x03; /* 0x73; TEXT2_IMPLICIT_X and something else */
        xrdp_orders_text(self->orders, f, flags, 0,
                         font->color, 0,
                         x, y, 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);
  return 0;
}