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.
33 #include <X11/Xutil.h>
34 #include <X11/extensions/Xcomposite.h>
35 #include <X11/extensions/Xdamage.h>
36 #include <X11/extensions/Xrender.h>
46 XserverRegion borderSize;
47 XserverRegion extents;
54 /* for drawing translucent windows */
55 XserverRegion borderClip;
56 struct _win *prev_trans;
59 typedef struct _conv {
73 XserverRegion allDamage;
74 int root_height, root_width;
77 #define BACKGROUND_PROP "_XROOTPMAP_ID"
79 #define WINDOW_SOLID 0
80 #define WINDOW_TRANS 1
83 #define TRANS_OPACITY 0.75
84 #define SHADOW_RADIUS 15
85 #define SHADOW_OPACITY 0.75
86 #define SHADOW_OFFSET_X (-SHADOW_RADIUS)
87 #define SHADOW_OFFSET_Y (-SHADOW_RADIUS)
91 gaussian (double r, double x, double y)
93 return ((1 / (sqrt (2 * M_PI * r))) *
94 exp ((- (x * x + y * y)) / (2 * r * r)));
99 make_gaussian_map (Display *dpy, double r)
102 int size = ((int) ceil ((r * 3)) + 1) & ~1;
103 int center = size / 2;
108 c = malloc (sizeof (conv) + size * size * sizeof (double));
110 c->data = (double *) (c + 1);
111 for (y = 0; y < size; y++)
112 for (x = 0; x < size; x++)
114 g = gaussian (r, (double) (x - center), (double) (y - center));
116 c->data[y * size + x] = g;
118 /* printf ("gaussian total %f\n", t); */
119 for (y = 0; y < size; y++)
120 for (x = 0; x < size; x++)
122 c->data[y*size + x] /= t;
128 * A picture will help
130 * -center 0 width width+center
131 * -center +-----+-------------------+-----+
134 * 0 +-----+-------------------+-----+
138 * height +-----+-------------------+-----+
141 * center +-----+-------------------+-----+
145 sum_gaussian (conv *map, double opacity, int x, int y, int width, int height)
149 double *g_line = map->data;
150 int g_size = map->size;
151 int center = g_size / 2;
152 int fx_start, fx_end;
153 int fy_start, fy_end;
157 * Compute set of filter values which are "in range",
158 * that's the set with:
159 * 0 <= x + (fx-center) && x + (fx-center) < width &&
160 * 0 <= y + (fy-center) && y + (fy-center) < height
162 * 0 <= x + (fx - center) x + fx - center < width
163 * center - x <= fx fx < width + center - x
166 fx_start = center - x;
169 fx_end = width + center - x;
173 fy_start = center - y;
176 fy_end = height + center - y;
180 g_line = g_line + fy_start * g_size + fx_start;
183 for (fy = fy_start; fy < fy_end; fy++)
188 for (fx = fx_start; fx < fx_end; fx++)
194 return ((unsigned int) (v * opacity * 255.0));
198 make_shadow (Display *dpy, double opacity, int width, int height)
202 int gsize = gussianMap->size;
204 int swidth = width + gsize;
205 int sheight = height + gsize;
206 int center = gsize / 2;
210 data = malloc (swidth * sheight * sizeof (unsigned char));
211 ximage = XCreateImage (dpy,
212 DefaultVisual(dpy, DefaultScreen(dpy)),
217 swidth, sheight, 8, swidth * sizeof (unsigned char));
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 (gussianMap, 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 (gussianMap, 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 (gussianMap, 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 (gussianMap, 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;
282 shadow_picture (Display *dpy, double opacity, int width, int height, int *wp, int *hp)
284 XImage *shadowImage = make_shadow (dpy, opacity, width, height);
285 Pixmap shadowPixmap = XCreatePixmap (dpy, root,
289 Picture shadowPicture = XRenderCreatePicture (dpy, shadowPixmap,
290 XRenderFindStandardFormat (dpy, PictStandardA8),
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, BACKGROUND_PROP, False),
330 0, 4, False, AnyPropertyType,
331 &actual_type, &actual_format, &nitems, &bytes_after, &prop) == Success &&
332 actual_type == XInternAtom (dpy, "PIXMAP", False) && actual_format == 32 && nitems == 1)
334 memcpy (&pixmap, prop, 4);
340 pixmap = XCreatePixmap (dpy, root, 1, 1, DefaultDepth (dpy, scr));
344 picture = XRenderCreatePicture (dpy, pixmap,
345 XRenderFindVisualFormat (dpy,
346 DefaultVisual (dpy, scr)),
352 c.red = c.green = c.blue = 0x8080;
354 XRenderFillRectangle (dpy, PictOpSrc, picture, &c,
361 paint_root (Display *dpy)
364 rootTile = root_tile (dpy);
366 XRenderComposite (dpy, PictOpSrc,
367 rootTile, None, rootBuffer,
368 0, 0, 0, 0, 0, 0, root_width, root_height);
372 win_extents (Display *dpy, win *w)
376 if (w->mode == WINDOW_ARGB)
380 r.width = w->a.width + w->a.border_width * 2;
381 r.height = w->a.height + w->a.border_width * 2;
387 double opacity = SHADOW_OPACITY;
388 if (w->mode == WINDOW_TRANS)
389 opacity = opacity * TRANS_OPACITY;
390 w->shadow = shadow_picture (dpy, opacity,
391 w->a.width, w->a.height,
392 &w->shadow_width, &w->shadow_height);
393 w->shadow_dx = SHADOW_OFFSET_X;
394 w->shadow_dy = SHADOW_OFFSET_Y;
396 r.x = w->a.x + w->a.border_width + w->shadow_dx;
397 r.y = w->a.y + w->a.border_width + w->shadow_dy;
398 r.width = w->shadow_width;
399 r.height = w->shadow_height;
401 return XFixesCreateRegion (dpy, &r, 1);
405 border_size (Display *dpy, win *w)
407 XserverRegion border;
408 border = XFixesCreateRegionFromWindow (dpy, w->id, WindowRegionBounding);
410 XFixesUnionRegion (dpy, border, border, w->a.x, w->a.y, None, 0, 0);
415 paint_all (Display *dpy, XserverRegion region)
425 r.width = root_width;
426 r.height = root_height;
427 region = XFixesCreateRegion (dpy, &r, 1);
431 Pixmap rootPixmap = XCreatePixmap (dpy, root, root_width, root_height,
432 DefaultDepth (dpy, scr));
433 rootBuffer = XRenderCreatePicture (dpy, rootPixmap,
434 XRenderFindVisualFormat (dpy,
435 DefaultVisual (dpy, scr)),
437 XFreePixmap (dpy, rootPixmap);
439 XFixesSetPictureClipRegion (dpy, rootPicture, 0, 0, region);
440 for (w = list; w; w = w->next)
442 if (w->a.map_state != IsViewable)
448 XFixesDestroyRegion (dpy, w->borderSize);
449 w->borderSize = border_size (dpy, w);
451 XFixesDestroyRegion (dpy, w->extents);
452 w->extents = win_extents (dpy, w);
453 if (w->mode == WINDOW_SOLID)
455 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
456 XFixesSubtractRegion (dpy, region, region, 0, 0, w->borderSize, 0, 0);
457 XRenderComposite (dpy, PictOpSrc, w->picture, None, rootBuffer,
459 w->a.x + w->a.border_width,
460 w->a.y + w->a.border_width,
464 w->borderClip = XFixesCreateRegion (dpy, 0, 0);
465 XFixesUnionRegion (dpy, w->borderClip, region, 0, 0, None, 0, 0);
469 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
471 for (w = t; w; w = w->prev_trans)
473 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, w->borderClip);
476 XRenderComposite (dpy, PictOpOver, blackPicture, w->shadow, rootBuffer,
478 w->a.x + w->a.border_width + w->shadow_dx,
479 w->a.y + w->a.border_width + w->shadow_dy,
480 w->shadow_width, w->shadow_height);
482 if (w->mode == WINDOW_TRANS)
483 XRenderComposite (dpy, PictOpOver, w->picture, transPicture, rootBuffer,
485 w->a.x + w->a.border_width,
486 w->a.y + w->a.border_width,
489 else if (w->mode == WINDOW_ARGB)
490 XRenderComposite (dpy, PictOpOver, w->picture, None, rootBuffer,
492 w->a.x + w->a.border_width,
493 w->a.y + w->a.border_width,
496 XFixesDestroyRegion (dpy, w->borderClip);
497 w->borderClip = None;
499 XFixesDestroyRegion (dpy, region);
500 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, None);
501 XRenderComposite (dpy, PictOpSrc, rootBuffer, None, rootPicture,
502 0, 0, 0, 0, 0, 0, root_width, root_height);
506 add_damage (Display *dpy, XserverRegion damage)
510 XFixesUnionRegion (dpy, allDamage, allDamage, 0, 0, damage, 0, 0);
511 XFixesDestroyRegion (dpy, damage);
518 repair_win (Display *dpy, Window id)
520 win *w = find_win (dpy, id);
525 /* printf ("repair 0x%x\n", w->id); */
526 parts = XFixesCreateRegion (dpy, 0, 0);
527 /* translate region */
528 XDamageSubtract (dpy, w->damage, None, parts);
529 XFixesUnionRegion (dpy, parts, parts, w->a.x, w->a.y, None, 0, 0);
530 add_damage (dpy, parts);
534 map_win (Display *dpy, Window id)
536 win *w = find_win (dpy, id);
537 XserverRegion region;
541 w->a.map_state = IsViewable;
544 w->damage = XDamageCreate (dpy, id, XDamageReportNonEmpty);
545 region = win_extents (dpy, w);
546 add_damage (dpy, region);
551 unmap_win (Display *dpy, Window id)
553 win *w = find_win (dpy, id);
557 w->a.map_state = IsUnmapped;
558 if (w->damage != None)
560 XDamageDestroy (dpy, w->damage);
563 if (w->extents != None)
565 add_damage (dpy, w->extents); /* destroys region */
571 add_win (Display *dpy, Window id, Window prev)
573 win *new = malloc (sizeof (win));
575 XRenderPictureAttributes pa;
576 XRenderPictFormat *format;
582 for (p = &list; *p; p = &(*p)->next)
583 if ((*p)->id == prev)
589 if (!XGetWindowAttributes (dpy, id, &new->a))
596 pa.subwindow_mode = IncludeInferiors;
597 if (new->a.class == InputOnly)
604 format = XRenderFindVisualFormat (dpy, new->a.visual);
605 new->picture = XRenderCreatePicture (dpy, id,
612 new->borderSize = None;
614 if (format && format->type == PictTypeDirect && format->direct.alphaMask)
615 new->mode = WINDOW_ARGB;
616 else if (new->a.override_redirect)
617 new->mode = WINDOW_TRANS;
619 new->mode = WINDOW_SOLID;
622 if (new->a.map_state == IsViewable)
627 configure_win (Display *dpy, XConfigureEvent *ce)
629 win *w = find_win (dpy, ce->window);
631 XserverRegion damage = None;
635 if (ce->window == root)
639 XRenderFreePicture (dpy, rootBuffer);
642 root_width = ce->width;
643 root_height = ce->height;
647 if (w->a.map_state == IsViewable)
649 damage = XFixesCreateRegion (dpy, 0, 0);
650 if (w->extents != None)
651 XFixesUnionRegion (dpy, damage, w->extents, 0, 0, None, 0, 0);
655 if (w->a.width != ce->width || w->a.height != ce->height)
658 XRenderFreePicture (dpy, w->shadow);
661 w->a.width = ce->width;
662 w->a.height = ce->height;
663 w->a.border_width = ce->border_width;
664 w->a.override_redirect = ce->override_redirect;
669 if (above != ce->above)
674 for (prev = &list; *prev; prev = &(*prev)->next)
680 for (prev = &list; *prev; prev = &(*prev)->next)
682 if ((*prev)->id == ce->above)
690 XserverRegion extents = win_extents (dpy, w);
691 XFixesUnionRegion (dpy, damage, damage, 0, 0, extents, 0, 0);
692 XFixesDestroyRegion (dpy, extents);
693 add_damage (dpy, damage);
698 destroy_win (Display *dpy, Window id, Bool gone)
702 for (prev = &list; (w = *prev); prev = &w->next)
709 XRenderFreePicture (dpy, w->picture);
721 printf ("\t%08lx: %d x %d + %d + %d (%d)\n", w->id,
722 w->a.width, w->a.height, w->a.x, w->a.y, w->a.border_width);
731 printf ("windows:\n");
732 for (w = list; w; w = w->next)
738 damage_win (Display *dpy, XDamageNotifyEvent *de)
740 repair_win (dpy, de->drawable);
744 error (Display *dpy, XErrorEvent *ev)
746 printf ("error %d request %d minor %d\n",
747 ev->error_code, ev->request_code, ev->minor_code);
753 expose_root (Display *dpy, Window root, XRectangle *rects, int nrects)
755 XserverRegion region = XFixesCreateRegion (dpy, rects, nrects);
757 add_damage (dpy, region);
764 time_in_millis (void)
768 gettimeofday (&tp, 0);
769 return(tp.tv_sec * 1000) + (tp.tv_usec / 1000);
777 int event_base, error_base;
778 Window root_return, parent_return;
782 unsigned int nchildren;
784 int damage_event, damage_error;
785 int xfixes_event, xfixes_error;
786 XRenderPictureAttributes pa;
788 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;
806 gussianMap = make_gaussian_map(dpy, SHADOW_RADIUS);
808 transPixmap = XCreatePixmap (dpy, root, 1, 1, 8);
810 transPicture = XRenderCreatePicture (dpy, transPixmap,
811 XRenderFindStandardFormat (dpy, PictStandardA8),
814 c.red = c.green = c.blue = 0;
816 XRenderFillRectangle (dpy, PictOpSrc, transPicture, &c, 0, 0, 1, 1);
818 root_width = DisplayWidth (dpy, scr);
819 root_height = DisplayHeight (dpy, scr);
821 rootPicture = XRenderCreatePicture (dpy, root,
822 XRenderFindVisualFormat (dpy,
823 DefaultVisual (dpy, scr)),
826 blackPixmap = XCreatePixmap (dpy, root, 1, 1, 32);
828 blackPicture = XRenderCreatePicture (dpy, blackPixmap,
829 XRenderFindStandardFormat (dpy, PictStandardARGB32),
832 c.red = c.green = c.blue = 0;
834 XRenderFillRectangle (dpy, PictOpSrc, blackPicture, &c, 0, 0, 1, 1);
835 if (!XCompositeQueryExtension (dpy, &event_base, &error_base))
837 fprintf (stderr, "No composite extension\n");
840 if (!XDamageQueryExtension (dpy, &damage_event, &damage_error))
842 fprintf (stderr, "No damage extension\n");
845 if (!XFixesQueryExtension (dpy, &xfixes_event, &xfixes_error))
847 fprintf (stderr, "No XFixes extension\n");
852 XCompositeRedirectSubwindows (dpy, root, CompositeRedirectManual);
853 XSelectInput (dpy, root,
854 SubstructureNotifyMask|
858 XQueryTree (dpy, root, &root_return, &parent_return, &children, &nchildren);
859 for (i = 0; i < nchildren; i++)
860 add_win (dpy, children[i], i ? children[i-1] : None);
863 paint_all (dpy, None);
865 last_update = time_in_millis ();
874 XNextEvent (dpy, &ev);
877 busy_start = time_in_millis();
879 /* printf ("event %d\n", ev.type); */
882 add_win (dpy, ev.xcreatewindow.window, 0);
884 case ConfigureNotify:
885 configure_win (dpy, &ev.xconfigure);
888 destroy_win (dpy, ev.xdestroywindow.window, True);
891 map_win (dpy, ev.xmap.window);
894 unmap_win (dpy, ev.xunmap.window);
897 if (ev.xreparent.parent == root)
898 add_win (dpy, ev.xreparent.window, 0);
900 destroy_win (dpy, ev.xreparent.window, False);
903 if (ev.xexpose.window == root)
905 int more = ev.xexpose.count + 1;
906 if (n_expose == size_expose)
910 expose_rects = realloc (expose_rects,
911 (size_expose + more) *
912 sizeof (XRectangle));
917 expose_rects = malloc (more * sizeof (XRectangle));
921 expose_rects[n_expose].x = ev.xexpose.x;
922 expose_rects[n_expose].y = ev.xexpose.y;
923 expose_rects[n_expose].width = ev.xexpose.width;
924 expose_rects[n_expose].height = ev.xexpose.height;
926 if (ev.xexpose.count == 0)
928 expose_root (dpy, root, expose_rects, n_expose);
934 if (ev.xproperty.atom == XInternAtom (dpy, BACKGROUND_PROP, False))
938 XClearArea (dpy, root, 0, 0, 0, 0, True);
939 XRenderFreePicture (dpy, rootTile);
945 if (ev.type == damage_event + XDamageNotify)
946 damage_win (dpy, (XDamageNotifyEvent *) &ev);
949 } while (XEventsQueued (dpy, QueuedAfterReading));
951 now = time_in_millis ();
952 /* printf ("\t\tbusy %d\n", now - busy_start); */
953 timeout = INTERVAL - (now - last_update);
956 ufd.fd = ConnectionNumber (dpy);
958 n = poll (&ufd, 1, timeout);
959 if (n > 0 && (ufd.revents & POLLIN) && XEventsQueued (dpy, QueuedAfterReading))
966 int old_update = last_update;
967 last_update = time_in_millis();
968 /* printf ("delta %d\n", last_update - old_update); */
970 paint_all (dpy, allDamage);