1 /* This file is part of the Project Athena Zephyr Notification System.
2 * It is one of the source files comprising zwgc, the Zephyr WindowGram
5 * Created by: Marc Horowitz <marc@athena.mit.edu>
9 * Copyright (c) 1989 by the Massachusetts Institute of Technology.
10 * For copying and distribution information, see the file
16 #if (!defined(lint) && !defined(SABER))
17 static const char rcsid_xselect_c[] = "$Id$";
20 #include <zephyr/mit-copyright.h>
22 /* xselect.c - ICCCM compliant cut-and-paste */
23 /* also includes some other ICCCMisms, such as the WM_PROTOCOL handling */
25 #ifndef X_DISPLAY_MISSING
28 #include <X11/Xproto.h>
29 #include <X11/Xatom.h>
30 #include "new_string.h"
33 extern char *getSelectedText(void);
35 static Time ownership_start = CurrentTime;
36 static Time ownership_end = CurrentTime;
37 Atom XA_WM_PROTOCOLS,XA_WM_DELETE_WINDOW;
38 static Atom ZA_TARGETS,ZA_MULTIPLE,ZA_TIMESTAMP,ZA_ATOM_PAIR;
40 static struct _ZAtom {
44 {&XA_WM_PROTOCOLS,"WM_PROTOCOLS"},
45 {&XA_WM_DELETE_WINDOW,"WM_DELETE_WINDOW"},
46 {&ZA_TARGETS,"TARGETS"},
47 {&ZA_MULTIPLE,"MULTIPLE"},
48 {&ZA_TIMESTAMP,"TIMESTAMP"},
49 {&ZA_ATOM_PAIR,"ATOM_PAIR"}
51 #define NumZAtoms (sizeof(ZAtom)/sizeof(struct _ZAtom))
53 /* internal static functions */
56 xselNotify(Display *dpy,
57 XSelectionRequestEvent *selreq,
62 ev.type=SelectionNotify;
63 ev.requestor=selreq->requestor;
64 ev.selection=selreq->selection;
65 ev.target=selreq->target;
69 XSendEvent(dpy,ev.requestor,False,0,(XEvent *) &ev);
72 /* pRequestAtoms and RequestAtoms should have the same size. */
73 static Atom *pRequestAtoms[] = {
74 &ZA_TARGETS,&ZA_MULTIPLE,&ZA_TIMESTAMP,NULL
76 static Atom RequestAtoms[] = {
77 None,None,None,XA_STRING
79 #define NumRequestAtoms (sizeof(RequestAtoms)/sizeof(Atom))
80 #define PROP(prop,targ) ((prop)!=None?(prop):(targ))
81 #define ChangeProp(type,format,data,size) \
82 XChangeProperty(dpy,w,PROP(property,target),(type),(format), \
83 PropModeReplace, (unsigned char *) (data),(size))
86 xselSetProperties(Display *dpy,
90 XSelectionRequestEvent *selreq)
92 if (target==ZA_TARGETS) {
94 ChangeProp(XA_ATOM,32,RequestAtoms,NumRequestAtoms);
96 } else if (target==ZA_MULTIPLE) {
100 unsigned long alistsize,i;
102 XGetWindowProperty(dpy,w,property,0L,0L,False,ZA_ATOM_PAIR,&atype,
103 &aformat,&i,&alistsize,(unsigned char **) &alist);
106 XGetWindowProperty(dpy,w,property,0L,alistsize/sizeof(Atom),False,
107 ZA_ATOM_PAIR,&atype,&aformat,&alistsize,&i,
108 (unsigned char **) &alist);
110 alistsize/=(sizeof(Atom)/4);
111 for (i=0;i<alistsize;i+=2)
112 xselSetProperties(dpy,w,alist[i+1],alist[i],selreq);
114 XFree((char *) alist);
115 } else if (target==ZA_TIMESTAMP) {
116 ChangeProp(XA_INTEGER,32,&ownership_start,1);
118 } else if (target==XA_STRING) {
121 if (selected = getSelectedText()) {
122 ChangeProp(XA_STRING,8,selected,string_Length(selected));
124 /* This should only happen if the pasting client is out of
125 spec (or if this program is buggy), but it could happen */
128 "SelectionRequest event received for unowned selection: requestor wid=0x%x", w);
130 ChangeProp(XA_STRING,8,"",0);
135 xselNotify(dpy,selreq,property);
138 /* global functions */
141 xicccmInitAtoms(Display *dpy)
145 for (i=0;i<NumZAtoms;i++)
146 *(ZAtom[i].patom)=XInternAtom(dpy,ZAtom[i].name,False);
147 for (i=0;i<NumRequestAtoms;i++)
148 if (pRequestAtoms[i])
149 RequestAtoms[i] = *(pRequestAtoms[i]);
153 xselGetOwnership(Display *dpy,
159 XSetSelectionOwner(dpy,XA_PRIMARY,w,time);
160 temp=(w == XGetSelectionOwner(dpy,XA_PRIMARY));
163 ownership_start = time;
168 /* Get the selection. Return !0 if success, 0 if fail */
170 xselProcessSelection(Display *dpy,
174 XSelectionRequestEvent *selreq = &(event->xselectionrequest);
177 if ((selreq->owner != w) || (selreq->selection != XA_PRIMARY))
178 fprintf(stderr,"SelectionRequest event has bogus field values\n");
181 if ((ownership_start == CurrentTime) ||
182 ((selreq->time != CurrentTime) &&
183 (selreq->time < ownership_start) ||
184 ((ownership_end != CurrentTime) &&
185 (ownership_end > ownership_start) &&
186 (selreq->time > ownership_end))))
187 xselNotify(dpy,selreq,None);
189 xselSetProperties(dpy,selreq->requestor,selreq->property,selreq->target,
196 xselOwnershipLost(Time time)
198 ownership_end = time;
203 xselGiveUpOwnership(Display *dpy,
206 XSetSelectionOwner(dpy,XA_PRIMARY,None,ownership_start);
208 ownership_end=ownership_start; /* Is this right? what should I use? */
211 #endif /* X_DISPLAY_MISSING */