Introduce drawables

A drawable is something that you can draw to, just like a drawin. However, a
drawable isn't necessarily its own windows. This will later on be used to
implement titlebars where the titlebars are drawables.

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2012-10-14 17:14:22 +02:00
parent f8e6a3a909
commit f0512eeaab
10 changed files with 300 additions and 62 deletions

View File

@ -70,6 +70,7 @@ set(AWE_SRCS
${SOURCE_DIR}/common/xutil.c
${SOURCE_DIR}/objects/button.c
${SOURCE_DIR}/objects/client.c
${SOURCE_DIR}/objects/drawable.c
${SOURCE_DIR}/objects/drawin.c
${SOURCE_DIR}/objects/key.c
${SOURCE_DIR}/objects/tag.c

View File

@ -47,6 +47,7 @@
| XCB_EVENT_MASK_PROPERTY_CHANGE \
}
typedef struct drawable_t drawable_t;
typedef struct drawin_t drawin_t;
typedef struct a_screen screen_t;
typedef struct button_t button_t;

View File

@ -34,7 +34,7 @@ local function do_redraw(_wibox)
end
local geom = _wibox.drawin:geometry()
local cr = cairo.Context(surface(_wibox.drawin.surface))
local cr = cairo.Context(surface(_wibox.drawin.drawable.surface))
-- Draw the background
cr:save()
@ -59,7 +59,7 @@ local function do_redraw(_wibox)
_wibox:widget_at(_wibox.widget, 0, 0, geom.width, geom.height)
end
_wibox.drawin:refresh()
_wibox.drawin.drawable:refresh()
end
--- Register a widget's position.

4
luaa.c
View File

@ -39,6 +39,7 @@
#include "objects/tag.h"
#include "objects/client.h"
#include "objects/drawin.h"
#include "objects/drawable.h"
#include "screen.h"
#include "event.h"
#include "selection.h"
@ -581,6 +582,9 @@ luaA_init(xdgHandle* xdg)
/* Export window */
window_class_setup(L);
/* Export drawable */
drawable_class_setup(L);
/* Export drawin */
drawin_class_setup(L);

44
luadoc/drawable.lua Normal file
View File

@ -0,0 +1,44 @@
--- awesome drawable API
-- @author Uli Schlachter &lt;psychon@znc.in&gt;
-- @copyright 2012 Uli Schlachter
module("drawable")
--- Drawable object.
-- @field surface The drawable's cairo surface.
-- @name drawable
--- Get drawable geometry. The geometry consists of x, y, width and height.
-- @param no_params luadoc is buggy.
-- @return A table with drawable coordinates and geometry.
-- @name geometry
-- @class function
--- Refresh the drawable. When you are drawing to the surface, you have
-- call this function when you are done to make the result visible.
-- @param no_params luadoc is buggy.
-- @name refresh
-- @class function
--- Add a signal.
-- @param name A signal name.
-- @param func A function to call when the signal is emitted.
-- @name connect_signal
-- @class function
--- Remove a signal.
-- @param name A signal name.
-- @param func A function to remove.
-- @name disconnect_signal
-- @class function
--- Emit a signal.
-- @param name A signal name.
-- @param ... Various arguments, optional.
-- @name emit_signal
-- @class function
--- Get the number of instances.
-- @param no_params luadoc is buggy.
-- @return The number of drawable objects alive.
-- @name instances
-- @class function

View File

@ -15,7 +15,7 @@ module("drawin")
-- @field y The y coordinates.
-- @field width The width of the drawin.
-- @field height The height of the drawin.
-- @field surface A cairo surface as light user datum that can be used for drawing.
-- @field drawable The drawin's drawable.
-- @class table
-- @name drawin
@ -37,12 +37,6 @@ module("drawin")
-- @name geometry
-- @class function
--- Refresh the drawin. When you are drawing to the window's surface, you have
-- call this function when you are done to make the result visible.
-- @param no_params luadoc is buggy.
-- @name refresh
-- @class function
--- Add a signal.
-- @param name A signal name.
-- @param func A function to call when the signal is emitted.

162
objects/drawable.c Normal file
View File

