xref: /OK3568_Linux_fs/external/xserver/dix/selection.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /************************************************************
2*4882a593Smuzhiyun 
3*4882a593Smuzhiyun Copyright 1987, 1989, 1998  The Open Group
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun Permission to use, copy, modify, distribute, and sell this software and its
6*4882a593Smuzhiyun documentation for any purpose is hereby granted without fee, provided that
7*4882a593Smuzhiyun the above copyright notice appear in all copies and that both that
8*4882a593Smuzhiyun copyright notice and this permission notice appear in supporting
9*4882a593Smuzhiyun documentation.
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun The above copyright notice and this permission notice shall be included in
12*4882a593Smuzhiyun all copies or substantial portions of the Software.
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15*4882a593Smuzhiyun IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16*4882a593Smuzhiyun FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17*4882a593Smuzhiyun OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18*4882a593Smuzhiyun AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19*4882a593Smuzhiyun CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun Except as contained in this notice, the name of The Open Group shall not be
22*4882a593Smuzhiyun used in advertising or otherwise to promote the sale, use or other dealings
23*4882a593Smuzhiyun in this Software without prior written authorization from The Open Group.
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun Copyright 1987, 1989 by Digital Equipment Corporation, Maynard, Massachusetts.
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun                         All Rights Reserved
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun Permission to use, copy, modify, and distribute this software and its
30*4882a593Smuzhiyun documentation for any purpose and without fee is hereby granted,
31*4882a593Smuzhiyun provided that the above copyright notice appear in all copies and that
32*4882a593Smuzhiyun both that copyright notice and this permission notice appear in
33*4882a593Smuzhiyun supporting documentation, and that the name of Digital not be
34*4882a593Smuzhiyun used in advertising or publicity pertaining to distribution of the
35*4882a593Smuzhiyun software without specific, written prior permission.
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
38*4882a593Smuzhiyun ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
39*4882a593Smuzhiyun DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
40*4882a593Smuzhiyun ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
41*4882a593Smuzhiyun WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
42*4882a593Smuzhiyun ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
43*4882a593Smuzhiyun SOFTWARE.
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun ********************************************************/
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun #ifdef HAVE_DIX_CONFIG_H
48*4882a593Smuzhiyun #include <dix-config.h>
49*4882a593Smuzhiyun #endif
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun #include "windowstr.h"
52*4882a593Smuzhiyun #include "dixstruct.h"
53*4882a593Smuzhiyun #include "dispatch.h"
54*4882a593Smuzhiyun #include "selection.h"
55*4882a593Smuzhiyun #include "xace.h"
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun /*****************************************************************
58*4882a593Smuzhiyun  * Selection Stuff
59*4882a593Smuzhiyun  *
60*4882a593Smuzhiyun  *    dixLookupSelection
61*4882a593Smuzhiyun  *
62*4882a593Smuzhiyun  *   Selections are global to the server.  The list of selections should
63*4882a593Smuzhiyun  *   not be traversed directly.  Instead, use the functions listed above.
64*4882a593Smuzhiyun  *
65*4882a593Smuzhiyun  *****************************************************************/
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun Selection *CurrentSelections;
68*4882a593Smuzhiyun CallbackListPtr SelectionCallback;
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun int
dixLookupSelection(Selection ** result,Atom selectionName,ClientPtr client,Mask access_mode)71*4882a593Smuzhiyun dixLookupSelection(Selection ** result, Atom selectionName,
72*4882a593Smuzhiyun                    ClientPtr client, Mask access_mode)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun     Selection *pSel;
75*4882a593Smuzhiyun     int rc = BadMatch;
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun     client->errorValue = selectionName;
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun     for (pSel = CurrentSelections; pSel; pSel = pSel->next)
80*4882a593Smuzhiyun         if (pSel->selection == selectionName)
81*4882a593Smuzhiyun             break;
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun     if (pSel)
84*4882a593Smuzhiyun         rc = XaceHookSelectionAccess(client, &pSel, access_mode);
85*4882a593Smuzhiyun     *result = pSel;
86*4882a593Smuzhiyun     return rc;
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun void
InitSelections(void)90*4882a593Smuzhiyun InitSelections(void)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun     Selection *pSel, *pNextSel;
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun     pSel = CurrentSelections;
95*4882a593Smuzhiyun     while (pSel) {
96*4882a593Smuzhiyun         pNextSel = pSel->next;
97*4882a593Smuzhiyun         dixFreeObjectWithPrivates(pSel, PRIVATE_SELECTION);
98*4882a593Smuzhiyun         pSel = pNextSel;
99*4882a593Smuzhiyun     }
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun     CurrentSelections = NULL;
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun static _X_INLINE void
CallSelectionCallback(Selection * pSel,ClientPtr client,SelectionCallbackKind kind)105*4882a593Smuzhiyun CallSelectionCallback(Selection * pSel, ClientPtr client,
106*4882a593Smuzhiyun                       SelectionCallbackKind kind)
107*4882a593Smuzhiyun {
108*4882a593Smuzhiyun     SelectionInfoRec info = { pSel, client, kind };
109*4882a593Smuzhiyun     CallCallbacks(&SelectionCallback, &info);
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun void
DeleteWindowFromAnySelections(WindowPtr pWin)113*4882a593Smuzhiyun DeleteWindowFromAnySelections(WindowPtr pWin)
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun     Selection *pSel;
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun     for (pSel = CurrentSelections; pSel; pSel = pSel->next)
118*4882a593Smuzhiyun         if (pSel->pWin == pWin) {
119*4882a593Smuzhiyun             CallSelectionCallback(pSel, NULL, SelectionWindowDestroy);
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun             pSel->pWin = (WindowPtr) NULL;
122*4882a593Smuzhiyun             pSel->window = None;
123*4882a593Smuzhiyun             pSel->client = NullClient;
124*4882a593Smuzhiyun         }
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun void
DeleteClientFromAnySelections(ClientPtr client)128*4882a593Smuzhiyun DeleteClientFromAnySelections(ClientPtr client)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun     Selection *pSel;
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun     for (pSel = CurrentSelections; pSel; pSel = pSel->next)
133*4882a593Smuzhiyun         if (pSel->client == client) {
134*4882a593Smuzhiyun             CallSelectionCallback(pSel, NULL, SelectionClientClose);
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun             pSel->pWin = (WindowPtr) NULL;
137*4882a593Smuzhiyun             pSel->window = None;
138*4882a593Smuzhiyun             pSel->client = NullClient;
139*4882a593Smuzhiyun         }
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun int
ProcSetSelectionOwner(ClientPtr client)143*4882a593Smuzhiyun ProcSetSelectionOwner(ClientPtr client)
144*4882a593Smuzhiyun {
145*4882a593Smuzhiyun     WindowPtr pWin = NULL;
146*4882a593Smuzhiyun     TimeStamp time;
147*4882a593Smuzhiyun     Selection *pSel;
148*4882a593Smuzhiyun     int rc;
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun     REQUEST(xSetSelectionOwnerReq);
151*4882a593Smuzhiyun     REQUEST_SIZE_MATCH(xSetSelectionOwnerReq);
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun     UpdateCurrentTime();
154*4882a593Smuzhiyun     time = ClientTimeToServerTime(stuff->time);
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun     /* If the client's time stamp is in the future relative to the server's
157*4882a593Smuzhiyun        time stamp, do not set the selection, just return success. */
158*4882a593Smuzhiyun     if (CompareTimeStamps(time, currentTime) == LATER)
159*4882a593Smuzhiyun         return Success;
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun     if (stuff->window != None) {
162*4882a593Smuzhiyun         rc = dixLookupWindow(&pWin, stuff->window, client, DixSetAttrAccess);
163*4882a593Smuzhiyun         if (rc != Success)
164*4882a593Smuzhiyun             return rc;
165*4882a593Smuzhiyun     }
166*4882a593Smuzhiyun     if (!ValidAtom(stuff->selection)) {
167*4882a593Smuzhiyun         client->errorValue = stuff->selection;
168*4882a593Smuzhiyun         return BadAtom;
169*4882a593Smuzhiyun     }
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun     /*
172*4882a593Smuzhiyun      * First, see if the selection is already set...
173*4882a593Smuzhiyun      */
174*4882a593Smuzhiyun     rc = dixLookupSelection(&pSel, stuff->selection, client, DixSetAttrAccess);
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun     if (rc == Success) {
177*4882a593Smuzhiyun         /* If the timestamp in client's request is in the past relative
178*4882a593Smuzhiyun            to the time stamp indicating the last time the owner of the
179*4882a593Smuzhiyun            selection was set, do not set the selection, just return
180*4882a593Smuzhiyun            success. */
181*4882a593Smuzhiyun         if (CompareTimeStamps(time, pSel->lastTimeChanged) == EARLIER)
182*4882a593Smuzhiyun             return Success;
183*4882a593Smuzhiyun         if (pSel->client && (!pWin || (pSel->client != client))) {
184*4882a593Smuzhiyun             xEvent event = {
185*4882a593Smuzhiyun                 .u.selectionClear.time = time.milliseconds,
186*4882a593Smuzhiyun                 .u.selectionClear.window = pSel->window,
187*4882a593Smuzhiyun                 .u.selectionClear.atom = pSel->selection
188*4882a593Smuzhiyun             };
189*4882a593Smuzhiyun             event.u.u.type = SelectionClear;
190*4882a593Smuzhiyun             WriteEventsToClient(pSel->client, 1, &event);
191*4882a593Smuzhiyun         }
192*4882a593Smuzhiyun     }
193*4882a593Smuzhiyun     else if (rc == BadMatch) {
194*4882a593Smuzhiyun         /*
195*4882a593Smuzhiyun          * It doesn't exist, so add it...
196*4882a593Smuzhiyun          */
197*4882a593Smuzhiyun         pSel = dixAllocateObjectWithPrivates(Selection, PRIVATE_SELECTION);
198*4882a593Smuzhiyun         if (!pSel)
199*4882a593Smuzhiyun             return BadAlloc;
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun         pSel->selection = stuff->selection;
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun         /* security creation/labeling check */
204*4882a593Smuzhiyun         rc = XaceHookSelectionAccess(client, &pSel,
205*4882a593Smuzhiyun                                      DixCreateAccess | DixSetAttrAccess);
206*4882a593Smuzhiyun         if (rc != Success) {
207*4882a593Smuzhiyun             free(pSel);
208*4882a593Smuzhiyun             return rc;
209*4882a593Smuzhiyun         }
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun         pSel->next = CurrentSelections;
212*4882a593Smuzhiyun         CurrentSelections = pSel;
213*4882a593Smuzhiyun     }
214*4882a593Smuzhiyun     else
215*4882a593Smuzhiyun         return rc;
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun     pSel->lastTimeChanged = time;
218*4882a593Smuzhiyun     pSel->window = stuff->window;
219*4882a593Smuzhiyun     pSel->pWin = pWin;
220*4882a593Smuzhiyun     pSel->client = (pWin ? client : NullClient);
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun     CallSelectionCallback(pSel, client, SelectionSetOwner);
223*4882a593Smuzhiyun     return Success;
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun int
ProcGetSelectionOwner(ClientPtr client)227*4882a593Smuzhiyun ProcGetSelectionOwner(ClientPtr client)
228*4882a593Smuzhiyun {
229*4882a593Smuzhiyun     int rc;
230*4882a593Smuzhiyun     Selection *pSel;
231*4882a593Smuzhiyun     xGetSelectionOwnerReply reply;
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun     REQUEST(xResourceReq);
234*4882a593Smuzhiyun     REQUEST_SIZE_MATCH(xResourceReq);
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun     if (!ValidAtom(stuff->id)) {
237*4882a593Smuzhiyun         client->errorValue = stuff->id;
238*4882a593Smuzhiyun         return BadAtom;
239*4882a593Smuzhiyun     }
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun     reply = (xGetSelectionOwnerReply) {
242*4882a593Smuzhiyun         .type = X_Reply,
243*4882a593Smuzhiyun         .sequenceNumber = client->sequence,
244*4882a593Smuzhiyun         .length = 0,
245*4882a593Smuzhiyun     };
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun     rc = dixLookupSelection(&pSel, stuff->id, client, DixGetAttrAccess);
248*4882a593Smuzhiyun     if (rc == Success)
249*4882a593Smuzhiyun         reply.owner = pSel->window;
250*4882a593Smuzhiyun     else if (rc == BadMatch)
251*4882a593Smuzhiyun         reply.owner = None;
252*4882a593Smuzhiyun     else
253*4882a593Smuzhiyun         return rc;
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun     WriteReplyToClient(client, sizeof(xGetSelectionOwnerReply), &reply);
256*4882a593Smuzhiyun     return Success;
257*4882a593Smuzhiyun }
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun int
ProcConvertSelection(ClientPtr client)260*4882a593Smuzhiyun ProcConvertSelection(ClientPtr client)
261*4882a593Smuzhiyun {
262*4882a593Smuzhiyun     Bool paramsOkay;
263*4882a593Smuzhiyun     xEvent event;
264*4882a593Smuzhiyun     WindowPtr pWin;
265*4882a593Smuzhiyun     Selection *pSel;
266*4882a593Smuzhiyun     int rc;
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun     REQUEST(xConvertSelectionReq);
269*4882a593Smuzhiyun     REQUEST_SIZE_MATCH(xConvertSelectionReq);
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun     rc = dixLookupWindow(&pWin, stuff->requestor, client, DixSetAttrAccess);
272*4882a593Smuzhiyun     if (rc != Success)
273*4882a593Smuzhiyun         return rc;
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun     paramsOkay = ValidAtom(stuff->selection) && ValidAtom(stuff->target);
276*4882a593Smuzhiyun     paramsOkay &= (stuff->property == None) || ValidAtom(stuff->property);
277*4882a593Smuzhiyun     if (!paramsOkay) {
278*4882a593Smuzhiyun         client->errorValue = stuff->property;
279*4882a593Smuzhiyun         return BadAtom;
280*4882a593Smuzhiyun     }
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun     if (stuff->time == CurrentTime)
283*4882a593Smuzhiyun         UpdateCurrentTime();
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun     rc = dixLookupSelection(&pSel, stuff->selection, client, DixReadAccess);
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun     memset(&event, 0, sizeof(xEvent));
288*4882a593Smuzhiyun     if (rc != Success && rc != BadMatch)
289*4882a593Smuzhiyun         return rc;
290*4882a593Smuzhiyun     else if (rc == Success && pSel->window != None) {
291*4882a593Smuzhiyun         event.u.u.type = SelectionRequest;
292*4882a593Smuzhiyun         event.u.selectionRequest.owner = pSel->window;
293*4882a593Smuzhiyun         event.u.selectionRequest.time = stuff->time;
294*4882a593Smuzhiyun         event.u.selectionRequest.requestor = stuff->requestor;
295*4882a593Smuzhiyun         event.u.selectionRequest.selection = stuff->selection;
296*4882a593Smuzhiyun         event.u.selectionRequest.target = stuff->target;
297*4882a593Smuzhiyun         event.u.selectionRequest.property = stuff->property;
298*4882a593Smuzhiyun         if (pSel->client && pSel->client != serverClient &&
299*4882a593Smuzhiyun             !pSel->client->clientGone) {
300*4882a593Smuzhiyun             WriteEventsToClient(pSel->client, 1, &event);
301*4882a593Smuzhiyun             return Success;
302*4882a593Smuzhiyun         }
303*4882a593Smuzhiyun     }
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun     event.u.u.type = SelectionNotify;
306*4882a593Smuzhiyun     event.u.selectionNotify.time = stuff->time;
307*4882a593Smuzhiyun     event.u.selectionNotify.requestor = stuff->requestor;
308*4882a593Smuzhiyun     event.u.selectionNotify.selection = stuff->selection;
309*4882a593Smuzhiyun     event.u.selectionNotify.target = stuff->target;
310*4882a593Smuzhiyun     event.u.selectionNotify.property = None;
311*4882a593Smuzhiyun     WriteEventsToClient(client, 1, &event);
312*4882a593Smuzhiyun     return Success;
313*4882a593Smuzhiyun }
314