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)
629 Picture shadowPicture;
630 Picture finalPicture;
633 shadowImage = make_shadow (dpy, opacity, width, height);
636 shadowPixmap = XCreatePixmap (dpy, root,
642 XDestroyImage (shadowImage);
646 shadowPicture = XRenderCreatePicture (dpy, shadowPixmap,
647 XRenderFindStandardFormat (dpy, PictStandardA8),
651 XDestroyImage (shadowImage);
652 XFreePixmap (dpy, shadowPixmap);
656 gc = XCreateGC (dpy, shadowPixmap, 0, 0);
659 XDestroyImage (shadowImage);
660 XFreePixmap (dpy, shadowPixmap);
661 XRenderFreePicture (dpy, shadowPicture);
665 XPutImage (dpy, shadowPixmap, gc, shadowImage, 0, 0, 0, 0,
667 shadowImage->height);
668 *wp = shadowImage->width;
669 *hp = shadowImage->height;
671 XDestroyImage (shadowImage);
672 XFreePixmap (dpy, shadowPixmap);
673 return shadowPicture;
677 solid_picture (Display *dpy, Bool argb, double a, double r, double g, double b)
681 XRenderPictureAttributes pa;
684 pixmap = XCreatePixmap (dpy, root, 1, 1, argb ? 32 : 8);
689 picture = XRenderCreatePicture (dpy, pixmap,
690 XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8),
695 XFreePixmap (dpy, pixmap);
699 c.alpha = a * 0xffff;
701 c.green = g * 0xffff;
703 XRenderFillRectangle (dpy, PictOpSrc, picture, &c, 0, 0, 1, 1);
704 XFreePixmap (dpy, pixmap);
709 discard_ignore (Display *dpy, unsigned long sequence)
713 if ((long) (sequence - ignore_head->sequence) > 0)
715 ignore *next = ignore_head->next;
719 ignore_tail = &ignore_head;
727 set_ignore (Display *dpy, unsigned long sequence)
729 ignore *i = malloc (sizeof (ignore));
732 i->sequence = sequence;
735 ignore_tail = &i->next;
739 should_ignore (Display *dpy, unsigned long sequence)
741 discard_ignore (dpy, sequence);
742 return ignore_head && ignore_head->sequence == sequence;
746 find_win (Display *dpy, Window id)
750 for (w = list; w; w = w->next)
756 static char *backgroundProps[] = {
763 root_tile (Display *dpy)
769 unsigned long nitems;
770 unsigned long bytes_after;
773 XRenderPictureAttributes pa;
777 for (p = 0; backgroundProps[p]; p++)
779 if (XGetWindowProperty (dpy, root, XInternAtom (dpy, backgroundProps[p], False),
780 0, 4, False, AnyPropertyType,
781 &actual_type, &actual_format, &nitems, &bytes_after, &prop) == Success &&
782 actual_type == XInternAtom (dpy, "PIXMAP", False) && actual_format == 32 && nitems == 1)
784 memcpy (&pixmap, prop, 4);
792 pixmap = XCreatePixmap (dpy, root, 1, 1, DefaultDepth (dpy, scr));
796 picture = XRenderCreatePicture (dpy, pixmap,
797 XRenderFindVisualFormat (dpy,
798 DefaultVisual (dpy, scr)),
804 c.red = c.green = c.blue = 0x8080;
806 XRenderFillRectangle (dpy, PictOpSrc, picture, &c,
813 paint_root (Display *dpy)
816 rootTile = root_tile (dpy);
818 XRenderComposite (dpy, PictOpSrc,
819 rootTile, None, rootBuffer,
820 0, 0, 0, 0, 0, 0, root_width, root_height);
824 win_extents (Display *dpy, win *w)
830 r.width = w->a.width + w->a.border_width * 2;
831 r.height = w->a.height + w->a.border_width * 2;
832 if (compMode != CompSimple && !(w->windowType == winDockAtom && excludeDockShadows))
834 if (compMode == CompServerShadows || w->mode != WINDOW_ARGB)
838 if (compMode == CompServerShadows)
842 w->shadow_width = w->a.width;
843 w->shadow_height = w->a.height;
847 w->shadow_dx = shadowOffsetX;
848 w->shadow_dy = shadowOffsetY;
851 double opacity = shadowOpacity;
852 if (w->mode == WINDOW_TRANS)
853 opacity = opacity * ((double)w->opacity)/((double)OPAQUE);
854 w->shadow = shadow_picture (dpy, opacity, w->alphaPict,
855 w->a.width + w->a.border_width * 2,
856 w->a.height + w->a.border_width * 2,
857 &w->shadow_width, &w->shadow_height);
860 sr.x = w->a.x + w->shadow_dx;
861 sr.y = w->a.y + w->shadow_dy;
862 sr.width = w->shadow_width;
863 sr.height = w->shadow_height;
866 r.width = (r.x + r.width) - sr.x;
871 r.height = (r.y + r.height) - sr.y;
874 if (sr.x + sr.width > r.x + r.width)
875 r.width = sr.x + sr.width - r.x;
876 if (sr.y + sr.height > r.y + r.height)
877 r.height = sr.y + sr.height - r.y;
880 return XFixesCreateRegion (dpy, &r, 1);
884 border_size (Display *dpy, win *w)
886 XserverRegion border;
888 * if window doesn't exist anymore, this will generate an error
889 * as well as not generate a region. Perhaps a better XFixes
890 * architecture would be to have a request that copies instead
891 * of creates, that way you'd just end up with an empty region
892 * instead of an invalid XID.
894 set_ignore (dpy, NextRequest (dpy));
895 border = XFixesCreateRegionFromWindow (dpy, w->id, WindowRegionBounding);
897 set_ignore (dpy, NextRequest (dpy));
898 XFixesTranslateRegion (dpy, border,
899 w->a.x + w->a.border_width,
900 w->a.y + w->a.border_width);
905 paint_all (Display *dpy, XserverRegion region)
915 r.width = root_width;
916 r.height = root_height;
917 region = XFixesCreateRegion (dpy, &r, 1);
920 rootBuffer = rootPicture;
924 Pixmap rootPixmap = XCreatePixmap (dpy, root, root_width, root_height,
925 DefaultDepth (dpy, scr));
926 rootBuffer = XRenderCreatePicture (dpy, rootPixmap,
927 XRenderFindVisualFormat (dpy,
928 DefaultVisual (dpy, scr)),
930 XFreePixmap (dpy, rootPixmap);
933 XFixesSetPictureClipRegion (dpy, rootPicture, 0, 0, region);
935 XRenderComposite (dpy, PictOpSrc, blackPicture, None, rootPicture,
936 0, 0, 0, 0, 0, 0, root_width, root_height);
941 for (w = list; w; w = w->next)
947 /* never painted, ignore it */
950 /* if invisible, ignore it */
951 if (w->a.x + w->a.width < 1 || w->a.y + w->a.height < 1
952 || w->a.x >= root_width || w->a.y >= root_height)
956 XRenderPictureAttributes pa;
957 XRenderPictFormat *format;
958 Drawable draw = w->id;
960 #if HAS_NAME_WINDOW_PIXMAP
961 if (hasNamePixmap && !w->pixmap)
962 w->pixmap = XCompositeNameWindowPixmap (dpy, w->id);
966 format = XRenderFindVisualFormat (dpy, w->a.visual);
967 pa.subwindow_mode = IncludeInferiors;
968 w->picture = XRenderCreatePicture (dpy, draw,
974 printf (" 0x%x", w->id);
980 set_ignore (dpy, NextRequest (dpy));
981 XFixesDestroyRegion (dpy, w->borderSize);
982 w->borderSize = None;
986 XFixesDestroyRegion (dpy, w->extents);
991 XFixesDestroyRegion (dpy, w->borderClip);
992 w->borderClip = None;
996 w->borderSize = border_size (dpy, w);
998 w->extents = win_extents (dpy, w);
999 if (w->mode == WINDOW_SOLID)
1002 #if HAS_NAME_WINDOW_PIXMAP
1005 wid = w->a.width + w->a.border_width * 2;
1006 hei = w->a.height + w->a.border_width * 2;
1008 x = w->a.x + w->a.border_width;
1009 y = w->a.y + w->a.border_width;
1013 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
1014 set_ignore (dpy, NextRequest (dpy));
1015 XFixesSubtractRegion (dpy, region, region, w->borderSize);
1016 set_ignore (dpy, NextRequest (dpy));
1017 XRenderComposite (dpy, PictOpSrc, w->picture, None, rootBuffer,
1023 w->borderClip = XFixesCreateRegion (dpy, 0, 0);
1024 XFixesCopyRegion (dpy, w->borderClip, region);
1033 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
1035 for (w = t; w; w = w->prev_trans)
1037 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, w->borderClip);
1041 case CompServerShadows:
1042 /* dont' bother drawing shadows on desktop windows */
1043 if (w->windowType == winDesktopAtom)
1045 set_ignore (dpy, NextRequest (dpy));
1046 if (w->opacity != OPAQUE && !w->shadowPict)
1047 w->shadowPict = solid_picture (dpy, True,
1048 (double) w->opacity / OPAQUE * 0.3,
1050 XRenderComposite (dpy, PictOpOver,
1051 w->shadowPict ? w->shadowPict : transBlackPicture,
1052 w->picture, rootBuffer,
1054 w->a.x + w->shadow_dx,
1055 w->a.y + w->shadow_dy,
1056 w->shadow_width, w->shadow_height);
1058 case CompClientShadows:
1059 /* don't bother drawing shadows on desktop windows */
1060 if (w->shadow && w->windowType != winDesktopAtom)
1062 XRenderComposite (dpy, PictOpOver, blackPicture, w->shadow, rootBuffer,
1064 w->a.x + w->shadow_dx,
1065 w->a.y + w->shadow_dy,
1066 w->shadow_width, w->shadow_height);
1070 if (w->opacity != OPAQUE && !w->alphaPict)
1071 w->alphaPict = solid_picture (dpy, False,
1072 (double) w->opacity / OPAQUE, 0, 0, 0);
1073 if (w->mode == WINDOW_TRANS)
1076 #if HAS_NAME_WINDOW_PIXMAP
1079 wid = w->a.width + w->a.border_width * 2;
1080 hei = w->a.height + w->a.border_width * 2;
1082 x = w->a.x + w->a.border_width;
1083 y = w->a.y + w->a.border_width;
1087 set_ignore (dpy, NextRequest (dpy));
1088 XRenderComposite (dpy, PictOpOver, w->picture, w->alphaPict, rootBuffer,
1092 else if (w->mode == WINDOW_ARGB)
1095 #if HAS_NAME_WINDOW_PIXMAP
1098 wid = w->a.width + w->a.border_width * 2;
1099 hei = w->a.height + w->a.border_width * 2;
1101 x = w->a.x + w->a.border_width;
1102 y = w->a.y + w->a.border_width;
1106 set_ignore (dpy, NextRequest (dpy));
1107 XRenderComposite (dpy, PictOpOver, w->picture, w->alphaPict, rootBuffer,
1111 XFixesDestroyRegion (dpy, w->borderClip);
1112 w->borderClip = None;
1114 XFixesDestroyRegion (dpy, region);
1115 if (rootBuffer != rootPicture)
1117 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, None);
1118 XRenderComposite (dpy, PictOpSrc, rootBuffer, None, rootPicture,
1119 0, 0, 0, 0, 0, 0, root_width, root_height);
1124 add_damage (Display *dpy, XserverRegion damage)
1128 XFixesUnionRegion (dpy, allDamage, allDamage, damage);
1129 XFixesDestroyRegion (dpy, damage);
1136 repair_win (Display *dpy, win *w)
1138 XserverRegion parts;
1142 parts = win_extents (dpy, w);
1143 set_ignore (dpy, NextRequest (dpy));
1144 XDamageSubtract (dpy, w->damage, None, None);
1149 parts = XFixesCreateRegion (dpy, 0, 0);
1150 set_ignore (dpy, NextRequest (dpy));
1151 XDamageSubtract (dpy, w->damage, None, parts);
1152 XFixesTranslateRegion (dpy, parts,
1153 w->a.x + w->a.border_width,
1154 w->a.y + w->a.border_width);
1155 if (compMode == CompServerShadows)
1157 o = XFixesCreateRegion (dpy, 0, 0);
1158 XFixesCopyRegion (dpy, o, parts);
1159 XFixesTranslateRegion (dpy, o, w->shadow_dx, w->shadow_dy);
1160 XFixesUnionRegion (dpy, parts, parts, o);
1161 XFixesDestroyRegion (dpy, o);
1164 add_damage (dpy, parts);
1169 get_opacity_prop (Display *dpy, win *w, unsigned int def);
1172 map_win (Display *dpy, Window id, unsigned long sequence, Bool fade)
1174 win *w = find_win (dpy, id);
1180 w->a.map_state = IsViewable;
1182 /* This needs to be here or else we lose transparency messages */
1183 XSelectInput (dpy, id, PropertyChangeMask);
1185 /* This needs to be here since we don't get PropertyNotify when unmapped */
1186 w->opacity = get_opacity_prop (dpy, w, OPAQUE);
1187 determine_mode (dpy, w);
1190 w->damage_bounds.x = w->damage_bounds.y = 0;
1191 w->damage_bounds.width = w->damage_bounds.height = 0;
1195 if (fade && fadeWindows)
1196 set_fade (dpy, w, 0, get_opacity_percent (dpy, w, 1.0), fade_in_step, 0, False, True, True);
1200 finish_unmap_win (Display *dpy, win *w)
1206 if (w->extents != None)
1208 add_damage (dpy, w->extents); /* destroys region */
1212 #if HAS_NAME_WINDOW_PIXMAP
1215 XFreePixmap (dpy, w->pixmap);
1222 set_ignore (dpy, NextRequest (dpy));
1223 XRenderFreePicture (dpy, w->picture);
1227 /* don't care about properties anymore */
1228 set_ignore (dpy, NextRequest (dpy));
1229 XSelectInput(dpy, w->id, 0);
1233 set_ignore (dpy, NextRequest (dpy));
1234 XFixesDestroyRegion (dpy, w->borderSize);
1235 w->borderSize = None;
1239 XRenderFreePicture (dpy, w->shadow);
1244 XFixesDestroyRegion (dpy, w->borderClip);
1245 w->borderClip = None;
1251 #if HAS_NAME_WINDOW_PIXMAP
1253 unmap_callback (Display *dpy, win *w, Bool gone)
1255 finish_unmap_win (dpy, w);
1260 unmap_win (Display *dpy, Window id, Bool fade)
1262 win *w = find_win (dpy, id);
1265 w->a.map_state = IsUnmapped;
1266 #if HAS_NAME_WINDOW_PIXMAP
1267 if (w->pixmap && fade && fadeWindows)
1268 set_fade (dpy, w, w->opacity*1.0/OPAQUE, 0.0, fade_out_step, unmap_callback, False, False, True);
1271 finish_unmap_win (dpy, w);
1274 /* Get the opacity prop from window
1279 get_opacity_prop(Display *dpy, win *w, unsigned int def)
1283 unsigned long n, left;
1285 unsigned char *data;
1286 int result = XGetWindowProperty(dpy, w->id, opacityAtom, 0L, 1L, False,
1287 XA_CARDINAL, &actual, &format,
1289 if (result == Success && data != NULL)
1292 memcpy (&i, data, sizeof (unsigned int));
1293 XFree( (void *) data);
1299 /* Get the opacity property from the window in a percent format
1301 otherwise: the value
1304 get_opacity_percent(Display *dpy, win *w, double def)
1306 unsigned int opacity = get_opacity_prop (dpy, w, (unsigned int)(OPAQUE*def));
1308 return opacity*1.0/OPAQUE;
1311 /* determine mode for window all in one place.
1312 Future might check for menu flag and other cool things
1316 get_wintype_prop(Display * dpy, Window w)
1320 unsigned long n, left;
1322 unsigned char *data;
1323 int result = XGetWindowProperty (dpy, w, winTypeAtom, 0L, 1L, False,
1324 XA_ATOM, &actual, &format,
1327 if (result == Success && data != None)
1330 memcpy (&a, data, sizeof (Atom));
1331 XFree ( (void *) data);
1334 return winNormalAtom;
1338 determine_mode(Display *dpy, win *w)
1341 XRenderPictFormat *format;
1342 unsigned int default_opacity;
1344 /* if trans prop == -1 fall back on previous tests*/
1348 XRenderFreePicture (dpy, w->alphaPict);
1349 w->alphaPict = None;
1353 XRenderFreePicture (dpy, w->shadowPict);
1354 w->shadowPict = None;
1357 if (w->a.class == InputOnly)
1363 format = XRenderFindVisualFormat (dpy, w->a.visual);
1366 if (format && format->type == PictTypeDirect && format->direct.alphaMask)
1370 else if (w->opacity != OPAQUE)
1372 mode = WINDOW_TRANS;
1376 mode = WINDOW_SOLID;
1381 XserverRegion damage;
1382 damage = XFixesCreateRegion (dpy, 0, 0);
1383 XFixesCopyRegion (dpy, damage, w->extents);
1384 add_damage (dpy, damage);
1389 determine_wintype (Display *dpy, Window w)
1391 Window root_return, parent_return;
1392 Window *children = NULL;
1393 unsigned int nchildren, i;
1396 type = get_wintype_prop (dpy, w);
1397 if (type != winNormalAtom)
1400 if (!XQueryTree (dpy, w, &root_return, &parent_return, &children,
1403 /* XQueryTree failed. */
1405 XFree ((void *)children);
1406 return winNormalAtom;
1409 for (i = 0;i < nchildren;i++)
1411 type = determine_wintype (dpy, children[i]);
1412 if (type != winNormalAtom)
1417 XFree ((void *)children);
1419 return winNormalAtom;
1423 add_win (Display *dpy, Window id, Window prev)
1425 win *new = malloc (sizeof (win));
1432 for (p = &list; *p; p = &(*p)->next)
1433 if ((*p)->id == prev)
1439 set_ignore (dpy, NextRequest (dpy));
1440 if (!XGetWindowAttributes (dpy, id, &new->a))
1447 new->usable = False;
1449 #if HAS_NAME_WINDOW_PIXMAP
1452 new->picture = None;
1453 if (new->a.class == InputOnly)
1455 new->damage_sequence = 0;
1460 new->damage_sequence = NextRequest (dpy);
1461 new->damage = XDamageCreate (dpy, id, XDamageReportNonEmpty);
1463 new->alphaPict = None;
1464 new->shadowPict = None;
1465 new->borderSize = None;
1466 new->extents = None;
1470 new->shadow_width = 0;
1471 new->shadow_height = 0;
1472 new->opacity = OPAQUE;
1474 new->borderClip = None;
1475 new->prev_trans = 0;
1477 new->windowType = determine_wintype (dpy, new->id);
1481 if (new->a.map_state == IsViewable)
1482 map_win (dpy, id, new->damage_sequence - 1, True);
1486 restack_win (Display *dpy, win *w, Window new_above)
1491 old_above = w->next->id;
1494 if (old_above != new_above)
1499 for (prev = &list; *prev; prev = &(*prev)->next)
1505 for (prev = &list; *prev; prev = &(*prev)->next)
1507 if ((*prev)->id == new_above)
1516 configure_win (Display *dpy, XConfigureEvent *ce)
1518 win *w = find_win (dpy, ce->window);
1520 XserverRegion damage = None;
1524 if (ce->window == root)
1528 XRenderFreePicture (dpy, rootBuffer);
1531 root_width = ce->width;
1532 root_height = ce->height;
1540 damage = XFixesCreateRegion (dpy, 0, 0);
1541 if (w->extents != None)
1542 XFixesCopyRegion (dpy, damage, w->extents);
1546 if (w->a.width != ce->width || w->a.height != ce->height)
1548 #if HAS_NAME_WINDOW_PIXMAP
1551 XFreePixmap (dpy, w->pixmap);
1555 XRenderFreePicture (dpy, w->picture);
1562 XRenderFreePicture (dpy, w->shadow);
1566 w->a.width = ce->width;
1567 w->a.height = ce->height;
1568 w->a.border_width = ce->border_width;
1569 w->a.override_redirect = ce->override_redirect;
1570 restack_win (dpy, w, ce->above);
1573 XserverRegion extents = win_extents (dpy, w);
1574 XFixesUnionRegion (dpy, damage, damage, extents);
1575 XFixesDestroyRegion (dpy, extents);
1576 add_damage (dpy, damage);
1582 circulate_win (Display *dpy, XCirculateEvent *ce)
1584 win *w = find_win (dpy, ce->window);
1590 if (ce->place == PlaceOnTop)
1591 new_above = list->id;
1594 restack_win (dpy, w, new_above);
1599 finish_destroy_win (Display *dpy, Window id, Bool gone)
1603 for (prev = &list; (w = *prev); prev = &w->next)
1607 finish_unmap_win (dpy, w);
1611 set_ignore (dpy, NextRequest (dpy));
1612 XRenderFreePicture (dpy, w->picture);
1617 XRenderFreePicture (dpy, w->alphaPict);
1618 w->alphaPict = None;
1622 XRenderFreePicture (dpy, w->shadowPict);
1623 w->shadowPict = None;
1625 if (w->damage != None)
1627 set_ignore (dpy, NextRequest (dpy));
1628 XDamageDestroy (dpy, w->damage);
1631 cleanup_fade (dpy, w);
1637 #if HAS_NAME_WINDOW_PIXMAP
1639 destroy_callback (Display *dpy, win *w, Bool gone)
1641 finish_destroy_win (dpy, w->id, gone);
1646 destroy_win (Display *dpy, Window id, Bool gone, Bool fade)
1648 win *w = find_win (dpy, id);
1649 #if HAS_NAME_WINDOW_PIXMAP
1650 if (w && w->pixmap && fade && fadeWindows)
1651 set_fade (dpy, w, w->opacity*1.0/OPAQUE, 0.0, fade_out_step, destroy_callback, gone, False, True);
1655 finish_destroy_win (dpy, id, gone);
1663 printf ("\t%08lx: %d x %d + %d + %d (%d)\n", w->id,
1664 w->a.width, w->a.height, w->a.x, w->a.y, w->a.border_width);
1673 printf ("windows:\n");
1674 for (w = list; w; w = w->next)
1680 damage_win (Display *dpy, XDamageNotifyEvent *de)
1682 win *w = find_win (dpy, de->drawable);
1689 if (w->damage_bounds.width == 0 || w->damage_bounds.height == 0)
1691 w->damage_bounds = de->area;
1695 if (de->area.x < w->damage_bounds.x)
1697 w->damage_bounds.width += (w->damage_bounds.x - de->area.x);
1698 w->damage_bounds.x = de->area.x;
1700 if (de->area.y < w->damage_bounds.y)
1702 w->damage_bounds.height += (w->damage_bounds.y - de->area.y);
1703 w->damage_bounds.y = de->area.y;
1705 if (de->area.x + de->area.width > w->damage_bounds.x + w->damage_bounds.width)
1706 w->damage_bounds.width = de->area.x + de->area.width - w->damage_bounds.x;
1707 if (de->area.y + de->area.height > w->damage_bounds.y + w->damage_bounds.height)
1708 w->damage_bounds.height = de->area.y + de->area.height - w->damage_bounds.y;
1711 printf ("unusable damage %d, %d: %d x %d bounds %d, %d: %d x %d\n",
1718 w->damage_bounds.width,
1719 w->damage_bounds.height);
1721 if (w->damage_bounds.x <= 0 &&
1722 w->damage_bounds.y <= 0 &&
1723 w->a.width <= w->damage_bounds.x + w->damage_bounds.width &&
1724 w->a.height <= w->damage_bounds.y + w->damage_bounds.height)
1728 set_fade (dpy, w, 0, get_opacity_percent (dpy, w, 1.0), fade_in_step, 0, False, True, True);
1734 repair_win (dpy, w);
1738 error (Display *dpy, XErrorEvent *ev)
1743 if (should_ignore (dpy, ev->serial))
1746 if (ev->request_code == composite_opcode &&
1747 ev->minor_code == X_CompositeRedirectSubwindows)
1749 fprintf (stderr, "Another composite manager is already running\n");
1753 o = ev->error_code - xfixes_error;
1755 case BadRegion: name = "BadRegion"; break;
1758 o = ev->error_code - damage_error;
1760 case BadDamage: name = "BadDamage"; break;
1763 o = ev->error_code - render_error;
1765 case BadPictFormat: name ="BadPictFormat"; break;
1766 case BadPicture: name ="BadPicture"; break;
1767 case BadPictOp: name ="BadPictOp"; break;
1768 case BadGlyphSet: name ="BadGlyphSet"; break;
1769 case BadGlyph: name ="BadGlyph"; break;
1773 printf ("error %d request %d minor %d serial %d\n",
1774 ev->error_code, ev->request_code, ev->minor_code, ev->serial);
1776 /* abort (); this is just annoying to most people */
1781 expose_root (Display *dpy, Window root, XRectangle *rects, int nrects)
1783 XserverRegion region = XFixesCreateRegion (dpy, rects, nrects);
1785 add_damage (dpy, region);
1790 ev_serial (XEvent *ev)
1792 if (ev->type & 0x7f != KeymapNotify)
1793 return ev->xany.serial;
1794 return NextRequest (ev->xany.display);
1799 ev_name (XEvent *ev)
1801 static char buf[128];
1802 switch (ev->type & 0x7f) {
1809 case ReparentNotify:
1811 case CirculateNotify:
1814 if (ev->type == damage_event + XDamageNotify)
1816 sprintf (buf, "Event %d", ev->type);
1822 ev_window (XEvent *ev)
1826 return ev->xexpose.window;
1828 return ev->xmap.window;
1830 return ev->xunmap.window;
1831 case ReparentNotify:
1832 return ev->xreparent.window;
1833 case CirculateNotify:
1834 return ev->xcirculate.window;
1836 if (ev->type == damage_event + XDamageNotify)
1837 return ((XDamageNotifyEvent *) ev)->drawable;
1843 usage (char *program)
1845 fprintf (stderr, "%s v1.1.3\n", program);
1846 fprintf (stderr, "usage: %s [options]\n", program);
1847 fprintf (stderr, "Options\n");
1848 fprintf (stderr, " -d display\n Specifies which display should be managed.\n");
1849 fprintf (stderr, " -r radius\n Specifies the blur radius for client-side shadows. (default 12)\n");
1850 fprintf (stderr, " -o opacity\n Specifies the translucency for client-side shadows. (default .75)\n");
1851 fprintf (stderr, " -l left-offset\n Specifies the left offset for client-side shadows. (default -15)\n");
1852 fprintf (stderr, " -t top-offset\n Specifies the top offset for clinet-side shadows. (default -15)\n");
1853 fprintf (stderr, " -I fade-in-step\n Specifies the opacity change between steps while fading in. (default 0.028)\n");
1854 fprintf (stderr, " -O fade-out-step\n Specifies the opacity change between steps while fading out. (default 0.03)\n");
1855 fprintf (stderr, " -D fade-delta-time\n Specifies the time between steps in a fade in milliseconds. (default 10)\n");
1856 fprintf (stderr, " -a\n Use automatic server-side compositing. Faster, but no special effects.\n");
1857 fprintf (stderr, " -c\n Draw client-side shadows with fuzzy edges.\n");
1858 fprintf (stderr, " -C\n Avoid drawing shadows on dock/panel windows.\n");
1859 fprintf (stderr, " -f\n Fade windows in/out when opening/closing.\n");
1860 fprintf (stderr, " -F\n Fade windows during opacity changes.\n");
1861 fprintf (stderr, " -n\n Normal client-side compositing with transparency support\n");
1862 fprintf (stderr, " -s\n Draw server-side shadows with sharp edges.\n");
1863 fprintf (stderr, " -S\n Enable synchronous operation (for debugging).\n");
1873 w = XCreateSimpleWindow (dpy, RootWindow (dpy, 0), 0, 0, 1, 1, 0, None,
1876 Xutf8SetWMProperties (dpy, w, "xcompmgr", "xcompmgr", NULL, 0, NULL, NULL,
1879 /* FIXME: Don't hard code the screen number */
1880 a = XInternAtom (dpy, "_NET_WM_CM_S0", False);
1882 XSetSelectionOwner (dpy, a, w, 0);
1886 main (int argc, char **argv)
1889 Window root_return, parent_return;
1893 unsigned int nchildren;
1895 XRenderPictureAttributes pa;
1897 XRectangle *expose_rects = 0;
1898 int size_expose = 0;
1905 int composite_major, composite_minor;
1909 while ((o = getopt (argc, argv, "D:I:O:d:r:o:l:t:scnfFCaS")) != -1)
1916 fade_delta = atoi (optarg);
1921 fade_in_step = atof (optarg);
1922 if (fade_in_step <= 0)
1923 fade_in_step = 0.01;
1926 fade_out_step = atof (optarg);
1927 if (fade_out_step <= 0)
1928 fade_out_step = 0.01;
1931 compMode = CompServerShadows;
1934 compMode = CompClientShadows;
1937 excludeDockShadows = True;
1940 compMode = CompSimple;
1949 autoRedirect = True;
1955 shadowRadius = atoi (optarg);
1958 shadowOpacity = atof (optarg);
1961 shadowOffsetX = atoi (optarg);
1964 shadowOffsetY = atoi (optarg);
1972 dpy = XOpenDisplay (display);
1975 fprintf (stderr, "Can't open display\n");
1978 XSetErrorHandler (error);
1980 XSynchronize (dpy, 1);
1981 scr = DefaultScreen (dpy);
1982 root = RootWindow (dpy, scr);
1984 if (!XRenderQueryExtension (dpy, &render_event, &render_error))
1986 fprintf (stderr, "No render extension\n");
1989 if (!XQueryExtension (dpy, COMPOSITE_NAME, &composite_opcode,
1990 &composite_event, &composite_error))
1992 fprintf (stderr, "No composite extension\n");
1995 XCompositeQueryVersion (dpy, &composite_major, &composite_minor);
1996 #if HAS_NAME_WINDOW_PIXMAP
1997 if (composite_major > 0 || composite_minor >= 2)
1998 hasNamePixmap = True;
2001 if (!XDamageQueryExtension (dpy, &damage_event, &damage_error))
2003 fprintf (stderr, "No damage extension\n");
2006 if (!XFixesQueryExtension (dpy, &xfixes_event, &xfixes_error))
2008 fprintf (stderr, "No XFixes extension\n");
2015 opacityAtom = XInternAtom (dpy, OPACITY_PROP, False);
2016 winTypeAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE", False);
2017 winDesktopAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DESKTOP", False);
2018 winDockAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DOCK", False);
2019 winToolbarAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_TOOLBAR", False);
2020 winMenuAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_MENU", False);
2021 winUtilAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_UTILITY", False);
2022 winSplashAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_SPLASH", False);
2023 winDialogAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
2024 winNormalAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_NORMAL", False);
2026 pa.subwindow_mode = IncludeInferiors;
2028 if (compMode == CompClientShadows)
2030 gaussianMap = make_gaussian_map(dpy, shadowRadius);
2031 presum_gaussian (gaussianMap);
2034 root_width = DisplayWidth (dpy, scr);
2035 root_height = DisplayHeight (dpy, scr);
2037 rootPicture = XRenderCreatePicture (dpy, root,
2038 XRenderFindVisualFormat (dpy,
2039 DefaultVisual (dpy, scr)),
2042 blackPicture = solid_picture (dpy, True, 1, 0, 0, 0);
2043 if (compMode == CompServerShadows)
2044 transBlackPicture = solid_picture (dpy, True, 0.3, 0, 0, 0);
2049 XCompositeRedirectSubwindows (dpy, root, CompositeRedirectAutomatic);
2052 XCompositeRedirectSubwindows (dpy, root, CompositeRedirectManual);
2053 XSelectInput (dpy, root,
2054 SubstructureNotifyMask|
2056 StructureNotifyMask|
2057 PropertyChangeMask);
2058 XQueryTree (dpy, root, &root_return, &parent_return, &children, &nchildren);
2059 for (i = 0; i < nchildren; i++)
2060 add_win (dpy, children[i], i ? children[i-1] : None);
2063 XUngrabServer (dpy);
2064 ufd.fd = ConnectionNumber (dpy);
2065 ufd.events = POLLIN;
2067 paint_all (dpy, None);
2076 if (poll (&ufd, 1, fade_timeout()) == 0)
2083 XNextEvent (dpy, &ev);
2084 if (ev.type & 0x7f != KeymapNotify)
2085 discard_ignore (dpy, ev.xany.serial);
2087 printf ("event %10.10s serial 0x%08x window 0x%08x\n",
2088 ev_name(&ev), ev_serial (&ev), ev_window (&ev));
2090 if (!autoRedirect) switch (ev.type) {
2092 add_win (dpy, ev.xcreatewindow.window, 0);
2094 case ConfigureNotify:
2095 configure_win (dpy, &ev.xconfigure);
2098 destroy_win (dpy, ev.xdestroywindow.window, True, True);
2101 map_win (dpy, ev.xmap.window, ev.xmap.serial, True);
2104 unmap_win (dpy, ev.xunmap.window, True);
2106 case ReparentNotify:
2107 if (ev.xreparent.parent == root)
2108 add_win (dpy, ev.xreparent.window, 0);
2110 destroy_win (dpy, ev.xreparent.window, False, True);
2112 case CirculateNotify:
2113 circulate_win (dpy, &ev.xcirculate);
2116 if (ev.xexpose.window == root)
2118 int more = ev.xexpose.count + 1;
2119 if (n_expose == size_expose)
2123 expose_rects = realloc (expose_rects,
2124 (size_expose + more) *
2125 sizeof (XRectangle));
2126 size_expose += more;
2130 expose_rects = malloc (more * sizeof (XRectangle));
2134 expose_rects[n_expose].x = ev.xexpose.x;
2135 expose_rects[n_expose].y = ev.xexpose.y;
2136 expose_rects[n_expose].width = ev.xexpose.width;
2137 expose_rects[n_expose].height = ev.xexpose.height;
2139 if (ev.xexpose.count == 0)
2141 expose_root (dpy, root, expose_rects, n_expose);
2146 case PropertyNotify:
2147 for (p = 0; backgroundProps[p]; p++)
2149 if (ev.xproperty.atom == XInternAtom (dpy, backgroundProps[p], False))
2153 XClearArea (dpy, root, 0, 0, 0, 0, True);
2154 XRenderFreePicture (dpy, rootTile);
2160 /* check if Trans property was changed */
2161 if (ev.xproperty.atom == opacityAtom)
2163 /* reset mode and redraw window */
2164 win * w = find_win(dpy, ev.xproperty.window);
2168 set_fade (dpy, w, w->opacity*1.0/OPAQUE, get_opacity_percent (dpy, w, 1.0),
2169 fade_out_step, 0, False, True, False);
2172 w->opacity = get_opacity_prop(dpy, w, OPAQUE);
2173 determine_mode(dpy, w);
2176 XRenderFreePicture (dpy, w->shadow);
2178 w->extents = win_extents (dpy, w);
2185 if (ev.type == damage_event + XDamageNotify)
2186 damage_win (dpy, (XDamageNotifyEvent *) &ev);
2189 } while (QLength (dpy));
2190 if (allDamage && !autoRedirect)
2193 paint_all (dpy, allDamage);
2197 clipChanged = False;