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 */
947 XRenderPictureAttributes pa;
948 XRenderPictFormat *format;
949 Drawable draw = w->id;
951 #if HAS_NAME_WINDOW_PIXMAP
952 if (hasNamePixmap && !w->pixmap)
953 w->pixmap = XCompositeNameWindowPixmap (dpy, w->id);
957 format = XRenderFindVisualFormat (dpy, w->a.visual);
958 pa.subwindow_mode = IncludeInferiors;
959 w->picture = XRenderCreatePicture (dpy, draw,
965 printf (" 0x%x", w->id);
971 set_ignore (dpy, NextRequest (dpy));
972 XFixesDestroyRegion (dpy, w->borderSize);
973 w->borderSize = None;
977 XFixesDestroyRegion (dpy, w->extents);
982 XFixesDestroyRegion (dpy, w->borderClip);
983 w->borderClip = None;
987 w->borderSize = border_size (dpy, w);
989 w->extents = win_extents (dpy, w);
990 if (w->mode == WINDOW_SOLID)
993 #if HAS_NAME_WINDOW_PIXMAP
996 wid = w->a.width + w->a.border_width * 2;
997 hei = w->a.height + w->a.border_width * 2;
999 x = w->a.x + w->a.border_width;
1000 y = w->a.y + w->a.border_width;
1004 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
1005 set_ignore (dpy, NextRequest (dpy));
1006 XFixesSubtractRegion (dpy, region, region, w->borderSize);
1007 set_ignore (dpy, NextRequest (dpy));
1008 XRenderComposite (dpy, PictOpSrc, w->picture, None, rootBuffer,
1014 w->borderClip = XFixesCreateRegion (dpy, 0, 0);
1015 XFixesCopyRegion (dpy, w->borderClip, region);
1024 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
1026 for (w = t; w; w = w->prev_trans)
1028 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, w->borderClip);
1032 case CompServerShadows:
1033 set_ignore (dpy, NextRequest (dpy));
1034 if (w->opacity != OPAQUE && !w->shadowPict)
1035 w->shadowPict = solid_picture (dpy, True,
1036 (double) w->opacity / OPAQUE * 0.3,
1038 XRenderComposite (dpy, PictOpOver,
1039 w->shadowPict ? w->shadowPict : transBlackPicture,
1040 w->picture, rootBuffer,
1042 w->a.x + w->shadow_dx,
1043 w->a.y + w->shadow_dy,
1044 w->shadow_width, w->shadow_height);
1046 case CompClientShadows:
1049 XRenderComposite (dpy, PictOpOver, blackPicture, w->shadow, rootBuffer,
1051 w->a.x + w->shadow_dx,
1052 w->a.y + w->shadow_dy,
1053 w->shadow_width, w->shadow_height);
1057 if (w->opacity != OPAQUE && !w->alphaPict)
1058 w->alphaPict = solid_picture (dpy, False,
1059 (double) w->opacity / OPAQUE, 0, 0, 0);
1060 if (w->mode == WINDOW_TRANS)
1063 #if HAS_NAME_WINDOW_PIXMAP
1066 wid = w->a.width + w->a.border_width * 2;
1067 hei = w->a.height + w->a.border_width * 2;
1069 x = w->a.x + w->a.border_width;
1070 y = w->a.y + w->a.border_width;
1074 set_ignore (dpy, NextRequest (dpy));
1075 XRenderComposite (dpy, PictOpOver, w->picture, w->alphaPict, rootBuffer,
1079 else if (w->mode == WINDOW_ARGB)
1082 #if HAS_NAME_WINDOW_PIXMAP
1085 wid = w->a.width + w->a.border_width * 2;
1086 hei = w->a.height + w->a.border_width * 2;
1088 x = w->a.x + w->a.border_width;
1089 y = w->a.y + w->a.border_width;
1093 set_ignore (dpy, NextRequest (dpy));
1094 XRenderComposite (dpy, PictOpOver, w->picture, w->alphaPict, rootBuffer,
1098 XFixesDestroyRegion (dpy, w->borderClip);
1099 w->borderClip = None;
1101 XFixesDestroyRegion (dpy, region);
1102 if (rootBuffer != rootPicture)
1104 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, None);
1105 XRenderComposite (dpy, PictOpSrc, rootBuffer, None, rootPicture,
1106 0, 0, 0, 0, 0, 0, root_width, root_height);
1111 add_damage (Display *dpy, XserverRegion damage)
1115 XFixesUnionRegion (dpy, allDamage, allDamage, damage);
1116 XFixesDestroyRegion (dpy, damage);
1123 repair_win (Display *dpy, win *w)
1125 XserverRegion parts;
1129 parts = win_extents (dpy, w);
1130 set_ignore (dpy, NextRequest (dpy));
1131 XDamageSubtract (dpy, w->damage, None, None);
1136 parts = XFixesCreateRegion (dpy, 0, 0);
1137 set_ignore (dpy, NextRequest (dpy));
1138 XDamageSubtract (dpy, w->damage, None, parts);
1139 XFixesTranslateRegion (dpy, parts,
1140 w->a.x + w->a.border_width,
1141 w->a.y + w->a.border_width);
1142 if (compMode == CompServerShadows)
1144 o = XFixesCreateRegion (dpy, 0, 0);
1145 XFixesCopyRegion (dpy, o, parts);
1146 XFixesTranslateRegion (dpy, o, w->shadow_dx, w->shadow_dy);
1147 XFixesUnionRegion (dpy, parts, parts, o);
1148 XFixesDestroyRegion (dpy, o);
1151 add_damage (dpy, parts);
1156 map_win (Display *dpy, Window id, unsigned long sequence, Bool fade)
1158 win *w = find_win (dpy, id);
1164 w->a.map_state = IsViewable;
1166 /* This needs to be here or else we lose transparency messages */
1167 XSelectInput (dpy, id, PropertyChangeMask);
1170 w->damage_bounds.x = w->damage_bounds.y = 0;
1171 w->damage_bounds.width = w->damage_bounds.height = 0;
1175 if (fade && fadeWindows)
1176 set_fade (dpy, w, 0, get_opacity_percent (dpy, w, 1.0), fade_in_step, 0, False, True, True);
1180 finish_unmap_win (Display *dpy, win *w)
1186 if (w->extents != None)
1188 add_damage (dpy, w->extents); /* destroys region */
1192 #if HAS_NAME_WINDOW_PIXMAP
1195 XFreePixmap (dpy, w->pixmap);
1202 set_ignore (dpy, NextRequest (dpy));
1203 XRenderFreePicture (dpy, w->picture);
1207 /* don't care about properties anymore */
1208 set_ignore (dpy, NextRequest (dpy));
1209 XSelectInput(dpy, w->id, 0);
1213 set_ignore (dpy, NextRequest (dpy));
1214 XFixesDestroyRegion (dpy, w->borderSize);
1215 w->borderSize = None;
1219 XRenderFreePicture (dpy, w->shadow);
1224 XFixesDestroyRegion (dpy, w->borderClip);
1225 w->borderClip = None;
1231 #if HAS_NAME_WINDOW_PIXMAP
1233 unmap_callback (Display *dpy, win *w, Bool gone)
1235 finish_unmap_win (dpy, w);
1240 unmap_win (Display *dpy, Window id, Bool fade)
1242 win *w = find_win (dpy, id);
1245 w->a.map_state = IsUnmapped;
1246 #if HAS_NAME_WINDOW_PIXMAP
1247 if (w->pixmap && fade && fadeWindows)
1248 set_fade (dpy, w, w->opacity*1.0/OPAQUE, 0.0, fade_out_step, unmap_callback, False, False, True);
1251 finish_unmap_win (dpy, w);
1254 /* Get the opacity prop from window
1259 get_opacity_prop(Display *dpy, win *w, unsigned int def)
1263 unsigned long n, left;
1265 unsigned char *data;
1266 int result = XGetWindowProperty(dpy, w->id, opacityAtom, 0L, 1L, False,
1267 XA_CARDINAL, &actual, &format,
1269 if (result == Success && data != NULL)
1272 memcpy (&i, data, sizeof (unsigned int));
1273 XFree( (void *) data);
1279 /* Get the opacity property from the window in a percent format
1281 otherwise: the value
1284 get_opacity_percent(Display *dpy, win *w, double def)
1286 unsigned int opacity = get_opacity_prop (dpy, w, (unsigned int)(OPAQUE*def));
1288 return opacity*1.0/OPAQUE;
1291 /* determine mode for window all in one place.
1292 Future might check for menu flag and other cool things
1296 get_wintype_prop(Display * dpy, Window w)
1300 unsigned long n, left;
1302 unsigned char *data;
1303 int result = XGetWindowProperty (dpy, w, winTypeAtom, 0L, 1L, False,
1304 XA_ATOM, &actual, &format,
1307 if (result == Success && data != None)
1310 memcpy (&a, data, sizeof (Atom));
1311 XFree ( (void *) data);
1314 return winNormalAtom;
1318 determine_mode(Display *dpy, win *w)
1321 XRenderPictFormat *format;
1322 unsigned int default_opacity;
1324 /* if trans prop == -1 fall back on previous tests*/
1328 XRenderFreePicture (dpy, w->alphaPict);
1329 w->alphaPict = None;
1333 XRenderFreePicture (dpy, w->shadowPict);
1334 w->shadowPict = None;
1337 if (w->a.class == InputOnly)
1343 format = XRenderFindVisualFormat (dpy, w->a.visual);
1346 if (format && format->type == PictTypeDirect && format->direct.alphaMask)
1350 else if (w->opacity != OPAQUE)
1352 mode = WINDOW_TRANS;
1356 mode = WINDOW_SOLID;
1361 XserverRegion damage;
1362 damage = XFixesCreateRegion (dpy, 0, 0);
1363 XFixesCopyRegion (dpy, damage, w->extents);
1364 add_damage (dpy, damage);
1369 determine_wintype (Display *dpy, Window w)
1371 Window root_return, parent_return;
1372 Window *children = NULL;
1373 unsigned int nchildren, i;
1376 type = get_wintype_prop (dpy, w);
1377 if (type != winNormalAtom)
1380 if (!XQueryTree (dpy, w, &root_return, &parent_return, &children,
1383 /* XQueryTree failed. */
1385 XFree ((void *)children);
1386 return winNormalAtom;
1389 for (i = 0;i < nchildren;i++)
1391 type = determine_wintype (dpy, children[i]);
1392 if (type != winNormalAtom)
1397 XFree ((void *)children);
1399 return winNormalAtom;
1403 add_win (Display *dpy, Window id, Window prev)
1405 win *new = malloc (sizeof (win));
1412 for (p = &list; *p; p = &(*p)->next)
1413 if ((*p)->id == prev)
1419 set_ignore (dpy, NextRequest (dpy));
1420 if (!XGetWindowAttributes (dpy, id, &new->a))
1427 new->usable = False;
1429 #if HAS_NAME_WINDOW_PIXMAP
1432 new->picture = None;
1433 if (new->a.class == InputOnly)
1435 new->damage_sequence = 0;
1440 new->damage_sequence = NextRequest (dpy);
1441 new->damage = XDamageCreate (dpy, id, XDamageReportNonEmpty);
1443 new->alphaPict = None;
1444 new->shadowPict = None;
1445 new->borderSize = None;
1446 new->extents = None;
1450 new->shadow_width = 0;
1451 new->shadow_height = 0;
1452 new->opacity = OPAQUE;
1454 new->borderClip = None;
1455 new->prev_trans = 0;
1457 /* moved mode setting to one place */
1458 new->opacity = get_opacity_prop (dpy, new, OPAQUE);
1459 new->windowType = determine_wintype (dpy, new->id);
1460 determine_mode (dpy, new);
1464 if (new->a.map_state == IsViewable)
1465 map_win (dpy, id, new->damage_sequence - 1, True);
1469 restack_win (Display *dpy, win *w, Window new_above)
1474 old_above = w->next->id;
1477 if (old_above != new_above)
1482 for (prev = &list; *prev; prev = &(*prev)->next)
1488 for (prev = &list; *prev; prev = &(*prev)->next)
1490 if ((*prev)->id == new_above)
1499 configure_win (Display *dpy, XConfigureEvent *ce)
1501 win *w = find_win (dpy, ce->window);
1503 XserverRegion damage = None;
1507 if (ce->window == root)
1511 XRenderFreePicture (dpy, rootBuffer);
1514 root_width = ce->width;
1515 root_height = ce->height;
1523 damage = XFixesCreateRegion (dpy, 0, 0);
1524 if (w->extents != None)
1525 XFixesCopyRegion (dpy, damage, w->extents);
1529 if (w->a.width != ce->width || w->a.height != ce->height)
1531 #if HAS_NAME_WINDOW_PIXMAP
1534 XFreePixmap (dpy, w->pixmap);
1538 XRenderFreePicture (dpy, w->picture);
1545 XRenderFreePicture (dpy, w->shadow);
1549 w->a.width = ce->width;
1550 w->a.height = ce->height;
1551 w->a.border_width = ce->border_width;
1552 w->a.override_redirect = ce->override_redirect;
1553 restack_win (dpy, w, ce->above);
1556 XserverRegion extents = win_extents (dpy, w);
1557 XFixesUnionRegion (dpy, damage, damage, extents);
1558 XFixesDestroyRegion (dpy, extents);
1559 add_damage (dpy, damage);
1565 circulate_win (Display *dpy, XCirculateEvent *ce)
1567 win *w = find_win (dpy, ce->window);
1573 if (ce->place == PlaceOnTop)
1574 new_above = list->id;
1577 restack_win (dpy, w, new_above);
1582 finish_destroy_win (Display *dpy, Window id, Bool gone)
1586 for (prev = &list; (w = *prev); prev = &w->next)
1590 finish_unmap_win (dpy, w);
1594 set_ignore (dpy, NextRequest (dpy));
1595 XRenderFreePicture (dpy, w->picture);
1600 XRenderFreePicture (dpy, w->alphaPict);
1601 w->alphaPict = None;
1605 XRenderFreePicture (dpy, w->shadowPict);
1606 w->shadowPict = None;
1608 if (w->damage != None)
1610 set_ignore (dpy, NextRequest (dpy));
1611 XDamageDestroy (dpy, w->damage);
1614 cleanup_fade (dpy, w);
1620 #if HAS_NAME_WINDOW_PIXMAP
1622 destroy_callback (Display *dpy, win *w, Bool gone)
1624 finish_destroy_win (dpy, w->id, gone);
1629 destroy_win (Display *dpy, Window id, Bool gone, Bool fade)
1631 win *w = find_win (dpy, id);
1632 #if HAS_NAME_WINDOW_PIXMAP
1633 if (w && w->pixmap && fade && fadeWindows)
1634 set_fade (dpy, w, w->opacity*1.0/OPAQUE, 0.0, fade_out_step, destroy_callback, gone, False, True);
1638 finish_destroy_win (dpy, id, gone);
1646 printf ("\t%08lx: %d x %d + %d + %d (%d)\n", w->id,
1647 w->a.width, w->a.height, w->a.x, w->a.y, w->a.border_width);
1656 printf ("windows:\n");
1657 for (w = list; w; w = w->next)
1663 damage_win (Display *dpy, XDamageNotifyEvent *de)
1665 win *w = find_win (dpy, de->drawable);
1672 if (w->damage_bounds.width == 0 || w->damage_bounds.height == 0)
1674 w->damage_bounds = de->area;
1678 if (de->area.x < w->damage_bounds.x)
1680 w->damage_bounds.width += (w->damage_bounds.x - de->area.x);
1681 w->damage_bounds.x = de->area.x;
1683 if (de->area.y < w->damage_bounds.y)
1685 w->damage_bounds.height += (w->damage_bounds.y - de->area.y);
1686 w->damage_bounds.y = de->area.y;
1688 if (de->area.x + de->area.width > w->damage_bounds.x + w->damage_bounds.width)
1689 w->damage_bounds.width = de->area.x + de->area.width - w->damage_bounds.x;
1690 if (de->area.y + de->area.height > w->damage_bounds.y + w->damage_bounds.height)
1691 w->damage_bounds.height = de->area.y + de->area.height - w->damage_bounds.y;
1694 printf ("unusable damage %d, %d: %d x %d bounds %d, %d: %d x %d\n",
1701 w->damage_bounds.width,
1702 w->damage_bounds.height);
1704 if (w->damage_bounds.x <= 0 &&
1705 w->damage_bounds.y <= 0 &&
1706 w->a.width <= w->damage_bounds.x + w->damage_bounds.width &&
1707 w->a.height <= w->damage_bounds.y + w->damage_bounds.height)
1711 set_fade (dpy, w, 0, get_opacity_percent (dpy, w, 1.0), fade_in_step, 0, False, True, True);
1717 repair_win (dpy, w);
1721 error (Display *dpy, XErrorEvent *ev)
1726 if (should_ignore (dpy, ev->serial))
1729 if (ev->request_code == composite_opcode &&
1730 ev->minor_code == X_CompositeRedirectSubwindows)
1732 fprintf (stderr, "Another composite manager is already running\n");
1736 o = ev->error_code - xfixes_error;
1738 case BadRegion: name = "BadRegion"; break;
1741 o = ev->error_code - damage_error;
1743 case BadDamage: name = "BadDamage"; break;
1746 o = ev->error_code - render_error;
1748 case BadPictFormat: name ="BadPictFormat"; break;
1749 case BadPicture: name ="BadPicture"; break;
1750 case BadPictOp: name ="BadPictOp"; break;
1751 case BadGlyphSet: name ="BadGlyphSet"; break;
1752 case BadGlyph: name ="BadGlyph"; break;
1756 printf ("error %d request %d minor %d serial %d\n",
1757 ev->error_code, ev->request_code, ev->minor_code, ev->serial);
1759 /* abort (); this is just annoying to most people */
1764 expose_root (Display *dpy, Window root, XRectangle *rects, int nrects)
1766 XserverRegion region = XFixesCreateRegion (dpy, rects, nrects);
1768 add_damage (dpy, region);
1773 ev_serial (XEvent *ev)
1775 if (ev->type & 0x7f != KeymapNotify)
1776 return ev->xany.serial;
1777 return NextRequest (ev->xany.display);
1782 ev_name (XEvent *ev)
1784 static char buf[128];
1785 switch (ev->type & 0x7f) {
1792 case ReparentNotify:
1794 case CirculateNotify:
1797 if (ev->type == damage_event + XDamageNotify)
1799 sprintf (buf, "Event %d", ev->type);
1805 ev_window (XEvent *ev)
1809 return ev->xexpose.window;
1811 return ev->xmap.window;
1813 return ev->xunmap.window;
1814 case ReparentNotify:
1815 return ev->xreparent.window;
1816 case CirculateNotify:
1817 return ev->xcirculate.window;
1819 if (ev->type == damage_event + XDamageNotify)
1820 return ((XDamageNotifyEvent *) ev)->drawable;
1826 usage (char *program)
1828 fprintf (stderr, "%s v1.0\n", program);
1829 fprintf (stderr, "usage: %s [options]\n", program);
1830 fprintf (stderr, "Options\n");
1831 fprintf (stderr, " -d display\n Specifies which display should be managed.\n");
1832 fprintf (stderr, " -r radius\n Specifies the blur radius for client-side shadows. (default 12)\n");
1833 fprintf (stderr, " -o opacity\n Specifies the translucency for client-side shadows. (default .75)\n");
1834 fprintf (stderr, " -l left-offset\n Specifies the left offset for client-side shadows. (default -15)\n");
1835 fprintf (stderr, " -t top-offset\n Specifies the top offset for clinet-side shadows. (default -15)\n");
1836 fprintf (stderr, " -a\n Use automatic server-side compositing. Faster, but no special effects.\n");
1837 fprintf (stderr, " -c\n Draw client-side shadows with fuzzy edges.\n");
1838 fprintf (stderr, " -C\n Avoid drawing shadows on dock/panel windows.\n");
1839 fprintf (stderr, " -f\n Fade windows in/out when opening/closing.\n");
1840 fprintf (stderr, " -F\n Fade windows during opacity changes.\n");
1841 fprintf (stderr, " -n\n Normal client-side compositing with transparency support\n");
1842 fprintf (stderr, " -s\n Draw server-side shadows with sharp edges.\n");
1843 fprintf (stderr, " -S\n Enable synchronous operation (for debugging).\n");
1848 main (int argc, char **argv)
1851 Window root_return, parent_return;
1855 unsigned int nchildren;
1857 XRenderPictureAttributes pa;
1859 XRectangle *expose_rects = 0;
1860 int size_expose = 0;
1867 int composite_major, composite_minor;
1871 while ((o = getopt (argc, argv, "d:r:o:l:t:scnfFCaS")) != -1)
1878 compMode = CompServerShadows;
1881 compMode = CompClientShadows;
1884 excludeDockShadows = True;
1887 compMode = CompSimple;
1896 autoRedirect = True;
1902 shadowRadius = atoi (optarg);
1905 shadowOpacity = atof (optarg);
1908 shadowOffsetX = atoi (optarg);
1911 shadowOffsetY = atoi (optarg);
1919 dpy = XOpenDisplay (display);
1922 fprintf (stderr, "Can't open display\n");
1925 XSetErrorHandler (error);
1927 XSynchronize (dpy, 1);
1928 scr = DefaultScreen (dpy);
1929 root = RootWindow (dpy, scr);
1931 if (!XRenderQueryExtension (dpy, &render_event, &render_error))
1933 fprintf (stderr, "No render extension\n");
1936 if (!XQueryExtension (dpy, COMPOSITE_NAME, &composite_opcode,
1937 &composite_event, &composite_error))
1939 fprintf (stderr, "No composite extension\n");
1942 XCompositeQueryVersion (dpy, &composite_major, &composite_minor);
1943 #if HAS_NAME_WINDOW_PIXMAP
1944 if (composite_major > 0 || composite_minor >= 2)
1945 hasNamePixmap = True;
1948 if (!XDamageQueryExtension (dpy, &damage_event, &damage_error))
1950 fprintf (stderr, "No damage extension\n");
1953 if (!XFixesQueryExtension (dpy, &xfixes_event, &xfixes_error))
1955 fprintf (stderr, "No XFixes extension\n");
1959 opacityAtom = XInternAtom (dpy, OPACITY_PROP, False);
1960 winTypeAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE", False);
1961 winDesktopAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DESKTOP", False);
1962 winDockAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DOCK", False);
1963 winToolbarAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_TOOLBAR", False);
1964 winMenuAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_MENU", False);
1965 winUtilAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_UTILITY", False);
1966 winSplashAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_SPLASH", False);
1967 winDialogAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1968 winNormalAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_NORMAL", False);
1970 pa.subwindow_mode = IncludeInferiors;
1972 if (compMode == CompClientShadows)
1974 gaussianMap = make_gaussian_map(dpy, shadowRadius);
1975 presum_gaussian (gaussianMap);
1978 root_width = DisplayWidth (dpy, scr);
1979 root_height = DisplayHeight (dpy, scr);
1981 rootPicture = XRenderCreatePicture (dpy, root,
1982 XRenderFindVisualFormat (dpy,
1983 DefaultVisual (dpy, scr)),
1986 blackPicture = solid_picture (dpy, True, 1, 0, 0, 0);
1987 if (compMode == CompServerShadows)
1988 transBlackPicture = solid_picture (dpy, True, 0.3, 0, 0, 0);
1993 XCompositeRedirectSubwindows (dpy, root, CompositeRedirectAutomatic);
1996 XCompositeRedirectSubwindows (dpy, root, CompositeRedirectManual);
1997 XSelectInput (dpy, root,
1998 SubstructureNotifyMask|
2000 StructureNotifyMask|
2001 PropertyChangeMask);
2002 XQueryTree (dpy, root, &root_return, &parent_return, &children, &nchildren);
2003 for (i = 0; i < nchildren; i++)
2004 add_win (dpy, children[i], i ? children[i-1] : None);
2007 XUngrabServer (dpy);
2008 ufd.fd = ConnectionNumber (dpy);
2009 ufd.events = POLLIN;
2011 paint_all (dpy, None);
2020 if (poll (&ufd, 1, fade_timeout()) == 0)
2027 XNextEvent (dpy, &ev);
2028 if (ev.type & 0x7f != KeymapNotify)
2029 discard_ignore (dpy, ev.xany.serial);
2031 printf ("event %10.10s serial 0x%08x window 0x%08x\n",
2032 ev_name(&ev), ev_serial (&ev), ev_window (&ev));
2034 if (!autoRedirect) switch (ev.type) {
2036 add_win (dpy, ev.xcreatewindow.window, 0);
2038 case ConfigureNotify:
2039 configure_win (dpy, &ev.xconfigure);
2042 destroy_win (dpy, ev.xdestroywindow.window, True, True);
2045 map_win (dpy, ev.xmap.window, ev.xmap.serial, True);
2048 unmap_win (dpy, ev.xunmap.window, True);
2050 case ReparentNotify:
2051 if (ev.xreparent.parent == root)
2052 add_win (dpy, ev.xreparent.window, 0);
2054 destroy_win (dpy, ev.xreparent.window, False, True);
2056 case CirculateNotify:
2057 circulate_win (dpy, &ev.xcirculate);
2060 if (ev.xexpose.window == root)
2062 int more = ev.xexpose.count + 1;
2063 if (n_expose == size_expose)
2067 expose_rects = realloc (expose_rects,
2068 (size_expose + more) *
2069 sizeof (XRectangle));
2070 size_expose += more;
2074 expose_rects = malloc (more * sizeof (XRectangle));
2078 expose_rects[n_expose].x = ev.xexpose.x;
2079 expose_rects[n_expose].y = ev.xexpose.y;
2080 expose_rects[n_expose].width = ev.xexpose.width;
2081 expose_rects[n_expose].height = ev.xexpose.height;
2083 if (ev.xexpose.count == 0)
2085 expose_root (dpy, root, expose_rects, n_expose);
2090 case PropertyNotify:
2091 for (p = 0; backgroundProps[p]; p++)
2093 if (ev.xproperty.atom == XInternAtom (dpy, backgroundProps[p], False))
2097 XClearArea (dpy, root, 0, 0, 0, 0, True);
2098 XRenderFreePicture (dpy, rootTile);
2104 /* check if Trans property was changed */
2105 if (ev.xproperty.atom == opacityAtom)
2107 /* reset mode and redraw window */
2108 win * w = find_win(dpy, ev.xproperty.window);
2112 set_fade (dpy, w, w->opacity*1.0/OPAQUE, get_opacity_percent (dpy, w, 1.0),
2113 fade_out_step, 0, False, True, False);
2116 w->opacity = get_opacity_prop(dpy, w, OPAQUE);
2117 determine_mode(dpy, w);
2120 XRenderFreePicture (dpy, w->shadow);
2122 w->extents = win_extents (dpy, w);
2129 if (ev.type == damage_event + XDamageNotify)
2130 damage_win (dpy, (XDamageNotifyEvent *) &ev);
2133 } while (QLength (dpy));
2134 if (allDamage && !autoRedirect)
2137 paint_all (dpy, allDamage);
2141 clipChanged = False;