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;
182 double fade_in_step = 0.028;
183 double fade_out_step = 0.03;
186 Bool fadeWindows = False;
187 Bool excludeDockShadows = False;
188 Bool fadeTrans = False;
190 Bool autoRedirect = False;
193 get_time_in_milliseconds ()
197 gettimeofday (&tv, NULL);
198 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
206 for (f = fades; f; f = f->next)
215 dequeue_fade (Display *dpy, fade *f)
219 for (prev = &fades; *prev; prev = &(*prev)->next)
224 (*f->callback) (dpy, f->w, f->gone);
231 cleanup_fade (Display *dpy, win *w)
233 fade *f = find_fade (w);
235 dequeue_fade (dpy, f);
239 enqueue_fade (Display *dpy, fade *f)
242 fade_time = get_time_in_milliseconds () + fade_delta;
248 set_fade (Display *dpy, win *w, double start, double finish, double step,
249 void (*callback) (Display *dpy, win *w, Bool gone),
250 Bool gone, Bool exec_callback, Bool override)
257 f = malloc (sizeof (fade));
261 enqueue_fade (dpy, f);
269 (*f->callback)(dpy, f->w, f->gone);
279 else if (f->cur > finish)
281 f->callback = callback;
283 w->opacity = f->cur * OPAQUE;
285 printf ("set_fade start %g step %g\n", f->cur, f->step);
287 determine_mode (dpy, w);
290 XRenderFreePicture (dpy, w->shadow);
292 w->extents = win_extents (dpy, w);
303 now = get_time_in_milliseconds();
304 delta = fade_time - now;
307 /* printf ("timeout %d\n", delta); */
312 run_fades (Display *dpy)
314 int now = get_time_in_milliseconds();
319 printf ("run fades\n");
321 if (fade_time - now > 0)
323 steps = 1 + (now - fade_time) / fade_delta;
324 for (next = fades; f = next; )
328 f->cur += f->step * steps;
334 printf ("opacity now %g\n", f->cur);
336 w->opacity = f->cur * OPAQUE;
339 if (f->cur >= f->finish)
341 w->opacity = f->finish*OPAQUE;
342 dequeue_fade (dpy, f);
347 if (f->cur <= f->finish)
349 w->opacity = f->finish*OPAQUE;
350 dequeue_fade (dpy, f);
353 determine_mode (dpy, w);
356 XRenderFreePicture (dpy, w->shadow);
358 w->extents = win_extents(dpy, w);
361 fade_time = now + fade_delta;
364 #define SHADOW_OPACITY 0.75
365 #define SHADOW_OFFSET_X (-shadowRadius * 5 / 4)
366 #define SHADOW_OFFSET_Y (-shadowRadius * 5 / 4)
369 gaussian (double r, double x, double y)
371 return ((1 / (sqrt (2 * M_PI * r))) *
372 exp ((- (x * x + y * y)) / (2 * r * r)));
377 make_gaussian_map (Display *dpy, double r)
380 int size = ((int) ceil ((r * 3)) + 1) & ~1;
381 int center = size / 2;
386 c = malloc (sizeof (conv) + size * size * sizeof (double));
388 c->data = (double *) (c + 1);
390 for (y = 0; y < size; y++)
391 for (x = 0; x < size; x++)
393 g = gaussian (r, (double) (x - center), (double) (y - center));
395 c->data[y * size + x] = g;
397 /* printf ("gaussian total %f\n", t); */
398 for (y = 0; y < size; y++)
399 for (x = 0; x < size; x++)
401 c->data[y*size + x] /= t;
407 * A picture will help
409 * -center 0 width width+center
410 * -center +-----+-------------------+-----+
413 * 0 +-----+-------------------+-----+
417 * height +-----+-------------------+-----+
420 * center +-----+-------------------+-----+
424 sum_gaussian (conv *map, double opacity, int x, int y, int width, int height)
428 double *g_line = map->data;
429 int g_size = map->size;
430 int center = g_size / 2;
431 int fx_start, fx_end;
432 int fy_start, fy_end;
436 * Compute set of filter values which are "in range",
437 * that's the set with:
438 * 0 <= x + (fx-center) && x + (fx-center) < width &&
439 * 0 <= y + (fy-center) && y + (fy-center) < height
441 * 0 <= x + (fx - center) x + fx - center < width
442 * center - x <= fx fx < width + center - x
445 fx_start = center - x;
448 fx_end = width + center - x;
452 fy_start = center - y;
455 fy_end = height + center - y;
459 g_line = g_line + fy_start * g_size + fx_start;
462 for (fy = fy_start; fy < fy_end; fy++)
467 for (fx = fx_start; fx < fx_end; fx++)
473 return ((unsigned char) (v * opacity * 255.0));
477 make_shadow (Display *dpy, double opacity, int width, int height)
481 int gsize = gaussianMap->size;
483 int swidth = width + gsize;
484 int sheight = height + gsize;
485 int center = gsize / 2;
490 data = malloc (swidth * sheight * sizeof (unsigned char));
493 ximage = XCreateImage (dpy,
494 DefaultVisual(dpy, DefaultScreen(dpy)),
499 swidth, sheight, 8, swidth * sizeof (unsigned char));
506 * Build the gaussian in sections
510 * center (fill the complete data array)
513 d = sum_gaussian (gaussianMap, opacity, center, center, width, height);
514 memset(data, d, sheight * swidth);
520 if (ylimit > sheight / 2)
521 ylimit = (sheight + 1) / 2;
523 if (xlimit > swidth / 2)
524 xlimit = (swidth + 1) / 2;
526 for (y = 0; y < ylimit; y++)
527 for (x = 0; x < xlimit; x++)
529 d = sum_gaussian (gaussianMap, opacity, x - center, y - center, width, height);
530 data[y * swidth + x] = d;
531 data[(sheight - y - 1) * swidth + x] = d;
532 data[(sheight - y - 1) * swidth + (swidth - x - 1)] = d;
533 data[y * swidth + (swidth - x - 1)] = d;
539 x_diff = swidth - (gsize * 2);
540 if (x_diff > 0 && ylimit > 0)
542 for (y = 0; y < ylimit; y++)
544 d = sum_gaussian (gaussianMap, opacity, center, y - center, width, height);
545 memset (&data[y * swidth + gsize], d, x_diff);
546 memset (&data[(sheight - y - 1) * swidth + gsize], d, x_diff);
554 for (x = 0; x < xlimit; x++)
556 d = sum_gaussian (gaussianMap, opacity, x - center, center, width, height);
557 for (y = gsize; y < sheight - gsize; y++)
559 data[y * swidth + x] = d;
560 data[y * swidth + (swidth - x - 1)] = d;
568 shadow_picture (Display *dpy, double opacity, Picture alpha_pict, int width, int height, int *wp, int *hp)
573 Picture shadowPicture;
574 Picture finalPicture;
577 shadowImage = make_shadow (dpy, opacity, width, height);
580 shadowPixmap = XCreatePixmap (dpy, root,
584 shadowPicture = XRenderCreatePicture (dpy, shadowPixmap,
585 XRenderFindStandardFormat (dpy, PictStandardA8),
587 gc = XCreateGC (dpy, shadowPixmap, 0, 0);
589 XPutImage (dpy, shadowPixmap, gc, shadowImage, 0, 0, 0, 0,
591 shadowImage->height);
592 *wp = shadowImage->width;
593 *hp = shadowImage->height;
595 XDestroyImage (shadowImage);
596 XFreePixmap (dpy, shadowPixmap);
597 return shadowPicture;
601 solid_picture (Display *dpy, Bool argb, double a, double r, double g, double b)
605 XRenderPictureAttributes pa;
608 pixmap = XCreatePixmap (dpy, root, 1, 1, argb ? 32 : 8);
610 picture = XRenderCreatePicture (dpy, pixmap,
611 XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8),
614 c.alpha = a * 0xffff;
616 c.green = g * 0xffff;
618 XRenderFillRectangle (dpy, PictOpSrc, picture, &c, 0, 0, 1, 1);
619 XFreePixmap (dpy, pixmap);
624 discard_ignore (Display *dpy, unsigned long sequence)
628 if ((long) (sequence - ignore_head->sequence) > 0)
630 ignore *next = ignore_head->next;
634 ignore_tail = &ignore_head;
642 set_ignore (Display *dpy, unsigned long sequence)
644 ignore *i = malloc (sizeof (ignore));
647 i->sequence = sequence;
650 ignore_tail = &i->next;
654 should_ignore (Display *dpy, unsigned long sequence)
656 discard_ignore (dpy, sequence);
657 return ignore_head && ignore_head->sequence == sequence;
661 find_win (Display *dpy, Window id)
665 for (w = list; w; w = w->next)
671 static char *backgroundProps[] = {
678 root_tile (Display *dpy)
684 unsigned long nitems;
685 unsigned long bytes_after;
688 XRenderPictureAttributes pa;
692 for (p = 0; backgroundProps[p]; p++)
694 if (XGetWindowProperty (dpy, root, XInternAtom (dpy, backgroundProps[p], False),
695 0, 4, False, AnyPropertyType,
696 &actual_type, &actual_format, &nitems, &bytes_after, &prop) == Success &&
697 actual_type == XInternAtom (dpy, "PIXMAP", False) && actual_format == 32 && nitems == 1)
699 memcpy (&pixmap, prop, 4);
707 pixmap = XCreatePixmap (dpy, root, 1, 1, DefaultDepth (dpy, scr));
711 picture = XRenderCreatePicture (dpy, pixmap,
712 XRenderFindVisualFormat (dpy,
713 DefaultVisual (dpy, scr)),
719 c.red = c.green = c.blue = 0x8080;
721 XRenderFillRectangle (dpy, PictOpSrc, picture, &c,
728 paint_root (Display *dpy)
731 rootTile = root_tile (dpy);
733 XRenderComposite (dpy, PictOpSrc,
734 rootTile, None, rootBuffer,
735 0, 0, 0, 0, 0, 0, root_width, root_height);
739 win_extents (Display *dpy, win *w)
745 r.width = w->a.width + w->a.border_width * 2;
746 r.height = w->a.height + w->a.border_width * 2;
747 if (compMode != CompSimple && !(w->windowType == winDockAtom && excludeDockShadows))
749 if (compMode == CompServerShadows || w->mode != WINDOW_ARGB)
753 if (compMode == CompServerShadows)
757 w->shadow_width = w->a.width;
758 w->shadow_height = w->a.height;
762 w->shadow_dx = SHADOW_OFFSET_X;
763 w->shadow_dy = SHADOW_OFFSET_Y;
766 double opacity = SHADOW_OPACITY;
767 if (w->mode == WINDOW_TRANS)
768 opacity = opacity * ((double)w->opacity)/((double)OPAQUE);
769 w->shadow = shadow_picture (dpy, opacity, w->alphaPict,
770 w->a.width + w->a.border_width * 2,
771 w->a.height + w->a.border_width * 2,
772 &w->shadow_width, &w->shadow_height);
775 sr.x = w->a.x + w->shadow_dx;
776 sr.y = w->a.y + w->shadow_dy;
777 sr.width = w->shadow_width;
778 sr.height = w->shadow_height;
781 r.width = (r.x + r.width) - sr.x;
786 r.height = (r.y + r.height) - sr.y;
789 if (sr.x + sr.width > r.x + r.width)
790 r.width = sr.x + sr.width - r.x;
791 if (sr.y + sr.height > r.y + r.height)
792 r.height = sr.y + sr.height - r.y;
795 return XFixesCreateRegion (dpy, &r, 1);
799 border_size (Display *dpy, win *w)
801 XserverRegion border;
803 * if window doesn't exist anymore, this will generate an error
804 * as well as not generate a region. Perhaps a better XFixes
805 * architecture would be to have a request that copies instead
806 * of creates, that way you'd just end up with an empty region
807 * instead of an invalid XID.
809 set_ignore (dpy, NextRequest (dpy));
810 border = XFixesCreateRegionFromWindow (dpy, w->id, WindowRegionBounding);
812 set_ignore (dpy, NextRequest (dpy));
813 XFixesTranslateRegion (dpy, border,
814 w->a.x + w->a.border_width,
815 w->a.y + w->a.border_width);
820 paint_all (Display *dpy, XserverRegion region)
830 r.width = root_width;
831 r.height = root_height;
832 region = XFixesCreateRegion (dpy, &r, 1);
835 rootBuffer = rootPicture;
839 Pixmap rootPixmap = XCreatePixmap (dpy, root, root_width, root_height,
840 DefaultDepth (dpy, scr));
841 rootBuffer = XRenderCreatePicture (dpy, rootPixmap,
842 XRenderFindVisualFormat (dpy,
843 DefaultVisual (dpy, scr)),
845 XFreePixmap (dpy, rootPixmap);
848 XFixesSetPictureClipRegion (dpy, rootPicture, 0, 0, region);
850 XRenderComposite (dpy, PictOpSrc, blackPicture, None, rootPicture,
851 0, 0, 0, 0, 0, 0, root_width, root_height);
856 for (w = list; w; w = w->next)
862 /* never painted, ignore it */
867 XRenderPictureAttributes pa;
868 XRenderPictFormat *format;
869 Drawable draw = w->id;
871 #if HAS_NAME_WINDOW_PIXMAP
872 if (hasNamePixmap && !w->pixmap)
873 w->pixmap = XCompositeNameWindowPixmap (dpy, w->id);
877 format = XRenderFindVisualFormat (dpy, w->a.visual);
878 pa.subwindow_mode = IncludeInferiors;
879 w->picture = XRenderCreatePicture (dpy, draw,
885 printf (" 0x%x", w->id);
891 set_ignore (dpy, NextRequest (dpy));
892 XFixesDestroyRegion (dpy, w->borderSize);
893 w->borderSize = None;
897 XFixesDestroyRegion (dpy, w->extents);
902 XFixesDestroyRegion (dpy, w->borderClip);
903 w->borderClip = None;
907 w->borderSize = border_size (dpy, w);
909 w->extents = win_extents (dpy, w);
910 if (w->mode == WINDOW_SOLID)
913 #if HAS_NAME_WINDOW_PIXMAP
916 wid = w->a.width + w->a.border_width * 2;
917 hei = w->a.height + w->a.border_width * 2;
919 x = w->a.x + w->a.border_width;
920 y = w->a.y + w->a.border_width;
924 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
925 set_ignore (dpy, NextRequest (dpy));
926 XFixesSubtractRegion (dpy, region, region, w->borderSize);
927 set_ignore (dpy, NextRequest (dpy));
928 XRenderComposite (dpy, PictOpSrc, w->picture, None, rootBuffer,
934 w->borderClip = XFixesCreateRegion (dpy, 0, 0);
935 XFixesCopyRegion (dpy, w->borderClip, region);
944 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
946 for (w = t; w; w = w->prev_trans)
948 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, w->borderClip);
952 case CompServerShadows:
953 set_ignore (dpy, NextRequest (dpy));
954 if (w->opacity != OPAQUE && !w->shadowPict)
955 w->shadowPict = solid_picture (dpy, True,
956 (double) w->opacity / OPAQUE * 0.3,
958 XRenderComposite (dpy, PictOpOver,
959 w->shadowPict ? w->shadowPict : transBlackPicture,
960 w->picture, rootBuffer,
962 w->a.x + w->shadow_dx,
963 w->a.y + w->shadow_dy,
964 w->shadow_width, w->shadow_height);
966 case CompClientShadows:
969 XRenderComposite (dpy, PictOpOver, blackPicture, w->shadow, rootBuffer,
971 w->a.x + w->shadow_dx,
972 w->a.y + w->shadow_dy,
973 w->shadow_width, w->shadow_height);
977 if (w->opacity != OPAQUE && !w->alphaPict)
978 w->alphaPict = solid_picture (dpy, False,
979 (double) w->opacity / OPAQUE, 0, 0, 0);
980 if (w->mode == WINDOW_TRANS)
983 #if HAS_NAME_WINDOW_PIXMAP
986 wid = w->a.width + w->a.border_width * 2;
987 hei = w->a.height + w->a.border_width * 2;
989 x = w->a.x + w->a.border_width;
990 y = w->a.y + w->a.border_width;
994 set_ignore (dpy, NextRequest (dpy));
995 XRenderComposite (dpy, PictOpOver, w->picture, w->alphaPict, rootBuffer,
999 else if (w->mode == WINDOW_ARGB)
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 XFixesDestroyRegion (dpy, w->borderClip);
1019 w->borderClip = None;
1021 XFixesDestroyRegion (dpy, region);
1022 if (rootBuffer != rootPicture)
1024 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, None);
1025 XRenderComposite (dpy, PictOpSrc, rootBuffer, None, rootPicture,
1026 0, 0, 0, 0, 0, 0, root_width, root_height);
1031 add_damage (Display *dpy, XserverRegion damage)
1035 XFixesUnionRegion (dpy, allDamage, allDamage, damage);
1036 XFixesDestroyRegion (dpy, damage);
1043 repair_win (Display *dpy, win *w)
1045 XserverRegion parts;
1049 parts = win_extents (dpy, w);
1050 set_ignore (dpy, NextRequest (dpy));
1051 XDamageSubtract (dpy, w->damage, None, None);
1056 parts = XFixesCreateRegion (dpy, 0, 0);
1057 set_ignore (dpy, NextRequest (dpy));
1058 XDamageSubtract (dpy, w->damage, None, parts);
1059 XFixesTranslateRegion (dpy, parts,
1060 w->a.x + w->a.border_width,
1061 w->a.y + w->a.border_width);
1062 if (compMode == CompServerShadows)
1064 o = XFixesCreateRegion (dpy, 0, 0);
1065 XFixesCopyRegion (dpy, o, parts);
1066 XFixesTranslateRegion (dpy, o, w->shadow_dx, w->shadow_dy);
1067 XFixesUnionRegion (dpy, parts, parts, o);
1068 XFixesDestroyRegion (dpy, o);
1071 add_damage (dpy, parts);
1076 map_win (Display *dpy, Window id, unsigned long sequence, Bool fade)
1078 win *w = find_win (dpy, id);
1084 w->a.map_state = IsViewable;
1086 /* This needs to be here or else we lose transparency messages */
1087 XSelectInput (dpy, id, PropertyChangeMask);
1090 w->damage_bounds.x = w->damage_bounds.y = 0;
1091 w->damage_bounds.width = w->damage_bounds.height = 0;
1095 if (fade && fadeWindows)
1096 set_fade (dpy, w, 0, get_opacity_percent (dpy, w, 1.0), fade_in_step, 0, False, True, True);
1100 finish_unmap_win (Display *dpy, win *w)
1106 if (w->extents != None)
1108 add_damage (dpy, w->extents); /* destroys region */
1112 #if HAS_NAME_WINDOW_PIXMAP
1115 XFreePixmap (dpy, w->pixmap);
1122 set_ignore (dpy, NextRequest (dpy));
1123 XRenderFreePicture (dpy, w->picture);
1127 /* don't care about properties anymore */
1128 set_ignore (dpy, NextRequest (dpy));
1129 XSelectInput(dpy, w->id, 0);
1133 set_ignore (dpy, NextRequest (dpy));
1134 XFixesDestroyRegion (dpy, w->borderSize);
1135 w->borderSize = None;
1139 XRenderFreePicture (dpy, w->shadow);
1144 XFixesDestroyRegion (dpy, w->borderClip);
1145 w->borderClip = None;
1151 #if HAS_NAME_WINDOW_PIXMAP
1153 unmap_callback (Display *dpy, win *w, Bool gone)
1155 finish_unmap_win (dpy, w);
1160 unmap_win (Display *dpy, Window id, Bool fade)
1162 win *w = find_win (dpy, id);
1165 w->a.map_state = IsUnmapped;
1166 #if HAS_NAME_WINDOW_PIXMAP
1167 if (w->pixmap && fade && fadeWindows)
1168 set_fade (dpy, w, w->opacity*1.0/OPAQUE, 0.0, fade_out_step, unmap_callback, False, False, True);
1171 finish_unmap_win (dpy, w);
1174 /* Get the opacity prop from window
1179 get_opacity_prop(Display *dpy, win *w, unsigned int def)
1183 unsigned long n, left;
1185 unsigned char *data;
1186 int result = XGetWindowProperty(dpy, w->id, opacityAtom, 0L, 1L, False,
1187 XA_CARDINAL, &actual, &format,
1189 if (result == Success && data != None)
1192 memcpy (&i, data, sizeof (unsigned int));
1193 XFree( (void *) data);
1199 /* Get the opacity property from the window in a percent format
1201 otherwise: the value
1204 get_opacity_percent(Display *dpy, win *w, double def)
1206 unsigned int opacity = get_opacity_prop (dpy, w, (unsigned int)(OPAQUE*def));
1208 return opacity*1.0/OPAQUE;
1211 /* determine mode for window all in one place.
1212 Future might check for menu flag and other cool things
1216 get_wintype_prop(Display * dpy, Window w)
1220 unsigned long n, left;
1222 unsigned char *data;
1223 int result = XGetWindowProperty (dpy, w, winTypeAtom, 0L, 1L, False,
1224 XA_ATOM, &actual, &format,
1227 if (result == Success && data != None)
1230 memcpy (&a, data, sizeof (Atom));
1231 XFree ( (void *) data);
1234 return winNormalAtom;
1238 determine_mode(Display *dpy, win *w)
1241 XRenderPictFormat *format;
1242 unsigned int default_opacity;
1244 /* if trans prop == -1 fall back on previous tests*/
1248 XRenderFreePicture (dpy, w->alphaPict);
1249 w->alphaPict = None;
1253 XRenderFreePicture (dpy, w->shadowPict);
1254 w->shadowPict = None;
1257 if (w->a.class == InputOnly)
1263 format = XRenderFindVisualFormat (dpy, w->a.visual);
1266 if (format && format->type == PictTypeDirect && format->direct.alphaMask)
1270 else if (w->opacity != OPAQUE)
1272 mode = WINDOW_TRANS;
1276 mode = WINDOW_SOLID;
1281 XserverRegion damage;
1282 damage = XFixesCreateRegion (dpy, 0, 0);
1283 XFixesCopyRegion (dpy, damage, w->extents);
1284 add_damage (dpy, damage);
1289 determine_wintype (Display *dpy, Window w)
1291 Window root_return, parent_return;
1293 unsigned int nchildren, i;
1296 type = get_wintype_prop (dpy, w);
1297 if (type != winNormalAtom)
1300 if (!XQueryTree (dpy, w, &root_return, &parent_return, &children,
1303 /* XQueryTree failed. */
1304 return winNormalAtom;
1307 for (i = 0;i < nchildren;i++)
1309 type = determine_wintype (dpy, children[i]);
1310 if (type != winNormalAtom)
1314 return winNormalAtom;
1318 add_win (Display *dpy, Window id, Window prev)
1320 win *new = malloc (sizeof (win));
1327 for (p = &list; *p; p = &(*p)->next)
1328 if ((*p)->id == prev)
1334 set_ignore (dpy, NextRequest (dpy));
1335 if (!XGetWindowAttributes (dpy, id, &new->a))
1342 new->usable = False;
1344 #if HAS_NAME_WINDOW_PIXMAP
1347 new->picture = None;
1348 if (new->a.class == InputOnly)
1350 new->damage_sequence = 0;
1355 new->damage_sequence = NextRequest (dpy);
1356 new->damage = XDamageCreate (dpy, id, XDamageReportNonEmpty);
1358 new->alphaPict = None;
1359 new->shadowPict = None;
1360 new->borderSize = None;
1361 new->extents = None;
1365 new->shadow_width = 0;
1366 new->shadow_height = 0;
1367 new->opacity = OPAQUE;
1369 new->borderClip = None;
1370 new->prev_trans = 0;
1372 /* moved mode setting to one place */
1373 new->opacity = get_opacity_prop (dpy, new, OPAQUE);
1374 new->windowType = determine_wintype (dpy, new->id);
1375 determine_mode (dpy, new);
1379 if (new->a.map_state == IsViewable)
1380 map_win (dpy, id, new->damage_sequence - 1, True);
1384 restack_win (Display *dpy, win *w, Window new_above)
1389 old_above = w->next->id;
1392 if (old_above != new_above)
1397 for (prev = &list; *prev; prev = &(*prev)->next)
1403 for (prev = &list; *prev; prev = &(*prev)->next)
1405 if ((*prev)->id == new_above)
1414 configure_win (Display *dpy, XConfigureEvent *ce)
1416 win *w = find_win (dpy, ce->window);
1418 XserverRegion damage = None;
1422 if (ce->window == root)
1426 XRenderFreePicture (dpy, rootBuffer);
1429 root_width = ce->width;
1430 root_height = ce->height;
1438 damage = XFixesCreateRegion (dpy, 0, 0);
1439 if (w->extents != None)
1440 XFixesCopyRegion (dpy, damage, w->extents);
1444 if (w->a.width != ce->width || w->a.height != ce->height)
1446 #if HAS_NAME_WINDOW_PIXMAP
1449 XFreePixmap (dpy, w->pixmap);
1453 XRenderFreePicture (dpy, w->picture);
1460 XRenderFreePicture (dpy, w->shadow);
1464 w->a.width = ce->width;
1465 w->a.height = ce->height;
1466 w->a.border_width = ce->border_width;
1467 w->a.override_redirect = ce->override_redirect;
1468 restack_win (dpy, w, ce->above);
1471 XserverRegion extents = win_extents (dpy, w);
1472 XFixesUnionRegion (dpy, damage, damage, extents);
1473 XFixesDestroyRegion (dpy, extents);
1474 add_damage (dpy, damage);
1480 circulate_win (Display *dpy, XCirculateEvent *ce)
1482 win *w = find_win (dpy, ce->window);
1485 if (ce->place == PlaceOnTop)
1486 new_above = list->id;
1489 restack_win (dpy, w, new_above);
1494 finish_destroy_win (Display *dpy, Window id, Bool gone)
1498 for (prev = &list; (w = *prev); prev = &w->next)
1502 finish_unmap_win (dpy, w);
1506 set_ignore (dpy, NextRequest (dpy));
1507 XRenderFreePicture (dpy, w->picture);
1512 XRenderFreePicture (dpy, w->alphaPict);
1513 w->alphaPict = None;
1517 XRenderFreePicture (dpy, w->shadowPict);
1518 w->shadowPict = None;
1520 if (w->damage != None)
1522 set_ignore (dpy, NextRequest (dpy));
1523 XDamageDestroy (dpy, w->damage);
1526 cleanup_fade (dpy, w);
1532 #if HAS_NAME_WINDOW_PIXMAP
1534 destroy_callback (Display *dpy, win *w, Bool gone)
1536 finish_destroy_win (dpy, w->id, gone);
1541 destroy_win (Display *dpy, Window id, Bool gone, Bool fade)
1543 win *w = find_win (dpy, id);
1544 #if HAS_NAME_WINDOW_PIXMAP
1545 if (w && w->pixmap && fade && fadeWindows)
1546 set_fade (dpy, w, w->opacity*1.0/OPAQUE, 0.0, fade_out_step, destroy_callback, gone, False, True);
1550 finish_destroy_win (dpy, id, gone);
1558 printf ("\t%08lx: %d x %d + %d + %d (%d)\n", w->id,
1559 w->a.width, w->a.height, w->a.x, w->a.y, w->a.border_width);
1568 printf ("windows:\n");
1569 for (w = list; w; w = w->next)
1575 damage_win (Display *dpy, XDamageNotifyEvent *de)
1577 win *w = find_win (dpy, de->drawable);
1584 if (w->damage_bounds.width == 0 || w->damage_bounds.height == 0)
1586 w->damage_bounds = de->area;
1590 if (de->area.x < w->damage_bounds.x)
1592 w->damage_bounds.width += (w->damage_bounds.x - de->area.x);
1593 w->damage_bounds.x = de->area.x;
1595 if (de->area.y < w->damage_bounds.y)
1597 w->damage_bounds.height += (w->damage_bounds.y - de->area.y);
1598 w->damage_bounds.y = de->area.y;
1600 if (de->area.x + de->area.width > w->damage_bounds.x + w->damage_bounds.width)
1601 w->damage_bounds.width = de->area.x + de->area.width - w->damage_bounds.x;
1602 if (de->area.y + de->area.height > w->damage_bounds.y + w->damage_bounds.height)
1603 w->damage_bounds.height = de->area.y + de->area.height - w->damage_bounds.y;
1606 printf ("unusable damage %d, %d: %d x %d bounds %d, %d: %d x %d\n",
1613 w->damage_bounds.width,
1614 w->damage_bounds.height);
1616 if (w->damage_bounds.x <= 0 &&
1617 w->damage_bounds.y <= 0 &&
1618 w->a.width <= w->damage_bounds.x + w->damage_bounds.width &&
1619 w->a.height <= w->damage_bounds.y + w->damage_bounds.height)
1623 set_fade (dpy, w, 0, get_opacity_percent (dpy, w, 1.0), fade_in_step, 0, False, True, True);
1629 repair_win (dpy, w);
1633 error (Display *dpy, XErrorEvent *ev)
1638 if (should_ignore (dpy, ev->serial))
1641 if (ev->request_code == composite_opcode &&
1642 ev->minor_code == X_CompositeRedirectSubwindows)
1644 fprintf (stderr, "Another composite manager is already running\n");
1648 o = ev->error_code - xfixes_error;
1650 case BadRegion: name = "BadRegion"; break;
1653 o = ev->error_code - damage_error;
1655 case BadDamage: name = "BadDamage"; break;
1658 o = ev->error_code - render_error;
1660 case BadPictFormat: name ="BadPictFormat"; break;
1661 case BadPicture: name ="BadPicture"; break;
1662 case BadPictOp: name ="BadPictOp"; break;
1663 case BadGlyphSet: name ="BadGlyphSet"; break;
1664 case BadGlyph: name ="BadGlyph"; break;
1668 printf ("error %d request %d minor %d serial %d\n",
1669 ev->error_code, ev->request_code, ev->minor_code, ev->serial);
1671 /* abort (); this is just annoying to most people */
1676 expose_root (Display *dpy, Window root, XRectangle *rects, int nrects)
1678 XserverRegion region = XFixesCreateRegion (dpy, rects, nrects);
1680 add_damage (dpy, region);
1685 ev_serial (XEvent *ev)
1687 if (ev->type & 0x7f != KeymapNotify)
1688 return ev->xany.serial;
1689 return NextRequest (ev->xany.display);
1694 ev_name (XEvent *ev)
1696 static char buf[128];
1697 switch (ev->type & 0x7f) {
1704 case ReparentNotify:
1706 case CirculateNotify:
1709 if (ev->type == damage_event + XDamageNotify)
1711 sprintf (buf, "Event %d", ev->type);
1717 ev_window (XEvent *ev)
1721 return ev->xexpose.window;
1723 return ev->xmap.window;
1725 return ev->xunmap.window;
1726 case ReparentNotify:
1727 return ev->xreparent.window;
1728 case CirculateNotify:
1729 return ev->xcirculate.window;
1731 if (ev->type == damage_event + XDamageNotify)
1732 return ((XDamageNotifyEvent *) ev)->drawable;
1738 usage (char *program)
1740 fprintf (stderr, "usage: %s [-d display] [-n] [-s] [-c] [-a] [-f] [-F] [-C]\n", program);
1745 main (int argc, char **argv)
1748 Window root_return, parent_return;
1752 unsigned int nchildren;
1754 XRenderPictureAttributes pa;
1756 XRectangle *expose_rects = 0;
1757 int size_expose = 0;
1764 int composite_major, composite_minor;
1768 while ((o = getopt (argc, argv, "d:scnfFCaS")) != -1)
1775 compMode = CompServerShadows;
1778 compMode = CompClientShadows;
1781 excludeDockShadows = True;
1784 compMode = CompSimple;
1793 autoRedirect = True;
1804 dpy = XOpenDisplay (display);
1807 fprintf (stderr, "Can't open display\n");
1810 XSetErrorHandler (error);
1812 XSynchronize (dpy, 1);
1813 scr = DefaultScreen (dpy);
1814 root = RootWindow (dpy, scr);
1816 if (!XRenderQueryExtension (dpy, &render_event, &render_error))
1818 fprintf (stderr, "No render extension\n");
1821 if (!XQueryExtension (dpy, COMPOSITE_NAME, &composite_opcode,
1822 &composite_event, &composite_error))
1824 fprintf (stderr, "No composite extension\n");
1827 XCompositeQueryVersion (dpy, &composite_major, &composite_minor);
1828 #if HAS_NAME_WINDOW_PIXMAP
1829 if (composite_major > 0 || composite_minor >= 2)
1830 hasNamePixmap = True;
1833 if (!XDamageQueryExtension (dpy, &damage_event, &damage_error))
1835 fprintf (stderr, "No damage extension\n");
1838 if (!XFixesQueryExtension (dpy, &xfixes_event, &xfixes_error))
1840 fprintf (stderr, "No XFixes extension\n");
1844 opacityAtom = XInternAtom (dpy, OPACITY_PROP, False);
1845 winTypeAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE", False);
1846 winDesktopAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DESKTOP", False);
1847 winDockAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DOCK", False);
1848 winToolbarAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_TOOLBAR", False);
1849 winMenuAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_MENU", False);
1850 winUtilAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_UTILITY", False);
1851 winSplashAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_SPLASH", False);
1852 winDialogAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1853 winNormalAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_NORMAL", False);
1855 pa.subwindow_mode = IncludeInferiors;
1857 if (compMode == CompClientShadows)
1858 gaussianMap = make_gaussian_map(dpy, shadowRadius);
1860 root_width = DisplayWidth (dpy, scr);
1861 root_height = DisplayHeight (dpy, scr);
1863 rootPicture = XRenderCreatePicture (dpy, root,
1864 XRenderFindVisualFormat (dpy,
1865 DefaultVisual (dpy, scr)),
1868 blackPicture = solid_picture (dpy, True, 1, 0, 0, 0);
1869 if (compMode == CompServerShadows)
1870 transBlackPicture = solid_picture (dpy, True, 0.3, 0, 0, 0);
1875 XCompositeRedirectSubwindows (dpy, root, CompositeRedirectAutomatic);
1878 XCompositeRedirectSubwindows (dpy, root, CompositeRedirectManual);
1879 XSelectInput (dpy, root,
1880 SubstructureNotifyMask|
1882 StructureNotifyMask|
1883 PropertyChangeMask);
1884 XQueryTree (dpy, root, &root_return, &parent_return, &children, &nchildren);
1885 for (i = 0; i < nchildren; i++)
1886 add_win (dpy, children[i], i ? children[i-1] : None);
1889 XUngrabServer (dpy);
1890 ufd.fd = ConnectionNumber (dpy);
1891 ufd.events = POLLIN;
1893 paint_all (dpy, None);
1902 if (poll (&ufd, 1, fade_timeout()) == 0)
1909 XNextEvent (dpy, &ev);
1910 if (ev.type & 0x7f != KeymapNotify)
1911 discard_ignore (dpy, ev.xany.serial);
1913 printf ("event %10.10s serial 0x%08x window 0x%08x\n",
1914 ev_name(&ev), ev_serial (&ev), ev_window (&ev));
1916 if (!autoRedirect) switch (ev.type) {
1918 add_win (dpy, ev.xcreatewindow.window, 0);
1920 case ConfigureNotify:
1921 configure_win (dpy, &ev.xconfigure);
1924 destroy_win (dpy, ev.xdestroywindow.window, True, True);
1927 map_win (dpy, ev.xmap.window, ev.xmap.serial, True);
1930 unmap_win (dpy, ev.xunmap.window, True);
1932 case ReparentNotify:
1933 if (ev.xreparent.parent == root)
1934 add_win (dpy, ev.xreparent.window, 0);
1936 destroy_win (dpy, ev.xreparent.window, False, True);
1938 case CirculateNotify:
1939 circulate_win (dpy, &ev.xcirculate);
1942 if (ev.xexpose.window == root)
1944 int more = ev.xexpose.count + 1;
1945 if (n_expose == size_expose)
1949 expose_rects = realloc (expose_rects,
1950 (size_expose + more) *
1951 sizeof (XRectangle));
1952 size_expose += more;
1956 expose_rects = malloc (more * sizeof (XRectangle));
1960 expose_rects[n_expose].x = ev.xexpose.x;
1961 expose_rects[n_expose].y = ev.xexpose.y;
1962 expose_rects[n_expose].width = ev.xexpose.width;
1963 expose_rects[n_expose].height = ev.xexpose.height;
1965 if (ev.xexpose.count == 0)
1967 expose_root (dpy, root, expose_rects, n_expose);
1972 case PropertyNotify:
1973 for (p = 0; backgroundProps[p]; p++)
1975 if (ev.xproperty.atom == XInternAtom (dpy, backgroundProps[p], False))
1979 XClearArea (dpy, root, 0, 0, 0, 0, True);
1980 XRenderFreePicture (dpy, rootTile);
1986 /* check if Trans property was changed */
1987 if (ev.xproperty.atom == opacityAtom)
1989 /* reset mode and redraw window */
1990 win * w = find_win(dpy, ev.xproperty.window);
1994 set_fade (dpy, w, w->opacity*1.0/OPAQUE, get_opacity_percent (dpy, w, 1.0),
1995 fade_out_step, 0, False, True, False);
1998 w->opacity = get_opacity_prop(dpy, w, OPAQUE);
1999 determine_mode(dpy, w);
2002 XRenderFreePicture (dpy, w->shadow);
2004 w->extents = win_extents (dpy, w);
2011 if (ev.type == damage_event + XDamageNotify)
2012 damage_win (dpy, (XDamageNotifyEvent *) &ev);
2015 } while (QLength (dpy));
2016 if (allDamage && !autoRedirect)
2019 paint_all (dpy, allDamage);
2023 clipChanged = False;