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 WINDOW_PLAIN 0
70 #define WINDOW_TRANS 2
71 #define TRANS_OPACITY 0.75
72 #define SHADOW_RADIUS 15
73 #define SHADOW_OPACITY 0.75
74 #define SHADOW_OFFSET_X (-SHADOW_RADIUS)
75 #define SHADOW_OFFSET_Y (-SHADOW_RADIUS)
79 gaussian (double r, double x, double y)
81 return ((1 / (sqrt (2 * M_PI * r))) *
82 exp ((- (x * x + y * y)) / (2 * r * r)));
85 typedef struct _conv {
91 make_gaussian_map (Display *dpy, double r)
94 int size = ((int) ceil ((r * 3)) + 1) & ~1;
95 int center = size / 2;
100 c = malloc (sizeof (conv) + size * size * sizeof (double));
102 c->data = (double *) (c + 1);
103 for (y = 0; y < size; y++)
104 for (x = 0; x < size; x++)
106 g = gaussian (r, (double) (x - center), (double) (y - center));
108 c->data[y * size + x] = g;
110 printf ("gaussian total %f\n", t);
111 for (y = 0; y < size; y++)
112 for (x = 0; x < size; x++)
114 c->data[y*size + x] /= t;
120 * A picture will help
122 * -center 0 width width+center
123 * -center +-----+-------------------+-----+
126 * 0 +-----+-------------------+-----+
130 * height +-----+-------------------+-----+
133 * center +-----+-------------------+-----+
137 sum_gaussian (conv *map, double opacity, int x, int y, int width, int height)
142 double *g_line = map->data;
143 int g_size = map->size;
144 int center = g_size / 2;
145 int fx_start, fx_end;
146 int fy_start, fy_end;
150 * Compute set of filter values which are "in range",
151 * that's the set with:
152 * 0 <= x + (fx-center) && x + (fx-center) < width &&
153 * 0 <= y + (fy-center) && y + (fy-center) < height
155 * 0 <= x + (fx - center) x + fx - center < width
156 * center - x <= fx fx < width + center - x
159 fx_start = center - x;
162 fx_end = width + center - x;
166 fy_start = center - y;
169 fy_end = height + center - y;
173 g_line = g_line + fy_start * g_size + fx_start;
176 for (fy = fy_start; fy < fy_end; fy++)
181 for (fx = fx_start; fx < fx_end; fx++)
187 return ((unsigned int) (v * opacity * 255.0)) << 24;
191 make_shadow (Display *dpy, double opacity, double r, int width, int height)
193 conv *map = make_gaussian_map (dpy, r);
195 double *gdata = map->data;
197 int gsize = map->size;
199 int swidth = width + gsize;
200 int sheight = height + gsize;
201 int center = gsize / 2;
209 data = malloc (swidth * sheight * sizeof (int));
210 ximage = XCreateImage (dpy,
211 DefaultVisual(dpy, DefaultScreen(dpy)),
216 swidth, sheight, 32, swidth * sizeof (int));
218 * Build the gaussian in sections
225 if (ylimit > sheight / 2)
226 ylimit = (sheight + 1) / 2;
228 if (xlimit > swidth / 2)
229 xlimit = (swidth + 1) / 2;
231 for (y = 0; y < ylimit; y++)
232 for (x = 0; x < xlimit; x++)
234 d = sum_gaussian (map, opacity, x - center, y - center, width, height);
235 data[y * swidth + x] = d;
236 data[(sheight - y - 1) * swidth + x] = d;
237 data[(sheight - y - 1) * swidth + (swidth - x - 1)] = d;
238 data[y * swidth + (swidth - x - 1)] = d;
244 for (y = 0; y < ylimit; y++)
246 d = sum_gaussian (map, opacity, center, y - center, width, height);
247 for (x = gsize; x < swidth - gsize; x++)
249 data[y * swidth + x] = d;
250 data[(sheight - y - 1) * swidth + x] = d;
258 for (x = 0; x < xlimit; x++)
260 d = sum_gaussian (map, opacity, x - center, center, width, height);
261 for (y = gsize; y < sheight - gsize; y++)
263 data[y * swidth + x] = d;
264 data[y * swidth + (swidth - x - 1)] = d;
272 d = sum_gaussian (map, opacity, center, center, width, height);
273 for (y = ylimit; y < sheight - ylimit; y++)
274 for (x = xlimit; x < swidth - xlimit; x++)
275 data[y * swidth + x] = d;
282 shadow_picture (Display *dpy, double opacity, double r, int width, int height, int *wp, int *hp)
284 XImage *shadowImage = make_shadow (dpy, opacity, r, width, height);
285 Pixmap shadowPixmap = XCreatePixmap (dpy, root,
289 Picture shadowPicture = XRenderCreatePicture (dpy, shadowPixmap,
290 XRenderFindStandardFormat (dpy, PictStandardARGB32),
292 GC gc = XCreateGC (dpy, shadowPixmap, 0, 0);
294 XPutImage (dpy, shadowPixmap, gc, shadowImage, 0, 0, 0, 0,
296 shadowImage->height);
297 *wp = shadowImage->width;
298 *hp = shadowImage->height;
300 XDestroyImage (shadowImage);
301 XFreePixmap (dpy, shadowPixmap);
302 return shadowPicture;
306 find_win (Display *dpy, Window id)
310 for (w = list; w; w = w->next)
317 root_tile (Display *dpy)
323 unsigned long nitems;
324 unsigned long bytes_after;
327 XRenderPictureAttributes pa;
329 if (XGetWindowProperty (dpy, root, XInternAtom (dpy, "_XROOTPMAP_ID", False),
330 0, 4, False, AnyPropertyType,
331 &actual_type, &actual_format, &nitems, &bytes_after, &prop) == Success)
333 memcpy (&pixmap, prop, 4);
339 pixmap = XCreatePixmap (dpy, root, 1, 1, DefaultDepth (dpy, scr));
343 picture = XRenderCreatePicture (dpy, pixmap,
344 XRenderFindVisualFormat (dpy,
345 DefaultVisual (dpy, scr)),
351 c.red = c.green = c.blue = 0x8080;
353 XRenderFillRectangle (dpy, PictOpSrc, picture, &c,
360 paint_root (Display *dpy)
363 rootTile = root_tile (dpy);
365 XRenderComposite (dpy, PictOpSrc,
366 rootTile, None, rootBuffer,
367 0, 0, 0, 0, 0, 0, root_width, root_height);
371 win_extents (Display *dpy, win *w)
377 double opacity = SHADOW_OPACITY;
378 if (w->mode == WINDOW_TRANS)
379 opacity = opacity * TRANS_OPACITY;
380 w->shadow = shadow_picture (dpy, opacity, SHADOW_RADIUS,
381 w->a.width, w->a.height,
382 &w->shadow_width, &w->shadow_height);
383 w->shadow_dx = SHADOW_OFFSET_X;
384 w->shadow_dy = SHADOW_OFFSET_Y;
386 r.x = w->a.x + w->a.border_width + w->shadow_dx;
387 r.y = w->a.y + w->a.border_width + w->shadow_dy;
388 r.width = w->shadow_width;
389 r.height = w->shadow_height;
390 return XFixesCreateRegion (dpy, &r, 1);
394 border_size (Display *dpy, win *w)
396 XserverRegion border;
397 border = XFixesCreateRegionFromWindow (dpy, w->id, WindowRegionBounding);
399 XFixesUnionRegion (dpy, border, border, w->a.x, w->a.y, None, 0, 0);
404 paint_all (Display *dpy, XserverRegion region)
414 r.width = root_width;
415 r.height = root_height;
416 region = XFixesCreateRegion (dpy, &r, 1);
420 Pixmap rootPixmap = XCreatePixmap (dpy, root, root_width, root_height,
421 DefaultDepth (dpy, scr));
422 rootBuffer = XRenderCreatePicture (dpy, rootPixmap,
423 XRenderFindVisualFormat (dpy,
424 DefaultVisual (dpy, scr)),
426 XFreePixmap (dpy, rootPixmap);
428 XFixesSetPictureClipRegion (dpy, rootPicture, 0, 0, region);
429 for (w = list; w; w = w->next)
433 if (w->a.map_state != IsViewable)
439 XFixesDestroyRegion (dpy, w->borderSize);
440 w->borderSize = border_size (dpy, w);
442 XFixesDestroyRegion (dpy, w->extents);
443 w->extents = win_extents (dpy, w);
444 if (w->mode != WINDOW_TRANS)
446 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
447 XFixesSubtractRegion (dpy, region, region, 0, 0, w->borderSize, 0, 0);
448 XRenderComposite (dpy, PictOpSrc, w->picture, None, rootBuffer,
450 w->a.x + w->a.border_width,
451 w->a.y + w->a.border_width,
455 w->borderClip = XFixesCreateRegion (dpy, 0, 0);
456 XFixesUnionRegion (dpy, w->borderClip, region, 0, 0, None, 0, 0);
460 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
462 for (w = t; w; w = w->prev_trans)
464 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, w->borderClip);
467 XRenderComposite (dpy, PictOpOver, w->shadow, None, rootBuffer,
469 w->a.x + w->a.border_width + w->shadow_dx,
470 w->a.y + w->a.border_width + w->shadow_dy,
471 w->shadow_width, w->shadow_height);
473 if (w->mode == WINDOW_TRANS)
474 XRenderComposite (dpy, PictOpOver, w->picture, transPicture, rootBuffer,
476 w->a.x + w->a.border_width,
477 w->a.y + w->a.border_width,
480 XFixesDestroyRegion (dpy, w->borderClip);
481 w->borderClip = None;
483 XFixesDestroyRegion (dpy, region);
484 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, None);
485 XRenderComposite (dpy, PictOpSrc, rootBuffer, None, rootPicture,
486 0, 0, 0, 0, 0, 0, root_width, root_height);
490 add_damage (Display *dpy, XserverRegion damage)
494 XFixesUnionRegion (dpy, allDamage, allDamage, 0, 0, damage, 0, 0);
495 XFixesDestroyRegion (dpy, damage);
502 repair_win (Display *dpy, Window id)
504 win *w = find_win (dpy, id);
509 /* printf ("repair 0x%x\n", w->id); */
510 parts = XFixesCreateRegion (dpy, 0, 0);
511 /* translate region */
512 XDamageSubtract (dpy, w->damage, None, parts);
513 XFixesUnionRegion (dpy, parts, parts, w->a.x, w->a.y, None, 0, 0);
514 add_damage (dpy, parts);
518 map_win (Display *dpy, Window id)
520 win *w = find_win (dpy, id);
521 XserverRegion region;
525 w->a.map_state = IsViewable;
528 w->damage = XDamageCreate (dpy, id, XDamageReportNonEmpty);
529 region = win_extents (dpy, w);
530 add_damage (dpy, region);
535 unmap_win (Display *dpy, Window id)
537 win *w = find_win (dpy, id);
541 w->a.map_state = IsUnmapped;
542 if (w->damage != None)
544 XDamageDestroy (dpy, w->damage);
547 if (w->extents != None)
549 add_damage (dpy, w->extents); /* destroys region */
555 add_win (Display *dpy, Window id, Window prev)
557 win *new = malloc (sizeof (win));
560 XRenderPictureAttributes pa;
566 for (p = &list; *p; p = &(*p)->next)
567 if ((*p)->id == prev)
573 if (!XGetWindowAttributes (dpy, id, &new->a))
580 pa.subwindow_mode = IncludeInferiors;
581 if (new->a.class == InputOnly)
585 new->picture = XRenderCreatePicture (dpy, id,
586 XRenderFindVisualFormat (dpy,
593 new->borderSize = None;
595 if (new->a.override_redirect)
596 new->mode = WINDOW_TRANS;
598 new->mode = WINDOW_DROP;
601 if (new->a.map_state == IsViewable)
606 configure_win (Display *dpy, XConfigureEvent *ce)
608 win *w = find_win (dpy, ce->window);
610 XserverRegion damage = None;
614 if (ce->window == root)
618 XRenderFreePicture (dpy, rootBuffer);
621 root_width = ce->width;
622 root_height = ce->height;
626 if (w->a.map_state == IsViewable)
628 damage = XFixesCreateRegion (dpy, 0, 0);
629 if (w->extents != None)
630 XFixesUnionRegion (dpy, damage, w->extents, 0, 0, None, 0, 0);
634 if (w->a.width != ce->width || w->a.height != ce->height)
637 XRenderFreePicture (dpy, w->shadow);
640 w->a.width = ce->width;
641 w->a.height = ce->height;
642 w->a.border_width = ce->border_width;
643 w->a.override_redirect = ce->override_redirect;
648 if (above != ce->above)
653 for (prev = &list; *prev; prev = &(*prev)->next)
659 for (prev = &list; *prev; prev = &(*prev)->next)
661 if ((*prev)->id == ce->above)
669 XserverRegion border = border_size (dpy, w);
670 XFixesUnionRegion (dpy, damage, damage, 0, 0, border, 0, 0);
671 add_damage (dpy, damage);
676 destroy_win (Display *dpy, Window id, Bool gone)
680 for (prev = &list; w = *prev; prev = &w->next)
687 XRenderFreePicture (dpy, w->picture);
698 printf ("\t%08x: %d x %d + %d + %d (%d)\n", w->id,
699 w->a.width, w->a.height, w->a.x, w->a.y, w->a.border_width);
707 printf ("windows:\n");
708 for (w = list; w; w = w->next)
713 damage_win (Display *dpy, XDamageNotifyEvent *de)
715 repair_win (dpy, de->drawable);
719 error (Display *dpy, XErrorEvent *ev)
721 printf ("error %d request %d minor %d\n",
722 ev->error_code, ev->request_code, ev->minor_code);
726 expose_root (Display *dpy, Window root, XRectangle *rects, int nrects)
728 XserverRegion region = XFixesCreateRegion (dpy, rects, nrects);
730 add_damage (dpy, region);
736 int event_base, error_base;
737 Window root_return, parent_return;
740 unsigned int nchildren;
742 int damage_event, damage_error;
743 int xfixes_event, xfixes_error;
744 XRenderPictureAttributes pa;
746 XRectangle *expose_rects = 0;
753 dpy = XOpenDisplay (0);
756 fprintf (stderr, "Can't open display\n");
759 XSetErrorHandler (error);
760 scr = DefaultScreen (dpy);
761 root = RootWindow (dpy, scr);
762 pa.subwindow_mode = IncludeInferiors;
763 transPixmap = XCreatePixmap (dpy, root, 1, 1, 8);
765 transPicture = XRenderCreatePicture (dpy, transPixmap,
766 XRenderFindStandardFormat (dpy, PictStandardA8),
769 c.red = c.green = c.blue = 0;
771 XRenderFillRectangle (dpy, PictOpSrc, transPicture, &c, 0, 0, 1, 1);
773 root_width = DisplayWidth (dpy, scr);
774 root_height = DisplayHeight (dpy, scr);
776 rootPicture = XRenderCreatePicture (dpy, root,
777 XRenderFindVisualFormat (dpy,
778 DefaultVisual (dpy, scr)),
781 if (!XCompositeQueryExtension (dpy, &event_base, &error_base))
783 fprintf (stderr, "No composite extension\n");
786 printf ("Composite error %d\n", error_base);
787 if (!XDamageQueryExtension (dpy, &damage_event, &damage_error))
789 fprintf (stderr, "No damage extension\n");
792 printf ("Damage error %d\n", damage_error);
793 if (!XFixesQueryExtension (dpy, &xfixes_event, &xfixes_error))
795 fprintf (stderr, "No XFixes extension\n");
798 printf ("XFixes error %d\n", xfixes_error);
801 XCompositeRedirectSubwindows (dpy, root, CompositeRedirectManual);
802 paint_all (dpy, None);
803 XSelectInput (dpy, root, SubstructureNotifyMask|ExposureMask|StructureNotifyMask);
804 XQueryTree (dpy, root, &root_return, &parent_return, &children, &nchildren);
805 for (i = 0; i < nchildren; i++)
806 add_win (dpy, children[i], i ? children[i-1] : None);
813 XNextEvent (dpy, &ev);
814 /* printf ("event %d\n", ev.type); */
817 add_win (dpy, ev.xcreatewindow.window, 0);
819 case ConfigureNotify:
820 configure_win (dpy, &ev.xconfigure);
823 destroy_win (dpy, ev.xdestroywindow.window, True);
826 map_win (dpy, ev.xmap.window);
829 unmap_win (dpy, ev.xunmap.window);
832 if (ev.xreparent.parent == root)
833 add_win (dpy, ev.xreparent.window, 0);
835 destroy_win (dpy, ev.xreparent.window, False);
838 if (ev.xexpose.window == root)
840 int more = ev.xexpose.count + 1;
841 if (n_expose == size_expose)
845 expose_rects = realloc (expose_rects,
846 (size_expose + more) *
847 sizeof (XRectangle));
852 expose_rects = malloc (more * sizeof (XRectangle));
856 expose_rects[n_expose].x = ev.xexpose.x;
857 expose_rects[n_expose].y = ev.xexpose.y;
858 expose_rects[n_expose].width = ev.xexpose.width;
859 expose_rects[n_expose].height = ev.xexpose.height;
861 if (ev.xexpose.count == 0)
863 expose_root (dpy, root, expose_rects, n_expose);
869 if (ev.type == damage_event + XDamageNotify)
870 damage_win (dpy, (XDamageNotifyEvent *) &ev);
873 } while (XEventsQueued (dpy, QueuedAfterReading));
874 ufd.fd = ConnectionNumber (dpy);
876 n = poll (&ufd, 1, 30);
877 if (n > 0 && (ufd.revents & POLLIN) && XEventsQueued (dpy, QueuedAfterReading))
881 paint_all (dpy, allDamage);