Make Imlib2 global cache flushing actually work.
[monky] / src / imlib2.c
1 /* Conky, a system monitor, based on torsmo
2  *
3  * Please see COPYING for details
4  *
5  * Copyright (c) 2005-2009 Brenden Matthews, et. al.
6  * All rights reserved.
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21
22 #include "imlib2.h"
23 #include "config.h"
24 #include "logging.h"
25 #include "common.h"
26
27 #include <Imlib2.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <limits.h>
32 #include <time.h>
33
34 struct image_list_s {
35         char name[DEFAULT_TEXT_BUFFER_SIZE];
36         Imlib_Image image;
37         int x, y, w, h;
38         int wh_set;
39         char no_cache;
40         int flush_interval;
41         int flush_last;
42         struct image_list_s *next;
43 };
44
45 struct image_list_s *image_list_start, *image_list_end;
46
47 /* areas to update */
48 Imlib_Updates updates, current_update;
49 /* our virtual framebuffer image we draw into */
50 Imlib_Image buffer, image;
51
52 static int cache_size_set = 0;
53
54 /* flush the image cache ever X seconds */
55 static int cimlib_cache_flush_interval = 0;
56 static int cimlib_cache_flush_last = 0;
57
58 #define DEFAULT_CACHE_SIZE 4096 * 1024 /* default cache size for loaded images */
59
60 void cimlib_set_cache_size(long size)
61 {
62         imlib_set_cache_size(size);
63         cache_size_set = 1;
64 }
65
66 void cimlib_set_cache_flush_interval(long interval)
67 {
68         cimlib_cache_flush_interval = interval;
69 }
70
71 void cimlib_cleanup(void)
72 {
73         struct image_list_s *cur = image_list_start, *last = NULL;
74         while (cur) {
75                 last = cur;
76                 cur = last->next;
77                 free(last);
78         }
79         image_list_start = image_list_end = NULL;
80 }
81
82 void cimlib_init(Display *display, Window drawable, Visual *visual, Colormap colourmap)
83 {
84         image_list_start = image_list_end = NULL;
85         if (!cache_size_set) cimlib_set_cache_size(DEFAULT_CACHE_SIZE);
86         /* set the maximum number of colors to allocate for 8bpp and less to 256 */
87         imlib_set_color_usage(256);
88         /* dither for depths < 24bpp */
89         imlib_context_set_dither(1);
90         /* set the display , visual, colormap and drawable we are using */
91         imlib_context_set_display(display);
92         imlib_context_set_visual(visual);
93         imlib_context_set_colormap(colourmap);
94         imlib_context_set_drawable(drawable);
95 }
96
97 void cimlib_add_image(const char *args)
98 {
99         struct image_list_s *cur = NULL;
100         char *tmp;
101
102         cur = malloc(sizeof(struct image_list_s));
103         memset(cur, 0, sizeof(struct image_list_s));
104
105         if (!sscanf(args, "%1024s", cur->name)) {
106                 ERR("Invalid args for $image.  Format is: '<path to image> (-p x,y) (-s WxH) (-n) (-f interval)' (got '%s')", args);
107         }
108         to_real_path(cur->name, cur->name);
109         // now we check for optional args
110         tmp = strstr(args, "-p ");
111         if (tmp) {
112                 tmp += 3;
113                 sscanf(tmp, "%i,%i", &cur->x, &cur->y);
114         }
115         tmp = strstr(args, "-s ");
116         if (tmp) {
117                 tmp += 3;
118                 if (sscanf(tmp, "%ix%i", &cur->w, &cur->h)) {
119                         cur->wh_set = 1;
120                 }
121         }
122
123         tmp = strstr(args, "-n");
124         if (tmp) {
125                 cur->no_cache = 1;
126         }
127
128         tmp = strstr(args, "-f ");
129         if (tmp) {
130                 tmp += 3;
131                 if (sscanf(tmp, "%d", &cur->flush_interval)) {
132                         cur->no_cache = 0;
133                 }
134         }
135
136         if (image_list_end) {
137                 image_list_end->next = cur;
138                 image_list_end = cur;
139         } else {
140                 image_list_start = image_list_end = cur;
141         }
142 }
143
144 static void cimlib_draw_image(struct image_list_s *cur, int *clip_x, int *clip_y, int *clip_x2, int *clip_y2)
145 {
146         image = imlib_load_image(cur->name);
147         if (image) {
148                 int w, h;
149                 time_t now = time(NULL);
150                 DBGP("Drawing image '%s' at (%i,%i) scaled to %ix%i, caching interval set to %i (with -n opt %i)", cur->name, cur->x, cur->y, cur->w, cur->h, cur->flush_interval, cur->no_cache);
151                 imlib_context_set_image(image);
152                 /* turn alpha channel on */
153                 imlib_image_set_has_alpha(1);
154                 w = imlib_image_get_width();
155                 h = imlib_image_get_height();
156                 if (!cur->wh_set) {
157                         cur->w = w;
158                         cur->h = h;
159                 }
160                 imlib_context_set_image(buffer);
161                 imlib_blend_image_onto_image(image, 1, 0, 0, w, h,
162                                 cur->x, cur->y, cur->w, cur->h);
163                 imlib_context_set_image(image);
164                 if (cur->no_cache || (cur->flush_interval && now - cur->flush_interval > cur->flush_last)) {
165                         imlib_free_image_and_decache();
166                         cur->flush_last = now;
167                 } else {
168                         imlib_free_image();
169                 }
170                 if (cur->x < *clip_x) *clip_x = cur->x;
171                 if (cur->y < *clip_y) *clip_y = cur->y;
172                 if (cur->x + cur->w > *clip_x2) *clip_x2 = cur->x + cur->w;
173                 if (cur->y + cur->h > *clip_y2) *clip_y2 = cur->y + cur->h;
174         } else {
175                 ERR("Unable to load image '%s'", cur->name);
176         }
177 }
178
179 static void cimlib_draw_all(int *clip_x, int *clip_y, int *clip_x2, int *clip_y2)
180 {
181         struct image_list_s *cur = image_list_start;
182         while (cur) {
183                 cimlib_draw_image(cur, clip_x, clip_y, clip_x2, clip_y2);
184                 cur = cur->next;
185         }
186 }
187
188 void cimlib_render(int x, int y, int width, int height)
189 {
190         int clip_x = INT_MAX, clip_y = INT_MAX;
191         int clip_x2 = 0, clip_y2 = 0;
192         time_t now;
193
194         if (!image_list_start) return; /* are we actually drawing anything? */
195
196         /* cheque if it's time to flush our cache */
197         now = time(NULL);
198         if (cimlib_cache_flush_interval && now - cimlib_cache_flush_interval > cimlib_cache_flush_last) {
199                 int size = imlib_get_cache_size();
200                 imlib_set_cache_size(0);
201                 imlib_set_cache_size(size);
202                 cimlib_cache_flush_last = now;
203                 DBGP("Flushing Imlib2 cache (%li)\n", now);
204         }
205
206         /* take all the little rectangles to redraw and merge them into
207          * something sane for rendering */
208         buffer = imlib_create_image(width, height);
209         /* clear our buffer */
210         imlib_context_set_image(buffer);
211         imlib_image_clear();
212         /* we can blend stuff now */
213         imlib_context_set_blend(1);
214         /* turn alpha channel on */
215         imlib_image_set_has_alpha(1);
216
217         cimlib_draw_all(&clip_x, &clip_y, &clip_x2, &clip_y2);
218
219         /* set the buffer image as our current image */
220         imlib_context_set_image(buffer);
221
222         /* setup our clip rect */
223         if (clip_x == INT_MAX) clip_x = 0;
224         if (clip_y == INT_MAX) clip_y = 0;
225
226         /* render the image at 0, 0 */
227         imlib_render_image_part_on_drawable_at_size(clip_x, clip_y, clip_x2 - clip_x,
228                         clip_y2 - clip_y, x + clip_x, y + clip_y, clip_x2 - clip_x,
229                         clip_y2 - clip_y);
230         /* don't need that temporary buffer image anymore */
231         imlib_free_image();
232 }
233