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