Browse Source

Merge pull request #2643 from psychon/selection_ownership

Add API for setting clipboard contents
mergify[bot] 8 months ago
parent
commit
2d2dba0d80
No account linked to committer's email address

+ 1
- 0
.luacheckrc View File

@@ -27,6 +27,7 @@ read_globals = {
27 27
     "keygrabber",
28 28
     "mousegrabber",
29 29
     "root",
30
+    "selection_acquire",
30 31
     "selection_getter",
31 32
     "selection",
32 33
     "selection_watcher",

+ 2
- 0
CMakeLists.txt View File

@@ -88,6 +88,8 @@ set(AWE_SRCS
88 88
     ${BUILD_DIR}/objects/drawin.c
89 89
     ${BUILD_DIR}/objects/key.c
90 90
     ${BUILD_DIR}/objects/screen.c
91
+    ${BUILD_DIR}/objects/selection_acquire.c
92
+    ${BUILD_DIR}/objects/selection_transfer.c
91 93
     ${BUILD_DIR}/objects/selection_watcher.c
92 94
     ${BUILD_DIR}/objects/tag.c
93 95
     ${BUILD_DIR}/objects/selection_getter.c

+ 4
- 1
event.c View File

@@ -25,6 +25,7 @@
25 25
 #include "objects/tag.h"
26 26
 #include "objects/selection_getter.h"
27 27
 #include "objects/drawin.h"
28
+#include "objects/selection_acquire.h"
28 29
 #include "objects/selection_watcher.h"
29 30
 #include "xwindow.h"
30 31
 #include "ewmh.h"
@@ -1013,7 +1014,8 @@ event_handle_selectionclear(xcb_selection_clear_event_t *ev)
1013 1014
     {
1014 1015
         warn("Lost WM_Sn selection, exiting...");
1015 1016
         g_main_loop_quit(globalconf.loop);
1016
-    }
1017
+    } else
1018
+        selection_handle_selectionclear(ev);
1017 1019
 }
1018 1020
 
1019 1021
 /** \brief awesome xerror function.
@@ -1109,6 +1111,7 @@ void event_handle(xcb_generic_event_t *event)
1109 1111
         EVENT(XCB_UNMAP_NOTIFY, event_handle_unmapnotify);
1110 1112
         EVENT(XCB_SELECTION_CLEAR, event_handle_selectionclear);
1111 1113
         EVENT(XCB_SELECTION_NOTIFY, event_handle_selectionnotify);
1114
+        EVENT(XCB_SELECTION_REQUEST, selection_handle_selectionrequest);
1112 1115
 #undef EVENT
1113 1116
     }
1114 1117
 

+ 8
- 0
luaa.c View File

@@ -50,6 +50,8 @@
50 50
 #include "objects/drawin.h"
51 51
 #include "objects/selection_getter.h"
52 52
 #include "objects/screen.h"
53
+#include "objects/selection_acquire.h"
54
+#include "objects/selection_transfer.h"
53 55
 #include "objects/selection_watcher.h"
54 56
 #include "objects/tag.h"
55 57
 #include "property.h"
@@ -1037,6 +1039,12 @@ luaA_init(xdgHandle* xdg, string_array_t *searchpath)
1037 1039
     /* Export keys */
1038 1040
     key_class_setup(L);
1039 1041
 
1042
+    /* Export selection acquire */
1043
+    selection_acquire_class_setup(L);
1044
+
1045
+    /* Export selection transfer */
1046
+    selection_transfer_class_setup(L);
1047
+
1040 1048
     /* Export selection watcher */
1041 1049
     selection_watcher_class_setup(L);
1042 1050
 

+ 243
- 0
objects/selection_acquire.c View File

