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"
70 #define WINDOW_SOLID 0
71 #define WINDOW_TRANS 1
74 #define TRANS_OPACITY 0.75
75 #define SHADOW_RADIUS 15
76 #define SHADOW_OPACITY 0.75
77 #define SHADOW_OFFSET_X (-SHADOW_RADIUS)
78 #define SHADOW_OFFSET_Y (-SHADOW_RADIUS)
82 gaussian (double r, double x, double y)
84 return ((1 / (sqrt (2 * M_PI * r))) *
85 exp ((- (x * x + y * y)) / (2 * r * r)));
88 typedef struct _conv {
94 make_gaussian_map (Display *dpy, double r)
97 int size = ((int) ceil ((r * 3)) + 1) & ~1;
98 int center = size / 2;
103 c = malloc (sizeof (conv) + size * size * sizeof (double));
105 c->data = (double *) (c + 1);
106 for (y = 0; y < size; y++)
107 for (x = 0; x < size; x++)
109 g = gaussian (r, (double) (x - center), (double) (y - center));
111 c->data[y * size + x] = g;
113 printf ("gaussian total %f\n", t);
114 for (y = 0; y < size; y++)
115 for (x = 0; x < size; x++)
117 c->data[y*size + x] /= t;
123 * A picture will help
125 * -center 0 width width+center
126 * -center +-----+-------------------+-----+
129 * 0 +-----+-------------------+-----+
133 * height +-----+-------------------+-----+
136 * center +-----+-------------------+-----+
140 sum_gaussian (conv *map, double opacity, int x, int y, int width, int height)
145 double *g_line = map->data;
146 int g_size = map->size;
147 int center = g_size / 2;
148 int fx_start, fx_end;
149 int fy_start, fy_end;
153 * Compute set of filter values which are "in range",
154 * that's the set with:
155 * 0 <= x + (fx-center) && x + (fx-center) < width &&
156 * 0 <= y + (fy-center) && y + (fy-center) < height
158 * 0 <= x + (fx - center) x + fx - center < width
159 * center - x <= fx fx < width + center - x
162 fx_start = center - x;
165 fx_end = width + center - x;
169 fy_start = center - y;
172 fy_end = height + center - y;
176 g_line = g_line + fy_start * g_size + fx_start;
179 for (fy = fy_start; fy < fy_end; fy++)
184 for (fx = fx_start; fx < fx_end; fx++)
190 return ((unsigned int) (v * opacity * 255.0)) << 24;
194 make_shadow (Display *dpy, double opacity, double r, int width, int height)
196 conv *map = make_gaussian_map (dpy, r);
198 double *gdata = map->data;
200 int gsize = map->size;
202 int swidth = width + gsize;
203 int sheight = height + gsize;
204 int center = gsize / 2;
212 data = malloc (swidth * sheight * sizeof (int));
213 ximage = XCreateImage (dpy,
214 DefaultVisual(dpy, DefaultScreen(dpy)),
219 swidth, sheight, 32, swidth * sizeof (int));
221 * Build the gaussian in sections
228 if (ylimit > sheight / 2)
229 ylimit = (sheight + 1) / 2;
231 if (xlimit > swidth / 2)
232 xlimit = (swidth + 1) / 2;
234 for (y = 0; y < ylimit; y++)
235 for (x = 0; x < xlimit; x++)
237 d = sum_gaussian (map, opacity, x - center, y - center, width, height);
238 data[y * swidth + x] = d;
239 data[(sheight - y - 1) * swidth + x] = d;
240 data[(sheight - y - 1) * swidth + (swidth - x - 1)] = d;
241 data[y * swidth + (swidth - x - 1)] = d;
247 for (y = 0; y < ylimit; y++)
249 d = sum_gaussian (map, opacity, center, y - center, width, height);
250 for (x = gsize; x < swidth - gsize; x++)
252 data[y * swidth + x] = d;
253 data[(sheight - y - 1) * swidth + x] = d;
261 for (x = 0; x < xlimit; x++)
263 d = sum_gaussian (map, opacity, x - center, center, width, height);
264 for (y = gsize; y < sheight - gsize; y++)
266 data[y * swidth + x] = d;
267 data[y * swidth + (swidth - x - 1)] = d;
275 d = sum_gaussian (map, opacity, center, center, width, height);
276 for (y = ylimit; y < sheight - ylimit; y++)
277 for (x = xlimit; x < swidth - xlimit; x++)
278 data[y * swidth + x] = d;
285 shadow_picture (Display *dpy, double opacity, double r, int width, int height, int *wp, int *hp)
287 XImage *shadowImage = make_shadow (dpy, opacity, r, width, height);
288 Pixmap shadowPixmap = XCreatePixmap (dpy, root,
292 Picture shadowPicture = XRenderCreatePicture (dpy, shadowPixmap,
293 XRenderFindStandardFormat (dpy, PictStandardARGB32),
295 GC gc = XCreateGC (dpy, shadowPixmap, 0, 0);
297 XPutImage (dpy, shadowPixmap, gc, shadowImage, 0, 0, 0, 0,
299 shadowImage->height);
300 *wp = shadowImage->width;
301 *hp = shadowImage->height;
303 XDestroyImage (shadowImage);
304 XFreePixmap (dpy, shadowPixmap);
305 return shadowPicture;
309 find_win (Display *dpy, Window id)
313 for (w = list; w; w = w->next)
320 root_tile (Display *dpy)
326 unsigned long nitems;
327 unsigned long bytes_after;
330 XRenderPictureAttributes pa;
332 if (XGetWindowProperty (dpy, root, XInternAtom (dpy, BACKGROUND_PROP, False),
333 0, 4, False, AnyPropertyType,
334 &actual_type, &actual_format, &nitems, &bytes_after, &prop) == Success &&
335 actual_type == XInternAtom (dpy, "PIXMAP", False) && actual_format == 32 && nitems == 1)
337 memcpy (&pixmap, prop, 4);
343 pixmap = XCreatePixmap (dpy, root, 1, 1, DefaultDepth (dpy, scr));
347 picture = XRenderCreatePicture (dpy, pixmap,
348 XRenderFindVisualFormat (dpy,
349 DefaultVisual (dpy, scr)),
355 c.red = c.green = c.blue = 0x8080;
357 XRenderFillRectangle (dpy, PictOpSrc, picture, &c,
364 paint_root (Display *dpy)
367 rootTile = root_tile (dpy);
369 XRenderComposite (dpy, PictOpSrc,
370 rootTile, None, rootBuffer,
371 0, 0, 0, 0, 0, 0, root_width, root_height);
375 win_extents (Display *dpy, win *w)
379 if (w->mode == WINDOW_ARGB)
383 r.width = w->a.width + w->a.border_width * 2;
384 r.height = w->a.height + w->a.border_width * 2;
390 double opacity = SHADOW_OPACITY;
391 if (w->mode == WINDOW_TRANS)
392 opacity = opacity * TRANS_OPACITY;
393 w->shadow = shadow_picture (dpy, opacity, SHADOW_RADIUS,
394 w->a.width, w->a.height,
395 &w->shadow_width, &w->shadow_height);
396 w->shadow_dx = SHADOW_OFFSET_X;
397 w->shadow_dy = SHADOW_OFFSET_Y;
399 r.x = w->a.x + w->a.border_width + w->shadow_dx;
400 r.y = w->a.y + w->a.border_width + w->shadow_dy;
401 r.width = w->shadow_width;
402 r.height = w->shadow_height;
404 return XFixesCreateRegion (dpy, &r, 1);
408 border_size (Display *dpy, win *w)
410 XserverRegion border;
411 border = XFixesCreateRegionFromWindow (dpy, w->id, WindowRegionBounding);
413 XFixesUnionRegion (dpy, border, border, w->a.x, w->a.y, None, 0, 0);
418 paint_all (Display *dpy, XserverRegion region)
428 r.width = root_width;
429 r.height = root_height;
430 region = XFixesCreateRegion (dpy, &r, 1);
434 Pixmap rootPixmap = XCreatePixmap (dpy, root, root_width, root_height,
435 DefaultDepth (dpy, scr));
436 rootBuffer = XRenderCreatePicture (dpy, rootPixmap,
437 XRenderFindVisualFormat (dpy,
438 DefaultVisual (dpy, scr)),
440 XFreePixmap (dpy, rootPixmap);
442 XFixesSetPictureClipRegion (dpy, rootPicture, 0, 0, region);
443 for (w = list; w; w = w->next)
447 if (w->a.map_state != IsViewable)
453 XFixesDestroyRegion (dpy, w->borderSize);
454 w->borderSize = border_size (dpy, w);
456 XFixesDestroyRegion (dpy, w->extents);
457 w->extents = win_extents (dpy, w);
458 if (w->mode == WINDOW_SOLID)
460 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
461 XFixesSubtractRegion (dpy, region, region, 0, 0, w->borderSize, 0, 0);
462 XRenderComposite (dpy, PictOpSrc, w->picture, None, rootBuffer,
464 w->a.x + w->a.border_width,
465 w->a.y + w->a.border_width,
469 w->borderClip = XFixesCreateRegion (dpy, 0, 0);
470 XFixesUnionRegion (dpy, w->borderClip, region, 0, 0, None, 0, 0);
474 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
476 for (w = t; w; w = w->prev_trans)
478 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, w->borderClip);
481 XRenderComposite (dpy, PictOpOver, w->shadow, None, rootBuffer,
483 w->a.x + w->a.border_width + w->shadow_dx,
484 w->a.y + w->a.border_width + w->shadow_dy,
485 w->shadow_width, w->shadow_height);
487 if (w->mode == WINDOW_TRANS)
488 XRenderComposite (dpy, PictOpOver, w->picture, transPicture, rootBuffer,
490 w->a.x + w->a.border_width,
491 w->a.y + w->a.border_width,
494 else if (w->mode == WINDOW_ARGB)
495 XRenderComposite (dpy, PictOpOver, w->picture, None, rootBuffer,
497 w->a.x + w->a.border_width,
498 w->a.y + w->a.border_width,
501 XFixesDestroyRegion (dpy, w->borderClip);
502 w->borderClip = None;
504 XFixesDestroyRegion (dpy, region);
505 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, None);
506 XRenderComposite (dpy, PictOpSrc, rootBuffer, None, rootPicture,
507 0, 0, 0, 0, 0, 0, root_width, root_height);
511 add_damage (Display *dpy, XserverRegion damage)
515 XFixesUnionRegion (dpy, allDamage, allDamage, 0, 0, damage, 0, 0);
516 XFixesDestroyRegion (dpy, damage);
523 repair_win (Display *dpy, Window id)
525 win *w = find_win (dpy, id);
530 /* printf ("repair 0x%x\n", w->id); */
531 parts = XFixesCreateRegion (dpy, 0, 0);
532 /* translate region */
533 XDamageSubtract (dpy, w->damage, None, parts);
534 XFixesUnionRegion (dpy, parts, parts, w->a.x, w->a.y, None, 0, 0);
535 add_damage (dpy, parts);
539 map_win (Display *dpy, Window id)
541 win *w = find_win (dpy, id);
542 XserverRegion region;
546 w->a.map_state = IsViewable;
549 w->damage = XDamageCreate (dpy, id, XDamageReportNonEmpty);
550 region = win_extents (dpy, w);
551 add_damage (dpy, region);
556 unmap_win (Display *dpy, Window id)
558 win *w = find_win (dpy, id);
562 w->a.map_state = IsUnmapped;
563 if (w->damage != None)
565 XDamageDestroy (dpy, w->damage);
568 if (w->extents != None)
570 add_damage (dpy, w->extents); /* destroys region */
576 add_win (Display *dpy, Window id, Window prev)
578 win *new = malloc (sizeof (win));
581 XRenderPictureAttributes pa;
582 XRenderPictFormat *format;
588 for (p = &list; *p; p = &(*p)->next)
589 if ((*p)->id == prev)
595 if (!XGetWindowAttributes (dpy, id, &new->a))
602 pa.subwindow_mode = IncludeInferiors;
603 if (new->a.class == InputOnly)
610 format = XRenderFindVisualFormat (dpy, new->a.visual);
611 new->picture = XRenderCreatePicture (dpy, id,
618 new->borderSize = None;
620 if (format && format->type == PictTypeDirect && format->direct.alphaMask)
621 new->mode = WINDOW_ARGB;
622 else if (new->a.override_redirect)
623 new->mode = WINDOW_TRANS;
625 new->mode = WINDOW_SOLID;
628 if (new->a.map_state == IsViewable)
633 configure_win (Display *dpy, XConfigureEvent *ce)
635 win *w = find_win (dpy, ce->window);
637 XserverRegion damage = None;
641 if (ce->window == root)
645 XRenderFreePicture (dpy, rootBuffer);
648 root_width = ce->width;
649 root_height = ce->height;
653 if (w->a.map_state == IsViewable)
655 damage = XFixesCreateRegion (dpy, 0, 0);
656 if (w->extents != None)
657 XFixesUnionRegion (dpy, damage, w->extents, 0, 0, None, 0, 0);
661 if (w->a.width != ce->width || w->a.height != ce->height)
664 XRenderFreePicture (dpy, w->shadow);
667 w->a.width = ce->width;
668 w->a.height = ce->height;
669 w->a.border_width = ce->border_width;
670 w->a.override_redirect = ce->override_redirect;
675 if (above != ce->above)
680 for (prev = &list; *prev; prev = &(*prev)->next)
686 for (prev = &list; *prev; prev = &(*prev)->next)
688 if ((*prev)->id == ce->above)
696 XserverRegion extents = win_extents (dpy, w);
697 XFixesUnionRegion (dpy, damage, damage, 0, 0, extents, 0, 0);
698 XFixesDestroyRegion (dpy, extents);
699 add_damage (dpy, damage);
704 destroy_win (Display *dpy, Window id, Bool gone)
708 for (prev = &list; w = *prev; prev = &w->next)
715 XRenderFreePicture (dpy, w->picture);
726 printf ("\t%08x: %d x %d + %d + %d (%d)\n", w->id,
727 w->a.width, w->a.height, w->a.x, w->a.y, w->a.border_width);
735 printf ("windows:\n");
736 for (w = list; w; w = w->next)
741 damage_win (Display *dpy, XDamageNotifyEvent *de)
743 repair_win (dpy, de->drawable);
747 error (Display *dpy, XErrorEvent *ev)
749 printf ("error %d request %d minor %d\n",
750 ev->error_code, ev->request_code, ev->minor_code);
754 expose_root (Display *dpy, Window root, XRectangle *rects, int nrects)
756 XserverRegion region = XFixesCreateRegion (dpy, rects, nrects);
758 add_damage (dpy, region);
766 gettimeofday (&tp, 0);
767 return(tp.tv_sec * 1000) + (tp.tv_usec / 1000);
775 int event_base, error_base;
776 Window root_return, parent_return;
779 unsigned int nchildren;
781 int damage_event, damage_error;
782 int xfixes_event, xfixes_error;
783 XRenderPictureAttributes pa;
785 XRectangle *expose_rects = 0;
795 dpy = XOpenDisplay (0);
798 fprintf (stderr, "Can't open display\n");
801 XSetErrorHandler (error);
802 scr = DefaultScreen (dpy);
803 root = RootWindow (dpy, scr);
804 pa.subwindow_mode = IncludeInferiors;
805 transPixmap = XCreatePixmap (dpy, root, 1, 1, 8);
807 transPicture = XRenderCreatePicture (dpy, transPixmap,
808 XRenderFindStandardFormat (dpy, PictStandardA8),
811 c.red = c.green = c.blue = 0;
813 XRenderFillRectangle (dpy, PictOpSrc, transPicture, &c, 0, 0, 1, 1);
815 root_width = DisplayWidth (dpy, scr);
816 root_height = DisplayHeight (dpy, scr);
818 rootPicture = XRenderCreatePicture (dpy, root,
819 XRenderFindVisualFormat (dpy,
820 DefaultVisual (dpy, scr)),
823 if (!XCompositeQueryExtension (dpy, &event_base, &error_base))
825 fprintf (stderr, "No composite extension\n");
828 printf ("Composite error %d\n", error_base);
829 if (!XDamageQueryExtension (dpy, &damage_event, &damage_error))
831 fprintf (stderr, "No damage extension\n");
834 printf ("Damage error %d\n", damage_error);
835 if (!XFixesQueryExtension (dpy, &xfixes_event, &xfixes_error))
837 fprintf (stderr, "No XFixes extension\n");
840 printf ("XFixes error %d\n", xfixes_error);
843 XCompositeRedirectSubwindows (dpy, root, CompositeRedirectManual);
844 XSelectInput (dpy, root,
845 SubstructureNotifyMask|
849 XQueryTree (dpy, root, &root_return, &parent_return, &children, &nchildren);
850 for (i = 0; i < nchildren; i++)
851 add_win (dpy, children[i], i ? children[i-1] : None);
854 paint_all (dpy, None);
855 last_update = time_in_millis ();
860 XNextEvent (dpy, &ev);
861 /* printf ("event %d\n", ev.type); */
864 add_win (dpy, ev.xcreatewindow.window, 0);
866 case ConfigureNotify:
867 configure_win (dpy, &ev.xconfigure);
870 destroy_win (dpy, ev.xdestroywindow.window, True);
873 map_win (dpy, ev.xmap.window);
876 unmap_win (dpy, ev.xunmap.window);
879 if (ev.xreparent.parent == root)
880 add_win (dpy, ev.xreparent.window, 0);
882 destroy_win (dpy, ev.xreparent.window, False);
885 if (ev.xexpose.window == root)
887 int more = ev.xexpose.count + 1;
888 if (n_expose == size_expose)
892 expose_rects = realloc (expose_rects,
893 (size_expose + more) *
894 sizeof (XRectangle));
899 expose_rects = malloc (more * sizeof (XRectangle));
903 expose_rects[n_expose].x = ev.xexpose.x;
904 expose_rects[n_expose].y = ev.xexpose.y;
905 expose_rects[n_expose].width = ev.xexpose.width;
906 expose_rects[n_expose].height = ev.xexpose.height;
908 if (ev.xexpose.count == 0)
910 expose_root (dpy, root, expose_rects, n_expose);
916 if (ev.xproperty.atom == XInternAtom (dpy, BACKGROUND_PROP, False))
920 XClearArea (dpy, root, 0, 0, 0, 0, True);
921 XRenderFreePicture (dpy, rootTile);
927 if (ev.type == damage_event + XDamageNotify)
928 damage_win (dpy, (XDamageNotifyEvent *) &ev);
931 } while (XEventsQueued (dpy, QueuedAfterReading));
932 now = time_in_millis ();
933 timeout = INTERVAL - (now - last_update);
936 ufd.fd = ConnectionNumber (dpy);
938 n = poll (&ufd, 1, timeout);
939 if (n > 0 && (ufd.revents & POLLIN) && XEventsQueued (dpy, QueuedAfterReading))
942 last_update = time_in_millis();
945 paint_all (dpy, allDamage);