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.
26 /* Modified by Matthew Hawn. I don't know what to say here so follow what it
27 says above. Not that I can really do anything about it
40 #include <X11/Xutil.h>
41 #include <X11/Xatom.h>
42 #include <X11/extensions/Xcomposite.h>
43 #include <X11/extensions/Xdamage.h>
44 #include <X11/extensions/Xrender.h>
46 #if COMPOSITE_MAJOR > 0 || COMPOSITE_MINOR >= 2
47 #define HAS_NAME_WINDOW_PIXMAP 1
50 #define CAN_DO_USABLE 0
52 typedef struct _ignore {
54 unsigned long sequence;
60 #if HAS_NAME_WINDOW_PIXMAP
65 Bool usable; /* mapped and all damaged at one point */
66 XRectangle damage_bounds; /* bounds of damage */
74 XserverRegion borderSize;
75 XserverRegion extents;
83 unsigned long damage_sequence; /* sequence when damage was created */
85 /* for drawing translucent windows */
86 XserverRegion borderClip;
87 struct _win *prev_trans;
90 typedef struct _conv {
95 typedef struct _fade {
101 void (*callback) (Display *dpy, win *w, Bool gone);
113 Picture blackPicture;
114 Picture transBlackPicture;
116 XserverRegion allDamage;
118 #if HAS_NAME_WINDOW_PIXMAP
121 int root_height, root_width;
122 ignore *ignore_head, **ignore_tail = &ignore_head;
123 int xfixes_event, xfixes_error;
124 int damage_event, damage_error;
125 int composite_event, composite_error;
126 int render_event, render_error;
128 int composite_opcode;
130 /* find these once and be done with it */
142 /* opacity property name; sometime soon I'll write up an EWMH spec for it */
143 #define OPACITY_PROP "_NET_WM_WINDOW_OPACITY"
145 #define TRANSLUCENT 0xe0000000
146 #define OPAQUE 0xffffffff
150 #define WINDOW_SOLID 0
151 #define WINDOW_TRANS 1
152 #define WINDOW_ARGB 2
154 #define TRANS_OPACITY 0.75
156 #define DEBUG_REPAINT 0
157 #define DEBUG_EVENTS 0
158 #define MONITOR_REPAINT 0
161 #define SHARP_SHADOW 0
163 typedef enum _compMode {
164 CompSimple, /* looks like a regular X server */
165 CompServerShadows, /* use window alpha for shadow; sharp, but precise */
166 CompClientShadows, /* use window extents for shadow, blurred */
170 determine_mode(Display *dpy, win *w);
173 get_opacity_percent(Display *dpy, win *w, double def);
176 win_extents (Display *dpy, win *w);
178 CompMode compMode = CompSimple;
180 int shadowRadius = 12;
181 int shadowOffsetX = -15;
182 int shadowOffsetY = -15;
183 double shadowOpacity = .75;
185 double fade_in_step = 0.028;
186 double fade_out_step = 0.03;
189 Bool fadeWindows = False;
190 Bool excludeDockShadows = False;
191 Bool fadeTrans = False;
193 Bool autoRedirect = False;
195 /* For shadow precomputation */
197 unsigned char *shadowCorner = NULL;
198 unsigned char *shadowTop = NULL;
201 get_time_in_milliseconds ()
205 gettimeofday (&tv, NULL);
206 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
214 for (f = fades; f; f = f->next)
223 dequeue_fade (Display *dpy, fade *f)
227 for (prev = &fades; *prev; prev = &(*prev)->next)
232 (*f->callback) (dpy, f->w, f->gone);
239 cleanup_fade (Display *dpy, win *w)
241 fade *f = find_fade (w);
243 dequeue_fade (dpy, f);
247 enqueue_fade (Display *dpy, fade *f)
250 fade_time = get_time_in_milliseconds () + fade_delta;
256 set_fade (Display *dpy, win *w, double start, double finish, double step,
257 void (*callback) (Display *dpy, win *w, Bool gone),
258 Bool gone, Bool exec_callback, Bool override)
265 f = malloc (sizeof (fade));
269 enqueue_fade (dpy, f);
277 (*f->callback)(dpy, f->w, f->gone);
287 else if (f->cur > finish)
289 f->callback = callback;
291 w->opacity = f->cur * OPAQUE;
293 printf ("set_fade start %g step %g\n", f->cur, f->step);
295 determine_mode (dpy, w);
298 XRenderFreePicture (dpy, w->shadow);
300 w->extents = win_extents (dpy, w);
311 now = get_time_in_milliseconds();
312 delta = fade_time - now;
315 /* printf ("timeout %d\n", delta); */
320 run_fades (Display *dpy)
322 int now = get_time_in_milliseconds();
327 printf ("run fades\n");
329 if (fade_time - now > 0)
331 steps = 1 + (now - fade_time) / fade_delta;
332 for (next = fades; f = next; )
336 f->cur += f->step * steps;
342 printf ("opacity now %g\n", f->cur);
344 w->opacity = f->cur * OPAQUE;
347 if (f->cur >= f->finish)
349 w->opacity = f->finish*OPAQUE;
350 dequeue_fade (dpy, f);
355 if (f->cur <= f->finish)
357 w->opacity = f->finish*OPAQUE;
358 dequeue_fade (dpy, f);
361 determine_mode (dpy, w);
364 XRenderFreePicture (dpy, w->shadow);
366 w->extents = win_extents(dpy, w);
369 fade_time = now + fade_delta;
373 gaussian (double r, double x, double y)
375 return ((1 / (sqrt (2 * M_PI * r))) *
376 exp ((- (x * x + y * y)) / (2 * r * r)));
381 make_gaussian_map (Display *dpy, double r)
384 int size = ((int) ceil ((r * 3)) + 1) & ~1;
385 int center = size / 2;
390 c = malloc (sizeof (conv) + size * size * sizeof (double));
392 c->data = (double *) (c + 1);
394 for (y = 0; y < size; y++)
395 for (x = 0; x < size; x++)
397 g = gaussian (r, (double) (x - center), (double) (y - center));
399 c->data[y * size + x] = g;
401 /* printf ("gaussian total %f\n", t); */
402 for (y = 0; y < size; y++)
403 for (x = 0; x < size; x++)
405 c->data[y*size + x] /= t;
411 * A picture will help
413 * -center 0 width width+center
414 * -center +-----+-------------------+-----+
417 * 0 +-----+-------------------+-----+
421 * height +-----+-------------------+-----+
424 * center +-----+-------------------+-----+
428 sum_gaussian (conv *map, double opacity, int x, int y, int width, int height)
432 double *g_line = map->data;
433 int g_size = map->size;
434 int center = g_size / 2;
435 int fx_start, fx_end;
436 int fy_start, fy_end;
440 * Compute set of filter values which are "in range",
441 * that's the set with:
442 * 0 <= x + (fx-center) && x + (fx-center) < width &&
443 * 0 <= y + (fy-center) && y + (fy-center) < height
445 * 0 <= x + (fx - center) x + fx - center < width
446 * center - x <= fx fx < width + center - x
449 fx_start = center - x;
452 fx_end = width + center - x;
456 fy_start = center - y;
459 fy_end = height + center - y;
463 g_line = g_line + fy_start * g_size + fx_start;
466 for (fy = fy_start; fy < fy_end; fy++)
471 for (fx = fx_start; fx < fx_end; fx++)
477 return ((unsigned char) (v * opacity * 255.0));
480 /* precompute shadow corners and sides to save time for large windows */
482 presum_gaussian (conv *map)
484 int center = map->size/2;
490 free ((void *)shadowCorner);
492 free ((void *)shadowTop);
494 shadowCorner = (unsigned char *)(malloc ((Gsize + 1) * (Gsize + 1) * 26));
495 shadowTop = (unsigned char *)(malloc ((Gsize + 1) * 26));
497 for (x = 0; x <= Gsize; x++)
499 shadowTop[25 * (Gsize + 1) + x] = sum_gaussian (map, 1, x - center, center, Gsize * 2, Gsize * 2);
500 for(opacity = 0; opacity < 25; opacity++)
501 shadowTop[opacity * (Gsize + 1) + x] = shadowTop[25 * (Gsize + 1) + x] * opacity / 25;
502 for(y = 0; y <= x; y++)
504 shadowCorner[25 * (Gsize + 1) * (Gsize + 1) + y * (Gsize + 1) + x]
505 = sum_gaussian (map, 1, x - center, y - center, Gsize * 2, Gsize * 2);
506 shadowCorner[25 * (Gsize + 1) * (Gsize + 1) + x * (Gsize + 1) + y]
507 = shadowCorner[25 * (Gsize + 1) * (Gsize + 1) + y * (Gsize + 1) + x];
508 for(opacity = 0; opacity < 25; opacity++)
509 shadowCorner[opacity * (Gsize + 1) * (Gsize + 1) + y * (Gsize + 1) + x]
510 = shadowCorner[opacity * (Gsize + 1) * (Gsize + 1) + x * (Gsize + 1) + y]
511 = shadowCorner[25 * (Gsize + 1) * (Gsize + 1) + y * (Gsize + 1) + x] * opacity / 25;
517 make_shadow (Display *dpy, double opacity, int width, int height)
521 int gsize = gaussianMap->size;
523 int swidth = width + gsize;
524 int sheight = height + gsize;
525 int center = gsize / 2;
529 int opacity_int = (int)(opacity * 25);
530 data = malloc (swidth * sheight * sizeof (unsigned char));
533 ximage = XCreateImage (dpy,
534 DefaultVisual(dpy, DefaultScreen(dpy)),
539 swidth, sheight, 8, swidth * sizeof (unsigned char));
546 * Build the gaussian in sections
550 * center (fill the complete data array)
553 d = shadowTop[opacity_int * (Gsize + 1) + Gsize];
555 d = sum_gaussian (gaussianMap, opacity, center, center, width, height);
556 memset(data, d, sheight * swidth);
562 if (ylimit > sheight / 2)
563 ylimit = (sheight + 1) / 2;
565 if (xlimit > swidth / 2)
566 xlimit = (swidth + 1) / 2;
568 for (y = 0; y < ylimit; y++)
569 for (x = 0; x < xlimit; x++)
571 if (xlimit == Gsize && ylimit == Gsize)
572 d = shadowCorner[opacity_int * (Gsize + 1) * (Gsize + 1) + y * (Gsize + 1) + x];
574 d = sum_gaussian (gaussianMap, opacity, x - center, y - center, width, height);
575 data[y * swidth + x] = d;
576 data[(sheight - y - 1) * swidth + x] = d;
577 data[(sheight - y - 1) * swidth + (swidth - x - 1)] = d;
578 data[y * swidth + (swidth - x - 1)] = d;
584 x_diff = swidth - (gsize * 2);
585 if (x_diff > 0 && ylimit > 0)
587 for (y = 0; y < ylimit; y++)
590 d = shadowTop[opacity_int * (Gsize + 1) + y];
592 d = sum_gaussian (gaussianMap, opacity, center, y - center, width, height);
593 memset (&data[y * swidth + gsize], d, x_diff);
594 memset (&data[(sheight - y - 1) * swidth + gsize], d, x_diff);
602 for (x = 0; x < xlimit; x++)
605 d = shadowTop[opacity_int * (Gsize + 1) + x];
607 d = sum_gaussian (gaussianMap, opacity, x - center, center, width, height);
608 for (y = gsize; y < sheight - gsize; y++)
610 data[y * swidth + x] = d;
611 data[y * swidth + (swidth - x - 1)] = d;
619 shadow_picture (Display *dpy, double opacity, Picture alpha_pict, int width, int height, int *wp, int *hp)
624 Picture shadowPicture;
625 Picture finalPicture;
628 shadowImage = make_shadow (dpy, opacity, width, height);
631 shadowPixmap = XCreatePixmap (dpy, root,
637 XDestroyImage (shadowImage);
641 shadowPicture = XRenderCreatePicture (dpy, shadowPixmap,
642 XRenderFindStandardFormat (dpy, PictStandardA8),
646 XDestroyImage (shadowImage);
647 XFreePixmap (dpy, shadowPixmap);
651 gc = XCreateGC (dpy, shadowPixmap, 0, 0);
654 XDestroyImage (shadowImage);
655 XFreePixmap (dpy, shadowPixmap);
656 XRenderFreePicture (dpy, shadowPicture);
660 XPutImage (dpy, shadowPixmap, gc, shadowImage, 0, 0, 0, 0,
662 shadowImage->height);
663 *wp = shadowImage->width;
664 *hp = shadowImage->height;
666 XDestroyImage (shadowImage);
667 XFreePixmap (dpy, shadowPixmap);
668 return shadowPicture;
672 solid_picture (Display *dpy, Bool argb, double a, double r, double g, double b)
676 XRenderPictureAttributes pa;
679 pixmap = XCreatePixmap (dpy, root, 1, 1, argb ? 32 : 8);
684 picture = XRenderCreatePicture (dpy, pixmap,
685 XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8),
690 XFreePixmap (dpy, pixmap);
694 c.alpha = a * 0xffff;
696 c.green = g * 0xffff;
698 XRenderFillRectangle (dpy, PictOpSrc, picture, &c, 0, 0, 1, 1);
699 XFreePixmap (dpy, pixmap);
704 discard_ignore (Display *dpy, unsigned long sequence)
708 if ((long) (sequence - ignore_head->sequence) > 0)
710 ignore *next = ignore_head->next;
714 ignore_tail = &ignore_head;
722 set_ignore (Display *dpy, unsigned long sequence)
724 ignore *i = malloc (sizeof (ignore));
727 i->sequence = sequence;
730 ignore_tail = &i->next;
734 should_ignore (Display *dpy, unsigned long sequence)
736 discard_ignore (dpy, sequence);
737 return ignore_head && ignore_head->sequence == sequence;
741 find_win (Display *dpy, Window id)
745 for (w = list; w; w = w->next)
751 static char *backgroundProps[] = {
758 root_tile (Display *dpy)
764 unsigned long nitems;
765 unsigned long bytes_after;
768 XRenderPictureAttributes pa;
772 for (p = 0; backgroundProps[p]; p++)
774 if (XGetWindowProperty (dpy, root, XInternAtom (dpy, backgroundProps[p], False),
775 0, 4, False, AnyPropertyType,
776 &actual_type, &actual_format, &nitems, &bytes_after, &prop) == Success &&
777 actual_type == XInternAtom (dpy, "PIXMAP", False) && actual_format == 32 && nitems == 1)
779 memcpy (&pixmap, prop, 4);
787 pixmap = XCreatePixmap (dpy, root, 1, 1, DefaultDepth (dpy, scr));
791 picture = XRenderCreatePicture (dpy, pixmap,
792 XRenderFindVisualFormat (dpy,
793 DefaultVisual (dpy, scr)),
799 c.red = c.green = c.blue = 0x8080;
801 XRenderFillRectangle (dpy, PictOpSrc, picture, &c,
808 paint_root (Display *dpy)
811 rootTile = root_tile (dpy);
813 XRenderComposite (dpy, PictOpSrc,
814 rootTile, None, rootBuffer,
815 0, 0, 0, 0, 0, 0, root_width, root_height);
819 win_extents (Display *dpy, win *w)
825 r.width = w->a.width + w->a.border_width * 2;
826 r.height = w->a.height + w->a.border_width * 2;
827 if (compMode != CompSimple && !(w->windowType == winDockAtom && excludeDockShadows))
829 if (compMode == CompServerShadows || w->mode != WINDOW_ARGB)
833 if (compMode == CompServerShadows)
837 w->shadow_width = w->a.width;
838 w->shadow_height = w->a.height;
842 w->shadow_dx = shadowOffsetX;
843 w->shadow_dy = shadowOffsetY;
846 double opacity = shadowOpacity;
847 if (w->mode == WINDOW_TRANS)
848 opacity = opacity * ((double)w->opacity)/((double)OPAQUE);
849 w->shadow = shadow_picture (dpy, opacity, w->alphaPict,
850 w->a.width + w->a.border_width * 2,
851 w->a.height + w->a.border_width * 2,
852 &w->shadow_width, &w->shadow_height);
855 sr.x = w->a.x + w->shadow_dx;
856 sr.y = w->a.y + w->shadow_dy;
857 sr.width = w->shadow_width;
858 sr.height = w->shadow_height;
861 r.width = (r.x + r.width) - sr.x;
866 r.height = (r.y + r.height) - sr.y;
869 if (sr.x + sr.width > r.x + r.width)
870 r.width = sr.x + sr.width - r.x;
871 if (sr.y + sr.height > r.y + r.height)
872 r.height = sr.y + sr.height - r.y;
875 return XFixesCreateRegion (dpy, &r, 1);
879 border_size (Display *dpy, win *w)
881 XserverRegion border;
883 * if window doesn't exist anymore, this will generate an error
884 * as well as not generate a region. Perhaps a better XFixes
885 * architecture would be to have a request that copies instead
886 * of creates, that way you'd just end up with an empty region
887 * instead of an invalid XID.
889 set_ignore (dpy, NextRequest (dpy));
890 border = XFixesCreateRegionFromWindow (dpy, w->id, WindowRegionBounding);
892 set_ignore (dpy, NextRequest (dpy));
893 XFixesTranslateRegion (dpy, border,
894 w->a.x + w->a.border_width,
895 w->a.y + w->a.border_width);
900 paint_all (Display *dpy, XserverRegion region)
910 r.width = root_width;
911 r.height = root_height;
912 region = XFixesCreateRegion (dpy, &r, 1);
915 rootBuffer = rootPicture;
919 Pixmap rootPixmap = XCreatePixmap (dpy, root, root_width, root_height,
920 DefaultDepth (dpy, scr));
921 rootBuffer = XRenderCreatePicture (dpy, rootPixmap,
922 XRenderFindVisualFormat (dpy,
923 DefaultVisual (dpy, scr)),
925 XFreePixmap (dpy, rootPixmap);
928 XFixesSetPictureClipRegion (dpy, rootPicture, 0, 0, region);
930 XRenderComposite (dpy, PictOpSrc, blackPicture, None, rootPicture,
931 0, 0, 0, 0, 0, 0, root_width, root_height);
936 for (w = list; w; w = w->next)
942 /* never painted, ignore it */
945 /* if invisible, ignore it */
946 if (w->a.x + w->a.width < 1 || w->a.y + w->a.height < 1
947 || w->a.x >= root_width || w->a.y >= root_height)
951 XRenderPictureAttributes pa;
952 XRenderPictFormat *format;
953 Drawable draw = w->id;
955 #if HAS_NAME_WINDOW_PIXMAP
956 if (hasNamePixmap && !w->pixmap)
957 w->pixmap = XCompositeNameWindowPixmap (dpy, w->id);
961 format = XRenderFindVisualFormat (dpy, w->a.visual);
962 pa.subwindow_mode = IncludeInferiors;
963 w->picture = XRenderCreatePicture (dpy, draw,
969 printf (" 0x%x", w->id);
975 set_ignore (dpy, NextRequest (dpy));
976 XFixesDestroyRegion (dpy, w->borderSize);
977 w->borderSize = None;
981 XFixesDestroyRegion (dpy, w->extents);
986 XFixesDestroyRegion (dpy, w->borderClip);
987 w->borderClip = None;
991 w->borderSize = border_size (dpy, w);
993 w->extents = win_extents (dpy, w);
994 if (w->mode == WINDOW_SOLID)
997 #if HAS_NAME_WINDOW_PIXMAP
1000 wid = w->a.width + w->a.border_width * 2;
1001 hei = w->a.height + w->a.border_width * 2;
1003 x = w->a.x + w->a.border_width;
1004 y = w->a.y + w->a.border_width;
1008 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
1009 set_ignore (dpy, NextRequest (dpy));
1010 XFixesSubtractRegion (dpy, region, region, w->borderSize);
1011 set_ignore (dpy, NextRequest (dpy));
1012 XRenderComposite (dpy, PictOpSrc, w->picture, None, rootBuffer,
1018 w->borderClip = XFixesCreateRegion (dpy, 0, 0);
1019 XFixesCopyRegion (dpy, w->borderClip, region);
1028 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
1030 for (w = t; w; w = w->prev_trans)
1032 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, w->borderClip);
1036 case CompServerShadows:
1037 /* dont' bother drawing shadows on desktop windows */
1038 if (w->windowType == winDesktopAtom)
1040 set_ignore (dpy, NextRequest (dpy));
1041 if (w->opacity != OPAQUE && !w->shadowPict)
1042 w->shadowPict = solid_picture (dpy, True,
1043 (double) w->opacity / OPAQUE * 0.3,
1045 XRenderComposite (dpy, PictOpOver,
1046 w->shadowPict ? w->shadowPict : transBlackPicture,
1047 w->picture, rootBuffer,
1049 w->a.x + w->shadow_dx,
1050 w->a.y + w->shadow_dy,
1051 w->shadow_width, w->shadow_height);
1053 case CompClientShadows:
1054 /* don't bother drawing shadows on desktop windows */
1055 if (w->shadow && w->windowType != winDesktopAtom)
1057 XRenderComposite (dpy, PictOpOver, blackPicture, w->shadow, rootBuffer,
1059 w->a.x + w->shadow_dx,
1060 w->a.y + w->shadow_dy,
1061 w->shadow_width, w->shadow_height);
1065 if (w->opacity != OPAQUE && !w->alphaPict)
1066 w->alphaPict = solid_picture (dpy, False,
1067 (double) w->opacity / OPAQUE, 0, 0, 0);
1068 if (w->mode == WINDOW_TRANS)
1071 #if HAS_NAME_WINDOW_PIXMAP
1074 wid = w->a.width + w->a.border_width * 2;
1075 hei = w->a.height + w->a.border_width * 2;
1077 x = w->a.x + w->a.border_width;
1078 y = w->a.y + w->a.border_width;
1082 set_ignore (dpy, NextRequest (dpy));
1083 XRenderComposite (dpy, PictOpOver, w->picture, w->alphaPict, rootBuffer,
1087 else if (w->mode == WINDOW_ARGB)
1090 #if HAS_NAME_WINDOW_PIXMAP
1093 wid = w->a.width + w->a.border_width * 2;
1094 hei = w->a.height + w->a.border_width * 2;
1096 x = w->a.x + w->a.border_width;
1097 y = w->a.y + w->a.border_width;
1101 set_ignore (dpy, NextRequest (dpy));
1102 XRenderComposite (dpy, PictOpOver, w->picture, w->alphaPict, rootBuffer,
1106 XFixesDestroyRegion (dpy, w->borderClip);
1107 w->borderClip = None;
1109 XFixesDestroyRegion (dpy, region);
1110 if (rootBuffer != rootPicture)
1112 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, None);
1113 XRenderComposite (dpy, PictOpSrc, rootBuffer, None, rootPicture,
1114 0, 0, 0, 0, 0, 0, root_width, root_height);
1119 add_damage (Display *dpy, XserverRegion damage)
1123 XFixesUnionRegion (dpy, allDamage, allDamage, damage);
1124 XFixesDestroyRegion (dpy, damage);
1131 repair_win (Display *dpy, win *w)
1133 XserverRegion parts;
1137 parts = win_extents (dpy, w);
1138 set_ignore (dpy, NextRequest (dpy));
1139 XDamageSubtract (dpy, w->damage, None, None);
1144 parts = XFixesCreateRegion (dpy, 0, 0);
1145 set_ignore (dpy, NextRequest (dpy));
1146 XDamageSubtract (dpy, w->damage, None, parts);
1147 XFixesTranslateRegion (dpy, parts,
1148 w->a.x + w->a.border_width,
1149 w->a.y + w->a.border_width);
1150 if (compMode == CompServerShadows)
1152 o = XFixesCreateRegion (dpy, 0, 0);
1153 XFixesCopyRegion (dpy, o, parts);
1154 XFixesTranslateRegion (dpy, o, w->shadow_dx, w->shadow_dy);
1155 XFixesUnionRegion (dpy, parts, parts, o);
1156 XFixesDestroyRegion (dpy, o);
1159 add_damage (dpy, parts);
1164 map_win (Display *dpy, Window id, unsigned long sequence, Bool fade)
1166 win *w = find_win (dpy, id);
1172 w->a.map_state = IsViewable;
1174 /* This needs to be here or else we lose transparency messages */
1175 XSelectInput (dpy, id, PropertyChangeMask);
1178 w->damage_bounds.x = w->damage_bounds.y = 0;
1179 w->damage_bounds.width = w->damage_bounds.height = 0;
1183 if (fade && fadeWindows)
1184 set_fade (dpy, w, 0, get_opacity_percent (dpy, w, 1.0), fade_in_step, 0, False, True, True);
1188 finish_unmap_win (Display *dpy, win *w)
1194 if (w->extents != None)
1196 add_damage (dpy, w->extents); /* destroys region */
1200 #if HAS_NAME_WINDOW_PIXMAP
1203 XFreePixmap (dpy, w->pixmap);
1210 set_ignore (dpy, NextRequest (dpy));
1211 XRenderFreePicture (dpy, w->picture);
1215 /* don't care about properties anymore */
1216 set_ignore (dpy, NextRequest (dpy));
1217 XSelectInput(dpy, w->id, 0);
1221 set_ignore (dpy, NextRequest (dpy));
1222 XFixesDestroyRegion (dpy, w->borderSize);
1223 w->borderSize = None;
1227 XRenderFreePicture (dpy, w->shadow);
1232 XFixesDestroyRegion (dpy, w->borderClip);
1233 w->borderClip = None;
1239 #if HAS_NAME_WINDOW_PIXMAP
1241 unmap_callback (Display *dpy, win *w, Bool gone)
1243 finish_unmap_win (dpy, w);
1248 unmap_win (Display *dpy, Window id, Bool fade)
1250 win *w = find_win (dpy, id);
1253 w->a.map_state = IsUnmapped;
1254 #if HAS_NAME_WINDOW_PIXMAP
1255 if (w->pixmap && fade && fadeWindows)
1256 set_fade (dpy, w, w->opacity*1.0/OPAQUE, 0.0, fade_out_step, unmap_callback, False, False, True);
1259 finish_unmap_win (dpy, w);
1262 /* Get the opacity prop from window
1267 get_opacity_prop(Display *dpy, win *w, unsigned int def)
1271 unsigned long n, left;
1273 unsigned char *data;
1274 int result = XGetWindowProperty(dpy, w->id, opacityAtom, 0L, 1L, False,
1275 XA_CARDINAL, &actual, &format,
1277 if (result == Success && data != NULL)
1280 memcpy (&i, data, sizeof (unsigned int));
1281 XFree( (void *) data);
1287 /* Get the opacity property from the window in a percent format
1289 otherwise: the value
1292 get_opacity_percent(Display *dpy, win *w, double def)
1294 unsigned int opacity = get_opacity_prop (dpy, w, (unsigned int)(OPAQUE*def));
1296 return opacity*1.0/OPAQUE;
1299 /* determine mode for window all in one place.
1300 Future might check for menu flag and other cool things
1304 get_wintype_prop(Display * dpy, Window w)
1308 unsigned long n, left;
1310 unsigned char *data;
1311 int result = XGetWindowProperty (dpy, w, winTypeAtom, 0L, 1L, False,
1312 XA_ATOM, &actual, &format,
1315 if (result == Success && data != None)
1318 memcpy (&a, data, sizeof (Atom));
1319 XFree ( (void *) data);
1322 return winNormalAtom;
1326 determine_mode(Display *dpy, win *w)
1329 XRenderPictFormat *format;
1330 unsigned int default_opacity;
1332 /* if trans prop == -1 fall back on previous tests*/
1336 XRenderFreePicture (dpy, w->alphaPict);
1337 w->alphaPict = None;
1341 XRenderFreePicture (dpy, w->shadowPict);
1342 w->shadowPict = None;
1345 if (w->a.class == InputOnly)
1351 format = XRenderFindVisualFormat (dpy, w->a.visual);
1354 if (format && format->type == PictTypeDirect && format->direct.alphaMask)
1358 else if (w->opacity != OPAQUE)
1360 mode = WINDOW_TRANS;
1364 mode = WINDOW_SOLID;
1369 XserverRegion damage;
1370 damage = XFixesCreateRegion (dpy, 0, 0);
1371 XFixesCopyRegion (dpy, damage, w->extents);
1372 add_damage (dpy, damage);
1377 determine_wintype (Display *dpy, Window w)
1379 Window root_return, parent_return;
1380 Window *children = NULL;
1381 unsigned int nchildren, i;
1384 type = get_wintype_prop (dpy, w);
1385 if (type != winNormalAtom)
1388 if (!XQueryTree (dpy, w, &root_return, &parent_return, &children,
1391 /* XQueryTree failed. */
1393 XFree ((void *)children);
1394 return winNormalAtom;
1397 for (i = 0;i < nchildren;i++)
1399 type = determine_wintype (dpy, children[i]);
1400 if (type != winNormalAtom)
1405 XFree ((void *)children);
1407 return winNormalAtom;
1411 add_win (Display *dpy, Window id, Window prev)
1413 win *new = malloc (sizeof (win));
1420 for (p = &list; *p; p = &(*p)->next)
1421 if ((*p)->id == prev)
1427 set_ignore (dpy, NextRequest (dpy));
1428 if (!XGetWindowAttributes (dpy, id, &new->a))
1435 new->usable = False;
1437 #if HAS_NAME_WINDOW_PIXMAP
1440 new->picture = None;
1441 if (new->a.class == InputOnly)
1443 new->damage_sequence = 0;
1448 new->damage_sequence = NextRequest (dpy);
1449 new->damage = XDamageCreate (dpy, id, XDamageReportNonEmpty);
1451 new->alphaPict = None;
1452 new->shadowPict = None;
1453 new->borderSize = None;
1454 new->extents = None;
1458 new->shadow_width = 0;
1459 new->shadow_height = 0;
1460 new->opacity = OPAQUE;
1462 new->borderClip = None;
1463 new->prev_trans = 0;
1465 /* moved mode setting to one place */
1466 new->opacity = get_opacity_prop (dpy, new, OPAQUE);
1467 new->windowType = determine_wintype (dpy, new->id);
1468 determine_mode (dpy, new);
1472 if (new->a.map_state == IsViewable)
1473 map_win (dpy, id, new->damage_sequence - 1, True);
1477 restack_win (Display *dpy, win *w, Window new_above)
1482 old_above = w->next->id;
1485 if (old_above != new_above)
1490 for (prev = &list; *prev; prev = &(*prev)->next)
1496 for (prev = &list; *prev; prev = &(*prev)->next)
1498 if ((*prev)->id == new_above)
1507 configure_win (Display *dpy, XConfigureEvent *ce)
1509 win *w = find_win (dpy, ce->window);
1511 XserverRegion damage = None;
1515 if (ce->window == root)
1519 XRenderFreePicture (dpy, rootBuffer);
1522 root_width = ce->width;
1523 root_height = ce->height;
1531 damage = XFixesCreateRegion (dpy, 0, 0);
1532 if (w->extents != None)
1533 XFixesCopyRegion (dpy, damage, w->extents);
1537 if (w->a.width != ce->width || w->a.height != ce->height)
1539 #if HAS_NAME_WINDOW_PIXMAP
1542 XFreePixmap (dpy, w->pixmap);
1546 XRenderFreePicture (dpy, w->picture);
1553 XRenderFreePicture (dpy, w->shadow);
1557 w->a.width = ce->width;
1558 w->a.height = ce->height;
1559 w->a.border_width = ce->border_width;
1560 w->a.override_redirect = ce->override_redirect;
1561 restack_win (dpy, w, ce->above);
1564 XserverRegion extents = win_extents (dpy, w);
1565 XFixesUnionRegion (dpy, damage, damage, extents);
1566 XFixesDestroyRegion (dpy, extents);
1567 add_damage (dpy, damage);
1573 circulate_win (Display *dpy, XCirculateEvent *ce)
1575 win *w = find_win (dpy, ce->window);
1581 if (ce->place == PlaceOnTop)
1582 new_above = list->id;
1585 restack_win (dpy, w, new_above);
1590 finish_destroy_win (Display *dpy, Window id, Bool gone)
1594 for (prev = &list; (w = *prev); prev = &w->next)
1598 finish_unmap_win (dpy, w);
1602 set_ignore (dpy, NextRequest (dpy));
1603 XRenderFreePicture (dpy, w->picture);
1608 XRenderFreePicture (dpy, w->alphaPict);
1609 w->alphaPict = None;
1613 XRenderFreePicture (dpy, w->shadowPict);
1614 w->shadowPict = None;
1616 if (w->damage != None)
1618 set_ignore (dpy, NextRequest (dpy));
1619 XDamageDestroy (dpy, w->damage);
1622 cleanup_fade (dpy, w);
1628 #if HAS_NAME_WINDOW_PIXMAP
1630 destroy_callback (Display *dpy, win *w, Bool gone)
1632 finish_destroy_win (dpy, w->id, gone);
1637 destroy_win (Display *dpy, Window id, Bool gone, Bool fade)
1639 win *w = find_win (dpy, id);
1640 #if HAS_NAME_WINDOW_PIXMAP
1641 if (w && w->pixmap && fade && fadeWindows)
1642 set_fade (dpy, w, w->opacity*1.0/OPAQUE, 0.0, fade_out_step, destroy_callback, gone, False, True);
1646 finish_destroy_win (dpy, id, gone);
1654 printf ("\t%08lx: %d x %d + %d + %d (%d)\n", w->id,
1655 w->a.width, w->a.height, w->a.x, w->a.y, w->a.border_width);
1664 printf ("windows:\n");
1665 for (w = list; w; w = w->next)
1671 damage_win (Display *dpy, XDamageNotifyEvent *de)
1673 win *w = find_win (dpy, de->drawable);
1680 if (w->damage_bounds.width == 0 || w->damage_bounds.height == 0)
1682 w->damage_bounds = de->area;
1686 if (de->area.x < w->damage_bounds.x)
1688 w->damage_bounds.width += (w->damage_bounds.x - de->area.x);
1689 w->damage_bounds.x = de->area.x;
1691 if (de->area.y < w->damage_bounds.y)
1693 w->damage_bounds.height += (w->damage_bounds.y - de->area.y);
1694 w->damage_bounds.y = de->area.y;
1696 if (de->area.x + de->area.width > w->damage_bounds.x + w->damage_bounds.width)
1697 w->damage_bounds.width = de->area.x + de->area.width - w->damage_bounds.x;
1698 if (de->area.y + de->area.height > w->damage_bounds.y + w->damage_bounds.height)
1699 w->damage_bounds.height = de->area.y + de->area.height - w->damage_bounds.y;
1702 printf ("unusable damage %d, %d: %d x %d bounds %d, %d: %d x %d\n",
1709 w->damage_bounds.width,
1710 w->damage_bounds.height);
1712 if (w->damage_bounds.x <= 0 &&
1713 w->damage_bounds.y <= 0 &&
1714 w->a.width <= w->damage_bounds.x + w->damage_bounds.width &&
1715 w->a.height <= w->damage_bounds.y + w->damage_bounds.height)
1719 set_fade (dpy, w, 0, get_opacity_percent (dpy, w, 1.0), fade_in_step, 0, False, True, True);
1725 repair_win (dpy, w);
1729 error (Display *dpy, XErrorEvent *ev)
1734 if (should_ignore (dpy, ev->serial))
1737 if (ev->request_code == composite_opcode &&
1738 ev->minor_code == X_CompositeRedirectSubwindows)
1740 fprintf (stderr, "Another composite manager is already running\n");
1744 o = ev->error_code - xfixes_error;
1746 case BadRegion: name = "BadRegion"; break;
1749 o = ev->error_code - damage_error;
1751 case BadDamage: name = "BadDamage"; break;
1754 o = ev->error_code - render_error;
1756 case BadPictFormat: name ="BadPictFormat"; break;
1757 case BadPicture: name ="BadPicture"; break;
1758 case BadPictOp: name ="BadPictOp"; break;
1759 case BadGlyphSet: name ="BadGlyphSet"; break;
1760 case BadGlyph: name ="BadGlyph"; break;
1764 printf ("error %d request %d minor %d serial %d\n",
1765 ev->error_code, ev->request_code, ev->minor_code, ev->serial);
1767 /* abort (); this is just annoying to most people */
1772 expose_root (Display *dpy, Window root, XRectangle *rects, int nrects)
1774 XserverRegion region = XFixesCreateRegion (dpy, rects, nrects);
1776 add_damage (dpy, region);
1781 ev_serial (XEvent *ev)
1783 if (ev->type & 0x7f != KeymapNotify)
1784 return ev->xany.serial;
1785 return NextRequest (ev->xany.display);
1790 ev_name (XEvent *ev)
1792 static char buf[128];
1793 switch (ev->type & 0x7f) {
1800 case ReparentNotify:
1802 case CirculateNotify:
1805 if (ev->type == damage_event + XDamageNotify)
1807 sprintf (buf, "Event %d", ev->type);
1813 ev_window (XEvent *ev)
1817 return ev->xexpose.window;
1819 return ev->xmap.window;
1821 return ev->xunmap.window;
1822 case ReparentNotify:
1823 return ev->xreparent.window;
1824 case CirculateNotify:
1825 return ev->xcirculate.window;
1827 if (ev->type == damage_event + XDamageNotify)
1828 return ((XDamageNotifyEvent *) ev)->drawable;
1834 usage (char *program)
1836 fprintf (stderr, "%s v1.1\n", program);
1837 fprintf (stderr, "usage: %s [options]\n", program);
1838 fprintf (stderr, "Options\n");
1839 fprintf (stderr, " -d display\n Specifies which display should be managed.\n");
1840 fprintf (stderr, " -r radius\n Specifies the blur radius for client-side shadows. (default 12)\n");
1841 fprintf (stderr, " -o opacity\n Specifies the translucency for client-side shadows. (default .75)\n");
1842 fprintf (stderr, " -l left-offset\n Specifies the left offset for client-side shadows. (default -15)\n");
1843 fprintf (stderr, " -t top-offset\n Specifies the top offset for clinet-side shadows. (default -15)\n");
1844 fprintf (stderr, " -I fade-in-step\n Specifies the opacity change between steps while fading in. (default 0.028)\n");
1845 fprintf (stderr, " -O fade-out-step\n Specifies the opacity change between steps while fading out. (default 0.03)\n");
1846 fprintf (stderr, " -D fade-delta-time\n Specifies the time between steps in a fade in milliseconds. (default 10)\n");
1847 fprintf (stderr, " -a\n Use automatic server-side compositing. Faster, but no special effects.\n");
1848 fprintf (stderr, " -c\n Draw client-side shadows with fuzzy edges.\n");
1849 fprintf (stderr, " -C\n Avoid drawing shadows on dock/panel windows.\n");
1850 fprintf (stderr, " -f\n Fade windows in/out when opening/closing.\n");
1851 fprintf (stderr, " -F\n Fade windows during opacity changes.\n");
1852 fprintf (stderr, " -n\n Normal client-side compositing with transparency support\n");
1853 fprintf (stderr, " -s\n Draw server-side shadows with sharp edges.\n");
1854 fprintf (stderr, " -S\n Enable synchronous operation (for debugging).\n");
1859 main (int argc, char **argv)
1862 Window root_return, parent_return;
1866 unsigned int nchildren;
1868 XRenderPictureAttributes pa;
1870 XRectangle *expose_rects = 0;
1871 int size_expose = 0;
1878 int composite_major, composite_minor;
1882 while ((o = getopt (argc, argv, "D:I:O:d:r:o:l:t:scnfFCaS")) != -1)
1889 fade_delta = atoi (optarg);
1894 fade_in_step = atof (optarg);
1895 if (fade_in_step <= 0)
1896 fade_in_step = 0.01;
1899 fade_out_step = atof (optarg);
1900 if (fade_out_step <= 0)
1901 fade_out_step = 0.01;
1904 compMode = CompServerShadows;
1907 compMode = CompClientShadows;
1910 excludeDockShadows = True;
1913 compMode = CompSimple;
1922 autoRedirect = True;
1928 shadowRadius = atoi (optarg);
1931 shadowOpacity = atof (optarg);
1934 shadowOffsetX = atoi (optarg);
1937 shadowOffsetY = atoi (optarg);
1945 dpy = XOpenDisplay (display);
1948 fprintf (stderr, "Can't open display\n");
1951 XSetErrorHandler (error);
1953 XSynchronize (dpy, 1);
1954 scr = DefaultScreen (dpy);
1955 root = RootWindow (dpy, scr);
1957 if (!XRenderQueryExtension (dpy, &render_event, &render_error))
1959 fprintf (stderr, "No render extension\n");
1962 if (!XQueryExtension (dpy, COMPOSITE_NAME, &composite_opcode,
1963 &composite_event, &composite_error))
1965 fprintf (stderr, "No composite extension\n");
1968 XCompositeQueryVersion (dpy, &composite_major, &composite_minor);
1969 #if HAS_NAME_WINDOW_PIXMAP
1970 if (composite_major > 0 || composite_minor >= 2)
1971 hasNamePixmap = True;
1974 if (!XDamageQueryExtension (dpy, &damage_event, &damage_error))
1976 fprintf (stderr, "No damage extension\n");
1979 if (!XFixesQueryExtension (dpy, &xfixes_event, &xfixes_error))
1981 fprintf (stderr, "No XFixes extension\n");
1985 opacityAtom = XInternAtom (dpy, OPACITY_PROP, False);
1986 winTypeAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE", False);
1987 winDesktopAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DESKTOP", False);
1988 winDockAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DOCK", False);
1989 winToolbarAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_TOOLBAR", False);
1990 winMenuAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_MENU", False);
1991 winUtilAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_UTILITY", False);
1992 winSplashAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_SPLASH", False);
1993 winDialogAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1994 winNormalAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_NORMAL", False);
1996 pa.subwindow_mode = IncludeInferiors;
1998 if (compMode == CompClientShadows)
2000 gaussianMap = make_gaussian_map(dpy, shadowRadius);
2001 presum_gaussian (gaussianMap);
2004 root_width = DisplayWidth (dpy, scr);
2005 root_height = DisplayHeight (dpy, scr);
2007 rootPicture = XRenderCreatePicture (dpy, root,
2008 XRenderFindVisualFormat (dpy,
2009 DefaultVisual (dpy, scr)),
2012 blackPicture = solid_picture (dpy, True, 1, 0, 0, 0);
2013 if (compMode == CompServerShadows)
2014 transBlackPicture = solid_picture (dpy, True, 0.3, 0, 0, 0);
2019 XCompositeRedirectSubwindows (dpy, root, CompositeRedirectAutomatic);
2022 XCompositeRedirectSubwindows (dpy, root, CompositeRedirectManual);
2023 XSelectInput (dpy, root,
2024 SubstructureNotifyMask|
2026 StructureNotifyMask|
2027 PropertyChangeMask);
2028 XQueryTree (dpy, root, &root_return, &parent_return, &children, &nchildren);
2029 for (i = 0; i < nchildren; i++)
2030 add_win (dpy, children[i], i ? children[i-1] : None);
2033 XUngrabServer (dpy);
2034 ufd.fd = ConnectionNumber (dpy);
2035 ufd.events = POLLIN;
2037 paint_all (dpy, None);
2046 if (poll (&ufd, 1, fade_timeout()) == 0)
2053 XNextEvent (dpy, &ev);
2054 if (ev.type & 0x7f != KeymapNotify)
2055 discard_ignore (dpy, ev.xany.serial);
2057 printf ("event %10.10s serial 0x%08x window 0x%08x\n",
2058 ev_name(&ev), ev_serial (&ev), ev_window (&ev));
2060 if (!autoRedirect) switch (ev.type) {
2062 add_win (dpy, ev.xcreatewindow.window, 0);
2064 case ConfigureNotify:
2065 configure_win (dpy, &ev.xconfigure);
2068 destroy_win (dpy, ev.xdestroywindow.window, True, True);
2071 map_win (dpy, ev.xmap.window, ev.xmap.serial, True);
2074 unmap_win (dpy, ev.xunmap.window, True);
2076 case ReparentNotify:
2077 if (ev.xreparent.parent == root)
2078 add_win (dpy, ev.xreparent.window, 0);
2080 destroy_win (dpy, ev.xreparent.window, False, True);
2082 case CirculateNotify:
2083 circulate_win (dpy, &ev.xcirculate);
2086 if (ev.xexpose.window == root)
2088 int more = ev.xexpose.count + 1;
2089 if (n_expose == size_expose)
2093 expose_rects = realloc (expose_rects,
2094 (size_expose + more) *
2095 sizeof (XRectangle));
2096 size_expose += more;
2100 expose_rects = malloc (more * sizeof (XRectangle));
2104 expose_rects[n_expose].x = ev.xexpose.x;
2105 expose_rects[n_expose].y = ev.xexpose.y;
2106 expose_rects[n_expose].width = ev.xexpose.width;
2107 expose_rects[n_expose].height = ev.xexpose.height;
2109 if (ev.xexpose.count == 0)
2111 expose_root (dpy, root, expose_rects, n_expose);
2116 case PropertyNotify:
2117 for (p = 0; backgroundProps[p]; p++)
2119 if (ev.xproperty.atom == XInternAtom (dpy, backgroundProps[p], False))
2123 XClearArea (dpy, root, 0, 0, 0, 0, True);
2124 XRenderFreePicture (dpy, rootTile);
2130 /* check if Trans property was changed */
2131 if (ev.xproperty.atom == opacityAtom)
2133 /* reset mode and redraw window */
2134 win * w = find_win(dpy, ev.xproperty.window);
2138 set_fade (dpy, w, w->opacity*1.0/OPAQUE, get_opacity_percent (dpy, w, 1.0),
2139 fade_out_step, 0, False, True, False);
2142 w->opacity = get_opacity_prop(dpy, w, OPAQUE);
2143 determine_mode(dpy, w);
2146 XRenderFreePicture (dpy, w->shadow);
2148 w->extents = win_extents (dpy, w);
2155 if (ev.type == damage_event + XDamageNotify)
2156 damage_win (dpy, (XDamageNotifyEvent *) &ev);
2159 } while (QLength (dpy));
2160 if (allDamage && !autoRedirect)
2163 paint_all (dpy, allDamage);
2167 clipChanged = False;