Add x11-xcb to the pkg-config checks
[awesome.git] / draw.c
blob06ddf5058076d938fc56b73ef4e299ff73c36037
1 /*
2 * draw.c - draw functions
4 * Copyright © 2007-2009 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <cairo-xcb.h>
24 #include "config.h"
26 #include <langinfo.h>
27 #include <iconv.h>
28 #include <errno.h>
29 #include <ctype.h>
30 #include <math.h>
32 #include <Imlib2.h>
34 #include "globalconf.h"
35 #include "screen.h"
37 #include "common/xutil.h"
39 /** Convert text from any charset to UTF-8 using iconv.
40 * \param iso The ISO string to convert.
41 * \param len The string size.
42 * \param dest The destination pointer. Memory will be allocated, up to you to
43 * free, like any char *.
44 * \param dlen The destination length, can be NULL.
45 * \return True if conversion was done.
47 bool
48 draw_iso2utf8(const char *iso, size_t len, char **dest, ssize_t *dlen)
50 static iconv_t iso2utf8 = (iconv_t) -1;
51 static int8_t dont_need_convert = -1;
53 if(dont_need_convert == -1)
54 dont_need_convert = A_STREQ(nl_langinfo(CODESET), "UTF-8");
56 if(!len || dont_need_convert)
57 return false;
59 if(iso2utf8 == (iconv_t) -1)
61 iso2utf8 = iconv_open("UTF-8", nl_langinfo(CODESET));
62 if(iso2utf8 == (iconv_t) -1)
64 if(errno == EINVAL)
65 warn("unable to convert text from %s to UTF-8, not available",
66 nl_langinfo(CODESET));
67 else
68 warn("unable to convert text: %s", strerror(errno));
70 return false;
74 size_t orig_utf8len, utf8len;
75 char *utf8;
77 orig_utf8len = utf8len = 2 * len + 1;
78 utf8 = *dest = p_new(char, utf8len);
80 if(iconv(iso2utf8, (char **) &iso, &len, &utf8, &utf8len) == (size_t) -1)
82 warn("text conversion failed: %s", strerror(errno));
83 p_delete(dest);
84 return false;
87 if(dlen)
88 *dlen = orig_utf8len - utf8len;
90 return true;
93 static cairo_user_data_key_t data_key;
95 static inline void
96 free_data(void *data)
98 p_delete(&data);
101 /** Create a surface object on the lua stack from this image data.
102 * \param L The lua stack.
103 * \param width The width of the image.
104 * \param height The height of the image
105 * \param data The image's data in ARGB format, will be copied by this function.
106 * \return Number of items pushed on the lua stack.
108 cairo_surface_t *
109 draw_surface_from_data(int width, int height, uint32_t *data)
111 unsigned long int len = width * height;
112 unsigned long int i;
113 uint32_t *buffer = p_new(uint32_t, len);
114 cairo_surface_t *surface;
116 /* Cairo wants premultiplied alpha, meh :( */
117 for(i = 0; i < len; i++)
119 uint8_t a = (data[i] >> 24) & 0xff;
120 double alpha = a / 255.0;
121 uint8_t r = ((data[i] >> 16) & 0xff) * alpha;
122 uint8_t g = ((data[i] >> 8) & 0xff) * alpha;
123 uint8_t b = ((data[i] >> 0) & 0xff) * alpha;
124 buffer[i] = (a << 24) | (r << 16) | (g << 8) | b;
127 surface =
128 cairo_image_surface_create_for_data((unsigned char *) buffer,
129 CAIRO_FORMAT_ARGB32,
130 width,
131 height,
132 width*4);
133 /* This makes sure that buffer will be freed */
134 cairo_surface_set_user_data(surface, &data_key, buffer, &free_data);
136 return surface;
139 /** Duplicate the specified image surface.
140 * \param surface The surface to copy
141 * \return A pointer to a new cairo image surface.
143 cairo_surface_t *
144 draw_dup_image_surface(cairo_surface_t *surface)
146 cairo_surface_t *res = cairo_image_surface_create(
147 cairo_image_surface_get_format(surface),
148 cairo_image_surface_get_width(surface),
149 cairo_image_surface_get_height(surface));
151 cairo_t *cr = cairo_create(res);
152 cairo_set_source_surface(cr, surface, 0, 0);
153 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
154 cairo_paint(cr);
155 cairo_destroy(cr);
157 return res;
160 static const char *
161 image_imlib_load_strerror(Imlib_Load_Error e)
163 switch(e)
165 case IMLIB_LOAD_ERROR_FILE_DOES_NOT_EXIST:
166 return "no such file or directory";
167 case IMLIB_LOAD_ERROR_FILE_IS_DIRECTORY:
168 return "file is a directory";
169 case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_READ:
170 return "read permission denied";
171 case IMLIB_LOAD_ERROR_NO_LOADER_FOR_FILE_FORMAT:
172 return "no loader for file format";
173 case IMLIB_LOAD_ERROR_PATH_TOO_LONG:
174 return "path too long";
175 case IMLIB_LOAD_ERROR_PATH_COMPONENT_NON_EXISTANT:
176 return "path component non existent";
177 case IMLIB_LOAD_ERROR_PATH_COMPONENT_NOT_DIRECTORY:
178 return "path component not a directory";
179 case IMLIB_LOAD_ERROR_PATH_POINTS_OUTSIDE_ADDRESS_SPACE:
180 return "path points outside address space";
181 case IMLIB_LOAD_ERROR_TOO_MANY_SYMBOLIC_LINKS:
182 return "too many symbolic links";
183 case IMLIB_LOAD_ERROR_OUT_OF_MEMORY:
184 return "out of memory";
185 case IMLIB_LOAD_ERROR_OUT_OF_FILE_DESCRIPTORS:
186 return "out of file descriptors";
187 case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_WRITE:
188 return "write permission denied";
189 case IMLIB_LOAD_ERROR_OUT_OF_DISK_SPACE:
190 return "out of disk space";
191 case IMLIB_LOAD_ERROR_UNKNOWN:
192 return "unknown error, that's really bad";
193 case IMLIB_LOAD_ERROR_NONE:
194 return "no error, oops";
197 return "unknown error";
200 /** Load the specified path into a cairo surface
201 * \param L Lua state
202 * \param path file to load
203 * \return A cairo image surface or NULL on error.
205 cairo_surface_t *
206 draw_load_image(lua_State *L, const char *path)
208 Imlib_Image imimage;
209 Imlib_Load_Error e = IMLIB_LOAD_ERROR_NONE;
210 cairo_surface_t *ret;
212 imimage = imlib_load_image_with_error_return(path, &e);
213 if (!imimage) {
214 luaL_error(L, "Cannot load image '%s': %s", path, image_imlib_load_strerror(e));
215 return NULL;
218 imlib_context_set_image(imimage);
219 ret = draw_surface_from_data(imlib_image_get_width(),
220 imlib_image_get_height(), imlib_image_get_data_for_reading_only());
221 imlib_free_image_and_decache();
222 return ret;
225 xcb_visualtype_t *draw_default_visual(const xcb_screen_t *s)
227 xcb_depth_iterator_t depth_iter = xcb_screen_allowed_depths_iterator(s);
229 if(depth_iter.data)
230 for(; depth_iter.rem; xcb_depth_next (&depth_iter))
231 for(xcb_visualtype_iterator_t visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
232 visual_iter.rem; xcb_visualtype_next (&visual_iter))
233 if(s->root_visual == visual_iter.data->visual_id)
234 return visual_iter.data;
236 return NULL;
239 xcb_visualtype_t *draw_argb_visual(const xcb_screen_t *s)
241 xcb_depth_iterator_t depth_iter = xcb_screen_allowed_depths_iterator(s);
243 if(depth_iter.data)
244 for(; depth_iter.rem; xcb_depth_next (&depth_iter))
245 if(depth_iter.data->depth == 32)
246 for(xcb_visualtype_iterator_t visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
247 visual_iter.rem; xcb_visualtype_next (&visual_iter))
248 return visual_iter.data;
250 return NULL;
253 uint8_t draw_visual_depth(const xcb_screen_t *s, xcb_visualid_t vis)
255 xcb_depth_iterator_t depth_iter = xcb_screen_allowed_depths_iterator(s);
257 if(depth_iter.data)
258 for(; depth_iter.rem; xcb_depth_next (&depth_iter))
259 for(xcb_visualtype_iterator_t visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
260 visual_iter.rem; xcb_visualtype_next (&visual_iter))
261 if(vis == visual_iter.data->visual_id)
262 return depth_iter.data->depth;
264 fatal("Could not find a visual's depth");
267 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80