A whole bunch of changes, mostly Lua related.
[monky] / src / imlib2.c
1 /* Conky, a system monitor, based on torsmo
2  *
3  * Please see COPYING for details
4  *
5  * Copyright (c) 2005-2009 Brenden Matthews, et. al.
6  * All rights reserved.
7  *
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.
12  *
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/>.
19  *
20  */
21
22 #include "config.h"
23 #include "imlib2.h"
24 #include "conky.h"
25 #include "logging.h"
26
27 #include <Imlib2.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <limits.h>
31 #include <string.h>
32 #include <time.h>
33
34 struct image_list_s {
35         char name[1024];
36         Imlib_Image image;
37         int x, y, w, h;
38         int wh_set;
39         char no_cache;
40         int flush_interval;
41         struct image_list_s *next;
42 };
43
44 struct image_list_s *image_list_start, *image_list_end;
45
46 /* areas to update */
47 Imlib_Updates updates, current_update;
48 /* our virtual framebuffer image we draw into */
49 Imlib_Image buffer, image;
50
51 static int cache_size_set = 0;
52
53 /* flush the image cache ever X seconds */
54 static int cimlib_cache_flush_interval = 0;
55 static int cimlib_cache_flush_last = 0;
56
57 #define DEFAULT_IMLIB2_CACHE_SIZE 4096 * 1024 /* default cache size for loaded images */
58
59 void cimlib_set_cache_size(long size)
60 {
61         imlib_set_cache_size(size);
62         cache_size_set = 1;
63 }
64
65 void cimlib_set_cache_flush_interval(long interval)
66 {
67         if (interval >= 0) {
68                 cimlib_cache_flush_interval = interval;
69         } else {
70                 ERR("Imlib2: flush interval should be >= 0");
71         }
72 }
73
74 void cimlib_cleanup(void)
75 {
76         struct image_list_s *cur = image_list_start, *last = NULL;
77         while (cur) {
78                 last = cur;
79                 cur = last->next;
80                 free(last);
81         }
82         image_list_start = image_list_end = NULL;
83 }
84
85 void cimlib_init(Display *disp, Window drawable, Visual *visual, Colormap colourmap)
86 {
87         image_list_start = image_list_end = NULL;
88         if (!cache_size_set) cimlib_set_cache_size(DEFAULT_IMLIB2_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(disp);
95         imlib_context_set_visual(visual);
96         imlib_context_set_colormap(colourmap);
97         imlib_context_set_drawable(drawable);
98 }
99
100 void cimlib_add_image(const char *args)
101 {
102         struct image_list_s *cur = NULL;
103         char *tmp;
104
105         cur = malloc(sizeof(struct image_list_s));
106         memset(cur, 0, sizeof(struct image_list_s));
107
108         if (!sscanf(args, "%1023s", cur->name)) {
109                 ERR("Invalid args for $image.  Format is: '<path to image> (-p x,y) (-s WxH) (-n) (-f interval)' (got '%s')", args);
110                 free(cur);
111                 return;
112         }
113         to_real_path(cur->name, cur->name);
114         // now we check for optional args
115         tmp = strstr(args, "-p ");
116         if (tmp) {
117                 tmp += 3;
118                 sscanf(tmp, "%i,%i", &cur->x, &cur->y);
119         }
120         tmp = strstr(args, "-s ");
121         if (tmp) {
122                 tmp += 3;
123                 if (sscanf(tmp, "%ix%i", &cur->w, &cur->h)) {
124                         cur->wh_set = 1;
125                 }
126         }
127
128         tmp = strstr(args, "-n");
129         if (tmp) {
130                 cur->no_cache = 1;
131         }
132
133         tmp = strstr(args, "-f ");
134         if (tmp) {
135                 tmp += 3;
136                 if (sscanf(tmp, "%d", &cur->flush_interval)) {
137                         cur->no_cache = 0;
138                 }
139         }
140         if (cur->flush_interval < 0) {
141                 ERR("Imlib2: flush interval should be >= 0");
142                 cur->flush_interval = 0;
143         }
144
145         if (image_list_end) {
146                 image_list_end->next = cur;
147                 image_list_end = cur;
148         } else {
149                 image_list_start = image_list_end = cur;
150         }
151 }
152
153 static void
154 cimlib_draw_image(struct image_list_s *cur, int *clip_x,
155                         int *clip_y, int *clip_x2, int *clip_y2)
156 {
157         int w, h;
158         time_t now = time(NULL);
159
160         image = imlib_load_image(cur->name);
161         if (!image) {
162                 ERR("Unable to load image '%s'", cur->name);
163                 return;
164         }
165
166         DBGP("Drawing image '%s' at (%i,%i) scaled to %ix%i, "
167              "caching interval set to %i (with -n opt %i)",
168              cur->name, cur->x, cur->y, cur->w, cur->h,
169              cur->flush_interval, cur->no_cache);
170
171         imlib_context_set_image(image);
172         /* turn alpha channel on */
173         imlib_image_set_has_alpha(1);
174         w = imlib_image_get_width();
175         h = imlib_image_get_height();
176         if (!cur->wh_set) {
177                 cur->w = w;
178                 cur->h = h;
179         }
180         imlib_context_set_image(buffer);
181         imlib_blend_image_onto_image(image, 1, 0, 0, w, h,
182                         cur->x, cur->y, cur->w, cur->h);
183         imlib_context_set_image(image);
184         if (cur->no_cache || (cur->flush_interval &&
185                               now % cur->flush_interval == 0)) {
186                 imlib_free_image_and_decache();
187         } else {
188                 imlib_free_image();
189         }
190         if (cur->x < *clip_x) *clip_x = cur->x;
191         if (cur->y < *clip_y) *clip_y = cur->y;
192         if (cur->x + cur->w > *clip_x2) *clip_x2 = cur->x + cur->w;
193         if (cur->y + cur->h > *clip_y2) *clip_y2 = cur->y + cur->h;
194 }
195
196 static void cimlib_draw_all(int *clip_x, int *clip_y, int *clip_x2, int *clip_y2)
197 {
198         struct image_list_s *cur = image_list_start;
199         while (cur) {
200                 cimlib_draw_image(cur, clip_x, clip_y, clip_x2, clip_y2);
201                 cur = cur->next;
202         }
203 }
204
205 void cimlib_render(int x, int y, int width, int height)
206 {
207         int clip_x = INT_MAX, clip_y = INT_MAX;
208         int clip_x2 = 0, clip_y2 = 0;
209         time_t now;
210
211         if (!image_list_start) return; /* are we actually drawing anything? */
212
213         /* cheque if it's time to flush our cache */
214         now = time(NULL);
215         if (cimlib_cache_flush_interval && now - cimlib_cache_flush_interval > cimlib_cache_flush_last) {
216                 int size = imlib_get_cache_size();
217                 imlib_set_cache_size(0);
218                 imlib_set_cache_size(size);
219                 cimlib_cache_flush_last = now;
220                 DBGP("Flushing Imlib2 cache (%li)\n", now);
221         }
222
223         /* take all the little rectangles to redraw and merge them into
224          * something sane for rendering */
225         buffer = imlib_create_image(width, height);
226         /* clear our buffer */
227         imlib_context_set_image(buffer);
228         imlib_image_clear();
229         /* we can blend stuff now */
230         imlib_context_set_blend(1);
231         /* turn alpha channel on */
232         imlib_image_set_has_alpha(1);
233
234         cimlib_draw_all(&clip_x, &clip_y, &clip_x2, &clip_y2);
235
236         /* set the buffer image as our current image */
237         imlib_context_set_image(buffer);
238
239         /* setup our clip rect */
240         if (clip_x == INT_MAX) clip_x = 0;
241         if (clip_y == INT_MAX) clip_y = 0;
242
243         /* render the image at 0, 0 */
244         imlib_render_image_part_on_drawable_at_size(clip_x, clip_y, clip_x2 - clip_x,
245                         clip_y2 - clip_y, x + clip_x, y + clip_y, clip_x2 - clip_x,
246                         clip_y2 - clip_y);
247         /* don't need that temporary buffer image anymore */
248         imlib_free_image();
249 }
250