cache.h (4471B)
1 #ifndef _CACHE_H_ 2 #define _CACHE_H_ 3 4 #include <stddef.h> 5 #include <X11/Xlib.h> 6 #include <pango/pangoxft.h> 7 8 /* 9 *The maximum number of layouts in the cache. 10 */ 11 #define LAYOUT_CACHE_SIZE 40 12 13 /* 14 * The initial number of pixmaps in the cache. 15 * The size is increased when more pixmaps are visible 16 * at the same time than there are entries in the cache. 17 */ 18 #define PIXMAP_CACHE_INITIAL_SIZE 20 19 20 typedef struct { 21 Pixmap pixmap; 22 XftDraw *draw; 23 int w, h; /* width and height of the pixmap */ 24 size_t line; /* the line associated with this entry */ 25 int valid; /* whether the entry is assigned to a line */ 26 } cache_pixmap; 27 28 typedef struct { 29 PangoLayout *layout; 30 size_t line; /* the line associated with this entry */ 31 int valid; /* whether the entry is assigned to a line */ 32 } cache_layout; 33 34 typedef struct { 35 Display *dpy; 36 cache_pixmap *pixmaps; 37 cache_layout *layouts; 38 size_t num_pixmaps; 39 size_t num_layouts; 40 size_t cur_pixmap_index; /* current replacement index for pixmaps */ 41 size_t cur_layout_index; /* current replacement index for layouts */ 42 } ledit_cache; 43 44 /* FIXME: maybe handle pixmap creation and resizing here */ 45 46 /* 47 * Create cache using X Display dpy (this is 48 * needed to destroy the pixmaps in the end). 49 */ 50 ledit_cache *cache_create(Display *dpy); 51 52 /* 53 * Reset line index of every cache entry (pixmaps and layouts). 54 * invalidate_pixmap_line is called with callback_data as its first 55 * argument and a line index which has been removed from the pixmap 56 * cache as the second argument. 57 * invalidate_layout_line is the same, but for the layout cache. 58 */ 59 void cache_flush( 60 ledit_cache *cache, 61 void *callback_data, 62 void (*invalidate_pixmap_line)(void *, size_t), 63 void (*invalidate_layout_line)(void *, size_t) 64 ); 65 66 /* 67 * Like cache_flush, but only line numbers >= start are invalidated. 68 */ 69 void cache_invalidate_from_line( 70 ledit_cache *cache, size_t start, 71 void *callback_data, 72 void (*invalidate_pixmap_line)(void *, size_t), 73 void (*invalidate_layout_line)(void *, size_t) 74 ); 75 76 /* 77 * Destroy cache. 78 */ 79 void cache_destroy(ledit_cache *cache); 80 81 /* 82 * Get the cache_pixmap at index. 83 */ 84 cache_pixmap *cache_get_pixmap(ledit_cache *cache, size_t index); 85 86 /* 87 * Get the cache_layout at index. 88 */ 89 cache_layout *cache_get_layout(ledit_cache *cache, size_t index); 90 91 /* 92 * The following two functions have a somewhat cumbersome interface 93 * because set_pixmap_line and set_layout_line are required as helper 94 * functions instead of just returning the cache index and letting the 95 * caller handle the updating. However, this has led to horrible bugs 96 * in the past, when one of the updating steps was forgotten, so this 97 * interface was designed instead to avoid some of those more basic bugs. 98 */ 99 100 /* 101 * Assign an unneeded pixmap cache index to the line with index 'line'. 102 * line_needed is used to check if a line is needed. 103 * It is called with callback_data as the first argument and the 104 * line to be checked as the second argument. 105 * The line of the cache entry is set to 'line' and if a line was 106 * set before, it is reset by calling 'invalidate_pixmap_line' with 107 * 'callback_data' as the first argumentand the old line as the 108 * second argument. 109 * The cache index of the new line is changed by calling 'set_pixmap_line' 110 * with 'callback_data' as the first argument, 'line' as the second 111 * argument, and the new cache index as the third argument. 112 */ 113 void cache_assign_pixmap_index( 114 ledit_cache *cache, size_t line, 115 void *callback_data, 116 int (*line_needed)(void *, size_t), 117 void (*set_pixmap_line)(void *, size_t, size_t), 118 void (*invalidate_pixmap_line)(void *, size_t) 119 ); 120 121 /* 122 * Assign a layout cache index to the line with index 'line'. 123 * Since it is not clear which layouts are needed more, this just 124 * uses the next index in a clock fashion. 125 * The line of the cache entry is set to 'line' and if a line was 126 * set before, it is reset by calling 'invalidate_layout_line' with 127 * 'callback_data' as the first argument and the old line as the 128 * second argument. 129 * The cache index of the new line is changed by calling 'set_layout_line' 130 * with 'callback_data' as the first argument, 'line' as the second 131 * argument, and the new cache index as the third argument. 132 */ 133 void cache_assign_layout_index( 134 ledit_cache *cache, size_t line, 135 void *callback_data, 136 void (*set_layout_line)(void *, size_t, size_t), 137 void (*invalidate_layout_line)(void *, size_t) 138 ); 139 140 #endif