@ -0,0 +1,162 @@
/*
* drawable.c - drawable functions
*
* Copyright © 2008-2009 Julien Danjou <julien@danjou.info>
* Copyright © 2010-2012 Uli Schlachter <psychon@znc.in>
*
* 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
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "drawable.h"
#include <cairo-xcb.h>
static lua_class_t drawable_class;
LUA_OBJECT_FUNCS(drawable_class, drawable_t, drawable)
drawable_t *
drawable_allocator(lua_State *L, cairo_surface_t *surface, drawable_refresh_callback *callback, void *data)
{
drawable_t *d = drawable_new(L);
d->refresh_callback = callback;
d->refresh_data = data;
drawable_set_surface(d, surface);
return d;
}
static void
drawable_wipe(drawable_t *d)
{
drawable_set_surface(d, NULL);
}
void
drawable_set_surface(drawable_t *d, cairo_surface_t *surface)
{
if (d->surface) {
cairo_surface_finish(d->surface);
cairo_surface_destroy(d->surface);
}
d->surface = cairo_surface_reference(surface);
if (d->surface != NULL)
{
/* Make sure the surface doesn't contain garbage by filling it with black */
cairo_t *cr = cairo_create(surface);
cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
cairo_paint(cr);
cairo_destroy(cr);
}
}
void
drawable_set_geometry(drawable_t *d, int didx, area_t geom)
{
area_t old = d->geometry;
d->geometry = geom;
if (old.x != geom.x)
luaA_object_emit_signal(globalconf.L, didx, "property::x", 0);
if (old.y != geom.y)
luaA_object_emit_signal(globalconf.L, didx, "property::y", 0);
if (old.width != geom.width)
luaA_object_emit_signal(globalconf.L, didx, "property::width", 0);
if (old.height != geom.height)
luaA_object_emit_signal(globalconf.L, didx, "property::height", 0);
}
/** Get a drawable's surface
* \param L The Lua VM state.
* \param drawable The drawable object.
* \return The number of elements pushed on stack.
*/
static int
luaA_drawable_get_surface(lua_State *L, drawable_t *drawable)
{
/* Lua gets its own reference which it will have to destroy */
lua_pushlightuserdata(L, cairo_surface_reference(drawable->surface));
return 1;
}
/** Refresh a drawable's content. This has to be called whenever some drawing to
* the drawable's surface has been done and should become visible.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
*/
static int
luaA_drawable_refresh(lua_State *L)
{
drawable_t *drawable = luaA_checkudata(L, 1, &drawable_class);
(*drawable->refresh_callback)(drawable->refresh_data);
return 0;
}
/** Return drawable geometry.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
* \luastack
* \lreturn A table with drawable coordinates.
*/
static int
luaA_drawable_geometry(lua_State *L)
{
drawable_t *d = luaA_checkudata(L, 1, &drawable_class);
return luaA_pusharea(L, d->geometry);
}
void
drawable_class_setup(lua_State *L)
{
static const struct luaL_Reg drawable_methods[] =
{
LUA_CLASS_METHODS(drawable)
{ NULL, NULL }
};
static const struct luaL_Reg drawable_meta[] =
{
LUA_OBJECT_META(drawable)
LUA_CLASS_META
{ "refresh", luaA_drawable_refresh },
{ "geometry", luaA_drawable_geometry },
{ NULL, NULL },
};
luaA_class_setup(L, &drawable_class, "drawable", NULL,
(lua_class_allocator_t) drawable_new,
(lua_class_collector_t) drawable_wipe, NULL,
luaA_class_index_miss_property, luaA_class_newindex_miss_property,
drawable_methods, drawable_meta);
luaA_class_add_property(&drawable_class, "surface",
NULL,
(lua_class_propfunc_t) luaA_drawable_get_surface,
NULL);
signal_add(&drawable_class.signals, "button::press");
signal_add(&drawable_class.signals, "button::release");
signal_add(&drawable_class.signals, "mouse::enter");
signal_add(&drawable_class.signals, "mouse::leave");
signal_add(&drawable_class.signals, "mouse::move");
signal_add(&drawable_class.signals, "property::height");
signal_add(&drawable_class.signals, "property::width");
signal_add(&drawable_class.signals, "property::x");
signal_add(&drawable_class.signals, "property::y");
}
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

