shape.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  1. /************************************************************
  2. Copyright 1989, 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. ********************************************************/
  20. #ifdef HAVE_DIX_CONFIG_H
  21. #include <dix-config.h>
  22. #endif
  23. #include <stdlib.h>
  24. #include <X11/X.h>
  25. #include <X11/Xproto.h>
  26. #include "misc.h"
  27. #include "os.h"
  28. #include "windowstr.h"
  29. #include "scrnintstr.h"
  30. #include "pixmapstr.h"
  31. #include "extnsionst.h"
  32. #include "dixstruct.h"
  33. #include "resource.h"
  34. #include "opaque.h"
  35. #define _SHAPE_SERVER_ /* don't want Xlib structures */
  36. #include <X11/extensions/shapeproto.h>
  37. #include "regionstr.h"
  38. #include "gcstruct.h"
  39. #include "extinit.h"
  40. typedef RegionPtr (*CreateDftPtr) (WindowPtr /* pWin */
  41. );
  42. static int ShapeFreeClient(pointer /* data */ ,
  43. XID /* id */
  44. );
  45. static int ShapeFreeEvents(pointer /* data */ ,
  46. XID /* id */
  47. );
  48. static void ShapeResetProc(ExtensionEntry * /* extEntry */
  49. );
  50. static void SShapeNotifyEvent(xShapeNotifyEvent * /* from */ ,
  51. xShapeNotifyEvent * /* to */
  52. );
  53. static int
  54. RegionOperate(ClientPtr /* client */ ,
  55. WindowPtr /* pWin */ ,
  56. int /* kind */ ,
  57. RegionPtr * /* destRgnp */ ,
  58. RegionPtr /* srcRgn */ ,
  59. int /* op */ ,
  60. int /* xoff */ ,
  61. int /* yoff */ ,
  62. CreateDftPtr /* create */
  63. );
  64. /* SendShapeNotify, CreateBoundingShape and CreateClipShape are used
  65. * externally by the Xfixes extension and are now defined in window.h
  66. */
  67. static DISPATCH_PROC(ProcShapeCombine);
  68. static DISPATCH_PROC(ProcShapeDispatch);
  69. static DISPATCH_PROC(ProcShapeGetRectangles);
  70. static DISPATCH_PROC(ProcShapeInputSelected);
  71. static DISPATCH_PROC(ProcShapeMask);
  72. static DISPATCH_PROC(ProcShapeOffset);
  73. static DISPATCH_PROC(ProcShapeQueryExtents);
  74. static DISPATCH_PROC(ProcShapeQueryVersion);
  75. static DISPATCH_PROC(ProcShapeRectangles);
  76. static DISPATCH_PROC(ProcShapeSelectInput);
  77. static DISPATCH_PROC(SProcShapeCombine);
  78. static DISPATCH_PROC(SProcShapeDispatch);
  79. static DISPATCH_PROC(SProcShapeGetRectangles);
  80. static DISPATCH_PROC(SProcShapeInputSelected);
  81. static DISPATCH_PROC(SProcShapeMask);
  82. static DISPATCH_PROC(SProcShapeOffset);
  83. static DISPATCH_PROC(SProcShapeQueryExtents);
  84. static DISPATCH_PROC(SProcShapeQueryVersion);
  85. static DISPATCH_PROC(SProcShapeRectangles);
  86. static DISPATCH_PROC(SProcShapeSelectInput);
  87. #if 0
  88. static unsigned char ShapeReqCode = 0;
  89. #endif
  90. static int ShapeEventBase = 0;
  91. static RESTYPE ClientType, EventType; /* resource types for event masks */
  92. /*
  93. * each window has a list of clients requesting
  94. * ShapeNotify events. Each client has a resource
  95. * for each window it selects ShapeNotify input for,
  96. * this resource is used to delete the ShapeNotifyRec
  97. * entry from the per-window queue.
  98. */
  99. typedef struct _ShapeEvent *ShapeEventPtr;
  100. typedef struct _ShapeEvent {
  101. ShapeEventPtr next;
  102. ClientPtr client;
  103. WindowPtr window;
  104. XID clientResource;
  105. } ShapeEventRec;
  106. /****************
  107. * ShapeExtensionInit
  108. *
  109. * Called from InitExtensions in main() or from QueryExtension() if the
  110. * extension is dynamically loaded.
  111. *
  112. ****************/
  113. void
  114. ShapeExtensionInit(void)
  115. {
  116. ExtensionEntry *extEntry;
  117. ClientType = CreateNewResourceType(ShapeFreeClient);
  118. EventType = CreateNewResourceType(ShapeFreeEvents);
  119. if (ClientType && EventType &&
  120. (extEntry = AddExtension(SHAPENAME, ShapeNumberEvents, 0,
  121. ProcShapeDispatch, SProcShapeDispatch,
  122. ShapeResetProc, StandardMinorOpcode))) {
  123. #if 0
  124. ShapeReqCode = (unsigned char) extEntry->base;
  125. #endif
  126. ShapeEventBase = extEntry->eventBase;
  127. EventSwapVector[ShapeEventBase] = (EventSwapPtr) SShapeNotifyEvent;
  128. }
  129. }
  130. /*ARGSUSED*/ static void
  131. ShapeResetProc(extEntry)
  132. ExtensionEntry *extEntry;
  133. {
  134. }
  135. static int
  136. RegionOperate(client, pWin, kind, destRgnp, srcRgn, op, xoff, yoff, create)
  137. ClientPtr client;
  138. WindowPtr pWin;
  139. int kind;
  140. RegionPtr *destRgnp, srcRgn;
  141. int op;
  142. int xoff, yoff;
  143. CreateDftPtr create; /* creates a reasonable *destRgnp */
  144. {
  145. ScreenPtr pScreen = pWin->drawable.pScreen;
  146. if (srcRgn && (xoff || yoff))
  147. REGION_TRANSLATE(srcRgn, xoff, yoff);
  148. if (!pWin->parent) {
  149. if (srcRgn)
  150. REGION_DESTROY(srcRgn);
  151. return Success;
  152. }
  153. /* May/30/2001:
  154. * The shape.PS specs say if src is None, existing shape is to be
  155. * removed (and so the op-code has no meaning in such removal);
  156. * see shape.PS, page 3, ShapeMask.
  157. */
  158. if (srcRgn == NULL) {
  159. if (*destRgnp != NULL) {
  160. REGION_DESTROY(*destRgnp);
  161. *destRgnp = 0;
  162. /* go on to remove shape and generate ShapeNotify */
  163. }
  164. else {
  165. /* May/30/2001:
  166. * The target currently has no shape in effect, so nothing to
  167. * do here. The specs say that ShapeNotify is generated whenever
  168. * the client region is "modified"; since no modification is done
  169. * here, we do not generate that event. The specs does not say
  170. * "it is an error to request removal when there is no shape in
  171. * effect", so we return good status.
  172. */
  173. return Success;
  174. }
  175. }
  176. else
  177. switch (op) {
  178. case ShapeSet:
  179. if (*destRgnp)
  180. REGION_DESTROY(*destRgnp);
  181. *destRgnp = srcRgn;
  182. srcRgn = 0;
  183. break;
  184. case ShapeUnion:
  185. if (*destRgnp)
  186. REGION_UNION(*destRgnp, *destRgnp, srcRgn);
  187. break;
  188. case ShapeIntersect:
  189. if (*destRgnp)
  190. REGION_INTERSECT(*destRgnp, *destRgnp, srcRgn);
  191. else {
  192. *destRgnp = srcRgn;
  193. srcRgn = 0;
  194. }
  195. break;
  196. case ShapeSubtract:
  197. if (!*destRgnp)
  198. *destRgnp = (*create) (pWin);
  199. REGION_SUBTRACT(*destRgnp, *destRgnp, srcRgn);
  200. break;
  201. case ShapeInvert:
  202. if (!*destRgnp)
  203. *destRgnp = REGION_CREATE((BoxPtr) 0, 0);
  204. else
  205. REGION_SUBTRACT(*destRgnp, srcRgn, *destRgnp);
  206. break;
  207. default:
  208. client->errorValue = op;
  209. return BadValue;
  210. }
  211. if (srcRgn)
  212. REGION_DESTROY(srcRgn);
  213. (*pScreen->SetShape) (pWin);
  214. SendShapeNotify(pWin, kind);
  215. return Success;
  216. }
  217. RegionPtr
  218. CreateBoundingShape(pWin)
  219. WindowPtr pWin;
  220. {
  221. BoxRec extents;
  222. extents.x1 = -wBorderWidth(pWin);
  223. extents.y1 = -wBorderWidth(pWin);
  224. extents.x2 = pWin->drawable.width + wBorderWidth(pWin);
  225. extents.y2 = pWin->drawable.height + wBorderWidth(pWin);
  226. return REGION_CREATE(&extents, 1);
  227. }
  228. RegionPtr
  229. CreateClipShape(pWin)
  230. WindowPtr pWin;
  231. {
  232. BoxRec extents;
  233. extents.x1 = 0;
  234. extents.y1 = 0;
  235. extents.x2 = pWin->drawable.width;
  236. extents.y2 = pWin->drawable.height;
  237. return REGION_CREATE(&extents, 1);
  238. }
  239. static int
  240. ProcShapeQueryVersion(client)
  241. ClientPtr client;
  242. {
  243. xShapeQueryVersionReply rep;
  244. REQUEST_SIZE_MATCH(xShapeQueryVersionReq);
  245. memset(&rep, 0, sizeof(xShapeQueryVersionReply));
  246. rep.type = X_Reply;
  247. rep.length = 0;
  248. rep.sequenceNumber = client->sequence;
  249. rep.majorVersion = SHAPE_MAJOR_VERSION;
  250. rep.minorVersion = SHAPE_MINOR_VERSION;
  251. if (client->swapped) {
  252. swaps(&rep.sequenceNumber);
  253. swapl(&rep.length);
  254. swaps(&rep.majorVersion);
  255. swaps(&rep.minorVersion);
  256. }
  257. WriteToClient(client, sizeof(xShapeQueryVersionReply), (char *) &rep);
  258. return (client->noClientException);
  259. }
  260. /*****************
  261. * ProcShapeRectangles
  262. *
  263. *****************/
  264. static int
  265. ProcShapeRectangles(client)
  266. ClientPtr client;
  267. {
  268. WindowPtr pWin;
  269. REQUEST(xShapeRectanglesReq);
  270. xRectangle *prects;
  271. int nrects, ctype;
  272. RegionPtr srcRgn;
  273. RegionPtr *destRgn;
  274. CreateDftPtr createDefault;
  275. REQUEST_AT_LEAST_SIZE(xShapeRectanglesReq);
  276. UpdateCurrentTime();
  277. pWin = LookupWindow(stuff->dest, client);
  278. if (!pWin)
  279. return BadWindow;
  280. switch (stuff->destKind) {
  281. case ShapeBounding:
  282. createDefault = CreateBoundingShape;
  283. break;
  284. case ShapeClip:
  285. createDefault = CreateClipShape;
  286. break;
  287. case ShapeInput:
  288. createDefault = CreateBoundingShape;
  289. break;
  290. default:
  291. client->errorValue = stuff->destKind;
  292. return BadValue;
  293. }
  294. if ((stuff->ordering != Unsorted) && (stuff->ordering != YSorted) &&
  295. (stuff->ordering != YXSorted) && (stuff->ordering != YXBanded)) {
  296. client->errorValue = stuff->ordering;
  297. return BadValue;
  298. }
  299. nrects = ((stuff->length << 2) - sizeof(xShapeRectanglesReq));
  300. if (nrects & 4)
  301. return BadLength;
  302. nrects >>= 3;
  303. prects = (xRectangle *) &stuff[1];
  304. ctype = VerifyRectOrder(nrects, prects, (int) stuff->ordering);
  305. if (ctype < 0)
  306. return BadMatch;
  307. srcRgn = RECTS_TO_REGION(nrects, prects, ctype);
  308. if (!pWin->optional)
  309. MakeWindowOptional(pWin);
  310. switch (stuff->destKind) {
  311. case ShapeBounding:
  312. destRgn = &pWin->optional->boundingShape;
  313. break;
  314. case ShapeClip:
  315. destRgn = &pWin->optional->clipShape;
  316. break;
  317. case ShapeInput:
  318. destRgn = &pWin->optional->inputShape;
  319. break;
  320. default:
  321. return BadValue;
  322. }
  323. return RegionOperate(client, pWin, (int) stuff->destKind,
  324. destRgn, srcRgn, (int) stuff->op,
  325. stuff->xOff, stuff->yOff, createDefault);
  326. }
  327. /**************
  328. * ProcShapeMask
  329. **************/
  330. static int
  331. ProcShapeMask(client)
  332. ClientPtr client;
  333. {
  334. WindowPtr pWin;
  335. ScreenPtr pScreen;
  336. REQUEST(xShapeMaskReq);
  337. RegionPtr srcRgn;
  338. RegionPtr *destRgn;
  339. PixmapPtr pPixmap;
  340. CreateDftPtr createDefault;
  341. REQUEST_SIZE_MATCH(xShapeMaskReq);
  342. UpdateCurrentTime();
  343. pWin = SecurityLookupWindow(stuff->dest, client, SecurityWriteAccess);
  344. if (!pWin)
  345. return BadWindow;
  346. switch (stuff->destKind) {
  347. case ShapeBounding:
  348. createDefault = CreateBoundingShape;
  349. break;
  350. case ShapeClip:
  351. createDefault = CreateClipShape;
  352. break;
  353. case ShapeInput:
  354. createDefault = CreateBoundingShape;
  355. break;
  356. default:
  357. client->errorValue = stuff->destKind;
  358. return BadValue;
  359. }
  360. pScreen = pWin->drawable.pScreen;
  361. if (stuff->src == None)
  362. srcRgn = 0;
  363. else {
  364. pPixmap = (PixmapPtr) SecurityLookupIDByType(client, stuff->src,
  365. RT_PIXMAP,
  366. SecurityReadAccess);
  367. if (!pPixmap)
  368. return BadPixmap;
  369. if (pPixmap->drawable.pScreen != pScreen ||
  370. pPixmap->drawable.depth != 1)
  371. return BadMatch;
  372. srcRgn = BITMAP_TO_REGION(pScreen, pPixmap);
  373. if (!srcRgn)
  374. return BadAlloc;
  375. }
  376. if (!pWin->optional)
  377. MakeWindowOptional(pWin);
  378. switch (stuff->destKind) {
  379. case ShapeBounding:
  380. destRgn = &pWin->optional->boundingShape;
  381. break;
  382. case ShapeClip:
  383. destRgn = &pWin->optional->clipShape;
  384. break;
  385. case ShapeInput:
  386. destRgn = &pWin->optional->inputShape;
  387. break;
  388. default:
  389. return BadValue;
  390. }
  391. return RegionOperate(client, pWin, (int) stuff->destKind,
  392. destRgn, srcRgn, (int) stuff->op,
  393. stuff->xOff, stuff->yOff, createDefault);
  394. }
  395. /************
  396. * ProcShapeCombine
  397. ************/
  398. static int
  399. ProcShapeCombine(client)
  400. ClientPtr client;
  401. {
  402. WindowPtr pSrcWin, pDestWin;
  403. ScreenPtr pScreen;
  404. REQUEST(xShapeCombineReq);
  405. RegionPtr srcRgn;
  406. RegionPtr *destRgn;
  407. CreateDftPtr createDefault;
  408. CreateDftPtr createSrc;
  409. RegionPtr tmp;
  410. REQUEST_SIZE_MATCH(xShapeCombineReq);
  411. UpdateCurrentTime();
  412. pDestWin = LookupWindow(stuff->dest, client);
  413. if (!pDestWin)
  414. return BadWindow;
  415. if (!pDestWin->optional)
  416. MakeWindowOptional(pDestWin);
  417. switch (stuff->destKind) {
  418. case ShapeBounding:
  419. createDefault = CreateBoundingShape;
  420. break;
  421. case ShapeClip:
  422. createDefault = CreateClipShape;
  423. break;
  424. case ShapeInput:
  425. createDefault = CreateBoundingShape;
  426. break;
  427. default:
  428. client->errorValue = stuff->destKind;
  429. return BadValue;
  430. }
  431. pScreen = pDestWin->drawable.pScreen;
  432. pSrcWin = LookupWindow(stuff->src, client);
  433. if (!pSrcWin)
  434. return BadWindow;
  435. switch (stuff->srcKind) {
  436. case ShapeBounding:
  437. srcRgn = wBoundingShape(pSrcWin);
  438. createSrc = CreateBoundingShape;
  439. break;
  440. case ShapeClip:
  441. srcRgn = wClipShape(pSrcWin);
  442. createSrc = CreateClipShape;
  443. break;
  444. case ShapeInput:
  445. srcRgn = wInputShape(pSrcWin);
  446. createSrc = CreateBoundingShape;
  447. break;
  448. default:
  449. client->errorValue = stuff->srcKind;
  450. return BadValue;
  451. }
  452. if (pSrcWin->drawable.pScreen != pScreen) {
  453. return BadMatch;
  454. }
  455. if (srcRgn) {
  456. tmp = REGION_CREATE((BoxPtr) 0, 0);
  457. REGION_COPY(tmp, srcRgn);
  458. srcRgn = tmp;
  459. }
  460. else
  461. srcRgn = (*createSrc) (pSrcWin);
  462. if (!pDestWin->optional)
  463. MakeWindowOptional(pDestWin);
  464. switch (stuff->destKind) {
  465. case ShapeBounding:
  466. destRgn = &pDestWin->optional->boundingShape;
  467. break;
  468. case ShapeClip:
  469. destRgn = &pDestWin->optional->clipShape;
  470. break;
  471. case ShapeInput:
  472. destRgn = &pDestWin->optional->inputShape;
  473. break;
  474. default:
  475. return BadValue;
  476. }
  477. return RegionOperate(client, pDestWin, (int) stuff->destKind,
  478. destRgn, srcRgn, (int) stuff->op,
  479. stuff->xOff, stuff->yOff, createDefault);
  480. }
  481. /*************
  482. * ProcShapeOffset
  483. *************/
  484. static int
  485. ProcShapeOffset(client)
  486. ClientPtr client;
  487. {
  488. WindowPtr pWin;
  489. ScreenPtr pScreen;
  490. REQUEST(xShapeOffsetReq);
  491. RegionPtr srcRgn;
  492. REQUEST_SIZE_MATCH(xShapeOffsetReq);
  493. UpdateCurrentTime();
  494. pWin = LookupWindow(stuff->dest, client);
  495. if (!pWin)
  496. return BadWindow;
  497. switch (stuff->destKind) {
  498. case ShapeBounding:
  499. srcRgn = wBoundingShape(pWin);
  500. break;
  501. case ShapeClip:
  502. srcRgn = wClipShape(pWin);
  503. break;
  504. case ShapeInput:
  505. srcRgn = wInputShape(pWin);
  506. break;
  507. default:
  508. client->errorValue = stuff->destKind;
  509. return BadValue;
  510. }
  511. pScreen = pWin->drawable.pScreen;
  512. if (srcRgn) {
  513. REGION_TRANSLATE(srcRgn, stuff->xOff, stuff->yOff);
  514. (*pScreen->SetShape) (pWin);
  515. }
  516. SendShapeNotify(pWin, (int) stuff->destKind);
  517. return Success;
  518. }
  519. static int
  520. ProcShapeQueryExtents(client)
  521. ClientPtr client;
  522. {
  523. REQUEST(xShapeQueryExtentsReq);
  524. WindowPtr pWin;
  525. xShapeQueryExtentsReply rep;
  526. BoxRec extents, *pExtents;
  527. RegionPtr region;
  528. REQUEST_SIZE_MATCH(xShapeQueryExtentsReq);
  529. pWin = LookupWindow(stuff->window, client);
  530. if (!pWin)
  531. return BadWindow;
  532. rep.type = X_Reply;
  533. rep.length = 0;
  534. rep.sequenceNumber = client->sequence;
  535. rep.boundingShaped = (wBoundingShape(pWin) != 0);
  536. rep.clipShaped = (wClipShape(pWin) != 0);
  537. if ((region = wBoundingShape(pWin))) {
  538. /* this is done in two steps because of a compiler bug on SunOS 4.1.3 */
  539. pExtents = REGION_EXTENTS(region);
  540. extents = *pExtents;
  541. }
  542. else {
  543. extents.x1 = -wBorderWidth(pWin);
  544. extents.y1 = -wBorderWidth(pWin);
  545. extents.x2 = pWin->drawable.width + wBorderWidth(pWin);
  546. extents.y2 = pWin->drawable.height + wBorderWidth(pWin);
  547. }
  548. rep.xBoundingShape = extents.x1;
  549. rep.yBoundingShape = extents.y1;
  550. rep.widthBoundingShape = extents.x2 - extents.x1;
  551. rep.heightBoundingShape = extents.y2 - extents.y1;
  552. if ((region = wClipShape(pWin))) {
  553. /* this is done in two steps because of a compiler bug on SunOS 4.1.3 */
  554. pExtents = REGION_EXTENTS(region);
  555. extents = *pExtents;
  556. }
  557. else {
  558. extents.x1 = 0;
  559. extents.y1 = 0;
  560. extents.x2 = pWin->drawable.width;
  561. extents.y2 = pWin->drawable.height;
  562. }
  563. rep.xClipShape = extents.x1;
  564. rep.yClipShape = extents.y1;
  565. rep.widthClipShape = extents.x2 - extents.x1;
  566. rep.heightClipShape = extents.y2 - extents.y1;
  567. if (client->swapped) {
  568. swaps(&rep.sequenceNumber);
  569. swapl(&rep.length);
  570. swaps(&rep.xBoundingShape);
  571. swaps(&rep.yBoundingShape);
  572. swaps(&rep.widthBoundingShape);
  573. swaps(&rep.heightBoundingShape);
  574. swaps(&rep.xClipShape);
  575. swaps(&rep.yClipShape);
  576. swaps(&rep.widthClipShape);
  577. swaps(&rep.heightClipShape);
  578. }
  579. WriteToClient(client, sizeof(xShapeQueryExtentsReply), (char *) &rep);
  580. return (client->noClientException);
  581. }
  582. /*ARGSUSED*/ static int
  583. ShapeFreeClient(data, id)
  584. pointer data;
  585. XID id;
  586. {
  587. ShapeEventPtr pShapeEvent;
  588. WindowPtr pWin;
  589. ShapeEventPtr *pHead, pCur, pPrev;
  590. pShapeEvent = (ShapeEventPtr) data;
  591. pWin = pShapeEvent->window;
  592. pHead = (ShapeEventPtr *) LookupIDByType(pWin->drawable.id, EventType);
  593. if (pHead) {
  594. pPrev = 0;
  595. for (pCur = *pHead; pCur && pCur != pShapeEvent; pCur = pCur->next)
  596. pPrev = pCur;
  597. if (pCur) {
  598. if (pPrev)
  599. pPrev->next = pShapeEvent->next;
  600. else
  601. *pHead = pShapeEvent->next;
  602. }
  603. }
  604. free((pointer) pShapeEvent);
  605. return 1;
  606. }
  607. /*ARGSUSED*/ static int
  608. ShapeFreeEvents(data, id)
  609. pointer data;
  610. XID id;
  611. {
  612. ShapeEventPtr *pHead, pCur, pNext;
  613. pHead = (ShapeEventPtr *) data;
  614. for (pCur = *pHead; pCur; pCur = pNext) {
  615. pNext = pCur->next;
  616. FreeResource(pCur->clientResource, ClientType);
  617. free((pointer) pCur);
  618. }
  619. free((pointer) pHead);
  620. return 1;
  621. }
  622. static int
  623. ProcShapeSelectInput(client)
  624. ClientPtr client;
  625. {
  626. REQUEST(xShapeSelectInputReq);
  627. WindowPtr pWin;
  628. ShapeEventPtr pShapeEvent, pNewShapeEvent, *pHead;
  629. XID clientResource;
  630. REQUEST_SIZE_MATCH(xShapeSelectInputReq);
  631. pWin = SecurityLookupWindow(stuff->window, client, SecurityWriteAccess);
  632. if (!pWin)
  633. return BadWindow;
  634. pHead = (ShapeEventPtr *) SecurityLookupIDByType(client,
  635. pWin->drawable.id,
  636. EventType,
  637. SecurityWriteAccess);
  638. switch (stuff->enable) {
  639. case xTrue:
  640. if (pHead) {
  641. /* check for existing entry. */
  642. for (pShapeEvent = *pHead;
  643. pShapeEvent; pShapeEvent = pShapeEvent->next) {
  644. if (pShapeEvent->client == client)
  645. return Success;
  646. }
  647. }
  648. /* build the entry */
  649. pNewShapeEvent = (ShapeEventPtr)
  650. malloc(sizeof(ShapeEventRec));
  651. if (!pNewShapeEvent)
  652. return BadAlloc;
  653. pNewShapeEvent->next = 0;
  654. pNewShapeEvent->client = client;
  655. pNewShapeEvent->window = pWin;
  656. /*
  657. * add a resource that will be deleted when
  658. * the client goes away
  659. */
  660. clientResource = FakeClientID(client->index);
  661. pNewShapeEvent->clientResource = clientResource;
  662. if (!AddResource(clientResource, ClientType, (pointer) pNewShapeEvent))
  663. return BadAlloc;
  664. /*
  665. * create a resource to contain a pointer to the list
  666. * of clients selecting input. This must be indirect as
  667. * the list may be arbitrarily rearranged which cannot be
  668. * done through the resource database.
  669. */
  670. if (!pHead) {
  671. pHead = malloc(sizeof(ShapeEventPtr));
  672. if (!pHead ||
  673. !AddResource(pWin->drawable.id, EventType, (pointer) pHead)) {
  674. FreeResource(clientResource, RT_NONE);
  675. return BadAlloc;
  676. }
  677. *pHead = 0;
  678. }
  679. pNewShapeEvent->next = *pHead;
  680. *pHead = pNewShapeEvent;
  681. break;
  682. case xFalse:
  683. /* delete the interest */
  684. if (pHead) {
  685. pNewShapeEvent = 0;
  686. for (pShapeEvent = *pHead; pShapeEvent;
  687. pShapeEvent = pShapeEvent->next) {
  688. if (pShapeEvent->client == client)
  689. break;
  690. pNewShapeEvent = pShapeEvent;
  691. }
  692. if (pShapeEvent) {
  693. FreeResource(pShapeEvent->clientResource, ClientType);
  694. if (pNewShapeEvent)
  695. pNewShapeEvent->next = pShapeEvent->next;
  696. else
  697. *pHead = pShapeEvent->next;
  698. free(pShapeEvent);
  699. }
  700. }
  701. break;
  702. default:
  703. client->errorValue = stuff->enable;
  704. return BadValue;
  705. }
  706. return Success;
  707. }
  708. /*
  709. * deliver the event
  710. */
  711. void
  712. SendShapeNotify(pWin, which)
  713. WindowPtr pWin;
  714. int which;
  715. {
  716. ShapeEventPtr *pHead, pShapeEvent;
  717. xShapeNotifyEvent se;
  718. BoxRec extents;
  719. RegionPtr region;
  720. BYTE shaped;
  721. pHead = (ShapeEventPtr *) LookupIDByType(pWin->drawable.id, EventType);
  722. if (!pHead)
  723. return;
  724. switch (which) {
  725. case ShapeBounding:
  726. region = wBoundingShape(pWin);
  727. if (region) {
  728. extents = *REGION_EXTENTS(region);
  729. shaped = xTrue;
  730. }
  731. else {
  732. extents.x1 = -wBorderWidth(pWin);
  733. extents.y1 = -wBorderWidth(pWin);
  734. extents.x2 = pWin->drawable.width + wBorderWidth(pWin);
  735. extents.y2 = pWin->drawable.height + wBorderWidth(pWin);
  736. shaped = xFalse;
  737. }
  738. break;
  739. case ShapeClip:
  740. region = wClipShape(pWin);
  741. if (region) {
  742. extents = *REGION_EXTENTS(region);
  743. shaped = xTrue;
  744. }
  745. else {
  746. extents.x1 = 0;
  747. extents.y1 = 0;
  748. extents.x2 = pWin->drawable.width;
  749. extents.y2 = pWin->drawable.height;
  750. shaped = xFalse;
  751. }
  752. break;
  753. case ShapeInput:
  754. region = wInputShape(pWin);
  755. if (region) {
  756. extents = *REGION_EXTENTS(region);
  757. shaped = xTrue;
  758. }
  759. else {
  760. extents.x1 = -wBorderWidth(pWin);
  761. extents.y1 = -wBorderWidth(pWin);
  762. extents.x2 = pWin->drawable.width + wBorderWidth(pWin);
  763. extents.y2 = pWin->drawable.height + wBorderWidth(pWin);
  764. shaped = xFalse;
  765. }
  766. break;
  767. default:
  768. return;
  769. }
  770. for (pShapeEvent = *pHead; pShapeEvent; pShapeEvent = pShapeEvent->next) {
  771. se.type = ShapeNotify + ShapeEventBase;
  772. se.kind = which;
  773. se.window = pWin->drawable.id;
  774. se.x = extents.x1;
  775. se.y = extents.y1;
  776. se.width = extents.x2 - extents.x1;
  777. se.height = extents.y2 - extents.y1;
  778. se.time = currentTime.milliseconds;
  779. se.shaped = shaped;
  780. WriteEventsToClient(pShapeEvent->client, 1, (xEvent *) &se);
  781. }
  782. }
  783. static int
  784. ProcShapeInputSelected(client)
  785. ClientPtr client;
  786. {
  787. REQUEST(xShapeInputSelectedReq);
  788. WindowPtr pWin;
  789. ShapeEventPtr pShapeEvent, *pHead;
  790. int enabled;
  791. xShapeInputSelectedReply rep;
  792. REQUEST_SIZE_MATCH(xShapeInputSelectedReq);
  793. pWin = LookupWindow(stuff->window, client);
  794. if (!pWin)
  795. return BadWindow;
  796. pHead = (ShapeEventPtr *) SecurityLookupIDByType(client,
  797. pWin->drawable.id,
  798. EventType,
  799. SecurityReadAccess);
  800. enabled = xFalse;
  801. if (pHead) {
  802. for (pShapeEvent = *pHead; pShapeEvent; pShapeEvent = pShapeEvent->next) {
  803. if (pShapeEvent->client == client) {
  804. enabled = xTrue;
  805. break;
  806. }
  807. }
  808. }
  809. rep.type = X_Reply;
  810. rep.length = 0;
  811. rep.sequenceNumber = client->sequence;
  812. rep.enabled = enabled;
  813. if (client->swapped) {
  814. swaps(&rep.sequenceNumber);
  815. swapl(&rep.length);
  816. }
  817. WriteToClient(client, sizeof(xShapeInputSelectedReply), (char *) &rep);
  818. return (client->noClientException);
  819. }
  820. static int
  821. ProcShapeGetRectangles(client)
  822. ClientPtr client;
  823. {
  824. REQUEST(xShapeGetRectanglesReq);
  825. WindowPtr pWin;
  826. xShapeGetRectanglesReply rep;
  827. xRectangle *rects;
  828. int nrects, i;
  829. RegionPtr region;
  830. REQUEST_SIZE_MATCH(xShapeGetRectanglesReq);
  831. pWin = LookupWindow(stuff->window, client);
  832. if (!pWin)
  833. return BadWindow;
  834. switch (stuff->kind) {
  835. case ShapeBounding:
  836. region = wBoundingShape(pWin);
  837. break;
  838. case ShapeClip:
  839. region = wClipShape(pWin);
  840. break;
  841. case ShapeInput:
  842. region = wInputShape(pWin);
  843. break;
  844. default:
  845. client->errorValue = stuff->kind;
  846. return BadValue;
  847. }
  848. if (!region) {
  849. nrects = 1;
  850. rects = (xRectangle *) ALLOCATE_LOCAL(sizeof(xRectangle));
  851. if (!rects)
  852. return BadAlloc;
  853. switch (stuff->kind) {
  854. case ShapeBounding:
  855. rects->x = -(int) wBorderWidth(pWin);
  856. rects->y = -(int) wBorderWidth(pWin);
  857. rects->width = pWin->drawable.width + wBorderWidth(pWin);
  858. rects->height = pWin->drawable.height + wBorderWidth(pWin);
  859. break;
  860. case ShapeClip:
  861. rects->x = 0;
  862. rects->y = 0;
  863. rects->width = pWin->drawable.width;
  864. rects->height = pWin->drawable.height;
  865. break;
  866. case ShapeInput:
  867. rects->x = -(int) wBorderWidth(pWin);
  868. rects->y = -(int) wBorderWidth(pWin);
  869. rects->width = pWin->drawable.width + wBorderWidth(pWin);
  870. rects->height = pWin->drawable.height + wBorderWidth(pWin);
  871. break;
  872. }
  873. }
  874. else {
  875. BoxPtr box;
  876. nrects = REGION_NUM_RECTS(region);
  877. box = REGION_RECTS(region);
  878. rects = (xRectangle *) ALLOCATE_LOCAL(nrects * sizeof(xRectangle));
  879. if (!rects && nrects)
  880. return BadAlloc;
  881. for (i = 0; i < nrects; i++, box++) {
  882. rects[i].x = box->x1;
  883. rects[i].y = box->y1;
  884. rects[i].width = box->x2 - box->x1;
  885. rects[i].height = box->y2 - box->y1;
  886. }
  887. }
  888. rep.type = X_Reply;
  889. rep.sequenceNumber = client->sequence;
  890. rep.length = (nrects * sizeof(xRectangle)) >> 2;
  891. rep.ordering = YXBanded;
  892. rep.nrects = nrects;
  893. if (client->swapped) {
  894. swaps(&rep.sequenceNumber);
  895. swapl(&rep.length);
  896. swapl(&rep.nrects);
  897. SwapShorts((short *) rects, (unsigned long) nrects * 4);
  898. }
  899. WriteToClient(client, sizeof(rep), (char *) &rep);
  900. WriteToClient(client, nrects * sizeof(xRectangle), (char *) rects);
  901. DEALLOCATE_LOCAL(rects);
  902. return client->noClientException;
  903. }
  904. static int
  905. ProcShapeDispatch(client)
  906. ClientPtr client;
  907. {
  908. REQUEST(xReq);
  909. switch (stuff->data) {
  910. case X_ShapeQueryVersion:
  911. return ProcShapeQueryVersion(client);
  912. case X_ShapeRectangles:
  913. return ProcShapeRectangles(client);
  914. case X_ShapeMask:
  915. return ProcShapeMask(client);
  916. case X_ShapeCombine:
  917. return ProcShapeCombine(client);
  918. case X_ShapeOffset:
  919. return ProcShapeOffset(client);
  920. case X_ShapeQueryExtents:
  921. return ProcShapeQueryExtents(client);
  922. case X_ShapeSelectInput:
  923. return ProcShapeSelectInput(client);
  924. case X_ShapeInputSelected:
  925. return ProcShapeInputSelected(client);
  926. case X_ShapeGetRectangles:
  927. return ProcShapeGetRectangles(client);
  928. default:
  929. return BadRequest;
  930. }
  931. }
  932. static void
  933. SShapeNotifyEvent(from, to)
  934. xShapeNotifyEvent *from, *to;
  935. {
  936. to->type = from->type;
  937. to->kind = from->kind;
  938. cpswapl(from->window, to->window);
  939. cpswaps(from->sequenceNumber, to->sequenceNumber);
  940. cpswaps(from->x, to->x);
  941. cpswaps(from->y, to->y);
  942. cpswaps(from->width, to->width);
  943. cpswaps(from->height, to->height);
  944. cpswapl(from->time, to->time);
  945. to->shaped = from->shaped;
  946. }
  947. static int
  948. SProcShapeQueryVersion(client)
  949. ClientPtr client;
  950. {
  951. REQUEST(xShapeQueryVersionReq);
  952. swaps(&stuff->length);
  953. return ProcShapeQueryVersion(client);
  954. }
  955. static int
  956. SProcShapeRectangles(client)
  957. ClientPtr client;
  958. {
  959. REQUEST(xShapeRectanglesReq);
  960. swaps(&stuff->length);
  961. REQUEST_AT_LEAST_SIZE(xShapeRectanglesReq);
  962. swapl(&stuff->dest);
  963. swaps(&stuff->xOff);
  964. swaps(&stuff->yOff);
  965. SwapRestS(stuff);
  966. return ProcShapeRectangles(client);
  967. }
  968. static int
  969. SProcShapeMask(client)
  970. ClientPtr client;
  971. {
  972. REQUEST(xShapeMaskReq);
  973. swaps(&stuff->length);
  974. REQUEST_SIZE_MATCH(xShapeMaskReq);
  975. swapl(&stuff->dest);
  976. swaps(&stuff->xOff);
  977. swaps(&stuff->yOff);
  978. swapl(&stuff->src);
  979. return ProcShapeMask(client);
  980. }
  981. static int
  982. SProcShapeCombine(client)
  983. ClientPtr client;
  984. {
  985. REQUEST(xShapeCombineReq);
  986. swaps(&stuff->length);
  987. REQUEST_SIZE_MATCH(xShapeCombineReq);
  988. swapl(&stuff->dest);
  989. swaps(&stuff->xOff);
  990. swaps(&stuff->yOff);
  991. swapl(&stuff->src);
  992. return ProcShapeCombine(client);
  993. }
  994. static int
  995. SProcShapeOffset(client)
  996. ClientPtr client;
  997. {
  998. REQUEST(xShapeOffsetReq);
  999. swaps(&stuff->length);
  1000. REQUEST_SIZE_MATCH(xShapeOffsetReq);
  1001. swapl(&stuff->dest);
  1002. swaps(&stuff->xOff);
  1003. swaps(&stuff->yOff);
  1004. return ProcShapeOffset(client);
  1005. }
  1006. static int
  1007. SProcShapeQueryExtents(client)
  1008. ClientPtr client;
  1009. {
  1010. REQUEST(xShapeQueryExtentsReq);
  1011. swaps(&stuff->length);
  1012. REQUEST_SIZE_MATCH(xShapeQueryExtentsReq);
  1013. swapl(&stuff->window);
  1014. return ProcShapeQueryExtents(client);
  1015. }
  1016. static int
  1017. SProcShapeSelectInput(client)
  1018. ClientPtr client;
  1019. {
  1020. REQUEST(xShapeSelectInputReq);
  1021. swaps(&stuff->length);
  1022. REQUEST_SIZE_MATCH(xShapeSelectInputReq);
  1023. swapl(&stuff->window);
  1024. return ProcShapeSelectInput(client);
  1025. }
  1026. static int
  1027. SProcShapeInputSelected(client)
  1028. ClientPtr client;
  1029. {
  1030. REQUEST(xShapeInputSelectedReq);
  1031. swaps(&stuff->length);
  1032. REQUEST_SIZE_MATCH(xShapeInputSelectedReq);
  1033. swapl(&stuff->window);
  1034. return ProcShapeInputSelected(client);
  1035. }
  1036. static int
  1037. SProcShapeGetRectangles(client)
  1038. ClientPtr client;
  1039. {
  1040. REQUEST(xShapeGetRectanglesReq);
  1041. swaps(&stuff->length);
  1042. REQUEST_SIZE_MATCH(xShapeGetRectanglesReq);
  1043. swapl(&stuff->window);
  1044. return ProcShapeGetRectangles(client);
  1045. }
  1046. static int
  1047. SProcShapeDispatch(client)
  1048. ClientPtr client;
  1049. {
  1050. REQUEST(xReq);
  1051. switch (stuff->data) {
  1052. case X_ShapeQueryVersion:
  1053. return SProcShapeQueryVersion(client);
  1054. case X_ShapeRectangles:
  1055. return SProcShapeRectangles(client);
  1056. case X_ShapeMask:
  1057. return SProcShapeMask(client);
  1058. case X_ShapeCombine:
  1059. return SProcShapeCombine(client);
  1060. case X_ShapeOffset:
  1061. return SProcShapeOffset(client);
  1062. case X_ShapeQueryExtents:
  1063. return SProcShapeQueryExtents(client);
  1064. case X_ShapeSelectInput:
  1065. return SProcShapeSelectInput(client);
  1066. case X_ShapeInputSelected:
  1067. return SProcShapeInputSelected(client);
  1068. case X_ShapeGetRectangles:
  1069. return SProcShapeGetRectangles(client);
  1070. default:
  1071. return BadRequest;
  1072. }
  1073. }