1 /***********************************************************
3 Copyright 1987, 1998 The Open Group
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 Except as contained in this notice, the name of The Open Group shall not be
22 used in advertising or otherwise to promote the sale, use or other dealings
23 in this Software without prior written authorization from The Open Group.
25 Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
29 Permission to use, copy, modify, and distribute this software and its
30 documentation for any purpose and without fee is hereby granted,
31 provided that the above copyright notice appear in all copies and that
32 both that copyright notice and this permission notice appear in
33 supporting documentation, and that the name of Digital not be
34 used in advertising or publicity pertaining to distribution of the
35 software without specific, written prior permission.
37 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
38 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
39 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
40 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
41 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
42 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
45 ******************************************************************/
49 (c)Copyright 1988,1991 Adobe Systems Incorporated. All rights reserved.
51 Permission to use, copy, modify, distribute, and sublicense this software and its
52 documentation for any purpose and without fee is hereby granted, provided that
53 the above copyright notices appear in all copies and that both those copyright
54 notices and this permission notice appear in supporting documentation and that
55 the name of Adobe Systems Incorporated not be used in advertising or publicity
56 pertaining to distribution of the software without specific, written prior
57 permission. No trademark license to use the Adobe trademarks is hereby
58 granted. If the Adobe trademark "Display PostScript"(tm) is used to describe
59 this software, its functionality or for any other purpose, such use shall be
60 limited to a statement that this software works in conjunction with the Display
61 PostScript system. Proper trademark attribution to reflect Adobe's ownership
62 of the trademark shall be given whenever any such reference to the Display
63 PostScript system is made.
65 ADOBE MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THE SOFTWARE FOR ANY
66 PURPOSE. IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY. ADOBE
67 DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED
68 WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-
69 INFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL ADOBE BE LIABLE TO YOU
70 OR ANY OTHER PARTY FOR ANY SPECIAL, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
71 DAMAGES WHATSOEVER WHETHER IN AN ACTION OF CONTRACT,NEGLIGENCE, STRICT
72 LIABILITY OR ANY OTHER ACTION ARISING OUT OF OR IN CONNECTION WITH THE USE OR
73 PERFORMANCE OF THIS SOFTWARE. ADOBE WILL NOT PROVIDE ANY TRAINING OR OTHER
74 SUPPORT FOR THE SOFTWARE.
76 Adobe, PostScript, and Display PostScript are trademarks of Adobe Systems
77 Incorporated which may be registered in certain jurisdictions.
79 Author: Adobe Systems Incorporated
83 #ifdef HAVE_DIX_CONFIG_H
84 #include <dix-config.h>
90 #include "windowstr.h"
91 #include "dixstruct.h"
92 #include "pixmapstr.h"
94 #include "scrnintstr.h"
96 #include <X11/keysymdef.h>
100 * CompareTimeStamps returns -1, 0, or +1 depending on if the first
101 * argument is less than, equal to or greater than the second argument.
105 CompareTimeStamps(TimeStamp a
, TimeStamp b
)
107 if (a
.months
< b
.months
)
109 if (a
.months
> b
.months
)
111 if (a
.milliseconds
< b
.milliseconds
)
113 if (a
.milliseconds
> b
.milliseconds
)
119 * convert client times to server TimeStamps
122 #define HALFMONTH ((unsigned long) 1<<31)
124 ClientTimeToServerTime(CARD32 c
)
128 if (c
== CurrentTime
)
130 ts
.months
= currentTime
.months
;
132 if (c
> currentTime
.milliseconds
) {
133 if (((unsigned long) c
- currentTime
.milliseconds
) > HALFMONTH
)
136 else if (c
< currentTime
.milliseconds
) {
137 if (((unsigned long) currentTime
.milliseconds
- c
) > HALFMONTH
)
144 * ISO Latin-1 case conversion routine
146 * this routine always null-terminates the result, so
147 * beware of too-small buffers
151 ISOLatin1ToLower(unsigned char source
)
155 if ((source
>= XK_A
) && (source
<= XK_Z
))
156 dest
= source
+ (XK_a
- XK_A
);
157 else if ((source
>= XK_Agrave
) && (source
<= XK_Odiaeresis
))
158 dest
= source
+ (XK_agrave
- XK_Agrave
);
159 else if ((source
>= XK_Ooblique
) && (source
<= XK_Thorn
))
160 dest
= source
+ (XK_oslash
- XK_Ooblique
);
167 CompareISOLatin1Lowered(const unsigned char *s1
, int s1len
,
168 const unsigned char *s2
, int s2len
)
170 unsigned char c1
, c2
;
173 /* note -- compare against zero so that -1 ignores len */
174 c1
= s1len
-- ? *s1
++ : '\0';
175 c2
= s2len
-- ? *s2
++ : '\0';
178 (c1
= ISOLatin1ToLower(c1
)) != (c2
= ISOLatin1ToLower(c2
))))
181 return (int) c1
- (int) c2
;
185 * dixLookupWindow and dixLookupDrawable:
186 * Look up the window/drawable taking into account the client doing the
187 * lookup, the type of drawable desired, and the type of access desired.
188 * Return Success with *pDraw set if the window/drawable exists and the client
189 * is allowed access, else return an error code with *pDraw set to NULL. The
190 * access mask values are defined in resource.h. The type mask values are
191 * defined in pixmap.h, with zero equivalent to M_DRAWABLE.
194 dixLookupDrawable(DrawablePtr
*pDraw
, XID id
, ClientPtr client
,
195 Mask type
, Mask access
)
202 rc
= dixLookupResourceByClass((pointer
*) &pTmp
, id
, RC_DRAWABLE
, client
,
206 client
->errorValue
= id
;
212 if (!((1 << pTmp
->type
) & (type
? type
: M_DRAWABLE
)))
220 dixLookupWindow(WindowPtr
*pWin
, XID id
, ClientPtr client
, Mask access
)
224 rc
= dixLookupDrawable((DrawablePtr
*) pWin
, id
, client
, M_WINDOW
, access
);
225 /* dixLookupDrawable returns BadMatch iff id is a valid Drawable
226 but is not a Window. Users of dixLookupWindow expect a BadWindow
227 error in this case; they don't care that it's a valid non-Window XID */
230 /* Similarly, users of dixLookupWindow don't want BadDrawable. */
231 if (rc
== BadDrawable
)
237 dixLookupGC(GCPtr
*pGC
, XID id
, ClientPtr client
, Mask access
)
239 return dixLookupResourceByType((pointer
*) pGC
, id
, RT_GC
, client
, access
);
243 dixLookupFontable(FontPtr
*pFont
, XID id
, ClientPtr client
, Mask access
)
248 client
->errorValue
= id
; /* EITHER font or gc */
249 rc
= dixLookupResourceByType((pointer
*) pFont
, id
, RT_FONT
, client
,
253 rc
= dixLookupResourceByType((pointer
*) &pGC
, id
, RT_GC
, client
, access
);
262 dixLookupClient(ClientPtr
*pClient
, XID rid
, ClientPtr client
, Mask access
)
265 int rc
= BadValue
, clientIndex
= CLIENT_ID(rid
);
267 if (!clientIndex
|| !clients
[clientIndex
] || (rid
& SERVER_BIT
))
270 rc
= dixLookupResourceByClass(&pRes
, rid
, RC_ANY
, client
, DixGetAttrAccess
);
274 rc
= XaceHook(XACE_CLIENT_ACCESS
, client
, clients
[clientIndex
], access
);
278 *pClient
= clients
[clientIndex
];
282 client
->errorValue
= rid
;
288 AlterSaveSetForClient(ClientPtr client
, WindowPtr pWin
, unsigned mode
,
289 Bool toRoot
, Bool map
)
292 SaveSetElt
*pTmp
= NULL
;
295 numnow
= client
->numSaved
;
298 pTmp
= client
->saveSet
;
299 while ((j
< numnow
) && (SaveSetWindow(pTmp
[j
]) != (pointer
) pWin
))
302 if (mode
== SetModeInsert
) {
303 if (j
< numnow
) /* duplicate */
306 pTmp
= (SaveSetElt
*) realloc(client
->saveSet
, sizeof(*pTmp
) * numnow
);
309 client
->saveSet
= pTmp
;
310 client
->numSaved
= numnow
;
311 SaveSetAssignWindow(client
->saveSet
[numnow
- 1], pWin
);
312 SaveSetAssignToRoot(client
->saveSet
[numnow
- 1], toRoot
);
313 SaveSetAssignMap(client
->saveSet
[numnow
- 1], map
);
316 else if ((mode
== SetModeDelete
) && (j
< numnow
)) {
317 while (j
< numnow
- 1) {
318 pTmp
[j
] = pTmp
[j
+ 1];
324 (SaveSetElt
*) realloc(client
->saveSet
, sizeof(*pTmp
) * numnow
);
326 client
->saveSet
= pTmp
;
329 free(client
->saveSet
);
330 client
->saveSet
= (SaveSetElt
*) NULL
;
332 client
->numSaved
= numnow
;
339 DeleteWindowFromAnySaveSet(WindowPtr pWin
)
344 for (i
= 0; i
< currentMaxClients
; i
++) {
346 if (client
&& client
->numSaved
)
347 (void) AlterSaveSetForClient(client
, pWin
, SetModeDelete
, FALSE
,
352 /* No-op Don't Do Anything : sometimes we need to be able to call a procedure
353 * that doesn't do anything. For example, on screen with only static
354 * colormaps, if someone calls install colormap, it's easier to have a dummy
355 * procedure to call than to check if there's a procedure
362 typedef struct _BlockHandler
{
363 BlockHandlerProcPtr BlockHandler
;
364 WakeupHandlerProcPtr WakeupHandler
;
367 } BlockHandlerRec
, *BlockHandlerPtr
;
369 static BlockHandlerPtr handlers
;
370 static int numHandlers
;
371 static int sizeHandlers
;
372 static Bool inHandler
;
373 static Bool handlerDeleted
;
377 * \param pTimeout DIX doesn't want to know how OS represents time
378 * \param pReadMask nor how it represents the det of descriptors
381 BlockHandler(pointer pTimeout
, pointer pReadmask
)
386 for (i
= 0; i
< screenInfo
.numScreens
; i
++)
387 (*screenInfo
.screens
[i
]->BlockHandler
) (screenInfo
.screens
[i
],
388 pTimeout
, pReadmask
);
389 for (i
= 0; i
< screenInfo
.numGPUScreens
; i
++)
390 (*screenInfo
.gpuscreens
[i
]->BlockHandler
) (screenInfo
.gpuscreens
[i
],
391 pTimeout
, pReadmask
);
392 for (i
= 0; i
< numHandlers
; i
++)
393 if (!handlers
[i
].deleted
)
394 (*handlers
[i
].BlockHandler
) (handlers
[i
].blockData
,
395 pTimeout
, pReadmask
);
396 if (handlerDeleted
) {
397 for (i
= 0; i
< numHandlers
;)
398 if (handlers
[i
].deleted
) {
399 for (j
= i
; j
< numHandlers
- 1; j
++)
400 handlers
[j
] = handlers
[j
+ 1];
405 handlerDeleted
= FALSE
;
412 * \param result 32 bits of undefined result from the wait
413 * \param pReadmask the resulting descriptor mask
416 WakeupHandler(int result
, pointer pReadmask
)
421 for (i
= numHandlers
- 1; i
>= 0; i
--)
422 if (!handlers
[i
].deleted
)
423 (*handlers
[i
].WakeupHandler
) (handlers
[i
].blockData
,
425 for (i
= 0; i
< screenInfo
.numScreens
; i
++)
426 (*screenInfo
.screens
[i
]->WakeupHandler
) (screenInfo
.screens
[i
],
428 for (i
= 0; i
< screenInfo
.numGPUScreens
; i
++)
429 (*screenInfo
.gpuscreens
[i
]->WakeupHandler
) (screenInfo
.gpuscreens
[i
],
431 if (handlerDeleted
) {
432 for (i
= 0; i
< numHandlers
;)
433 if (handlers
[i
].deleted
) {
434 for (j
= i
; j
< numHandlers
- 1; j
++)
435 handlers
[j
] = handlers
[j
+ 1];
440 handlerDeleted
= FALSE
;
446 * Reentrant with BlockHandler and WakeupHandler, except wakeup won't
447 * get called until next time
450 RegisterBlockAndWakeupHandlers(BlockHandlerProcPtr blockHandler
,
451 WakeupHandlerProcPtr wakeupHandler
,
456 if (numHandlers
>= sizeHandlers
) {
457 new = (BlockHandlerPtr
) realloc(handlers
, (numHandlers
+ 1) *
458 sizeof(BlockHandlerRec
));
462 sizeHandlers
= numHandlers
+ 1;
464 handlers
[numHandlers
].BlockHandler
= blockHandler
;
465 handlers
[numHandlers
].WakeupHandler
= wakeupHandler
;
466 handlers
[numHandlers
].blockData
= blockData
;
467 handlers
[numHandlers
].deleted
= FALSE
;
468 numHandlers
= numHandlers
+ 1;
473 RemoveBlockAndWakeupHandlers(BlockHandlerProcPtr blockHandler
,
474 WakeupHandlerProcPtr wakeupHandler
,
479 for (i
= 0; i
< numHandlers
; i
++)
480 if (handlers
[i
].BlockHandler
== blockHandler
&&
481 handlers
[i
].WakeupHandler
== wakeupHandler
&&
482 handlers
[i
].blockData
== blockData
) {
484 handlerDeleted
= TRUE
;
485 handlers
[i
].deleted
= TRUE
;
488 for (; i
< numHandlers
- 1; i
++)
489 handlers
[i
] = handlers
[i
+ 1];
497 InitBlockAndWakeupHandlers(void)
500 handlers
= (BlockHandlerPtr
) 0;
506 * A general work queue. Perform some task before the server
510 WorkQueuePtr workQueue
;
511 static WorkQueuePtr
*workQueueLast
= &workQueue
;
514 ProcessWorkQueue(void)
520 * Scan the work queue once, calling each function. Those
521 * which return TRUE are removed from the queue, otherwise
522 * they will be called again. This must be reentrant with
526 if ((*q
->function
) (q
->client
, q
->closure
)) {
527 /* remove q from the list */
528 *p
= q
->next
; /* don't fetch until after func called */
532 p
= &q
->next
; /* don't fetch until after func called */
539 ProcessWorkQueueZombies(void)
545 if (q
->client
&& q
->client
->clientGone
) {
546 (void) (*q
->function
) (q
->client
, q
->closure
);
547 /* remove q from the list */
548 *p
= q
->next
; /* don't fetch until after func called */
552 p
= &q
->next
; /* don't fetch until after func called */
559 QueueWorkProc(Bool (*function
)
560 (ClientPtr
/* pClient */ , pointer
/* closure */ ),
561 ClientPtr client
, pointer closure
)
565 q
= malloc(sizeof *q
);
568 q
->function
= function
;
570 q
->closure
= closure
;
573 workQueueLast
= &q
->next
;
578 * Manage a queue of sleeping clients, awakening them
579 * when requested, by using the OS functions IgnoreClient
580 * and AttendClient. Note that this *ignores* the troubles
581 * with request data interleaving itself with events, but
582 * we'll leave that until a later time.
585 typedef struct _SleepQueue
{
586 struct _SleepQueue
*next
;
588 ClientSleepProcPtr function
;
590 } SleepQueueRec
, *SleepQueuePtr
;
592 static SleepQueuePtr sleepQueue
= NULL
;
595 ClientSleep(ClientPtr client
, ClientSleepProcPtr function
, pointer closure
)
599 q
= malloc(sizeof *q
);
603 IgnoreClient(client
);
604 q
->next
= sleepQueue
;
606 q
->function
= function
;
607 q
->closure
= closure
;
613 ClientSignal(ClientPtr client
)
617 for (q
= sleepQueue
; q
; q
= q
->next
)
618 if (q
->client
== client
) {
619 return QueueWorkProc(q
->function
, q
->client
, q
->closure
);
625 ClientWakeup(ClientPtr client
)
627 SleepQueuePtr q
, *prev
;
630 while ((q
= *prev
)) {
631 if (q
->client
== client
) {
634 if (client
->clientGone
)
635 /* Oops -- new zombie cleanup code ensures this only
636 * happens from inside CloseDownClient; don't want to
639 /* CloseDownClient(client) */ ;
641 AttendClient(client
);
649 ClientIsAsleep(ClientPtr client
)
653 for (q
= sleepQueue
; q
; q
= q
->next
)
654 if (q
->client
== client
)
660 * Generic Callback Manager
663 /* ===== Private Procedures ===== */
665 static int numCallbackListsToCleanup
= 0;
666 static CallbackListPtr
**listsToCleanup
= NULL
;
669 _AddCallback(CallbackListPtr
*pcbl
, CallbackProcPtr callback
, pointer data
)
673 cbr
= malloc(sizeof(CallbackRec
));
676 cbr
->proc
= callback
;
678 cbr
->next
= (*pcbl
)->list
;
679 cbr
->deleted
= FALSE
;
685 _DeleteCallback(CallbackListPtr
*pcbl
, CallbackProcPtr callback
, pointer data
)
687 CallbackListPtr cbl
= *pcbl
;
688 CallbackPtr cbr
, pcbr
;
690 for (pcbr
= NULL
, cbr
= cbl
->list
; cbr
!= NULL
; pcbr
= cbr
, cbr
= cbr
->next
) {
691 if ((cbr
->proc
== callback
) && (cbr
->data
== data
))
695 if (cbl
->inCallback
) {
701 cbl
->list
= cbr
->next
;
703 pcbr
->next
= cbr
->next
;
712 _CallCallbacks(CallbackListPtr
*pcbl
, pointer call_data
)
714 CallbackListPtr cbl
= *pcbl
;
715 CallbackPtr cbr
, pcbr
;
718 for (cbr
= cbl
->list
; cbr
!= NULL
; cbr
= cbr
->next
) {
719 (*(cbr
->proc
)) (pcbl
, cbr
->data
, call_data
);
726 /* Was the entire list marked for deletion? */
729 DeleteCallbackList(pcbl
);
733 /* Were some individual callbacks on the list marked for deletion?
734 * If so, do the deletions.
737 if (cbl
->numDeleted
) {
738 for (pcbr
= NULL
, cbr
= cbl
->list
; (cbr
!= NULL
) && cbl
->numDeleted
;) {
752 else { /* this one wasn't deleted */
762 _DeleteCallbackList(CallbackListPtr
*pcbl
)
764 CallbackListPtr cbl
= *pcbl
;
765 CallbackPtr cbr
, nextcbr
;
768 if (cbl
->inCallback
) {
773 for (i
= 0; i
< numCallbackListsToCleanup
; i
++) {
774 if (listsToCleanup
[i
] == pcbl
) {
775 listsToCleanup
[i
] = NULL
;
780 for (cbr
= cbl
->list
; cbr
!= NULL
; cbr
= nextcbr
) {
789 CreateCallbackList(CallbackListPtr
*pcbl
)
796 cbl
= malloc(sizeof(CallbackListRec
));
800 cbl
->deleted
= FALSE
;
805 for (i
= 0; i
< numCallbackListsToCleanup
; i
++) {
806 if (!listsToCleanup
[i
]) {
807 listsToCleanup
[i
] = pcbl
;
812 listsToCleanup
= (CallbackListPtr
**) xnfrealloc(listsToCleanup
,
813 sizeof(CallbackListPtr
*) *
814 (numCallbackListsToCleanup
816 listsToCleanup
[numCallbackListsToCleanup
] = pcbl
;
817 numCallbackListsToCleanup
++;
821 /* ===== Public Procedures ===== */
824 AddCallback(CallbackListPtr
*pcbl
, CallbackProcPtr callback
, pointer data
)
828 if (!*pcbl
) { /* list hasn't been created yet; go create it */
829 if (!CreateCallbackList(pcbl
))
832 return _AddCallback(pcbl
, callback
, data
);
836 DeleteCallback(CallbackListPtr
*pcbl
, CallbackProcPtr callback
, pointer data
)
840 return _DeleteCallback(pcbl
, callback
, data
);
844 DeleteCallbackList(CallbackListPtr
*pcbl
)
848 _DeleteCallbackList(pcbl
);
852 DeleteCallbackManager(void)
856 for (i
= 0; i
< numCallbackListsToCleanup
; i
++) {
857 DeleteCallbackList(listsToCleanup
[i
]);
859 free(listsToCleanup
);
861 numCallbackListsToCleanup
= 0;
862 listsToCleanup
= NULL
;
866 InitCallbackManager(void)
868 DeleteCallbackManager();