1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
/* This file is part of the Project Athena Zephyr Notification System.
* It is one of the source files comprising zwgc, the Zephyr WindowGram
* client.
*
* Created by: Marc Horowitz <marc@athena.mit.edu>
*
* $Source$
* $Author$
*
* Copyright (c) 1989 by the Massachusetts Institute of Technology.
* For copying and distribution information, see the file
* "mit-copyright.h".
*/
#if (!defined(lint) && !defined(SABER))
static char rcsid_xselect_c[] = "$Id$";
#endif
#include <zephyr/mit-copyright.h>
/* xselect.c - ICCCM compliant cut-and-paste */
#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xproto.h>
#include <X11/Xatom.h>
#include "new_string.h"
#include "xselect.h"
extern char *getSelectedText();
static Time ownership_start = CurrentTime;
static Time ownership_end = CurrentTime;
static Atom ZA_TARGETS,ZA_MULTIPLE,ZA_TIMESTAMP,ZA_ATOM_PAIR;
static struct _ZAtom {
Atom *patom;
char *name;
} ZAtom[] = {
{&ZA_TARGETS,"TARGETS"},
{&ZA_MULTIPLE,"MULTIPLE"},
{&ZA_TIMESTAMP,"TIMESTAMP"},
{&ZA_ATOM_PAIR,"ATOM_PAIR"}
};
#define NumZAtoms (sizeof(ZAtom)/sizeof(struct _ZAtom))
/* internal static functions */
static void xselNotify(dpy,selreq,property)
Display *dpy;
XSelectionRequestEvent *selreq;
Atom property;
{
XSelectionEvent ev;
ev.type=SelectionNotify;
ev.requestor=selreq->requestor;
ev.selection=selreq->selection;
ev.target=selreq->target;
ev.property=property;
ev.time=selreq->time;
XSendEvent(dpy,ev.requestor,False,0,(XEvent *) &ev);
}
/* pRequestAtoms and RequestAtoms should have the same size. */
static Atom *pRequestAtoms[] = {
&ZA_TARGETS,&ZA_MULTIPLE,&ZA_TIMESTAMP,NULL
};
static Atom RequestAtoms[] = {
None,None,None,XA_STRING
};
#define NumRequestAtoms (sizeof(RequestAtoms)/sizeof(Atom))
#define PROP(prop,targ) ((prop)!=None?(prop):(targ))
#define ChangeProp(type,format,data,size) \
XChangeProperty(dpy,w,PROP(property,target),(type),(format), \
PropModeReplace, (unsigned char *) (data),(size))
static void xselSetProperties(dpy,w,property,target,selreq)
Display *dpy;
Window w;
Atom property,target;
XSelectionRequestEvent *selreq;
{
if (target==ZA_TARGETS) {
ChangeProp(XA_ATOM,32,RequestAtoms,NumRequestAtoms);
XSync(dpy,0);
} else if (target==ZA_MULTIPLE) {
Atom atype;
int aformat;
Atom *alist;
unsigned long alistsize,i;
XGetWindowProperty(dpy,w,property,0L,0L,False,ZA_ATOM_PAIR,&atype,
&aformat,&i,&alistsize,(unsigned char **) &alist);
if (alistsize)
XGetWindowProperty(dpy,w,property,0L,alistsize/sizeof(Atom),False,
ZA_ATOM_PAIR,&atype,&aformat,&alistsize,&i,
(unsigned char **) &alist);
alistsize/=(sizeof(Atom)/4);
for (i=0;i<alistsize;i+=2)
xselSetProperties(dpy,w,alist[i+1],alist[i],selreq);
XFree(alist);
} else if (target==ZA_TIMESTAMP) {
ChangeProp(XA_INTEGER,32,&ownership_start,1);
XSync(dpy,0);
} else if (target==XA_STRING) {
char *selected;
selected=getSelectedText();
ChangeProp(XA_STRING,8,selected,string_Length(selected));
XSync(dpy,0);
}
xselNotify(dpy,selreq,property);
}
/* global functions */
void xselInitAtoms(dpy)
Display *dpy;
{
int i;
for (i=0;i<NumZAtoms;i++)
*(ZAtom[i].patom)=XInternAtom(dpy,ZAtom[i].name,False);
for (i=0;i<NumRequestAtoms;i++)
if (pRequestAtoms[i])
RequestAtoms[i]=*(pRequestAtoms[i]);
}
int xselGetOwnership(dpy,w,time)
Display *dpy;
Window w;
Time time;
{
int temp;
XSetSelectionOwner(dpy,XA_PRIMARY,w,time);
temp=(w == XGetSelectionOwner(dpy,XA_PRIMARY));
if (temp)
ownership_start = time;
return(temp);
}
/* Get the selection. Return !0 if success, 0 if fail */
int xselProcessSelection(dpy,w,event)
Display *dpy;
Window w;
XEvent *event;
{
XSelectionRequestEvent *selreq = &(event->xselectionrequest);
int (*syncfunc)();
Atom targetprop;
char *selvalue;
#ifdef DEBUG
if ((selreq->owner != w) || (selreq->selection != XA_PRIMARY))
fprintf(stderr,"SelectionRequest event has bogus field values\n");
#endif
if ((ownership_start == CurrentTime) ||
(selreq->time < ownership_start) ||
((ownership_end != CurrentTime) &&
(ownership_end > ownership_start) &&
(selreq->time > ownership_end)))
xselNotify(dpy,event,None);
xselSetProperties(dpy,selreq->requestor,selreq->property,selreq->target,
selreq);
return(1);
}
void xselOwnershipLost(time)
Time time;
{
ownership_end = time;
}
void xselGiveUpOwnership(dpy,w)
Display *dpy;
Window w;
{
XSetSelectionOwner(dpy,XA_PRIMARY,None,ownership_start);
ownership_end=ownership_start; /* Is this right? what should I use? */
}
|