home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / server / dix / dispatch.c.orig < prev    next >
Encoding:
Text File  |  1991-07-18  |  85.6 KB  |  3,426 lines

  1. /* $XConsortium: dispatch.c,v 5.34 91/07/18 22:41:32 keith Exp $ */
  2. /************************************************************
  3. Copyright 1987, 1989 by Digital Equipment Corporation, Maynard, Massachusetts,
  4. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  5.  
  6.                         All Rights Reserved
  7.  
  8. Permission to use, copy, modify, and distribute this software and its 
  9. documentation for any purpose and without fee is hereby granted, 
  10. provided that the above copyright notice appear in all copies and that
  11. both that copyright notice and this permission notice appear in 
  12. supporting documentation, and that the names of Digital or MIT not be
  13. used in advertising or publicity pertaining to distribution of the
  14. software without specific, written prior permission.  
  15.  
  16. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  17. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  18. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  19. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  21. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  22. SOFTWARE.
  23.  
  24. ********************************************************/
  25.  
  26. #include "X.h"
  27. #define NEED_REPLIES
  28. #define NEED_EVENTS
  29. #include "Xproto.h"
  30. #include "windowstr.h"
  31. #include "fontstruct.h"
  32. #include "dixfontstr.h"
  33. #include "gcstruct.h"
  34. #include "osstruct.h"
  35. #include "selection.h"
  36. #include "colormapst.h"
  37. #include "cursorstr.h"
  38. #include "scrnintstr.h"
  39. #include "opaque.h"
  40. #include "input.h"
  41. #include "servermd.h"
  42. #include "extnsionst.h"
  43.  
  44. extern WindowPtr *WindowTable;
  45. extern xConnSetupPrefix connSetupPrefix;
  46. extern char *ConnectionInfo;
  47. extern void ValidateGC();
  48. extern Atom MakeAtom();
  49. extern char *NameForAtom();
  50. extern void SaveScreens();
  51. extern void ReleaseActiveGrabs();
  52. extern void QueryFont();
  53. extern void NotImplemented();
  54. extern WindowPtr RealChildHead();
  55. extern Bool InitClientResources();
  56. extern unsigned char *GetFontPath();
  57.  
  58. Selection *CurrentSelections;
  59. int NumCurrentSelections;
  60.  
  61. extern long ScreenSaverTime;
  62. extern long ScreenSaverInterval;
  63. extern int  ScreenSaverBlanking;
  64. extern int  ScreenSaverAllowExposures;
  65. extern long defaultScreenSaverTime;
  66. extern long defaultScreenSaverInterval;
  67. extern int  defaultScreenSaverBlanking;
  68. extern int  defaultScreenSaverAllowExposures;
  69. static ClientPtr onlyClient;
  70. static Bool grabbingClient = FALSE;
  71. long    *checkForInput[2];
  72. extern int connBlockScreenStart;
  73.  
  74. extern int (* InitialVector[3]) ();
  75. extern int (* ProcVector[256]) ();
  76. extern int (* SwappedProcVector[256]) ();
  77. extern void (* EventSwapVector[128]) ();
  78. extern void (* ReplySwapVector[256]) ();
  79. extern void Swap32Write(), SLHostsExtend(), SQColorsExtend(), WriteSConnectionInfo();
  80. extern void WriteSConnSetupPrefix();
  81. extern char *ClientAuthorized();
  82. extern Bool InsertFakeRequest();
  83. static void KillAllClients();
  84. static void DeleteClientFromAnySelections();
  85. extern void ProcessWorkQueue();
  86.  
  87.  
  88. static int nextFreeClientID; /* always MIN free client ID */
  89.  
  90. static int    nClients;    /* number active clients */
  91.  
  92. char dispatchException = 0;
  93. char isItTimeToYield;
  94.  
  95. /* Various of the DIX function interfaces were not designed to allow
  96.  * the client->errorValue to be set on BadValue and other errors.
  97.  * Rather than changing interfaces and breaking untold code we introduce
  98.  * a new global that dispatch can use.
  99.  */
  100. XID clientErrorValue;   /* XXX this is a kludge */
  101.  
  102. #define SAME_SCREENS(a, b) (\
  103.     (a.pScreen == b.pScreen))
  104.  
  105. void
  106. SetInputCheck(c0, c1)
  107.     long *c0, *c1;
  108. {
  109.     checkForInput[0] = c0;
  110.     checkForInput[1] = c1;
  111. }
  112.  
  113. void
  114. UpdateCurrentTime()
  115. {
  116.     TimeStamp systime;
  117.  
  118.     /* To avoid time running backwards, we must call GetTimeInMillis before
  119.      * calling ProcessInputEvents.
  120.      */
  121.     systime.months = currentTime.months;
  122.     systime.milliseconds = GetTimeInMillis();
  123.     if (systime.milliseconds < currentTime.milliseconds)
  124.     systime.months++;
  125.     if (*checkForInput[0] != *checkForInput[1])
  126.     ProcessInputEvents();
  127.     if (CompareTimeStamps(systime, currentTime) == LATER)
  128.     currentTime = systime;
  129. }
  130.  
  131. /* Like UpdateCurrentTime, but can't call ProcessInputEvents */
  132. void
  133. UpdateCurrentTimeIf()
  134. {
  135.     TimeStamp systime;
  136.  
  137.     systime.months = currentTime.months;
  138.     systime.milliseconds = GetTimeInMillis();
  139.     if (systime.milliseconds < currentTime.milliseconds)
  140.     systime.months++;
  141.     if (*checkForInput[0] == *checkForInput[1])
  142.     currentTime = systime;
  143. }
  144.  
  145. void
  146. InitSelections()
  147. {
  148.     if (CurrentSelections)
  149.     xfree(CurrentSelections);
  150.     CurrentSelections = (Selection *)NULL;
  151.     NumCurrentSelections = 0;
  152. }
  153.  
  154. void 
  155. FlushClientCaches(id)
  156.     XID id;
  157. {
  158.     int i;
  159.     register ClientPtr client;
  160.  
  161.     client = clients[CLIENT_ID(id)];
  162.     if (client == NullClient)
  163.         return ;
  164.     for (i=0; i<currentMaxClients; i++)
  165.     {
  166.     client = clients[i];
  167.         if (client != NullClient)
  168.     {
  169.             if (client->lastDrawableID == id)
  170.         {
  171.                 client->lastDrawableID = INVALID;
  172.         client->lastDrawable = (DrawablePtr)NULL;
  173.         }
  174.             else if (client->lastGCID == id)
  175.         {
  176.                 client->lastGCID = INVALID;
  177.         client->lastGC = (GCPtr)NULL;
  178.         }
  179.     }
  180.     }
  181. }
  182.  
  183. #define MAJOROP ((xReq *)client->requestBuffer)->reqType
  184.  
  185. Dispatch()
  186. {
  187.     register int        *clientReady;     /* array of request ready clients */
  188.     register int    result;
  189.     register ClientPtr    client;
  190.     register int    nready;
  191.     register long    **icheck = checkForInput;
  192.  
  193.     nextFreeClientID = 1;
  194.     InitSelections();
  195.     nClients = 0;
  196.  
  197.     clientReady = (int *) ALLOCATE_LOCAL(sizeof(int) * MaxClients);
  198.     if (!clientReady)
  199.     return;
  200.  
  201.     while (!dispatchException)
  202.     {
  203.         if (*icheck[0] != *icheck[1])
  204.     {
  205.         ProcessInputEvents();
  206.         FlushIfCriticalOutputPending();
  207.     }
  208.  
  209.     nready = WaitForSomething(clientReady);
  210.  
  211.        /***************** 
  212.     *  Handle events in round robin fashion, doing input between 
  213.     *  each round 
  214.     *****************/
  215.  
  216.     while (!dispatchException && (--nready >= 0))
  217.     {
  218.         client = clients[clientReady[nready]];
  219.         if (! client)
  220.         {
  221.         /* KillClient can cause this to happen */
  222.         continue;
  223.         }
  224.         /* GrabServer activation can cause this to be true */
  225.         if (grabbingClient && (client != onlyClient))
  226.         break;
  227.         isItTimeToYield = FALSE;
  228.  
  229.             requestingClient = client;
  230.         while (!isItTimeToYield)
  231.         {
  232.             if (*icheck[0] != *icheck[1])
  233.         {
  234.             ProcessInputEvents();
  235.             FlushIfCriticalOutputPending();
  236.         }
  237.        
  238.         /* now, finally, deal with client requests */
  239.  
  240.             result = ReadRequestFromClient(client);
  241.             if (result <= 0) 
  242.             {
  243.             if (result < 0)
  244.             CloseDownClient(client);
  245.             break;
  246.             }
  247.  
  248.         client->sequence++;
  249. #ifdef DEBUG
  250.         if (client->requestLogIndex == MAX_REQUEST_LOG)
  251.             client->requestLogIndex = 0;
  252.         client->requestLog[client->requestLogIndex] = MAJOROP;
  253.         client->requestLogIndex++;
  254. #endif
  255.         if (result > (MAX_REQUEST_SIZE << 2))
  256.             result = BadLength;
  257.         else
  258.             result = (* client->requestVector[MAJOROP])(client);
  259.         
  260.         if (result != Success) 
  261.         {
  262.             if (client->noClientException != Success)
  263.                         CloseDownClient(client);
  264.                     else
  265.                 SendErrorToClient(client, MAJOROP,
  266.                       MinorOpcodeOfRequest(client),
  267.                       client->errorValue, result);
  268.             break;
  269.             }
  270.         }
  271.         FlushAllOutput();
  272.     }
  273.     }
  274.     KillAllClients();
  275.     DEALLOCATE_LOCAL(clientReady);
  276.     dispatchException &= ~DE_RESET;
  277. }
  278.  
  279. #undef MAJOROP
  280.  
  281. /*ARGSUSED*/
  282. int
  283. ProcBadRequest(client)
  284.     ClientPtr client;
  285. {
  286.     return (BadRequest);
  287. }
  288.  
  289. extern int Ones();
  290.  
  291. int
  292. ProcCreateWindow(client)
  293.     register ClientPtr client;
  294. {
  295.     register WindowPtr pParent, pWin;
  296.     REQUEST(xCreateWindowReq);
  297.     int result;
  298.     int len;
  299.  
  300.     REQUEST_AT_LEAST_SIZE(xCreateWindowReq);
  301.     
  302.     LEGAL_NEW_RESOURCE(stuff->wid, client);
  303.     if (!(pParent = (WindowPtr)LookupWindow(stuff->parent, client)))
  304.         return BadWindow;
  305.     len = stuff->length -  (sizeof(xCreateWindowReq) >> 2);
  306.     if (Ones(stuff->mask) != len)
  307.         return BadLength;
  308.     if (!stuff->width || !stuff->height)
  309.     {
  310.     client->errorValue = 0;
  311.         return BadValue;
  312.     }
  313.     pWin = CreateWindow(stuff->wid, pParent, stuff->x,
  314.                   stuff->y, stuff->width, stuff->height, 
  315.                   stuff->borderWidth, stuff->class,
  316.                   stuff->mask, (XID *) &stuff[1], 
  317.                   (int)stuff->depth, 
  318.                   client, stuff->visual, &result);
  319.     if (pWin)
  320.     {
  321.     Mask mask = pWin->eventMask;
  322.  
  323.     pWin->eventMask = 0; /* subterfuge in case AddResource fails */
  324.     if (!AddResource(stuff->wid, RT_WINDOW, (pointer)pWin))
  325.         return BadAlloc;
  326.     pWin->eventMask = mask;
  327.     }
  328.     if (client->noClientException != Success)
  329.         return(client->noClientException);
  330.     else
  331.         return(result);
  332. }
  333.  
  334. int
  335. ProcChangeWindowAttributes(client)
  336.     register ClientPtr client;
  337. {
  338.     register WindowPtr pWin;
  339.     REQUEST(xChangeWindowAttributesReq);
  340.     register int result;
  341.     int len;
  342.  
  343.     REQUEST_AT_LEAST_SIZE(xChangeWindowAttributesReq);
  344.     pWin = (WindowPtr)LookupWindow(stuff->window, client);
  345.     if (!pWin)
  346.         return(BadWindow);
  347.     len = stuff->length - (sizeof(xChangeWindowAttributesReq) >> 2);
  348.     if (len != Ones(stuff->valueMask))
  349.         return BadLength;
  350.     result =  ChangeWindowAttributes(pWin, 
  351.                   stuff->valueMask, 
  352.                   (XID *) &stuff[1], 
  353.                   client);
  354.     if (client->noClientException != Success)
  355.         return(client->noClientException);
  356.     else
  357.         return(result);
  358. }
  359.  
  360. int
  361. ProcGetWindowAttributes(client)
  362.     register ClientPtr client;
  363. {
  364.     register WindowPtr pWin;
  365.     REQUEST(xResourceReq);
  366.  
  367.     REQUEST_SIZE_MATCH(xResourceReq);
  368.     pWin = (WindowPtr)LookupWindow(stuff->id, client);
  369.     if (!pWin)
  370.         return(BadWindow);
  371.     GetWindowAttributes(pWin, client);
  372.     return(client->noClientException);
  373. }
  374.  
  375. int
  376. ProcDestroyWindow(client)
  377.     register ClientPtr client;
  378. {
  379.     register WindowPtr pWin;
  380.     REQUEST(xResourceReq);
  381.  
  382.     REQUEST_SIZE_MATCH(xResourceReq);
  383.     pWin = (WindowPtr)LookupWindow(stuff->id, client);
  384.     if (!pWin)
  385.         return(BadWindow);
  386.     if (pWin->parent)
  387.     FreeResource(stuff->id, RT_NONE);
  388.     return(client->noClientException);
  389. }
  390.  
  391. int
  392. ProcDestroySubwindows(client)
  393.     register ClientPtr client;
  394. {
  395.     register WindowPtr pWin;
  396.     REQUEST(xResourceReq);
  397.  
  398.     REQUEST_SIZE_MATCH(xResourceReq);
  399.     pWin = (WindowPtr)LookupWindow(stuff->id, client);
  400.     if (!pWin)
  401.         return(BadWindow);
  402.     DestroySubwindows(pWin, client);
  403.     return(client->noClientException);
  404. }
  405.  
  406. int
  407. ProcChangeSaveSet(client)
  408.     register ClientPtr client;
  409. {
  410.     register WindowPtr pWin;
  411.     REQUEST(xChangeSaveSetReq);
  412.     register result;
  413.           
  414.     REQUEST_SIZE_MATCH(xChangeSaveSetReq);
  415.     pWin = (WindowPtr)LookupWindow(stuff->window, client);
  416.     if (!pWin)
  417.         return(BadWindow);
  418.     if (client->clientAsMask == (CLIENT_BITS(pWin->drawable.id)))
  419.         return BadMatch;
  420.     if ((stuff->mode == SetModeInsert) || (stuff->mode == SetModeDelete))
  421.     {
  422.         result = AlterSaveSetForClient(client, pWin, stuff->mode);
  423.     if (client->noClientException != Success)
  424.         return(client->noClientException);
  425.     else
  426.             return(result);
  427.     }
  428.     else
  429.     {
  430.     client->errorValue = stuff->mode;
  431.     return( BadValue );
  432.     }
  433. }
  434.  
  435. int
  436. ProcReparentWindow(client)
  437.     register ClientPtr client;
  438. {
  439.     register WindowPtr pWin, pParent;
  440.     REQUEST(xReparentWindowReq);
  441.     register int result;
  442.  
  443.     REQUEST_SIZE_MATCH(xReparentWindowReq);
  444.     pWin = (WindowPtr)LookupWindow(stuff->window, client);
  445.     if (!pWin)
  446.         return(BadWindow);
  447.     pParent = (WindowPtr)LookupWindow(stuff->parent, client);
  448.     if (!pParent)
  449.         return(BadWindow);
  450.     if (SAME_SCREENS(pWin->drawable, pParent->drawable))
  451.     {
  452.         if ((pWin->backgroundState == ParentRelative) &&
  453.             (pParent->drawable.depth != pWin->drawable.depth))
  454.             return BadMatch;
  455.     if ((pWin->drawable.class != InputOnly) &&
  456.         (pParent->drawable.class == InputOnly))
  457.         return BadMatch;
  458.         result =  ReparentWindow(pWin, pParent, 
  459.              (short)stuff->x, (short)stuff->y, client);
  460.     if (client->noClientException != Success)
  461.             return(client->noClientException);
  462.     else
  463.             return(result);
  464.     }
  465.     else 
  466.         return (BadMatch);
  467. }
  468.  
  469. int
  470. ProcMapWindow(client)
  471.     register ClientPtr client;
  472. {
  473.     register WindowPtr pWin;
  474.     REQUEST(xResourceReq);
  475.  
  476.     REQUEST_SIZE_MATCH(xResourceReq);
  477.     pWin = (WindowPtr)LookupWindow(stuff->id, client);
  478.     if (!pWin)
  479.         return(BadWindow);
  480.     MapWindow(pWin, client);
  481.            /* update cache to say it is mapped */
  482.     return(client->noClientException);
  483. }
  484.  
  485. int
  486. ProcMapSubwindows(client)
  487.     register ClientPtr client;
  488. {
  489.     register WindowPtr pWin;
  490.     REQUEST(xResourceReq);
  491.  
  492.     REQUEST_SIZE_MATCH(xResourceReq);
  493.     pWin = (WindowPtr)LookupWindow( stuff->id, client);
  494.     if (!pWin)
  495.         return(BadWindow);
  496.     MapSubwindows(pWin, client);
  497.            /* update cache to say it is mapped */
  498.     return(client->noClientException);
  499. }
  500.  
  501. int
  502. ProcUnmapWindow(client)
  503.     register ClientPtr client;
  504. {
  505.     register WindowPtr pWin;
  506.     REQUEST(xResourceReq);
  507.  
  508.     REQUEST_SIZE_MATCH(xResourceReq);
  509.     pWin = (WindowPtr)LookupWindow( stuff->id, client);
  510.     if (!pWin)
  511.         return(BadWindow);
  512.     UnmapWindow(pWin, FALSE);
  513.            /* update cache to say it is mapped */
  514.     return(client->noClientException);
  515. }
  516.  
  517. int
  518. ProcUnmapSubwindows(client)
  519.     register ClientPtr client;
  520. {
  521.     register WindowPtr pWin;
  522.     REQUEST(xResourceReq);
  523.  
  524.     REQUEST_SIZE_MATCH(xResourceReq);
  525.     pWin = (WindowPtr)LookupWindow( stuff->id, client);
  526.     if (!pWin)
  527.         return(BadWindow);
  528.     UnmapSubwindows(pWin);
  529.     return(client->noClientException);
  530. }
  531.  
  532. int
  533. ProcConfigureWindow(client)
  534.     register ClientPtr client;
  535. {
  536.     register WindowPtr pWin;
  537.     REQUEST(xConfigureWindowReq);
  538.     register int result;
  539.     int len;
  540.  
  541.     REQUEST_AT_LEAST_SIZE(xConfigureWindowReq);
  542.     pWin = (WindowPtr)LookupWindow( stuff->window, client);
  543.     if (!pWin)
  544.         return(BadWindow);
  545.     len = stuff->length - (sizeof(xConfigureWindowReq) >> 2);
  546.     if (Ones((Mask)stuff->mask) != len)
  547.         return BadLength;
  548.     result =  ConfigureWindow(pWin, (Mask)stuff->mask, (XID *) &stuff[1], 
  549.                   client);
  550.     if (client->noClientException != Success)
  551.         return(client->noClientException);
  552.     else
  553.         return(result);
  554. }
  555.  
  556. int
  557. ProcCirculateWindow(client)
  558.     register ClientPtr client;
  559. {
  560.     register WindowPtr pWin;
  561.     REQUEST(xCirculateWindowReq);
  562.  
  563.     REQUEST_SIZE_MATCH(xCirculateWindowReq);
  564.     if ((stuff->direction != RaiseLowest) &&
  565.     (stuff->direction != LowerHighest))
  566.     {
  567.     client->errorValue = stuff->direction;
  568.         return BadValue;
  569.     }
  570.     pWin = (WindowPtr)LookupWindow(stuff->window, client);
  571.     if (!pWin)
  572.         return(BadWindow);
  573.     CirculateWindow(pWin, (int)stuff->direction, client);
  574.     return(client->noClientException);
  575. }
  576.  
  577. int
  578. ProcGetGeometry(client)
  579.     register ClientPtr client;
  580. {
  581.     xGetGeometryReply rep;
  582.     register DrawablePtr pDraw;
  583.     REQUEST(xResourceReq);
  584.  
  585.     REQUEST_SIZE_MATCH(xResourceReq);
  586.     if (!(pDraw = LOOKUP_DRAWABLE(stuff->id, client)))
  587.     {                /* can be inputonly */
  588.         if (!(pDraw = (DrawablePtr)LookupWindow(stuff->id, client))) 
  589.             return (BadDrawable);
  590.     }
  591.     rep.type = X_Reply;
  592.     rep.length = 0;
  593.     rep.sequenceNumber = client->sequence;
  594.     rep.root = WindowTable[pDraw->pScreen->myNum]->drawable.id;
  595.     rep.depth = pDraw->depth;
  596.  
  597.     rep.width = pDraw->width;
  598.     rep.height = pDraw->height;
  599.     if (pDraw->type == DRAWABLE_PIXMAP)
  600.     {
  601.     rep.x = rep.y = rep.borderWidth = 0;
  602.     }
  603.     else
  604.     {
  605.         register WindowPtr pWin = (WindowPtr)pDraw;
  606.     rep.x = pWin->origin.x - wBorderWidth (pWin);
  607.     rep.y = pWin->origin.y - wBorderWidth (pWin);
  608.     rep.borderWidth = pWin->borderWidth;
  609.     }
  610.     WriteReplyToClient(client, sizeof(xGetGeometryReply), &rep);
  611.     return(client->noClientException);
  612. }
  613.  
  614. int
  615. ProcQueryTree(client)
  616.     register ClientPtr client;
  617. {
  618.  
  619.     xQueryTreeReply reply;
  620.     int numChildren = 0;
  621.     register WindowPtr pChild, pWin, pHead;
  622.     Window  *childIDs = (Window *)NULL;
  623.     REQUEST(xResourceReq);
  624.  
  625.     REQUEST_SIZE_MATCH(xResourceReq);
  626.     pWin = (WindowPtr)LookupWindow(stuff->id, client);
  627.     if (!pWin)
  628.         return(BadWindow);
  629.     reply.type = X_Reply;
  630.     reply.root = WindowTable[pWin->drawable.pScreen->myNum]->drawable.id;
  631.     reply.sequenceNumber = client->sequence;
  632.     if (pWin->parent)
  633.     reply.parent = pWin->parent->drawable.id;
  634.     else
  635.         reply.parent = (Window)None;
  636.  
  637.     pHead = RealChildHead(pWin);
  638.     for (pChild = pWin->lastChild; pChild != pHead; pChild = pChild->prevSib)
  639.     numChildren++;
  640.     if (numChildren)
  641.     {
  642.     int curChild = 0;
  643.  
  644.     childIDs = (Window *) ALLOCATE_LOCAL(numChildren * sizeof(Window));
  645.     if (!childIDs)
  646.         return BadAlloc;
  647.     for (pChild = pWin->lastChild; pChild != pHead; pChild = pChild->prevSib)
  648.         childIDs[curChild++] = pChild->drawable.id;
  649.     }
  650.     
  651.     reply.nChildren = numChildren;
  652.     reply.length = (numChildren * sizeof(Window)) >> 2;
  653.     
  654.     WriteReplyToClient(client, sizeof(xQueryTreeReply), &reply);
  655.     if (numChildren)
  656.     {
  657.         client->pSwapReplyFunc = Swap32Write;
  658.     WriteSwappedDataToClient(client, numChildren * sizeof(Window), childIDs);
  659.     DEALLOCATE_LOCAL(childIDs);
  660.     }
  661.  
  662.     return(client->noClientException);
  663. }
  664.  
  665. int
  666. ProcInternAtom(client)
  667.     register ClientPtr client;
  668. {
  669.     Atom atom;
  670.     char *tchar;
  671.     REQUEST(xInternAtomReq);
  672.  
  673.     REQUEST_FIXED_SIZE(xInternAtomReq, stuff->nbytes);
  674.     if ((stuff->onlyIfExists != xTrue) && (stuff->onlyIfExists != xFalse))
  675.     {
  676.     client->errorValue = stuff->onlyIfExists;
  677.         return(BadValue);
  678.     }
  679.     tchar = (char *) &stuff[1];
  680.     atom = MakeAtom(tchar, stuff->nbytes, !stuff->onlyIfExists);
  681.     if (atom != BAD_RESOURCE)
  682.     {
  683.     xInternAtomReply reply;
  684.     reply.type = X_Reply;
  685.     reply.length = 0;
  686.     reply.sequenceNumber = client->sequence;
  687.     reply.atom = atom;
  688.     WriteReplyToClient(client, sizeof(xInternAtomReply), &reply);
  689.     return(client->noClientException);
  690.     }
  691.     else
  692.     return (BadAlloc);
  693. }
  694.  
  695. int
  696. ProcGetAtomName(client)
  697.     register ClientPtr client;
  698. {
  699.     char *str;
  700.     xGetAtomNameReply reply;
  701.     int len;
  702.     REQUEST(xResourceReq);
  703.  
  704.     REQUEST_SIZE_MATCH(xResourceReq);
  705.     if (str = NameForAtom(stuff->id)) 
  706.     {
  707.     len = strlen(str);
  708.     reply.type = X_Reply;
  709.     reply.length = (len + 3) >> 2;
  710.     reply.sequenceNumber = client->sequence;
  711.     reply.nameLength = len;
  712.     WriteReplyToClient(client, sizeof(xGetAtomNameReply), &reply);
  713.     (void)WriteToClient(client, len, str);
  714.     return(client->noClientException);
  715.     }
  716.     else 
  717.     { 
  718.     client->errorValue = stuff->id;
  719.     return (BadAtom);
  720.     }
  721. }
  722.  
  723. int 
  724. ProcDeleteProperty(client)
  725.     register ClientPtr client;
  726. {
  727.     WindowPtr pWin;
  728.     REQUEST(xDeletePropertyReq);
  729.     int result;
  730.               
  731.     REQUEST_SIZE_MATCH(xDeletePropertyReq);
  732.     UpdateCurrentTime();
  733.     pWin = (WindowPtr)LookupWindow(stuff->window, client);
  734.     if (!pWin)
  735.         return(BadWindow);
  736.     if (ValidAtom(stuff->property))
  737.     {
  738.     result = DeleteProperty(pWin, stuff->property);
  739.         if (client->noClientException != Success)
  740.             return(client->noClientException);
  741.     else
  742.         return(result);
  743.     }
  744.     else 
  745.     {
  746.     client->errorValue = stuff->property;
  747.     return (BadAtom);
  748.     }
  749. }
  750.  
  751.  
  752. int
  753. ProcSetSelectionOwner(client)
  754.     register ClientPtr client;
  755. {
  756.     WindowPtr pWin;
  757.     TimeStamp time;
  758.     REQUEST(xSetSelectionOwnerReq);
  759.  
  760.     REQUEST_SIZE_MATCH(xSetSelectionOwnerReq);
  761.     UpdateCurrentTime();
  762.     time = ClientTimeToServerTime(stuff->time);
  763.  
  764.     /* If the client's time stamp is in the future relative to the server's
  765.     time stamp, do not set the selection, just return success. */
  766.     if (CompareTimeStamps(time, currentTime) == LATER)
  767.         return Success;
  768.     if (stuff->window != None)
  769.     {
  770.         pWin = (WindowPtr)LookupWindow(stuff->window, client);
  771.         if (!pWin)
  772.             return(BadWindow);
  773.     }
  774.     else
  775.         pWin = (WindowPtr)None;
  776.     if (ValidAtom(stuff->selection))
  777.     {
  778.     int i = 0;
  779.  
  780.     /*
  781.      * First, see if the selection is already set... 
  782.      */
  783.     while ((i < NumCurrentSelections) && 
  784.            CurrentSelections[i].selection != stuff->selection) 
  785.             i++;
  786.         if (i < NumCurrentSelections)
  787.         {        
  788.         xEvent event;
  789.  
  790.         /* If the timestamp in client's request is in the past relative
  791.         to the time stamp indicating the last time the owner of the
  792.         selection was set, do not set the selection, just return 
  793.         success. */
  794.             if (CompareTimeStamps(time, CurrentSelections[i].lastTimeChanged)
  795.         == EARLIER)
  796.         return Success;
  797.         if (CurrentSelections[i].client &&
  798.         (!pWin || (CurrentSelections[i].client != client)))
  799.         {
  800.         event.u.u.type = SelectionClear;
  801.         event.u.selectionClear.time = time.milliseconds;
  802.         event.u.selectionClear.window = CurrentSelections[i].window;
  803.         event.u.selectionClear.atom = CurrentSelections[i].selection;
  804.         (void) TryClientEvents (CurrentSelections[i].client, &event, 1,
  805.                 NoEventMask, NoEventMask /* CantBeFiltered */,
  806.                 NullGrab);
  807.         }
  808.     }
  809.     else
  810.     {
  811.         /*
  812.          * It doesn't exist, so add it...
  813.          */
  814.         Selection *newsels;
  815.  
  816.         if (i == 0)
  817.         newsels = (Selection *)xalloc(sizeof(Selection));
  818.         else
  819.         newsels = (Selection *)xrealloc(CurrentSelections,
  820.                 (NumCurrentSelections + 1) * sizeof(Selection));
  821.         if (!newsels)
  822.         return BadAlloc;
  823.         NumCurrentSelections++;
  824.         CurrentSelections = newsels;
  825.         CurrentSelections[i].selection = stuff->selection;
  826.     }
  827.         CurrentSelections[i].lastTimeChanged = time;
  828.     CurrentSelections[i].window = stuff->window;
  829.     CurrentSelections[i].pWin = pWin;
  830.     CurrentSelections[i].client = (pWin ? client : NullClient);
  831.     return (client->noClientException);
  832.     }
  833.     else 
  834.     {
  835.     client->errorValue = stuff->selection;
  836.         return (BadAtom);
  837.     }
  838. }
  839.  
  840. int
  841. ProcGetSelectionOwner(client)
  842.     register ClientPtr client;
  843. {
  844.     REQUEST(xResourceReq);
  845.  
  846.     REQUEST_SIZE_MATCH(xResourceReq);
  847.     if (ValidAtom(stuff->id))
  848.     {
  849.     int i;
  850.         xGetSelectionOwnerReply reply;
  851.  
  852.     i = 0;
  853.         while ((i < NumCurrentSelections) && 
  854.            CurrentSelections[i].selection != stuff->id) i++;
  855.         reply.type = X_Reply;
  856.     reply.length = 0;
  857.     reply.sequenceNumber = client->sequence;
  858.         if (i < NumCurrentSelections)
  859.             reply.owner = CurrentSelections[i].window;
  860.         else
  861.             reply.owner = None;
  862.         WriteReplyToClient(client, sizeof(xGetSelectionOwnerReply), &reply);
  863.         return(client->noClientException);
  864.     }
  865.     else            
  866.     {
  867.     client->errorValue = stuff->id;
  868.         return (BadAtom); 
  869.     }
  870. }
  871.  
  872. int
  873. ProcConvertSelection(client)
  874.     register ClientPtr client;
  875. {
  876.     Bool paramsOkay;
  877.     xEvent event;
  878.     WindowPtr pWin;
  879.     REQUEST(xConvertSelectionReq);
  880.  
  881.     REQUEST_SIZE_MATCH(xConvertSelectionReq);
  882.     pWin = (WindowPtr)LookupWindow(stuff->requestor, client);
  883.     if (!pWin)
  884.         return(BadWindow);
  885.  
  886.     paramsOkay = (ValidAtom(stuff->selection) && ValidAtom(stuff->target));
  887.     if (stuff->property != None)
  888.     paramsOkay &= ValidAtom(stuff->property);
  889.     if (paramsOkay)
  890.     {
  891.     int i;
  892.  
  893.     i = 0;
  894.     while ((i < NumCurrentSelections) && 
  895.            CurrentSelections[i].selection != stuff->selection) i++;
  896.     if ((i < NumCurrentSelections) && 
  897.         (CurrentSelections[i].window != None))
  898.     {        
  899.         event.u.u.type = SelectionRequest;
  900.         event.u.selectionRequest.time = stuff->time;
  901.         event.u.selectionRequest.owner = 
  902.             CurrentSelections[i].window;
  903.         event.u.selectionRequest.requestor = stuff->requestor;
  904.         event.u.selectionRequest.selection = stuff->selection;
  905.         event.u.selectionRequest.target = stuff->target;
  906.         event.u.selectionRequest.property = stuff->property;
  907.         if (TryClientEvents(
  908.         CurrentSelections[i].client, &event, 1, NoEventMask,
  909.         NoEventMask /* CantBeFiltered */, NullGrab))
  910.         return (client->noClientException);
  911.     }
  912.     event.u.u.type = SelectionNotify;
  913.     event.u.selectionNotify.time = stuff->time;
  914.     event.u.selectionNotify.requestor = stuff->requestor;
  915.     event.u.selectionNotify.selection = stuff->selection;
  916.     event.u.selectionNotify.target = stuff->target;
  917.     event.u.selectionNotify.property = None;
  918.     (void) TryClientEvents(client, &event, 1, NoEventMask,
  919.                    NoEventMask /* CantBeFiltered */, NullGrab);
  920.     return (client->noClientException);
  921.     }
  922.     else 
  923.     {
  924.     client->errorValue = stuff->property;
  925.         return (BadAtom);
  926.     }
  927. }
  928.  
  929. int
  930. ProcGrabServer(client)
  931.     register ClientPtr client;
  932. {
  933.     OnlyListenToOneClient(client);
  934.     grabbingClient = TRUE;
  935.     onlyClient = client;
  936.     return(client->noClientException);
  937. }
  938.  
  939. int
  940. ProcUngrabServer(client)
  941.     register ClientPtr client;
  942. {
  943.     REQUEST(xReq);
  944.     REQUEST_SIZE_MATCH(xReq);
  945.     grabbingClient = FALSE;
  946.     ListenToAllClients();
  947.     return(client->noClientException);
  948. }
  949.  
  950. int
  951. ProcTranslateCoords(client)
  952.     register ClientPtr client;
  953. {
  954.     REQUEST(xTranslateCoordsReq);
  955.  
  956.     register WindowPtr pWin, pDst;
  957.     xTranslateCoordsReply rep;
  958.  
  959.     REQUEST_SIZE_MATCH(xTranslateCoordsReq);
  960.     pWin = (WindowPtr)LookupWindow(stuff->srcWid, client);
  961.     if (!pWin)
  962.         return(BadWindow);
  963.     pDst = (WindowPtr)LookupWindow(stuff->dstWid, client);
  964.     if (!pDst)
  965.         return(BadWindow);
  966.     rep.type = X_Reply;
  967.     rep.length = 0;
  968.     rep.sequenceNumber = client->sequence;
  969.     if (!SAME_SCREENS(pWin->drawable, pDst->drawable))
  970.     {
  971.     rep.sameScreen = xFalse;
  972.         rep.child = None;
  973.     rep.dstX = rep.dstY = 0;
  974.     }
  975.     else
  976.     {
  977.     INT16 x, y;
  978.     rep.sameScreen = xTrue;
  979.     rep.child = None;
  980.     /* computing absolute coordinates -- adjust to destination later */
  981.     x = pWin->drawable.x + stuff->srcX;
  982.     y = pWin->drawable.y + stuff->srcY;
  983.     pWin = pDst->firstChild;
  984.     while (pWin)
  985.     {
  986. #ifdef SHAPE
  987.         BoxRec  box;
  988. #endif
  989.         if ((pWin->mapped) &&
  990.         (x >= pWin->drawable.x - wBorderWidth (pWin)) &&
  991.         (x < pWin->drawable.x + (int)pWin->drawable.width +
  992.          wBorderWidth (pWin)) &&
  993.         (y >= pWin->drawable.y - wBorderWidth (pWin)) &&
  994.         (y < pWin->drawable.y + (int)pWin->drawable.height +
  995.          wBorderWidth (pWin))
  996. #ifdef SHAPE
  997.         /* When a window is shaped, a further check
  998.          * is made to see if the point is inside
  999.          * borderSize
  1000.          */
  1001.         && (!wBoundingShape(pWin) ||
  1002.             (*pWin->drawable.pScreen->PointInRegion)
  1003.                 (&pWin->borderSize, x, y, &box))
  1004. #endif
  1005.         )
  1006.             {
  1007.         rep.child = pWin->drawable.id;
  1008.         pWin = (WindowPtr) NULL;
  1009.         }
  1010.         else
  1011.         pWin = pWin->nextSib;
  1012.     }
  1013.     /* adjust to destination coordinates */
  1014.     rep.dstX = x - pDst->drawable.x;
  1015.     rep.dstY = y - pDst->drawable.y;
  1016.     }
  1017.     WriteReplyToClient(client, sizeof(xTranslateCoordsReply), &rep);
  1018.     return(client->noClientException);
  1019. }
  1020.  
  1021. int
  1022. ProcOpenFont(client)
  1023.     register ClientPtr client;
  1024. {
  1025.     int    err;
  1026.     REQUEST(xOpenFontReq);
  1027.  
  1028.     REQUEST_FIXED_SIZE(xOpenFontReq, stuff->nbytes);
  1029.     client->errorValue = stuff->fid;
  1030.     LEGAL_NEW_RESOURCE(stuff->fid, client);
  1031.     err = OpenFont(client, stuff->fid, (Mask) 0,
  1032.         stuff->nbytes, (char *)&stuff[1]);
  1033.     if (err == Success)
  1034.     {
  1035.     return(client->noClientException);
  1036.     }
  1037.     else
  1038.     return err;
  1039. }
  1040.  
  1041. int
  1042. ProcCloseFont(client)
  1043.     register ClientPtr client;
  1044. {
  1045.     FontPtr pFont;
  1046.     REQUEST(xResourceReq);
  1047.  
  1048.     REQUEST_SIZE_MATCH(xResourceReq);
  1049.     pFont = (FontPtr)LookupIDByType(stuff->id, RT_FONT);
  1050.     if ( pFont != (FontPtr)NULL)    /* id was valid */
  1051.     {
  1052.         FreeResource(stuff->id, RT_NONE);
  1053.     return(client->noClientException);
  1054.     }
  1055.     else
  1056.     {
  1057.     client->errorValue = stuff->id;
  1058.         return (BadFont);
  1059.     }
  1060. }
  1061.  
  1062. int
  1063. ProcQueryFont(client)
  1064.     register ClientPtr client;
  1065. {
  1066.     xQueryFontReply    *reply;
  1067.     FontPtr pFont;
  1068.     register GC *pGC;
  1069.     REQUEST(xResourceReq);
  1070.  
  1071.     REQUEST_SIZE_MATCH(xResourceReq);
  1072.     client->errorValue = stuff->id;        /* EITHER font or gc */
  1073.     pFont = (FontPtr)LookupIDByType(stuff->id, RT_FONT);
  1074.     if (!pFont)
  1075.     {
  1076.       /* can't use VERIFY_GC because it might return BadGC */
  1077.     pGC = (GC *) LookupIDByType(stuff->id, RT_GC);
  1078.         if (!pGC)
  1079.     {
  1080.         client->errorValue = stuff->id;
  1081.             return(BadFont);     /* procotol spec says only error is BadFont */
  1082.     }
  1083.     pFont = pGC->font;
  1084.     }
  1085.  
  1086.     {
  1087.     xCharInfo    *pmax = FONTINKMAX(pFont);
  1088.     xCharInfo    *pmin = FONTINKMIN(pFont);
  1089.     int        nprotoxcistructs;
  1090.     int        rlength;
  1091.  
  1092.     nprotoxcistructs = (
  1093.        pmax->rightSideBearing == pmin->rightSideBearing &&
  1094.        pmax->leftSideBearing == pmin->leftSideBearing &&
  1095.        pmax->descent == pmin->descent &&
  1096.        pmax->ascent == pmin->ascent &&
  1097.        pmax->characterWidth == pmin->characterWidth) ?
  1098.         0 : N2dChars(pFont);
  1099.  
  1100.     rlength = sizeof(xQueryFontReply) +
  1101.                  FONTINFONPROPS(FONTCHARSET(pFont)) * sizeof(xFontProp)  +
  1102.              nprotoxcistructs * sizeof(xCharInfo);
  1103.     reply = (xQueryFontReply *)ALLOCATE_LOCAL(rlength);
  1104.     if(!reply)
  1105.     {
  1106.         return(BadAlloc);
  1107.     }
  1108.  
  1109.     reply->type = X_Reply;
  1110.     reply->length = (rlength - sizeof(xGenericReply)) >> 2;
  1111.     reply->sequenceNumber = client->sequence;
  1112.     QueryFont( pFont, reply, nprotoxcistructs);
  1113.  
  1114.         WriteReplyToClient(client, rlength, reply);
  1115.     DEALLOCATE_LOCAL(reply);
  1116.     return(client->noClientException);
  1117.     }
  1118. }
  1119.  
  1120. int
  1121. ProcQueryTextExtents(client)
  1122.     register ClientPtr client;
  1123. {
  1124.     REQUEST(xQueryTextExtentsReq);
  1125.     xQueryTextExtentsReply reply;
  1126.     FontPtr pFont;
  1127.     GC *pGC;
  1128.     ExtentInfoRec info;
  1129.     unsigned long length;
  1130.  
  1131.     REQUEST_AT_LEAST_SIZE(xQueryTextExtentsReq);
  1132.         
  1133.     pFont = (FontPtr)LookupIDByType(stuff->fid, RT_FONT);
  1134.     if (!pFont)
  1135.     {
  1136.         pGC = (GC *)LookupIDByType(stuff->fid, RT_GC);
  1137.         if (!pGC)
  1138.     {
  1139.         client->errorValue = stuff->fid;
  1140.             return(BadFont);
  1141.     }
  1142.     pFont = pGC->font;
  1143.     }
  1144.     length = stuff->length - (sizeof(xQueryTextExtentsReq) >> 2);
  1145.     length = length << 1;
  1146.     if (stuff->oddLength)
  1147.     {
  1148.     if (length == 0)
  1149.         return(BadLength);
  1150.         length--;
  1151.     }
  1152.     if (!QueryTextExtents(pFont, length, (unsigned char *)&stuff[1], &info))
  1153.     return(BadAlloc);
  1154.     reply.type = X_Reply;
  1155.     reply.length = 0;
  1156.     reply.sequenceNumber = client->sequence;
  1157.     reply.drawDirection = info.drawDirection;
  1158.     reply.fontAscent = info.fontAscent;
  1159.     reply.fontDescent = info.fontDescent;
  1160.     reply.overallAscent = info.overallAscent;
  1161.     reply.overallDescent = info.overallDescent;
  1162.     reply.overallWidth = info.overallWidth;
  1163.     reply.overallLeft = info.overallLeft;
  1164.     reply.overallRight = info.overallRight;
  1165.     WriteReplyToClient(client, sizeof(xQueryTextExtentsReply), &reply);
  1166.     return(client->noClientException);
  1167. }
  1168.  
  1169. int
  1170. ProcListFonts(client)
  1171.     register ClientPtr client;
  1172. {
  1173.     REQUEST(xListFontsReq);
  1174.  
  1175.     REQUEST_FIXED_SIZE(xListFontsReq, stuff->nbytes);
  1176.  
  1177.     return ListFonts(client, (unsigned char *) &stuff[1], stuff->nbytes, 
  1178.     stuff->maxNames);
  1179. }
  1180.  
  1181. int
  1182. ProcListFontsWithInfo(client)
  1183.     register ClientPtr client;
  1184. {
  1185.     REQUEST(xListFontsWithInfoReq);
  1186.  
  1187.     REQUEST_FIXED_SIZE(xListFontsWithInfoReq, stuff->nbytes);
  1188.  
  1189.     return StartListFontsWithInfo(client, stuff->nbytes,
  1190.                     (char *) &stuff[1], stuff->maxNames);
  1191. }
  1192.  
  1193. /*ARGSUSED*/
  1194. dixDestroyPixmap(pPixmap, pid)
  1195.     PixmapPtr pPixmap;
  1196.     Pixmap pid;
  1197. {
  1198.     (*pPixmap->drawable.pScreen->DestroyPixmap)(pPixmap);
  1199. }
  1200.  
  1201. int
  1202. ProcCreatePixmap(client)
  1203.     register ClientPtr client;
  1204. {
  1205.     PixmapPtr pMap;
  1206.     register DrawablePtr pDraw;
  1207.     REQUEST(xCreatePixmapReq);
  1208.     DepthPtr pDepth;
  1209.     register int i;
  1210.  
  1211.     REQUEST_SIZE_MATCH(xCreatePixmapReq);
  1212.     client->errorValue = stuff->pid;
  1213.     LEGAL_NEW_RESOURCE(stuff->pid, client);
  1214.     if (!(pDraw = LOOKUP_DRAWABLE(stuff->drawable, client)))
  1215.     {        /* can be inputonly */
  1216.         if (!(pDraw = (DrawablePtr)LookupWindow(stuff->drawable, client))) 
  1217.             return (BadDrawable);
  1218.     }
  1219.  
  1220.     if (!stuff->width || !stuff->height)
  1221.     {
  1222.     client->errorValue = 0;
  1223.         return BadValue;
  1224.     }
  1225.     if (stuff->depth != 1)
  1226.     {
  1227.         pDepth = pDraw->pScreen->allowedDepths;
  1228.         for (i=0; i<pDraw->pScreen->numDepths; i++, pDepth++)
  1229.        if (pDepth->depth == stuff->depth)
  1230.                goto CreatePmap;
  1231.     client->errorValue = stuff->depth;
  1232.         return BadValue;
  1233.     }
  1234. CreatePmap:
  1235.     pMap = (PixmapPtr)(*pDraw->pScreen->CreatePixmap)
  1236.         (pDraw->pScreen, stuff->width,
  1237.          stuff->height, stuff->depth);
  1238.     if (pMap)
  1239.     {
  1240.     pMap->drawable.serialNumber = NEXT_SERIAL_NUMBER;
  1241.     pMap->drawable.id = stuff->pid;
  1242.     if (AddResource(stuff->pid, RT_PIXMAP, (pointer)pMap))
  1243.         return(client->noClientException);
  1244.     }
  1245.     return (BadAlloc);
  1246. }
  1247.  
  1248. int
  1249. ProcFreePixmap(client)
  1250.     register ClientPtr client;
  1251. {
  1252.     PixmapPtr pMap;
  1253.  
  1254.     REQUEST(xResourceReq);
  1255.  
  1256.     REQUEST_SIZE_MATCH(xResourceReq);
  1257.     pMap = (PixmapPtr)LookupIDByType(stuff->id, RT_PIXMAP);
  1258.     if (pMap) 
  1259.     {
  1260.     FreeResource(stuff->id, RT_NONE);
  1261.     return(client->noClientException);
  1262.     }
  1263.     else 
  1264.     {
  1265.     client->errorValue = stuff->id;
  1266.     return (BadPixmap);
  1267.     }
  1268. }
  1269.  
  1270. int
  1271. ProcCreateGC(client)
  1272.     register ClientPtr client;
  1273. {
  1274.     int error;
  1275.     GC *pGC;
  1276.     register DrawablePtr pDraw;
  1277.     unsigned len;
  1278.     REQUEST(xCreateGCReq);
  1279.  
  1280.     REQUEST_AT_LEAST_SIZE(xCreateGCReq);
  1281.     client->errorValue = stuff->gc;
  1282.     LEGAL_NEW_RESOURCE(stuff->gc, client);
  1283.     if (!(pDraw = LOOKUP_DRAWABLE( stuff->drawable, client) ))
  1284.     {
  1285.     client->errorValue = stuff->drawable;
  1286.         return (BadDrawable);
  1287.     }
  1288.     len = stuff->length -  (sizeof(xCreateGCReq) >> 2);
  1289.     if (len != Ones(stuff->mask))
  1290.         return BadLength;
  1291.     pGC = (GC *)CreateGC(pDraw, stuff->mask, 
  1292.              (XID *) &stuff[1], &error);
  1293.     if (error != Success)
  1294.         return error;
  1295.     if (!AddResource(stuff->gc, RT_GC, (pointer)pGC))
  1296.     return (BadAlloc);
  1297.     return(client->noClientException);
  1298. }
  1299.  
  1300. int
  1301. ProcChangeGC(client)
  1302.     register ClientPtr client;
  1303. {
  1304.     GC *pGC;
  1305.     REQUEST(xChangeGCReq);
  1306.     int result;
  1307.     unsigned len;
  1308.         
  1309.     REQUEST_AT_LEAST_SIZE(xChangeGCReq);
  1310.     VERIFY_GC(pGC, stuff->gc, client);
  1311.     len = stuff->length -  (sizeof(xChangeGCReq) >> 2);
  1312.     if (len != Ones(stuff->mask))
  1313.         return BadLength;
  1314.     result = DoChangeGC(pGC, stuff->mask, (XID *) &stuff[1], 0);
  1315.     if (client->noClientException != Success)
  1316.         return(client->noClientException);
  1317.     else
  1318.     {
  1319.     client->errorValue = clientErrorValue;
  1320.         return(result);
  1321.     }
  1322. }
  1323.  
  1324. int
  1325. ProcCopyGC(client)
  1326.     register ClientPtr client;
  1327. {
  1328.     register GC *dstGC;
  1329.     register GC *pGC;
  1330.     int result;
  1331.     REQUEST(xCopyGCReq);
  1332.  
  1333.     REQUEST_SIZE_MATCH(xCopyGCReq);
  1334.     VERIFY_GC( pGC, stuff->srcGC, client);
  1335.     VERIFY_GC( dstGC, stuff->dstGC, client);
  1336.     if ((dstGC->pScreen != pGC->pScreen) || (dstGC->depth != pGC->depth))
  1337.         return (BadMatch);    
  1338.     result = CopyGC(pGC, dstGC, stuff->mask);
  1339.     if (client->noClientException != Success)
  1340.         return(client->noClientException);
  1341.     else
  1342.     {
  1343.     client->errorValue = clientErrorValue;
  1344.         return(result);
  1345.     }
  1346. }
  1347.  
  1348. int
  1349. ProcSetDashes(client)
  1350.     register ClientPtr client;
  1351. {
  1352.     register GC *pGC;
  1353.     int result;
  1354.     REQUEST(xSetDashesReq);
  1355.  
  1356.     REQUEST_FIXED_SIZE(xSetDashesReq, stuff->nDashes);
  1357.     if (stuff->nDashes == 0)
  1358.     {
  1359.      client->errorValue = 0;
  1360.          return BadValue;
  1361.     }
  1362.  
  1363.     VERIFY_GC(pGC,stuff->gc, client);
  1364.  
  1365.     result = SetDashes(pGC, stuff->dashOffset, stuff->nDashes,
  1366.                (unsigned char *)&stuff[1]);
  1367.     if (client->noClientException != Success)
  1368.         return(client->noClientException);
  1369.     else
  1370.     {
  1371.     client->errorValue = clientErrorValue;
  1372.         return(result);
  1373.     }
  1374. }
  1375.  
  1376. int
  1377. ProcSetClipRectangles(client)
  1378.     register ClientPtr client;
  1379. {
  1380.     int    nr;
  1381.     int result;
  1382.     register GC *pGC;
  1383.     REQUEST(xSetClipRectanglesReq);
  1384.  
  1385.     REQUEST_AT_LEAST_SIZE(xSetClipRectanglesReq);
  1386.     if ((stuff->ordering != Unsorted) && (stuff->ordering != YSorted) &&
  1387.     (stuff->ordering != YXSorted) && (stuff->ordering != YXBanded))
  1388.     {
  1389.     client->errorValue = stuff->ordering;
  1390.         return BadValue;
  1391.     }
  1392.     VERIFY_GC(pGC,stuff->gc, client);
  1393.          
  1394.     nr = (stuff->length << 2) - sizeof(xSetClipRectanglesReq);
  1395.     if (nr & 4)
  1396.     return(BadLength);
  1397.     nr >>= 3;
  1398.     result = SetClipRects(pGC, stuff->xOrigin, stuff->yOrigin,
  1399.               nr, (xRectangle *)&stuff[1], (int)stuff->ordering);
  1400.     if (client->noClientException != Success)
  1401.         return(client->noClientException);
  1402.     else
  1403.         return(result);
  1404. }
  1405.  
  1406. int
  1407. ProcFreeGC(client)
  1408.     register ClientPtr client;
  1409. {
  1410.     register GC *pGC;
  1411.     REQUEST(xResourceReq);
  1412.  
  1413.     REQUEST_SIZE_MATCH(xResourceReq);
  1414.     VERIFY_GC(pGC,stuff->id,client);
  1415.     FreeResource(stuff->id, RT_NONE);
  1416.     return(client->noClientException);
  1417. }
  1418.  
  1419. int
  1420. ProcClearToBackground(client)
  1421.     register ClientPtr client;
  1422. {
  1423.     REQUEST(xClearAreaReq);
  1424.     register WindowPtr pWin;
  1425.  
  1426.     REQUEST_SIZE_MATCH(xClearAreaReq);
  1427.     pWin = (WindowPtr)LookupWindow( stuff->window, client);
  1428.     if (!pWin)
  1429.         return(BadWindow);
  1430.     if (pWin->drawable.class == InputOnly)
  1431.     {
  1432.     client->errorValue = stuff->window;
  1433.     return (BadMatch);
  1434.     }            
  1435.     if ((stuff->exposures != xTrue) && (stuff->exposures != xFalse))
  1436.     {
  1437.     client->errorValue = stuff->exposures;
  1438.         return(BadValue);
  1439.     }
  1440.     (*pWin->drawable.pScreen->ClearToBackground)(pWin, stuff->x, stuff->y,
  1441.                    stuff->width, stuff->height,
  1442.                    (Bool)stuff->exposures);
  1443.     return(client->noClientException);
  1444. }
  1445.  
  1446. int
  1447. ProcCopyArea(client)
  1448.     register ClientPtr client;
  1449. {
  1450.     register DrawablePtr pDst;
  1451.     register DrawablePtr pSrc;
  1452.     register GC *pGC;
  1453.     REQUEST(xCopyAreaReq);
  1454.     RegionPtr pRgn;
  1455.  
  1456.     REQUEST_SIZE_MATCH(xCopyAreaReq);
  1457.  
  1458.     VALIDATE_DRAWABLE_AND_GC(stuff->dstDrawable, pDst, pGC, client); 
  1459.     if (stuff->dstDrawable != stuff->srcDrawable)
  1460.     {
  1461.         if (!(pSrc = LOOKUP_DRAWABLE(stuff->srcDrawable, client)))
  1462.     {
  1463.         client->errorValue = stuff->srcDrawable;
  1464.             return(BadDrawable);
  1465.     }
  1466.     if ((pDst->pScreen != pSrc->pScreen) || (pDst->depth != pSrc->depth))
  1467.     {
  1468.         client->errorValue = stuff->dstDrawable;
  1469.         return (BadMatch);
  1470.     }
  1471.     }
  1472.     else
  1473.         pSrc = pDst;
  1474.     pRgn = (*pGC->ops->CopyArea)(pSrc, pDst, pGC, stuff->srcX, stuff->srcY,
  1475.                  stuff->width, stuff->height, 
  1476.                  stuff->dstX, stuff->dstY);
  1477.     if (pGC->graphicsExposures)
  1478.     {
  1479.     (*pDst->pScreen->SendGraphicsExpose)
  1480.          (client, pRgn, stuff->dstDrawable, X_CopyArea, 0);
  1481.     if (pRgn)
  1482.         (*pDst->pScreen->RegionDestroy) (pRgn);
  1483.     }
  1484.  
  1485.     return(client->noClientException);
  1486. }
  1487.  
  1488. int
  1489. ProcCopyPlane(client)
  1490.     register ClientPtr client;
  1491. {
  1492.     register DrawablePtr psrcDraw, pdstDraw;
  1493.     register GC *pGC;
  1494.     REQUEST(xCopyPlaneReq);
  1495.     RegionPtr pRgn;
  1496.  
  1497.     REQUEST_SIZE_MATCH(xCopyPlaneReq);
  1498.  
  1499.     VALIDATE_DRAWABLE_AND_GC(stuff->dstDrawable, pdstDraw, pGC, client);
  1500.     if (stuff->dstDrawable != stuff->srcDrawable)
  1501.     {
  1502.         if (!(psrcDraw = LOOKUP_DRAWABLE(stuff->srcDrawable, client)))
  1503.     {
  1504.         client->errorValue = stuff->srcDrawable;
  1505.             return(BadDrawable);
  1506.     }
  1507.     if (pdstDraw->pScreen != psrcDraw->pScreen)
  1508.     {
  1509.         client->errorValue = stuff->dstDrawable;
  1510.         return (BadMatch);
  1511.     }
  1512.     }
  1513.     else
  1514.         psrcDraw = pdstDraw;
  1515.  
  1516.     /* Check to see if stuff->bitPlane has exactly ONE good bit set */
  1517.     if(stuff->bitPlane == 0 || (stuff->bitPlane & (stuff->bitPlane - 1)) ||
  1518.        (stuff->bitPlane > (1L << (psrcDraw->depth - 1))))
  1519.     {
  1520.        client->errorValue = stuff->bitPlane;
  1521.        return(BadValue);
  1522.     }
  1523.  
  1524.     pRgn = (*pGC->ops->CopyPlane)(psrcDraw, pdstDraw, pGC, stuff->srcX, stuff->srcY,
  1525.                  stuff->width, stuff->height, 
  1526.                  stuff->dstX, stuff->dstY, stuff->bitPlane);
  1527.     if (pGC->graphicsExposures)
  1528.     {
  1529.     (*pdstDraw->pScreen->SendGraphicsExpose)
  1530.          (client, pRgn, stuff->dstDrawable, X_CopyPlane, 0);
  1531.     if (pRgn)
  1532.         (*pdstDraw->pScreen->RegionDestroy) (pRgn);
  1533.     }
  1534.     return(client->noClientException);
  1535. }
  1536.  
  1537. int
  1538. ProcPolyPoint(client)
  1539.     register ClientPtr client;
  1540. {
  1541.     int npoint;
  1542.     register GC *pGC;
  1543.     register DrawablePtr pDraw;
  1544.     REQUEST(xPolyPointReq);
  1545.  
  1546.     REQUEST_AT_LEAST_SIZE(xPolyPointReq);
  1547.     if ((stuff->coordMode != CoordModeOrigin) && 
  1548.     (stuff->coordMode != CoordModePrevious))
  1549.     {
  1550.     client->errorValue = stuff->coordMode;
  1551.         return BadValue;
  1552.     }
  1553.     VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client); 
  1554.     npoint = ((stuff->length << 2) - sizeof(xPolyPointReq)) >> 2;
  1555.     if (npoint)
  1556.         (*pGC->ops->PolyPoint)(pDraw, pGC, stuff->coordMode, npoint,
  1557.               (xPoint *) &stuff[1]);
  1558.     return (client->noClientException);
  1559. }
  1560.  
  1561. int
  1562. ProcPolyLine(client)
  1563.     register ClientPtr client;
  1564. {
  1565.     int npoint;
  1566.     register GC *pGC;
  1567.     register DrawablePtr pDraw;
  1568.     REQUEST(xPolyLineReq);
  1569.  
  1570.     REQUEST_AT_LEAST_SIZE(xPolyLineReq);
  1571.     if ((stuff->coordMode != CoordModeOrigin) && 
  1572.     (stuff->coordMode != CoordModePrevious))
  1573.     {
  1574.     client->errorValue = stuff->coordMode;
  1575.         return BadValue;
  1576.     }
  1577.     VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1578.     npoint = ((stuff->length << 2) - sizeof(xPolyLineReq)) >> 2;
  1579.     if (npoint)
  1580.     (*pGC->ops->Polylines)(pDraw, pGC, stuff->coordMode, npoint, 
  1581.                   (xPoint *) &stuff[1]);
  1582.     return(client->noClientException);
  1583. }
  1584.  
  1585. int
  1586. ProcPolySegment(client)
  1587.     register ClientPtr client;
  1588. {
  1589.     int nsegs;
  1590.     register GC *pGC;
  1591.     register DrawablePtr pDraw;
  1592.     REQUEST(xPolySegmentReq);
  1593.  
  1594.     REQUEST_AT_LEAST_SIZE(xPolySegmentReq);
  1595.     VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1596.     nsegs = (stuff->length << 2) - sizeof(xPolySegmentReq);
  1597.     if (nsegs & 4)
  1598.     return(BadLength);
  1599.     nsegs >>= 3;
  1600.     if (nsegs)
  1601.         (*pGC->ops->PolySegment)(pDraw, pGC, nsegs, (xSegment *) &stuff[1]);
  1602.     return (client->noClientException);
  1603. }
  1604.  
  1605. int
  1606. ProcPolyRectangle (client)
  1607.     register ClientPtr client;
  1608. {
  1609.     int nrects;
  1610.     register GC *pGC;
  1611.     register DrawablePtr pDraw;
  1612.     REQUEST(xPolyRectangleReq);
  1613.  
  1614.     REQUEST_AT_LEAST_SIZE(xPolyRectangleReq);
  1615.     VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1616.     nrects = (stuff->length << 2) - sizeof(xPolyRectangleReq);
  1617.     if (nrects & 4)
  1618.     return(BadLength);
  1619.     nrects >>= 3;
  1620.     if (nrects)
  1621.         (*pGC->ops->PolyRectangle)(pDraw, pGC, 
  1622.             nrects, (xRectangle *) &stuff[1]);
  1623.     return(client->noClientException);
  1624. }
  1625.  
  1626. int
  1627. ProcPolyArc(client)
  1628.     register ClientPtr client;
  1629. {
  1630.     int        narcs;
  1631.     register GC *pGC;
  1632.     register DrawablePtr pDraw;
  1633.     REQUEST(xPolyArcReq);
  1634.  
  1635.     REQUEST_AT_LEAST_SIZE(xPolyArcReq);
  1636.     VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1637.     narcs = (stuff->length << 2) - sizeof(xPolyArcReq);
  1638.     if (narcs % sizeof(xArc))
  1639.     return(BadLength);
  1640.     narcs /= sizeof(xArc);
  1641.     if (narcs)
  1642.         (*pGC->ops->PolyArc)(pDraw, pGC, narcs, (xArc *) &stuff[1]);
  1643.     return (client->noClientException);
  1644. }
  1645.  
  1646. int
  1647. ProcFillPoly(client)
  1648.     register ClientPtr client;
  1649. {
  1650.     int          things;
  1651.     register GC *pGC;
  1652.     register DrawablePtr pDraw;
  1653.     REQUEST(xFillPolyReq);
  1654.  
  1655.     REQUEST_AT_LEAST_SIZE(xFillPolyReq);
  1656.     if ((stuff->shape != Complex) && (stuff->shape != Nonconvex) &&  
  1657.     (stuff->shape != Convex))
  1658.     {
  1659.     client->errorValue = stuff->shape;
  1660.         return BadValue;
  1661.     }
  1662.     if ((stuff->coordMode != CoordModeOrigin) && 
  1663.     (stuff->coordMode != CoordModePrevious))
  1664.     {
  1665.     client->errorValue = stuff->coordMode;
  1666.         return BadValue;
  1667.     }
  1668.  
  1669.     VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1670.     things = ((stuff->length << 2) - sizeof(xFillPolyReq)) >> 2;
  1671.     if (things)
  1672.         (*pGC->ops->FillPolygon) (pDraw, pGC, stuff->shape,
  1673.              stuff->coordMode, things,
  1674.              (DDXPointPtr) &stuff[1]);
  1675.     return(client->noClientException);
  1676. }
  1677.  
  1678. int
  1679. ProcPolyFillRectangle(client)
  1680.     register ClientPtr client;
  1681. {
  1682.     int             things;
  1683.     register GC *pGC;
  1684.     register DrawablePtr pDraw;
  1685.     REQUEST(xPolyFillRectangleReq);
  1686.  
  1687.     REQUEST_AT_LEAST_SIZE(xPolyFillRectangleReq);
  1688.     VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1689.     things = (stuff->length << 2) - sizeof(xPolyFillRectangleReq);
  1690.     if (things & 4)
  1691.     return(BadLength);
  1692.     things >>= 3;
  1693.     if (things)
  1694.         (*pGC->ops->PolyFillRect) (pDraw, pGC, things,
  1695.               (xRectangle *) &stuff[1]);
  1696.     return (client->noClientException);
  1697. }
  1698.  
  1699. int
  1700. ProcPolyFillArc               (client)
  1701.     register ClientPtr client;
  1702. {
  1703.     int        narcs;
  1704.     register GC *pGC;
  1705.     register DrawablePtr pDraw;
  1706.     REQUEST(xPolyFillArcReq);
  1707.  
  1708.     REQUEST_AT_LEAST_SIZE(xPolyFillArcReq);
  1709.     VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1710.     narcs = (stuff->length << 2) - sizeof(xPolyFillArcReq);
  1711.     if (narcs % sizeof(xArc))
  1712.     return(BadLength);
  1713.     narcs /= sizeof(xArc);
  1714.     if (narcs)
  1715.         (*pGC->ops->PolyFillArc) (pDraw, pGC, narcs, (xArc *) &stuff[1]);
  1716.     return (client->noClientException);
  1717. }
  1718.  
  1719. int
  1720. ProcPutImage(client)
  1721.     register ClientPtr client;
  1722. {
  1723.     register GC *pGC;
  1724.     register DrawablePtr pDraw;
  1725.     long length;
  1726.     REQUEST(xPutImageReq);
  1727.  
  1728.     REQUEST_AT_LEAST_SIZE(xPutImageReq);
  1729.     VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1730.     if (stuff->format == XYBitmap)
  1731.     {
  1732.         if ((stuff->depth != 1) ||
  1733.         (stuff->leftPad >= screenInfo.bitmapScanlinePad))
  1734.             return BadMatch;
  1735.         length = PixmapBytePad(stuff->width + stuff->leftPad, 1);
  1736.     }
  1737.     else if (stuff->format == XYPixmap)
  1738.     {
  1739.         if ((pDraw->depth != stuff->depth) || 
  1740.         (stuff->leftPad >= screenInfo.bitmapScanlinePad))
  1741.             return BadMatch;
  1742.         length = PixmapBytePad(stuff->width + stuff->leftPad, 1);
  1743.     length *= stuff->depth;
  1744.     }
  1745.     else if (stuff->format == ZPixmap)
  1746.     {
  1747.         if ((pDraw->depth != stuff->depth) || (stuff->leftPad != 0))
  1748.             return BadMatch;
  1749.         length = PixmapBytePad(stuff->width, stuff->depth);
  1750.     }
  1751.     else
  1752.     {
  1753.     client->errorValue = stuff->format;
  1754.         return BadValue;
  1755.     }
  1756.     length *= stuff->height;
  1757.     if ((((length + 3) >> 2) + (sizeof(xPutImageReq) >> 2)) != stuff->length)
  1758.     return BadLength;
  1759.     (*pGC->ops->PutImage) (pDraw, pGC, stuff->depth, stuff->dstX, stuff->dstY,
  1760.           stuff->width, stuff->height, 
  1761.           stuff->leftPad, stuff->format, 
  1762.           (char *) &stuff[1]);
  1763.      return (client->noClientException);
  1764. }
  1765.  
  1766. int
  1767. ProcGetImage(client)
  1768.     register ClientPtr    client;
  1769. {
  1770.     register DrawablePtr pDraw;
  1771.     int            nlines, linesPerBuf;
  1772.     register int    height, linesDone;
  1773.     long        widthBytesLine, length;
  1774.     Mask        plane;
  1775.     char        *pBuf;
  1776.     xGetImageReply    xgi;
  1777.  
  1778.     REQUEST(xGetImageReq);
  1779.  
  1780.     height = stuff->height;
  1781.     REQUEST_SIZE_MATCH(xGetImageReq);
  1782.     if ((stuff->format != XYPixmap) && (stuff->format != ZPixmap))
  1783.     {
  1784.     client->errorValue = stuff->format;
  1785.         return(BadValue);
  1786.     }
  1787.     if(!(pDraw = LOOKUP_DRAWABLE(stuff->drawable, client) ))
  1788.     {
  1789.     client->errorValue = stuff->drawable;
  1790.     return (BadDrawable);
  1791.     }
  1792.     if(pDraw->type == DRAWABLE_WINDOW)
  1793.     {
  1794.       if( /* check for being viewable */
  1795.      !((WindowPtr) pDraw)->realized ||
  1796.       /* check for being on screen */
  1797.          pDraw->x + stuff->x < 0 ||
  1798.       pDraw->x + stuff->x + (int)stuff->width > pDraw->pScreen->width ||
  1799.          pDraw->y + stuff->y < 0 ||
  1800.          pDraw->y + stuff->y + height > pDraw->pScreen->height ||
  1801.           /* check for being inside of border */
  1802.          stuff->x < - wBorderWidth((WindowPtr)pDraw) ||
  1803.          stuff->x + (int)stuff->width >
  1804.         wBorderWidth((WindowPtr)pDraw) + (int)pDraw->width ||
  1805.          stuff->y < -wBorderWidth((WindowPtr)pDraw) ||
  1806.          stuff->y + height >
  1807.         wBorderWidth ((WindowPtr)pDraw) + (int)pDraw->height
  1808.         )
  1809.         return(BadMatch);
  1810.     xgi.visual = wVisual (((WindowPtr) pDraw));
  1811.     }
  1812.     else
  1813.     {
  1814.       if(stuff->x < 0 ||
  1815.          stuff->x+(int)stuff->width > pDraw->width ||
  1816.          stuff->y < 0 ||
  1817.          stuff->y+height > pDraw->height
  1818.         )
  1819.         return(BadMatch);
  1820.     xgi.visual = None;
  1821.     }
  1822.     xgi.type = X_Reply;
  1823.     xgi.sequenceNumber = client->sequence;
  1824.     xgi.depth = pDraw->depth;
  1825.     if(stuff->format == ZPixmap)
  1826.     {
  1827.     widthBytesLine = PixmapBytePad(stuff->width, pDraw->depth);
  1828.     length = widthBytesLine * height;
  1829.     }
  1830.     else 
  1831.     {
  1832.     widthBytesLine = PixmapBytePad(stuff->width, 1);
  1833.     plane = ((Mask)1) << (pDraw->depth - 1);
  1834.     /* only planes asked for */
  1835.     length = widthBytesLine * height *
  1836.          Ones(stuff->planeMask & (plane | (plane - 1)));
  1837.     }
  1838.     xgi.length = (length + 3) >> 2;
  1839.     if (widthBytesLine == 0 || height == 0)
  1840.     linesPerBuf = 0;
  1841.     else if (widthBytesLine >= IMAGE_BUFSIZE)
  1842.     linesPerBuf = 1;
  1843.     else
  1844.     {
  1845.     linesPerBuf = IMAGE_BUFSIZE / widthBytesLine;
  1846.     if (linesPerBuf > height)
  1847.         linesPerBuf = height;
  1848.     }
  1849.     length = linesPerBuf * widthBytesLine;
  1850.     if (linesPerBuf < height)
  1851.     {
  1852.     /* we have to make sure intermediate buffers don't need padding */
  1853.     while ((linesPerBuf > 1) && (length & 3))
  1854.     {
  1855.         linesPerBuf--;
  1856.         length -= widthBytesLine;
  1857.     }
  1858.     while (length & 3)
  1859.     {
  1860.         linesPerBuf++;
  1861.         length += widthBytesLine;
  1862.     }
  1863.     }
  1864.     if(!(pBuf = (char *) ALLOCATE_LOCAL(length)))
  1865.         return (BadAlloc);
  1866.  
  1867.     WriteReplyToClient(client, sizeof (xGetImageReply), &xgi);
  1868.  
  1869.     if (linesPerBuf == 0)
  1870.     {
  1871.     /* nothing to do */
  1872.     }
  1873.     else if (stuff->format == ZPixmap)
  1874.     {
  1875.         linesDone = 0;
  1876.         while (height - linesDone > 0)
  1877.         {
  1878.         nlines = min(linesPerBuf, height - linesDone);
  1879.         (*pDraw->pScreen->GetImage) (pDraw,
  1880.                                      stuff->x,
  1881.                          stuff->y + linesDone,
  1882.                          stuff->width, 
  1883.                          nlines,
  1884.                          stuff->format,
  1885.                          stuff->planeMask,
  1886.                          pBuf);
  1887.         /* Note that this is NOT a call to WriteSwappedDataToClient,
  1888.                as we do NOT byte swap */
  1889.         (void)WriteToClient(client, (int)(nlines * widthBytesLine), pBuf);
  1890.         linesDone += nlines;
  1891.         }
  1892.     }
  1893.     else
  1894.     {
  1895.         for (; plane; plane >>= 1)
  1896.     {
  1897.         if (stuff->planeMask & plane)
  1898.         {
  1899.             linesDone = 0;
  1900.             while (height - linesDone > 0)
  1901.             {
  1902.             nlines = min(linesPerBuf, height - linesDone);
  1903.                 (*pDraw->pScreen->GetImage) (pDraw,
  1904.                                              stuff->x,
  1905.                                  stuff->y + linesDone,
  1906.                                  stuff->width, 
  1907.                                  nlines,
  1908.                                  stuff->format,
  1909.                                  plane,
  1910.                                  pBuf);
  1911.             /* Note: NOT a call to WriteSwappedDataToClient,
  1912.                as we do NOT byte swap */
  1913.             (void)WriteToClient(client, (int)(nlines * widthBytesLine),
  1914.                     pBuf);
  1915.             linesDone += nlines;
  1916.         }
  1917.             }
  1918.     }
  1919.     }
  1920.     DEALLOCATE_LOCAL(pBuf);
  1921.     return (client->noClientException);
  1922. }
  1923.  
  1924.  
  1925. int
  1926. ProcPolyText(client)
  1927.     register ClientPtr client;
  1928. {
  1929.     int        xorg;
  1930.     REQUEST(xPolyTextReq);
  1931.     register DrawablePtr pDraw;
  1932.     register GC *pGC;
  1933.     register FontPtr pFont;
  1934.  
  1935.     int (* polyText)();
  1936.     register unsigned char *pElt;
  1937.     unsigned char *pNextElt;
  1938.     unsigned char *endReq;
  1939.     int        itemSize;
  1940.     
  1941. #define TextEltHeader 2
  1942. #define FontShiftSize 5
  1943.  
  1944.     REQUEST_AT_LEAST_SIZE(xPolyTextReq);
  1945.     VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1946.  
  1947.     pElt = (unsigned char *)&stuff[1];
  1948.     endReq = ((unsigned char *) stuff) + (stuff->length <<2);
  1949.     xorg = stuff->x;
  1950.     if (stuff->reqType == X_PolyText8)
  1951.     {
  1952.     polyText = pGC->ops->PolyText8;
  1953.     itemSize = 1;
  1954.     }
  1955.     else
  1956.     {
  1957.     polyText =  pGC->ops->PolyText16;
  1958.     itemSize = 2;
  1959.     }
  1960.  
  1961.     while (endReq - pElt > TextEltHeader)
  1962.     {
  1963.     if (*pElt == FontChange)
  1964.         {
  1965.         Font    fid;
  1966.  
  1967.         if (endReq - pElt < FontShiftSize)
  1968.          return (BadLength);
  1969.         fid =  ((Font)*(pElt+4))        /* big-endian */
  1970.          | ((Font)*(pElt+3)) << 8
  1971.          | ((Font)*(pElt+2)) << 16
  1972.          | ((Font)*(pElt+1)) << 24;
  1973.         pFont = (FontPtr)LookupIDByType(fid, RT_FONT);
  1974.         if (!pFont)
  1975.         {
  1976.         client->errorValue = fid;
  1977.         return (BadFont);
  1978.         }
  1979.         if (pFont != pGC->font)
  1980.         {
  1981.         DoChangeGC( pGC, GCFont, &fid, 0);
  1982.         ValidateGC(pDraw, pGC);
  1983.         if (stuff->reqType == X_PolyText8)
  1984.             polyText = pGC->ops->PolyText8;
  1985.         else
  1986.             polyText = pGC->ops->PolyText16;
  1987.         }
  1988.         pElt += FontShiftSize;
  1989.     }
  1990.     else    /* print a string */
  1991.     {
  1992.         pNextElt = pElt + TextEltHeader + (*pElt)*itemSize;
  1993.         if ( pNextElt > endReq)
  1994.         return( BadLength);
  1995.         xorg += *((INT8 *)(pElt + 1));    /* must be signed */
  1996.         (void) LoadGlyphs(client, pGC->font, *pElt, itemSize,
  1997.         pElt + TextEltHeader);
  1998.         xorg = (* polyText)(pDraw, pGC, xorg, stuff->y, *pElt,
  1999.         pElt + TextEltHeader);
  2000.         pElt = pNextElt;
  2001.     }
  2002.     }
  2003.     return (client->noClientException);
  2004. #undef TextEltHeader
  2005. #undef FontShiftSize
  2006. }
  2007.  
  2008. int
  2009. ProcImageText8(client)
  2010.     register ClientPtr client;
  2011. {
  2012.     register DrawablePtr pDraw;
  2013.     register GC *pGC;
  2014.  
  2015.     REQUEST(xImageTextReq);
  2016.  
  2017.     REQUEST_FIXED_SIZE(xImageTextReq, stuff->nChars);
  2018.     VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  2019.  
  2020.     (void) LoadGlyphs(client, pGC->font, stuff->nChars, 1, &stuff[1]);
  2021.     (*pGC->ops->ImageText8)(pDraw, pGC, stuff->x, stuff->y,
  2022.                stuff->nChars, &stuff[1]);
  2023.     return (client->noClientException);
  2024. }
  2025.  
  2026. int
  2027. ProcImageText16(client)
  2028.     register ClientPtr client;
  2029. {
  2030.     register DrawablePtr pDraw;
  2031.     register GC *pGC;
  2032.  
  2033.     REQUEST(xImageTextReq);
  2034.  
  2035.     REQUEST_FIXED_SIZE(xImageTextReq, stuff->nChars << 1);
  2036.     VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  2037.  
  2038.     (void) LoadGlyphs(client, pGC->font, stuff->nChars, 2, &stuff[1]);
  2039.     (*pGC->ops->ImageText16)(pDraw, pGC, stuff->x, stuff->y,
  2040.             stuff->nChars, &stuff[1]);
  2041.     return (client->noClientException);
  2042. }
  2043.  
  2044.  
  2045. int
  2046. ProcCreateColormap(client)
  2047.     register ClientPtr client;
  2048. {
  2049.     VisualPtr    pVisual;
  2050.     ColormapPtr    pmap;
  2051.     Colormap    mid;
  2052.     register WindowPtr   pWin;
  2053.     ScreenPtr pScreen;
  2054.     REQUEST(xCreateColormapReq);
  2055.     int i, result;
  2056.  
  2057.     REQUEST_SIZE_MATCH(xCreateColormapReq);
  2058.  
  2059.     if ((stuff->alloc != AllocNone) && (stuff->alloc != AllocAll))
  2060.     {
  2061.     client->errorValue = stuff->alloc;
  2062.         return(BadValue);
  2063.     }
  2064.     mid = stuff->mid;
  2065.     LEGAL_NEW_RESOURCE(mid, client);
  2066.     pWin = (WindowPtr)LookupWindow(stuff->window, client);
  2067.     if (!pWin)
  2068.         return(BadWindow);
  2069.  
  2070.     pScreen = pWin->drawable.pScreen;
  2071.     for (i = 0, pVisual = pScreen->visuals;
  2072.      i < pScreen->numVisuals;
  2073.      i++, pVisual++)
  2074.     {
  2075.     if (pVisual->vid != stuff->visual)
  2076.         continue;
  2077.     result =  CreateColormap(mid, pScreen, pVisual, &pmap,
  2078.                  (int)stuff->alloc, client->index);
  2079.     if (client->noClientException != Success)
  2080.         return(client->noClientException);
  2081.     else
  2082.         return(result);
  2083.     }
  2084.     client->errorValue = stuff->visual;
  2085.     return(BadValue);
  2086. }
  2087.  
  2088. int
  2089. ProcFreeColormap(client)
  2090.     register ClientPtr client;
  2091. {
  2092.     ColormapPtr pmap;
  2093.     REQUEST(xResourceReq);
  2094.  
  2095.     REQUEST_SIZE_MATCH(xResourceReq);
  2096.     pmap = (ColormapPtr )LookupIDByType(stuff->id, RT_COLORMAP);
  2097.     if (pmap) 
  2098.     {
  2099.     /* Freeing a default colormap is a no-op */
  2100.     if (!(pmap->flags & IsDefault))
  2101.         FreeResource(stuff->id, RT_NONE);
  2102.     return (client->noClientException);
  2103.     }
  2104.     else 
  2105.     {
  2106.     client->errorValue = stuff->id;
  2107.     return (BadColor);
  2108.     }
  2109. }
  2110.  
  2111.  
  2112. int
  2113. ProcCopyColormapAndFree(client)
  2114.     register ClientPtr client;
  2115. {
  2116.     Colormap    mid;
  2117.     ColormapPtr    pSrcMap;
  2118.     REQUEST(xCopyColormapAndFreeReq);
  2119.     int result;
  2120.  
  2121.     REQUEST_SIZE_MATCH(xCopyColormapAndFreeReq);
  2122.     mid = stuff->mid;
  2123.     LEGAL_NEW_RESOURCE(mid, client);
  2124.     if(pSrcMap = (ColormapPtr )LookupIDByType(stuff->srcCmap, RT_COLORMAP))
  2125.     {
  2126.     result = CopyColormapAndFree(mid, pSrcMap, client->index);
  2127.     if (client->noClientException != Success)
  2128.             return(client->noClientException);
  2129.     else
  2130.             return(result);
  2131.     }
  2132.     else
  2133.     {
  2134.     client->errorValue = stuff->srcCmap;
  2135.     return(BadColor);
  2136.     }
  2137. }
  2138.  
  2139. int
  2140. ProcInstallColormap(client)
  2141.     register ClientPtr client;
  2142. {
  2143.     ColormapPtr pcmp;
  2144.     REQUEST(xResourceReq);
  2145.  
  2146.     REQUEST_SIZE_MATCH(xResourceReq);
  2147.     pcmp = (ColormapPtr  )LookupIDByType(stuff->id, RT_COLORMAP);
  2148.     if (pcmp)
  2149.     {
  2150.         (*(pcmp->pScreen->InstallColormap)) (pcmp);
  2151.         return (client->noClientException);        
  2152.     }
  2153.     else
  2154.     {
  2155.         client->errorValue = stuff->id;
  2156.         return (BadColor);
  2157.     }
  2158. }
  2159.  
  2160. int
  2161. ProcUninstallColormap(client)
  2162.     register ClientPtr client;
  2163. {
  2164.     ColormapPtr pcmp;
  2165.     REQUEST(xResourceReq);
  2166.  
  2167.     REQUEST_SIZE_MATCH(xResourceReq);
  2168.     pcmp = (ColormapPtr )LookupIDByType(stuff->id, RT_COLORMAP);
  2169.     if (pcmp)
  2170.     {
  2171.     if(pcmp->mid != pcmp->pScreen->defColormap)
  2172.             (*(pcmp->pScreen->UninstallColormap)) (pcmp);
  2173.         return (client->noClientException);        
  2174.     }
  2175.     else
  2176.     {
  2177.         client->errorValue = stuff->id;
  2178.         return (BadColor);
  2179.     }
  2180. }
  2181.  
  2182. int
  2183. ProcListInstalledColormaps(client)
  2184.     register ClientPtr client;
  2185. {
  2186.     xListInstalledColormapsReply *preply; 
  2187.     int nummaps;
  2188.     WindowPtr pWin;
  2189.     REQUEST(xResourceReq);
  2190.  
  2191.     REQUEST_SIZE_MATCH(xResourceReq);
  2192.     pWin = (WindowPtr)LookupWindow(stuff->id, client);
  2193.  
  2194.     if (!pWin)
  2195.         return(BadWindow);
  2196.  
  2197.     preply = (xListInstalledColormapsReply *) 
  2198.         ALLOCATE_LOCAL(sizeof(xListInstalledColormapsReply) +
  2199.              pWin->drawable.pScreen->maxInstalledCmaps *
  2200.              sizeof(Colormap));
  2201.     if(!preply)
  2202.         return(BadAlloc);
  2203.  
  2204.     preply->type = X_Reply;
  2205.     preply->sequenceNumber = client->sequence;
  2206.     nummaps = (*pWin->drawable.pScreen->ListInstalledColormaps)
  2207.         (pWin->drawable.pScreen, (Colormap *)&preply[1]);
  2208.     preply->nColormaps = nummaps;
  2209.     preply->length = nummaps;
  2210.     WriteReplyToClient(client, sizeof (xListInstalledColormapsReply), preply);
  2211.     client->pSwapReplyFunc = Swap32Write;
  2212.     WriteSwappedDataToClient(client, nummaps * sizeof(Colormap), &preply[1]);
  2213.     DEALLOCATE_LOCAL(preply);
  2214.     return(client->noClientException);
  2215. }
  2216.  
  2217. int
  2218. ProcAllocColor                (client)
  2219.     register ClientPtr client;
  2220. {
  2221.     ColormapPtr pmap;
  2222.     int    retval;
  2223.     xAllocColorReply acr;
  2224.     REQUEST(xAllocColorReq);
  2225.  
  2226.     REQUEST_SIZE_MATCH(xAllocColorReq);
  2227.     pmap = (ColormapPtr )LookupIDByType(stuff->cmap, RT_COLORMAP);
  2228.     if (pmap)
  2229.     {
  2230.     acr.type = X_Reply;
  2231.     acr.length = 0;
  2232.     acr.sequenceNumber = client->sequence;
  2233.     acr.red = stuff->red;
  2234.     acr.green = stuff->green;
  2235.     acr.blue = stuff->blue;
  2236.     acr.pixel = 0;
  2237.     if(retval = AllocColor(pmap, &acr.red, &acr.green, &acr.blue,
  2238.                            &acr.pixel, client->index))
  2239.     {
  2240.             if (client->noClientException != Success)
  2241.                 return(client->noClientException);
  2242.         else
  2243.             return (retval);
  2244.     }
  2245.         WriteReplyToClient(client, sizeof(xAllocColorReply), &acr);
  2246.     return (client->noClientException);
  2247.  
  2248.     }
  2249.     else
  2250.     {
  2251.         client->errorValue = stuff->cmap;
  2252.         return (BadColor);
  2253.     }
  2254. }
  2255.  
  2256. int
  2257. ProcAllocNamedColor           (client)
  2258.     register ClientPtr client;
  2259. {
  2260.     ColormapPtr pcmp;
  2261.     REQUEST(xAllocNamedColorReq);
  2262.  
  2263.     REQUEST_FIXED_SIZE(xAllocNamedColorReq, stuff->nbytes);
  2264.     pcmp = (ColormapPtr )LookupIDByType(stuff->cmap, RT_COLORMAP);
  2265.     if (pcmp)
  2266.     {
  2267.     int        retval;
  2268.  
  2269.     xAllocNamedColorReply ancr;
  2270.  
  2271.     ancr.type = X_Reply;
  2272.     ancr.length = 0;
  2273.     ancr.sequenceNumber = client->sequence;
  2274.  
  2275.     if(OsLookupColor(pcmp->pScreen->myNum, (char *)&stuff[1], stuff->nbytes,
  2276.                      &ancr.exactRed, &ancr.exactGreen, &ancr.exactBlue))
  2277.     {
  2278.         ancr.screenRed = ancr.exactRed;
  2279.         ancr.screenGreen = ancr.exactGreen;
  2280.         ancr.screenBlue = ancr.exactBlue;
  2281.         ancr.pixel = 0;
  2282.         if(retval = AllocColor(pcmp,
  2283.                      &ancr.screenRed, &ancr.screenGreen, &ancr.screenBlue,
  2284.              &ancr.pixel, client->index))
  2285.         {
  2286.                 if (client->noClientException != Success)
  2287.                     return(client->noClientException);
  2288.                 else
  2289.                     return(retval);
  2290.         }
  2291.             WriteReplyToClient(client, sizeof (xAllocNamedColorReply), &ancr);
  2292.         return (client->noClientException);
  2293.     }
  2294.     else
  2295.         return(BadName);
  2296.     
  2297.     }
  2298.     else
  2299.     {
  2300.         client->errorValue = stuff->cmap;
  2301.         return (BadColor);
  2302.     }
  2303. }
  2304.  
  2305. int
  2306. ProcAllocColorCells           (client)
  2307.     register ClientPtr client;
  2308. {
  2309.     ColormapPtr pcmp;
  2310.     REQUEST(xAllocColorCellsReq);
  2311.  
  2312.     REQUEST_SIZE_MATCH(xAllocColorCellsReq);
  2313.     pcmp = (ColormapPtr )LookupIDByType(stuff->cmap, RT_COLORMAP);
  2314.     if (pcmp)
  2315.     {
  2316.     xAllocColorCellsReply    accr;
  2317.     int            npixels, nmasks, retval;
  2318.     long            length;
  2319.     unsigned long        *ppixels, *pmasks;
  2320.  
  2321.     npixels = stuff->colors;
  2322.     if (!npixels)
  2323.     {
  2324.         client->errorValue = npixels;
  2325.         return (BadValue);
  2326.     }
  2327.     if (stuff->contiguous != xTrue && stuff->contiguous != xFalse)
  2328.     {
  2329.         client->errorValue = stuff->contiguous;
  2330.         return (BadValue);
  2331.     }
  2332.     nmasks = stuff->planes;
  2333.     length = ((long)npixels + (long)nmasks) * sizeof(Pixel);
  2334.     ppixels = (Pixel *)ALLOCATE_LOCAL(length);
  2335.     if(!ppixels)
  2336.             return(BadAlloc);
  2337.     pmasks = ppixels + npixels;
  2338.  
  2339.     if(retval = AllocColorCells(client->index, pcmp, npixels, nmasks, 
  2340.                     (Bool)stuff->contiguous, ppixels, pmasks))
  2341.     {
  2342.         DEALLOCATE_LOCAL(ppixels);
  2343.             if (client->noClientException != Success)
  2344.                 return(client->noClientException);
  2345.         else
  2346.             return(retval);
  2347.     }
  2348.     accr.type = X_Reply;
  2349.     accr.length = length >> 2;
  2350.     accr.sequenceNumber = client->sequence;
  2351.     accr.nPixels = npixels;
  2352.     accr.nMasks = nmasks;
  2353.         WriteReplyToClient(client, sizeof (xAllocColorCellsReply), &accr);
  2354.     client->pSwapReplyFunc = Swap32Write;
  2355.     WriteSwappedDataToClient(client, length, ppixels);
  2356.     DEALLOCATE_LOCAL(ppixels);
  2357.         return (client->noClientException);        
  2358.     }
  2359.     else
  2360.     {
  2361.         client->errorValue = stuff->cmap;
  2362.         return (BadColor);
  2363.     }
  2364. }
  2365.  
  2366. int
  2367. ProcAllocColorPlanes(client)
  2368.     register ClientPtr client;
  2369. {
  2370.     ColormapPtr pcmp;
  2371.     REQUEST(xAllocColorPlanesReq);
  2372.  
  2373.     REQUEST_SIZE_MATCH(xAllocColorPlanesReq);
  2374.     pcmp = (ColormapPtr )LookupIDByType(stuff->cmap, RT_COLORMAP);
  2375.     if (pcmp)
  2376.     {
  2377.     xAllocColorPlanesReply    acpr;
  2378.     int            npixels, retval;
  2379.     long            length;
  2380.     unsigned long        *ppixels;
  2381.  
  2382.     npixels = stuff->colors;
  2383.     if (!npixels)
  2384.     {
  2385.         client->errorValue = npixels;
  2386.         return (BadValue);
  2387.     }
  2388.     if (stuff->contiguous != xTrue && stuff->contiguous != xFalse)
  2389.     {
  2390.         client->errorValue = stuff->contiguous;
  2391.         return (BadValue);
  2392.     }
  2393.     acpr.type = X_Reply;
  2394.     acpr.sequenceNumber = client->sequence;
  2395.     acpr.nPixels = npixels;
  2396.     length = (long)npixels * sizeof(Pixel);
  2397.     ppixels = (Pixel *)ALLOCATE_LOCAL(length);
  2398.     if(!ppixels)
  2399.             return(BadAlloc);
  2400.     if(retval = AllocColorPlanes(client->index, pcmp, npixels,
  2401.         (int)stuff->red, (int)stuff->green, (int)stuff->blue,
  2402.         (int)stuff->contiguous, ppixels,
  2403.         &acpr.redMask, &acpr.greenMask, &acpr.blueMask))
  2404.     {
  2405.             DEALLOCATE_LOCAL(ppixels);
  2406.             if (client->noClientException != Success)
  2407.                 return(client->noClientException);
  2408.         else
  2409.             return(retval);
  2410.     }
  2411.     acpr.length = length >> 2;
  2412.     WriteReplyToClient(client, sizeof(xAllocColorPlanesReply), &acpr);
  2413.     client->pSwapReplyFunc = Swap32Write;
  2414.     WriteSwappedDataToClient(client, length, ppixels);
  2415.     DEALLOCATE_LOCAL(ppixels);
  2416.         return (client->noClientException);        
  2417.     }
  2418.     else
  2419.     {
  2420.         client->errorValue = stuff->cmap;
  2421.         return (BadColor);
  2422.     }
  2423. }
  2424.  
  2425. int
  2426. ProcFreeColors          (client)
  2427.     register ClientPtr client;
  2428. {
  2429.     ColormapPtr pcmp;
  2430.     REQUEST(xFreeColorsReq);
  2431.  
  2432.     REQUEST_AT_LEAST_SIZE(xFreeColorsReq);
  2433.     pcmp = (ColormapPtr )LookupIDByType(stuff->cmap, RT_COLORMAP);
  2434.     if (pcmp)
  2435.     {
  2436.     int    count;
  2437.         int     retval;
  2438.  
  2439.     if(pcmp->flags & AllAllocated)
  2440.         return(BadAccess);
  2441.     count = ((stuff->length << 2)- sizeof(xFreeColorsReq)) >> 2;
  2442.     retval =  FreeColors(pcmp, client->index, count,
  2443.         (unsigned long *)&stuff[1], stuff->planeMask);
  2444.         if (client->noClientException != Success)
  2445.             return(client->noClientException);
  2446.         else
  2447.     {
  2448.         client->errorValue = clientErrorValue;
  2449.             return(retval);
  2450.     }
  2451.  
  2452.     }
  2453.     else
  2454.     {
  2455.         client->errorValue = stuff->cmap;
  2456.         return (BadColor);
  2457.     }
  2458. }
  2459.  
  2460. int
  2461. ProcStoreColors               (client)
  2462.     register ClientPtr client;
  2463. {
  2464.     ColormapPtr pcmp;
  2465.     REQUEST(xStoreColorsReq);
  2466.  
  2467.     REQUEST_AT_LEAST_SIZE(xStoreColorsReq);
  2468.     pcmp = (ColormapPtr )LookupIDByType(stuff->cmap, RT_COLORMAP);
  2469.     if (pcmp)
  2470.     {
  2471.     int    count;
  2472.         int     retval;
  2473.  
  2474.         count = (stuff->length << 2) - sizeof(xStoreColorsReq);
  2475.     if (count % sizeof(xColorItem))
  2476.         return(BadLength);
  2477.     count /= sizeof(xColorItem);
  2478.     retval = StoreColors(pcmp, count, (xColorItem *)&stuff[1]);
  2479.         if (client->noClientException != Success)
  2480.             return(client->noClientException);
  2481.         else
  2482.     {
  2483.         client->errorValue = clientErrorValue;
  2484.             return(retval);
  2485.     }
  2486.     }
  2487.     else
  2488.     {
  2489.         client->errorValue = stuff->cmap;
  2490.         return (BadColor);
  2491.     }
  2492. }
  2493.  
  2494. int
  2495. ProcStoreNamedColor           (client)
  2496.     register ClientPtr client;
  2497. {
  2498.     ColormapPtr pcmp;
  2499.     REQUEST(xStoreNamedColorReq);
  2500.  
  2501.     REQUEST_FIXED_SIZE(xStoreNamedColorReq, stuff->nbytes);
  2502.     pcmp = (ColormapPtr )LookupIDByType(stuff->cmap, RT_COLORMAP);
  2503.     if (pcmp)
  2504.     {
  2505.     xColorItem    def;
  2506.         int             retval;
  2507.  
  2508.     if(OsLookupColor(pcmp->pScreen->myNum, (char *)&stuff[1],
  2509.                      stuff->nbytes, &def.red, &def.green, &def.blue))
  2510.     {
  2511.         def.flags = stuff->flags;
  2512.         def.pixel = stuff->pixel;
  2513.         retval = StoreColors(pcmp, 1, &def);
  2514.             if (client->noClientException != Success)
  2515.                 return(client->noClientException);
  2516.         else
  2517.         return(retval);
  2518.     }
  2519.         return (BadName);        
  2520.     }
  2521.     else
  2522.     {
  2523.         client->errorValue = stuff->cmap;
  2524.         return (BadColor);
  2525.     }
  2526. }
  2527.  
  2528. int
  2529. ProcQueryColors(client)
  2530.     register ClientPtr client;
  2531. {
  2532.     ColormapPtr pcmp;
  2533.     REQUEST(xQueryColorsReq);
  2534.  
  2535.     REQUEST_AT_LEAST_SIZE(xQueryColorsReq);
  2536.     pcmp = (ColormapPtr )LookupIDByType(stuff->cmap, RT_COLORMAP);
  2537.     if (pcmp)
  2538.     {
  2539.     int            count, retval;
  2540.     xrgb             *prgbs;
  2541.     xQueryColorsReply    qcr;
  2542.  
  2543.     count = ((stuff->length << 2) - sizeof(xQueryColorsReq)) >> 2;
  2544.     prgbs = (xrgb *)ALLOCATE_LOCAL(count * sizeof(xrgb));
  2545.     if(!prgbs && count)
  2546.             return(BadAlloc);
  2547.     if(retval = QueryColors(pcmp, count, (unsigned long *)&stuff[1], prgbs))
  2548.     {
  2549.            if (prgbs) DEALLOCATE_LOCAL(prgbs);
  2550.         if (client->noClientException != Success)
  2551.                 return(client->noClientException);
  2552.         else
  2553.         {
  2554.         client->errorValue = clientErrorValue;
  2555.             return (retval);
  2556.         }
  2557.     }
  2558.     qcr.type = X_Reply;
  2559.     qcr.length = (count * sizeof(xrgb)) >> 2;
  2560.     qcr.sequenceNumber = client->sequence;
  2561.     qcr.nColors = count;
  2562.     WriteReplyToClient(client, sizeof(xQueryColorsReply), &qcr);
  2563.     if (count)
  2564.     {
  2565.         client->pSwapReplyFunc = SQColorsExtend;
  2566.         WriteSwappedDataToClient(client, count * sizeof(xrgb), prgbs);
  2567.     }
  2568.     if (prgbs) DEALLOCATE_LOCAL(prgbs);
  2569.     return(client->noClientException);
  2570.     
  2571.     }
  2572.     else
  2573.     {
  2574.         client->errorValue = stuff->cmap;
  2575.         return (BadColor);
  2576.     }
  2577.  
  2578. int
  2579. ProcLookupColor(client)
  2580.     register ClientPtr client;
  2581. {
  2582.     ColormapPtr pcmp;
  2583.     REQUEST(xLookupColorReq);
  2584.  
  2585.     REQUEST_FIXED_SIZE(xLookupColorReq, stuff->nbytes);
  2586.     pcmp = (ColormapPtr )LookupIDByType(stuff->cmap, RT_COLORMAP);
  2587.     if (pcmp)
  2588.     {
  2589.     xLookupColorReply lcr;
  2590.  
  2591.     if(OsLookupColor(pcmp->pScreen->myNum, (char *)&stuff[1], stuff->nbytes,
  2592.                      &lcr.exactRed, &lcr.exactGreen, &lcr.exactBlue))
  2593.     {
  2594.         lcr.type = X_Reply;
  2595.         lcr.length = 0;
  2596.         lcr.sequenceNumber = client->sequence;
  2597.         lcr.screenRed = lcr.exactRed;
  2598.         lcr.screenGreen = lcr.exactGreen;
  2599.         lcr.screenBlue = lcr.exactBlue;
  2600.         (*pcmp->pScreen->ResolveColor)(&lcr.screenRed,
  2601.                                        &lcr.screenGreen,
  2602.                        &lcr.screenBlue,
  2603.                        pcmp->pVisual);
  2604.         WriteReplyToClient(client, sizeof(xLookupColorReply), &lcr);
  2605.         return(client->noClientException);
  2606.     }
  2607.         return (BadName);        
  2608.     }
  2609.     else
  2610.     {
  2611.         client->errorValue = stuff->cmap;
  2612.         return (BadColor);
  2613.     }
  2614. }
  2615.  
  2616. int
  2617. ProcCreateCursor( client)
  2618.     register ClientPtr client;
  2619. {
  2620.     CursorPtr    pCursor;
  2621.  
  2622.     register PixmapPtr     src;
  2623.     register PixmapPtr     msk;
  2624.     unsigned char *    srcbits;
  2625.     unsigned char *    mskbits;
  2626.     unsigned short    width, height;
  2627.     long        n;
  2628.     CursorMetricRec cm;
  2629.  
  2630.  
  2631.     REQUEST(xCreateCursorReq);
  2632.  
  2633.     REQUEST_SIZE_MATCH(xCreateCursorReq);
  2634.     LEGAL_NEW_RESOURCE(stuff->cid, client);
  2635.  
  2636.     src = (PixmapPtr)LookupIDByType(stuff->source, RT_PIXMAP);
  2637.     msk = (PixmapPtr)LookupIDByType(stuff->mask, RT_PIXMAP);
  2638.     if (   src == (PixmapPtr)NULL)
  2639.     {
  2640.     client->errorValue = stuff->source;
  2641.     return (BadPixmap);
  2642.     }
  2643.     if ( msk == (PixmapPtr)NULL)
  2644.     {
  2645.     if (stuff->mask != None)
  2646.     {
  2647.         client->errorValue = stuff->mask;
  2648.         return (BadPixmap);
  2649.     }
  2650.     }
  2651.     else if (  src->drawable.width != msk->drawable.width
  2652.         || src->drawable.height != msk->drawable.height
  2653.         || src->drawable.depth != 1
  2654.         || msk->drawable.depth != 1)
  2655.     return (BadMatch);
  2656.  
  2657.     width = src->drawable.width;
  2658.     height = src->drawable.height;
  2659.  
  2660.     if ( stuff->x > width 
  2661.       || stuff->y > height )
  2662.     return (BadMatch);
  2663.  
  2664.     n = PixmapBytePad(width, 1)*height;
  2665.     srcbits = (unsigned char *)xalloc(n);
  2666.     if (!srcbits)
  2667.     return (BadAlloc);
  2668.     mskbits = (unsigned char *)xalloc(n);
  2669.     if (!mskbits)
  2670.     {
  2671.     xfree(srcbits);
  2672.     return (BadAlloc);
  2673.     }
  2674.  
  2675.     /* zeroing the (pad) bits helps some ddx cursor handling */
  2676.     bzero((char *)srcbits, n);
  2677.     (* src->drawable.pScreen->GetImage)( src, 0, 0, width, height,
  2678.                      XYPixmap, 1, srcbits);
  2679.     if ( msk == (PixmapPtr)NULL)
  2680.     {
  2681.     register unsigned char *bits = mskbits;
  2682.     while (--n >= 0)
  2683.         *bits++ = ~0;
  2684.     }
  2685.     else
  2686.     {
  2687.     /* zeroing the (pad) bits helps some ddx cursor handling */
  2688.     bzero((char *)mskbits, n);
  2689.     (* msk->drawable.pScreen->GetImage)( msk, 0, 0, width, height,
  2690.                          XYPixmap, 1, mskbits);
  2691.     }
  2692.     cm.width = width;
  2693.     cm.height = height;
  2694.     cm.xhot = stuff->x;
  2695.     cm.yhot = stuff->y;
  2696.     pCursor = AllocCursor( srcbits, mskbits, &cm,
  2697.         stuff->foreRed, stuff->foreGreen, stuff->foreBlue,
  2698.         stuff->backRed, stuff->backGreen, stuff->backBlue);
  2699.  
  2700.     if (pCursor && AddResource(stuff->cid, RT_CURSOR, (pointer)pCursor))
  2701.         return (client->noClientException);
  2702.     return BadAlloc;
  2703. }
  2704.  
  2705. int
  2706. ProcCreateGlyphCursor( client)
  2707.     register ClientPtr client;
  2708. {
  2709.     CursorPtr pCursor;
  2710.     int res;
  2711.  
  2712.     REQUEST(xCreateGlyphCursorReq);
  2713.  
  2714.     REQUEST_SIZE_MATCH(xCreateGlyphCursorReq);
  2715.     LEGAL_NEW_RESOURCE(stuff->cid, client);
  2716.  
  2717.     res = AllocGlyphCursor(stuff->source, stuff->sourceChar,
  2718.                stuff->mask, stuff->maskChar,
  2719.                stuff->foreRed, stuff->foreGreen, stuff->foreBlue,
  2720.                stuff->backRed, stuff->backGreen, stuff->backBlue,
  2721.                &pCursor, client);
  2722.     if (res != Success)
  2723.     return res;
  2724.     if (AddResource(stuff->cid, RT_CURSOR, (pointer)pCursor))
  2725.     return client->noClientException;
  2726.     return BadAlloc;
  2727. }
  2728.  
  2729.  
  2730. int
  2731. ProcFreeCursor(client)
  2732.     register ClientPtr client;
  2733. {
  2734.     CursorPtr pCursor;
  2735.     REQUEST(xResourceReq);
  2736.  
  2737.     REQUEST_SIZE_MATCH(xResourceReq);
  2738.     pCursor = (CursorPtr)LookupIDByType(stuff->id, RT_CURSOR);
  2739.     if (pCursor) 
  2740.     {
  2741.     FreeResource(stuff->id, RT_NONE);
  2742.     return (client->noClientException);
  2743.     }
  2744.     else 
  2745.     {
  2746.     client->errorValue = stuff->id;
  2747.     return (BadCursor);
  2748.     }
  2749. }
  2750.  
  2751. int
  2752. ProcQueryBestSize   (client)
  2753.     register ClientPtr client;
  2754. {
  2755.     xQueryBestSizeReply    reply;
  2756.     register DrawablePtr pDraw;
  2757.     ScreenPtr pScreen;
  2758.     REQUEST(xQueryBestSizeReq);
  2759.  
  2760.     REQUEST_SIZE_MATCH(xQueryBestSizeReq);
  2761.     if ((stuff->class != CursorShape) && 
  2762.     (stuff->class != TileShape) && 
  2763.     (stuff->class != StippleShape))
  2764.     {
  2765.     client->errorValue = stuff->class;
  2766.         return(BadValue);
  2767.     }
  2768.     if (!(pDraw = LOOKUP_DRAWABLE(stuff->drawable, client)))
  2769.     {
  2770.         if (!(pDraw = (DrawablePtr)LookupWindow(stuff->drawable, client))) 
  2771.     {
  2772.         client->errorValue = stuff->drawable;
  2773.         return (BadDrawable);
  2774.     }
  2775.     if (stuff->class != CursorShape)
  2776.         return (BadMatch);
  2777.     }
  2778.     pScreen = pDraw->pScreen;
  2779.     (* pScreen->QueryBestSize)(stuff->class, &stuff->width,
  2780.                    &stuff->height, pScreen);
  2781.     reply.type = X_Reply;
  2782.     reply.length = 0;
  2783.     reply.sequenceNumber = client->sequence;
  2784.     reply.width = stuff->width;
  2785.     reply.height = stuff->height;
  2786.     WriteReplyToClient(client, sizeof(xQueryBestSizeReply), &reply);
  2787.     return (client->noClientException);
  2788. }
  2789.  
  2790.  
  2791. int
  2792. ProcSetScreenSaver            (client)
  2793.     register ClientPtr client;
  2794. {
  2795.     int blankingOption, exposureOption;
  2796.     REQUEST(xSetScreenSaverReq);
  2797.  
  2798.     REQUEST_SIZE_MATCH(xSetScreenSaverReq);
  2799.     blankingOption = stuff->preferBlank;
  2800.     if ((blankingOption != DontPreferBlanking) &&
  2801.         (blankingOption != PreferBlanking) &&
  2802.         (blankingOption != DefaultBlanking))
  2803.     {
  2804.     client->errorValue = blankingOption;
  2805.         return BadValue;
  2806.     }
  2807.     exposureOption = stuff->allowExpose;
  2808.     if ((exposureOption != DontAllowExposures) &&
  2809.         (exposureOption != AllowExposures) &&
  2810.         (exposureOption != DefaultExposures))
  2811.     {
  2812.     client->errorValue = exposureOption;
  2813.         return BadValue;
  2814.     }
  2815.     if (stuff->timeout < -1)
  2816.     {
  2817.     client->errorValue = stuff->timeout;
  2818.         return BadValue;
  2819.     }
  2820.     if (stuff->interval < -1)
  2821.     {
  2822.     client->errorValue = stuff->interval;
  2823.         return BadValue;
  2824.     }
  2825.  
  2826.     if (blankingOption == DefaultBlanking)
  2827.     ScreenSaverBlanking = defaultScreenSaverBlanking;
  2828.     else
  2829.     ScreenSaverBlanking = blankingOption; 
  2830.     if (exposureOption == DefaultExposures)
  2831.     ScreenSaverAllowExposures = defaultScreenSaverAllowExposures;
  2832.     else
  2833.     ScreenSaverAllowExposures = exposureOption;
  2834.  
  2835.     if (stuff->timeout >= 0)
  2836.     ScreenSaverTime = stuff->timeout * MILLI_PER_SECOND;
  2837.     else 
  2838.     ScreenSaverTime = defaultScreenSaverTime;
  2839.     if (stuff->interval >= 0)
  2840.     ScreenSaverInterval = stuff->interval * MILLI_PER_SECOND;
  2841.     else
  2842.     ScreenSaverInterval = defaultScreenSaverInterval;
  2843.     return (client->noClientException);
  2844. }
  2845.  
  2846. int
  2847. ProcGetScreenSaver(client)
  2848.     register ClientPtr client;
  2849. {
  2850.     xGetScreenSaverReply rep;
  2851.  
  2852.     rep.type = X_Reply;
  2853.     rep.length = 0;
  2854.     rep.sequenceNumber = client->sequence;
  2855.     rep.timeout = ScreenSaverTime / MILLI_PER_SECOND;
  2856.     rep.interval = ScreenSaverInterval / MILLI_PER_SECOND;
  2857.     rep.preferBlanking = ScreenSaverBlanking;
  2858.     rep.allowExposures = ScreenSaverAllowExposures;
  2859.     WriteReplyToClient(client, sizeof(xGetScreenSaverReply), &rep);
  2860.     return (client->noClientException);
  2861. }
  2862.  
  2863. int
  2864. ProcChangeHosts(client)
  2865.     register ClientPtr client;
  2866. {
  2867.     REQUEST(xChangeHostsReq);
  2868.     int result;
  2869.  
  2870.     REQUEST_FIXED_SIZE(xChangeHostsReq, stuff->hostLength);
  2871.  
  2872.     if(stuff->mode == HostInsert)
  2873.     result = AddHost(client, (int)stuff->hostFamily,
  2874.              stuff->hostLength, (pointer)&stuff[1]);
  2875.     else if (stuff->mode == HostDelete)
  2876.     result = RemoveHost(client, (int)stuff->hostFamily, 
  2877.                 stuff->hostLength, (pointer)&stuff[1]);  
  2878.     else
  2879.     {
  2880.     client->errorValue = stuff->mode;
  2881.         return BadValue;
  2882.     }
  2883.     if (!result)
  2884.     result = client->noClientException;
  2885.     return (result);
  2886. }
  2887.  
  2888. int
  2889. ProcListHosts(client)
  2890.     register ClientPtr client;
  2891. {
  2892. extern int GetHosts();
  2893.     xListHostsReply reply;
  2894.     int    len, nHosts, result;
  2895.     pointer    pdata;
  2896.     REQUEST(xListHostsReq);
  2897.  
  2898.     REQUEST_SIZE_MATCH(xListHostsReq);
  2899.     result = GetHosts(&pdata, &nHosts, &len, &reply.enabled);
  2900.     if (result != Success)
  2901.     return(result);
  2902.     reply.type = X_Reply;
  2903.     reply.sequenceNumber = client->sequence;
  2904.     reply.nHosts = nHosts;
  2905.     reply.length = len >> 2;
  2906.     WriteReplyToClient(client, sizeof(xListHostsReply), &reply);
  2907.     if (nHosts)
  2908.     {
  2909.     client->pSwapReplyFunc = SLHostsExtend;
  2910.     WriteSwappedDataToClient(client, len, pdata);
  2911.     }
  2912.     xfree(pdata);
  2913.     return (client->noClientException);
  2914. }
  2915.  
  2916. int
  2917. ProcChangeAccessControl(client)
  2918.     register ClientPtr client;
  2919. {
  2920.     int result;
  2921.     REQUEST(xSetAccessControlReq);
  2922.  
  2923.     REQUEST_SIZE_MATCH(xSetAccessControlReq);
  2924.     if ((stuff->mode != EnableAccess) && (stuff->mode != DisableAccess))
  2925.     {
  2926.     client->errorValue = stuff->mode;
  2927.         return BadValue;
  2928.     }
  2929.     result = ChangeAccessControl(client, stuff->mode == EnableAccess);
  2930.     if (!result)
  2931.     result = client->noClientException;
  2932.     return (result);
  2933. }
  2934.  
  2935. int
  2936. ProcKillClient(client)
  2937.     register ClientPtr client;
  2938. {
  2939.     REQUEST(xResourceReq);
  2940.  
  2941.     pointer *pResource;
  2942.     int clientIndex, myIndex;
  2943.  
  2944.     REQUEST_SIZE_MATCH(xResourceReq);
  2945.     if (stuff->id == AllTemporary)
  2946.     {
  2947.     CloseDownRetainedResources();
  2948.         return (client->noClientException);
  2949.     }
  2950.     pResource = (pointer *)LookupIDByClass(stuff->id, RC_ANY);
  2951.   
  2952.     clientIndex = CLIENT_ID(stuff->id);
  2953.  
  2954.     if (clientIndex && pResource && clients[clientIndex] &&
  2955.     !(stuff->id & SERVER_BIT) &&
  2956.     (clients[clientIndex]->requestVector != InitialVector))
  2957.     {
  2958.     myIndex = client->index;
  2959.     CloseDownClient(clients[clientIndex]);
  2960.     if (myIndex == clientIndex)
  2961.     {
  2962.         /* force yield and return Success, so that Dispatch()
  2963.          * doesn't try to touch client
  2964.          */
  2965.         isItTimeToYield = TRUE;
  2966.         return (Success);
  2967.     }
  2968.     return (client->noClientException);
  2969.     }
  2970.     else
  2971.     {
  2972.     client->errorValue = stuff->id;
  2973.     return (BadValue);
  2974.     }
  2975. }
  2976.  
  2977. int
  2978. ProcSetFontPath(client)
  2979.     register ClientPtr client;
  2980. {
  2981.     unsigned char *ptr;
  2982.     unsigned long nbytes, total;
  2983.     long nfonts;
  2984.     int n, result;
  2985.     int error;
  2986.     REQUEST(xSetFontPathReq);
  2987.     
  2988.     REQUEST_AT_LEAST_SIZE(xSetFontPathReq);
  2989.     
  2990.     nbytes = (stuff->length << 2) - sizeof(xSetFontPathReq);
  2991.     total = nbytes;
  2992.     ptr = (unsigned char *)&stuff[1];
  2993.     nfonts = stuff->nFonts;
  2994.     while (--nfonts >= 0)
  2995.     {
  2996.     if ((total == 0) || (total < (n = (*ptr + 1))))
  2997.         return(BadLength);
  2998.     total -= n;
  2999.     ptr += n;
  3000.     }
  3001.     if (total >= 4)
  3002.     return(BadLength);
  3003.     result = SetFontPath(client, stuff->nFonts, (char *)&stuff[1], &error);
  3004.     if (error != -1)
  3005.     client->errorValue = error;
  3006.     if (!result)
  3007.     result = client->noClientException;
  3008.     return (result);
  3009. }
  3010.  
  3011. int
  3012. ProcGetFontPath(client)
  3013.     register ClientPtr client;
  3014. {
  3015.     xGetFontPathReply reply;
  3016.     int stringLens, numpaths;
  3017.     unsigned char *bufferStart;
  3018.     REQUEST (xReq);
  3019.  
  3020.     REQUEST_SIZE_MATCH(xReq);
  3021.     bufferStart = GetFontPath(&numpaths, &stringLens);
  3022.  
  3023.     reply.type = X_Reply;
  3024.     reply.sequenceNumber = client->sequence;
  3025.     reply.length = (stringLens + numpaths + 3) >> 2;
  3026.     reply.nPaths = numpaths;
  3027.  
  3028.     WriteReplyToClient(client, sizeof(xGetFontPathReply), &reply);
  3029.     if (stringLens || numpaths)
  3030.     (void)WriteToClient(client, stringLens + numpaths, bufferStart);
  3031.     return(client->noClientException);
  3032. }
  3033.  
  3034. int
  3035. ProcChangeCloseDownMode(client)
  3036.     register ClientPtr client;
  3037. {
  3038.     REQUEST(xSetCloseDownModeReq);
  3039.  
  3040.     REQUEST_SIZE_MATCH(xSetCloseDownModeReq);
  3041.     if ((stuff->mode == AllTemporary) ||
  3042.     (stuff->mode == RetainPermanent) ||
  3043.     (stuff->mode == RetainTemporary))
  3044.     {
  3045.     client->closeDownMode = stuff->mode;
  3046.     return (client->noClientException);
  3047.     }
  3048.     else   
  3049.     {
  3050.     client->errorValue = stuff->mode;
  3051.     return (BadValue);
  3052.     }
  3053. }
  3054.  
  3055. int ProcForceScreenSaver(client)
  3056.     register ClientPtr client;
  3057. {    
  3058.     REQUEST(xForceScreenSaverReq);
  3059.  
  3060.     REQUEST_SIZE_MATCH(xForceScreenSaverReq);
  3061.     
  3062.     if ((stuff->mode != ScreenSaverReset) && 
  3063.     (stuff->mode != ScreenSaverActive))
  3064.     {
  3065.     client->errorValue = stuff->mode;
  3066.         return BadValue;
  3067.     }
  3068.     SaveScreens(SCREEN_SAVER_FORCER, (int)stuff->mode);
  3069.     return client->noClientException;
  3070. }
  3071.  
  3072. int ProcNoOperation(client)
  3073.     register ClientPtr client;
  3074. {
  3075.     REQUEST(xReq);
  3076.  
  3077.     REQUEST_AT_LEAST_SIZE(xReq);
  3078.     
  3079.     /* noop -- don't do anything */
  3080.     return(client->noClientException);
  3081. }
  3082.  
  3083. void
  3084. InitProcVectors()
  3085. {
  3086.     int i;
  3087.     for (i = 0; i<256; i++)
  3088.     {
  3089.     if(!ProcVector[i])
  3090.     {
  3091.             ProcVector[i] = SwappedProcVector[i] = ProcBadRequest;
  3092.         ReplySwapVector[i] = NotImplemented;
  3093.     }
  3094.     }
  3095.     for(i = LASTEvent; i < 128; i++)
  3096.     {
  3097.     EventSwapVector[i] = NotImplemented;
  3098.     }
  3099.     
  3100. }
  3101.  
  3102. /**********************
  3103.  * CloseDownClient
  3104.  *
  3105.  *  Client can either mark his resources destroy or retain.  If retained and
  3106.  *  then killed again, the client is really destroyed.
  3107.  *********************/
  3108.  
  3109. Bool terminateAtReset = FALSE;
  3110.  
  3111. void
  3112. CloseDownClient(client)
  3113.     register ClientPtr client;
  3114. {
  3115.     if (!client->clientGone)
  3116.     {
  3117.     /* ungrab server if grabbing client dies */
  3118.     if (grabbingClient &&  (onlyClient == client))
  3119.     {
  3120.         grabbingClient = FALSE;
  3121.         ListenToAllClients();
  3122.     }
  3123.     DeleteClientFromAnySelections(client);
  3124.     ReleaseActiveGrabs(client);
  3125.     DeleteClientFontStuff(client);
  3126.     
  3127.     if (client->closeDownMode == DestroyAll)
  3128.     {
  3129.         client->clientGone = TRUE;  /* so events aren't sent to client */
  3130.         CloseDownConnection(client);
  3131.         FreeClientResources(client);
  3132.         if (ClientIsAsleep (client))
  3133.         ClientSignal (client);
  3134.         if (client->index < nextFreeClientID)
  3135.         nextFreeClientID = client->index;
  3136.         clients[client->index] = NullClient;
  3137.         if ((client->requestVector != InitialVector) &&
  3138.         (--nClients == 0))
  3139.         {
  3140.         if (terminateAtReset)
  3141.             dispatchException |= DE_TERMINATE;
  3142.         else
  3143.             dispatchException |= DE_RESET;
  3144.         }
  3145.         xfree(client);
  3146.     }
  3147.     else
  3148.     {
  3149.         client->clientGone = TRUE;
  3150.         CloseDownConnection(client);
  3151.         --nClients;
  3152.     }
  3153.     }
  3154.     else
  3155.     {
  3156.     /* really kill resources this time */
  3157.         FreeClientResources(client);
  3158.     if (ClientIsAsleep (client))
  3159.         ClientSignal (client);
  3160.     if (client->index < nextFreeClientID)
  3161.         nextFreeClientID = client->index;
  3162.     clients[client->index] = NullClient;
  3163.         xfree(client);
  3164.     }
  3165.  
  3166.     while (!clients[currentMaxClients-1])
  3167.       currentMaxClients--;
  3168. }
  3169.  
  3170. static void
  3171. KillAllClients()
  3172. {
  3173.     int i;
  3174.     for (i=1; i<currentMaxClients; i++)
  3175.         if (clients[i])
  3176.             CloseDownClient(clients[i]);     
  3177. }
  3178.  
  3179. /*********************
  3180.  * CloseDownRetainedResources
  3181.  *
  3182.  *    Find all clients that are gone and have terminated in RetainTemporary 
  3183.  *    and  destroy their resources.
  3184.  *********************/
  3185.  
  3186. CloseDownRetainedResources()
  3187. {
  3188.     register int i;
  3189.     register ClientPtr client;
  3190.  
  3191.     for (i=1; i<currentMaxClients; i++)
  3192.     {
  3193.         client = clients[i];
  3194.         if (client && (client->closeDownMode == RetainTemporary)
  3195.         && (client->clientGone))
  3196.         CloseDownClient(client);
  3197.     }
  3198. }
  3199.  
  3200. /************************
  3201.  * int NextAvailableClient(ospriv)
  3202.  *
  3203.  * OS dependent portion can't assign client id's because of CloseDownModes.
  3204.  * Returns NULL if there are no free clients.
  3205.  *************************/
  3206.  
  3207. ClientPtr
  3208. NextAvailableClient(ospriv)
  3209.     pointer ospriv;
  3210. {
  3211.     register int i;
  3212.     register ClientPtr client;
  3213.     xReq data;
  3214.  
  3215.     i = nextFreeClientID;
  3216.     if (i == MAXCLIENTS)
  3217.     return (ClientPtr)NULL;
  3218.     clients[i] = client = (ClientPtr)xalloc(sizeof(ClientRec));
  3219.     if (!client)
  3220.     return (ClientPtr)NULL;
  3221.     client->index = i;
  3222.     client->sequence = 0; 
  3223.     client->clientAsMask = ((Mask)i) << CLIENTOFFSET;
  3224.     client->closeDownMode = DestroyAll;
  3225.     client->clientGone = FALSE;
  3226.     client->lastDrawable = (DrawablePtr) NULL;
  3227.     client->lastDrawableID = INVALID;
  3228.     client->lastGC = (GCPtr) NULL;
  3229.     client->lastGCID = INVALID;
  3230.     client->numSaved = 0;
  3231.     client->saveSet = (pointer *)NULL;
  3232.     client->noClientException = Success;
  3233. #ifdef DEBUG
  3234.     client->requestLogIndex = 0;
  3235. #endif
  3236.     client->requestVector = InitialVector;
  3237.     client->osPrivate = ospriv;
  3238.     client->swapped = FALSE;
  3239.     if (!InitClientResources(client))
  3240.     {
  3241.     xfree(client);
  3242.     return (ClientPtr)NULL;
  3243.     }
  3244.     data.reqType = 1;
  3245.     data.length = (sz_xReq + sz_xConnClientPrefix) >> 2;
  3246.     if (!InsertFakeRequest(client, (char *)&data, sz_xReq))
  3247.     {
  3248.     FreeClientResources(client);
  3249.     xfree(client);
  3250.     return (ClientPtr)NULL;
  3251.     }
  3252.     if (i == currentMaxClients)
  3253.     currentMaxClients++;
  3254.     while ((nextFreeClientID < MAXCLIENTS) && clients[nextFreeClientID])
  3255.     nextFreeClientID++;
  3256.     return(client);
  3257. }
  3258.  
  3259. int
  3260. ProcInitialConnection(client)
  3261.     register ClientPtr client;
  3262. {
  3263.     REQUEST(xReq);
  3264.     register xConnClientPrefix *prefix;
  3265.     int whichbyte = 1;
  3266.  
  3267.     prefix = (xConnClientPrefix *)((char *)stuff + sz_xReq);
  3268.     if ((prefix->byteOrder != 'l') && (prefix->byteOrder != 'B'))
  3269.     return (client->noClientException = -1);
  3270.     if (((*(char *) &whichbyte) && (prefix->byteOrder == 'B')) ||
  3271.     (!(*(char *) &whichbyte) && (prefix->byteOrder == 'l')))
  3272.     {
  3273.     client->swapped = TRUE;
  3274.     SwapConnClientPrefix(prefix);
  3275.     }
  3276.     stuff->reqType = 2;
  3277.     stuff->length += ((prefix->nbytesAuthProto + 3) >> 2) +
  3278.              ((prefix->nbytesAuthString + 3) >> 2);
  3279.     if (client->swapped)
  3280.     {
  3281.     swaps(&stuff->length, whichbyte);
  3282.     }
  3283.     ResetCurrentRequest(client);
  3284.     return (client->noClientException);
  3285. }
  3286.  
  3287. int
  3288. ProcEstablishConnection(client)
  3289.     register ClientPtr client;
  3290. {
  3291.     char *reason, *auth_proto, *auth_string;
  3292.     register xConnClientPrefix *prefix;
  3293.     register xWindowRoot *root;
  3294.     register int i;
  3295.     REQUEST(xReq);
  3296.  
  3297.     prefix = (xConnClientPrefix *)((char *)stuff + sz_xReq);
  3298.     auth_proto = (char *)prefix + sz_xConnClientPrefix;
  3299.     auth_string = auth_proto + ((prefix->nbytesAuthProto + 3) & ~3);
  3300.     if ((prefix->majorVersion != X_PROTOCOL) ||
  3301.     (prefix->minorVersion != X_PROTOCOL_REVISION))
  3302.     reason = "Protocol version mismatch";
  3303.     else
  3304.     reason = ClientAuthorized(client,
  3305.                   (unsigned short)prefix->nbytesAuthProto,
  3306.                   auth_proto,
  3307.                   (unsigned short)prefix->nbytesAuthString,
  3308.                   auth_string);
  3309.     if (reason)
  3310.     {
  3311.     xConnSetupPrefix csp;
  3312.     char pad[3];
  3313.  
  3314.     csp.success = xFalse;
  3315.     csp.lengthReason = strlen(reason);
  3316.     csp.length = (csp.lengthReason + 3) >> 2;
  3317.     csp.majorVersion = X_PROTOCOL;
  3318.     csp.minorVersion = X_PROTOCOL_REVISION;
  3319.     if (client->swapped)
  3320.         WriteSConnSetupPrefix(client, &csp);
  3321.     else
  3322.         (void)WriteToClient(client, sz_xConnSetupPrefix, (char *) &csp);
  3323.         (void)WriteToClient(client, (int)csp.lengthReason, reason);
  3324.     if (csp.lengthReason & 3)
  3325.         (void)WriteToClient(client, (int)(4 - (csp.lengthReason & 3)),
  3326.                 pad);
  3327.     return (client->noClientException = -1);
  3328.     }
  3329.  
  3330.     nClients++;
  3331.     client->requestVector = client->swapped ? SwappedProcVector : ProcVector;
  3332.     client->sequence = 0;
  3333.     ((xConnSetup *)ConnectionInfo)->ridBase = client->clientAsMask;
  3334.     ((xConnSetup *)ConnectionInfo)->ridMask = RESOURCE_ID_MASK;
  3335.     /* fill in the "currentInputMask" */
  3336.     root = (xWindowRoot *)(ConnectionInfo + connBlockScreenStart);
  3337.     for (i=0; i<screenInfo.numScreens; i++) 
  3338.     {
  3339.     register int j;
  3340.     register xDepth *pDepth;
  3341.  
  3342.         root->currentInputMask = WindowTable[i]->eventMask |
  3343.                      wOtherEventMasks (WindowTable[i]);
  3344.     pDepth = (xDepth *)(root + 1);
  3345.     for (j = 0; j < root->nDepths; j++)
  3346.     {
  3347.         pDepth = (xDepth *)(((char *)(pDepth + 1)) +
  3348.                 pDepth->nVisuals * sizeof(xVisualType));
  3349.     }
  3350.     root = (xWindowRoot *)pDepth;
  3351.     }
  3352.  
  3353.     if (client->swapped)
  3354.     {
  3355.     WriteSConnSetupPrefix(client, &connSetupPrefix);
  3356.     WriteSConnectionInfo(client,
  3357.                  (unsigned long)(connSetupPrefix.length << 2),
  3358.                  ConnectionInfo);
  3359.     }
  3360.     else
  3361.     {
  3362.     (void)WriteToClient(client, sizeof(xConnSetupPrefix),
  3363.                 (char *) &connSetupPrefix);
  3364.     (void)WriteToClient(client, (int)(connSetupPrefix.length << 2),
  3365.                 ConnectionInfo);
  3366.     }
  3367.     return (client->noClientException);
  3368. }
  3369.  
  3370. SendErrorToClient(client, majorCode, minorCode, resId, errorCode)
  3371.     ClientPtr client;
  3372.     unsigned majorCode;
  3373.     unsigned short minorCode;
  3374.     XID resId;
  3375.     int errorCode;
  3376. {
  3377.     xError rep;
  3378.  
  3379.     rep.type = X_Error;
  3380.     rep.sequenceNumber = client->sequence;
  3381.     rep.errorCode = errorCode;
  3382.     rep.majorCode = majorCode;
  3383.     rep.minorCode = minorCode;
  3384.     rep.resourceID = resId;
  3385.  
  3386.     WriteEventsToClient (client, 1, (xEvent *)&rep);
  3387. }
  3388.  
  3389. void
  3390. DeleteWindowFromAnySelections(pWin)
  3391.     WindowPtr pWin;
  3392. {
  3393.     register int i;
  3394.  
  3395.     for (i = 0; i< NumCurrentSelections; i++)
  3396.         if (CurrentSelections[i].pWin == pWin)
  3397.         {
  3398.             CurrentSelections[i].pWin = (WindowPtr)NULL;
  3399.             CurrentSelections[i].window = None;
  3400.         CurrentSelections[i].client = NullClient;
  3401.     }
  3402. }
  3403.  
  3404. static void
  3405. DeleteClientFromAnySelections(client)
  3406.     ClientPtr client;
  3407. {
  3408.     register int i;
  3409.  
  3410.     for (i = 0; i< NumCurrentSelections; i++)
  3411.         if (CurrentSelections[i].client == client)
  3412.         {
  3413.             CurrentSelections[i].pWin = (WindowPtr)NULL;
  3414.             CurrentSelections[i].window = None;
  3415.         CurrentSelections[i].client = NullClient;
  3416.     }
  3417. }
  3418.  
  3419. void
  3420. MarkClientException(client)
  3421.     ClientPtr client;
  3422. {
  3423.     client->noClientException = -1;
  3424. }
  3425.