@@ -0,0 +1,243 @@
1
+/*
2
+ * selection_acquire.c - objects for selection ownership
3
+ *
4
+ * Copyright © 2019 Uli Schlachter <psychon@znc.in>
5
+ *
6
+ * This program is free software; you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation; either version 2 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * This program is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+ * GNU General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU General Public License along
17
+ * with this program; if not, write to the Free Software Foundation, Inc.,
18
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
+ *
20
+ */
21
+
22
+#include "objects/selection_acquire.h"
23
+#include "objects/selection_transfer.h"
24
+#include "common/luaobject.h"
25
+#include "globalconf.h"
26
+
27
+#define REGISTRY_ACQUIRE_TABLE_INDEX "awesome_selection_acquires"
28
+
29
+typedef struct selection_acquire_t
30
+{
31
+    LUA_OBJECT_HEADER
32
+    /** The selection that is being owned. */
33
+    xcb_atom_t selection;
34
+    /** Window used for owning the selection. */
35
+    xcb_window_t window;
36
+    /** Timestamp used for acquiring the selection. */
37
+    xcb_timestamp_t timestamp;
38
+} selection_acquire_t;
39
+
40
+static lua_class_t selection_acquire_class;
41
+LUA_OBJECT_FUNCS(selection_acquire_class, selection_acquire_t, selection_acquire)
42
+
43
+static void
44
+luaA_pushatom(lua_State *L, xcb_atom_t atom)
45
+{
46
+    lua_pushnumber(L, atom);
47
+}
48
+
49
+static int
50
+selection_acquire_find_by_window(lua_State *L, xcb_window_t window)
51
+{
52
+    /* Iterate over all active selection acquire objects */
53
+    lua_pushliteral(L, REGISTRY_ACQUIRE_TABLE_INDEX);
54
+    lua_rawget(L, LUA_REGISTRYINDEX);
55
+    lua_pushnil(L);
56
+    while (lua_next(L, -2) != 0) {
57
+        if (lua_type(L, -1) == LUA_TUSERDATA) {
58
+            selection_acquire_t *selection = lua_touserdata(L, -1);
59
+            if (selection->window == window)
60
+            {
61
+                /* Remove table and key */
62
+                lua_remove(L, -2);
63
+                lua_remove(L, -2);
64
+                return 1;
65
+            }
66
+        }
67
+        /* Remove the value, leaving only the key */
68
+        lua_pop(L, 1);
69
+    }
70
+    /* Remove the table */
71
+    lua_pop(L, 1);
72
+
73
+    return 0;
74
+}
75
+
76
+static void
77
+selection_release(lua_State *L, int ud)
78
+{
79
+    selection_acquire_t *selection = luaA_checkudata(L, ud, &selection_acquire_class);
80
+
81
+    luaA_object_emit_signal(L, ud, "release", 0);
82
+
83
+    /* Destroy the window, this also releases the selection in X11 */
84
+    xcb_destroy_window(globalconf.connection, selection->window);
85
+    selection->window = XCB_NONE;
86
+
87
+    /* Unreference the object, it's now dead */
88
+    lua_pushliteral(L, REGISTRY_ACQUIRE_TABLE_INDEX);
89
+    lua_rawget(L, LUA_REGISTRYINDEX);
90
+    luaA_pushatom(L, selection->selection);
91
+    lua_pushnil(L);
92
+    lua_rawset(L, -3);
93
+
94
+    selection->selection = XCB_NONE;
95
+
96
+    lua_pop(L, 1);
97
+}
98
+
99
+void
100
+selection_handle_selectionclear(xcb_selection_clear_event_t *ev)
101
+{
102
+    lua_State *L = globalconf_get_lua_State();
103
+
104
+    if (selection_acquire_find_by_window(L, ev->owner) == 0)
105
+        return;
106
+
107
+    selection_release(L, -1);
108
+    lua_pop(L, 1);
109
+}
110
+
111
+void
112
+selection_handle_selectionrequest(xcb_selection_request_event_t *ev)
113
+{
114
+    lua_State *L = globalconf_get_lua_State();
115
+
116
+    if (ev->property == XCB_NONE)
117
+        /* Obsolete client */
118
+        ev->property = ev->target;
119
+
120
+    if (selection_acquire_find_by_window(L, ev->owner) == 0)
121
+    {
122
+        selection_transfer_reject(ev->requestor, ev->selection, ev->target, ev->time);
123
+        return;
124
+    }
125
+
126
+    selection_transfer_begin(L, -1, ev->requestor, ev->selection, ev->target,
127
+            ev->property, ev->time);
128
+
129
+    lua_pop(L, 1);
130
+}
131
+
132
+static int
133
+luaA_selection_acquire_new(lua_State *L)
134
+{
135
+    size_t name_length;
136
+    const char *name;
137
+    xcb_intern_atom_reply_t *reply;
138
+    xcb_get_selection_owner_reply_t *selection_reply;
139
+    xcb_atom_t name_atom;
140
+    selection_acquire_t *selection;
141
+
142
+    luaA_checktable(L, 2);
143
+    lua_pushliteral(L, "selection");
144
+    lua_gettable(L, 2);
145
+    name = luaL_checklstring(L, -1, &name_length);
146
+
147
+    /* Get the atom identifying the selection */
148
+    reply = xcb_intern_atom_reply(globalconf.connection,
149
+            xcb_intern_atom_unchecked(globalconf.connection, false, name_length, name),
150
+            NULL);
151
+    name_atom = reply ? reply->atom : XCB_NONE;
152
+    p_delete(&reply);
153
+
154
+    /* Create a selection object */
155
+    selection = (void *) selection_acquire_class.allocator(L);
156
+    selection->selection = name_atom;
157
+    selection->timestamp = globalconf.timestamp;
158
+    selection->window = xcb_generate_id(globalconf.connection);
159
+    xcb_create_window(globalconf.connection, globalconf.screen->root_depth,
160
+            selection->window, globalconf.screen->root, -1, -1, 1, 1, 0,
161
+            XCB_COPY_FROM_PARENT, globalconf.screen->root_visual, 0, NULL);
162
+
163
+    /* Try to acquire the selection */
164
+    xcb_set_selection_owner(globalconf.connection, selection->window, name_atom, selection->timestamp);
165
+    selection_reply = xcb_get_selection_owner_reply(globalconf.connection,
166
+            xcb_get_selection_owner(globalconf.connection, name_atom),
167
+            NULL);
168
+    if (selection_reply == NULL || selection_reply->owner != selection->window) {
169
+        /* Acquiring the selection failed, return nothing */
170
+        p_delete(&selection_reply);
171
+
172
+        xcb_destroy_window(globalconf.connection, selection->window);
173
+        selection->window = XCB_NONE;
174
+        return 0;
175
+    }
176
+
177
+    /* Everything worked, register the object in the table */
178
+    lua_pushliteral(L, REGISTRY_ACQUIRE_TABLE_INDEX);
179
+    lua_rawget(L, LUA_REGISTRYINDEX);
180
+
181
+    luaA_pushatom(L, name_atom);
182
+    lua_rawget(L, -2);
183
+    if (!lua_isnil(L, -1)) {
184
+        /* There is already another selection_acquire object for this selection,
185
+         * release it now. X11 does not send us SelectionClear events for our
186
+         * own changes to the selection.
187
+         */
188
+        selection_release(L, -1);
189
+    }
190
+
191
+    luaA_pushatom(L, name_atom);
192
+    lua_pushvalue(L, -4);
193
+    lua_rawset(L, -4);
194
+    lua_pop(L, 2);
195
+
196
+    return 1;
197
+}
198
+
199
+static int
200
+luaA_selection_acquire_release(lua_State *L)
201
+{
202
+    luaA_checkudata(L, 1, &selection_acquire_class);
203
+    selection_release(L, 1);
204
+
205
+    return 0;
206
+}
207
+
208
+static bool
209
+selection_acquire_checker(selection_acquire_t *selection)
210
+{
211
+    return selection->selection != XCB_NONE;
212
+}
213
+
214
+void
215
+selection_acquire_class_setup(lua_State *L)
216
+{
217
+    static const struct luaL_Reg selection_acquire_methods[] =
218
+    {
219
+        { "__call", luaA_selection_acquire_new },
220
+        { NULL, NULL }
221
+    };
222
+
223
+    static const struct luaL_Reg selection_acquire_meta[] =
224
+    {
225
+        LUA_OBJECT_META(selection_acquire)
226
+        LUA_CLASS_META
227
+        { "release", luaA_selection_acquire_release },
228
+        { NULL, NULL }
229
+    };
230
+
231
+    /* Store a table in the registry that tracks active selection_acquire_t. */
232
+    lua_pushliteral(L, REGISTRY_ACQUIRE_TABLE_INDEX);
233
+    lua_newtable(L);
234
+    lua_rawset(L, LUA_REGISTRYINDEX);
235
+
236
+    luaA_class_setup(L, &selection_acquire_class, "selection_acquire", NULL,
237
+            (lua_class_allocator_t) selection_acquire_new, NULL,
238
+            (lua_class_checker_t) selection_acquire_checker,
239
+            luaA_class_index_miss_property, luaA_class_newindex_miss_property,
240
+            selection_acquire_methods, selection_acquire_meta);
241
+}
242
+
243
+// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

