home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / server / dix / cursor.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-14  |  8.8 KB  |  369 lines

  1. /***********************************************************
  2. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
  3. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Digital or MIT not be
  12. used in advertising or publicity pertaining to distribution of the
  13. software without specific, written prior permission.  
  14.  
  15. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  16. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  17. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  18. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  19. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  20. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  21. SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25.  
  26. /* $XConsortium: cursor.c,v 1.40 91/02/14 19:35:34 keith Exp $ */
  27.  
  28. #include "X.h"
  29. #include "Xmd.h"
  30. #include "servermd.h"
  31. #include "scrnintstr.h"
  32. #include "dixstruct.h"
  33. #include "cursorstr.h"
  34. #include "dixfontstr.h"
  35. #include "opaque.h"
  36.  
  37. typedef struct _GlyphShare {
  38.     FontPtr font;
  39.     unsigned short sourceChar;
  40.     unsigned short maskChar;
  41.     CursorBitsPtr bits;
  42.     struct _GlyphShare *next;
  43. } GlyphShare, *GlyphSharePtr;
  44.  
  45. static GlyphSharePtr sharedGlyphs = (GlyphSharePtr)NULL;
  46.  
  47. static void
  48. FreeCursorBits(bits)
  49.     CursorBitsPtr bits;
  50. {
  51.     if (--bits->refcnt > 0)
  52.     return;
  53.     xfree(bits->source);
  54.     xfree(bits->mask);
  55.     if (bits->refcnt == 0)
  56.     {
  57.     register GlyphSharePtr *prev, this;
  58.  
  59.     for (prev = &sharedGlyphs;
  60.          (this = *prev) && (this->bits != bits);
  61.          prev = &this->next)
  62.         ;
  63.     if (this)
  64.     {
  65.         *prev = this->next;
  66.         CloseFont(this->font, (Font)0);
  67.         xfree(this);
  68.     }
  69.     xfree(bits);
  70.     }
  71. }
  72.  
  73. /*
  74.  * To be called indirectly by DeleteResource; must use exactly two args
  75.  */
  76. /*ARGSUSED*/
  77. int
  78. FreeCursor( pCurs, cid)
  79.     CursorPtr     pCurs;
  80.     Cursor     cid;    
  81. {
  82.     int        nscr;
  83.  
  84.     ScreenPtr    pscr;
  85.  
  86.     if ( --pCurs->refcnt > 0)
  87.     return(Success);
  88.  
  89.     for (nscr = 0; nscr < screenInfo.numScreens; nscr++)
  90.     {
  91.     pscr = screenInfo.screens[nscr];
  92.     (void)( *pscr->UnrealizeCursor)( pscr, pCurs);
  93.     }
  94.     FreeCursorBits(pCurs->bits);
  95.     xfree( pCurs);
  96.     return(Success);
  97. }
  98.  
  99. /*
  100.  * does nothing about the resource table, just creates the data structure.
  101.  * does not copy the src and mask bits
  102.  */
  103. CursorPtr 
  104. AllocCursor(psrcbits, pmaskbits, cm,
  105.         foreRed, foreGreen, foreBlue, backRed, backGreen, backBlue)
  106.     unsigned char *    psrcbits;        /* server-defined padding */
  107.     unsigned char *    pmaskbits;        /* server-defined padding */
  108.     CursorMetricPtr    cm;
  109.     unsigned        foreRed, foreGreen, foreBlue;
  110.     unsigned        backRed, backGreen, backBlue;
  111. {
  112.     CursorBitsPtr  bits;
  113.     CursorPtr     pCurs;
  114.     int        nscr;
  115.     ScreenPtr     pscr;
  116.  
  117.     pCurs = (CursorPtr)xalloc(sizeof(CursorRec) + sizeof(CursorBits));
  118.     if (!pCurs)
  119.     return (CursorPtr)NULL;
  120.     bits = (CursorBitsPtr)((char *)pCurs + sizeof(CursorRec));
  121.     bits->source = psrcbits;
  122.     bits->mask = pmaskbits;
  123.     bits->width = cm->width;
  124.     bits->height = cm->height;
  125.     bits->xhot = cm->xhot;
  126.     bits->yhot = cm->yhot;
  127.     bits->refcnt = -1;
  128.  
  129.     pCurs->bits = bits;
  130.     pCurs->refcnt = 1;        
  131.  
  132.     pCurs->foreRed = foreRed;
  133.     pCurs->foreGreen = foreGreen;
  134.     pCurs->foreBlue = foreBlue;
  135.  
  136.     pCurs->backRed = backRed;
  137.     pCurs->backGreen = backGreen;
  138.     pCurs->backBlue = backBlue;
  139.  
  140.     /*
  141.      * realize the cursor for every screen
  142.      */
  143.     for (nscr = 0; nscr < screenInfo.numScreens; nscr++)
  144.     {
  145.     pscr = screenInfo.screens[nscr];
  146.         if (!( *pscr->RealizeCursor)( pscr, pCurs))
  147.     {
  148.         while (--nscr >= 0)
  149.         {
  150.         pscr = screenInfo.screens[nscr];
  151.         ( *pscr->UnrealizeCursor)( pscr, pCurs);
  152.         }
  153.         xfree(pCurs);
  154.         return (CursorPtr)NULL;
  155.     }
  156.     }
  157.     return pCurs;
  158. }
  159.  
  160. int
  161. AllocGlyphCursor(source, sourceChar, mask, maskChar,
  162.          foreRed, foreGreen, foreBlue, backRed, backGreen, backBlue,
  163.          ppCurs, client)
  164.     Font source, mask;
  165.     unsigned short sourceChar, maskChar;
  166.     unsigned foreRed, foreGreen, foreBlue;
  167.     unsigned backRed, backGreen, backBlue;
  168.     CursorPtr *ppCurs;
  169.     ClientPtr client;
  170. {
  171.     FontPtr  sourcefont, maskfont;
  172.     unsigned char   *srcbits;
  173.     unsigned char   *mskbits;
  174.     CursorMetricRec cm;
  175.     int res;
  176.     CursorBitsPtr  bits;
  177.     CursorPtr     pCurs;
  178.     int        nscr;
  179.     ScreenPtr     pscr;
  180.     GlyphSharePtr pShare;
  181.  
  182.     sourcefont = (FontPtr) LookupIDByType(source, RT_FONT);
  183.     maskfont = (FontPtr) LookupIDByType(mask, RT_FONT);
  184.  
  185.     if (!sourcefont)
  186.     {
  187.     client->errorValue = source;
  188.     return(BadFont);
  189.     }
  190.     if (!maskfont && (mask != None))
  191.     {
  192.     client->errorValue = mask;
  193.     return(BadFont);
  194.     }
  195.     if (sourcefont != maskfont)
  196.     pShare = (GlyphSharePtr)NULL;
  197.     else
  198.     {
  199.     for (pShare = sharedGlyphs;
  200.          pShare &&
  201.          ((pShare->font != sourcefont) ||
  202.           (pShare->sourceChar != sourceChar) ||
  203.           (pShare->maskChar != maskChar));
  204.          pShare = pShare->next)
  205.         ;
  206.     }
  207.     if (pShare)
  208.     {
  209.     pCurs = (CursorPtr)xalloc(sizeof(CursorRec));
  210.     if (!pCurs)
  211.         return BadAlloc;
  212.     bits = pShare->bits;
  213.     bits->refcnt++;
  214.     }
  215.     else
  216.     {
  217.     if (!CursorMetricsFromGlyph(sourcefont, sourceChar, &cm))
  218.     {
  219.         client->errorValue = sourceChar;
  220.         return BadValue;
  221.     }
  222.     if (!maskfont)
  223.     {
  224.         register long n;
  225.         register unsigned char *bits;
  226.  
  227.         n = PixmapBytePad(cm.width, 1)*(long)cm.height;
  228.         bits = mskbits = (unsigned char *)xalloc(n);
  229.         if (!bits)
  230.         return BadAlloc;
  231.         while (--n >= 0)
  232.         *bits++ = ~0;
  233.     }
  234.     else
  235.     {
  236.         if (!CursorMetricsFromGlyph(maskfont, maskChar, &cm))
  237.         {
  238.         client->errorValue = maskChar;
  239.         return BadValue;
  240.         }
  241.         if (res = ServerBitsFromGlyph(maskfont, maskChar, &cm, &mskbits))
  242.         return res;
  243.     }
  244.     if (res = ServerBitsFromGlyph(sourcefont, sourceChar, &cm, &srcbits))
  245.     {
  246.         xfree(mskbits);
  247.         return res;
  248.     }
  249.     if (sourcefont != maskfont)
  250.     {
  251.         pCurs = (CursorPtr)xalloc(sizeof(CursorRec) + sizeof(CursorBits));
  252.         if (pCurs)
  253.         bits = (CursorBitsPtr)((char *)pCurs + sizeof(CursorRec));
  254.         else
  255.         bits = (CursorBitsPtr)NULL;
  256.     }
  257.     else
  258.     {
  259.         pCurs = (CursorPtr)xalloc(sizeof(CursorRec));
  260.         if (pCurs)
  261.         bits = (CursorBitsPtr)xalloc(sizeof(CursorBits));
  262.         else
  263.         bits = (CursorBitsPtr)NULL;
  264.     }
  265.     if (!bits)
  266.     {
  267.         xfree(pCurs);
  268.         xfree(mskbits);
  269.         xfree(srcbits);
  270.         return BadAlloc;
  271.     }
  272.     bits->source = srcbits;
  273.     bits->mask = mskbits;
  274.     bits->width = cm.width;
  275.     bits->height = cm.height;
  276.     bits->xhot = cm.xhot;
  277.     bits->yhot = cm.yhot;
  278.     if (sourcefont != maskfont)
  279.         bits->refcnt = -1;
  280.     else
  281.     {
  282.         bits->refcnt = 1;
  283.         pShare = (GlyphSharePtr)xalloc(sizeof(GlyphShare));
  284.         if (!pShare)
  285.         {
  286.         FreeCursorBits(bits);
  287.         return BadAlloc;
  288.         }
  289.         pShare->font = sourcefont;
  290.         sourcefont->refcnt++;
  291.         pShare->sourceChar = sourceChar;
  292.         pShare->maskChar = maskChar;
  293.         pShare->bits = bits;
  294.         pShare->next = sharedGlyphs;
  295.         sharedGlyphs = pShare;
  296.     }
  297.     }
  298.     pCurs->bits = bits;
  299.     pCurs->refcnt = 1;
  300.  
  301.     pCurs->foreRed = foreRed;
  302.     pCurs->foreGreen = foreGreen;
  303.     pCurs->foreBlue = foreBlue;
  304.  
  305.     pCurs->backRed = backRed;
  306.     pCurs->backGreen = backGreen;
  307.     pCurs->backBlue = backBlue;
  308.  
  309.     /*
  310.      * realize the cursor for every screen
  311.      */
  312.     for (nscr = 0; nscr < screenInfo.numScreens; nscr++)
  313.     {
  314.     pscr = screenInfo.screens[nscr];
  315.         if (!( *pscr->RealizeCursor)( pscr, pCurs))
  316.     {
  317.         while (--nscr >= 0)
  318.         {
  319.         pscr = screenInfo.screens[nscr];
  320.         ( *pscr->UnrealizeCursor)( pscr, pCurs);
  321.         }
  322.         FreeCursorBits(pCurs->bits);
  323.         xfree(pCurs);
  324.         return BadAlloc;
  325.     }
  326.     }
  327.     *ppCurs = pCurs;
  328.     return Success;
  329. }
  330.  
  331. /***********************************************************
  332.  * CreateRootCursor
  333.  *
  334.  * look up the name of a font
  335.  * open the font
  336.  * add the font to the resource table
  337.  * make a cursor from the glyphs
  338.  * add the cursor to the resource table
  339.  *************************************************************/
  340.  
  341. CursorPtr 
  342. CreateRootCursor(pfilename, glyph)
  343.     char *        pfilename;
  344.     unsigned short    glyph;
  345. {
  346.     CursorPtr     curs;
  347.     FontPtr     cursorfont;
  348.     int    err;
  349.     XID        fontID;
  350.  
  351.     fontID = FakeClientID(0);
  352.     err = OpenFont(serverClient, fontID, FontLoadAll | FontOpenSync,
  353.     (unsigned)strlen( pfilename), pfilename);
  354.     if (err != Success)
  355.     return NullCursor;
  356.  
  357.     cursorfont = (FontPtr)LookupIDByType(fontID, RT_FONT);
  358.     if (!cursorfont)
  359.     return NullCursor;
  360.     if (AllocGlyphCursor(fontID, glyph, fontID, glyph + 1,
  361.              0, 0, 0, ~0, ~0, ~0, &curs, serverClient) != Success)
  362.     return NullCursor;
  363.  
  364.     if (!AddResource(FakeClientID(0), RT_CURSOR, (pointer)curs))
  365.     return NullCursor;
  366.  
  367.     return curs;
  368. }
  369.