diff --git a/client.c b/client.c index ba796070..d3cb55b1 100644 --- a/client.c +++ b/client.c @@ -93,9 +93,9 @@ client_maybevisible(client_t *c, int screen) /** Return the content of a client as an image. * That's just take a screenshot. * \param c The client. - * \return An image. + * \return 1 if the image has been pushed on stack, false otherwise. */ -static image_t * +static int client_getcontent(client_t *c) { xcb_image_t *ximage = xcb_image_get(globalconf.connection, @@ -104,24 +104,27 @@ client_getcontent(client_t *c) c->geometries.internal.width, c->geometries.internal.height, ~0, XCB_IMAGE_FORMAT_Z_PIXMAP); + int retval = 0; - if(!ximage || ximage->bpp < 24) - return NULL; - - uint32_t *data = p_alloca(uint32_t, ximage->width * ximage->height); - - for(int y = 0; y < ximage->height; y++) - for(int x = 0; x < ximage->width; x++) + if(ximage) + { + if(ximage->bpp >= 24) { - data[y * ximage->width + x] = xcb_image_get_pixel(ximage, x, y); - data[y * ximage->width + x] |= 0xff000000; /* set alpha to 0xff */ + uint32_t *data = p_alloca(uint32_t, ximage->width * ximage->height); + + for(int y = 0; y < ximage->height; y++) + for(int x = 0; x < ximage->width; x++) + { + data[y * ximage->width + x] = xcb_image_get_pixel(ximage, x, y); + data[y * ximage->width + x] |= 0xff000000; /* set alpha to 0xff */ + } + + retval = image_new_from_argb32(ximage->width, ximage->height, data); } + xcb_image_destroy(ximage); + } - image_t *image = image_new_from_argb32(ximage->width, ximage->height, data); - - xcb_image_destroy(ximage); - - return image; + return retval; } /** Get a client by its window. @@ -425,7 +428,6 @@ client_manage(xcb_window_t w, xcb_get_geometry_reply_t *wgeom, int phys_screen, { xcb_get_property_cookie_t ewmh_icon_cookie; client_t *c, *tc = NULL; - image_t *icon; int screen; const uint32_t select_input_val[] = { CLIENT_SELECT_INPUT_EVENT_MASK }; @@ -457,8 +459,9 @@ client_manage(xcb_window_t w, xcb_get_geometry_reply_t *wgeom, int phys_screen, c->geometries.internal.width = wgeom->width; c->geometries.internal.height = wgeom->height; client_setborder(c, wgeom->border_width); - if((icon = ewmh_window_icon_get_reply(ewmh_icon_cookie))) - c->icon = image_ref(&icon); + + if(ewmh_window_icon_get_reply(ewmh_icon_cookie)) + c->icon = image_ref(globalconf.L); /* we honor size hints by default */ c->size_hints_honor = true; @@ -1650,7 +1653,6 @@ luaA_client_newindex(lua_State *L) double d; int i; wibox_t **t = NULL; - image_t **image; if((*c)->invalid) luaL_error(L, "client is invalid\n"); @@ -1687,10 +1689,8 @@ luaA_client_newindex(lua_State *L) client_setmaxvert(*c, luaA_checkboolean(L, 3)); break; case A_TK_ICON: - image = luaA_checkudata(L, 3, "image"); - image_unref(&(*c)->icon); - image_ref(image); - (*c)->icon = *image; + image_unref(L, (*c)->icon); + (*c)->icon = image_ref(L); /* execute hook */ hooks_property(*c, "icon"); break; @@ -1808,8 +1808,6 @@ luaA_client_index(lua_State *L) switch(a_tokenize(buf, len)) { - image_t *image; - case A_TK_NAME: lua_pushstring(L, (*c)->name); break; @@ -1821,9 +1819,7 @@ luaA_client_index(lua_State *L) lua_pushboolean(L, (*c)->skiptb); break; case A_TK_CONTENT: - if((image = client_getcontent(*c))) - return luaA_image_userdata_new(L, image); - return 0; + return client_getcontent(*c); case A_TK_TYPE: switch((*c)->type) { @@ -1937,10 +1933,7 @@ luaA_client_index(lua_State *L) lua_pushboolean(L, (*c)->ismaxvert); break; case A_TK_ICON: - if((*c)->icon) - luaA_image_userdata_new(L, (*c)->icon); - else - return 0; + image_push(L, (*c)->icon); break; case A_TK_OPACITY: if((d = window_opacity_get((*c)->win)) >= 0) diff --git a/ewmh.c b/ewmh.c index 7d8b9318..3640a20e 100644 --- a/ewmh.c +++ b/ewmh.c @@ -656,32 +656,29 @@ ewmh_window_icon_get_unchecked(xcb_window_t w) _NET_WM_ICON, CARDINAL, 0, UINT32_MAX); } -image_t * +int ewmh_window_icon_from_reply(xcb_get_property_reply_t *r) { uint32_t *data; - if(!r || r->type != CARDINAL || r->format != 32 || r->length < 2 || - !(data = (uint32_t *) xcb_get_property_value(r))) - return NULL; - - if(data[0] && data[1]) + if(r && r->type == CARDINAL && r->format == 32 && r->length >= 2 && + (data = (uint32_t *) xcb_get_property_value(r)) && data[0] && data[1]) return image_new_from_argb32(data[0], data[1], data + 2); - return NULL; + return 0; } /** Get NET_WM_ICON. * \param cookie The cookie. - * \return A draw_image_t structure which must be deleted after usage. + * \return The number of elements on stack. */ -image_t * +int ewmh_window_icon_get_reply(xcb_get_property_cookie_t cookie) { xcb_get_property_reply_t *r = xcb_get_property_reply(globalconf.connection, cookie, NULL); - image_t *icon = ewmh_window_icon_from_reply(r); + int ret = ewmh_window_icon_from_reply(r); p_delete(&r); - return icon; + return ret; } // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80 diff --git a/ewmh.h b/ewmh.h index 5a610d69..dfad0a1c 100644 --- a/ewmh.h +++ b/ewmh.h @@ -39,8 +39,8 @@ void ewmh_update_workarea(int); void ewmh_process_client_strut(client_t *, xcb_get_property_reply_t *); void ewmh_update_client_strut(client_t *c); xcb_get_property_cookie_t ewmh_window_icon_get_unchecked(xcb_window_t); -image_t *ewmh_window_icon_from_reply(xcb_get_property_reply_t *); -image_t *ewmh_window_icon_get_reply(xcb_get_property_cookie_t); +int ewmh_window_icon_from_reply(xcb_get_property_reply_t *); +int ewmh_window_icon_get_reply(xcb_get_property_cookie_t); #endif // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80 diff --git a/image.c b/image.c index 5c12ca75..a57f22af 100644 --- a/image.c +++ b/image.c @@ -1,7 +1,7 @@ /* * image.c - image object * - * Copyright © 2008 Julien Danjou + * Copyright © 2008-2009 Julien Danjou * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -21,9 +21,17 @@ #include "structs.h" -DO_LUA_NEW(extern, image_t, image, "image", image_ref) -DO_LUA_GC(image_t, image, "image", image_unref) -DO_LUA_EQ(image_t, image, "image") +DO_LUA_TOSTRING(image_t, image, "image"); + +static int +luaA_image_gc(lua_State *L) +{ + image_t *p = luaL_checkudata(L, 1, "image"); + imlib_context_set_image(p->image); + imlib_free_image(); + p_delete(&p->data); + return 0; +} static const char * image_imlib_load_strerror(Imlib_Load_Error e) @@ -104,31 +112,31 @@ image_compute(image_t *image) * \param width The image width. * \param height The image height. * \param data The image data. - * \return A brand new image. + * \return 1 if an image has been pushed on stack, 0 otherwise. */ -image_t * +int image_new_from_argb32(int width, int height, uint32_t *data) { Imlib_Image imimage; - image_t *image = NULL; if((imimage = imlib_create_image_using_copied_data(width, height, data))) { imlib_context_set_image(imimage); imlib_image_set_has_alpha(true); - image = p_new(image_t, 1); + image_t *image = image_new(globalconf.L); image->image = imimage; image_compute(image); + return 1; } - return image; + return 0; } /** Load an image from filename. * \param filename The image file to load. - * \return A new image. + * \return 1 if image is loaded and on stack, 0 otherwise. */ -static image_t * +static int image_new_from_file(const char *filename) { Imlib_Image imimage; @@ -136,20 +144,20 @@ image_new_from_file(const char *filename) image_t *image; if(!filename) - return NULL; + return 0; if(!(imimage = imlib_load_image_with_error_return(filename, &e))) { warn("cannot load image %s: %s", filename, image_imlib_load_strerror(e)); - return NULL; + return 0; } - image = p_new(image_t, 1); + image = image_new(globalconf.L); image->image = imimage; image_compute(image); - return image; + return 1; } /** Create a new image object. @@ -167,11 +175,7 @@ luaA_image_new(lua_State *L) const char *filename; if((filename = lua_tostring(L, 2))) - { - image_t *image; - if((image = image_new_from_file(filename))) - return luaA_image_userdata_new(L, image); - } + return image_new_from_file(filename); else if(lua_isnil(L, 2)) { int width = luaL_checknumber(L, 3); @@ -181,10 +185,10 @@ luaA_image_new(lua_State *L) luaL_error(L, "request image has invalid size"); Imlib_Image imimage = imlib_create_image(width, height); - image_t *image = p_new(image_t, 1); + image_t *image = image_new(L); image->image = imimage; image_compute(image); - return luaA_image_userdata_new(L, image); + return 1; } return 0; @@ -203,7 +207,6 @@ static int luaA_image_argb32_new(lua_State *L) { size_t len; - image_t *image; unsigned int width = luaL_checknumber(L, 1); unsigned int height = luaL_checknumber(L, 2); const char *data = luaL_checklstring(L, 3, &len); @@ -211,10 +214,7 @@ luaA_image_argb32_new(lua_State *L) if(width * height * 4 != len) luaL_error(L, "string size does not match image size"); - if((image = image_new_from_argb32(width, height, (uint32_t *) data))) - return luaA_image_userdata_new(L, image); - - return 0; + return image_new_from_argb32(width, height, (uint32_t *) data); } /** Performs 90 degree rotations on the current image. Passing 0 orientation @@ -229,13 +229,13 @@ luaA_image_argb32_new(lua_State *L) static int luaA_image_orientate(lua_State *L) { - image_t **image = luaA_checkudata(L, 1, "image"); + image_t *image = luaL_checkudata(L, 1, "image"); int orientation = luaL_checknumber(L, 2); - imlib_context_set_image((*image)->image); + imlib_context_set_image(image->image); imlib_image_orientate(orientation); - image_compute(*image); + image_compute(image); return 0; } @@ -251,17 +251,17 @@ luaA_image_orientate(lua_State *L) static int luaA_image_rotate(lua_State *L) { - image_t **image = luaA_checkudata(L, 1, "image"), *new; + image_t *image = luaL_checkudata(L, 1, "image"), *new; double angle = luaL_checknumber(L, 2); - new = p_new(image_t, 1); + new = image_new(L); - imlib_context_set_image((*image)->image); + imlib_context_set_image(image->image); new->image = imlib_create_rotated_image(angle); image_compute(new); - return luaA_image_userdata_new(L, new); + return 1; } /** Crop an image to the given rectangle. @@ -277,20 +277,20 @@ luaA_image_rotate(lua_State *L) static int luaA_image_crop(lua_State *L) { - image_t **image = luaA_checkudata(L, 1, "image"), *new; + image_t *image = luaL_checkudata(L, 1, "image"), *new; int x = luaL_checkint(L, 2); int y = luaL_checkint(L, 3); int w = luaL_checkint(L, 4); int h = luaL_checkint(L, 5); - new = p_new(image_t, 1); + new = image_new(L); - imlib_context_set_image((*image)->image); + imlib_context_set_image(image->image); new->image = imlib_create_cropped_image(x, y, w, h); image_compute(new); - return luaA_image_userdata_new(L, new); + return 1; } /** Crop the image to the given rectangle and scales it. @@ -309,7 +309,7 @@ luaA_image_crop(lua_State *L) static int luaA_image_crop_and_scale(lua_State *L) { - image_t **image = luaA_checkudata(L, 1, "image"), *new; + image_t *image = luaL_checkudata(L, 1, "image"), *new; int source_x = luaL_checkint(L, 2); int source_y = luaL_checkint(L, 3); int w = luaL_checkint(L, 4); @@ -317,9 +317,9 @@ luaA_image_crop_and_scale(lua_State *L) int dest_w = luaL_checkint(L, 6); int dest_h = luaL_checkint(L, 7); - new = p_new(image_t, 1); + new = image_new(L); - imlib_context_set_image((*image)->image); + imlib_context_set_image(image->image); new->image = imlib_create_cropped_scaled_image(source_x, source_y, w, h, @@ -327,7 +327,7 @@ luaA_image_crop_and_scale(lua_State *L) image_compute(new); - return luaA_image_userdata_new(L, new); + return 1; } /** Saves the image to the given path. The file extension (e.g. .png or .jpg) @@ -341,11 +341,11 @@ luaA_image_crop_and_scale(lua_State *L) static int luaA_image_save(lua_State *L) { - image_t **image = luaA_checkudata(L, 1, "image"); + image_t *image = luaL_checkudata(L, 1, "image"); const char *path = luaL_checkstring(L, 2); Imlib_Load_Error err; - imlib_context_set_image((*image)->image); + imlib_context_set_image(image->image); imlib_save_image_with_error_return(path, &err); if(err != IMLIB_LOAD_ERROR_NONE) @@ -368,22 +368,22 @@ luaA_image_index(lua_State *L) if(luaA_usemetatable(L, 1, 2)) return 1; - image_t **image = luaA_checkudata(L, 1, "image"); + image_t *image = luaL_checkudata(L, 1, "image"); size_t len; const char *attr = luaL_checklstring(L, 2, &len); switch(a_tokenize(attr, len)) { case A_TK_WIDTH: - imlib_context_set_image((*image)->image); + imlib_context_set_image(image->image); lua_pushnumber(L, imlib_image_get_width()); break; case A_TK_HEIGHT: - imlib_context_set_image((*image)->image); + imlib_context_set_image(image->image); lua_pushnumber(L, imlib_image_get_height()); break; case A_TK_ALPHA: - imlib_context_set_image((*image)->image); + imlib_context_set_image(image->image); lua_pushboolean(L, imlib_image_has_alpha()); break; default: @@ -408,7 +408,6 @@ const struct luaL_reg awesome_image_meta[] = { "crop_and_scale", luaA_image_crop_and_scale }, { "save", luaA_image_save }, { "__gc", luaA_image_gc }, - { "__eq", luaA_image_eq }, { "__tostring", luaA_image_tostring }, { NULL, NULL } }; diff --git a/image.h b/image.h index 4b38222d..b3669ec9 100644 --- a/image.h +++ b/image.h @@ -22,16 +22,15 @@ #ifndef AWESOME_IMAGE_H #define AWESOME_IMAGE_H -#include #include #include "common/util.h" -#include "common/refcount.h" +#include "common/luaobject.h" typedef struct { - /** Reference counter */ - int refcount; + /** Lua references */ + luaA_ref_array_t refs; /** Imlib2 image */ Imlib_Image image; /** Image width */ @@ -42,23 +41,9 @@ typedef struct uint8_t *data; } image_t; -static inline void -image_delete(image_t **i) -{ - if(*i) - { - imlib_context_set_image((*i)->image); - imlib_free_image(); - p_delete(&(*i)->data); - p_delete(i); - } -} +LUA_OBJECT_FUNCS(image_t, image, "image") -DO_RCNT(image_t, image, image_delete) - -image_t * image_new_from_argb32(int, int, uint32_t *); - -int luaA_image_userdata_new(lua_State *, image_t *); +int image_new_from_argb32(int, int, uint32_t *); #endif // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80 diff --git a/property.c b/property.c index e677dd6c..cc96d4a6 100644 --- a/property.c +++ b/property.c @@ -336,11 +336,9 @@ property_handle_net_wm_icon(void *data, if(c) { - image_t *icon; - image_unref(&c->icon); - icon = ewmh_window_icon_from_reply(reply); - c->icon = icon ? image_ref(&icon) : NULL; - + image_unref(globalconf.L, c->icon); + if(ewmh_window_icon_from_reply(reply)) + c->icon = image_ref(globalconf.L); /* execute hook */ hooks_property(c, "icon"); } diff --git a/wibox.c b/wibox.c index 59e51a98..f4f2a990 100644 --- a/wibox.c +++ b/wibox.c @@ -875,10 +875,7 @@ luaA_wibox_index(lua_State *L) luaA_pushcolor(L, &(*wibox)->sw.ctx.bg); break; case A_TK_BG_IMAGE: - if((*wibox)->bg_image) - luaA_image_userdata_new(L, (*wibox)->bg_image); - else - return 0; + image_push(L, (*wibox)->bg_image); break; case A_TK_POSITION: lua_pushstring(L, position_tostr((*wibox)->position)); @@ -996,21 +993,9 @@ luaA_wibox_newindex(lua_State *L) (*wibox)->need_update = true; break; case A_TK_BG_IMAGE: - { - if(lua_isnil(L, 3)) - { - image_unref(&(*wibox)->bg_image); - (*wibox)->bg_image = NULL; - (*wibox)->need_update = true; - } - else - { - image_t **img = luaA_checkudata(L, 3, "image"); - image_unref(&(*wibox)->bg_image); - (*wibox)->bg_image = image_ref(img); - (*wibox)->need_update = true; - } - } + image_unref(L, (*wibox)->bg_image); + (*wibox)->bg_image = image_ref(L); + (*wibox)->need_update = true; break; case A_TK_ALIGN: buf = luaL_checklstring(L, 3, &len); diff --git a/widgets/imagebox.c b/widgets/imagebox.c index 20076465..c88c9e29 100644 --- a/widgets/imagebox.c +++ b/widgets/imagebox.c @@ -115,7 +115,7 @@ static void imagebox_destructor(widget_t *w) { imagebox_data_t *d = w->data; - image_unref(&d->image); + image_unref(globalconf.L, d->image); p_delete(&d); } @@ -138,10 +138,8 @@ luaA_imagebox_index(lua_State *L, awesome_token_t token) switch(token) { case A_TK_IMAGE: - if(d->image) - return luaA_image_userdata_new(L, d->image); - else - return 0; + image_push(L, d->image); + break; case A_TK_BG: luaA_pushcolor(L, &d->bg); break; @@ -167,7 +165,6 @@ static int luaA_imagebox_newindex(lua_State *L, awesome_token_t token) { widget_t **widget = luaA_checkudata(L, 1, "widget"); - image_t **image = NULL; imagebox_data_t *d = (*widget)->data; switch(token) @@ -176,16 +173,8 @@ luaA_imagebox_newindex(lua_State *L, awesome_token_t token) size_t len; case A_TK_IMAGE: - if(lua_isnil(L, 3) - || (image = luaA_checkudata(L, 3, "image"))) - { - /* unref image */ - image_unref(&d->image); - if(image) - d->image = image_ref(image); - else - d->image = NULL; - } + image_unref(L, d->image); + d->image = image_ref(L); break; case A_TK_BG: if(lua_isnil(L, 3)) diff --git a/widgets/textbox.c b/widgets/textbox.c index 6bbee05c..ca10e61e 100644 --- a/widgets/textbox.c +++ b/widgets/textbox.c @@ -184,10 +184,7 @@ luaA_textbox_index(lua_State *L, awesome_token_t token) lua_pushstring(L, draw_align_tostr(d->bg_align)); return 1; case A_TK_BG_IMAGE: - if(d->bg_image) - return luaA_image_userdata_new(L, d->bg_image); - else - return 0; + return image_push(L, d->bg_image); case A_TK_BG: return luaA_pushcolor(L, &d->bg); case A_TK_MARGIN: @@ -257,7 +254,6 @@ luaA_textbox_newindex(lua_State *L, awesome_token_t token) widget_t **widget = luaA_checkudata(L, 1, "widget"); const char *buf = NULL; textbox_data_t *d = (*widget)->data; - image_t **image = NULL; switch(token) { @@ -269,16 +265,8 @@ luaA_textbox_newindex(lua_State *L, awesome_token_t token) d->bg_resize = luaA_checkboolean(L, 3); break; case A_TK_BG_IMAGE: - if(lua_isnil(L, 3) - || (image = luaA_checkudata(L, 3, "image"))) - { - /* unref image */ - image_unref(&d->bg_image); - if(image) - d->bg_image = image_ref(image); - else - d->bg_image = NULL; - } + image_unref(L, d->bg_image); + d->bg_image = image_ref(L); break; case A_TK_BG: if(lua_isnil(L, 3))