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 {
100 void (*callback) (Display *dpy, win *w, Bool gone);
112 Picture blackPicture;
113 Picture transBlackPicture;
115 XserverRegion allDamage;
117 #if HAS_NAME_WINDOW_PIXMAP
120 int root_height, root_width;
121 ignore *ignore_head, **ignore_tail = &ignore_head;
122 int xfixes_event, xfixes_error;
123 int damage_event, damage_error;
124 int composite_event, composite_error;
125 int render_event, render_error;
127 int composite_opcode;
129 /* find these once and be done with it */
132 /* opacity property name; sometime soon I'll write up an EWMH spec for it */
133 #define OPACITY_PROP "_NET_WM_WINDOW_OPACITY"
135 #define TRANSLUCENT 0xe0000000
136 #define OPAQUE 0xffffffff
140 #define WINDOW_SOLID 0
141 #define WINDOW_TRANS 1
142 #define WINDOW_ARGB 2
144 #define TRANS_OPACITY 0.75
146 #define DEBUG_REPAINT 0
147 #define DEBUG_EVENTS 0
148 #define MONITOR_REPAINT 0
151 #define SHARP_SHADOW 0
153 typedef enum _compMode {
154 CompSimple, /* looks like a regular X server */
155 CompServerShadows, /* use window alpha for shadow; sharp, but precise */
156 CompClientShadows, /* use window extents for shadow, blurred */
160 determine_mode(Display *dpy, win *w);
162 CompMode compMode = CompSimple;
164 int shadowRadius = 12;
166 double fade_step = 0.05;
171 Bool autoRedirect = False;
174 get_time_in_milliseconds ()
178 gettimeofday (&tv, NULL);
179 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
187 for (f = fades; f; f = f->next)
196 dequeue_fade (Display *dpy, fade *f)
200 for (prev = &fades; *prev; prev = &(*prev)->next)
205 (*f->callback) (dpy, f->w, f->gone);
212 cleanup_fade (Display *dpy, win *w)
214 fade *f = find_fade (w);
216 dequeue_fade (dpy, f);
220 enqueue_fade (Display *dpy, fade *f)
223 fade_time = get_time_in_milliseconds () + fade_delta;
229 set_fade (Display *dpy, win *w, Bool in,
230 void (*callback) (Display *dpy, win *w, Bool gone),
238 f = malloc (sizeof (fade));
245 enqueue_fade (dpy, f);
250 f->step = -fade_step;
251 f->callback = callback;
253 w->opacity = f->cur * OPAQUE;
255 printf ("set_fade start %g step %g\n", f->cur, f->step);
257 determine_mode (dpy, w);
267 now = get_time_in_milliseconds();
268 delta = fade_time - now;
271 /* printf ("timeout %d\n", delta); */
276 run_fades (Display *dpy)
278 int now = get_time_in_milliseconds();
283 printf ("run fades\n");
285 if (fade_time - now > 0)
287 steps = 1 + (now - fade_time) / fade_delta;
288 for (next = fades; f = next; )
292 f->cur += f->step * steps;
298 printf ("opacity now %g\n", f->cur);
300 w->opacity = f->cur * OPAQUE;
304 dequeue_fade (dpy, f);
309 dequeue_fade (dpy, f);
311 determine_mode (dpy, w);
313 fade_time = now + fade_delta;
316 #define SHADOW_OPACITY 0.75
317 #define SHADOW_OFFSET_X (-shadowRadius * 5 / 4)
318 #define SHADOW_OFFSET_Y (-shadowRadius * 5 / 4)
321 gaussian (double r, double x, double y)
323 return ((1 / (sqrt (2 * M_PI * r))) *
324 exp ((- (x * x + y * y)) / (2 * r * r)));
329 make_gaussian_map (Display *dpy, double r)
332 int size = ((int) ceil ((r * 3)) + 1) & ~1;
333 int center = size / 2;
338 c = malloc (sizeof (conv) + size * size * sizeof (double));
340 c->data = (double *) (c + 1);
342 for (y = 0; y < size; y++)
343 for (x = 0; x < size; x++)
345 g = gaussian (r, (double) (x - center), (double) (y - center));
347 c->data[y * size + x] = g;
349 /* printf ("gaussian total %f\n", t); */
350 for (y = 0; y < size; y++)
351 for (x = 0; x < size; x++)
353 c->data[y*size + x] /= t;
359 * A picture will help
361 * -center 0 width width+center
362 * -center +-----+-------------------+-----+
365 * 0 +-----+-------------------+-----+
369 * height +-----+-------------------+-----+
372 * center +-----+-------------------+-----+
376 sum_gaussian (conv *map, double opacity, int x, int y, int width, int height)
380 double *g_line = map->data;
381 int g_size = map->size;
382 int center = g_size / 2;
383 int fx_start, fx_end;
384 int fy_start, fy_end;
388 * Compute set of filter values which are "in range",
389 * that's the set with:
390 * 0 <= x + (fx-center) && x + (fx-center) < width &&
391 * 0 <= y + (fy-center) && y + (fy-center) < height
393 * 0 <= x + (fx - center) x + fx - center < width
394 * center - x <= fx fx < width + center - x
397 fx_start = center - x;
400 fx_end = width + center - x;
404 fy_start = center - y;
407 fy_end = height + center - y;
411 g_line = g_line + fy_start * g_size + fx_start;
414 for (fy = fy_start; fy < fy_end; fy++)
419 for (fx = fx_start; fx < fx_end; fx++)
425 return ((unsigned char) (v * opacity * 255.0));
429 make_shadow (Display *dpy, double opacity, int width, int height)
433 int gsize = gaussianMap->size;
435 int swidth = width + gsize;
436 int sheight = height + gsize;
437 int center = gsize / 2;
442 data = malloc (swidth * sheight * sizeof (unsigned char));
445 ximage = XCreateImage (dpy,
446 DefaultVisual(dpy, DefaultScreen(dpy)),
451 swidth, sheight, 8, swidth * sizeof (unsigned char));
458 * Build the gaussian in sections
462 * center (fill the complete data array)
465 d = sum_gaussian (gaussianMap, opacity, center, center, width, height);
466 memset(data, d, sheight * swidth);
472 if (ylimit > sheight / 2)
473 ylimit = (sheight + 1) / 2;
475 if (xlimit > swidth / 2)
476 xlimit = (swidth + 1) / 2;
478 for (y = 0; y < ylimit; y++)
479 for (x = 0; x < xlimit; x++)
481 d = sum_gaussian (gaussianMap, opacity, x - center, y - center, width, height);
482 data[y * swidth + x] = d;
483 data[(sheight - y - 1) * swidth + x] = d;
484 data[(sheight - y - 1) * swidth + (swidth - x - 1)] = d;
485 data[y * swidth + (swidth - x - 1)] = d;
491 x_diff = swidth - (gsize * 2);
492 if (x_diff > 0 && ylimit > 0)
494 for (y = 0; y < ylimit; y++)
496 d = sum_gaussian (gaussianMap, opacity, center, y - center, width, height);
497 memset (&data[y * swidth + gsize], d, x_diff);
498 memset (&data[(sheight - y - 1) * swidth + gsize], d, x_diff);
506 for (x = 0; x < xlimit; x++)
508 d = sum_gaussian (gaussianMap, opacity, x - center, center, width, height);
509 for (y = gsize; y < sheight - gsize; y++)
511 data[y * swidth + x] = d;
512 data[y * swidth + (swidth - x - 1)] = d;
520 shadow_picture (Display *dpy, double opacity, int width, int height, int *wp, int *hp)
524 Picture shadowPicture;
527 shadowImage = make_shadow (dpy, opacity, width, height);
530 shadowPixmap = XCreatePixmap (dpy, root,
534 shadowPicture = XRenderCreatePicture (dpy, shadowPixmap,
535 XRenderFindStandardFormat (dpy, PictStandardA8),
537 gc = XCreateGC (dpy, shadowPixmap, 0, 0);
539 XPutImage (dpy, shadowPixmap, gc, shadowImage, 0, 0, 0, 0,
541 shadowImage->height);
542 *wp = shadowImage->width;
543 *hp = shadowImage->height;
545 XDestroyImage (shadowImage);
546 XFreePixmap (dpy, shadowPixmap);
547 return shadowPicture;
551 solid_picture (Display *dpy, Bool argb, double a, double r, double g, double b)
555 XRenderPictureAttributes pa;
558 pixmap = XCreatePixmap (dpy, root, 1, 1, argb ? 32 : 8);
560 picture = XRenderCreatePicture (dpy, pixmap,
561 XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8),
564 c.alpha = a * 0xffff;
566 c.green = g * 0xffff;
568 XRenderFillRectangle (dpy, PictOpSrc, picture, &c, 0, 0, 1, 1);
569 XFreePixmap (dpy, pixmap);
574 discard_ignore (Display *dpy, unsigned long sequence)
578 if ((long) (sequence - ignore_head->sequence) > 0)
580 ignore *next = ignore_head->next;
584 ignore_tail = &ignore_head;
592 set_ignore (Display *dpy, unsigned long sequence)
594 ignore *i = malloc (sizeof (ignore));
597 i->sequence = sequence;
600 ignore_tail = &i->next;
604 should_ignore (Display *dpy, unsigned long sequence)
606 discard_ignore (dpy, sequence);
607 return ignore_head && ignore_head->sequence == sequence;
611 find_win (Display *dpy, Window id)
615 for (w = list; w; w = w->next)
621 static char *backgroundProps[] = {
628 root_tile (Display *dpy)
634 unsigned long nitems;
635 unsigned long bytes_after;
638 XRenderPictureAttributes pa;
642 for (p = 0; backgroundProps[p]; p++)
644 if (XGetWindowProperty (dpy, root, XInternAtom (dpy, backgroundProps[p], False),
645 0, 4, False, AnyPropertyType,
646 &actual_type, &actual_format, &nitems, &bytes_after, &prop) == Success &&
647 actual_type == XInternAtom (dpy, "PIXMAP", False) && actual_format == 32 && nitems == 1)
649 memcpy (&pixmap, prop, 4);
657 pixmap = XCreatePixmap (dpy, root, 1, 1, DefaultDepth (dpy, scr));
661 picture = XRenderCreatePicture (dpy, pixmap,
662 XRenderFindVisualFormat (dpy,
663 DefaultVisual (dpy, scr)),
669 c.red = c.green = c.blue = 0x8080;
671 XRenderFillRectangle (dpy, PictOpSrc, picture, &c,
678 paint_root (Display *dpy)
681 rootTile = root_tile (dpy);
683 XRenderComposite (dpy, PictOpSrc,
684 rootTile, None, rootBuffer,
685 0, 0, 0, 0, 0, 0, root_width, root_height);
689 win_extents (Display *dpy, win *w)
695 r.width = w->a.width + w->a.border_width * 2;
696 r.height = w->a.height + w->a.border_width * 2;
697 if (compMode != CompSimple)
699 if (compMode == CompServerShadows || w->mode != WINDOW_ARGB)
703 if (compMode == CompServerShadows)
707 w->shadow_width = w->a.width;
708 w->shadow_height = w->a.height;
712 w->shadow_dx = SHADOW_OFFSET_X;
713 w->shadow_dy = SHADOW_OFFSET_Y;
716 double opacity = SHADOW_OPACITY;
717 if (w->mode == WINDOW_TRANS)
718 opacity = opacity * TRANS_OPACITY;
719 w->shadow = shadow_picture (dpy, opacity,
720 w->a.width + w->a.border_width * 2,
721 w->a.height + w->a.border_width * 2,
722 &w->shadow_width, &w->shadow_height);
725 sr.x = w->a.x + w->shadow_dx;
726 sr.y = w->a.y + w->shadow_dy;
727 sr.width = w->shadow_width;
728 sr.height = w->shadow_height;
731 r.width = (r.x + r.width) - sr.x;
736 r.height = (r.y + r.height) - sr.y;
739 if (sr.x + sr.width > r.x + r.width)
740 r.width = sr.x + sr.width - r.x;
741 if (sr.y + sr.height > r.y + r.height)
742 r.height = sr.y + sr.height - r.y;
745 return XFixesCreateRegion (dpy, &r, 1);
749 border_size (Display *dpy, win *w)
751 XserverRegion border;
753 * if window doesn't exist anymore, this will generate an error
754 * as well as not generate a region. Perhaps a better XFixes
755 * architecture would be to have a request that copies instead
756 * of creates, that way you'd just end up with an empty region
757 * instead of an invalid XID.
759 set_ignore (dpy, NextRequest (dpy));
760 border = XFixesCreateRegionFromWindow (dpy, w->id, WindowRegionBounding);
762 set_ignore (dpy, NextRequest (dpy));
763 XFixesTranslateRegion (dpy, border,
764 w->a.x + w->a.border_width,
765 w->a.y + w->a.border_width);
770 paint_all (Display *dpy, XserverRegion region)
780 r.width = root_width;
781 r.height = root_height;
782 region = XFixesCreateRegion (dpy, &r, 1);
785 rootBuffer = rootPicture;
789 Pixmap rootPixmap = XCreatePixmap (dpy, root, root_width, root_height,
790 DefaultDepth (dpy, scr));
791 rootBuffer = XRenderCreatePicture (dpy, rootPixmap,
792 XRenderFindVisualFormat (dpy,
793 DefaultVisual (dpy, scr)),
795 XFreePixmap (dpy, rootPixmap);
798 XFixesSetPictureClipRegion (dpy, rootPicture, 0, 0, region);
800 XRenderComposite (dpy, PictOpSrc, blackPicture, None, rootPicture,
801 0, 0, 0, 0, 0, 0, root_width, root_height);
806 for (w = list; w; w = w->next)
812 /* never painted, ignore it */
817 XRenderPictureAttributes pa;
818 XRenderPictFormat *format;
819 Drawable draw = w->id;
821 #if HAS_NAME_WINDOW_PIXMAP
822 if (hasNamePixmap && !w->pixmap)
823 w->pixmap = XCompositeNameWindowPixmap (dpy, w->id);
827 format = XRenderFindVisualFormat (dpy, w->a.visual);
828 pa.subwindow_mode = IncludeInferiors;
829 w->picture = XRenderCreatePicture (dpy, draw,
835 printf (" 0x%x", w->id);
841 set_ignore (dpy, NextRequest (dpy));
842 XFixesDestroyRegion (dpy, w->borderSize);
843 w->borderSize = None;
847 XFixesDestroyRegion (dpy, w->extents);
852 XFixesDestroyRegion (dpy, w->borderClip);
853 w->borderClip = None;
857 w->borderSize = border_size (dpy, w);
859 w->extents = win_extents (dpy, w);
860 if (w->mode == WINDOW_SOLID)
863 #if HAS_NAME_WINDOW_PIXMAP
866 wid = w->a.width + w->a.border_width * 2;
867 hei = w->a.height + w->a.border_width * 2;
869 x = w->a.x + w->a.border_width;
870 y = w->a.y + w->a.border_width;
874 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
875 set_ignore (dpy, NextRequest (dpy));
876 XFixesSubtractRegion (dpy, region, region, w->borderSize);
877 set_ignore (dpy, NextRequest (dpy));
878 XRenderComposite (dpy, PictOpSrc, w->picture, None, rootBuffer,
884 w->borderClip = XFixesCreateRegion (dpy, 0, 0);
885 XFixesCopyRegion (dpy, w->borderClip, region);
894 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
896 for (w = t; w; w = w->prev_trans)
898 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, w->borderClip);
902 case CompServerShadows:
903 set_ignore (dpy, NextRequest (dpy));
904 if (w->opacity != OPAQUE && !w->shadowPict)
905 w->shadowPict = solid_picture (dpy, True,
906 (double) w->opacity / OPAQUE * 0.3,
908 XRenderComposite (dpy, PictOpOver,
909 w->shadowPict ? w->shadowPict : transBlackPicture,
910 w->picture, rootBuffer,
912 w->a.x + w->shadow_dx,
913 w->a.y + w->shadow_dy,
914 w->shadow_width, w->shadow_height);
916 case CompClientShadows:
919 XRenderComposite (dpy, PictOpOver, blackPicture, w->shadow, rootBuffer,
921 w->a.x + w->shadow_dx,
922 w->a.y + w->shadow_dy,
923 w->shadow_width, w->shadow_height);
927 if (w->opacity != OPAQUE && !w->alphaPict)
928 w->alphaPict = solid_picture (dpy, False,
929 (double) w->opacity / OPAQUE, 0, 0, 0);
930 if (w->mode == WINDOW_TRANS)
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 set_ignore (dpy, NextRequest (dpy));
945 XRenderComposite (dpy, PictOpOver, w->picture, w->alphaPict, rootBuffer,
949 else if (w->mode == WINDOW_ARGB)
952 #if HAS_NAME_WINDOW_PIXMAP
955 wid = w->a.width + w->a.border_width * 2;
956 hei = w->a.height + w->a.border_width * 2;
958 x = w->a.x + w->a.border_width;
959 y = w->a.y + w->a.border_width;
963 set_ignore (dpy, NextRequest (dpy));
964 XRenderComposite (dpy, PictOpOver, w->picture, w->alphaPict, rootBuffer,
968 XFixesDestroyRegion (dpy, w->borderClip);
969 w->borderClip = None;
971 XFixesDestroyRegion (dpy, region);
972 if (rootBuffer != rootPicture)
974 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, None);
975 XRenderComposite (dpy, PictOpSrc, rootBuffer, None, rootPicture,
976 0, 0, 0, 0, 0, 0, root_width, root_height);
981 add_damage (Display *dpy, XserverRegion damage)
985 XFixesUnionRegion (dpy, allDamage, allDamage, damage);
986 XFixesDestroyRegion (dpy, damage);
993 repair_win (Display *dpy, win *w)
999 parts = win_extents (dpy, w);
1000 set_ignore (dpy, NextRequest (dpy));
1001 XDamageSubtract (dpy, w->damage, None, None);
1006 parts = XFixesCreateRegion (dpy, 0, 0);
1007 set_ignore (dpy, NextRequest (dpy));
1008 XDamageSubtract (dpy, w->damage, None, parts);
1009 XFixesTranslateRegion (dpy, parts,
1010 w->a.x + w->a.border_width,
1011 w->a.y + w->a.border_width);
1012 if (compMode == CompServerShadows)
1014 o = XFixesCreateRegion (dpy, 0, 0);
1015 XFixesCopyRegion (dpy, o, parts);
1016 XFixesTranslateRegion (dpy, o, w->shadow_dx, w->shadow_dy);
1017 XFixesUnionRegion (dpy, parts, parts, o);
1018 XFixesDestroyRegion (dpy, o);
1021 add_damage (dpy, parts);
1026 map_win (Display *dpy, Window id, unsigned long sequence, Bool fade)
1028 win *w = find_win (dpy, id);
1033 w->a.map_state = IsViewable;
1036 w->damage_bounds.x = w->damage_bounds.y = 0;
1037 w->damage_bounds.width = w->damage_bounds.height = 0;
1043 finish_unmap_win (Display *dpy, win *w)
1049 if (w->extents != None)
1051 add_damage (dpy, w->extents); /* destroys region */
1055 #if HAS_NAME_WINDOW_PIXMAP
1058 XFreePixmap (dpy, w->pixmap);
1065 set_ignore (dpy, NextRequest (dpy));
1066 XRenderFreePicture (dpy, w->picture);
1070 /* don't care about properties anymore */
1071 set_ignore (dpy, NextRequest (dpy));
1072 XSelectInput(dpy, w->id, 0);
1076 set_ignore (dpy, NextRequest (dpy));
1077 XFixesDestroyRegion (dpy, w->borderSize);
1078 w->borderSize = None;
1082 XRenderFreePicture (dpy, w->shadow);
1087 XFixesDestroyRegion (dpy, w->borderClip);
1088 w->borderClip = None;
1094 #if HAS_NAME_WINDOW_PIXMAP
1096 unmap_callback (Display *dpy, win *w, Bool gone)
1098 finish_unmap_win (dpy, w);
1103 unmap_win (Display *dpy, Window id, Bool fade)
1105 win *w = find_win (dpy, id);
1108 #if HAS_NAME_WINDOW_PIXMAP
1109 if (w->pixmap && fade && fadeWindows)
1110 set_fade (dpy, w, False, unmap_callback, False);
1113 finish_unmap_win (dpy, w);
1116 /* Get the opacity prop from window
1121 get_opacity_prop(Display *dpy, win *w, unsigned int def)
1125 unsigned long n, left;
1128 XGetWindowProperty(dpy, w->id, opacityAtom, 0L, 1L, False,
1129 XA_CARDINAL, &actual, &format,
1130 &n, &left, (unsigned char **) &data);
1134 memcpy (&i, data, sizeof (unsigned int));
1135 XFree( (void *) data);
1141 /* determine mode for window all in one place.
1142 Future might check for menu flag and other cool things
1146 determine_mode(Display *dpy, win *w)
1149 XRenderPictFormat *format;
1150 unsigned int default_opacity;
1152 /* if trans prop == -1 fall back on previous tests*/
1156 XRenderFreePicture (dpy, w->alphaPict);
1157 w->alphaPict = None;
1161 XRenderFreePicture (dpy, w->shadowPict);
1162 w->shadowPict = None;
1165 if (w->a.class == InputOnly)
1171 format = XRenderFindVisualFormat (dpy, w->a.visual);
1174 if (format && format->type == PictTypeDirect && format->direct.alphaMask)
1178 else if (w->opacity != OPAQUE)
1180 mode = WINDOW_TRANS;
1184 mode = WINDOW_SOLID;
1189 XserverRegion damage;
1190 damage = XFixesCreateRegion (dpy, 0, 0);
1191 XFixesCopyRegion (dpy, damage, w->extents);
1192 add_damage (dpy, damage);
1197 add_win (Display *dpy, Window id, Window prev)
1199 win *new = malloc (sizeof (win));
1206 for (p = &list; *p; p = &(*p)->next)
1207 if ((*p)->id == prev)
1213 set_ignore (dpy, NextRequest (dpy));
1214 if (!XGetWindowAttributes (dpy, id, &new->a))
1221 new->usable = False;
1223 #if HAS_NAME_WINDOW_PIXMAP
1226 new->picture = None;
1227 if (new->a.class == InputOnly)
1229 new->damage_sequence = 0;
1234 new->damage_sequence = NextRequest (dpy);
1235 new->damage = XDamageCreate (dpy, id, XDamageReportNonEmpty);
1237 new->alphaPict = None;
1238 new->shadowPict = None;
1239 new->borderSize = None;
1240 new->extents = None;
1244 new->shadow_width = 0;
1245 new->shadow_height = 0;
1246 new->opacity = OPAQUE;
1248 new->borderClip = None;
1249 new->prev_trans = 0;
1251 /* moved mode setting to one place */
1252 XSelectInput(dpy, id, PropertyChangeMask);
1253 new->opacity = get_opacity_prop(dpy, new, OPAQUE);
1254 determine_mode (dpy, new);
1258 if (new->a.map_state == IsViewable)
1259 map_win (dpy, id, new->damage_sequence - 1, False);
1263 restack_win (Display *dpy, win *w, Window new_above)
1268 old_above = w->next->id;
1271 if (old_above != new_above)
1276 for (prev = &list; *prev; prev = &(*prev)->next)
1282 for (prev = &list; *prev; prev = &(*prev)->next)
1284 if ((*prev)->id == new_above)
1293 configure_win (Display *dpy, XConfigureEvent *ce)
1295 win *w = find_win (dpy, ce->window);
1297 XserverRegion damage = None;
1301 if (ce->window == root)
1305 XRenderFreePicture (dpy, rootBuffer);
1308 root_width = ce->width;
1309 root_height = ce->height;
1317 damage = XFixesCreateRegion (dpy, 0, 0);
1318 if (w->extents != None)
1319 XFixesCopyRegion (dpy, damage, w->extents);
1323 if (w->a.width != ce->width || w->a.height != ce->height)
1325 #if HAS_NAME_WINDOW_PIXMAP
1328 XFreePixmap (dpy, w->pixmap);
1332 XRenderFreePicture (dpy, w->picture);
1339 XRenderFreePicture (dpy, w->shadow);
1343 w->a.width = ce->width;
1344 w->a.height = ce->height;
1345 w->a.border_width = ce->border_width;
1346 w->a.override_redirect = ce->override_redirect;
1347 restack_win (dpy, w, ce->above);
1350 XserverRegion extents = win_extents (dpy, w);
1351 XFixesUnionRegion (dpy, damage, damage, extents);
1352 XFixesDestroyRegion (dpy, extents);
1353 add_damage (dpy, damage);
1359 circulate_win (Display *dpy, XCirculateEvent *ce)
1361 win *w = find_win (dpy, ce->window);
1364 if (ce->place == PlaceOnTop)
1365 new_above = list->id;
1368 restack_win (dpy, w, new_above);
1373 finish_destroy_win (Display *dpy, Window id, Bool gone)
1377 for (prev = &list; (w = *prev); prev = &w->next)
1381 finish_unmap_win (dpy, w);
1385 set_ignore (dpy, NextRequest (dpy));
1386 XRenderFreePicture (dpy, w->picture);
1390 XRenderFreePicture (dpy, w->alphaPict);
1391 w->alphaPict = None;
1395 XRenderFreePicture (dpy, w->shadowPict);
1396 w->shadowPict = None;
1398 if (w->damage != None)
1400 set_ignore (dpy, NextRequest (dpy));
1401 XDamageDestroy (dpy, w->damage);
1403 cleanup_fade (dpy, w);
1409 #if HAS_NAME_WINDOW_PIXMAP
1411 destroy_callback (Display *dpy, win *w, Bool gone)
1413 finish_destroy_win (dpy, w->id, gone);
1418 destroy_win (Display *dpy, Window id, Bool gone, Bool fade)
1420 win *w = find_win (dpy, id);
1421 #if HAS_NAME_WINDOW_PIXMAP
1422 if (w && w->pixmap && fade && fadeWindows)
1423 set_fade (dpy, w, False, destroy_callback, gone);
1427 finish_destroy_win (dpy, id, gone);
1435 printf ("\t%08lx: %d x %d + %d + %d (%d)\n", w->id,
1436 w->a.width, w->a.height, w->a.x, w->a.y, w->a.border_width);
1445 printf ("windows:\n");
1446 for (w = list; w; w = w->next)
1452 damage_win (Display *dpy, XDamageNotifyEvent *de)
1454 win *w = find_win (dpy, de->drawable);
1461 if (w->damage_bounds.width == 0 || w->damage_bounds.height == 0)
1463 w->damage_bounds = de->area;
1467 if (de->area.x < w->damage_bounds.x)
1469 w->damage_bounds.width += (w->damage_bounds.x - de->area.x);
1470 w->damage_bounds.x = de->area.x;
1472 if (de->area.y < w->damage_bounds.y)
1474 w->damage_bounds.height += (w->damage_bounds.y - de->area.y);
1475 w->damage_bounds.y = de->area.y;
1477 if (de->area.x + de->area.width > w->damage_bounds.x + w->damage_bounds.width)
1478 w->damage_bounds.width = de->area.x + de->area.width - w->damage_bounds.x;
1479 if (de->area.y + de->area.height > w->damage_bounds.y + w->damage_bounds.height)
1480 w->damage_bounds.height = de->area.y + de->area.height - w->damage_bounds.y;
1483 printf ("unusable damage %d, %d: %d x %d bounds %d, %d: %d x %d\n",
1490 w->damage_bounds.width,
1491 w->damage_bounds.height);
1493 if (w->damage_bounds.x <= 0 &&
1494 w->damage_bounds.y <= 0 &&
1495 w->a.width <= w->damage_bounds.x + w->damage_bounds.width &&
1496 w->a.height <= w->damage_bounds.y + w->damage_bounds.height)
1500 set_fade (dpy, w, True, 0, False);
1506 repair_win (dpy, w);
1510 error (Display *dpy, XErrorEvent *ev)
1515 if (should_ignore (dpy, ev->serial))
1518 if (ev->request_code == composite_opcode &&
1519 ev->minor_code == X_CompositeRedirectSubwindows)
1521 fprintf (stderr, "Another composite manager is already running\n");
1525 o = ev->error_code - xfixes_error;
1527 case BadRegion: name = "BadRegion"; break;
1530 o = ev->error_code - damage_error;
1532 case BadDamage: name = "BadDamage"; break;
1535 o = ev->error_code - render_error;
1537 case BadPictFormat: name ="BadPictFormat"; break;
1538 case BadPicture: name ="BadPicture"; break;
1539 case BadPictOp: name ="BadPictOp"; break;
1540 case BadGlyphSet: name ="BadGlyphSet"; break;
1541 case BadGlyph: name ="BadGlyph"; break;
1545 printf ("error %d request %d minor %d serial %d\n",
1546 ev->error_code, ev->request_code, ev->minor_code, ev->serial);
1548 /* abort (); this is just annoying to most people */
1553 expose_root (Display *dpy, Window root, XRectangle *rects, int nrects)
1555 XserverRegion region = XFixesCreateRegion (dpy, rects, nrects);
1557 add_damage (dpy, region);
1562 ev_serial (XEvent *ev)
1564 if (ev->type & 0x7f != KeymapNotify)
1565 return ev->xany.serial;
1566 return NextRequest (ev->xany.display);
1571 ev_name (XEvent *ev)
1573 static char buf[128];
1574 switch (ev->type & 0x7f) {
1581 case ReparentNotify:
1583 case CirculateNotify:
1586 if (ev->type == damage_event + XDamageNotify)
1588 sprintf (buf, "Event %d", ev->type);
1594 ev_window (XEvent *ev)
1598 return ev->xexpose.window;
1600 return ev->xmap.window;
1602 return ev->xunmap.window;
1603 case ReparentNotify:
1604 return ev->xreparent.window;
1605 case CirculateNotify:
1606 return ev->xcirculate.window;
1608 if (ev->type == damage_event + XDamageNotify)
1609 return ((XDamageNotifyEvent *) ev)->drawable;
1615 usage (char *program)
1617 fprintf (stderr, "usage: %s [-d display] [-n] [-s] [-c] [-a]\n", program);
1622 main (int argc, char **argv)
1625 Window root_return, parent_return;
1629 unsigned int nchildren;
1631 XRenderPictureAttributes pa;
1633 XRectangle *expose_rects = 0;
1634 int size_expose = 0;
1641 int composite_major, composite_minor;
1645 while ((o = getopt (argc, argv, "d:scnfaS")) != -1)
1652 compMode = CompServerShadows;
1655 compMode = CompClientShadows;
1658 compMode = CompSimple;
1664 autoRedirect = True;
1675 dpy = XOpenDisplay (display);
1678 fprintf (stderr, "Can't open display\n");
1681 XSetErrorHandler (error);
1683 XSynchronize (dpy, 1);
1684 scr = DefaultScreen (dpy);
1685 root = RootWindow (dpy, scr);
1687 if (!XRenderQueryExtension (dpy, &render_event, &render_error))
1689 fprintf (stderr, "No render extension\n");
1692 if (!XQueryExtension (dpy, COMPOSITE_NAME, &composite_opcode,
1693 &composite_event, &composite_error))
1695 fprintf (stderr, "No composite extension\n");
1698 XCompositeQueryVersion (dpy, &composite_major, &composite_minor);
1699 #if HAS_NAME_WINDOW_PIXMAP
1700 if (composite_major > 0 || composite_minor >= 2)
1701 hasNamePixmap = True;
1704 if (!XDamageQueryExtension (dpy, &damage_event, &damage_error))
1706 fprintf (stderr, "No damage extension\n");
1709 if (!XFixesQueryExtension (dpy, &xfixes_event, &xfixes_error))
1711 fprintf (stderr, "No XFixes extension\n");
1715 opacityAtom = XInternAtom (dpy, OPACITY_PROP, False);
1717 pa.subwindow_mode = IncludeInferiors;
1719 if (compMode == CompClientShadows)
1720 gaussianMap = make_gaussian_map(dpy, shadowRadius);
1722 root_width = DisplayWidth (dpy, scr);
1723 root_height = DisplayHeight (dpy, scr);
1725 rootPicture = XRenderCreatePicture (dpy, root,
1726 XRenderFindVisualFormat (dpy,
1727 DefaultVisual (dpy, scr)),
1730 blackPicture = solid_picture (dpy, True, 1, 0, 0, 0);
1731 if (compMode == CompServerShadows)
1732 transBlackPicture = solid_picture (dpy, True, 0.3, 0, 0, 0);
1737 XCompositeRedirectSubwindows (dpy, root, CompositeRedirectAutomatic);
1740 XCompositeRedirectSubwindows (dpy, root, CompositeRedirectManual);
1741 XSelectInput (dpy, root,
1742 SubstructureNotifyMask|
1744 StructureNotifyMask|
1745 PropertyChangeMask);
1746 XQueryTree (dpy, root, &root_return, &parent_return, &children, &nchildren);
1747 for (i = 0; i < nchildren; i++)
1748 add_win (dpy, children[i], i ? children[i-1] : None);
1751 XUngrabServer (dpy);
1752 ufd.fd = ConnectionNumber (dpy);
1753 ufd.events = POLLIN;
1755 paint_all (dpy, None);
1764 if (poll (&ufd, 1, fade_timeout()) == 0)
1771 XNextEvent (dpy, &ev);
1772 if (ev.type & 0x7f != KeymapNotify)
1773 discard_ignore (dpy, ev.xany.serial);
1775 printf ("event %10.10s serial 0x%08x window 0x%08x\n",
1776 ev_name(&ev), ev_serial (&ev), ev_window (&ev));
1778 if (!autoRedirect) switch (ev.type) {
1780 add_win (dpy, ev.xcreatewindow.window, 0);
1782 case ConfigureNotify:
1783 configure_win (dpy, &ev.xconfigure);
1786 destroy_win (dpy, ev.xdestroywindow.window, True, True);
1789 map_win (dpy, ev.xmap.window, ev.xmap.serial, True);
1792 unmap_win (dpy, ev.xunmap.window, True);
1794 case ReparentNotify:
1795 if (ev.xreparent.parent == root)
1796 add_win (dpy, ev.xreparent.window, 0);
1798 destroy_win (dpy, ev.xreparent.window, False, True);
1800 case CirculateNotify:
1801 circulate_win (dpy, &ev.xcirculate);
1804 if (ev.xexpose.window == root)
1806 int more = ev.xexpose.count + 1;
1807 if (n_expose == size_expose)
1811 expose_rects = realloc (expose_rects,
1812 (size_expose + more) *
1813 sizeof (XRectangle));
1814 size_expose += more;
1818 expose_rects = malloc (more * sizeof (XRectangle));
1822 expose_rects[n_expose].x = ev.xexpose.x;
1823 expose_rects[n_expose].y = ev.xexpose.y;
1824 expose_rects[n_expose].width = ev.xexpose.width;
1825 expose_rects[n_expose].height = ev.xexpose.height;
1827 if (ev.xexpose.count == 0)
1829 expose_root (dpy, root, expose_rects, n_expose);
1834 case PropertyNotify:
1835 for (p = 0; backgroundProps[p]; p++)
1837 if (ev.xproperty.atom == XInternAtom (dpy, backgroundProps[p], False))
1841 XClearArea (dpy, root, 0, 0, 0, 0, True);
1842 XRenderFreePicture (dpy, rootTile);
1848 /* check if Trans property was changed */
1849 if (ev.xproperty.atom == opacityAtom)
1851 /* reset mode and redraw window */
1852 win * w = find_win(dpy, ev.xproperty.window);
1855 w->opacity = get_opacity_prop(dpy, w, OPAQUE);
1856 determine_mode(dpy, w);
1861 if (ev.type == damage_event + XDamageNotify)
1862 damage_win (dpy, (XDamageNotifyEvent *) &ev);
1865 } while (QLength (dpy));
1866 if (allDamage && !autoRedirect)
1869 paint_all (dpy, allDamage);
1873 clipChanged = False;