m_copypaste.c revision 1.1 1 1.1 christos /*-
2 1.1 christos * Copyright (c) 1996
3 1.1 christos * Rob Zimmermann. All rights reserved.
4 1.1 christos * Copyright (c) 1996
5 1.1 christos * Keith Bostic. All rights reserved.
6 1.1 christos *
7 1.1 christos * See the LICENSE file for redistribution information.
8 1.1 christos */
9 1.1 christos
10 1.1 christos #include "config.h"
11 1.1 christos
12 1.1 christos #ifndef lint
13 1.1 christos static const char sccsid[] = "Id: m_copypaste.c,v 8.10 2003/11/05 17:09:59 skimo Exp (Berkeley) Date: 2003/11/05 17:09:59 ";
14 1.1 christos #endif /* not lint */
15 1.1 christos
16 1.1 christos /* ICCCM Cut and paste Utilities: */
17 1.1 christos
18 1.1 christos #include <sys/types.h>
19 1.1 christos #include <sys/queue.h>
20 1.1 christos
21 1.1 christos #include <X11/X.h>
22 1.1 christos #include <X11/Intrinsic.h>
23 1.1 christos #include <X11/Xatom.h>
24 1.1 christos
25 1.1 christos #include <bitstring.h>
26 1.1 christos #include <stdio.h>
27 1.1 christos
28 1.1 christos #undef LOCK_SUCCESS
29 1.1 christos #include "../common/common.h"
30 1.1 christos #include "../ipc/ip.h"
31 1.1 christos #include "m_motif.h"
32 1.1 christos
33 1.1 christos typedef int (*PFI)();
34 1.1 christos
35 1.1 christos static PFI icccm_paste,
36 1.1 christos icccm_copy,
37 1.1 christos icccm_clear,
38 1.1 christos icccm_error;
39 1.1 christos
40 1.1 christos /*
41 1.1 christos * InitCopyPaste --
42 1.1 christos *
43 1.1 christos * PUBLIC: void __vi_InitCopyPaste
44 1.1 christos * PUBLIC: __P((int (*)(), int (*)(), int (*)(), int (*)()));
45 1.1 christos */
46 1.1 christos void
47 1.1 christos __vi_InitCopyPaste(PFI f_copy, PFI f_paste, PFI f_clear, PFI f_error)
48 1.1 christos {
49 1.1 christos icccm_paste = f_paste;
50 1.1 christos icccm_clear = f_clear;
51 1.1 christos icccm_copy = f_copy;
52 1.1 christos icccm_error = f_error;
53 1.1 christos }
54 1.1 christos
55 1.1 christos
56 1.1 christos #if defined(__STDC__)
57 1.1 christos static void peekProc( Widget widget,
58 1.1 christos void *data,
59 1.1 christos Atom *selection,
60 1.1 christos Atom *type,
61 1.1 christos void *value,
62 1.1 christos unsigned long *length,
63 1.1 christos int *format
64 1.1 christos )
65 1.1 christos #else
66 1.1 christos static void peekProc( widget, data, selection, type, value, length, format )
67 1.1 christos Widget widget;
68 1.1 christos void *data;
69 1.1 christos Atom *selection, *type;
70 1.1 christos void *value;
71 1.1 christos unsigned long *length;
72 1.1 christos int *format;
73 1.1 christos #endif
74 1.1 christos {
75 1.1 christos if ( *type == 0 )
76 1.1 christos (*icccm_error)( stderr, "Nothing in the primary selection buffer");
77 1.1 christos else if ( *type != XA_STRING )
78 1.1 christos (*icccm_error)( stderr, "Unknown type return from selection");
79 1.1 christos else
80 1.1 christos XtFree( value );
81 1.1 christos }
82 1.1 christos
83 1.1 christos
84 1.1 christos #if 0
85 1.1 christos #if defined(__STDC__)
86 1.1 christos void _vi_AcquireClipboard( Widget wid )
87 1.1 christos #else
88 1.1 christos void _vi_AcquireClipboard( wid )
89 1.1 christos Widget wid;
90 1.1 christos #endif
91 1.1 christos {
92 1.1 christos XtGetSelectionValue( wid,
93 1.1 christos XA_PRIMARY,
94 1.1 christos XA_STRING,
95 1.1 christos (XtSelectionCallbackProc) peekProc,
96 1.1 christos NULL,
97 1.1 christos XtLastTimestampProcessed( XtDisplay(wid) )
98 1.1 christos );
99 1.1 christos }
100 1.1 christos #endif
101 1.1 christos
102 1.1 christos
103 1.1 christos #if defined(__STDC__)
104 1.1 christos static void loseProc( Widget widget )
105 1.1 christos #else
106 1.1 christos static void loseProc( widget )
107 1.1 christos Widget widget;
108 1.1 christos #endif
109 1.1 christos {
110 1.1 christos /* we have lost ownership of the selection. clear it */
111 1.1 christos (*icccm_clear)( widget );
112 1.1 christos
113 1.1 christos /* also participate in the protocols */
114 1.1 christos XtDisownSelection( widget,
115 1.1 christos XA_PRIMARY,
116 1.1 christos XtLastTimestampProcessed( XtDisplay(widget) )
117 1.1 christos );
118 1.1 christos }
119 1.1 christos
120 1.1 christos
121 1.1 christos #if defined(__STDC__)
122 1.1 christos static int convertProc( Widget widget,
123 1.1 christos Atom *selection,
124 1.1 christos Atom *target,
125 1.1 christos Atom *type,
126 1.1 christos void **value,
127 1.1 christos int *length,
128 1.1 christos int *format
129 1.1 christos )
130 1.1 christos #else
131 1.1 christos static int convertProc( widget, selection, target, type, value, length, format )
132 1.1 christos Widget widget;
133 1.1 christos Atom *selection, *target, *type;
134 1.1 christos void **value;
135 1.1 christos int *length;
136 1.1 christos int *format;
137 1.1 christos #endif
138 1.1 christos {
139 1.1 christos String buffer;
140 1.1 christos int len;
141 1.1 christos
142 1.1 christos /* someone wants a copy of the selection. is there one? */
143 1.1 christos (*icccm_copy)( &buffer, &len );
144 1.1 christos if ( len == 0 ) return False;
145 1.1 christos
146 1.1 christos /* do they want the string? */
147 1.1 christos if ( *target == XA_STRING ) {
148 1.1 christos *length = len;
149 1.1 christos *value = (void *) XtMalloc( len );
150 1.1 christos *type = XA_STRING;
151 1.1 christos *format = 8;
152 1.1 christos memcpy( (char *) *value, buffer, *length );
153 1.1 christos return True;
154 1.1 christos }
155 1.1 christos
156 1.1 christos /* do they want the length? */
157 1.1 christos if ( *target == XInternAtom( XtDisplay(widget), "LENGTH", FALSE) ) {
158 1.1 christos *length = 1;
159 1.1 christos *value = (void *) XtMalloc( sizeof(int) );
160 1.1 christos *type = *target;
161 1.1 christos *format = 32;
162 1.1 christos * ((int *) *value) = len;
163 1.1 christos return True;
164 1.1 christos }
165 1.1 christos
166 1.1 christos /* we lose */
167 1.1 christos return False;
168 1.1 christos }
169 1.1 christos
170 1.1 christos /*
171 1.1 christos * __vi_AcquirePrimary --
172 1.1 christos *
173 1.1 christos * PUBLIC: void __vi_AcquirePrimary __P((Widget));
174 1.1 christos */
175 1.1 christos void
176 1.1 christos __vi_AcquirePrimary(Widget widget)
177 1.1 christos {
178 1.1 christos /* assert we own the primary selection */
179 1.1 christos XtOwnSelection( widget,
180 1.1 christos XA_PRIMARY,
181 1.1 christos XtLastTimestampProcessed( XtDisplay(widget) ),
182 1.1 christos (XtConvertSelectionProc) convertProc,
183 1.1 christos (XtLoseSelectionProc) loseProc,
184 1.1 christos NULL
185 1.1 christos );
186 1.1 christos
187 1.1 christos #if defined(OPENLOOK)
188 1.1 christos /* assert we also own the clipboard */
189 1.1 christos XtOwnSelection( widget,
190 1.1 christos XA_CLIPBOARD( XtDisplay(widget) ),
191 1.1 christos XtLastTimestampProcessed( XtDisplay(widget) ),
192 1.1 christos convertProc,
193 1.1 christos loseProc,
194 1.1 christos NULL
195 1.1 christos );
196 1.1 christos #endif
197 1.1 christos }
198 1.1 christos
199 1.1 christos
200 1.1 christos #if defined(__STDC__)
201 1.1 christos static void gotProc( Widget widget,
202 1.1 christos void *data,
203 1.1 christos Atom *selection,
204 1.1 christos Atom *type,
205 1.1 christos void *value,
206 1.1 christos unsigned long *length,
207 1.1 christos int *format
208 1.1 christos )
209 1.1 christos #else
210 1.1 christos static void gotProc( widget, data, selection, type, value, length, format )
211 1.1 christos Widget widget;
212 1.1 christos void *data;
213 1.1 christos Atom *selection, *type;
214 1.1 christos void *value;
215 1.1 christos unsigned long *length;
216 1.1 christos int *format;
217 1.1 christos #endif
218 1.1 christos {
219 1.1 christos if ( *type == 0 )
220 1.1 christos (*icccm_error)( stderr, "Nothing in the primary selection buffer");
221 1.1 christos else if ( *type != XA_STRING )
222 1.1 christos (*icccm_error)( stderr, "Unknown type return from selection");
223 1.1 christos else {
224 1.1 christos (*icccm_paste)( widget, value, *length );
225 1.1 christos XtFree( value );
226 1.1 christos }
227 1.1 christos }
228 1.1 christos
229 1.1 christos /*
230 1.1 christos * __vi_PasteFromClipboard --
231 1.1 christos *
232 1.1 christos * PUBLIC: void __vi_PasteFromClipboard __P((Widget));
233 1.1 christos */
234 1.1 christos void
235 1.1 christos __vi_PasteFromClipboard(Widget widget)
236 1.1 christos {
237 1.1 christos XtGetSelectionValue( widget,
238 1.1 christos XA_PRIMARY,
239 1.1 christos XA_STRING,
240 1.1 christos (XtSelectionCallbackProc) gotProc,
241 1.1 christos NULL,
242 1.1 christos XtLastTimestampProcessed( XtDisplay(widget) )
243 1.1 christos );
244 1.1 christos }
245