52
objects/drawable.h Normal file
View File

@ -0,0 +1,52 @@
/*
* drawable.h - drawable functions header
*
* Copyright © 2007-2009 Julien Danjou <julien@danjou.info>
* Copyright © 2010-2012 Uli Schlachter <psychon@znc.in>
*
* 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
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#ifndef AWESOME_OBJECTS_DRAWABLE_H
#define AWESOME_OBJECTS_DRAWABLE_H
#include "common/luaclass.h"
#include "globalconf.h"
typedef void drawable_refresh_callback(void *);
/** drawable type */
struct drawable_t
{
LUA_OBJECT_HEADER
/** Surface for drawing. */
cairo_surface_t *surface;
/** The geometry of the drawable (in root window coordinates). */
area_t geometry;
/** Callback for refreshing. */
drawable_refresh_callback *refresh_callback;
/** Data for refresh callback. */
void *refresh_data;
};
drawable_t *drawable_allocator(lua_State *, cairo_surface_t *,
drawable_refresh_callback *, void *);
void drawable_set_surface(drawable_t *, cairo_surface_t *);
void drawable_set_geometry(drawable_t *, int, area_t);
void drawable_class_setup(lua_State *);
#endif
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

View File

@ -61,14 +61,7 @@ drawin_wipe(drawin_t *w)
/* The drawin must already be unmapped, else it
* couldn't be garbage collected -> no unmap needed */
p_delete(&w->cursor);
if(w->surface)
{
/* Make sure that cairo knows that this surface can't be unused anymore.
* This is needed since lua could still have a reference to it. */
cairo_surface_finish(w->surface);
cairo_surface_destroy(w->surface);
w->surface = NULL;
}
cairo_surface_finish(w->drawable->surface);
if(w->window)
{
/* Activate BMA */
@ -85,6 +78,8 @@ drawin_wipe(drawin_t *w)
xcb_free_pixmap(globalconf.connection, w->pixmap);
w->pixmap = XCB_NONE;
}
luaA_object_unref_item(globalconf.L, -1, w->drawable);
w->drawable = NULL;
}
void
@ -94,20 +89,14 @@ drawin_unref_simplified(drawin_t **item)
}
static void
drawin_update_drawing(drawin_t *w)
drawin_update_drawing(drawin_t *w, int widx)
{
/* If this drawin isn't visible, we don't need an up-to-date cairo surface
* for it. (drawin_map() will later make sure we are called again) */
if(!w->visible)
return;
/* Clean up old stuff */
if(w->surface)
{
/* In case lua still got a reference to the surface, it still won't be
* able to do anything with it because we finish it. */
cairo_surface_finish(w->surface);
cairo_surface_destroy(w->surface);
}
drawable_set_surface(w->drawable, NULL);
if(w->pixmap)
xcb_free_pixmap(globalconf.connection, w->pixmap);
@ -117,14 +106,14 @@ drawin_update_drawing(drawin_t *w)
xcb_create_pixmap(globalconf.connection, globalconf.default_depth, w->pixmap, s->root,
w->geometry.width, w->geometry.height);
/* and create a surface for that pixmap */
w->surface = cairo_xcb_surface_create(globalconf.connection,
cairo_surface_t *surface = cairo_xcb_surface_create(globalconf.connection,
w->pixmap, globalconf.visual,
w->geometry.width, w->geometry.height);
/* Make sure the pixmap doesn't contain garbage by filling it with black */
cairo_t *cr = cairo_create(w->surface);
cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
cairo_paint(cr);
cairo_destroy(cr);
drawable_set_surface(w->drawable, surface);
luaA_object_push_item(globalconf.L, widx, w->drawable);
drawable_set_geometry(w->drawable, -1, w->geometry);
lua_pop(globalconf.L, 1);
}
/** Initialize a drawin.
@ -208,7 +197,7 @@ drawin_moveresize(lua_State *L, int udx, area_t geometry)
}
if(mask_vals & (XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT))
drawin_update_drawing(w);
drawin_update_drawing(w, udx);
/* Activate BMA */
client_ignore_enterleave_events();
@ -241,21 +230,24 @@ drawin_refresh_pixmap_partial(drawin_t *drawin,
int16_t x, int16_t y,
uint16_t w, uint16_t h)
{
if (!drawin->visible)
return;
/* Make cairo do all pending drawing */
cairo_surface_flush(drawin->surface);
cairo_surface_flush(drawin->drawable->surface);
xcb_copy_area(globalconf.connection, drawin->pixmap,
drawin->window, globalconf.gc, x, y, x, y,
w, h);
}
static void
drawin_map(drawin_t *drawin)
drawin_map(drawin_t *drawin, int widx)
{
/* Activate BMA */
client_ignore_enterleave_events();
/* Map the drawin */
xcb_map_window(globalconf.connection, drawin->window);
drawin_update_drawing(drawin);
drawin_update_drawing(drawin, widx);
/* Deactivate BMA */
client_restore_enterleave_events();
/* Stack this drawin correctly */
@ -304,7 +296,7 @@ drawin_set_visible(lua_State *L, int udx, bool v)
if(drawin->visible)
{
drawin_map(drawin);
drawin_map(drawin, udx);
/* duplicate drawin */
lua_pushvalue(L, udx);
/* ref it */
@ -345,6 +337,9 @@ drawin_allocator(lua_State *L)
w->geometry.height = 1;
w->type = _NET_WM_WINDOW_TYPE_NORMAL;
drawable_allocator(L, NULL, (drawable_refresh_callback *) drawin_refresh_pixmap, w);
w->drawable = luaA_object_ref_item(L, -2, -1);
drawin_init(w);
return w;
@ -524,33 +519,18 @@ luaA_drawin_set_visible(lua_State *L, drawin_t *drawin)
return 0;
}
/** Get a drawin's surface
/** Get a drawin's drawable
* \param L The Lua VM state.
* \param drawin The drawin object.
* \return The number of elements pushed on stack.
*/
static int
luaA_drawin_get_surface(lua_State *L, drawin_t *drawin)
luaA_drawin_get_drawable(lua_State *L, drawin_t *drawin)
{
/* Lua gets its own reference which it will have to destroy */
lua_pushlightuserdata(L, cairo_surface_reference(drawin->surface));
luaA_object_push_item(L, -2, drawin->drawable);
return 1;
}
/** Refresh a drawin's content. This has to be called whenever some drawing to
* the drawin's surface has been done and should become visible.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
*/
static int
luaA_drawin_refresh(lua_State *L)
{
drawin_t *drawin = luaA_checkudata(L, 1, &drawin_class);
drawin_refresh_pixmap(drawin);
return 0;
}
void
drawin_class_setup(lua_State *L)
{
@ -566,7 +546,6 @@ drawin_class_setup(lua_State *L)
LUA_OBJECT_META(drawin)
LUA_CLASS_META
{ "geometry", luaA_drawin_geometry },
{ "refresh", luaA_drawin_refresh },
{ NULL, NULL },
};
@ -576,9 +555,9 @@ drawin_class_setup(lua_State *L)
NULL,
luaA_class_index_miss_property, luaA_class_newindex_miss_property,
drawin_methods, drawin_meta);
luaA_class_add_property(&drawin_class, "surface",
luaA_class_add_property(&drawin_class, "drawable",
NULL,
(lua_class_propfunc_t) luaA_drawin_get_surface,
(lua_class_propfunc_t) luaA_drawin_get_drawable,
NULL);
luaA_class_add_property(&drawin_class, "visible",
(lua_class_propfunc_t) luaA_drawin_set_visible,

View File

@ -25,6 +25,7 @@
#include "objects/window.h"
#include "common/luaobject.h"
#include "objects/drawable.h"
#include "draw.h"
/** Drawin type */
@ -37,10 +38,10 @@ struct drawin_t
bool visible;
/** Cursor */
char *cursor;
/** The pixmap copied to the window object. */
/** The pixmap for double buffering. */
xcb_pixmap_t pixmap;
/** Surface for drawing to the pixmap. */
cairo_surface_t *surface;
/** The drawable for this drawin. */
drawable_t *drawable;
/** The window geometry. */
area_t geometry;
};