1 /*
2  * cutpaste.c - routines to deal with cut & paste buffers / selection.
3  */
4 
5 /*
6  *  Copyright (C) 1999 AT&T Laboratories Cambridge.  All Rights Reserved.
7  *
8  *  This is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This software is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this software; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
21  *  USA.
22  */
23 
24 #include <stdio.h>
25 #define NEED_EVENTS
26 #include <X.h>
27 #include <Xproto.h>
28 #include "rfb.h"
29 #include "selection.h"
30 #include "input.h"
31 #include <Xatom.h>
32 
33 extern WindowPtr *WindowTable; /* Why isn't this in a header file? */
34 extern Selection *CurrentSelections;
35 extern int NumCurrentSelections;
36 
37 
38 static Bool inSetXCutText = FALSE;
39 
40 /*
41  * rfbSetXCutText sets the cut buffer to be the given string.  We also clear
42  * the primary selection.  Ideally we'd like to set it to the same thing, but I
43  * can't work out how to do that without some kind of helper X client.
44  */
45 
46 void
rfbSetXCutText(char * str,int len)47 rfbSetXCutText(char *str, int len)
48 {
49     int i = 0;
50 
51     inSetXCutText = TRUE;
52     ChangeWindowProperty(WindowTable[0], XA_CUT_BUFFER0, XA_STRING,
53 			 8, PropModeReplace, len,
54 			 (pointer)str, TRUE);
55 
56     while ((i < NumCurrentSelections) &&
57 	   CurrentSelections[i].selection != XA_PRIMARY)
58 	i++;
59 
60     if (i < NumCurrentSelections) {
61 	xEvent event;
62 
63 	if (CurrentSelections[i].client) {
64 	    event.u.u.type = SelectionClear;
65 	    event.u.selectionClear.time = GetTimeInMillis();
66 	    event.u.selectionClear.window = CurrentSelections[i].window;
67 	    event.u.selectionClear.atom = CurrentSelections[i].selection;
68 	    (void) TryClientEvents (CurrentSelections[i].client, &event, 1,
69 				NoEventMask, NoEventMask /* CantBeFiltered */,
70 				NullGrab);
71 	}
72 
73 	CurrentSelections[i].window = None;
74 	CurrentSelections[i].pWin = NULL;
75 	CurrentSelections[i].client = NullClient;
76     }
77 
78     inSetXCutText = FALSE;
79 }
80 
81 
rfbGotXCutText(char * str,int len)82 void rfbGotXCutText(char *str, int len)
83 {
84     if (!inSetXCutText)
85 	rfbSendServerCutText(str, len);
86 }
87