1/***********************************************************
2
3Copyright 1987, 1998  The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21Except as contained in this notice, the name of The Open Group shall not be
22used in advertising or otherwise to promote the sale, use or other dealings
23in this Software without prior written authorization from The Open Group.
24
25
26Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
27
28                        All Rights Reserved
29
30Permission to use, copy, modify, and distribute this software and its
31documentation for any purpose and without fee is hereby granted,
32provided that the above copyright notice appear in all copies and that
33both that copyright notice and this permission notice appear in
34supporting documentation, and that the name of Digital not be
35used in advertising or publicity pertaining to distribution of the
36software without specific, written prior permission.
37
38DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
39ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
40DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
41ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
42WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
43ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
44SOFTWARE.
45
46******************************************************************/
47
48#ifndef DIX_H
49#define DIX_H
50
51#include "callback.h"
52#include "gc.h"
53#include "window.h"
54#include "input.h"
55#include "cursor.h"
56#include "geext.h"
57#include "events.h"
58#include <X11/extensions/XI.h>
59
60#define EARLIER -1
61#define SAMETIME 0
62#define LATER 1
63
64#define NullClient ((ClientPtr) 0)
65#define REQUEST(type) \
66	type *stuff = (type *)client->requestBuffer
67
68
69#define REQUEST_SIZE_MATCH(req)\
70    if ((sizeof(req) >> 2) != client->req_len)\
71         return(BadLength)
72
73#define REQUEST_AT_LEAST_SIZE(req) \
74    if ((sizeof(req) >> 2) > client->req_len )\
75         return(BadLength)
76
77#define REQUEST_AT_LEAST_EXTRA_SIZE(req, extra)  \
78    if (((sizeof(req) + ((uint64_t) extra)) >> 2) > client->req_len ) \
79         return(BadLength)
80
81#define REQUEST_FIXED_SIZE(req, n)\
82    if (((sizeof(req) >> 2) > client->req_len) || \
83        (((n) >> 2) >= client->req_len) || \
84        ((((uint64_t) sizeof(req) + (n) + 3) >> 2) != (uint64_t) client->req_len))  \
85         return(BadLength)
86
87#define LEGAL_NEW_RESOURCE(id,client)\
88    if (!LegalNewID(id,client)) \
89    {\
90	client->errorValue = id;\
91        return BadIDChoice;\
92    }
93
94#define VALIDATE_DRAWABLE_AND_GC(drawID, pDraw, mode)\
95    {\
96	int rc = dixLookupDrawable(&(pDraw), drawID, client, M_ANY, mode);\
97	if (rc != Success)\
98	    return rc;\
99	rc = dixLookupGC(&(pGC), stuff->gc, client, DixUseAccess);\
100	if (rc != Success)\
101	    return rc;\
102	if ((pGC->depth != pDraw->depth) || (pGC->pScreen != pDraw->pScreen))\
103	    return BadMatch;\
104    }\
105    if (pGC->serialNumber != pDraw->serialNumber)\
106	ValidateGC(pDraw, pGC);
107
108
109#define WriteReplyToClient(pClient, size, pReply) { \
110   if ((pClient)->swapped) \
111      (*ReplySwapVector[((xReq *)(pClient)->requestBuffer)->reqType]) \
112           (pClient, (int)(size), pReply); \
113      else (void) WriteToClient(pClient, (int)(size), (char *)(pReply)); }
114
115#define WriteSwappedDataToClient(pClient, size, pbuf) \
116   if ((pClient)->swapped) \
117      (*(pClient)->pSwapReplyFunc)(pClient, (int)(size), pbuf); \
118   else (void) WriteToClient (pClient, (int)(size), (char *)(pbuf));
119
120typedef struct _TimeStamp *TimeStampPtr;
121
122#ifndef _XTYPEDEF_CLIENTPTR
123typedef struct _Client *ClientPtr; /* also in misc.h */
124#define _XTYPEDEF_CLIENTPTR
125#endif
126
127typedef struct _WorkQueue	*WorkQueuePtr;
128
129extern _X_EXPORT ClientPtr clients[MAXCLIENTS];
130extern _X_EXPORT ClientPtr serverClient;
131extern _X_EXPORT int currentMaxClients;
132extern _X_EXPORT char dispatchExceptionAtReset;
133
134typedef int HWEventQueueType;
135typedef HWEventQueueType* HWEventQueuePtr;
136
137extern _X_EXPORT HWEventQueuePtr checkForInput[2];
138
139typedef struct _TimeStamp {
140    CARD32 months;	/* really ~49.7 days */
141    CARD32 milliseconds;
142}           TimeStamp;
143
144/* dispatch.c */
145
146extern _X_EXPORT void SetInputCheck(
147    HWEventQueuePtr /*c0*/,
148    HWEventQueuePtr /*c1*/);
149
150extern _X_EXPORT void CloseDownClient(
151    ClientPtr /*client*/);
152
153extern _X_EXPORT void UpdateCurrentTime(void);
154
155extern _X_EXPORT void UpdateCurrentTimeIf(void);
156
157extern _X_EXPORT int dixDestroyPixmap(
158    pointer /*value*/,
159    XID /*pid*/);
160
161extern _X_EXPORT void InitClient(
162    ClientPtr /*client*/,
163    int /*i*/,
164    pointer /*ospriv*/);
165
166extern _X_EXPORT ClientPtr NextAvailableClient(
167    pointer /*ospriv*/);
168
169extern _X_EXPORT void SendErrorToClient(
170    ClientPtr /*client*/,
171    unsigned int /*majorCode*/,
172    unsigned int /*minorCode*/,
173    XID /*resId*/,
174    int /*errorCode*/);
175
176extern _X_EXPORT void MarkClientException(
177    ClientPtr /*client*/);
178
179extern _X_HIDDEN Bool CreateConnectionBlock(void);
180/* dixutils.c */
181
182extern _X_EXPORT int CompareISOLatin1Lowered(
183    unsigned char * /*a*/,
184    int alen,
185    unsigned char * /*b*/,
186    int blen);
187
188extern _X_EXPORT int dixLookupWindow(
189    WindowPtr *result,
190    XID id,
191    ClientPtr client,
192    Mask access_mode);
193
194extern _X_EXPORT int dixLookupDrawable(
195    DrawablePtr *result,
196    XID id,
197    ClientPtr client,
198    Mask type_mask,
199    Mask access_mode);
200
201extern _X_EXPORT int dixLookupGC(
202    GCPtr *result,
203    XID id,
204    ClientPtr client,
205    Mask access_mode);
206
207extern _X_EXPORT int dixLookupFontable(
208    FontPtr *result,
209    XID id,
210    ClientPtr client,
211    Mask access_mode);
212
213extern _X_EXPORT int dixLookupClient(
214    ClientPtr *result,
215    XID id,
216    ClientPtr client,
217    Mask access_mode);
218
219extern _X_EXPORT void NoopDDA(void);
220
221extern _X_EXPORT int AlterSaveSetForClient(
222    ClientPtr /*client*/,
223    WindowPtr /*pWin*/,
224    unsigned /*mode*/,
225    Bool /*toRoot*/,
226    Bool /*map*/);
227
228extern _X_EXPORT void DeleteWindowFromAnySaveSet(
229    WindowPtr /*pWin*/);
230
231extern _X_EXPORT void BlockHandler(
232    pointer /*pTimeout*/,
233    pointer /*pReadmask*/);
234
235extern _X_EXPORT void WakeupHandler(
236    int /*result*/,
237    pointer /*pReadmask*/);
238
239void
240EnableLimitedSchedulingLatency(void);
241
242void
243DisableLimitedSchedulingLatency(void);
244
245typedef void (* WakeupHandlerProcPtr)(
246    pointer /* blockData */,
247    int /* result */,
248    pointer /* pReadmask */);
249
250extern _X_EXPORT Bool RegisterBlockAndWakeupHandlers(
251    BlockHandlerProcPtr /*blockHandler*/,
252    WakeupHandlerProcPtr /*wakeupHandler*/,
253    pointer /*blockData*/);
254
255extern _X_EXPORT void RemoveBlockAndWakeupHandlers(
256    BlockHandlerProcPtr /*blockHandler*/,
257    WakeupHandlerProcPtr /*wakeupHandler*/,
258    pointer /*blockData*/);
259
260extern _X_EXPORT void InitBlockAndWakeupHandlers(void);
261
262extern _X_EXPORT void ProcessWorkQueue(void);
263
264extern _X_EXPORT void ProcessWorkQueueZombies(void);
265
266extern _X_EXPORT Bool QueueWorkProc(
267    Bool (* /*function*/)(
268        ClientPtr /*clientUnused*/,
269        pointer /*closure*/),
270    ClientPtr /*client*/,
271    pointer /*closure*/
272);
273
274typedef Bool (* ClientSleepProcPtr)(
275    ClientPtr /*client*/,
276    pointer /*closure*/);
277
278extern _X_EXPORT Bool ClientSleep(
279    ClientPtr /*client*/,
280    ClientSleepProcPtr /* function */,
281    pointer /*closure*/);
282
283#ifndef ___CLIENTSIGNAL_DEFINED___
284#define ___CLIENTSIGNAL_DEFINED___
285extern _X_EXPORT Bool ClientSignal(
286    ClientPtr /*client*/);
287#endif /* ___CLIENTSIGNAL_DEFINED___ */
288
289extern _X_EXPORT void ClientWakeup(
290    ClientPtr /*client*/);
291
292extern _X_EXPORT Bool ClientIsAsleep(
293    ClientPtr /*client*/);
294
295/* atom.c */
296
297extern _X_EXPORT Atom MakeAtom(
298    const char * /*string*/,
299    unsigned /*len*/,
300    Bool /*makeit*/);
301
302extern _X_EXPORT Bool ValidAtom(
303    Atom /*atom*/);
304
305extern _X_EXPORT const char *NameForAtom(
306    Atom /*atom*/);
307
308extern _X_EXPORT void AtomError(void) _X_NORETURN;
309
310extern _X_EXPORT void FreeAllAtoms(void);
311
312extern _X_EXPORT void InitAtoms(void);
313
314/* main.c */
315
316extern _X_EXPORT void SetVendorRelease(int release);
317
318extern _X_EXPORT void SetVendorString(char *string);
319
320/* events.c */
321
322extern void SetMaskForEvent(
323    int /* deviceid */,
324    Mask /* mask */,
325    int /* event */);
326
327extern _X_EXPORT void ConfineToShape(
328    DeviceIntPtr /* pDev */,
329    RegionPtr /* shape */,
330    int*      /* px */,
331    int*      /* py */);
332
333extern _X_EXPORT Bool IsParent(
334    WindowPtr /* maybeparent */,
335    WindowPtr /* child */);
336
337extern _X_EXPORT WindowPtr GetCurrentRootWindow(DeviceIntPtr pDev);
338
339extern _X_EXPORT WindowPtr GetSpriteWindow(DeviceIntPtr pDev);
340
341
342extern _X_EXPORT void NoticeEventTime(InternalEvent *ev);
343
344extern void EnqueueEvent(
345    InternalEvent * /* ev */,
346    DeviceIntPtr  /* device */);
347
348extern void ActivatePointerGrab(
349    DeviceIntPtr /* mouse */,
350    GrabPtr /* grab */,
351    TimeStamp /* time */,
352    Bool /* autoGrab */);
353
354extern void DeactivatePointerGrab(
355    DeviceIntPtr /* mouse */);
356
357extern void ActivateKeyboardGrab(
358    DeviceIntPtr /* keybd */,
359    GrabPtr /* grab */,
360    TimeStamp /* time */,
361    Bool /* passive */);
362
363extern void DeactivateKeyboardGrab(
364    DeviceIntPtr /* keybd */);
365
366extern BOOL ActivateFocusInGrab(
367    DeviceIntPtr /* dev */,
368    WindowPtr /* old */,
369    WindowPtr /* win */);
370
371extern void AllowSome(
372    ClientPtr	/* client */,
373    TimeStamp /* time */,
374    DeviceIntPtr /* thisDev */,
375    int /* newState */);
376
377extern void ReleaseActiveGrabs(
378    ClientPtr client);
379
380extern GrabPtr CheckPassiveGrabsOnWindow(
381    WindowPtr /* pWin */,
382    DeviceIntPtr /* device */,
383    DeviceEvent * /* event */,
384    BOOL /* checkCore */,
385    BOOL /* activate */);
386
387extern _X_EXPORT int DeliverEventsToWindow(
388    DeviceIntPtr /* pWin */,
389    WindowPtr /* pWin */,
390    xEventPtr /* pEvents */,
391    int /* count */,
392    Mask /* filter */,
393    GrabPtr /* grab */);
394
395extern int DeliverDeviceEvents(
396    WindowPtr /* pWin */,
397    InternalEvent* /* event */,
398    GrabPtr /* grab */,
399    WindowPtr /* stopAt */,
400    DeviceIntPtr /* dev */);
401
402extern void InitializeSprite(
403    DeviceIntPtr /* pDev */,
404    WindowPtr    /* pWin */);
405
406extern void UpdateSpriteForScreen(
407    DeviceIntPtr /* pDev */,
408    ScreenPtr /* pScreen */);
409
410extern _X_EXPORT void WindowHasNewCursor(
411    WindowPtr /* pWin */);
412
413extern Bool CheckDeviceGrabs(
414    DeviceIntPtr /* device */,
415    DeviceEvent* /* event */,
416    WindowPtr /* ancestor */);
417
418extern void DeliverFocusedEvent(
419    DeviceIntPtr /* keybd */,
420    InternalEvent* /* event */,
421    WindowPtr /* window */);
422
423extern void DeliverGrabbedEvent(
424    InternalEvent* /* event */,
425    DeviceIntPtr /* thisDev */,
426    Bool /* deactivateGrab */);
427
428extern void FixKeyState(
429    DeviceEvent* /* event */,
430    DeviceIntPtr /* keybd */);
431
432extern void RecalculateDeliverableEvents(
433    WindowPtr /* pWin */);
434
435extern _X_EXPORT int OtherClientGone(
436    pointer /* value */,
437    XID /* id */);
438
439extern void DoFocusEvents(
440    DeviceIntPtr /* dev */,
441    WindowPtr /* fromWin */,
442    WindowPtr /* toWin */,
443    int /* mode */);
444
445extern int SetInputFocus(
446    ClientPtr /* client */,
447    DeviceIntPtr /* dev */,
448    Window /* focusID */,
449    CARD8 /* revertTo */,
450    Time /* ctime */,
451    Bool /* followOK */);
452
453extern int GrabDevice(
454    ClientPtr /* client */,
455    DeviceIntPtr /* dev */,
456    unsigned /* this_mode */,
457    unsigned /* other_mode */,
458    Window /* grabWindow */,
459    unsigned /* ownerEvents */,
460    Time /* ctime */,
461    GrabMask* /* mask */,
462    int /* grabtype */,
463    Cursor /* curs */,
464    Window /* confineToWin */,
465    CARD8 * /* status */);
466
467extern void InitEvents(void);
468
469extern void CloseDownEvents(void);
470
471extern void DeleteWindowFromAnyEvents(
472    WindowPtr	/* pWin */,
473    Bool /* freeResources */);
474
475
476extern Mask EventMaskForClient(
477    WindowPtr /* pWin */,
478    ClientPtr /* client */);
479
480
481
482extern _X_EXPORT int DeliverEvents(
483    WindowPtr /*pWin*/,
484    xEventPtr /*xE*/,
485    int /*count*/,
486    WindowPtr /*otherParent*/);
487
488extern Bool CheckMotion(
489    DeviceEvent* /* ev */,
490    DeviceIntPtr /* pDev */);
491
492extern _X_EXPORT void WriteEventsToClient(
493    ClientPtr /*pClient*/,
494    int	     /*count*/,
495    xEventPtr /*events*/);
496
497extern _X_EXPORT int TryClientEvents(
498    ClientPtr /*client*/,
499    DeviceIntPtr /* device */,
500    xEventPtr /*pEvents*/,
501    int /*count*/,
502    Mask /*mask*/,
503    Mask /*filter*/,
504    GrabPtr /*grab*/);
505
506extern _X_EXPORT void WindowsRestructured(void);
507
508extern int SetClientPointer(
509        ClientPtr /* client */,
510        DeviceIntPtr /* device */);
511
512extern _X_EXPORT DeviceIntPtr PickPointer(
513    ClientPtr /* client */);
514
515extern _X_EXPORT DeviceIntPtr PickKeyboard(
516    ClientPtr /* client */);
517
518extern Bool IsInterferingGrab(
519        ClientPtr /* client */,
520        DeviceIntPtr /* dev */,
521        xEvent* /* events */);
522
523#ifdef PANORAMIX
524extern _X_EXPORT void ReinitializeRootWindow(WindowPtr win, int xoff, int yoff);
525#endif
526
527#ifdef RANDR
528extern _X_EXPORT void
529ScreenRestructured (ScreenPtr pScreen);
530#endif
531
532#ifndef HAVE_FFS
533extern _X_EXPORT int ffs(int i);
534#endif
535
536
537/*
538 *  ServerGrabCallback stuff
539 */
540
541extern _X_EXPORT CallbackListPtr ServerGrabCallback;
542
543typedef enum {SERVER_GRABBED, SERVER_UNGRABBED,
544	      CLIENT_PERVIOUS, CLIENT_IMPERVIOUS } ServerGrabState;
545
546typedef struct {
547    ClientPtr client;
548    ServerGrabState grabstate;
549} ServerGrabInfoRec;
550
551/*
552 *  EventCallback stuff
553 */
554
555extern _X_EXPORT CallbackListPtr EventCallback;
556
557typedef struct {
558    ClientPtr client;
559    xEventPtr events;
560    int count;
561} EventInfoRec;
562
563/*
564 *  DeviceEventCallback stuff
565 */
566
567extern _X_EXPORT CallbackListPtr DeviceEventCallback;
568
569typedef struct {
570    InternalEvent *event;
571    DeviceIntPtr device;
572} DeviceEventInfoRec;
573
574extern int XItoCoreType(int xi_type);
575extern Bool DevHasCursor(DeviceIntPtr pDev);
576extern Bool _X_EXPORT IsPointerDevice( DeviceIntPtr dev);
577extern Bool _X_EXPORT IsKeyboardDevice(DeviceIntPtr dev);
578extern Bool IsPointerEvent(InternalEvent *event);
579extern _X_EXPORT Bool IsMaster(DeviceIntPtr dev);
580
581extern _X_HIDDEN void CopyKeyClass(DeviceIntPtr device, DeviceIntPtr master);
582extern _X_HIDDEN int CorePointerProc(DeviceIntPtr dev, int what);
583extern _X_HIDDEN int CoreKeyboardProc(DeviceIntPtr dev, int what);
584
585
586/*
587 * These are deprecated compatibility functions and will be removed soon!
588 * Please use the noted replacements instead.
589 */
590/* replaced by dixLookupWindow */
591extern _X_EXPORT WindowPtr SecurityLookupWindow(
592    XID id,
593    ClientPtr client,
594    Mask access_mode);
595/* replaced by dixLookupWindow */
596extern _X_EXPORT WindowPtr LookupWindow(
597    XID id,
598    ClientPtr client);
599
600/* replaced by dixLookupDrawable */
601extern _X_EXPORT pointer SecurityLookupDrawable(
602    XID id,
603    ClientPtr client,
604    Mask access_mode);
605
606/* replaced by dixLookupDrawable */
607extern _X_EXPORT pointer LookupDrawable(
608    XID id,
609    ClientPtr client);
610
611/* replaced by dixLookupClient */
612extern _X_EXPORT ClientPtr LookupClient(
613    XID id,
614    ClientPtr client);
615
616#endif /* DIX_H */
617