home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / server / dix / dispatch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-21  |  85.7 KB  |  3,428 lines

  1. /* $XConsortium: dispatch.c,v 5.36 91/10/30 14:51:40 rws 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 = WindowTable[0]->drawable.id;
  172.         client->lastDrawable = (DrawablePtr)WindowTable[0];
  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.     REQUEST(xReq);
  934.     REQUEST_SIZE_MATCH(xReq);
  935.     OnlyListenToOneClient(client);
  936.     grabbingClient = TRUE;
  937.     onlyClient = client;
  938.     return(client->noClientException);
  939. }
  940.  
  941. int
  942. ProcUngrabServer(client)
  943.     register ClientPtr client;
  944. {
  945.     REQUEST(xReq);
  946.     REQUEST_SIZE_MATCH(xReq);
  947.     grabbingClient = FALSE;
  948.     ListenToAllClients();
  949.     return(client->noClientException);
  950. }
  951.  
  952. int
  953. ProcTranslateCoords(client)
  954.     register ClientPtr client;
  955. {
  956.     REQUEST(xTranslateCoordsReq);
  957.  
  958.     register WindowPtr pWin, pDst;
  959.     xTranslateCoordsReply rep;
  960.  
  961.     REQUEST_SIZE_MATCH(xTranslateCoordsReq);
  962.     pWin = (WindowPtr)LookupWindow(stuff->srcWid, client);
  963.     if (!pWin)
  964.         return(BadWindow);
  965.     pDst = (WindowPtr)LookupWindow(stuff->dstWid, client);
  966.     if (!pDst)
  967.         return(BadWindow);
  968.     rep.type = X_Reply;
  969.     rep.length = 0;
  970.     rep.sequenceNumber = client->sequence;
  971.     if (!SAME_SCREENS(pWin->drawable, pDst->drawable))
  972.     {
  973.     rep.sameScreen = xFalse;
  974.         rep.child = None;
  975.     rep.dstX = rep.dstY = 0;
  976.     }
  977.     else
  978.     {
  979.     INT16 x, y;
  980.     rep.sameScreen = xTrue;
  981.     rep.child = None;
  982.     /* computing absolute coordinates -- adjust to destination later */
  983.     x = pWin->drawable.x + stuff->srcX;
  984.     y = pWin->drawable.y + stuff->srcY;
  985.     pWin = pDst->firstChild;
  986.     while (pWin)
  987.     {
  988. #ifdef SHAPE
  989.         BoxRec  box;
  990. #endif
  991.         if ((pWin->mapped) &&
  992.         (x >= pWin->drawable.x - wBorderWidth (pWin)) &&
  993.         (x < pWin->drawable.x + (int)pWin->drawable.width +
  994.          wBorderWidth (pWin)) &&
  995.         (y >= pWin->drawable.y - wBorderWidth (pWin)) &&
  996.         (y < pWin->drawable.y + (int)pWin->drawable.height +
  997.          wBorderWidth (pWin))
  998. #ifdef SHAPE
  999.         /* When a window is shaped, a further check
  1000.          * is made to see if the point is inside
  1001.          * borderSize
  1002.          */
  1003.         && (!wBoundingShape(pWin) ||
  1004.             (*pWin->drawable.pScreen->PointInRegion)
  1005.                 (&pWin->borderSize, x, y, &box))
  1006. #endif
  1007.         )
  1008.             {
  1009.         rep.child = pWin->drawable.id;
  1010.         pWin = (WindowPtr) NULL;
  1011.         }
  1012.         else
  1013.         pWin = pWin->nextSib;
  1014.     }
  1015.     /* adjust to destination coordinates */
  1016.     rep.dstX = x - pDst->drawable.x;
  1017.     rep.dstY = y - pDst->drawable.y;
  1018.     }
  1019.     WriteReplyToClient(client, sizeof(xTranslateCoordsReply), &rep);
  1020.     return(client->noClientException);
  1021. }
  1022.  
  1023. int
  1024. ProcOpenFont(client)
  1025.     register ClientPtr client;
  1026. {
  1027.     int    err;
  1028.     REQUEST(xOpenFontReq);
  1029.  
  1030.     REQUEST_FIXED_SIZE(xOpenFontReq, stuff->nbytes);
  1031.     client->errorValue = stuff->fid;
  1032.     LEGAL_NEW_RESOURCE(stuff->fid, client);
  1033.     err = OpenFont(client, stuff->fid, (Mask) 0,
  1034.         stuff->nbytes, (char *)&stuff[1]);
  1035.     if (err == Success)
  1036.     {
  1037.     return(client->noClientException);
  1038.     }
  1039.     else
  1040.     return err;
  1041. }
  1042.  
  1043. int
  1044. ProcCloseFont(client)
  1045.     register ClientPtr client;
  1046. {
  1047.     FontPtr pFont;
  1048.     REQUEST(xResourceReq);
  1049.  
  1050.     REQUEST_SIZE_MATCH(xResourceReq);
  1051.     pFont = (FontPtr)LookupIDByType(stuff->id, RT_FONT);
  1052.     if ( pFont != (FontPtr)NULL)    /* id was valid */
  1053.     {
  1054.         FreeResource(stuff->id, RT_NONE);
  1055.     return(client->noClientException);
  1056.     }
  1057.     else
  1058.     {
  1059.     client->errorValue = stuff->id;
  1060.         return (BadFont);
  1061.     }
  1062. }
  1063.  
  1064. int
  1065. ProcQueryFont(client)
  1066.     register ClientPtr client;
  1067. {
  1068.     xQueryFontReply    *reply;
  1069.     FontPtr pFont;
  1070.     register GC *pGC;
  1071.     REQUEST(xResourceReq);
  1072.  
  1073.     REQUEST_SIZE_MATCH(xResourceReq);
  1074.     client->errorValue = stuff->id;        /* EITHER font or gc */
  1075.     pFont = (FontPtr)LookupIDByType(stuff->id, RT_FONT);
  1076.     if (!pFont)
  1077.     {
  1078.       /* can't use VERIFY_GC because it might return BadGC */
  1079.     pGC = (GC *) LookupIDByType(stuff->id, RT_GC);
  1080.         if (!pGC)
  1081.     {
  1082.         client->errorValue = stuff->id;
  1083.             return(BadFont);     /* procotol spec says only error is BadFont */
  1084.     }
  1085.     pFont = pGC->font;
  1086.     }
  1087.  
  1088.     {
  1089.     xCharInfo    *pmax = FONTINKMAX(pFont);
  1090.     xCharInfo    *pmin = FONTINKMIN(pFont);
  1091.     int        nprotoxcistructs;
  1092.     int        rlength;
  1093.  
  1094.     nprotoxcistructs = (
  1095.        pmax->rightSideBearing == pmin->rightSideBearing &&
  1096.        pmax->leftSideBearing == pmin->leftSideBearing &&
  1097.        pmax->descent == pmin->descent &&
  1098.        pmax->ascent == pmin->ascent &&
  1099.        pmax->characterWidth == pmin->characterWidth) ?
  1100.         0 : N2dChars(pFont);
  1101.  
  1102.     rlength = sizeof(xQueryFontReply) +
  1103.                  FONTINFONPROPS(FONTCHARSET(pFont)) * sizeof(xFontProp)  +
  1104.              nprotoxcistructs * sizeof(xCharInfo);
  1105.     reply = (xQueryFontReply *)ALLOCATE_LOCAL(rlength);
  1106.     if(!reply)
  1107.     {
  1108.         return(BadAlloc);
  1109.     }
  1110.  
  1111.     reply->type = X_Reply;
  1112.     reply->length = (rlength - sizeof(xGenericReply)) >> 2;
  1113.     reply->sequenceNumber = client->sequence;
  1114.     QueryFont( pFont, reply, nprotoxcistructs);
  1115.  
  1116.         WriteReplyToClient(client, rlength, reply);
  1117.     DEALLOCATE_LOCAL(reply);
  1118.     return(client->noClientException);
  1119.     }
  1120. }
  1121.  
  1122. int
  1123. ProcQueryTextExtents(client)
  1124.     register ClientPtr client;
  1125. {
  1126.     REQUEST(xQueryTextExtentsReq);
  1127.     xQueryTextExtentsReply reply;
  1128.     FontPtr pFont;
  1129.     GC *pGC;
  1130.     ExtentInfoRec info;
  1131.     unsigned long length;
  1132.  
  1133.     REQUEST_AT_LEAST_SIZE(xQueryTextExtentsReq);
  1134.         
  1135.     pFont = (FontPtr)LookupIDByType(stuff->fid, RT_FONT);
  1136.     if (!pFont)
  1137.     {
  1138.         pGC = (GC *)LookupIDByType(stuff->fid, RT_GC);
  1139.         if (!pGC)
  1140.     {
  1141.         client->errorValue = stuff->fid;
  1142.             return(BadFont);
  1143.     }
  1144.     pFont = pGC->font;
  1145.     }
  1146.     length = stuff->length - (sizeof(xQueryTextExtentsReq) >> 2);
  1147.     length = length << 1;
  1148.     if (stuff->oddLength)
  1149.     {
  1150.     if (length == 0)
  1151.         return(BadLength);
  1152.         length--;
  1153.     }
  1154.     if (!QueryTextExtents(pFont, length, (unsigned char *)&stuff[1], &info))
  1155.     return(BadAlloc);
  1156.     reply.type = X_Reply;
  1157.     reply.length = 0;
  1158.     reply.sequenceNumber = client->sequence;
  1159.     reply.drawDirection = info.drawDirection;
  1160.     reply.fontAscent = info.fontAscent;
  1161.     reply.fontDescent = info.fontDescent;
  1162.     reply.overallAscent = info.overallAscent;
  1163.     reply.overallDescent = info.overallDescent;
  1164.     reply.overallWidth = info.overallWidth;
  1165.     reply.overallLeft = info.overallLeft;
  1166.     reply.overallRight = info.overallRight;
  1167.     WriteReplyToClient(client, sizeof(xQueryTextExtentsReply), &reply);
  1168.     return(client->noClientException);
  1169. }
  1170.  
  1171. int
  1172. ProcListFonts(client)
  1173.     register ClientPtr client;
  1174. {
  1175.     REQUEST(xListFontsReq);
  1176.  
  1177.     REQUEST_FIXED_SIZE(xListFontsReq, stuff->nbytes);
  1178.  
  1179.     return ListFonts(client, (unsigned char *) &stuff[1], stuff->nbytes, 
  1180.     stuff->maxNames);
  1181. }
  1182.  
  1183. int
  1184. ProcListFontsWithInfo(client)
  1185.     register ClientPtr client;
  1186. {
  1187.     REQUEST(xListFontsWithInfoReq);
  1188.  
  1189.     REQUEST_FIXED_SIZE(xListFontsWithInfoReq, stuff->nbytes);
  1190.  
  1191.     return StartListFontsWithInfo(client, stuff->nbytes,
  1192.                     (char *) &stuff[1], stuff->maxNames);
  1193. }
  1194.  
  1195. /*ARGSUSED*/
  1196. dixDestroyPixmap(pPixmap, pid)
  1197.     PixmapPtr pPixmap;
  1198.     Pixmap pid;
  1199. {
  1200.     (*pPixmap->drawable.pScreen->DestroyPixmap)(pPixmap);
  1201. }
  1202.  
  1203. int
  1204. ProcCreatePixmap(client)
  1205.     register ClientPtr client;
  1206. {
  1207.     PixmapPtr pMap;
  1208.     register DrawablePtr pDraw;
  1209.     REQUEST(xCreatePixmapReq);
  1210.     DepthPtr pDepth;
  1211.     register int i;
  1212.  
  1213.     REQUEST_SIZE_MATCH(xCreatePixmapReq);
  1214.     client->errorValue = stuff->pid;
  1215.     LEGAL_NEW_RESOURCE(stuff->pid, client);
  1216.     if (!(pDraw = LOOKUP_DRAWABLE(stuff->drawable, client)))
  1217.     {        /* can be inputonly */
  1218.         if (!(pDraw = (DrawablePtr)LookupWindow(stuff->drawable, client))) 
  1219.             return (BadDrawable);
  1220.     }
  1221.  
  1222.     if (!stuff->width || !stuff->height)
  1223.     {
  1224.     client->errorValue = 0;
  1225.         return BadValue;
  1226.     }
  1227.     if (stuff->depth != 1)
  1228.     {
  1229.         pDepth = pDraw->pScreen->allowedDepths;
  1230.         for (i=0; i<pDraw->pScreen->numDepths; i++, pDepth++)
  1231.        if (pDepth->depth == stuff->depth)
  1232.                goto CreatePmap;
  1233.     client->errorValue = stuff->depth;
  1234.         return BadValue;
  1235.     }
  1236. CreatePmap:
  1237.     pMap = (PixmapPtr)(*pDraw->pScreen->CreatePixmap)
  1238.         (pDraw->pScreen, stuff->width,
  1239.          stuff->height, stuff->depth);
  1240.     if (pMap)
  1241.     {
  1242.     pMap->drawable.serialNumber = NEXT_SERIAL_NUMBER;
  1243.     pMap->drawable.id = stuff->pid;
  1244.     if (AddResource(stuff->pid, RT_PIXMAP, (pointer)pMap))
  1245.         return(client->noClientException);
  1246.     }
  1247.     return (BadAlloc);
  1248. }
  1249.  
  1250. int
  1251. ProcFreePixmap(client)
  1252.     register ClientPtr client;
  1253. {
  1254.     PixmapPtr pMap;
  1255.  
  1256.     REQUEST(xResourceReq);
  1257.  
  1258.     REQUEST_SIZE_MATCH(xResourceReq);
  1259.     pMap = (PixmapPtr)LookupIDByType(stuff->id, RT_PIXMAP);
  1260.     if (pMap) 
  1261.     {
  1262.     FreeResource(stuff->id, RT_NONE);
  1263.     return(client->noClientException);
  1264.     }
  1265.     else 
  1266.     {
  1267.     client->errorValue = stuff->id;
  1268.     return (BadPixmap);
  1269.     }
  1270. }
  1271.  
  1272. int
  1273. ProcCreateGC(client)
  1274.     register ClientPtr client;
  1275. {
  1276.     int error;
  1277.     GC *pGC;
  1278.     register DrawablePtr pDraw;
  1279.     unsigned len;
  1280.     REQUEST(xCreateGCReq);
  1281.  
  1282.     REQUEST_AT_LEAST_SIZE(xCreateGCReq);
  1283.     client->errorValue = stuff->gc;
  1284.     LEGAL_NEW_RESOURCE(stuff->gc, client);
  1285.     if (!(pDraw = LOOKUP_DRAWABLE( stuff->drawable, client) ))
  1286.     {
  1287.     client->errorValue = stuff->drawable;
  1288.         return (BadDrawable);
  1289.     }
  1290.     len = stuff->length -  (sizeof(xCreateGCReq) >> 2);
  1291.     if (len != Ones(stuff->mask))
  1292.         return BadLength;
  1293.     pGC = (GC *)CreateGC(pDraw, stuff->mask, 
  1294.              (XID *) &stuff[1], &error);
  1295.     if (error != Success)
  1296.         return error;
  1297.     if (!AddResource(stuff->gc, RT_GC, (pointer)pGC))
  1298.     return (BadAlloc);
  1299.     return(client->noClientException);
  1300. }
  1301.  
  1302. int
  1303. ProcChangeGC(client)
  1304.     register ClientPtr client;
  1305. {
  1306.     GC *pGC;
  1307.     REQUEST(xChangeGCReq);
  1308.     int result;
  1309.     unsigned len;
  1310.         
  1311.     REQUEST_AT_LEAST_SIZE(xChangeGCReq);
  1312.     VERIFY_GC(pGC, stuff->gc, client);
  1313.     len = stuff->length -  (sizeof(xChangeGCReq) >> 2);
  1314.     if (len != Ones(stuff->mask))
  1315.         return BadLength;
  1316.     result = DoChangeGC(pGC, stuff->mask, (XID *) &stuff[1], 0);
  1317.     if (client->noClientException != Success)
  1318.         return(client->noClientException);
  1319.     else
  1320.     {
  1321.     client->errorValue = clientErrorValue;
  1322.         return(result);
  1323.     }
  1324. }
  1325.  
  1326. int
  1327. ProcCopyGC(client)
  1328.     register ClientPtr client;
  1329. {
  1330.     register GC *dstGC;
  1331.     register GC *pGC;
  1332.     int result;
  1333.     REQUEST(xCopyGCReq);
  1334.  
  1335.     REQUEST_SIZE_MATCH(xCopyGCReq);
  1336.     VERIFY_GC( pGC, stuff->srcGC, client);
  1337.     VERIFY_GC( dstGC, stuff->dstGC, client);
  1338.     if ((dstGC->pScreen != pGC->pScreen) || (dstGC->depth != pGC->depth))
  1339.         return (BadMatch);    
  1340.     result = CopyGC(pGC, dstGC, stuff->mask);
  1341.     if (client->noClientException != Success)
  1342.         return(client->noClientException);
  1343.     else
  1344.     {
  1345.     client->errorValue = clientErrorValue;
  1346.         return(result);
  1347.     }
  1348. }
  1349.  
  1350. int
  1351. ProcSetDashes(client)
  1352.     register ClientPtr client;
  1353. {
  1354.     register GC *pGC;
  1355.     int result;
  1356.     REQUEST(xSetDashesReq);
  1357.  
  1358.     REQUEST_FIXED_SIZE(xSetDashesReq, stuff->nDashes);
  1359.     if (stuff->nDashes == 0)
  1360.     {
  1361.      client->errorValue = 0;
  1362.          return BadValue;
  1363.     }
  1364.  
  1365.     VERIFY_GC(pGC,stuff->gc, client);
  1366.  
  1367.     result = SetDashes(pGC, stuff->dashOffset, stuff->nDashes,
  1368.                (unsigned char *)&stuff[1]);
  1369.     if (client->noClientException != Success)
  1370.         return(client->noClientException);
  1371.     else
  1372.     {
  1373.     client->errorValue = clientErrorValue;
  1374.         return(result);
  1375.     }
  1376. }
  1377.  
  1378. int
  1379. ProcSetClipRectangles(client)
  1380.     register ClientPtr client;
  1381. {
  1382.     int    nr;
  1383.     int result;
  1384.     register GC *pGC;
  1385.     REQUEST(xSetClipRectanglesReq);
  1386.  
  1387.     REQUEST_AT_LEAST_SIZE(xSetClipRectanglesReq);
  1388.     if ((stuff->ordering != Unsorted) && (stuff->ordering != YSorted) &&
  1389.     (stuff->ordering != YXSorted) && (stuff->ordering != YXBanded))
  1390.     {
  1391.     client->errorValue = stuff->ordering;
  1392.         return BadValue;
  1393.     }
  1394.     VERIFY_GC(pGC,stuff->gc, client);
  1395.          
  1396.     nr = (stuff->length << 2) - sizeof(xSetClipRectanglesReq);
  1397.     if (nr & 4)
  1398.     return(BadLength);
  1399.     nr >>= 3;
  1400.     result = SetClipRects(pGC, stuff->xOrigin, stuff->yOrigin,
  1401.               nr, (xRectangle *)&stuff[1], (int)stuff->ordering);
  1402.     if (client->noClientException != Success)
  1403.         return(client->noClientException);
  1404.     else
  1405.         return(result);
  1406. }
  1407.  
  1408. int
  1409. ProcFreeGC(client)
  1410.     register ClientPtr client;
  1411. {
  1412.     register GC *pGC;
  1413.     REQUEST(xResourceReq);
  1414.  
  1415.     REQUEST_SIZE_MATCH(xResourceReq);
  1416.     VERIFY_GC(pGC,stuff->id,client);
  1417.     FreeResource(stuff->id, RT_NONE);
  1418.     return(client->noClientException);
  1419. }
  1420.  
  1421. int
  1422. ProcClearToBackground(client)
  1423.     register ClientPtr client;
  1424. {
  1425.     REQUEST(xClearAreaReq);
  1426.     register WindowPtr pWin;
  1427.  
  1428.     REQUEST_SIZE_MATCH(xClearAreaReq);
  1429.     pWin = (WindowPtr)LookupWindow( stuff->window, client);
  1430.     if (!pWin)
  1431.         return(BadWindow);
  1432.     if (pWin->drawable.class == InputOnly)
  1433.     {
  1434.     client->errorValue = stuff->window;
  1435.     return (BadMatch);
  1436.     }            
  1437.     if ((stuff->exposures != xTrue) && (stuff->exposures != xFalse))
  1438.     {
  1439.     client->errorValue = stuff->exposures;
  1440.         return(BadValue);
  1441.     }
  1442.     (*pWin->drawable.pScreen->ClearToBackground)(pWin, stuff->x, stuff->y,
  1443.                    stuff->width, stuff->height,
  1444.                    (Bool)stuff->exposures);
  1445.     return(client->noClientException);
  1446. }
  1447.  
  1448. int
  1449. ProcCopyArea(client)
  1450.     register ClientPtr client;
  1451. {
  1452.     register DrawablePtr pDst;
  1453.     register DrawablePtr pSrc;
  1454.     register GC *pGC;
  1455.     REQUEST(xCopyAreaReq);
  1456.     RegionPtr pRgn;
  1457.  
  1458.     REQUEST_SIZE_MATCH(xCopyAreaReq);
  1459.  
  1460.     VALIDATE_DRAWABLE_AND_GC(stuff->dstDrawable, pDst, pGC, client); 
  1461.     if (stuff->dstDrawable != stuff->srcDrawable)
  1462.     {
  1463.         if (!(pSrc = LOOKUP_DRAWABLE(stuff->srcDrawable, client)))
  1464.     {
  1465.         client->errorValue = stuff->srcDrawable;
  1466.             return(BadDrawable);
  1467.     }
  1468.     if ((pDst->pScreen != pSrc->pScreen) || (pDst->depth != pSrc->depth))
  1469.     {
  1470.         client->errorValue = stuff->dstDrawable;
  1471.         return (BadMatch);
  1472.     }
  1473.     }
  1474.     else
  1475.         pSrc = pDst;
  1476.     pRgn = (*pGC->ops->CopyArea)(pSrc, pDst, pGC, stuff->srcX, stuff->srcY,
  1477.                  stuff->width, stuff->height, 
  1478.                  stuff->dstX, stuff->dstY);
  1479.     if (pGC->graphicsExposures)
  1480.     {
  1481.     (*pDst->pScreen->SendGraphicsExpose)
  1482.          (client, pRgn, stuff->dstDrawable, X_CopyArea, 0);
  1483.     if (pRgn)
  1484.         (*pDst->pScreen->RegionDestroy) (pRgn);
  1485.     }
  1486.  
  1487.     return(client->noClientException);
  1488. }
  1489.  
  1490. int
  1491. ProcCopyPlane(client)
  1492.     register ClientPtr client;
  1493. {
  1494.     register DrawablePtr psrcDraw, pdstDraw;
  1495.     register GC *pGC;
  1496.     REQUEST(xCopyPlaneReq);
  1497.     RegionPtr pRgn;
  1498.  
  1499.     REQUEST_SIZE_MATCH(xCopyPlaneReq);
  1500.  
  1501.     VALIDATE_DRAWABLE_AND_GC(stuff->dstDrawable, pdstDraw, pGC, client);
  1502.     if (stuff->dstDrawable != stuff->srcDrawable)
  1503.     {
  1504.         if (!(psrcDraw = LOOKUP_DRAWABLE(stuff->srcDrawable, client)))
  1505.     {
  1506.         client->errorValue = stuff->srcDrawable;
  1507.             return(BadDrawable);
  1508.     }
  1509.     if (pdstDraw->pScreen != psrcDraw->pScreen)
  1510.     {
  1511.         client->errorValue = stuff->dstDrawable;
  1512.         return (BadMatch);
  1513.     }
  1514.     }
  1515.     else
  1516.         psrcDraw = pdstDraw;
  1517.  
  1518.     /* Check to see if stuff->bitPlane has exactly ONE good bit set */
  1519.     if(stuff->bitPlane == 0 || (stuff->bitPlane & (stuff->bitPlane - 1)) ||
  1520.        (stuff->bitPlane > (1L << (psrcDraw->depth - 1))))
  1521.     {
  1522.        client->errorValue = stuff->bitPlane;
  1523.        return(BadValue);
  1524.     }
  1525.  
  1526.     pRgn = (*pGC->ops->CopyPlane)(psrcDraw, pdstDraw, pGC, stuff->srcX, stuff->srcY,
  1527.                  stuff->width, stuff->height, 
  1528.                  stuff->dstX, stuff->dstY, stuff->bitPlane);
  1529.     if (pGC->graphicsExposures)
  1530.     {
  1531.     (*pdstDraw->pScreen->SendGraphicsExpose)
  1532.          (client, pRgn, stuff->dstDrawable, X_CopyPlane, 0);
  1533.     if (pRgn)
  1534.         (*pdstDraw->pScreen->RegionDestroy) (pRgn);
  1535.     }
  1536.     return(client->noClientException);
  1537. }
  1538.  
  1539. int
  1540. ProcPolyPoint(client)
  1541.     register ClientPtr client;
  1542. {
  1543.     int npoint;
  1544.     register GC *pGC;
  1545.     register DrawablePtr pDraw;
  1546.     REQUEST(xPolyPointReq);
  1547.  
  1548.     REQUEST_AT_LEAST_SIZE(xPolyPointReq);
  1549.     if ((stuff->coordMode != CoordModeOrigin) && 
  1550.     (stuff->coordMode != CoordModePrevious))
  1551.     {
  1552.     client->errorValue = stuff->coordMode;
  1553.         return BadValue;
  1554.     }
  1555.     VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client); 
  1556.     npoint = ((stuff->length << 2) - sizeof(xPolyPointReq)) >> 2;
  1557.     if (npoint)
  1558.         (*pGC->ops->PolyPoint)(pDraw, pGC, stuff->coordMode, npoint,
  1559.               (xPoint *) &stuff[1]);
  1560.     return (client->noClientException);
  1561. }
  1562.  
  1563. int
  1564. ProcPolyLine(client)
  1565.     register ClientPtr client;
  1566. {
  1567.     int npoint;
  1568.     register GC *pGC;
  1569.     register DrawablePtr pDraw;
  1570.     REQUEST(xPolyLineReq);
  1571.  
  1572.     REQUEST_AT_LEAST_SIZE(xPolyLineReq);
  1573.     if ((stuff->coordMode != CoordModeOrigin) && 
  1574.     (stuff->coordMode != CoordModePrevious))
  1575.     {
  1576.     client->errorValue = stuff->coordMode;
  1577.         return BadValue;
  1578.     }
  1579.     VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1580.     npoint = ((stuff->length << 2) - sizeof(xPolyLineReq)) >> 2;
  1581.     if (npoint)
  1582.     (*pGC->ops->Polylines)(pDraw, pGC, stuff->coordMode, npoint, 
  1583.                   (xPoint *) &stuff[1]);
  1584.     return(client->noClientException);
  1585. }
  1586.  
  1587. int
  1588. ProcPolySegment(client)
  1589.     register ClientPtr client;
  1590. {
  1591.     int nsegs;
  1592.     register GC *pGC;
  1593.     register DrawablePtr pDraw;
  1594.     REQUEST(xPolySegmentReq);
  1595.  
  1596.     REQUEST_AT_LEAST_SIZE(xPolySegmentReq);
  1597.     VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1598.     nsegs = (stuff->length << 2) - sizeof(xPolySegmentReq);
  1599.     if (nsegs & 4)
  1600.     return(BadLength);
  1601.     nsegs >>= 3;
  1602.     if (nsegs)
  1603.         (*pGC->ops->PolySegment)(pDraw, pGC, nsegs, (xSegment *) &stuff[1]);
  1604.     return (client->noClientException);
  1605. }
  1606.  
  1607. int
  1608. ProcPolyRectangle (client)
  1609.     register ClientPtr client;
  1610. {
  1611.     int nrects;
  1612.     register GC *pGC;
  1613.     register DrawablePtr pDraw;
  1614.     REQUEST(xPolyRectangleReq);
  1615.  
  1616.     REQUEST_AT_LEAST_SIZE(xPolyRectangleReq);
  1617.     VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1618.     nrects = (stuff->length << 2) - sizeof(xPolyRectangleReq);
  1619.     if (nrects & 4)
  1620.     return(BadLength);
  1621.     nrects >>= 3;
  1622.     if (nrects)
  1623.         (*pGC->ops->PolyRectangle)(pDraw, pGC, 
  1624.             nrects, (xRectangle *) &stuff[1]);
  1625.     return(client->noClientException);
  1626. }
  1627.  
  1628. int
  1629. ProcPolyArc(client)
  1630.     register ClientPtr client;
  1631. {
  1632.     int        narcs;
  1633.     register GC *pGC;
  1634.     register DrawablePtr pDraw;
  1635.     REQUEST(xPolyArcReq);
  1636.  
  1637.     REQUEST_AT_LEAST_SIZE(xPolyArcReq);
  1638.     VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1639.     narcs = (stuff->length << 2) - sizeof(xPolyArcReq);
  1640.     if (narcs % sizeof(xArc))
  1641.     return(BadLength);
  1642.     narcs /= sizeof(xArc);
  1643.     if (narcs)
  1644.         (*pGC->ops->PolyArc)(pDraw, pGC, narcs, (xArc *) &stuff[1]);
  1645.     return (client->noClientException);
  1646. }
  1647.  
  1648. int
  1649. ProcFillPoly(client)
  1650.     register ClientPtr client;
  1651. {
  1652.     int          things;
  1653.     register GC *pGC;
  1654.     register DrawablePtr pDraw;
  1655.     REQUEST(xFillPolyReq);
  1656.  
  1657.     REQUEST_AT_LEAST_SIZE(xFillPolyReq);
  1658.     if ((stuff->shape != Complex) && (stuff->shape != Nonconvex) &&  
  1659.     (stuff->shape != Convex))
  1660.     {
  1661.     client->errorValue = stuff->shape;
  1662.         return BadValue;
  1663.     }
  1664.     if ((stuff->coordMode != CoordModeOrigin) && 
  1665.     (stuff->coordMode != CoordModePrevious))
  1666.     {
  1667.     client->errorValue = stuff->coordMode;
  1668.         return BadValue;
  1669.     }
  1670.  
  1671.     VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1672.     things = ((stuff->length << 2) - sizeof(xFillPolyReq)) >> 2;
  1673.     if (things)
  1674.         (*pGC->ops->FillPolygon) (pDraw, pGC, stuff->shape,
  1675.              stuff->coordMode, things,
  1676.              (DDXPointPtr) &stuff[1]);
  1677.     return(client->noClientException);
  1678. }
  1679.  
  1680. int
  1681. ProcPolyFillRectangle(client)
  1682.     register ClientPtr client;
  1683. {
  1684.     int             things;
  1685.     register GC *pGC;
  1686.     register DrawablePtr pDraw;
  1687.     REQUEST(xPolyFillRectangleReq);
  1688.  
  1689.     REQUEST_AT_LEAST_SIZE(xPolyFillRectangleReq);
  1690.     VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1691.     things = (stuff->length << 2) - sizeof(xPolyFillRectangleReq);
  1692.     if (things & 4)
  1693.     return(BadLength);
  1694.     things >>= 3;
  1695.     if (things)
  1696.         (*pGC->ops->PolyFillRect) (pDraw, pGC, things,
  1697.               (xRectangle *) &stuff[1]);
  1698.     return (client->noClientException);
  1699. }
  1700.  
  1701. int
  1702. ProcPolyFillArc               (client)
  1703.     register ClientPtr client;
  1704. {
  1705.     int        narcs;
  1706.     register GC *pGC;
  1707.     register DrawablePtr pDraw;
  1708.     REQUEST(xPolyFillArcReq);
  1709.  
  1710.     REQUEST_AT_LEAST_SIZE(xPolyFillArcReq);
  1711.     VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1712.     narcs = (stuff->length << 2) - sizeof(xPolyFillArcReq);
  1713.     if (narcs % sizeof(xArc))
  1714.     return(BadLength);
  1715.     narcs /= sizeof(xArc);
  1716.     if (narcs)
  1717.         (*pGC->ops->PolyFillArc) (pDraw, pGC, narcs, (xArc *) &stuff[1]);
  1718.     return (client->noClientException);
  1719. }
  1720.  
  1721. int
  1722. ProcPutImage(client)
  1723.     register ClientPtr client;
  1724. {
  1725.     register GC *pGC;
  1726.     register DrawablePtr pDraw;
  1727.     long length;
  1728.     REQUEST(xPutImageReq);
  1729.  
  1730.     REQUEST_AT_LEAST_SIZE(xPutImageReq);
  1731.     VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1732.     if (stuff->format == XYBitmap)
  1733.     {
  1734.         if ((stuff->depth != 1) ||
  1735.         (stuff->leftPad >= screenInfo.bitmapScanlinePad))
  1736.             return BadMatch;
  1737.         length = PixmapBytePad(stuff->width + stuff->leftPad, 1);
  1738.     }
  1739.     else if (stuff->format == XYPixmap)
  1740.     {
  1741.         if ((pDraw->depth != stuff->depth) || 
  1742.         (stuff->leftPad >= screenInfo.bitmapScanlinePad))
  1743.             return BadMatch;
  1744.         length = PixmapBytePad(stuff->width + stuff->leftPad, 1);
  1745.     length *= stuff->depth;
  1746.     }
  1747.     else if (stuff->format == ZPixmap)
  1748.     {
  1749.         if ((pDraw->depth != stuff->depth) || (stuff->leftPad != 0))
  1750.             return BadMatch;
  1751.         length = PixmapBytePad(stuff->width, stuff->depth);
  1752.     }
  1753.     else
  1754.     {
  1755.     client->errorValue = stuff->format;
  1756.         return BadValue;
  1757.     }
  1758.     length *= stuff->height;
  1759.     if ((((length + 3) >> 2) + (sizeof(xPutImageReq) >> 2)) != stuff->length)
  1760.     return BadLength;
  1761.     (*pGC->ops->PutImage) (pDraw, pGC, stuff->depth, stuff->dstX, stuff->dstY,
  1762.           stuff->width, stuff->height, 
  1763.           stuff->leftPad, stuff->format, 
  1764.           (char *) &stuff[1]);
  1765.      return (client->noClientException);
  1766. }
  1767.  
  1768. int
  1769. ProcGetImage(client)
  1770.     register ClientPtr    client;
  1771. {
  1772.     register DrawablePtr pDraw;
  1773.     int            nlines, linesPerBuf;
  1774.     register int    height, linesDone;
  1775.     long        widthBytesLine, length;
  1776.     Mask        plane;
  1777.     char        *pBuf;
  1778.     xGetImageReply    xgi;
  1779.  
  1780.     REQUEST(xGetImageReq);
  1781.  
  1782.     height = stuff->height;
  1783.     REQUEST_SIZE_MATCH(xGetImageReq);
  1784.     if ((stuff->format != XYPixmap) && (stuff->format != ZPixmap))
  1785.     {
  1786.     client->errorValue = stuff->format;
  1787.         return(BadValue);
  1788.     }
  1789.     if(!(pDraw = LOOKUP_DRAWABLE(stuff->drawable, client) ))
  1790.     {
  1791.     client->errorValue = stuff->drawable;
  1792.     return (BadDrawable);
  1793.     }
  1794.     if(pDraw->type == DRAWABLE_WINDOW)
  1795.     {
  1796.       if( /* check for being viewable */
  1797.      !((WindowPtr) pDraw)->realized ||
  1798.       /* check for being on screen */
  1799.          pDraw->x + stuff->x < 0 ||
  1800.       pDraw->x + stuff->x + (int)stuff->width > pDraw->pScreen->width ||
  1801.          pDraw->y + stuff->y < 0 ||
  1802.          pDraw->y + stuff->y + height > pDraw->pScreen->height ||
  1803.           /* check for being inside of border */
  1804.          stuff->x < - wBorderWidth((WindowPtr)pDraw) ||
  1805.          stuff->x + (int)stuff->width >
  1806.         wBorderWidth((WindowPtr)pDraw) + (int)pDraw->width ||
  1807.          stuff->y < -wBorderWidth((WindowPtr)pDraw) ||
  1808.          stuff->y + height >
  1809.         wBorderWidth ((WindowPtr)pDraw) + (int)pDraw->height
  1810.         )
  1811.         return(BadMatch);
  1812.     xgi.visual = wVisual (((WindowPtr) pDraw));
  1813.     }
  1814.     else
  1815.     {
  1816.       if(stuff->x < 0 ||
  1817.          stuff->x+(int)stuff->width > pDraw->width ||
  1818.          stuff->y < 0 ||
  1819.          stuff->y+height > pDraw->height
  1820.         )
  1821.         return(BadMatch);
  1822.     xgi.visual = None;
  1823.     }
  1824.     xgi.type = X_Reply;
  1825.     xgi.sequenceNumber = client->sequence;
  1826.     xgi.depth = pDraw->depth;
  1827.     if(stuff->format == ZPixmap)
  1828.     {
  1829.     widthBytesLine = PixmapBytePad(stuff->width, pDraw->depth);
  1830.     length = widthBytesLine * height;
  1831.     }
  1832.     else 
  1833.     {
  1834.     widthBytesLine = PixmapBytePad(stuff->width, 1);
  1835.     plane = ((Mask)1) << (pDraw->depth - 1);
  1836.     /* only planes asked for */
  1837.     length = widthBytesLine * height *
  1838.          Ones(stuff->planeMask & (plane | (plane - 1)));
  1839.     }
  1840.     xgi.length = (length + 3) >> 2;
  1841.     if (widthBytesLine == 0 || height == 0)
  1842.     linesPerBuf = 0;
  1843.     else if (widthBytesLine >= IMAGE_BUFSIZE)
  1844.     linesPerBuf = 1;
  1845.     else
  1846.     {
  1847.     linesPerBuf = IMAGE_BUFSIZE / widthBytesLine;
  1848.     if (linesPerBuf > height)
  1849.         linesPerBuf = height;
  1850.     }
  1851.     length = linesPerBuf * widthBytesLine;
  1852.     if (linesPerBuf < height)
  1853.     {
  1854.     /* we have to make sure intermediate buffers don't need padding */
  1855.     while ((linesPerBuf > 1) && (length & 3))
  1856.     {
  1857.         linesPerBuf--;
  1858.         length -= widthBytesLine;
  1859.     }
  1860.     while (length & 3)
  1861.     {
  1862.         linesPerBuf++;
  1863.         length += widthBytesLine;
  1864.     }
  1865.     }
  1866.     if(!(pBuf = (char *) ALLOCATE_LOCAL(length)))
  1867.         return (BadAlloc);
  1868.  
  1869.     WriteReplyToClient(client, sizeof (xGetImageReply), &xgi);
  1870.  
  1871.     if (linesPerBuf == 0)
  1872.     {
  1873.     /* nothing to do */
  1874.     }
  1875.     else if (stuff->format == ZPixmap)
  1876.     {
  1877.         linesDone = 0;
  1878.         while (height - linesDone > 0)
  1879.         {
  1880.         nlines = min(linesPerBuf, height - linesDone);
  1881.         (*pDraw->pScreen->GetImage) (pDraw,
  1882.                                      stuff->x,
  1883.                          stuff->y + linesDone,
  1884.                          stuff->width, 
  1885.                          nlines,
  1886.                          stuff->format,
  1887.                          stuff->planeMask,
  1888.                          pBuf);
  1889.         /* Note that this is NOT a call to WriteSwappedDataToClient,
  1890.                as we do NOT byte swap */
  1891.         (void)WriteToClient(client, (int)(nlines * widthBytesLine), pBuf);
  1892.         linesDone += nlines;
  1893.         }
  1894.     }
  1895.     else
  1896.     {
  1897.         for (; plane; plane >>= 1)
  1898.     {
  1899.         if (stuff->planeMask & plane)
  1900.         {
  1901.             linesDone = 0;
  1902.             while (height - linesDone > 0)
  1903.             {
  1904.             nlines = min(linesPerBuf, height - linesDone);
  1905.                 (*pDraw->pScreen->GetImage) (pDraw,
  1906.                                              stuff->x,
  1907.                                  stuff->y + linesDone,
  1908.                                  stuff->width, 
  1909.                                  nlines,
  1910.                                  stuff->format,
  1911.                                  plane,
  1912.                                  pBuf);
  1913.             /* Note: NOT a call to WriteSwappedDataToClient,
  1914.                as we do NOT byte swap */
  1915.             (void)WriteToClient(client, (int)(nlines * widthBytesLine),
  1916.                     pBuf);
  1917.             linesDone += nlines;
  1918.         }
  1919.             }
  1920.     }
  1921.     }
  1922.     DEALLOCATE_LOCAL(pBuf);
  1923.     return (client->noClientException);
  1924. }
  1925.  
  1926.  
  1927. int
  1928. ProcPolyText(client)
  1929.     register ClientPtr client;
  1930. {
  1931.     int        xorg;
  1932.     REQUEST(xPolyTextReq);
  1933.     register DrawablePtr pDraw;
  1934.     register GC *pGC;
  1935.     register FontPtr pFont;
  1936.  
  1937.     int (* polyText)();
  1938.     register unsigned char *pElt;
  1939.     unsigned char *pNextElt;
  1940.     unsigned char *endReq;
  1941.     int        itemSize;
  1942.     
  1943. #define TextEltHeader 2
  1944. #define FontShiftSize 5
  1945.  
  1946.     REQUEST_AT_LEAST_SIZE(xPolyTextReq);
  1947.     VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  1948.  
  1949.     pElt = (unsigned char *)&stuff[1];
  1950.     endReq = ((unsigned char *) stuff) + (stuff->length <<2);
  1951.     xorg = stuff->x;
  1952.     if (stuff->reqType == X_PolyText8)
  1953.     {
  1954.     polyText = pGC->ops->PolyText8;
  1955.     itemSize = 1;
  1956.     }
  1957.     else
  1958.     {
  1959.     polyText =  pGC->ops->PolyText16;
  1960.     itemSize = 2;
  1961.     }
  1962.  
  1963.     while (endReq - pElt > TextEltHeader)
  1964.     {
  1965.     if (*pElt == FontChange)
  1966.         {
  1967.         Font    fid;
  1968.  
  1969.         if (endReq - pElt < FontShiftSize)
  1970.          return (BadLength);
  1971.         fid =  ((Font)*(pElt+4))        /* big-endian */
  1972.          | ((Font)*(pElt+3)) << 8
  1973.          | ((Font)*(pElt+2)) << 16
  1974.          | ((Font)*(pElt+1)) << 24;
  1975.         pFont = (FontPtr)LookupIDByType(fid, RT_FONT);
  1976.         if (!pFont)
  1977.         {
  1978.         client->errorValue = fid;
  1979.         return (BadFont);
  1980.         }
  1981.         if (pFont != pGC->font)
  1982.         {
  1983.         DoChangeGC( pGC, GCFont, &fid, 0);
  1984.         ValidateGC(pDraw, pGC);
  1985.         if (stuff->reqType == X_PolyText8)
  1986.             polyText = pGC->ops->PolyText8;
  1987.         else
  1988.             polyText = pGC->ops->PolyText16;
  1989.         }
  1990.         pElt += FontShiftSize;
  1991.     }
  1992.     else    /* print a string */
  1993.     {
  1994.         pNextElt = pElt + TextEltHeader + (*pElt)*itemSize;
  1995.         if ( pNextElt > endReq)
  1996.         return( BadLength);
  1997.         xorg += *((INT8 *)(pElt + 1));    /* must be signed */
  1998.         (void) LoadGlyphs(client, pGC->font, *pElt, itemSize,
  1999.         pElt + TextEltHeader);
  2000.         xorg = (* polyText)(pDraw, pGC, xorg, stuff->y, *pElt,
  2001.         pElt + TextEltHeader);
  2002.         pElt = pNextElt;
  2003.     }
  2004.     }
  2005.     return (client->noClientException);
  2006. #undef TextEltHeader
  2007. #undef FontShiftSize
  2008. }
  2009.  
  2010. int
  2011. ProcImageText8(client)
  2012.     register ClientPtr client;
  2013. {
  2014.     register DrawablePtr pDraw;
  2015.     register GC *pGC;
  2016.  
  2017.     REQUEST(xImageTextReq);
  2018.  
  2019.     REQUEST_FIXED_SIZE(xImageTextReq, stuff->nChars);
  2020.     VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  2021.  
  2022.     (void) LoadGlyphs(client, pGC->font, stuff->nChars, 1, &stuff[1]);
  2023.     (*pGC->ops->ImageText8)(pDraw, pGC, stuff->x, stuff->y,
  2024.                stuff->nChars, &stuff[1]);
  2025.     return (client->noClientException);
  2026. }
  2027.  
  2028. int
  2029. ProcImageText16(client)
  2030.     register ClientPtr client;
  2031. {
  2032.     register DrawablePtr pDraw;
  2033.     register GC *pGC;
  2034.  
  2035.     REQUEST(xImageTextReq);
  2036.  
  2037.     REQUEST_FIXED_SIZE(xImageTextReq, stuff->nChars << 1);
  2038.     VALIDATE_DRAWABLE_AND_GC(stuff->drawable, pDraw, pGC, client);
  2039.  
  2040.     (void) LoadGlyphs(client, pGC->font, stuff->nChars, 2, &stuff[1]);
  2041.     (*pGC->ops->ImageText16)(pDraw, pGC, stuff->x, stuff->y,
  2042.             stuff->nChars, &stuff[1]);
  2043.     return (client->noClientException);
  2044. }
  2045.  
  2046.  
  2047. int
  2048. ProcCreateColormap(client)
  2049.     register ClientPtr client;
  2050. {
  2051.     VisualPtr    pVisual;
  2052.     ColormapPtr    pmap;
  2053.     Colormap    mid;
  2054.     register WindowPtr   pWin;
  2055.     ScreenPtr pScreen;
  2056.     REQUEST(xCreateColormapReq);
  2057.     int i, result;
  2058.  
  2059.     REQUEST_SIZE_MATCH(xCreateColormapReq);
  2060.  
  2061.     if ((stuff->alloc != AllocNone) && (stuff->alloc != AllocAll))
  2062.     {
  2063.     client->errorValue = stuff->alloc;
  2064.         return(BadValue);
  2065.     }
  2066.     mid = stuff->mid;
  2067.     LEGAL_NEW_RESOURCE(mid, client);
  2068.     pWin = (WindowPtr)LookupWindow(stuff->window, client);
  2069.     if (!pWin)
  2070.         return(BadWindow);
  2071.  
  2072.     pScreen = pWin->drawable.pScreen;
  2073.     for (i = 0, pVisual = pScreen->visuals;
  2074.      i < pScreen->numVisuals;
  2075.      i++, pVisual++)
  2076.     {
  2077.     if (pVisual->vid != stuff->visual)
  2078.         continue;
  2079.     result =  CreateColormap(mid, pScreen, pVisual, &pmap,
  2080.                  (int)stuff->alloc, client->index);
  2081.     if (client->noClientException != Success)
  2082.         return(client->noClientException);
  2083.     else
  2084.         return(result);
  2085.     }
  2086.     client->errorValue = stuff->visual;
  2087.     return(BadValue);
  2088. }
  2089.  
  2090. int
  2091. ProcFreeColormap(client)
  2092.     register ClientPtr client;
  2093. {
  2094.     ColormapPtr pmap;
  2095.     REQUEST(xResourceReq);
  2096.  
  2097.     REQUEST_SIZE_MATCH(xResourceReq);
  2098.     pmap = (ColormapPtr )LookupIDByType(stuff->id, RT_COLORMAP);
  2099.     if (pmap) 
  2100.     {
  2101.     /* Freeing a default colormap is a no-op */
  2102.     if (!(pmap->flags & IsDefault))
  2103.         FreeResource(stuff->id, RT_NONE);
  2104.     return (client->noClientException);
  2105.     }
  2106.     else 
  2107.     {
  2108.     client->errorValue = stuff->id;
  2109.     return (BadColor);
  2110.     }
  2111. }
  2112.  
  2113.  
  2114. int
  2115. ProcCopyColormapAndFree(client)
  2116.     register ClientPtr client;
  2117. {
  2118.     Colormap    mid;
  2119.     ColormapPtr    pSrcMap;
  2120.     REQUEST(xCopyColormapAndFreeReq);
  2121.     int result;
  2122.  
  2123.     REQUEST_SIZE_MATCH(xCopyColormapAndFreeReq);
  2124.     mid = stuff->mid;
  2125.     LEGAL_NEW_RESOURCE(mid, client);
  2126.     if(pSrcMap = (ColormapPtr )LookupIDByType(stuff->srcCmap, RT_COLORMAP))
  2127.     {
  2128.     result = CopyColormapAndFree(mid, pSrcMap, client->index);
  2129.     if (client->noClientException != Success)
  2130.             return(client->noClientException);
  2131.     else
  2132.             return(result);
  2133.     }
  2134.     else
  2135.     {
  2136.     client->errorValue = stuff->srcCmap;
  2137.     return(BadColor);
  2138.     }
  2139. }
  2140.  
  2141. int
  2142. ProcInstallColormap(client)
  2143.     register ClientPtr client;
  2144. {
  2145.     ColormapPtr pcmp;
  2146.     REQUEST(xResourceReq);
  2147.  
  2148.     REQUEST_SIZE_MATCH(xResourceReq);
  2149.     pcmp = (ColormapPtr  )LookupIDByType(stuff->id, RT_COLORMAP);
  2150.     if (pcmp)
  2151.     {
  2152.         (*(pcmp->pScreen->InstallColormap)) (pcmp);
  2153.         return (client->noClientException);        
  2154.     }
  2155.     else
  2156.     {
  2157.         client->errorValue = stuff->id;
  2158.         return (BadColor);
  2159.     }
  2160. }
  2161.  
  2162. int
  2163. ProcUninstallColormap(client)
  2164.     register ClientPtr client;
  2165. {
  2166.     ColormapPtr pcmp;
  2167.     REQUEST(xResourceReq);
  2168.  
  2169.     REQUEST_SIZE_MATCH(xResourceReq);
  2170.     pcmp = (ColormapPtr )LookupIDByType(stuff->id, RT_COLORMAP);
  2171.     if (pcmp)
  2172.     {
  2173.     if(pcmp->mid != pcmp->pScreen->defColormap)
  2174.             (*(pcmp->pScreen->UninstallColormap)) (pcmp);
  2175.         return (client->noClientException);        
  2176.     }
  2177.     else
  2178.     {
  2179.         client->errorValue = stuff->id;
  2180.         return (BadColor);
  2181.     }
  2182. }
  2183.  
  2184. int
  2185. ProcListInstalledColormaps(client)
  2186.     register ClientPtr client;
  2187. {
  2188.     xListInstalledColormapsReply *preply; 
  2189.     int nummaps;
  2190.     WindowPtr pWin;
  2191.     REQUEST(xResourceReq);
  2192.  
  2193.     REQUEST_SIZE_MATCH(xResourceReq);
  2194.     pWin = (WindowPtr)LookupWindow(stuff->id, client);
  2195.  
  2196.     if (!pWin)
  2197.         return(BadWindow);
  2198.  
  2199.     preply = (xListInstalledColormapsReply *) 
  2200.         ALLOCATE_LOCAL(sizeof(xListInstalledColormapsReply) +
  2201.              pWin->drawable.pScreen->maxInstalledCmaps *
  2202.              sizeof(Colormap));
  2203.     if(!preply)
  2204.         return(BadAlloc);
  2205.  
  2206.     preply->type = X_Reply;
  2207.     preply->sequenceNumber = client->sequence;
  2208.     nummaps = (*pWin->drawable.pScreen->ListInstalledColormaps)
  2209.         (pWin->drawable.pScreen, (Colormap *)&preply[1]);
  2210.     preply->nColormaps = nummaps;
  2211.     preply->length = nummaps;
  2212.     WriteReplyToClient(client, sizeof (xListInstalledColormapsReply), preply);
  2213.     client->pSwapReplyFunc = Swap32Write;
  2214.     WriteSwappedDataToClient(client, nummaps * sizeof(Colormap), &preply[1]);
  2215.     DEALLOCATE_LOCAL(preply);
  2216.     return(client->noClientException);
  2217. }
  2218.  
  2219. int
  2220. ProcAllocColor                (client)
  2221.     register ClientPtr client;
  2222. {
  2223.     ColormapPtr pmap;
  2224.     int    retval;
  2225.     xAllocColorReply acr;
  2226.     REQUEST(xAllocColorReq);
  2227.  
  2228.     REQUEST_SIZE_MATCH(xAllocColorReq);
  2229.     pmap = (ColormapPtr )LookupIDByType(stuff->cmap, RT_COLORMAP);
  2230.     if (pmap)
  2231.     {
  2232.     acr.type = X_Reply;
  2233.     acr.length = 0;
  2234.     acr.sequenceNumber = client->sequence;
  2235.     acr.red = stuff->red;
  2236.     acr.green = stuff->green;
  2237.     acr.blue = stuff->blue;
  2238.     acr.pixel = 0;
  2239.     if(retval = AllocColor(pmap, &acr.red, &acr.green, &acr.blue,
  2240.                            &acr.pixel, client->index))
  2241.     {
  2242.             if (client->noClientException != Success)
  2243.                 return(client->noClientException);
  2244.         else
  2245.             return (retval);
  2246.     }
  2247.         WriteReplyToClient(client, sizeof(xAllocColorReply), &acr);
  2248.     return (client->noClientException);
  2249.  
  2250.     }
  2251.     else
  2252.     {
  2253.         client->errorValue = stuff->cmap;
  2254.         return (BadColor);
  2255.     }
  2256. }
  2257.  
  2258. int
  2259. ProcAllocNamedColor           (client)
  2260.     register ClientPtr client;
  2261. {
  2262.     ColormapPtr pcmp;
  2263.     REQUEST(xAllocNamedColorReq);
  2264.  
  2265.     REQUEST_FIXED_SIZE(xAllocNamedColorReq, stuff->nbytes);
  2266.     pcmp = (ColormapPtr )LookupIDByType(stuff->cmap, RT_COLORMAP);
  2267.     if (pcmp)
  2268.     {
  2269.     int        retval;
  2270.  
  2271.     xAllocNamedColorReply ancr;
  2272.  
  2273.     ancr.type = X_Reply;
  2274.     ancr.length = 0;
  2275.     ancr.sequenceNumber = client->sequence;
  2276.  
  2277.     if(OsLookupColor(pcmp->pScreen->myNum, (char *)&stuff[1], stuff->nbytes,
  2278.                      &ancr.exactRed, &ancr.exactGreen, &ancr.exactBlue))
  2279.     {
  2280.         ancr.screenRed = ancr.exactRed;
  2281.         ancr.screenGreen = ancr.exactGreen;
  2282.         ancr.screenBlue = ancr.exactBlue;
  2283.         ancr.pixel = 0;
  2284.         if(retval = AllocColor(pcmp,
  2285.                      &ancr.screenRed, &ancr.screenGreen, &ancr.screenBlue,
  2286.              &ancr.pixel, client->index))
  2287.         {
  2288.                 if (client->noClientException != Success)
  2289.                     return(client->noClientException);
  2290.                 else
  2291.                     return(retval);
  2292.         }
  2293.             WriteReplyToClient(client, sizeof (xAllocNamedColorReply), &ancr);
  2294.         return (client->noClientException);
  2295.     }
  2296.     else
  2297.         return(BadName);
  2298.     
  2299.     }
  2300.     else
  2301.     {
  2302.         client->errorValue = stuff->cmap;
  2303.         return (BadColor);
  2304.     }
  2305. }
  2306.  
  2307. int
  2308. ProcAllocColorCells           (client)
  2309.     register ClientPtr client;
  2310. {
  2311.     ColormapPtr pcmp;
  2312.     REQUEST(xAllocColorCellsReq);
  2313.  
  2314.     REQUEST_SIZE_MATCH(xAllocColorCellsReq);
  2315.     pcmp = (ColormapPtr )LookupIDByType(stuff->cmap, RT_COLORMAP);
  2316.     if (pcmp)
  2317.     {
  2318.     xAllocColorCellsReply    accr;
  2319.     int            npixels, nmasks, retval;
  2320.     long            length;
  2321.     unsigned long        *ppixels, *pmasks;
  2322.  
  2323.     npixels = stuff->colors;
  2324.     if (!npixels)
  2325.     {
  2326.         client->errorValue = npixels;
  2327.         return (BadValue);
  2328.     }
  2329.     if (stuff->contiguous != xTrue && stuff->contiguous != xFalse)
  2330.     {
  2331.         client->errorValue = stuff->contiguous;
  2332.         return (BadValue);
  2333.     }
  2334.     nmasks = stuff->planes;
  2335.     length = ((long)npixels + (long)nmasks) * sizeof(Pixel);
  2336.     ppixels = (Pixel *)ALLOCATE_LOCAL(length);
  2337.     if(!ppixels)
  2338.             return(BadAlloc);
  2339.     pmasks = ppixels + npixels;
  2340.  
  2341.     if(retval = AllocColorCells(client->index, pcmp, npixels, nmasks, 
  2342.                     (Bool)stuff->contiguous, ppixels, pmasks))
  2343.     {
  2344.         DEALLOCATE_LOCAL(ppixels);
  2345.             if (client->noClientException != Success)
  2346.                 return(client->noClientException);
  2347.         else
  2348.             return(retval);
  2349.     }
  2350.     accr.type = X_Reply;
  2351.     accr.length = length >> 2;
  2352.     accr.sequenceNumber = client->sequence;
  2353.     accr.nPixels = npixels;
  2354.     accr.nMasks = nmasks;
  2355.         WriteReplyToClient(client, sizeof (xAllocColorCellsReply), &accr);
  2356.     client->pSwapReplyFunc = Swap32Write;
  2357.     WriteSwappedDataToClient(client, length, ppixels);
  2358.     DEALLOCATE_LOCAL(ppixels);
  2359.         return (client->noClientException);        
  2360.     }
  2361.     else
  2362.     {
  2363.         client->errorValue = stuff->cmap;
  2364.         return (BadColor);
  2365.     }
  2366. }
  2367.  
  2368. int
  2369. ProcAllocColorPlanes(client)
  2370.     register ClientPtr client;
  2371. {
  2372.     ColormapPtr pcmp;
  2373.     REQUEST(xAllocColorPlanesReq);
  2374.  
  2375.     REQUEST_SIZE_MATCH(xAllocColorPlanesReq);
  2376.     pcmp = (ColormapPtr )LookupIDByType(stuff->cmap, RT_COLORMAP);
  2377.     if (pcmp)
  2378.     {
  2379.     xAllocColorPlanesReply    acpr;
  2380.     int            npixels, retval;
  2381.     long            length;
  2382.     unsigned long        *ppixels;
  2383.  
  2384.     npixels = stuff->colors;
  2385.     if (!npixels)
  2386.     {
  2387.         client->errorValue = npixels;
  2388.         return (BadValue);
  2389.     }
  2390.     if (stuff->contiguous != xTrue && stuff->contiguous != xFalse)
  2391.     {
  2392.         client->errorValue = stuff->contiguous;
  2393.         return (BadValue);
  2394.     }
  2395.     acpr.type = X_Reply;
  2396.     acpr.sequenceNumber = client->sequence;
  2397.     acpr.nPixels = npixels;
  2398.     length = (long)npixels * sizeof(Pixel);
  2399.     ppixels = (Pixel *)ALLOCATE_LOCAL(length);
  2400.     if(!ppixels)
  2401.             return(BadAlloc);
  2402.     if(retval = AllocColorPlanes(client->index, pcmp, npixels,
  2403.         (int)stuff->red, (int)stuff->green, (int)stuff->blue,
  2404.         (int)stuff->contiguous, ppixels,
  2405.         &acpr.redMask, &acpr.greenMask, &acpr.blueMask))
  2406.     {
  2407.             DEALLOCATE_LOCAL(ppixels);
  2408.             if (client->noClientException != Success)
  2409.                 return(client->noClientException);
  2410.         else
  2411.             return(retval);
  2412.     }
  2413.     acpr.length = length >> 2;
  2414.     WriteReplyToClient(client, sizeof(xAllocColorPlanesReply), &acpr);
  2415.     client->pSwapReplyFunc = Swap32Write;
  2416.     WriteSwappedDataToClient(client, length, ppixels);
  2417.     DEALLOCATE_LOCAL(ppixels);
  2418.         return (client->noClientException);        
  2419.     }
  2420.     else
  2421.     {
  2422.         client->errorValue = stuff->cmap;
  2423.         return (BadColor);
  2424.     }
  2425. }
  2426.  
  2427. int
  2428. ProcFreeColors          (client)
  2429.     register ClientPtr client;
  2430. {
  2431.     ColormapPtr pcmp;
  2432.     REQUEST(xFreeColorsReq);
  2433.  
  2434.     REQUEST_AT_LEAST_SIZE(xFreeColorsReq);
  2435.     pcmp = (ColormapPtr )LookupIDByType(stuff->cmap, RT_COLORMAP);
  2436.     if (pcmp)
  2437.     {
  2438.     int    count;
  2439.         int     retval;
  2440.  
  2441.     if(pcmp->flags & AllAllocated)
  2442.         return(BadAccess);
  2443.     count = ((stuff->length << 2)- sizeof(xFreeColorsReq)) >> 2;
  2444.     retval =  FreeColors(pcmp, client->index, count,
  2445.         (unsigned long *)&stuff[1], stuff->planeMask);
  2446.         if (client->noClientException != Success)
  2447.             return(client->noClientException);
  2448.         else
  2449.     {
  2450.         client->errorValue = clientErrorValue;
  2451.             return(retval);
  2452.     }
  2453.  
  2454.     }
  2455.     else
  2456.     {
  2457.         client->errorValue = stuff->cmap;
  2458.         return (BadColor);
  2459.     }
  2460. }
  2461.  
  2462. int
  2463. ProcStoreColors               (client)
  2464.     register ClientPtr client;
  2465. {
  2466.     ColormapPtr pcmp;
  2467.     REQUEST(xStoreColorsReq);
  2468.  
  2469.     REQUEST_AT_LEAST_SIZE(xStoreColorsReq);
  2470.     pcmp = (ColormapPtr )LookupIDByType(stuff->cmap, RT_COLORMAP);
  2471.     if (pcmp)
  2472.     {
  2473.     int    count;
  2474.         int     retval;
  2475.  
  2476.         count = (stuff->length << 2) - sizeof(xStoreColorsReq);
  2477.     if (count % sizeof(xColorItem))
  2478.         return(BadLength);
  2479.     count /= sizeof(xColorItem);
  2480.     retval = StoreColors(pcmp, count, (xColorItem *)&stuff[1]);
  2481.         if (client->noClientException != Success)
  2482.             return(client->noClientException);
  2483.         else
  2484.     {
  2485.         client->errorValue = clientErrorValue;
  2486.             return(retval);
  2487.     }
  2488.     }
  2489.     else
  2490.     {
  2491.         client->errorValue = stuff->cmap;
  2492.         return (BadColor);
  2493.     }
  2494. }
  2495.  
  2496. int
  2497. ProcStoreNamedColor           (client)
  2498.     register ClientPtr client;
  2499. {
  2500.     ColormapPtr pcmp;
  2501.     REQUEST(xStoreNamedColorReq);
  2502.  
  2503.     REQUEST_FIXED_SIZE(xStoreNamedColorReq, stuff->nbytes);
  2504.     pcmp = (ColormapPtr )LookupIDByType(stuff->cmap, RT_COLORMAP);
  2505.     if (pcmp)
  2506.     {
  2507.     xColorItem    def;
  2508.         int             retval;
  2509.  
  2510.     if(OsLookupColor(pcmp->pScreen->myNum, (char *)&stuff[1],
  2511.                      stuff->nbytes, &def.red, &def.green, &def.blue))
  2512.     {
  2513.         def.flags = stuff->flags;
  2514.         def.pixel = stuff->pixel;
  2515.         retval = StoreColors(pcmp, 1, &def);
  2516.             if (client->noClientException != Success)
  2517.                 return(client->noClientException);
  2518.         else
  2519.         return(retval);
  2520.     }
  2521.         return (BadName);        
  2522.     }
  2523.     else
  2524.     {
  2525.         client->errorValue = stuff->cmap;
  2526.         return (BadColor);
  2527.     }
  2528. }
  2529.  
  2530. int
  2531. ProcQueryColors(client)
  2532.     register ClientPtr client;
  2533. {
  2534.     ColormapPtr pcmp;
  2535.     REQUEST(xQueryColorsReq);
  2536.  
  2537.     REQUEST_AT_LEAST_SIZE(xQueryColorsReq);
  2538.     pcmp = (ColormapPtr )LookupIDByType(stuff->cmap, RT_COLORMAP);
  2539.     if (pcmp)
  2540.     {
  2541.     int            count, retval;
  2542.     xrgb             *prgbs;
  2543.     xQueryColorsReply    qcr;
  2544.  
  2545.     count = ((stuff->length << 2) - sizeof(xQueryColorsReq)) >> 2;
  2546.     prgbs = (xrgb *)ALLOCATE_LOCAL(count * sizeof(xrgb));
  2547.     if(!prgbs && count)
  2548.             return(BadAlloc);
  2549.     if(retval = QueryColors(pcmp, count, (unsigned long *)&stuff[1], prgbs))
  2550.     {
  2551.            if (prgbs) DEALLOCATE_LOCAL(prgbs);
  2552.         if (client->noClientException != Success)
  2553.                 return(client->noClientException);
  2554.         else
  2555.         {
  2556.         client->errorValue = clientErrorValue;
  2557.             return (retval);
  2558.         }
  2559.     }
  2560.     qcr.type = X_Reply;
  2561.     qcr.length = (count * sizeof(xrgb)) >> 2;
  2562.     qcr.sequenceNumber = client->sequence;
  2563.     qcr.nColors = count;
  2564.     WriteReplyToClient(client, sizeof(xQueryColorsReply), &qcr);
  2565.     if (count)
  2566.     {
  2567.         client->pSwapReplyFunc = SQColorsExtend;
  2568.         WriteSwappedDataToClient(client, count * sizeof(xrgb), prgbs);
  2569.     }
  2570.     if (prgbs) DEALLOCATE_LOCAL(prgbs);
  2571.     return(client->noClientException);
  2572.     
  2573.     }
  2574.     else
  2575.     {
  2576.         client->errorValue = stuff->cmap;
  2577.         return (BadColor);
  2578.     }
  2579.  
  2580. int
  2581. ProcLookupColor(client)
  2582.     register ClientPtr client;
  2583. {
  2584.     ColormapPtr pcmp;
  2585.     REQUEST(xLookupColorReq);
  2586.  
  2587.     REQUEST_FIXED_SIZE(xLookupColorReq, stuff->nbytes);
  2588.     pcmp = (ColormapPtr )LookupIDByType(stuff->cmap, RT_COLORMAP);
  2589.     if (pcmp)
  2590.     {
  2591.     xLookupColorReply lcr;
  2592.  
  2593.     if(OsLookupColor(pcmp->pScreen->myNum, (char *)&stuff[1], stuff->nbytes,
  2594.                      &lcr.exactRed, &lcr.exactGreen, &lcr.exactBlue))
  2595.     {
  2596.         lcr.type = X_Reply;
  2597.         lcr.length = 0;
  2598.         lcr.sequenceNumber = client->sequence;
  2599.         lcr.screenRed = lcr.exactRed;
  2600.         lcr.screenGreen = lcr.exactGreen;
  2601.         lcr.screenBlue = lcr.exactBlue;
  2602.         (*pcmp->pScreen->ResolveColor)(&lcr.screenRed,
  2603.                                        &lcr.screenGreen,
  2604.                        &lcr.screenBlue,
  2605.                        pcmp->pVisual);
  2606.         WriteReplyToClient(client, sizeof(xLookupColorReply), &lcr);
  2607.         return(client->noClientException);
  2608.     }
  2609.         return (BadName);        
  2610.     }
  2611.     else
  2612.     {
  2613.         client->errorValue = stuff->cmap;
  2614.         return (BadColor);
  2615.     }
  2616. }
  2617.  
  2618. int
  2619. ProcCreateCursor( client)
  2620.     register ClientPtr client;
  2621. {
  2622.     CursorPtr    pCursor;
  2623.  
  2624.     register PixmapPtr     src;
  2625.     register PixmapPtr     msk;
  2626.     unsigned char *    srcbits;
  2627.     unsigned char *    mskbits;
  2628.     unsigned short    width, height;
  2629.     long        n;
  2630.     CursorMetricRec cm;
  2631.  
  2632.  
  2633.     REQUEST(xCreateCursorReq);
  2634.  
  2635.     REQUEST_SIZE_MATCH(xCreateCursorReq);
  2636.     LEGAL_NEW_RESOURCE(stuff->cid, client);
  2637.  
  2638.     src = (PixmapPtr)LookupIDByType(stuff->source, RT_PIXMAP);
  2639.     msk = (PixmapPtr)LookupIDByType(stuff->mask, RT_PIXMAP);
  2640.     if (   src == (PixmapPtr)NULL)
  2641.     {
  2642.     client->errorValue = stuff->source;
  2643.     return (BadPixmap);
  2644.     }
  2645.     if ( msk == (PixmapPtr)NULL)
  2646.     {
  2647.     if (stuff->mask != None)
  2648.     {
  2649.         client->errorValue = stuff->mask;
  2650.         return (BadPixmap);
  2651.     }
  2652.     }
  2653.     else if (  src->drawable.width != msk->drawable.width
  2654.         || src->drawable.height != msk->drawable.height
  2655.         || src->drawable.depth != 1
  2656.         || msk->drawable.depth != 1)
  2657.     return (BadMatch);
  2658.  
  2659.     width = src->drawable.width;
  2660.     height = src->drawable.height;
  2661.  
  2662.     if ( stuff->x > width 
  2663.       || stuff->y > height )
  2664.     return (BadMatch);
  2665.  
  2666.     n = PixmapBytePad(width, 1)*height;
  2667.     srcbits = (unsigned char *)xalloc(n);
  2668.     if (!srcbits)
  2669.     return (BadAlloc);
  2670.     mskbits = (unsigned char *)xalloc(n);
  2671.     if (!mskbits)
  2672.     {
  2673.     xfree(srcbits);
  2674.     return (BadAlloc);
  2675.     }
  2676.  
  2677.     /* zeroing the (pad) bits helps some ddx cursor handling */
  2678.     bzero((char *)srcbits, n);
  2679.     (* src->drawable.pScreen->GetImage)( src, 0, 0, width, height,
  2680.                      XYPixmap, 1, srcbits);
  2681.     if ( msk == (PixmapPtr)NULL)
  2682.     {
  2683.     register unsigned char *bits = mskbits;
  2684.     while (--n >= 0)
  2685.         *bits++ = ~0;
  2686.     }
  2687.     else
  2688.     {
  2689.     /* zeroing the (pad) bits helps some ddx cursor handling */
  2690.     bzero((char *)mskbits, n);
  2691.     (* msk->drawable.pScreen->GetImage)( msk, 0, 0, width, height,
  2692.                          XYPixmap, 1, mskbits);
  2693.     }
  2694.     cm.width = width;
  2695.     cm.height = height;
  2696.     cm.xhot = stuff->x;
  2697.     cm.yhot = stuff->y;
  2698.     pCursor = AllocCursor( srcbits, mskbits, &cm,
  2699.         stuff->foreRed, stuff->foreGreen, stuff->foreBlue,
  2700.         stuff->backRed, stuff->backGreen, stuff->backBlue);
  2701.  
  2702.     if (pCursor && AddResource(stuff->cid, RT_CURSOR, (pointer)pCursor))
  2703.         return (client->noClientException);
  2704.     return BadAlloc;
  2705. }
  2706.  
  2707. int
  2708. ProcCreateGlyphCursor( client)
  2709.     register ClientPtr client;
  2710. {
  2711.     CursorPtr pCursor;
  2712.     int res;
  2713.  
  2714.     REQUEST(xCreateGlyphCursorReq);
  2715.  
  2716.     REQUEST_SIZE_MATCH(xCreateGlyphCursorReq);
  2717.     LEGAL_NEW_RESOURCE(stuff->cid, client);
  2718.  
  2719.     res = AllocGlyphCursor(stuff->source, stuff->sourceChar,
  2720.                stuff->mask, stuff->maskChar,
  2721.                stuff->foreRed, stuff->foreGreen, stuff->foreBlue,
  2722.                stuff->backRed, stuff->backGreen, stuff->backBlue,
  2723.                &pCursor, client);
  2724.     if (res != Success)
  2725.     return res;
  2726.     if (AddResource(stuff->cid, RT_CURSOR, (pointer)pCursor))
  2727.     return client->noClientException;
  2728.     return BadAlloc;
  2729. }
  2730.  
  2731.  
  2732. int
  2733. ProcFreeCursor(client)
  2734.     register ClientPtr client;
  2735. {
  2736.     CursorPtr pCursor;
  2737.     REQUEST(xResourceReq);
  2738.  
  2739.     REQUEST_SIZE_MATCH(xResourceReq);
  2740.     pCursor = (CursorPtr)LookupIDByType(stuff->id, RT_CURSOR);
  2741.     if (pCursor) 
  2742.     {
  2743.     FreeResource(stuff->id, RT_NONE);
  2744.     return (client->noClientException);
  2745.     }
  2746.     else 
  2747.     {
  2748.     client->errorValue = stuff->id;
  2749.     return (BadCursor);
  2750.     }
  2751. }
  2752.  
  2753. int
  2754. ProcQueryBestSize   (client)
  2755.     register ClientPtr client;
  2756. {
  2757.     xQueryBestSizeReply    reply;
  2758.     register DrawablePtr pDraw;
  2759.     ScreenPtr pScreen;
  2760.     REQUEST(xQueryBestSizeReq);
  2761.  
  2762.     REQUEST_SIZE_MATCH(xQueryBestSizeReq);
  2763.     if ((stuff->class != CursorShape) && 
  2764.     (stuff->class != TileShape) && 
  2765.     (stuff->class != StippleShape))
  2766.     {
  2767.     client->errorValue = stuff->class;
  2768.         return(BadValue);
  2769.     }
  2770.     if (!(pDraw = LOOKUP_DRAWABLE(stuff->drawable, client)))
  2771.     {
  2772.         if (!(pDraw = (DrawablePtr)LookupWindow(stuff->drawable, client))) 
  2773.     {
  2774.         client->errorValue = stuff->drawable;
  2775.         return (BadDrawable);
  2776.     }
  2777.     if (stuff->class != CursorShape)
  2778.         return (BadMatch);
  2779.     }
  2780.     pScreen = pDraw->pScreen;
  2781.     (* pScreen->QueryBestSize)(stuff->class, &stuff->width,
  2782.                    &stuff->height, pScreen);
  2783.     reply.type = X_Reply;
  2784.     reply.length = 0;
  2785.     reply.sequenceNumber = client->sequence;
  2786.     reply.width = stuff->width;
  2787.     reply.height = stuff->height;
  2788.     WriteReplyToClient(client, sizeof(xQueryBestSizeReply), &reply);
  2789.     return (client->noClientException);
  2790. }
  2791.  
  2792.  
  2793. int
  2794. ProcSetScreenSaver            (client)
  2795.     register ClientPtr client;
  2796. {
  2797.     int blankingOption, exposureOption;
  2798.     REQUEST(xSetScreenSaverReq);
  2799.  
  2800.     REQUEST_SIZE_MATCH(xSetScreenSaverReq);
  2801.     blankingOption = stuff->preferBlank;
  2802.     if ((blankingOption != DontPreferBlanking) &&
  2803.         (blankingOption != PreferBlanking) &&
  2804.         (blankingOption != DefaultBlanking))
  2805.     {
  2806.     client->errorValue = blankingOption;
  2807.         return BadValue;
  2808.     }
  2809.     exposureOption = stuff->allowExpose;
  2810.     if ((exposureOption != DontAllowExposures) &&
  2811.         (exposureOption != AllowExposures) &&
  2812.         (exposureOption != DefaultExposures))
  2813.     {
  2814.     client->errorValue = exposureOption;
  2815.         return BadValue;
  2816.     }
  2817.     if (stuff->timeout < -1)
  2818.     {
  2819.     client->errorValue = stuff->timeout;
  2820.         return BadValue;
  2821.     }
  2822.     if (stuff->interval < -1)
  2823.     {
  2824.     client->errorValue = stuff->interval;
  2825.         return BadValue;
  2826.     }
  2827.  
  2828.     if (blankingOption == DefaultBlanking)
  2829.     ScreenSaverBlanking = defaultScreenSaverBlanking;
  2830.     else
  2831.     ScreenSaverBlanking = blankingOption; 
  2832.     if (exposureOption == DefaultExposures)
  2833.     ScreenSaverAllowExposures = defaultScreenSaverAllowExposures;
  2834.     else
  2835.     ScreenSaverAllowExposures = exposureOption;
  2836.  
  2837.     if (stuff->timeout >= 0)
  2838.     ScreenSaverTime = stuff->timeout * MILLI_PER_SECOND;
  2839.     else 
  2840.     ScreenSaverTime = defaultScreenSaverTime;
  2841.     if (stuff->interval >= 0)
  2842.     ScreenSaverInterval = stuff->interval * MILLI_PER_SECOND;
  2843.     else
  2844.     ScreenSaverInterval = defaultScreenSaverInterval;
  2845.     return (client->noClientException);
  2846. }
  2847.  
  2848. int
  2849. ProcGetScreenSaver(client)
  2850.     register ClientPtr client;
  2851. {
  2852.     xGetScreenSaverReply rep;
  2853.  
  2854.     rep.type = X_Reply;
  2855.     rep.length = 0;
  2856.     rep.sequenceNumber = client->sequence;
  2857.     rep.timeout = ScreenSaverTime / MILLI_PER_SECOND;
  2858.     rep.interval = ScreenSaverInterval / MILLI_PER_SECOND;
  2859.     rep.preferBlanking = ScreenSaverBlanking;
  2860.     rep.allowExposures = ScreenSaverAllowExposures;
  2861.     WriteReplyToClient(client, sizeof(xGetScreenSaverReply), &rep);
  2862.     return (client->noClientException);
  2863. }
  2864.  
  2865. int
  2866. ProcChangeHosts(client)
  2867.     register ClientPtr client;
  2868. {
  2869.     REQUEST(xChangeHostsReq);
  2870.     int result;
  2871.  
  2872.     REQUEST_FIXED_SIZE(xChangeHostsReq, stuff->hostLength);
  2873.  
  2874.     if(stuff->mode == HostInsert)
  2875.     result = AddHost(client, (int)stuff->hostFamily,
  2876.              stuff->hostLength, (pointer)&stuff[1]);
  2877.     else if (stuff->mode == HostDelete)
  2878.     result = RemoveHost(client, (int)stuff->hostFamily, 
  2879.                 stuff->hostLength, (pointer)&stuff[1]);  
  2880.     else
  2881.     {
  2882.     client->errorValue = stuff->mode;
  2883.         return BadValue;
  2884.     }
  2885.     if (!result)
  2886.     result = client->noClientException;
  2887.     return (result);
  2888. }
  2889.  
  2890. int
  2891. ProcListHosts(client)
  2892.     register ClientPtr client;
  2893. {
  2894. extern int GetHosts();
  2895.     xListHostsReply reply;
  2896.     int    len, nHosts, result;
  2897.     pointer    pdata;
  2898.     REQUEST(xListHostsReq);
  2899.  
  2900.     REQUEST_SIZE_MATCH(xListHostsReq);
  2901.     result = GetHosts(&pdata, &nHosts, &len, &reply.enabled);
  2902.     if (result != Success)
  2903.     return(result);
  2904.     reply.type = X_Reply;
  2905.     reply.sequenceNumber = client->sequence;
  2906.     reply.nHosts = nHosts;
  2907.     reply.length = len >> 2;
  2908.     WriteReplyToClient(client, sizeof(xListHostsReply), &reply);
  2909.     if (nHosts)
  2910.     {
  2911.     client->pSwapReplyFunc = SLHostsExtend;
  2912.     WriteSwappedDataToClient(client, len, pdata);
  2913.     }
  2914.     xfree(pdata);
  2915.     return (client->noClientException);
  2916. }
  2917.  
  2918. int
  2919. ProcChangeAccessControl(client)
  2920.     register ClientPtr client;
  2921. {
  2922.     int result;
  2923.     REQUEST(xSetAccessControlReq);
  2924.  
  2925.     REQUEST_SIZE_MATCH(xSetAccessControlReq);
  2926.     if ((stuff->mode != EnableAccess) && (stuff->mode != DisableAccess))
  2927.     {
  2928.     client->errorValue = stuff->mode;
  2929.         return BadValue;
  2930.     }
  2931.     result = ChangeAccessControl(client, stuff->mode == EnableAccess);
  2932.     if (!result)
  2933.     result = client->noClientException;
  2934.     return (result);
  2935. }
  2936.  
  2937. int
  2938. ProcKillClient(client)
  2939.     register ClientPtr client;
  2940. {
  2941.     REQUEST(xResourceReq);
  2942.  
  2943.     pointer *pResource;
  2944.     int clientIndex, myIndex;
  2945.  
  2946.     REQUEST_SIZE_MATCH(xResourceReq);
  2947.     if (stuff->id == AllTemporary)
  2948.     {
  2949.     CloseDownRetainedResources();
  2950.         return (client->noClientException);
  2951.     }
  2952.     pResource = (pointer *)LookupIDByClass(stuff->id, RC_ANY);
  2953.   
  2954.     clientIndex = CLIENT_ID(stuff->id);
  2955.  
  2956.     if (clientIndex && pResource && clients[clientIndex] &&
  2957.     !(stuff->id & SERVER_BIT) &&
  2958.     (clients[clientIndex]->requestVector != InitialVector))
  2959.     {
  2960.     myIndex = client->index;
  2961.     CloseDownClient(clients[clientIndex]);
  2962.     if (myIndex == clientIndex)
  2963.     {
  2964.         /* force yield and return Success, so that Dispatch()
  2965.          * doesn't try to touch client
  2966.          */
  2967.         isItTimeToYield = TRUE;
  2968.         return (Success);
  2969.     }
  2970.     return (client->noClientException);
  2971.     }
  2972.     else
  2973.     {
  2974.     client->errorValue = stuff->id;
  2975.     return (BadValue);
  2976.     }
  2977. }
  2978.  
  2979. int
  2980. ProcSetFontPath(client)
  2981.     register ClientPtr client;
  2982. {
  2983.     unsigned char *ptr;
  2984.     unsigned long nbytes, total;
  2985.     long nfonts;
  2986.     int n, result;
  2987.     int error;
  2988.     REQUEST(xSetFontPathReq);
  2989.     
  2990.     REQUEST_AT_LEAST_SIZE(xSetFontPathReq);
  2991.     
  2992.     nbytes = (stuff->length << 2) - sizeof(xSetFontPathReq);
  2993.     total = nbytes;
  2994.     ptr = (unsigned char *)&stuff[1];
  2995.     nfonts = stuff->nFonts;
  2996.     while (--nfonts >= 0)
  2997.     {
  2998.     if ((total == 0) || (total < (n = (*ptr + 1))))
  2999.         return(BadLength);
  3000.     total -= n;
  3001.     ptr += n;
  3002.     }
  3003.     if (total >= 4)
  3004.     return(BadLength);
  3005.     result = SetFontPath(client, stuff->nFonts, (char *)&stuff[1], &error);
  3006.     if (error != -1)
  3007.     client->errorValue = error;
  3008.     if (!result)
  3009.     result = client->noClientException;
  3010.     return (result);
  3011. }
  3012.  
  3013. int
  3014. ProcGetFontPath(client)
  3015.     register ClientPtr client;
  3016. {
  3017.     xGetFontPathReply reply;
  3018.     int stringLens, numpaths;
  3019.     unsigned char *bufferStart;
  3020.     REQUEST (xReq);
  3021.  
  3022.     REQUEST_SIZE_MATCH(xReq);
  3023.     bufferStart = GetFontPath(&numpaths, &stringLens);
  3024.  
  3025.     reply.type = X_Reply;
  3026.     reply.sequenceNumber = client->sequence;
  3027.     reply.length = (stringLens + numpaths + 3) >> 2;
  3028.     reply.nPaths = numpaths;
  3029.  
  3030.     WriteReplyToClient(client, sizeof(xGetFontPathReply), &reply);
  3031.     if (stringLens || numpaths)
  3032.     (void)WriteToClient(client, stringLens + numpaths, bufferStart);
  3033.     return(client->noClientException);
  3034. }
  3035.  
  3036. int
  3037. ProcChangeCloseDownMode(client)
  3038.     register ClientPtr client;
  3039. {
  3040.     REQUEST(xSetCloseDownModeReq);
  3041.  
  3042.     REQUEST_SIZE_MATCH(xSetCloseDownModeReq);
  3043.     if ((stuff->mode == AllTemporary) ||
  3044.     (stuff->mode == RetainPermanent) ||
  3045.     (stuff->mode == RetainTemporary))
  3046.     {
  3047.     client->closeDownMode = stuff->mode;
  3048.     return (client->noClientException);
  3049.     }
  3050.     else   
  3051.     {
  3052.     client->errorValue = stuff->mode;
  3053.     return (BadValue);
  3054.     }
  3055. }
  3056.  
  3057. int ProcForceScreenSaver(client)
  3058.     register ClientPtr client;
  3059. {    
  3060.     REQUEST(xForceScreenSaverReq);
  3061.  
  3062.     REQUEST_SIZE_MATCH(xForceScreenSaverReq);
  3063.     
  3064.     if ((stuff->mode != ScreenSaverReset) && 
  3065.     (stuff->mode != ScreenSaverActive))
  3066.     {
  3067.     client->errorValue = stuff->mode;
  3068.         return BadValue;
  3069.     }
  3070.     SaveScreens(SCREEN_SAVER_FORCER, (int)stuff->mode);
  3071.     return client->noClientException;
  3072. }
  3073.  
  3074. int ProcNoOperation(client)
  3075.     register ClientPtr client;
  3076. {
  3077.     REQUEST(xReq);
  3078.  
  3079.     REQUEST_AT_LEAST_SIZE(xReq);
  3080.     
  3081.     /* noop -- don't do anything */
  3082.     return(client->noClientException);
  3083. }
  3084.  
  3085. void
  3086. InitProcVectors()
  3087. {
  3088.     int i;
  3089.     for (i = 0; i<256; i++)
  3090.     {
  3091.     if(!ProcVector[i])
  3092.     {
  3093.             ProcVector[i] = SwappedProcVector[i] = ProcBadRequest;
  3094.         ReplySwapVector[i] = NotImplemented;
  3095.     }
  3096.     }
  3097.     for(i = LASTEvent; i < 128; i++)
  3098.     {
  3099.     EventSwapVector[i] = NotImplemented;
  3100.     }
  3101.     
  3102. }
  3103.  
  3104. /**********************
  3105.  * CloseDownClient
  3106.  *
  3107.  *  Client can either mark his resources destroy or retain.  If retained and
  3108.  *  then killed again, the client is really destroyed.
  3109.  *********************/
  3110.  
  3111. Bool terminateAtReset = FALSE;
  3112.  
  3113. void
  3114. CloseDownClient(client)
  3115.     register ClientPtr client;
  3116. {
  3117.     if (!client->clientGone)
  3118.     {
  3119.     /* ungrab server if grabbing client dies */
  3120.     if (grabbingClient &&  (onlyClient == client))
  3121.     {
  3122.         grabbingClient = FALSE;
  3123.         ListenToAllClients();
  3124.     }
  3125.     DeleteClientFromAnySelections(client);
  3126.     ReleaseActiveGrabs(client);
  3127.     DeleteClientFontStuff(client);
  3128.     
  3129.     if (client->closeDownMode == DestroyAll)
  3130.     {
  3131.         client->clientGone = TRUE;  /* so events aren't sent to client */
  3132.         CloseDownConnection(client);
  3133.         FreeClientResources(client);
  3134.         if (ClientIsAsleep (client))
  3135.         ClientSignal (client);
  3136.         if (client->index < nextFreeClientID)
  3137.         nextFreeClientID = client->index;
  3138.         clients[client->index] = NullClient;
  3139.         if ((client->requestVector != InitialVector) &&
  3140.         (--nClients == 0))
  3141.         {
  3142.         if (terminateAtReset)
  3143.             dispatchException |= DE_TERMINATE;
  3144.         else
  3145.             dispatchException |= DE_RESET;
  3146.         }
  3147.         xfree(client);
  3148.     }
  3149.     else
  3150.     {
  3151.         client->clientGone = TRUE;
  3152.         CloseDownConnection(client);
  3153.         --nClients;
  3154.     }
  3155.     }
  3156.     else
  3157.     {
  3158.     /* really kill resources this time */
  3159.         FreeClientResources(client);
  3160.     if (ClientIsAsleep (client))
  3161.         ClientSignal (client);
  3162.     if (client->index < nextFreeClientID)
  3163.         nextFreeClientID = client->index;
  3164.     clients[client->index] = NullClient;
  3165.         xfree(client);
  3166.     }
  3167.  
  3168.     while (!clients[currentMaxClients-1])
  3169.       currentMaxClients--;
  3170. }
  3171.  
  3172. static void
  3173. KillAllClients()
  3174. {
  3175.     int i;
  3176.     for (i=1; i<currentMaxClients; i++)
  3177.         if (clients[i])
  3178.             CloseDownClient(clients[i]);     
  3179. }
  3180.  
  3181. /*********************
  3182.  * CloseDownRetainedResources
  3183.  *
  3184.  *    Find all clients that are gone and have terminated in RetainTemporary 
  3185.  *    and  destroy their resources.
  3186.  *********************/
  3187.  
  3188. CloseDownRetainedResources()
  3189. {
  3190.     register int i;
  3191.     register ClientPtr client;
  3192.  
  3193.     for (i=1; i<currentMaxClients; i++)
  3194.     {
  3195.         client = clients[i];
  3196.         if (client && (client->closeDownMode == RetainTemporary)
  3197.         && (client->clientGone))
  3198.         CloseDownClient(client);
  3199.     }
  3200. }
  3201.  
  3202. /************************
  3203.  * int NextAvailableClient(ospriv)
  3204.  *
  3205.  * OS dependent portion can't assign client id's because of CloseDownModes.
  3206.  * Returns NULL if there are no free clients.
  3207.  *************************/
  3208.  
  3209. ClientPtr
  3210. NextAvailableClient(ospriv)
  3211.     pointer ospriv;
  3212. {
  3213.     register int i;
  3214.     register ClientPtr client;
  3215.     xReq data;
  3216.  
  3217.     i = nextFreeClientID;
  3218.     if (i == MAXCLIENTS)
  3219.     return (ClientPtr)NULL;
  3220.     clients[i] = client = (ClientPtr)xalloc(sizeof(ClientRec));
  3221.     if (!client)
  3222.     return (ClientPtr)NULL;
  3223.     client->index = i;
  3224.     client->sequence = 0; 
  3225.     client->clientAsMask = ((Mask)i) << CLIENTOFFSET;
  3226.     client->closeDownMode = DestroyAll;
  3227.     client->clientGone = FALSE;
  3228.     client->lastDrawable = (DrawablePtr)WindowTable[0];
  3229.     client->lastDrawableID = WindowTable[0]->drawable.id;
  3230.     client->lastGC = (GCPtr) NULL;
  3231.     client->lastGCID = INVALID;
  3232.     client->numSaved = 0;
  3233.     client->saveSet = (pointer *)NULL;
  3234.     client->noClientException = Success;
  3235. #ifdef DEBUG
  3236.     client->requestLogIndex = 0;
  3237. #endif
  3238.     client->requestVector = InitialVector;
  3239.     client->osPrivate = ospriv;
  3240.     client->swapped = FALSE;
  3241.     if (!InitClientResources(client))
  3242.     {
  3243.     xfree(client);
  3244.     return (ClientPtr)NULL;
  3245.     }
  3246.     data.reqType = 1;
  3247.     data.length = (sz_xReq + sz_xConnClientPrefix) >> 2;
  3248.     if (!InsertFakeRequest(client, (char *)&data, sz_xReq))
  3249.     {
  3250.     FreeClientResources(client);
  3251.     xfree(client);
  3252.     return (ClientPtr)NULL;
  3253.     }
  3254.     if (i == currentMaxClients)
  3255.     currentMaxClients++;
  3256.     while ((nextFreeClientID < MAXCLIENTS) && clients[nextFreeClientID])
  3257.     nextFreeClientID++;
  3258.     return(client);
  3259. }
  3260.  
  3261. int
  3262. ProcInitialConnection(client)
  3263.     register ClientPtr client;
  3264. {
  3265.     REQUEST(xReq);
  3266.     register xConnClientPrefix *prefix;
  3267.     int whichbyte = 1;
  3268.  
  3269.     prefix = (xConnClientPrefix *)((char *)stuff + sz_xReq);
  3270.     if ((prefix->byteOrder != 'l') && (prefix->byteOrder != 'B'))
  3271.     return (client->noClientException = -1);
  3272.     if (((*(char *) &whichbyte) && (prefix->byteOrder == 'B')) ||
  3273.     (!(*(char *) &whichbyte) && (prefix->byteOrder == 'l')))
  3274.     {
  3275.     client->swapped = TRUE;
  3276.     SwapConnClientPrefix(prefix);
  3277.     }
  3278.     stuff->reqType = 2;
  3279.     stuff->length += ((prefix->nbytesAuthProto + 3) >> 2) +
  3280.              ((prefix->nbytesAuthString + 3) >> 2);
  3281.     if (client->swapped)
  3282.     {
  3283.     swaps(&stuff->length, whichbyte);
  3284.     }
  3285.     ResetCurrentRequest(client);
  3286.     return (client->noClientException);
  3287. }
  3288.  
  3289. int
  3290. ProcEstablishConnection(client)
  3291.     register ClientPtr client;
  3292. {
  3293.     char *reason, *auth_proto, *auth_string;
  3294.     register xConnClientPrefix *prefix;
  3295.     register xWindowRoot *root;
  3296.     register int i;
  3297.     REQUEST(xReq);
  3298.  
  3299.     prefix = (xConnClientPrefix *)((char *)stuff + sz_xReq);
  3300.     auth_proto = (char *)prefix + sz_xConnClientPrefix;
  3301.     auth_string = auth_proto + ((prefix->nbytesAuthProto + 3) & ~3);
  3302.     if ((prefix->majorVersion != X_PROTOCOL) ||
  3303.     (prefix->minorVersion != X_PROTOCOL_REVISION))
  3304.     reason = "Protocol version mismatch";
  3305.     else
  3306.     reason = ClientAuthorized(client,
  3307.                   (unsigned short)prefix->nbytesAuthProto,
  3308.                   auth_proto,
  3309.                   (unsigned short)prefix->nbytesAuthString,
  3310.                   auth_string);
  3311.     if (reason)
  3312.     {
  3313.     xConnSetupPrefix csp;
  3314.     char pad[3];
  3315.  
  3316.     csp.success = xFalse;
  3317.     csp.lengthReason = strlen(reason);
  3318.     csp.length = (csp.lengthReason + 3) >> 2;
  3319.     csp.majorVersion = X_PROTOCOL;
  3320.     csp.minorVersion = X_PROTOCOL_REVISION;
  3321.     if (client->swapped)
  3322.         WriteSConnSetupPrefix(client, &csp);
  3323.     else
  3324.         (void)WriteToClient(client, sz_xConnSetupPrefix, (char *) &csp);
  3325.         (void)WriteToClient(client, (int)csp.lengthReason, reason);
  3326.     if (csp.lengthReason & 3)
  3327.         (void)WriteToClient(client, (int)(4 - (csp.lengthReason & 3)),
  3328.                 pad);
  3329.     return (client->noClientException = -1);
  3330.     }
  3331.  
  3332.     nClients++;
  3333.     client->requestVector = client->swapped ? SwappedProcVector : ProcVector;
  3334.     client->sequence = 0;
  3335.     ((xConnSetup *)ConnectionInfo)->ridBase = client->clientAsMask;
  3336.     ((xConnSetup *)ConnectionInfo)->ridMask = RESOURCE_ID_MASK;
  3337.     /* fill in the "currentInputMask" */
  3338.     root = (xWindowRoot *)(ConnectionInfo + connBlockScreenStart);
  3339.     for (i=0; i<screenInfo.numScreens; i++) 
  3340.     {
  3341.     register int j;
  3342.     register xDepth *pDepth;
  3343.  
  3344.         root->currentInputMask = WindowTable[i]->eventMask |
  3345.                      wOtherEventMasks (WindowTable[i]);
  3346.     pDepth = (xDepth *)(root + 1);
  3347.     for (j = 0; j < root->nDepths; j++)
  3348.     {
  3349.         pDepth = (xDepth *)(((char *)(pDepth + 1)) +
  3350.                 pDepth->nVisuals * sizeof(xVisualType));
  3351.     }
  3352.     root = (xWindowRoot *)pDepth;
  3353.     }
  3354.  
  3355.     if (client->swapped)
  3356.     {
  3357.     WriteSConnSetupPrefix(client, &connSetupPrefix);
  3358.     WriteSConnectionInfo(client,
  3359.                  (unsigned long)(connSetupPrefix.length << 2),
  3360.                  ConnectionInfo);
  3361.     }
  3362.     else
  3363.     {
  3364.     (void)WriteToClient(client, sizeof(xConnSetupPrefix),
  3365.                 (char *) &connSetupPrefix);
  3366.     (void)WriteToClient(client, (int)(connSetupPrefix.length << 2),
  3367.                 ConnectionInfo);
  3368.     }
  3369.     return (client->noClientException);
  3370. }
  3371.  
  3372. SendErrorToClient(client, majorCode, minorCode, resId, errorCode)
  3373.     ClientPtr client;
  3374.     unsigned majorCode;
  3375.     unsigned short minorCode;
  3376.     XID resId;
  3377.     int errorCode;
  3378. {
  3379.     xError rep;
  3380.  
  3381.     rep.type = X_Error;
  3382.     rep.sequenceNumber = client->sequence;
  3383.     rep.errorCode = errorCode;
  3384.     rep.majorCode = majorCode;
  3385.     rep.minorCode = minorCode;
  3386.     rep.resourceID = resId;
  3387.  
  3388.     WriteEventsToClient (client, 1, (xEvent *)&rep);
  3389. }
  3390.  
  3391. void
  3392. DeleteWindowFromAnySelections(pWin)
  3393.     WindowPtr pWin;
  3394. {
  3395.     register int i;
  3396.  
  3397.     for (i = 0; i< NumCurrentSelections; i++)
  3398.         if (CurrentSelections[i].pWin == pWin)
  3399.         {
  3400.             CurrentSelections[i].pWin = (WindowPtr)NULL;
  3401.             CurrentSelections[i].window = None;
  3402.         CurrentSelections[i].client = NullClient;
  3403.     }
  3404. }
  3405.  
  3406. static void
  3407. DeleteClientFromAnySelections(client)
  3408.     ClientPtr client;
  3409. {
  3410.     register int i;
  3411.  
  3412.     for (i = 0; i< NumCurrentSelections; i++)
  3413.         if (CurrentSelections[i].client == client)
  3414.         {
  3415.             CurrentSelections[i].pWin = (WindowPtr)NULL;
  3416.             CurrentSelections[i].window = None;
  3417.         CurrentSelections[i].client = NullClient;
  3418.     }
  3419. }
  3420.  
  3421. void
  3422. MarkClientException(client)
  3423.     ClientPtr client;
  3424. {
  3425.     client->noClientException = -1;
  3426. }
  3427.