4 * Copyright © 2003 Keith Packard
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of Keith Packard not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. Keith Packard makes no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
16 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22 * PERFORMANCE OF THIS SOFTWARE.
26 /* Modified by Matthew Hawn. I don't know what to say here so follow what it
27 says above. Not that I can really do anything about it
40 #include <X11/Xutil.h>
41 #include <X11/Xatom.h>
42 #include <X11/extensions/Xcomposite.h>
43 #include <X11/extensions/Xdamage.h>
44 #include <X11/extensions/Xrender.h>
46 #if COMPOSITE_MAJOR > 0 || COMPOSITE_MINOR >= 2
47 #define HAS_NAME_WINDOW_PIXMAP 1
50 #define CAN_DO_USABLE 0
52 typedef struct _ignore {
54 unsigned long sequence;
60 #if HAS_NAME_WINDOW_PIXMAP
65 Bool usable; /* mapped and all damaged at one point */
66 XRectangle damage_bounds; /* bounds of damage */
74 XserverRegion borderSize;
75 XserverRegion extents;
83 unsigned long damage_sequence; /* sequence when damage was created */
85 /* for drawing translucent windows */
86 XserverRegion borderClip;
87 struct _win *prev_trans;
90 typedef struct _conv {
95 typedef struct _fade {
101 void (*callback) (Display *dpy, win *w, Bool gone);
113 Picture blackPicture;
114 Picture transBlackPicture;
116 XserverRegion allDamage;
118 #if HAS_NAME_WINDOW_PIXMAP
121 int root_height, root_width;
122 ignore *ignore_head, **ignore_tail = &ignore_head;
123 int xfixes_event, xfixes_error;
124 int damage_event, damage_error;
125 int composite_event, composite_error;
126 int render_event, render_error;
128 int composite_opcode;
130 /* find these once and be done with it */
142 /* opacity property name; sometime soon I'll write up an EWMH spec for it */
143 #define OPACITY_PROP "_NET_WM_WINDOW_OPACITY"
145 #define TRANSLUCENT 0xe0000000
146 #define OPAQUE 0xffffffff
150 #define WINDOW_SOLID 0
151 #define WINDOW_TRANS 1
152 #define WINDOW_ARGB 2
154 #define TRANS_OPACITY 0.75
156 #define DEBUG_REPAINT 0
157 #define DEBUG_EVENTS 0
158 #define MONITOR_REPAINT 0
161 #define SHARP_SHADOW 0
163 typedef enum _compMode {
164 CompSimple, /* looks like a regular X server */
165 CompServerShadows, /* use window alpha for shadow; sharp, but precise */
166 CompClientShadows, /* use window extents for shadow, blurred */
170 determine_mode(Display *dpy, win *w);
173 get_opacity_percent(Display *dpy, win *w, double def);
176 win_extents (Display *dpy, win *w);
178 CompMode compMode = CompSimple;
180 int shadowRadius = 12;
181 int shadowOffsetX = -15;
182 int shadowOffsetY = -15;
183 double shadowOpacity = .75;
185 double fade_in_step = 0.028;
186 double fade_out_step = 0.03;
189 Bool fadeWindows = False;
190 Bool excludeDockShadows = False;
191 Bool fadeTrans = False;
193 Bool autoRedirect = False;
195 /* For shadow precomputation */
197 unsigned char *shadowCorner = NULL;
198 unsigned char *shadowTop = NULL;
201 get_time_in_milliseconds ()
205 gettimeofday (&tv, NULL);
206 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
214 for (f = fades; f; f = f->next)
223 dequeue_fade (Display *dpy, fade *f)
227 for (prev = &fades; *prev; prev = &(*prev)->next)
232 (*f->callback) (dpy, f->w, f->gone);
239 cleanup_fade (Display *dpy, win *w)
241 fade *f = find_fade (w);
243 dequeue_fade (dpy, f);
247 enqueue_fade (Display *dpy, fade *f)
250 fade_time = get_time_in_milliseconds () + fade_delta;
256 set_fade (Display *dpy, win *w, double start, double finish, double step,
257 void (*callback) (Display *dpy, win *w, Bool gone),
258 Bool gone, Bool exec_callback, Bool override)
265 f = malloc (sizeof (fade));
269 enqueue_fade (dpy, f);
277 (*f->callback)(dpy, f->w, f->gone);
287 else if (f->cur > finish)
289 f->callback = callback;
291 w->opacity = f->cur * OPAQUE;
293 printf ("set_fade start %g step %g\n", f->cur, f->step);
295 determine_mode (dpy, w);
298 XRenderFreePicture (dpy, w->shadow);
300 w->extents = win_extents (dpy, w);
311 now = get_time_in_milliseconds();
312 delta = fade_time - now;
315 /* printf ("timeout %d\n", delta); */
320 run_fades (Display *dpy)
322 int now = get_time_in_milliseconds();
328 printf ("run fades\n");
330 if (fade_time - now > 0)
332 steps = 1 + (now - fade_time) / fade_delta;
333 for (next = fades; f = next; )
337 f->cur += f->step * steps;
343 printf ("opacity now %g\n", f->cur);
345 w->opacity = f->cur * OPAQUE;
346 need_dequeue = False;
349 if (f->cur >= f->finish)
351 w->opacity = f->finish*OPAQUE;
357 if (f->cur <= f->finish)
359 w->opacity = f->finish*OPAQUE;
363 determine_mode (dpy, w);
366 XRenderFreePicture (dpy, w->shadow);
368 w->extents = win_extents(dpy, w);
370 /* Must do this last as it might destroy f->w in callbacks */
372 dequeue_fade (dpy, f);
374 fade_time = now + fade_delta;
378 gaussian (double r, double x, double y)
380 return ((1 / (sqrt (2 * M_PI * r))) *
381 exp ((- (x * x + y * y)) / (2 * r * r)));
386 make_gaussian_map (Display *dpy, double r)
389 int size = ((int) ceil ((r * 3)) + 1) & ~1;
390 int center = size / 2;
395 c = malloc (sizeof (conv) + size * size * sizeof (double));
397 c->data = (double *) (c + 1);
399 for (y = 0; y < size; y++)
400 for (x = 0; x < size; x++)
402 g = gaussian (r, (double) (x - center), (double) (y - center));
404 c->data[y * size + x] = g;
406 /* printf ("gaussian total %f\n", t); */
407 for (y = 0; y < size; y++)
408 for (x = 0; x < size; x++)
410 c->data[y*size + x] /= t;
416 * A picture will help
418 * -center 0 width width+center
419 * -center +-----+-------------------+-----+
422 * 0 +-----+-------------------+-----+
426 * height +-----+-------------------+-----+
429 * center +-----+-------------------+-----+
433 sum_gaussian (conv *map, double opacity, int x, int y, int width, int height)
437 double *g_line = map->data;
438 int g_size = map->size;
439 int center = g_size / 2;
440 int fx_start, fx_end;
441 int fy_start, fy_end;
445 * Compute set of filter values which are "in range",
446 * that's the set with:
447 * 0 <= x + (fx-center) && x + (fx-center) < width &&
448 * 0 <= y + (fy-center) && y + (fy-center) < height
450 * 0 <= x + (fx - center) x + fx - center < width
451 * center - x <= fx fx < width + center - x
454 fx_start = center - x;
457 fx_end = width + center - x;
461 fy_start = center - y;
464 fy_end = height + center - y;
468 g_line = g_line + fy_start * g_size + fx_start;
471 for (fy = fy_start; fy < fy_end; fy++)
476 for (fx = fx_start; fx < fx_end; fx++)
482 return ((unsigned char) (v * opacity * 255.0));
485 /* precompute shadow corners and sides to save time for large windows */
487 presum_gaussian (conv *map)
489 int center = map->size/2;
495 free ((void *)shadowCorner);
497 free ((void *)shadowTop);
499 shadowCorner = (unsigned char *)(malloc ((Gsize + 1) * (Gsize + 1) * 26));
500 shadowTop = (unsigned char *)(malloc ((Gsize + 1) * 26));
502 for (x = 0; x <= Gsize; x++)
504 shadowTop[25 * (Gsize + 1) + x] = sum_gaussian (map, 1, x - center, center, Gsize * 2, Gsize * 2);
505 for(opacity = 0; opacity < 25; opacity++)
506 shadowTop[opacity * (Gsize + 1) + x] = shadowTop[25 * (Gsize + 1) + x] * opacity / 25;
507 for(y = 0; y <= x; y++)
509 shadowCorner[25 * (Gsize + 1) * (Gsize + 1) + y * (Gsize + 1) + x]
510 = sum_gaussian (map, 1, x - center, y - center, Gsize * 2, Gsize * 2);
511 shadowCorner[25 * (Gsize + 1) * (Gsize + 1) + x * (Gsize + 1) + y]
512 = shadowCorner[25 * (Gsize + 1) * (Gsize + 1) + y * (Gsize + 1) + x];
513 for(opacity = 0; opacity < 25; opacity++)
514 shadowCorner[opacity * (Gsize + 1) * (Gsize + 1) + y * (Gsize + 1) + x]
515 = shadowCorner[opacity * (Gsize + 1) * (Gsize + 1) + x * (Gsize + 1) + y]
516 = shadowCorner[25 * (Gsize + 1) * (Gsize + 1) + y * (Gsize + 1) + x] * opacity / 25;
522 make_shadow (Display *dpy, double opacity, int width, int height)
526 int gsize = gaussianMap->size;
528 int swidth = width + gsize;
529 int sheight = height + gsize;
530 int center = gsize / 2;
534 int opacity_int = (int)(opacity * 25);
535 data = malloc (swidth * sheight * sizeof (unsigned char));
538 ximage = XCreateImage (dpy,
539 DefaultVisual(dpy, DefaultScreen(dpy)),
544 swidth, sheight, 8, swidth * sizeof (unsigned char));
551 * Build the gaussian in sections
555 * center (fill the complete data array)
558 d = shadowTop[opacity_int * (Gsize + 1) + Gsize];
560 d = sum_gaussian (gaussianMap, opacity, center, center, width, height);
561 memset(data, d, sheight * swidth);
567 if (ylimit > sheight / 2)
568 ylimit = (sheight + 1) / 2;
570 if (xlimit > swidth / 2)
571 xlimit = (swidth + 1) / 2;
573 for (y = 0; y < ylimit; y++)
574 for (x = 0; x < xlimit; x++)
576 if (xlimit == Gsize && ylimit == Gsize)
577 d = shadowCorner[opacity_int * (Gsize + 1) * (Gsize + 1) + y * (Gsize + 1) + x];
579 d = sum_gaussian (gaussianMap, opacity, x - center, y - center, width, height);
580 data[y * swidth + x] = d;
581 data[(sheight - y - 1) * swidth + x] = d;
582 data[(sheight - y - 1) * swidth + (swidth - x - 1)] = d;
583 data[y * swidth + (swidth - x - 1)] = d;
589 x_diff = swidth - (gsize * 2);
590 if (x_diff > 0 && ylimit > 0)
592 for (y = 0; y < ylimit; y++)
595 d = shadowTop[opacity_int * (Gsize + 1) + y];
597 d = sum_gaussian (gaussianMap, opacity, center, y - center, width, height);
598 memset (&data[y * swidth + gsize], d, x_diff);
599 memset (&data[(sheight - y - 1) * swidth + gsize], d, x_diff);
607 for (x = 0; x < xlimit; x++)
610 d = shadowTop[opacity_int * (Gsize + 1) + x];
612 d = sum_gaussian (gaussianMap, opacity, x - center, center, width, height);
613 for (y = gsize; y < sheight - gsize; y++)
615 data[y * swidth + x] = d;
616 data[y * swidth + (swidth - x - 1)] = d;
624 shadow_picture (Display *dpy, double opacity, Picture alpha_pict, int width, int height, int *wp, int *hp)
628 Picture shadowPicture;
631 shadowImage = make_shadow (dpy, opacity, width, height);
634 shadowPixmap = XCreatePixmap (dpy, root,
640 XDestroyImage (shadowImage);
644 shadowPicture = XRenderCreatePicture (dpy, shadowPixmap,
645 XRenderFindStandardFormat (dpy, PictStandardA8),
649 XDestroyImage (shadowImage);
650 XFreePixmap (dpy, shadowPixmap);
654 gc = XCreateGC (dpy, shadowPixmap, 0, 0);
657 XDestroyImage (shadowImage);
658 XFreePixmap (dpy, shadowPixmap);
659 XRenderFreePicture (dpy, shadowPicture);
663 XPutImage (dpy, shadowPixmap, gc, shadowImage, 0, 0, 0, 0,
665 shadowImage->height);
666 *wp = shadowImage->width;
667 *hp = shadowImage->height;
669 XDestroyImage (shadowImage);
670 XFreePixmap (dpy, shadowPixmap);
671 return shadowPicture;
675 solid_picture (Display *dpy, Bool argb, double a, double r, double g, double b)
679 XRenderPictureAttributes pa;
682 pixmap = XCreatePixmap (dpy, root, 1, 1, argb ? 32 : 8);
687 picture = XRenderCreatePicture (dpy, pixmap,
688 XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8),
693 XFreePixmap (dpy, pixmap);
697 c.alpha = a * 0xffff;
699 c.green = g * 0xffff;
701 XRenderFillRectangle (dpy, PictOpSrc, picture, &c, 0, 0, 1, 1);
702 XFreePixmap (dpy, pixmap);
707 discard_ignore (Display *dpy, unsigned long sequence)
711 if ((long) (sequence - ignore_head->sequence) > 0)
713 ignore *next = ignore_head->next;
717 ignore_tail = &ignore_head;
725 set_ignore (Display *dpy, unsigned long sequence)
727 ignore *i = malloc (sizeof (ignore));
730 i->sequence = sequence;
733 ignore_tail = &i->next;
737 should_ignore (Display *dpy, unsigned long sequence)
739 discard_ignore (dpy, sequence);
740 return ignore_head && ignore_head->sequence == sequence;
744 find_win (Display *dpy, Window id)
748 for (w = list; w; w = w->next)
754 static char *backgroundProps[] = {
761 root_tile (Display *dpy)
767 unsigned long nitems;
768 unsigned long bytes_after;
771 XRenderPictureAttributes pa;
775 for (p = 0; backgroundProps[p]; p++)
777 if (XGetWindowProperty (dpy, root, XInternAtom (dpy, backgroundProps[p], False),
778 0, 4, False, AnyPropertyType,
779 &actual_type, &actual_format, &nitems, &bytes_after, &prop) == Success &&
780 actual_type == XInternAtom (dpy, "PIXMAP", False) && actual_format == 32 && nitems == 1)
782 memcpy (&pixmap, prop, 4);
790 pixmap = XCreatePixmap (dpy, root, 1, 1, DefaultDepth (dpy, scr));
794 picture = XRenderCreatePicture (dpy, pixmap,
795 XRenderFindVisualFormat (dpy,
796 DefaultVisual (dpy, scr)),
802 c.red = c.green = c.blue = 0x8080;
804 XRenderFillRectangle (dpy, PictOpSrc, picture, &c,
811 paint_root (Display *dpy)
814 rootTile = root_tile (dpy);
816 XRenderComposite (dpy, PictOpSrc,
817 rootTile, None, rootBuffer,
818 0, 0, 0, 0, 0, 0, root_width, root_height);
822 win_extents (Display *dpy, win *w)
828 r.width = w->a.width + w->a.border_width * 2;
829 r.height = w->a.height + w->a.border_width * 2;
830 if (compMode != CompSimple && !(w->windowType == winDockAtom && excludeDockShadows))
832 if (compMode == CompServerShadows || w->mode != WINDOW_ARGB)
836 if (compMode == CompServerShadows)
840 w->shadow_width = w->a.width;
841 w->shadow_height = w->a.height;
845 w->shadow_dx = shadowOffsetX;
846 w->shadow_dy = shadowOffsetY;
849 double opacity = shadowOpacity;
850 if (w->mode == WINDOW_TRANS)
851 opacity = opacity * ((double)w->opacity)/((double)OPAQUE);
852 w->shadow = shadow_picture (dpy, opacity, w->alphaPict,
853 w->a.width + w->a.border_width * 2,
854 w->a.height + w->a.border_width * 2,
855 &w->shadow_width, &w->shadow_height);
858 sr.x = w->a.x + w->shadow_dx;
859 sr.y = w->a.y + w->shadow_dy;
860 sr.width = w->shadow_width;
861 sr.height = w->shadow_height;
864 r.width = (r.x + r.width) - sr.x;
869 r.height = (r.y + r.height) - sr.y;
872 if (sr.x + sr.width > r.x + r.width)
873 r.width = sr.x + sr.width - r.x;
874 if (sr.y + sr.height > r.y + r.height)
875 r.height = sr.y + sr.height - r.y;
878 return XFixesCreateRegion (dpy, &r, 1);
882 border_size (Display *dpy, win *w)
884 XserverRegion border;
886 * if window doesn't exist anymore, this will generate an error
887 * as well as not generate a region. Perhaps a better XFixes
888 * architecture would be to have a request that copies instead
889 * of creates, that way you'd just end up with an empty region
890 * instead of an invalid XID.
892 set_ignore (dpy, NextRequest (dpy));
893 border = XFixesCreateRegionFromWindow (dpy, w->id, WindowRegionBounding);
895 set_ignore (dpy, NextRequest (dpy));
896 XFixesTranslateRegion (dpy, border,
897 w->a.x + w->a.border_width,
898 w->a.y + w->a.border_width);
903 paint_all (Display *dpy, XserverRegion region)
913 r.width = root_width;
914 r.height = root_height;
915 region = XFixesCreateRegion (dpy, &r, 1);
918 rootBuffer = rootPicture;
922 Pixmap rootPixmap = XCreatePixmap (dpy, root, root_width, root_height,
923 DefaultDepth (dpy, scr));
924 rootBuffer = XRenderCreatePicture (dpy, rootPixmap,
925 XRenderFindVisualFormat (dpy,
926 DefaultVisual (dpy, scr)),
928 XFreePixmap (dpy, rootPixmap);
931 XFixesSetPictureClipRegion (dpy, rootPicture, 0, 0, region);
933 XRenderComposite (dpy, PictOpSrc, blackPicture, None, rootPicture,
934 0, 0, 0, 0, 0, 0, root_width, root_height);
939 for (w = list; w; w = w->next)
945 /* never painted, ignore it */
948 /* if invisible, ignore it */
949 if (w->a.x + w->a.width < 1 || w->a.y + w->a.height < 1
950 || w->a.x >= root_width || w->a.y >= root_height)
954 XRenderPictureAttributes pa;
955 XRenderPictFormat *format;
956 Drawable draw = w->id;
958 #if HAS_NAME_WINDOW_PIXMAP
959 if (hasNamePixmap && !w->pixmap)
960 w->pixmap = XCompositeNameWindowPixmap (dpy, w->id);
964 format = XRenderFindVisualFormat (dpy, w->a.visual);
965 pa.subwindow_mode = IncludeInferiors;
966 w->picture = XRenderCreatePicture (dpy, draw,
972 printf (" 0x%x", w->id);
978 set_ignore (dpy, NextRequest (dpy));
979 XFixesDestroyRegion (dpy, w->borderSize);
980 w->borderSize = None;
984 XFixesDestroyRegion (dpy, w->extents);
989 XFixesDestroyRegion (dpy, w->borderClip);
990 w->borderClip = None;
994 w->borderSize = border_size (dpy, w);
996 w->extents = win_extents (dpy, w);
997 if (w->mode == WINDOW_SOLID)
1000 #if HAS_NAME_WINDOW_PIXMAP
1003 wid = w->a.width + w->a.border_width * 2;
1004 hei = w->a.height + w->a.border_width * 2;
1006 x = w->a.x + w->a.border_width;
1007 y = w->a.y + w->a.border_width;
1011 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
1012 set_ignore (dpy, NextRequest (dpy));
1013 XFixesSubtractRegion (dpy, region, region, w->borderSize);
1014 set_ignore (dpy, NextRequest (dpy));
1015 XRenderComposite (dpy, PictOpSrc, w->picture, None, rootBuffer,
1021 w->borderClip = XFixesCreateRegion (dpy, 0, 0);
1022 XFixesCopyRegion (dpy, w->borderClip, region);
1031 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
1033 for (w = t; w; w = w->prev_trans)
1035 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, w->borderClip);
1039 case CompServerShadows:
1040 /* dont' bother drawing shadows on desktop windows */
1041 if (w->windowType == winDesktopAtom)
1043 set_ignore (dpy, NextRequest (dpy));
1044 if (w->opacity != OPAQUE && !w->shadowPict)
1045 w->shadowPict = solid_picture (dpy, True,
1046 (double) w->opacity / OPAQUE * 0.3,
1048 XRenderComposite (dpy, PictOpOver,
1049 w->shadowPict ? w->shadowPict : transBlackPicture,
1050 w->picture, rootBuffer,
1052 w->a.x + w->shadow_dx,
1053 w->a.y + w->shadow_dy,
1054 w->shadow_width, w->shadow_height);
1056 case CompClientShadows:
1057 /* don't bother drawing shadows on desktop windows */
1058 if (w->shadow && w->windowType != winDesktopAtom)
1060 XRenderComposite (dpy, PictOpOver, blackPicture, w->shadow, rootBuffer,
1062 w->a.x + w->shadow_dx,
1063 w->a.y + w->shadow_dy,
1064 w->shadow_width, w->shadow_height);
1068 if (w->opacity != OPAQUE && !w->alphaPict)
1069 w->alphaPict = solid_picture (dpy, False,
1070 (double) w->opacity / OPAQUE, 0, 0, 0);
1071 if (w->mode == WINDOW_TRANS)
1074 #if HAS_NAME_WINDOW_PIXMAP
1077 wid = w->a.width + w->a.border_width * 2;
1078 hei = w->a.height + w->a.border_width * 2;
1080 x = w->a.x + w->a.border_width;
1081 y = w->a.y + w->a.border_width;
1085 set_ignore (dpy, NextRequest (dpy));
1086 XRenderComposite (dpy, PictOpOver, w->picture, w->alphaPict, rootBuffer,
1090 else if (w->mode == WINDOW_ARGB)
1093 #if HAS_NAME_WINDOW_PIXMAP
1096 wid = w->a.width + w->a.border_width * 2;
1097 hei = w->a.height + w->a.border_width * 2;
1099 x = w->a.x + w->a.border_width;
1100 y = w->a.y + w->a.border_width;
1104 set_ignore (dpy, NextRequest (dpy));
1105 XRenderComposite (dpy, PictOpOver, w->picture, w->alphaPict, rootBuffer,
1109 XFixesDestroyRegion (dpy, w->borderClip);
1110 w->borderClip = None;
1112 XFixesDestroyRegion (dpy, region);
1113 if (rootBuffer != rootPicture)
1115 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, None);
1116 XRenderComposite (dpy, PictOpSrc, rootBuffer, None, rootPicture,
1117 0, 0, 0, 0, 0, 0, root_width, root_height);
1122 add_damage (Display *dpy, XserverRegion damage)
1126 XFixesUnionRegion (dpy, allDamage, allDamage, damage);
1127 XFixesDestroyRegion (dpy, damage);
1134 repair_win (Display *dpy, win *w)
1136 XserverRegion parts;
1140 parts = win_extents (dpy, w);
1141 set_ignore (dpy, NextRequest (dpy));
1142 XDamageSubtract (dpy, w->damage, None, None);
1147 parts = XFixesCreateRegion (dpy, 0, 0);
1148 set_ignore (dpy, NextRequest (dpy));
1149 XDamageSubtract (dpy, w->damage, None, parts);
1150 XFixesTranslateRegion (dpy, parts,
1151 w->a.x + w->a.border_width,
1152 w->a.y + w->a.border_width);
1153 if (compMode == CompServerShadows)
1155 o = XFixesCreateRegion (dpy, 0, 0);
1156 XFixesCopyRegion (dpy, o, parts);
1157 XFixesTranslateRegion (dpy, o, w->shadow_dx, w->shadow_dy);
1158 XFixesUnionRegion (dpy, parts, parts, o);
1159 XFixesDestroyRegion (dpy, o);
1162 add_damage (dpy, parts);
1167 get_opacity_prop (Display *dpy, win *w, unsigned int def);
1170 map_win (Display *dpy, Window id, unsigned long sequence, Bool fade)
1172 win *w = find_win (dpy, id);
1177 w->a.map_state = IsViewable;
1179 /* This needs to be here or else we lose transparency messages */
1180 XSelectInput (dpy, id, PropertyChangeMask);
1182 /* This needs to be here since we don't get PropertyNotify when unmapped */
1183 w->opacity = get_opacity_prop (dpy, w, OPAQUE);
1184 determine_mode (dpy, w);
1187 w->damage_bounds.x = w->damage_bounds.y = 0;
1188 w->damage_bounds.width = w->damage_bounds.height = 0;
1192 if (fade && fadeWindows)
1193 set_fade (dpy, w, 0, get_opacity_percent (dpy, w, 1.0), fade_in_step, 0, False, True, True);
1197 finish_unmap_win (Display *dpy, win *w)
1203 if (w->extents != None)
1205 add_damage (dpy, w->extents); /* destroys region */
1209 #if HAS_NAME_WINDOW_PIXMAP
1212 XFreePixmap (dpy, w->pixmap);
1219 set_ignore (dpy, NextRequest (dpy));
1220 XRenderFreePicture (dpy, w->picture);
1224 /* don't care about properties anymore */
1225 set_ignore (dpy, NextRequest (dpy));
1226 XSelectInput(dpy, w->id, 0);
1230 set_ignore (dpy, NextRequest (dpy));
1231 XFixesDestroyRegion (dpy, w->borderSize);
1232 w->borderSize = None;
1236 XRenderFreePicture (dpy, w->shadow);
1241 XFixesDestroyRegion (dpy, w->borderClip);
1242 w->borderClip = None;
1248 #if HAS_NAME_WINDOW_PIXMAP
1250 unmap_callback (Display *dpy, win *w, Bool gone)
1252 finish_unmap_win (dpy, w);
1257 unmap_win (Display *dpy, Window id, Bool fade)
1259 win *w = find_win (dpy, id);
1262 w->a.map_state = IsUnmapped;
1263 #if HAS_NAME_WINDOW_PIXMAP
1264 if (w->pixmap && fade && fadeWindows)
1265 set_fade (dpy, w, w->opacity*1.0/OPAQUE, 0.0, fade_out_step, unmap_callback, False, False, True);
1268 finish_unmap_win (dpy, w);
1271 /* Get the opacity prop from window
1276 get_opacity_prop(Display *dpy, win *w, unsigned int def)
1280 unsigned long n, left;
1282 unsigned char *data;
1283 int result = XGetWindowProperty(dpy, w->id, opacityAtom, 0L, 1L, False,
1284 XA_CARDINAL, &actual, &format,
1286 if (result == Success && data != NULL)
1289 memcpy (&i, data, sizeof (unsigned int));
1290 XFree( (void *) data);
1296 /* Get the opacity property from the window in a percent format
1298 otherwise: the value
1301 get_opacity_percent(Display *dpy, win *w, double def)
1303 unsigned int opacity = get_opacity_prop (dpy, w, (unsigned int)(OPAQUE*def));
1305 return opacity*1.0/OPAQUE;
1308 /* determine mode for window all in one place.
1309 Future might check for menu flag and other cool things
1313 get_wintype_prop(Display * dpy, Window w)
1317 unsigned long n, left;
1319 unsigned char *data;
1320 int result = XGetWindowProperty (dpy, w, winTypeAtom, 0L, 1L, False,
1321 XA_ATOM, &actual, &format,
1324 if (result == Success && data != None)
1327 memcpy (&a, data, sizeof (Atom));
1328 XFree ( (void *) data);
1331 return winNormalAtom;
1335 determine_mode(Display *dpy, win *w)
1338 XRenderPictFormat *format;
1340 /* if trans prop == -1 fall back on previous tests*/
1344 XRenderFreePicture (dpy, w->alphaPict);
1345 w->alphaPict = None;
1349 XRenderFreePicture (dpy, w->shadowPict);
1350 w->shadowPict = None;
1353 if (w->a.class == InputOnly)
1359 format = XRenderFindVisualFormat (dpy, w->a.visual);
1362 if (format && format->type == PictTypeDirect && format->direct.alphaMask)
1366 else if (w->opacity != OPAQUE)
1368 mode = WINDOW_TRANS;
1372 mode = WINDOW_SOLID;
1377 XserverRegion damage;
1378 damage = XFixesCreateRegion (dpy, 0, 0);
1379 XFixesCopyRegion (dpy, damage, w->extents);
1380 add_damage (dpy, damage);
1385 determine_wintype (Display *dpy, Window w)
1387 Window root_return, parent_return;
1388 Window *children = NULL;
1389 unsigned int nchildren, i;
1392 type = get_wintype_prop (dpy, w);
1393 if (type != winNormalAtom)
1396 if (!XQueryTree (dpy, w, &root_return, &parent_return, &children,
1399 /* XQueryTree failed. */
1401 XFree ((void *)children);
1402 return winNormalAtom;
1405 for (i = 0;i < nchildren;i++)
1407 type = determine_wintype (dpy, children[i]);
1408 if (type != winNormalAtom)
1413 XFree ((void *)children);
1415 return winNormalAtom;
1419 add_win (Display *dpy, Window id, Window prev)
1421 win *new = malloc (sizeof (win));
1428 for (p = &list; *p; p = &(*p)->next)
1429 if ((*p)->id == prev)
1435 set_ignore (dpy, NextRequest (dpy));
1436 if (!XGetWindowAttributes (dpy, id, &new->a))
1443 new->usable = False;
1445 #if HAS_NAME_WINDOW_PIXMAP
1448 new->picture = None;
1449 if (new->a.class == InputOnly)
1451 new->damage_sequence = 0;
1456 new->damage_sequence = NextRequest (dpy);
1457 new->damage = XDamageCreate (dpy, id, XDamageReportNonEmpty);
1459 new->alphaPict = None;
1460 new->shadowPict = None;
1461 new->borderSize = None;
1462 new->extents = None;
1466 new->shadow_width = 0;
1467 new->shadow_height = 0;
1468 new->opacity = OPAQUE;
1470 new->borderClip = None;
1471 new->prev_trans = 0;
1473 new->windowType = determine_wintype (dpy, new->id);
1477 if (new->a.map_state == IsViewable)
1478 map_win (dpy, id, new->damage_sequence - 1, True);
1482 restack_win (Display *dpy, win *w, Window new_above)
1487 old_above = w->next->id;
1490 if (old_above != new_above)
1495 for (prev = &list; *prev; prev = &(*prev)->next)
1501 for (prev = &list; *prev; prev = &(*prev)->next)
1503 if ((*prev)->id == new_above)
1512 configure_win (Display *dpy, XConfigureEvent *ce)
1514 win *w = find_win (dpy, ce->window);
1515 XserverRegion damage = None;
1519 if (ce->window == root)
1523 XRenderFreePicture (dpy, rootBuffer);
1526 root_width = ce->width;
1527 root_height = ce->height;
1535 damage = XFixesCreateRegion (dpy, 0, 0);
1536 if (w->extents != None)
1537 XFixesCopyRegion (dpy, damage, w->extents);
1541 if (w->a.width != ce->width || w->a.height != ce->height)
1543 #if HAS_NAME_WINDOW_PIXMAP
1546 XFreePixmap (dpy, w->pixmap);
1550 XRenderFreePicture (dpy, w->picture);
1557 XRenderFreePicture (dpy, w->shadow);
1561 w->a.width = ce->width;
1562 w->a.height = ce->height;
1563 w->a.border_width = ce->border_width;
1564 w->a.override_redirect = ce->override_redirect;
1565 restack_win (dpy, w, ce->above);
1568 XserverRegion extents = win_extents (dpy, w);
1569 XFixesUnionRegion (dpy, damage, damage, extents);
1570 XFixesDestroyRegion (dpy, extents);
1571 add_damage (dpy, damage);
1577 circulate_win (Display *dpy, XCirculateEvent *ce)
1579 win *w = find_win (dpy, ce->window);
1585 if (ce->place == PlaceOnTop)
1586 new_above = list->id;
1589 restack_win (dpy, w, new_above);
1594 finish_destroy_win (Display *dpy, Window id, Bool gone)
1598 for (prev = &list; (w = *prev); prev = &w->next)
1602 finish_unmap_win (dpy, w);
1606 set_ignore (dpy, NextRequest (dpy));
1607 XRenderFreePicture (dpy, w->picture);
1612 XRenderFreePicture (dpy, w->alphaPict);
1613 w->alphaPict = None;
1617 XRenderFreePicture (dpy, w->shadowPict);
1618 w->shadowPict = None;
1620 if (w->damage != None)
1622 set_ignore (dpy, NextRequest (dpy));
1623 XDamageDestroy (dpy, w->damage);
1626 cleanup_fade (dpy, w);
1632 #if HAS_NAME_WINDOW_PIXMAP
1634 destroy_callback (Display *dpy, win *w, Bool gone)
1636 finish_destroy_win (dpy, w->id, gone);
1641 destroy_win (Display *dpy, Window id, Bool gone, Bool fade)
1643 win *w = find_win (dpy, id);
1644 #if HAS_NAME_WINDOW_PIXMAP
1645 if (w && w->pixmap && fade && fadeWindows)
1646 set_fade (dpy, w, w->opacity*1.0/OPAQUE, 0.0, fade_out_step, destroy_callback, gone, False, True);
1650 finish_destroy_win (dpy, id, gone);
1658 printf ("\t%08lx: %d x %d + %d + %d (%d)\n", w->id,
1659 w->a.width, w->a.height, w->a.x, w->a.y, w->a.border_width);
1668 printf ("windows:\n");
1669 for (w = list; w; w = w->next)
1675 damage_win (Display *dpy, XDamageNotifyEvent *de)
1677 win *w = find_win (dpy, de->drawable);
1684 if (w->damage_bounds.width == 0 || w->damage_bounds.height == 0)
1686 w->damage_bounds = de->area;
1690 if (de->area.x < w->damage_bounds.x)
1692 w->damage_bounds.width += (w->damage_bounds.x - de->area.x);
1693 w->damage_bounds.x = de->area.x;
1695 if (de->area.y < w->damage_bounds.y)
1697 w->damage_bounds.height += (w->damage_bounds.y - de->area.y);
1698 w->damage_bounds.y = de->area.y;
1700 if (de->area.x + de->area.width > w->damage_bounds.x + w->damage_bounds.width)
1701 w->damage_bounds.width = de->area.x + de->area.width - w->damage_bounds.x;
1702 if (de->area.y + de->area.height > w->damage_bounds.y + w->damage_bounds.height)
1703 w->damage_bounds.height = de->area.y + de->area.height - w->damage_bounds.y;
1706 printf ("unusable damage %d, %d: %d x %d bounds %d, %d: %d x %d\n",
1713 w->damage_bounds.width,
1714 w->damage_bounds.height);
1716 if (w->damage_bounds.x <= 0 &&
1717 w->damage_bounds.y <= 0 &&
1718 w->a.width <= w->damage_bounds.x + w->damage_bounds.width &&
1719 w->a.height <= w->damage_bounds.y + w->damage_bounds.height)
1723 set_fade (dpy, w, 0, get_opacity_percent (dpy, w, 1.0), fade_in_step, 0, False, True, True);
1729 repair_win (dpy, w);
1733 error (Display *dpy, XErrorEvent *ev)
1738 if (should_ignore (dpy, ev->serial))
1741 if (ev->request_code == composite_opcode &&
1742 ev->minor_code == X_CompositeRedirectSubwindows)
1744 fprintf (stderr, "Another composite manager is already running\n");
1748 o = ev->error_code - xfixes_error;
1750 case BadRegion: name = "BadRegion"; break;
1753 o = ev->error_code - damage_error;
1755 case BadDamage: name = "BadDamage"; break;
1758 o = ev->error_code - render_error;
1760 case BadPictFormat: name ="BadPictFormat"; break;
1761 case BadPicture: name ="BadPicture"; break;
1762 case BadPictOp: name ="BadPictOp"; break;
1763 case BadGlyphSet: name ="BadGlyphSet"; break;
1764 case BadGlyph: name ="BadGlyph"; break;
1768 printf ("error %d request %d minor %d serial %lu\n",
1769 ev->error_code, ev->request_code, ev->minor_code, ev->serial);
1771 /* abort (); this is just annoying to most people */
1776 expose_root (Display *dpy, Window root, XRectangle *rects, int nrects)
1778 XserverRegion region = XFixesCreateRegion (dpy, rects, nrects);
1780 add_damage (dpy, region);
1785 ev_serial (XEvent *ev)
1787 if (ev->type & 0x7f != KeymapNotify)
1788 return ev->xany.serial;
1789 return NextRequest (ev->xany.display);
1793 ev_name (XEvent *ev)
1795 static char buf[128];
1796 switch (ev->type & 0x7f) {
1803 case ReparentNotify:
1805 case CirculateNotify:
1808 if (ev->type == damage_event + XDamageNotify)
1810 sprintf (buf, "Event %d", ev->type);
1816 ev_window (XEvent *ev)
1820 return ev->xexpose.window;
1822 return ev->xmap.window;
1824 return ev->xunmap.window;
1825 case ReparentNotify:
1826 return ev->xreparent.window;
1827 case CirculateNotify:
1828 return ev->xcirculate.window;
1830 if (ev->type == damage_event + XDamageNotify)
1831 return ((XDamageNotifyEvent *) ev)->drawable;
1838 usage (char *program)
1840 fprintf (stderr, "%s v1.1.3\n", program);
1841 fprintf (stderr, "usage: %s [options]\n", program);
1842 fprintf (stderr, "Options\n");
1843 fprintf (stderr, " -d display\n Specifies which display should be managed.\n");
1844 fprintf (stderr, " -r radius\n Specifies the blur radius for client-side shadows. (default 12)\n");
1845 fprintf (stderr, " -o opacity\n Specifies the translucency for client-side shadows. (default .75)\n");
1846 fprintf (stderr, " -l left-offset\n Specifies the left offset for client-side shadows. (default -15)\n");
1847 fprintf (stderr, " -t top-offset\n Specifies the top offset for clinet-side shadows. (default -15)\n");
1848 fprintf (stderr, " -I fade-in-step\n Specifies the opacity change between steps while fading in. (default 0.028)\n");
1849 fprintf (stderr, " -O fade-out-step\n Specifies the opacity change between steps while fading out. (default 0.03)\n");
1850 fprintf (stderr, " -D fade-delta-time\n Specifies the time between steps in a fade in milliseconds. (default 10)\n");
1851 fprintf (stderr, " -a\n Use automatic server-side compositing. Faster, but no special effects.\n");
1852 fprintf (stderr, " -c\n Draw client-side shadows with fuzzy edges.\n");
1853 fprintf (stderr, " -C\n Avoid drawing shadows on dock/panel windows.\n");
1854 fprintf (stderr, " -f\n Fade windows in/out when opening/closing.\n");
1855 fprintf (stderr, " -F\n Fade windows during opacity changes.\n");
1856 fprintf (stderr, " -n\n Normal client-side compositing with transparency support\n");
1857 fprintf (stderr, " -s\n Draw server-side shadows with sharp edges.\n");
1858 fprintf (stderr, " -S\n Enable synchronous operation (for debugging).\n");
1868 w = XCreateSimpleWindow (dpy, RootWindow (dpy, 0), 0, 0, 1, 1, 0, None,
1871 Xutf8SetWMProperties (dpy, w, "xcompmgr", "xcompmgr", NULL, 0, NULL, NULL,
1874 /* FIXME: Don't hard code the screen number */
1875 a = XInternAtom (dpy, "_NET_WM_CM_S0", False);
1877 XSetSelectionOwner (dpy, a, w, 0);
1881 main (int argc, char **argv)
1884 Window root_return, parent_return;
1886 unsigned int nchildren;
1888 XRenderPictureAttributes pa;
1889 XRectangle *expose_rects = 0;
1890 int size_expose = 0;
1894 int composite_major, composite_minor;
1898 while ((o = getopt (argc, argv, "D:I:O:d:r:o:l:t:scnfFCaS")) != -1)
1905 fade_delta = atoi (optarg);
1910 fade_in_step = atof (optarg);
1911 if (fade_in_step <= 0)
1912 fade_in_step = 0.01;
1915 fade_out_step = atof (optarg);
1916 if (fade_out_step <= 0)
1917 fade_out_step = 0.01;
1920 compMode = CompServerShadows;
1923 compMode = CompClientShadows;
1926 excludeDockShadows = True;
1929 compMode = CompSimple;
1938 autoRedirect = True;
1944 shadowRadius = atoi (optarg);
1947 shadowOpacity = atof (optarg);
1950 shadowOffsetX = atoi (optarg);
1953 shadowOffsetY = atoi (optarg);
1961 dpy = XOpenDisplay (display);
1964 fprintf (stderr, "Can't open display\n");
1967 XSetErrorHandler (error);
1969 XSynchronize (dpy, 1);
1970 scr = DefaultScreen (dpy);
1971 root = RootWindow (dpy, scr);
1973 if (!XRenderQueryExtension (dpy, &render_event, &render_error))
1975 fprintf (stderr, "No render extension\n");
1978 if (!XQueryExtension (dpy, COMPOSITE_NAME, &composite_opcode,
1979 &composite_event, &composite_error))
1981 fprintf (stderr, "No composite extension\n");
1984 XCompositeQueryVersion (dpy, &composite_major, &composite_minor);
1985 #if HAS_NAME_WINDOW_PIXMAP
1986 if (composite_major > 0 || composite_minor >= 2)
1987 hasNamePixmap = True;
1990 if (!XDamageQueryExtension (dpy, &damage_event, &damage_error))
1992 fprintf (stderr, "No damage extension\n");
1995 if (!XFixesQueryExtension (dpy, &xfixes_event, &xfixes_error))
1997 fprintf (stderr, "No XFixes extension\n");
2004 opacityAtom = XInternAtom (dpy, OPACITY_PROP, False);
2005 winTypeAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE", False);
2006 winDesktopAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DESKTOP", False);
2007 winDockAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DOCK", False);
2008 winToolbarAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_TOOLBAR", False);
2009 winMenuAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_MENU", False);
2010 winUtilAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_UTILITY", False);
2011 winSplashAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_SPLASH", False);
2012 winDialogAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
2013 winNormalAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_NORMAL", False);
2015 pa.subwindow_mode = IncludeInferiors;
2017 if (compMode == CompClientShadows)
2019 gaussianMap = make_gaussian_map(dpy, shadowRadius);
2020 presum_gaussian (gaussianMap);
2023 root_width = DisplayWidth (dpy, scr);
2024 root_height = DisplayHeight (dpy, scr);
2026 rootPicture = XRenderCreatePicture (dpy, root,
2027 XRenderFindVisualFormat (dpy,
2028 DefaultVisual (dpy, scr)),
2031 blackPicture = solid_picture (dpy, True, 1, 0, 0, 0);
2032 if (compMode == CompServerShadows)
2033 transBlackPicture = solid_picture (dpy, True, 0.3, 0, 0, 0);
2038 XCompositeRedirectSubwindows (dpy, root, CompositeRedirectAutomatic);
2041 XCompositeRedirectSubwindows (dpy, root, CompositeRedirectManual);
2042 XSelectInput (dpy, root,
2043 SubstructureNotifyMask|
2045 StructureNotifyMask|
2046 PropertyChangeMask);
2047 XQueryTree (dpy, root, &root_return, &parent_return, &children, &nchildren);
2048 for (i = 0; i < nchildren; i++)
2049 add_win (dpy, children[i], i ? children[i-1] : None);
2052 XUngrabServer (dpy);
2053 ufd.fd = ConnectionNumber (dpy);
2054 ufd.events = POLLIN;
2056 paint_all (dpy, None);
2065 if (poll (&ufd, 1, fade_timeout()) == 0)
2072 XNextEvent (dpy, &ev);
2073 if ((ev.type & 0x7f) != KeymapNotify)
2074 discard_ignore (dpy, ev.xany.serial);
2076 printf ("event %10.10s serial 0x%08x window 0x%08x\n",
2077 ev_name(&ev), ev_serial (&ev), ev_window (&ev));
2079 if (!autoRedirect) switch (ev.type) {
2081 add_win (dpy, ev.xcreatewindow.window, 0);
2083 case ConfigureNotify:
2084 configure_win (dpy, &ev.xconfigure);
2087 destroy_win (dpy, ev.xdestroywindow.window, True, True);
2090 map_win (dpy, ev.xmap.window, ev.xmap.serial, True);
2093 unmap_win (dpy, ev.xunmap.window, True);
2095 case ReparentNotify:
2096 if (ev.xreparent.parent == root)
2097 add_win (dpy, ev.xreparent.window, 0);
2099 destroy_win (dpy, ev.xreparent.window, False, True);
2101 case CirculateNotify:
2102 circulate_win (dpy, &ev.xcirculate);
2105 if (ev.xexpose.window == root)
2107 int more = ev.xexpose.count + 1;
2108 if (n_expose == size_expose)
2112 expose_rects = realloc (expose_rects,
2113 (size_expose + more) *
2114 sizeof (XRectangle));
2115 size_expose += more;
2119 expose_rects = malloc (more * sizeof (XRectangle));
2123 expose_rects[n_expose].x = ev.xexpose.x;
2124 expose_rects[n_expose].y = ev.xexpose.y;
2125 expose_rects[n_expose].width = ev.xexpose.width;
2126 expose_rects[n_expose].height = ev.xexpose.height;
2128 if (ev.xexpose.count == 0)
2130 expose_root (dpy, root, expose_rects, n_expose);
2135 case PropertyNotify:
2136 for (p = 0; backgroundProps[p]; p++)
2138 if (ev.xproperty.atom == XInternAtom (dpy, backgroundProps[p], False))
2142 XClearArea (dpy, root, 0, 0, 0, 0, True);
2143 XRenderFreePicture (dpy, rootTile);
2149 /* check if Trans property was changed */
2150 if (ev.xproperty.atom == opacityAtom)
2152 /* reset mode and redraw window */
2153 win * w = find_win(dpy, ev.xproperty.window);
2157 set_fade (dpy, w, w->opacity*1.0/OPAQUE, get_opacity_percent (dpy, w, 1.0),
2158 fade_out_step, 0, False, True, False);
2161 w->opacity = get_opacity_prop(dpy, w, OPAQUE);
2162 determine_mode(dpy, w);
2165 XRenderFreePicture (dpy, w->shadow);
2167 w->extents = win_extents (dpy, w);
2174 if (ev.type == damage_event + XDamageNotify)
2175 damage_win (dpy, (XDamageNotifyEvent *) &ev);
2178 } while (QLength (dpy));
2179 if (allDamage && !autoRedirect)
2182 paint_all (dpy, allDamage);
2186 clipChanged = False;