9 #include <X11/extensions/shape.h>
10 #include <X11/extensions/render.h>
11 #include <X11/Xft/Xft.h>
37 main(int argc, char **argv)
41 struct osd_state osd_static;
42 bzero(&osd_static, sizeof(osd_static));
43 struct osd_state *osd = &osd_static;
45 osd->dpy = XOpenDisplay(NULL);
47 die("Cannot open display");
49 int event_basep, error_basep;
50 if (!XShapeQueryExtension(osd->dpy, &event_basep, &error_basep))
51 die("XShape extension not supported by X server, giving up");
53 osd->screen = XDefaultScreen(osd->dpy);
54 osd->visual = XDefaultVisual(osd->dpy, osd->screen);
55 osd->depth = XDefaultDepth(osd->dpy, osd->screen);
56 osd->cmap = DefaultColormap(osd->dpy, osd->screen);
58 // These can change. And what about Xinerama?
59 osd->screen_width = XDisplayWidth(osd->dpy, osd->screen);
60 osd->screen_height = XDisplayHeight(osd->dpy, osd->screen);
62 XSetWindowAttributes win_attr = {
63 .override_redirect = 1,
65 osd->win = XCreateWindow(osd->dpy,
66 XRootWindow(osd->dpy, osd->screen),
68 osd->screen_width, osd->screen_height,
75 XStoreName(osd->dpy, osd->win, "OSD");
78 osd->mask_bitmap = XCreatePixmap(osd->dpy, osd->win, osd->screen_width, osd->screen_height, 1);
79 osd->image_pixmap = XCreatePixmap(osd->dpy, osd->win, osd->screen_width, osd->screen_height, osd->depth);
80 DBG("depth = %d\n", osd->depth);
83 .graphics_exposures = 0,
85 osd->gc = XCreateGC(osd->dpy, osd->win, GCGraphicsExposures, &gcv);
86 osd->mask_gc = XCreateGC(osd->dpy, osd->mask_bitmap, GCGraphicsExposures, &gcv);
88 XSetBackground(osd->dpy, osd->gc, BlackPixel(osd->dpy, osd->screen));
89 XColor shadow_color = { .red = 0xffff, .green = 0xffff, .blue = 0 };
90 XAllocColor(osd->dpy, osd->cmap, &shadow_color);
91 XSetForeground(osd->dpy, osd->gc, shadow_color.pixel);
93 XSetBackground(osd->dpy, osd->mask_gc, WhitePixel(osd->dpy, osd->screen));
94 XSetForeground(osd->dpy, osd->mask_gc, BlackPixel(osd->dpy, osd->screen));
98 XFillRectangle(osd->dpy, osd->image_pixmap, osd->gc, 0, 0, osd->screen_width, osd->screen_height);
100 XFillRectangle(osd->dpy, osd->mask_bitmap, osd->mask_gc, 0, 0, osd->screen_width, osd->screen_height);
102 osd->font = XftFontOpenName(osd->dpy, osd->screen, "times-64");
104 die("Cannot open font");
105 DBG("Font: asc=%d desc=%d ht=%d", osd->font->ascent, osd->font->descent, osd->font->height);
107 osd->mask_draw = XftDrawCreateBitmap(osd->dpy, osd->mask_bitmap);
109 die("Cannot create XftDraw");
111 osd->image_draw = XftDrawCreate(osd->dpy, osd->image_pixmap, osd->visual, osd->cmap);
112 if (!osd->image_draw)
113 die("Cannot create XftDraw");
115 XRenderColor xrc = { .red = 0, .green = 0xffff, .blue = 0, .alpha = 0xffff };
117 if (!XftColorAllocValue(osd->dpy, osd->visual, osd->cmap, &xrc, &xfc))
118 die("XftColorAllocValue failed");
119 const unsigned char str[] = "Žluťoučká vlkodlačice";
120 XftDrawStringUtf8(osd->image_draw, &xfc, osd->font, 100, 100, str, strlen((char *) str));
122 XRenderColor xrcm = { .red = 0xffff, .green = 0xffff, .blue = 0xffff, .alpha = 0xffff };
124 if (!XftColorAllocValue(osd->dpy, osd->visual, osd->cmap, &xrcm, &xfcm))
125 die("XftColorAllocValue failed");
127 // This is slow, but unlike the method used by libxosd, the result isn't ugly.
129 for (int dx = -outline; dx <= outline; dx++)
130 for (int dy = -outline; dy <= outline; dy++)
131 if (dx*dx + dy*dy <= outline*outline)
132 XftDrawStringUtf8(osd->mask_draw, &xfcm, osd->font, 100 + dx, 100 + dy, str, strlen((char *) str));
136 XftTextExtentsUtf8(osd->dpy, osd->font, str, strlen((char *) str), &gi);
137 DBG("Glyph info: (%d,%d)+(%d,%d) off (%d,%d)\n", gi.x, gi.y, gi.width, gi.height, gi.xOff, gi.yOff);
138 XftDrawRect(osd->image_draw, &xfc, 100 + gi.x, 100 - gi.y, gi.width, gi.height);
141 XShapeCombineMask(osd->dpy, osd->win, ShapeBounding, 0, 0, osd->mask_bitmap, ShapeSet);
143 XSelectInput(osd->dpy, osd->win, ExposureMask);
144 XMapRaised(osd->dpy, osd->win);
147 struct pollfd pfd = {
148 .fd = ConnectionNumber(osd->dpy),
154 timestamp_t now = get_current_time();
155 timestamp_t wait_until = now - 1;
157 DBG("... waiting for %d ms\n", (int)(wait_until - now));
158 poll(&pfd, 1, wait_until - now);
159 if (pfd.revents & POLLIN)
161 while (XPending(osd->dpy))
164 XNextEvent(osd->dpy, &ev);
169 XExposeEvent *ex = &ev.xexpose;
170 DBG("Expose cnt=%d (%d,%d)+(%d,%d)\n", ex->count, ex->x, ex->y, ex->width, ex->height);
171 XCopyArea(osd->dpy, osd->image_pixmap, osd->win, osd->gc, ex->x, ex->y, ex->width, ex->height, ex->x, ex->y);
175 DBG("Event %d\n", ev.type);