gc.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. /***********************************************************
  2. Copyright 1987, 1998 The Open Group
  3. Permission to use, copy, modify, distribute, and sell this software and its
  4. documentation for any purpose is hereby granted without fee, provided that
  5. the above copyright notice appear in all copies and that both that
  6. copyright notice and this permission notice appear in supporting
  7. documentation.
  8. The above copyright notice and this permission notice shall be included in
  9. all copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  13. OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  14. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  15. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. Except as contained in this notice, the name of The Open Group shall not be
  17. used in advertising or otherwise to promote the sale, use or other dealings
  18. in this Software without prior written authorization from The Open Group.
  19. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
  20. All Rights Reserved
  21. Permission to use, copy, modify, and distribute this software and its
  22. documentation for any purpose and without fee is hereby granted,
  23. provided that the above copyright notice appear in all copies and that
  24. both that copyright notice and this permission notice appear in
  25. supporting documentation, and that the name of Digital not be
  26. used in advertising or publicity pertaining to distribution of the
  27. software without specific, written prior permission.
  28. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  29. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  30. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  31. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  32. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  33. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  34. SOFTWARE.
  35. ******************************************************************/
  36. #ifdef HAVE_DIX_CONFIG_H
  37. #include <dix-config.h>
  38. #endif
  39. #include <X11/X.h>
  40. #include <X11/Xmd.h>
  41. #include <X11/Xproto.h>
  42. #include "misc.h"
  43. #include "resource.h"
  44. #include "gcstruct.h"
  45. #include "pixmapstr.h"
  46. #include "dixfontstr.h"
  47. #include "scrnintstr.h"
  48. #include "region.h"
  49. #include "dix.h"
  50. #include <assert.h>
  51. extern XID clientErrorValue;
  52. extern FontPtr defaultFont;
  53. static Bool CreateDefaultTile(GCPtr pGC);
  54. static unsigned char DefaultDash[2] = { 4, 4 };
  55. _X_EXPORT void
  56. ValidateGC(DrawablePtr pDraw, GC * pGC)
  57. {
  58. (*pGC->funcs->ValidateGC) (pGC, pGC->stateChanges, pDraw);
  59. pGC->stateChanges = 0;
  60. pGC->serialNumber = pDraw->serialNumber;
  61. }
  62. /* dixChangeGC(client, pGC, mask, pC32, pUnion)
  63. *
  64. * This function was created as part of the Security extension
  65. * implementation. The client performing the gc change must be passed so
  66. * that access checks can be performed on any tiles, stipples, or fonts
  67. * that are specified. ddxen can call this too; they should normally
  68. * pass NullClient for the client since any access checking should have
  69. * already been done at a higher level.
  70. *
  71. * Since we had to create a new function anyway, we decided to change the
  72. * way the list of gc values is passed to eliminate the compiler warnings
  73. * caused by the DoChangeGC interface. You can pass the values via pC32
  74. * or pUnion, but not both; one of them must be NULL. If you don't need
  75. * to pass any pointers, you can use either one:
  76. *
  77. * example calling dixChangeGC using pC32 parameter
  78. *
  79. * CARD32 v[2];
  80. * v[0] = foreground;
  81. * v[1] = background;
  82. * dixChangeGC(client, pGC, GCForeground|GCBackground, v, NULL);
  83. *
  84. * example calling dixChangeGC using pUnion parameter;
  85. * same effect as above
  86. *
  87. * ChangeGCVal v[2];
  88. * v[0].val = foreground;
  89. * v[1].val = background;
  90. * dixChangeGC(client, pGC, GCForeground|GCBackground, NULL, v);
  91. *
  92. * However, if you need to pass a pointer to a pixmap or font, you MUST
  93. * use the pUnion parameter.
  94. *
  95. * example calling dixChangeGC passing pointers in the value list
  96. * v[1].ptr is a pointer to a pixmap
  97. *
  98. * ChangeGCVal v[2];
  99. * v[0].val = FillTiled;
  100. * v[1].ptr = pPixmap;
  101. * dixChangeGC(client, pGC, GCFillStyle|GCTile, NULL, v);
  102. *
  103. * Note: we could have gotten by with just the pUnion parameter, but on
  104. * 64 bit machines that would have forced us to copy the value list that
  105. * comes in the ChangeGC request.
  106. *
  107. * Ideally, we'd change all the DoChangeGC calls to dixChangeGC, but this
  108. * is far too many changes to consider at this time, so we've only
  109. * changed the ones that caused compiler warnings. New code should use
  110. * dixChangeGC.
  111. *
  112. * dpw
  113. */
  114. #define NEXTVAL(_type, _var) { \
  115. if (pC32) _var = (_type)*pC32++; \
  116. else { \
  117. _var = (_type)(pUnion->val); pUnion++; \
  118. } \
  119. }
  120. #define NEXT_PTR(_type, _var) { \
  121. assert(pUnion); _var = (_type)pUnion->ptr; pUnion++; }
  122. _X_EXPORT int
  123. dixChangeGC(ClientPtr client, register GC * pGC, register BITS32 mask,
  124. CARD32 *pC32, ChangeGCValPtr pUnion)
  125. {
  126. BITS32 index2;
  127. int error = 0;
  128. PixmapPtr pPixmap;
  129. BITS32 maskQ;
  130. assert((pC32 && !pUnion) || (!pC32 && pUnion));
  131. pGC->serialNumber |= GC_CHANGE_SERIAL_BIT;
  132. maskQ = mask; /* save these for when we walk the GCque */
  133. while (mask && !error) {
  134. index2 = (BITS32) lowbit(mask);
  135. mask &= ~index2;
  136. pGC->stateChanges |= index2;
  137. switch (index2) {
  138. case GCFunction:
  139. {
  140. CARD8 newalu;
  141. NEXTVAL(CARD8, newalu);
  142. if (newalu <= GXset)
  143. pGC->alu = newalu;
  144. else {
  145. clientErrorValue = newalu;
  146. error = BadValue;
  147. }
  148. break;
  149. }
  150. case GCPlaneMask:
  151. NEXTVAL(unsigned long, pGC->planemask);
  152. break;
  153. case GCForeground:
  154. NEXTVAL(unsigned long, pGC->fgPixel);
  155. /*
  156. * this is for CreateGC
  157. */
  158. if (!pGC->tileIsPixel && !pGC->tile.pixmap) {
  159. pGC->tileIsPixel = TRUE;
  160. pGC->tile.pixel = pGC->fgPixel;
  161. }
  162. break;
  163. case GCBackground:
  164. NEXTVAL(unsigned long, pGC->bgPixel);
  165. break;
  166. case GCLineWidth: /* ??? line width is a CARD16 */
  167. NEXTVAL(CARD16, pGC->lineWidth);
  168. break;
  169. case GCLineStyle:
  170. {
  171. unsigned int newlinestyle;
  172. NEXTVAL(unsigned int, newlinestyle);
  173. if (newlinestyle <= LineDoubleDash)
  174. pGC->lineStyle = newlinestyle;
  175. else {
  176. clientErrorValue = newlinestyle;
  177. error = BadValue;
  178. }
  179. break;
  180. }
  181. case GCCapStyle:
  182. {
  183. unsigned int newcapstyle;
  184. NEXTVAL(unsigned int, newcapstyle);
  185. if (newcapstyle <= CapProjecting)
  186. pGC->capStyle = newcapstyle;
  187. else {
  188. clientErrorValue = newcapstyle;
  189. error = BadValue;
  190. }
  191. break;
  192. }
  193. case GCJoinStyle:
  194. {
  195. unsigned int newjoinstyle;
  196. NEXTVAL(unsigned int, newjoinstyle);
  197. if (newjoinstyle <= JoinBevel)
  198. pGC->joinStyle = newjoinstyle;
  199. else {
  200. clientErrorValue = newjoinstyle;
  201. error = BadValue;
  202. }
  203. break;
  204. }
  205. case GCFillStyle:
  206. {
  207. unsigned int newfillstyle;
  208. NEXTVAL(unsigned int, newfillstyle);
  209. if (newfillstyle <= FillOpaqueStippled)
  210. pGC->fillStyle = newfillstyle;
  211. else {
  212. clientErrorValue = newfillstyle;
  213. error = BadValue;
  214. }
  215. break;
  216. }
  217. case GCFillRule:
  218. {
  219. unsigned int newfillrule;
  220. NEXTVAL(unsigned int, newfillrule);
  221. if (newfillrule <= WindingRule)
  222. pGC->fillRule = newfillrule;
  223. else {
  224. clientErrorValue = newfillrule;
  225. error = BadValue;
  226. }
  227. break;
  228. }
  229. case GCTile:
  230. {
  231. XID newpix = 0;
  232. if (pUnion) {
  233. NEXT_PTR(PixmapPtr, pPixmap);
  234. }
  235. else {
  236. NEXTVAL(XID, newpix);
  237. pPixmap = (PixmapPtr) SecurityLookupIDByType(client,
  238. newpix, RT_PIXMAP,
  239. SecurityReadAccess);
  240. }
  241. if (pPixmap) {
  242. if ((pPixmap->drawable.depth != pGC->depth) ||
  243. (pPixmap->drawable.pScreen != pGC->pScreen)) {
  244. error = BadMatch;
  245. }
  246. else {
  247. pPixmap->refcnt++;
  248. if (!pGC->tileIsPixel)
  249. (*pGC->pScreen->DestroyPixmap) (pGC->tile.pixmap);
  250. pGC->tileIsPixel = FALSE;
  251. pGC->tile.pixmap = pPixmap;
  252. }
  253. }
  254. else {
  255. clientErrorValue = newpix;
  256. error = BadPixmap;
  257. }
  258. break;
  259. }
  260. case GCStipple:
  261. {
  262. XID newstipple = 0;
  263. if (pUnion) {
  264. NEXT_PTR(PixmapPtr, pPixmap);
  265. }
  266. else {
  267. NEXTVAL(XID, newstipple)
  268. pPixmap = (PixmapPtr) SecurityLookupIDByType(client,
  269. newstipple,
  270. RT_PIXMAP,
  271. SecurityReadAccess);
  272. }
  273. if (pPixmap) {
  274. if ((pPixmap->drawable.depth != 1) ||
  275. (pPixmap->drawable.pScreen != pGC->pScreen)) {
  276. error = BadMatch;
  277. }
  278. else {
  279. pPixmap->refcnt++;
  280. if (pGC->stipple)
  281. (*pGC->pScreen->DestroyPixmap) (pGC->stipple);
  282. pGC->stipple = pPixmap;
  283. }
  284. }
  285. else {
  286. clientErrorValue = newstipple;
  287. error = BadPixmap;
  288. }
  289. break;
  290. }
  291. case GCTileStipXOrigin:
  292. NEXTVAL(INT16, pGC->patOrg.x);
  293. break;
  294. case GCTileStipYOrigin:
  295. NEXTVAL(INT16, pGC->patOrg.y);
  296. break;
  297. case GCFont:
  298. {
  299. FontPtr pFont;
  300. XID newfont = 0;
  301. if (pUnion) {
  302. NEXT_PTR(FontPtr, pFont);
  303. }
  304. else {
  305. NEXTVAL(XID, newfont)
  306. pFont = (FontPtr) SecurityLookupIDByType(client, newfont,
  307. RT_FONT,
  308. SecurityReadAccess);
  309. }
  310. if (pFont) {
  311. pFont->refcnt++;
  312. if (pGC->font)
  313. CloseFont(pGC->font, (Font) 0);
  314. pGC->font = pFont;
  315. }
  316. else {
  317. clientErrorValue = newfont;
  318. error = BadFont;
  319. }
  320. break;
  321. }
  322. case GCSubwindowMode:
  323. {
  324. unsigned int newclipmode;
  325. NEXTVAL(unsigned int, newclipmode);
  326. if (newclipmode <= IncludeInferiors)
  327. pGC->subWindowMode = newclipmode;
  328. else {
  329. clientErrorValue = newclipmode;
  330. error = BadValue;
  331. }
  332. break;
  333. }
  334. case GCGraphicsExposures:
  335. {
  336. unsigned int newge;
  337. NEXTVAL(unsigned int, newge);
  338. if (newge <= xTrue)
  339. pGC->graphicsExposures = newge;
  340. else {
  341. clientErrorValue = newge;
  342. error = BadValue;
  343. }
  344. break;
  345. }
  346. case GCClipXOrigin:
  347. NEXTVAL(INT16, pGC->clipOrg.x);
  348. break;
  349. case GCClipYOrigin:
  350. NEXTVAL(INT16, pGC->clipOrg.y);
  351. break;
  352. case GCClipMask:
  353. {
  354. Pixmap pid = 0;
  355. int clipType = 0;
  356. if (pUnion) {
  357. NEXT_PTR(PixmapPtr, pPixmap);
  358. }
  359. else {
  360. NEXTVAL(Pixmap, pid)
  361. if (pid == None) {
  362. clipType = CT_NONE;
  363. pPixmap = NullPixmap;
  364. }
  365. else
  366. pPixmap = (PixmapPtr) SecurityLookupIDByType(client,
  367. pid, RT_PIXMAP,
  368. SecurityReadAccess);
  369. }
  370. if (pPixmap) {
  371. if ((pPixmap->drawable.depth != 1) ||
  372. (pPixmap->drawable.pScreen != pGC->pScreen)) {
  373. error = BadMatch;
  374. }
  375. else {
  376. clipType = CT_PIXMAP;
  377. pPixmap->refcnt++;
  378. }
  379. }
  380. else if (!pUnion && (pid != None)) {
  381. clientErrorValue = pid;
  382. error = BadPixmap;
  383. }
  384. if (error == Success) {
  385. (*pGC->funcs->ChangeClip) (pGC, clipType, (pointer) pPixmap, 0);
  386. }
  387. break;
  388. }
  389. case GCDashOffset:
  390. NEXTVAL(INT16, pGC->dashOffset);
  391. break;
  392. case GCDashList:
  393. {
  394. CARD8 newdash;
  395. NEXTVAL(CARD8, newdash);
  396. if (newdash == 4) {
  397. if (pGC->dash != DefaultDash) {
  398. free(pGC->dash);
  399. pGC->numInDashList = 2;
  400. pGC->dash = DefaultDash;
  401. }
  402. }
  403. else if (newdash != 0) {
  404. unsigned char *dash;
  405. dash = malloc(2 * sizeof(unsigned char));
  406. if (dash) {
  407. if (pGC->dash != DefaultDash)
  408. free(pGC->dash);
  409. pGC->numInDashList = 2;
  410. pGC->dash = dash;
  411. dash[0] = newdash;
  412. dash[1] = newdash;
  413. }
  414. else
  415. error = BadAlloc;
  416. }
  417. else {
  418. clientErrorValue = newdash;
  419. error = BadValue;
  420. }
  421. break;
  422. }
  423. case GCArcMode:
  424. {
  425. unsigned int newarcmode;
  426. NEXTVAL(unsigned int, newarcmode);
  427. if (newarcmode <= ArcPieSlice)
  428. pGC->arcMode = newarcmode;
  429. else {
  430. clientErrorValue = newarcmode;
  431. error = BadValue;
  432. }
  433. break;
  434. }
  435. default:
  436. clientErrorValue = maskQ;
  437. error = BadValue;
  438. break;
  439. }
  440. } /* end while mask && !error */
  441. if (pGC->fillStyle == FillTiled && pGC->tileIsPixel) {
  442. if (!CreateDefaultTile(pGC)) {
  443. pGC->fillStyle = FillSolid;
  444. error = BadAlloc;
  445. }
  446. }
  447. (*pGC->funcs->ChangeGC) (pGC, maskQ);
  448. return error;
  449. }
  450. #undef NEXTVAL
  451. #undef NEXT_PTR
  452. /* Publically defined entry to ChangeGC. Just calls dixChangeGC and tells
  453. * it that all of the entries are constants or IDs */
  454. _X_EXPORT int
  455. ChangeGC(register GC * pGC, register BITS32 mask, XID *pval)
  456. {
  457. return (dixChangeGC(NullClient, pGC, mask, pval, NULL));
  458. }
  459. /* DoChangeGC(pGC, mask, pval, fPointer)
  460. mask is a set of bits indicating which values to change.
  461. pval contains an appropriate value for each mask.
  462. fPointer is true if the values for tiles, stipples, fonts or clipmasks
  463. are pointers instead of IDs. Note: if you are passing pointers you
  464. MUST declare the array of values as type pointer! Other data types
  465. may not be large enough to hold pointers on some machines. Yes,
  466. this means you have to cast to (XID *) when you pass the array to
  467. DoChangeGC. Similarly, if you are not passing pointers (fPointer = 0) you
  468. MUST declare the array as type XID (not unsigned long!), or again the wrong
  469. size data type may be used. To avoid this cruftiness, use dixChangeGC
  470. above.
  471. if there is an error, the value is marked as changed
  472. anyway, which is probably wrong, but infrequent.
  473. NOTE:
  474. all values sent over the protocol for ChangeGC requests are
  475. 32 bits long
  476. */
  477. _X_EXPORT int
  478. DoChangeGC(register GC * pGC, register BITS32 mask, XID *pval, int fPointer)
  479. {
  480. if (fPointer)
  481. /* XXX might be a problem on 64 bit big-endian servers */
  482. return dixChangeGC(NullClient, pGC, mask, NULL, (ChangeGCValPtr) pval);
  483. else
  484. return dixChangeGC(NullClient, pGC, mask, pval, NULL);
  485. }
  486. /* CreateGC(pDrawable, mask, pval, pStatus)
  487. creates a default GC for the given drawable, using mask to fill
  488. in any non-default values.
  489. Returns a pointer to the new GC on success, NULL otherwise.
  490. returns status of non-default fields in pStatus
  491. BUG:
  492. should check for failure to create default tile
  493. */
  494. static GCPtr
  495. AllocateGC(ScreenPtr pScreen)
  496. {
  497. GCPtr pGC;
  498. char *ptr;
  499. DevUnion *ppriv;
  500. unsigned *sizes;
  501. unsigned size;
  502. int i;
  503. pGC = malloc(pScreen->totalGCSize);
  504. if (pGC) {
  505. ppriv = (DevUnion *) (pGC + 1);
  506. pGC->devPrivates = ppriv;
  507. sizes = pScreen->GCPrivateSizes;
  508. ptr = (char *) (ppriv + pScreen->GCPrivateLen);
  509. for (i = pScreen->GCPrivateLen; --i >= 0; ppriv++, sizes++) {
  510. if ((size = *sizes)) {
  511. ppriv->ptr = (pointer) ptr;
  512. ptr += size;
  513. }
  514. else
  515. ppriv->ptr = (pointer) NULL;
  516. }
  517. }
  518. return pGC;
  519. }
  520. _X_EXPORT GCPtr
  521. CreateGC(DrawablePtr pDrawable, BITS32 mask, XID *pval, int *pStatus)
  522. {
  523. GCPtr pGC;
  524. pGC = AllocateGC(pDrawable->pScreen);
  525. if (!pGC) {
  526. *pStatus = BadAlloc;
  527. return (GCPtr) NULL;
  528. }
  529. pGC->pScreen = pDrawable->pScreen;
  530. pGC->depth = pDrawable->depth;
  531. pGC->alu = GXcopy; /* dst <- src */
  532. pGC->planemask = ~0;
  533. pGC->serialNumber = GC_CHANGE_SERIAL_BIT;
  534. pGC->funcs = 0;
  535. pGC->fgPixel = 0;
  536. pGC->bgPixel = 1;
  537. pGC->lineWidth = 0;
  538. pGC->lineStyle = LineSolid;
  539. pGC->capStyle = CapButt;
  540. pGC->joinStyle = JoinMiter;
  541. pGC->fillStyle = FillSolid;
  542. pGC->fillRule = EvenOddRule;
  543. pGC->arcMode = ArcPieSlice;
  544. if (mask & GCForeground) {
  545. /*
  546. * magic special case -- ChangeGC checks for this condition
  547. * and snags the Foreground value to create a pseudo default-tile
  548. */
  549. pGC->tileIsPixel = FALSE;
  550. pGC->tile.pixmap = NullPixmap;
  551. }
  552. else {
  553. pGC->tileIsPixel = TRUE;
  554. pGC->tile.pixel = 0;
  555. }
  556. pGC->patOrg.x = 0;
  557. pGC->patOrg.y = 0;
  558. pGC->subWindowMode = ClipByChildren;
  559. pGC->graphicsExposures = TRUE;
  560. pGC->clipOrg.x = 0;
  561. pGC->clipOrg.y = 0;
  562. pGC->clientClipType = CT_NONE;
  563. pGC->clientClip = (pointer) NULL;
  564. pGC->numInDashList = 2;
  565. pGC->dash = DefaultDash;
  566. pGC->dashOffset = 0;
  567. pGC->lastWinOrg.x = 0;
  568. pGC->lastWinOrg.y = 0;
  569. /* use the default font and stipple */
  570. pGC->font = defaultFont;
  571. defaultFont->refcnt++;
  572. pGC->stipple = pGC->pScreen->PixmapPerDepth[0];
  573. pGC->stipple->refcnt++;
  574. pGC->stateChanges = (1 << (GCLastBit + 1)) - 1;
  575. if (!(*pGC->pScreen->CreateGC) (pGC))
  576. *pStatus = BadAlloc;
  577. else if (mask)
  578. *pStatus = ChangeGC(pGC, mask, pval);
  579. else
  580. *pStatus = Success;
  581. if (*pStatus != Success) {
  582. if (!pGC->tileIsPixel && !pGC->tile.pixmap)
  583. pGC->tileIsPixel = TRUE; /* undo special case */
  584. FreeGC(pGC, (XID) 0);
  585. pGC = (GCPtr) NULL;
  586. }
  587. return (pGC);
  588. }
  589. static Bool
  590. CreateDefaultTile(GCPtr pGC)
  591. {
  592. XID tmpval[3];
  593. PixmapPtr pTile;
  594. GCPtr pgcScratch;
  595. xRectangle rect;
  596. CARD16 w, h;
  597. w = 1;
  598. h = 1;
  599. (*pGC->pScreen->QueryBestSize) (TileShape, &w, &h, pGC->pScreen);
  600. pTile = (PixmapPtr)
  601. (*pGC->pScreen->CreatePixmap) (pGC->pScreen, w, h, pGC->depth);
  602. pgcScratch = GetScratchGC(pGC->depth, pGC->pScreen);
  603. if (!pTile || !pgcScratch) {
  604. if (pTile)
  605. (*pTile->drawable.pScreen->DestroyPixmap) (pTile);
  606. if (pgcScratch)
  607. FreeScratchGC(pgcScratch);
  608. return FALSE;
  609. }
  610. tmpval[0] = GXcopy;
  611. tmpval[1] = pGC->tile.pixel;
  612. tmpval[2] = FillSolid;
  613. (void) ChangeGC(pgcScratch, GCFunction | GCForeground | GCFillStyle,
  614. tmpval);
  615. ValidateGC((DrawablePtr) pTile, pgcScratch);
  616. rect.x = 0;
  617. rect.y = 0;
  618. rect.width = w;
  619. rect.height = h;
  620. (*pgcScratch->ops->PolyFillRect) ((DrawablePtr) pTile, pgcScratch, 1,
  621. &rect);
  622. /* Always remember to free the scratch graphics context after use. */
  623. FreeScratchGC(pgcScratch);
  624. pGC->tileIsPixel = FALSE;
  625. pGC->tile.pixmap = pTile;
  626. return TRUE;
  627. }
  628. _X_EXPORT int
  629. CopyGC(register GC * pgcSrc, register GC * pgcDst, register BITS32 mask)
  630. {
  631. BITS32 index2;
  632. BITS32 maskQ;
  633. int error = 0;
  634. if (pgcSrc == pgcDst)
  635. return Success;
  636. pgcDst->serialNumber |= GC_CHANGE_SERIAL_BIT;
  637. pgcDst->stateChanges |= mask;
  638. maskQ = mask;
  639. while (mask) {
  640. index2 = (BITS32) lowbit(mask);
  641. mask &= ~index2;
  642. switch (index2) {
  643. case GCFunction:
  644. pgcDst->alu = pgcSrc->alu;
  645. break;
  646. case GCPlaneMask:
  647. pgcDst->planemask = pgcSrc->planemask;
  648. break;
  649. case GCForeground:
  650. pgcDst->fgPixel = pgcSrc->fgPixel;
  651. break;
  652. case GCBackground:
  653. pgcDst->bgPixel = pgcSrc->bgPixel;
  654. break;
  655. case GCLineWidth:
  656. pgcDst->lineWidth = pgcSrc->lineWidth;
  657. break;
  658. case GCLineStyle:
  659. pgcDst->lineStyle = pgcSrc->lineStyle;
  660. break;
  661. case GCCapStyle:
  662. pgcDst->capStyle = pgcSrc->capStyle;
  663. break;
  664. case GCJoinStyle:
  665. pgcDst->joinStyle = pgcSrc->joinStyle;
  666. break;
  667. case GCFillStyle:
  668. pgcDst->fillStyle = pgcSrc->fillStyle;
  669. break;
  670. case GCFillRule:
  671. pgcDst->fillRule = pgcSrc->fillRule;
  672. break;
  673. case GCTile:
  674. {
  675. if (EqualPixUnion(pgcDst->tileIsPixel,
  676. pgcDst->tile,
  677. pgcSrc->tileIsPixel, pgcSrc->tile)) {
  678. break;
  679. }
  680. if (!pgcDst->tileIsPixel)
  681. (*pgcDst->pScreen->DestroyPixmap) (pgcDst->tile.pixmap);
  682. pgcDst->tileIsPixel = pgcSrc->tileIsPixel;
  683. pgcDst->tile = pgcSrc->tile;
  684. if (!pgcDst->tileIsPixel)
  685. pgcDst->tile.pixmap->refcnt++;
  686. break;
  687. }
  688. case GCStipple:
  689. {
  690. if (pgcDst->stipple == pgcSrc->stipple)
  691. break;
  692. if (pgcDst->stipple)
  693. (*pgcDst->pScreen->DestroyPixmap) (pgcDst->stipple);
  694. pgcDst->stipple = pgcSrc->stipple;
  695. if (pgcDst->stipple)
  696. pgcDst->stipple->refcnt++;
  697. break;
  698. }
  699. case GCTileStipXOrigin:
  700. pgcDst->patOrg.x = pgcSrc->patOrg.x;
  701. break;
  702. case GCTileStipYOrigin:
  703. pgcDst->patOrg.y = pgcSrc->patOrg.y;
  704. break;
  705. case GCFont:
  706. if (pgcDst->font == pgcSrc->font)
  707. break;
  708. if (pgcDst->font)
  709. CloseFont(pgcDst->font, (Font) 0);
  710. if ((pgcDst->font = pgcSrc->font) != NullFont)
  711. (pgcDst->font)->refcnt++;
  712. break;
  713. case GCSubwindowMode:
  714. pgcDst->subWindowMode = pgcSrc->subWindowMode;
  715. break;
  716. case GCGraphicsExposures:
  717. pgcDst->graphicsExposures = pgcSrc->graphicsExposures;
  718. break;
  719. case GCClipXOrigin:
  720. pgcDst->clipOrg.x = pgcSrc->clipOrg.x;
  721. break;
  722. case GCClipYOrigin:
  723. pgcDst->clipOrg.y = pgcSrc->clipOrg.y;
  724. break;
  725. case GCClipMask:
  726. (*pgcDst->funcs->CopyClip) (pgcDst, pgcSrc);
  727. break;
  728. case GCDashOffset:
  729. pgcDst->dashOffset = pgcSrc->dashOffset;
  730. break;
  731. case GCDashList:
  732. if (pgcSrc->dash == DefaultDash) {
  733. if (pgcDst->dash != DefaultDash) {
  734. free(pgcDst->dash);
  735. pgcDst->numInDashList = pgcSrc->numInDashList;
  736. pgcDst->dash = pgcSrc->dash;
  737. }
  738. }
  739. else {
  740. unsigned char *dash;
  741. unsigned int i;
  742. dash = malloc(pgcSrc->numInDashList *
  743. sizeof(unsigned char));
  744. if (dash) {
  745. if (pgcDst->dash != DefaultDash)
  746. free(pgcDst->dash);
  747. pgcDst->numInDashList = pgcSrc->numInDashList;
  748. pgcDst->dash = dash;
  749. for (i = 0; i < pgcSrc->numInDashList; i++)
  750. dash[i] = pgcSrc->dash[i];
  751. }
  752. else
  753. error = BadAlloc;
  754. }
  755. break;
  756. case GCArcMode:
  757. pgcDst->arcMode = pgcSrc->arcMode;
  758. break;
  759. default:
  760. clientErrorValue = maskQ;
  761. error = BadValue;
  762. break;
  763. }
  764. }
  765. if (pgcDst->fillStyle == FillTiled && pgcDst->tileIsPixel) {
  766. if (!CreateDefaultTile(pgcDst)) {
  767. pgcDst->fillStyle = FillSolid;
  768. error = BadAlloc;
  769. }
  770. }
  771. (*pgcDst->funcs->CopyGC) (pgcSrc, maskQ, pgcDst);
  772. return error;
  773. }
  774. /**
  775. * does the diX part of freeing the characteristics in the GC.
  776. *
  777. * \param value must conform to DeleteType
  778. */
  779. _X_EXPORT int
  780. FreeGC(pointer value, XID gid)
  781. {
  782. GCPtr pGC = (GCPtr) value;
  783. CloseFont(pGC->font, (Font) 0);
  784. (*pGC->funcs->DestroyClip) (pGC);
  785. if (!pGC->tileIsPixel)
  786. (*pGC->pScreen->DestroyPixmap) (pGC->tile.pixmap);
  787. if (pGC->stipple)
  788. (*pGC->pScreen->DestroyPixmap) (pGC->stipple);
  789. (*pGC->funcs->DestroyGC) (pGC);
  790. if (pGC->dash != DefaultDash)
  791. free(pGC->dash);
  792. free(pGC);
  793. return (Success);
  794. }
  795. void
  796. SetGCMask(GCPtr pGC, Mask selectMask, Mask newDataMask)
  797. {
  798. pGC->stateChanges = (~selectMask & pGC->stateChanges) |
  799. (selectMask & newDataMask);
  800. if (selectMask & newDataMask)
  801. pGC->serialNumber |= GC_CHANGE_SERIAL_BIT;
  802. }
  803. /* CreateScratchGC(pScreen, depth)
  804. like CreateGC, but doesn't do the default tile or stipple,
  805. since we can't create them without already having a GC. any code
  806. using the tile or stipple has to set them explicitly anyway,
  807. since the state of the scratch gc is unknown. This is OK
  808. because ChangeGC() has to be able to deal with NULL tiles and
  809. stipples anyway (in case the CreateGC() call has provided a
  810. value for them -- we can't set the default tile until the
  811. client-supplied attributes are installed, since the fgPixel
  812. is what fills the default tile. (maybe this comment should
  813. go with CreateGC() or ChangeGC().)
  814. */
  815. _X_EXPORT GCPtr
  816. CreateScratchGC(ScreenPtr pScreen, unsigned depth)
  817. {
  818. GCPtr pGC;
  819. pGC = AllocateGC(pScreen);
  820. if (!pGC)
  821. return (GCPtr) NULL;
  822. pGC->pScreen = pScreen;
  823. pGC->depth = depth;
  824. pGC->alu = GXcopy; /* dst <- src */
  825. pGC->planemask = ~0;
  826. pGC->serialNumber = 0;
  827. pGC->fgPixel = 0;
  828. pGC->bgPixel = 1;
  829. pGC->lineWidth = 0;
  830. pGC->lineStyle = LineSolid;
  831. pGC->capStyle = CapButt;
  832. pGC->joinStyle = JoinMiter;
  833. pGC->fillStyle = FillSolid;
  834. pGC->fillRule = EvenOddRule;
  835. pGC->arcMode = ArcPieSlice;
  836. pGC->font = defaultFont;
  837. if (pGC->font) /* necessary, because open of default font could fail */
  838. pGC->font->refcnt++;
  839. pGC->tileIsPixel = TRUE;
  840. pGC->tile.pixel = 0;
  841. pGC->stipple = NullPixmap;
  842. pGC->patOrg.x = 0;
  843. pGC->patOrg.y = 0;
  844. pGC->subWindowMode = ClipByChildren;
  845. pGC->graphicsExposures = TRUE;
  846. pGC->clipOrg.x = 0;
  847. pGC->clipOrg.y = 0;
  848. pGC->clientClipType = CT_NONE;
  849. pGC->dashOffset = 0;
  850. pGC->numInDashList = 2;
  851. pGC->dash = DefaultDash;
  852. pGC->lastWinOrg.x = 0;
  853. pGC->lastWinOrg.y = 0;
  854. pGC->stateChanges = (1 << (GCLastBit + 1)) - 1;
  855. if (!(*pScreen->CreateGC) (pGC)) {
  856. FreeGC(pGC, (XID) 0);
  857. pGC = (GCPtr) NULL;
  858. }
  859. return pGC;
  860. }
  861. void
  862. FreeGCperDepth(int screenNum)
  863. {
  864. int i;
  865. ScreenPtr pScreen;
  866. GCPtr *ppGC;
  867. pScreen = screenInfo.screens[screenNum];
  868. ppGC = pScreen->GCperDepth;
  869. for (i = 0; i <= pScreen->numDepths; i++)
  870. (void) FreeGC(ppGC[i], (XID) 0);
  871. pScreen->rgf = ~0L;
  872. }
  873. Bool
  874. CreateGCperDepth(int screenNum)
  875. {
  876. int i;
  877. ScreenPtr pScreen;
  878. DepthPtr pDepth;
  879. GCPtr *ppGC;
  880. pScreen = screenInfo.screens[screenNum];
  881. pScreen->rgf = 0;
  882. ppGC = pScreen->GCperDepth;
  883. /* do depth 1 separately because it's not included in list */
  884. if (!(ppGC[0] = CreateScratchGC(pScreen, 1)))
  885. return FALSE;
  886. ppGC[0]->graphicsExposures = FALSE;
  887. /* Make sure we don't overflow GCperDepth[] */
  888. if (pScreen->numDepths > MAXFORMATS)
  889. return FALSE;
  890. pDepth = pScreen->allowedDepths;
  891. for (i = 0; i < pScreen->numDepths; i++, pDepth++) {
  892. if (!(ppGC[i + 1] = CreateScratchGC(pScreen, pDepth->depth))) {
  893. for (; i >= 0; i--)
  894. (void) FreeGC(ppGC[i], (XID) 0);
  895. return FALSE;
  896. }
  897. ppGC[i + 1]->graphicsExposures = FALSE;
  898. }
  899. return TRUE;
  900. }
  901. Bool
  902. CreateDefaultStipple(int screenNum)
  903. {
  904. ScreenPtr pScreen;
  905. XID tmpval[3];
  906. xRectangle rect;
  907. CARD16 w, h;
  908. GCPtr pgcScratch;
  909. pScreen = screenInfo.screens[screenNum];
  910. w = 16;
  911. h = 16;
  912. (*pScreen->QueryBestSize) (StippleShape, &w, &h, pScreen);
  913. if (!(pScreen->PixmapPerDepth[0] =
  914. (*pScreen->CreatePixmap) (pScreen, w, h, 1)))
  915. return FALSE;
  916. /* fill stipple with 1 */
  917. tmpval[0] = GXcopy;
  918. tmpval[1] = 1;
  919. tmpval[2] = FillSolid;
  920. pgcScratch = GetScratchGC(1, pScreen);
  921. if (!pgcScratch) {
  922. (*pScreen->DestroyPixmap) (pScreen->PixmapPerDepth[0]);
  923. return FALSE;
  924. }
  925. (void) ChangeGC(pgcScratch, GCFunction | GCForeground | GCFillStyle,
  926. tmpval);
  927. ValidateGC((DrawablePtr) pScreen->PixmapPerDepth[0], pgcScratch);
  928. rect.x = 0;
  929. rect.y = 0;
  930. rect.width = w;
  931. rect.height = h;
  932. (*pgcScratch->ops->PolyFillRect) ((DrawablePtr) pScreen->PixmapPerDepth[0],
  933. pgcScratch, 1, &rect);
  934. FreeScratchGC(pgcScratch);
  935. return TRUE;
  936. }
  937. void
  938. FreeDefaultStipple(int screenNum)
  939. {
  940. ScreenPtr pScreen = screenInfo.screens[screenNum];
  941. (*pScreen->DestroyPixmap) (pScreen->PixmapPerDepth[0]);
  942. }
  943. _X_EXPORT int
  944. SetDashes(register GCPtr pGC, unsigned offset, unsigned ndash,
  945. unsigned char *pdash)
  946. {
  947. long i;
  948. unsigned char *p, *indash;
  949. BITS32 maskQ = 0;
  950. i = ndash;
  951. p = pdash;
  952. while (i--) {
  953. if (!*p++) {
  954. /* dash segment must be > 0 */
  955. clientErrorValue = 0;
  956. return BadValue;
  957. }
  958. }
  959. if (ndash & 1)
  960. p = malloc(2 * ndash * sizeof(unsigned char));
  961. else
  962. p = malloc(ndash * sizeof(unsigned char));
  963. if (!p)
  964. return BadAlloc;
  965. pGC->serialNumber |= GC_CHANGE_SERIAL_BIT;
  966. if (offset != pGC->dashOffset) {
  967. pGC->dashOffset = offset;
  968. pGC->stateChanges |= GCDashOffset;
  969. maskQ |= GCDashOffset;
  970. }
  971. if (pGC->dash != DefaultDash)
  972. free(pGC->dash);
  973. pGC->numInDashList = ndash;
  974. pGC->dash = p;
  975. if (ndash & 1) {
  976. pGC->numInDashList += ndash;
  977. indash = pdash;
  978. i = ndash;
  979. while (i--)
  980. *p++ = *indash++;
  981. }
  982. while (ndash--)
  983. *p++ = *pdash++;
  984. pGC->stateChanges |= GCDashList;
  985. maskQ |= GCDashList;
  986. if (pGC->funcs->ChangeGC)
  987. (*pGC->funcs->ChangeGC) (pGC, maskQ);
  988. return Success;
  989. }
  990. _X_EXPORT int
  991. VerifyRectOrder(int nrects, xRectangle *prects, int ordering)
  992. {
  993. xRectangle *prectP, *prectN;
  994. int i;
  995. switch (ordering) {
  996. case Unsorted:
  997. return CT_UNSORTED;
  998. case YSorted:
  999. if (nrects > 1) {
  1000. for (i = 1, prectP = prects, prectN = prects + 1;
  1001. i < nrects; i++, prectP++, prectN++)
  1002. if (prectN->y < prectP->y)
  1003. return -1;
  1004. }
  1005. return CT_YSORTED;
  1006. case YXSorted:
  1007. if (nrects > 1) {
  1008. for (i = 1, prectP = prects, prectN = prects + 1;
  1009. i < nrects; i++, prectP++, prectN++)
  1010. if ((prectN->y < prectP->y) ||
  1011. ((prectN->y == prectP->y) && (prectN->x < prectP->x)))
  1012. return -1;
  1013. }
  1014. return CT_YXSORTED;
  1015. case YXBanded:
  1016. if (nrects > 1) {
  1017. for (i = 1, prectP = prects, prectN = prects + 1;
  1018. i < nrects; i++, prectP++, prectN++)
  1019. if ((prectN->y != prectP->y &&
  1020. prectN->y < prectP->y + (int) prectP->height) ||
  1021. ((prectN->y == prectP->y) &&
  1022. (prectN->height != prectP->height ||
  1023. prectN->x < prectP->x + (int) prectP->width)))
  1024. return -1;
  1025. }
  1026. return CT_YXBANDED;
  1027. }
  1028. return -1;
  1029. }
  1030. _X_EXPORT int
  1031. SetClipRects(GCPtr pGC, int xOrigin, int yOrigin, int nrects,
  1032. xRectangle *prects, int ordering)
  1033. {
  1034. int newct, size;
  1035. xRectangle *prectsNew;
  1036. newct = VerifyRectOrder(nrects, prects, ordering);
  1037. if (newct < 0)
  1038. return (BadMatch);
  1039. size = nrects * sizeof(xRectangle);
  1040. prectsNew = malloc(size);
  1041. if (!prectsNew && size)
  1042. return BadAlloc;
  1043. pGC->serialNumber |= GC_CHANGE_SERIAL_BIT;
  1044. pGC->clipOrg.x = xOrigin;
  1045. pGC->stateChanges |= GCClipXOrigin;
  1046. pGC->clipOrg.y = yOrigin;
  1047. pGC->stateChanges |= GCClipYOrigin;
  1048. if (size)
  1049. memmove((char *) prectsNew, (char *) prects, size);
  1050. (*pGC->funcs->ChangeClip) (pGC, newct, (pointer) prectsNew, nrects);
  1051. if (pGC->funcs->ChangeGC)
  1052. (*pGC->funcs->ChangeGC) (pGC,
  1053. GCClipXOrigin | GCClipYOrigin | GCClipMask);
  1054. return Success;
  1055. }
  1056. /*
  1057. sets reasonable defaults
  1058. if we can get a pre-allocated one, use it and mark it as used.
  1059. if we can't, create one out of whole cloth (The Velveteen GC -- if
  1060. you use it often enough it will become real.)
  1061. */
  1062. _X_EXPORT GCPtr
  1063. GetScratchGC(register unsigned depth, register ScreenPtr pScreen)
  1064. {
  1065. int i;
  1066. GCPtr pGC;
  1067. for (i = 0; i <= pScreen->numDepths; i++)
  1068. if (pScreen->GCperDepth[i]->depth == depth &&
  1069. !(pScreen->rgf & (1L << (i + 1)))
  1070. ) {
  1071. pScreen->rgf |= (1L << (i + 1));
  1072. pGC = (pScreen->GCperDepth[i]);
  1073. pGC->alu = GXcopy;
  1074. pGC->planemask = ~0;
  1075. pGC->serialNumber = 0;
  1076. pGC->fgPixel = 0;
  1077. pGC->bgPixel = 1;
  1078. pGC->lineWidth = 0;
  1079. pGC->lineStyle = LineSolid;
  1080. pGC->capStyle = CapButt;
  1081. pGC->joinStyle = JoinMiter;
  1082. pGC->fillStyle = FillSolid;
  1083. pGC->fillRule = EvenOddRule;
  1084. pGC->arcMode = ArcChord;
  1085. pGC->patOrg.x = 0;
  1086. pGC->patOrg.y = 0;
  1087. pGC->subWindowMode = ClipByChildren;
  1088. pGC->graphicsExposures = FALSE;
  1089. pGC->clipOrg.x = 0;
  1090. pGC->clipOrg.y = 0;
  1091. if (pGC->clientClipType != CT_NONE)
  1092. (*pGC->funcs->ChangeClip) (pGC, CT_NONE, NULL, 0);
  1093. pGC->stateChanges = (1 << (GCLastBit + 1)) - 1;
  1094. return pGC;
  1095. }
  1096. /* if we make it this far, need to roll our own */
  1097. pGC = CreateScratchGC(pScreen, depth);
  1098. if (pGC)
  1099. pGC->graphicsExposures = FALSE;
  1100. return pGC;
  1101. }
  1102. /*
  1103. if the gc to free is in the table of pre-existing ones,
  1104. mark it as available.
  1105. if not, free it for real
  1106. */
  1107. _X_EXPORT void
  1108. FreeScratchGC(register GCPtr pGC)
  1109. {
  1110. ScreenPtr pScreen = pGC->pScreen;
  1111. int i;
  1112. for (i = 0; i <= pScreen->numDepths; i++) {
  1113. if (pScreen->GCperDepth[i] == pGC) {
  1114. pScreen->rgf &= ~(1L << (i + 1));
  1115. return;
  1116. }
  1117. }
  1118. (void) FreeGC(pGC, (GContext) 0);
  1119. }