-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctrlsel.3
879 lines (870 loc) · 21.7 KB
/
ctrlsel.3
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
.Dd March 6, 2023
.Dt CTRLSEL 3
.Os
.Sh NAME
.Nm ctrlsel_filltarget ,
.Nm ctrlsel_request ,
.Nm ctrlsel_receive ,
.Nm ctrlsel_cancel ,
.Nm ctrlsel_setowner ,
.Nm ctrlsel_send ,
.Nm ctrlsel_disown ,
.Nm ctrlsel_dndwatch ,
.Nm ctrlsel_dndreceive ,
.Nm ctrlsel_dndclose ,
.Nm ctrlsel_dndown ,
.Nm ctrlsel_dndsend ,
.Nm ctrlsel_dnddisown
.Nd X11 selection ownership and request helper functions
.Sh SYNOPSIS
.In "X11/Xlib.h"
.In "control/ctrlsel.h"
.Ft void
.Fo "ctrlsel_filltarget"
.Fa "Atom target"
.Fa "Atom type"
.Fa "int format"
.Fa "unsigned char *buffer"
.Fa "unsigned long size"
.Fa "struct CtrlSelTarget *target_fill"
.Fc
.Ft "CtrlSelContext *"
.Fo "ctrlsel_request"
.Fa "Display *display"
.Fa "Window window"
.Fa "Atom selection"
.Fa "Time time"
.Fa "struct CtrlSelTarget targets[]"
.Fa "unsigned long ntargets"
.Fc
.Ft int
.Fo "ctrlsel_receive"
.Fa "CtrlSelContext *context"
.Fa "XEvent *event"
.Fc
.Ft void
.Fo "ctrlsel_cancel"
.Fa "CtrlSelContext *context"
.Fc
.Ft "CtrlSelContext *"
.Fo "ctrlsel_setowner"
.Fa "Display *display"
.Fa "Window window"
.Fa "Atom selection"
.Fa "Time time"
.Fa "int ismanager"
.Fa "struct CtrlSelTarget targets[]"
.Fa "unsigned long ntargets"
.Fc
.Ft int
.Fo "ctrlsel_send"
.Fa "CtrlSelContext *context"
.Fa "XEvent *event"
.Fc
.Ft void
.Fo "ctrlsel_disown"
.Fa "CtrlSelContext *context"
.Fc
.Ft "CtrlSelContext *"
.Fo "ctrlsel_dndwatch"
.Fa "Display *display"
.Fa "Window window"
.Fa "unsigned int actions"
.Fa "struct CtrlSelTarget targets[]"
.Fa "unsigned long ntargets"
.Fc
.Ft int
.Fo "ctrlsel_dndreceive"
.Fa "CtrlSelContext *"
.Fa "XEvent *event"
.Fc
.Ft void
.Fo "ctrlsel_dndclose"
.Fa "CtrlSelContext *context"
.Fc
.Ft int
.Fo "ctrlsel_dndown"
.Fa "Display *display"
.Fa "Window window"
.Fa "Window miniature"
.Fa "Time time"
.Fa "struct CtrlSelTarget targets[]"
.Fa "unsigned long ntargets"
.Fa "CtrlSelContext **context_ret"
.Fc
.Ft int
.Fo "ctrlsel_dndsend"
.Fa "CtrlSelContext *context"
.Fa "XEvent *event"
.Fc
.Ft void
.Fo "ctrlsel_dnddisown"
.Fa "CtrlSelContext *context"
.Fc
.Sh ARGUMENTS
.Bl -tag -width Ds
.It Fa "actions"
Specifies the supported drag-and-drop actions.
Its value should be a bitwise OR of supported actions:
.Ic "CTRLSEL_COPY" ,
.Ic "CTRLSEL_MOVE" ,
.Ic "CTRLSEL_LINK" ,
.Ic "CTRLSEL_ASK" ,
or
.Ic "CTRLSEL_PRIVATE" .
.It Fa "buffer"
Specifies the data of the selection in the target.
If the
.Fa "target_fill"
argument is for a selection to be owned,
.Fa buffer
should exist (not be freed) until the selection is lost or disowned.
If the
.Fa "target_fill"
argument is for a selection to be requested,
then any value (such as
.Ic "NULL" )
can be given, for the selection content is yet to be requested.
.It Fa context
Specifies the pointer to a
.Ft CtrlSelContext
structure containing the context of the selection request or ownership.
.It Fa context_ret
Returns the context for the drag-and-drop operation.
.It Fa "display"
Specifies the connection to the X server.
.It Fa "event"
Specifies the pointer to a
.Ft "XEvent"
to be filtered.
.It Fa "format"
Specifies the format of each member of the selection content converted into the target.
It should be either
.Ic "8"
.Pq Ft "char" ,
.Ic "16"
.Pq Ft "short" ,
or
.Ic "32"
.Pq Ft "long" .
This argument need only be set when the
.Fa "target_fill"
argument is for a selection to be owned (given to
.Fn "ctrlsel_setowner" ) ,
for the content type is already known.
If
.Fa "target_fill"
is for a selection to be requested (that is, given to
.Fn "ctrlsel_request" ) ,
then any value (such as
.Ic "0" )
can be given,
for the format is not known yet.
.It Fa "ismanager"
Specifies whether the selection whose ownership is being asked by
.Fn "ctrlsel_setowner"
is a manager selection.
.It Fa "miniature"
Specifies the window of the miniature (aka "icon") to follow the mouse pointer during a drag-and-drop (aka "dnd") operation.
This argument can be
.Ic "None" ,
in which case no miniature/icon will follow the pointer;
but that is discouraged,
for the user may not have a visual response of the drag-and-drop, if the mouse cursor theme does not support the dragging cursor.
.It Fa "ntargets"
Specifies the number of members of the
.Fa targets
array.
.It Fa "selection"
Specifies the atom identifying the requested selection (for
.Fn "ctrlsel_request" ) ,
or the selection to be owned (for
.Fn "ctrlsel_setowner" ) .
.It Fa "size"
Specifies the size, in bytes, of
.Fa "buffer" .
Ignored if the target is to be requested.
.It Fa "target"
Specifies the atom identifying a target which the selection can be converted to.
.It Fa "targets"
Specifies a pointer of
.Fa "ntargets"
structures of type
.Ft "struct CtrlSelTarget"
to be included in the
.Fa "context" .
.Fa "targets"
must be accessible (not freed) until the selection request or ownership is done;
that is, until
.Fn "ctrlsel_receive"
returns
.Ic "CTRLSEL_ERROR"
or
.Fn "ctrlsel_cancel"
is called, for selection requests; or until
.Fn "ctrlsel_send"
returns
.Ic "CTRLSEL_LOST"
or
.Fn "ctrlsel_disown"
is called, for selection ownerships.
.It Fa "target_fill"
Specifies the pointer to a
.Ft "struct CtrlSelTarget"
structure whose members will be defined.
.It Fa "time"
specifies the timestamp of the selection request (for
.Fn "ctrlsel_request" )
or the selection ownership (for
.Fn "ctrlsel_setowner" ) .
.It Fa "type"
Specifies the atom identifying the type of the content of the selection in the target.
It is usually the same atom as the target.
This argument need only be set when the
.Fa "target_fill"
argument is for a selection to be owned (that is, given to
.Fn "ctrlsel_setowner" ) ,
for the content is already converted into the target.
If
.Fa "target_fill"
is for a selection to be requested (that is, given to
.Fn "ctrlsel_request" ) ,
then any value (such as
.Ic "None" )
can be given, for the type is not known yet.
.It Fa "window"
Specifies the window requesting (for
.Fn "ctrlsel_request" )
or willing to own (for
.Fn "ctrlsel_setowner" )
the given
.Fa "selection" .
.El
.Sh DESCRIPTION
These functions and related data structures provide management of ownership and request of X11 selections, including for drag-and-drop operations.
In addition to the
.Fn "ctrlsel_filltarget"
function (used to set up
.Ft "struct CtrlSelTarget"
structures,
there are the following four triplets of functions that deal with the ownership and request of arbitrary selections and the drag-and-drop selection:
.Bl -bullet
.It
The
.Fn "ctrlsel_request" ,
.Fn "ctrlsel_receive" ,
and
.Fn "ctrlsel_cancel"
functions request the contents of a selection,
receive the requested contents,
and cancel the request, respectively.
The
.Fn "ctrlsel_receive"
and
.Fn "ctrlsel_cancel"
functions must only be called after
.Fn "ctrlsel_request"
with the
.Fa context
returned by it.
.It
The
.Fn "ctrlsel_setowner" ,
.Fn "ctrlsel_send" ,
and
.Fn "ctrlsel_disown"
functions make a window the owner of a selection,
send the selection contents to requestor clients,
and disown the selection ownership, respectively.
The
.Fn "ctrlsel_send"
and
.Fn "ctrlsel_disown"
functions must only be called after
.Fn "ctrlsel_setowner"
with the
.Fa context
returned by it.
.It
The
.Fn "ctrlsel_dndwatch" ,
.Fn "ctrlsel_dndreceive" ,
and
.Fn "ctrlsel_dndclose"
functions initiate the watching for dropped contents on a window,
receive dropped contents,
and cancel the watching, respectively.
The
.Fn "ctrlsel_dndreceive"
and
.Fn "ctrlsel_dndclose"
functions must only be called after
.Fn "ctrlsel_dndwatch"
with the
.Fa context
returned by it.
.It
The
.Fn "ctrlsel_dndown" ,
.Fn "ctrlsel_dndsend" ,
and
.Fn "ctrlsel_dnddisown"
functions initiate a drag-and-drop operation,
send the dropped content to a watching window,
and interrupts the drag-and-drop operation, respectively.
The
.Fn "ctrlsel_dndsend"
and
.Fn "ctrlsel_dnddisown"
functions must only be called after
.Fn "ctrlsel_dndown"
with the
.Fa context
returned by it.
.El
.Pp
The
.Fn "ctrlsel_filltarget"
function fills in the members of the
.Fa "target_fill"
structure with the values given in as arguments and other values computed from those.
.Fa "target_fill"
could be initialized or assigned manually by the programmer; however it is recommended to use the
.Fn ctrlsel_filltarget
function instead because it both provides compile-time checking of whether all the members have been properly assigned,
and also compute and defines the
.Fn "nitems"
member, which is derived from the other ones.
.Pp
The
.Fn "ctrlsel_request"
function asks the
.Xr Xserver 1
represented by
.Fa "display"
to convert the
.Fa "selection"
into all the
.Fa "targets"
for the
.Fa "window" .
It returns a pointer to a
.Ft CtrlSelContext
to be passed to the
.Fn "ctrlsel_receive"
and
.Fn "ctrlsel_cancel"
functions (therefore, those functions must have access to the
.Fa "targets"
argument).
It returns NULL on error.
.Pp
The
.Fn "ctrlsel_receive"
function filters the X event pointed to by
.Fa "event" ,
checks whether it is related to the
.Fa context
returned by
.Fn "ctrlsel_request" ,
and receives part or all of the content of the requested selection into the requested targets.
One of the following enum constants is returned:
.Bl -tag -width Ds
.It Ic "CTRLSEL_NONE"
The event is not related to the request.
The caller can further process the event.
.It Ic "CTRLSEL_RECEIVED"
The selection have been successfully converted into all the targets.
All the
.Fa "buffer"
members of the
.Ft "struct CtrlSelTarget"
structures in the array given to
.Fn "ctrlsel_request"
have been allocated and set to the selection content in that target.
The
.Fa "bufsize" ,
.Fa "format" ,
.Fa "nitems" ,
and
.Fa "type"
members are set to the size in bytes of the content, format of the
content, number of items and type of the content, respectively.
The caller should not further process the event.
The caller can then use the
.Fa "buffer" ,
and must
.Xr free 3
it when done with it.
.It Ic CTRLSEL_INTERNAL
The selection has been partially converted into the targets.
The caller should not further process the event.
.It Ic CTRLSEL_ERROR
An error has occurred during the selection conversion.
The caller should not further process the event.
Any allocated
.Fa "buffer"
is freed by the function, so the caller should not free it.
.El
.Pp
The
.Fn "ctrlsel_cancel"
function cancels the selection conversion represented by the
.Fa "context" ,
terminates any incremental transference in progress,
and frees the context itself and any allocated buffer.
.Pp
The
.Fn "ctrlsel_setowner"
function asks the
.Xr Xserver 1
represented to by
.Fa "display"
to own the
.Fa "selection"
for the
.Fa "window" ,
so it can provide the contents of all
.Fa "targets" .
If the
.Fa "selection"
is a manager selection, the
.Fa "ismanager"
argument must be set to nonzero.
It returns a pointer to a
.Ft CtrlSelContext
to be passed to the
.Fn "ctrlsel_send"
and
.Fn "ctrlsel_disown"
functions (therefore, those functions must have access to the
.Fa "targets"
argument).
It returns NULL on error.
.Pp
The
.Fn "ctrlsel_send"
function filters the X event pointed to by
.Fa "event" ,
checks whether it is related to the
.Fa context
returned by
.Fn "ctrlsel_setowner" ,
and sends the converted selection to any requestor client.
One of the following enum constants is returned:
.Bl -tag -width Ds
.It Ic "CTRLSEL_NONE"
The event is not related to the ownership.
The caller can further process the event.
.It Ic "CTRLSEL_INTERNAL"
The selection is converted to a requestor client who have requested the selection.
The caller should not further process the event.
.It Ic "CTRLSEL_LOST"
The selection ownership has been lost.
The caller should not further process the event.
.El
.Pp
The
.Fn "ctrlsel_disown"
function cancels the selection ownership represented by the
.Fa "context" ,
terminates any incremental transference in progress, and frees the context itself.
This function does not free any buffer.
.Pp
The
.Fn "ctrlsel_dndwatch"
function begins watching dropped content on
.Fa "window"
in the
.Xr Xserver 1
represented by
.Fa display .
The content must be on the specified
.Fa "targets"
and must be dropped by the specified
.Fa "actions" .
It returns a pointer to a
.Ft CtrlSelContext
to be passed to the
.Fn "ctrlsel_dndreceive"
and
.Fn "ctrlsel_dndclose"
functions
(therefore, those functions must have access to the
.Fa "targets"
argumnent).
It returns NULL on error.
This function should be called once, at program initialization.
.Pp
The
.Fn "ctrlsel_dndreceive"
function filters the X event pointed to by
.Fa "event" ,
checks whether it is related to the
.Fa context
returned by
.Fn ctrlsel_dndwatch ,
and receives part or all of the dropped content.
One of the following enum constants is returned:
.Bl -tag -width Ds
.It Ic CTRLSEL_NONE
The event is not related to the drop watch.
The caller can further process the event.
.It Ic CTRLSEL_RECEIVED
A content has been dropped into one of the targets.
The
.Fa buffer
member of one of the
.Ft struct CtrlSelTarget
structures in the array given to
.Fn ctrlsel_dndwatch
has been allocated and set to the dropped content in that target.
The
.Fa bufsize ,
.Fa format ,
.Fa nitems ,
.Fa type ,
and
.Fa action
members are set to the size in bytes of the content,
format of the content,
number of items,
type of the content, and
action that resulted on the drop, respectively.
The caller should not further process the event.
The caller can then use the
.Fa buffer ,
and must
.Xr free 3
it when done with it.
.It Ic CTRLSEL_INTERNAL
A drop has been partially converted into a target.
The caller should not further process the event.
.El
.Pp
The
.Fn ctrlsel_dndclose
function cancels the watch for drops represented by
.Fa context ,
terminates any incremental transference in progress,
and frees the context itself and any allocated buffer.
.Pp
The
.Fn ctrlsel_dndown
function asks the
.Xr Xserver 1
represented to by
.Fa display
to own the drag-and-drop selection for
.Fa window ,
so it can provide the contents of
.Fa targets .
This function blocks until the user drops the dragged content (by releasing a mouse button),
or cancels the drag-and-drop operation (by pressing ESC).
If the given
.Fa miniature
window is not
.Ic None ,
the window is mapped, raised and moved around to follow the user's mouse pointer.
It saves into
.Fa context_ret
a pointer to
.Ft CtrlSelContext
with data to be passed to the
.Fn ctrlsel_dndsend
and
.Fn ctrlsel_dnddisown
functions.
One of the following enum constants is returned:
.Bl -tag -width Ds
.It Ic CTRLSEL_ERROR
The drag-and-drop selection could not be owned.
The drag-and-drop operation is over.
.It Ic CTRLSEL_NONE
The content was dropped on a non-watching window.
The drag-and-drop operation is over.
.It Ic CTRLSEL_DROPSELF
The content was dropped on the window that initiated the drag-and-drop operation.
The drag-and-drop operation is over.
.It Ic CTRLSEL_DROPOTHER
The content was dropped on another window watching for content drops.
The drag-and-drop operation is not over, the actual content must be sent with
.Fn ctrlsel_dndsend
or the drag-and-drop operation must be cancelled with
.Fn ctrlsel_dnddisown .
.El
.Pp
The
.Fn ctrlsel_dndsend
function
filters the X event pointed to by
.Fa event ,
checks whether it is related to the
.Fa context
returned by
.Fn ctrlsel_dndown ,
and sends the dropped content to the window it was dropped on.
One of the following enum constants is returned:
.Bl -tag -width Ds
.It Ic CTRLSEL_NONE
The event is not related to the drag-and-drop operation.
The caller can further process the event.
.It Ic CTRLSEL_LOST
The drag-and-drop ownership has been lost.
The caller should not further process the event.
.It Ic CTRLSEL_SENT
The content was fully sent to the window it was dropped on.
The caller should not further process the event.
.It Ic CTRLSEL_INTERNAL
The content is being sent to the window it was dropped on.
The caller should not further process the event.
.El
.Pp
The
.Fn ctrlsel_dnddisown
function cancels the transference of the dropped content represented by the
.Fa context ,
and frees the context itself.
This function does not free any buffer.
.Sh STRUCTURES
The
.Ft "struct CtrlSelTarget"
structure contains:
.Bd -literal -offset indent
struct CtrlSelTarget {
Atom target;
Atom type;
int format;
unsigned int action;
unsigned long nitems;
unsigned long bufsize;
unsigned char *buffer;
};
.Ed
.Pp
A
.Ft "struct CtrlSelTarget"
structure
could be set manually by the programmer; however it is recommended to use the
.Fn ctrlsel_filltarget
as explained earlier.
The structure members must be as follows:
.Bl -tag -width Ds
.It Fa "action"
The action that resulted on the drop of a content by a drag-and-drop operation.
Its value is equal to one of the supported actions:
.Ic "CTRLSEL_COPY" ,
.Ic "CTRLSEL_MOVE" ,
.Ic "CTRLSEL_LINK" ,
.Ic "CTRLSEL_ASK" ,
or
.Ic "CTRLSEL_PRIVATE" .
This member is only set by the
.Fn ctrlsel_dndreceive
function.
.It Fa "target"
The atom identifying the target.
Its value is either defined in
.In "X11/Xatom.h"
or obtained from the
.Xr XInternAtom 3
or
.Xr XInternAtoms 3
functions.
.It Fa "type"
The atom identifying the type of the target content.
Its value is usually the same as
.Fa "target" ,
but not always (it depends on the target).
Its value is either defined in
.In "X11/Xatom.h"
or obtained from the
.Xr XInternAtom 3
or
.Xr XInternAtoms 3
functions.
.It Fa "format"
The format of each member of the target content.
It should be
.Ic "8"
.Pq Ft "char"
for most
.Fa types
of targets.
Exceptions are types like
.Ic "XA_ATOM"
and
.Ic "XA_WINDOW" ,
which require the
.Ic "32"
.Pq Ft "long"
format; and types for width, height or RGB color values, which require the
.Ic "16"
.Pq Ft "short"
format.
.It Fa "nitems"
The number of items in
.Fa "buffer"
according to the target
.Fa "format".
If format is
.Ic "8" ,
its value is the same as
.Fa "bufsize" .
If the format is
.Ic "16"
or
.Ic "32" ,
it is the number of
.Ft "short"
or
.Ft "long"
(either signed or unsigned) elements in the buffer, respectively;
that is, it is equal to
.Fa bufsize
divided by
.Ic "sizeof(short)"
or
.Ic "sizeof(long)" .
.It Fa "bufsize"
The size in bytes of
.Fa "buffer" .
.It Fa "buffer"
The buffer containing the selection content converted into
.Fa "target" .
.El
.Sh RETURN VALUES
Upon error,
the
.Fn "ctrlsel_request"
and
.Fn "ctrlsel_setowner"
functions return zero; and the
.Fn "ctrlsel_receive"
function returns
.Ic "CTRLSEL_ERROR" .
.Sh EXAMPLES
The following is an example of the implementation of a function requesting the contents of the primary selection into two targets.
The contents of the selection must be received in a loop because the content can be too long for a single transference.
If the request is triggered by an event (for example, the user pressed the mouse's middle button), the variable
.Fa "time"
must be the timestamp of the event that caused it
(for example, in a
.Xr XButtonEvent 3
event).
If the request is not triggered by an event, the value
.Ic "CurrentTime"
must be given instead.
.Bd -literal -offset indent
CtrlSelContext *context;
struct CtrlSelTarget targets[2];
XEvent event;
Display *display;
Window window;
Atom utf8, html;
Time time;
/* step 0: fill targets */
utf8 = XInternAtom(display, "UTF8_STRING", False);
html = XInternAtom(display, "text/html", False);
ctrlsel_filltarget(utf8, utf8, 0, NULL, 0, &targets[0]);
ctrlsel_filltarget(html, html, 0, NULL, 0, &targets[1]);
/* step 1: request selection contents */
context = ctrlsel_request(display, window, XA_PRIMARY,
time, targets, 2);
if (context == NULL) {
fprintf(stderr, "could not request selection");
return;
}
/* step 2: receive selection contents */
for (;;) {
(void)XNextEvent(display, &event);
switch (ctrlsel_receive(context, &event)) {
case CTRLSEL_NONE:
/* call event handler */
break;
case CTRLSEL_RECEIVED:
goto done;
case CTRLSEL_INTERNAL:
continue;
case CTRLSEL_ERROR:
fprintf(stderr, "warning: could not get selection");
return;
}
}
/* step 3: process selection contents; and free them */
done:
printf("plain text: %.*s\n",
targets[0].bufsize,
targets[0].buffer);
printf("html text: %.*s\n",
targets[1].bufsize,
targets[1].buffer);
free(targets[0].buffer);
free(targets[1].buffer);
/* step 4: free the selection context */
ctrlsel_cancel(context);
context = NULL;
.Ed
.Pp
The following is an example of the implementation of a function asking for the ownership of the primary selection with two targets.
The contents of the selection must be sent to requestors in a loop,
because other clients can request the selection asynchronously at any time.
If the ownership is triggered by an event (for example, the user selected a text with the mouse), the variable
.Fa time
must be the timestamp of the event that caused it (for example, in a
.Xr XButtonEvent 3
event).
If the ownership is not triggered by an event, the value
.Ic CurrentTime
must be given instead.
.Bd -literal -offset indent
CtrlSelContext *context;
struct CtrlSelTarget targets[2];
XEvent event;
Display *display;
Window window;
Atom utf8, html;
char *plain, *markd;
Time time;
/* step 0: fill targets */
plain = "Hello World!"
markd = "<p>Hello <i>World</i>!</p>"
utf8 = XInternAtom(display, "UTF8_STRING", False);
html = XInternAtom(display, "text/html", False);
ctrlsel_filltarget(utf8, utf8, 8, plain, strlen(plain), &targets[0]);
ctrlsel_filltarget(html, html, 8, markd, strlen(markd), &targets[1]);
/* step 1: ask for selection ownership */
context = ctrlsel_setowner(display, window, XA_PRIMARY,
time, 0, targets, 2);
if (context == NULL) {
fprintf(stderr, "warning: could not own selection");
return;
}
/* step 2: provide selection contents */
for (;;) {
(void)XNextEvent(display, &event);
switch (ctrlsel_send(context, &event)) {
case CTRLSEL_NONE:
/* call event handler */
break;
case CTRLSEL_INTERNAL:
continue;
case CTRLSEL_LOST:
return;
}
}
/* step 3: free the contents */
ctrlsel_disown(context);
context = NULL;
.Ed
.Sh SEE ALSO
.Xr X 1 ,
.Xr xclipd 1
.Rs
.%Q "X Consortium Standard"
.%B "Xlib - C Language X Interface"
.Re
.Rs
.%Q "X Consortium Standard"
.%B "Inter-Client Communication Conventions Manual"
.Re