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);
681 picture = XRenderCreatePicture (dpy, pixmap,
682 XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8),
685 c.alpha = a * 0xffff;
687 c.green = g * 0xffff;
689 XRenderFillRectangle (dpy, PictOpSrc, picture, &c, 0, 0, 1, 1);
690 XFreePixmap (dpy, pixmap);
695 discard_ignore (Display *dpy, unsigned long sequence)
699 if ((long) (sequence - ignore_head->sequence) > 0)
701 ignore *next = ignore_head->next;
705 ignore_tail = &ignore_head;
713 set_ignore (Display *dpy, unsigned long sequence)
715 ignore *i = malloc (sizeof (ignore));
718 i->sequence = sequence;
721 ignore_tail = &i->next;
725 should_ignore (Display *dpy, unsigned long sequence)
727 discard_ignore (dpy, sequence);
728 return ignore_head && ignore_head->sequence == sequence;
732 find_win (Display *dpy, Window id)
736 for (w = list; w; w = w->next)
742 static char *backgroundProps[] = {
749 root_tile (Display *dpy)
755 unsigned long nitems;
756 unsigned long bytes_after;
759 XRenderPictureAttributes pa;
763 for (p = 0; backgroundProps[p]; p++)
765 if (XGetWindowProperty (dpy, root, XInternAtom (dpy, backgroundProps[p], False),
766 0, 4, False, AnyPropertyType,
767 &actual_type, &actual_format, &nitems, &bytes_after, &prop) == Success &&
768 actual_type == XInternAtom (dpy, "PIXMAP", False) && actual_format == 32 && nitems == 1)
770 memcpy (&pixmap, prop, 4);
778 pixmap = XCreatePixmap (dpy, root, 1, 1, DefaultDepth (dpy, scr));
782 picture = XRenderCreatePicture (dpy, pixmap,
783 XRenderFindVisualFormat (dpy,
784 DefaultVisual (dpy, scr)),
790 c.red = c.green = c.blue = 0x8080;
792 XRenderFillRectangle (dpy, PictOpSrc, picture, &c,
799 paint_root (Display *dpy)
802 rootTile = root_tile (dpy);
804 XRenderComposite (dpy, PictOpSrc,
805 rootTile, None, rootBuffer,
806 0, 0, 0, 0, 0, 0, root_width, root_height);
810 win_extents (Display *dpy, win *w)
816 r.width = w->a.width + w->a.border_width * 2;
817 r.height = w->a.height + w->a.border_width * 2;
818 if (compMode != CompSimple && !(w->windowType == winDockAtom && excludeDockShadows))
820 if (compMode == CompServerShadows || w->mode != WINDOW_ARGB)
824 if (compMode == CompServerShadows)
828 w->shadow_width = w->a.width;
829 w->shadow_height = w->a.height;
833 w->shadow_dx = shadowOffsetX;
834 w->shadow_dy = shadowOffsetY;
837 double opacity = shadowOpacity;
838 if (w->mode == WINDOW_TRANS)
839 opacity = opacity * ((double)w->opacity)/((double)OPAQUE);
840 w->shadow = shadow_picture (dpy, opacity, w->alphaPict,
841 w->a.width + w->a.border_width * 2,
842 w->a.height + w->a.border_width * 2,
843 &w->shadow_width, &w->shadow_height);
846 sr.x = w->a.x + w->shadow_dx;
847 sr.y = w->a.y + w->shadow_dy;
848 sr.width = w->shadow_width;
849 sr.height = w->shadow_height;
852 r.width = (r.x + r.width) - sr.x;
857 r.height = (r.y + r.height) - sr.y;
860 if (sr.x + sr.width > r.x + r.width)
861 r.width = sr.x + sr.width - r.x;
862 if (sr.y + sr.height > r.y + r.height)
863 r.height = sr.y + sr.height - r.y;
866 return XFixesCreateRegion (dpy, &r, 1);
870 border_size (Display *dpy, win *w)
872 XserverRegion border;
874 * if window doesn't exist anymore, this will generate an error
875 * as well as not generate a region. Perhaps a better XFixes
876 * architecture would be to have a request that copies instead
877 * of creates, that way you'd just end up with an empty region
878 * instead of an invalid XID.
880 set_ignore (dpy, NextRequest (dpy));
881 border = XFixesCreateRegionFromWindow (dpy, w->id, WindowRegionBounding);
883 set_ignore (dpy, NextRequest (dpy));
884 XFixesTranslateRegion (dpy, border,
885 w->a.x + w->a.border_width,
886 w->a.y + w->a.border_width);
891 paint_all (Display *dpy, XserverRegion region)
901 r.width = root_width;
902 r.height = root_height;
903 region = XFixesCreateRegion (dpy, &r, 1);
906 rootBuffer = rootPicture;
910 Pixmap rootPixmap = XCreatePixmap (dpy, root, root_width, root_height,
911 DefaultDepth (dpy, scr));
912 rootBuffer = XRenderCreatePicture (dpy, rootPixmap,
913 XRenderFindVisualFormat (dpy,
914 DefaultVisual (dpy, scr)),
916 XFreePixmap (dpy, rootPixmap);
919 XFixesSetPictureClipRegion (dpy, rootPicture, 0, 0, region);
921 XRenderComposite (dpy, PictOpSrc, blackPicture, None, rootPicture,
922 0, 0, 0, 0, 0, 0, root_width, root_height);
927 for (w = list; w; w = w->next)
933 /* never painted, ignore it */
938 XRenderPictureAttributes pa;
939 XRenderPictFormat *format;
940 Drawable draw = w->id;
942 #if HAS_NAME_WINDOW_PIXMAP
943 if (hasNamePixmap && !w->pixmap)
944 w->pixmap = XCompositeNameWindowPixmap (dpy, w->id);
948 format = XRenderFindVisualFormat (dpy, w->a.visual);
949 pa.subwindow_mode = IncludeInferiors;
950 w->picture = XRenderCreatePicture (dpy, draw,
956 printf (" 0x%x", w->id);
962 set_ignore (dpy, NextRequest (dpy));
963 XFixesDestroyRegion (dpy, w->borderSize);
964 w->borderSize = None;
968 XFixesDestroyRegion (dpy, w->extents);
973 XFixesDestroyRegion (dpy, w->borderClip);
974 w->borderClip = None;
978 w->borderSize = border_size (dpy, w);
980 w->extents = win_extents (dpy, w);
981 if (w->mode == WINDOW_SOLID)
984 #if HAS_NAME_WINDOW_PIXMAP
987 wid = w->a.width + w->a.border_width * 2;
988 hei = w->a.height + w->a.border_width * 2;
990 x = w->a.x + w->a.border_width;
991 y = w->a.y + w->a.border_width;
995 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
996 set_ignore (dpy, NextRequest (dpy));
997 XFixesSubtractRegion (dpy, region, region, w->borderSize);
998 set_ignore (dpy, NextRequest (dpy));
999 XRenderComposite (dpy, PictOpSrc, w->picture, None, rootBuffer,
1005 w->borderClip = XFixesCreateRegion (dpy, 0, 0);
1006 XFixesCopyRegion (dpy, w->borderClip, region);
1015 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
1017 for (w = t; w; w = w->prev_trans)
1019 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, w->borderClip);
1023 case CompServerShadows:
1024 set_ignore (dpy, NextRequest (dpy));
1025 if (w->opacity != OPAQUE && !w->shadowPict)
1026 w->shadowPict = solid_picture (dpy, True,
1027 (double) w->opacity / OPAQUE * 0.3,
1029 XRenderComposite (dpy, PictOpOver,
1030 w->shadowPict ? w->shadowPict : transBlackPicture,
1031 w->picture, rootBuffer,
1033 w->a.x + w->shadow_dx,
1034 w->a.y + w->shadow_dy,
1035 w->shadow_width, w->shadow_height);
1037 case CompClientShadows:
1040 XRenderComposite (dpy, PictOpOver, blackPicture, w->shadow, rootBuffer,
1042 w->a.x + w->shadow_dx,
1043 w->a.y + w->shadow_dy,
1044 w->shadow_width, w->shadow_height);
1048 if (w->opacity != OPAQUE && !w->alphaPict)
1049 w->alphaPict = solid_picture (dpy, False,
1050 (double) w->opacity / OPAQUE, 0, 0, 0);
1051 if (w->mode == WINDOW_TRANS)
1054 #if HAS_NAME_WINDOW_PIXMAP
1057 wid = w->a.width + w->a.border_width * 2;
1058 hei = w->a.height + w->a.border_width * 2;
1060 x = w->a.x + w->a.border_width;
1061 y = w->a.y + w->a.border_width;
1065 set_ignore (dpy, NextRequest (dpy));
1066 XRenderComposite (dpy, PictOpOver, w->picture, w->alphaPict, rootBuffer,
1070 else if (w->mode == WINDOW_ARGB)
1073 #if HAS_NAME_WINDOW_PIXMAP
1076 wid = w->a.width + w->a.border_width * 2;
1077 hei = w->a.height + w->a.border_width * 2;
1079 x = w->a.x + w->a.border_width;
1080 y = w->a.y + w->a.border_width;
1084 set_ignore (dpy, NextRequest (dpy));
1085 XRenderComposite (dpy, PictOpOver, w->picture, w->alphaPict, rootBuffer,
1089 XFixesDestroyRegion (dpy, w->borderClip);
1090 w->borderClip = None;
1092 XFixesDestroyRegion (dpy, region);
1093 if (rootBuffer != rootPicture)
1095 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, None);
1096 XRenderComposite (dpy, PictOpSrc, rootBuffer, None, rootPicture,
1097 0, 0, 0, 0, 0, 0, root_width, root_height);
1102 add_damage (Display *dpy, XserverRegion damage)
1106 XFixesUnionRegion (dpy, allDamage, allDamage, damage);
1107 XFixesDestroyRegion (dpy, damage);
1114 repair_win (Display *dpy, win *w)
1116 XserverRegion parts;
1120 parts = win_extents (dpy, w);
1121 set_ignore (dpy, NextRequest (dpy));
1122 XDamageSubtract (dpy, w->damage, None, None);
1127 parts = XFixesCreateRegion (dpy, 0, 0);
1128 set_ignore (dpy, NextRequest (dpy));
1129 XDamageSubtract (dpy, w->damage, None, parts);
1130 XFixesTranslateRegion (dpy, parts,
1131 w->a.x + w->a.border_width,
1132 w->a.y + w->a.border_width);
1133 if (compMode == CompServerShadows)
1135 o = XFixesCreateRegion (dpy, 0, 0);
1136 XFixesCopyRegion (dpy, o, parts);
1137 XFixesTranslateRegion (dpy, o, w->shadow_dx, w->shadow_dy);
1138 XFixesUnionRegion (dpy, parts, parts, o);
1139 XFixesDestroyRegion (dpy, o);
1142 add_damage (dpy, parts);
1147 map_win (Display *dpy, Window id, unsigned long sequence, Bool fade)
1149 win *w = find_win (dpy, id);
1155 w->a.map_state = IsViewable;
1157 /* This needs to be here or else we lose transparency messages */
1158 XSelectInput (dpy, id, PropertyChangeMask);
1161 w->damage_bounds.x = w->damage_bounds.y = 0;
1162 w->damage_bounds.width = w->damage_bounds.height = 0;
1166 if (fade && fadeWindows)
1167 set_fade (dpy, w, 0, get_opacity_percent (dpy, w, 1.0), fade_in_step, 0, False, True, True);
1171 finish_unmap_win (Display *dpy, win *w)
1177 if (w->extents != None)
1179 add_damage (dpy, w->extents); /* destroys region */
1183 #if HAS_NAME_WINDOW_PIXMAP
1186 XFreePixmap (dpy, w->pixmap);
1193 set_ignore (dpy, NextRequest (dpy));
1194 XRenderFreePicture (dpy, w->picture);
1198 /* don't care about properties anymore */
1199 set_ignore (dpy, NextRequest (dpy));
1200 XSelectInput(dpy, w->id, 0);
1204 set_ignore (dpy, NextRequest (dpy));
1205 XFixesDestroyRegion (dpy, w->borderSize);
1206 w->borderSize = None;
1210 XRenderFreePicture (dpy, w->shadow);
1215 XFixesDestroyRegion (dpy, w->borderClip);
1216 w->borderClip = None;
1222 #if HAS_NAME_WINDOW_PIXMAP
1224 unmap_callback (Display *dpy, win *w, Bool gone)
1226 finish_unmap_win (dpy, w);
1231 unmap_win (Display *dpy, Window id, Bool fade)
1233 win *w = find_win (dpy, id);
1236 w->a.map_state = IsUnmapped;
1237 #if HAS_NAME_WINDOW_PIXMAP
1238 if (w->pixmap && fade && fadeWindows)
1239 set_fade (dpy, w, w->opacity*1.0/OPAQUE, 0.0, fade_out_step, unmap_callback, False, False, True);
1242 finish_unmap_win (dpy, w);
1245 /* Get the opacity prop from window
1250 get_opacity_prop(Display *dpy, win *w, unsigned int def)
1254 unsigned long n, left;
1256 unsigned char *data;
1257 int result = XGetWindowProperty(dpy, w->id, opacityAtom, 0L, 1L, False,
1258 XA_CARDINAL, &actual, &format,
1260 if (result == Success && data != None)
1263 memcpy (&i, data, sizeof (unsigned int));
1264 XFree( (void *) data);
1270 /* Get the opacity property from the window in a percent format
1272 otherwise: the value
1275 get_opacity_percent(Display *dpy, win *w, double def)
1277 unsigned int opacity = get_opacity_prop (dpy, w, (unsigned int)(OPAQUE*def));
1279 return opacity*1.0/OPAQUE;
1282 /* determine mode for window all in one place.
1283 Future might check for menu flag and other cool things
1287 get_wintype_prop(Display * dpy, Window w)
1291 unsigned long n, left;
1293 unsigned char *data;
1294 int result = XGetWindowProperty (dpy, w, winTypeAtom, 0L, 1L, False,
1295 XA_ATOM, &actual, &format,
1298 if (result == Success && data != None)
1301 memcpy (&a, data, sizeof (Atom));
1302 XFree ( (void *) data);
1305 return winNormalAtom;
1309 determine_mode(Display *dpy, win *w)
1312 XRenderPictFormat *format;
1313 unsigned int default_opacity;
1315 /* if trans prop == -1 fall back on previous tests*/
1319 XRenderFreePicture (dpy, w->alphaPict);
1320 w->alphaPict = None;
1324 XRenderFreePicture (dpy, w->shadowPict);
1325 w->shadowPict = None;
1328 if (w->a.class == InputOnly)
1334 format = XRenderFindVisualFormat (dpy, w->a.visual);
1337 if (format && format->type == PictTypeDirect && format->direct.alphaMask)
1341 else if (w->opacity != OPAQUE)
1343 mode = WINDOW_TRANS;
1347 mode = WINDOW_SOLID;
1352 XserverRegion damage;
1353 damage = XFixesCreateRegion (dpy, 0, 0);
1354 XFixesCopyRegion (dpy, damage, w->extents);
1355 add_damage (dpy, damage);
1360 determine_wintype (Display *dpy, Window w)
1362 Window root_return, parent_return;
1364 unsigned int nchildren, i;
1367 type = get_wintype_prop (dpy, w);
1368 if (type != winNormalAtom)
1371 if (!XQueryTree (dpy, w, &root_return, &parent_return, &children,
1374 /* XQueryTree failed. */
1375 return winNormalAtom;
1378 for (i = 0;i < nchildren;i++)
1380 type = determine_wintype (dpy, children[i]);
1381 if (type != winNormalAtom)
1385 return winNormalAtom;
1389 add_win (Display *dpy, Window id, Window prev)
1391 win *new = malloc (sizeof (win));
1398 for (p = &list; *p; p = &(*p)->next)
1399 if ((*p)->id == prev)
1405 set_ignore (dpy, NextRequest (dpy));
1406 if (!XGetWindowAttributes (dpy, id, &new->a))
1413 new->usable = False;
1415 #if HAS_NAME_WINDOW_PIXMAP
1418 new->picture = None;
1419 if (new->a.class == InputOnly)
1421 new->damage_sequence = 0;
1426 new->damage_sequence = NextRequest (dpy);
1427 new->damage = XDamageCreate (dpy, id, XDamageReportNonEmpty);
1429 new->alphaPict = None;
1430 new->shadowPict = None;
1431 new->borderSize = None;
1432 new->extents = None;
1436 new->shadow_width = 0;
1437 new->shadow_height = 0;
1438 new->opacity = OPAQUE;
1440 new->borderClip = None;
1441 new->prev_trans = 0;
1443 /* moved mode setting to one place */
1444 new->opacity = get_opacity_prop (dpy, new, OPAQUE);
1445 new->windowType = determine_wintype (dpy, new->id);
1446 determine_mode (dpy, new);
1450 if (new->a.map_state == IsViewable)
1451 map_win (dpy, id, new->damage_sequence - 1, True);
1455 restack_win (Display *dpy, win *w, Window new_above)
1460 old_above = w->next->id;
1463 if (old_above != new_above)
1468 for (prev = &list; *prev; prev = &(*prev)->next)
1474 for (prev = &list; *prev; prev = &(*prev)->next)
1476 if ((*prev)->id == new_above)
1485 configure_win (Display *dpy, XConfigureEvent *ce)
1487 win *w = find_win (dpy, ce->window);
1489 XserverRegion damage = None;
1493 if (ce->window == root)
1497 XRenderFreePicture (dpy, rootBuffer);
1500 root_width = ce->width;
1501 root_height = ce->height;
1509 damage = XFixesCreateRegion (dpy, 0, 0);
1510 if (w->extents != None)
1511 XFixesCopyRegion (dpy, damage, w->extents);
1515 if (w->a.width != ce->width || w->a.height != ce->height)
1517 #if HAS_NAME_WINDOW_PIXMAP
1520 XFreePixmap (dpy, w->pixmap);
1524 XRenderFreePicture (dpy, w->picture);
1531 XRenderFreePicture (dpy, w->shadow);
1535 w->a.width = ce->width;
1536 w->a.height = ce->height;
1537 w->a.border_width = ce->border_width;
1538 w->a.override_redirect = ce->override_redirect;
1539 restack_win (dpy, w, ce->above);
1542 XserverRegion extents = win_extents (dpy, w);
1543 XFixesUnionRegion (dpy, damage, damage, extents);
1544 XFixesDestroyRegion (dpy, extents);
1545 add_damage (dpy, damage);
1551 circulate_win (Display *dpy, XCirculateEvent *ce)
1553 win *w = find_win (dpy, ce->window);
1556 if (ce->place == PlaceOnTop)
1557 new_above = list->id;
1560 restack_win (dpy, w, new_above);
1565 finish_destroy_win (Display *dpy, Window id, Bool gone)
1569 for (prev = &list; (w = *prev); prev = &w->next)
1573 finish_unmap_win (dpy, w);
1577 set_ignore (dpy, NextRequest (dpy));
1578 XRenderFreePicture (dpy, w->picture);
1583 XRenderFreePicture (dpy, w->alphaPict);
1584 w->alphaPict = None;
1588 XRenderFreePicture (dpy, w->shadowPict);
1589 w->shadowPict = None;
1591 if (w->damage != None)
1593 set_ignore (dpy, NextRequest (dpy));
1594 XDamageDestroy (dpy, w->damage);
1597 cleanup_fade (dpy, w);
1603 #if HAS_NAME_WINDOW_PIXMAP
1605 destroy_callback (Display *dpy, win *w, Bool gone)
1607 finish_destroy_win (dpy, w->id, gone);
1612 destroy_win (Display *dpy, Window id, Bool gone, Bool fade)
1614 win *w = find_win (dpy, id);
1615 #if HAS_NAME_WINDOW_PIXMAP
1616 if (w && w->pixmap && fade && fadeWindows)
1617 set_fade (dpy, w, w->opacity*1.0/OPAQUE, 0.0, fade_out_step, destroy_callback, gone, False, True);
1621 finish_destroy_win (dpy, id, gone);
1629 printf ("\t%08lx: %d x %d + %d + %d (%d)\n", w->id,
1630 w->a.width, w->a.height, w->a.x, w->a.y, w->a.border_width);
1639 printf ("windows:\n");
1640 for (w = list; w; w = w->next)
1646 damage_win (Display *dpy, XDamageNotifyEvent *de)
1648 win *w = find_win (dpy, de->drawable);
1655 if (w->damage_bounds.width == 0 || w->damage_bounds.height == 0)
1657 w->damage_bounds = de->area;
1661 if (de->area.x < w->damage_bounds.x)
1663 w->damage_bounds.width += (w->damage_bounds.x - de->area.x);
1664 w->damage_bounds.x = de->area.x;
1666 if (de->area.y < w->damage_bounds.y)
1668 w->damage_bounds.height += (w->damage_bounds.y - de->area.y);
1669 w->damage_bounds.y = de->area.y;
1671 if (de->area.x + de->area.width > w->damage_bounds.x + w->damage_bounds.width)
1672 w->damage_bounds.width = de->area.x + de->area.width - w->damage_bounds.x;
1673 if (de->area.y + de->area.height > w->damage_bounds.y + w->damage_bounds.height)
1674 w->damage_bounds.height = de->area.y + de->area.height - w->damage_bounds.y;
1677 printf ("unusable damage %d, %d: %d x %d bounds %d, %d: %d x %d\n",
1684 w->damage_bounds.width,
1685 w->damage_bounds.height);
1687 if (w->damage_bounds.x <= 0 &&
1688 w->damage_bounds.y <= 0 &&
1689 w->a.width <= w->damage_bounds.x + w->damage_bounds.width &&
1690 w->a.height <= w->damage_bounds.y + w->damage_bounds.height)
1694 set_fade (dpy, w, 0, get_opacity_percent (dpy, w, 1.0), fade_in_step, 0, False, True, True);
1700 repair_win (dpy, w);
1704 error (Display *dpy, XErrorEvent *ev)
1709 if (should_ignore (dpy, ev->serial))
1712 if (ev->request_code == composite_opcode &&
1713 ev->minor_code == X_CompositeRedirectSubwindows)
1715 fprintf (stderr, "Another composite manager is already running\n");
1719 o = ev->error_code - xfixes_error;
1721 case BadRegion: name = "BadRegion"; break;
1724 o = ev->error_code - damage_error;
1726 case BadDamage: name = "BadDamage"; break;
1729 o = ev->error_code - render_error;
1731 case BadPictFormat: name ="BadPictFormat"; break;
1732 case BadPicture: name ="BadPicture"; break;
1733 case BadPictOp: name ="BadPictOp"; break;
1734 case BadGlyphSet: name ="BadGlyphSet"; break;
1735 case BadGlyph: name ="BadGlyph"; break;
1739 printf ("error %d request %d minor %d serial %d\n",
1740 ev->error_code, ev->request_code, ev->minor_code, ev->serial);
1742 /* abort (); this is just annoying to most people */
1747 expose_root (Display *dpy, Window root, XRectangle *rects, int nrects)
1749 XserverRegion region = XFixesCreateRegion (dpy, rects, nrects);
1751 add_damage (dpy, region);
1756 ev_serial (XEvent *ev)
1758 if (ev->type & 0x7f != KeymapNotify)
1759 return ev->xany.serial;
1760 return NextRequest (ev->xany.display);
1765 ev_name (XEvent *ev)
1767 static char buf[128];
1768 switch (ev->type & 0x7f) {
1775 case ReparentNotify:
1777 case CirculateNotify:
1780 if (ev->type == damage_event + XDamageNotify)
1782 sprintf (buf, "Event %d", ev->type);
1788 ev_window (XEvent *ev)
1792 return ev->xexpose.window;
1794 return ev->xmap.window;
1796 return ev->xunmap.window;
1797 case ReparentNotify:
1798 return ev->xreparent.window;
1799 case CirculateNotify:
1800 return ev->xcirculate.window;
1802 if (ev->type == damage_event + XDamageNotify)
1803 return ((XDamageNotifyEvent *) ev)->drawable;
1809 usage (char *program)
1811 fprintf (stderr, "%s v1.0\n", program);
1812 fprintf (stderr, "usage: %s [options]\n", program);
1813 fprintf (stderr, "Options\n");
1814 fprintf (stderr, " -d display\n Specifies which display should be managed.\n");
1815 fprintf (stderr, " -r radius\n Specifies the blur radius for client-side shadows. (default 12)\n");
1816 fprintf (stderr, " -o opacity\n Specifies the translucency for client-side shadows. (default .75)\n");
1817 fprintf (stderr, " -l left-offset\n Specifies the left offset for client-side shadows. (default -15)\n");
1818 fprintf (stderr, " -t top-offset\n Specifies the top offset for clinet-side shadows. (default -15)\n");
1819 fprintf (stderr, " -a\n Use automatic server-side compositing. Faster, but no special effects.\n");
1820 fprintf (stderr, " -c\n Draw client-side shadows with fuzzy edges.\n");
1821 fprintf (stderr, " -C\n Avoid drawing shadows on dock/panel windows.\n");
1822 fprintf (stderr, " -f\n Fade windows in/out when opening/closing.\n");
1823 fprintf (stderr, " -F\n Fade windows during opacity changes.\n");
1824 fprintf (stderr, " -n\n Normal client-side compositing with transparency support\n");
1825 fprintf (stderr, " -s\n Draw server-side shadows with sharp edges.\n");
1826 fprintf (stderr, " -S\n Enable synchronous operation (for debugging).\n");
1831 main (int argc, char **argv)
1834 Window root_return, parent_return;
1838 unsigned int nchildren;
1840 XRenderPictureAttributes pa;
1842 XRectangle *expose_rects = 0;
1843 int size_expose = 0;
1850 int composite_major, composite_minor;
1854 while ((o = getopt (argc, argv, "d:r:o:l:t:scnfFCaS")) != -1)
1861 compMode = CompServerShadows;
1864 compMode = CompClientShadows;
1867 excludeDockShadows = True;
1870 compMode = CompSimple;
1879 autoRedirect = True;
1885 shadowRadius = atoi (optarg);
1888 shadowOpacity = atof (optarg);
1891 shadowOffsetX = atoi (optarg);
1894 shadowOffsetY = atoi (optarg);
1902 dpy = XOpenDisplay (display);
1905 fprintf (stderr, "Can't open display\n");
1908 XSetErrorHandler (error);
1910 XSynchronize (dpy, 1);
1911 scr = DefaultScreen (dpy);
1912 root = RootWindow (dpy, scr);
1914 if (!XRenderQueryExtension (dpy, &render_event, &render_error))
1916 fprintf (stderr, "No render extension\n");
1919 if (!XQueryExtension (dpy, COMPOSITE_NAME, &composite_opcode,
1920 &composite_event, &composite_error))
1922 fprintf (stderr, "No composite extension\n");
1925 XCompositeQueryVersion (dpy, &composite_major, &composite_minor);
1926 #if HAS_NAME_WINDOW_PIXMAP
1927 if (composite_major > 0 || composite_minor >= 2)
1928 hasNamePixmap = True;
1931 if (!XDamageQueryExtension (dpy, &damage_event, &damage_error))
1933 fprintf (stderr, "No damage extension\n");
1936 if (!XFixesQueryExtension (dpy, &xfixes_event, &xfixes_error))
1938 fprintf (stderr, "No XFixes extension\n");
1942 opacityAtom = XInternAtom (dpy, OPACITY_PROP, False);
1943 winTypeAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE", False);
1944 winDesktopAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DESKTOP", False);
1945 winDockAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DOCK", False);
1946 winToolbarAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_TOOLBAR", False);
1947 winMenuAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_MENU", False);
1948 winUtilAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_UTILITY", False);
1949 winSplashAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_SPLASH", False);
1950 winDialogAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1951 winNormalAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_NORMAL", False);
1953 pa.subwindow_mode = IncludeInferiors;
1955 if (compMode == CompClientShadows)
1957 gaussianMap = make_gaussian_map(dpy, shadowRadius);
1958 presum_gaussian (gaussianMap);
1961 root_width = DisplayWidth (dpy, scr);
1962 root_height = DisplayHeight (dpy, scr);
1964 rootPicture = XRenderCreatePicture (dpy, root,
1965 XRenderFindVisualFormat (dpy,
1966 DefaultVisual (dpy, scr)),
1969 blackPicture = solid_picture (dpy, True, 1, 0, 0, 0);
1970 if (compMode == CompServerShadows)
1971 transBlackPicture = solid_picture (dpy, True, 0.3, 0, 0, 0);
1976 XCompositeRedirectSubwindows (dpy, root, CompositeRedirectAutomatic);
1979 XCompositeRedirectSubwindows (dpy, root, CompositeRedirectManual);
1980 XSelectInput (dpy, root,
1981 SubstructureNotifyMask|
1983 StructureNotifyMask|
1984 PropertyChangeMask);
1985 XQueryTree (dpy, root, &root_return, &parent_return, &children, &nchildren);
1986 for (i = 0; i < nchildren; i++)
1987 add_win (dpy, children[i], i ? children[i-1] : None);
1990 XUngrabServer (dpy);
1991 ufd.fd = ConnectionNumber (dpy);
1992 ufd.events = POLLIN;
1994 paint_all (dpy, None);
2003 if (poll (&ufd, 1, fade_timeout()) == 0)
2010 XNextEvent (dpy, &ev);
2011 if (ev.type & 0x7f != KeymapNotify)
2012 discard_ignore (dpy, ev.xany.serial);
2014 printf ("event %10.10s serial 0x%08x window 0x%08x\n",
2015 ev_name(&ev), ev_serial (&ev), ev_window (&ev));
2017 if (!autoRedirect) switch (ev.type) {
2019 add_win (dpy, ev.xcreatewindow.window, 0);
2021 case ConfigureNotify:
2022 configure_win (dpy, &ev.xconfigure);
2025 destroy_win (dpy, ev.xdestroywindow.window, True, True);
2028 map_win (dpy, ev.xmap.window, ev.xmap.serial, True);
2031 unmap_win (dpy, ev.xunmap.window, True);
2033 case ReparentNotify:
2034 if (ev.xreparent.parent == root)
2035 add_win (dpy, ev.xreparent.window, 0);
2037 destroy_win (dpy, ev.xreparent.window, False, True);
2039 case CirculateNotify:
2040 circulate_win (dpy, &ev.xcirculate);
2043 if (ev.xexpose.window == root)
2045 int more = ev.xexpose.count + 1;
2046 if (n_expose == size_expose)
2050 expose_rects = realloc (expose_rects,
2051 (size_expose + more) *
2052 sizeof (XRectangle));
2053 size_expose += more;
2057 expose_rects = malloc (more * sizeof (XRectangle));
2061 expose_rects[n_expose].x = ev.xexpose.x;
2062 expose_rects[n_expose].y = ev.xexpose.y;
2063 expose_rects[n_expose].width = ev.xexpose.width;
2064 expose_rects[n_expose].height = ev.xexpose.height;
2066 if (ev.xexpose.count == 0)
2068 expose_root (dpy, root, expose_rects, n_expose);
2073 case PropertyNotify:
2074 for (p = 0; backgroundProps[p]; p++)
2076 if (ev.xproperty.atom == XInternAtom (dpy, backgroundProps[p], False))
2080 XClearArea (dpy, root, 0, 0, 0, 0, True);
2081 XRenderFreePicture (dpy, rootTile);
2087 /* check if Trans property was changed */
2088 if (ev.xproperty.atom == opacityAtom)
2090 /* reset mode and redraw window */
2091 win * w = find_win(dpy, ev.xproperty.window);
2095 set_fade (dpy, w, w->opacity*1.0/OPAQUE, get_opacity_percent (dpy, w, 1.0),
2096 fade_out_step, 0, False, True, False);
2099 w->opacity = get_opacity_prop(dpy, w, OPAQUE);
2100 determine_mode(dpy, w);
2103 XRenderFreePicture (dpy, w->shadow);
2105 w->extents = win_extents (dpy, w);
2112 if (ev.type == damage_event + XDamageNotify)
2113 damage_win (dpy, (XDamageNotifyEvent *) &ev);
2116 } while (QLength (dpy));
2117 if (allDamage && !autoRedirect)
2120 paint_all (dpy, allDamage);
2124 clipChanged = False;