last commit i forgot to save changes in x11.c ; this fixes that file
[monky] / src / imlib2.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  * vim: ts=4 sw=4 noet ai cindent syntax=c
3  *
4  * Conky, a system monitor, based on torsmo
5  *
6  * Please see COPYING for details
7  *
8  * Copyright (c) 2005-2010 Brenden Matthews, et. al.
9  * All rights reserved.
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  */
24
25 #include "config.h"
26 #include "imlib2.h"
27 #include "conky.h"
28 #include "logging.h"
29
30 #include <Imlib2.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <limits.h>
34 #include <string.h>
35 #include <time.h>
36
37 struct image_list_s {
38         char name[1024];
39         Imlib_Image image;
40         int x, y, w, h;
41         int wh_set;
42         char no_cache;
43         int flush_interval;
44         struct image_list_s *next;
45 };
46
47 struct image_list_s *image_list_start, *image_list_end;
48
49 /* areas to update */
50 Imlib_Updates updates, current_update;
51 /* our virtual framebuffer image we draw into */
52 Imlib_Image buffer, image;
53
54 static int cache_size_set = 0;
55
56 /* flush the image cache ever X seconds */
57 static int cimlib_cache_flush_interval = 0;
58 static int cimlib_cache_flush_last = 0;
59
60 #define DEFAULT_IMLIB2_CACHE_SIZE 4096 * 1024 /* default cache size for loaded images */
61
62 void cimlib_set_cache_size(long size)
63 {
64     fprintf(stderr, PACKAGE_NAME": cimlib_set_cache_size start\n");
65         imlib_set_cache_size(size);
66         cache_size_set = 1;
67 }
68
69 void cimlib_set_cache_flush_interval(long interval)
70 {
71     fprintf(stderr, PACKAGE_NAME": cimlib_set_cache_flush_interval start\n");
72         if (interval >= 0) {
73                 cimlib_cache_flush_interval = interval;
74         } else {
75                 NORM_ERR("Imlib2: flush interval should be >= 0");
76         }
77 }
78
79 void cimlib_cleanup(void)
80 {
81     fprintf(stderr, PACKAGE_NAME": cimlib_cleanup start\n");
82         struct image_list_s *cur = image_list_start, *last = NULL;
83         while (cur) {
84                 last = cur;
85                 cur = last->next;
86                 free(last);
87         }
88         image_list_start = image_list_end = NULL;
89 }
90
91 Imlib_Context context;
92
93 void cimlib_init(Display *disp, Window drawable, Visual *visual, Colormap
94                 colourmap)
95 {
96     fprintf(stderr, PACKAGE_NAME": cimlib_init start\n");
97         image_list_start = image_list_end = NULL;
98         context = imlib_context_new();
99         imlib_context_push(context);
100         if (!cache_size_set) cimlib_set_cache_size(DEFAULT_IMLIB2_CACHE_SIZE);
101         /* set the maximum number of colors to allocate for 8bpp and less to 256 */
102         imlib_set_color_usage(256);
103         /* dither for depths < 24bpp */
104         imlib_context_set_dither(1);
105         /* set the display , visual, colormap and drawable we are using */
106         imlib_context_set_display(disp);
107         imlib_context_set_visual(visual);
108         imlib_context_set_colormap(colourmap);
109         imlib_context_set_drawable(drawable);
110         fprintf(stderr, PACKAGE_NAME": cimlib_init okay\n");
111 }
112
113 void cimlib_initp(Display *disp, Pixmap drawable, Visual *visual, Colormap
114                 colourmap)
115 {
116         fprintf(stderr, PACKAGE_NAME": cimlib_initp start\n");
117         image_list_start = image_list_end = NULL;
118         context = imlib_context_new();
119         imlib_context_push(context);
120         if (!cache_size_set) cimlib_set_cache_size(DEFAULT_IMLIB2_CACHE_SIZE);
121         /* set the maximum number of colors to allocate for 8bpp and less to 256 */
122         imlib_set_color_usage(256);
123         /* dither for depths < 24bpp */
124         imlib_context_set_dither(1);
125         /* set the display , visual, colormap and drawable we are using */
126         imlib_context_set_display(disp);
127         imlib_context_set_visual(visual);
128         imlib_context_set_colormap(colourmap);
129         imlib_context_set_drawable(drawable);
130         fprintf(stderr, PACKAGE_NAME": cimlib_initp okay\n");
131 }
132
133 void cimlib_deinit(void)
134 {
135     fprintf(stderr, PACKAGE_NAME": cimlib_deinit start\n");
136         cimlib_cleanup();
137         cache_size_set = 0;
138 //      imlib_context_disconnect_display();
139         imlib_context_pop();
140         imlib_context_free(context);
141         fprintf(stderr, PACKAGE_NAME": cimlib_deinit okay\n");
142 }
143
144 void cimlib_add_image(const char *args)
145 {
146     fprintf(stderr, PACKAGE_NAME": cimlib_add_image start\n");
147         struct image_list_s *cur = NULL;
148         char *tmp;
149
150         cur = malloc(sizeof(struct image_list_s));
151         memset(cur, 0, sizeof(struct image_list_s));
152
153         if (!sscanf(args, "%1023s", cur->name)) {
154                 NORM_ERR("Invalid args for $image.  Format is: '<path to image> (-p"
155                                 "x,y) (-s WxH) (-n) (-f interval)' (got '%s')", args);
156                 free(cur);
157                 return;
158         }
159         to_real_path(cur->name, cur->name);
160         // now we check for optional args
161         tmp = strstr(args, "-p ");
162         if (tmp) {
163                 tmp += 3;
164                 sscanf(tmp, "%i,%i", &cur->x, &cur->y);
165         }
166         tmp = strstr(args, "-s ");
167         if (tmp) {
168                 tmp += 3;
169                 if (sscanf(tmp, "%ix%i", &cur->w, &cur->h)) {
170                         cur->wh_set = 1;
171                 }
172         }
173
174         tmp = strstr(args, "-n");
175         if (tmp) {
176                 cur->no_cache = 1;
177         }
178
179         tmp = strstr(args, "-f ");
180         if (tmp) {
181                 tmp += 3;
182                 if (sscanf(tmp, "%d", &cur->flush_interval)) {
183                         cur->no_cache = 0;
184                 }
185         }
186         if (cur->flush_interval < 0) {
187                 NORM_ERR("Imlib2: flush interval should be >= 0");
188                 cur->flush_interval = 0;
189         }
190
191         if (image_list_end) {
192                 image_list_end->next = cur;
193                 image_list_end = cur;
194         } else {
195                 image_list_start = image_list_end = cur;
196         }
197         fprintf(stderr, PACKAGE_NAME": cimlib_add_image okay\n");
198 }
199
200 static void cimlib_draw_image(struct image_list_s *cur, int *clip_x, int
201                 *clip_y, int *clip_x2, int *clip_y2)
202 {
203     fprintf(stderr, PACKAGE_NAME": cimlib_draw_image start\n");
204         int w, h;
205         time_t now = time(NULL);
206         static int rep = 0;
207
208         image = imlib_load_image(cur->name);
209         if (!image) {
210                 if (!rep)
211                         NORM_ERR("Unable to load image '%s'", cur->name);
212                 rep = 1;
213                 return;
214         }
215         rep = 0;        /* reset so disappearing images are reported */
216
217         DBGP("Drawing image '%s' at (%i,%i) scaled to %ix%i, "
218              "caching interval set to %i (with -n opt %i)",
219              cur->name, cur->x, cur->y, cur->w, cur->h,
220              cur->flush_interval, cur->no_cache);
221
222         imlib_context_set_image(image);
223         /* turn alpha channel on */
224         imlib_image_set_has_alpha(1);
225         w = imlib_image_get_width();
226         h = imlib_image_get_height();
227         if (!cur->wh_set) {
228                 cur->w = w;
229                 cur->h = h;
230         }
231         imlib_context_set_image(buffer);
232         imlib_blend_image_onto_image(image, 1, 0, 0, w, h,
233                         cur->x, cur->y, cur->w, cur->h);
234         imlib_context_set_image(image);
235         if (cur->no_cache || (cur->flush_interval &&
236                               now % cur->flush_interval == 0)) {
237                 imlib_free_image_and_decache();
238         } else {
239                 imlib_free_image();
240         }
241         if (cur->x < *clip_x) *clip_x = cur->x;
242         if (cur->y < *clip_y) *clip_y = cur->y;
243         if (cur->x + cur->w > *clip_x2) *clip_x2 = cur->x + cur->w;
244         if (cur->y + cur->h > *clip_y2) *clip_y2 = cur->y + cur->h;
245         fprintf(stderr, PACKAGE_NAME": cimlib_draw_image okay\n");
246 }
247
248 static void cimlib_draw_all(int *clip_x, int *clip_y, int *clip_x2, int *clip_y2)
249 {
250     fprintf(stderr, PACKAGE_NAME": cimlib_draw_all start\n");
251         struct image_list_s *cur = image_list_start;
252         while (cur) {
253                 cimlib_draw_image(cur, clip_x, clip_y, clip_x2, clip_y2);
254                 cur = cur->next;
255         }
256         fprintf(stderr, PACKAGE_NAME": cimlib_draw_all okay\n");
257 }
258
259 void cimlib_render(int x, int y, int width, int height)
260 {
261
262         int clip_x = INT_MAX, clip_y = INT_MAX;
263         int clip_x2 = 0, clip_y2 = 0;
264         time_t now;
265
266         if (!image_list_start) return; /* are we actually drawing anything? */
267         fprintf(stderr, PACKAGE_NAME": cimlib_render start\n");
268         /* check if it's time to flush our cache */
269         now = time(NULL);
270         if (cimlib_cache_flush_interval && now - cimlib_cache_flush_interval > cimlib_cache_flush_last) {
271                 int size = imlib_get_cache_size();
272                 imlib_set_cache_size(0);
273                 imlib_set_cache_size(size);
274                 cimlib_cache_flush_last = now;
275                 DBGP("Flushing Imlib2 cache (%li)\n", now);
276         }
277         fprintf(stderr, PACKAGE_NAME": render1\n");
278         /* take all the little rectangles to redraw and merge them into
279          * something sane for rendering */
280         buffer = imlib_create_image(width, height);
281         fprintf(stderr, PACKAGE_NAME": render2\n");
282         /* clear our buffer */
283         imlib_context_set_image(buffer);
284         fprintf(stderr, PACKAGE_NAME": render3\n");
285         imlib_image_clear();
286         fprintf(stderr, PACKAGE_NAME": render4\n");
287         /* we can blend stuff now */
288         imlib_context_set_blend(1);
289         fprintf(stderr, PACKAGE_NAME": render5\n");
290         /* turn alpha channel on */
291         imlib_image_set_has_alpha(1);
292         fprintf(stderr, PACKAGE_NAME": rende6\n");
293
294         cimlib_draw_all(&clip_x, &clip_y, &clip_x2, &clip_y2);
295         fprintf(stderr, PACKAGE_NAME": render7\n");
296
297         /* set the buffer image as our current image */
298         imlib_context_set_image(buffer);
299         fprintf(stderr, PACKAGE_NAME": render8\n");
300
301         /* setup our clip rect */
302         if (clip_x == INT_MAX) clip_x = 0;
303         if (clip_y == INT_MAX) clip_y = 0;
304         fprintf(stderr, PACKAGE_NAME": render9\n");
305
306         /* render the image at 0, 0 */
307         imlib_render_image_part_on_drawable_at_size(clip_x, clip_y, clip_x2 - clip_x,
308                         clip_y2 - clip_y, x + clip_x, y + clip_y, clip_x2 - clip_x,
309                         clip_y2 - clip_y);
310         fprintf(stderr, PACKAGE_NAME": render10\n");
311         /* don't need that temporary buffer image anymore */
312         imlib_free_image();
313         fprintf(stderr, PACKAGE_NAME": cimlib_render okay\n");
314 }
315