184d9c625SLionel Sambuc /*-
284d9c625SLionel Sambuc * Copyright (c) 1996
384d9c625SLionel Sambuc * Rob Zimmermann. All rights reserved.
484d9c625SLionel Sambuc * Copyright (c) 1996
584d9c625SLionel Sambuc * Keith Bostic. All rights reserved.
684d9c625SLionel Sambuc *
784d9c625SLionel Sambuc * See the LICENSE file for redistribution information.
884d9c625SLionel Sambuc */
984d9c625SLionel Sambuc
1084d9c625SLionel Sambuc #include "config.h"
1184d9c625SLionel Sambuc
12*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
13*0a6a1f1dSLionel Sambuc #if 0
1484d9c625SLionel Sambuc #ifndef lint
1584d9c625SLionel Sambuc 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 ";
1684d9c625SLionel Sambuc #endif /* not lint */
17*0a6a1f1dSLionel Sambuc #else
18*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: m_copypaste.c,v 1.2 2014/01/26 21:43:45 christos Exp $");
19*0a6a1f1dSLionel Sambuc #endif
2084d9c625SLionel Sambuc
2184d9c625SLionel Sambuc /* ICCCM Cut and paste Utilities: */
2284d9c625SLionel Sambuc
2384d9c625SLionel Sambuc #include <sys/types.h>
2484d9c625SLionel Sambuc #include <sys/queue.h>
2584d9c625SLionel Sambuc
2684d9c625SLionel Sambuc #include <X11/X.h>
2784d9c625SLionel Sambuc #include <X11/Intrinsic.h>
2884d9c625SLionel Sambuc #include <X11/Xatom.h>
2984d9c625SLionel Sambuc
3084d9c625SLionel Sambuc #include <bitstring.h>
3184d9c625SLionel Sambuc #include <stdio.h>
3284d9c625SLionel Sambuc
3384d9c625SLionel Sambuc #undef LOCK_SUCCESS
3484d9c625SLionel Sambuc #include "../common/common.h"
3584d9c625SLionel Sambuc #include "../ipc/ip.h"
3684d9c625SLionel Sambuc #include "m_motif.h"
3784d9c625SLionel Sambuc
3884d9c625SLionel Sambuc typedef int (*PFI)();
3984d9c625SLionel Sambuc
4084d9c625SLionel Sambuc static PFI icccm_paste,
4184d9c625SLionel Sambuc icccm_copy,
4284d9c625SLionel Sambuc icccm_clear,
4384d9c625SLionel Sambuc icccm_error;
4484d9c625SLionel Sambuc
4584d9c625SLionel Sambuc /*
4684d9c625SLionel Sambuc * InitCopyPaste --
4784d9c625SLionel Sambuc *
4884d9c625SLionel Sambuc * PUBLIC: void __vi_InitCopyPaste
4984d9c625SLionel Sambuc * PUBLIC: __P((int (*)(), int (*)(), int (*)(), int (*)()));
5084d9c625SLionel Sambuc */
5184d9c625SLionel Sambuc void
__vi_InitCopyPaste(PFI f_copy,PFI f_paste,PFI f_clear,PFI f_error)5284d9c625SLionel Sambuc __vi_InitCopyPaste(PFI f_copy, PFI f_paste, PFI f_clear, PFI f_error)
5384d9c625SLionel Sambuc {
5484d9c625SLionel Sambuc icccm_paste = f_paste;
5584d9c625SLionel Sambuc icccm_clear = f_clear;
5684d9c625SLionel Sambuc icccm_copy = f_copy;
5784d9c625SLionel Sambuc icccm_error = f_error;
5884d9c625SLionel Sambuc }
5984d9c625SLionel Sambuc
6084d9c625SLionel Sambuc
6184d9c625SLionel Sambuc #if defined(__STDC__)
peekProc(Widget widget,void * data,Atom * selection,Atom * type,void * value,unsigned long * length,int * format)6284d9c625SLionel Sambuc static void peekProc( Widget widget,
6384d9c625SLionel Sambuc void *data,
6484d9c625SLionel Sambuc Atom *selection,
6584d9c625SLionel Sambuc Atom *type,
6684d9c625SLionel Sambuc void *value,
6784d9c625SLionel Sambuc unsigned long *length,
6884d9c625SLionel Sambuc int *format
6984d9c625SLionel Sambuc )
7084d9c625SLionel Sambuc #else
7184d9c625SLionel Sambuc static void peekProc( widget, data, selection, type, value, length, format )
7284d9c625SLionel Sambuc Widget widget;
7384d9c625SLionel Sambuc void *data;
7484d9c625SLionel Sambuc Atom *selection, *type;
7584d9c625SLionel Sambuc void *value;
7684d9c625SLionel Sambuc unsigned long *length;
7784d9c625SLionel Sambuc int *format;
7884d9c625SLionel Sambuc #endif
7984d9c625SLionel Sambuc {
8084d9c625SLionel Sambuc if ( *type == 0 )
8184d9c625SLionel Sambuc (*icccm_error)( stderr, "Nothing in the primary selection buffer");
8284d9c625SLionel Sambuc else if ( *type != XA_STRING )
8384d9c625SLionel Sambuc (*icccm_error)( stderr, "Unknown type return from selection");
8484d9c625SLionel Sambuc else
8584d9c625SLionel Sambuc XtFree( value );
8684d9c625SLionel Sambuc }
8784d9c625SLionel Sambuc
8884d9c625SLionel Sambuc
8984d9c625SLionel Sambuc #if 0
9084d9c625SLionel Sambuc #if defined(__STDC__)
9184d9c625SLionel Sambuc void _vi_AcquireClipboard( Widget wid )
9284d9c625SLionel Sambuc #else
9384d9c625SLionel Sambuc void _vi_AcquireClipboard( wid )
9484d9c625SLionel Sambuc Widget wid;
9584d9c625SLionel Sambuc #endif
9684d9c625SLionel Sambuc {
9784d9c625SLionel Sambuc XtGetSelectionValue( wid,
9884d9c625SLionel Sambuc XA_PRIMARY,
9984d9c625SLionel Sambuc XA_STRING,
10084d9c625SLionel Sambuc (XtSelectionCallbackProc) peekProc,
10184d9c625SLionel Sambuc NULL,
10284d9c625SLionel Sambuc XtLastTimestampProcessed( XtDisplay(wid) )
10384d9c625SLionel Sambuc );
10484d9c625SLionel Sambuc }
10584d9c625SLionel Sambuc #endif
10684d9c625SLionel Sambuc
10784d9c625SLionel Sambuc
10884d9c625SLionel Sambuc #if defined(__STDC__)
loseProc(Widget widget)10984d9c625SLionel Sambuc static void loseProc( Widget widget )
11084d9c625SLionel Sambuc #else
11184d9c625SLionel Sambuc static void loseProc( widget )
11284d9c625SLionel Sambuc Widget widget;
11384d9c625SLionel Sambuc #endif
11484d9c625SLionel Sambuc {
11584d9c625SLionel Sambuc /* we have lost ownership of the selection. clear it */
11684d9c625SLionel Sambuc (*icccm_clear)( widget );
11784d9c625SLionel Sambuc
11884d9c625SLionel Sambuc /* also participate in the protocols */
11984d9c625SLionel Sambuc XtDisownSelection( widget,
12084d9c625SLionel Sambuc XA_PRIMARY,
12184d9c625SLionel Sambuc XtLastTimestampProcessed( XtDisplay(widget) )
12284d9c625SLionel Sambuc );
12384d9c625SLionel Sambuc }
12484d9c625SLionel Sambuc
12584d9c625SLionel Sambuc
12684d9c625SLionel Sambuc #if defined(__STDC__)
convertProc(Widget widget,Atom * selection,Atom * target,Atom * type,void ** value,int * length,int * format)12784d9c625SLionel Sambuc static int convertProc( Widget widget,
12884d9c625SLionel Sambuc Atom *selection,
12984d9c625SLionel Sambuc Atom *target,
13084d9c625SLionel Sambuc Atom *type,
13184d9c625SLionel Sambuc void **value,
13284d9c625SLionel Sambuc int *length,
13384d9c625SLionel Sambuc int *format
13484d9c625SLionel Sambuc )
13584d9c625SLionel Sambuc #else
13684d9c625SLionel Sambuc static int convertProc( widget, selection, target, type, value, length, format )
13784d9c625SLionel Sambuc Widget widget;
13884d9c625SLionel Sambuc Atom *selection, *target, *type;
13984d9c625SLionel Sambuc void **value;
14084d9c625SLionel Sambuc int *length;
14184d9c625SLionel Sambuc int *format;
14284d9c625SLionel Sambuc #endif
14384d9c625SLionel Sambuc {
14484d9c625SLionel Sambuc String buffer;
14584d9c625SLionel Sambuc int len;
14684d9c625SLionel Sambuc
14784d9c625SLionel Sambuc /* someone wants a copy of the selection. is there one? */
14884d9c625SLionel Sambuc (*icccm_copy)( &buffer, &len );
14984d9c625SLionel Sambuc if ( len == 0 ) return False;
15084d9c625SLionel Sambuc
15184d9c625SLionel Sambuc /* do they want the string? */
15284d9c625SLionel Sambuc if ( *target == XA_STRING ) {
15384d9c625SLionel Sambuc *length = len;
15484d9c625SLionel Sambuc *value = (void *) XtMalloc( len );
15584d9c625SLionel Sambuc *type = XA_STRING;
15684d9c625SLionel Sambuc *format = 8;
15784d9c625SLionel Sambuc memcpy( (char *) *value, buffer, *length );
15884d9c625SLionel Sambuc return True;
15984d9c625SLionel Sambuc }
16084d9c625SLionel Sambuc
16184d9c625SLionel Sambuc /* do they want the length? */
16284d9c625SLionel Sambuc if ( *target == XInternAtom( XtDisplay(widget), "LENGTH", FALSE) ) {
16384d9c625SLionel Sambuc *length = 1;
16484d9c625SLionel Sambuc *value = (void *) XtMalloc( sizeof(int) );
16584d9c625SLionel Sambuc *type = *target;
16684d9c625SLionel Sambuc *format = 32;
16784d9c625SLionel Sambuc * ((int *) *value) = len;
16884d9c625SLionel Sambuc return True;
16984d9c625SLionel Sambuc }
17084d9c625SLionel Sambuc
17184d9c625SLionel Sambuc /* we lose */
17284d9c625SLionel Sambuc return False;
17384d9c625SLionel Sambuc }
17484d9c625SLionel Sambuc
17584d9c625SLionel Sambuc /*
17684d9c625SLionel Sambuc * __vi_AcquirePrimary --
17784d9c625SLionel Sambuc *
17884d9c625SLionel Sambuc * PUBLIC: void __vi_AcquirePrimary __P((Widget));
17984d9c625SLionel Sambuc */
18084d9c625SLionel Sambuc void
__vi_AcquirePrimary(Widget widget)18184d9c625SLionel Sambuc __vi_AcquirePrimary(Widget widget)
18284d9c625SLionel Sambuc {
18384d9c625SLionel Sambuc /* assert we own the primary selection */
18484d9c625SLionel Sambuc XtOwnSelection( widget,
18584d9c625SLionel Sambuc XA_PRIMARY,
18684d9c625SLionel Sambuc XtLastTimestampProcessed( XtDisplay(widget) ),
18784d9c625SLionel Sambuc (XtConvertSelectionProc) convertProc,
18884d9c625SLionel Sambuc (XtLoseSelectionProc) loseProc,
18984d9c625SLionel Sambuc NULL
19084d9c625SLionel Sambuc );
19184d9c625SLionel Sambuc
19284d9c625SLionel Sambuc #if defined(OPENLOOK)
19384d9c625SLionel Sambuc /* assert we also own the clipboard */
19484d9c625SLionel Sambuc XtOwnSelection( widget,
19584d9c625SLionel Sambuc XA_CLIPBOARD( XtDisplay(widget) ),
19684d9c625SLionel Sambuc XtLastTimestampProcessed( XtDisplay(widget) ),
19784d9c625SLionel Sambuc convertProc,
19884d9c625SLionel Sambuc loseProc,
19984d9c625SLionel Sambuc NULL
20084d9c625SLionel Sambuc );
20184d9c625SLionel Sambuc #endif
20284d9c625SLionel Sambuc }
20384d9c625SLionel Sambuc
20484d9c625SLionel Sambuc
20584d9c625SLionel Sambuc #if defined(__STDC__)
gotProc(Widget widget,void * data,Atom * selection,Atom * type,void * value,unsigned long * length,int * format)20684d9c625SLionel Sambuc static void gotProc( Widget widget,
20784d9c625SLionel Sambuc void *data,
20884d9c625SLionel Sambuc Atom *selection,
20984d9c625SLionel Sambuc Atom *type,
21084d9c625SLionel Sambuc void *value,
21184d9c625SLionel Sambuc unsigned long *length,
21284d9c625SLionel Sambuc int *format
21384d9c625SLionel Sambuc )
21484d9c625SLionel Sambuc #else
21584d9c625SLionel Sambuc static void gotProc( widget, data, selection, type, value, length, format )
21684d9c625SLionel Sambuc Widget widget;
21784d9c625SLionel Sambuc void *data;
21884d9c625SLionel Sambuc Atom *selection, *type;
21984d9c625SLionel Sambuc void *value;
22084d9c625SLionel Sambuc unsigned long *length;
22184d9c625SLionel Sambuc int *format;
22284d9c625SLionel Sambuc #endif
22384d9c625SLionel Sambuc {
22484d9c625SLionel Sambuc if ( *type == 0 )
22584d9c625SLionel Sambuc (*icccm_error)( stderr, "Nothing in the primary selection buffer");
22684d9c625SLionel Sambuc else if ( *type != XA_STRING )
22784d9c625SLionel Sambuc (*icccm_error)( stderr, "Unknown type return from selection");
22884d9c625SLionel Sambuc else {
22984d9c625SLionel Sambuc (*icccm_paste)( widget, value, *length );
23084d9c625SLionel Sambuc XtFree( value );
23184d9c625SLionel Sambuc }
23284d9c625SLionel Sambuc }
23384d9c625SLionel Sambuc
23484d9c625SLionel Sambuc /*
23584d9c625SLionel Sambuc * __vi_PasteFromClipboard --
23684d9c625SLionel Sambuc *
23784d9c625SLionel Sambuc * PUBLIC: void __vi_PasteFromClipboard __P((Widget));
23884d9c625SLionel Sambuc */
23984d9c625SLionel Sambuc void
__vi_PasteFromClipboard(Widget widget)24084d9c625SLionel Sambuc __vi_PasteFromClipboard(Widget widget)
24184d9c625SLionel Sambuc {
24284d9c625SLionel Sambuc XtGetSelectionValue( widget,
24384d9c625SLionel Sambuc XA_PRIMARY,
24484d9c625SLionel Sambuc XA_STRING,
24584d9c625SLionel Sambuc (XtSelectionCallbackProc) gotProc,
24684d9c625SLionel Sambuc NULL,
24784d9c625SLionel Sambuc XtLastTimestampProcessed( XtDisplay(widget) )
24884d9c625SLionel Sambuc );
24984d9c625SLionel Sambuc }
250