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 map_win (Display *dpy, Window id, unsigned long sequence, Bool fade)
1171 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);
1183 w->damage_bounds.x = w->damage_bounds.y = 0;
1184 w->damage_bounds.width = w->damage_bounds.height = 0;
1188 if (fade && fadeWindows)
1189 set_fade (dpy, w, 0, get_opacity_percent (dpy, w, 1.0), fade_in_step, 0, False, True, True);
1193 finish_unmap_win (Display *dpy, win *w)
1199 if (w->extents != None)
1201 add_damage (dpy, w->extents); /* destroys region */
1205 #if HAS_NAME_WINDOW_PIXMAP
1208 XFreePixmap (dpy, w->pixmap);
1215 set_ignore (dpy, NextRequest (dpy));
1216 XRenderFreePicture (dpy, w->picture);
1220 /* don't care about properties anymore */
1221 set_ignore (dpy, NextRequest (dpy));
1222 XSelectInput(dpy, w->id, 0);
1226 set_ignore (dpy, NextRequest (dpy));
1227 XFixesDestroyRegion (dpy, w->borderSize);
1228 w->borderSize = None;
1232 XRenderFreePicture (dpy, w->shadow);
1237 XFixesDestroyRegion (dpy, w->borderClip);
1238 w->borderClip = None;
1244 #if HAS_NAME_WINDOW_PIXMAP
1246 unmap_callback (Display *dpy, win *w, Bool gone)
1248 finish_unmap_win (dpy, w);
1253 unmap_win (Display *dpy, Window id, Bool fade)
1255 win *w = find_win (dpy, id);
1258 w->a.map_state = IsUnmapped;
1259 #if HAS_NAME_WINDOW_PIXMAP
1260 if (w->pixmap && fade && fadeWindows)
1261 set_fade (dpy, w, w->opacity*1.0/OPAQUE, 0.0, fade_out_step, unmap_callback, False, False, True);
1264 finish_unmap_win (dpy, w);
1267 /* Get the opacity prop from window
1272 get_opacity_prop(Display *dpy, win *w, unsigned int def)
1276 unsigned long n, left;
1278 unsigned char *data;
1279 int result = XGetWindowProperty(dpy, w->id, opacityAtom, 0L, 1L, False,
1280 XA_CARDINAL, &actual, &format,
1282 if (result == Success && data != NULL)
1285 memcpy (&i, data, sizeof (unsigned int));
1286 XFree( (void *) data);
1292 /* Get the opacity property from the window in a percent format
1294 otherwise: the value
1297 get_opacity_percent(Display *dpy, win *w, double def)
1299 unsigned int opacity = get_opacity_prop (dpy, w, (unsigned int)(OPAQUE*def));
1301 return opacity*1.0/OPAQUE;
1304 /* determine mode for window all in one place.
1305 Future might check for menu flag and other cool things
1309 get_wintype_prop(Display * dpy, Window w)
1313 unsigned long n, left;
1315 unsigned char *data;
1316 int result = XGetWindowProperty (dpy, w, winTypeAtom, 0L, 1L, False,
1317 XA_ATOM, &actual, &format,
1320 if (result == Success && data != None)
1323 memcpy (&a, data, sizeof (Atom));
1324 XFree ( (void *) data);
1327 return winNormalAtom;
1331 determine_mode(Display *dpy, win *w)
1334 XRenderPictFormat *format;
1335 unsigned int default_opacity;
1337 /* if trans prop == -1 fall back on previous tests*/
1341 XRenderFreePicture (dpy, w->alphaPict);
1342 w->alphaPict = None;
1346 XRenderFreePicture (dpy, w->shadowPict);
1347 w->shadowPict = None;
1350 if (w->a.class == InputOnly)
1356 format = XRenderFindVisualFormat (dpy, w->a.visual);
1359 if (format && format->type == PictTypeDirect && format->direct.alphaMask)
1363 else if (w->opacity != OPAQUE)
1365 mode = WINDOW_TRANS;
1369 mode = WINDOW_SOLID;
1374 XserverRegion damage;
1375 damage = XFixesCreateRegion (dpy, 0, 0);
1376 XFixesCopyRegion (dpy, damage, w->extents);
1377 add_damage (dpy, damage);
1382 determine_wintype (Display *dpy, Window w)
1384 Window root_return, parent_return;
1385 Window *children = NULL;
1386 unsigned int nchildren, i;
1389 type = get_wintype_prop (dpy, w);
1390 if (type != winNormalAtom)
1393 if (!XQueryTree (dpy, w, &root_return, &parent_return, &children,
1396 /* XQueryTree failed. */
1398 XFree ((void *)children);
1399 return winNormalAtom;
1402 for (i = 0;i < nchildren;i++)
1404 type = determine_wintype (dpy, children[i]);
1405 if (type != winNormalAtom)
1410 XFree ((void *)children);
1412 return winNormalAtom;
1416 add_win (Display *dpy, Window id, Window prev)
1418 win *new = malloc (sizeof (win));
1425 for (p = &list; *p; p = &(*p)->next)
1426 if ((*p)->id == prev)
1432 set_ignore (dpy, NextRequest (dpy));
1433 if (!XGetWindowAttributes (dpy, id, &new->a))
1440 new->usable = False;
1442 #if HAS_NAME_WINDOW_PIXMAP
1445 new->picture = None;
1446 if (new->a.class == InputOnly)
1448 new->damage_sequence = 0;
1453 new->damage_sequence = NextRequest (dpy);
1454 new->damage = XDamageCreate (dpy, id, XDamageReportNonEmpty);
1456 new->alphaPict = None;
1457 new->shadowPict = None;
1458 new->borderSize = None;
1459 new->extents = None;
1463 new->shadow_width = 0;
1464 new->shadow_height = 0;
1465 new->opacity = OPAQUE;
1467 new->borderClip = None;
1468 new->prev_trans = 0;
1470 /* moved mode setting to one place */
1471 new->opacity = get_opacity_prop (dpy, new, OPAQUE);
1472 new->windowType = determine_wintype (dpy, new->id);
1473 determine_mode (dpy, new);
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);
1516 XserverRegion damage = None;
1520 if (ce->window == root)
1524 XRenderFreePicture (dpy, rootBuffer);
1527 root_width = ce->width;
1528 root_height = ce->height;
1536 damage = XFixesCreateRegion (dpy, 0, 0);
1537 if (w->extents != None)
1538 XFixesCopyRegion (dpy, damage, w->extents);
1542 if (w->a.width != ce->width || w->a.height != ce->height)
1544 #if HAS_NAME_WINDOW_PIXMAP
1547 XFreePixmap (dpy, w->pixmap);
1551 XRenderFreePicture (dpy, w->picture);
1558 XRenderFreePicture (dpy, w->shadow);
1562 w->a.width = ce->width;
1563 w->a.height = ce->height;
1564 w->a.border_width = ce->border_width;
1565 w->a.override_redirect = ce->override_redirect;
1566 restack_win (dpy, w, ce->above);
1569 XserverRegion extents = win_extents (dpy, w);
1570 XFixesUnionRegion (dpy, damage, damage, extents);
1571 XFixesDestroyRegion (dpy, extents);
1572 add_damage (dpy, damage);
1578 circulate_win (Display *dpy, XCirculateEvent *ce)
1580 win *w = find_win (dpy, ce->window);
1586 if (ce->place == PlaceOnTop)
1587 new_above = list->id;
1590 restack_win (dpy, w, new_above);
1595 finish_destroy_win (Display *dpy, Window id, Bool gone)
1599 for (prev = &list; (w = *prev); prev = &w->next)
1603 finish_unmap_win (dpy, w);
1607 set_ignore (dpy, NextRequest (dpy));
1608 XRenderFreePicture (dpy, w->picture);
1613 XRenderFreePicture (dpy, w->alphaPict);
1614 w->alphaPict = None;
1618 XRenderFreePicture (dpy, w->shadowPict);
1619 w->shadowPict = None;
1621 if (w->damage != None)
1623 set_ignore (dpy, NextRequest (dpy));
1624 XDamageDestroy (dpy, w->damage);
1627 cleanup_fade (dpy, w);
1633 #if HAS_NAME_WINDOW_PIXMAP
1635 destroy_callback (Display *dpy, win *w, Bool gone)
1637 finish_destroy_win (dpy, w->id, gone);
1642 destroy_win (Display *dpy, Window id, Bool gone, Bool fade)
1644 win *w = find_win (dpy, id);
1645 #if HAS_NAME_WINDOW_PIXMAP
1646 if (w && w->pixmap && fade && fadeWindows)
1647 set_fade (dpy, w, w->opacity*1.0/OPAQUE, 0.0, fade_out_step, destroy_callback, gone, False, True);
1651 finish_destroy_win (dpy, id, gone);
1659 printf ("\t%08lx: %d x %d + %d + %d (%d)\n", w->id,
1660 w->a.width, w->a.height, w->a.x, w->a.y, w->a.border_width);
1669 printf ("windows:\n");
1670 for (w = list; w; w = w->next)
1676 damage_win (Display *dpy, XDamageNotifyEvent *de)
1678 win *w = find_win (dpy, de->drawable);
1685 if (w->damage_bounds.width == 0 || w->damage_bounds.height == 0)
1687 w->damage_bounds = de->area;
1691 if (de->area.x < w->damage_bounds.x)
1693 w->damage_bounds.width += (w->damage_bounds.x - de->area.x);
1694 w->damage_bounds.x = de->area.x;
1696 if (de->area.y < w->damage_bounds.y)
1698 w->damage_bounds.height += (w->damage_bounds.y - de->area.y);
1699 w->damage_bounds.y = de->area.y;
1701 if (de->area.x + de->area.width > w->damage_bounds.x + w->damage_bounds.width)
1702 w->damage_bounds.width = de->area.x + de->area.width - w->damage_bounds.x;
1703 if (de->area.y + de->area.height > w->damage_bounds.y + w->damage_bounds.height)
1704 w->damage_bounds.height = de->area.y + de->area.height - w->damage_bounds.y;
1707 printf ("unusable damage %d, %d: %d x %d bounds %d, %d: %d x %d\n",
1714 w->damage_bounds.width,
1715 w->damage_bounds.height);
1717 if (w->damage_bounds.x <= 0 &&
1718 w->damage_bounds.y <= 0 &&
1719 w->a.width <= w->damage_bounds.x + w->damage_bounds.width &&
1720 w->a.height <= w->damage_bounds.y + w->damage_bounds.height)
1724 set_fade (dpy, w, 0, get_opacity_percent (dpy, w, 1.0), fade_in_step, 0, False, True, True);
1730 repair_win (dpy, w);
1734 error (Display *dpy, XErrorEvent *ev)
1739 if (should_ignore (dpy, ev->serial))
1742 if (ev->request_code == composite_opcode &&
1743 ev->minor_code == X_CompositeRedirectSubwindows)
1745 fprintf (stderr, "Another composite manager is already running\n");
1749 o = ev->error_code - xfixes_error;
1751 case BadRegion: name = "BadRegion"; break;
1754 o = ev->error_code - damage_error;
1756 case BadDamage: name = "BadDamage"; break;
1759 o = ev->error_code - render_error;
1761 case BadPictFormat: name ="BadPictFormat"; break;
1762 case BadPicture: name ="BadPicture"; break;
1763 case BadPictOp: name ="BadPictOp"; break;
1764 case BadGlyphSet: name ="BadGlyphSet"; break;
1765 case BadGlyph: name ="BadGlyph"; break;
1769 printf ("error %d request %d minor %d serial %d\n",
1770 ev->error_code, ev->request_code, ev->minor_code, ev->serial);
1772 /* abort (); this is just annoying to most people */
1777 expose_root (Display *dpy, Window root, XRectangle *rects, int nrects)
1779 XserverRegion region = XFixesCreateRegion (dpy, rects, nrects);
1781 add_damage (dpy, region);
1786 ev_serial (XEvent *ev)
1788 if (ev->type & 0x7f != KeymapNotify)
1789 return ev->xany.serial;
1790 return NextRequest (ev->xany.display);
1795 ev_name (XEvent *ev)
1797 static char buf[128];
1798 switch (ev->type & 0x7f) {
1805 case ReparentNotify:
1807 case CirculateNotify:
1810 if (ev->type == damage_event + XDamageNotify)
1812 sprintf (buf, "Event %d", ev->type);
1818 ev_window (XEvent *ev)
1822 return ev->xexpose.window;
1824 return ev->xmap.window;
1826 return ev->xunmap.window;
1827 case ReparentNotify:
1828 return ev->xreparent.window;
1829 case CirculateNotify:
1830 return ev->xcirculate.window;
1832 if (ev->type == damage_event + XDamageNotify)
1833 return ((XDamageNotifyEvent *) ev)->drawable;
1839 usage (char *program)
1841 fprintf (stderr, "%s v1.1.2\n", program);
1842 fprintf (stderr, "usage: %s [options]\n", program);
1843 fprintf (stderr, "Options\n");
1844 fprintf (stderr, " -d display\n Specifies which display should be managed.\n");
1845 fprintf (stderr, " -r radius\n Specifies the blur radius for client-side shadows. (default 12)\n");
1846 fprintf (stderr, " -o opacity\n Specifies the translucency for client-side shadows. (default .75)\n");
1847 fprintf (stderr, " -l left-offset\n Specifies the left offset for client-side shadows. (default -15)\n");
1848 fprintf (stderr, " -t top-offset\n Specifies the top offset for clinet-side shadows. (default -15)\n");
1849 fprintf (stderr, " -I fade-in-step\n Specifies the opacity change between steps while fading in. (default 0.028)\n");
1850 fprintf (stderr, " -O fade-out-step\n Specifies the opacity change between steps while fading out. (default 0.03)\n");
1851 fprintf (stderr, " -D fade-delta-time\n Specifies the time between steps in a fade in milliseconds. (default 10)\n");
1852 fprintf (stderr, " -a\n Use automatic server-side compositing. Faster, but no special effects.\n");
1853 fprintf (stderr, " -c\n Draw client-side shadows with fuzzy edges.\n");
1854 fprintf (stderr, " -C\n Avoid drawing shadows on dock/panel windows.\n");
1855 fprintf (stderr, " -f\n Fade windows in/out when opening/closing.\n");
1856 fprintf (stderr, " -F\n Fade windows during opacity changes.\n");
1857 fprintf (stderr, " -n\n Normal client-side compositing with transparency support\n");
1858 fprintf (stderr, " -s\n Draw server-side shadows with sharp edges.\n");
1859 fprintf (stderr, " -S\n Enable synchronous operation (for debugging).\n");
1864 give_me_a_name (void)
1868 w = XCreateSimpleWindow (dpy, RootWindow (dpy, 0), 0, 0, 1, 1, 0, None,
1871 Xutf8SetWMProperties (dpy, w, "xcompmgr", "xcompmgr", NULL, 0, NULL, NULL,
1876 main (int argc, char **argv)
1879 Window root_return, parent_return;
1883 unsigned int nchildren;
1885 XRenderPictureAttributes pa;
1887 XRectangle *expose_rects = 0;
1888 int size_expose = 0;
1895 int composite_major, composite_minor;
1899 while ((o = getopt (argc, argv, "D:I:O:d:r:o:l:t:scnfFCaS")) != -1)
1906 fade_delta = atoi (optarg);
1911 fade_in_step = atof (optarg);
1912 if (fade_in_step <= 0)
1913 fade_in_step = 0.01;
1916 fade_out_step = atof (optarg);
1917 if (fade_out_step <= 0)
1918 fade_out_step = 0.01;
1921 compMode = CompServerShadows;
1924 compMode = CompClientShadows;
1927 excludeDockShadows = True;
1930 compMode = CompSimple;
1939 autoRedirect = True;
1945 shadowRadius = atoi (optarg);
1948 shadowOpacity = atof (optarg);
1951 shadowOffsetX = atoi (optarg);
1954 shadowOffsetY = atoi (optarg);
1962 dpy = XOpenDisplay (display);
1965 fprintf (stderr, "Can't open display\n");
1968 XSetErrorHandler (error);
1970 XSynchronize (dpy, 1);
1971 scr = DefaultScreen (dpy);
1972 root = RootWindow (dpy, scr);
1974 if (!XRenderQueryExtension (dpy, &render_event, &render_error))
1976 fprintf (stderr, "No render extension\n");
1979 if (!XQueryExtension (dpy, COMPOSITE_NAME, &composite_opcode,
1980 &composite_event, &composite_error))
1982 fprintf (stderr, "No composite extension\n");
1985 XCompositeQueryVersion (dpy, &composite_major, &composite_minor);
1986 #if HAS_NAME_WINDOW_PIXMAP
1987 if (composite_major > 0 || composite_minor >= 2)
1988 hasNamePixmap = True;
1991 if (!XDamageQueryExtension (dpy, &damage_event, &damage_error))
1993 fprintf (stderr, "No damage extension\n");
1996 if (!XFixesQueryExtension (dpy, &xfixes_event, &xfixes_error))
1998 fprintf (stderr, "No XFixes extension\n");
2005 opacityAtom = XInternAtom (dpy, OPACITY_PROP, False);
2006 winTypeAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE", False);
2007 winDesktopAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DESKTOP", False);
2008 winDockAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DOCK", False);
2009 winToolbarAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_TOOLBAR", False);
2010 winMenuAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_MENU", False);
2011 winUtilAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_UTILITY", False);
2012 winSplashAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_SPLASH", False);
2013 winDialogAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
2014 winNormalAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_NORMAL", False);
2016 pa.subwindow_mode = IncludeInferiors;
2018 if (compMode == CompClientShadows)
2020 gaussianMap = make_gaussian_map(dpy, shadowRadius);
2021 presum_gaussian (gaussianMap);
2024 root_width = DisplayWidth (dpy, scr);
2025 root_height = DisplayHeight (dpy, scr);
2027 rootPicture = XRenderCreatePicture (dpy, root,
2028 XRenderFindVisualFormat (dpy,
2029 DefaultVisual (dpy, scr)),
2032 blackPicture = solid_picture (dpy, True, 1, 0, 0, 0);
2033 if (compMode == CompServerShadows)
2034 transBlackPicture = solid_picture (dpy, True, 0.3, 0, 0, 0);
2039 XCompositeRedirectSubwindows (dpy, root, CompositeRedirectAutomatic);
2042 XCompositeRedirectSubwindows (dpy, root, CompositeRedirectManual);
2043 XSelectInput (dpy, root,
2044 SubstructureNotifyMask|
2046 StructureNotifyMask|
2047 PropertyChangeMask);
2048 XQueryTree (dpy, root, &root_return, &parent_return, &children, &nchildren);
2049 for (i = 0; i < nchildren; i++)
2050 add_win (dpy, children[i], i ? children[i-1] : None);
2053 XUngrabServer (dpy);
2054 ufd.fd = ConnectionNumber (dpy);
2055 ufd.events = POLLIN;
2057 paint_all (dpy, None);
2066 if (poll (&ufd, 1, fade_timeout()) == 0)
2073 XNextEvent (dpy, &ev);
2074 if (ev.type & 0x7f != KeymapNotify)
2075 discard_ignore (dpy, ev.xany.serial);
2077 printf ("event %10.10s serial 0x%08x window 0x%08x\n",
2078 ev_name(&ev), ev_serial (&ev), ev_window (&ev));
2080 if (!autoRedirect) switch (ev.type) {
2082 add_win (dpy, ev.xcreatewindow.window, 0);
2084 case ConfigureNotify:
2085 configure_win (dpy, &ev.xconfigure);
2088 destroy_win (dpy, ev.xdestroywindow.window, True, True);
2091 map_win (dpy, ev.xmap.window, ev.xmap.serial, True);
2094 unmap_win (dpy, ev.xunmap.window, True);
2096 case ReparentNotify:
2097 if (ev.xreparent.parent == root)
2098 add_win (dpy, ev.xreparent.window, 0);
2100 destroy_win (dpy, ev.xreparent.window, False, True);
2102 case CirculateNotify:
2103 circulate_win (dpy, &ev.xcirculate);
2106 if (ev.xexpose.window == root)
2108 int more = ev.xexpose.count + 1;
2109 if (n_expose == size_expose)
2113 expose_rects = realloc (expose_rects,
2114 (size_expose + more) *
2115 sizeof (XRectangle));
2116 size_expose += more;
2120 expose_rects = malloc (more * sizeof (XRectangle));
2124 expose_rects[n_expose].x = ev.xexpose.x;
2125 expose_rects[n_expose].y = ev.xexpose.y;
2126 expose_rects[n_expose].width = ev.xexpose.width;
2127 expose_rects[n_expose].height = ev.xexpose.height;
2129 if (ev.xexpose.count == 0)
2131 expose_root (dpy, root, expose_rects, n_expose);
2136 case PropertyNotify:
2137 for (p = 0; backgroundProps[p]; p++)
2139 if (ev.xproperty.atom == XInternAtom (dpy, backgroundProps[p], False))
2143 XClearArea (dpy, root, 0, 0, 0, 0, True);
2144 XRenderFreePicture (dpy, rootTile);
2150 /* check if Trans property was changed */
2151 if (ev.xproperty.atom == opacityAtom)
2153 /* reset mode and redraw window */
2154 win * w = find_win(dpy, ev.xproperty.window);
2158 set_fade (dpy, w, w->opacity*1.0/OPAQUE, get_opacity_percent (dpy, w, 1.0),
2159 fade_out_step, 0, False, True, False);
2162 w->opacity = get_opacity_prop(dpy, w, OPAQUE);
2163 determine_mode(dpy, w);
2166 XRenderFreePicture (dpy, w->shadow);
2168 w->extents = win_extents (dpy, w);
2175 if (ev.type == damage_event + XDamageNotify)
2176 damage_win (dpy, (XDamageNotifyEvent *) &ev);
2179 } while (QLength (dpy));
2180 if (allDamage && !autoRedirect)
2183 paint_all (dpy, allDamage);
2187 clipChanged = False;