forked from awesomeWM/awesome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
selection.c
181 lines (148 loc) · 5.58 KB
/
selection.c
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
173
174
175
176
177
178
179
180
181
/*
* selection.c - Selection handling
*
* Copyright © 2009 Julien Danjou <[email protected]>
* Copyright © 2009 Gregor Best <[email protected]>
*
* 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.
*
*/
/** awesome selection (clipboard) API
* @author Julien Danjou <[email protected]>
* @copyright 2008-2009 Julien Danjou
* @module selection
*/
#include "selection.h"
#include "globalconf.h"
#include "common/atoms.h"
#include "event.h"
#include "xwindow.h"
#include <xcb/xcb_atom.h>
#include <xcb/xcb_event.h>
/** Get the selection (clipboard) content.
*
* @return A string with the selection (clipboard) content.
* @staticfct selection
*/
static xcb_window_t selection_window = XCB_NONE;
/** Get the current X selection buffer.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
* \luastack
* \lreturn A string with the current X selection buffer.
*/
static int
luaA_selection_get(lua_State *L)
{
luaA_deprecate(L, "selection.getter(\"PRIMARY\", \"UTF8_STRING\")");
if(selection_window == XCB_NONE)
{
xcb_screen_t *screen = globalconf.screen;
uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
uint32_t values[] = { screen->black_pixel, 1, XCB_EVENT_MASK_PROPERTY_CHANGE };
selection_window = xcb_generate_id(globalconf.connection);
xcb_create_window(globalconf.connection, screen->root_depth, selection_window, screen->root,
0, 0, 1, 1, 0, XCB_COPY_FROM_PARENT, screen->root_visual,
mask, values);
xwindow_set_class_instance(selection_window);
xwindow_set_name_static(selection_window, "Awesome selection window");
}
xcb_convert_selection(globalconf.connection, selection_window,
XCB_ATOM_PRIMARY, UTF8_STRING, XSEL_DATA, globalconf.timestamp);
xcb_flush(globalconf.connection);
xcb_generic_event_t *event;
while(true)
{
event = xcb_wait_for_event(globalconf.connection);
if(!event)
return 0;
if(XCB_EVENT_RESPONSE_TYPE(event) != XCB_SELECTION_NOTIFY)
{
/* \todo Eventually, this may be rewritten with adding a static
* buffer, then a event handler for XCB_SELECTION_NOTIFY, then call
* xcb_event_poll_for_event_loop() and awesome_refresh(),
* then check if some static buffer has been filled with data.
* If yes, that'd be the xsel data, otherwise, re-loop.
* Anyway that's still brakes the socket or D-Bus, so maybe using
* ev_loop() would be even better.
*/
event_handle(event);
p_delete(&event);
awesome_refresh();
continue;
}
xcb_selection_notify_event_t *event_notify =
(xcb_selection_notify_event_t *) event;
if(event_notify->selection == XCB_ATOM_PRIMARY
&& event_notify->property != XCB_NONE)
{
xcb_icccm_get_text_property_reply_t prop;
xcb_get_property_cookie_t cookie =
xcb_icccm_get_text_property(globalconf.connection,
event_notify->requestor,
event_notify->property);
if(xcb_icccm_get_text_property_reply(globalconf.connection,
cookie, &prop, NULL))
{
lua_pushlstring(L, prop.name, prop.name_len);
xcb_icccm_get_text_property_reply_wipe(&prop);
xcb_delete_property(globalconf.connection,
event_notify->requestor,
event_notify->property);
p_delete(&event);
return 1;
}
else
break;
}
}
p_delete(&event);
return 0;
}
static void
move_global_to_table(lua_State *L, int index, const char *global_name, const char *local_name)
{
index = luaA_absindex(L, index);
/* Get the global */
lua_getglobal(L, global_name);
assert(!lua_isnil(L, -1));
/* Save it locally */
lua_setfield(L, index, local_name);
/* Set the global to nil */
lua_pushnil(L);
lua_setglobal(L, global_name);
}
void
selection_setup(lua_State *L)
{
/* This table will be the "selection" global */
lua_newtable(L);
/* Setup a metatable */
lua_newtable(L);
/* metatable.__index = metatable */
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
/* Set some more fields */
lua_pushcfunction(L, luaA_selection_get);
lua_setfield(L, -2, "__call");
move_global_to_table(L, -2, "selection_acquire", "acquire");
move_global_to_table(L, -2, "selection_getter", "getter");
move_global_to_table(L, -2, "selection_watcher", "watcher");
/* Set the metatable */
lua_setmetatable(L, -2);
/* Set the "selection" global */
lua_setglobal(L, "selection");
}
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80