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;
172 get_time_in_milliseconds ()
176 gettimeofday (&tv, NULL);
177 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
185 for (f = fades; f; f = f->next)
194 dequeue_fade (Display *dpy, fade *f)
198 for (prev = &fades; *prev; prev = &(*prev)->next)
203 (*f->callback) (dpy, f->w, f->gone);
210 cleanup_fade (Display *dpy, win *w)
212 fade *f = find_fade (w);
214 dequeue_fade (dpy, f);
218 enqueue_fade (Display *dpy, fade *f)
221 fade_time = get_time_in_milliseconds () + fade_delta;
227 set_fade (Display *dpy, win *w, Bool in,
228 void (*callback) (Display *dpy, win *w, Bool gone),
236 f = malloc (sizeof (fade));
243 enqueue_fade (dpy, f);
248 f->step = -fade_step;
249 f->callback = callback;
251 w->opacity = f->cur * OPAQUE;
253 printf ("set_fade start %g step %g\n", f->cur, f->step);
255 determine_mode (dpy, w);
265 now = get_time_in_milliseconds();
266 delta = fade_time - now;
269 /* printf ("timeout %d\n", delta); */
274 run_fades (Display *dpy)
276 int now = get_time_in_milliseconds();
281 printf ("run fades\n");
283 if (fade_time - now > 0)
285 steps = 1 + (now - fade_time) / fade_delta;
286 for (next = fades; f = next; )
290 f->cur += f->step * steps;
296 printf ("opacity now %g\n", f->cur);
298 w->opacity = f->cur * OPAQUE;
302 dequeue_fade (dpy, f);
307 dequeue_fade (dpy, f);
309 determine_mode (dpy, w);
311 fade_time = now + fade_delta;
314 #define SHADOW_OPACITY 0.75
315 #define SHADOW_OFFSET_X (-shadowRadius * 5 / 4)
316 #define SHADOW_OFFSET_Y (-shadowRadius * 5 / 4)
319 gaussian (double r, double x, double y)
321 return ((1 / (sqrt (2 * M_PI * r))) *
322 exp ((- (x * x + y * y)) / (2 * r * r)));
327 make_gaussian_map (Display *dpy, double r)
330 int size = ((int) ceil ((r * 3)) + 1) & ~1;
331 int center = size / 2;
336 c = malloc (sizeof (conv) + size * size * sizeof (double));
338 c->data = (double *) (c + 1);
340 for (y = 0; y < size; y++)
341 for (x = 0; x < size; x++)
343 g = gaussian (r, (double) (x - center), (double) (y - center));
345 c->data[y * size + x] = g;
347 /* printf ("gaussian total %f\n", t); */
348 for (y = 0; y < size; y++)
349 for (x = 0; x < size; x++)
351 c->data[y*size + x] /= t;
357 * A picture will help
359 * -center 0 width width+center
360 * -center +-----+-------------------+-----+
363 * 0 +-----+-------------------+-----+
367 * height +-----+-------------------+-----+
370 * center +-----+-------------------+-----+
374 sum_gaussian (conv *map, double opacity, int x, int y, int width, int height)
378 double *g_line = map->data;
379 int g_size = map->size;
380 int center = g_size / 2;
381 int fx_start, fx_end;
382 int fy_start, fy_end;
386 * Compute set of filter values which are "in range",
387 * that's the set with:
388 * 0 <= x + (fx-center) && x + (fx-center) < width &&
389 * 0 <= y + (fy-center) && y + (fy-center) < height
391 * 0 <= x + (fx - center) x + fx - center < width
392 * center - x <= fx fx < width + center - x
395 fx_start = center - x;
398 fx_end = width + center - x;
402 fy_start = center - y;
405 fy_end = height + center - y;
409 g_line = g_line + fy_start * g_size + fx_start;
412 for (fy = fy_start; fy < fy_end; fy++)
417 for (fx = fx_start; fx < fx_end; fx++)
423 return ((unsigned char) (v * opacity * 255.0));
427 make_shadow (Display *dpy, double opacity, int width, int height)
431 int gsize = gaussianMap->size;
433 int swidth = width + gsize;
434 int sheight = height + gsize;
435 int center = gsize / 2;
440 data = malloc (swidth * sheight * sizeof (unsigned char));
443 ximage = XCreateImage (dpy,
444 DefaultVisual(dpy, DefaultScreen(dpy)),
449 swidth, sheight, 8, swidth * sizeof (unsigned char));
456 * Build the gaussian in sections
460 * center (fill the complete data array)
463 d = sum_gaussian (gaussianMap, opacity, center, center, width, height);
464 memset(data, d, sheight * swidth);
470 if (ylimit > sheight / 2)
471 ylimit = (sheight + 1) / 2;
473 if (xlimit > swidth / 2)
474 xlimit = (swidth + 1) / 2;
476 for (y = 0; y < ylimit; y++)
477 for (x = 0; x < xlimit; x++)
479 d = sum_gaussian (gaussianMap, opacity, x - center, y - center, width, height);
480 data[y * swidth + x] = d;
481 data[(sheight - y - 1) * swidth + x] = d;
482 data[(sheight - y - 1) * swidth + (swidth - x - 1)] = d;
483 data[y * swidth + (swidth - x - 1)] = d;
489 x_diff = swidth - (gsize * 2);
490 if (x_diff > 0 && ylimit > 0)
492 for (y = 0; y < ylimit; y++)
494 d = sum_gaussian (gaussianMap, opacity, center, y - center, width, height);
495 memset (&data[y * swidth + gsize], d, x_diff);
496 memset (&data[(sheight - y - 1) * swidth + gsize], d, x_diff);
504 for (x = 0; x < xlimit; x++)
506 d = sum_gaussian (gaussianMap, opacity, x - center, center, width, height);
507 for (y = gsize; y < sheight - gsize; y++)
509 data[y * swidth + x] = d;
510 data[y * swidth + (swidth - x - 1)] = d;
518 shadow_picture (Display *dpy, double opacity, int width, int height, int *wp, int *hp)
522 Picture shadowPicture;
525 shadowImage = make_shadow (dpy, opacity, width, height);
528 shadowPixmap = XCreatePixmap (dpy, root,
532 shadowPicture = XRenderCreatePicture (dpy, shadowPixmap,
533 XRenderFindStandardFormat (dpy, PictStandardA8),
535 gc = XCreateGC (dpy, shadowPixmap, 0, 0);
537 XPutImage (dpy, shadowPixmap, gc, shadowImage, 0, 0, 0, 0,
539 shadowImage->height);
540 *wp = shadowImage->width;
541 *hp = shadowImage->height;
543 XDestroyImage (shadowImage);
544 XFreePixmap (dpy, shadowPixmap);
545 return shadowPicture;
549 solid_picture (Display *dpy, Bool argb, double a, double r, double g, double b)
553 XRenderPictureAttributes pa;
556 pixmap = XCreatePixmap (dpy, root, 1, 1, argb ? 32 : 8);
558 picture = XRenderCreatePicture (dpy, pixmap,
559 XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8),
562 c.alpha = a * 0xffff;
564 c.green = g * 0xffff;
566 XRenderFillRectangle (dpy, PictOpSrc, picture, &c, 0, 0, 1, 1);
567 XFreePixmap (dpy, pixmap);
572 discard_ignore (Display *dpy, unsigned long sequence)
576 if ((long) (sequence - ignore_head->sequence) > 0)
578 ignore *next = ignore_head->next;
582 ignore_tail = &ignore_head;
590 set_ignore (Display *dpy, unsigned long sequence)
592 ignore *i = malloc (sizeof (ignore));
595 i->sequence = sequence;
598 ignore_tail = &i->next;
602 should_ignore (Display *dpy, unsigned long sequence)
604 discard_ignore (dpy, sequence);
605 return ignore_head && ignore_head->sequence == sequence;
609 find_win (Display *dpy, Window id)
613 for (w = list; w; w = w->next)
619 static char *backgroundProps[] = {
626 root_tile (Display *dpy)
632 unsigned long nitems;
633 unsigned long bytes_after;
636 XRenderPictureAttributes pa;
640 for (p = 0; backgroundProps[p]; p++)
642 if (XGetWindowProperty (dpy, root, XInternAtom (dpy, backgroundProps[p], False),
643 0, 4, False, AnyPropertyType,
644 &actual_type, &actual_format, &nitems, &bytes_after, &prop) == Success &&
645 actual_type == XInternAtom (dpy, "PIXMAP", False) && actual_format == 32 && nitems == 1)
647 memcpy (&pixmap, prop, 4);
655 pixmap = XCreatePixmap (dpy, root, 1, 1, DefaultDepth (dpy, scr));
659 picture = XRenderCreatePicture (dpy, pixmap,
660 XRenderFindVisualFormat (dpy,
661 DefaultVisual (dpy, scr)),
667 c.red = c.green = c.blue = 0x8080;
669 XRenderFillRectangle (dpy, PictOpSrc, picture, &c,
676 paint_root (Display *dpy)
679 rootTile = root_tile (dpy);
681 XRenderComposite (dpy, PictOpSrc,
682 rootTile, None, rootBuffer,
683 0, 0, 0, 0, 0, 0, root_width, root_height);
687 win_extents (Display *dpy, win *w)
693 r.width = w->a.width + w->a.border_width * 2;
694 r.height = w->a.height + w->a.border_width * 2;
695 if (compMode != CompSimple)
697 if (compMode == CompServerShadows || w->mode != WINDOW_ARGB)
701 if (compMode == CompServerShadows)
705 w->shadow_width = w->a.width;
706 w->shadow_height = w->a.height;
710 w->shadow_dx = SHADOW_OFFSET_X;
711 w->shadow_dy = SHADOW_OFFSET_Y;
714 double opacity = SHADOW_OPACITY;
715 if (w->mode == WINDOW_TRANS)
716 opacity = opacity * TRANS_OPACITY;
717 w->shadow = shadow_picture (dpy, opacity,
718 w->a.width + w->a.border_width * 2,
719 w->a.height + w->a.border_width * 2,
720 &w->shadow_width, &w->shadow_height);
723 sr.x = w->a.x + w->shadow_dx;
724 sr.y = w->a.y + w->shadow_dy;
725 sr.width = w->shadow_width;
726 sr.height = w->shadow_height;
729 r.width = (r.x + r.width) - sr.x;
734 r.height = (r.y + r.height) - sr.y;
737 if (sr.x + sr.width > r.x + r.width)
738 r.width = sr.x + sr.width - r.x;
739 if (sr.y + sr.height > r.y + r.height)
740 r.height = sr.y + sr.height - r.y;
743 return XFixesCreateRegion (dpy, &r, 1);
747 border_size (Display *dpy, win *w)
749 XserverRegion border;
751 * if window doesn't exist anymore, this will generate an error
752 * as well as not generate a region. Perhaps a better XFixes
753 * architecture would be to have a request that copies instead
754 * of creates, that way you'd just end up with an empty region
755 * instead of an invalid XID.
757 set_ignore (dpy, NextRequest (dpy));
758 border = XFixesCreateRegionFromWindow (dpy, w->id, WindowRegionBounding);
760 set_ignore (dpy, NextRequest (dpy));
761 XFixesTranslateRegion (dpy, border,
762 w->a.x + w->a.border_width,
763 w->a.y + w->a.border_width);
768 paint_all (Display *dpy, XserverRegion region)
778 r.width = root_width;
779 r.height = root_height;
780 region = XFixesCreateRegion (dpy, &r, 1);
783 rootBuffer = rootPicture;
787 Pixmap rootPixmap = XCreatePixmap (dpy, root, root_width, root_height,
788 DefaultDepth (dpy, scr));
789 rootBuffer = XRenderCreatePicture (dpy, rootPixmap,
790 XRenderFindVisualFormat (dpy,
791 DefaultVisual (dpy, scr)),
793 XFreePixmap (dpy, rootPixmap);
796 XFixesSetPictureClipRegion (dpy, rootPicture, 0, 0, region);
798 XRenderComposite (dpy, PictOpSrc, blackPicture, None, rootPicture,
799 0, 0, 0, 0, 0, 0, root_width, root_height);
804 for (w = list; w; w = w->next)
810 /* never painted, ignore it */
815 XRenderPictureAttributes pa;
816 XRenderPictFormat *format;
817 Drawable draw = w->id;
819 #if HAS_NAME_WINDOW_PIXMAP
820 if (hasNamePixmap && !w->pixmap)
821 w->pixmap = XCompositeNameWindowPixmap (dpy, w->id);
825 format = XRenderFindVisualFormat (dpy, w->a.visual);
826 pa.subwindow_mode = IncludeInferiors;
827 w->picture = XRenderCreatePicture (dpy, draw,
833 printf (" 0x%x", w->id);
839 set_ignore (dpy, NextRequest (dpy));
840 XFixesDestroyRegion (dpy, w->borderSize);
841 w->borderSize = None;
845 XFixesDestroyRegion (dpy, w->extents);
850 XFixesDestroyRegion (dpy, w->borderClip);
851 w->borderClip = None;
855 w->borderSize = border_size (dpy, w);
857 w->extents = win_extents (dpy, w);
858 if (w->mode == WINDOW_SOLID)
861 #if HAS_NAME_WINDOW_PIXMAP
864 wid = w->a.width + w->a.border_width * 2;
865 hei = w->a.height + w->a.border_width * 2;
867 x = w->a.x + w->a.border_width;
868 y = w->a.y + w->a.border_width;
872 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
873 set_ignore (dpy, NextRequest (dpy));
874 XFixesSubtractRegion (dpy, region, region, w->borderSize);
875 set_ignore (dpy, NextRequest (dpy));
876 XRenderComposite (dpy, PictOpSrc, w->picture, None, rootBuffer,
882 w->borderClip = XFixesCreateRegion (dpy, 0, 0);
883 XFixesCopyRegion (dpy, w->borderClip, region);
892 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
894 for (w = t; w; w = w->prev_trans)
896 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, w->borderClip);
900 case CompServerShadows:
901 set_ignore (dpy, NextRequest (dpy));
902 if (w->opacity != OPAQUE && !w->shadowPict)
903 w->shadowPict = solid_picture (dpy, True,
904 (double) w->opacity / OPAQUE * 0.3,
906 XRenderComposite (dpy, PictOpOver,
907 w->shadowPict ? w->shadowPict : transBlackPicture,
908 w->picture, rootBuffer,
910 w->a.x + w->shadow_dx,
911 w->a.y + w->shadow_dy,
912 w->shadow_width, w->shadow_height);
914 case CompClientShadows:
917 XRenderComposite (dpy, PictOpOver, blackPicture, w->shadow, rootBuffer,
919 w->a.x + w->shadow_dx,
920 w->a.y + w->shadow_dy,
921 w->shadow_width, w->shadow_height);
925 if (w->opacity != OPAQUE && !w->alphaPict)
926 w->alphaPict = solid_picture (dpy, False,
927 (double) w->opacity / OPAQUE, 0, 0, 0);
928 if (w->mode == WINDOW_TRANS)
931 #if HAS_NAME_WINDOW_PIXMAP
934 wid = w->a.width + w->a.border_width * 2;
935 hei = w->a.height + w->a.border_width * 2;
937 x = w->a.x + w->a.border_width;
938 y = w->a.y + w->a.border_width;
942 set_ignore (dpy, NextRequest (dpy));
943 XRenderComposite (dpy, PictOpOver, w->picture, w->alphaPict, rootBuffer,
947 else if (w->mode == WINDOW_ARGB)
950 #if HAS_NAME_WINDOW_PIXMAP
953 wid = w->a.width + w->a.border_width * 2;
954 hei = w->a.height + w->a.border_width * 2;
956 x = w->a.x + w->a.border_width;
957 y = w->a.y + w->a.border_width;
961 set_ignore (dpy, NextRequest (dpy));
962 XRenderComposite (dpy, PictOpOver, w->picture, w->alphaPict, rootBuffer,
966 XFixesDestroyRegion (dpy, w->borderClip);
967 w->borderClip = None;
969 XFixesDestroyRegion (dpy, region);
970 if (rootBuffer != rootPicture)
972 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, None);
973 XRenderComposite (dpy, PictOpSrc, rootBuffer, None, rootPicture,
974 0, 0, 0, 0, 0, 0, root_width, root_height);
979 add_damage (Display *dpy, XserverRegion damage)
983 XFixesUnionRegion (dpy, allDamage, allDamage, damage);
984 XFixesDestroyRegion (dpy, damage);
991 repair_win (Display *dpy, win *w)
997 parts = win_extents (dpy, w);
998 set_ignore (dpy, NextRequest (dpy));
999 XDamageSubtract (dpy, w->damage, None, None);
1004 parts = XFixesCreateRegion (dpy, 0, 0);
1005 set_ignore (dpy, NextRequest (dpy));
1006 XDamageSubtract (dpy, w->damage, None, parts);
1007 XFixesTranslateRegion (dpy, parts,
1008 w->a.x + w->a.border_width,
1009 w->a.y + w->a.border_width);
1010 if (compMode == CompServerShadows)
1012 o = XFixesCreateRegion (dpy, 0, 0);
1013 XFixesCopyRegion (dpy, o, parts);
1014 XFixesTranslateRegion (dpy, o, w->shadow_dx, w->shadow_dy);
1015 XFixesUnionRegion (dpy, parts, parts, o);
1016 XFixesDestroyRegion (dpy, o);
1019 add_damage (dpy, parts);
1024 map_win (Display *dpy, Window id, unsigned long sequence, Bool fade)
1026 win *w = find_win (dpy, id);
1031 w->a.map_state = IsViewable;
1034 w->damage_bounds.x = w->damage_bounds.y = 0;
1035 w->damage_bounds.width = w->damage_bounds.height = 0;
1041 finish_unmap_win (Display *dpy, win *w)
1047 if (w->extents != None)
1049 add_damage (dpy, w->extents); /* destroys region */
1053 #if HAS_NAME_WINDOW_PIXMAP
1056 XFreePixmap (dpy, w->pixmap);
1063 set_ignore (dpy, NextRequest (dpy));
1064 XRenderFreePicture (dpy, w->picture);
1068 /* don't care about properties anymore */
1069 set_ignore (dpy, NextRequest (dpy));
1070 XSelectInput(dpy, w->id, 0);
1074 set_ignore (dpy, NextRequest (dpy));
1075 XFixesDestroyRegion (dpy, w->borderSize);
1076 w->borderSize = None;
1080 XRenderFreePicture (dpy, w->shadow);
1085 XFixesDestroyRegion (dpy, w->borderClip);
1086 w->borderClip = None;
1092 #if HAS_NAME_WINDOW_PIXMAP
1094 unmap_callback (Display *dpy, win *w, Bool gone)
1096 finish_unmap_win (dpy, w);
1101 unmap_win (Display *dpy, Window id, Bool fade)
1103 win *w = find_win (dpy, id);
1106 #if HAS_NAME_WINDOW_PIXMAP
1107 if (w->pixmap && fade && fadeWindows)
1108 set_fade (dpy, w, False, unmap_callback, False);
1111 finish_unmap_win (dpy, w);
1114 /* Get the opacity prop from window
1119 get_opacity_prop(Display *dpy, win *w, unsigned int def)
1123 unsigned long n, left;
1126 XGetWindowProperty(dpy, w->id, opacityAtom, 0L, 1L, False,
1127 XA_CARDINAL, &actual, &format,
1128 &n, &left, (unsigned char **) &data);
1132 memcpy (&i, data, sizeof (unsigned int));
1133 XFree( (void *) data);
1139 /* determine mode for window all in one place.
1140 Future might check for menu flag and other cool things
1144 determine_mode(Display *dpy, win *w)
1147 XRenderPictFormat *format;
1148 unsigned int default_opacity;
1150 /* if trans prop == -1 fall back on previous tests*/
1154 XRenderFreePicture (dpy, w->alphaPict);
1155 w->alphaPict = None;
1159 XRenderFreePicture (dpy, w->shadowPict);
1160 w->shadowPict = None;
1163 if (w->a.class == InputOnly)
1169 format = XRenderFindVisualFormat (dpy, w->a.visual);
1172 if (format && format->type == PictTypeDirect && format->direct.alphaMask)
1176 else if (w->opacity != OPAQUE)
1178 mode = WINDOW_TRANS;
1182 mode = WINDOW_SOLID;
1187 XserverRegion damage;
1188 damage = XFixesCreateRegion (dpy, 0, 0);
1189 XFixesCopyRegion (dpy, damage, w->extents);
1190 add_damage (dpy, damage);
1195 add_win (Display *dpy, Window id, Window prev)
1197 win *new = malloc (sizeof (win));
1204 for (p = &list; *p; p = &(*p)->next)
1205 if ((*p)->id == prev)
1211 set_ignore (dpy, NextRequest (dpy));
1212 if (!XGetWindowAttributes (dpy, id, &new->a))
1219 new->usable = False;
1221 #if HAS_NAME_WINDOW_PIXMAP
1224 new->picture = None;
1225 if (new->a.class == InputOnly)
1227 new->damage_sequence = 0;
1232 new->damage_sequence = NextRequest (dpy);
1233 new->damage = XDamageCreate (dpy, id, XDamageReportNonEmpty);
1235 new->alphaPict = None;
1236 new->shadowPict = None;
1237 new->borderSize = None;
1238 new->extents = None;
1242 new->shadow_width = 0;
1243 new->shadow_height = 0;
1244 new->opacity = OPAQUE;
1246 new->borderClip = None;
1247 new->prev_trans = 0;
1249 /* moved mode setting to one place */
1250 XSelectInput(dpy, id, PropertyChangeMask);
1251 new->opacity = get_opacity_prop(dpy, new, OPAQUE);
1252 determine_mode (dpy, new);
1256 if (new->a.map_state == IsViewable)
1257 map_win (dpy, id, new->damage_sequence - 1, False);
1261 restack_win (Display *dpy, win *w, Window new_above)
1266 old_above = w->next->id;
1269 if (old_above != new_above)
1274 for (prev = &list; *prev; prev = &(*prev)->next)
1280 for (prev = &list; *prev; prev = &(*prev)->next)
1282 if ((*prev)->id == new_above)
1291 configure_win (Display *dpy, XConfigureEvent *ce)
1293 win *w = find_win (dpy, ce->window);
1295 XserverRegion damage = None;
1299 if (ce->window == root)
1303 XRenderFreePicture (dpy, rootBuffer);
1306 root_width = ce->width;
1307 root_height = ce->height;
1315 damage = XFixesCreateRegion (dpy, 0, 0);
1316 if (w->extents != None)
1317 XFixesCopyRegion (dpy, damage, w->extents);
1321 if (w->a.width != ce->width || w->a.height != ce->height)
1323 #if HAS_NAME_WINDOW_PIXMAP
1326 XFreePixmap (dpy, w->pixmap);
1330 XRenderFreePicture (dpy, w->picture);
1337 XRenderFreePicture (dpy, w->shadow);
1341 w->a.width = ce->width;
1342 w->a.height = ce->height;
1343 w->a.border_width = ce->border_width;
1344 w->a.override_redirect = ce->override_redirect;
1345 restack_win (dpy, w, ce->above);
1348 XserverRegion extents = win_extents (dpy, w);
1349 XFixesUnionRegion (dpy, damage, damage, extents);
1350 XFixesDestroyRegion (dpy, extents);
1351 add_damage (dpy, damage);
1357 circulate_win (Display *dpy, XCirculateEvent *ce)
1359 win *w = find_win (dpy, ce->window);
1362 if (ce->place == PlaceOnTop)
1363 new_above = list->id;
1366 restack_win (dpy, w, new_above);
1371 finish_destroy_win (Display *dpy, Window id, Bool gone)
1375 for (prev = &list; (w = *prev); prev = &w->next)
1379 finish_unmap_win (dpy, w);
1383 set_ignore (dpy, NextRequest (dpy));
1384 XRenderFreePicture (dpy, w->picture);
1388 XRenderFreePicture (dpy, w->alphaPict);
1389 w->alphaPict = None;
1393 XRenderFreePicture (dpy, w->shadowPict);
1394 w->shadowPict = None;
1396 if (w->damage != None)
1398 set_ignore (dpy, NextRequest (dpy));
1399 XDamageDestroy (dpy, w->damage);
1401 cleanup_fade (dpy, w);
1407 #if HAS_NAME_WINDOW_PIXMAP
1409 destroy_callback (Display *dpy, win *w, Bool gone)
1411 finish_destroy_win (dpy, w->id, gone);
1416 destroy_win (Display *dpy, Window id, Bool gone, Bool fade)
1418 win *w = find_win (dpy, id);
1419 #if HAS_NAME_WINDOW_PIXMAP
1420 if (w && w->pixmap && fade && fadeWindows)
1421 set_fade (dpy, w, False, destroy_callback, gone);
1425 finish_destroy_win (dpy, id, gone);
1433 printf ("\t%08lx: %d x %d + %d + %d (%d)\n", w->id,
1434 w->a.width, w->a.height, w->a.x, w->a.y, w->a.border_width);
1443 printf ("windows:\n");
1444 for (w = list; w; w = w->next)
1450 damage_win (Display *dpy, XDamageNotifyEvent *de)
1452 win *w = find_win (dpy, de->drawable);
1459 if (w->damage_bounds.width == 0 || w->damage_bounds.height == 0)
1461 w->damage_bounds = de->area;
1465 if (de->area.x < w->damage_bounds.x)
1467 w->damage_bounds.width += (w->damage_bounds.x - de->area.x);
1468 w->damage_bounds.x = de->area.x;
1470 if (de->area.y < w->damage_bounds.y)
1472 w->damage_bounds.height += (w->damage_bounds.y - de->area.y);
1473 w->damage_bounds.y = de->area.y;
1475 if (de->area.x + de->area.width > w->damage_bounds.x + w->damage_bounds.width)
1476 w->damage_bounds.width = de->area.x + de->area.width - w->damage_bounds.x;
1477 if (de->area.y + de->area.height > w->damage_bounds.y + w->damage_bounds.height)
1478 w->damage_bounds.height = de->area.y + de->area.height - w->damage_bounds.y;
1481 printf ("unusable damage %d, %d: %d x %d bounds %d, %d: %d x %d\n",
1488 w->damage_bounds.width,
1489 w->damage_bounds.height);
1491 if (w->damage_bounds.x <= 0 &&
1492 w->damage_bounds.y <= 0 &&
1493 w->a.width <= w->damage_bounds.x + w->damage_bounds.width &&
1494 w->a.height <= w->damage_bounds.y + w->damage_bounds.height)
1498 set_fade (dpy, w, True, 0, False);
1504 repair_win (dpy, w);
1508 error (Display *dpy, XErrorEvent *ev)
1513 if (should_ignore (dpy, ev->serial))
1516 if (ev->request_code == composite_opcode &&
1517 ev->minor_code == X_CompositeRedirectSubwindows)
1519 fprintf (stderr, "Another composite manager is already running\n");
1523 o = ev->error_code - xfixes_error;
1525 case BadRegion: name = "BadRegion"; break;
1528 o = ev->error_code - damage_error;
1530 case BadDamage: name = "BadDamage"; break;
1533 o = ev->error_code - render_error;
1535 case BadPictFormat: name ="BadPictFormat"; break;
1536 case BadPicture: name ="BadPicture"; break;
1537 case BadPictOp: name ="BadPictOp"; break;
1538 case BadGlyphSet: name ="BadGlyphSet"; break;
1539 case BadGlyph: name ="BadGlyph"; break;
1543 printf ("error %d request %d minor %d serial %d\n",
1544 ev->error_code, ev->request_code, ev->minor_code, ev->serial);
1551 expose_root (Display *dpy, Window root, XRectangle *rects, int nrects)
1553 XserverRegion region = XFixesCreateRegion (dpy, rects, nrects);
1555 add_damage (dpy, region);
1560 ev_serial (XEvent *ev)
1562 if (ev->type & 0x7f != KeymapNotify)
1563 return ev->xany.serial;
1564 return NextRequest (ev->xany.display);
1569 ev_name (XEvent *ev)
1571 static char buf[128];
1572 switch (ev->type & 0x7f) {
1579 case ReparentNotify:
1581 case CirculateNotify:
1584 if (ev->type == damage_event + XDamageNotify)
1586 sprintf (buf, "Event %d", ev->type);
1592 ev_window (XEvent *ev)
1596 return ev->xexpose.window;
1598 return ev->xmap.window;
1600 return ev->xunmap.window;
1601 case ReparentNotify:
1602 return ev->xreparent.window;
1603 case CirculateNotify:
1604 return ev->xcirculate.window;
1606 if (ev->type == damage_event + XDamageNotify)
1607 return ((XDamageNotifyEvent *) ev)->drawable;
1613 usage (char *program)
1615 fprintf (stderr, "usage: %s [-d display] [-n] [-s] [-c]\n", program);
1620 main (int argc, char **argv)
1623 Window root_return, parent_return;
1627 unsigned int nchildren;
1629 XRenderPictureAttributes pa;
1631 XRectangle *expose_rects = 0;
1632 int size_expose = 0;
1639 int composite_major, composite_minor;
1643 while ((o = getopt (argc, argv, "d:scnfS")) != -1)
1650 compMode = CompServerShadows;
1653 compMode = CompClientShadows;
1656 compMode = CompSimple;
1670 dpy = XOpenDisplay (display);
1673 fprintf (stderr, "Can't open display\n");
1676 XSetErrorHandler (error);
1678 XSynchronize (dpy, 1);
1679 scr = DefaultScreen (dpy);
1680 root = RootWindow (dpy, scr);
1682 if (!XRenderQueryExtension (dpy, &render_event, &render_error))
1684 fprintf (stderr, "No render extension\n");
1687 if (!XQueryExtension (dpy, COMPOSITE_NAME, &composite_opcode,
1688 &composite_event, &composite_error))
1690 fprintf (stderr, "No composite extension\n");
1693 XCompositeQueryVersion (dpy, &composite_major, &composite_minor);
1694 #if HAS_NAME_WINDOW_PIXMAP
1695 if (composite_major > 0 || composite_minor >= 2)
1696 hasNamePixmap = True;
1699 if (!XDamageQueryExtension (dpy, &damage_event, &damage_error))
1701 fprintf (stderr, "No damage extension\n");
1704 if (!XFixesQueryExtension (dpy, &xfixes_event, &xfixes_error))
1706 fprintf (stderr, "No XFixes extension\n");
1710 opacityAtom = XInternAtom (dpy, OPACITY_PROP, False);
1712 pa.subwindow_mode = IncludeInferiors;
1714 if (compMode == CompClientShadows)
1715 gaussianMap = make_gaussian_map(dpy, shadowRadius);
1717 root_width = DisplayWidth (dpy, scr);
1718 root_height = DisplayHeight (dpy, scr);
1720 rootPicture = XRenderCreatePicture (dpy, root,
1721 XRenderFindVisualFormat (dpy,
1722 DefaultVisual (dpy, scr)),
1725 blackPicture = solid_picture (dpy, True, 1, 0, 0, 0);
1726 if (compMode == CompServerShadows)
1727 transBlackPicture = solid_picture (dpy, True, 0.3, 0, 0, 0);
1731 XCompositeRedirectSubwindows (dpy, root, CompositeRedirectManual);
1732 XSelectInput (dpy, root,
1733 SubstructureNotifyMask|
1735 StructureNotifyMask|
1736 PropertyChangeMask);
1737 XQueryTree (dpy, root, &root_return, &parent_return, &children, &nchildren);
1738 for (i = 0; i < nchildren; i++)
1739 add_win (dpy, children[i], i ? children[i-1] : None);
1741 XUngrabServer (dpy);
1742 ufd.fd = ConnectionNumber (dpy);
1743 ufd.events = POLLIN;
1744 paint_all (dpy, None);
1751 if (poll (&ufd, 1, fade_timeout()) == 0)
1758 XNextEvent (dpy, &ev);
1759 if (ev.type & 0x7f != KeymapNotify)
1760 discard_ignore (dpy, ev.xany.serial);
1762 printf ("event %10.10s serial 0x%08x window 0x%08x\n",
1763 ev_name(&ev), ev_serial (&ev), ev_window (&ev));
1767 add_win (dpy, ev.xcreatewindow.window, 0);
1769 case ConfigureNotify:
1770 configure_win (dpy, &ev.xconfigure);
1773 destroy_win (dpy, ev.xdestroywindow.window, True, True);
1776 map_win (dpy, ev.xmap.window, ev.xmap.serial, True);
1779 unmap_win (dpy, ev.xunmap.window, True);
1781 case ReparentNotify:
1782 if (ev.xreparent.parent == root)
1783 add_win (dpy, ev.xreparent.window, 0);
1785 destroy_win (dpy, ev.xreparent.window, False, True);
1787 case CirculateNotify:
1788 circulate_win (dpy, &ev.xcirculate);
1791 if (ev.xexpose.window == root)
1793 int more = ev.xexpose.count + 1;
1794 if (n_expose == size_expose)
1798 expose_rects = realloc (expose_rects,
1799 (size_expose + more) *
1800 sizeof (XRectangle));
1801 size_expose += more;
1805 expose_rects = malloc (more * sizeof (XRectangle));
1809 expose_rects[n_expose].x = ev.xexpose.x;
1810 expose_rects[n_expose].y = ev.xexpose.y;
1811 expose_rects[n_expose].width = ev.xexpose.width;
1812 expose_rects[n_expose].height = ev.xexpose.height;
1814 if (ev.xexpose.count == 0)
1816 expose_root (dpy, root, expose_rects, n_expose);
1821 case PropertyNotify:
1822 for (p = 0; backgroundProps[p]; p++)
1824 if (ev.xproperty.atom == XInternAtom (dpy, backgroundProps[p], False))
1828 XClearArea (dpy, root, 0, 0, 0, 0, True);
1829 XRenderFreePicture (dpy, rootTile);
1835 /* check if Trans property was changed */
1836 if (ev.xproperty.atom == opacityAtom)
1838 /* reset mode and redraw window */
1839 win * w = find_win(dpy, ev.xproperty.window);
1842 w->opacity = get_opacity_prop(dpy, w, OPAQUE);
1843 determine_mode(dpy, w);
1848 if (ev.type == damage_event + XDamageNotify)
1849 damage_win (dpy, (XDamageNotifyEvent *) &ev);
1852 } while (QLength (dpy));
1856 paint_all (dpy, allDamage);
1860 clipChanged = False;