all repos — barito @ 71126fd27598d983afa58672a599e6b21115856e

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
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <poll.h>
#include <unistd.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_text(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 (e.type) {
// 		case Expose:
// 			printf("%d %d", e.xexpose.x, e.xexpose.y);
// 			XCopyArea(dpy, pix, e.xexpose.window, gc, e.xexpose.x,
// 				  e.xexpose.y, e.xexpose.width,
// 				  e.xexpose.height, e.xexpose.x,
// 				  e.xexpose.y);
// 			break;
// 		}
// 	}

// }

void draw_bar(Window win, GC gc, Pixmap pix, XftFont *font, XftColor *color,
	      const char *text, XExposeEvent xexpose)
{
	draw_text(pix, font, color, text);
	XMapWindow(dpy, win);
	XCopyArea(dpy, pix, win, gc, xexpose.x, xexpose.y, xexpose.width,
		  xexpose.height, xexpose.x, xexpose.y);
}

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

	/* read line from stdin */
	char buffer[BUFSIZ];
	int reading = 1;
	int flags; /* stdin flags */

	struct pollfd pfd;
	pfd.fd = STDIN_FILENO;
	pfd.events = POLLIN;

	/* make stdin non-blocking */
	if ((flags = fcntl(STDIN_FILENO, F_GETFL)) == -1)
		errx(1, "cannot get status flags");
	if (fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK) == -1)
		errx(1, "cannot set flags on stdin");

	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);

	/* wait for expose event to draw our bar */
	while (1) {
		XEvent e;
		XNextEvent(dpy, &e);
		if (e.type == Expose) {
			xexpose = e.xexpose;
			break;
		}
	}

	while (reading) {
		if (pfd.revents & POLLHUP) {
			pfd.fd = -1;
			reading = 0;
		}
		if (poll(&pfd, 1, -1)) {
			if (pfd.revents & POLLIN) {
				if (fgets(buffer, sizeof buffer, stdin) == NULL)
					break;

				draw_bar(win, gc, pix, font, &color, buffer,
					 xexpose);
			}
		}
	}

	/* 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;
}