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
41 #include <X11/Xutil.h>
42 #include <X11/Xatom.h>
43 #include <X11/extensions/Xcomposite.h>
44 #include <X11/extensions/Xdamage.h>
45 #include <X11/extensions/Xrender.h>
47 #if COMPOSITE_MAJOR > 0 || COMPOSITE_MINOR >= 2
48 #define HAS_NAME_WINDOW_PIXMAP 1
51 #define CAN_DO_USABLE 0
53 typedef struct _ignore {
55 unsigned long sequence;
61 #if HAS_NAME_WINDOW_PIXMAP
66 Bool usable; /* mapped and all damaged at one point */
67 XRectangle damage_bounds; /* bounds of damage */
75 XserverRegion borderSize;
76 XserverRegion extents;
84 unsigned long damage_sequence; /* sequence when damage was created */
86 /* for drawing translucent windows */
87 XserverRegion borderClip;
88 struct _win *prev_trans;
91 typedef struct _conv {
96 typedef struct _fade {
102 void (*callback) (Display *dpy, win *w, Bool gone);
114 Picture blackPicture;
115 Picture transBlackPicture;
117 XserverRegion allDamage;
119 #if HAS_NAME_WINDOW_PIXMAP
122 int root_height, root_width;
123 ignore *ignore_head, **ignore_tail = &ignore_head;
124 int xfixes_event, xfixes_error;
125 int damage_event, damage_error;
126 int composite_event, composite_error;
127 int render_event, render_error;
129 int composite_opcode;
131 /* find these once and be done with it */
143 /* opacity property name; sometime soon I'll write up an EWMH spec for it */
144 #define OPACITY_PROP "_NET_WM_WINDOW_OPACITY"
146 #define TRANSLUCENT 0xe0000000
147 #define OPAQUE 0xffffffff
151 #define WINDOW_SOLID 0
152 #define WINDOW_TRANS 1
153 #define WINDOW_ARGB 2
155 #define TRANS_OPACITY 0.75
157 #define DEBUG_REPAINT 0
158 #define DEBUG_EVENTS 0
159 #define MONITOR_REPAINT 0
162 #define SHARP_SHADOW 0
164 typedef enum _compMode {
165 CompSimple, /* looks like a regular X server */
166 CompServerShadows, /* use window alpha for shadow; sharp, but precise */
167 CompClientShadows, /* use window extents for shadow, blurred */
171 determine_mode(Display *dpy, win *w);
174 get_opacity_percent(Display *dpy, win *w, double def);
177 win_extents (Display *dpy, win *w);
179 CompMode compMode = CompSimple;
181 int shadowRadius = 12;
182 int shadowOffsetX = -15;
183 int shadowOffsetY = -15;
184 double shadowOpacity = .75;
186 double fade_in_step = 0.028;
187 double fade_out_step = 0.03;
190 Bool fadeWindows = False;
191 Bool excludeDockShadows = False;
192 Bool fadeTrans = False;
194 Bool autoRedirect = False;
196 /* For shadow precomputation */
198 unsigned char *shadowCorner = NULL;
199 unsigned char *shadowTop = NULL;
202 get_time_in_milliseconds ()
206 gettimeofday (&tv, NULL);
207 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
215 for (f = fades; f; f = f->next)
224 dequeue_fade (Display *dpy, fade *f)
228 for (prev = &fades; *prev; prev = &(*prev)->next)
233 (*f->callback) (dpy, f->w, f->gone);
240 cleanup_fade (Display *dpy, win *w)
242 fade *f = find_fade (w);
244 dequeue_fade (dpy, f);
248 enqueue_fade (Display *dpy, fade *f)
251 fade_time = get_time_in_milliseconds () + fade_delta;
257 set_fade (Display *dpy, win *w, double start, double finish, double step,
258 void (*callback) (Display *dpy, win *w, Bool gone),
259 Bool gone, Bool exec_callback, Bool override)
266 f = malloc (sizeof (fade));
270 enqueue_fade (dpy, f);
278 (*f->callback)(dpy, f->w, f->gone);
288 else if (f->cur > finish)
290 f->callback = callback;
292 w->opacity = f->cur * OPAQUE;
294 printf ("set_fade start %g step %g\n", f->cur, f->step);
296 determine_mode (dpy, w);
299 XRenderFreePicture (dpy, w->shadow);
301 w->extents = win_extents (dpy, w);
312 now = get_time_in_milliseconds();
313 delta = fade_time - now;
316 /* printf ("timeout %d\n", delta); */
321 run_fades (Display *dpy)
323 int now = get_time_in_milliseconds();
329 printf ("run fades\n");
331 if (fade_time - now > 0)
333 steps = 1 + (now - fade_time) / fade_delta;
334 for (next = fades; f = next; )
338 f->cur += f->step * steps;
344 printf ("opacity now %g\n", f->cur);
346 w->opacity = f->cur * OPAQUE;
347 need_dequeue = False;
350 if (f->cur >= f->finish)
352 w->opacity = f->finish*OPAQUE;
358 if (f->cur <= f->finish)
360 w->opacity = f->finish*OPAQUE;
364 determine_mode (dpy, w);
367 XRenderFreePicture (dpy, w->shadow);
369 w->extents = win_extents(dpy, w);
371 /* Must do this last as it might destroy f->w in callbacks */
373 dequeue_fade (dpy, f);
375 fade_time = now + fade_delta;
379 gaussian (double r, double x, double y)
381 return ((1 / (sqrt (2 * M_PI * r))) *
382 exp ((- (x * x + y * y)) / (2 * r * r)));
387 make_gaussian_map (Display *dpy, double r)
390 int size = ((int) ceil ((r * 3)) + 1) & ~1;
391 int center = size / 2;
396 c = malloc (sizeof (conv) + size * size * sizeof (double));
398 c->data = (double *) (c + 1);
400 for (y = 0; y < size; y++)
401 for (x = 0; x < size; x++)
403 g = gaussian (r, (double) (x - center), (double) (y - center));
405 c->data[y * size + x] = g;
407 /* printf ("gaussian total %f\n", t); */
408 for (y = 0; y < size; y++)
409 for (x = 0; x < size; x++)
411 c->data[y*size + x] /= t;
417 * A picture will help
419 * -center 0 width width+center
420 * -center +-----+-------------------+-----+
423 * 0 +-----+-------------------+-----+
427 * height +-----+-------------------+-----+
430 * center +-----+-------------------+-----+
434 sum_gaussian (conv *map, double opacity, int x, int y, int width, int height)
438 double *g_line = map->data;
439 int g_size = map->size;
440 int center = g_size / 2;
441 int fx_start, fx_end;
442 int fy_start, fy_end;
446 * Compute set of filter values which are "in range",
447 * that's the set with:
448 * 0 <= x + (fx-center) && x + (fx-center) < width &&
449 * 0 <= y + (fy-center) && y + (fy-center) < height
451 * 0 <= x + (fx - center) x + fx - center < width
452 * center - x <= fx fx < width + center - x
455 fx_start = center - x;
458 fx_end = width + center - x;
462 fy_start = center - y;
465 fy_end = height + center - y;
469 g_line = g_line + fy_start * g_size + fx_start;
472 for (fy = fy_start; fy < fy_end; fy++)
477 for (fx = fx_start; fx < fx_end; fx++)
483 return ((unsigned char) (v * opacity * 255.0));
486 /* precompute shadow corners and sides to save time for large windows */
488 presum_gaussian (conv *map)
490 int center = map->size/2;
496 free ((void *)shadowCorner);
498 free ((void *)shadowTop);
500 shadowCorner = (unsigned char *)(malloc ((Gsize + 1) * (Gsize + 1) * 26));
501 shadowTop = (unsigned char *)(malloc ((Gsize + 1) * 26));
503 for (x = 0; x <= Gsize; x++)
505 shadowTop[25 * (Gsize + 1) + x] = sum_gaussian (map, 1, x - center, center, Gsize * 2, Gsize * 2);
506 for(opacity = 0; opacity < 25; opacity++)
507 shadowTop[opacity * (Gsize + 1) + x] = shadowTop[25 * (Gsize + 1) + x] * opacity / 25;
508 for(y = 0; y <= x; y++)
510 shadowCorner[25 * (Gsize + 1) * (Gsize + 1) + y * (Gsize + 1) + x]
511 = sum_gaussian (map, 1, x - center, y - center, Gsize * 2, Gsize * 2);
512 shadowCorner[25 * (Gsize + 1) * (Gsize + 1) + x * (Gsize + 1) + y]
513 = shadowCorner[25 * (Gsize + 1) * (Gsize + 1) + y * (Gsize + 1) + x];
514 for(opacity = 0; opacity < 25; opacity++)
515 shadowCorner[opacity * (Gsize + 1) * (Gsize + 1) + y * (Gsize + 1) + x]
516 = shadowCorner[opacity * (Gsize + 1) * (Gsize + 1) + x * (Gsize + 1) + y]
517 = shadowCorner[25 * (Gsize + 1) * (Gsize + 1) + y * (Gsize + 1) + x] * opacity / 25;
523 make_shadow (Display *dpy, double opacity, int width, int height)
527 int gsize = gaussianMap->size;
529 int swidth = width + gsize;
530 int sheight = height + gsize;
531 int center = gsize / 2;
535 int opacity_int = (int)(opacity * 25);
536 data = malloc (swidth * sheight * sizeof (unsigned char));
539 ximage = XCreateImage (dpy,
540 DefaultVisual(dpy, DefaultScreen(dpy)),
545 swidth, sheight, 8, swidth * sizeof (unsigned char));
552 * Build the gaussian in sections
556 * center (fill the complete data array)
559 d = shadowTop[opacity_int * (Gsize + 1) + Gsize];
561 d = sum_gaussian (gaussianMap, opacity, center, center, width, height);
562 memset(data, d, sheight * swidth);
568 if (ylimit > sheight / 2)
569 ylimit = (sheight + 1) / 2;
571 if (xlimit > swidth / 2)
572 xlimit = (swidth + 1) / 2;
574 for (y = 0; y < ylimit; y++)
575 for (x = 0; x < xlimit; x++)
577 if (xlimit == Gsize && ylimit == Gsize)
578 d = shadowCorner[opacity_int * (Gsize + 1) * (Gsize + 1) + y * (Gsize + 1) + x];
580 d = sum_gaussian (gaussianMap, opacity, x - center, y - center, width, height);
581 data[y * swidth + x] = d;
582 data[(sheight - y - 1) * swidth + x] = d;
583 data[(sheight - y - 1) * swidth + (swidth - x - 1)] = d;
584 data[y * swidth + (swidth - x - 1)] = d;
590 x_diff = swidth - (gsize * 2);
591 if (x_diff > 0 && ylimit > 0)
593 for (y = 0; y < ylimit; y++)
596 d = shadowTop[opacity_int * (Gsize + 1) + y];
598 d = sum_gaussian (gaussianMap, opacity, center, y - center, width, height);
599 memset (&data[y * swidth + gsize], d, x_diff);
600 memset (&data[(sheight - y - 1) * swidth + gsize], d, x_diff);
608 for (x = 0; x < xlimit; x++)
611 d = shadowTop[opacity_int * (Gsize + 1) + x];
613 d = sum_gaussian (gaussianMap, opacity, x - center, center, width, height);
614 for (y = gsize; y < sheight - gsize; y++)
616 data[y * swidth + x] = d;
617 data[y * swidth + (swidth - x - 1)] = d;
625 shadow_picture (Display *dpy, double opacity, Picture alpha_pict, int width, int height, int *wp, int *hp)
629 Picture shadowPicture;
632 shadowImage = make_shadow (dpy, opacity, width, height);
635 shadowPixmap = XCreatePixmap (dpy, root,
641 XDestroyImage (shadowImage);
645 shadowPicture = XRenderCreatePicture (dpy, shadowPixmap,
646 XRenderFindStandardFormat (dpy, PictStandardA8),
650 XDestroyImage (shadowImage);
651 XFreePixmap (dpy, shadowPixmap);
655 gc = XCreateGC (dpy, shadowPixmap, 0, 0);
658 XDestroyImage (shadowImage);
659 XFreePixmap (dpy, shadowPixmap);
660 XRenderFreePicture (dpy, shadowPicture);
664 XPutImage (dpy, shadowPixmap, gc, shadowImage, 0, 0, 0, 0,
666 shadowImage->height);
667 *wp = shadowImage->width;
668 *hp = shadowImage->height;
670 XDestroyImage (shadowImage);
671 XFreePixmap (dpy, shadowPixmap);
672 return shadowPicture;
676 solid_picture (Display *dpy, Bool argb, double a, double r, double g, double b)
680 XRenderPictureAttributes pa;
683 pixmap = XCreatePixmap (dpy, root, 1, 1, argb ? 32 : 8);
688 picture = XRenderCreatePicture (dpy, pixmap,
689 XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8),
694 XFreePixmap (dpy, pixmap);
698 c.alpha = a * 0xffff;
700 c.green = g * 0xffff;
702 XRenderFillRectangle (dpy, PictOpSrc, picture, &c, 0, 0, 1, 1);
703 XFreePixmap (dpy, pixmap);
708 discard_ignore (Display *dpy, unsigned long sequence)
712 if ((long) (sequence - ignore_head->sequence) > 0)
714 ignore *next = ignore_head->next;
718 ignore_tail = &ignore_head;
726 set_ignore (Display *dpy, unsigned long sequence)
728 ignore *i = malloc (sizeof (ignore));
731 i->sequence = sequence;
734 ignore_tail = &i->next;
738 should_ignore (Display *dpy, unsigned long sequence)
740 discard_ignore (dpy, sequence);
741 return ignore_head && ignore_head->sequence == sequence;
745 find_win (Display *dpy, Window id)
749 for (w = list; w; w = w->next)
755 static char *backgroundProps[] = {
762 root_tile (Display *dpy)
768 unsigned long nitems;
769 unsigned long bytes_after;
772 XRenderPictureAttributes pa;
776 for (p = 0; backgroundProps[p]; p++)
778 if (XGetWindowProperty (dpy, root, XInternAtom (dpy, backgroundProps[p], False),
779 0, 4, False, AnyPropertyType,
780 &actual_type, &actual_format, &nitems, &bytes_after, &prop) == Success &&
781 actual_type == XInternAtom (dpy, "PIXMAP", False) && actual_format == 32 && nitems == 1)
783 memcpy (&pixmap, prop, 4);
791 pixmap = XCreatePixmap (dpy, root, 1, 1, DefaultDepth (dpy, scr));
795 picture = XRenderCreatePicture (dpy, pixmap,
796 XRenderFindVisualFormat (dpy,
797 DefaultVisual (dpy, scr)),
803 c.red = c.green = c.blue = 0x8080;
805 XRenderFillRectangle (dpy, PictOpSrc, picture, &c,
812 paint_root (Display *dpy)
815 rootTile = root_tile (dpy);
817 XRenderComposite (dpy, PictOpSrc,
818 rootTile, None, rootBuffer,
819 0, 0, 0, 0, 0, 0, root_width, root_height);
823 win_extents (Display *dpy, win *w)
829 r.width = w->a.width + w->a.border_width * 2;
830 r.height = w->a.height + w->a.border_width * 2;
831 if (compMode != CompSimple && !(w->windowType == winDockAtom && excludeDockShadows))
833 if (compMode == CompServerShadows || w->mode != WINDOW_ARGB)
837 if (compMode == CompServerShadows)
841 w->shadow_width = w->a.width;
842 w->shadow_height = w->a.height;
846 w->shadow_dx = shadowOffsetX;
847 w->shadow_dy = shadowOffsetY;
850 double opacity = shadowOpacity;
851 if (w->mode == WINDOW_TRANS)
852 opacity = opacity * ((double)w->opacity)/((double)OPAQUE);
853 w->shadow = shadow_picture (dpy, opacity, w->alphaPict,
854 w->a.width + w->a.border_width * 2,
855 w->a.height + w->a.border_width * 2,
856 &w->shadow_width, &w->shadow_height);
859 sr.x = w->a.x + w->shadow_dx;
860 sr.y = w->a.y + w->shadow_dy;
861 sr.width = w->shadow_width;
862 sr.height = w->shadow_height;
865 r.width = (r.x + r.width) - sr.x;
870 r.height = (r.y + r.height) - sr.y;
873 if (sr.x + sr.width > r.x + r.width)
874 r.width = sr.x + sr.width - r.x;
875 if (sr.y + sr.height > r.y + r.height)
876 r.height = sr.y + sr.height - r.y;
879 return XFixesCreateRegion (dpy, &r, 1);
883 border_size (Display *dpy, win *w)
885 XserverRegion border;
887 * if window doesn't exist anymore, this will generate an error
888 * as well as not generate a region. Perhaps a better XFixes
889 * architecture would be to have a request that copies instead
890 * of creates, that way you'd just end up with an empty region
891 * instead of an invalid XID.
893 set_ignore (dpy, NextRequest (dpy));
894 border = XFixesCreateRegionFromWindow (dpy, w->id, WindowRegionBounding);
896 set_ignore (dpy, NextRequest (dpy));
897 XFixesTranslateRegion (dpy, border,
898 w->a.x + w->a.border_width,
899 w->a.y + w->a.border_width);
904 paint_all (Display *dpy, XserverRegion region)
914 r.width = root_width;
915 r.height = root_height;
916 region = XFixesCreateRegion (dpy, &r, 1);
919 rootBuffer = rootPicture;
923 Pixmap rootPixmap = XCreatePixmap (dpy, root, root_width, root_height,
924 DefaultDepth (dpy, scr));
925 rootBuffer = XRenderCreatePicture (dpy, rootPixmap,
926 XRenderFindVisualFormat (dpy,
927 DefaultVisual (dpy, scr)),
929 XFreePixmap (dpy, rootPixmap);
932 XFixesSetPictureClipRegion (dpy, rootPicture, 0, 0, region);
934 XRenderComposite (dpy, PictOpSrc, blackPicture, None, rootPicture,
935 0, 0, 0, 0, 0, 0, root_width, root_height);
940 for (w = list; w; w = w->next)
946 /* never painted, ignore it */
949 /* if invisible, ignore it */
950 if (w->a.x + w->a.width < 1 || w->a.y + w->a.height < 1
951 || w->a.x >= root_width || w->a.y >= root_height)
955 XRenderPictureAttributes pa;
956 XRenderPictFormat *format;
957 Drawable draw = w->id;
959 #if HAS_NAME_WINDOW_PIXMAP
960 if (hasNamePixmap && !w->pixmap)
961 w->pixmap = XCompositeNameWindowPixmap (dpy, w->id);
965 format = XRenderFindVisualFormat (dpy, w->a.visual);
966 pa.subwindow_mode = IncludeInferiors;
967 w->picture = XRenderCreatePicture (dpy, draw,
973 printf (" 0x%x", w->id);
979 set_ignore (dpy, NextRequest (dpy));
980 XFixesDestroyRegion (dpy, w->borderSize);
981 w->borderSize = None;
985 XFixesDestroyRegion (dpy, w->extents);
990 XFixesDestroyRegion (dpy, w->borderClip);
991 w->borderClip = None;
995 w->borderSize = border_size (dpy, w);
997 w->extents = win_extents (dpy, w);
998 if (w->mode == WINDOW_SOLID)
1001 #if HAS_NAME_WINDOW_PIXMAP
1004 wid = w->a.width + w->a.border_width * 2;
1005 hei = w->a.height + w->a.border_width * 2;
1007 x = w->a.x + w->a.border_width;
1008 y = w->a.y + w->a.border_width;
1012 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
1013 set_ignore (dpy, NextRequest (dpy));
1014 XFixesSubtractRegion (dpy, region, region, w->borderSize);
1015 set_ignore (dpy, NextRequest (dpy));
1016 XRenderComposite (dpy, PictOpSrc, w->picture, None, rootBuffer,
1022 w->borderClip = XFixesCreateRegion (dpy, 0, 0);
1023 XFixesCopyRegion (dpy, w->borderClip, region);
1032 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
1034 for (w = t; w; w = w->prev_trans)
1036 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, w->borderClip);
1040 case CompServerShadows:
1041 /* dont' bother drawing shadows on desktop windows */
1042 if (w->windowType == winDesktopAtom)
1044 set_ignore (dpy, NextRequest (dpy));
1045 if (w->opacity != OPAQUE && !w->shadowPict)
1046 w->shadowPict = solid_picture (dpy, True,
1047 (double) w->opacity / OPAQUE * 0.3,
1049 XRenderComposite (dpy, PictOpOver,
1050 w->shadowPict ? w->shadowPict : transBlackPicture,
1051 w->picture, rootBuffer,
1053 w->a.x + w->shadow_dx,
1054 w->a.y + w->shadow_dy,
1055 w->shadow_width, w->shadow_height);
1057 case CompClientShadows:
1058 /* don't bother drawing shadows on desktop windows */
1059 if (w->shadow && w->windowType != winDesktopAtom)
1061 XRenderComposite (dpy, PictOpOver, blackPicture, w->shadow, rootBuffer,
1063 w->a.x + w->shadow_dx,
1064 w->a.y + w->shadow_dy,
1065 w->shadow_width, w->shadow_height);
1069 if (w->opacity != OPAQUE && !w->alphaPict)
1070 w->alphaPict = solid_picture (dpy, False,
1071 (double) w->opacity / OPAQUE, 0, 0, 0);
1072 if (w->mode == WINDOW_TRANS)
1075 #if HAS_NAME_WINDOW_PIXMAP
1078 wid = w->a.width + w->a.border_width * 2;
1079 hei = w->a.height + w->a.border_width * 2;
1081 x = w->a.x + w->a.border_width;
1082 y = w->a.y + w->a.border_width;
1086 set_ignore (dpy, NextRequest (dpy));
1087 XRenderComposite (dpy, PictOpOver, w->picture, w->alphaPict, rootBuffer,
1091 else if (w->mode == WINDOW_ARGB)
1094 #if HAS_NAME_WINDOW_PIXMAP
1097 wid = w->a.width + w->a.border_width * 2;
1098 hei = w->a.height + w->a.border_width * 2;
1100 x = w->a.x + w->a.border_width;
1101 y = w->a.y + w->a.border_width;
1105 set_ignore (dpy, NextRequest (dpy));
1106 XRenderComposite (dpy, PictOpOver, w->picture, w->alphaPict, rootBuffer,
1110 XFixesDestroyRegion (dpy, w->borderClip);
1111 w->borderClip = None;
1113 XFixesDestroyRegion (dpy, region);
1114 if (rootBuffer != rootPicture)
1116 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, None);
1117 XRenderComposite (dpy, PictOpSrc, rootBuffer, None, rootPicture,
1118 0, 0, 0, 0, 0, 0, root_width, root_height);
1123 add_damage (Display *dpy, XserverRegion damage)
1127 XFixesUnionRegion (dpy, allDamage, allDamage, damage);
1128 XFixesDestroyRegion (dpy, damage);
1135 repair_win (Display *dpy, win *w)
1137 XserverRegion parts;
1141 parts = win_extents (dpy, w);
1142 set_ignore (dpy, NextRequest (dpy));
1143 XDamageSubtract (dpy, w->damage, None, None);
1148 parts = XFixesCreateRegion (dpy, 0, 0);
1149 set_ignore (dpy, NextRequest (dpy));
1150 XDamageSubtract (dpy, w->damage, None, parts);
1151 XFixesTranslateRegion (dpy, parts,
1152 w->a.x + w->a.border_width,
1153 w->a.y + w->a.border_width);
1154 if (compMode == CompServerShadows)
1156 o = XFixesCreateRegion (dpy, 0, 0);
1157 XFixesCopyRegion (dpy, o, parts);
1158 XFixesTranslateRegion (dpy, o, w->shadow_dx, w->shadow_dy);
1159 XFixesUnionRegion (dpy, parts, parts, o);
1160 XFixesDestroyRegion (dpy, o);
1163 add_damage (dpy, parts);
1168 get_opacity_prop (Display *dpy, win *w, unsigned int def);
1171 map_win (Display *dpy, Window id, unsigned long sequence, Bool fade)
1173 win *w = find_win (dpy, id);
1178 w->a.map_state = IsViewable;
1180 /* This needs to be here or else we lose transparency messages */
1181 XSelectInput (dpy, id, PropertyChangeMask);
1183 /* This needs to be here since we don't get PropertyNotify when unmapped */
1184 w->opacity = get_opacity_prop (dpy, w, OPAQUE);
1185 determine_mode (dpy, w);
1188 w->damage_bounds.x = w->damage_bounds.y = 0;
1189 w->damage_bounds.width = w->damage_bounds.height = 0;
1193 if (fade && fadeWindows)
1194 set_fade (dpy, w, 0, get_opacity_percent (dpy, w, 1.0), fade_in_step, 0, False, True, True);
1198 finish_unmap_win (Display *dpy, win *w)
1204 if (w->extents != None)
1206 add_damage (dpy, w->extents); /* destroys region */
1210 #if HAS_NAME_WINDOW_PIXMAP
1213 XFreePixmap (dpy, w->pixmap);
1220 set_ignore (dpy, NextRequest (dpy));
1221 XRenderFreePicture (dpy, w->picture);
1225 /* don't care about properties anymore */
1226 set_ignore (dpy, NextRequest (dpy));
1227 XSelectInput(dpy, w->id, 0);
1231 set_ignore (dpy, NextRequest (dpy));
1232 XFixesDestroyRegion (dpy, w->borderSize);
1233 w->borderSize = None;
1237 XRenderFreePicture (dpy, w->shadow);
1242 XFixesDestroyRegion (dpy, w->borderClip);
1243 w->borderClip = None;
1249 #if HAS_NAME_WINDOW_PIXMAP
1251 unmap_callback (Display *dpy, win *w, Bool gone)
1253 finish_unmap_win (dpy, w);
1258 unmap_win (Display *dpy, Window id, Bool fade)
1260 win *w = find_win (dpy, id);
1263 w->a.map_state = IsUnmapped;
1264 #if HAS_NAME_WINDOW_PIXMAP
1265 if (w->pixmap && fade && fadeWindows)
1266 set_fade (dpy, w, w->opacity*1.0/OPAQUE, 0.0, fade_out_step, unmap_callback, False, False, True);
1269 finish_unmap_win (dpy, w);
1272 /* Get the opacity prop from window
1277 get_opacity_prop(Display *dpy, win *w, unsigned int def)
1281 unsigned long n, left;
1283 unsigned char *data;
1284 int result = XGetWindowProperty(dpy, w->id, opacityAtom, 0L, 1L, False,
1285 XA_CARDINAL, &actual, &format,
1287 if (result == Success && data != NULL)
1290 memcpy (&i, data, sizeof (unsigned int));
1291 XFree( (void *) data);
1297 /* Get the opacity property from the window in a percent format
1299 otherwise: the value
1302 get_opacity_percent(Display *dpy, win *w, double def)
1304 unsigned int opacity = get_opacity_prop (dpy, w, (unsigned int)(OPAQUE*def));
1306 return opacity*1.0/OPAQUE;
1309 /* determine mode for window all in one place.
1310 Future might check for menu flag and other cool things
1314 get_wintype_prop(Display * dpy, Window w)
1318 unsigned long n, left;
1320 unsigned char *data;
1321 int result = XGetWindowProperty (dpy, w, winTypeAtom, 0L, 1L, False,
1322 XA_ATOM, &actual, &format,
1325 if (result == Success && data != None)
1328 memcpy (&a, data, sizeof (Atom));
1329 XFree ( (void *) data);
1332 return winNormalAtom;
1336 determine_mode(Display *dpy, win *w)
1339 XRenderPictFormat *format;
1341 /* if trans prop == -1 fall back on previous tests*/
1345 XRenderFreePicture (dpy, w->alphaPict);
1346 w->alphaPict = None;
1350 XRenderFreePicture (dpy, w->shadowPict);
1351 w->shadowPict = None;
1354 if (w->a.class == InputOnly)
1360 format = XRenderFindVisualFormat (dpy, w->a.visual);
1363 if (format && format->type == PictTypeDirect && format->direct.alphaMask)
1367 else if (w->opacity != OPAQUE)
1369 mode = WINDOW_TRANS;
1373 mode = WINDOW_SOLID;
1378 XserverRegion damage;
1379 damage = XFixesCreateRegion (dpy, 0, 0);
1380 XFixesCopyRegion (dpy, damage, w->extents);
1381 add_damage (dpy, damage);
1386 determine_wintype (Display *dpy, Window w)
1388 Window root_return, parent_return;
1389 Window *children = NULL;
1390 unsigned int nchildren, i;
1393 type = get_wintype_prop (dpy, w);
1394 if (type != winNormalAtom)
1397 if (!XQueryTree (dpy, w, &root_return, &parent_return, &children,
1400 /* XQueryTree failed. */
1402 XFree ((void *)children);
1403 return winNormalAtom;
1406 for (i = 0;i < nchildren;i++)
1408 type = determine_wintype (dpy, children[i]);
1409 if (type != winNormalAtom)
1414 XFree ((void *)children);
1416 return winNormalAtom;
1420 add_win (Display *dpy, Window id, Window prev)
1422 win *new = malloc (sizeof (win));
1429 for (p = &list; *p; p = &(*p)->next)
1430 if ((*p)->id == prev)
1436 set_ignore (dpy, NextRequest (dpy));
1437 if (!XGetWindowAttributes (dpy, id, &new->a))
1444 new->usable = False;
1446 #if HAS_NAME_WINDOW_PIXMAP
1449 new->picture = None;
1450 if (new->a.class == InputOnly)
1452 new->damage_sequence = 0;
1457 new->damage_sequence = NextRequest (dpy);
1458 new->damage = XDamageCreate (dpy, id, XDamageReportNonEmpty);
1460 new->alphaPict = None;
1461 new->shadowPict = None;
1462 new->borderSize = None;
1463 new->extents = None;
1467 new->shadow_width = 0;
1468 new->shadow_height = 0;
1469 new->opacity = OPAQUE;
1471 new->borderClip = None;
1472 new->prev_trans = 0;
1474 new->windowType = determine_wintype (dpy, new->id);
1478 if (new->a.map_state == IsViewable)
1479 map_win (dpy, id, new->damage_sequence - 1, True);
1483 restack_win (Display *dpy, win *w, Window new_above)
1488 old_above = w->next->id;
1491 if (old_above != new_above)
1496 for (prev = &list; *prev; prev = &(*prev)->next)
1502 for (prev = &list; *prev; prev = &(*prev)->next)
1504 if ((*prev)->id == new_above)
1513 configure_win (Display *dpy, XConfigureEvent *ce)
1515 win *w = find_win (dpy, ce->window);
1516 XserverRegion damage = None;
1520 if (ce->window == root)
1524 XRenderFreePicture (dpy, rootBuffer);
1527 root_width = ce->width;
1528 root_height = ce->height;
1536 damage = XFixesCreateRegion (dpy, 0, 0);
1537 if (w->extents != None)
1538 XFixesCopyRegion (dpy, damage, w->extents);
1542 if (w->a.width != ce->width || w->a.height != ce->height)
1544 #if HAS_NAME_WINDOW_PIXMAP
1547 XFreePixmap (dpy, w->pixmap);
1551 XRenderFreePicture (dpy, w->picture);
1558 XRenderFreePicture (dpy, w->shadow);
1562 w->a.width = ce->width;
1563 w->a.height = ce->height;
1564 w->a.border_width = ce->border_width;
1565 w->a.override_redirect = ce->override_redirect;
1566 restack_win (dpy, w, ce->above);
1569 XserverRegion extents = win_extents (dpy, w);
1570 XFixesUnionRegion (dpy, damage, damage, extents);
1571 XFixesDestroyRegion (dpy, extents);
1572 add_damage (dpy, damage);
1578 circulate_win (Display *dpy, XCirculateEvent *ce)
1580 win *w = find_win (dpy, ce->window);
1586 if (ce->place == PlaceOnTop)
1587 new_above = list->id;
1590 restack_win (dpy, w, new_above);
1595 finish_destroy_win (Display *dpy, Window id, Bool gone)
1599 for (prev = &list; (w = *prev); prev = &w->next)
1603 finish_unmap_win (dpy, w);
1607 set_ignore (dpy, NextRequest (dpy));
1608 XRenderFreePicture (dpy, w->picture);
1613 XRenderFreePicture (dpy, w->alphaPict);
1614 w->alphaPict = None;
1618 XRenderFreePicture (dpy, w->shadowPict);
1619 w->shadowPict = None;
1621 if (w->damage != None)
1623 set_ignore (dpy, NextRequest (dpy));
1624 XDamageDestroy (dpy, w->damage);
1627 cleanup_fade (dpy, w);
1633 #if HAS_NAME_WINDOW_PIXMAP
1635 destroy_callback (Display *dpy, win *w, Bool gone)
1637 finish_destroy_win (dpy, w->id, gone);
1642 destroy_win (Display *dpy, Window id, Bool gone, Bool fade)
1644 win *w = find_win (dpy, id);
1645 #if HAS_NAME_WINDOW_PIXMAP
1646 if (w && w->pixmap && fade && fadeWindows)
1647 set_fade (dpy, w, w->opacity*1.0/OPAQUE, 0.0, fade_out_step, destroy_callback, gone, False, True);
1651 finish_destroy_win (dpy, id, gone);
1659 printf ("\t%08lx: %d x %d + %d + %d (%d)\n", w->id,
1660 w->a.width, w->a.height, w->a.x, w->a.y, w->a.border_width);
1669 printf ("windows:\n");
1670 for (w = list; w; w = w->next)
1676 damage_win (Display *dpy, XDamageNotifyEvent *de)
1678 win *w = find_win (dpy, de->drawable);
1685 if (w->damage_bounds.width == 0 || w->damage_bounds.height == 0)
1687 w->damage_bounds = de->area;
1691 if (de->area.x < w->damage_bounds.x)
1693 w->damage_bounds.width += (w->damage_bounds.x - de->area.x);
1694 w->damage_bounds.x = de->area.x;
1696 if (de->area.y < w->damage_bounds.y)
1698 w->damage_bounds.height += (w->damage_bounds.y - de->area.y);
1699 w->damage_bounds.y = de->area.y;
1701 if (de->area.x + de->area.width > w->damage_bounds.x + w->damage_bounds.width)
1702 w->damage_bounds.width = de->area.x + de->area.width - w->damage_bounds.x;
1703 if (de->area.y + de->area.height > w->damage_bounds.y + w->damage_bounds.height)
1704 w->damage_bounds.height = de->area.y + de->area.height - w->damage_bounds.y;
1707 printf ("unusable damage %d, %d: %d x %d bounds %d, %d: %d x %d\n",
1714 w->damage_bounds.width,
1715 w->damage_bounds.height);
1717 if (w->damage_bounds.x <= 0 &&
1718 w->damage_bounds.y <= 0 &&
1719 w->a.width <= w->damage_bounds.x + w->damage_bounds.width &&
1720 w->a.height <= w->damage_bounds.y + w->damage_bounds.height)
1724 set_fade (dpy, w, 0, get_opacity_percent (dpy, w, 1.0), fade_in_step, 0, False, True, True);
1730 repair_win (dpy, w);
1734 error (Display *dpy, XErrorEvent *ev)
1739 if (should_ignore (dpy, ev->serial))
1742 if (ev->request_code == composite_opcode &&
1743 ev->minor_code == X_CompositeRedirectSubwindows)
1745 fprintf (stderr, "Another composite manager is already running\n");
1749 o = ev->error_code - xfixes_error;
1751 case BadRegion: name = "BadRegion"; break;
1754 o = ev->error_code - damage_error;
1756 case BadDamage: name = "BadDamage"; break;
1759 o = ev->error_code - render_error;
1761 case BadPictFormat: name ="BadPictFormat"; break;
1762 case BadPicture: name ="BadPicture"; break;
1763 case BadPictOp: name ="BadPictOp"; break;
1764 case BadGlyphSet: name ="BadGlyphSet"; break;
1765 case BadGlyph: name ="BadGlyph"; break;
1769 printf ("error %d request %d minor %d serial %lu\n",
1770 ev->error_code, ev->request_code, ev->minor_code, ev->serial);
1772 /* abort (); this is just annoying to most people */
1777 expose_root (Display *dpy, Window root, XRectangle *rects, int nrects)
1779 XserverRegion region = XFixesCreateRegion (dpy, rects, nrects);
1781 add_damage (dpy, region);
1786 ev_serial (XEvent *ev)
1788 if (ev->type & 0x7f != KeymapNotify)
1789 return ev->xany.serial;
1790 return NextRequest (ev->xany.display);
1794 ev_name (XEvent *ev)
1796 static char buf[128];
1797 switch (ev->type & 0x7f) {
1804 case ReparentNotify:
1806 case CirculateNotify:
1809 if (ev->type == damage_event + XDamageNotify)
1811 sprintf (buf, "Event %d", ev->type);
1817 ev_window (XEvent *ev)
1821 return ev->xexpose.window;
1823 return ev->xmap.window;
1825 return ev->xunmap.window;
1826 case ReparentNotify:
1827 return ev->xreparent.window;
1828 case CirculateNotify:
1829 return ev->xcirculate.window;
1831 if (ev->type == damage_event + XDamageNotify)
1832 return ((XDamageNotifyEvent *) ev)->drawable;
1839 usage (char *program)
1841 fprintf (stderr, "%s v1.1.3\n", program);
1842 fprintf (stderr, "usage: %s [options]\n", program);
1843 fprintf (stderr, "Options\n");
1844 fprintf (stderr, " -d display\n Specifies which display should be managed.\n");
1845 fprintf (stderr, " -r radius\n Specifies the blur radius for client-side shadows. (default 12)\n");
1846 fprintf (stderr, " -o opacity\n Specifies the translucency for client-side shadows. (default .75)\n");
1847 fprintf (stderr, " -l left-offset\n Specifies the left offset for client-side shadows. (default -15)\n");
1848 fprintf (stderr, " -t top-offset\n Specifies the top offset for clinet-side shadows. (default -15)\n");
1849 fprintf (stderr, " -I fade-in-step\n Specifies the opacity change between steps while fading in. (default 0.028)\n");
1850 fprintf (stderr, " -O fade-out-step\n Specifies the opacity change between steps while fading out. (default 0.03)\n");
1851 fprintf (stderr, " -D fade-delta-time\n Specifies the time between steps in a fade in milliseconds. (default 10)\n");
1852 fprintf (stderr, " -a\n Use automatic server-side compositing. Faster, but no special effects.\n");
1853 fprintf (stderr, " -c\n Draw client-side shadows with fuzzy edges.\n");
1854 fprintf (stderr, " -C\n Avoid drawing shadows on dock/panel windows.\n");
1855 fprintf (stderr, " -f\n Fade windows in/out when opening/closing.\n");
1856 fprintf (stderr, " -F\n Fade windows during opacity changes.\n");
1857 fprintf (stderr, " -n\n Normal client-side compositing with transparency support\n");
1858 fprintf (stderr, " -s\n Draw server-side shadows with sharp edges.\n");
1859 fprintf (stderr, " -S\n Enable synchronous operation (for debugging).\n");
1869 w = XCreateSimpleWindow (dpy, RootWindow (dpy, 0), 0, 0, 1, 1, 0, None,
1872 Xutf8SetWMProperties (dpy, w, "xcompmgr", "xcompmgr", NULL, 0, NULL, NULL,
1875 /* FIXME: Don't hard code the screen number */
1876 a = XInternAtom (dpy, "_NET_WM_CM_S0", False);
1878 XSetSelectionOwner (dpy, a, w, 0);
1882 main (int argc, char **argv)
1885 Window root_return, parent_return;
1887 unsigned int nchildren;
1889 XRenderPictureAttributes pa;
1890 XRectangle *expose_rects = 0;
1891 int size_expose = 0;
1895 int composite_major, composite_minor;
1899 while ((o = getopt (argc, argv, "D:I:O:d:r:o:l:t:scnfFCaS")) != -1)
1906 fade_delta = atoi (optarg);
1911 fade_in_step = atof (optarg);
1912 if (fade_in_step <= 0)
1913 fade_in_step = 0.01;
1916 fade_out_step = atof (optarg);
1917 if (fade_out_step <= 0)
1918 fade_out_step = 0.01;
1921 compMode = CompServerShadows;
1924 compMode = CompClientShadows;
1927 excludeDockShadows = True;
1930 compMode = CompSimple;
1939 autoRedirect = True;
1945 shadowRadius = atoi (optarg);
1948 shadowOpacity = atof (optarg);
1951 shadowOffsetX = atoi (optarg);
1954 shadowOffsetY = atoi (optarg);
1962 dpy = XOpenDisplay (display);
1965 fprintf (stderr, "Can't open display\n");
1968 XSetErrorHandler (error);
1970 XSynchronize (dpy, 1);
1971 scr = DefaultScreen (dpy);
1972 root = RootWindow (dpy, scr);
1974 if (!XRenderQueryExtension (dpy, &render_event, &render_error))
1976 fprintf (stderr, "No render extension\n");
1979 if (!XQueryExtension (dpy, COMPOSITE_NAME, &composite_opcode,
1980 &composite_event, &composite_error))
1982 fprintf (stderr, "No composite extension\n");
1985 XCompositeQueryVersion (dpy, &composite_major, &composite_minor);
1986 #if HAS_NAME_WINDOW_PIXMAP
1987 if (composite_major > 0 || composite_minor >= 2)
1988 hasNamePixmap = True;
1991 if (!XDamageQueryExtension (dpy, &damage_event, &damage_error))
1993 fprintf (stderr, "No damage extension\n");
1996 if (!XFixesQueryExtension (dpy, &xfixes_event, &xfixes_error))
1998 fprintf (stderr, "No XFixes extension\n");
2005 opacityAtom = XInternAtom (dpy, OPACITY_PROP, False);
2006 winTypeAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE", False);
2007 winDesktopAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DESKTOP", False);
2008 winDockAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DOCK", False);
2009 winToolbarAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_TOOLBAR", False);
2010 winMenuAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_MENU", False);
2011 winUtilAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_UTILITY", False);
2012 winSplashAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_SPLASH", False);
2013 winDialogAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
2014 winNormalAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_NORMAL", False);
2016 pa.subwindow_mode = IncludeInferiors;
2018 if (compMode == CompClientShadows)
2020 gaussianMap = make_gaussian_map(dpy, shadowRadius);
2021 presum_gaussian (gaussianMap);
2024 root_width = DisplayWidth (dpy, scr);
2025 root_height = DisplayHeight (dpy, scr);
2027 rootPicture = XRenderCreatePicture (dpy, root,
2028 XRenderFindVisualFormat (dpy,
2029 DefaultVisual (dpy, scr)),
2032 blackPicture = solid_picture (dpy, True, 1, 0, 0, 0);
2033 if (compMode == CompServerShadows)
2034 transBlackPicture = solid_picture (dpy, True, 0.3, 0, 0, 0);
2039 XCompositeRedirectSubwindows (dpy, root, CompositeRedirectAutomatic);
2042 XCompositeRedirectSubwindows (dpy, root, CompositeRedirectManual);
2043 XSelectInput (dpy, root,
2044 SubstructureNotifyMask|
2046 StructureNotifyMask|
2047 PropertyChangeMask);
2048 XQueryTree (dpy, root, &root_return, &parent_return, &children, &nchildren);
2049 for (i = 0; i < nchildren; i++)
2050 add_win (dpy, children[i], i ? children[i-1] : None);
2053 XUngrabServer (dpy);
2054 ufd.fd = ConnectionNumber (dpy);
2055 ufd.events = POLLIN;
2057 paint_all (dpy, None);
2066 if (poll (&ufd, 1, fade_timeout()) == 0)
2073 XNextEvent (dpy, &ev);
2074 if ((ev.type & 0x7f) != KeymapNotify)
2075 discard_ignore (dpy, ev.xany.serial);
2077 printf ("event %10.10s serial 0x%08x window 0x%08x\n",
2078 ev_name(&ev), ev_serial (&ev), ev_window (&ev));
2080 if (!autoRedirect) switch (ev.type) {
2082 add_win (dpy, ev.xcreatewindow.window, 0);
2084 case ConfigureNotify:
2085 configure_win (dpy, &ev.xconfigure);
2088 destroy_win (dpy, ev.xdestroywindow.window, True, True);
2091 map_win (dpy, ev.xmap.window, ev.xmap.serial, True);
2094 unmap_win (dpy, ev.xunmap.window, True);
2096 case ReparentNotify:
2097 if (ev.xreparent.parent == root)
2098 add_win (dpy, ev.xreparent.window, 0);
2100 destroy_win (dpy, ev.xreparent.window, False, True);
2102 case CirculateNotify:
2103 circulate_win (dpy, &ev.xcirculate);
2106 if (ev.xexpose.window == root)
2108 int more = ev.xexpose.count + 1;
2109 if (n_expose == size_expose)
2113 expose_rects = realloc (expose_rects,
2114 (size_expose + more) *
2115 sizeof (XRectangle));
2116 size_expose += more;
2120 expose_rects = malloc (more * sizeof (XRectangle));
2124 expose_rects[n_expose].x = ev.xexpose.x;
2125 expose_rects[n_expose].y = ev.xexpose.y;
2126 expose_rects[n_expose].width = ev.xexpose.width;
2127 expose_rects[n_expose].height = ev.xexpose.height;
2129 if (ev.xexpose.count == 0)
2131 expose_root (dpy, root, expose_rects, n_expose);
2136 case PropertyNotify:
2137 for (p = 0; backgroundProps[p]; p++)
2139 if (ev.xproperty.atom == XInternAtom (dpy, backgroundProps[p], False))
2143 XClearArea (dpy, root, 0, 0, 0, 0, True);
2144 XRenderFreePicture (dpy, rootTile);
2150 /* check if Trans property was changed */
2151 if (ev.xproperty.atom == opacityAtom)
2153 /* reset mode and redraw window */
2154 win * w = find_win(dpy, ev.xproperty.window);
2158 set_fade (dpy, w, w->opacity*1.0/OPAQUE, get_opacity_percent (dpy, w, 1.0),
2159 fade_out_step, 0, False, True, False);
2162 w->opacity = get_opacity_prop(dpy, w, OPAQUE);
2163 determine_mode(dpy, w);
2166 XRenderFreePicture (dpy, w->shadow);
2168 w->extents = win_extents (dpy, w);
2175 if (ev.type == damage_event + XDamageNotify)
2176 damage_win (dpy, (XDamageNotifyEvent *) &ev);
2179 } while (QLength (dpy));
2180 if (allDamage && !autoRedirect)
2183 paint_all (dpy, allDamage);
2187 clipChanged = False;