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;
196 get_time_in_milliseconds ()
200 gettimeofday (&tv, NULL);
201 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
209 for (f = fades; f; f = f->next)
218 dequeue_fade (Display *dpy, fade *f)
222 for (prev = &fades; *prev; prev = &(*prev)->next)
227 (*f->callback) (dpy, f->w, f->gone);
234 cleanup_fade (Display *dpy, win *w)
236 fade *f = find_fade (w);
238 dequeue_fade (dpy, f);
242 enqueue_fade (Display *dpy, fade *f)
245 fade_time = get_time_in_milliseconds () + fade_delta;
251 set_fade (Display *dpy, win *w, double start, double finish, double step,
252 void (*callback) (Display *dpy, win *w, Bool gone),
253 Bool gone, Bool exec_callback, Bool override)
260 f = malloc (sizeof (fade));
264 enqueue_fade (dpy, f);
272 (*f->callback)(dpy, f->w, f->gone);
282 else if (f->cur > finish)
284 f->callback = callback;
286 w->opacity = f->cur * OPAQUE;
288 printf ("set_fade start %g step %g\n", f->cur, f->step);
290 determine_mode (dpy, w);
293 XRenderFreePicture (dpy, w->shadow);
295 w->extents = win_extents (dpy, w);
306 now = get_time_in_milliseconds();
307 delta = fade_time - now;
310 /* printf ("timeout %d\n", delta); */
315 run_fades (Display *dpy)
317 int now = get_time_in_milliseconds();
322 printf ("run fades\n");
324 if (fade_time - now > 0)
326 steps = 1 + (now - fade_time) / fade_delta;
327 for (next = fades; f = next; )
331 f->cur += f->step * steps;
337 printf ("opacity now %g\n", f->cur);
339 w->opacity = f->cur * OPAQUE;
342 if (f->cur >= f->finish)
344 w->opacity = f->finish*OPAQUE;
345 dequeue_fade (dpy, f);
350 if (f->cur <= f->finish)
352 w->opacity = f->finish*OPAQUE;
353 dequeue_fade (dpy, f);
356 determine_mode (dpy, w);
359 XRenderFreePicture (dpy, w->shadow);
361 w->extents = win_extents(dpy, w);
364 fade_time = now + fade_delta;
368 gaussian (double r, double x, double y)
370 return ((1 / (sqrt (2 * M_PI * r))) *
371 exp ((- (x * x + y * y)) / (2 * r * r)));
376 make_gaussian_map (Display *dpy, double r)
379 int size = ((int) ceil ((r * 3)) + 1) & ~1;
380 int center = size / 2;
385 c = malloc (sizeof (conv) + size * size * sizeof (double));
387 c->data = (double *) (c + 1);
389 for (y = 0; y < size; y++)
390 for (x = 0; x < size; x++)
392 g = gaussian (r, (double) (x - center), (double) (y - center));
394 c->data[y * size + x] = g;
396 /* printf ("gaussian total %f\n", t); */
397 for (y = 0; y < size; y++)
398 for (x = 0; x < size; x++)
400 c->data[y*size + x] /= t;
406 * A picture will help
408 * -center 0 width width+center
409 * -center +-----+-------------------+-----+
412 * 0 +-----+-------------------+-----+
416 * height +-----+-------------------+-----+
419 * center +-----+-------------------+-----+
423 sum_gaussian (conv *map, double opacity, int x, int y, int width, int height)
427 double *g_line = map->data;
428 int g_size = map->size;
429 int center = g_size / 2;
430 int fx_start, fx_end;
431 int fy_start, fy_end;
435 * Compute set of filter values which are "in range",
436 * that's the set with:
437 * 0 <= x + (fx-center) && x + (fx-center) < width &&
438 * 0 <= y + (fy-center) && y + (fy-center) < height
440 * 0 <= x + (fx - center) x + fx - center < width
441 * center - x <= fx fx < width + center - x
444 fx_start = center - x;
447 fx_end = width + center - x;
451 fy_start = center - y;
454 fy_end = height + center - y;
458 g_line = g_line + fy_start * g_size + fx_start;
461 for (fy = fy_start; fy < fy_end; fy++)
466 for (fx = fx_start; fx < fx_end; fx++)
472 return ((unsigned char) (v * opacity * 255.0));
476 make_shadow (Display *dpy, double opacity, int width, int height)
480 int gsize = gaussianMap->size;
482 int swidth = width + gsize;
483 int sheight = height + gsize;
484 int center = gsize / 2;
489 data = malloc (swidth * sheight * sizeof (unsigned char));
492 ximage = XCreateImage (dpy,
493 DefaultVisual(dpy, DefaultScreen(dpy)),
498 swidth, sheight, 8, swidth * sizeof (unsigned char));
505 * Build the gaussian in sections
509 * center (fill the complete data array)
512 d = sum_gaussian (gaussianMap, opacity, center, center, width, height);
513 memset(data, d, sheight * swidth);
519 if (ylimit > sheight / 2)
520 ylimit = (sheight + 1) / 2;
522 if (xlimit > swidth / 2)
523 xlimit = (swidth + 1) / 2;
525 for (y = 0; y < ylimit; y++)
526 for (x = 0; x < xlimit; x++)
528 d = sum_gaussian (gaussianMap, opacity, x - center, y - center, width, height);
529 data[y * swidth + x] = d;
530 data[(sheight - y - 1) * swidth + x] = d;
531 data[(sheight - y - 1) * swidth + (swidth - x - 1)] = d;
532 data[y * swidth + (swidth - x - 1)] = d;
538 x_diff = swidth - (gsize * 2);
539 if (x_diff > 0 && ylimit > 0)
541 for (y = 0; y < ylimit; y++)
543 d = sum_gaussian (gaussianMap, opacity, center, y - center, width, height);
544 memset (&data[y * swidth + gsize], d, x_diff);
545 memset (&data[(sheight - y - 1) * swidth + gsize], d, x_diff);
553 for (x = 0; x < xlimit; x++)
555 d = sum_gaussian (gaussianMap, opacity, x - center, center, width, height);
556 for (y = gsize; y < sheight - gsize; y++)
558 data[y * swidth + x] = d;
559 data[y * swidth + (swidth - x - 1)] = d;
567 shadow_picture (Display *dpy, double opacity, Picture alpha_pict, int width, int height, int *wp, int *hp)
572 Picture shadowPicture;
573 Picture finalPicture;
576 shadowImage = make_shadow (dpy, opacity, width, height);
579 shadowPixmap = XCreatePixmap (dpy, root,
585 XDestroyImage (shadowImage);
589 shadowPicture = XRenderCreatePicture (dpy, shadowPixmap,
590 XRenderFindStandardFormat (dpy, PictStandardA8),
594 XDestroyImage (shadowImage);
595 XFreePixmap (dpy, shadowPixmap);
599 gc = XCreateGC (dpy, shadowPixmap, 0, 0);
602 XDestroyImage (shadowImage);
603 XFreePixmap (dpy, shadowPixmap);
604 XRenderFreePicture (dpy, shadowPicture);
608 XPutImage (dpy, shadowPixmap, gc, shadowImage, 0, 0, 0, 0,
610 shadowImage->height);
611 *wp = shadowImage->width;
612 *hp = shadowImage->height;
614 XDestroyImage (shadowImage);
615 XFreePixmap (dpy, shadowPixmap);
616 return shadowPicture;
620 solid_picture (Display *dpy, Bool argb, double a, double r, double g, double b)
624 XRenderPictureAttributes pa;
627 pixmap = XCreatePixmap (dpy, root, 1, 1, argb ? 32 : 8);
629 picture = XRenderCreatePicture (dpy, pixmap,
630 XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8),
633 c.alpha = a * 0xffff;
635 c.green = g * 0xffff;
637 XRenderFillRectangle (dpy, PictOpSrc, picture, &c, 0, 0, 1, 1);
638 XFreePixmap (dpy, pixmap);
643 discard_ignore (Display *dpy, unsigned long sequence)
647 if ((long) (sequence - ignore_head->sequence) > 0)
649 ignore *next = ignore_head->next;
653 ignore_tail = &ignore_head;
661 set_ignore (Display *dpy, unsigned long sequence)
663 ignore *i = malloc (sizeof (ignore));
666 i->sequence = sequence;
669 ignore_tail = &i->next;
673 should_ignore (Display *dpy, unsigned long sequence)
675 discard_ignore (dpy, sequence);
676 return ignore_head && ignore_head->sequence == sequence;
680 find_win (Display *dpy, Window id)
684 for (w = list; w; w = w->next)
690 static char *backgroundProps[] = {
697 root_tile (Display *dpy)
703 unsigned long nitems;
704 unsigned long bytes_after;
707 XRenderPictureAttributes pa;
711 for (p = 0; backgroundProps[p]; p++)
713 if (XGetWindowProperty (dpy, root, XInternAtom (dpy, backgroundProps[p], False),
714 0, 4, False, AnyPropertyType,
715 &actual_type, &actual_format, &nitems, &bytes_after, &prop) == Success &&
716 actual_type == XInternAtom (dpy, "PIXMAP", False) && actual_format == 32 && nitems == 1)
718 memcpy (&pixmap, prop, 4);
726 pixmap = XCreatePixmap (dpy, root, 1, 1, DefaultDepth (dpy, scr));
730 picture = XRenderCreatePicture (dpy, pixmap,
731 XRenderFindVisualFormat (dpy,
732 DefaultVisual (dpy, scr)),
738 c.red = c.green = c.blue = 0x8080;
740 XRenderFillRectangle (dpy, PictOpSrc, picture, &c,
747 paint_root (Display *dpy)
750 rootTile = root_tile (dpy);
752 XRenderComposite (dpy, PictOpSrc,
753 rootTile, None, rootBuffer,
754 0, 0, 0, 0, 0, 0, root_width, root_height);
758 win_extents (Display *dpy, win *w)
764 r.width = w->a.width + w->a.border_width * 2;
765 r.height = w->a.height + w->a.border_width * 2;
766 if (compMode != CompSimple && !(w->windowType == winDockAtom && excludeDockShadows))
768 if (compMode == CompServerShadows || w->mode != WINDOW_ARGB)
772 if (compMode == CompServerShadows)
776 w->shadow_width = w->a.width;
777 w->shadow_height = w->a.height;
781 w->shadow_dx = shadowOffsetX;
782 w->shadow_dy = shadowOffsetY;
785 double opacity = shadowOpacity;
786 if (w->mode == WINDOW_TRANS)
787 opacity = opacity * ((double)w->opacity)/((double)OPAQUE);
788 w->shadow = shadow_picture (dpy, opacity, w->alphaPict,
789 w->a.width + w->a.border_width * 2,
790 w->a.height + w->a.border_width * 2,
791 &w->shadow_width, &w->shadow_height);
794 sr.x = w->a.x + w->shadow_dx;
795 sr.y = w->a.y + w->shadow_dy;
796 sr.width = w->shadow_width;
797 sr.height = w->shadow_height;
800 r.width = (r.x + r.width) - sr.x;
805 r.height = (r.y + r.height) - sr.y;
808 if (sr.x + sr.width > r.x + r.width)
809 r.width = sr.x + sr.width - r.x;
810 if (sr.y + sr.height > r.y + r.height)
811 r.height = sr.y + sr.height - r.y;
814 return XFixesCreateRegion (dpy, &r, 1);
818 border_size (Display *dpy, win *w)
820 XserverRegion border;
822 * if window doesn't exist anymore, this will generate an error
823 * as well as not generate a region. Perhaps a better XFixes
824 * architecture would be to have a request that copies instead
825 * of creates, that way you'd just end up with an empty region
826 * instead of an invalid XID.
828 set_ignore (dpy, NextRequest (dpy));
829 border = XFixesCreateRegionFromWindow (dpy, w->id, WindowRegionBounding);
831 set_ignore (dpy, NextRequest (dpy));
832 XFixesTranslateRegion (dpy, border,
833 w->a.x + w->a.border_width,
834 w->a.y + w->a.border_width);
839 paint_all (Display *dpy, XserverRegion region)
849 r.width = root_width;
850 r.height = root_height;
851 region = XFixesCreateRegion (dpy, &r, 1);
854 rootBuffer = rootPicture;
858 Pixmap rootPixmap = XCreatePixmap (dpy, root, root_width, root_height,
859 DefaultDepth (dpy, scr));
860 rootBuffer = XRenderCreatePicture (dpy, rootPixmap,
861 XRenderFindVisualFormat (dpy,
862 DefaultVisual (dpy, scr)),
864 XFreePixmap (dpy, rootPixmap);
867 XFixesSetPictureClipRegion (dpy, rootPicture, 0, 0, region);
869 XRenderComposite (dpy, PictOpSrc, blackPicture, None, rootPicture,
870 0, 0, 0, 0, 0, 0, root_width, root_height);
875 for (w = list; w; w = w->next)
881 /* never painted, ignore it */
886 XRenderPictureAttributes pa;
887 XRenderPictFormat *format;
888 Drawable draw = w->id;
890 #if HAS_NAME_WINDOW_PIXMAP
891 if (hasNamePixmap && !w->pixmap)
892 w->pixmap = XCompositeNameWindowPixmap (dpy, w->id);
896 format = XRenderFindVisualFormat (dpy, w->a.visual);
897 pa.subwindow_mode = IncludeInferiors;
898 w->picture = XRenderCreatePicture (dpy, draw,
904 printf (" 0x%x", w->id);
910 set_ignore (dpy, NextRequest (dpy));
911 XFixesDestroyRegion (dpy, w->borderSize);
912 w->borderSize = None;
916 XFixesDestroyRegion (dpy, w->extents);
921 XFixesDestroyRegion (dpy, w->borderClip);
922 w->borderClip = None;
926 w->borderSize = border_size (dpy, w);
928 w->extents = win_extents (dpy, w);
929 if (w->mode == WINDOW_SOLID)
932 #if HAS_NAME_WINDOW_PIXMAP
935 wid = w->a.width + w->a.border_width * 2;
936 hei = w->a.height + w->a.border_width * 2;
938 x = w->a.x + w->a.border_width;
939 y = w->a.y + w->a.border_width;
943 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
944 set_ignore (dpy, NextRequest (dpy));
945 XFixesSubtractRegion (dpy, region, region, w->borderSize);
946 set_ignore (dpy, NextRequest (dpy));
947 XRenderComposite (dpy, PictOpSrc, w->picture, None, rootBuffer,
953 w->borderClip = XFixesCreateRegion (dpy, 0, 0);
954 XFixesCopyRegion (dpy, w->borderClip, region);
963 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
965 for (w = t; w; w = w->prev_trans)
967 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, w->borderClip);
971 case CompServerShadows:
972 set_ignore (dpy, NextRequest (dpy));
973 if (w->opacity != OPAQUE && !w->shadowPict)
974 w->shadowPict = solid_picture (dpy, True,
975 (double) w->opacity / OPAQUE * 0.3,
977 XRenderComposite (dpy, PictOpOver,
978 w->shadowPict ? w->shadowPict : transBlackPicture,
979 w->picture, rootBuffer,
981 w->a.x + w->shadow_dx,
982 w->a.y + w->shadow_dy,
983 w->shadow_width, w->shadow_height);
985 case CompClientShadows:
988 XRenderComposite (dpy, PictOpOver, blackPicture, w->shadow, rootBuffer,
990 w->a.x + w->shadow_dx,
991 w->a.y + w->shadow_dy,
992 w->shadow_width, w->shadow_height);
996 if (w->opacity != OPAQUE && !w->alphaPict)
997 w->alphaPict = solid_picture (dpy, False,
998 (double) w->opacity / OPAQUE, 0, 0, 0);
999 if (w->mode == WINDOW_TRANS)
1002 #if HAS_NAME_WINDOW_PIXMAP
1005 wid = w->a.width + w->a.border_width * 2;
1006 hei = w->a.height + w->a.border_width * 2;
1008 x = w->a.x + w->a.border_width;
1009 y = w->a.y + w->a.border_width;
1013 set_ignore (dpy, NextRequest (dpy));
1014 XRenderComposite (dpy, PictOpOver, w->picture, w->alphaPict, rootBuffer,
1018 else if (w->mode == WINDOW_ARGB)
1021 #if HAS_NAME_WINDOW_PIXMAP
1024 wid = w->a.width + w->a.border_width * 2;
1025 hei = w->a.height + w->a.border_width * 2;
1027 x = w->a.x + w->a.border_width;
1028 y = w->a.y + w->a.border_width;
1032 set_ignore (dpy, NextRequest (dpy));
1033 XRenderComposite (dpy, PictOpOver, w->picture, w->alphaPict, rootBuffer,
1037 XFixesDestroyRegion (dpy, w->borderClip);
1038 w->borderClip = None;
1040 XFixesDestroyRegion (dpy, region);
1041 if (rootBuffer != rootPicture)
1043 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, None);
1044 XRenderComposite (dpy, PictOpSrc, rootBuffer, None, rootPicture,
1045 0, 0, 0, 0, 0, 0, root_width, root_height);
1050 add_damage (Display *dpy, XserverRegion damage)
1054 XFixesUnionRegion (dpy, allDamage, allDamage, damage);
1055 XFixesDestroyRegion (dpy, damage);
1062 repair_win (Display *dpy, win *w)
1064 XserverRegion parts;
1068 parts = win_extents (dpy, w);
1069 set_ignore (dpy, NextRequest (dpy));
1070 XDamageSubtract (dpy, w->damage, None, None);
1075 parts = XFixesCreateRegion (dpy, 0, 0);
1076 set_ignore (dpy, NextRequest (dpy));
1077 XDamageSubtract (dpy, w->damage, None, parts);
1078 XFixesTranslateRegion (dpy, parts,
1079 w->a.x + w->a.border_width,
1080 w->a.y + w->a.border_width);
1081 if (compMode == CompServerShadows)
1083 o = XFixesCreateRegion (dpy, 0, 0);
1084 XFixesCopyRegion (dpy, o, parts);
1085 XFixesTranslateRegion (dpy, o, w->shadow_dx, w->shadow_dy);
1086 XFixesUnionRegion (dpy, parts, parts, o);
1087 XFixesDestroyRegion (dpy, o);
1090 add_damage (dpy, parts);
1095 map_win (Display *dpy, Window id, unsigned long sequence, Bool fade)
1097 win *w = find_win (dpy, id);
1103 w->a.map_state = IsViewable;
1105 /* This needs to be here or else we lose transparency messages */
1106 XSelectInput (dpy, id, PropertyChangeMask);
1109 w->damage_bounds.x = w->damage_bounds.y = 0;
1110 w->damage_bounds.width = w->damage_bounds.height = 0;
1114 if (fade && fadeWindows)
1115 set_fade (dpy, w, 0, get_opacity_percent (dpy, w, 1.0), fade_in_step, 0, False, True, True);
1119 finish_unmap_win (Display *dpy, win *w)
1125 if (w->extents != None)
1127 add_damage (dpy, w->extents); /* destroys region */
1131 #if HAS_NAME_WINDOW_PIXMAP
1134 XFreePixmap (dpy, w->pixmap);
1141 set_ignore (dpy, NextRequest (dpy));
1142 XRenderFreePicture (dpy, w->picture);
1146 /* don't care about properties anymore */
1147 set_ignore (dpy, NextRequest (dpy));
1148 XSelectInput(dpy, w->id, 0);
1152 set_ignore (dpy, NextRequest (dpy));
1153 XFixesDestroyRegion (dpy, w->borderSize);
1154 w->borderSize = None;
1158 XRenderFreePicture (dpy, w->shadow);
1163 XFixesDestroyRegion (dpy, w->borderClip);
1164 w->borderClip = None;
1170 #if HAS_NAME_WINDOW_PIXMAP
1172 unmap_callback (Display *dpy, win *w, Bool gone)
1174 finish_unmap_win (dpy, w);
1179 unmap_win (Display *dpy, Window id, Bool fade)
1181 win *w = find_win (dpy, id);
1184 w->a.map_state = IsUnmapped;
1185 #if HAS_NAME_WINDOW_PIXMAP
1186 if (w->pixmap && fade && fadeWindows)
1187 set_fade (dpy, w, w->opacity*1.0/OPAQUE, 0.0, fade_out_step, unmap_callback, False, False, True);
1190 finish_unmap_win (dpy, w);
1193 /* Get the opacity prop from window
1198 get_opacity_prop(Display *dpy, win *w, unsigned int def)
1202 unsigned long n, left;
1204 unsigned char *data;
1205 int result = XGetWindowProperty(dpy, w->id, opacityAtom, 0L, 1L, False,
1206 XA_CARDINAL, &actual, &format,
1208 if (result == Success && data != None)
1211 memcpy (&i, data, sizeof (unsigned int));
1212 XFree( (void *) data);
1218 /* Get the opacity property from the window in a percent format
1220 otherwise: the value
1223 get_opacity_percent(Display *dpy, win *w, double def)
1225 unsigned int opacity = get_opacity_prop (dpy, w, (unsigned int)(OPAQUE*def));
1227 return opacity*1.0/OPAQUE;
1230 /* determine mode for window all in one place.
1231 Future might check for menu flag and other cool things
1235 get_wintype_prop(Display * dpy, Window w)
1239 unsigned long n, left;
1241 unsigned char *data;
1242 int result = XGetWindowProperty (dpy, w, winTypeAtom, 0L, 1L, False,
1243 XA_ATOM, &actual, &format,
1246 if (result == Success && data != None)
1249 memcpy (&a, data, sizeof (Atom));
1250 XFree ( (void *) data);
1253 return winNormalAtom;
1257 determine_mode(Display *dpy, win *w)
1260 XRenderPictFormat *format;
1261 unsigned int default_opacity;
1263 /* if trans prop == -1 fall back on previous tests*/
1267 XRenderFreePicture (dpy, w->alphaPict);
1268 w->alphaPict = None;
1272 XRenderFreePicture (dpy, w->shadowPict);
1273 w->shadowPict = None;
1276 if (w->a.class == InputOnly)
1282 format = XRenderFindVisualFormat (dpy, w->a.visual);
1285 if (format && format->type == PictTypeDirect && format->direct.alphaMask)
1289 else if (w->opacity != OPAQUE)
1291 mode = WINDOW_TRANS;
1295 mode = WINDOW_SOLID;
1300 XserverRegion damage;
1301 damage = XFixesCreateRegion (dpy, 0, 0);
1302 XFixesCopyRegion (dpy, damage, w->extents);
1303 add_damage (dpy, damage);
1308 determine_wintype (Display *dpy, Window w)
1310 Window root_return, parent_return;
1312 unsigned int nchildren, i;
1315 type = get_wintype_prop (dpy, w);
1316 if (type != winNormalAtom)
1319 if (!XQueryTree (dpy, w, &root_return, &parent_return, &children,
1322 /* XQueryTree failed. */
1323 return winNormalAtom;
1326 for (i = 0;i < nchildren;i++)
1328 type = determine_wintype (dpy, children[i]);
1329 if (type != winNormalAtom)
1333 return winNormalAtom;
1337 add_win (Display *dpy, Window id, Window prev)
1339 win *new = malloc (sizeof (win));
1346 for (p = &list; *p; p = &(*p)->next)
1347 if ((*p)->id == prev)
1353 set_ignore (dpy, NextRequest (dpy));
1354 if (!XGetWindowAttributes (dpy, id, &new->a))
1361 new->usable = False;
1363 #if HAS_NAME_WINDOW_PIXMAP
1366 new->picture = None;
1367 if (new->a.class == InputOnly)
1369 new->damage_sequence = 0;
1374 new->damage_sequence = NextRequest (dpy);
1375 new->damage = XDamageCreate (dpy, id, XDamageReportNonEmpty);
1377 new->alphaPict = None;
1378 new->shadowPict = None;
1379 new->borderSize = None;
1380 new->extents = None;
1384 new->shadow_width = 0;
1385 new->shadow_height = 0;
1386 new->opacity = OPAQUE;
1388 new->borderClip = None;
1389 new->prev_trans = 0;
1391 /* moved mode setting to one place */
1392 new->opacity = get_opacity_prop (dpy, new, OPAQUE);
1393 new->windowType = determine_wintype (dpy, new->id);
1394 determine_mode (dpy, new);
1398 if (new->a.map_state == IsViewable)
1399 map_win (dpy, id, new->damage_sequence - 1, True);
1403 restack_win (Display *dpy, win *w, Window new_above)
1408 old_above = w->next->id;
1411 if (old_above != new_above)
1416 for (prev = &list; *prev; prev = &(*prev)->next)
1422 for (prev = &list; *prev; prev = &(*prev)->next)
1424 if ((*prev)->id == new_above)
1433 configure_win (Display *dpy, XConfigureEvent *ce)
1435 win *w = find_win (dpy, ce->window);
1437 XserverRegion damage = None;
1441 if (ce->window == root)
1445 XRenderFreePicture (dpy, rootBuffer);
1448 root_width = ce->width;
1449 root_height = ce->height;
1457 damage = XFixesCreateRegion (dpy, 0, 0);
1458 if (w->extents != None)
1459 XFixesCopyRegion (dpy, damage, w->extents);
1463 if (w->a.width != ce->width || w->a.height != ce->height)
1465 #if HAS_NAME_WINDOW_PIXMAP
1468 XFreePixmap (dpy, w->pixmap);
1472 XRenderFreePicture (dpy, w->picture);
1479 XRenderFreePicture (dpy, w->shadow);
1483 w->a.width = ce->width;
1484 w->a.height = ce->height;
1485 w->a.border_width = ce->border_width;
1486 w->a.override_redirect = ce->override_redirect;
1487 restack_win (dpy, w, ce->above);
1490 XserverRegion extents = win_extents (dpy, w);
1491 XFixesUnionRegion (dpy, damage, damage, extents);
1492 XFixesDestroyRegion (dpy, extents);
1493 add_damage (dpy, damage);
1499 circulate_win (Display *dpy, XCirculateEvent *ce)
1501 win *w = find_win (dpy, ce->window);
1504 if (ce->place == PlaceOnTop)
1505 new_above = list->id;
1508 restack_win (dpy, w, new_above);
1513 finish_destroy_win (Display *dpy, Window id, Bool gone)
1517 for (prev = &list; (w = *prev); prev = &w->next)
1521 finish_unmap_win (dpy, w);
1525 set_ignore (dpy, NextRequest (dpy));
1526 XRenderFreePicture (dpy, w->picture);
1531 XRenderFreePicture (dpy, w->alphaPict);
1532 w->alphaPict = None;
1536 XRenderFreePicture (dpy, w->shadowPict);
1537 w->shadowPict = None;
1539 if (w->damage != None)
1541 set_ignore (dpy, NextRequest (dpy));
1542 XDamageDestroy (dpy, w->damage);
1545 cleanup_fade (dpy, w);
1551 #if HAS_NAME_WINDOW_PIXMAP
1553 destroy_callback (Display *dpy, win *w, Bool gone)
1555 finish_destroy_win (dpy, w->id, gone);
1560 destroy_win (Display *dpy, Window id, Bool gone, Bool fade)
1562 win *w = find_win (dpy, id);
1563 #if HAS_NAME_WINDOW_PIXMAP
1564 if (w && w->pixmap && fade && fadeWindows)
1565 set_fade (dpy, w, w->opacity*1.0/OPAQUE, 0.0, fade_out_step, destroy_callback, gone, False, True);
1569 finish_destroy_win (dpy, id, gone);
1577 printf ("\t%08lx: %d x %d + %d + %d (%d)\n", w->id,
1578 w->a.width, w->a.height, w->a.x, w->a.y, w->a.border_width);
1587 printf ("windows:\n");
1588 for (w = list; w; w = w->next)
1594 damage_win (Display *dpy, XDamageNotifyEvent *de)
1596 win *w = find_win (dpy, de->drawable);
1603 if (w->damage_bounds.width == 0 || w->damage_bounds.height == 0)
1605 w->damage_bounds = de->area;
1609 if (de->area.x < w->damage_bounds.x)
1611 w->damage_bounds.width += (w->damage_bounds.x - de->area.x);
1612 w->damage_bounds.x = de->area.x;
1614 if (de->area.y < w->damage_bounds.y)
1616 w->damage_bounds.height += (w->damage_bounds.y - de->area.y);
1617 w->damage_bounds.y = de->area.y;
1619 if (de->area.x + de->area.width > w->damage_bounds.x + w->damage_bounds.width)
1620 w->damage_bounds.width = de->area.x + de->area.width - w->damage_bounds.x;
1621 if (de->area.y + de->area.height > w->damage_bounds.y + w->damage_bounds.height)
1622 w->damage_bounds.height = de->area.y + de->area.height - w->damage_bounds.y;
1625 printf ("unusable damage %d, %d: %d x %d bounds %d, %d: %d x %d\n",
1632 w->damage_bounds.width,
1633 w->damage_bounds.height);
1635 if (w->damage_bounds.x <= 0 &&
1636 w->damage_bounds.y <= 0 &&
1637 w->a.width <= w->damage_bounds.x + w->damage_bounds.width &&
1638 w->a.height <= w->damage_bounds.y + w->damage_bounds.height)
1642 set_fade (dpy, w, 0, get_opacity_percent (dpy, w, 1.0), fade_in_step, 0, False, True, True);
1648 repair_win (dpy, w);
1652 error (Display *dpy, XErrorEvent *ev)
1657 if (should_ignore (dpy, ev->serial))
1660 if (ev->request_code == composite_opcode &&
1661 ev->minor_code == X_CompositeRedirectSubwindows)
1663 fprintf (stderr, "Another composite manager is already running\n");
1667 o = ev->error_code - xfixes_error;
1669 case BadRegion: name = "BadRegion"; break;
1672 o = ev->error_code - damage_error;
1674 case BadDamage: name = "BadDamage"; break;
1677 o = ev->error_code - render_error;
1679 case BadPictFormat: name ="BadPictFormat"; break;
1680 case BadPicture: name ="BadPicture"; break;
1681 case BadPictOp: name ="BadPictOp"; break;
1682 case BadGlyphSet: name ="BadGlyphSet"; break;
1683 case BadGlyph: name ="BadGlyph"; break;
1687 printf ("error %d request %d minor %d serial %d\n",
1688 ev->error_code, ev->request_code, ev->minor_code, ev->serial);
1690 /* abort (); this is just annoying to most people */
1695 expose_root (Display *dpy, Window root, XRectangle *rects, int nrects)
1697 XserverRegion region = XFixesCreateRegion (dpy, rects, nrects);
1699 add_damage (dpy, region);
1704 ev_serial (XEvent *ev)
1706 if (ev->type & 0x7f != KeymapNotify)
1707 return ev->xany.serial;
1708 return NextRequest (ev->xany.display);
1713 ev_name (XEvent *ev)
1715 static char buf[128];
1716 switch (ev->type & 0x7f) {
1723 case ReparentNotify:
1725 case CirculateNotify:
1728 if (ev->type == damage_event + XDamageNotify)
1730 sprintf (buf, "Event %d", ev->type);
1736 ev_window (XEvent *ev)
1740 return ev->xexpose.window;
1742 return ev->xmap.window;
1744 return ev->xunmap.window;
1745 case ReparentNotify:
1746 return ev->xreparent.window;
1747 case CirculateNotify:
1748 return ev->xcirculate.window;
1750 if (ev->type == damage_event + XDamageNotify)
1751 return ((XDamageNotifyEvent *) ev)->drawable;
1757 usage (char *program)
1759 fprintf (stderr, "usage: %s [options]\n", program);
1760 fprintf (stderr, "Options\n");
1761 fprintf (stderr, " -d display\n Specifies which display should be managed.\n");
1762 fprintf (stderr, " -r radius\n Specifies the blur radius for client-side shadows. (default 12)\n");
1763 fprintf (stderr, " -o opacity\n Specifies the translucency for client-side shadows. (default .75)\n");
1764 fprintf (stderr, " -l left-offset\n Specifies the left offset for client-side shadows. (default -15)\n");
1765 fprintf (stderr, " -t top-offset\n Specifies the top offset for clinet-side shadows. (default -15)\n");
1766 fprintf (stderr, " -a\n Use automatic server-side compositing. Faster, but no special effects.\n");
1767 fprintf (stderr, " -c\n Draw client-side shadows with fuzzy edges.\n");
1768 fprintf (stderr, " -C\n Avoid drawing shadows on dock/panel windows.\n");
1769 fprintf (stderr, " -f\n Fade windows in/out when opening/closing.\n");
1770 fprintf (stderr, " -F\n Fade windows during opacity changes.\n");
1771 fprintf (stderr, " -n\n Normal client-side compositing with transparency support\n");
1772 fprintf (stderr, " -s\n Draw server-side shadows with sharp edges.\n");
1773 fprintf (stderr, " -S\n Enable synchronous operation (for debugging).\n");
1778 main (int argc, char **argv)
1781 Window root_return, parent_return;
1785 unsigned int nchildren;
1787 XRenderPictureAttributes pa;
1789 XRectangle *expose_rects = 0;
1790 int size_expose = 0;
1797 int composite_major, composite_minor;
1801 while ((o = getopt (argc, argv, "d:r:o:l:t:scnfFCaS")) != -1)
1808 compMode = CompServerShadows;
1811 compMode = CompClientShadows;
1814 excludeDockShadows = True;
1817 compMode = CompSimple;
1826 autoRedirect = True;
1832 shadowRadius = atoi (optarg);
1835 shadowOpacity = atof (optarg);
1838 shadowOffsetX = atoi (optarg);
1841 shadowOffsetY = atoi (optarg);
1849 dpy = XOpenDisplay (display);
1852 fprintf (stderr, "Can't open display\n");
1855 XSetErrorHandler (error);
1857 XSynchronize (dpy, 1);
1858 scr = DefaultScreen (dpy);
1859 root = RootWindow (dpy, scr);
1861 if (!XRenderQueryExtension (dpy, &render_event, &render_error))
1863 fprintf (stderr, "No render extension\n");
1866 if (!XQueryExtension (dpy, COMPOSITE_NAME, &composite_opcode,
1867 &composite_event, &composite_error))
1869 fprintf (stderr, "No composite extension\n");
1872 XCompositeQueryVersion (dpy, &composite_major, &composite_minor);
1873 #if HAS_NAME_WINDOW_PIXMAP
1874 if (composite_major > 0 || composite_minor >= 2)
1875 hasNamePixmap = True;
1878 if (!XDamageQueryExtension (dpy, &damage_event, &damage_error))
1880 fprintf (stderr, "No damage extension\n");
1883 if (!XFixesQueryExtension (dpy, &xfixes_event, &xfixes_error))
1885 fprintf (stderr, "No XFixes extension\n");
1889 opacityAtom = XInternAtom (dpy, OPACITY_PROP, False);
1890 winTypeAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE", False);
1891 winDesktopAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DESKTOP", False);
1892 winDockAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DOCK", False);
1893 winToolbarAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_TOOLBAR", False);
1894 winMenuAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_MENU", False);
1895 winUtilAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_UTILITY", False);
1896 winSplashAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_SPLASH", False);
1897 winDialogAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1898 winNormalAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_NORMAL", False);
1900 pa.subwindow_mode = IncludeInferiors;
1902 if (compMode == CompClientShadows)
1903 gaussianMap = make_gaussian_map(dpy, shadowRadius);
1905 root_width = DisplayWidth (dpy, scr);
1906 root_height = DisplayHeight (dpy, scr);
1908 rootPicture = XRenderCreatePicture (dpy, root,
1909 XRenderFindVisualFormat (dpy,
1910 DefaultVisual (dpy, scr)),
1913 blackPicture = solid_picture (dpy, True, 1, 0, 0, 0);
1914 if (compMode == CompServerShadows)
1915 transBlackPicture = solid_picture (dpy, True, 0.3, 0, 0, 0);
1920 XCompositeRedirectSubwindows (dpy, root, CompositeRedirectAutomatic);
1923 XCompositeRedirectSubwindows (dpy, root, CompositeRedirectManual);
1924 XSelectInput (dpy, root,
1925 SubstructureNotifyMask|
1927 StructureNotifyMask|
1928 PropertyChangeMask);
1929 XQueryTree (dpy, root, &root_return, &parent_return, &children, &nchildren);
1930 for (i = 0; i < nchildren; i++)
1931 add_win (dpy, children[i], i ? children[i-1] : None);
1934 XUngrabServer (dpy);
1935 ufd.fd = ConnectionNumber (dpy);
1936 ufd.events = POLLIN;
1938 paint_all (dpy, None);
1947 if (poll (&ufd, 1, fade_timeout()) == 0)
1954 XNextEvent (dpy, &ev);
1955 if (ev.type & 0x7f != KeymapNotify)
1956 discard_ignore (dpy, ev.xany.serial);
1958 printf ("event %10.10s serial 0x%08x window 0x%08x\n",
1959 ev_name(&ev), ev_serial (&ev), ev_window (&ev));
1961 if (!autoRedirect) switch (ev.type) {
1963 add_win (dpy, ev.xcreatewindow.window, 0);
1965 case ConfigureNotify:
1966 configure_win (dpy, &ev.xconfigure);
1969 destroy_win (dpy, ev.xdestroywindow.window, True, True);
1972 map_win (dpy, ev.xmap.window, ev.xmap.serial, True);
1975 unmap_win (dpy, ev.xunmap.window, True);
1977 case ReparentNotify:
1978 if (ev.xreparent.parent == root)
1979 add_win (dpy, ev.xreparent.window, 0);
1981 destroy_win (dpy, ev.xreparent.window, False, True);
1983 case CirculateNotify:
1984 circulate_win (dpy, &ev.xcirculate);
1987 if (ev.xexpose.window == root)
1989 int more = ev.xexpose.count + 1;
1990 if (n_expose == size_expose)
1994 expose_rects = realloc (expose_rects,
1995 (size_expose + more) *
1996 sizeof (XRectangle));
1997 size_expose += more;
2001 expose_rects = malloc (more * sizeof (XRectangle));
2005 expose_rects[n_expose].x = ev.xexpose.x;
2006 expose_rects[n_expose].y = ev.xexpose.y;
2007 expose_rects[n_expose].width = ev.xexpose.width;
2008 expose_rects[n_expose].height = ev.xexpose.height;
2010 if (ev.xexpose.count == 0)
2012 expose_root (dpy, root, expose_rects, n_expose);
2017 case PropertyNotify:
2018 for (p = 0; backgroundProps[p]; p++)
2020 if (ev.xproperty.atom == XInternAtom (dpy, backgroundProps[p], False))
2024 XClearArea (dpy, root, 0, 0, 0, 0, True);
2025 XRenderFreePicture (dpy, rootTile);
2031 /* check if Trans property was changed */
2032 if (ev.xproperty.atom == opacityAtom)
2034 /* reset mode and redraw window */
2035 win * w = find_win(dpy, ev.xproperty.window);
2039 set_fade (dpy, w, w->opacity*1.0/OPAQUE, get_opacity_percent (dpy, w, 1.0),
2040 fade_out_step, 0, False, True, False);
2043 w->opacity = get_opacity_prop(dpy, w, OPAQUE);
2044 determine_mode(dpy, w);
2047 XRenderFreePicture (dpy, w->shadow);
2049 w->extents = win_extents (dpy, w);
2056 if (ev.type == damage_event + XDamageNotify)
2057 damage_win (dpy, (XDamageNotifyEvent *) &ev);
2060 } while (QLength (dpy));
2061 if (allDamage && !autoRedirect)
2064 paint_all (dpy, allDamage);
2068 clipChanged = False;