2000-07-25 14:34:29 +02:00
|
|
|
/*
|
|
|
|
rdesktop: A Remote Desktop Protocol client.
|
|
|
|
Cache routines
|
2002-09-26 16:26:46 +02:00
|
|
|
Copyright (C) Matthew Chapman 1999-2002
|
2000-07-25 14:34:29 +02:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
2000-08-15 12:23:24 +02:00
|
|
|
#include "rdesktop.h"
|
2000-07-25 14:34:29 +02:00
|
|
|
|
2000-08-15 12:23:24 +02:00
|
|
|
#define NUM_ELEMENTS(array) (sizeof(array) / sizeof(array[0]))
|
2004-06-27 19:51:54 +02:00
|
|
|
#define TOUCH(id, idx) (g_bmpcache[id][idx].usage = ++g_stamp)
|
|
|
|
#define IS_PERSISTENT(id) (g_pstcache_fd[id] > 0)
|
|
|
|
|
|
|
|
extern int g_pstcache_fd[];
|
|
|
|
|
|
|
|
uint32 g_stamp;
|
|
|
|
int g_num_bitmaps_in_memory[3];
|
2000-08-15 12:23:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* BITMAP CACHE */
|
2004-06-27 19:51:54 +02:00
|
|
|
static BMPCACHEENTRY g_bmpcache[3][0xa00];
|
2004-06-29 18:22:41 +02:00
|
|
|
static HBITMAP g_volatile_bc[3];
|
2004-06-27 19:51:54 +02:00
|
|
|
|
|
|
|
/* Remove the least-recently used bitmap from the cache */
|
|
|
|
void
|
|
|
|
cache_remove_lru_bitmap(uint8 cache_id)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
uint16 cache_idx = 0;
|
2004-07-12 23:07:39 +02:00
|
|
|
uint32 m = (uint32) - 1;
|
2004-06-27 19:51:54 +02:00
|
|
|
BMPCACHEENTRY *pbce;
|
|
|
|
|
|
|
|
for (i = 0; i < NUM_ELEMENTS(g_bmpcache[cache_id]); i++)
|
|
|
|
{
|
|
|
|
if (g_bmpcache[cache_id][i].bitmap && g_bmpcache[cache_id][i].usage < m)
|
|
|
|
{
|
|
|
|
cache_idx = i;
|
|
|
|
m = g_bmpcache[cache_id][i].usage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pbce = &g_bmpcache[cache_id][cache_idx];
|
|
|
|
ui_destroy_bitmap(pbce->bitmap);
|
|
|
|
--g_num_bitmaps_in_memory[cache_id];
|
|
|
|
pbce->bitmap = 0;
|
|
|
|
pbce->usage = 0;
|
|
|
|
}
|
2000-08-15 12:23:24 +02:00
|
|
|
|
|
|
|
/* Retrieve a bitmap from the cache */
|
2001-01-06 04:47:04 +01:00
|
|
|
HBITMAP
|
|
|
|
cache_get_bitmap(uint8 cache_id, uint16 cache_idx)
|
2000-07-25 14:34:29 +02:00
|
|
|
{
|
2004-06-27 19:51:54 +02:00
|
|
|
HBITMAP *pbitmap;
|
2000-07-25 14:34:29 +02:00
|
|
|
|
2003-05-30 23:31:59 +02:00
|
|
|
if ((cache_id < NUM_ELEMENTS(g_bmpcache)) && (cache_idx < NUM_ELEMENTS(g_bmpcache[0])))
|
2000-07-25 14:34:29 +02:00
|
|
|
{
|
2004-06-27 19:51:54 +02:00
|
|
|
pbitmap = &g_bmpcache[cache_id][cache_idx].bitmap;
|
|
|
|
if ((*pbitmap != 0) || pstcache_load_bitmap(cache_id, cache_idx))
|
|
|
|
{
|
|
|
|
if (IS_PERSISTENT(cache_id))
|
|
|
|
TOUCH(cache_id, cache_idx);
|
|
|
|
|
|
|
|
return *pbitmap;
|
|
|
|
}
|
2000-07-25 14:34:29 +02:00
|
|
|
}
|
2004-06-29 18:22:41 +02:00
|
|
|
else if ((cache_id < NUM_ELEMENTS(g_volatile_bc)) && (cache_idx == 0x7fff))
|
|
|
|
{
|
|
|
|
return g_volatile_bc[cache_id];
|
|
|
|
}
|
2000-07-25 14:34:29 +02:00
|
|
|
|
2001-09-14 15:51:38 +02:00
|
|
|
error("get bitmap %d:%d\n", cache_id, cache_idx);
|
2000-07-25 14:34:29 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-08-15 12:23:24 +02:00
|
|
|
/* Store a bitmap in the cache */
|
2001-01-06 04:47:04 +01:00
|
|
|
void
|
2004-06-27 19:51:54 +02:00
|
|
|
cache_put_bitmap(uint8 cache_id, uint16 cache_idx, HBITMAP bitmap, uint32 stamp)
|
2000-07-25 14:34:29 +02:00
|
|
|
{
|
|
|
|
HBITMAP old;
|
|
|
|
|
2003-05-30 23:31:59 +02:00
|
|
|
if ((cache_id < NUM_ELEMENTS(g_bmpcache)) && (cache_idx < NUM_ELEMENTS(g_bmpcache[0])))
|
2000-07-25 14:34:29 +02:00
|
|
|
{
|
2004-06-27 19:51:54 +02:00
|
|
|
old = g_bmpcache[cache_id][cache_idx].bitmap;
|
2000-07-25 14:34:29 +02:00
|
|
|
if (old != NULL)
|
2004-06-27 19:51:54 +02:00
|
|
|
{
|
2000-08-15 12:23:24 +02:00
|
|
|
ui_destroy_bitmap(old);
|
2004-06-27 19:51:54 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (++g_num_bitmaps_in_memory[cache_id] > BMPCACHE2_C2_CELLS)
|
|
|
|
cache_remove_lru_bitmap(cache_id);
|
|
|
|
}
|
2000-07-25 14:34:29 +02:00
|
|
|
|
2004-06-27 19:51:54 +02:00
|
|
|
g_bmpcache[cache_id][cache_idx].bitmap = bitmap;
|
|
|
|
g_bmpcache[cache_id][cache_idx].usage = stamp;
|
2000-07-25 14:34:29 +02:00
|
|
|
}
|
2004-06-29 18:22:41 +02:00
|
|
|
else if ((cache_id < NUM_ELEMENTS(g_volatile_bc)) && (cache_idx == 0x7fff))
|
|
|
|
{
|
|
|
|
old = g_volatile_bc[cache_id];
|
|
|
|
if (old != NULL)
|
|
|
|
ui_destroy_bitmap(old);
|
|
|
|
g_volatile_bc[cache_id] = bitmap;
|
|
|
|
}
|
2000-07-25 14:34:29 +02:00
|
|
|
else
|
|
|
|
{
|
2001-09-14 15:51:38 +02:00
|
|
|
error("put bitmap %d:%d\n", cache_id, cache_idx);
|
2000-07-25 14:34:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-06-27 19:51:54 +02:00
|
|
|
/* Updates the persistent bitmap cache MRU information on exit */
|
|
|
|
void
|
|
|
|
cache_save_state(void)
|
|
|
|
{
|
|
|
|
int id, idx;
|
|
|
|
|
|
|
|
for (id = 0; id < NUM_ELEMENTS(g_bmpcache); id++)
|
|
|
|
if (IS_PERSISTENT(id))
|
|
|
|
for (idx = 0; idx < NUM_ELEMENTS(g_bmpcache[id]); idx++)
|
|
|
|
pstcache_touch_bitmap(id, idx, g_bmpcache[id][idx].usage);
|
|
|
|
}
|
|
|
|
|
2000-08-15 12:23:24 +02:00
|
|
|
|
|
|
|
/* FONT CACHE */
|
2003-05-30 23:31:59 +02:00
|
|
|
static FONTGLYPH g_fontcache[12][256];
|
2000-08-15 12:23:24 +02:00
|
|
|
|
|
|
|
/* Retrieve a glyph from the font cache */
|
2001-01-06 04:47:04 +01:00
|
|
|
FONTGLYPH *
|
|
|
|
cache_get_font(uint8 font, uint16 character)
|
2000-07-25 14:34:29 +02:00
|
|
|
{
|
2000-08-15 12:23:24 +02:00
|
|
|
FONTGLYPH *glyph;
|
2000-07-25 14:34:29 +02:00
|
|
|
|
2003-05-30 23:31:59 +02:00
|
|
|
if ((font < NUM_ELEMENTS(g_fontcache)) && (character < NUM_ELEMENTS(g_fontcache[0])))
|
2000-07-25 14:34:29 +02:00
|
|
|
{
|
2003-05-30 23:31:59 +02:00
|
|
|
glyph = &g_fontcache[font][character];
|
2000-07-25 14:34:29 +02:00
|
|
|
if (glyph->pixmap != NULL)
|
|
|
|
return glyph;
|
|
|
|
}
|
|
|
|
|
2001-09-14 15:51:38 +02:00
|
|
|
error("get font %d:%d\n", font, character);
|
2000-07-25 14:34:29 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-08-15 12:23:24 +02:00
|
|
|
/* Store a glyph in the font cache */
|
2001-01-06 04:47:04 +01:00
|
|
|
void
|
|
|
|
cache_put_font(uint8 font, uint16 character, uint16 offset,
|
|
|
|
uint16 baseline, uint16 width, uint16 height, HGLYPH pixmap)
|
2000-07-25 14:34:29 +02:00
|
|
|
{
|
2000-08-15 12:23:24 +02:00
|
|
|
FONTGLYPH *glyph;
|
2000-07-25 14:34:29 +02:00
|
|
|
|
2003-05-30 23:31:59 +02:00
|
|
|
if ((font < NUM_ELEMENTS(g_fontcache)) && (character < NUM_ELEMENTS(g_fontcache[0])))
|
2000-07-25 14:34:29 +02:00
|
|
|
{
|
2003-05-30 23:31:59 +02:00
|
|
|
glyph = &g_fontcache[font][character];
|
2000-07-25 14:34:29 +02:00
|
|
|
if (glyph->pixmap != NULL)
|
2000-08-15 12:23:24 +02:00
|
|
|
ui_destroy_glyph(glyph->pixmap);
|
2000-07-25 14:34:29 +02:00
|
|
|
|
2000-10-16 09:37:52 +02:00
|
|
|
glyph->offset = offset;
|
2000-07-25 14:34:29 +02:00
|
|
|
glyph->baseline = baseline;
|
|
|
|
glyph->width = width;
|
|
|
|
glyph->height = height;
|
|
|
|
glyph->pixmap = pixmap;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-09-14 15:51:38 +02:00
|
|
|
error("put font %d:%d\n", font, character);
|
2000-07-25 14:34:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-08-15 12:23:24 +02:00
|
|
|
|
|
|
|
/* TEXT CACHE */
|
2003-05-30 23:31:59 +02:00
|
|
|
static DATABLOB g_textcache[256];
|
2000-08-15 12:23:24 +02:00
|
|
|
|
|
|
|
/* Retrieve a text item from the cache */
|
2001-01-06 04:47:04 +01:00
|
|
|
DATABLOB *
|
|
|
|
cache_get_text(uint8 cache_id)
|
2000-07-25 14:34:29 +02:00
|
|
|
{
|
2000-08-15 12:23:24 +02:00
|
|
|
DATABLOB *text;
|
2000-07-25 14:34:29 +02:00
|
|
|
|
2003-05-30 23:31:59 +02:00
|
|
|
if (cache_id < NUM_ELEMENTS(g_textcache))
|
2000-07-25 14:34:29 +02:00
|
|
|
{
|
2003-05-30 23:31:59 +02:00
|
|
|
text = &g_textcache[cache_id];
|
2000-07-25 14:34:29 +02:00
|
|
|
if (text->data != NULL)
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2001-09-14 15:51:38 +02:00
|
|
|
error("get text %d\n", cache_id);
|
2000-07-25 14:34:29 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-08-15 12:23:24 +02:00
|
|
|
/* Store a text item in the cache */
|
2001-01-06 04:47:04 +01:00
|
|
|
void
|
|
|
|
cache_put_text(uint8 cache_id, void *data, int length)
|
2000-07-25 14:34:29 +02:00
|
|
|
{
|
2000-08-15 12:23:24 +02:00
|
|
|
DATABLOB *text;
|
2000-07-25 14:34:29 +02:00
|
|
|
|
2003-05-30 23:31:59 +02:00
|
|
|
if (cache_id < NUM_ELEMENTS(g_textcache))
|
2000-07-25 14:34:29 +02:00
|
|
|
{
|
2003-05-30 23:31:59 +02:00
|
|
|
text = &g_textcache[cache_id];
|
2000-07-25 14:34:29 +02:00
|
|
|
if (text->data != NULL)
|
2000-08-15 12:23:24 +02:00
|
|
|
xfree(text->data);
|
2000-07-25 14:34:29 +02:00
|
|
|
|
2000-08-15 12:23:24 +02:00
|
|
|
text->data = xmalloc(length);
|
2000-07-25 14:34:29 +02:00
|
|
|
text->size = length;
|
|
|
|
memcpy(text->data, data, length);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-09-14 15:51:38 +02:00
|
|
|
error("put text %d\n", cache_id);
|
2000-07-25 14:34:29 +02:00
|
|
|
}
|
|
|
|
}
|
2000-08-15 12:23:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* DESKTOP CACHE */
|
2003-05-30 23:31:59 +02:00
|
|
|
static uint8 g_deskcache[0x38400 * 4];
|
2000-08-15 12:23:24 +02:00
|
|
|
|
|
|
|
/* Retrieve desktop data from the cache */
|
2001-01-06 04:47:04 +01:00
|
|
|
uint8 *
|
2001-06-20 15:54:48 +02:00
|
|
|
cache_get_desktop(uint32 offset, int cx, int cy, int bytes_per_pixel)
|
2000-08-15 12:23:24 +02:00
|
|
|
{
|
2001-06-20 15:54:48 +02:00
|
|
|
int length = cx * cy * bytes_per_pixel;
|
2000-09-28 09:04:14 +02:00
|
|
|
|
2003-12-24 18:23:03 +01:00
|
|
|
if (offset > sizeof(g_deskcache))
|
|
|
|
offset = 0;
|
|
|
|
|
2003-05-30 23:31:59 +02:00
|
|
|
if ((offset + length) <= sizeof(g_deskcache))
|
2000-08-15 12:23:24 +02:00
|
|
|
{
|
2003-05-30 23:31:59 +02:00
|
|
|
return &g_deskcache[offset];
|
2000-08-15 12:23:24 +02:00
|
|
|
}
|
|
|
|
|
2001-09-14 15:51:38 +02:00
|
|
|
error("get desktop %d:%d\n", offset, length);
|
2000-08-15 12:23:24 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Store desktop data in the cache */
|
2001-01-06 04:47:04 +01:00
|
|
|
void
|
2002-07-30 09:18:48 +02:00
|
|
|
cache_put_desktop(uint32 offset, int cx, int cy, int scanline, int bytes_per_pixel, uint8 * data)
|
2000-08-15 12:23:24 +02:00
|
|
|
{
|
2001-06-20 15:54:48 +02:00
|
|
|
int length = cx * cy * bytes_per_pixel;
|
2000-09-28 09:04:14 +02:00
|
|
|
|
2003-12-24 18:23:03 +01:00
|
|
|
if (offset > sizeof(g_deskcache))
|
|
|
|
offset = 0;
|
|
|
|
|
2003-05-30 23:31:59 +02:00
|
|
|
if ((offset + length) <= sizeof(g_deskcache))
|
2000-08-15 12:23:24 +02:00
|
|
|
{
|
2001-06-20 15:54:48 +02:00
|
|
|
cx *= bytes_per_pixel;
|
2000-09-28 09:04:14 +02:00
|
|
|
while (cy--)
|
|
|
|
{
|
2003-05-30 23:31:59 +02:00
|
|
|
memcpy(&g_deskcache[offset], data, cx);
|
2000-09-28 09:04:14 +02:00
|
|
|
data += scanline;
|
|
|
|
offset += cx;
|
|
|
|
}
|
2000-08-15 12:23:24 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-09-14 15:51:38 +02:00
|
|
|
error("put desktop %d:%d\n", offset, length);
|
2000-08-15 12:23:24 +02:00
|
|
|
}
|
|
|
|
}
|
2001-06-20 15:54:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* CURSOR CACHE */
|
2003-05-30 23:31:59 +02:00
|
|
|
static HCURSOR g_cursorcache[0x20];
|
2001-06-20 15:54:48 +02:00
|
|
|
|
|
|
|
/* Retrieve cursor from cache */
|
2002-07-18 18:38:31 +02:00
|
|
|
HCURSOR
|
|
|
|
cache_get_cursor(uint16 cache_idx)
|
2001-06-20 15:54:48 +02:00
|
|
|
{
|
|
|
|
HCURSOR cursor;
|
|
|
|
|
2003-05-30 23:31:59 +02:00
|
|
|
if (cache_idx < NUM_ELEMENTS(g_cursorcache))
|
2001-06-20 15:54:48 +02:00
|
|
|
{
|
2003-05-30 23:31:59 +02:00
|
|
|
cursor = g_cursorcache[cache_idx];
|
2001-06-20 15:54:48 +02:00
|
|
|
if (cursor != NULL)
|
|
|
|
return cursor;
|
|
|
|
}
|
|
|
|
|
2001-09-14 15:51:38 +02:00
|
|
|
error("get cursor %d\n", cache_idx);
|
2001-06-20 15:54:48 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Store cursor in cache */
|
|
|
|
void
|
|
|
|
cache_put_cursor(uint16 cache_idx, HCURSOR cursor)
|
|
|
|
{
|
|
|
|
HCURSOR old;
|
|
|
|
|
2003-05-30 23:31:59 +02:00
|
|
|
if (cache_idx < NUM_ELEMENTS(g_cursorcache))
|
2001-06-20 15:54:48 +02:00
|
|
|
{
|
2003-05-30 23:31:59 +02:00
|
|
|
old = g_cursorcache[cache_idx];
|
2001-06-20 15:54:48 +02:00
|
|
|
if (old != NULL)
|
|
|
|
ui_destroy_cursor(old);
|
|
|
|
|
2003-05-30 23:31:59 +02:00
|
|
|
g_cursorcache[cache_idx] = cursor;
|
2001-06-20 15:54:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-09-14 15:51:38 +02:00
|
|
|
error("put cursor %d\n", cache_idx);
|
2001-06-20 15:54:48 +02:00
|
|
|
}
|
|
|
|
}
|