Drawable: Improve signal behavior
[awesome.git] / draw.c
blob0c83b96584ff62fc9c71f05317a1a53ba2827b7e
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 <gdk-pixbuf/gdk-pixbuf.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 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 /** Create a surface object from this pixbuf
140 * \param buf The pixbuf
141 * \return Number of items pushed on the lua stack.
143 static cairo_surface_t *
144 draw_surface_from_pixbuf(GdkPixbuf *buf)
146 int width = gdk_pixbuf_get_width(buf);
147 int height = gdk_pixbuf_get_height(buf);
148 int pix_stride = gdk_pixbuf_get_rowstride(buf);
149 guchar *pixels = gdk_pixbuf_get_pixels(buf);
150 int channels = gdk_pixbuf_get_n_channels(buf);
151 cairo_surface_t *surface;
152 int cairo_stride;
153 unsigned char *cairo_pixels;
155 cairo_format_t format = CAIRO_FORMAT_ARGB32;
156 if (channels == 3)
157 format = CAIRO_FORMAT_RGB24;
159 surface = cairo_image_surface_create(format, width, height);
160 cairo_surface_flush(surface);
161 cairo_stride = cairo_image_surface_get_stride(surface);
162 cairo_pixels = cairo_image_surface_get_data(surface);
164 for (int y = 0; y < height; y++)
166 guchar *row = pixels;
167 uint32_t *cairo = (uint32_t *) cairo_pixels;
168 for (int x = 0; x < width; x++) {
169 if (channels == 3)
171 uint8_t r = *row++;
172 uint8_t g = *row++;
173 uint8_t b = *row++;
174 *cairo++ = (r << 16) | (g << 8) | b;
175 } else {
176 uint8_t r = *row++;
177 uint8_t g = *row++;
178 uint8_t b = *row++;
179 uint8_t a = *row++;
180 double alpha = a / 255.0;
181 r = r * alpha;
182 g = g * alpha;
183 b = b * alpha;
184 *cairo++ = (a << 24) | (r << 16) | (g << 8) | b;
187 pixels += pix_stride;
188 cairo_pixels += cairo_stride;
191 cairo_surface_mark_dirty(surface);
192 return surface;
195 /** Duplicate the specified image surface.
196 * \param surface The surface to copy
197 * \return A pointer to a new cairo image surface.
199 cairo_surface_t *
200 draw_dup_image_surface(cairo_surface_t *surface)
202 cairo_surface_t *res = cairo_image_surface_create(
203 cairo_image_surface_get_format(surface),
204 cairo_image_surface_get_width(surface),
205 cairo_image_surface_get_height(surface));
207 cairo_t *cr = cairo_create(res);
208 cairo_set_source_surface(cr, surface, 0, 0);
209 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
210 cairo_paint(cr);
211 cairo_destroy(cr);
213 return res;
216 /** Load the specified path into a cairo surface
217 * \param L Lua state
218 * \param path file to load
219 * \return A cairo image surface or NULL on error.
221 cairo_surface_t *
222 draw_load_image(lua_State *L, const char *path)
224 GError *error = NULL;
225 cairo_surface_t *ret;
226 GdkPixbuf *buf = gdk_pixbuf_new_from_file(path, &error);
228 if (!buf) {
229 luaL_where(L, 1);
230 lua_pushstring(L, error->message);
231 lua_concat(L, 2);
232 g_error_free(error);
233 lua_error(L);
234 return NULL;
237 ret = draw_surface_from_pixbuf(buf);
238 g_object_unref(buf);
239 return ret;
242 xcb_visualtype_t *draw_default_visual(const xcb_screen_t *s)
244 xcb_depth_iterator_t depth_iter = xcb_screen_allowed_depths_iterator(s);
246 if(depth_iter.data)
247 for(; depth_iter.rem; xcb_depth_next (&depth_iter))
248 for(xcb_visualtype_iterator_t visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
249 visual_iter.rem; xcb_visualtype_next (&visual_iter))
250 if(s->root_visual == visual_iter.data->visual_id)
251 return visual_iter.data;
253 return NULL;
256 xcb_visualtype_t *draw_argb_visual(const xcb_screen_t *s)
258 xcb_depth_iterator_t depth_iter = xcb_screen_allowed_depths_iterator(s);
260 if(depth_iter.data)
261 for(; depth_iter.rem; xcb_depth_next (&depth_iter))
262 if(depth_iter.data->depth == 32)
263 for(xcb_visualtype_iterator_t visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
264 visual_iter.rem; xcb_visualtype_next (&visual_iter))
265 return visual_iter.data;
267 return NULL;
270 uint8_t draw_visual_depth(const xcb_screen_t *s, xcb_visualid_t vis)
272 xcb_depth_iterator_t depth_iter = xcb_screen_allowed_depths_iterator(s);
274 if(depth_iter.data)
275 for(; depth_iter.rem; xcb_depth_next (&depth_iter))
276 for(xcb_visualtype_iterator_t visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
277 visual_iter.rem; xcb_visualtype_next (&visual_iter))
278 if(vis == visual_iter.data->visual_id)
279 return depth_iter.data->depth;
281 fatal("Could not find a visual's depth");
284 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80