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 unsigned long damage_sequence; /* sequence when damage was created */
56 /* for drawing translucent windows */
57 XserverRegion borderClip;
58 struct _win *prev_trans;
61 typedef struct _conv {
75 XserverRegion allDamage;
76 int root_height, root_width;
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)
90 #define MONITOR_REPAINT 0
93 gaussian (double r, double x, double y)
95 return ((1 / (sqrt (2 * M_PI * r))) *
96 exp ((- (x * x + y * y)) / (2 * r * r)));
101 make_gaussian_map (Display *dpy, double r)
104 int size = ((int) ceil ((r * 3)) + 1) & ~1;
105 int center = size / 2;
110 c = malloc (sizeof (conv) + size * size * sizeof (double));
112 c->data = (double *) (c + 1);
113 for (y = 0; y < size; y++)
114 for (x = 0; x < size; x++)
116 g = gaussian (r, (double) (x - center), (double) (y - center));
118 c->data[y * size + x] = g;
120 /* printf ("gaussian total %f\n", t); */
121 for (y = 0; y < size; y++)
122 for (x = 0; x < size; x++)
124 c->data[y*size + x] /= t;
130 * A picture will help
132 * -center 0 width width+center
133 * -center +-----+-------------------+-----+
136 * 0 +-----+-------------------+-----+
140 * height +-----+-------------------+-----+
143 * center +-----+-------------------+-----+
147 sum_gaussian (conv *map, double opacity, int x, int y, int width, int height)
151 double *g_line = map->data;
152 int g_size = map->size;
153 int center = g_size / 2;
154 int fx_start, fx_end;
155 int fy_start, fy_end;
159 * Compute set of filter values which are "in range",
160 * that's the set with:
161 * 0 <= x + (fx-center) && x + (fx-center) < width &&
162 * 0 <= y + (fy-center) && y + (fy-center) < height
164 * 0 <= x + (fx - center) x + fx - center < width
165 * center - x <= fx fx < width + center - x
168 fx_start = center - x;
171 fx_end = width + center - x;
175 fy_start = center - y;
178 fy_end = height + center - y;
182 g_line = g_line + fy_start * g_size + fx_start;
185 for (fy = fy_start; fy < fy_end; fy++)
190 for (fx = fx_start; fx < fx_end; fx++)
196 return ((unsigned int) (v * opacity * 255.0));
200 make_shadow (Display *dpy, double opacity, int width, int height)
204 int gsize = gaussianMap->size;
206 int swidth = width + gsize;
207 int sheight = height + gsize;
208 int center = gsize / 2;
212 data = malloc (swidth * sheight * sizeof (unsigned char));
213 ximage = XCreateImage (dpy,
214 DefaultVisual(dpy, DefaultScreen(dpy)),
219 swidth, sheight, 8, swidth * sizeof (unsigned char));
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 (gaussianMap, 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 (gaussianMap, 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 (gaussianMap, 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 (gaussianMap, 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;
284 shadow_picture (Display *dpy, double opacity, int width, int height, int *wp, int *hp)
286 XImage *shadowImage = make_shadow (dpy, opacity, width, height);
287 Pixmap shadowPixmap = XCreatePixmap (dpy, root,
291 Picture shadowPicture = XRenderCreatePicture (dpy, shadowPixmap,
292 XRenderFindStandardFormat (dpy, PictStandardA8),
294 GC gc = XCreateGC (dpy, shadowPixmap, 0, 0);
296 XPutImage (dpy, shadowPixmap, gc, shadowImage, 0, 0, 0, 0,
298 shadowImage->height);
299 *wp = shadowImage->width;
300 *hp = shadowImage->height;
302 XDestroyImage (shadowImage);
303 XFreePixmap (dpy, shadowPixmap);
304 return shadowPicture;
308 find_win (Display *dpy, Window id)
312 for (w = list; w; w = w->next)
318 static char *backgroundProps[] = {
325 root_tile (Display *dpy)
331 unsigned long nitems;
332 unsigned long bytes_after;
335 XRenderPictureAttributes pa;
338 for (p = 0; backgroundProps[p]; p++)
340 if (XGetWindowProperty (dpy, root, XInternAtom (dpy, backgroundProps[p], False),
341 0, 4, False, AnyPropertyType,
342 &actual_type, &actual_format, &nitems, &bytes_after, &prop) == Success &&
343 actual_type == XInternAtom (dpy, "PIXMAP", False) && actual_format == 32 && nitems == 1)
345 memcpy (&pixmap, prop, 4);
351 if (!backgroundProps[p])
353 pixmap = XCreatePixmap (dpy, root, 1, 1, DefaultDepth (dpy, scr));
357 picture = XRenderCreatePicture (dpy, pixmap,
358 XRenderFindVisualFormat (dpy,
359 DefaultVisual (dpy, scr)),
365 c.red = c.green = c.blue = 0x8080;
367 XRenderFillRectangle (dpy, PictOpSrc, picture, &c,
374 paint_root (Display *dpy)
377 rootTile = root_tile (dpy);
379 XRenderComposite (dpy, PictOpSrc,
380 rootTile, None, rootBuffer,
381 0, 0, 0, 0, 0, 0, root_width, root_height);
385 win_extents (Display *dpy, win *w)
391 r.width = w->a.width + w->a.border_width * 2;
392 r.height = w->a.height + w->a.border_width * 2;
393 if (w->mode != WINDOW_ARGB)
399 double opacity = SHADOW_OPACITY;
400 if (w->mode == WINDOW_TRANS)
401 opacity = opacity * TRANS_OPACITY;
402 w->shadow = shadow_picture (dpy, opacity,
403 w->a.width + w->a.border_width * 2,
404 w->a.height + w->a.border_width * 2,
405 &w->shadow_width, &w->shadow_height);
406 w->shadow_dx = SHADOW_OFFSET_X;
407 w->shadow_dy = SHADOW_OFFSET_Y;
409 sr.x = w->a.x + w->shadow_dx;
410 sr.y = w->a.y + w->shadow_dy;
411 sr.width = w->shadow_width;
412 sr.height = w->shadow_height;
415 r.width = (r.x + r.width) - sr.x;
420 r.height = (r.y + r.height) - sr.y;
423 if (sr.width > r.width)
425 if (sr.height > r.height)
426 r.height = sr.height;
428 return XFixesCreateRegion (dpy, &r, 1);
432 border_size (Display *dpy, win *w)
434 XserverRegion border;
435 border = XFixesCreateRegionFromWindow (dpy, w->id, WindowRegionBounding);
437 XFixesTranslateRegion (dpy, border,
438 w->a.x + w->a.border_width,
439 w->a.y + w->a.border_width);
444 paint_all (Display *dpy, XserverRegion region)
454 r.width = root_width;
455 r.height = root_height;
456 region = XFixesCreateRegion (dpy, &r, 1);
459 rootBuffer = rootPicture;
463 Pixmap rootPixmap = XCreatePixmap (dpy, root, root_width, root_height,
464 DefaultDepth (dpy, scr));
465 rootBuffer = XRenderCreatePicture (dpy, rootPixmap,
466 XRenderFindVisualFormat (dpy,
467 DefaultVisual (dpy, scr)),
469 XFreePixmap (dpy, rootPixmap);
472 XFixesSetPictureClipRegion (dpy, rootPicture, 0, 0, region);
474 XRenderComposite (dpy, PictOpSrc, blackPicture, None, rootPicture,
475 0, 0, 0, 0, 0, 0, root_width, root_height);
480 for (w = list; w; w = w->next)
482 if (w->a.map_state != IsViewable)
484 /* never painted, ignore it */
490 printf (" 0x%x", w->id);
493 XFixesDestroyRegion (dpy, w->borderSize);
494 w->borderSize = border_size (dpy, w);
496 XFixesDestroyRegion (dpy, w->extents);
497 w->extents = win_extents (dpy, w);
498 if (w->mode == WINDOW_SOLID)
500 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
501 XFixesSubtractRegion (dpy, region, region, w->borderSize);
502 XRenderComposite (dpy, PictOpSrc, w->picture, None, rootBuffer,
504 w->a.x + w->a.border_width,
505 w->a.y + w->a.border_width,
509 w->borderClip = XFixesCreateRegion (dpy, 0, 0);
510 XFixesCopyRegion (dpy, w->borderClip, region);
517 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
519 for (w = t; w; w = w->prev_trans)
521 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, w->borderClip);
524 XRenderComposite (dpy, PictOpOver, blackPicture, w->shadow, rootBuffer,
526 w->a.x + w->shadow_dx,
527 w->a.y + w->shadow_dy,
528 w->shadow_width, w->shadow_height);
530 if (w->mode == WINDOW_TRANS)
531 XRenderComposite (dpy, PictOpOver, w->picture, transPicture, rootBuffer,
533 w->a.x + w->a.border_width,
534 w->a.y + w->a.border_width,
537 else if (w->mode == WINDOW_ARGB)
538 XRenderComposite (dpy, PictOpOver, w->picture, None, rootBuffer,
540 w->a.x + w->a.border_width,
541 w->a.y + w->a.border_width,
544 XFixesDestroyRegion (dpy, w->borderClip);
545 w->borderClip = None;
547 XFixesDestroyRegion (dpy, region);
548 if (rootBuffer != rootPicture)
550 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, None);
551 XRenderComposite (dpy, PictOpSrc, rootBuffer, None, rootPicture,
552 0, 0, 0, 0, 0, 0, root_width, root_height);
557 add_damage (Display *dpy, XserverRegion damage)
561 XFixesUnionRegion (dpy, allDamage, allDamage, damage);
562 XFixesDestroyRegion (dpy, damage);
569 repair_win (Display *dpy, Window id)
571 win *w = find_win (dpy, id);
578 parts = win_extents (dpy, w);
579 XDamageSubtract (dpy, w->damage, None, None);
583 parts = XFixesCreateRegion (dpy, 0, 0);
584 XDamageSubtract (dpy, w->damage, None, parts);
585 XFixesTranslateRegion (dpy, parts,
586 w->a.x + w->a.border_width,
587 w->a.y + w->a.border_width);
589 add_damage (dpy, parts);
594 map_win (Display *dpy, Window id, unsigned long sequence)
596 win *w = find_win (dpy, id);
600 w->a.map_state = IsViewable;
605 unmap_win (Display *dpy, Window id)
607 win *w = find_win (dpy, id);
611 w->a.map_state = IsUnmapped;
613 if (w->extents != None)
615 add_damage (dpy, w->extents); /* destroys region */
621 add_win (Display *dpy, Window id, Window prev)
623 win *new = malloc (sizeof (win));
625 XRenderPictureAttributes pa;
626 XRenderPictFormat *format;
632 for (p = &list; *p; p = &(*p)->next)
633 if ((*p)->id == prev)
639 if (!XGetWindowAttributes (dpy, id, &new->a))
647 pa.subwindow_mode = IncludeInferiors;
648 if (new->a.class == InputOnly)
652 new->damage_sequence = 0;
656 format = XRenderFindVisualFormat (dpy, new->a.visual);
657 new->picture = XRenderCreatePicture (dpy, id,
661 new->damage_sequence = NextRequest (dpy);
662 new->damage = XDamageCreate (dpy, id, XDamageReportNonEmpty);
668 new->shadow_width = 0;
669 new->shadow_height = 0;
670 new->borderSize = None;
672 if (format && format->type == PictTypeDirect && format->direct.alphaMask)
673 new->mode = WINDOW_ARGB;
674 else if (new->a.override_redirect)
675 new->mode = WINDOW_TRANS;
677 new->mode = WINDOW_SOLID;
680 if (new->a.map_state == IsViewable)
681 map_win (dpy, id, new->damage_sequence - 1);
685 restack_win (Display *dpy, win *w, Window new_above)
690 old_above = w->next->id;
693 if (old_above != new_above)
698 for (prev = &list; *prev; prev = &(*prev)->next)
704 for (prev = &list; *prev; prev = &(*prev)->next)
706 if ((*prev)->id == new_above)
715 configure_win (Display *dpy, XConfigureEvent *ce)
717 win *w = find_win (dpy, ce->window);
719 XserverRegion damage = None;
723 if (ce->window == root)
727 XRenderFreePicture (dpy, rootBuffer);
730 root_width = ce->width;
731 root_height = ce->height;
735 if (w->a.map_state == IsViewable)
737 damage = XFixesCreateRegion (dpy, 0, 0);
738 if (w->extents != None)
739 XFixesCopyRegion (dpy, damage, w->extents);
743 if (w->a.width != ce->width || w->a.height != ce->height)
746 XRenderFreePicture (dpy, w->shadow);
749 w->a.width = ce->width;
750 w->a.height = ce->height;
751 w->a.border_width = ce->border_width;
752 w->a.override_redirect = ce->override_redirect;
753 restack_win (dpy, w, ce->above);
756 XserverRegion extents = win_extents (dpy, w);
757 XFixesUnionRegion (dpy, damage, damage, extents);
758 XFixesDestroyRegion (dpy, extents);
759 add_damage (dpy, damage);
764 circulate_win (Display *dpy, XCirculateEvent *ce)
766 win *w = find_win (dpy, ce->window);
769 if (ce->place == PlaceOnTop)
770 new_above = list->id;
773 restack_win (dpy, w, new_above);
777 destroy_win (Display *dpy, Window id, Bool gone)
781 for (prev = &list; (w = *prev); prev = &w->next)
788 XRenderFreePicture (dpy, w->picture);
789 if (w->damage != None)
790 XDamageDestroy (dpy, w->damage);
800 printf ("\t%08lx: %d x %d + %d + %d (%d)\n", w->id,
801 w->a.width, w->a.height, w->a.x, w->a.y, w->a.border_width);
810 printf ("windows:\n");
811 for (w = list; w; w = w->next)
817 damage_win (Display *dpy, XDamageNotifyEvent *de)
819 repair_win (dpy, de->drawable);
823 error (Display *dpy, XErrorEvent *ev)
825 printf ("error %d request %d minor %d\n",
826 ev->error_code, ev->request_code, ev->minor_code);
832 expose_root (Display *dpy, Window root, XRectangle *rects, int nrects)
834 XserverRegion region = XFixesCreateRegion (dpy, rects, nrects);
836 add_damage (dpy, region);
843 time_in_millis (void)
847 gettimeofday (&tp, 0);
848 return(tp.tv_sec * 1000) + (tp.tv_usec / 1000);
855 ev_serial (XEvent *ev)
857 if (ev->type & 0x7f != KeymapNotify)
858 return ev->xany.serial;
859 return NextRequest (ev->xany.display);
862 int damage_event, damage_error;
867 static char buf[128];
868 switch (ev->type & 0x7f) {
877 case CirculateNotify:
880 if (ev->type == damage_event + XDamageNotify)
882 sprintf (buf, "Event %d", ev->type);
888 ev_window (XEvent *ev)
892 return ev->xexpose.window;
894 return ev->xmap.window;
896 return ev->xunmap.window;
898 return ev->xreparent.window;
899 case CirculateNotify:
900 return ev->xcirculate.window;
902 if (ev->type == damage_event + XDamageNotify)
903 return ((XDamageNotifyEvent *) ev)->drawable;
909 main (int argc, char **argv)
912 int event_base, error_base;
913 Window root_return, parent_return;
917 unsigned int nchildren;
919 int xfixes_event, xfixes_error;
920 XRenderPictureAttributes pa;
922 XRectangle *expose_rects = 0;
934 dpy = XOpenDisplay (0);
937 fprintf (stderr, "Can't open display\n");
940 XSetErrorHandler (error);
941 scr = DefaultScreen (dpy);
942 root = RootWindow (dpy, scr);
943 pa.subwindow_mode = IncludeInferiors;
945 gaussianMap = make_gaussian_map(dpy, SHADOW_RADIUS);
947 transPixmap = XCreatePixmap (dpy, root, 1, 1, 8);
949 transPicture = XRenderCreatePicture (dpy, transPixmap,
950 XRenderFindStandardFormat (dpy, PictStandardA8),
953 c.red = c.green = c.blue = 0;
955 XRenderFillRectangle (dpy, PictOpSrc, transPicture, &c, 0, 0, 1, 1);
957 root_width = DisplayWidth (dpy, scr);
958 root_height = DisplayHeight (dpy, scr);
960 rootPicture = XRenderCreatePicture (dpy, root,
961 XRenderFindVisualFormat (dpy,
962 DefaultVisual (dpy, scr)),
965 blackPixmap = XCreatePixmap (dpy, root, 1, 1, 32);
967 blackPicture = XRenderCreatePicture (dpy, blackPixmap,
968 XRenderFindStandardFormat (dpy, PictStandardARGB32),
971 c.red = c.green = c.blue = 0;
973 XRenderFillRectangle (dpy, PictOpSrc, blackPicture, &c, 0, 0, 1, 1);
974 if (!XCompositeQueryExtension (dpy, &event_base, &error_base))
976 fprintf (stderr, "No composite extension\n");
979 if (!XDamageQueryExtension (dpy, &damage_event, &damage_error))
981 fprintf (stderr, "No damage extension\n");
984 if (!XFixesQueryExtension (dpy, &xfixes_event, &xfixes_error))
986 fprintf (stderr, "No XFixes extension\n");
991 XCompositeRedirectSubwindows (dpy, root, CompositeRedirectManual);
992 XSelectInput (dpy, root,
993 SubstructureNotifyMask|
997 XQueryTree (dpy, root, &root_return, &parent_return, &children, &nchildren);
998 for (i = 0; i < nchildren; i++)
999 add_win (dpy, children[i], i ? children[i-1] : None);
1001 XUngrabServer (dpy);
1002 paint_all (dpy, None);
1004 last_update = time_in_millis ();
1013 XNextEvent (dpy, &ev);
1016 busy_start = time_in_millis();
1019 printf ("event %10.10s serial 0x%08x window 0x%08x\n",
1020 ev_name(&ev), ev_serial (&ev), ev_window (&ev));
1024 add_win (dpy, ev.xcreatewindow.window, 0);
1026 case ConfigureNotify:
1027 configure_win (dpy, &ev.xconfigure);
1030 destroy_win (dpy, ev.xdestroywindow.window, True);
1033 map_win (dpy, ev.xmap.window, ev.xmap.serial);
1036 unmap_win (dpy, ev.xunmap.window);
1038 case ReparentNotify:
1039 if (ev.xreparent.parent == root)
1040 add_win (dpy, ev.xreparent.window, 0);
1042 destroy_win (dpy, ev.xreparent.window, False);
1044 case CirculateNotify:
1045 circulate_win (dpy, &ev.xcirculate);
1048 if (ev.xexpose.window == root)
1050 int more = ev.xexpose.count + 1;
1051 if (n_expose == size_expose)
1055 expose_rects = realloc (expose_rects,
1056 (size_expose + more) *
1057 sizeof (XRectangle));
1058 size_expose += more;
1062 expose_rects = malloc (more * sizeof (XRectangle));
1066 expose_rects[n_expose].x = ev.xexpose.x;
1067 expose_rects[n_expose].y = ev.xexpose.y;
1068 expose_rects[n_expose].width = ev.xexpose.width;
1069 expose_rects[n_expose].height = ev.xexpose.height;
1071 if (ev.xexpose.count == 0)
1073 expose_root (dpy, root, expose_rects, n_expose);
1078 case PropertyNotify:
1079 for (p = 0; backgroundProps[p]; p++)
1081 if (ev.xproperty.atom == XInternAtom (dpy, backgroundProps[p], False))
1085 XClearArea (dpy, root, 0, 0, 0, 0, True);
1086 XRenderFreePicture (dpy, rootTile);
1094 if (ev.type == damage_event + XDamageNotify)
1095 damage_win (dpy, (XDamageNotifyEvent *) &ev);
1098 } while (QLength (dpy));
1100 now = time_in_millis ();
1101 /* printf ("\t\tbusy %d\n", now - busy_start); */
1102 timeout = INTERVAL - (now - last_update);
1105 ufd.fd = ConnectionNumber (dpy);
1106 ufd.events = POLLIN;
1107 n = poll (&ufd, 1, timeout);
1108 if (n > 0 && (ufd.revents & POLLIN) && XEventsQueued (dpy, QueuedAfterReading))
1115 int old_update = last_update;
1116 last_update = time_in_millis();
1117 /* printf ("delta %d\n", last_update - old_update); */
1119 paint_all (dpy, allDamage);