-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib-x11selection-gtk.c
349 lines (299 loc) · 8.65 KB
/
lib-x11selection-gtk.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
* Copyright Neil Brown ©2020-2021 <[email protected]>
* May be distributed under terms of GPLv2 - see file:COPYING
*
* x11selection - integrate X11 clipboards with copybuf and selection.
*
* Use gtk_clipboard interfaces to provide the selection and recent copied
* content to other applications, and to use what is provided by those
* applications to satisfy internal requests.
*
* We overload copy:save to claim both PRIMARY and CLIPBOARD so other apps will
* ask us for content. When asked we call copy:get to get the content, but see
* selections below.
* We overload copy:get to interpolate PRIMARY and CLIPBOARD into the list
* of copies, if they are exist, are not owned by us and only consider CLIPBOARD
* if it is different to PRIMARY.
*
* We also claim the edlib selection at startup on behalf of whichever X11
* application owns it. If it is claimed from us, we claim ownership of PRIMARY.
* If it is committed, we ask for text from the owner of PRIMARY and save that.
* If we lose ownership of the PRIMARY, we reclaim the selection.
*/
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#define PANE_DATA_TYPE struct xs_info
#include "core.h"
#ifndef __CHECKER__
#include <gtk/gtk.h>
#else
/* sparse/smatch don't like gtk.h, so provide our own declarations */
typedef void *gpointer;
typedef unsigned int guint;
typedef char gchar;
typedef struct _GdkDisplay {} GdkDisplay;
typedef struct _GtkTargetEntry {} GtkTargetEntry;
typedef struct _GtkTargetList {} GtkTargetList;
typedef struct _GtkClipboard {} GtkClipboard;
typedef struct _GtkSelectionData {} GtkSelectionData;
typedef int GdkAtom;
GdkAtom gdk_atom_intern(gchar *, int);
#define TRUE (1)
#define FALSE (0)
GdkDisplay *gdk_display_open(gchar*);
void gdk_display_close(GdkDisplay*);
GtkClipboard *gtk_clipboard_get_for_display(GdkDisplay*, GdkAtom);
void gtk_clipboard_set_with_data(GtkClipboard*, GtkTargetEntry*, guint,
void (*get)(GtkClipboard *, GtkSelectionData *,
guint, gpointer d safe),
void (*clear)(GtkClipboard *, gpointer safe),
gpointer d safe);
void gtk_clipboard_clear(GtkClipboard*);
void gtk_selection_data_set_text(GtkSelectionData *, gchar *, guint);
gchar *gtk_clipboard_wait_for_text(GtkClipboard*);
int gtk_clipboard_wait_is_text_available(GtkClipboard*);
void g_free(gpointer);
GtkTargetList *gtk_target_list_new(gpointer, guint);
void gtk_target_list_add_text_targets(GtkTargetList *, guint);
void gtk_target_list_unref(GtkTargetList *);
GtkTargetEntry *gtk_target_table_new_from_list(GtkTargetList *, int *);
void gtk_target_table_free(GtkTargetEntry*, int);
#endif
struct xs_info {
struct pane *self safe;
GdkDisplay *display;
struct cb {
/* 'data' is allocated space that stores a pointer to this
* xs_info. Data is given the gtk has a handle.
*/
struct xs_info **data;
int saved;
GtkClipboard *cb;
} primary, clipboard;
GtkTargetEntry *text_targets;
int n_text_targets;
};
#include "core-pane.h"
static void do_get(GtkClipboard *cb, GtkSelectionData *sd,
guint info, gpointer vdata safe)
{
/* Another X11 application has asked for clipboard data */
struct xs_info **data = vdata;
struct xs_info *xsi = *data;
char *s;
if (!xsi)
return;
if (cb == xsi->primary.cb)
/* If there is an active selection, now if the time for
* the content to be copied.
*/
call("selection:commit", xsi->self);
s = call_ret(strsave, "copy:get", xsi->self);
if (!s)
s = "";
gtk_selection_data_set_text(sd, s, strlen(s));
}
static void do_clear(GtkClipboard *cb, gpointer vdata safe)
{
struct xs_info **data = vdata;
struct xs_info *xsi = *data;
if (!xsi)
return;
/* Some other X11 application wants us to release ownership
* of the clipboard.
*/
if (data == xsi->primary.data) {
/* This means some other application now has a "selection",
* so we claim it on their behalf.
*/
xsi->primary.data = NULL;
call("selection:claim", xsi->self);
}
if (data == xsi->clipboard.data)
xsi->clipboard.data = NULL;
free(data);
}
static void claim_primary(struct xs_info *xsi safe)
{
struct xs_info **data;
data = malloc(sizeof(*data));
*data = xsi;
gtk_clipboard_set_with_data(xsi->primary.cb,
xsi->text_targets,
xsi->n_text_targets,
do_get, do_clear, data);
xsi->primary.data = data;
xsi->primary.saved = 0;
}
static void claim_both(struct xs_info *xsi safe)
{
struct xs_info **data;
claim_primary(xsi);
data = malloc(sizeof(*data));
*data = xsi;
gtk_clipboard_set_with_data(xsi->clipboard.cb,
xsi->text_targets,
xsi->n_text_targets,
do_get, do_clear, data);
xsi->clipboard.data = data;
xsi->clipboard.saved = 0;
}
DEF_CMD(xs_copy_save)
{
struct xs_info *xsi = ci->home->data;
claim_both(xsi);
/* Some edlib pane own the selection, so we renounce any ownership
* by any X11 application.
*/
call("selection:discard", ci->home);
return Efallthrough;
}
DEF_CMD(xs_copy_get)
{
struct xs_info *xsi = ci->home->data;
int num = ci->num;
if (xsi->clipboard.data == NULL) {
if (num == 0) {
/* Return CLIPBOARD if it exists */
gchar *s = NULL;
if (gtk_clipboard_wait_is_text_available(
xsi->clipboard.cb))
s = gtk_clipboard_wait_for_text(
xsi->clipboard.cb);
if (s && *s) {
comm_call(ci->comm2, "cb", ci->focus,
0, NULL, s);
num -= 1;
}
g_free(s);
} else {
/* Just check if a string exists */
if (gtk_clipboard_wait_is_text_available(
xsi->clipboard.cb))
num -= 1;
}
}
if (num < 0)
return 1;
return call_comm(ci->key, ci->home->parent, ci->comm2, num);
}
DEF_CMD(xs_sel_claimed)
{
struct xs_info *xsi = ci->home->data;
if (ci->focus != ci->home)
/* not for me */
return Efallthrough;
/* Some other pane holds the selection, so better tell
* other X11 clients
*/
claim_primary(xsi);
return 1;
}
DEF_CMD(xs_sel_commit)
{
struct xs_info *xsi = ci->home->data;
gchar *s;
/* Someone wants to paste the selection */
/* Record PRIMARY if it exists */
if (ci->focus != ci->home)
/* not for me */
return Efallthrough;
if (xsi->primary.data || xsi->primary.saved)
/* We own the primary, so nothing to do */
return 1;
if (!xsi->clipboard.data && !xsi->clipboard.saved) {
/* get the clipboard first - to make sure it is available
* as second saved text.
*/
s = NULL;
if (gtk_clipboard_wait_is_text_available(
xsi->clipboard.cb))
s = gtk_clipboard_wait_for_text(xsi->clipboard.cb);
if (s && *s) {
call("copy:save", ci->home->parent,
0, NULL, s);
xsi->clipboard.saved = 1;
}
g_free(s);
}
s = NULL;
if (gtk_clipboard_wait_is_text_available(xsi->primary.cb))
s = gtk_clipboard_wait_for_text(xsi->primary.cb);
if (s && *s) {
call("copy:save", ci->home->parent,
0, NULL, s);
xsi->primary.saved = 1;
}
g_free(s);
return Efallthrough;
}
DEF_CMD_CLOSED(xs_close)
{
struct xs_info *xsi = ci->home->data;
if (xsi->primary.data)
gtk_clipboard_clear(xsi->primary.cb);
if (xsi->clipboard.data)
gtk_clipboard_clear(xsi->clipboard.cb);
free(xsi->primary.data);
free(xsi->clipboard.data);
gtk_target_table_free(xsi->text_targets, xsi->n_text_targets);
gdk_display_close(xsi->display);
return 1;
}
DEF_CMD(xs_clone)
{
struct pane *p;
p = call_ret(pane, "attach-x11selection", ci->focus);
pane_clone_children(ci->home, p);
return 1;
}
static struct map *xs_map;
DEF_LOOKUP_CMD(xs_handle, xs_map);
DEF_CMD(xs_attach)
{
char *d;
struct xs_info *xsi;
struct pane *p;
GdkAtom primary, clipboard;
GtkTargetList *list;
GdkDisplay *dis;
d = pane_attr_get(ci->focus, "DISPLAY");
if (!d || !*d)
return 1;
dis = gdk_display_open(d);
if (!dis)
return 1;
call("attach-glibevents", ci->focus);
p = pane_register(ci->focus, 0, &xs_handle.c);
if (!p)
return Efail;
xsi = p->data;
xsi->display = dis;
primary = gdk_atom_intern("PRIMARY", TRUE);
clipboard = gdk_atom_intern("CLIPBOARD", TRUE);
xsi->primary.cb = gtk_clipboard_get_for_display(dis, primary);
xsi->clipboard.cb = gtk_clipboard_get_for_display(dis, clipboard);
list = gtk_target_list_new(NULL, 0);
gtk_target_list_add_text_targets(list, 0);
xsi->text_targets =
gtk_target_table_new_from_list(list, &xsi->n_text_targets);
gtk_target_list_unref (list);
claim_both(xsi);
xsi->self = p;
return comm_call(ci->comm2, "cb:attach", xsi->self);
}
void edlib_init(struct pane *ed safe)
{
if (!xs_map) {
xs_map = key_alloc();
key_add(xs_map, "copy:save", &xs_copy_save);
key_add(xs_map, "copy:get", &xs_copy_get);
key_add(xs_map, "Notify:selection:claimed", &xs_sel_claimed);
key_add(xs_map, "Notify:selection:commit", &xs_sel_commit);
key_add(xs_map, "Clone", &xs_clone);
key_add(xs_map, "Close", &xs_close);
}
call_comm("global-set-command", ed, &xs_attach,
0, NULL, "attach-x11selection");
}