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,
586 XDestroyImage (shadowImage);
590 shadowPicture = XRenderCreatePicture (dpy, shadowPixmap,
591 XRenderFindStandardFormat (dpy, PictStandardA8),
595 XDestroyImage (shadowImage);
596 XFreePixmap (dpy, shadowPixmap);
600 gc = XCreateGC (dpy, shadowPixmap, 0, 0);
603 XDestroyImage (shadowImage);
604 XFreePixmap (dpy, shadowPixmap);
605 XRenderFreePicture (dpy, shadowPicture);
609 XPutImage (dpy, shadowPixmap, gc, shadowImage, 0, 0, 0, 0,
611 shadowImage->height);
612 *wp = shadowImage->width;
613 *hp = shadowImage->height;
615 XDestroyImage (shadowImage);
616 XFreePixmap (dpy, shadowPixmap);
617 return shadowPicture;
621 solid_picture (Display *dpy, Bool argb, double a, double r, double g, double b)
625 XRenderPictureAttributes pa;
628 pixmap = XCreatePixmap (dpy, root, 1, 1, argb ? 32 : 8);
630 picture = XRenderCreatePicture (dpy, pixmap,
631 XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8),
634 c.alpha = a * 0xffff;
636 c.green = g * 0xffff;
638 XRenderFillRectangle (dpy, PictOpSrc, picture, &c, 0, 0, 1, 1);
639 XFreePixmap (dpy, pixmap);
644 discard_ignore (Display *dpy, unsigned long sequence)
648 if ((long) (sequence - ignore_head->sequence) > 0)
650 ignore *next = ignore_head->next;
654 ignore_tail = &ignore_head;
662 set_ignore (Display *dpy, unsigned long sequence)
664 ignore *i = malloc (sizeof (ignore));
667 i->sequence = sequence;
670 ignore_tail = &i->next;
674 should_ignore (Display *dpy, unsigned long sequence)
676 discard_ignore (dpy, sequence);
677 return ignore_head && ignore_head->sequence == sequence;
681 find_win (Display *dpy, Window id)
685 for (w = list; w; w = w->next)
691 static char *backgroundProps[] = {
698 root_tile (Display *dpy)
704 unsigned long nitems;
705 unsigned long bytes_after;
708 XRenderPictureAttributes pa;
712 for (p = 0; backgroundProps[p]; p++)
714 if (XGetWindowProperty (dpy, root, XInternAtom (dpy, backgroundProps[p], False),
715 0, 4, False, AnyPropertyType,
716 &actual_type, &actual_format, &nitems, &bytes_after, &prop) == Success &&
717 actual_type == XInternAtom (dpy, "PIXMAP", False) && actual_format == 32 && nitems == 1)
719 memcpy (&pixmap, prop, 4);
727 pixmap = XCreatePixmap (dpy, root, 1, 1, DefaultDepth (dpy, scr));
731 picture = XRenderCreatePicture (dpy, pixmap,
732 XRenderFindVisualFormat (dpy,
733 DefaultVisual (dpy, scr)),
739 c.red = c.green = c.blue = 0x8080;
741 XRenderFillRectangle (dpy, PictOpSrc, picture, &c,
748 paint_root (Display *dpy)
751 rootTile = root_tile (dpy);
753 XRenderComposite (dpy, PictOpSrc,
754 rootTile, None, rootBuffer,
755 0, 0, 0, 0, 0, 0, root_width, root_height);
759 win_extents (Display *dpy, win *w)
765 r.width = w->a.width + w->a.border_width * 2;
766 r.height = w->a.height + w->a.border_width * 2;
767 if (compMode != CompSimple && !(w->windowType == winDockAtom && excludeDockShadows))
769 if (compMode == CompServerShadows || w->mode != WINDOW_ARGB)
773 if (compMode == CompServerShadows)
777 w->shadow_width = w->a.width;
778 w->shadow_height = w->a.height;
782 w->shadow_dx = SHADOW_OFFSET_X;
783 w->shadow_dy = SHADOW_OFFSET_Y;
786 double opacity = SHADOW_OPACITY;
787 if (w->mode == WINDOW_TRANS)
788 opacity = opacity * ((double)w->opacity)/((double)OPAQUE);
789 w->shadow = shadow_picture (dpy, opacity, w->alphaPict,
790 w->a.width + w->a.border_width * 2,
791 w->a.height + w->a.border_width * 2,
792 &w->shadow_width, &w->shadow_height);
795 sr.x = w->a.x + w->shadow_dx;
796 sr.y = w->a.y + w->shadow_dy;
797 sr.width = w->shadow_width;
798 sr.height = w->shadow_height;
801 r.width = (r.x + r.width) - sr.x;
806 r.height = (r.y + r.height) - sr.y;
809 if (sr.x + sr.width > r.x + r.width)
810 r.width = sr.x + sr.width - r.x;
811 if (sr.y + sr.height > r.y + r.height)
812 r.height = sr.y + sr.height - r.y;
815 return XFixesCreateRegion (dpy, &r, 1);
819 border_size (Display *dpy, win *w)
821 XserverRegion border;
823 * if window doesn't exist anymore, this will generate an error
824 * as well as not generate a region. Perhaps a better XFixes
825 * architecture would be to have a request that copies instead
826 * of creates, that way you'd just end up with an empty region
827 * instead of an invalid XID.
829 set_ignore (dpy, NextRequest (dpy));
830 border = XFixesCreateRegionFromWindow (dpy, w->id, WindowRegionBounding);
832 set_ignore (dpy, NextRequest (dpy));
833 XFixesTranslateRegion (dpy, border,
834 w->a.x + w->a.border_width,
835 w->a.y + w->a.border_width);
840 paint_all (Display *dpy, XserverRegion region)
850 r.width = root_width;
851 r.height = root_height;
852 region = XFixesCreateRegion (dpy, &r, 1);
855 rootBuffer = rootPicture;
859 Pixmap rootPixmap = XCreatePixmap (dpy, root, root_width, root_height,
860 DefaultDepth (dpy, scr));
861 rootBuffer = XRenderCreatePicture (dpy, rootPixmap,
862 XRenderFindVisualFormat (dpy,
863 DefaultVisual (dpy, scr)),
865 XFreePixmap (dpy, rootPixmap);
868 XFixesSetPictureClipRegion (dpy, rootPicture, 0, 0, region);
870 XRenderComposite (dpy, PictOpSrc, blackPicture, None, rootPicture,
871 0, 0, 0, 0, 0, 0, root_width, root_height);
876 for (w = list; w; w = w->next)
882 /* never painted, ignore it */
887 XRenderPictureAttributes pa;
888 XRenderPictFormat *format;
889 Drawable draw = w->id;
891 #if HAS_NAME_WINDOW_PIXMAP
892 if (hasNamePixmap && !w->pixmap)
893 w->pixmap = XCompositeNameWindowPixmap (dpy, w->id);
897 format = XRenderFindVisualFormat (dpy, w->a.visual);
898 pa.subwindow_mode = IncludeInferiors;
899 w->picture = XRenderCreatePicture (dpy, draw,
905 printf (" 0x%x", w->id);
911 set_ignore (dpy, NextRequest (dpy));
912 XFixesDestroyRegion (dpy, w->borderSize);
913 w->borderSize = None;
917 XFixesDestroyRegion (dpy, w->extents);
922 XFixesDestroyRegion (dpy, w->borderClip);
923 w->borderClip = None;
927 w->borderSize = border_size (dpy, w);
929 w->extents = win_extents (dpy, w);
930 if (w->mode == WINDOW_SOLID)
933 #if HAS_NAME_WINDOW_PIXMAP
936 wid = w->a.width + w->a.border_width * 2;
937 hei = w->a.height + w->a.border_width * 2;
939 x = w->a.x + w->a.border_width;
940 y = w->a.y + w->a.border_width;
944 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
945 set_ignore (dpy, NextRequest (dpy));
946 XFixesSubtractRegion (dpy, region, region, w->borderSize);
947 set_ignore (dpy, NextRequest (dpy));
948 XRenderComposite (dpy, PictOpSrc, w->picture, None, rootBuffer,
954 w->borderClip = XFixesCreateRegion (dpy, 0, 0);
955 XFixesCopyRegion (dpy, w->borderClip, region);
964 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
966 for (w = t; w; w = w->prev_trans)
968 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, w->borderClip);
972 case CompServerShadows:
973 set_ignore (dpy, NextRequest (dpy));
974 if (w->opacity != OPAQUE && !w->shadowPict)
975 w->shadowPict = solid_picture (dpy, True,
976 (double) w->opacity / OPAQUE * 0.3,
978 XRenderComposite (dpy, PictOpOver,
979 w->shadowPict ? w->shadowPict : transBlackPicture,
980 w->picture, rootBuffer,
982 w->a.x + w->shadow_dx,
983 w->a.y + w->shadow_dy,
984 w->shadow_width, w->shadow_height);
986 case CompClientShadows:
989 XRenderComposite (dpy, PictOpOver, blackPicture, w->shadow, rootBuffer,
991 w->a.x + w->shadow_dx,
992 w->a.y + w->shadow_dy,
993 w->shadow_width, w->shadow_height);
997 if (w->opacity != OPAQUE && !w->alphaPict)
998 w->alphaPict = solid_picture (dpy, False,
999 (double) w->opacity / OPAQUE, 0, 0, 0);
1000 if (w->mode == WINDOW_TRANS)
1003 #if HAS_NAME_WINDOW_PIXMAP
1006 wid = w->a.width + w->a.border_width * 2;
1007 hei = w->a.height + w->a.border_width * 2;
1009 x = w->a.x + w->a.border_width;
1010 y = w->a.y + w->a.border_width;
1014 set_ignore (dpy, NextRequest (dpy));
1015 XRenderComposite (dpy, PictOpOver, w->picture, w->alphaPict, rootBuffer,
1019 else if (w->mode == WINDOW_ARGB)
1022 #if HAS_NAME_WINDOW_PIXMAP
1025 wid = w->a.width + w->a.border_width * 2;
1026 hei = w->a.height + w->a.border_width * 2;
1028 x = w->a.x + w->a.border_width;
1029 y = w->a.y + w->a.border_width;
1033 set_ignore (dpy, NextRequest (dpy));
1034 XRenderComposite (dpy, PictOpOver, w->picture, w->alphaPict, rootBuffer,
1038 XFixesDestroyRegion (dpy, w->borderClip);
1039 w->borderClip = None;
1041 XFixesDestroyRegion (dpy, region);
1042 if (rootBuffer != rootPicture)
1044 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, None);
1045 XRenderComposite (dpy, PictOpSrc, rootBuffer, None, rootPicture,
1046 0, 0, 0, 0, 0, 0, root_width, root_height);
1051 add_damage (Display *dpy, XserverRegion damage)
1055 XFixesUnionRegion (dpy, allDamage, allDamage, damage);
1056 XFixesDestroyRegion (dpy, damage);
1063 repair_win (Display *dpy, win *w)
1065 XserverRegion parts;
1069 parts = win_extents (dpy, w);
1070 set_ignore (dpy, NextRequest (dpy));
1071 XDamageSubtract (dpy, w->damage, None, None);
1076 parts = XFixesCreateRegion (dpy, 0, 0);
1077 set_ignore (dpy, NextRequest (dpy));
1078 XDamageSubtract (dpy, w->damage, None, parts);
1079 XFixesTranslateRegion (dpy, parts,
1080 w->a.x + w->a.border_width,
1081 w->a.y + w->a.border_width);
1082 if (compMode == CompServerShadows)
1084 o = XFixesCreateRegion (dpy, 0, 0);
1085 XFixesCopyRegion (dpy, o, parts);
1086 XFixesTranslateRegion (dpy, o, w->shadow_dx, w->shadow_dy);
1087 XFixesUnionRegion (dpy, parts, parts, o);
1088 XFixesDestroyRegion (dpy, o);
1091 add_damage (dpy, parts);
1096 map_win (Display *dpy, Window id, unsigned long sequence, Bool fade)
1098 win *w = find_win (dpy, id);
1104 w->a.map_state = IsViewable;
1106 /* This needs to be here or else we lose transparency messages */
1107 XSelectInput (dpy, id, PropertyChangeMask);
1110 w->damage_bounds.x = w->damage_bounds.y = 0;
1111 w->damage_bounds.width = w->damage_bounds.height = 0;
1115 if (fade && fadeWindows)
1116 set_fade (dpy, w, 0, get_opacity_percent (dpy, w, 1.0), fade_in_step, 0, False, True, True);
1120 finish_unmap_win (Display *dpy, win *w)
1126 if (w->extents != None)
1128 add_damage (dpy, w->extents); /* destroys region */
1132 #if HAS_NAME_WINDOW_PIXMAP
1135 XFreePixmap (dpy, w->pixmap);
1142 set_ignore (dpy, NextRequest (dpy));
1143 XRenderFreePicture (dpy, w->picture);
1147 /* don't care about properties anymore */
1148 set_ignore (dpy, NextRequest (dpy));
1149 XSelectInput(dpy, w->id, 0);
1153 set_ignore (dpy, NextRequest (dpy));
1154 XFixesDestroyRegion (dpy, w->borderSize);
1155 w->borderSize = None;
1159 XRenderFreePicture (dpy, w->shadow);
1164 XFixesDestroyRegion (dpy, w->borderClip);
1165 w->borderClip = None;
1171 #if HAS_NAME_WINDOW_PIXMAP
1173 unmap_callback (Display *dpy, win *w, Bool gone)
1175 finish_unmap_win (dpy, w);
1180 unmap_win (Display *dpy, Window id, Bool fade)
1182 win *w = find_win (dpy, id);
1185 w->a.map_state = IsUnmapped;
1186 #if HAS_NAME_WINDOW_PIXMAP
1187 if (w->pixmap && fade && fadeWindows)
1188 set_fade (dpy, w, w->opacity*1.0/OPAQUE, 0.0, fade_out_step, unmap_callback, False, False, True);
1191 finish_unmap_win (dpy, w);
1194 /* Get the opacity prop from window
1199 get_opacity_prop(Display *dpy, win *w, unsigned int def)
1203 unsigned long n, left;
1205 unsigned char *data;
1206 int result = XGetWindowProperty(dpy, w->id, opacityAtom, 0L, 1L, False,
1207 XA_CARDINAL, &actual, &format,
1209 if (result == Success && data != None)
1212 memcpy (&i, data, sizeof (unsigned int));
1213 XFree( (void *) data);
1219 /* Get the opacity property from the window in a percent format
1221 otherwise: the value
1224 get_opacity_percent(Display *dpy, win *w, double def)
1226 unsigned int opacity = get_opacity_prop (dpy, w, (unsigned int)(OPAQUE*def));
1228 return opacity*1.0/OPAQUE;
1231 /* determine mode for window all in one place.
1232 Future might check for menu flag and other cool things
1236 get_wintype_prop(Display * dpy, Window w)
1240 unsigned long n, left;
1242 unsigned char *data;
1243 int result = XGetWindowProperty (dpy, w, winTypeAtom, 0L, 1L, False,
1244 XA_ATOM, &actual, &format,
1247 if (result == Success && data != None)
1250 memcpy (&a, data, sizeof (Atom));
1251 XFree ( (void *) data);
1254 return winNormalAtom;
1258 determine_mode(Display *dpy, win *w)
1261 XRenderPictFormat *format;
1262 unsigned int default_opacity;
1264 /* if trans prop == -1 fall back on previous tests*/
1268 XRenderFreePicture (dpy, w->alphaPict);
1269 w->alphaPict = None;
1273 XRenderFreePicture (dpy, w->shadowPict);
1274 w->shadowPict = None;
1277 if (w->a.class == InputOnly)
1283 format = XRenderFindVisualFormat (dpy, w->a.visual);
1286 if (format && format->type == PictTypeDirect && format->direct.alphaMask)
1290 else if (w->opacity != OPAQUE)
1292 mode = WINDOW_TRANS;
1296 mode = WINDOW_SOLID;
1301 XserverRegion damage;
1302 damage = XFixesCreateRegion (dpy, 0, 0);
1303 XFixesCopyRegion (dpy, damage, w->extents);
1304 add_damage (dpy, damage);
1309 determine_wintype (Display *dpy, Window w)
1311 Window root_return, parent_return;
1313 unsigned int nchildren, i;
1316 type = get_wintype_prop (dpy, w);
1317 if (type != winNormalAtom)
1320 if (!XQueryTree (dpy, w, &root_return, &parent_return, &children,
1323 /* XQueryTree failed. */
1324 return winNormalAtom;
1327 for (i = 0;i < nchildren;i++)
1329 type = determine_wintype (dpy, children[i]);
1330 if (type != winNormalAtom)
1334 return winNormalAtom;
1338 add_win (Display *dpy, Window id, Window prev)
1340 win *new = malloc (sizeof (win));
1347 for (p = &list; *p; p = &(*p)->next)
1348 if ((*p)->id == prev)
1354 set_ignore (dpy, NextRequest (dpy));
1355 if (!XGetWindowAttributes (dpy, id, &new->a))
1362 new->usable = False;
1364 #if HAS_NAME_WINDOW_PIXMAP
1367 new->picture = None;
1368 if (new->a.class == InputOnly)
1370 new->damage_sequence = 0;
1375 new->damage_sequence = NextRequest (dpy);
1376 new->damage = XDamageCreate (dpy, id, XDamageReportNonEmpty);
1378 new->alphaPict = None;
1379 new->shadowPict = None;
1380 new->borderSize = None;
1381 new->extents = None;
1385 new->shadow_width = 0;
1386 new->shadow_height = 0;
1387 new->opacity = OPAQUE;
1389 new->borderClip = None;
1390 new->prev_trans = 0;
1392 /* moved mode setting to one place */
1393 new->opacity = get_opacity_prop (dpy, new, OPAQUE);
1394 new->windowType = determine_wintype (dpy, new->id);
1395 determine_mode (dpy, new);
1399 if (new->a.map_state == IsViewable)
1400 map_win (dpy, id, new->damage_sequence - 1, True);
1404 restack_win (Display *dpy, win *w, Window new_above)
1409 old_above = w->next->id;
1412 if (old_above != new_above)
1417 for (prev = &list; *prev; prev = &(*prev)->next)
1423 for (prev = &list; *prev; prev = &(*prev)->next)
1425 if ((*prev)->id == new_above)
1434 configure_win (Display *dpy, XConfigureEvent *ce)
1436 win *w = find_win (dpy, ce->window);
1438 XserverRegion damage = None;
1442 if (ce->window == root)
1446 XRenderFreePicture (dpy, rootBuffer);
1449 root_width = ce->width;
1450 root_height = ce->height;
1458 damage = XFixesCreateRegion (dpy, 0, 0);
1459 if (w->extents != None)
1460 XFixesCopyRegion (dpy, damage, w->extents);
1464 if (w->a.width != ce->width || w->a.height != ce->height)
1466 #if HAS_NAME_WINDOW_PIXMAP
1469 XFreePixmap (dpy, w->pixmap);
1473 XRenderFreePicture (dpy, w->picture);
1480 XRenderFreePicture (dpy, w->shadow);
1484 w->a.width = ce->width;
1485 w->a.height = ce->height;
1486 w->a.border_width = ce->border_width;
1487 w->a.override_redirect = ce->override_redirect;
1488 restack_win (dpy, w, ce->above);
1491 XserverRegion extents = win_extents (dpy, w);
1492 XFixesUnionRegion (dpy, damage, damage, extents);
1493 XFixesDestroyRegion (dpy, extents);
1494 add_damage (dpy, damage);
1500 circulate_win (Display *dpy, XCirculateEvent *ce)
1502 win *w = find_win (dpy, ce->window);
1505 if (ce->place == PlaceOnTop)
1506 new_above = list->id;
1509 restack_win (dpy, w, new_above);
1514 finish_destroy_win (Display *dpy, Window id, Bool gone)
1518 for (prev = &list; (w = *prev); prev = &w->next)
1522 finish_unmap_win (dpy, w);
1526 set_ignore (dpy, NextRequest (dpy));
1527 XRenderFreePicture (dpy, w->picture);
1532 XRenderFreePicture (dpy, w->alphaPict);
1533 w->alphaPict = None;
1537 XRenderFreePicture (dpy, w->shadowPict);
1538 w->shadowPict = None;
1540 if (w->damage != None)
1542 set_ignore (dpy, NextRequest (dpy));
1543 XDamageDestroy (dpy, w->damage);
1546 cleanup_fade (dpy, w);
1552 #if HAS_NAME_WINDOW_PIXMAP
1554 destroy_callback (Display *dpy, win *w, Bool gone)
1556 finish_destroy_win (dpy, w->id, gone);
1561 destroy_win (Display *dpy, Window id, Bool gone, Bool fade)
1563 win *w = find_win (dpy, id);
1564 #if HAS_NAME_WINDOW_PIXMAP
1565 if (w && w->pixmap && fade && fadeWindows)
1566 set_fade (dpy, w, w->opacity*1.0/OPAQUE, 0.0, fade_out_step, destroy_callback, gone, False, True);
1570 finish_destroy_win (dpy, id, gone);
1578 printf ("\t%08lx: %d x %d + %d + %d (%d)\n", w->id,
1579 w->a.width, w->a.height, w->a.x, w->a.y, w->a.border_width);
1588 printf ("windows:\n");
1589 for (w = list; w; w = w->next)
1595 damage_win (Display *dpy, XDamageNotifyEvent *de)
1597 win *w = find_win (dpy, de->drawable);
1604 if (w->damage_bounds.width == 0 || w->damage_bounds.height == 0)
1606 w->damage_bounds = de->area;
1610 if (de->area.x < w->damage_bounds.x)
1612 w->damage_bounds.width += (w->damage_bounds.x - de->area.x);
1613 w->damage_bounds.x = de->area.x;
1615 if (de->area.y < w->damage_bounds.y)
1617 w->damage_bounds.height += (w->damage_bounds.y - de->area.y);
1618 w->damage_bounds.y = de->area.y;
1620 if (de->area.x + de->area.width > w->damage_bounds.x + w->damage_bounds.width)
1621 w->damage_bounds.width = de->area.x + de->area.width - w->damage_bounds.x;
1622 if (de->area.y + de->area.height > w->damage_bounds.y + w->damage_bounds.height)
1623 w->damage_bounds.height = de->area.y + de->area.height - w->damage_bounds.y;
1626 printf ("unusable damage %d, %d: %d x %d bounds %d, %d: %d x %d\n",
1633 w->damage_bounds.width,
1634 w->damage_bounds.height);
1636 if (w->damage_bounds.x <= 0 &&
1637 w->damage_bounds.y <= 0 &&
1638 w->a.width <= w->damage_bounds.x + w->damage_bounds.width &&
1639 w->a.height <= w->damage_bounds.y + w->damage_bounds.height)
1643 set_fade (dpy, w, 0, get_opacity_percent (dpy, w, 1.0), fade_in_step, 0, False, True, True);
1649 repair_win (dpy, w);
1653 error (Display *dpy, XErrorEvent *ev)
1658 if (should_ignore (dpy, ev->serial))
1661 if (ev->request_code == composite_opcode &&
1662 ev->minor_code == X_CompositeRedirectSubwindows)
1664 fprintf (stderr, "Another composite manager is already running\n");
1668 o = ev->error_code - xfixes_error;
1670 case BadRegion: name = "BadRegion"; break;
1673 o = ev->error_code - damage_error;
1675 case BadDamage: name = "BadDamage"; break;
1678 o = ev->error_code - render_error;
1680 case BadPictFormat: name ="BadPictFormat"; break;
1681 case BadPicture: name ="BadPicture"; break;
1682 case BadPictOp: name ="BadPictOp"; break;
1683 case BadGlyphSet: name ="BadGlyphSet"; break;
1684 case BadGlyph: name ="BadGlyph"; break;
1688 printf ("error %d request %d minor %d serial %d\n",
1689 ev->error_code, ev->request_code, ev->minor_code, ev->serial);
1691 /* abort (); this is just annoying to most people */
1696 expose_root (Display *dpy, Window root, XRectangle *rects, int nrects)
1698 XserverRegion region = XFixesCreateRegion (dpy, rects, nrects);
1700 add_damage (dpy, region);
1705 ev_serial (XEvent *ev)
1707 if (ev->type & 0x7f != KeymapNotify)
1708 return ev->xany.serial;
1709 return NextRequest (ev->xany.display);
1714 ev_name (XEvent *ev)
1716 static char buf[128];
1717 switch (ev->type & 0x7f) {
1724 case ReparentNotify:
1726 case CirculateNotify:
1729 if (ev->type == damage_event + XDamageNotify)
1731 sprintf (buf, "Event %d", ev->type);
1737 ev_window (XEvent *ev)
1741 return ev->xexpose.window;
1743 return ev->xmap.window;
1745 return ev->xunmap.window;
1746 case ReparentNotify:
1747 return ev->xreparent.window;
1748 case CirculateNotify:
1749 return ev->xcirculate.window;
1751 if (ev->type == damage_event + XDamageNotify)
1752 return ((XDamageNotifyEvent *) ev)->drawable;
1758 usage (char *program)
1760 fprintf (stderr, "usage: %s [-d display] [-acCfFnsS]\n", program);
1761 fprintf (stderr, "Options\n");
1762 fprintf (stderr, " -d display\n Specifies which display should be managed.\n");
1763 fprintf (stderr, " -a\n Use automatic server-side compositing. Faster, but no special effects.\n");
1764 fprintf (stderr, " -c\n Draw client-side shadows with fuzzy edges.\n");
1765 fprintf (stderr, " -C\n Avoid drawing shadows on dock/panel windows.\n");
1766 fprintf (stderr, " -f\n Fade windows in/out when opening/closing.\n");
1767 fprintf (stderr, " -F\n Fade windows during opacity changes.\n");
1768 fprintf (stderr, " -n\n Normal client-side compositing with transparency support\n");
1769 fprintf (stderr, " -s\n Draw server-side shadows with sharp edges.\n");
1770 fprintf (stderr, " -S\n Enable synchronous operation (for debugging).\n");
1775 main (int argc, char **argv)
1778 Window root_return, parent_return;
1782 unsigned int nchildren;
1784 XRenderPictureAttributes pa;
1786 XRectangle *expose_rects = 0;
1787 int size_expose = 0;
1794 int composite_major, composite_minor;
1798 while ((o = getopt (argc, argv, "d:scnfFCaS")) != -1)
1805 compMode = CompServerShadows;
1808 compMode = CompClientShadows;
1811 excludeDockShadows = True;
1814 compMode = CompSimple;
1823 autoRedirect = True;
1834 dpy = XOpenDisplay (display);
1837 fprintf (stderr, "Can't open display\n");
1840 XSetErrorHandler (error);
1842 XSynchronize (dpy, 1);
1843 scr = DefaultScreen (dpy);
1844 root = RootWindow (dpy, scr);
1846 if (!XRenderQueryExtension (dpy, &render_event, &render_error))
1848 fprintf (stderr, "No render extension\n");
1851 if (!XQueryExtension (dpy, COMPOSITE_NAME, &composite_opcode,
1852 &composite_event, &composite_error))
1854 fprintf (stderr, "No composite extension\n");
1857 XCompositeQueryVersion (dpy, &composite_major, &composite_minor);
1858 #if HAS_NAME_WINDOW_PIXMAP
1859 if (composite_major > 0 || composite_minor >= 2)
1860 hasNamePixmap = True;
1863 if (!XDamageQueryExtension (dpy, &damage_event, &damage_error))
1865 fprintf (stderr, "No damage extension\n");
1868 if (!XFixesQueryExtension (dpy, &xfixes_event, &xfixes_error))
1870 fprintf (stderr, "No XFixes extension\n");
1874 opacityAtom = XInternAtom (dpy, OPACITY_PROP, False);
1875 winTypeAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE", False);
1876 winDesktopAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DESKTOP", False);
1877 winDockAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DOCK", False);
1878 winToolbarAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_TOOLBAR", False);
1879 winMenuAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_MENU", False);
1880 winUtilAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_UTILITY", False);
1881 winSplashAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_SPLASH", False);
1882 winDialogAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1883 winNormalAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_NORMAL", False);
1885 pa.subwindow_mode = IncludeInferiors;
1887 if (compMode == CompClientShadows)
1888 gaussianMap = make_gaussian_map(dpy, shadowRadius);
1890 root_width = DisplayWidth (dpy, scr);
1891 root_height = DisplayHeight (dpy, scr);
1893 rootPicture = XRenderCreatePicture (dpy, root,
1894 XRenderFindVisualFormat (dpy,
1895 DefaultVisual (dpy, scr)),
1898 blackPicture = solid_picture (dpy, True, 1, 0, 0, 0);
1899 if (compMode == CompServerShadows)
1900 transBlackPicture = solid_picture (dpy, True, 0.3, 0, 0, 0);
1905 XCompositeRedirectSubwindows (dpy, root, CompositeRedirectAutomatic);
1908 XCompositeRedirectSubwindows (dpy, root, CompositeRedirectManual);
1909 XSelectInput (dpy, root,
1910 SubstructureNotifyMask|
1912 StructureNotifyMask|
1913 PropertyChangeMask);
1914 XQueryTree (dpy, root, &root_return, &parent_return, &children, &nchildren);
1915 for (i = 0; i < nchildren; i++)
1916 add_win (dpy, children[i], i ? children[i-1] : None);
1919 XUngrabServer (dpy);
1920 ufd.fd = ConnectionNumber (dpy);
1921 ufd.events = POLLIN;
1923 paint_all (dpy, None);
1932 if (poll (&ufd, 1, fade_timeout()) == 0)
1939 XNextEvent (dpy, &ev);
1940 if (ev.type & 0x7f != KeymapNotify)
1941 discard_ignore (dpy, ev.xany.serial);
1943 printf ("event %10.10s serial 0x%08x window 0x%08x\n",
1944 ev_name(&ev), ev_serial (&ev), ev_window (&ev));
1946 if (!autoRedirect) switch (ev.type) {
1948 add_win (dpy, ev.xcreatewindow.window, 0);
1950 case ConfigureNotify:
1951 configure_win (dpy, &ev.xconfigure);
1954 destroy_win (dpy, ev.xdestroywindow.window, True, True);
1957 map_win (dpy, ev.xmap.window, ev.xmap.serial, True);
1960 unmap_win (dpy, ev.xunmap.window, True);
1962 case ReparentNotify:
1963 if (ev.xreparent.parent == root)
1964 add_win (dpy, ev.xreparent.window, 0);
1966 destroy_win (dpy, ev.xreparent.window, False, True);
1968 case CirculateNotify:
1969 circulate_win (dpy, &ev.xcirculate);
1972 if (ev.xexpose.window == root)
1974 int more = ev.xexpose.count + 1;
1975 if (n_expose == size_expose)
1979 expose_rects = realloc (expose_rects,
1980 (size_expose + more) *
1981 sizeof (XRectangle));
1982 size_expose += more;
1986 expose_rects = malloc (more * sizeof (XRectangle));
1990 expose_rects[n_expose].x = ev.xexpose.x;
1991 expose_rects[n_expose].y = ev.xexpose.y;
1992 expose_rects[n_expose].width = ev.xexpose.width;
1993 expose_rects[n_expose].height = ev.xexpose.height;
1995 if (ev.xexpose.count == 0)
1997 expose_root (dpy, root, expose_rects, n_expose);
2002 case PropertyNotify:
2003 for (p = 0; backgroundProps[p]; p++)
2005 if (ev.xproperty.atom == XInternAtom (dpy, backgroundProps[p], False))
2009 XClearArea (dpy, root, 0, 0, 0, 0, True);
2010 XRenderFreePicture (dpy, rootTile);
2016 /* check if Trans property was changed */
2017 if (ev.xproperty.atom == opacityAtom)
2019 /* reset mode and redraw window */
2020 win * w = find_win(dpy, ev.xproperty.window);
2024 set_fade (dpy, w, w->opacity*1.0/OPAQUE, get_opacity_percent (dpy, w, 1.0),
2025 fade_out_step, 0, False, True, False);
2028 w->opacity = get_opacity_prop(dpy, w, OPAQUE);
2029 determine_mode(dpy, w);
2032 XRenderFreePicture (dpy, w->shadow);
2034 w->extents = win_extents (dpy, w);
2041 if (ev.type == damage_event + XDamageNotify)
2042 damage_win (dpy, (XDamageNotifyEvent *) &ev);
2045 } while (QLength (dpy));
2046 if (allDamage && !autoRedirect)
2049 paint_all (dpy, allDamage);
2053 clipChanged = False;