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