4 * Copyright © 2003 Keith Packard
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of Keith Packard not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. Keith Packard makes no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
16 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22 * PERFORMANCE OF THIS SOFTWARE.
30 #include <X11/Xutil.h>
31 #include <X11/extensions/Xcomposite.h>
32 #include <X11/extensions/Xdamage.h>
33 #include <X11/extensions/Xrender.h>
43 XserverRegion borderSize;
44 XserverRegion extents;
51 /* for drawing translucent windows */
52 XserverRegion borderClip;
53 struct _win *prev_trans;
66 XserverRegion allDamage;
67 int root_height, root_width;
69 #define BACKGROUND_PROP "_XROOTPMAP_ID"
71 #define WINDOW_SOLID 0
72 #define WINDOW_TRANS 1
75 #define TRANS_OPACITY 0.75
76 #define SHADOW_RADIUS 15
77 #define SHADOW_OPACITY 0.75
78 #define SHADOW_OFFSET_X (-SHADOW_RADIUS)
79 #define SHADOW_OFFSET_Y (-SHADOW_RADIUS)
83 gaussian (double r, double x, double y)
85 return ((1 / (sqrt (2 * M_PI * r))) *
86 exp ((- (x * x + y * y)) / (2 * r * r)));
89 typedef struct _conv {
95 make_gaussian_map (Display *dpy, double r)
98 int size = ((int) ceil ((r * 3)) + 1) & ~1;
99 int center = size / 2;
104 c = malloc (sizeof (conv) + size * size * sizeof (double));
106 c->data = (double *) (c + 1);
107 for (y = 0; y < size; y++)
108 for (x = 0; x < size; x++)
110 g = gaussian (r, (double) (x - center), (double) (y - center));
112 c->data[y * size + x] = g;
114 /* printf ("gaussian total %f\n", t); */
115 for (y = 0; y < size; y++)
116 for (x = 0; x < size; x++)
118 c->data[y*size + x] /= t;
124 * A picture will help
126 * -center 0 width width+center
127 * -center +-----+-------------------+-----+
130 * 0 +-----+-------------------+-----+
134 * height +-----+-------------------+-----+
137 * center +-----+-------------------+-----+
141 sum_gaussian (conv *map, double opacity, int x, int y, int width, int height)
146 double *g_line = map->data;
147 int g_size = map->size;
148 int center = g_size / 2;
149 int fx_start, fx_end;
150 int fy_start, fy_end;
154 * Compute set of filter values which are "in range",
155 * that's the set with:
156 * 0 <= x + (fx-center) && x + (fx-center) < width &&
157 * 0 <= y + (fy-center) && y + (fy-center) < height
159 * 0 <= x + (fx - center) x + fx - center < width
160 * center - x <= fx fx < width + center - x
163 fx_start = center - x;
166 fx_end = width + center - x;
170 fy_start = center - y;
173 fy_end = height + center - y;
177 g_line = g_line + fy_start * g_size + fx_start;
180 for (fy = fy_start; fy < fy_end; fy++)
185 for (fx = fx_start; fx < fx_end; fx++)
191 return ((unsigned int) (v * opacity * 255.0));
195 make_shadow (Display *dpy, double opacity, double r, int width, int height)
197 conv *map = make_gaussian_map (dpy, r);
199 double *gdata = map->data;
201 int gsize = map->size;
203 int swidth = width + gsize;
204 int sheight = height + gsize;
205 int center = gsize / 2;
213 data = malloc (swidth * sheight * sizeof (unsigned char));
214 ximage = XCreateImage (dpy,
215 DefaultVisual(dpy, DefaultScreen(dpy)),
220 swidth, sheight, 8, swidth * sizeof (unsigned char));
222 * Build the gaussian in sections
229 if (ylimit > sheight / 2)
230 ylimit = (sheight + 1) / 2;
232 if (xlimit > swidth / 2)
233 xlimit = (swidth + 1) / 2;
235 for (y = 0; y < ylimit; y++)
236 for (x = 0; x < xlimit; x++)
238 d = sum_gaussian (map, opacity, x - center, y - center, width, height);
239 data[y * swidth + x] = d;
240 data[(sheight - y - 1) * swidth + x] = d;
241 data[(sheight - y - 1) * swidth + (swidth - x - 1)] = d;
242 data[y * swidth + (swidth - x - 1)] = d;
248 for (y = 0; y < ylimit; y++)
250 d = sum_gaussian (map, opacity, center, y - center, width, height);
251 for (x = gsize; x < swidth - gsize; x++)
253 data[y * swidth + x] = d;
254 data[(sheight - y - 1) * swidth + x] = d;
262 for (x = 0; x < xlimit; x++)
264 d = sum_gaussian (map, opacity, x - center, center, width, height);
265 for (y = gsize; y < sheight - gsize; y++)
267 data[y * swidth + x] = d;
268 data[y * swidth + (swidth - x - 1)] = d;
276 d = sum_gaussian (map, opacity, center, center, width, height);
277 for (y = ylimit; y < sheight - ylimit; y++)
278 for (x = xlimit; x < swidth - xlimit; x++)
279 data[y * swidth + x] = d;
286 shadow_picture (Display *dpy, double opacity, double r, int width, int height, int *wp, int *hp)
288 XImage *shadowImage = make_shadow (dpy, opacity, r, width, height);
289 Pixmap shadowPixmap = XCreatePixmap (dpy, root,
293 Picture shadowPicture = XRenderCreatePicture (dpy, shadowPixmap,
294 XRenderFindStandardFormat (dpy, PictStandardA8),
296 GC gc = XCreateGC (dpy, shadowPixmap, 0, 0);
298 XPutImage (dpy, shadowPixmap, gc, shadowImage, 0, 0, 0, 0,
300 shadowImage->height);
301 *wp = shadowImage->width;
302 *hp = shadowImage->height;
304 XDestroyImage (shadowImage);
305 XFreePixmap (dpy, shadowPixmap);
306 return shadowPicture;
310 find_win (Display *dpy, Window id)
314 for (w = list; w; w = w->next)
321 root_tile (Display *dpy)
327 unsigned long nitems;
328 unsigned long bytes_after;
331 XRenderPictureAttributes pa;
333 if (XGetWindowProperty (dpy, root, XInternAtom (dpy, BACKGROUND_PROP, False),
334 0, 4, False, AnyPropertyType,
335 &actual_type, &actual_format, &nitems, &bytes_after, &prop) == Success &&
336 actual_type == XInternAtom (dpy, "PIXMAP", False) && actual_format == 32 && nitems == 1)
338 memcpy (&pixmap, prop, 4);
344 pixmap = XCreatePixmap (dpy, root, 1, 1, DefaultDepth (dpy, scr));
348 picture = XRenderCreatePicture (dpy, pixmap,
349 XRenderFindVisualFormat (dpy,
350 DefaultVisual (dpy, scr)),
356 c.red = c.green = c.blue = 0x8080;
358 XRenderFillRectangle (dpy, PictOpSrc, picture, &c,
365 paint_root (Display *dpy)
368 rootTile = root_tile (dpy);
370 XRenderComposite (dpy, PictOpSrc,
371 rootTile, None, rootBuffer,
372 0, 0, 0, 0, 0, 0, root_width, root_height);
376 win_extents (Display *dpy, win *w)
380 if (w->mode == WINDOW_ARGB)
384 r.width = w->a.width + w->a.border_width * 2;
385 r.height = w->a.height + w->a.border_width * 2;
391 double opacity = SHADOW_OPACITY;
392 if (w->mode == WINDOW_TRANS)
393 opacity = opacity * TRANS_OPACITY;
394 w->shadow = shadow_picture (dpy, opacity, SHADOW_RADIUS,
395 w->a.width, w->a.height,
396 &w->shadow_width, &w->shadow_height);
397 w->shadow_dx = SHADOW_OFFSET_X;
398 w->shadow_dy = SHADOW_OFFSET_Y;
400 r.x = w->a.x + w->a.border_width + w->shadow_dx;
401 r.y = w->a.y + w->a.border_width + w->shadow_dy;
402 r.width = w->shadow_width;
403 r.height = w->shadow_height;
405 return XFixesCreateRegion (dpy, &r, 1);
409 border_size (Display *dpy, win *w)
411 XserverRegion border;
412 border = XFixesCreateRegionFromWindow (dpy, w->id, WindowRegionBounding);
414 XFixesUnionRegion (dpy, border, border, w->a.x, w->a.y, None, 0, 0);
419 paint_all (Display *dpy, XserverRegion region)
429 r.width = root_width;
430 r.height = root_height;
431 region = XFixesCreateRegion (dpy, &r, 1);
435 Pixmap rootPixmap = XCreatePixmap (dpy, root, root_width, root_height,
436 DefaultDepth (dpy, scr));
437 rootBuffer = XRenderCreatePicture (dpy, rootPixmap,
438 XRenderFindVisualFormat (dpy,
439 DefaultVisual (dpy, scr)),
441 XFreePixmap (dpy, rootPixmap);
443 XFixesSetPictureClipRegion (dpy, rootPicture, 0, 0, region);
444 for (w = list; w; w = w->next)
448 if (w->a.map_state != IsViewable)
454 XFixesDestroyRegion (dpy, w->borderSize);
455 w->borderSize = border_size (dpy, w);
457 XFixesDestroyRegion (dpy, w->extents);
458 w->extents = win_extents (dpy, w);
459 if (w->mode == WINDOW_SOLID)
461 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
462 XFixesSubtractRegion (dpy, region, region, 0, 0, w->borderSize, 0, 0);
463 XRenderComposite (dpy, PictOpSrc, w->picture, None, rootBuffer,
465 w->a.x + w->a.border_width,
466 w->a.y + w->a.border_width,
470 w->borderClip = XFixesCreateRegion (dpy, 0, 0);
471 XFixesUnionRegion (dpy, w->borderClip, region, 0, 0, None, 0, 0);
475 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
477 for (w = t; w; w = w->prev_trans)
479 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, w->borderClip);
482 XRenderComposite (dpy, PictOpOver, blackPicture, w->shadow, rootBuffer,
484 w->a.x + w->a.border_width + w->shadow_dx,
485 w->a.y + w->a.border_width + w->shadow_dy,
486 w->shadow_width, w->shadow_height);
488 if (w->mode == WINDOW_TRANS)
489 XRenderComposite (dpy, PictOpOver, w->picture, transPicture, rootBuffer,
491 w->a.x + w->a.border_width,
492 w->a.y + w->a.border_width,
495 else if (w->mode == WINDOW_ARGB)
496 XRenderComposite (dpy, PictOpOver, w->picture, None, rootBuffer,
498 w->a.x + w->a.border_width,
499 w->a.y + w->a.border_width,
502 XFixesDestroyRegion (dpy, w->borderClip);
503 w->borderClip = None;
505 XFixesDestroyRegion (dpy, region);
506 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, None);
507 XRenderComposite (dpy, PictOpSrc, rootBuffer, None, rootPicture,
508 0, 0, 0, 0, 0, 0, root_width, root_height);
512 add_damage (Display *dpy, XserverRegion damage)
516 XFixesUnionRegion (dpy, allDamage, allDamage, 0, 0, damage, 0, 0);
517 XFixesDestroyRegion (dpy, damage);
524 repair_win (Display *dpy, Window id)
526 win *w = find_win (dpy, id);
531 /* printf ("repair 0x%x\n", w->id); */
532 parts = XFixesCreateRegion (dpy, 0, 0);
533 /* translate region */
534 XDamageSubtract (dpy, w->damage, None, parts);
535 XFixesUnionRegion (dpy, parts, parts, w->a.x, w->a.y, None, 0, 0);
536 add_damage (dpy, parts);
540 map_win (Display *dpy, Window id)
542 win *w = find_win (dpy, id);
543 XserverRegion region;
547 w->a.map_state = IsViewable;
550 w->damage = XDamageCreate (dpy, id, XDamageReportNonEmpty);
551 region = win_extents (dpy, w);
552 add_damage (dpy, region);
557 unmap_win (Display *dpy, Window id)
559 win *w = find_win (dpy, id);
563 w->a.map_state = IsUnmapped;
564 if (w->damage != None)
566 XDamageDestroy (dpy, w->damage);
569 if (w->extents != None)
571 add_damage (dpy, w->extents); /* destroys region */
577 add_win (Display *dpy, Window id, Window prev)
579 win *new = malloc (sizeof (win));
582 XRenderPictureAttributes pa;
583 XRenderPictFormat *format;
589 for (p = &list; *p; p = &(*p)->next)
590 if ((*p)->id == prev)
596 if (!XGetWindowAttributes (dpy, id, &new->a))
603 pa.subwindow_mode = IncludeInferiors;
604 if (new->a.class == InputOnly)
611 format = XRenderFindVisualFormat (dpy, new->a.visual);
612 new->picture = XRenderCreatePicture (dpy, id,
619 new->borderSize = None;
621 if (format && format->type == PictTypeDirect && format->direct.alphaMask)
622 new->mode = WINDOW_ARGB;
623 else if (new->a.override_redirect)
624 new->mode = WINDOW_TRANS;
626 new->mode = WINDOW_SOLID;
629 if (new->a.map_state == IsViewable)
634 configure_win (Display *dpy, XConfigureEvent *ce)
636 win *w = find_win (dpy, ce->window);
638 XserverRegion damage = None;
642 if (ce->window == root)
646 XRenderFreePicture (dpy, rootBuffer);
649 root_width = ce->width;
650 root_height = ce->height;
654 if (w->a.map_state == IsViewable)
656 damage = XFixesCreateRegion (dpy, 0, 0);
657 if (w->extents != None)
658 XFixesUnionRegion (dpy, damage, w->extents, 0, 0, None, 0, 0);
662 if (w->a.width != ce->width || w->a.height != ce->height)
665 XRenderFreePicture (dpy, w->shadow);
668 w->a.width = ce->width;
669 w->a.height = ce->height;
670 w->a.border_width = ce->border_width;
671 w->a.override_redirect = ce->override_redirect;
676 if (above != ce->above)
681 for (prev = &list; *prev; prev = &(*prev)->next)
687 for (prev = &list; *prev; prev = &(*prev)->next)
689 if ((*prev)->id == ce->above)
697 XserverRegion extents = win_extents (dpy, w);
698 XFixesUnionRegion (dpy, damage, damage, 0, 0, extents, 0, 0);
699 XFixesDestroyRegion (dpy, extents);
700 add_damage (dpy, damage);
705 destroy_win (Display *dpy, Window id, Bool gone)
709 for (prev = &list; w = *prev; prev = &w->next)
716 XRenderFreePicture (dpy, w->picture);
727 printf ("\t%08x: %d x %d + %d + %d (%d)\n", w->id,
728 w->a.width, w->a.height, w->a.x, w->a.y, w->a.border_width);
736 printf ("windows:\n");
737 for (w = list; w; w = w->next)
742 damage_win (Display *dpy, XDamageNotifyEvent *de)
744 repair_win (dpy, de->drawable);
748 error (Display *dpy, XErrorEvent *ev)
750 printf ("error %d request %d minor %d\n",
751 ev->error_code, ev->request_code, ev->minor_code);
755 expose_root (Display *dpy, Window root, XRectangle *rects, int nrects)
757 XserverRegion region = XFixesCreateRegion (dpy, rects, nrects);
759 add_damage (dpy, region);
767 gettimeofday (&tp, 0);
768 return(tp.tv_sec * 1000) + (tp.tv_usec / 1000);
776 int event_base, error_base;
777 Window root_return, parent_return;
781 unsigned int nchildren;
783 int damage_event, damage_error;
784 int xfixes_event, xfixes_error;
785 XRenderPictureAttributes pa;
787 XRectangle *expose_rects = 0;
797 dpy = XOpenDisplay (0);
800 fprintf (stderr, "Can't open display\n");
803 XSetErrorHandler (error);
804 scr = DefaultScreen (dpy);
805 root = RootWindow (dpy, scr);
806 pa.subwindow_mode = IncludeInferiors;
807 transPixmap = XCreatePixmap (dpy, root, 1, 1, 8);
809 transPicture = XRenderCreatePicture (dpy, transPixmap,
810 XRenderFindStandardFormat (dpy, PictStandardA8),
813 c.red = c.green = c.blue = 0;
815 XRenderFillRectangle (dpy, PictOpSrc, transPicture, &c, 0, 0, 1, 1);
817 root_width = DisplayWidth (dpy, scr);
818 root_height = DisplayHeight (dpy, scr);
820 rootPicture = XRenderCreatePicture (dpy, root,
821 XRenderFindVisualFormat (dpy,
822 DefaultVisual (dpy, scr)),
825 blackPixmap = XCreatePixmap (dpy, root, 1, 1, 32);
827 blackPicture = XRenderCreatePicture (dpy, blackPixmap,
828 XRenderFindStandardFormat (dpy, PictStandardARGB32),
831 c.red = c.green = c.blue = 0;
833 XRenderFillRectangle (dpy, PictOpSrc, blackPicture, &c, 0, 0, 1, 1);
834 if (!XCompositeQueryExtension (dpy, &event_base, &error_base))
836 fprintf (stderr, "No composite extension\n");
839 printf ("Composite error %d\n", error_base);
840 if (!XDamageQueryExtension (dpy, &damage_event, &damage_error))
842 fprintf (stderr, "No damage extension\n");
845 printf ("Damage error %d\n", damage_error);
846 if (!XFixesQueryExtension (dpy, &xfixes_event, &xfixes_error))
848 fprintf (stderr, "No XFixes extension\n");
851 printf ("XFixes error %d\n", xfixes_error);
854 XCompositeRedirectSubwindows (dpy, root, CompositeRedirectManual);
855 XSelectInput (dpy, root,
856 SubstructureNotifyMask|
860 XQueryTree (dpy, root, &root_return, &parent_return, &children, &nchildren);
861 for (i = 0; i < nchildren; i++)
862 add_win (dpy, children[i], i ? children[i-1] : None);
865 paint_all (dpy, None);
866 last_update = time_in_millis ();
872 XNextEvent (dpy, &ev);
874 busy_start = time_in_millis();
875 /* printf ("event %d\n", ev.type); */
878 add_win (dpy, ev.xcreatewindow.window, 0);
880 case ConfigureNotify:
881 configure_win (dpy, &ev.xconfigure);
884 destroy_win (dpy, ev.xdestroywindow.window, True);
887 map_win (dpy, ev.xmap.window);
890 unmap_win (dpy, ev.xunmap.window);
893 if (ev.xreparent.parent == root)
894 add_win (dpy, ev.xreparent.window, 0);
896 destroy_win (dpy, ev.xreparent.window, False);
899 if (ev.xexpose.window == root)
901 int more = ev.xexpose.count + 1;
902 if (n_expose == size_expose)
906 expose_rects = realloc (expose_rects,
907 (size_expose + more) *
908 sizeof (XRectangle));
913 expose_rects = malloc (more * sizeof (XRectangle));
917 expose_rects[n_expose].x = ev.xexpose.x;
918 expose_rects[n_expose].y = ev.xexpose.y;
919 expose_rects[n_expose].width = ev.xexpose.width;
920 expose_rects[n_expose].height = ev.xexpose.height;
922 if (ev.xexpose.count == 0)
924 expose_root (dpy, root, expose_rects, n_expose);
930 if (ev.xproperty.atom == XInternAtom (dpy, BACKGROUND_PROP, False))
934 XClearArea (dpy, root, 0, 0, 0, 0, True);
935 XRenderFreePicture (dpy, rootTile);
941 if (ev.type == damage_event + XDamageNotify)
942 damage_win (dpy, (XDamageNotifyEvent *) &ev);
945 } while (XEventsQueued (dpy, QueuedAfterReading));
946 now = time_in_millis ();
947 /* printf ("\t\tbusy %d\n", now - busy_start); */
948 timeout = INTERVAL - (now - last_update);
951 ufd.fd = ConnectionNumber (dpy);
953 n = poll (&ufd, 1, timeout);
954 if (n > 0 && (ufd.revents & POLLIN) && XEventsQueued (dpy, QueuedAfterReading))
959 int old_update = last_update;
960 last_update = time_in_millis();
961 /* printf ("delta %d\n", last_update - old_update); */
962 paint_all (dpy, allDamage);