Don't need a prev ptr in this list.
[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 "imlib2.h"
23 #include "config.h"
24 #include "logging.h"
25
26 #include <Imlib2.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #define NAME_SIZE 1024
32
33 struct image_list_s {
34         char name[NAME_SIZE];
35         Imlib_Image image;
36         int x, y, w, h;
37         int wh_set;
38         struct image_list_s *next;
39 };
40
41 struct image_list_s *image_list_start, *image_list_end;
42
43 /* areas to update */
44 Imlib_Updates updates, current_update;
45 /* our virtual framebuffer image we draw into */
46 Imlib_Image buffer, image;
47
48 #define DEFAULT_CACHE_SIZE 4096 * 1024 /* default cache size for loaded images */
49
50 void cimlib_set_cache_size(long size)
51 {
52         imlib_set_cache_size(size);
53 }
54
55 void cimlib_cleanup(void)
56 {
57         struct image_list_s *cur = image_list_start, *last = NULL;
58         while (cur) {
59                 last = cur;
60                 cur = last->next;
61                 free(last);
62         }
63         image_list_start = image_list_end = NULL;
64 }
65
66 void cimlib_init(Display *display, Window drawable, Visual *visual, Colormap colourmap)
67 {
68         image_list_start = image_list_end = NULL;
69         cimlib_set_cache_size(DEFAULT_CACHE_SIZE);
70         /* set the maximum number of colors to allocate for 8bpp and less to 256 */
71         imlib_set_color_usage(256);
72         /* dither for depths < 24bpp */
73         imlib_context_set_dither(1);
74         /* set the display , visual, colormap and drawable we are using */
75         imlib_context_set_display(display);
76         imlib_context_set_visual(visual);
77         imlib_context_set_colormap(colourmap);
78         imlib_context_set_drawable(drawable);
79 }
80
81 void cimlib_add_image(const char *args)
82 {
83         struct image_list_s *cur = NULL;
84         char *tmp;
85
86         cur = malloc(sizeof(struct image_list_s));
87         memset(cur, 0, sizeof(struct image_list_s));
88
89         if (!sscanf(args, "%1024s", cur->name)) {
90                 ERR("Invalid args for $image.  Format is: '<path to image> (-p x,y) (-s WxH)' (got '%s')", args);
91         }
92         // now we check for optional args
93         tmp = strstr(args, "-p ");
94         if (tmp) {
95                 tmp += 3;
96                 sscanf(tmp, "%i,%i", &cur->x, &cur->y);
97         }
98         tmp = strstr(args, "-s ");
99         if (tmp) {
100                 tmp += 3;
101                 if (sscanf(tmp, "%ix%i", &cur->w, &cur->h)) {
102                         cur->wh_set = 1;
103                 }
104         }
105
106         if (image_list_end) {
107                 image_list_end->next = cur;
108                 image_list_end = cur;
109         } else {
110                 image_list_start = image_list_end = cur;
111         }
112 }
113
114 static void cimlib_draw_image(struct image_list_s *cur)
115 {
116         image = imlib_load_image(cur->name);
117         if (image) {
118                 int w, h;
119                 DBGP("Drawing image '%s' at (%i,%i) scaled to %ix%i", cur->name, cur->x, cur->y, cur->w, cur->h);
120                 imlib_context_set_image(image);
121                 w = imlib_image_get_width();
122                 h = imlib_image_get_height();
123                 imlib_context_set_image(buffer);
124                 imlib_blend_image_onto_image(image, 1, 0, 0, h, w,
125                                 cur->x, cur->y, cur->w, cur->h);
126                 imlib_context_set_image(image);
127                 imlib_free_image();
128         } else {
129                 ERR("Unable to load image '%s'", cur->name);
130         }
131 }
132
133 static void cimlib_draw_all(void)
134 {
135         struct image_list_s *cur = image_list_start;
136         while (cur) {
137                 cimlib_draw_image(cur);
138                 cur = cur->next;
139         }
140 }
141
142 void cimlib_render(int x, int y, int width, int height)
143 {
144         if (!image_list_start) return; /* are we actually drawing anything? */
145         /* take all the little rectangles to redraw and merge them into
146          * something sane for rendering */
147         buffer = imlib_create_image(width, height);
148         /* clear our buffer */
149         imlib_context_set_image(buffer);
150         imlib_image_clear();
151         /* we can blend stuff now */
152         imlib_context_set_blend(1);
153
154         cimlib_draw_all();
155
156         /* set the buffer image as our current image */
157         imlib_context_set_image(buffer);
158         /* render the image at 0, 0 */
159         imlib_render_image_on_drawable(x, y);
160         /* don't need that temporary buffer image anymore */
161         imlib_free_image();
162 }
163