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];
41 struct image_list_s *next;
44 struct image_list_s *image_list_start, *image_list_end;
47 Imlib_Updates updates, current_update;
48 /* our virtual framebuffer image we draw into */
49 Imlib_Image buffer, image;
51 static int cache_size_set = 0;
53 /* flush the image cache ever X seconds */
54 static int cimlib_cache_flush_interval = 0;
55 static int cimlib_cache_flush_last = 0;
57 #define DEFAULT_CACHE_SIZE 4096 * 1024 /* default cache size for loaded images */
59 void cimlib_set_cache_size(long size)
61 imlib_set_cache_size(size);
65 void cimlib_set_cache_flush_interval(long interval)
68 cimlib_cache_flush_interval = interval;
70 ERR("Imlib2: flush interval should be >= 0");
74 void cimlib_cleanup(void)
76 struct image_list_s *cur = image_list_start, *last = NULL;
82 image_list_start = image_list_end = NULL;
85 void cimlib_init(Display *display, Window drawable, Visual *visual, Colormap colourmap)
87 image_list_start = image_list_end = NULL;
88 if (!cache_size_set) cimlib_set_cache_size(DEFAULT_CACHE_SIZE);
89 /* set the maximum number of colors to allocate for 8bpp and less to 256 */
90 imlib_set_color_usage(256);
91 /* dither for depths < 24bpp */
92 imlib_context_set_dither(1);
93 /* set the display , visual, colormap and drawable we are using */
94 imlib_context_set_display(display);
95 imlib_context_set_visual(visual);
96 imlib_context_set_colormap(colourmap);
97 imlib_context_set_drawable(drawable);
100 void cimlib_add_image(const char *args)
102 struct image_list_s *cur = NULL;
105 cur = malloc(sizeof(struct image_list_s));
106 memset(cur, 0, sizeof(struct image_list_s));
108 if (!sscanf(args, "%1024s", cur->name)) {
109 ERR("Invalid args for $image. Format is: '<path to image> (-p x,y) (-s WxH) (-n) (-f interval)' (got '%s')", args);
111 to_real_path(cur->name, cur->name);
112 // now we check for optional args
113 tmp = strstr(args, "-p ");
116 sscanf(tmp, "%i,%i", &cur->x, &cur->y);
118 tmp = strstr(args, "-s ");
121 if (sscanf(tmp, "%ix%i", &cur->w, &cur->h)) {
126 tmp = strstr(args, "-n");
131 tmp = strstr(args, "-f ");
134 if (sscanf(tmp, "%d", &cur->flush_interval)) {
138 if (cur->flush_interval < 0) {
139 ERR("Imlib2: flush interval should be >= 0");
140 cur->flush_interval = 0;
143 if (image_list_end) {
144 image_list_end->next = cur;
145 image_list_end = cur;
147 image_list_start = image_list_end = cur;
151 static void cimlib_draw_image(struct image_list_s *cur, int *clip_x, int *clip_y, int *clip_x2, int *clip_y2)
153 image = imlib_load_image(cur->name);
156 time_t now = time(NULL);
157 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);
158 imlib_context_set_image(image);
159 /* turn alpha channel on */
160 imlib_image_set_has_alpha(1);
161 w = imlib_image_get_width();
162 h = imlib_image_get_height();
167 imlib_context_set_image(buffer);
168 imlib_blend_image_onto_image(image, 1, 0, 0, w, h,
169 cur->x, cur->y, cur->w, cur->h);
170 imlib_context_set_image(image);
171 if (cur->no_cache || (cur->flush_interval && now % cur->flush_interval == 0)) {
172 imlib_free_image_and_decache();
176 if (cur->x < *clip_x) *clip_x = cur->x;
177 if (cur->y < *clip_y) *clip_y = cur->y;
178 if (cur->x + cur->w > *clip_x2) *clip_x2 = cur->x + cur->w;
179 if (cur->y + cur->h > *clip_y2) *clip_y2 = cur->y + cur->h;
181 ERR("Unable to load image '%s'", cur->name);
185 static void cimlib_draw_all(int *clip_x, int *clip_y, int *clip_x2, int *clip_y2)
187 struct image_list_s *cur = image_list_start;
189 cimlib_draw_image(cur, clip_x, clip_y, clip_x2, clip_y2);
194 void cimlib_render(int x, int y, int width, int height)
196 int clip_x = INT_MAX, clip_y = INT_MAX;
197 int clip_x2 = 0, clip_y2 = 0;
200 if (!image_list_start) return; /* are we actually drawing anything? */
202 /* cheque if it's time to flush our cache */
204 if (cimlib_cache_flush_interval && now - cimlib_cache_flush_interval > cimlib_cache_flush_last) {
205 int size = imlib_get_cache_size();
206 imlib_set_cache_size(0);
207 imlib_set_cache_size(size);
208 cimlib_cache_flush_last = now;
209 DBGP("Flushing Imlib2 cache (%li)\n", now);
212 /* take all the little rectangles to redraw and merge them into
213 * something sane for rendering */
214 buffer = imlib_create_image(width, height);
215 /* clear our buffer */
216 imlib_context_set_image(buffer);
218 /* we can blend stuff now */
219 imlib_context_set_blend(1);
220 /* turn alpha channel on */
221 imlib_image_set_has_alpha(1);
223 cimlib_draw_all(&clip_x, &clip_y, &clip_x2, &clip_y2);
225 /* set the buffer image as our current image */
226 imlib_context_set_image(buffer);
228 /* setup our clip rect */
229 if (clip_x == INT_MAX) clip_x = 0;
230 if (clip_y == INT_MAX) clip_y = 0;
232 /* render the image at 0, 0 */
233 imlib_render_image_part_on_drawable_at_size(clip_x, clip_y, clip_x2 - clip_x,
234 clip_y2 - clip_y, x + clip_x, y + clip_y, clip_x2 - clip_x,
236 /* don't need that temporary buffer image anymore */