/[rdesktop]/sourceforge.net/trunk/rdesktop/cache.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /sourceforge.net/trunk/rdesktop/cache.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 789 - (hide annotations)
Wed Oct 27 00:49:54 2004 UTC (19 years, 7 months ago) by jsorg71
File MIME type: text/plain
File size: 6595 byte(s)
remove compiler warning

1 matty 9 /*
2     rdesktop: A Remote Desktop Protocol client.
3     Cache routines
4 matthewc 207 Copyright (C) Matthew Chapman 1999-2002
5 jsorg71 774
6 matty 9 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 jsorg71 774
11 matty 9 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 jsorg71 774
16 matty 9 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 matty 10 #include "rdesktop.h"
22 matty 9
23 matty 10 #define NUM_ELEMENTS(array) (sizeof(array) / sizeof(array[0]))
24 jsorg71 725 #define TOUCH(id, idx) (g_bmpcache[id][idx].usage = ++g_stamp)
25     #define IS_PERSISTENT(id) (g_pstcache_fd[id] > 0)
26 matty 10
27 jsorg71 725 extern int g_pstcache_fd[];
28 jsorg71 774 extern BOOL g_use_rdp5;
29 matty 10
30 jsorg71 725 uint32 g_stamp;
31    
32 jsorg71 775 static int g_num_bitmaps_in_memory[3];
33 jsorg71 725
34 matty 10 /* BITMAP CACHE */
35 jsorg71 725 static BMPCACHEENTRY g_bmpcache[3][0xa00];
36 jsorg71 730 static HBITMAP g_volatile_bc[3];
37 matty 10
38 jsorg71 725 /* Remove the least-recently used bitmap from the cache */
39     void
40     cache_remove_lru_bitmap(uint8 cache_id)
41     {
42 jsorg71 777 uint32 i;
43 jsorg71 725 uint16 cache_idx = 0;
44 jsorg71 775 uint32 m = 0xffffffff;
45 jsorg71 725 BMPCACHEENTRY *pbce;
46    
47     for (i = 0; i < NUM_ELEMENTS(g_bmpcache[cache_id]); i++)
48     {
49     if (g_bmpcache[cache_id][i].bitmap && g_bmpcache[cache_id][i].usage < m)
50     {
51     cache_idx = i;
52     m = g_bmpcache[cache_id][i].usage;
53     }
54     }
55    
56     pbce = &g_bmpcache[cache_id][cache_idx];
57     ui_destroy_bitmap(pbce->bitmap);
58     --g_num_bitmaps_in_memory[cache_id];
59     pbce->bitmap = 0;
60     pbce->usage = 0;
61     }
62    
63 matty 10 /* Retrieve a bitmap from the cache */
64 matty 25 HBITMAP
65     cache_get_bitmap(uint8 cache_id, uint16 cache_idx)
66 matty 9 {
67 jsorg71 725 HBITMAP *pbitmap;
68 matty 9
69 jsorg71 379 if ((cache_id < NUM_ELEMENTS(g_bmpcache)) && (cache_idx < NUM_ELEMENTS(g_bmpcache[0])))
70 matty 9 {
71 jsorg71 725 pbitmap = &g_bmpcache[cache_id][cache_idx].bitmap;
72     if ((*pbitmap != 0) || pstcache_load_bitmap(cache_id, cache_idx))
73     {
74     if (IS_PERSISTENT(cache_id))
75     TOUCH(cache_id, cache_idx);
76    
77     return *pbitmap;
78     }
79 matty 9 }
80 jsorg71 730 else if ((cache_id < NUM_ELEMENTS(g_volatile_bc)) && (cache_idx == 0x7fff))
81     {
82     return g_volatile_bc[cache_id];
83     }
84 matty 9
85 matty 30 error("get bitmap %d:%d\n", cache_id, cache_idx);
86 matty 9 return NULL;
87     }
88    
89 matty 10 /* Store a bitmap in the cache */
90 matty 25 void
91 jsorg71 725 cache_put_bitmap(uint8 cache_id, uint16 cache_idx, HBITMAP bitmap, uint32 stamp)
92 matty 9 {
93     HBITMAP old;
94    
95 jsorg71 379 if ((cache_id < NUM_ELEMENTS(g_bmpcache)) && (cache_idx < NUM_ELEMENTS(g_bmpcache[0])))
96 matty 9 {
97 jsorg71 725 old = g_bmpcache[cache_id][cache_idx].bitmap;
98 matty 9 if (old != NULL)
99 jsorg71 725 {
100 matty 10 ui_destroy_bitmap(old);
101 jsorg71 725 }
102 jsorg71 774 else if (g_use_rdp5)
103 jsorg71 725 {
104     if (++g_num_bitmaps_in_memory[cache_id] > BMPCACHE2_C2_CELLS)
105     cache_remove_lru_bitmap(cache_id);
106     }
107 matty 9
108 jsorg71 725 g_bmpcache[cache_id][cache_idx].bitmap = bitmap;
109     g_bmpcache[cache_id][cache_idx].usage = stamp;
110 matty 9 }
111 jsorg71 730 else if ((cache_id < NUM_ELEMENTS(g_volatile_bc)) && (cache_idx == 0x7fff))
112     {
113     old = g_volatile_bc[cache_id];
114     if (old != NULL)
115     ui_destroy_bitmap(old);
116     g_volatile_bc[cache_id] = bitmap;
117     }
118 matty 9 else
119     {
120 matty 30 error("put bitmap %d:%d\n", cache_id, cache_idx);
121 matty 9 }
122     }
123    
124 jsorg71 725 /* Updates the persistent bitmap cache MRU information on exit */
125     void
126     cache_save_state(void)
127     {
128 jsorg71 777 uint32 id, idx;
129 matty 10
130 jsorg71 725 for (id = 0; id < NUM_ELEMENTS(g_bmpcache); id++)
131     if (IS_PERSISTENT(id))
132     for (idx = 0; idx < NUM_ELEMENTS(g_bmpcache[id]); idx++)
133     pstcache_touch_bitmap(id, idx, g_bmpcache[id][idx].usage);
134     }
135    
136    
137 matty 10 /* FONT CACHE */
138 jsorg71 379 static FONTGLYPH g_fontcache[12][256];
139 matty 10
140     /* Retrieve a glyph from the font cache */
141 matty 25 FONTGLYPH *
142     cache_get_font(uint8 font, uint16 character)
143 matty 9 {
144 matty 10 FONTGLYPH *glyph;
145 matty 9
146 jsorg71 379 if ((font < NUM_ELEMENTS(g_fontcache)) && (character < NUM_ELEMENTS(g_fontcache[0])))
147 matty 9 {
148 jsorg71 379 glyph = &g_fontcache[font][character];
149 matty 9 if (glyph->pixmap != NULL)
150     return glyph;
151     }
152    
153 matty 30 error("get font %d:%d\n", font, character);
154 matty 9 return NULL;
155     }
156    
157 matty 10 /* Store a glyph in the font cache */
158 matty 25 void
159     cache_put_font(uint8 font, uint16 character, uint16 offset,
160     uint16 baseline, uint16 width, uint16 height, HGLYPH pixmap)
161 matty 9 {
162 matty 10 FONTGLYPH *glyph;
163 matty 9
164 jsorg71 379 if ((font < NUM_ELEMENTS(g_fontcache)) && (character < NUM_ELEMENTS(g_fontcache[0])))
165 matty 9 {
166 jsorg71 379 glyph = &g_fontcache[font][character];
167 matty 9 if (glyph->pixmap != NULL)
168 matty 10 ui_destroy_glyph(glyph->pixmap);
169 matty 9
170 matty 20 glyph->offset = offset;
171 matty 9 glyph->baseline = baseline;
172     glyph->width = width;
173     glyph->height = height;
174     glyph->pixmap = pixmap;
175     }
176     else
177     {
178 matty 30 error("put font %d:%d\n", font, character);
179 matty 9 }
180     }
181    
182 matty 10
183     /* TEXT CACHE */
184 jsorg71 379 static DATABLOB g_textcache[256];
185 matty 10
186     /* Retrieve a text item from the cache */
187 matty 25 DATABLOB *
188     cache_get_text(uint8 cache_id)
189 matty 9 {
190 matty 10 DATABLOB *text;
191 matty 9
192 jsorg71 789 text = &g_textcache[cache_id];
193     return text;
194 matty 9 }
195    
196 matty 10 /* Store a text item in the cache */
197 matty 25 void
198     cache_put_text(uint8 cache_id, void *data, int length)
199 matty 9 {
200 matty 10 DATABLOB *text;
201 matty 9
202 jsorg71 789 text = &g_textcache[cache_id];
203     if (text->data != NULL)
204     xfree(text->data);
205     text->data = xmalloc(length);
206     text->size = length;
207     memcpy(text->data, data, length);
208 matty 9 }
209 matty 10
210    
211     /* DESKTOP CACHE */
212 jsorg71 379 static uint8 g_deskcache[0x38400 * 4];
213 matty 10
214     /* Retrieve desktop data from the cache */
215 matty 25 uint8 *
216 matty 28 cache_get_desktop(uint32 offset, int cx, int cy, int bytes_per_pixel)
217 matty 10 {
218 matty 28 int length = cx * cy * bytes_per_pixel;
219 matty 16
220 jsorg71 563 if (offset > sizeof(g_deskcache))
221     offset = 0;
222    
223 jsorg71 379 if ((offset + length) <= sizeof(g_deskcache))
224 matty 10 {
225 jsorg71 379 return &g_deskcache[offset];
226 matty 10 }
227    
228 matty 30 error("get desktop %d:%d\n", offset, length);
229 matty 10 return NULL;
230     }
231    
232     /* Store desktop data in the cache */
233 matty 25 void
234 astrand 82 cache_put_desktop(uint32 offset, int cx, int cy, int scanline, int bytes_per_pixel, uint8 * data)
235 matty 10 {
236 matty 28 int length = cx * cy * bytes_per_pixel;
237 matty 16
238 jsorg71 563 if (offset > sizeof(g_deskcache))
239     offset = 0;
240    
241 jsorg71 379 if ((offset + length) <= sizeof(g_deskcache))
242 matty 10 {
243 matty 28 cx *= bytes_per_pixel;
244 matty 16 while (cy--)
245     {
246 jsorg71 379 memcpy(&g_deskcache[offset], data, cx);
247 matty 16 data += scanline;
248     offset += cx;
249     }
250 matty 10 }
251     else
252     {
253 matty 30 error("put desktop %d:%d\n", offset, length);
254 matty 10 }
255     }
256 matty 28
257    
258     /* CURSOR CACHE */
259 jsorg71 379 static HCURSOR g_cursorcache[0x20];
260 matty 28
261     /* Retrieve cursor from cache */
262 astrand 64 HCURSOR
263     cache_get_cursor(uint16 cache_idx)
264 matty 28 {
265     HCURSOR cursor;
266    
267 jsorg71 379 if (cache_idx < NUM_ELEMENTS(g_cursorcache))
268 matty 28 {
269 jsorg71 379 cursor = g_cursorcache[cache_idx];
270 matty 28 if (cursor != NULL)
271     return cursor;
272     }
273    
274 matty 30 error("get cursor %d\n", cache_idx);
275 matty 28 return NULL;
276     }
277    
278     /* Store cursor in cache */
279     void
280     cache_put_cursor(uint16 cache_idx, HCURSOR cursor)
281     {
282     HCURSOR old;
283    
284 jsorg71 379 if (cache_idx < NUM_ELEMENTS(g_cursorcache))
285 matty 28 {
286 jsorg71 379 old = g_cursorcache[cache_idx];
287 matty 28 if (old != NULL)
288     ui_destroy_cursor(old);
289    
290 jsorg71 379 g_cursorcache[cache_idx] = cursor;
291 matty 28 }
292     else
293     {
294 matty 30 error("put cursor %d\n", cache_idx);
295 matty 28 }
296     }

  ViewVC Help
Powered by ViewVC 1.1.26