Window.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /*
  2. Copyright 1993 by Davor Matic
  3. Permission to use, copy, modify, distribute, and sell this software
  4. and its documentation for any purpose is hereby granted without fee,
  5. provided that the above copyright notice appear in all copies and that
  6. both that copyright notice and this permission notice appear in
  7. supporting documentation. Davor Matic makes no representations about
  8. the suitability of this software for any purpose. It is provided "as
  9. is" without express or implied warranty.
  10. */
  11. #ifdef HAVE_XNEST_CONFIG_H
  12. #include <xnest-config.h>
  13. #endif
  14. #include <X11/X.h>
  15. #include <X11/Xproto.h>
  16. #include "gcstruct.h"
  17. #include "window.h"
  18. #include "windowstr.h"
  19. #include "pixmapstr.h"
  20. #include "colormapst.h"
  21. #include "scrnintstr.h"
  22. #include "region.h"
  23. #include "mi.h"
  24. #include "Xnest.h"
  25. #include "Display.h"
  26. #include "Screen.h"
  27. #include "XNGC.h"
  28. #include "Drawable.h"
  29. #include "Color.h"
  30. #include "Visual.h"
  31. #include "Events.h"
  32. #include "Args.h"
  33. DevPrivateKeyRec xnestWindowPrivateKeyRec;
  34. static int
  35. xnestFindWindowMatch(WindowPtr pWin, void *ptr)
  36. {
  37. xnestWindowMatch *wm = (xnestWindowMatch *) ptr;
  38. if (wm->window == xnestWindow(pWin)) {
  39. wm->pWin = pWin;
  40. return WT_STOPWALKING;
  41. }
  42. else
  43. return WT_WALKCHILDREN;
  44. }
  45. WindowPtr
  46. xnestWindowPtr(Window window)
  47. {
  48. xnestWindowMatch wm;
  49. int i;
  50. wm.pWin = NullWindow;
  51. wm.window = window;
  52. for (i = 0; i < xnestNumScreens; i++) {
  53. WalkTree(screenInfo.screens[i], xnestFindWindowMatch, (void *) &wm);
  54. if (wm.pWin)
  55. break;
  56. }
  57. return wm.pWin;
  58. }
  59. Bool
  60. xnestCreateWindow(WindowPtr pWin)
  61. {
  62. unsigned long mask;
  63. XSetWindowAttributes attributes;
  64. Visual *visual;
  65. ColormapPtr pCmap;
  66. if (pWin->drawable.class == InputOnly) {
  67. mask = 0L;
  68. visual = CopyFromParent;
  69. }
  70. else {
  71. mask = CWEventMask | CWBackingStore;
  72. attributes.event_mask = ExposureMask;
  73. attributes.backing_store = NotUseful;
  74. if (pWin->parent) {
  75. if (pWin->optional &&
  76. pWin->optional->visual != wVisual(pWin->parent)) {
  77. visual =
  78. xnestVisualFromID(pWin->drawable.pScreen, wVisual(pWin));
  79. mask |= CWColormap;
  80. if (pWin->optional->colormap) {
  81. dixLookupResourceByType((void **) &pCmap, wColormap(pWin),
  82. RT_COLORMAP, serverClient,
  83. DixUseAccess);
  84. attributes.colormap = xnestColormap(pCmap);
  85. }
  86. else
  87. attributes.colormap = xnestDefaultVisualColormap(visual);
  88. }
  89. else
  90. visual = CopyFromParent;
  91. }
  92. else { /* root windows have their own colormaps at creation time */
  93. visual = xnestVisualFromID(pWin->drawable.pScreen, wVisual(pWin));
  94. dixLookupResourceByType((void **) &pCmap, wColormap(pWin),
  95. RT_COLORMAP, serverClient, DixUseAccess);
  96. mask |= CWColormap;
  97. attributes.colormap = xnestColormap(pCmap);
  98. }
  99. }
  100. xnestWindowPriv(pWin)->window = XCreateWindow(xnestDisplay,
  101. xnestWindowParent(pWin),
  102. pWin->origin.x -
  103. wBorderWidth(pWin),
  104. pWin->origin.y -
  105. wBorderWidth(pWin),
  106. pWin->drawable.width,
  107. pWin->drawable.height,
  108. pWin->borderWidth,
  109. pWin->drawable.depth,
  110. pWin->drawable.class,
  111. visual, mask, &attributes);
  112. xnestWindowPriv(pWin)->parent = xnestWindowParent(pWin);
  113. xnestWindowPriv(pWin)->x = pWin->origin.x - wBorderWidth(pWin);
  114. xnestWindowPriv(pWin)->y = pWin->origin.y - wBorderWidth(pWin);
  115. xnestWindowPriv(pWin)->width = pWin->drawable.width;
  116. xnestWindowPriv(pWin)->height = pWin->drawable.height;
  117. xnestWindowPriv(pWin)->border_width = pWin->borderWidth;
  118. xnestWindowPriv(pWin)->sibling_above = None;
  119. if (pWin->nextSib)
  120. xnestWindowPriv(pWin->nextSib)->sibling_above = xnestWindow(pWin);
  121. xnestWindowPriv(pWin)->bounding_shape = RegionCreate(NULL, 1);
  122. xnestWindowPriv(pWin)->clip_shape = RegionCreate(NULL, 1);
  123. if (!pWin->parent) /* only the root window will have the right colormap */
  124. xnestSetInstalledColormapWindows(pWin->drawable.pScreen);
  125. return True;
  126. }
  127. Bool
  128. xnestDestroyWindow(WindowPtr pWin)
  129. {
  130. if (pWin->nextSib)
  131. xnestWindowPriv(pWin->nextSib)->sibling_above =
  132. xnestWindowPriv(pWin)->sibling_above;
  133. RegionDestroy(xnestWindowPriv(pWin)->bounding_shape);
  134. RegionDestroy(xnestWindowPriv(pWin)->clip_shape);
  135. XDestroyWindow(xnestDisplay, xnestWindow(pWin));
  136. xnestWindowPriv(pWin)->window = None;
  137. if (pWin->optional && pWin->optional->colormap && pWin->parent)
  138. xnestSetInstalledColormapWindows(pWin->drawable.pScreen);
  139. return True;
  140. }
  141. Bool
  142. xnestPositionWindow(WindowPtr pWin, int x, int y)
  143. {
  144. xnestConfigureWindow(pWin,
  145. CWParent |
  146. CWX | CWY | CWWidth | CWHeight | CWBorderWidth);
  147. return True;
  148. }
  149. void
  150. xnestConfigureWindow(WindowPtr pWin, unsigned int mask)
  151. {
  152. unsigned int valuemask;
  153. XWindowChanges values;
  154. if (mask & CWParent &&
  155. xnestWindowPriv(pWin)->parent != xnestWindowParent(pWin)) {
  156. XReparentWindow(xnestDisplay, xnestWindow(pWin),
  157. xnestWindowParent(pWin),
  158. pWin->origin.x - wBorderWidth(pWin),
  159. pWin->origin.y - wBorderWidth(pWin));
  160. xnestWindowPriv(pWin)->parent = xnestWindowParent(pWin);
  161. xnestWindowPriv(pWin)->x = pWin->origin.x - wBorderWidth(pWin);
  162. xnestWindowPriv(pWin)->y = pWin->origin.y - wBorderWidth(pWin);
  163. xnestWindowPriv(pWin)->sibling_above = None;
  164. if (pWin->nextSib)
  165. xnestWindowPriv(pWin->nextSib)->sibling_above = xnestWindow(pWin);
  166. }
  167. valuemask = 0;
  168. if (mask & CWX &&
  169. xnestWindowPriv(pWin)->x != pWin->origin.x - wBorderWidth(pWin)) {
  170. valuemask |= CWX;
  171. values.x =
  172. xnestWindowPriv(pWin)->x = pWin->origin.x - wBorderWidth(pWin);
  173. }
  174. if (mask & CWY &&
  175. xnestWindowPriv(pWin)->y != pWin->origin.y - wBorderWidth(pWin)) {
  176. valuemask |= CWY;
  177. values.y =
  178. xnestWindowPriv(pWin)->y = pWin->origin.y - wBorderWidth(pWin);
  179. }
  180. if (mask & CWWidth && xnestWindowPriv(pWin)->width != pWin->drawable.width) {
  181. valuemask |= CWWidth;
  182. values.width = xnestWindowPriv(pWin)->width = pWin->drawable.width;
  183. }
  184. if (mask & CWHeight &&
  185. xnestWindowPriv(pWin)->height != pWin->drawable.height) {
  186. valuemask |= CWHeight;
  187. values.height = xnestWindowPriv(pWin)->height = pWin->drawable.height;
  188. }
  189. if (mask & CWBorderWidth &&
  190. xnestWindowPriv(pWin)->border_width != pWin->borderWidth) {
  191. valuemask |= CWBorderWidth;
  192. values.border_width =
  193. xnestWindowPriv(pWin)->border_width = pWin->borderWidth;
  194. }
  195. if (valuemask)
  196. XConfigureWindow(xnestDisplay, xnestWindow(pWin), valuemask, &values);
  197. if (mask & CWStackingOrder &&
  198. xnestWindowPriv(pWin)->sibling_above != xnestWindowSiblingAbove(pWin)) {
  199. WindowPtr pSib;
  200. /* find the top sibling */
  201. for (pSib = pWin; pSib->prevSib != NullWindow; pSib = pSib->prevSib);
  202. /* the top sibling */
  203. valuemask = CWStackMode;
  204. values.stack_mode = Above;
  205. XConfigureWindow(xnestDisplay, xnestWindow(pSib), valuemask, &values);
  206. xnestWindowPriv(pSib)->sibling_above = None;
  207. /* the rest of siblings */
  208. for (pSib = pSib->nextSib; pSib != NullWindow; pSib = pSib->nextSib) {
  209. valuemask = CWSibling | CWStackMode;
  210. values.sibling = xnestWindowSiblingAbove(pSib);
  211. values.stack_mode = Below;
  212. XConfigureWindow(xnestDisplay, xnestWindow(pSib), valuemask,
  213. &values);
  214. xnestWindowPriv(pSib)->sibling_above =
  215. xnestWindowSiblingAbove(pSib);
  216. }
  217. }
  218. }
  219. Bool
  220. xnestChangeWindowAttributes(WindowPtr pWin, unsigned long mask)
  221. {
  222. XSetWindowAttributes attributes;
  223. if (mask & CWBackPixmap)
  224. switch (pWin->backgroundState) {
  225. case None:
  226. attributes.background_pixmap = None;
  227. break;
  228. case ParentRelative:
  229. attributes.background_pixmap = ParentRelative;
  230. break;
  231. case BackgroundPixmap:
  232. attributes.background_pixmap = xnestPixmap(pWin->background.pixmap);
  233. break;
  234. case BackgroundPixel:
  235. mask &= ~CWBackPixmap;
  236. break;
  237. }
  238. if (mask & CWBackPixel) {
  239. if (pWin->backgroundState == BackgroundPixel)
  240. attributes.background_pixel = xnestPixel(pWin->background.pixel);
  241. else
  242. mask &= ~CWBackPixel;
  243. }
  244. if (mask & CWBorderPixmap) {
  245. if (pWin->borderIsPixel)
  246. mask &= ~CWBorderPixmap;
  247. else
  248. attributes.border_pixmap = xnestPixmap(pWin->border.pixmap);
  249. }
  250. if (mask & CWBorderPixel) {
  251. if (pWin->borderIsPixel)
  252. attributes.border_pixel = xnestPixel(pWin->border.pixel);
  253. else
  254. mask &= ~CWBorderPixel;
  255. }
  256. if (mask & CWBitGravity)
  257. attributes.bit_gravity = pWin->bitGravity;
  258. if (mask & CWWinGravity) /* dix does this for us */
  259. mask &= ~CWWinGravity;
  260. if (mask & CWBackingStore) /* this is really not useful */
  261. mask &= ~CWBackingStore;
  262. if (mask & CWBackingPlanes) /* this is really not useful */
  263. mask &= ~CWBackingPlanes;
  264. if (mask & CWBackingPixel) /* this is really not useful */
  265. mask &= ~CWBackingPixel;
  266. if (mask & CWOverrideRedirect)
  267. attributes.override_redirect = pWin->overrideRedirect;
  268. if (mask & CWSaveUnder) /* this is really not useful */
  269. mask &= ~CWSaveUnder;
  270. if (mask & CWEventMask) /* events are handled elsewhere */
  271. mask &= ~CWEventMask;
  272. if (mask & CWDontPropagate) /* events are handled elsewhere */
  273. mask &= ~CWDontPropagate;
  274. if (mask & CWColormap) {
  275. ColormapPtr pCmap;
  276. dixLookupResourceByType((void **) &pCmap, wColormap(pWin),
  277. RT_COLORMAP, serverClient, DixUseAccess);
  278. attributes.colormap = xnestColormap(pCmap);
  279. xnestSetInstalledColormapWindows(pWin->drawable.pScreen);
  280. }
  281. if (mask & CWCursor) /* this is handeled in cursor code */
  282. mask &= ~CWCursor;
  283. if (mask)
  284. XChangeWindowAttributes(xnestDisplay, xnestWindow(pWin),
  285. mask, &attributes);
  286. return True;
  287. }
  288. Bool
  289. xnestRealizeWindow(WindowPtr pWin)
  290. {
  291. xnestConfigureWindow(pWin, CWStackingOrder);
  292. xnestShapeWindow(pWin);
  293. XMapWindow(xnestDisplay, xnestWindow(pWin));
  294. return True;
  295. }
  296. Bool
  297. xnestUnrealizeWindow(WindowPtr pWin)
  298. {
  299. XUnmapWindow(xnestDisplay, xnestWindow(pWin));
  300. return True;
  301. }
  302. void
  303. xnestCopyWindow(WindowPtr pWin, xPoint oldOrigin, RegionPtr oldRegion)
  304. {
  305. }
  306. void
  307. xnestClipNotify(WindowPtr pWin, int dx, int dy)
  308. {
  309. xnestConfigureWindow(pWin, CWStackingOrder);
  310. xnestShapeWindow(pWin);
  311. }
  312. static Bool
  313. xnestWindowExposurePredicate(Display * dpy, XEvent * event, XPointer ptr)
  314. {
  315. return (event->type == Expose && event->xexpose.window == *(Window *) ptr);
  316. }
  317. void
  318. xnestWindowExposures(WindowPtr pWin, RegionPtr pRgn, RegionPtr other_exposed)
  319. {
  320. XEvent event;
  321. Window window;
  322. BoxRec Box;
  323. XSync(xnestDisplay, False);
  324. window = xnestWindow(pWin);
  325. while (XCheckIfEvent(xnestDisplay, &event,
  326. xnestWindowExposurePredicate, (char *) &window)) {
  327. Box.x1 = pWin->drawable.x + wBorderWidth(pWin) + event.xexpose.x;
  328. Box.y1 = pWin->drawable.y + wBorderWidth(pWin) + event.xexpose.y;
  329. Box.x2 = Box.x1 + event.xexpose.width;
  330. Box.y2 = Box.y1 + event.xexpose.height;
  331. event.xexpose.type = ProcessedExpose;
  332. if (RegionContainsRect(pRgn, &Box) != rgnIN)
  333. XPutBackEvent(xnestDisplay, &event);
  334. }
  335. miWindowExposures(pWin, pRgn, other_exposed);
  336. }
  337. void
  338. xnestSetShape(WindowPtr pWin, int kind)
  339. {
  340. xnestShapeWindow(pWin);
  341. miSetShape(pWin, kind);
  342. }
  343. static Bool
  344. xnestRegionEqual(RegionPtr pReg1, RegionPtr pReg2)
  345. {
  346. BoxPtr pBox1, pBox2;
  347. unsigned int n1, n2;
  348. if (pReg1 == pReg2)
  349. return True;
  350. if (pReg1 == NullRegion || pReg2 == NullRegion)
  351. return False;
  352. pBox1 = RegionRects(pReg1);
  353. n1 = RegionNumRects(pReg1);
  354. pBox2 = RegionRects(pReg2);
  355. n2 = RegionNumRects(pReg2);
  356. if (n1 != n2)
  357. return False;
  358. if (pBox1 == pBox2)
  359. return True;
  360. if (memcmp(pBox1, pBox2, n1 * sizeof(BoxRec)))
  361. return False;
  362. return True;
  363. }
  364. void
  365. xnestShapeWindow(WindowPtr pWin)
  366. {
  367. Region reg;
  368. BoxPtr pBox;
  369. XRectangle rect;
  370. int i;
  371. if (!xnestRegionEqual(xnestWindowPriv(pWin)->bounding_shape,
  372. wBoundingShape(pWin))) {
  373. if (wBoundingShape(pWin)) {
  374. RegionCopy(xnestWindowPriv(pWin)->bounding_shape,
  375. wBoundingShape(pWin));
  376. reg = XCreateRegion();
  377. pBox = RegionRects(xnestWindowPriv(pWin)->bounding_shape);
  378. for (i = 0;
  379. i < RegionNumRects(xnestWindowPriv(pWin)->bounding_shape);
  380. i++) {
  381. rect.x = pBox[i].x1;
  382. rect.y = pBox[i].y1;
  383. rect.width = pBox[i].x2 - pBox[i].x1;
  384. rect.height = pBox[i].y2 - pBox[i].y1;
  385. XUnionRectWithRegion(&rect, reg, reg);
  386. }
  387. XShapeCombineRegion(xnestDisplay, xnestWindow(pWin),
  388. ShapeBounding, 0, 0, reg, ShapeSet);
  389. XDestroyRegion(reg);
  390. }
  391. else {
  392. RegionEmpty(xnestWindowPriv(pWin)->bounding_shape);
  393. XShapeCombineMask(xnestDisplay, xnestWindow(pWin),
  394. ShapeBounding, 0, 0, None, ShapeSet);
  395. }
  396. }
  397. if (!xnestRegionEqual(xnestWindowPriv(pWin)->clip_shape, wClipShape(pWin))) {
  398. if (wClipShape(pWin)) {
  399. RegionCopy(xnestWindowPriv(pWin)->clip_shape, wClipShape(pWin));
  400. reg = XCreateRegion();
  401. pBox = RegionRects(xnestWindowPriv(pWin)->clip_shape);
  402. for (i = 0;
  403. i < RegionNumRects(xnestWindowPriv(pWin)->clip_shape); i++) {
  404. rect.x = pBox[i].x1;
  405. rect.y = pBox[i].y1;
  406. rect.width = pBox[i].x2 - pBox[i].x1;
  407. rect.height = pBox[i].y2 - pBox[i].y1;
  408. XUnionRectWithRegion(&rect, reg, reg);
  409. }
  410. XShapeCombineRegion(xnestDisplay, xnestWindow(pWin),
  411. ShapeClip, 0, 0, reg, ShapeSet);
  412. XDestroyRegion(reg);
  413. }
  414. else {
  415. RegionEmpty(xnestWindowPriv(pWin)->clip_shape);
  416. XShapeCombineMask(xnestDisplay, xnestWindow(pWin),
  417. ShapeClip, 0, 0, None, ShapeSet);
  418. }
  419. }
  420. }