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);
409 XFixesTranslateRegion (dpy, border, w->a.x, w->a.y);
414 paint_all (Display *dpy, XserverRegion region)
424 r.width = root_width;
425 r.height = root_height;
426 region = XFixesCreateRegion (dpy, &r, 1);
430 Pixmap rootPixmap = XCreatePixmap (dpy, root, root_width, root_height,
431 DefaultDepth (dpy, scr));
432 rootBuffer = XRenderCreatePicture (dpy, rootPixmap,
433 XRenderFindVisualFormat (dpy,
434 DefaultVisual (dpy, scr)),
436 XFreePixmap (dpy, rootPixmap);
438 XFixesSetPictureClipRegion (dpy, rootPicture, 0, 0, region);
439 for (w = list; w; w = w->next)
441 if (w->a.map_state != IsViewable)
447 XFixesDestroyRegion (dpy, w->borderSize);
448 w->borderSize = border_size (dpy, w);
450 XFixesDestroyRegion (dpy, w->extents);
451 w->extents = win_extents (dpy, w);
452 if (w->mode == WINDOW_SOLID)
454 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
455 XFixesSubtractRegion (dpy, region, region, w->borderSize);
456 XRenderComposite (dpy, PictOpSrc, w->picture, None, rootBuffer,
458 w->a.x + w->a.border_width,
459 w->a.y + w->a.border_width,
463 w->borderClip = XFixesCreateRegion (dpy, 0, 0);
464 XFixesCopyRegion (dpy, w->borderClip, region);
468 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
470 for (w = t; w; w = w->prev_trans)
472 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, w->borderClip);
475 XRenderComposite (dpy, PictOpOver, blackPicture, w->shadow, rootBuffer,
477 w->a.x + w->a.border_width + w->shadow_dx,
478 w->a.y + w->a.border_width + w->shadow_dy,
479 w->shadow_width, w->shadow_height);
481 if (w->mode == WINDOW_TRANS)
482 XRenderComposite (dpy, PictOpOver, w->picture, transPicture, rootBuffer,
484 w->a.x + w->a.border_width,
485 w->a.y + w->a.border_width,
488 else if (w->mode == WINDOW_ARGB)
489 XRenderComposite (dpy, PictOpOver, w->picture, None, rootBuffer,
491 w->a.x + w->a.border_width,
492 w->a.y + w->a.border_width,
495 XFixesDestroyRegion (dpy, w->borderClip);
496 w->borderClip = None;
498 XFixesDestroyRegion (dpy, region);
499 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, None);
500 XRenderComposite (dpy, PictOpSrc, rootBuffer, None, rootPicture,
501 0, 0, 0, 0, 0, 0, root_width, root_height);
505 add_damage (Display *dpy, XserverRegion damage)
509 XFixesUnionRegion (dpy, allDamage, allDamage, damage);
510 XFixesDestroyRegion (dpy, damage);
517 repair_win (Display *dpy, Window id)
519 win *w = find_win (dpy, id);
524 /* printf ("repair 0x%x\n", w->id); */
525 parts = XFixesCreateRegion (dpy, 0, 0);
526 XDamageSubtract (dpy, w->damage, None, parts);
527 XFixesTranslateRegion (dpy, parts, w->a.x, w->a.y);
528 add_damage (dpy, parts);
532 map_win (Display *dpy, Window id)
534 win *w = find_win (dpy, id);
535 XserverRegion region;
539 w->a.map_state = IsViewable;
542 w->damage = XDamageCreate (dpy, id, XDamageReportNonEmpty);
543 region = win_extents (dpy, w);
544 add_damage (dpy, region);
549 unmap_win (Display *dpy, Window id)
551 win *w = find_win (dpy, id);
555 w->a.map_state = IsUnmapped;
556 if (w->damage != None)
558 XDamageDestroy (dpy, w->damage);
561 if (w->extents != None)
563 add_damage (dpy, w->extents); /* destroys region */
569 add_win (Display *dpy, Window id, Window prev)
571 win *new = malloc (sizeof (win));
573 XRenderPictureAttributes pa;
574 XRenderPictFormat *format;
580 for (p = &list; *p; p = &(*p)->next)
581 if ((*p)->id == prev)
587 if (!XGetWindowAttributes (dpy, id, &new->a))
594 pa.subwindow_mode = IncludeInferiors;
595 if (new->a.class == InputOnly)
602 format = XRenderFindVisualFormat (dpy, new->a.visual);
603 new->picture = XRenderCreatePicture (dpy, id,
610 new->borderSize = None;
612 if (format && format->type == PictTypeDirect && format->direct.alphaMask)
613 new->mode = WINDOW_ARGB;
614 else if (new->a.override_redirect)
615 new->mode = WINDOW_TRANS;
617 new->mode = WINDOW_SOLID;
620 if (new->a.map_state == IsViewable)
625 configure_win (Display *dpy, XConfigureEvent *ce)
627 win *w = find_win (dpy, ce->window);
629 XserverRegion damage = None;
633 if (ce->window == root)
637 XRenderFreePicture (dpy, rootBuffer);
640 root_width = ce->width;
641 root_height = ce->height;
645 if (w->a.map_state == IsViewable)
647 damage = XFixesCreateRegion (dpy, 0, 0);
648 if (w->extents != None)
649 XFixesCopyRegion (dpy, damage, w->extents);
653 if (w->a.width != ce->width || w->a.height != ce->height)
656 XRenderFreePicture (dpy, w->shadow);
659 w->a.width = ce->width;
660 w->a.height = ce->height;
661 w->a.border_width = ce->border_width;
662 w->a.override_redirect = ce->override_redirect;
667 if (above != ce->above)
672 for (prev = &list; *prev; prev = &(*prev)->next)
678 for (prev = &list; *prev; prev = &(*prev)->next)
680 if ((*prev)->id == ce->above)
688 XserverRegion extents = win_extents (dpy, w);
689 XFixesUnionRegion (dpy, damage, damage, extents);
690 XFixesDestroyRegion (dpy, extents);
691 add_damage (dpy, damage);
696 destroy_win (Display *dpy, Window id, Bool gone)
700 for (prev = &list; (w = *prev); prev = &w->next)
707 XRenderFreePicture (dpy, w->picture);
719 printf ("\t%08lx: %d x %d + %d + %d (%d)\n", w->id,
720 w->a.width, w->a.height, w->a.x, w->a.y, w->a.border_width);
729 printf ("windows:\n");
730 for (w = list; w; w = w->next)
736 damage_win (Display *dpy, XDamageNotifyEvent *de)
738 repair_win (dpy, de->drawable);
742 error (Display *dpy, XErrorEvent *ev)
744 printf ("error %d request %d minor %d\n",
745 ev->error_code, ev->request_code, ev->minor_code);
751 expose_root (Display *dpy, Window root, XRectangle *rects, int nrects)
753 XserverRegion region = XFixesCreateRegion (dpy, rects, nrects);
755 add_damage (dpy, region);
762 time_in_millis (void)
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;
780 unsigned int nchildren;
782 int damage_event, damage_error;
783 int xfixes_event, xfixes_error;
784 XRenderPictureAttributes pa;
786 XRectangle *expose_rects = 0;
793 dpy = XOpenDisplay (0);
796 fprintf (stderr, "Can't open display\n");
799 XSetErrorHandler (error);
800 scr = DefaultScreen (dpy);
801 root = RootWindow (dpy, scr);
802 pa.subwindow_mode = IncludeInferiors;
804 gussianMap = make_gaussian_map(dpy, SHADOW_RADIUS);
806 transPixmap = XCreatePixmap (dpy, root, 1, 1, 8);
808 transPicture = XRenderCreatePicture (dpy, transPixmap,
809 XRenderFindStandardFormat (dpy, PictStandardA8),
812 c.red = c.green = c.blue = 0;
814 XRenderFillRectangle (dpy, PictOpSrc, transPicture, &c, 0, 0, 1, 1);
816 root_width = DisplayWidth (dpy, scr);
817 root_height = DisplayHeight (dpy, scr);
819 rootPicture = XRenderCreatePicture (dpy, root,
820 XRenderFindVisualFormat (dpy,
821 DefaultVisual (dpy, scr)),
824 blackPixmap = XCreatePixmap (dpy, root, 1, 1, 32);
826 blackPicture = XRenderCreatePicture (dpy, blackPixmap,
827 XRenderFindStandardFormat (dpy, PictStandardARGB32),
830 c.red = c.green = c.blue = 0;
832 XRenderFillRectangle (dpy, PictOpSrc, blackPicture, &c, 0, 0, 1, 1);
833 if (!XCompositeQueryExtension (dpy, &event_base, &error_base))
835 fprintf (stderr, "No composite extension\n");
838 if (!XDamageQueryExtension (dpy, &damage_event, &damage_error))
840 fprintf (stderr, "No damage extension\n");
843 if (!XFixesQueryExtension (dpy, &xfixes_event, &xfixes_error))
845 fprintf (stderr, "No XFixes extension\n");
850 XCompositeRedirectSubwindows (dpy, root, CompositeRedirectManual);
851 XSelectInput (dpy, root,
852 SubstructureNotifyMask|
856 XQueryTree (dpy, root, &root_return, &parent_return, &children, &nchildren);
857 for (i = 0; i < nchildren; i++)
858 add_win (dpy, children[i], i ? children[i-1] : None);
861 paint_all (dpy, None);
863 last_update = time_in_millis ();
872 XNextEvent (dpy, &ev);
875 busy_start = time_in_millis();
877 /* printf ("event %d\n", ev.type); */
880 add_win (dpy, ev.xcreatewindow.window, 0);
882 case ConfigureNotify:
883 configure_win (dpy, &ev.xconfigure);
886 destroy_win (dpy, ev.xdestroywindow.window, True);
889 map_win (dpy, ev.xmap.window);
892 unmap_win (dpy, ev.xunmap.window);
895 if (ev.xreparent.parent == root)
896 add_win (dpy, ev.xreparent.window, 0);
898 destroy_win (dpy, ev.xreparent.window, False);
901 if (ev.xexpose.window == root)
903 int more = ev.xexpose.count + 1;
904 if (n_expose == size_expose)
908 expose_rects = realloc (expose_rects,
909 (size_expose + more) *
910 sizeof (XRectangle));
915 expose_rects = malloc (more * sizeof (XRectangle));
919 expose_rects[n_expose].x = ev.xexpose.x;
920 expose_rects[n_expose].y = ev.xexpose.y;
921 expose_rects[n_expose].width = ev.xexpose.width;
922 expose_rects[n_expose].height = ev.xexpose.height;
924 if (ev.xexpose.count == 0)
926 expose_root (dpy, root, expose_rects, n_expose);
932 if (ev.xproperty.atom == XInternAtom (dpy, BACKGROUND_PROP, False))
936 XClearArea (dpy, root, 0, 0, 0, 0, True);
937 XRenderFreePicture (dpy, rootTile);
943 if (ev.type == damage_event + XDamageNotify)
944 damage_win (dpy, (XDamageNotifyEvent *) &ev);
947 } while (XEventsQueued (dpy, QueuedAfterReading));
949 now = time_in_millis ();
950 /* printf ("\t\tbusy %d\n", now - busy_start); */
951 timeout = INTERVAL - (now - last_update);
954 ufd.fd = ConnectionNumber (dpy);
956 n = poll (&ufd, 1, timeout);
957 if (n > 0 && (ufd.revents & POLLIN) && XEventsQueued (dpy, QueuedAfterReading))
964 int old_update = last_update;
965 last_update = time_in_millis();
966 /* printf ("delta %d\n", last_update - old_update); */
968 paint_all (dpy, allDamage);