1 /* Conky, a system monitor, based on torsmo
3 * Please see COPYING for details
5 * Copyright (c) 2005-2009 Brenden Matthews, et. al.
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.
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/>.
35 char name[DEFAULT_TEXT_BUFFER_SIZE];
42 struct image_list_s *next;
45 struct image_list_s *image_list_start, *image_list_end;
48 Imlib_Updates updates, current_update;
49 /* our virtual framebuffer image we draw into */
50 Imlib_Image buffer, image;
52 static int cache_size_set = 0;
54 /* flush the image cache ever X seconds */
55 static int cimlib_cache_flush_interval = 0;
56 static int cimlib_cache_flush_last = 0;
58 #define DEFAULT_CACHE_SIZE 4096 * 1024 /* default cache size for loaded images */
60 void cimlib_set_cache_size(long size)
62 imlib_set_cache_size(size);
66 void cimlib_set_cache_flush_interval(long interval)
68 cimlib_cache_flush_interval = interval;
71 void cimlib_cleanup(void)
73 struct image_list_s *cur = image_list_start, *last = NULL;
79 image_list_start = image_list_end = NULL;
82 void cimlib_init(Display *display, Window drawable, Visual *visual, Colormap colourmap)
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);
97 void cimlib_add_image(const char *args)
99 struct image_list_s *cur = NULL;
102 cur = malloc(sizeof(struct image_list_s));
103 memset(cur, 0, sizeof(struct image_list_s));
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);
108 to_real_path(cur->name, cur->name);
109 // now we check for optional args
110 tmp = strstr(args, "-p ");
113 sscanf(tmp, "%i,%i", &cur->x, &cur->y);
115 tmp = strstr(args, "-s ");
118 if (sscanf(tmp, "%ix%i", &cur->w, &cur->h)) {
123 tmp = strstr(args, "-n");
128 tmp = strstr(args, "-f ");
131 if (sscanf(tmp, "%d", &cur->flush_interval)) {
136 if (image_list_end) {
137 image_list_end->next = cur;
138 image_list_end = cur;
140 image_list_start = image_list_end = cur;
144 static void cimlib_draw_image(struct image_list_s *cur, int *clip_x, int *clip_y, int *clip_x2, int *clip_y2)
146 image = imlib_load_image(cur->name);
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();
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;
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;
175 ERR("Unable to load image '%s'", cur->name);
179 static void cimlib_draw_all(int *clip_x, int *clip_y, int *clip_x2, int *clip_y2)
181 struct image_list_s *cur = image_list_start;
183 cimlib_draw_image(cur, clip_x, clip_y, clip_x2, clip_y2);
188 void cimlib_render(int x, int y, int width, int height)
190 int clip_x = INT_MAX, clip_y = INT_MAX;
191 int clip_x2 = 0, clip_y2 = 0;
194 if (!image_list_start) return; /* are we actually drawing anything? */
196 /* cheque if it's time to flush our cache */
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);
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);
212 /* we can blend stuff now */
213 imlib_context_set_blend(1);
214 /* turn alpha channel on */
215 imlib_image_set_has_alpha(1);
217 cimlib_draw_all(&clip_x, &clip_y, &clip_x2, &clip_y2);
219 /* set the buffer image as our current image */
220 imlib_context_set_image(buffer);
222 /* setup our clip rect */
223 if (clip_x == INT_MAX) clip_x = 0;
224 if (clip_y == INT_MAX) clip_y = 0;
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,
230 /* don't need that temporary buffer image anymore */