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