-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctrlsel.c
1621 lines (1533 loc) · 38.5 KB
/
ctrlsel.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdlib.h>
#include <string.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/keysym.h>
#include <X11/cursorfont.h>
#include <X11/Xcursor/Xcursor.h>
#include "ctrlsel.h"
#define _TIMESTAMP_PROP "_TIMESTAMP_PROP"
#define TIMESTAMP "TIMESTAMP"
#define ATOM_PAIR "ATOM_PAIR"
#define MULTIPLE "MULTIPLE"
#define MANAGER "MANAGER"
#define TARGETS "TARGETS"
#define INCR "INCR"
#define SELDEFSIZE 0x4000
#define FLAG(f, b) (((f) & (b)) == (b))
#define MOTION_TIME 32
#define DND_DISTANCE 8 /* distance from pointer to dnd miniwindow */
#define XDND_VERSION 5 /* XDND protocol version */
#define NCLIENTMSG_DATA 5 /* number of members on a the .data.l[] array of a XClientMessageEvent */
enum {
CONTENT_INCR,
CONTENT_ZERO,
CONTENT_ERROR,
CONTENT_SUCCESS,
};
enum {
PAIR_TARGET,
PAIR_PROPERTY,
PAIR_LAST
};
enum {
/* xdnd window properties */
XDND_AWARE,
/* xdnd selections */
XDND_SELECTION,
/* xdnd client messages */
XDND_ENTER,
XDND_POSITION,
XDND_STATUS,
XDND_LEAVE,
XDND_DROP,
XDND_FINISHED,
/* xdnd actions */
XDND_ACTION_COPY,
XDND_ACTION_MOVE,
XDND_ACTION_LINK,
XDND_ACTION_ASK,
XDND_ACTION_PRIVATE,
XDND_ATOM_LAST,
};
enum {
CURSOR_TARGET,
CURSOR_PIRATE,
CURSOR_DRAG,
CURSOR_COPY,
CURSOR_MOVE,
CURSOR_LINK,
CURSOR_NODROP,
CURSOR_LAST,
};
struct Transfer {
/*
* When a client request the clipboard but its content is too
* large, we perform incremental transfer. We keep track of
* each incremental transfer in a list of transfers.
*/
struct Transfer *prev, *next;
struct CtrlSelTarget *target;
Window requestor;
Atom property;
unsigned long size; /* how much have we transferred */
};
struct PredArg {
CtrlSelContext *context;
Window window;
Atom message_type;
};
struct CtrlSelContext {
Display *display;
Window window;
Atom selection;
Time time;
unsigned long ntargets;
struct CtrlSelTarget *targets;
/*
* Items below are used internally to keep track of any
* incremental transference in progress.
*/
unsigned long selmaxsize;
unsigned long ndone;
void *transfers;
/*
* Items below are used internally for drag-and-dropping.
*/
Window dndwindow;
unsigned int dndactions, dndresult;
};
static char *atomnames[XDND_ATOM_LAST] = {
[XDND_AWARE] = "XdndAware",
[XDND_SELECTION] = "XdndSelection",
[XDND_ENTER] = "XdndEnter",
[XDND_POSITION] = "XdndPosition",
[XDND_STATUS] = "XdndStatus",
[XDND_LEAVE] = "XdndLeave",
[XDND_DROP] = "XdndDrop",
[XDND_FINISHED] = "XdndFinished",
[XDND_ACTION_COPY] = "XdndActionCopy",
[XDND_ACTION_MOVE] = "XdndActionMove",
[XDND_ACTION_LINK] = "XdndActionLink",
[XDND_ACTION_ASK] = "XdndActionAsk",
[XDND_ACTION_PRIVATE] = "XdndActionPrivate",
};
static int
between(int x, int y, int x0, int y0, int w0, int h0)
{
return x >= x0 && x < x0 + w0 && y >= y0 && y < y0 + h0;
}
static void
clientmsg(Display *dpy, Window win, Atom atom, long d[5])
{
XEvent ev;
ev.xclient.type = ClientMessage;
ev.xclient.display = dpy;
ev.xclient.serial = 0;
ev.xclient.send_event = True;
ev.xclient.message_type = atom;
ev.xclient.window = win;
ev.xclient.format = 32;
ev.xclient.data.l[0] = d[0];
ev.xclient.data.l[1] = d[1];
ev.xclient.data.l[2] = d[2];
ev.xclient.data.l[3] = d[3];
ev.xclient.data.l[4] = d[4];
(void)XSendEvent(dpy, win, False, 0x0, &ev);
}
static unsigned long
getselmaxsize(Display *display)
{
unsigned long n;
if ((n = XExtendedMaxRequestSize(display)) > 0)
return n;
if ((n = XMaxRequestSize(display)) > 0)
return n;
return SELDEFSIZE;
}
static int
getservertime(Display *display, Time *time)
{
XEvent xev;
Window window;
Atom timeprop;
/*
* According to ICCCM, a client wishing to acquire ownership of
* a selection should set the specfied time to some time between
* the current last-change time of the selection concerned and
* the current server time.
*
* Those clients should not set the time value to `CurrentTime`,
* because if they do so, they have no way of finding when they
* gained ownership of the selection.
*
* In the case that an event triggers the acquisition of the
* selection, this time value can be obtained from the event
* itself.
*
* In the case that the client must unconditionally acquire the
* ownership of a selection (which is our case), a zero-length
* append to a property is a way to obtain a timestamp for this
* purpose. The timestamp is in the corresponding
* `PropertyNotify` event.
*/
if (time != CurrentTime)
return 1;
timeprop = XInternAtom(display, _TIMESTAMP_PROP, False);
if (timeprop == None)
goto error;
window = XCreateWindow(
display,
DefaultRootWindow(display),
0, 0, 1, 1, 0,
CopyFromParent, CopyFromParent, CopyFromParent,
CWEventMask,
&(XSetWindowAttributes){
.event_mask = PropertyChangeMask,
}
);
if (window == None)
goto error;
XChangeProperty(
display, window,
timeprop, timeprop,
8L, PropModeAppend, NULL, 0
);
while (!XWindowEvent(display, window, PropertyChangeMask, &xev)) {
if (xev.type == PropertyNotify &&
xev.xproperty.window == window &&
xev.xproperty.atom == timeprop) {
*time = xev.xproperty.time;
break;
}
}
(void)XDestroyWindow(display, window);
return 1;
error:
return 0;
}
static int
nbytes(int format)
{
switch (format) {
default: return sizeof(char);
case 16: return sizeof(short);
case 32: return sizeof(long);
}
}
static int
getcontent(struct CtrlSelTarget *target, Display *display, Window window, Atom property)
{
unsigned char *p, *q;
unsigned long len, addsize, size;
unsigned long dl; /* dummy variable */
int status;
Atom incr;
incr = XInternAtom(display, INCR, False),
status = XGetWindowProperty(
display,
window,
property,
0L, 0x1FFFFFFF,
True,
AnyPropertyType,
&target->type,
&target->format,
&len, &dl, &p
);
if (target->format != 32 && target->format != 16)
target->format = 8;
if (target->type == incr) {
XFree(p);
return CONTENT_INCR;
}
if (len == 0) {
XFree(p);
return CONTENT_ZERO;
}
if (status != Success) {
XFree(p);
return CONTENT_ERROR;
}
if (p == NULL) {
XFree(p);
return CONTENT_ERROR;
}
addsize = len * nbytes(target->format);
size = addsize;
if (target->buffer != NULL) {
/* append buffer */
size += target->bufsize;
if ((q = realloc(target->buffer, size + 1)) == NULL) {
XFree(p);
return CONTENT_ERROR;
}
memcpy(q + target->bufsize, p, addsize);
target->buffer = q;
target->bufsize = size;
target->nitems += len;
} else {
/* new buffer */
if ((q = malloc(size + 1)) == NULL) {
XFree(p);
return CONTENT_ERROR;
}
memcpy(q, p, addsize);
target->buffer = q;
target->bufsize = size;
target->nitems = len;
}
target->buffer[size] = '\0';
XFree(p);
return CONTENT_SUCCESS;
}
static void
deltransfer(CtrlSelContext *context, struct Transfer *transfer)
{
if (transfer->prev != NULL) {
transfer->prev->next = transfer->next;
} else {
context->transfers = transfer->next;
}
if (transfer->next != NULL) {
transfer->next->prev = transfer->prev;
}
}
static void
freetransferences(CtrlSelContext *context)
{
struct Transfer *transfer;
while (context->transfers != NULL) {
transfer = (struct Transfer *)context->transfers;
context->transfers = ((struct Transfer *)context->transfers)->next;
XDeleteProperty(
context->display,
transfer->requestor,
transfer->property
);
free(transfer);
}
context->transfers = NULL;
}
static void
freebuffers(CtrlSelContext *context)
{
unsigned long i;
for (i = 0; i < context->ntargets; i++) {
free(context->targets[i].buffer);
context->targets[i].buffer = NULL;
context->targets[i].nitems = 0;
context->targets[i].bufsize = 0;
}
}
static unsigned long
getatomsprop(Display *display, Window window, Atom property, Atom type, Atom **atoms)
{
unsigned char *p;
unsigned long len;
unsigned long dl; /* dummy variable */
int format;
Atom gottype;
unsigned long size;
int success;
success = XGetWindowProperty(
display,
window,
property,
0L, 0x1FFFFFFF,
False,
type, &gottype,
&format, &len,
&dl, &p
);
if (success != Success || len == 0 || p == NULL || format != 32)
goto error;
if (type != AnyPropertyType && type != gottype)
goto error;
size = len * sizeof(**atoms);
if ((*atoms = malloc(size)) == NULL)
goto error;
memcpy(*atoms, p, size);
XFree(p);
return len;
error:
XFree(p);
*atoms = NULL;
return 0;
}
static int
newtransfer(CtrlSelContext *context, struct CtrlSelTarget *target, Window requestor, Atom property)
{
struct Transfer *transfer;
transfer = malloc(sizeof(*transfer));
if (transfer == NULL)
return 0;
*transfer = (struct Transfer){
.prev = NULL,
.next = (struct Transfer *)context->transfers,
.requestor = requestor,
.property = property,
.target = target,
.size = 0,
};
if (context->transfers != NULL)
((struct Transfer *)context->transfers)->prev = transfer;
context->transfers = transfer;
return 1;
}
static Bool
convert(CtrlSelContext *context, Window requestor, Atom target, Atom property)
{
Atom multiple, timestamp, targets, incr;
Atom *supported;
unsigned long i;
int nsupported;
incr = XInternAtom(context->display, INCR, False);
targets = XInternAtom(context->display, TARGETS, False);
multiple = XInternAtom(context->display, MULTIPLE, False);
timestamp = XInternAtom(context->display, TIMESTAMP, False);
if (target == multiple) {
/* A MULTIPLE should be handled when processing a
* SelectionRequest event. We do not support nested
* MULTIPLE targets.
*/
return False;
}
if (target == timestamp) {
/*
* According to ICCCM, to avoid some race conditions, it
* is important that requestors be able to discover the
* timestamp the owner used to acquire ownership.
* Requestors do that by requesting selection owners to
* convert the `TIMESTAMP` target. Selection owners
* must return the timestamp as an `XA_INTEGER`.
*/
XChangeProperty(
context->display,
requestor,
property,
XA_INTEGER, 32,
PropModeReplace,
(unsigned char *)&context->time,
1
);
return True;
}
if (target == targets) {
/*
* According to ICCCM, when requested for the `TARGETS`
* target, the selection owner should return a list of
* atoms representing the targets for which an attempt
* to convert the selection will (hopefully) succeed.
*/
nsupported = context->ntargets + 2; /* +2 for MULTIPLE + TIMESTAMP */
if ((supported = calloc(nsupported, sizeof(*supported))) == NULL)
return False;
for (i = 0; i < context->ntargets; i++) {
supported[i] = context->targets[i].target;
}
supported[i++] = multiple;
supported[i++] = timestamp;
XChangeProperty(
context->display,
requestor,
property,
XA_ATOM, 32,
PropModeReplace,
(unsigned char *)supported,
nsupported
);
free(supported);
return True;
}
for (i = 0; i < context->ntargets; i++) {
if (target == context->targets[i].target)
goto found;
}
return False;
found:
if (context->targets[i].bufsize > context->selmaxsize) {
XSelectInput(
context->display,
requestor,
StructureNotifyMask | PropertyChangeMask
);
XChangeProperty(
context->display,
requestor,
property,
incr,
32L,
PropModeReplace,
(unsigned char *)context->targets[i].buffer,
1
);
newtransfer(context, &context->targets[i], requestor, property);
} else {
XChangeProperty(
context->display,
requestor,
property,
target,
context->targets[i].format,
PropModeReplace,
context->targets[i].buffer,
context->targets[i].nitems
);
}
return True;
}
static int
request(CtrlSelContext *context)
{
Atom multiple, atom_pair;
Atom *pairs;
unsigned long i, size;
for (i = 0; i < context->ntargets; i++) {
context->targets[i].nitems = 0;
context->targets[i].bufsize = 0;
context->targets[i].buffer = NULL;
}
if (context->ntargets == 1) {
(void)XConvertSelection(
context->display,
context->selection,
context->targets[0].target,
context->targets[0].target,
context->window,
context->time
);
} else if (context->ntargets > 1) {
multiple = XInternAtom(context->display, MULTIPLE, False);
atom_pair = XInternAtom(context->display, ATOM_PAIR, False);
size = 2 * context->ntargets;
pairs = calloc(size, sizeof(*pairs));
if (pairs == NULL)
return 0;
for (i = 0; i < context->ntargets; i++) {
pairs[i * 2 + 0] = context->targets[i].target;
pairs[i * 2 + 1] = context->targets[i].target;
}
(void)XChangeProperty(
context->display,
context->window,
multiple,
atom_pair,
32,
PropModeReplace,
(unsigned char *)pairs,
size
);
(void)XConvertSelection(
context->display,
context->selection,
multiple,
multiple,
context->window,
context->time
);
free(pairs);
}
return 1;
}
void
ctrlsel_filltarget(
Atom target,
Atom type,
int format,
unsigned char *buffer,
unsigned long size,
struct CtrlSelTarget *fill
) {
if (fill == NULL)
return;
if (format != 32 && format != 16)
format = 8;
*fill = (struct CtrlSelTarget){
.target = target,
.type = type,
.action = None,
.format = format,
.nitems = size / nbytes(format),
.buffer = buffer,
.bufsize = size,
};
}
CtrlSelContext *
ctrlsel_request(
Display *display,
Window window,
Atom selection,
Time time,
struct CtrlSelTarget targets[],
unsigned long ntargets
) {
CtrlSelContext *context;
if (!getservertime(display, &time))
return NULL;
if ((context = malloc(sizeof(*context))) == NULL)
return NULL;
*context = (CtrlSelContext){
.display = display,
.window = window,
.selection = selection,
.time = time,
.targets = targets,
.ntargets = ntargets,
.selmaxsize = getselmaxsize(display),
.ndone = 0,
.transfers = NULL,
.dndwindow = None,
.dndactions = 0x00,
.dndresult = 0x00,
};
if (ntargets == 0)
return context;
if (request(context))
return context;
free(context);
return NULL;
}
CtrlSelContext *
ctrlsel_setowner(
Display *display,
Window window,
Atom selection,
Time time,
int ismanager,
struct CtrlSelTarget targets[],
unsigned long ntargets
) {
CtrlSelContext *context;
Window root;
root = DefaultRootWindow(display);
if (!getservertime(display, &time))
return NULL;
if ((context = malloc(sizeof(*context))) == NULL)
return NULL;
*context = (CtrlSelContext){
.display = display,
.window = window,
.selection = selection,
.time = time,
.targets = targets,
.ntargets = ntargets,
.selmaxsize = getselmaxsize(display),
.ndone = 0,
.transfers = NULL,
.dndwindow = None,
.dndactions = 0x00,
.dndresult = 0x00,
};
(void)XSetSelectionOwner(display, selection, window, time);
if (XGetSelectionOwner(display, selection) != window) {
free(context);
return NULL;
}
if (!ismanager)
return context;
/*
* According to ICCCM, a manager client (that is, a client
* responsible for managing shared resources) should take
* ownership of an appropriate selection.
*
* Immediately after a manager successfully acquires ownership
* of a manager selection, it should announce its arrival by
* sending a `ClientMessage` event. (That is necessary for
* clients to be able to know when a specific manager has
* started: any client that wish to do so should select for
* `StructureNotify` on the root window and should watch for
* the appropriate `MANAGER` `ClientMessage`).
*/
(void)XSendEvent(
display,
root,
False,
StructureNotifyMask,
(XEvent *)&(XClientMessageEvent){
.type = ClientMessage,
.window = root,
.message_type = XInternAtom(display, MANAGER, False),
.format = 32,
.data.l[0] = time, /* timestamp */
.data.l[1] = selection, /* manager selection atom */
.data.l[2] = window, /* window owning the selection */
.data.l[3] = 0, /* manager-specific data */
.data.l[4] = 0, /* manager-specific data */
}
);
return context;
}
static int
receiveinit(CtrlSelContext *context, XEvent *xev)
{
struct CtrlSelTarget *targetp;
XSelectionEvent *xselev;
Atom multiple, atom_pair;
Atom *pairs;
Atom pair[PAIR_LAST];
unsigned long j, natoms;
unsigned long i;
int status, success;
multiple = XInternAtom(context->display, MULTIPLE, False);
atom_pair = XInternAtom(context->display, ATOM_PAIR, False);
xselev = &xev->xselection;
if (xselev->selection != context->selection)
return CTRLSEL_NONE;
if (xselev->requestor != context->window)
return CTRLSEL_NONE;
if (xselev->property == None)
return CTRLSEL_ERROR;
if (xselev->target == multiple) {
natoms = getatomsprop(
xselev->display,
xselev->requestor,
xselev->property,
atom_pair,
&pairs
);
if (natoms == 0 || pairs == NULL) {
free(pairs);
return CTRLSEL_ERROR;
}
} else {
pair[PAIR_TARGET] = xselev->target;
pair[PAIR_PROPERTY] = xselev->property;
pairs = pair;
natoms = 2;
}
success = 1;
for (j = 0; j < natoms; j += 2) {
targetp = NULL;
for (i = 0; i < context->ntargets; i++) {
if (pairs[j + PAIR_TARGET] == context->targets[i].target) {
targetp = &context->targets[i];
break;
}
}
if (pairs[j + PAIR_PROPERTY] == None)
pairs[j + PAIR_PROPERTY] = pairs[j + PAIR_TARGET];
if (targetp == NULL) {
success = 0;
continue;
}
status = getcontent(
targetp,
xselev->display,
xselev->requestor,
pairs[j + PAIR_PROPERTY]
);
switch (status) {
case CONTENT_ERROR:
success = 0;
break;
case CONTENT_SUCCESS:
/* fallthrough */
case CONTENT_ZERO:
context->ndone++;
break;
case CONTENT_INCR:
if (!newtransfer(context, targetp, xselev->requestor, pairs[j + PAIR_PROPERTY]))
success = 0;
break;
}
}
if (xselev->target == multiple)
free(pairs);
return success ? CTRLSEL_INTERNAL : CTRLSEL_ERROR;
}
static int
receiveincr(CtrlSelContext *context, XEvent *xev)
{
struct Transfer *transfer;
XPropertyEvent *xpropev;
int status;
xpropev = &xev->xproperty;
if (xpropev->state != PropertyNewValue)
return CTRLSEL_NONE;
if (xpropev->window != context->window)
return CTRLSEL_NONE;
for (transfer = (struct Transfer *)context->transfers; transfer != NULL; transfer = transfer->next)
if (transfer->property == xpropev->atom)
goto found;
return CTRLSEL_NONE;
found:
status = getcontent(
transfer->target,
xpropev->display,
xpropev->window,
xpropev->atom
);
switch (status) {
case CONTENT_ERROR:
case CONTENT_INCR:
return CTRLSEL_ERROR;
case CONTENT_SUCCESS:
return CTRLSEL_INTERNAL;
case CONTENT_ZERO:
context->ndone++;
deltransfer(context, transfer);
break;
}
return CTRLSEL_INTERNAL;
}
int
ctrlsel_receive(CtrlSelContext *context, XEvent *xev)
{
int status;
if (xev->type == SelectionNotify)
status = receiveinit(context, xev);
else if (xev->type == PropertyNotify)
status = receiveincr(context, xev);
else
return CTRLSEL_NONE;
if (status == CTRLSEL_INTERNAL) {
if (context->ndone >= context->ntargets) {
status = CTRLSEL_RECEIVED;
goto done;
}
} else if (status == CTRLSEL_ERROR) {
freebuffers(context);
freetransferences(context);
}
done:
if (status == CTRLSEL_RECEIVED)
freetransferences(context);
return status;
}
static int
sendinit(CtrlSelContext *context, XEvent *xev)
{
XSelectionRequestEvent *xreqev;
XSelectionEvent xselev;
unsigned long natoms, i;
Atom *pairs;
Atom pair[PAIR_LAST];
Atom multiple, atom_pair;
Bool success;
xreqev = &xev->xselectionrequest;
if (xreqev->selection != context->selection)
return CTRLSEL_NONE;
multiple = XInternAtom(context->display, MULTIPLE, False);
atom_pair = XInternAtom(context->display, ATOM_PAIR, False);
xselev = (XSelectionEvent){
.type = SelectionNotify,
.display = xreqev->display,
.requestor = xreqev->requestor,
.selection = xreqev->selection,
.time = xreqev->time,
.target = xreqev->target,
.property = None,
};
if (xreqev->time != CurrentTime && xreqev->time < context->time) {
/*
* According to ICCCM, the selection owner
* should compare the timestamp with the period
* it has owned the selection and, if the time
* is outside, refuse the `SelectionRequest` by
* sending the requestor window a
* `SelectionNotify` event with the property set
* to `None` (by means of a `SendEvent` request
* with an empty event mask).
*/
goto done;
}
if (xreqev->target == multiple) {
if (xreqev->property == None)
goto done;
natoms = getatomsprop(
xreqev->display,
xreqev->requestor,
xreqev->property,
atom_pair,
&pairs
);
} else {
pair[PAIR_TARGET] = xreqev->target;
pair[PAIR_PROPERTY] = xreqev->property;
pairs = pair;
natoms = 2;
}
success = True;
for (i = 0; i < natoms; i += 2) {
if (!convert(context, xreqev->requestor,
pairs[i + PAIR_TARGET],
pairs[i + PAIR_PROPERTY])) {
success = False;
pairs[i + PAIR_PROPERTY] = None;
}
}
if (xreqev->target == multiple) {
XChangeProperty(
xreqev->display,
xreqev->requestor,
xreqev->property,
atom_pair,
32, PropModeReplace,
(unsigned char *)pairs,
natoms
);
free(pairs);
}
if (success) {
if (xreqev->property == None) {
xselev.property = xreqev->target;
} else {
xselev.property = xreqev->property;
}
}
done:
XSendEvent(
xreqev->display,
xreqev->requestor,
False,
NoEventMask,
(XEvent *)&xselev
);
return CTRLSEL_INTERNAL;
}
static int
sendlost(CtrlSelContext *context, XEvent *xev)
{
XSelectionClearEvent *xclearev;
xclearev = &xev->xselectionclear;
if (xclearev->selection == context->selection &&
xclearev->window == context->window) {
return CTRLSEL_LOST;
}
return CTRLSEL_NONE;
}
static int
senddestroy(CtrlSelContext *context, XEvent *xev)
{
struct Transfer *transfer;
XDestroyWindowEvent *xdestroyev;
xdestroyev = &xev->xdestroywindow;
for (transfer = context->transfers; transfer != NULL; transfer = transfer->next)
if (transfer->requestor == xdestroyev->window)
deltransfer(context, transfer);
return CTRLSEL_NONE;
}
static int
sendincr(CtrlSelContext *context, XEvent *xev)
{
struct Transfer *transfer;
XPropertyEvent *xpropev;
unsigned long size;
xpropev = &xev->xproperty;
if (xpropev->state != PropertyDelete)
return CTRLSEL_NONE;
for (transfer = context->transfers; transfer != NULL; transfer = transfer->next)
if (transfer->property == xpropev->atom &&
transfer->requestor == xpropev->window)
goto found;
return CTRLSEL_NONE;
found:
if (transfer->size >= transfer->target->bufsize)
transfer->size = transfer->target->bufsize;
size = transfer->target->bufsize - transfer->size;
if (size > context->selmaxsize)
size = context->selmaxsize;
XChangeProperty(
xpropev->display,
xpropev->window,
xpropev->atom,
transfer->target->target,
transfer->target->format,
PropModeReplace,
transfer->target->buffer + transfer->size,
size / nbytes(transfer->target->format)