1 |
/* |
2 |
rdesktop: A Remote Desktop Protocol client. |
3 |
Cache routines |
4 |
Copyright (C) Matthew Chapman 1999-2001 |
5 |
|
6 |
This program is free software; you can redistribute it and/or modify |
7 |
it under the terms of the GNU General Public License as published by |
8 |
the Free Software Foundation; either version 2 of the License, or |
9 |
(at your option) any later version. |
10 |
|
11 |
This program is distributed in the hope that it will be useful, |
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
GNU General Public License for more details. |
15 |
|
16 |
You should have received a copy of the GNU General Public License |
17 |
along with this program; if not, write to the Free Software |
18 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 |
*/ |
20 |
|
21 |
#include "rdesktop.h" |
22 |
|
23 |
#define NUM_ELEMENTS(array) (sizeof(array) / sizeof(array[0])) |
24 |
|
25 |
|
26 |
/* BITMAP CACHE */ |
27 |
static HBITMAP bmpcache[3][600]; |
28 |
|
29 |
/* Retrieve a bitmap from the cache */ |
30 |
HBITMAP |
31 |
cache_get_bitmap(uint8 cache_id, uint16 cache_idx) |
32 |
{ |
33 |
HBITMAP bitmap; |
34 |
|
35 |
if ((cache_id < NUM_ELEMENTS(bmpcache)) |
36 |
&& (cache_idx < NUM_ELEMENTS(bmpcache[0]))) |
37 |
{ |
38 |
bitmap = bmpcache[cache_id][cache_idx]; |
39 |
if (bitmap != NULL) |
40 |
return bitmap; |
41 |
} |
42 |
|
43 |
error("get bitmap %d:%d\n", cache_id, cache_idx); |
44 |
return NULL; |
45 |
} |
46 |
|
47 |
/* Store a bitmap in the cache */ |
48 |
void |
49 |
cache_put_bitmap(uint8 cache_id, uint16 cache_idx, HBITMAP bitmap) |
50 |
{ |
51 |
HBITMAP old; |
52 |
|
53 |
if ((cache_id < NUM_ELEMENTS(bmpcache)) |
54 |
&& (cache_idx < NUM_ELEMENTS(bmpcache[0]))) |
55 |
{ |
56 |
old = bmpcache[cache_id][cache_idx]; |
57 |
if (old != NULL) |
58 |
ui_destroy_bitmap(old); |
59 |
|
60 |
bmpcache[cache_id][cache_idx] = bitmap; |
61 |
} |
62 |
else |
63 |
{ |
64 |
error("put bitmap %d:%d\n", cache_id, cache_idx); |
65 |
} |
66 |
} |
67 |
|
68 |
|
69 |
/* FONT CACHE */ |
70 |
static FONTGLYPH fontcache[12][256]; |
71 |
|
72 |
/* Retrieve a glyph from the font cache */ |
73 |
FONTGLYPH * |
74 |
cache_get_font(uint8 font, uint16 character) |
75 |
{ |
76 |
FONTGLYPH *glyph; |
77 |
|
78 |
if ((font < NUM_ELEMENTS(fontcache)) |
79 |
&& (character < NUM_ELEMENTS(fontcache[0]))) |
80 |
{ |
81 |
glyph = &fontcache[font][character]; |
82 |
if (glyph->pixmap != NULL) |
83 |
return glyph; |
84 |
} |
85 |
|
86 |
error("get font %d:%d\n", font, character); |
87 |
return NULL; |
88 |
} |
89 |
|
90 |
/* Store a glyph in the font cache */ |
91 |
void |
92 |
cache_put_font(uint8 font, uint16 character, uint16 offset, |
93 |
uint16 baseline, uint16 width, uint16 height, HGLYPH pixmap) |
94 |
{ |
95 |
FONTGLYPH *glyph; |
96 |
|
97 |
if ((font < NUM_ELEMENTS(fontcache)) |
98 |
&& (character < NUM_ELEMENTS(fontcache[0]))) |
99 |
{ |
100 |
glyph = &fontcache[font][character]; |
101 |
if (glyph->pixmap != NULL) |
102 |
ui_destroy_glyph(glyph->pixmap); |
103 |
|
104 |
glyph->offset = offset; |
105 |
glyph->baseline = baseline; |
106 |
glyph->width = width; |
107 |
glyph->height = height; |
108 |
glyph->pixmap = pixmap; |
109 |
} |
110 |
else |
111 |
{ |
112 |
error("put font %d:%d\n", font, character); |
113 |
} |
114 |
} |
115 |
|
116 |
|
117 |
/* TEXT CACHE */ |
118 |
static DATABLOB textcache[256]; |
119 |
|
120 |
/* Retrieve a text item from the cache */ |
121 |
DATABLOB * |
122 |
cache_get_text(uint8 cache_id) |
123 |
{ |
124 |
DATABLOB *text; |
125 |
|
126 |
if (cache_id < NUM_ELEMENTS(textcache)) |
127 |
{ |
128 |
text = &textcache[cache_id]; |
129 |
if (text->data != NULL) |
130 |
return text; |
131 |
} |
132 |
|
133 |
error("get text %d\n", cache_id); |
134 |
return NULL; |
135 |
} |
136 |
|
137 |
/* Store a text item in the cache */ |
138 |
void |
139 |
cache_put_text(uint8 cache_id, void *data, int length) |
140 |
{ |
141 |
DATABLOB *text; |
142 |
|
143 |
if (cache_id < NUM_ELEMENTS(textcache)) |
144 |
{ |
145 |
text = &textcache[cache_id]; |
146 |
if (text->data != NULL) |
147 |
xfree(text->data); |
148 |
|
149 |
text->data = xmalloc(length); |
150 |
text->size = length; |
151 |
memcpy(text->data, data, length); |
152 |
} |
153 |
else |
154 |
{ |
155 |
error("put text %d\n", cache_id); |
156 |
} |
157 |
} |
158 |
|
159 |
|
160 |
/* DESKTOP CACHE */ |
161 |
static uint8 deskcache[0x38400 * 4]; |
162 |
|
163 |
/* Retrieve desktop data from the cache */ |
164 |
uint8 * |
165 |
cache_get_desktop(uint32 offset, int cx, int cy, int bytes_per_pixel) |
166 |
{ |
167 |
int length = cx * cy * bytes_per_pixel; |
168 |
|
169 |
if ((offset + length) <= sizeof(deskcache)) |
170 |
{ |
171 |
return &deskcache[offset]; |
172 |
} |
173 |
|
174 |
error("get desktop %d:%d\n", offset, length); |
175 |
return NULL; |
176 |
} |
177 |
|
178 |
/* Store desktop data in the cache */ |
179 |
void |
180 |
cache_put_desktop(uint32 offset, int cx, int cy, int scanline, |
181 |
int bytes_per_pixel, uint8 *data) |
182 |
{ |
183 |
int length = cx * cy * bytes_per_pixel; |
184 |
|
185 |
if ((offset + length) <= sizeof(deskcache)) |
186 |
{ |
187 |
cx *= bytes_per_pixel; |
188 |
while (cy--) |
189 |
{ |
190 |
memcpy(&deskcache[offset], data, cx); |
191 |
data += scanline; |
192 |
offset += cx; |
193 |
} |
194 |
} |
195 |
else |
196 |
{ |
197 |
error("put desktop %d:%d\n", offset, length); |
198 |
} |
199 |
} |
200 |
|
201 |
|
202 |
/* CURSOR CACHE */ |
203 |
static HCURSOR cursorcache[0x20]; |
204 |
|
205 |
/* Retrieve cursor from cache */ |
206 |
HCURSOR cache_get_cursor(uint16 cache_idx) |
207 |
{ |
208 |
HCURSOR cursor; |
209 |
|
210 |
if (cache_idx < NUM_ELEMENTS(cursorcache)) |
211 |
{ |
212 |
cursor = cursorcache[cache_idx]; |
213 |
if (cursor != NULL) |
214 |
return cursor; |
215 |
} |
216 |
|
217 |
error("get cursor %d\n", cache_idx); |
218 |
return NULL; |
219 |
} |
220 |
|
221 |
/* Store cursor in cache */ |
222 |
void |
223 |
cache_put_cursor(uint16 cache_idx, HCURSOR cursor) |
224 |
{ |
225 |
HCURSOR old; |
226 |
|
227 |
if (cache_idx < NUM_ELEMENTS(cursorcache)) |
228 |
{ |
229 |
old = cursorcache[cache_idx]; |
230 |
if (old != NULL) |
231 |
ui_destroy_cursor(old); |
232 |
|
233 |
cursorcache[cache_idx] = cursor; |
234 |
} |
235 |
else |
236 |
{ |
237 |
error("put cursor %d\n", cache_idx); |
238 |
} |
239 |
} |