1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
3 * Conky, a system monitor, based on torsmo
5 * Please see COPYING for details
7 * Copyright (c) 2005-2009 Brenden Matthews, et. al.
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 * vim: ts=4 sw=4 noet ai cindent syntax=c
45 struct image_list_s *next;
48 struct image_list_s *image_list_start, *image_list_end;
51 Imlib_Updates updates, current_update;
52 /* our virtual framebuffer image we draw into */
53 Imlib_Image buffer, image;
55 static int cache_size_set = 0;
57 /* flush the image cache ever X seconds */
58 static int cimlib_cache_flush_interval = 0;
59 static int cimlib_cache_flush_last = 0;
61 #define DEFAULT_IMLIB2_CACHE_SIZE 4096 * 1024 /* default cache size for loaded images */
63 void cimlib_set_cache_size(long size)
65 imlib_set_cache_size(size);
69 void cimlib_set_cache_flush_interval(long interval)
72 cimlib_cache_flush_interval = interval;
74 NORM_ERR("Imlib2: flush interval should be >= 0");
78 void cimlib_cleanup(void)
80 struct image_list_s *cur = image_list_start, *last = NULL;
86 image_list_start = image_list_end = NULL;
89 Imlib_Context context;
91 void cimlib_init(Display *disp, Window drawable, Visual *visual, Colormap
94 image_list_start = image_list_end = NULL;
95 context = imlib_context_new();
96 imlib_context_push(context);
97 if (!cache_size_set) cimlib_set_cache_size(DEFAULT_IMLIB2_CACHE_SIZE);
98 /* set the maximum number of colors to allocate for 8bpp and less to 256 */
99 imlib_set_color_usage(256);
100 /* dither for depths < 24bpp */
101 imlib_context_set_dither(1);
102 /* set the display , visual, colormap and drawable we are using */
103 imlib_context_set_display(disp);
104 imlib_context_set_visual(visual);
105 imlib_context_set_colormap(colourmap);
106 imlib_context_set_drawable(drawable);
109 void cimlib_deinit(void)
113 imlib_context_disconnect_display();
115 imlib_context_free(context);
118 void cimlib_add_image(const char *args)
120 struct image_list_s *cur = NULL;
123 cur = malloc(sizeof(struct image_list_s));
124 memset(cur, 0, sizeof(struct image_list_s));
126 if (!sscanf(args, "%1023s", cur->name)) {
127 NORM_ERR("Invalid args for $image. Format is: '<path to image> (-p"
128 "x,y) (-s WxH) (-n) (-f interval)' (got '%s')", args);
132 to_real_path(cur->name, cur->name);
133 // now we check for optional args
134 tmp = strstr(args, "-p ");
137 sscanf(tmp, "%i,%i", &cur->x, &cur->y);
139 tmp = strstr(args, "-s ");
142 if (sscanf(tmp, "%ix%i", &cur->w, &cur->h)) {
147 tmp = strstr(args, "-n");
152 tmp = strstr(args, "-f ");
155 if (sscanf(tmp, "%d", &cur->flush_interval)) {
159 if (cur->flush_interval < 0) {
160 NORM_ERR("Imlib2: flush interval should be >= 0");
161 cur->flush_interval = 0;
164 if (image_list_end) {
165 image_list_end->next = cur;
166 image_list_end = cur;
168 image_list_start = image_list_end = cur;
172 static void cimlib_draw_image(struct image_list_s *cur, int *clip_x, int
173 *clip_y, int *clip_x2, int *clip_y2)
176 time_t now = time(NULL);
178 image = imlib_load_image(cur->name);
180 NORM_ERR("Unable to load image '%s'", cur->name);
184 DBGP("Drawing image '%s' at (%i,%i) scaled to %ix%i, "
185 "caching interval set to %i (with -n opt %i)",
186 cur->name, cur->x, cur->y, cur->w, cur->h,
187 cur->flush_interval, cur->no_cache);
189 imlib_context_set_image(image);
190 /* turn alpha channel on */
191 imlib_image_set_has_alpha(1);
192 w = imlib_image_get_width();
193 h = imlib_image_get_height();
198 imlib_context_set_image(buffer);
199 imlib_blend_image_onto_image(image, 1, 0, 0, w, h,
200 cur->x, cur->y, cur->w, cur->h);
201 imlib_context_set_image(image);
202 if (cur->no_cache || (cur->flush_interval &&
203 now % cur->flush_interval == 0)) {
204 imlib_free_image_and_decache();
208 if (cur->x < *clip_x) *clip_x = cur->x;
209 if (cur->y < *clip_y) *clip_y = cur->y;
210 if (cur->x + cur->w > *clip_x2) *clip_x2 = cur->x + cur->w;
211 if (cur->y + cur->h > *clip_y2) *clip_y2 = cur->y + cur->h;
214 static void cimlib_draw_all(int *clip_x, int *clip_y, int *clip_x2, int *clip_y2)
216 struct image_list_s *cur = image_list_start;
218 cimlib_draw_image(cur, clip_x, clip_y, clip_x2, clip_y2);
223 void cimlib_render(int x, int y, int width, int height)
225 int clip_x = INT_MAX, clip_y = INT_MAX;
226 int clip_x2 = 0, clip_y2 = 0;
229 if (!image_list_start) return; /* are we actually drawing anything? */
231 /* cheque if it's time to flush our cache */
233 if (cimlib_cache_flush_interval && now - cimlib_cache_flush_interval > cimlib_cache_flush_last) {
234 int size = imlib_get_cache_size();
235 imlib_set_cache_size(0);
236 imlib_set_cache_size(size);
237 cimlib_cache_flush_last = now;
238 DBGP("Flushing Imlib2 cache (%li)\n", now);
241 /* take all the little rectangles to redraw and merge them into
242 * something sane for rendering */
243 buffer = imlib_create_image(width, height);
244 /* clear our buffer */
245 imlib_context_set_image(buffer);
247 /* we can blend stuff now */
248 imlib_context_set_blend(1);
249 /* turn alpha channel on */
250 imlib_image_set_has_alpha(1);
252 cimlib_draw_all(&clip_x, &clip_y, &clip_x2, &clip_y2);
254 /* set the buffer image as our current image */
255 imlib_context_set_image(buffer);
257 /* setup our clip rect */
258 if (clip_x == INT_MAX) clip_x = 0;
259 if (clip_y == INT_MAX) clip_y = 0;
261 /* render the image at 0, 0 */
262 imlib_render_image_part_on_drawable_at_size(clip_x, clip_y, clip_x2 - clip_x,
263 clip_y2 - clip_y, x + clip_x, y + clip_y, clip_x2 - clip_x,
265 /* don't need that temporary buffer image anymore */