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 &&
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)
378 double opacity = SHADOW_OPACITY;
379 if (w->mode == WINDOW_TRANS)
380 opacity = opacity * TRANS_OPACITY;
381 w->shadow = shadow_picture (dpy, opacity, SHADOW_RADIUS,
382 w->a.width, w->a.height,
383 &w->shadow_width, &w->shadow_height);
384 w->shadow_dx = SHADOW_OFFSET_X;
385 w->shadow_dy = SHADOW_OFFSET_Y;
387 r.x = w->a.x + w->a.border_width + w->shadow_dx;
388 r.y = w->a.y + w->a.border_width + w->shadow_dy;
389 r.width = w->shadow_width;
390 r.height = w->shadow_height;
391 return XFixesCreateRegion (dpy, &r, 1);
395 border_size (Display *dpy, win *w)
397 XserverRegion border;
398 border = XFixesCreateRegionFromWindow (dpy, w->id, WindowRegionBounding);
400 XFixesUnionRegion (dpy, border, border, w->a.x, w->a.y, None, 0, 0);
405 paint_all (Display *dpy, XserverRegion region)
415 r.width = root_width;
416 r.height = root_height;
417 region = XFixesCreateRegion (dpy, &r, 1);
421 Pixmap rootPixmap = XCreatePixmap (dpy, root, root_width, root_height,
422 DefaultDepth (dpy, scr));
423 rootBuffer = XRenderCreatePicture (dpy, rootPixmap,
424 XRenderFindVisualFormat (dpy,
425 DefaultVisual (dpy, scr)),
427 XFreePixmap (dpy, rootPixmap);
429 XFixesSetPictureClipRegion (dpy, rootPicture, 0, 0, region);
430 for (w = list; w; w = w->next)
434 if (w->a.map_state != IsViewable)
440 XFixesDestroyRegion (dpy, w->borderSize);
441 w->borderSize = border_size (dpy, w);
443 XFixesDestroyRegion (dpy, w->extents);
444 w->extents = win_extents (dpy, w);
445 if (w->mode != WINDOW_TRANS)
447 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
448 XFixesSubtractRegion (dpy, region, region, 0, 0, w->borderSize, 0, 0);
449 XRenderComposite (dpy, PictOpSrc, w->picture, None, rootBuffer,
451 w->a.x + w->a.border_width,
452 w->a.y + w->a.border_width,
456 w->borderClip = XFixesCreateRegion (dpy, 0, 0);
457 XFixesUnionRegion (dpy, w->borderClip, region, 0, 0, None, 0, 0);
461 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
463 for (w = t; w; w = w->prev_trans)
465 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, w->borderClip);
468 XRenderComposite (dpy, PictOpOver, w->shadow, None, rootBuffer,
470 w->a.x + w->a.border_width + w->shadow_dx,
471 w->a.y + w->a.border_width + w->shadow_dy,
472 w->shadow_width, w->shadow_height);
474 if (w->mode == WINDOW_TRANS)
475 XRenderComposite (dpy, PictOpOver, w->picture, transPicture, rootBuffer,
477 w->a.x + w->a.border_width,
478 w->a.y + w->a.border_width,
481 XFixesDestroyRegion (dpy, w->borderClip);
482 w->borderClip = None;
484 XFixesDestroyRegion (dpy, region);
485 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, None);
486 XRenderComposite (dpy, PictOpSrc, rootBuffer, None, rootPicture,
487 0, 0, 0, 0, 0, 0, root_width, root_height);
491 add_damage (Display *dpy, XserverRegion damage)
495 XFixesUnionRegion (dpy, allDamage, allDamage, 0, 0, damage, 0, 0);
496 XFixesDestroyRegion (dpy, damage);
503 repair_win (Display *dpy, Window id)
505 win *w = find_win (dpy, id);
510 /* printf ("repair 0x%x\n", w->id); */
511 parts = XFixesCreateRegion (dpy, 0, 0);
512 /* translate region */
513 XDamageSubtract (dpy, w->damage, None, parts);
514 XFixesUnionRegion (dpy, parts, parts, w->a.x, w->a.y, None, 0, 0);
515 add_damage (dpy, parts);
519 map_win (Display *dpy, Window id)
521 win *w = find_win (dpy, id);
522 XserverRegion region;
526 w->a.map_state = IsViewable;
529 w->damage = XDamageCreate (dpy, id, XDamageReportNonEmpty);
530 region = win_extents (dpy, w);
531 add_damage (dpy, region);
536 unmap_win (Display *dpy, Window id)
538 win *w = find_win (dpy, id);
542 w->a.map_state = IsUnmapped;
543 if (w->damage != None)
545 XDamageDestroy (dpy, w->damage);
548 if (w->extents != None)
550 add_damage (dpy, w->extents); /* destroys region */
556 add_win (Display *dpy, Window id, Window prev)
558 win *new = malloc (sizeof (win));
561 XRenderPictureAttributes pa;
567 for (p = &list; *p; p = &(*p)->next)
568 if ((*p)->id == prev)
574 if (!XGetWindowAttributes (dpy, id, &new->a))
581 pa.subwindow_mode = IncludeInferiors;
582 if (new->a.class == InputOnly)
586 new->picture = XRenderCreatePicture (dpy, id,
587 XRenderFindVisualFormat (dpy,
594 new->borderSize = None;
596 if (new->a.override_redirect)
597 new->mode = WINDOW_TRANS;
599 new->mode = WINDOW_DROP;
602 if (new->a.map_state == IsViewable)
607 configure_win (Display *dpy, XConfigureEvent *ce)
609 win *w = find_win (dpy, ce->window);
611 XserverRegion damage = None;
615 if (ce->window == root)
619 XRenderFreePicture (dpy, rootBuffer);
622 root_width = ce->width;
623 root_height = ce->height;
627 if (w->a.map_state == IsViewable)
629 damage = XFixesCreateRegion (dpy, 0, 0);
630 if (w->extents != None)
631 XFixesUnionRegion (dpy, damage, w->extents, 0, 0, None, 0, 0);
635 if (w->a.width != ce->width || w->a.height != ce->height)
638 XRenderFreePicture (dpy, w->shadow);
641 w->a.width = ce->width;
642 w->a.height = ce->height;
643 w->a.border_width = ce->border_width;
644 w->a.override_redirect = ce->override_redirect;
649 if (above != ce->above)
654 for (prev = &list; *prev; prev = &(*prev)->next)
660 for (prev = &list; *prev; prev = &(*prev)->next)
662 if ((*prev)->id == ce->above)
670 XserverRegion extents = win_extents (dpy, w);
671 XFixesUnionRegion (dpy, damage, damage, 0, 0, extents, 0, 0);
672 XFixesDestroyRegion (dpy, extents);
673 add_damage (dpy, damage);
678 destroy_win (Display *dpy, Window id, Bool gone)
682 for (prev = &list; w = *prev; prev = &w->next)
689 XRenderFreePicture (dpy, w->picture);
700 printf ("\t%08x: %d x %d + %d + %d (%d)\n", w->id,
701 w->a.width, w->a.height, w->a.x, w->a.y, w->a.border_width);
709 printf ("windows:\n");
710 for (w = list; w; w = w->next)
715 damage_win (Display *dpy, XDamageNotifyEvent *de)
717 repair_win (dpy, de->drawable);
721 error (Display *dpy, XErrorEvent *ev)
723 printf ("error %d request %d minor %d\n",
724 ev->error_code, ev->request_code, ev->minor_code);
728 expose_root (Display *dpy, Window root, XRectangle *rects, int nrects)
730 XserverRegion region = XFixesCreateRegion (dpy, rects, nrects);
732 add_damage (dpy, region);
740 gettimeofday (&tp, 0);
741 return(tp.tv_sec * 1000) + (tp.tv_usec / 1000);
749 int event_base, error_base;
750 Window root_return, parent_return;
753 unsigned int nchildren;
755 int damage_event, damage_error;
756 int xfixes_event, xfixes_error;
757 XRenderPictureAttributes pa;
759 XRectangle *expose_rects = 0;
769 dpy = XOpenDisplay (0);
772 fprintf (stderr, "Can't open display\n");
775 XSetErrorHandler (error);
776 scr = DefaultScreen (dpy);
777 root = RootWindow (dpy, scr);
778 pa.subwindow_mode = IncludeInferiors;
779 transPixmap = XCreatePixmap (dpy, root, 1, 1, 8);
781 transPicture = XRenderCreatePicture (dpy, transPixmap,
782 XRenderFindStandardFormat (dpy, PictStandardA8),
785 c.red = c.green = c.blue = 0;
787 XRenderFillRectangle (dpy, PictOpSrc, transPicture, &c, 0, 0, 1, 1);
789 root_width = DisplayWidth (dpy, scr);
790 root_height = DisplayHeight (dpy, scr);
792 rootPicture = XRenderCreatePicture (dpy, root,
793 XRenderFindVisualFormat (dpy,
794 DefaultVisual (dpy, scr)),
797 if (!XCompositeQueryExtension (dpy, &event_base, &error_base))
799 fprintf (stderr, "No composite extension\n");
802 printf ("Composite error %d\n", error_base);
803 if (!XDamageQueryExtension (dpy, &damage_event, &damage_error))
805 fprintf (stderr, "No damage extension\n");
808 printf ("Damage error %d\n", damage_error);
809 if (!XFixesQueryExtension (dpy, &xfixes_event, &xfixes_error))
811 fprintf (stderr, "No XFixes extension\n");
814 printf ("XFixes error %d\n", xfixes_error);
817 XCompositeRedirectSubwindows (dpy, root, CompositeRedirectManual);
818 paint_all (dpy, None);
819 XSelectInput (dpy, root, SubstructureNotifyMask|ExposureMask|StructureNotifyMask);
820 XQueryTree (dpy, root, &root_return, &parent_return, &children, &nchildren);
821 for (i = 0; i < nchildren; i++)
822 add_win (dpy, children[i], i ? children[i-1] : None);
825 last_update = time_in_millis ();
830 XNextEvent (dpy, &ev);
831 /* printf ("event %d\n", ev.type); */
834 add_win (dpy, ev.xcreatewindow.window, 0);
836 case ConfigureNotify:
837 configure_win (dpy, &ev.xconfigure);
840 destroy_win (dpy, ev.xdestroywindow.window, True);
843 map_win (dpy, ev.xmap.window);
846 unmap_win (dpy, ev.xunmap.window);
849 if (ev.xreparent.parent == root)
850 add_win (dpy, ev.xreparent.window, 0);
852 destroy_win (dpy, ev.xreparent.window, False);
855 if (ev.xexpose.window == root)
857 int more = ev.xexpose.count + 1;
858 if (n_expose == size_expose)
862 expose_rects = realloc (expose_rects,
863 (size_expose + more) *
864 sizeof (XRectangle));
869 expose_rects = malloc (more * sizeof (XRectangle));
873 expose_rects[n_expose].x = ev.xexpose.x;
874 expose_rects[n_expose].y = ev.xexpose.y;
875 expose_rects[n_expose].width = ev.xexpose.width;
876 expose_rects[n_expose].height = ev.xexpose.height;
878 if (ev.xexpose.count == 0)
880 expose_root (dpy, root, expose_rects, n_expose);
886 if (ev.type == damage_event + XDamageNotify)
887 damage_win (dpy, (XDamageNotifyEvent *) &ev);
890 } while (XEventsQueued (dpy, QueuedAfterReading));
891 now = time_in_millis ();
892 timeout = INTERVAL - (now - last_update);
895 ufd.fd = ConnectionNumber (dpy);
897 n = poll (&ufd, 1, timeout);
898 if (n > 0 && (ufd.revents & POLLIN) && XEventsQueued (dpy, QueuedAfterReading))
901 last_update = time_in_millis();
904 paint_all (dpy, allDamage);