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