1 /***********************************************************
3 Copyright (c) 1987 X Consortium
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 Except as contained in this notice, the name of the X Consortium shall not be
23 used in advertising or otherwise to promote the sale, use or other dealings
24 in this Software without prior written authorization from the X Consortium.
27 Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
31 Permission to use, copy, modify, and distribute this software and its
32 documentation for any purpose and without fee is hereby granted,
33 provided that the above copyright notice appear in all copies and that
34 both that copyright notice and this permission notice appear in
35 supporting documentation, and that the name of Digital not be
36 used in advertising or publicity pertaining to distribution of the
37 software without specific, written prior permission.
39 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
40 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
41 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
42 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
43 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
44 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
47 ******************************************************************/
51 (c)Copyright 1988,1991 Adobe Systems Incorporated. All rights reserved.
53 Permission to use, copy, modify, distribute, and sublicense this software and its
54 documentation for any purpose and without fee is hereby granted, provided that
55 the above copyright notices appear in all copies and that both those copyright
56 notices and this permission notice appear in supporting documentation and that
57 the name of Adobe Systems Incorporated not be used in advertising or publicity
58 pertaining to distribution of the software without specific, written prior
59 permission. No trademark license to use the Adobe trademarks is hereby
60 granted. If the Adobe trademark "Display PostScript"(tm) is used to describe
61 this software, its functionality or for any other purpose, such use shall be
62 limited to a statement that this software works in conjunction with the Display
63 PostScript system. Proper trademark attribution to reflect Adobe's ownership
64 of the trademark shall be given whenever any such reference to the Display
65 PostScript system is made.
67 ADOBE MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THE SOFTWARE FOR ANY
68 PURPOSE. IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY. ADOBE
69 DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED
70 WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-
71 INFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL ADOBE BE LIABLE TO YOU
72 OR ANY OTHER PARTY FOR ANY SPECIAL, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
73 DAMAGES WHATSOEVER WHETHER IN AN ACTION OF CONTRACT,NEGLIGENCE, STRICT
74 LIABILITY OR ANY OTHER ACTION ARISING OUT OF OR IN CONNECTION WITH THE USE OR
75 PERFORMANCE OF THIS SOFTWARE. ADOBE WILL NOT PROVIDE ANY TRAINING OR OTHER
76 SUPPORT FOR THE SOFTWARE.
78 Adobe, PostScript, and Display PostScript are trademarks of Adobe Systems
79 Incorporated which may be registered in certain jurisdictions.
81 Author: Adobe Systems Incorporated
85 /* $TOG: dixutils.c /main/33 1997/05/22 10:02:20 kaleb $ */
90 /* $XFree86: xc/programs/Xserver/dix/dixutils.c,v 3.1.2.1 1997/05/23 12:19:35 dawes Exp $ */
95 #include "windowstr.h"
96 #include "dixstruct.h"
97 #include "pixmapstr.h"
98 #include "scrnintstr.h"
100 #include "keysymdef.h"
102 #define _SECURITY_SERVER
103 #include "extensions/security.h"
107 * CompareTimeStamps returns -1, 0, or +1 depending on if the first
108 * argument is less than, equal to or greater than the second argument.
112 CompareTimeStamps(a, b)
115 if (a.months < b.months)
117 if (a.months > b.months)
119 if (a.milliseconds < b.milliseconds)
121 if (a.milliseconds > b.milliseconds)
127 * convert client times to server TimeStamps
130 #define HALFMONTH ((unsigned long) 1<<31)
132 ClientTimeToServerTime(c)
136 if (c == CurrentTime)
138 ts.months = currentTime.months;
140 if (c > currentTime.milliseconds)
142 if (((unsigned long) c - currentTime.milliseconds) > HALFMONTH)
145 else if (c < currentTime.milliseconds)
147 if (((unsigned long)currentTime.milliseconds - c) > HALFMONTH)
154 * ISO Latin-1 case conversion routine
156 * this routine always null-terminates the result, so
157 * beware of too-small buffers
161 CopyISOLatin1Lowered(dest, source, length)
162 register unsigned char *dest, *source;
167 for (i = 0; i < length; i++, source++, dest++)
169 if ((*source >= XK_A) && (*source <= XK_Z))
170 *dest = *source + (XK_a - XK_A);
171 else if ((*source >= XK_Agrave) && (*source <= XK_Odiaeresis))
172 *dest = *source + (XK_agrave - XK_Agrave);
173 else if ((*source >= XK_Ooblique) && (*source <= XK_Thorn))
174 *dest = *source + (XK_oslash - XK_Ooblique);
183 /* SecurityLookupWindow and SecurityLookupDrawable:
184 * Look up the window/drawable taking into account the client doing
185 * the lookup and the type of access desired. Return the window/drawable
186 * if it exists and the client is allowed access, else return NULL.
187 * Most Proc* functions should be calling these instead of
188 * LookupWindow and LookupDrawable, which do no access checks.
192 SecurityLookupWindow(rid, client, access_mode)
199 client->errorValue = rid;
202 if (client->trustLevel != XSecurityClientTrusted)
203 return (WindowPtr)SecurityLookupIDByType(client, rid, RT_WINDOW, access_mode);
204 if (client->lastDrawableID == rid)
206 if (client->lastDrawable->type == DRAWABLE_WINDOW)
207 return ((WindowPtr) client->lastDrawable);
208 return (WindowPtr) NULL;
210 pWin = (WindowPtr)SecurityLookupIDByType(client, rid, RT_WINDOW, access_mode);
211 if (pWin && pWin->drawable.type == DRAWABLE_WINDOW) {
212 client->lastDrawable = (DrawablePtr) pWin;
213 client->lastDrawableID = rid;
214 client->lastGCID = INVALID;
215 client->lastGC = (GCPtr)NULL;
222 SecurityLookupDrawable(rid, client, access_mode)
227 register DrawablePtr pDraw;
230 return (pointer) NULL;
231 if (client->trustLevel != XSecurityClientTrusted)
232 return (DrawablePtr)SecurityLookupIDByClass(client, rid, RC_DRAWABLE,
234 if (client->lastDrawableID == rid)
235 return ((pointer) client->lastDrawable);
236 pDraw = (DrawablePtr)SecurityLookupIDByClass(client, rid, RC_DRAWABLE,
238 if (pDraw && (pDraw->type != UNDRAWABLE_WINDOW))
239 return (pointer)pDraw;
240 return (pointer)NULL;
243 /* We can't replace the LookupWindow and LookupDrawable functions with
244 * macros because of compatibility with loadable servers.
248 LookupWindow(rid, client)
252 return SecurityLookupWindow(rid, client, SecurityUnknownAccess);
256 LookupDrawable(rid, client)
260 return SecurityLookupDrawable(rid, client, SecurityUnknownAccess);
263 #else /* not XCSECURITY */
266 LookupWindow(rid, client)
272 client->errorValue = rid;
275 if (client->lastDrawableID == rid)
277 if (client->lastDrawable->type == DRAWABLE_WINDOW)
278 return ((WindowPtr) client->lastDrawable);
279 return (WindowPtr) NULL;
281 pWin = (WindowPtr)LookupIDByType(rid, RT_WINDOW);
282 if (pWin && pWin->drawable.type == DRAWABLE_WINDOW) {
283 client->lastDrawable = (DrawablePtr) pWin;
284 client->lastDrawableID = rid;
285 client->lastGCID = INVALID;
286 client->lastGC = (GCPtr)NULL;
293 LookupDrawable(rid, client)
297 register DrawablePtr pDraw;
300 return (pointer) NULL;
301 if (client->lastDrawableID == rid)
302 return ((pointer) client->lastDrawable);
303 pDraw = (DrawablePtr)LookupIDByClass(rid, RC_DRAWABLE);
304 if (pDraw && (pDraw->type != UNDRAWABLE_WINDOW))
305 return (pointer)pDraw;
306 return (pointer)NULL;
309 #endif /* XCSECURITY */
312 LookupClient(rid, client)
316 pointer pRes = (pointer)SecurityLookupIDByClass(client, rid, RC_ANY,
318 int clientIndex = CLIENT_ID(rid);
320 if (clientIndex && pRes && clients[clientIndex] && !(rid & SERVER_BIT))
322 return clients[clientIndex];
324 return (ClientPtr)NULL;
329 AlterSaveSetForClient(client, pWin, mode)
338 numnow = client->numSaved;
342 pTmp = client->saveSet;
343 while ((j < numnow) && (pTmp[j] != (pointer)pWin))
346 if (mode == SetModeInsert)
348 if (j < numnow) /* duplicate */
351 pTmp = (pointer *)xrealloc(client->saveSet, sizeof(pointer) * numnow);
354 client->saveSet = pTmp;
355 client->numSaved = numnow;
356 client->saveSet[numnow - 1] = (pointer)pWin;
359 else if ((mode == SetModeDelete) && (j < numnow))
369 pTmp = (pointer *)xrealloc(client->saveSet,
370 sizeof(pointer) * numnow);
372 client->saveSet = pTmp;
376 xfree(client->saveSet);
377 client->saveSet = (pointer *)NULL;
379 client->numSaved = numnow;
386 DeleteWindowFromAnySaveSet(pWin)
390 register ClientPtr client;
392 for (i = 0; i< currentMaxClients; i++)
395 if (client && client->numSaved)
396 (void)AlterSaveSetForClient(client, pWin, SetModeDelete);
400 /* No-op Don't Do Anything : sometimes we need to be able to call a procedure
401 * that doesn't do anything. For example, on screen with only static
402 * colormaps, if someone calls install colormap, it's easier to have a dummy
403 * procedure to call than to check if there's a procedure
407 #if NeedVarargsPrototypes
414 typedef struct _BlockHandler {
415 BlockHandlerProcPtr BlockHandler;
416 WakeupHandlerProcPtr WakeupHandler;
419 } BlockHandlerRec, *BlockHandlerPtr;
421 static BlockHandlerPtr handlers;
422 static int numHandlers;
423 static int sizeHandlers;
424 static Bool inHandler;
425 static Bool handlerDeleted;
427 /* called from the OS layer */
429 BlockHandler(pTimeout, pReadmask)
430 pointer pTimeout; /* DIX doesn't want to know how OS represents time */
431 pointer pReadmask; /* nor how it represents the set of descriptors */
436 for (i = 0; i < screenInfo.numScreens; i++)
437 (* screenInfo.screens[i]->BlockHandler)(i,
438 screenInfo.screens[i]->blockData,
439 pTimeout, pReadmask);
440 for (i = 0; i < numHandlers; i++)
441 (*handlers[i].BlockHandler) (handlers[i].blockData,
442 pTimeout, pReadmask);
445 for (i = 0; i < numHandlers;)
446 if (handlers[i].deleted)
448 for (j = i; j < numHandlers - 1; j++)
449 handlers[j] = handlers[j+1];
454 handlerDeleted = FALSE;
460 WakeupHandler(result, pReadmask)
461 int result; /* 32 bits of undefined result from the wait */
462 pointer pReadmask; /* the resulting descriptor mask */
467 for (i = numHandlers - 1; i >= 0; i--)
468 (*handlers[i].WakeupHandler) (handlers[i].blockData,
470 for (i = 0; i < screenInfo.numScreens; i++)
471 (* screenInfo.screens[i]->WakeupHandler)(i,
472 screenInfo.screens[i]->wakeupData,
476 for (i = 0; i < numHandlers;)
477 if (handlers[i].deleted)
479 for (j = i; j < numHandlers - 1; j++)
480 handlers[j] = handlers[j+1];
485 handlerDeleted = FALSE;
490 /* Reentrant with BlockHandler and WakeupHandler, except wakeup won't
491 * get called until next time
495 RegisterBlockAndWakeupHandlers (blockHandler, wakeupHandler, blockData)
496 BlockHandlerProcPtr blockHandler;
497 WakeupHandlerProcPtr wakeupHandler;
502 if (numHandlers >= sizeHandlers)
504 new = (BlockHandlerPtr) xrealloc (handlers, (numHandlers + 1) *
505 sizeof (BlockHandlerRec));
509 sizeHandlers = numHandlers + 1;
511 handlers[numHandlers].BlockHandler = blockHandler;
512 handlers[numHandlers].WakeupHandler = wakeupHandler;
513 handlers[numHandlers].blockData = blockData;
514 handlers[numHandlers].deleted = FALSE;
515 numHandlers = numHandlers + 1;
520 RemoveBlockAndWakeupHandlers (blockHandler, wakeupHandler, blockData)
521 BlockHandlerProcPtr blockHandler;
522 WakeupHandlerProcPtr wakeupHandler;
527 for (i = 0; i < numHandlers; i++)
528 if (handlers[i].BlockHandler == blockHandler &&
529 handlers[i].WakeupHandler == wakeupHandler &&
530 handlers[i].blockData == blockData)
534 handlerDeleted = TRUE;
535 handlers[i].deleted = TRUE;
539 for (; i < numHandlers - 1; i++)
540 handlers[i] = handlers[i+1];
548 InitBlockAndWakeupHandlers ()
551 handlers = (BlockHandlerPtr) 0;
557 * A general work queue. Perform some task before the server
561 WorkQueuePtr workQueue;
562 static WorkQueuePtr *workQueueLast = &workQueue;
571 * Scan the work queue once, calling each function. Those
572 * which return TRUE are removed from the queue, otherwise
573 * they will be called again. This must be reentrant with
578 if ((*q->function) (q->client, q->closure))
580 /* remove q from the list */
581 *p = q->next; /* don't fetch until after func called */
586 p = &q->next; /* don't fetch until after func called */
593 ProcessWorkQueueZombies()
600 if (q->client && q->client->clientGone)
602 (void) (*q->function) (q->client, q->closure);
603 /* remove q from the list */
604 *p = q->next; /* don't fetch until after func called */
609 p = &q->next; /* don't fetch until after func called */
616 #if NeedFunctionPrototypes
619 #if NeedNestedPrototypes
620 ClientPtr /* pClient */,
621 pointer /* closure */
627 QueueWorkProc (function, client, closure)
635 q = (WorkQueuePtr) xalloc (sizeof *q);
638 q->function = function;
640 q->closure = closure;
643 workQueueLast = &q->next;
648 * Manage a queue of sleeping clients, awakening them
649 * when requested, by using the OS functions IgnoreClient
650 * and AttendClient. Note that this *ignores* the troubles
651 * with request data interleaving itself with events, but
652 * we'll leave that until a later time.
655 typedef struct _SleepQueue {
656 struct _SleepQueue *next;
658 ClientSleepProcPtr function;
660 } SleepQueueRec, *SleepQueuePtr;
662 static SleepQueuePtr sleepQueue = NULL;
665 ClientSleep (client, function, closure)
667 ClientSleepProcPtr function;
672 q = (SleepQueuePtr) xalloc (sizeof *q);
676 IgnoreClient (client);
677 q->next = sleepQueue;
679 q->function = function;
680 q->closure = closure;
686 ClientSignal (client)
691 for (q = sleepQueue; q; q = q->next)
692 if (q->client == client)
694 return QueueWorkProc (q->function, q->client, q->closure);
700 ClientWakeup (client)
703 SleepQueuePtr q, *prev;
706 while ( (q = *prev) )
708 if (q->client == client)
712 if (client->clientGone)
713 CloseDownClient(client);
715 AttendClient (client);
723 ClientIsAsleep (client)
728 for (q = sleepQueue; q; q = q->next)
729 if (q->client == client)
735 * Generic Callback Manager
738 /* ===== Private Procedures ===== */
740 static int numCallbackListsToCleanup = 0;
741 static CallbackListPtr **listsToCleanup = NULL;
744 #if NeedFunctionPrototypes
746 CallbackListPtr *pcbl,
747 CallbackProcPtr callback,
750 _AddCallback(pcbl, callback, data)
751 CallbackListPtr *pcbl;
752 CallbackProcPtr callback;
758 cbr = (CallbackPtr) xalloc(sizeof(CallbackRec));
761 cbr->proc = callback;
763 cbr->next = (*pcbl)->list;
764 cbr->deleted = FALSE;
770 #if NeedFunctionPrototypes
772 CallbackListPtr *pcbl,
773 CallbackProcPtr callback,
776 _DeleteCallback(pcbl, callback, data)
777 CallbackListPtr *pcbl;
778 CallbackProcPtr callback;
782 CallbackListPtr cbl = *pcbl;
783 CallbackPtr cbr, pcbr;
785 for (pcbr = NULL, cbr = cbl->list;
787 pcbr = cbr, cbr = cbr->next)
789 if ((cbr->proc == callback) && (cbr->data == data))
802 cbl->list = cbr->next;
804 pcbr->next = cbr->next;
813 #if NeedFunctionPrototypes
815 CallbackListPtr *pcbl,
818 _CallCallbacks(pcbl, call_data)
819 CallbackListPtr *pcbl;
823 CallbackListPtr cbl = *pcbl;
824 CallbackPtr cbr, pcbr;
827 for (cbr = cbl->list; cbr != NULL; cbr = cbr->next)
829 (*(cbr->proc)) (pcbl, cbr->data, call_data);
833 if (cbl->inCallback) return;
835 /* Was the entire list marked for deletion? */
839 DeleteCallbackList(pcbl);
843 /* Were some individual callbacks on the list marked for deletion?
844 * If so, do the deletions.
849 for (pcbr = NULL, cbr = cbl->list; (cbr != NULL) && cbl->numDeleted; )
866 else /* this one wasn't deleted */
876 #if NeedFunctionPrototypes
878 CallbackListPtr *pcbl)
880 _DeleteCallbackList(pcbl)
881 CallbackListPtr *pcbl;
884 CallbackListPtr cbl = *pcbl;
885 CallbackPtr cbr, nextcbr;
894 for (i = 0; i < numCallbackListsToCleanup; i++)
896 if ((listsToCleanup[i] = pcbl) != 0)
898 listsToCleanup[i] = NULL;
903 for (cbr = cbl->list; cbr != NULL; cbr = nextcbr)
912 static CallbackFuncsRec default_cbfuncs =
920 /* ===== Public Procedures ===== */
923 CreateCallbackList(pcbl, cbfuncs)
924 CallbackListPtr *pcbl;
925 CallbackFuncsPtr cbfuncs;
930 if (!pcbl) return FALSE;
931 cbl = (CallbackListPtr) xalloc(sizeof(CallbackListRec));
932 if (!cbl) return FALSE;
933 cbl->funcs = cbfuncs ? *cbfuncs : default_cbfuncs;
935 cbl->deleted = FALSE;
940 for (i = 0; i < numCallbackListsToCleanup; i++)
942 if (!listsToCleanup[i])
944 listsToCleanup[i] = pcbl;
949 listsToCleanup = (CallbackListPtr **)xnfrealloc(listsToCleanup,
950 sizeof(CallbackListPtr *) * (numCallbackListsToCleanup+1));
951 listsToCleanup[numCallbackListsToCleanup] = pcbl;
952 numCallbackListsToCleanup++;
957 AddCallback(pcbl, callback, data)
958 CallbackListPtr *pcbl;
959 CallbackProcPtr callback;
962 if (!pcbl) return FALSE;
964 { /* list hasn't been created yet; go create it */
965 if (!CreateCallbackList(pcbl, (CallbackFuncsPtr)NULL))
968 return ((*(*pcbl)->funcs.AddCallback) (pcbl, callback, data));
972 DeleteCallback(pcbl, callback, data)
973 CallbackListPtr *pcbl;
974 CallbackProcPtr callback;
977 if (!pcbl || !*pcbl) return FALSE;
978 return ((*(*pcbl)->funcs.DeleteCallback) (pcbl, callback, data));
982 CallCallbacks(pcbl, call_data)
983 CallbackListPtr *pcbl;
986 if (!pcbl || !*pcbl) return;
987 (*(*pcbl)->funcs.CallCallbacks) (pcbl, call_data);
991 DeleteCallbackList(pcbl)
992 CallbackListPtr *pcbl;
994 if (!pcbl || !*pcbl) return;
995 (*(*pcbl)->funcs.DeleteCallbackList) (pcbl);
999 InitCallbackManager()
1003 for (i = 0; i < numCallbackListsToCleanup; i++)
1005 DeleteCallbackList(listsToCleanup[i]);
1007 if (listsToCleanup) xfree(listsToCleanup);
1009 numCallbackListsToCleanup = 0;
1010 listsToCleanup = NULL;