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/>.
34 char name[DEFAULT_TEXT_BUFFER_SIZE];
38 struct image_list_s *next;
41 struct image_list_s *image_list_start, *image_list_end;
44 Imlib_Updates updates, current_update;
45 /* our virtual framebuffer image we draw into */
46 Imlib_Image buffer, image;
48 static int cache_size_set = 0;
50 #define DEFAULT_CACHE_SIZE 4096 * 1024 /* default cache size for loaded images */
52 void cimlib_set_cache_size(long size)
54 imlib_set_cache_size(size);
58 void cimlib_cleanup(void)
60 struct image_list_s *cur = image_list_start, *last = NULL;
66 image_list_start = image_list_end = NULL;
69 void cimlib_init(Display *display, Window drawable, Visual *visual, Colormap colourmap)
71 image_list_start = image_list_end = NULL;
72 if (!cache_size_set) cimlib_set_cache_size(DEFAULT_CACHE_SIZE);
73 /* set the maximum number of colors to allocate for 8bpp and less to 256 */
74 imlib_set_color_usage(256);
75 /* dither for depths < 24bpp */
76 imlib_context_set_dither(1);
77 /* set the display , visual, colormap and drawable we are using */
78 imlib_context_set_display(display);
79 imlib_context_set_visual(visual);
80 imlib_context_set_colormap(colourmap);
81 imlib_context_set_drawable(drawable);
84 void cimlib_add_image(const char *args)
86 struct image_list_s *cur = NULL;
89 cur = malloc(sizeof(struct image_list_s));
90 memset(cur, 0, sizeof(struct image_list_s));
92 if (!sscanf(args, "%1024s", cur->name)) {
93 ERR("Invalid args for $image. Format is: '<path to image> (-p x,y) (-s WxH)' (got '%s')", args);
95 to_real_path(cur->name, cur->name);
96 // now we check for optional args
97 tmp = strstr(args, "-p ");
100 sscanf(tmp, "%i,%i", &cur->x, &cur->y);
102 tmp = strstr(args, "-s ");
105 if (sscanf(tmp, "%ix%i", &cur->w, &cur->h)) {
110 if (image_list_end) {
111 image_list_end->next = cur;
112 image_list_end = cur;
114 image_list_start = image_list_end = cur;
118 static void cimlib_draw_image(struct image_list_s *cur, int *clip_x, int *clip_y, int *clip_x2, int *clip_y2)
120 image = imlib_load_image(cur->name);
123 DBGP("Drawing image '%s' at (%i,%i) scaled to %ix%i", cur->name, cur->x, cur->y, cur->w, cur->h);
124 imlib_context_set_image(image);
125 /* turn alpha channel on */
126 imlib_image_set_has_alpha(1);
127 w = imlib_image_get_width();
128 h = imlib_image_get_height();
133 imlib_context_set_image(buffer);
134 imlib_blend_image_onto_image(image, 1, 0, 0, w, h,
135 cur->x, cur->y, cur->w, cur->h);
136 imlib_context_set_image(image);
138 if (cur->x < *clip_x) *clip_x = cur->x;
139 if (cur->y < *clip_y) *clip_y = cur->y;
140 if (cur->x + cur->w > *clip_x2) *clip_x2 = cur->x + cur->w;
141 if (cur->y + cur->h > *clip_y2) *clip_y2 = cur->y + cur->h;
143 ERR("Unable to load image '%s'", cur->name);
147 static void cimlib_draw_all(int *clip_x, int *clip_y, int *clip_x2, int *clip_y2)
149 struct image_list_s *cur = image_list_start;
151 cimlib_draw_image(cur, clip_x, clip_y, clip_x2, clip_y2);
156 void cimlib_render(int x, int y, int width, int height)
158 int clip_x = INT_MAX, clip_y = INT_MAX;
159 int clip_x2 = 0, clip_y2 = 0;
161 if (!image_list_start) return; /* are we actually drawing anything? */
162 /* take all the little rectangles to redraw and merge them into
163 * something sane for rendering */
164 buffer = imlib_create_image(width, height);
165 /* clear our buffer */
166 imlib_context_set_image(buffer);
168 /* we can blend stuff now */
169 imlib_context_set_blend(1);
170 /* turn alpha channel on */
171 imlib_image_set_has_alpha(1);
173 cimlib_draw_all(&clip_x, &clip_y, &clip_x2, &clip_y2);
175 /* set the buffer image as our current image */
176 imlib_context_set_image(buffer);
178 /* setup our clip rect */
179 if (clip_x == INT_MAX) clip_x = 0;
180 if (clip_y == INT_MAX) clip_y = 0;
182 /* render the image at 0, 0 */
183 imlib_render_image_part_on_drawable_at_size(clip_x, clip_y, clip_x2 - clip_x,
184 clip_y2 - clip_y, x + clip_x, y + clip_y, clip_x2 - clip_x,
186 /* don't need that temporary buffer image anymore */