+ 34
- 0
objects/selection_acquire.h View File

@@ -0,0 +1,34 @@
1
+/*
2
+ * selection_acquire.c - objects for selection ownership header
3
+ *
4
+ * Copyright © 2019 Uli Schlachter <psychon@znc.in>
5
+ *
6
+ * This program is free software; you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation; either version 2 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * This program is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+ * GNU General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU General Public License along
17
+ * with this program; if not, write to the Free Software Foundation, Inc.,
18
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
+ *
20
+ */
21
+
22
+#ifndef AWESOME_OBJECTS_SELECTION_ACQUIRE_H
23
+#define AWESOME_OBJECTS_SELECTION_ACQUIRE_H
24
+
25
+#include <lua.h>
26
+#include <xcb/xcb.h>
27
+
28
+void selection_acquire_class_setup(lua_State*);
29
+void selection_handle_selectionclear(xcb_selection_clear_event_t*);
30
+void selection_handle_selectionrequest(xcb_selection_request_event_t*);
31
+
32
+#endif
33
+
34
+// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

+ 386
- 0
objects/selection_transfer.c View File

@@ -0,0 +1,386 @@
1
+/*
2
+ * selection_transfer.c - objects for selection transfer header
3
+ *
4
+ * Copyright © 2019 Uli Schlachter <psychon@znc.in>
5
+ *
6
+ * This program is free software; you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation; either version 2 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * This program is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+ * GNU General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU General Public License along
17
+ * with this program; if not, write to the Free Software Foundation, Inc.,
18
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
+ *
20
+ */
21
+
22
+#include "objects/selection_transfer.h"
23
+#include "common/luaobject.h"
24
+#include "common/atoms.h"
25
+#include "globalconf.h"
26
+
27
+#define REGISTRY_TRANSFER_TABLE_INDEX "awesome_selection_transfers"
28
+#define TRANSFER_DATA_INDEX "data_for_next_chunk"
29
+
30
+enum transfer_state {
31
+    TRANSFER_WAIT_FOR_DATA,
32
+    TRANSFER_INCREMENTAL_SENDING,
33
+    TRANSFER_INCREMENTAL_DONE,
34
+    TRANSFER_DONE
35
+};
36
+
37
+typedef struct selection_transfer_t
38
+{
39
+    LUA_OBJECT_HEADER
40
+    /** Reference in the special table to this object */
41
+    int ref;
42
+    /* Information from the xcb_selection_request_event_t */
43
+    xcb_window_t requestor;
44
+    xcb_atom_t selection;
45
+    xcb_atom_t target;
46
+    xcb_atom_t property;
47
+    xcb_timestamp_t time;
48
+    /* Current state of the transfer */
49
+    enum transfer_state state;
50
+    /* Offset into TRANSFER_DATA_INDEX for the next chunk of data */
51
+    size_t offset;
52
+    /* Can there be more data coming from Lua? */
53
+    bool more_data;
54
+} selection_transfer_t;
55
+
56
+static lua_class_t selection_transfer_class;
57
+LUA_OBJECT_FUNCS(selection_transfer_class, selection_transfer_t, selection_transfer)
58
+
59
+static size_t max_property_length(void)
60
+{
61
+    uint32_t max_request_length = xcb_get_maximum_request_length(globalconf.connection);
62
+    max_request_length = MIN(max_request_length, (1<<16) - 1);
63
+    return max_request_length * 4 - sizeof(xcb_change_property_request_t);
64
+}
65
+
66
+static void
67
+selection_transfer_notify(xcb_window_t requestor, xcb_atom_t selection,
68
+        xcb_atom_t target, xcb_atom_t property, xcb_timestamp_t time)
69
+{
70
+    xcb_selection_notify_event_t ev;
71
+
72
+    p_clear(&ev, 1);
73
+    ev.response_type = XCB_SELECTION_NOTIFY;
74
+    ev.requestor = requestor;
75
+    ev.selection = selection;
76
+    ev.target = target;
77
+    ev.property = property;
78
+    ev.time = time;
79
+
80
+    xcb_send_event(globalconf.connection, false, requestor,
81
+            XCB_EVENT_MASK_NO_EVENT, (char *) &ev);
82
+}
83
+
84
+void
85
+selection_transfer_reject(xcb_window_t requestor, xcb_atom_t selection,
86
+        xcb_atom_t target, xcb_timestamp_t time)
87
+{
88
+    selection_transfer_notify(requestor, selection, target, XCB_NONE, time);
89
+}
90
+
91
+static void
92
+transfer_done(lua_State *L, selection_transfer_t *transfer)
93
+{
94
+    transfer->state = TRANSFER_DONE;
95
+
96
+    lua_pushliteral(L, REGISTRY_TRANSFER_TABLE_INDEX);
97
+    lua_rawget(L, LUA_REGISTRYINDEX);
98
+    luaL_unref(L, -1, transfer->ref);
99
+    transfer->ref = LUA_NOREF;
100
+    lua_pop(L, 1);
101
+}
102
+
103
+static void
104
+transfer_continue_incremental(lua_State *L, int ud)
105
+{
106
+    const char *data;
107
+    size_t data_length;
108
+    selection_transfer_t *transfer = luaA_checkudata(L, ud, &selection_transfer_class);
109
+
110
+    ud = luaA_absindex(L, ud);
111
+
112
+    /* Get the data that is to be sent next */
113
+    luaA_getuservalue(L, ud);
114
+    lua_pushliteral(L, TRANSFER_DATA_INDEX);
115
+    lua_rawget(L, -2);
116
+    lua_remove(L, -2);
117
+
118
+    data = luaL_checklstring(L, -1, &data_length);
119
+    if (transfer->offset == data_length) {
120
+        if (transfer->more_data) {
121
+            /* Request the next piece of data from Lua */
122
+            transfer->state = TRANSFER_INCREMENTAL_DONE;
123
+            luaA_object_emit_signal(L, ud, "continue", 0);
124
+            if (transfer->state != TRANSFER_INCREMENTAL_DONE) {
125
+                /* Lua gave us more data to send. */
126
+                lua_pop(L, 1);
127
+                return;
128
+            }
129
+        }
130
+        /* End of transfer */
131
+        xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
132
+                transfer->requestor, transfer->property, UTF8_STRING, 8,
133
+                0, NULL);
134
+        xcb_change_window_attributes(globalconf.connection,
135
+                transfer->requestor, XCB_CW_EVENT_MASK,
136
+                (uint32_t[]) { 0 });
137
+        transfer_done(L, transfer);
138
+    } else {
139
+        /* Send next piece of data */
140
+        assert(transfer->offset < data_length);
141
+        size_t next_length = MIN(data_length - transfer->offset, max_property_length());
142
+        xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
143
+                transfer->requestor, transfer->property, UTF8_STRING, 8,
144
+                next_length, &data[transfer->offset]);
145
+        transfer->offset += next_length;
146
+    }
147
+    lua_pop(L, 1);
148
+}
149
+
150
+void
151
+selection_transfer_begin(lua_State *L, int ud, xcb_window_t requestor,
152
+        xcb_atom_t selection, xcb_atom_t target, xcb_atom_t property,
153
+        xcb_timestamp_t time)
154
+{
155
+    ud = luaA_absindex(L, ud);
156
+
157
+    /* Allocate a transfer object */
158
+    selection_transfer_t *transfer = (void *) selection_transfer_class.allocator(L);
159
+    transfer->requestor = requestor;
160
+    transfer->selection = selection;
161
+    transfer->target = target;
162
+    transfer->property = property;
163
+    transfer->time = time;
164
+    transfer->state = TRANSFER_WAIT_FOR_DATA;
165
+
166
+    /* Save the object in the registry */
167
+    lua_pushliteral(L, REGISTRY_TRANSFER_TABLE_INDEX);
168
+    lua_rawget(L, LUA_REGISTRYINDEX);
169
+    lua_pushvalue(L, -2);
170
+    transfer->ref = luaL_ref(L, -2);
171
+    lua_pop(L, 1);
172
+
173
+    /* Get the atom name */
174
+    xcb_get_atom_name_reply_t *reply = xcb_get_atom_name_reply(globalconf.connection,
175
+            xcb_get_atom_name_unchecked(globalconf.connection, target), NULL);
176
+    if (reply) {
177
+        lua_pushlstring(L, xcb_get_atom_name_name(reply),
178
+                xcb_get_atom_name_name_length(reply));
179
+        p_delete(&reply);
180
+    } else
181
+        lua_pushnil(L);
182
+
183
+    /* Emit the request signal with target and transfer object */
184
+    lua_pushvalue(L, -2);
185
+    luaA_object_emit_signal(L, ud, "request", 2);
186
+
187
+    /* Reject the transfer if Lua did not do anything */
188
+    if (transfer->state == TRANSFER_WAIT_FOR_DATA) {
189
+        selection_transfer_reject(requestor, selection, target, time);
190
+        transfer_done(L, transfer);
191
+    }
192
+
193
+    /* Remove the transfer object from the stack */
194
+    lua_pop(L, 1);
195
+}
196
+
197
+static int
198
+luaA_selection_transfer_send(lua_State *L)
199
+{
200
+    size_t data_length;
201
+    bool incr = false;
202
+    size_t incr_size = 0;
203
+
204
+    selection_transfer_t *transfer = luaA_checkudata(L, 1, &selection_transfer_class);
205
+    if (transfer->state != TRANSFER_WAIT_FOR_DATA && transfer->state != TRANSFER_INCREMENTAL_DONE)
206
+        luaL_error(L, "Transfer object is not ready for more data to be sent");
207
+
208
+    luaA_checktable(L, 2);
209
+
210
+    lua_pushliteral(L, "continue");
211
+    lua_rawget(L, 2);
212
+    transfer->more_data = incr = lua_toboolean(L, -1);
213
+    if (incr && lua_isnumber(L, -1))
214
+        incr_size = lua_tonumber(L, -1);
215
+    lua_pop(L, 1);
216
+
217
+    if (transfer->state == TRANSFER_INCREMENTAL_DONE) {
218
+        /* Save the data on the transfer object */
219
+        lua_pushliteral(L, "data");
220
+        lua_rawget(L, 2);
221
+
222
+        luaA_getuservalue(L, 1);
223
+        lua_pushliteral(L, TRANSFER_DATA_INDEX);
224
+        lua_pushvalue(L, -3);
225
+        lua_rawset(L, -3);
226
+        lua_pop(L, 1);
227
+
228
+        /* Continue the incremental transfer */
229
+        transfer->state = TRANSFER_INCREMENTAL_SENDING;
230
+        transfer->offset = 0;
231
+
232
+        transfer_continue_incremental(L, 1);
233
+
234
+        return 0;
235
+    }
236
+
237
+    /* Get format and data from the table */
238
+    lua_pushliteral(L, "format");
239
+    lua_rawget(L, 2);
240
+    lua_pushliteral(L, "data");
241
+    lua_rawget(L, 2);
242
+
243
+    if (lua_isstring(L, -2)) {
244
+        const char *format_string = luaL_checkstring(L, -2);
245
+        if (A_STRNEQ(format_string, "atom"))
246
+            luaL_error(L, "Unknown format '%s'", format_string);
247
+        if (incr)
248
+            luaL_error(L, "Cannot transfer atoms in pieces");
249
+
250
+        /* 'data' is a table with strings */
251
+        size_t len = luaA_rawlen(L, -1);
252
+
253
+        /* Get an array with atoms */
254
+        size_t atom_lengths[len];
255
+        const char *atom_strings[len];
256
+        for (size_t i = 0; i < len; i++) {
257
+            lua_rawgeti(L, -1, i+1);
258
+            atom_strings[i] = luaL_checklstring(L, -1, &atom_lengths[i]);
259
+            lua_pop(L, 1);
260
+        }
261
+
262
+        xcb_intern_atom_cookie_t cookies[len];
263
+        xcb_atom_t atoms[len];
264
+        for (size_t i = 0; i < len; i++) {
265
+            cookies[i] = xcb_intern_atom_unchecked(globalconf.connection, false,
266
+                    atom_lengths[i], atom_strings[i]);
267
+        }
268
+        for (size_t i = 0; i < len; i++) {
269
+            xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(globalconf.connection,
270
+                    cookies[i], NULL);
271
+            atoms[i] = reply ? reply->atom : XCB_NONE;
272
+            p_delete(&reply);
273
+        }
274
+
275
+        xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
276
+                transfer->requestor, transfer->property, XCB_ATOM_ATOM, 32,
277
+                len, &atoms[0]);
278
+    } else {
279
+        /* 'data' is a string with the data to transfer */
280
+        const char *data = luaL_checklstring(L, -1, &data_length);
281
+
282
+        if (!incr)
283
+            incr_size = data_length;
284
+
285
+        if (data_length >= max_property_length())
286
+            incr = true;
287
+
288
+        if (incr) {
289
+            xcb_change_window_attributes(globalconf.connection,
290
+                    transfer->requestor, XCB_CW_EVENT_MASK,
291
+                    (uint32_t[]) { XCB_EVENT_MASK_PROPERTY_CHANGE });
292
+
293
+            xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
294
+                    transfer->requestor, transfer->property, INCR, 32, 1,
295
+                    (const uint32_t[]) { incr_size });
296
+
297
+            /* Save the data on the transfer object */
298
+            luaA_getuservalue(L, 1);
299
+            lua_pushliteral(L, TRANSFER_DATA_INDEX);
300
+            lua_pushvalue(L, -3);
301
+            lua_rawset(L, -3);
302
+            lua_pop(L, 1);
303
+
304
+            transfer->state = TRANSFER_INCREMENTAL_SENDING;
305
+            transfer->offset = 0;
306
+        } else {
307
+            xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
308
+                    transfer->requestor, transfer->property, UTF8_STRING, 8,
309
+                    data_length, data);
310
+        }
311
+    }
312
+
313
+    selection_transfer_notify(transfer->requestor, transfer->selection,
314
+            transfer->target, transfer->property, transfer->time);
315
+    if (!incr)
316
+        transfer_done(L, transfer);
317
+
318
+    return 0;
319
+}
320
+
321
+void
322
+selection_transfer_handle_propertynotify(xcb_property_notify_event_t *ev)
323
+{
324
+    lua_State *L = globalconf_get_lua_State();
325
+
326
+    if (ev->state != XCB_PROPERTY_DELETE)
327
+        return;
328
+
329
+    /* Iterate over all active selection acquire objects */
330
+    lua_pushliteral(L, REGISTRY_TRANSFER_TABLE_INDEX);
331
+    lua_rawget(L, LUA_REGISTRYINDEX);
332
+    lua_pushnil(L);
333
+    while (lua_next(L, -2) != 0) {
334
+        if (lua_type(L, -1) == LUA_TUSERDATA) {
335
+            selection_transfer_t *transfer = lua_touserdata(L, -1);
336
+            if (transfer->state == TRANSFER_INCREMENTAL_SENDING
337
+                    && transfer->requestor == ev->window
338
+                    && transfer->property == ev->atom) {
339
+                transfer_continue_incremental(L, -1);
340
+                /* Remove table, key and transfer object */
341
+                lua_pop(L, 3);
342
+                return;
343
+            }
344
+        }
345
+        /* Remove the value, leaving only the key */
346
+        lua_pop(L, 1);
347
+    }
348
+    /* Remove the table */
349
+    lua_pop(L, 1);
350
+}
351
+
352
+static bool
353
+selection_transfer_checker(selection_transfer_t *transfer)
354
+{
355
+    return transfer->state != TRANSFER_DONE;
356
+}
357
+
358
+void
359
+selection_transfer_class_setup(lua_State *L)
360
+{
361
+    static const struct luaL_Reg selection_transfer_methods[] =
362
+    {
363
+        { NULL, NULL }
364
+    };
365
+
366
+    static const struct luaL_Reg selection_transfer_meta[] =
367
+    {
368
+        LUA_OBJECT_META(selection_transfer)
369
+        LUA_CLASS_META
370
+        { "send", luaA_selection_transfer_send },
371
+        { NULL, NULL }
372
+    };
373
+
374
+    /* Store a table in the registry that tracks active selection_transfer_t. */
375
+    lua_pushliteral(L, REGISTRY_TRANSFER_TABLE_INDEX);
376
+    lua_newtable(L);
377
+    lua_rawset(L, LUA_REGISTRYINDEX);
378
+
379
+    luaA_class_setup(L, &selection_transfer_class, "selection_transfer", NULL,
380
+            (lua_class_allocator_t) selection_transfer_new, NULL,
381
+            (lua_class_checker_t) selection_transfer_checker,
382
+            luaA_class_index_miss_property, luaA_class_newindex_miss_property,
383
+            selection_transfer_methods, selection_transfer_meta);
384
+}
385
+
386
+// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

