all repos — barito @ 42b4968b290ae85709706782813825fb6a32c490

an x11 bar

barito.c (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>
#include <X11/Xft/Xft.h>
#include "config.h"

static Display *dpy;
static Visual *visual;
static Window root;
static Colormap colormap;
static int depth;
static int screen;

static unsigned long alloc_color(const char *s)
{
	XColor color;
	if (!XAllocNamedColor(dpy, colormap, s, &color, &color))
		errx(1, "cannot allocate color");

	return color.pixel;
}

static Window create_win(int x, int y, int w, int h)
{
	XSetWindowAttributes swa;
	Window win;

	swa.background_pixel = alloc_color(bg);
	swa.override_redirect = 1;
	swa.event_mask = ExposureMask;
	win = XCreateWindow(dpy, root, x, y, w, h, 0, CopyFromParent,
			    CopyFromParent, CopyFromParent,
			    CWBackPixel | CWOverrideRedirect | CWEventMask,
			    &swa);

	return win;
}

static void init_atoms(Window win)
{
	/* set up atoms in the worst possibly way */
	Atom dock = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DOCK", 0);
	Atom win_type = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", 0);
	XChangeProperty(dpy, win, win_type, XA_ATOM, 32, PropModeReplace,
			(unsigned char *)&dock, 1);

	Atom win_state = XInternAtom(dpy, "_NET_WM_STATE", 0);
	Atom sticky = XInternAtom(dpy, "_NET_WM_STATE_STICKY", 0);
	XChangeProperty(dpy, win, win_state, XA_ATOM, 32, PropModeReplace,
			(unsigned char *)&sticky, 1);

	Atom above = XInternAtom(dpy, "_NET_WM_STATE_ABOVE", 0);
	XChangeProperty(dpy, win, win_state, XA_ATOM, 32, PropModeReplace,
			(unsigned char *)&above, 1);

	Atom wm_name = XInternAtom(dpy, "_NET_WM_NAME", 0);
	Atom utf8string = XInternAtom(dpy, "UTF8_STRING", 0);
	char *name = "icybar";
	XChangeProperty(dpy, win, wm_name, utf8string, 8, PropModeReplace,
			(unsigned char *)name, strlen(name));

	unsigned long prop = 0xFFFFFFFF;
	Atom desktop = XInternAtom(dpy, "_NET_WM_DESKTOP", 0);
	XChangeProperty(dpy, win, desktop, XA_CARDINAL, 32, PropModeReplace,
			(unsigned char *)&prop, 1);
}

static Pixmap create_pixmap(GC gc, int w, int h)
{
	Pixmap pix;

	pix = XCreatePixmap(dpy, root, w, h, depth);

	XSetForeground(dpy, gc, alloc_color(bg));
	XFillRectangle(dpy, pix, gc, 0, 0, w, h);
	XSetForeground(dpy, gc, alloc_color(bg));

	return pix;
}

/* allocate color for Xft */
static void xft_alloc_color(const char *colorname, XftColor *color)
{
	if (!XftColorAllocName(dpy, visual, colormap, colorname, color))
		errx(1, "cannot allocate color");
}

/* draw text on pixmap */
static void draw(Drawable pix, XftFont *font, XftColor *color, const char *text)
{
	XGlyphInfo ext;
	XftDraw *draw;
	int x, y;
	size_t len;

	draw = XftDrawCreate(dpy, pix, visual, colormap);

	len = strlen(text);

	XftTextExtentsUtf8(dpy, font, text, len, &ext);
	x = (WIDTH - ext.width) / 2;
	y = (HEIGHT + ext.height) / 2;

	XftDrawStringUtf8(draw, color, font, x, y, text, len);

	XftDrawDestroy(draw);
}

/* enter the event loop */
static void run(GC gc, Pixmap pix)
{
	XEvent ev;

	while (XNextEvent(dpy, &ev) == 0) {
		switch (ev.type) {
		case Expose:
			XCopyArea(dpy, pix, ev.xexpose.window, gc, ev.xexpose.x,
				  ev.xexpose.y, ev.xexpose.width,
				  ev.xexpose.height, ev.xexpose.x,
				  ev.xexpose.y);
			break;
		}
	}
}

int main(int argc, char *argv[])
{
	XftColor color;
	XftFont *font;
	Pixmap pix;
	Window win;
	GC gc;

	if (argc != 2) {
		fprintf(stderr, "usage: %s STRING\n", argv[0]);
		return 1;
	}

	if ((dpy = XOpenDisplay(NULL)) == NULL)
		errx(1, "could not open display");
	screen = DefaultScreen(dpy);
	colormap = DefaultColormap(dpy, screen);
	visual = DefaultVisual(dpy, screen);
	depth = DefaultDepth(dpy, screen);
	root = RootWindow(dpy, screen);

	win = create_win(0, 0, WIDTH, HEIGHT);
	gc = XCreateGC(dpy, root, 0, NULL);
	pix = create_pixmap(gc, WIDTH, HEIGHT);
	font = XftFontOpenName(dpy, screen, fontname);
	xft_alloc_color(fg, &color);

	init_atoms(win);

	draw(pix, font, &color, argv[1]);
	XMapWindow(dpy, win);
	run(gc, pix);

	/* housekeeping */
	XUnmapWindow(dpy, win);
	XDestroyWindow(dpy, win);
	XFreePixmap(dpy, pix);
	XFreeGC(dpy, gc);
	XftColorFree(dpy, visual, colormap, &color);
	XftFontClose(dpy, font);
	XCloseDisplay(dpy);

	return 0;
}