2019-02-07 15:16:48 +01:00
|
|
|
/*
|
|
|
|
* selection_getter.c - selection content getter
|
|
|
|
*
|
|
|
|
* Copyright © 2019 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 "objects/selection_getter.h"
|
2019-02-07 15:47:06 +01:00
|
|
|
#include "common/luaobject.h"
|
2019-02-07 16:20:02 +01:00
|
|
|
#include "common/atoms.h"
|
2019-02-07 15:47:06 +01:00
|
|
|
#include "globalconf.h"
|
|
|
|
|
|
|
|
#define REGISTRY_GETTER_TABLE_INDEX "awesome_selection_getters"
|
|
|
|
|
|
|
|
typedef struct selection_getter_t
|
|
|
|
{
|
|
|
|
LUA_OBJECT_HEADER
|
|
|
|
/** Reference in the special table to this object */
|
|
|
|
int ref;
|
|
|
|
/** Window used for the transfer */
|
|
|
|
xcb_window_t window;
|
|
|
|
} selection_getter_t;
|
|
|
|
|
|
|
|
static lua_class_t selection_getter_class;
|
|
|
|
LUA_OBJECT_FUNCS(selection_getter_class, selection_getter_t, selection_getter)
|
|
|
|
|
|
|
|
static void
|
|
|
|
selection_getter_wipe(selection_getter_t *selection)
|
|
|
|
{
|
|
|
|
xcb_destroy_window(globalconf.connection, selection->window);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
luaA_selection_getter_new(lua_State *L)
|
|
|
|
{
|
|
|
|
size_t name_length, target_length;
|
|
|
|
const char *name, *target;
|
|
|
|
xcb_intern_atom_cookie_t cookies[2];
|
|
|
|
xcb_intern_atom_reply_t *reply;
|
|
|
|
selection_getter_t *selection;
|
|
|
|
xcb_atom_t name_atom, target_atom;
|
|
|
|
|
|
|
|
name = luaL_checklstring(L, 2, &name_length);
|
|
|
|
target = luaL_checklstring(L, 3, &target_length);
|
|
|
|
|
|
|
|
/* Create a selection object */
|
|
|
|
selection = (void *) selection_getter_class.allocator(L);
|
|
|
|
selection->window = xcb_generate_id(globalconf.connection);
|
|
|
|
xcb_create_window(globalconf.connection, globalconf.screen->root_depth,
|
|
|
|
selection->window, globalconf.screen->root, -1, -1, 1, 1, 0,
|
|
|
|
XCB_COPY_FROM_PARENT, globalconf.screen->root_visual, 0, NULL);
|
|
|
|
|
|
|
|
/* Save it in the registry */
|
|
|
|
lua_pushliteral(L, REGISTRY_GETTER_TABLE_INDEX);
|
|
|
|
lua_rawget(L, LUA_REGISTRYINDEX);
|
|
|
|
lua_pushvalue(L, -2);
|
|
|
|
selection->ref = luaL_ref(L, -2);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
|
|
|
/* Get the atoms identifying the request */
|
|
|
|
cookies[0] = xcb_intern_atom_unchecked(globalconf.connection, false, name_length, name);
|
|
|
|
cookies[1] = xcb_intern_atom_unchecked(globalconf.connection, false, target_length, target);
|
|
|
|
|
|
|
|
reply = xcb_intern_atom_reply(globalconf.connection, cookies[0], NULL);
|
|
|
|
name_atom = reply ? reply->atom : XCB_NONE;
|
|
|
|
p_delete(&reply);
|
|
|
|
|
|
|
|
reply = xcb_intern_atom_reply(globalconf.connection, cookies[1], NULL);
|
|
|
|
target_atom = reply ? reply->atom : XCB_NONE;
|
|
|
|
p_delete(&reply);
|
|
|
|
|
2019-02-07 16:20:02 +01:00
|
|
|
xcb_convert_selection(globalconf.connection, selection->window, name_atom,
|
|
|
|
target_atom, AWESOME_SELECTION_ATOM, globalconf.timestamp);
|
2019-02-07 15:47:06 +01:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2019-02-07 15:16:48 +01:00
|
|
|
|
2019-02-07 16:20:02 +01:00
|
|
|
static void
|
|
|
|
selection_handle_selectionnotify(lua_State *L, int ud, xcb_atom_t property)
|
|
|
|
{
|
|
|
|
selection_getter_t *selection;
|
|
|
|
|
|
|
|
ud = luaA_absindex(L, ud);
|
|
|
|
selection = lua_touserdata(L, ud);
|
|
|
|
|
|
|
|
if (property != XCB_NONE)
|
|
|
|
{
|
|
|
|
xcb_get_property_reply_t *property_r = xcb_get_property_reply(globalconf.connection,
|
|
|
|
xcb_get_property(globalconf.connection, true, selection->window, AWESOME_SELECTION_ATOM,
|
|
|
|
XCB_GET_PROPERTY_TYPE_ANY, 0, 0xffffffff), NULL);
|
|
|
|
|
|
|
|
if (property_r)
|
|
|
|
{
|
|
|
|
lua_pushlstring(L, xcb_get_property_value(property_r), xcb_get_property_value_length(property_r));
|
|
|
|
luaA_object_emit_signal(L, ud, "data", 1);
|
|
|
|
p_delete(&property_r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
luaA_object_emit_signal(L, ud, "data_end", 0);
|
|
|
|
|
|
|
|
/* Now unreference the selection object; it's dead */
|
|
|
|
lua_pushliteral(L, REGISTRY_GETTER_TABLE_INDEX);
|
|
|
|
lua_rawget(L, LUA_REGISTRYINDEX);
|
|
|
|
luaL_unref(L, -1, selection->ref);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
|
|
|
selection->ref = LUA_NOREF;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
event_handle_selectionnotify(xcb_selection_notify_event_t *ev)
|
|
|
|
{
|
|
|
|
lua_State *L = globalconf_get_lua_State();
|
|
|
|
|
|
|
|
/* Iterate over all active selection getters */
|
|
|
|
lua_pushliteral(L, REGISTRY_GETTER_TABLE_INDEX);
|
|
|
|
lua_rawget(L, LUA_REGISTRYINDEX);
|
|
|
|
lua_pushnil(L);
|
|
|
|
while (lua_next(L, -2) != 0) {
|
|
|
|
if (lua_type(L, -1) == LUA_TUSERDATA) {
|
|
|
|
selection_getter_t *selection = lua_touserdata(L, -1);
|
|
|
|
if (ev->requestor == selection->window) {
|
|
|
|
/* Found the right selection */
|
|
|
|
selection_handle_selectionnotify(L, -1, ev->property);
|
|
|
|
lua_pop(L, 2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Remove the value, leaving only the key */
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
/* Remove the getter table */
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
|
2019-02-07 15:16:48 +01:00
|
|
|
void
|
|
|
|
selection_getter_class_setup(lua_State *L)
|
|
|
|
{
|
2019-02-07 15:47:06 +01:00
|
|
|
static const struct luaL_Reg selection_getter_methods[] =
|
|
|
|
{
|
|
|
|
{ "__call", luaA_selection_getter_new },
|
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct luaL_Reg selection_getter_metha[] = {
|
|
|
|
LUA_OBJECT_META(selection_getter)
|
|
|
|
LUA_CLASS_META
|
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Store a table in the registry that tracks active getters. This code does
|
|
|
|
* debug.getregistry(){REGISTRY_GETTER_TABLE_INDEX] = {}
|
|
|
|
*/
|
|
|
|
lua_pushliteral(L, REGISTRY_GETTER_TABLE_INDEX);
|
|
|
|
lua_newtable(L);
|
|
|
|
lua_rawset(L, LUA_REGISTRYINDEX);
|
|
|
|
|
|
|
|
luaA_class_setup(L, &selection_getter_class, "selection_getter", NULL,
|
|
|
|
(lua_class_allocator_t) selection_getter_new,
|
|
|
|
(lua_class_collector_t) selection_getter_wipe, NULL,
|
|
|
|
luaA_class_index_miss_property, luaA_class_newindex_miss_property,
|
|
|
|
selection_getter_methods, selection_getter_metha);
|
2019-02-07 15:16:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|