1 |
jsorg71 |
725 |
/* |
2 |
|
|
rdesktop: A Remote Desktop Protocol client. |
3 |
|
|
Persistent Bitmap Cache routines |
4 |
|
|
Copyright (C) Jeroen Meijer 2004 |
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 |
astrand |
738 |
#define MAX_CELL_SIZE 0x1000 /* pixels */ |
24 |
jsorg71 |
725 |
|
25 |
|
|
#define IS_PERSISTENT(id) (g_pstcache_fd[id] > 0) |
26 |
|
|
|
27 |
|
|
extern int g_server_bpp; |
28 |
|
|
extern uint32 g_stamp; |
29 |
|
|
extern BOOL g_bitmap_cache; |
30 |
|
|
extern BOOL g_bitmap_cache_persist_enable; |
31 |
|
|
extern BOOL g_bitmap_cache_precache; |
32 |
|
|
|
33 |
|
|
int g_pstcache_fd[8]; |
34 |
|
|
int g_pstcache_Bpp; |
35 |
|
|
BOOL g_pstcache_enumerated = False; |
36 |
astrand |
738 |
uint8 zero_id[] = { 0, 0, 0, 0, 0, 0, 0, 0 }; |
37 |
jsorg71 |
725 |
|
38 |
|
|
|
39 |
|
|
/* Update usage info for a bitmap */ |
40 |
|
|
void |
41 |
|
|
pstcache_touch_bitmap(uint8 cache_id, uint16 cache_idx, uint32 stamp) |
42 |
|
|
{ |
43 |
|
|
int fd; |
44 |
|
|
|
45 |
|
|
if (!IS_PERSISTENT(cache_id)) |
46 |
|
|
return; |
47 |
|
|
|
48 |
|
|
fd = g_pstcache_fd[cache_id]; |
49 |
|
|
rd_lseek_file(fd, 12 + cache_idx * (g_pstcache_Bpp * MAX_CELL_SIZE + sizeof(CELLHEADER))); |
50 |
|
|
rd_write_file(fd, &stamp, sizeof(stamp)); |
51 |
|
|
} |
52 |
|
|
|
53 |
|
|
/* Load a bitmap from the persistent cache */ |
54 |
|
|
BOOL |
55 |
|
|
pstcache_load_bitmap(uint8 cache_id, uint16 cache_idx) |
56 |
|
|
{ |
57 |
|
|
uint8 *celldata; |
58 |
|
|
int fd; |
59 |
|
|
CELLHEADER cellhdr; |
60 |
|
|
HBITMAP bitmap; |
61 |
|
|
|
62 |
|
|
if (!(g_bitmap_cache_persist_enable && IS_PERSISTENT(cache_id))) |
63 |
|
|
return False; |
64 |
|
|
|
65 |
|
|
fd = g_pstcache_fd[cache_id]; |
66 |
|
|
rd_lseek_file(fd, cache_idx * (g_pstcache_Bpp * MAX_CELL_SIZE + sizeof(CELLHEADER))); |
67 |
|
|
rd_read_file(fd, &cellhdr, sizeof(CELLHEADER)); |
68 |
astrand |
738 |
celldata = (uint8 *) xmalloc(cellhdr.length); |
69 |
jsorg71 |
725 |
rd_read_file(fd, celldata, cellhdr.length); |
70 |
|
|
|
71 |
|
|
DEBUG(("Loading bitmap from disk (%d:%d)\n", cache_id, cache_idx)); |
72 |
|
|
|
73 |
|
|
bitmap = ui_create_bitmap(cellhdr.width, cellhdr.height, celldata); |
74 |
|
|
cache_put_bitmap(cache_id, cache_idx, bitmap, cellhdr.stamp); |
75 |
|
|
|
76 |
|
|
xfree(celldata); |
77 |
|
|
return True; |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
/* Store a bitmap in the persistent cache */ |
81 |
|
|
BOOL |
82 |
astrand |
738 |
pstcache_put_bitmap(uint8 cache_id, uint16 cache_idx, uint8 * bitmap_id, |
83 |
|
|
uint16 width, uint16 height, uint16 length, uint8 * data) |
84 |
jsorg71 |
725 |
{ |
85 |
|
|
int fd; |
86 |
|
|
CELLHEADER cellhdr; |
87 |
|
|
|
88 |
|
|
if (!IS_PERSISTENT(cache_id)) |
89 |
|
|
return False; |
90 |
|
|
|
91 |
|
|
memcpy(cellhdr.bitmap_id, bitmap_id, sizeof(BITMAP_ID)); |
92 |
|
|
cellhdr.width = width; |
93 |
|
|
cellhdr.height = height; |
94 |
|
|
cellhdr.length = length; |
95 |
|
|
cellhdr.stamp = 0; |
96 |
|
|
|
97 |
|
|
fd = g_pstcache_fd[cache_id]; |
98 |
|
|
rd_lseek_file(fd, cache_idx * (g_pstcache_Bpp * MAX_CELL_SIZE + sizeof(CELLHEADER))); |
99 |
|
|
rd_write_file(fd, &cellhdr, sizeof(CELLHEADER)); |
100 |
|
|
rd_write_file(fd, data, length); |
101 |
|
|
|
102 |
|
|
return True; |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
/* list the bitmaps from the persistent cache file */ |
106 |
|
|
int |
107 |
astrand |
738 |
pstcache_enumerate(uint8 cache_id, uint8 * idlist) |
108 |
jsorg71 |
725 |
{ |
109 |
|
|
int fd, n, c = 0; |
110 |
|
|
CELLHEADER cellhdr; |
111 |
|
|
|
112 |
|
|
if (!(g_bitmap_cache && g_bitmap_cache_persist_enable && IS_PERSISTENT(cache_id))) |
113 |
|
|
return 0; |
114 |
|
|
|
115 |
|
|
/* The server disconnects if the bitmap cache content is sent more than once */ |
116 |
|
|
if (g_pstcache_enumerated) |
117 |
|
|
return 0; |
118 |
|
|
|
119 |
|
|
DEBUG(("pstcache enumeration... ")); |
120 |
|
|
for (n = 0; n < BMPCACHE2_NUM_PSTCELLS; n++) |
121 |
|
|
{ |
122 |
|
|
fd = g_pstcache_fd[cache_id]; |
123 |
|
|
rd_lseek_file(fd, n * (g_pstcache_Bpp * MAX_CELL_SIZE + sizeof(CELLHEADER))); |
124 |
|
|
if (rd_read_file(fd, &cellhdr, sizeof(CELLHEADER)) <= 0) |
125 |
|
|
break; |
126 |
|
|
|
127 |
|
|
if (memcmp(cellhdr.bitmap_id, zero_id, sizeof(BITMAP_ID)) != 0) |
128 |
|
|
{ |
129 |
|
|
memcpy(idlist + n * sizeof(BITMAP_ID), cellhdr.bitmap_id, |
130 |
astrand |
738 |
sizeof(BITMAP_ID)); |
131 |
jsorg71 |
725 |
|
132 |
|
|
if (cellhdr.stamp) |
133 |
|
|
{ |
134 |
|
|
/* Pre-caching is not possible with 8bpp because a colourmap |
135 |
|
|
* is needed to load them */ |
136 |
|
|
if (g_bitmap_cache_precache && (g_server_bpp > 8)) |
137 |
|
|
{ |
138 |
|
|
if (pstcache_load_bitmap(cache_id, n)) |
139 |
|
|
c++; |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
g_stamp = MAX(g_stamp, cellhdr.stamp); |
143 |
|
|
} |
144 |
|
|
} |
145 |
|
|
else |
146 |
|
|
{ |
147 |
|
|
break; |
148 |
|
|
} |
149 |
|
|
} |
150 |
|
|
|
151 |
|
|
DEBUG(("%d bitmaps in persistent cache, %d bitmaps loaded in memory\n", n, c)); |
152 |
|
|
g_pstcache_enumerated = True; |
153 |
|
|
return n; |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
/* initialise the persistent bitmap cache */ |
157 |
|
|
BOOL |
158 |
|
|
pstcache_init(uint8 cache_id) |
159 |
|
|
{ |
160 |
|
|
int fd; |
161 |
|
|
char filename[256]; |
162 |
|
|
|
163 |
|
|
if (g_pstcache_enumerated) |
164 |
|
|
return True; |
165 |
|
|
|
166 |
|
|
g_pstcache_fd[cache_id] = 0; |
167 |
|
|
|
168 |
|
|
if (!(g_bitmap_cache && g_bitmap_cache_persist_enable)) |
169 |
|
|
return False; |
170 |
|
|
|
171 |
|
|
if (!rd_pstcache_mkdir()) |
172 |
|
|
{ |
173 |
|
|
DEBUG(("failed to get/make cache directory!\n")); |
174 |
|
|
return False; |
175 |
|
|
} |
176 |
|
|
|
177 |
|
|
g_pstcache_Bpp = (g_server_bpp + 7) / 8; |
178 |
|
|
sprintf(filename, "cache/pstcache_%d_%d", cache_id, g_pstcache_Bpp); |
179 |
|
|
DEBUG(("persistent bitmap cache file: %s\n", filename)); |
180 |
|
|
|
181 |
|
|
fd = rd_open_file(filename); |
182 |
|
|
if (fd == -1) |
183 |
|
|
return False; |
184 |
|
|
|
185 |
|
|
if (!rd_lock_file(fd, 0, 0)) |
186 |
|
|
{ |
187 |
|
|
warning("Persistent bitmap caching is disabled. (The file is already in use)\n"); |
188 |
|
|
rd_close_file(fd); |
189 |
|
|
return False; |
190 |
|
|
} |
191 |
|
|
|
192 |
|
|
g_pstcache_fd[cache_id] = fd; |
193 |
|
|
return True; |
194 |
|
|
} |