summaryrefslogtreecommitdiffstats
path: root/xrdp/xrdp_cache.c
blob: 7f5aa95d8d95cdd64860bcc515210b775961c9e8 (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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
/**
 * xrdp: A Remote Desktop Protocol server.
 *
 * Copyright (C) Jay Sorg 2004-2014
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * cache
 */

#if defined(HAVE_CONFIG_H)
#include <config_ac.h>
#endif

#include "xrdp.h"
#include "log.h"

#define LLOG_LEVEL 1
#define LLOGLN(_level, _args) \
  do \
  { \
    if (_level < LLOG_LEVEL) \
    { \
        g_write("xrdp:xrdp_cache [%10.10u]: ", g_time3()); \
        g_writeln _args ; \
    } \
  } \
  while (0)

/*****************************************************************************/
static int
xrdp_cache_reset_lru(struct xrdp_cache *self)
{
    int index;
    int jndex;
    struct xrdp_lru_item *lru;

    for (index = 0; index < XRDP_MAX_BITMAP_CACHE_ID; index++)
    {
        /* fist item */
        lru = &(self->bitmap_lrus[index][0]);
        lru->next = 1;
        lru->prev = -1;
        /* middle items */
        for (jndex = 1; jndex < XRDP_MAX_BITMAP_CACHE_IDX - 1; jndex++)
        {
            lru = &(self->bitmap_lrus[index][jndex]);
            lru->next = jndex + 1;
            lru->prev = jndex - 1;
        }
        /* last item */
        lru = &(self->bitmap_lrus[index][XRDP_MAX_BITMAP_CACHE_IDX - 1]);
        lru->next = -1;
        lru->prev = XRDP_MAX_BITMAP_CACHE_IDX - 2;

        self->lru_head[index] = 0;
        self->lru_tail[index] = XRDP_MAX_BITMAP_CACHE_IDX - 1;

        self->lru_reset[index] = 1;
    }
    return 0;
}

/*****************************************************************************/
static int
xrdp_cache_reset_crc(struct xrdp_cache *self)
{
    int index;
    int jndex;

    for (index = 0; index < XRDP_MAX_BITMAP_CACHE_ID; index++)
    {
        for (jndex = 0; jndex < 64 * 1024; jndex++)
        {
            /* it's ok to deinit a zeroed out struct list16 */
            list16_deinit(&(self->crc16[index][jndex]));
            list16_init(&(self->crc16[index][jndex]));
        }
    }
    return 0;
}

/*****************************************************************************/
struct xrdp_cache *
xrdp_cache_create(struct xrdp_wm *owner,
                  struct xrdp_session *session,
                  struct xrdp_client_info *client_info)
{
    struct xrdp_cache *self;

    self = (struct xrdp_cache *)g_malloc(sizeof(struct xrdp_cache), 1);
    self->wm = owner;
    self->session = session;
    self->use_bitmap_comp = client_info->use_bitmap_comp;

    self->cache1_entries = MIN(XRDP_MAX_BITMAP_CACHE_IDX,
                               client_info->cache1_entries);
    self->cache1_entries = MAX(self->cache1_entries, 0);
    self->cache1_size = client_info->cache1_size;

    self->cache2_entries = MIN(XRDP_MAX_BITMAP_CACHE_IDX,
                               client_info->cache2_entries);
    self->cache2_entries = MAX(self->cache2_entries, 0);
    self->cache2_size = client_info->cache2_size;

    self->cache3_entries = MIN(XRDP_MAX_BITMAP_CACHE_IDX,
                               client_info->cache3_entries);
    self->cache3_entries = MAX(self->cache3_entries, 0);
    self->cache3_size = client_info->cache3_size;

    self->bitmap_cache_persist_enable = client_info->bitmap_cache_persist_enable;
    self->bitmap_cache_version = client_info->bitmap_cache_version;
    self->pointer_cache_entries = client_info->pointer_cache_entries;
    self->xrdp_os_del_list = list_create();
    xrdp_cache_reset_lru(self);
    xrdp_cache_reset_crc(self);
    LLOGLN(10, ("xrdp_cache_create: 0 %d 1 %d 2 %d",
                self->cache1_entries, self->cache2_entries, self->cache3_entries));
    return self;
}

/*****************************************************************************/
void
xrdp_cache_delete(struct xrdp_cache *self)
{
    int i;
    int j;

    if (self == 0)
    {
        return;
    }

    /* free all the cached bitmaps */
    for (i = 0; i < XRDP_MAX_BITMAP_CACHE_ID; i++)
    {
        for (j = 0; j < XRDP_MAX_BITMAP_CACHE_IDX; j++)
        {
            xrdp_bitmap_delete(self->bitmap_items[i][j].bitmap);
        }
    }

    /* free all the cached font items */
    for (i = 0; i < 12; i++)
    {
        for (j = 0; j < 256; j++)
        {
            g_free(self->char_items[i][j].font_item.data);
        }
    }

    /* free all the off screen bitmaps */
    for (i = 0; i < 2000; i++)
    {
        xrdp_bitmap_delete(self->os_bitmap_items[i].bitmap);
    }

    list_delete(self->xrdp_os_del_list);

    /* free all crc lists */
    for (i = 0; i < XRDP_MAX_BITMAP_CACHE_ID; i++)
    {
        for (j = 0; j < 64 * 1024; j++)
        {
            list16_deinit(&(self->crc16[i][j]));
        }
    }

    g_free(self);
}

/*****************************************************************************/
int
xrdp_cache_reset(struct xrdp_cache *self,
                 struct xrdp_client_info *client_info)
{
    struct xrdp_wm *wm;
    struct xrdp_session *session;
    int i;
    int j;

    /* free all the cached bitmaps */
    for (i = 0; i < XRDP_MAX_BITMAP_CACHE_ID; i++)
    {
        for (j = 0; j < XRDP_MAX_BITMAP_CACHE_IDX; j++)
        {
            xrdp_bitmap_delete(self->bitmap_items[i][j].bitmap);
        }
    }

    /* free all the cached font items */
    for (i = 0; i < 12; i++)
    {
        for (j = 0; j < 256; j++)
        {
            g_free(self->char_items[i][j].font_item.data);
        }
    }

    /* save these */
    wm = self->wm;
    session = self->session;
    /* set whole struct to zero */
    g_memset(self, 0, sizeof(struct xrdp_cache));
    /* set some stuff back */
    self->wm = wm;
    self->session = session;
    self->use_bitmap_comp = client_info->use_bitmap_comp;
    self->cache1_entries = client_info->cache1_entries;
    self->cache1_size = client_info->cache1_size;
    self->cache2_entries = client_info->cache2_entries;
    self->cache2_size = client_info->cache2_size;
    self->cache3_entries = client_info->cache3_entries;
    self->cache3_size = client_info->cache3_size;
    self->bitmap_cache_persist_enable = client_info->bitmap_cache_persist_enable;
    self->bitmap_cache_version = client_info->bitmap_cache_version;
    self->pointer_cache_entries = client_info->pointer_cache_entries;
    xrdp_cache_reset_lru(self);
    xrdp_cache_reset_crc(self);
    return 0;
}

#define COMPARE_WITH_CRC32(_b1, _b2) \
 ((_b1 != 0) && (_b2 != 0) && (_b1->crc32 == _b2->crc32) && \
  (_b1->bpp == _b2->bpp) && \
  (_b1->width == _b2->width) && (_b1->height == _b2->height))

/*****************************************************************************/
static int
xrdp_cache_update_lru(struct xrdp_cache *self, int cache_id, int lru_index)
{
    int tail_index;
    struct xrdp_lru_item *nextlru;
    struct xrdp_lru_item *prevlru;
    struct xrdp_lru_item *thislru;
    struct xrdp_lru_item *taillru;

    LLOGLN(10, ("xrdp_cache_update_lru: lru_index %d", lru_index));
    if ((lru_index < 0) || (lru_index >= XRDP_MAX_BITMAP_CACHE_IDX))
    {
        LLOGLN(0, ("xrdp_cache_update_lru: error"));
        return 1;
    }
    if (self->lru_tail[cache_id] == lru_index)
    {
        /* nothing to do */
        return 0;
    }
    else if (self->lru_head[cache_id] == lru_index)
    {
        /* moving head item to tail */

        thislru = &(self->bitmap_lrus[cache_id][lru_index]);
        nextlru = &(self->bitmap_lrus[cache_id][thislru->next]);
        tail_index = self->lru_tail[cache_id];
        taillru = &(self->bitmap_lrus[cache_id][tail_index]);

        /* unhook old */
        nextlru->prev = -1;

        /* set head to next */
        self->lru_head[cache_id] = thislru->next;

        /* move to tail and hook up */
        taillru->next = lru_index;
        thislru->prev = tail_index;
        thislru->next = -1;

        /* update tail */
        self->lru_tail[cache_id] = lru_index;

    }
    else
    {
        /* move middle item */

        thislru = &(self->bitmap_lrus[cache_id][lru_index]);
        prevlru = &(self->bitmap_lrus[cache_id][thislru->prev]);
        nextlru = &(self->bitmap_lrus[cache_id][thislru->next]);
        tail_index = self->lru_tail[cache_id];
        taillru = &(self->bitmap_lrus[cache_id][tail_index]);

        /* unhook old */
        prevlru->next = thislru->next;
        nextlru->prev = thislru->prev;

        /* move to tail and hook up */
        taillru->next = lru_index;
        thislru->prev = tail_index;
        thislru->next = -1;

        /* update tail */
        self->lru_tail[cache_id] = lru_index;
    }
    return 0;
}

/*****************************************************************************/
/* returns cache id */
int
xrdp_cache_add_bitmap(struct xrdp_cache *self, struct xrdp_bitmap *bitmap,
                      int hints)
{
    int index;
    int jndex;
    int cache_id;
    int cache_idx;
    int bmp_size;
    int e;
    int Bpp;
    int crc16;
    int iig;
    int found;
    int cache_entries;
    int lru_index;
    struct list16 *ll;
    struct xrdp_bitmap *lbm;
    struct xrdp_lru_item *llru;

    LLOGLN(10, ("xrdp_cache_add_bitmap:"));
    LLOGLN(10, ("xrdp_cache_add_bitmap: crc16 0x%4.4x",
           bitmap->crc16));

    e = (4 - (bitmap->width % 4)) & 3;
    found = 0;
    cache_id = 0;
    cache_entries = 0;

    /* client Bpp, bmp_size */
    Bpp = (bitmap->bpp + 7) / 8;
    bmp_size = (bitmap->width + e) * bitmap->height * Bpp;
    self->bitmap_stamp++;

    if (bmp_size <= self->cache1_size)
    {
        cache_id = 0;
        cache_entries = self->cache1_entries;
    }
    else if (bmp_size <= self->cache2_size)
    {
        cache_id = 1;
        cache_entries = self->cache2_entries;
    }
    else if (bmp_size <= self->cache3_size)
    {
        cache_id = 2;
        cache_entries = self->cache3_entries;
    }
    else
    {
        log_message(LOG_LEVEL_ERROR, "error in xrdp_cache_add_bitmap, "
                    "too big(%d) bpp %d", bmp_size, bitmap->bpp);
        return 0;
    }

    crc16 = bitmap->crc16;
    ll = &(self->crc16[cache_id][crc16]);
    for (jndex = 0; jndex < ll->count; jndex++)
    {
        cache_idx = list16_get_item(ll, jndex);
        if (COMPARE_WITH_CRC32
                 (self->bitmap_items[cache_id][cache_idx].bitmap, bitmap))
        {
            LLOGLN(10, ("found bitmap at %d %d", index, jndex));
            found = 1;
            break;
        }
    }
    if (found)
    {
        lru_index = self->bitmap_items[cache_id][cache_idx].lru_index;
        self->bitmap_items[cache_id][cache_idx].stamp = self->bitmap_stamp;
        xrdp_bitmap_delete(bitmap);

        /* update lru to end */
        xrdp_cache_update_lru(self, cache_id, lru_index);

        return MAKELONG(cache_idx, cache_id);
    }

    /* find lru */

    /* check for reset */
    if (self->lru_reset[cache_id])
    {
        self->lru_reset[cache_id] = 0;
        LLOGLN(0, ("xrdp_cache_add_bitmap: reset detected cache_id %d",
               cache_id));
        self->lru_tail[cache_id] = cache_entries - 1;
        index = self->lru_tail[cache_id];
        llru = &(self->bitmap_lrus[cache_id][index]);
        llru->next = -1;
    }

    /* lru is item at head */
    lru_index = self->lru_head[cache_id];
    cache_idx = lru_index;

    /* update lru to end */
    xrdp_cache_update_lru(self, cache_id, lru_index);

    LLOGLN(10, ("xrdp_cache_add_bitmap: oldest %d %d", cache_id, cache_idx));

    LLOGLN(10, ("adding bitmap at %d %d old ptr %p new ptr %p",
           cache_id, cache_idx,
           self->bitmap_items[cache_id][cache_idx].bitmap,
           bitmap));

    /* remove old, about to be deleted, from crc16 list */
    lbm = self->bitmap_items[cache_id][cache_idx].bitmap;
    if (lbm != 0)
    {
        crc16 = lbm->crc16;
        ll = &(self->crc16[cache_id][crc16]);
        iig = list16_index_of(ll, cache_idx);
        if (iig == -1)
        {
            LLOGLN(0, ("xrdp_cache_add_bitmap: error removing cache_idx"));
        }
        LLOGLN(10, ("xrdp_cache_add_bitmap: removing index %d from crc16 %d",
               iig, crc16));
        list16_remove_item(ll, iig);
        xrdp_bitmap_delete(lbm);
    }

    /* set, send bitmap and return */

    self->bitmap_items[cache_id][cache_idx].bitmap = bitmap;
    self->bitmap_items[cache_id][cache_idx].stamp = self->bitmap_stamp;
    self->bitmap_items[cache_id][cache_idx].lru_index = lru_index;

    /* add to crc16 list */
    crc16 = bitmap->crc16;
    ll = &(self->crc16[cache_id][crc16]);
    list16_add_item(ll, cache_idx);
    if (ll->count > 1)
    {
        LLOGLN(10, ("xrdp_cache_add_bitmap: count %d", ll->count));
    }

    if (self->use_bitmap_comp)
    {
        if (self->bitmap_cache_version & 4)
        {
            if (libxrdp_orders_send_bitmap3(self->session, bitmap->width,
                                            bitmap->height, bitmap->bpp,
                                            bitmap->data, cache_id, cache_idx,
                                            hints) == 0)
            {
                return MAKELONG(cache_idx, cache_id);
            }
        }

        if (self->bitmap_cache_version & 2)
        {
            libxrdp_orders_send_bitmap2(self->session, bitmap->width,
                                        bitmap->height, bitmap->bpp,
                                        bitmap->data, cache_id, cache_idx,
                                        hints);
        }
        else if (self->bitmap_cache_version & 1)
        {
            libxrdp_orders_send_bitmap(self->session, bitmap->width,
                                       bitmap->height, bitmap->bpp,
                                       bitmap->data, cache_id, cache_idx);
        }
    }
    else
    {
        if (self->bitmap_cache_version & 2)
        {
            libxrdp_orders_send_raw_bitmap2(self->session, bitmap->width,
                                            bitmap->height, bitmap->bpp,
                                            bitmap->data, cache_id, cache_idx);
        }
        else if (self->bitmap_cache_version & 1)
        {
            libxrdp_orders_send_raw_bitmap(self->session, bitmap->width,
                                           bitmap->height, bitmap->bpp,
                                           bitmap->data, cache_id, cache_idx);
        }
    }

    return MAKELONG(cache_idx, cache_id);
}

/*****************************************************************************/
/* not used */
/* not sure how to use a palette in rdp */
int
xrdp_cache_add_palette(struct xrdp_cache *self, int *palette)
{
    int i;
    int oldest;
    int index;

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

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

    if (self->wm->screen->bpp > 8)
    {
        return 0;
    }

    self->palette_stamp++;

    /* look for match */
    for (i = 0; i < 6; i++)
    {
        if (g_memcmp(palette, self->palette_items[i].palette,
                     256 * sizeof(int)) == 0)
        {
            self->palette_items[i].stamp = self->palette_stamp;
            return i;
        }
    }

    /* look for oldest */
    index = 0;
    oldest = 0x7fffffff;

    for (i = 0; i < 6; i++)
    {
        if (self->palette_items[i].stamp < oldest)
        {
            oldest = self->palette_items[i].stamp;
            index = i;
        }
    }

    /* set, send palette and return */
    g_memcpy(self->palette_items[index].palette, palette, 256 * sizeof(int));
    self->palette_items[index].stamp = self->palette_stamp;
    libxrdp_orders_send_palette(self->session, palette, index);
    return index;
}

/*****************************************************************************/
int
xrdp_cache_add_char(struct xrdp_cache *self,
                    struct xrdp_font_char *font_item)
{
    int i;
    int j;
    int oldest;
    int f;
    int c;
    int datasize;
    struct xrdp_font_char *fi;

    self->char_stamp++;

    /* look for match */
    for (i = 7; i < 12; i++)
    {
        for (j = 0; j < 250; j++)
        {
            if (xrdp_font_item_compare(&self->char_items[i][j].font_item, font_item))
            {
                self->char_items[i][j].stamp = self->char_stamp;
                DEBUG(("found font at %d %d", i, j));
                return MAKELONG(j, i);
            }
        }
    }

    /* look for oldest */
    f = 0;
    c = 0;
    oldest = 0x7fffffff;

    for (i = 7; i < 12; i++)
    {
        for (j = 0; j < 250; j++)
        {
            if (self->char_items[i][j].stamp < oldest)
            {
                oldest = self->char_items[i][j].stamp;
                f = i;
                c = j;
            }
        }
    }

    DEBUG(("adding char at %d %d", f, c));
    /* set, send char and return */
    fi = &self->char_items[f][c].font_item;
    g_free(fi->data);
    datasize = FONT_DATASIZE(font_item);
    fi->data = (char *)g_malloc(datasize, 1);
    g_memcpy(fi->data, font_item->data, datasize);
    fi->offset = font_item->offset;
    fi->baseline = font_item->baseline;
    fi->width = font_item->width;
    fi->height = font_item->height;
    self->char_items[f][c].stamp = self->char_stamp;
    libxrdp_orders_send_font(self->session, fi, f, c);
    return MAKELONG(c, f);
}

/*****************************************************************************/
/* added the pointer to the cache and send it to client, it also sets the
   client if it finds it
   returns the index in the cache
   does not take ownership of pointer_item */
int
xrdp_cache_add_pointer(struct xrdp_cache *self,
                       struct xrdp_pointer_item *pointer_item)
{
    int i;
    int oldest;
    int index;

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

    self->pointer_stamp++;

    /* look for match */
    for (i = 2; i < self->pointer_cache_entries; i++)
    {
        if (self->pointer_items[i].x == pointer_item->x &&
                self->pointer_items[i].y == pointer_item->y &&
                g_memcmp(self->pointer_items[i].data,
                         pointer_item->data, 32 * 32 * 4) == 0 &&
                g_memcmp(self->pointer_items[i].mask,
                         pointer_item->mask, 32 * 32 / 8) == 0 &&
                self->pointer_items[i].bpp == pointer_item->bpp)
        {
            self->pointer_items[i].stamp = self->pointer_stamp;
            xrdp_wm_set_pointer(self->wm, i);
            self->wm->current_pointer = i;
            DEBUG(("found pointer at %d", i));
            return i;
        }
    }

    /* look for oldest */
    index = 2;
    oldest = 0x7fffffff;

    for (i = 2; i < self->pointer_cache_entries; i++)
    {
        if (self->pointer_items[i].stamp < oldest)
        {
            oldest = self->pointer_items[i].stamp;
            index = i;
        }
    }

    self->pointer_items[index].x = pointer_item->x;
    self->pointer_items[index].y = pointer_item->y;
    g_memcpy(self->pointer_items[index].data,
             pointer_item->data, 32 * 32 * 4);
    g_memcpy(self->pointer_items[index].mask,
             pointer_item->mask, 32 * 32 / 8);
    self->pointer_items[index].stamp = self->pointer_stamp;
    self->pointer_items[index].bpp = pointer_item->bpp;
    xrdp_wm_send_pointer(self->wm, index,
                         self->pointer_items[index].data,
                         self->pointer_items[index].mask,
                         self->pointer_items[index].x,
                         self->pointer_items[index].y,
                         self->pointer_items[index].bpp);
    self->wm->current_pointer = index;
    DEBUG(("adding pointer at %d", index));
    return index;
}

/*****************************************************************************/
/* this does not take ownership of pointer_item, it makes a copy */
int
xrdp_cache_add_pointer_static(struct xrdp_cache *self,
                              struct xrdp_pointer_item *pointer_item,
                              int index)
{

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

    self->pointer_items[index].x = pointer_item->x;
    self->pointer_items[index].y = pointer_item->y;
    g_memcpy(self->pointer_items[index].data,
             pointer_item->data, 32 * 32 * 4);
    g_memcpy(self->pointer_items[index].mask,
             pointer_item->mask, 32 * 32 / 8);
    self->pointer_items[index].stamp = self->pointer_stamp;
    self->pointer_items[index].bpp = pointer_item->bpp;
    xrdp_wm_send_pointer(self->wm, index,
                         self->pointer_items[index].data,
                         self->pointer_items[index].mask,
                         self->pointer_items[index].x,
                         self->pointer_items[index].y,
                         self->pointer_items[index].bpp);
    self->wm->current_pointer = index;
    DEBUG(("adding pointer at %d", index));
    return index;
}

/*****************************************************************************/
/* this does not take ownership of brush_item_data, it makes a copy */
int
xrdp_cache_add_brush(struct xrdp_cache *self,
                     char *brush_item_data)
{
    int i;
    int oldest;
    int index;

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

    self->brush_stamp++;

    /* look for match */
    for (i = 0; i < 64; i++)
    {
        if (g_memcmp(self->brush_items[i].pattern,
                     brush_item_data, 8) == 0)
        {
            self->brush_items[i].stamp = self->brush_stamp;
            DEBUG(("found brush at %d", i));
            return i;
        }
    }

    /* look for oldest */
    index = 0;
    oldest = 0x7fffffff;

    for (i = 0; i < 64; i++)
    {
        if (self->brush_items[i].stamp < oldest)
        {
            oldest = self->brush_items[i].stamp;
            index = i;
        }
    }

    g_memcpy(self->brush_items[index].pattern,
             brush_item_data, 8);
    self->brush_items[index].stamp = self->brush_stamp;
    libxrdp_orders_send_brush(self->session, 8, 8, 1, 0x81, 8,
                              self->brush_items[index].pattern, index);
    DEBUG(("adding brush at %d", index));
    return index;
}

/*****************************************************************************/
/* returns error */
int
xrdp_cache_add_os_bitmap(struct xrdp_cache *self, struct xrdp_bitmap *bitmap,
                         int rdpindex)
{
    struct xrdp_os_bitmap_item *bi;

    if ((rdpindex < 0) || (rdpindex >= 2000))
    {
        return 1;
    }

    bi = self->os_bitmap_items + rdpindex;
    bi->bitmap = bitmap;
    return 0;
}

/*****************************************************************************/
/* returns error */
int
xrdp_cache_remove_os_bitmap(struct xrdp_cache *self, int rdpindex)
{
    struct xrdp_os_bitmap_item *bi;
    int index;

    if ((rdpindex < 0) || (rdpindex >= 2000))
    {
        return 1;
    }

    bi = self->os_bitmap_items + rdpindex;

    if (bi->bitmap->tab_stop)
    {
        index = list_index_of(self->xrdp_os_del_list, rdpindex);

        if (index == -1)
        {
            list_add_item(self->xrdp_os_del_list, rdpindex);
        }
    }

    xrdp_bitmap_delete(bi->bitmap);
    g_memset(bi, 0, sizeof(struct xrdp_os_bitmap_item));
    return 0;
}

/*****************************************************************************/
struct xrdp_os_bitmap_item *
xrdp_cache_get_os_bitmap(struct xrdp_cache *self, int rdpindex)
{
    struct xrdp_os_bitmap_item *bi;

    if ((rdpindex < 0) || (rdpindex >= 2000))
    {
        return 0;
    }

    bi = self->os_bitmap_items + rdpindex;
    return bi;
}