/[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 726 - (hide annotations)
Sun Jun 27 18:53:08 2004 UTC (19 years, 11 months ago) by stargo
File MIME type: text/plain
File size: 6435 byte(s)
Fix compiler error:
"orders.c", line 887: a cast does not yield an lvalue

Fix warnings:
"rdp.c", line 550: warning: integer overflow detected: op "<<"
"cache.c", line 42: warning: initializer does not fit or is out of range: -1

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

  ViewVC Help
Powered by ViewVC 1.1.26