+ 36
- 0
objects/selection_transfer.h View File

@@ -0,0 +1,36 @@
1
+/*
2
+ * selection_transfer.c - objects for selection transfer header
3
+ *
4
+ * Copyright © 2019 Uli Schlachter <psychon@znc.in>
5
+ *
6
+ * This program is free software; you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation; either version 2 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * This program is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+ * GNU General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU General Public License along
17
+ * with this program; if not, write to the Free Software Foundation, Inc.,
18
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
+ *
20
+ */
21
+
22
+#ifndef AWESOME_OBJECTS_SELECTION_TRANSFER_H
23
+#define AWESOME_OBJECTS_SELECTION_TRANSFER_H
24
+
25
+#include <lua.h>
26
+#include <xcb/xcb.h>
27
+
28
+void selection_transfer_class_setup(lua_State*);
29
+void selection_transfer_reject(xcb_window_t, xcb_atom_t, xcb_atom_t, xcb_timestamp_t);
30
+void selection_transfer_begin(lua_State*, int, xcb_window_t, xcb_atom_t,
31
+        xcb_atom_t, xcb_atom_t, xcb_timestamp_t);
32
+void selection_transfer_handle_propertynotify(xcb_property_notify_event_t*);
33
+
34
+#endif
35
+
36
+// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

