1 ! Copyright (C) 2006, 2007 Slava Pestov
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types alien.strings classes.struct
4 io.encodings.utf8 kernel namespaces sequences
5 specialized-arrays.int x11 x11.constants x11.xlib ;
8 ! This code was based on by McCLIM's Backends/CLX/port.lisp
9 ! and http://common-lisp.net/~crhodes/clx/demo/clipboard.lisp.
11 : XA_CLIPBOARD ( -- atom ) "CLIPBOARD" x-atom ;
13 : XA_UTF8_STRING ( -- atom ) "UTF8_STRING" x-atom ;
15 TUPLE: x-clipboard atom contents ;
17 : <x-clipboard> ( atom -- clipboard )
20 : selection-property ( -- n )
21 "org.factorcode.Factor.SELECTION" x-atom ;
23 : convert-selection ( win selection -- )
24 swap [ [ dpy get ] dip XA_UTF8_STRING selection-property ] dip
25 CurrentTime XConvertSelection drop ;
27 : snarf-property ( prop-return -- string )
28 dup *void* [ *void* utf8 alien>string ] [ drop f ] if ;
30 : window-property ( win prop delete? -- string )
31 [ [ dpy get ] 2dip 0 -1 ] dip AnyPropertyType
32 0 <Atom> 0 <int> 0 <ulong> 0 <ulong> f <void*>
33 [ XGetWindowProperty drop ] keep snarf-property ;
35 : selection-from-event ( event window -- string )
37 [ drop f ] [ selection-property 1 window-property ] if ;
39 : own-selection ( prop win -- )
40 [ dpy get ] 2dip CurrentTime XSetSelectionOwner drop
43 : set-targets-prop ( evt -- )
44 [ dpy get ] dip [ requestor>> ] [ property>> ] bi
45 "TARGETS" x-atom 32 PropModeReplace
47 "UTF8_STRING" "STRING" "TARGETS" "TIMESTAMP"
48 } [ x-atom ] int-array{ } map-as
49 4 XChangeProperty drop ;
51 : set-timestamp-prop ( evt -- )
54 [ property>> "TIMESTAMP" x-atom 32 PropModeReplace ]
56 1 XChangeProperty drop ;
58 : send-notify ( evt prop -- )
59 XSelectionEvent <struct>
60 SelectionNotify >>type
62 over display>> >>display
63 over requestor>> >>requestor
64 over selection>> >>selection
65 over target>> >>target
67 [ [ dpy get ] dip requestor>> 0 0 ] dip
71 : send-notify-success ( evt -- )
72 dup property>> send-notify ;
74 : send-notify-failure ( evt -- )