aboutsummaryrefslogtreecommitdiff
path: root/mstatus.c
blob: e6b081d13f8c2909fdcba0f03bad9425d7f10f32 (plain) (blame)
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/stat.h>

#include <ctype.h>
#include <errno.h>
#include <linux/limits.h>
#include <paths.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdnoreturn.h>
#include <signal.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>

#include <X11/Xlib.h>

#define CTOI(x) ((x) ^ 48)

struct Block {
	int pos;
	char *text;
	bool remove;
};

bool rflag;
const char *argv0;
struct {
	char *str;
	size_t len;
} seperator = {.str = " | ", .len = 3};

static noreturn void
usage(void)
{
	fprintf(stderr, "Usage: %s [-r] [-s seperator]\n", argv0);
	exit(EXIT_FAILURE);
}

static noreturn void
die(const char *s)
{
	char *err = strerror(errno);
	syslog(LOG_ERR, "%s: %s", s, err);
	fprintf(stderr, "%s: %s: %s\n", argv0, s, err);
	exit(EXIT_FAILURE);
}

static void
xfork(void)
{
	pid_t pid = fork();
	if (pid == -1)
		die("fork");
	if (pid != 0)
		exit(EXIT_SUCCESS);
}

static void *
xrealloc(void *ptr, size_t size)
{
	void *ret;
	if (!(ret = realloc(ptr, size)))
		die("realloc");
	return ret;
}

static void
xfree(char **ptr)
{
	free(*ptr);
	*ptr = NULL;
}

static void
write_status(struct Block b)
{
	static struct {
		char **blocks;
		int count;
		size_t length;
	} sb;

	if (b.remove) {
		if (b.pos > sb.count)
			return;

		/* b.remove && !b.pos is a special case to remove everything from the bar */
		if (!b.pos) {
			for (int i = 0; i < sb.count; i++)
				free(sb.blocks[i]);
			sb.count = 0;
			sb.blocks = xrealloc(sb.blocks, sizeof(char *));
			goto update_bar;
		}

		/* If the block is not NULL we free it */
		if (sb.blocks[--b.pos]) {
			sb.length -= strlen(sb.blocks[b.pos]);
			xfree(&sb.blocks[b.pos]);
		}

		/* If the block is the last one, we resize the bar to remove trailing NULL blocks */
		if (b.pos + 1 == sb.count) {
			for (; !sb.blocks[b.pos] && b.pos; b.pos--)
				sb.count--;
			sb.blocks = xrealloc(sb.blocks, sizeof(char *) * ++b.pos);
		}
		goto update_bar;
	}

	/* If the position exceeds the space allocated, allocate more blocks */
	if (b.pos > sb.count) {
		sb.blocks = xrealloc(sb.blocks, sizeof(char *) * b.pos);
		/* Make sure to set all the newly allocated blocks to NULL */
		for (int i = sb.count; i < b.pos; i++)
			sb.blocks[i] = NULL;
		sb.count = b.pos;
	}

	/* If the block is NULL we dont need to bother with strlen and free */
	if (sb.blocks[--b.pos]) {
		sb.length -= strlen(sb.blocks[b.pos]);
		xfree(&sb.blocks[b.pos]);
	}
	if (!(sb.blocks[b.pos] = strdup(b.text)))
		die("strdup");
	sb.length += strlen(b.text);

	/*
	 * The buffer to store the text that will be displayed in. It needs space for the text, the
	 * seperators between the different blocks, the NUL byte at the end, and the right padding
	 * space.
	 */
update_bar:;
	char buf[sb.length + (sb.count - 1) * seperator.len + 2];
	memset(buf, '\0', sb.length + 1);

	/* Double for loops so that the seperator isnt printed to the left of the first block */
	int i;
	for (i = 0; i < sb.count; i++) {
		if (sb.blocks[i]) {
			strcpy(buf, sb.blocks[i]);
			break;
		}
	}
	for (i++; i < sb.count; i++)
		if (sb.blocks[i])
			sprintf(buf, "%s%s%s", buf, seperator.str, sb.blocks[i]);
	if (rflag)
		strcat(buf, " ");

	/* Xlib magic to set the DWM status */
	Display *dpy = XOpenDisplay(NULL);
	int screen = DefaultScreen(dpy);
	Window root = RootWindow(dpy, screen);
	(void) XStoreName(dpy, root, buf);
	(void) XCloseDisplay(dpy);
}

static bool
process(char *line, ssize_t len, struct Block *b)
{
	/* For some reason output with newlines can cause performance issues */
	if (line[--len] == '\n')
		line[len] = '\0';

	if (*line == '-') {
		b->remove = true;
		line++;
	}
	else
		b->remove = false;

	if (!isdigit(*line))
		b->pos = 1; /* Default position */
	else {
		b->pos = 0;
		while (isdigit(*line))
			b->pos = b->pos * 10 + CTOI(*line++);
		if (!b->pos && !b->remove)
			return false;
	}

	b->text = line;
	return true;
}

static void
create_fifo(char *fifo_path)
{
	char *runtime_dir = getenv("XDG_RUNTIME_DIR");
	if (runtime_dir) {
		size_t end = strlen(runtime_dir) - 1;
		if (runtime_dir[end] == '/')
			runtime_dir[end] = '\0';
		sprintf(fifo_path, "%s/%s.pipe", runtime_dir, argv0);
	}
	else
		sprintf(fifo_path, _PATH_VARRUN "user/%d/%s.pipe", getuid(), argv0);

	umask(0);

create_fifo:
	if (mkfifo(fifo_path, DEFFILEMODE) == -1) {
		if (errno == EEXIST) {
			if (unlink(fifo_path) == -1)
				die("unlink");
			goto create_fifo;
		}
		else
			die("mkfifo");
	}

	syslog(LOG_INFO, "Created input FIFO '%s'", fifo_path);
}

static void
daemonize(void)
{
	xfork();
	if (setsid() == -1)
		die("setsid");

	(void) signal(SIGCHLD, SIG_IGN);
	xfork();

	(void) chdir("/");
	(void) close(STDIN_FILENO);
	(void) close(STDOUT_FILENO);
	(void) close(STDERR_FILENO);

	stdin = fopen(_PATH_DEVNULL, "r");
	stdout = fopen(_PATH_DEVNULL, "w+");
	stderr = fopen(_PATH_DEVNULL, "w+");

	syslog(LOG_INFO, "Daemonized '%s'", argv0);
}

int
main(int argc, char **argv)
{
	(void) argc;
	argv0 = argv[0];

	int opt;
	while ((opt = getopt(argc, argv, ":rs:")) != -1) {
		switch (opt) {
		case 'r':
			rflag = true;
			break;
		case 's':
			seperator.str = optarg;
			seperator.len = strlen(optarg);
			break;
		default:
			usage();
		}
	}

	openlog(argv0, LOG_PID | LOG_CONS, LOG_DAEMON);
	char fifo_path[PATH_MAX];
	create_fifo(fifo_path);
	daemonize();

	char *line = NULL;
	size_t len = 0;
	while (true) {
		FILE *fp;
		if (!(fp = fopen(fifo_path, "r")))
			die("fopen");

		ssize_t nr;
		while ((nr = getline(&line, &len, fp)) != -1) {
			syslog(LOG_INFO, "Recieved command '%s'", line);
			struct Block b;
			if (!process(line, nr, &b))
				continue;
			write_status(b);
		}
		if (ferror(fp))
			die("getline");

		(void) fclose(fp);
	}
	/* NOTREACHED */
}