]> gitweb.factorcode.org Git - factor.git/blob - basis/x11/clipboard/clipboard.factor
Specialized array overhaul
[factor.git] / basis / x11 / clipboard / clipboard.factor
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 x11 x11.constants x11.xlib ;
6 SPECIALIZED-ARRAY: int
7 IN: x11.clipboard
8
9 ! This code was based on by McCLIM's Backends/CLX/port.lisp
10 ! and http://common-lisp.net/~crhodes/clx/demo/clipboard.lisp.
11
12 : XA_CLIPBOARD ( -- atom ) "CLIPBOARD" x-atom ;
13
14 : XA_UTF8_STRING ( -- atom ) "UTF8_STRING" x-atom ;
15
16 TUPLE: x-clipboard atom contents ;
17
18 : <x-clipboard> ( atom -- clipboard )
19     "" x-clipboard boa ;
20
21 : selection-property ( -- n )
22     "org.factorcode.Factor.SELECTION" x-atom ;
23
24 : convert-selection ( win selection -- )
25     swap [ [ dpy get ] dip XA_UTF8_STRING selection-property ] dip
26     CurrentTime XConvertSelection drop ;
27
28 : snarf-property ( prop-return -- string )
29     dup *void* [ *void* utf8 alien>string ] [ drop f ] if ;
30
31 : window-property ( win prop delete? -- string )
32     [ [ dpy get ] 2dip 0 -1 ] dip AnyPropertyType
33     0 <Atom> 0 <int> 0 <ulong> 0 <ulong> f <void*>
34     [ XGetWindowProperty drop ] keep snarf-property ;
35
36 : selection-from-event ( event window -- string )
37     swap property>> 0 =
38     [ drop f ] [ selection-property 1 window-property ] if ;
39
40 : own-selection ( prop win -- )
41     [ dpy get ] 2dip CurrentTime XSetSelectionOwner drop
42     flush-dpy ;
43
44 : set-targets-prop ( evt -- )
45     [ dpy get ] dip [ requestor>> ] [ property>> ] bi
46     "TARGETS" x-atom 32 PropModeReplace
47     {
48         "UTF8_STRING" "STRING" "TARGETS" "TIMESTAMP"
49     } [ x-atom ] int-array{ } map-as
50     4 XChangeProperty drop ;
51
52 : set-timestamp-prop ( evt -- )
53     [ dpy get ] dip
54     [ requestor>> ]
55     [ property>> "TIMESTAMP" x-atom 32 PropModeReplace ]
56     [ time>> <int> ] tri
57     1 XChangeProperty drop ;
58
59 : send-notify ( evt prop -- )
60     XSelectionEvent <struct>
61     SelectionNotify >>type
62     swap >>property
63     over display>>   >>display
64     over requestor>> >>requestor
65     over selection>> >>selection
66     over target>>    >>target
67     over time>>      >>time
68     [ [ dpy get ] dip requestor>> 0 0 ] dip
69     XSendEvent drop
70     flush-dpy ;
71
72 : send-notify-success ( evt -- )
73     dup property>> send-notify ;
74
75 : send-notify-failure ( evt -- )
76     0 send-notify ;