forked from awesomeWM/awesome
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrawable.c
More file actions
172 lines (153 loc) · 5.59 KB
/
Copy pathdrawable.c
File metadata and controls
172 lines (153 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* 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, drawable_refresh_callback *callback, void *data)
{
drawable_t *d = drawable_new(L);
d->refresh_callback = callback;
d->refresh_data = data;
d->refreshed = false;
d->surface = NULL;
d->pixmap = XCB_NONE;
return d;
}
static void
drawable_unset_surface(drawable_t *d)
{
cairo_surface_finish(d->surface);
cairo_surface_destroy(d->surface);
if (d->pixmap)
xcb_free_pixmap(globalconf.connection, d->pixmap);
d->refreshed = false;
d->surface = NULL;
d->pixmap = XCB_NONE;
}
static void
drawable_wipe(drawable_t *d)
{
drawable_unset_surface(d);
}
void
drawable_set_geometry(drawable_t *d, int didx, area_t geom)
{
area_t old = d->geometry;
d->geometry = geom;
bool size_changed = (old.width != geom.width) || (old.height != geom.height);
if (size_changed)
drawable_unset_surface(d);
if (size_changed && geom.width > 0 && geom.height > 0)
{
d->pixmap = xcb_generate_id(globalconf.connection);
xcb_create_pixmap(globalconf.connection, globalconf.default_depth, d->pixmap,
globalconf.screen->root, geom.width, geom.height);
d->surface = cairo_xcb_surface_create(globalconf.connection,
d->pixmap, globalconf.visual,
geom.width, geom.height);
luaA_object_emit_signal(globalconf.L, didx, "property::surface", 0);
}
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->refreshed = true;
(*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");
signal_add(&drawable_class.signals, "property::surface");
}
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80