+ 2
- 0
property.c View File

@@ -26,6 +26,7 @@
26 26
 #include "objects/client.h"
27 27
 #include "objects/drawin.h"
28 28
 #include "objects/selection_getter.h"
29
+#include "objects/selection_transfer.h"
29 30
 #include "xwindow.h"
30 31
 
31 32
 #include <xcb/xcb_atom.h>
@@ -478,6 +479,7 @@ property_handle_propertynotify(xcb_property_notify_event_t *ev)
478 479
     globalconf.timestamp = ev->time;
479 480
 
480 481
     property_handle_propertynotify_xproperty(ev);
482
+    selection_transfer_handle_propertynotify(ev);
481 483
 
482 484
     /* Find the correct event handler */
483 485
 #define HANDLE(atom_, cb) \

+ 237
- 0
tests/test-selection-transfer.lua View File

@@ -0,0 +1,237 @@
1
+-- Test the selection ownership and transfer API
2
+
3
+local runner = require("_runner")
4
+local spawn = require("awful.spawn")
5
+
6
+-- Assemble data for the large transfer that will be done later
7
+local large_transfer_piece = "a"
8
+for _ = 1, 25 do
9
+    large_transfer_piece = large_transfer_piece .. large_transfer_piece
10
+end
11
+large_transfer_piece = large_transfer_piece .. large_transfer_piece .. large_transfer_piece
12
+
13
+local large_transfer_piece_count = 3
14
+local large_transfer_size = #large_transfer_piece * large_transfer_piece_count
15
+
16
+local header = [[
17
+local lgi = require("lgi")
18
+local Gdk = lgi.Gdk
19
+local Gtk = lgi.Gtk
20
+local GLib = lgi.GLib
21
+local function assert_equal(a, b)
22
+    assert(a == b,
23
+        string.format("%s == %s", a or "nil/false", b or "nil/false"))
24
+end
25
+local clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
26
+]]
27
+
28
+local acquire_and_clear_clipboard = header .. [[
29
+clipboard:set_text("This is an experiment", -1)
30
+GLib.idle_add(GLib.PRIORITY_DEFAULT, Gtk.main_quit)
31
+Gtk.main()
32
+]]
33
+
34
+local done_footer = [[
35
+io.stdout:write("done")
36
+io.stdout:flush()
37
+]]
38
+
39
+local check_targets_and_text = header .. [[
40
+local targets = clipboard:wait_for_targets()
41
+assert_equal(targets[1]:name(), "TARGETS")
42
+assert_equal(targets[2]:name(), "UTF8_STRING")
43
+assert_equal(#targets, 2)
44
+assert_equal(clipboard:wait_for_text(), "Hello World!")
45
+]] .. done_footer
46
+
47
+local check_large_transfer = header
48
+    .. string.format("\nassert_equal(#clipboard:wait_for_text(), %d)\n", large_transfer_size)
49
+    .. done_footer
50
+
51
+local check_empty_selection = header .. [[
52
+assert_equal(clipboard:wait_for_targets(), nil)
53
+assert_equal(clipboard:wait_for_text(), nil)
54
+]] .. done_footer
55
+
56
+local selection
57
+local selection_released
58
+local continue
59
+
60
+local function wait_a_bit(count)
61
+    if continue or count == 5 then
62
+        return true
63
+    end
64
+end
65
+
66
+runner.run_steps{
67
+    function()
68
+        -- Get the selection
69
+        local s = assert(selection_acquire{ selection = "CLIPBOARD" },
70
+            "Failed to acquire the clipboard selection")
71
+
72
+        -- Steal selection ownership from ourselves and test that it works
73
+        local s_released
74
+        s:connect_signal("release", function() s_released = true end)
75
+
76
+        selection = assert(selection_acquire{ selection = "CLIPBOARD" },
77
+            "Failed to acquire the clipboard selection")
78
+
79
+        assert(s_released)
80
+
81
+        -- Now test selection transfers
82
+        selection = assert(selection_acquire{ selection = "CLIPBOARD" },
83
+            "Failed to acquire the clipboard selection")
84
+        selection:connect_signal("request", function(_, target, transfer)
85
+            if target == "TARGETS" then
86
+                transfer:send{
87
+                    format = "atom",
88
+                    data = { "TARGETS", "UTF8_STRING" },
89
+                }
90
+            elseif target == "UTF8_STRING" then
91
+                transfer:send{ data = "Hello World!" }
92
+            end
93
+        end)
94
+        awesome.sync()
95
+        spawn.with_line_callback({ "lua", "-e", check_targets_and_text },
96
+            { stdout = function(line)
97
+                assert(line == "done", "Unexpected line: " .. line)
98
+                continue = true
99
+            end })
100
+        return true
101
+    end,
102
+
103
+    function()
104
+        -- Wait for the test to succeed
105
+        if not continue then
106
+            return
107
+        end
108
+        continue = false
109
+
110
+        -- Now test piece-wise selection transfers
111
+        selection = assert(selection_acquire{ selection = "CLIPBOARD" },
112
+            "Failed to acquire the clipboard selection")
113
+        selection:connect_signal("request", function(_, target, transfer)
114
+            if target == "TARGETS" then
115
+                transfer:send{
116
+                    format = "atom",
117
+                    data = { "TARGETS", "UTF8_STRING" },
118
+                }
119
+            elseif target == "UTF8_STRING" then
120
+                local offset = 1
121
+                local data = "Hello World!"
122
+                local function send_piece()
123
+                    local piece = data:sub(offset, offset)
124
+                    transfer:send{
125
+                        data = piece,
126
+                        continue = piece ~= ""
127
+                    }
128
+                    offset = offset + 1
129
+                end
130
+                transfer:connect_signal("continue", send_piece)
131
+                send_piece()
132
+            end
133
+        end)
134
+        awesome.sync()
135
+        spawn.with_line_callback({ "lua", "-e", check_targets_and_text },
136
+            { stdout = function(line)
137
+                assert(line == "done", "Unexpected line: " .. line)
138
+                continue = true
139
+            end })
140
+        return true
141
+    end,
142
+
143
+    function()
144
+        -- Wait for the test to succeed
145
+        if not continue then
146
+            return
147
+        end
148
+        continue = false
149
+
150
+        -- Now test a huge transfer
151
+        selection = assert(selection_acquire{ selection = "CLIPBOARD" },
152
+            "Failed to acquire the clipboard selection")
153
+        selection:connect_signal("request", function(_, target, transfer)
154
+            if target == "TARGETS" then
155
+                transfer:send{
156
+                    format = "atom",
157
+                    data = { "TARGETS", "UTF8_STRING" },
158
+                }
159
+            elseif target == "UTF8_STRING" then
160
+                local count = 0
161
+                local function send_piece()
162
+                    count = count + 1
163
+                    local done = count == large_transfer_piece_count
164
+                    transfer:send{
165
+                        data = large_transfer_piece,
166
+                        continue = not done,
167
+                    }
168
+                end
169
+                transfer:connect_signal("continue", send_piece)
170
+                send_piece()
171
+            end
172
+        end)
173
+        awesome.sync()
174
+        spawn.with_line_callback({ "lua", "-e", check_large_transfer },
175
+            { stdout = function(line)
176
+                assert(line == "done", "Unexpected line: " .. line)
177
+                continue = true
178
+            end })
179
+        return true
180
+    end,
181
+
182
+    -- The large data transfer above transfers 3 * 2^25 bytes of data. That's
183
+    -- 96 MiB and takes a while.
184
+    wait_a_bit,
185
+    wait_a_bit,
186
+    wait_a_bit,
187
+    wait_a_bit,
188
+    wait_a_bit,
189
+
190
+    function()
191
+        -- Wait for the test to succeed
192
+        if not continue then
193
+            return
194
+        end
195
+        continue = false
196
+
197
+        -- Now test that :release() works
198
+        selection:release()
199
+        awesome.sync()
200
+        spawn.with_line_callback({ "lua", "-e", check_empty_selection },
201
+            { stdout = function(line)
202
+                assert(line == "done", "Unexpected line: " .. line)
203
+                continue = true
204
+            end })
205
+
206
+        return true
207
+    end,
208
+
209
+    function()
210
+        -- Wait for the test to succeed
211
+        if not continue then
212
+            return
213
+        end
214
+        continue = false
215
+
216
+        -- Test for "release" signal when we lose selection
217
+        selection = assert(selection_acquire{ selection = "CLIPBOARD" },
218
+            "Failed to acquire the clipboard selection")
219
+        selection:connect_signal("release", function() selection_released = true end)
220
+        awesome.sync()
221
+        spawn.with_line_callback({ "lua", "-e", acquire_and_clear_clipboard },
222
+            { exit = function() continue = true end })
223
+        return true
224
+    end,
225
+
226
+    function()
227
+        -- Wait for the previous test to succeed
228
+        if not continue then
229
+            return
230
+        end
231
+        continue = false
232
+        assert(selection_released)
233
+        return true
234
+    end,
235
+}
236
+
237
+-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

Loading…
Cancel
Save