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
41 #include <X11/Xutil.h>
42 #include <X11/Xatom.h>
43 #include <X11/extensions/Xcomposite.h>
44 #include <X11/extensions/Xdamage.h>
45 #include <X11/extensions/Xrender.h>
47 #if COMPOSITE_MAJOR > 0 || COMPOSITE_MINOR >= 2
48 #define HAS_NAME_WINDOW_PIXMAP 1
51 #define CAN_DO_USABLE 0
53 typedef struct _ignore {
55 unsigned long sequence;
61 #if HAS_NAME_WINDOW_PIXMAP
66 Bool usable; /* mapped and all damaged at one point */
67 XRectangle damage_bounds; /* bounds of damage */
75 XserverRegion borderSize;
76 XserverRegion extents;
84 unsigned long damage_sequence; /* sequence when damage was created */
86 /* for drawing translucent windows */
87 XserverRegion borderClip;
88 struct _win *prev_trans;
91 typedef struct _conv {
96 typedef struct _fade {
102 void (*callback) (Display *dpy, win *w, Bool gone);
114 Picture blackPicture;
115 Picture transBlackPicture;
117 XserverRegion allDamage;
119 #if HAS_NAME_WINDOW_PIXMAP
122 int root_height, root_width;
123 ignore *ignore_head, **ignore_tail = &ignore_head;
124 int xfixes_event, xfixes_error;
125 int damage_event, damage_error;
126 int composite_event, composite_error;
127 int render_event, render_error;
129 int composite_opcode;
131 /* find these once and be done with it */
143 /* opacity property name; sometime soon I'll write up an EWMH spec for it */
144 #define OPACITY_PROP "_NET_WM_WINDOW_OPACITY"
146 #define TRANSLUCENT 0xe0000000
147 #define OPAQUE 0xffffffff
151 #define WINDOW_SOLID 0
152 #define WINDOW_TRANS 1
153 #define WINDOW_ARGB 2
155 #define TRANS_OPACITY 0.75
157 #define DEBUG_REPAINT 0
158 #define DEBUG_EVENTS 0
159 #define MONITOR_REPAINT 0
162 #define SHARP_SHADOW 0
164 typedef enum _compMode {
165 CompSimple, /* looks like a regular X server */
166 CompServerShadows, /* use window alpha for shadow; sharp, but precise */
167 CompClientShadows, /* use window extents for shadow, blurred */
171 determine_mode(Display *dpy, win *w);
174 get_opacity_percent(Display *dpy, win *w, double def);
177 win_extents (Display *dpy, win *w);
179 CompMode compMode = CompSimple;
181 int shadowRadius = 12;
182 int shadowOffsetX = -15;
183 int shadowOffsetY = -15;
184 double shadowOpacity = .75;
186 double fade_in_step = 0.028;
187 double fade_out_step = 0.03;
190 Bool fadeWindows = False;
191 Bool excludeDockShadows = False;
192 Bool fadeTrans = False;
194 Bool autoRedirect = False;
196 /* For shadow precomputation */
198 unsigned char *shadowCorner = NULL;
199 unsigned char *shadowTop = NULL;
202 get_time_in_milliseconds ()
206 gettimeofday (&tv, NULL);
207 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
215 for (f = fades; f; f = f->next)
224 dequeue_fade (Display *dpy, fade *f)
228 for (prev = &fades; *prev; prev = &(*prev)->next)
233 (*f->callback) (dpy, f->w, f->gone);
240 cleanup_fade (Display *dpy, win *w)
242 fade *f = find_fade (w);
244 dequeue_fade (dpy, f);
248 enqueue_fade (Display *dpy, fade *f)
251 fade_time = get_time_in_milliseconds () + fade_delta;
257 set_fade (Display *dpy, win *w, double start, double finish, double step,
258 void (*callback) (Display *dpy, win *w, Bool gone),
259 Bool gone, Bool exec_callback, Bool override)
266 f = malloc (sizeof (fade));
270 enqueue_fade (dpy, f);
278 (*f->callback)(dpy, f->w, f->gone);
288 else if (f->cur > finish)
290 f->callback = callback;
292 w->opacity = f->cur * OPAQUE;
294 printf ("set_fade start %g step %g\n", f->cur, f->step);
296 determine_mode (dpy, w);
299 XRenderFreePicture (dpy, w->shadow);
301 w->extents = win_extents (dpy, w);
312 now = get_time_in_milliseconds();
313 delta = fade_time - now;
316 /* printf ("timeout %d\n", delta); */
321 run_fades (Display *dpy)
323 int now = get_time_in_milliseconds();
329 printf ("run fades\n");
331 if (fade_time - now > 0)
333 steps = 1 + (now - fade_time) / fade_delta;
340 f->cur += f->step * steps;
346 printf ("opacity now %g\n", f->cur);
348 w->opacity = f->cur * OPAQUE;
349 need_dequeue = False;
352 if (f->cur >= f->finish)
354 w->opacity = f->finish*OPAQUE;
360 if (f->cur <= f->finish)
362 w->opacity = f->finish*OPAQUE;
366 determine_mode (dpy, w);
369 XRenderFreePicture (dpy, w->shadow);
371 w->extents = win_extents(dpy, w);
373 /* Must do this last as it might destroy f->w in callbacks */
375 dequeue_fade (dpy, f);
377 fade_time = now + fade_delta;
381 gaussian (double r, double x, double y)
383 return ((1 / (sqrt (2 * M_PI * r))) *
384 exp ((- (x * x + y * y)) / (2 * r * r)));
389 make_gaussian_map (Display *dpy, double r)
392 int size = ((int) ceil ((r * 3)) + 1) & ~1;
393 int center = size / 2;
398 c = malloc (sizeof (conv) + size * size * sizeof (double));
400 c->data = (double *) (c + 1);
402 for (y = 0; y < size; y++)
403 for (x = 0; x < size; x++)
405 g = gaussian (r, (double) (x - center), (double) (y - center));
407 c->data[y * size + x] = g;
409 /* printf ("gaussian total %f\n", t); */
410 for (y = 0; y < size; y++)
411 for (x = 0; x < size; x++)
413 c->data[y*size + x] /= t;
419 * A picture will help
421 * -center 0 width width+center
422 * -center +-----+-------------------+-----+
425 * 0 +-----+-------------------+-----+
429 * height +-----+-------------------+-----+
432 * center +-----+-------------------+-----+
436 sum_gaussian (conv *map, double opacity, int x, int y, int width, int height)
440 double *g_line = map->data;
441 int g_size = map->size;
442 int center = g_size / 2;
443 int fx_start, fx_end;
444 int fy_start, fy_end;
448 * Compute set of filter values which are "in range",
449 * that's the set with:
450 * 0 <= x + (fx-center) && x + (fx-center) < width &&
451 * 0 <= y + (fy-center) && y + (fy-center) < height
453 * 0 <= x + (fx - center) x + fx - center < width
454 * center - x <= fx fx < width + center - x
457 fx_start = center - x;
460 fx_end = width + center - x;
464 fy_start = center - y;
467 fy_end = height + center - y;
471 g_line = g_line + fy_start * g_size + fx_start;
474 for (fy = fy_start; fy < fy_end; fy++)
479 for (fx = fx_start; fx < fx_end; fx++)
485 return ((unsigned char) (v * opacity * 255.0));
488 /* precompute shadow corners and sides to save time for large windows */
490 presum_gaussian (conv *map)
492 int center = map->size/2;
498 free ((void *)shadowCorner);
500 free ((void *)shadowTop);
502 shadowCorner = (unsigned char *)(malloc ((Gsize + 1) * (Gsize + 1) * 26));
503 shadowTop = (unsigned char *)(malloc ((Gsize + 1) * 26));
505 for (x = 0; x <= Gsize; x++)
507 shadowTop[25 * (Gsize + 1) + x] = sum_gaussian (map, 1, x - center, center, Gsize * 2, Gsize * 2);
508 for(opacity = 0; opacity < 25; opacity++)
509 shadowTop[opacity * (Gsize + 1) + x] = shadowTop[25 * (Gsize + 1) + x] * opacity / 25;
510 for(y = 0; y <= x; y++)
512 shadowCorner[25 * (Gsize + 1) * (Gsize + 1) + y * (Gsize + 1) + x]
513 = sum_gaussian (map, 1, x - center, y - center, Gsize * 2, Gsize * 2);
514 shadowCorner[25 * (Gsize + 1) * (Gsize + 1) + x * (Gsize + 1) + y]
515 = shadowCorner[25 * (Gsize + 1) * (Gsize + 1) + y * (Gsize + 1) + x];
516 for(opacity = 0; opacity < 25; opacity++)
517 shadowCorner[opacity * (Gsize + 1) * (Gsize + 1) + y * (Gsize + 1) + x]
518 = shadowCorner[opacity * (Gsize + 1) * (Gsize + 1) + x * (Gsize + 1) + y]
519 = shadowCorner[25 * (Gsize + 1) * (Gsize + 1) + y * (Gsize + 1) + x] * opacity / 25;
525 make_shadow (Display *dpy, double opacity, int width, int height)
529 int gsize = gaussianMap->size;
531 int swidth = width + gsize;
532 int sheight = height + gsize;
533 int center = gsize / 2;
537 int opacity_int = (int)(opacity * 25);
538 data = malloc (swidth * sheight * sizeof (unsigned char));
541 ximage = XCreateImage (dpy,
542 DefaultVisual(dpy, DefaultScreen(dpy)),
547 swidth, sheight, 8, swidth * sizeof (unsigned char));
554 * Build the gaussian in sections
558 * center (fill the complete data array)
561 d = shadowTop[opacity_int * (Gsize + 1) + Gsize];
563 d = sum_gaussian (gaussianMap, opacity, center, center, width, height);
564 memset(data, d, sheight * swidth);
570 if (ylimit > sheight / 2)
571 ylimit = (sheight + 1) / 2;
573 if (xlimit > swidth / 2)
574 xlimit = (swidth + 1) / 2;
576 for (y = 0; y < ylimit; y++)
577 for (x = 0; x < xlimit; x++)
579 if (xlimit == Gsize && ylimit == Gsize)
580 d = shadowCorner[opacity_int * (Gsize + 1) * (Gsize + 1) + y * (Gsize + 1) + x];
582 d = sum_gaussian (gaussianMap, opacity, x - center, y - center, width, height);
583 data[y * swidth + x] = d;
584 data[(sheight - y - 1) * swidth + x] = d;
585 data[(sheight - y - 1) * swidth + (swidth - x - 1)] = d;
586 data[y * swidth + (swidth - x - 1)] = d;
592 x_diff = swidth - (gsize * 2);
593 if (x_diff > 0 && ylimit > 0)
595 for (y = 0; y < ylimit; y++)
598 d = shadowTop[opacity_int * (Gsize + 1) + y];
600 d = sum_gaussian (gaussianMap, opacity, center, y - center, width, height);
601 memset (&data[y * swidth + gsize], d, x_diff);
602 memset (&data[(sheight - y - 1) * swidth + gsize], d, x_diff);
610 for (x = 0; x < xlimit; x++)
613 d = shadowTop[opacity_int * (Gsize + 1) + x];
615 d = sum_gaussian (gaussianMap, opacity, x - center, center, width, height);
616 for (y = gsize; y < sheight - gsize; y++)
618 data[y * swidth + x] = d;
619 data[y * swidth + (swidth - x - 1)] = d;
627 shadow_picture (Display *dpy, double opacity, Picture alpha_pict, int width, int height, int *wp, int *hp)
631 Picture shadowPicture;
634 shadowImage = make_shadow (dpy, opacity, width, height);
637 shadowPixmap = XCreatePixmap (dpy, root,
643 XDestroyImage (shadowImage);
647 shadowPicture = XRenderCreatePicture (dpy, shadowPixmap,
648 XRenderFindStandardFormat (dpy, PictStandardA8),
652 XDestroyImage (shadowImage);
653 XFreePixmap (dpy, shadowPixmap);
657 gc = XCreateGC (dpy, shadowPixmap, 0, 0);
660 XDestroyImage (shadowImage);
661 XFreePixmap (dpy, shadowPixmap);
662 XRenderFreePicture (dpy, shadowPicture);
666 XPutImage (dpy, shadowPixmap, gc, shadowImage, 0, 0, 0, 0,
668 shadowImage->height);
669 *wp = shadowImage->width;
670 *hp = shadowImage->height;
672 XDestroyImage (shadowImage);
673 XFreePixmap (dpy, shadowPixmap);
674 return shadowPicture;
678 solid_picture (Display *dpy, Bool argb, double a, double r, double g, double b)
682 XRenderPictureAttributes pa;
685 pixmap = XCreatePixmap (dpy, root, 1, 1, argb ? 32 : 8);
690 picture = XRenderCreatePicture (dpy, pixmap,
691 XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8),
696 XFreePixmap (dpy, pixmap);
700 c.alpha = a * 0xffff;
702 c.green = g * 0xffff;
704 XRenderFillRectangle (dpy, PictOpSrc, picture, &c, 0, 0, 1, 1);
705 XFreePixmap (dpy, pixmap);
710 discard_ignore (Display *dpy, unsigned long sequence)
714 if ((long) (sequence - ignore_head->sequence) > 0)
716 ignore *next = ignore_head->next;
720 ignore_tail = &ignore_head;
728 set_ignore (Display *dpy, unsigned long sequence)
730 ignore *i = malloc (sizeof (ignore));
733 i->sequence = sequence;
736 ignore_tail = &i->next;
740 should_ignore (Display *dpy, unsigned long sequence)
742 discard_ignore (dpy, sequence);
743 return ignore_head && ignore_head->sequence == sequence;
747 find_win (Display *dpy, Window id)
751 for (w = list; w; w = w->next)
757 static const char *backgroundProps[] = {
764 root_tile (Display *dpy)
770 unsigned long nitems;
771 unsigned long bytes_after;
774 XRenderPictureAttributes pa;
778 for (p = 0; backgroundProps[p]; p++)
780 if (XGetWindowProperty (dpy, root, XInternAtom (dpy, backgroundProps[p], False),
781 0, 4, False, AnyPropertyType,
782 &actual_type, &actual_format, &nitems, &bytes_after, &prop) == Success &&
783 actual_type == XInternAtom (dpy, "PIXMAP", False) && actual_format == 32 && nitems == 1)
785 memcpy (&pixmap, prop, 4);
793 pixmap = XCreatePixmap (dpy, root, 1, 1, DefaultDepth (dpy, scr));
797 picture = XRenderCreatePicture (dpy, pixmap,
798 XRenderFindVisualFormat (dpy,
799 DefaultVisual (dpy, scr)),
805 c.red = c.green = c.blue = 0x8080;
807 XRenderFillRectangle (dpy, PictOpSrc, picture, &c,
814 paint_root (Display *dpy)
817 rootTile = root_tile (dpy);
819 XRenderComposite (dpy, PictOpSrc,
820 rootTile, None, rootBuffer,
821 0, 0, 0, 0, 0, 0, root_width, root_height);
825 win_extents (Display *dpy, win *w)
831 r.width = w->a.width + w->a.border_width * 2;
832 r.height = w->a.height + w->a.border_width * 2;
833 if (compMode != CompSimple && !(w->windowType == winDockAtom && excludeDockShadows))
835 if (compMode == CompServerShadows || w->mode != WINDOW_ARGB)
839 if (compMode == CompServerShadows)
843 w->shadow_width = w->a.width;
844 w->shadow_height = w->a.height;
848 w->shadow_dx = shadowOffsetX;
849 w->shadow_dy = shadowOffsetY;
852 double opacity = shadowOpacity;
853 if (w->mode == WINDOW_TRANS)
854 opacity = opacity * ((double)w->opacity)/((double)OPAQUE);
855 w->shadow = shadow_picture (dpy, opacity, w->alphaPict,
856 w->a.width + w->a.border_width * 2,
857 w->a.height + w->a.border_width * 2,
858 &w->shadow_width, &w->shadow_height);
861 sr.x = w->a.x + w->shadow_dx;
862 sr.y = w->a.y + w->shadow_dy;
863 sr.width = w->shadow_width;
864 sr.height = w->shadow_height;
867 r.width = (r.x + r.width) - sr.x;
872 r.height = (r.y + r.height) - sr.y;
875 if (sr.x + sr.width > r.x + r.width)
876 r.width = sr.x + sr.width - r.x;
877 if (sr.y + sr.height > r.y + r.height)
878 r.height = sr.y + sr.height - r.y;
881 return XFixesCreateRegion (dpy, &r, 1);
885 border_size (Display *dpy, win *w)
887 XserverRegion border;
889 * if window doesn't exist anymore, this will generate an error
890 * as well as not generate a region. Perhaps a better XFixes
891 * architecture would be to have a request that copies instead
892 * of creates, that way you'd just end up with an empty region
893 * instead of an invalid XID.
895 set_ignore (dpy, NextRequest (dpy));
896 border = XFixesCreateRegionFromWindow (dpy, w->id, WindowRegionBounding);
898 set_ignore (dpy, NextRequest (dpy));
899 XFixesTranslateRegion (dpy, border,
900 w->a.x + w->a.border_width,
901 w->a.y + w->a.border_width);
906 paint_all (Display *dpy, XserverRegion region)
916 r.width = root_width;
917 r.height = root_height;
918 region = XFixesCreateRegion (dpy, &r, 1);
921 rootBuffer = rootPicture;
925 Pixmap rootPixmap = XCreatePixmap (dpy, root, root_width, root_height,
926 DefaultDepth (dpy, scr));
927 rootBuffer = XRenderCreatePicture (dpy, rootPixmap,
928 XRenderFindVisualFormat (dpy,
929 DefaultVisual (dpy, scr)),
931 XFreePixmap (dpy, rootPixmap);
934 XFixesSetPictureClipRegion (dpy, rootPicture, 0, 0, region);
936 XRenderComposite (dpy, PictOpSrc, blackPicture, None, rootPicture,
937 0, 0, 0, 0, 0, 0, root_width, root_height);
942 for (w = list; w; w = w->next)
948 /* never painted, ignore it */
951 /* if invisible, ignore it */
952 if (w->a.x + w->a.width < 1 || w->a.y + w->a.height < 1
953 || w->a.x >= root_width || w->a.y >= root_height)
957 XRenderPictureAttributes pa;
958 XRenderPictFormat *format;
959 Drawable draw = w->id;
961 #if HAS_NAME_WINDOW_PIXMAP
962 if (hasNamePixmap && !w->pixmap)
963 w->pixmap = XCompositeNameWindowPixmap (dpy, w->id);
967 format = XRenderFindVisualFormat (dpy, w->a.visual);
968 pa.subwindow_mode = IncludeInferiors;
969 w->picture = XRenderCreatePicture (dpy, draw,
975 printf (" 0x%x", w->id);
981 set_ignore (dpy, NextRequest (dpy));
982 XFixesDestroyRegion (dpy, w->borderSize);
983 w->borderSize = None;
987 XFixesDestroyRegion (dpy, w->extents);
992 XFixesDestroyRegion (dpy, w->borderClip);
993 w->borderClip = None;
997 w->borderSize = border_size (dpy, w);
999 w->extents = win_extents (dpy, w);
1000 if (w->mode == WINDOW_SOLID)
1003 #if HAS_NAME_WINDOW_PIXMAP
1006 wid = w->a.width + w->a.border_width * 2;
1007 hei = w->a.height + w->a.border_width * 2;
1009 x = w->a.x + w->a.border_width;
1010 y = w->a.y + w->a.border_width;
1014 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
1015 set_ignore (dpy, NextRequest (dpy));
1016 XFixesSubtractRegion (dpy, region, region, w->borderSize);
1017 set_ignore (dpy, NextRequest (dpy));
1018 XRenderComposite (dpy, PictOpSrc, w->picture, None, rootBuffer,
1024 w->borderClip = XFixesCreateRegion (dpy, 0, 0);
1025 XFixesCopyRegion (dpy, w->borderClip, region);
1034 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
1036 for (w = t; w; w = w->prev_trans)
1038 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, w->borderClip);
1042 case CompServerShadows:
1043 /* dont' bother drawing shadows on desktop windows */
1044 if (w->windowType == winDesktopAtom)
1046 set_ignore (dpy, NextRequest (dpy));
1047 if (w->opacity != OPAQUE && !w->shadowPict)
1048 w->shadowPict = solid_picture (dpy, True,
1049 (double) w->opacity / OPAQUE * 0.3,
1051 XRenderComposite (dpy, PictOpOver,
1052 w->shadowPict ? w->shadowPict : transBlackPicture,
1053 w->picture, rootBuffer,
1055 w->a.x + w->shadow_dx,
1056 w->a.y + w->shadow_dy,
1057 w->shadow_width, w->shadow_height);
1059 case CompClientShadows:
1060 /* don't bother drawing shadows on desktop windows */
1061 if (w->shadow && w->windowType != winDesktopAtom)
1063 XRenderComposite (dpy, PictOpOver, blackPicture, w->shadow, rootBuffer,
1065 w->a.x + w->shadow_dx,
1066 w->a.y + w->shadow_dy,
1067 w->shadow_width, w->shadow_height);
1071 if (w->opacity != OPAQUE && !w->alphaPict)
1072 w->alphaPict = solid_picture (dpy, False,
1073 (double) w->opacity / OPAQUE, 0, 0, 0);
1074 if (w->mode == WINDOW_TRANS)
1077 #if HAS_NAME_WINDOW_PIXMAP
1080 wid = w->a.width + w->a.border_width * 2;
1081 hei = w->a.height + w->a.border_width * 2;
1083 x = w->a.x + w->a.border_width;
1084 y = w->a.y + w->a.border_width;
1088 set_ignore (dpy, NextRequest (dpy));
1089 XRenderComposite (dpy, PictOpOver, w->picture, w->alphaPict, rootBuffer,
1093 else if (w->mode == WINDOW_ARGB)
1096 #if HAS_NAME_WINDOW_PIXMAP
1099 wid = w->a.width + w->a.border_width * 2;
1100 hei = w->a.height + w->a.border_width * 2;
1102 x = w->a.x + w->a.border_width;
1103 y = w->a.y + w->a.border_width;
1107 set_ignore (dpy, NextRequest (dpy));
1108 XRenderComposite (dpy, PictOpOver, w->picture, w->alphaPict, rootBuffer,
1112 XFixesDestroyRegion (dpy, w->borderClip);
1113 w->borderClip = None;
1115 XFixesDestroyRegion (dpy, region);
1116 if (rootBuffer != rootPicture)
1118 XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, None);
1119 XRenderComposite (dpy, PictOpSrc, rootBuffer, None, rootPicture,
1120 0, 0, 0, 0, 0, 0, root_width, root_height);
1125 add_damage (Display *dpy, XserverRegion damage)
1129 XFixesUnionRegion (dpy, allDamage, allDamage, damage);
1130 XFixesDestroyRegion (dpy, damage);
1137 repair_win (Display *dpy, win *w)
1139 XserverRegion parts;
1143 parts = win_extents (dpy, w);
1144 set_ignore (dpy, NextRequest (dpy));
1145 XDamageSubtract (dpy, w->damage, None, None);
1150 parts = XFixesCreateRegion (dpy, 0, 0);
1151 set_ignore (dpy, NextRequest (dpy));
1152 XDamageSubtract (dpy, w->damage, None, parts);
1153 XFixesTranslateRegion (dpy, parts,
1154 w->a.x + w->a.border_width,
1155 w->a.y + w->a.border_width);
1156 if (compMode == CompServerShadows)
1158 o = XFixesCreateRegion (dpy, 0, 0);
1159 XFixesCopyRegion (dpy, o, parts);
1160 XFixesTranslateRegion (dpy, o, w->shadow_dx, w->shadow_dy);
1161 XFixesUnionRegion (dpy, parts, parts, o);
1162 XFixesDestroyRegion (dpy, o);
1165 add_damage (dpy, parts);
1170 get_opacity_prop (Display *dpy, win *w, unsigned int def);
1173 map_win (Display *dpy, Window id, unsigned long sequence, Bool fade)
1175 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;
1343 /* if trans prop == -1 fall back on previous tests*/
1347 XRenderFreePicture (dpy, w->alphaPict);
1348 w->alphaPict = None;
1352 XRenderFreePicture (dpy, w->shadowPict);
1353 w->shadowPict = None;
1356 if (w->a.class == InputOnly)
1362 format = XRenderFindVisualFormat (dpy, w->a.visual);
1365 if (format && format->type == PictTypeDirect && format->direct.alphaMask)
1369 else if (w->opacity != OPAQUE)
1371 mode = WINDOW_TRANS;
1375 mode = WINDOW_SOLID;
1380 XserverRegion damage;
1381 damage = XFixesCreateRegion (dpy, 0, 0);
1382 XFixesCopyRegion (dpy, damage, w->extents);
1383 add_damage (dpy, damage);
1388 determine_wintype (Display *dpy, Window w)
1390 Window root_return, parent_return;
1391 Window *children = NULL;
1392 unsigned int nchildren, i;
1395 type = get_wintype_prop (dpy, w);
1396 if (type != winNormalAtom)
1399 if (!XQueryTree (dpy, w, &root_return, &parent_return, &children,
1402 /* XQueryTree failed. */
1404 XFree ((void *)children);
1405 return winNormalAtom;
1408 for (i = 0;i < nchildren;i++)
1410 type = determine_wintype (dpy, children[i]);
1411 if (type != winNormalAtom)
1416 XFree ((void *)children);
1418 return winNormalAtom;
1422 add_win (Display *dpy, Window id, Window prev)
1424 win *new = malloc (sizeof (win));
1431 for (p = &list; *p; p = &(*p)->next)
1432 if ((*p)->id == prev)
1438 set_ignore (dpy, NextRequest (dpy));
1439 if (!XGetWindowAttributes (dpy, id, &new->a))
1446 new->usable = False;
1448 #if HAS_NAME_WINDOW_PIXMAP
1451 new->picture = None;
1452 if (new->a.class == InputOnly)
1454 new->damage_sequence = 0;
1459 new->damage_sequence = NextRequest (dpy);
1460 new->damage = XDamageCreate (dpy, id, XDamageReportNonEmpty);
1462 new->alphaPict = None;
1463 new->shadowPict = None;
1464 new->borderSize = None;
1465 new->extents = None;
1469 new->shadow_width = 0;
1470 new->shadow_height = 0;
1471 new->opacity = OPAQUE;
1473 new->borderClip = None;
1474 new->prev_trans = 0;
1476 new->windowType = determine_wintype (dpy, new->id);
1480 if (new->a.map_state == IsViewable)
1481 map_win (dpy, id, new->damage_sequence - 1, True);
1485 restack_win (Display *dpy, win *w, Window new_above)
1490 old_above = w->next->id;
1493 if (old_above != new_above)
1498 for (prev = &list; *prev; prev = &(*prev)->next)
1504 for (prev = &list; *prev; prev = &(*prev)->next)
1506 if ((*prev)->id == new_above)
1515 configure_win (Display *dpy, XConfigureEvent *ce)
1517 win *w = find_win (dpy, ce->window);
1518 XserverRegion damage = None;
1522 if (ce->window == root)
1526 XRenderFreePicture (dpy, rootBuffer);
1529 root_width = ce->width;
1530 root_height = ce->height;
1538 damage = XFixesCreateRegion (dpy, 0, 0);
1539 if (w->extents != None)
1540 XFixesCopyRegion (dpy, damage, w->extents);
1544 if (w->a.width != ce->width || w->a.height != ce->height)
1546 #if HAS_NAME_WINDOW_PIXMAP
1549 XFreePixmap (dpy, w->pixmap);
1553 XRenderFreePicture (dpy, w->picture);
1560 XRenderFreePicture (dpy, w->shadow);
1564 w->a.width = ce->width;
1565 w->a.height = ce->height;
1566 w->a.border_width = ce->border_width;
1567 w->a.override_redirect = ce->override_redirect;
1568 restack_win (dpy, w, ce->above);
1571 XserverRegion extents = win_extents (dpy, w);
1572 XFixesUnionRegion (dpy, damage, damage, extents);
1573 XFixesDestroyRegion (dpy, extents);
1574 add_damage (dpy, damage);
1580 circulate_win (Display *dpy, XCirculateEvent *ce)
1582 win *w = find_win (dpy, ce->window);
1588 if (ce->place == PlaceOnTop)
1589 new_above = list->id;
1592 restack_win (dpy, w, new_above);
1597 finish_destroy_win (Display *dpy, Window id, Bool gone)
1601 for (prev = &list; (w = *prev); prev = &w->next)
1605 finish_unmap_win (dpy, w);
1609 set_ignore (dpy, NextRequest (dpy));
1610 XRenderFreePicture (dpy, w->picture);
1615 XRenderFreePicture (dpy, w->alphaPict);
1616 w->alphaPict = None;
1620 XRenderFreePicture (dpy, w->shadowPict);
1621 w->shadowPict = None;
1625 XRenderFreePicture (dpy, w->shadow);
1628 if (w->damage != None)
1630 set_ignore (dpy, NextRequest (dpy));
1631 XDamageDestroy (dpy, w->damage);
1634 cleanup_fade (dpy, w);
1640 #if HAS_NAME_WINDOW_PIXMAP
1642 destroy_callback (Display *dpy, win *w, Bool gone)
1644 finish_destroy_win (dpy, w->id, gone);
1649 destroy_win (Display *dpy, Window id, Bool gone, Bool fade)
1651 win *w = find_win (dpy, id);
1652 #if HAS_NAME_WINDOW_PIXMAP
1653 if (w && w->pixmap && fade && fadeWindows)
1654 set_fade (dpy, w, w->opacity*1.0/OPAQUE, 0.0, fade_out_step, destroy_callback, gone, False, True);
1658 finish_destroy_win (dpy, id, gone);
1666 printf ("\t%08lx: %d x %d + %d + %d (%d)\n", w->id,
1667 w->a.width, w->a.height, w->a.x, w->a.y, w->a.border_width);
1676 printf ("windows:\n");
1677 for (w = list; w; w = w->next)
1683 damage_win (Display *dpy, XDamageNotifyEvent *de)
1685 win *w = find_win (dpy, de->drawable);
1692 if (w->damage_bounds.width == 0 || w->damage_bounds.height == 0)
1694 w->damage_bounds = de->area;
1698 if (de->area.x < w->damage_bounds.x)
1700 w->damage_bounds.width += (w->damage_bounds.x - de->area.x);
1701 w->damage_bounds.x = de->area.x;
1703 if (de->area.y < w->damage_bounds.y)
1705 w->damage_bounds.height += (w->damage_bounds.y - de->area.y);
1706 w->damage_bounds.y = de->area.y;
1708 if (de->area.x + de->area.width > w->damage_bounds.x + w->damage_bounds.width)
1709 w->damage_bounds.width = de->area.x + de->area.width - w->damage_bounds.x;
1710 if (de->area.y + de->area.height > w->damage_bounds.y + w->damage_bounds.height)
1711 w->damage_bounds.height = de->area.y + de->area.height - w->damage_bounds.y;
1714 printf ("unusable damage %d, %d: %d x %d bounds %d, %d: %d x %d\n",
1721 w->damage_bounds.width,
1722 w->damage_bounds.height);
1724 if (w->damage_bounds.x <= 0 &&
1725 w->damage_bounds.y <= 0 &&
1726 w->a.width <= w->damage_bounds.x + w->damage_bounds.width &&
1727 w->a.height <= w->damage_bounds.y + w->damage_bounds.height)
1731 set_fade (dpy, w, 0, get_opacity_percent (dpy, w, 1.0), fade_in_step, 0, False, True, True);
1737 repair_win (dpy, w);
1741 error (Display *dpy, XErrorEvent *ev)
1744 const char *name = 0;
1746 if (should_ignore (dpy, ev->serial))
1749 if (ev->request_code == composite_opcode &&
1750 ev->minor_code == X_CompositeRedirectSubwindows)
1752 fprintf (stderr, "Another composite manager is already running\n");
1756 o = ev->error_code - xfixes_error;
1758 case BadRegion: name = "BadRegion"; break;
1761 o = ev->error_code - damage_error;
1763 case BadDamage: name = "BadDamage"; break;
1766 o = ev->error_code - render_error;
1768 case BadPictFormat: name ="BadPictFormat"; break;
1769 case BadPicture: name ="BadPicture"; break;
1770 case BadPictOp: name ="BadPictOp"; break;
1771 case BadGlyphSet: name ="BadGlyphSet"; break;
1772 case BadGlyph: name ="BadGlyph"; break;
1776 printf ("error %d request %d minor %d serial %lu\n",
1777 ev->error_code, ev->request_code, ev->minor_code, ev->serial);
1779 /* abort (); this is just annoying to most people */
1784 expose_root (Display *dpy, Window root, XRectangle *rects, int nrects)
1786 XserverRegion region = XFixesCreateRegion (dpy, rects, nrects);
1788 add_damage (dpy, region);
1793 ev_serial (XEvent *ev)
1795 if (ev->type & 0x7f != KeymapNotify)
1796 return ev->xany.serial;
1797 return NextRequest (ev->xany.display);
1801 ev_name (XEvent *ev)
1803 static char buf[128];
1804 switch (ev->type & 0x7f) {
1811 case ReparentNotify:
1813 case CirculateNotify:
1816 if (ev->type == damage_event + XDamageNotify)
1818 sprintf (buf, "Event %d", ev->type);
1824 ev_window (XEvent *ev)
1828 return ev->xexpose.window;
1830 return ev->xmap.window;
1832 return ev->xunmap.window;
1833 case ReparentNotify:
1834 return ev->xreparent.window;
1835 case CirculateNotify:
1836 return ev->xcirculate.window;
1838 if (ev->type == damage_event + XDamageNotify)
1839 return ((XDamageNotifyEvent *) ev)->drawable;
1846 usage (char *program)
1848 fprintf (stderr, "%s v1.1.3\n", program);
1849 fprintf (stderr, "usage: %s [options]\n", program);
1850 fprintf (stderr, "Options\n");
1851 fprintf (stderr, " -d display\n Specifies which display should be managed.\n");
1852 fprintf (stderr, " -r radius\n Specifies the blur radius for client-side shadows. (default 12)\n");
1853 fprintf (stderr, " -o opacity\n Specifies the translucency for client-side shadows. (default .75)\n");
1854 fprintf (stderr, " -l left-offset\n Specifies the left offset for client-side shadows. (default -15)\n");
1855 fprintf (stderr, " -t top-offset\n Specifies the top offset for clinet-side shadows. (default -15)\n");
1856 fprintf (stderr, " -I fade-in-step\n Specifies the opacity change between steps while fading in. (default 0.028)\n");
1857 fprintf (stderr, " -O fade-out-step\n Specifies the opacity change between steps while fading out. (default 0.03)\n");
1858 fprintf (stderr, " -D fade-delta-time\n Specifies the time between steps in a fade in milliseconds. (default 10)\n");
1859 fprintf (stderr, " -a\n Use automatic server-side compositing. Faster, but no special effects.\n");
1860 fprintf (stderr, " -c\n Draw client-side shadows with fuzzy edges.\n");
1861 fprintf (stderr, " -C\n Avoid drawing shadows on dock/panel windows.\n");
1862 fprintf (stderr, " -f\n Fade windows in/out when opening/closing.\n");
1863 fprintf (stderr, " -F\n Fade windows during opacity changes.\n");
1864 fprintf (stderr, " -n\n Normal client-side compositing with transparency support\n");
1865 fprintf (stderr, " -s\n Draw server-side shadows with sharp edges.\n");
1866 fprintf (stderr, " -S\n Enable synchronous operation (for debugging).\n");
1876 w = XCreateSimpleWindow (dpy, RootWindow (dpy, 0), 0, 0, 1, 1, 0, None,
1879 Xutf8SetWMProperties (dpy, w, "xcompmgr", "xcompmgr", NULL, 0, NULL, NULL,
1882 /* FIXME: Don't hard code the screen number */
1883 a = XInternAtom (dpy, "_NET_WM_CM_S0", False);
1885 XSetSelectionOwner (dpy, a, w, 0);
1889 main (int argc, char **argv)
1892 Window root_return, parent_return;
1894 unsigned int nchildren;
1896 XRenderPictureAttributes pa;
1897 XRectangle *expose_rects = 0;
1898 int size_expose = 0;
1902 int composite_major, composite_minor;
1906 while ((o = getopt (argc, argv, "D:I:O:d:r:o:l:t:scnfFCaS")) != -1)
1913 fade_delta = atoi (optarg);
1918 fade_in_step = atof (optarg);
1919 if (fade_in_step <= 0)
1920 fade_in_step = 0.01;
1923 fade_out_step = atof (optarg);
1924 if (fade_out_step <= 0)
1925 fade_out_step = 0.01;
1928 compMode = CompServerShadows;
1931 compMode = CompClientShadows;
1934 excludeDockShadows = True;
1937 compMode = CompSimple;
1946 autoRedirect = True;
1952 shadowRadius = atoi (optarg);
1955 shadowOpacity = atof (optarg);
1958 shadowOffsetX = atoi (optarg);
1961 shadowOffsetY = atoi (optarg);
1969 dpy = XOpenDisplay (display);
1972 fprintf (stderr, "Can't open display\n");
1975 XSetErrorHandler (error);
1977 XSynchronize (dpy, 1);
1978 scr = DefaultScreen (dpy);
1979 root = RootWindow (dpy, scr);
1981 if (!XRenderQueryExtension (dpy, &render_event, &render_error))
1983 fprintf (stderr, "No render extension\n");
1986 if (!XQueryExtension (dpy, COMPOSITE_NAME, &composite_opcode,
1987 &composite_event, &composite_error))
1989 fprintf (stderr, "No composite extension\n");
1992 XCompositeQueryVersion (dpy, &composite_major, &composite_minor);
1993 #if HAS_NAME_WINDOW_PIXMAP
1994 if (composite_major > 0 || composite_minor >= 2)
1995 hasNamePixmap = True;
1998 if (!XDamageQueryExtension (dpy, &damage_event, &damage_error))
2000 fprintf (stderr, "No damage extension\n");
2003 if (!XFixesQueryExtension (dpy, &xfixes_event, &xfixes_error))
2005 fprintf (stderr, "No XFixes extension\n");
2012 opacityAtom = XInternAtom (dpy, OPACITY_PROP, False);
2013 winTypeAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE", False);
2014 winDesktopAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DESKTOP", False);
2015 winDockAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DOCK", False);
2016 winToolbarAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_TOOLBAR", False);
2017 winMenuAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_MENU", False);
2018 winUtilAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_UTILITY", False);
2019 winSplashAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_SPLASH", False);
2020 winDialogAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
2021 winNormalAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_NORMAL", False);
2023 pa.subwindow_mode = IncludeInferiors;
2025 if (compMode == CompClientShadows)
2027 gaussianMap = make_gaussian_map(dpy, shadowRadius);
2028 presum_gaussian (gaussianMap);
2031 root_width = DisplayWidth (dpy, scr);
2032 root_height = DisplayHeight (dpy, scr);
2034 rootPicture = XRenderCreatePicture (dpy, root,
2035 XRenderFindVisualFormat (dpy,
2036 DefaultVisual (dpy, scr)),
2039 blackPicture = solid_picture (dpy, True, 1, 0, 0, 0);
2040 if (compMode == CompServerShadows)
2041 transBlackPicture = solid_picture (dpy, True, 0.3, 0, 0, 0);
2046 XCompositeRedirectSubwindows (dpy, root, CompositeRedirectAutomatic);
2049 XCompositeRedirectSubwindows (dpy, root, CompositeRedirectManual);
2050 XSelectInput (dpy, root,
2051 SubstructureNotifyMask|
2053 StructureNotifyMask|
2054 PropertyChangeMask);
2055 XQueryTree (dpy, root, &root_return, &parent_return, &children, &nchildren);
2056 for (i = 0; i < nchildren; i++)
2057 add_win (dpy, children[i], i ? children[i-1] : None);
2060 XUngrabServer (dpy);
2061 ufd.fd = ConnectionNumber (dpy);
2062 ufd.events = POLLIN;
2064 paint_all (dpy, None);
2073 if (poll (&ufd, 1, fade_timeout()) == 0)
2080 XNextEvent (dpy, &ev);
2081 if ((ev.type & 0x7f) != KeymapNotify)
2082 discard_ignore (dpy, ev.xany.serial);
2084 printf ("event %10.10s serial 0x%08x window 0x%08x\n",
2085 ev_name(&ev), ev_serial (&ev), ev_window (&ev));
2087 if (!autoRedirect) switch (ev.type) {
2089 add_win (dpy, ev.xcreatewindow.window, 0);
2091 case ConfigureNotify:
2092 configure_win (dpy, &ev.xconfigure);
2095 destroy_win (dpy, ev.xdestroywindow.window, True, True);
2098 map_win (dpy, ev.xmap.window, ev.xmap.serial, True);
2101 unmap_win (dpy, ev.xunmap.window, True);
2103 case ReparentNotify:
2104 if (ev.xreparent.parent == root)
2105 add_win (dpy, ev.xreparent.window, 0);
2107 destroy_win (dpy, ev.xreparent.window, False, True);
2109 case CirculateNotify:
2110 circulate_win (dpy, &ev.xcirculate);
2113 if (ev.xexpose.window == root)
2115 int more = ev.xexpose.count + 1;
2116 if (n_expose == size_expose)
2120 expose_rects = realloc (expose_rects,
2121 (size_expose + more) *
2122 sizeof (XRectangle));
2123 size_expose += more;
2127 expose_rects = malloc (more * sizeof (XRectangle));
2131 expose_rects[n_expose].x = ev.xexpose.x;
2132 expose_rects[n_expose].y = ev.xexpose.y;
2133 expose_rects[n_expose].width = ev.xexpose.width;
2134 expose_rects[n_expose].height = ev.xexpose.height;
2136 if (ev.xexpose.count == 0)
2138 expose_root (dpy, root, expose_rects, n_expose);
2143 case PropertyNotify:
2144 for (p = 0; backgroundProps[p]; p++)
2146 if (ev.xproperty.atom == XInternAtom (dpy, backgroundProps[p], False))
2150 XClearArea (dpy, root, 0, 0, 0, 0, True);
2151 XRenderFreePicture (dpy, rootTile);
2157 /* check if Trans property was changed */
2158 if (ev.xproperty.atom == opacityAtom)
2160 /* reset mode and redraw window */
2161 win * w = find_win(dpy, ev.xproperty.window);
2165 set_fade (dpy, w, w->opacity*1.0/OPAQUE, get_opacity_percent (dpy, w, 1.0),
2166 fade_out_step, 0, False, True, False);
2169 w->opacity = get_opacity_prop(dpy, w, OPAQUE);
2170 determine_mode(dpy, w);
2173 XRenderFreePicture (dpy, w->shadow);
2175 w->extents = win_extents (dpy, w);
2182 if (ev.type == damage_event + XDamageNotify)
2183 damage_win (dpy, (XDamageNotifyEvent *) &ev);
2186 } while (QLength (dpy));
2187 if (allDamage && !autoRedirect)
2190 paint_all (dpy, allDamage);
2194 clipChanged = False;