6 #include <X11/extensions/Xcomposite.h>
7 #include <X11/extensions/Xdamage.h>
8 #include <X11/extensions/Xrender.h>
18 XserverRegion borderSize;
19 XserverRegion extents;
26 /* for drawing translucent windows */
27 XserverRegion borderClip;
28 struct _win *prev_trans;
38 XserverRegion allDamage;
40 #define WINDOW_PLAIN 0
42 #define WINDOW_TRANS 2
43 #define TRANS_OPACITY 0.75
44 #define SHADOW_RADIUS 15
45 #define SHADOW_OPACITY 0.75
46 #define SHADOW_OFFSET_X (-SHADOW_RADIUS)
47 #define SHADOW_OFFSET_Y (-SHADOW_RADIUS)
51 gaussian (double r, double x, double y)
53 return ((1 / (sqrt (2 * M_PI * r))) *
54 exp ((- (x * x + y * y)) / (2 * r * r)));
57 typedef struct _conv {
63 make_gaussian_map (Display *dpy, double r)
66 int size = ((int) ceil ((r * 3)) + 1) & ~1;
67 int center = size / 2;
72 c = malloc (sizeof (conv) + size * size * sizeof (double));
74 c->data = (double *) (c + 1);
75 for (y = 0; y < size; y++)
76 for (x = 0; x < size; x++)
78 g = gaussian (r, (double) (x - center), (double) (y - center));
80 c->data[y * size + x] = g;
82 printf ("gaussian total %f\n", t);
83 for (y = 0; y < size; y++)
84 for (x = 0; x < size; x++)
86 c->data[y*size + x] /= t;
94 * -center 0 width width+center
95 * -center +-----+-------------------+-----+
98 * 0 +-----+-------------------+-----+
102 * height +-----+-------------------+-----+
105 * center +-----+-------------------+-----+
109 sum_gaussian (conv *map, double opacity, int x, int y, int width, int height)
114 double *g_line = map->data;
115 int g_size = map->size;
116 int center = g_size / 2;
117 int fx_start, fx_end;
118 int fy_start, fy_end;
122 * Compute set of filter values which are "in range",
123 * that's the set with:
124 * 0 <= x + (fx-center) && x + (fx-center) < width &&
125 * 0 <= y + (fy-center) && y + (fy-center) < height
127 * 0 <= x + (fx - center) x + fx - center < width
128 * center - x <= fx fx < width + center - x
131 fx_start = center - x;
134 fx_end = width + center - x;
138 fy_start = center - y;
141 fy_end = height + center - y;
145 g_line = g_line + fy_start * g_size + fx_start;
148 for (fy = fy_start; fy < fy_end; fy++)
153 for (fx = fx_start; fx < fx_end; fx++)
159 return ((unsigned int) (v * opacity * 255.0)) << 24;
163 make_shadow (Display *dpy, double opacity, double r, int width, int height)
165 conv *map = make_gaussian_map (dpy, r);
167 double *gdata = map->data;
169 int gsize = map->size;
171 int swidth = width + gsize;
172 int sheight = height + gsize;
173 int center = gsize / 2;
181 data = malloc (swidth * sheight * sizeof (int));
182 ximage = XCreateImage (dpy,
183 DefaultVisual(dpy, DefaultScreen(dpy)),
188 swidth, sheight, 32, swidth * sizeof (int));
190 * Build the gaussian in sections
197 if (ylimit > sheight / 2)
198 ylimit = (sheight + 1) / 2;
200 if (xlimit > swidth / 2)
201 xlimit = (swidth + 1) / 2;
203 for (y = 0; y < ylimit; y++)
204 for (x = 0; x < xlimit; x++)
206 d = sum_gaussian (map, opacity, x - center, y - center, width, height);
207 data[y * swidth + x] = d;
208 data[(sheight - y - 1) * swidth + x] = d;
209 data[(sheight - y - 1) * swidth + (swidth - x - 1)] = d;
210 data[y * swidth + (swidth - x - 1)] = d;
216 for (y = 0; y < ylimit; y++)
218 d = sum_gaussian (map, opacity, center, y - center, width, height);
219 for (x = gsize; x < swidth - gsize; x++)
221 data[y * swidth + x] = d;
222 data[(sheight - y - 1) * swidth + x] = d;
230 for (x = 0; x < xlimit; x++)
232 d = sum_gaussian (map, opacity, x - center, center, width, height);
233 for (y = gsize; y < sheight - gsize; y++)
235 data[y * swidth + x] = d;
236 data[y * swidth + (swidth - x - 1)] = d;
244 d = sum_gaussian (map, opacity, center, center, width, height);
245 for (y = ylimit; y < sheight - ylimit; y++)
246 for (x = xlimit; x < swidth - xlimit; x++)
247 data[y * swidth + x] = d;
254 shadow_picture (Display *dpy, double opacity, double r, int width, int height, int *wp, int *hp)
256 XImage *shadowImage = make_shadow (dpy, opacity, r, width, height);
257 Pixmap shadowPixmap = XCreatePixmap (dpy, root,
261 Picture shadowPicture = XRenderCreatePicture (dpy, shadowPixmap,
262 XRenderFindStandardFormat (dpy, PictStandardARGB32),
264 GC gc = XCreateGC (dpy, shadowPixmap, 0, 0);
266 XPutImage (dpy, shadowPixmap, gc, shadowImage, 0, 0, 0, 0,
268 shadowImage->height);
269 *wp = shadowImage->width;
270 *hp = shadowImage->height;
272 XDestroyImage (shadowImage);
273 XFreePixmap (dpy, shadowPixmap);
274 return shadowPicture;
278 find_win (Display *dpy, Window id)
282 for (w = list; w; w = w->next)
289 paint_root (Display *dpy)
293 c.red = c.green = c.blue = 0x8080;
295 XRenderFillRectangle (dpy, PictOpSrc, rootPicture, &c,
300 win_extents (Display *dpy, win *w)
306 double opacity = SHADOW_OPACITY;
307 if (w->mode == WINDOW_TRANS)
308 opacity = opacity * TRANS_OPACITY;
309 w->shadow = shadow_picture (dpy, opacity, SHADOW_RADIUS,
310 w->a.width, w->a.height,
311 &w->shadow_width, &w->shadow_height);
312 w->shadow_dx = SHADOW_OFFSET_X;
313 w->shadow_dy = SHADOW_OFFSET_Y;
315 r.x = w->a.x + w->a.border_width + w->shadow_dx;
316 r.y = w->a.y + w->a.border_width + w->shadow_dy;
317 r.width = w->shadow_width;
318 r.height = w->shadow_height;
319 return XFixesCreateRegion (dpy, &r, 1);
323 border_size (Display *dpy, win *w)
325 XserverRegion border;
326 border = XFixesCreateRegionFromWindow (dpy, w->id, WindowRegionBounding);
328 XFixesUnionRegion (dpy, border, border, w->a.x, w->a.y, None, 0, 0);
333 paint_all (Display *dpy, XserverRegion region)
338 for (w = list; w; w = w->next)
342 if (w->a.map_state != IsViewable)
345 XFixesDestroyRegion (dpy, w->borderSize);
346 w->borderSize = border_size (dpy, w);
348 XFixesDestroyRegion (dpy, w->extents);
349 w->extents = win_extents (dpy, w);
350 if (w->mode != WINDOW_TRANS)
352 XFixesSetPictureClipRegion (dpy, rootPicture, 0, 0, region);
353 XFixesSubtractRegion (dpy, region, region, 0, 0, w->borderSize, 0, 0);
354 XRenderComposite (dpy, PictOpSrc, w->picture, None, rootPicture,
356 w->a.x + w->a.border_width,
357 w->a.y + w->a.border_width,
361 w->borderClip = XFixesCreateRegion (dpy, 0, 0);
362 XFixesUnionRegion (dpy, w->borderClip, region, 0, 0, None, 0, 0);
366 XFixesSetPictureClipRegion (dpy, rootPicture, 0, 0, region);
368 for (w = t; w; w = w->prev_trans)
370 XFixesSetPictureClipRegion (dpy, rootPicture, 0, 0, w->borderClip);
373 XRenderComposite (dpy, PictOpOver, w->shadow, None, rootPicture,
375 w->a.x + w->a.border_width + w->shadow_dx,
376 w->a.y + w->a.border_width + w->shadow_dy,
377 w->shadow_width, w->shadow_height);
379 if (w->mode == WINDOW_TRANS)
380 XRenderComposite (dpy, PictOpOver, w->picture, transPicture, rootPicture,
382 w->a.x + w->a.border_width,
383 w->a.y + w->a.border_width,
386 XFixesDestroyRegion (dpy, w->borderClip);
387 w->borderClip = None;
389 XFixesDestroyRegion (dpy, region);
393 add_damage (Display *dpy, XserverRegion damage)
397 XFixesUnionRegion (dpy, allDamage, allDamage, 0, 0, damage, 0, 0);
398 XFixesDestroyRegion (dpy, damage);
405 repair_win (Display *dpy, Window id)
407 win *w = find_win (dpy, id);
412 /* printf ("repair 0x%x\n", w->id); */
413 parts = XFixesCreateRegion (dpy, 0, 0);
414 /* translate region */
415 XDamageSubtract (dpy, w->damage, None, parts);
416 XFixesUnionRegion (dpy, parts, parts, w->a.x, w->a.y, None, 0, 0);
417 add_damage (dpy, parts);
421 map_win (Display *dpy, Window id)
423 win *w = find_win (dpy, id);
424 XserverRegion region;
428 w->a.map_state = IsViewable;
429 w->damage = XDamageCreate (dpy, id, XDamageReportNonEmpty);
430 region = win_extents (dpy, w);
431 add_damage (dpy, region);
435 unmap_win (Display *dpy, Window id)
437 win *w = find_win (dpy, id);
441 w->a.map_state = IsUnmapped;
442 if (w->damage != None)
444 XDamageDestroy (dpy, w->damage);
447 if (w->extents != None)
449 add_damage (dpy, w->extents); /* destroys region */
455 add_win (Display *dpy, Window id, Window prev)
457 win *new = malloc (sizeof (win));
460 XRenderPictureAttributes pa;
466 for (p = &list; *p; p = &(*p)->next)
467 if ((*p)->id == prev)
473 if (!XGetWindowAttributes (dpy, id, &new->a))
480 pa.subwindow_mode = IncludeInferiors;
481 new->picture = XRenderCreatePicture (dpy, id,
482 XRenderFindVisualFormat (dpy,
488 new->borderSize = None;
490 if (new->a.override_redirect)
491 new->mode = WINDOW_TRANS;
493 new->mode = WINDOW_DROP;
496 if (new->a.map_state == IsViewable)
501 configure_win (Display *dpy, XConfigureEvent *ce)
503 win *w = find_win (dpy, ce->window);
505 XserverRegion damage = None;
509 if (w->a.map_state == IsViewable)
511 damage = XFixesCreateRegion (dpy, 0, 0);
512 if (w->extents != None)
513 XFixesUnionRegion (dpy, damage, w->extents, 0, 0, None, 0, 0);
517 if (w->a.width != ce->width || w->a.height != ce->height)
520 XRenderFreePicture (dpy, w->shadow);
523 w->a.width = ce->width;
524 w->a.height = ce->height;
525 w->a.border_width = ce->border_width;
526 w->a.override_redirect = ce->override_redirect;
531 if (above != ce->above)
536 for (prev = &list; *prev; prev = &(*prev)->next)
542 for (prev = &list; *prev; prev = &(*prev)->next)
544 if ((*prev)->id == ce->above)
552 XserverRegion border = border_size (dpy, w);
553 XFixesUnionRegion (dpy, damage, damage, 0, 0, border, 0, 0);
554 add_damage (dpy, damage);
559 destroy_win (Display *dpy, Window id, Bool gone)
563 for (prev = &list; w = *prev; prev = &w->next)
569 XRenderFreePicture (dpy, w->picture);
580 printf ("\t%08x: %d x %d + %d + %d (%d)\n", w->id,
581 w->a.width, w->a.height, w->a.x, w->a.y, w->a.border_width);
589 printf ("windows:\n");
590 for (w = list; w; w = w->next)
595 damage_win (Display *dpy, XDamageNotifyEvent *de)
597 repair_win (dpy, de->drawable);
601 error (Display *dpy, XErrorEvent *ev)
603 printf ("error %d request %d minor %d\n",
604 ev->error_code, ev->request_code, ev->minor_code);
608 expose_root (Display *dpy, Window root, XRectangle *rects, int nrects)
610 XserverRegion region = XFixesCreateRegion (dpy, rects, nrects);
612 add_damage (dpy, region);
618 int event_base, error_base;
619 Window root_return, parent_return;
622 unsigned int nchildren;
624 int damage_event, damage_error;
625 int xfixes_event, xfixes_error;
626 XRenderPictureAttributes pa;
628 XRectangle *expose_rects = 0;
633 dpy = XOpenDisplay (0);
636 fprintf (stderr, "Can't open display\n");
639 XSetErrorHandler (error);
640 scr = DefaultScreen (dpy);
641 root = RootWindow (dpy, scr);
642 pa.subwindow_mode = IncludeInferiors;
643 transPixmap = XCreatePixmap (dpy, root, 1, 1, 8);
645 transPicture = XRenderCreatePicture (dpy, transPixmap,
646 XRenderFindStandardFormat (dpy, PictStandardA8),
649 c.red = c.green = c.blue = 0;
651 XRenderFillRectangle (dpy, PictOpSrc, transPicture, &c, 0, 0, 1, 1);
653 rootPicture = XRenderCreatePicture (dpy, root,
654 XRenderFindVisualFormat (dpy,
655 DefaultVisual (dpy, scr)),
658 if (!XCompositeQueryExtension (dpy, &event_base, &error_base))
660 fprintf (stderr, "No composite extension\n");
663 if (!XDamageQueryExtension (dpy, &damage_event, &damage_error))
665 fprintf (stderr, "No damage extension\n");
668 if (!XFixesQueryExtension (dpy, &xfixes_event, &xfixes_error))
670 fprintf (stderr, "No XFixes extension\n");
675 XCompositeRedirectSubwindows (dpy, root, CompositeRedirectManual);
677 XSelectInput (dpy, root, SubstructureNotifyMask|ExposureMask);
678 XQueryTree (dpy, root, &root_return, &parent_return, &children, &nchildren);
679 for (i = 0; i < nchildren; i++)
680 add_win (dpy, children[i], i ? children[i-1] : None);
687 XNextEvent (dpy, &ev);
688 /* printf ("event %d\n", ev.type); */
691 add_win (dpy, ev.xcreatewindow.window, 0);
693 case ConfigureNotify:
694 configure_win (dpy, &ev.xconfigure);
697 destroy_win (dpy, ev.xdestroywindow.window, True);
700 map_win (dpy, ev.xmap.window);
703 unmap_win (dpy, ev.xunmap.window);
706 if (ev.xreparent.parent == root)
707 add_win (dpy, ev.xreparent.window, 0);
709 destroy_win (dpy, ev.xreparent.window, False);
712 if (ev.xexpose.window == root)
714 int more = ev.xexpose.count + 1;
715 if (n_expose == size_expose)
719 expose_rects = realloc (expose_rects,
720 (size_expose + more) *
721 sizeof (XRectangle));
726 expose_rects = malloc (more * sizeof (XRectangle));
730 expose_rects[n_expose].x = ev.xexpose.x;
731 expose_rects[n_expose].y = ev.xexpose.y;
732 expose_rects[n_expose].width = ev.xexpose.width;
733 expose_rects[n_expose].height = ev.xexpose.height;
735 if (ev.xexpose.count == 0)
737 expose_root (dpy, root, expose_rects, n_expose);
743 if (ev.type == damage_event + XDamageNotify)
744 damage_win (dpy, (XDamageNotifyEvent *) &ev);
747 } while (XEventsQueued (dpy, QueuedAfterReading));
750 paint_all (dpy, allDamage);