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;
65 XserverRegion allDamage;
66 int root_height, root_width;
68 #define BACKGROUND_PROP "_XROOTPMAP_ID"
69 #define WINDOW_PLAIN 0
71 #define WINDOW_TRANS 2
72 #define TRANS_OPACITY 0.75
73 #define SHADOW_RADIUS 15
74 #define SHADOW_OPACITY 0.75
75 #define SHADOW_OFFSET_X (-SHADOW_RADIUS)
76 #define SHADOW_OFFSET_Y (-SHADOW_RADIUS)
80 gaussian (double r, double x, double y)
82 return ((1 / (sqrt (2 * M_PI * r))) *
83 exp ((- (x * x + y * y)) / (2 * r * r)));
86 typedef struct _conv {
92 make_gaussian_map (Display *dpy, double r)
95 int size = ((int) ceil ((r * 3)) + 1) & ~1;
96 int center = size / 2;
101 c = malloc (sizeof (conv) + size * size * sizeof (double));
103 c->data = (double *) (c + 1);
104 for (y = 0; y < size; y++)
105 for (x = 0; x < size; x++)
107 g = gaussian (r, (double) (x - center), (double) (y - center));
109 c->data[y * size + x] = g;
111 printf ("gaussian total %f\n", t);
112 for (y = 0; y < size; y++)
113 for (x = 0; x < size; x++)
115 c->data[y*size + x] /= t;
121 * A picture will help
123 * -center 0 width width+center
124 * -center +-----+-------------------+-----+
127 * 0 +-----+-------------------+-----+
131 * height +-----+-------------------+-----+
134 * center +-----+-------------------+-----+
138 sum_gaussian (conv *map, double opacity, int x, int y, int width, int height)
143 double *g_line = map->data;
144 int g_size = map->size;
145 int center = g_size / 2;
146 int fx_start, fx_end;
147 int fy_start, fy_end;
151 * Compute set of filter values which are "in range",
152 * that's the set with:
153 * 0 <= x + (fx-center) && x + (fx-center) < width &&
154 * 0 <= y + (fy-center) && y + (fy-center) < height
156 * 0 <= x + (fx - center) x + fx - center < width
157 * center - x <= fx fx < width + center - x
160 fx_start = center - x;
163 fx_end = width + center - x;
167 fy_start = center - y;
170 fy_end = height + center - y;
174 g_line = g_line + fy_start * g_size + fx_start;
177 for (fy = fy_start; fy < fy_end; fy++)
182 for (fx = fx_start; fx < fx_end; fx++)
188 return ((unsigned int) (v * opacity * 255.0)) << 24;
192 make_shadow (Display *dpy, double opacity, double r, int width, int height)
194 conv *map = make_gaussian_map (dpy, r);
196 double *gdata = map->data;
198 int gsize = map->size;
200 int swidth = width + gsize;
201 int sheight = height + gsize;
202 int center = gsize / 2;
210 data = malloc (swidth * sheight * sizeof (int));
211 ximage = XCreateImage (dpy,
212 DefaultVisual(dpy, DefaultScreen(dpy)),
217 swidth, sheight, 32, swidth * sizeof (int));
219 * Build the gaussian in sections
226 if (ylimit > sheight / 2)
227 ylimit = (sheight + 1) / 2;
229 if (xlimit > swidth / 2)
230 xlimit = (swidth + 1) / 2;
232 for (y = 0; y < ylimit; y++)
233 for (x = 0; x < xlimit; x++)
235 d = sum_gaussian (map, opacity, x - center, y - center, width, height);
236 data[y * swidth + x] = d;
237 data[(sheight - y - 1) * swidth + x] = d;
238 data[(sheight - y - 1) * swidth + (swidth - x - 1)] = d;
239 data[y * swidth + (swidth - x - 1)] = d;
245 for (y = 0; y < ylimit; y++)
247 d = sum_gaussian (map, opacity, center, y - center, width, height);
248 for (x = gsize; x < swidth - gsize; x++)
250 data[y * swidth + x] = d;
251 data[(sheight - y - 1) * swidth + x] = d;
259 for (x = 0; x < xlimit; x++)
261 d = sum_gaussian (map, opacity, x - center, center, width, height);
262 for (y = gsize; y < sheight - gsize; y++)
264 data[y * swidth + x] = d;
265 data[y * swidth + (swidth - x - 1)] = d;
273 d = sum_gaussian (map, opacity, center, center, width, height);
274 for (y = ylimit; y < sheight - ylimit; y++)
275 for (x = xlimit; x < swidth - xlimit; x++)
276 data[y * swidth + x] = d;
283 shadow_picture (Display *dpy, double opacity, double r, int width, int height, int *wp, int *hp)
285 XImage *shadowImage = make_shadow (dpy, opacity, r, width, height);
286 Pixmap shadowPixmap = XCreatePixmap (dpy, root,
290 Picture shadowPicture = XRenderCreatePicture (dpy, shadowPixmap,
291 XRenderFindStandardFormat (dpy, PictStandardARGB32),
293 GC gc = XCreateGC (dpy, shadowPixmap, 0, 0);
295 XPutImage (dpy, shadowPixmap, gc, shadowImage, 0, 0, 0, 0,
297 shadowImage->height);
298 *wp = shadowImage->width;
299 *hp = shadowImage->height;
301 XDestroyImage (shadowImage);
302 XFreePixmap (dpy, shadowPixmap);
303 return shadowPicture;
307 find_win (Display *dpy, Window id)
311 for (w = list; w; w = w->next)
318 root_tile (Display *dpy)
324 unsigned long nitems;
325 unsigned long bytes_after;
328 XRenderPictureAttributes pa;
330 if (XGetWindowProperty (dpy, root, XInternAtom (dpy, BACKGROUND_PROP, False),
331 0, 4, False, AnyPropertyType,
332 &actual_type, &actual_format, &nitems, &bytes_after, &prop) == Success &&
333 actual_type == XInternAtom (dpy, "PIXMAP", False) && actual_format == 32 && nitems == 1)
335 memcpy (&pixmap, prop, 4);
341 pixmap = XCreatePixmap (dpy, root, 1, 1, DefaultDepth (dpy, scr));
345 picture = XRenderCreatePicture (dpy, pixmap,
346 XRenderFindVisualFormat (dpy,
347 DefaultVisual (dpy, scr)),
353 c.red = c.green = c.blue = 0x8080;
355 XRenderFillRectangle (dpy, PictOpSrc, picture, &c,
362 paint_root (Display *dpy)
365 rootTile = root_tile (dpy);
367 XRenderComposite (dpy, PictOpSrc,
368 rootTile, None, rootBuffer,
369 0, 0, 0, 0, 0, 0, root_width, root_height);
373 win_extents (Display *dpy, win *w)
379 double opacity = SHADOW_OPACITY;
380 if (w->mode == WINDOW_TRANS)
381 opacity = opacity * TRANS_OPACITY;
382 w->shadow = shadow_picture (dpy, opacity, SHADOW_RADIUS,
383 w->a.width, w->a.height,
384 &w->shadow_width, &w->shadow_height);
385 w->shadow_dx = SHADOW_OFFSET_X;
386 w->shadow_dy = SHADOW_OFFSET_Y;
388 r.x = w->a.x + w->a.border_width + w->shadow_dx;
389 r.y = w->a.y + w->a.border_width + w->shadow_dy;
390 r.width = w->shadow_width;
391 r.height = w->shadow_height;
392 return XFixesCreateRegion (dpy, &r, 1);
396 border_size (Display *dpy, win *w)
398 XserverRegion border;
399 border = XFixesCreateRegionFromWindow (dpy, w->id, WindowRegionBounding);
401 XFixesUnionRegion (dpy, border, border, w->a.x, w->a.y, None, 0, 0);
406 paint_all (Display *dpy, XserverRegion region)
416 r.width = root_width;
417 r.height = root_height;
418 region = XFixesCreateRegion (dpy, &r, 1);
422 Pixmap rootPixmap = XCreatePixmap (dpy, root, root_width, root_height,
423 DefaultDepth (dpy, scr));
424 rootBuffer = XRenderCreatePicture (dpy, rootPixmap,
425 XRenderFindVisualFormat (dpy,
426 DefaultVisual (dpy, scr)),
428 XFreePixmap (dpy, rootPixmap);
430 XFixesSetPictureClipRegion (dpy, rootPicture, 0, 0, region);
431 for (w = list; w; w = w->next)
435 if (w->a.map_state != IsViewable)
441 XFixesDestroyRegion (dpy, w->borderSize);
442 w->borderSize = border_size (dpy, w);
444 XFixesDestroyRegion (dpy, w->extents);
445 w->extents = win_extents (dpy, w);
446 if (w->mode != WINDOW_TRANS)
448 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
449 XFixesSubtractRegion (dpy, region, region, 0, 0, w->borderSize, 0, 0);
450 XRenderComposite (dpy, PictOpSrc, w->picture, None, rootBuffer,
452 w->a.x + w->a.border_width,
453 w->a.y + w->a.border_width,
457 w->borderClip = XFixesCreateRegion (dpy, 0, 0);
458 XFixesUnionRegion (dpy, w->borderClip, region, 0, 0, None, 0, 0);
462 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
464 for (w = t; w; w = w->prev_trans)
466 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, w->borderClip);
469 XRenderComposite (dpy, PictOpOver, w->shadow, None, rootBuffer,
471 w->a.x + w->a.border_width + w->shadow_dx,
472 w->a.y + w->a.border_width + w->shadow_dy,
473 w->shadow_width, w->shadow_height);
475 if (w->mode == WINDOW_TRANS)
476 XRenderComposite (dpy, PictOpOver, w->picture, transPicture, rootBuffer,
478 w->a.x + w->a.border_width,
479 w->a.y + w->a.border_width,
482 XFixesDestroyRegion (dpy, w->borderClip);
483 w->borderClip = None;
485 XFixesDestroyRegion (dpy, region);
486 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, None);
487 XRenderComposite (dpy, PictOpSrc, rootBuffer, None, rootPicture,
488 0, 0, 0, 0, 0, 0, root_width, root_height);
492 add_damage (Display *dpy, XserverRegion damage)
496 XFixesUnionRegion (dpy, allDamage, allDamage, 0, 0, damage, 0, 0);
497 XFixesDestroyRegion (dpy, damage);
504 repair_win (Display *dpy, Window id)
506 win *w = find_win (dpy, id);
511 /* printf ("repair 0x%x\n", w->id); */
512 parts = XFixesCreateRegion (dpy, 0, 0);
513 /* translate region */
514 XDamageSubtract (dpy, w->damage, None, parts);
515 XFixesUnionRegion (dpy, parts, parts, w->a.x, w->a.y, None, 0, 0);
516 add_damage (dpy, parts);
520 map_win (Display *dpy, Window id)
522 win *w = find_win (dpy, id);
523 XserverRegion region;
527 w->a.map_state = IsViewable;
530 w->damage = XDamageCreate (dpy, id, XDamageReportNonEmpty);
531 region = win_extents (dpy, w);
532 add_damage (dpy, region);
537 unmap_win (Display *dpy, Window id)
539 win *w = find_win (dpy, id);
543 w->a.map_state = IsUnmapped;
544 if (w->damage != None)
546 XDamageDestroy (dpy, w->damage);
549 if (w->extents != None)
551 add_damage (dpy, w->extents); /* destroys region */
557 add_win (Display *dpy, Window id, Window prev)
559 win *new = malloc (sizeof (win));
562 XRenderPictureAttributes pa;
568 for (p = &list; *p; p = &(*p)->next)
569 if ((*p)->id == prev)
575 if (!XGetWindowAttributes (dpy, id, &new->a))
582 pa.subwindow_mode = IncludeInferiors;
583 if (new->a.class == InputOnly)
587 new->picture = XRenderCreatePicture (dpy, id,
588 XRenderFindVisualFormat (dpy,
595 new->borderSize = None;
597 if (new->a.override_redirect)
598 new->mode = WINDOW_TRANS;
600 new->mode = WINDOW_DROP;
603 if (new->a.map_state == IsViewable)
608 configure_win (Display *dpy, XConfigureEvent *ce)
610 win *w = find_win (dpy, ce->window);
612 XserverRegion damage = None;
616 if (ce->window == root)
620 XRenderFreePicture (dpy, rootBuffer);
623 root_width = ce->width;
624 root_height = ce->height;
628 if (w->a.map_state == IsViewable)
630 damage = XFixesCreateRegion (dpy, 0, 0);
631 if (w->extents != None)
632 XFixesUnionRegion (dpy, damage, w->extents, 0, 0, None, 0, 0);
636 if (w->a.width != ce->width || w->a.height != ce->height)
639 XRenderFreePicture (dpy, w->shadow);
642 w->a.width = ce->width;
643 w->a.height = ce->height;
644 w->a.border_width = ce->border_width;
645 w->a.override_redirect = ce->override_redirect;
650 if (above != ce->above)
655 for (prev = &list; *prev; prev = &(*prev)->next)
661 for (prev = &list; *prev; prev = &(*prev)->next)
663 if ((*prev)->id == ce->above)
671 XserverRegion extents = win_extents (dpy, w);
672 XFixesUnionRegion (dpy, damage, damage, 0, 0, extents, 0, 0);
673 XFixesDestroyRegion (dpy, extents);
674 add_damage (dpy, damage);
679 destroy_win (Display *dpy, Window id, Bool gone)
683 for (prev = &list; w = *prev; prev = &w->next)
690 XRenderFreePicture (dpy, w->picture);
701 printf ("\t%08x: %d x %d + %d + %d (%d)\n", w->id,
702 w->a.width, w->a.height, w->a.x, w->a.y, w->a.border_width);
710 printf ("windows:\n");
711 for (w = list; w; w = w->next)
716 damage_win (Display *dpy, XDamageNotifyEvent *de)
718 repair_win (dpy, de->drawable);
722 error (Display *dpy, XErrorEvent *ev)
724 printf ("error %d request %d minor %d\n",
725 ev->error_code, ev->request_code, ev->minor_code);
729 expose_root (Display *dpy, Window root, XRectangle *rects, int nrects)
731 XserverRegion region = XFixesCreateRegion (dpy, rects, nrects);
733 add_damage (dpy, region);
741 gettimeofday (&tp, 0);
742 return(tp.tv_sec * 1000) + (tp.tv_usec / 1000);
750 int event_base, error_base;
751 Window root_return, parent_return;
754 unsigned int nchildren;
756 int damage_event, damage_error;
757 int xfixes_event, xfixes_error;
758 XRenderPictureAttributes pa;
760 XRectangle *expose_rects = 0;
770 dpy = XOpenDisplay (0);
773 fprintf (stderr, "Can't open display\n");
776 XSetErrorHandler (error);
777 scr = DefaultScreen (dpy);
778 root = RootWindow (dpy, scr);
779 pa.subwindow_mode = IncludeInferiors;
780 transPixmap = XCreatePixmap (dpy, root, 1, 1, 8);
782 transPicture = XRenderCreatePicture (dpy, transPixmap,
783 XRenderFindStandardFormat (dpy, PictStandardA8),
786 c.red = c.green = c.blue = 0;
788 XRenderFillRectangle (dpy, PictOpSrc, transPicture, &c, 0, 0, 1, 1);
790 root_width = DisplayWidth (dpy, scr);
791 root_height = DisplayHeight (dpy, scr);
793 rootPicture = XRenderCreatePicture (dpy, root,
794 XRenderFindVisualFormat (dpy,
795 DefaultVisual (dpy, scr)),
798 if (!XCompositeQueryExtension (dpy, &event_base, &error_base))
800 fprintf (stderr, "No composite extension\n");
803 printf ("Composite error %d\n", error_base);
804 if (!XDamageQueryExtension (dpy, &damage_event, &damage_error))
806 fprintf (stderr, "No damage extension\n");
809 printf ("Damage error %d\n", damage_error);
810 if (!XFixesQueryExtension (dpy, &xfixes_event, &xfixes_error))
812 fprintf (stderr, "No XFixes extension\n");
815 printf ("XFixes error %d\n", xfixes_error);
818 XCompositeRedirectSubwindows (dpy, root, CompositeRedirectManual);
819 paint_all (dpy, None);
820 XSelectInput (dpy, root,
821 SubstructureNotifyMask|
825 XQueryTree (dpy, root, &root_return, &parent_return, &children, &nchildren);
826 for (i = 0; i < nchildren; i++)
827 add_win (dpy, children[i], i ? children[i-1] : None);
830 last_update = time_in_millis ();
835 XNextEvent (dpy, &ev);
836 /* printf ("event %d\n", ev.type); */
839 add_win (dpy, ev.xcreatewindow.window, 0);
841 case ConfigureNotify:
842 configure_win (dpy, &ev.xconfigure);
845 destroy_win (dpy, ev.xdestroywindow.window, True);
848 map_win (dpy, ev.xmap.window);
851 unmap_win (dpy, ev.xunmap.window);
854 if (ev.xreparent.parent == root)
855 add_win (dpy, ev.xreparent.window, 0);
857 destroy_win (dpy, ev.xreparent.window, False);
860 if (ev.xexpose.window == root)
862 int more = ev.xexpose.count + 1;
863 if (n_expose == size_expose)
867 expose_rects = realloc (expose_rects,
868 (size_expose + more) *
869 sizeof (XRectangle));
874 expose_rects = malloc (more * sizeof (XRectangle));
878 expose_rects[n_expose].x = ev.xexpose.x;
879 expose_rects[n_expose].y = ev.xexpose.y;
880 expose_rects[n_expose].width = ev.xexpose.width;
881 expose_rects[n_expose].height = ev.xexpose.height;
883 if (ev.xexpose.count == 0)
885 expose_root (dpy, root, expose_rects, n_expose);
891 if (ev.xproperty.atom == XInternAtom (dpy, BACKGROUND_PROP, False))
895 XClearArea (dpy, root, 0, 0, 0, 0, True);
896 XRenderFreePicture (dpy, rootTile);
902 if (ev.type == damage_event + XDamageNotify)
903 damage_win (dpy, (XDamageNotifyEvent *) &ev);
906 } while (XEventsQueued (dpy, QueuedAfterReading));
907 now = time_in_millis ();
908 timeout = INTERVAL - (now - last_update);
911 ufd.fd = ConnectionNumber (dpy);
913 n = poll (&ufd, 1, timeout);
914 if (n > 0 && (ufd.revents & POLLIN) && XEventsQueued (dpy, QueuedAfterReading))
917 last_update = time_in_millis();
920 paint_all (dpy, allDamage);