summaryrefslogtreecommitdiffstats
path: root/xrdp/xrdp_cache.c
blob: 20445b63313b08c53cad2c8ddd54f347bdf2daa9 (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

/*
   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

   cache

*/

#include "xrdp.h"

/*****************************************************************************/
struct xrdp_cache* xrdp_cache_create(struct xrdp_wm* owner,
                                     struct xrdp_orders* orders)
{
  struct xrdp_cache* self;

  self = (struct xrdp_cache*)g_malloc(sizeof(struct xrdp_cache), 1);
  self->wm = owner;
  self->orders = orders;
  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 < 3; i++)
    for (j = 0; j < 600; 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);
  g_free(self);
}

/*****************************************************************************/
/* returns cache id */
int xrdp_cache_add_bitmap(struct xrdp_cache* self, struct xrdp_bitmap* bitmap)
{
  int i;
  int j;
  int min_use;
  int cache_id;
  int cache_idx;
  struct xrdp_bitmap* b;

  /* look for match */
  for (i = 0; i < 3; i++)
  {
    for (j = 0; j < 600; j++)
    {
      if (xrdp_bitmap_compare(self->bitmap_items[i][j].bitmap, bitmap))
      {
        self->bitmap_items[i][j].use_count++;
        DEBUG(("found bitmap at %d %d\n", i, j));
        return MAKELONG(i, j);
      }
    }
  }
  /* look for least used */
  cache_id = 0;
  cache_idx = 0;
  min_use = 999999;
  for (i = 0; i < 3; i++)
  {
    for (j = 0; j < 600; j++)
    {
      if (self->bitmap_items[i][j].use_count < min_use)
      {
        min_use = self->bitmap_items[i][j].use_count;
        cache_id = i;
        cache_idx = j;
      }
    }
  }
  DEBUG(("adding bitmap at %d %d\n", cache_id, cache_idx));
  /* set, send bitmap and return */
  xrdp_bitmap_delete(self->bitmap_items[cache_id][cache_idx].bitmap);
  b = xrdp_bitmap_create(bitmap->width, bitmap->height, bitmap->bpp, 0);
  xrdp_bitmap_copy_box(bitmap, b, 0, 0, bitmap->width, bitmap->height);
  self->bitmap_items[cache_id][cache_idx].bitmap = b;
  self->bitmap_items[cache_id][cache_idx].use_count++;
  xrdp_orders_send_raw_bitmap(self->orders, b, cache_id, cache_idx);
  return MAKELONG(cache_id, cache_idx);
}

/*****************************************************************************/
int xrdp_cache_add_palette(struct xrdp_cache* self, int* palette)
{
  int i;
  int min_use;
  int min_use_index;

  if (self == 0)
    return 0;
  if (palette == 0)
    return 0;
  if (self->wm->screen->bpp > 8)
    return 0;
  /* 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].use_count++;
      return i;
    }
  }
  /* look for least used */
  min_use_index = 0;
  min_use = 999999;
  for (i = 0; i < 6; i++)
  {
    if (self->palette_items[i].use_count < min_use)
    {
      min_use = self->palette_items[i].use_count;
      min_use_index = i;
    }
  }
  /* set, send palette and return */
  g_memcpy(self->palette_items[min_use_index].palette, palette,
           256 * sizeof(int));
  self->palette_items[min_use_index].use_count++;
  xrdp_orders_send_palette(self->orders, palette, min_use_index);
  return min_use_index;
}

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

  /* 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].use_count++;
        DEBUG(("found font at %d %d\n\r", i, j));
        return MAKELONG(i, j);
      }
    }
  }
  /* look for least used */
  f = 0;
  c = 0;
  min_use = 999999;
  for (i = 7; i < 12; i++)
  {
    for (j = 0; j < 250; j++)
    {
      if (self->char_items[i][j].use_count < min_use)
      {
        min_use = self->char_items[i][j].use_count;
        f = i;
        c = j;
      }
    }
  }
  DEBUG(("adding char at %d %d\n\r", 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].use_count++;
  xrdp_orders_send_font(self->orders, fi, f, c);
  return MAKELONG(f, c);
}