1 ! Copyright (C) 2006 Slava Pestov
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien arrays gadgets kernel math namespaces sequences ;
6 ! This code was based on by McCLIM's Backends/CLX/port.lisp
7 ! and http://common-lisp.net/~crhodes/clx/demo/clipboard.lisp.
9 TUPLE: x-clipboard atom contents ;
11 C: x-clipboard ( atom -- clipboard )
12 [ set-x-clipboard-atom ] keep
13 "" over set-x-clipboard-contents ;
15 : selection-property ( -- n )
16 "org.factorcode.Factor.SELECTION" x-atom ;
18 : convert-selection ( win selection -- )
19 swap >r >r dpy get r> XA_STRING selection-property r>
20 CurrentTime XConvertSelection drop ;
22 : snarf-property ( prop-return -- string )
23 dup *void* [ *char* ] [ drop f ] if ;
25 : window-property ( win prop delete? -- string )
26 >r dpy get -rot 0 -1 r> AnyPropertyType
27 0 <Atom> 0 <int> 0 <ulong> 0 <ulong> f <void*>
28 [ XGetWindowProperty drop ] keep snarf-property ;
30 : selection-from-event ( event window -- string )
31 >r XSelectionEvent-property zero? [
34 r> selection-property 1 window-property
37 : own-selection ( prop win -- )
38 dpy get -rot CurrentTime XSetSelectionOwner drop
41 : clipboard-for-atom ( atom -- clipboard )
43 { [ dup XA_PRIMARY = ] [ drop selection get ] }
44 { [ dup "CLIPBOARD" x-atom = ] [ drop clipboard get ] }
45 { [ t ] [ drop <clipboard> ] }
48 : set-selection-prop ( evt -- )
50 [ XSelectionRequestEvent-requestor ] keep
51 [ XSelectionRequestEvent-property ] keep
52 >r XA_STRING 8 PropModeReplace r>
53 XSelectionRequestEvent-selection
54 clipboard-for-atom x-clipboard-contents
55 dup string>char-alien swap length XChangeProperty drop ;
57 : set-targets-prop ( evt -- )
59 [ XSelectionRequestEvent-requestor ] keep
60 XSelectionRequestEvent-property
61 "TARGETS" x-atom 32 PropModeReplace
62 { "STRING" "TARGETS" "TIMESTAMP" } [ x-atom ] map >int-array
63 32 XChangeProperty drop ;
65 : set-timestamp-prop ( evt -- )
67 [ XSelectionRequestEvent-requestor ] keep
68 [ XSelectionRequestEvent-property ] keep
69 >r "TIMESTAMP" x-atom 32 PropModeReplace r>
70 XSelectionRequestEvent-time 1array >int-array
71 32 XChangeProperty drop ;
73 : send-notify ( evt prop -- )
74 "XSelectionEvent" <c-object>
75 SelectionNotify over set-XSelectionEvent-type
76 [ set-XSelectionEvent-property ] keep
77 over XSelectionRequestEvent-display over set-XSelectionEvent-display
78 over XSelectionRequestEvent-requestor over set-XSelectionEvent-requestor
79 over XSelectionRequestEvent-selection over set-XSelectionEvent-selection
80 over XSelectionRequestEvent-target over set-XSelectionEvent-target
81 over XSelectionRequestEvent-time over set-XSelectionEvent-time
82 >r dpy get swap XSelectionRequestEvent-requestor 0 0 r>
86 : send-notify-success ( evt -- )
87 dup XSelectionRequestEvent-property send-notify ;
89 : send-notify-failure ( evt -- )
92 : x-clipboard@ ( gadget clipboard -- prop win )
93 x-clipboard-atom swap find-world world-handle first ;
95 M: x-clipboard copy-clipboard
96 [ x-clipboard@ own-selection ] keep
97 set-x-clipboard-contents ;
99 M: x-clipboard paste-clipboard
100 >r find-world world-handle first r> x-clipboard-atom
103 : init-clipboard ( -- )
104 XA_PRIMARY <x-clipboard> selection set-global
105 "CLIPBOARD" x-atom <x-clipboard> clipboard set-global ;