enables images (via imlib2) -- thanks dbrodie!
[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         imlib_set_cache_size(size);
65         cache_size_set = 1;
66 }
67
68 void cimlib_set_cache_flush_interval(long interval)
69 {
70         if (interval >= 0) {
71                 cimlib_cache_flush_interval = interval;
72         } else {
73                 NORM_ERR("Imlib2: flush interval should be >= 0");
74         }
75 }
76
77 void cimlib_cleanup(void)
78 {
79         struct image_list_s *cur = image_list_start, *last = NULL;
80         while (cur) {
81                 last = cur;
82                 cur = last->next;
83                 free(last);
84         }
85         image_list_start = image_list_end = NULL;
86 }
87
88 Imlib_Context context;
89
90 void cimlib_init(Display *disp, Window drawable, Visual *visual, Colormap
91                 colourmap)
92 {
93         image_list_start = image_list_end = NULL;
94         context = imlib_context_new();
95         imlib_context_push(context);
96         if (!cache_size_set) cimlib_set_cache_size(DEFAULT_IMLIB2_CACHE_SIZE);
97         /* set the maximum number of colors to allocate for 8bpp and less to 256 */
98         imlib_set_color_usage(256);
99         /* dither for depths < 24bpp */
100         imlib_context_set_dither(1);
101         /* set the display , visual, colormap and drawable we are using */
102         imlib_context_set_display(disp);
103         imlib_context_set_visual(visual);
104         imlib_context_set_colormap(colourmap);
105         imlib_context_set_drawable(drawable);
106 }
107
108 void cimlib_deinit(void)
109 {
110         cimlib_cleanup();
111         cache_size_set = 0;
112 //      imlib_context_disconnect_display();
113         imlib_context_pop();
114         imlib_context_free(context);
115 }
116
117 void cimlib_add_image(const char *args)
118 {
119         struct image_list_s *cur = NULL;
120         char *tmp;
121
122         cur = malloc(sizeof(struct image_list_s));
123         memset(cur, 0, sizeof(struct image_list_s));
124
125         if (!sscanf(args, "%1023s", cur->name)) {
126                 NORM_ERR("Invalid args for $image.  Format is: '<path to image> (-p"
127                                 "x,y) (-s WxH) (-n) (-f interval)' (got '%s')", args);
128                 free(cur);
129                 return;
130         }
131         to_real_path(cur->name, cur->name);
132         // now we check for optional args
133         tmp = strstr(args, "-p ");
134         if (tmp) {
135                 tmp += 3;
136                 sscanf(tmp, "%i,%i", &cur->x, &cur->y);
137         }
138         tmp = strstr(args, "-s ");
139         if (tmp) {
140                 tmp += 3;
141                 if (sscanf(tmp, "%ix%i", &cur->w, &cur->h)) {
142                         cur->wh_set = 1;
143                 }
144         }
145
146         tmp = strstr(args, "-n");
147         if (tmp) {
148                 cur->no_cache = 1;
149         }
150
151         tmp = strstr(args, "-f ");
152         if (tmp) {
153                 tmp += 3;
154                 if (sscanf(tmp, "%d", &cur->flush_interval)) {
155                         cur->no_cache = 0;
156                 }
157         }
158         if (cur->flush_interval < 0) {
159                 NORM_ERR("Imlib2: flush interval should be >= 0");
160                 cur->flush_interval = 0;
161         }
162
163         if (image_list_end) {
164                 image_list_end->next = cur;
165                 image_list_end = cur;
166         } else {
167                 image_list_start = image_list_end = cur;
168         }
169 }
170
171 static void cimlib_draw_image(struct image_list_s *cur, int *clip_x, int
172                 *clip_y, int *clip_x2, int *clip_y2)
173 {
174         int w, h;
175         time_t now = time(NULL);
176         static int rep = 0;
177
178         image = imlib_load_image(cur->name);
179         if (!image) {
180                 if (!rep)
181                         NORM_ERR("Unable to load image '%s'", cur->name);
182                 rep = 1;
183                 return;
184         }
185         rep = 0;        /* reset so disappearing images are reported */
186
187         DBGP("Drawing image '%s' at (%i,%i) scaled to %ix%i, "
188              "caching interval set to %i (with -n opt %i)",
189              cur->name, cur->x, cur->y, cur->w, cur->h,
190              cur->flush_interval, cur->no_cache);
191
192         imlib_context_set_image(image);
193         /* turn alpha channel on */
194         imlib_image_set_has_alpha(1);
195         w = imlib_image_get_width();
196         h = imlib_image_get_height();
197         if (!cur->wh_set) {
198                 cur->w = w;
199                 cur->h = h;
200         }
201         imlib_context_set_image(buffer);
202         imlib_blend_image_onto_image(image, 1, 0, 0, w, h,
203                         cur->x, cur->y, cur->w, cur->h);
204         imlib_context_set_image(image);
205         if (cur->no_cache || (cur->flush_interval &&
206                               now % cur->flush_interval == 0)) {
207                 imlib_free_image_and_decache();
208         } else {
209                 imlib_free_image();
210         }
211         if (cur->x < *clip_x) *clip_x = cur->x;
212         if (cur->y < *clip_y) *clip_y = cur->y;
213         if (cur->x + cur->w > *clip_x2) *clip_x2 = cur->x + cur->w;
214         if (cur->y + cur->h > *clip_y2) *clip_y2 = cur->y + cur->h;
215 }
216
217 static void cimlib_draw_all(int *clip_x, int *clip_y, int *clip_x2, int *clip_y2)
218 {
219         struct image_list_s *cur = image_list_start;
220         while (cur) {
221                 cimlib_draw_image(cur, clip_x, clip_y, clip_x2, clip_y2);
222                 cur = cur->next;
223         }
224 }
225
226 void cimlib_render(int x, int y, int width, int height)
227 {
228         int clip_x = INT_MAX, clip_y = INT_MAX;
229         int clip_x2 = 0, clip_y2 = 0;
230         time_t now;
231
232         if (!image_list_start) return; /* are we actually drawing anything? */
233
234         /* cheque if it's time to flush our cache */
235         now = time(NULL);
236         if (cimlib_cache_flush_interval && now - cimlib_cache_flush_interval > cimlib_cache_flush_last) {
237                 int size = imlib_get_cache_size();
238                 imlib_set_cache_size(0);
239                 imlib_set_cache_size(size);
240                 cimlib_cache_flush_last = now;
241                 DBGP("Flushing Imlib2 cache (%li)\n", now);
242         }
243
244         /* take all the little rectangles to redraw and merge them into
245          * something sane for rendering */
246         buffer = imlib_create_image(width, height);
247         /* clear our buffer */
248         imlib_context_set_image(buffer);
249         imlib_image_clear();
250         /* we can blend stuff now */
251         imlib_context_set_blend(1);
252         /* turn alpha channel on */
253         imlib_image_set_has_alpha(1);
254
255         cimlib_draw_all(&clip_x, &clip_y, &clip_x2, &clip_y2);
256
257         /* set the buffer image as our current image */
258         imlib_context_set_image(buffer);
259
260         /* setup our clip rect */
261         if (clip_x == INT_MAX) clip_x = 0;
262         if (clip_y == INT_MAX) clip_y = 0;
263
264         /* render the image at 0, 0 */
265         imlib_render_image_part_on_drawable_at_size(clip_x, clip_y, clip_x2 - clip_x,
266                         clip_y2 - clip_y, x + clip_x, y + clip_y, clip_x2 - clip_x,
267                         clip_y2 - clip_y);
268         /* don't need that temporary buffer image anymore */
269         imlib_free_image();
270 }
271