aboutsummaryrefslogtreecommitdiffstats
path: root/urgent.c
blob: c74192f049636d73bc954d10b96f883c11da44ab (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
/*
 * $Id$
 *
 * Compile with
 *     gcc -lX11 -o urgent urgent.c
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>

typedef long (*FLAGMANIP) (long);

static long set_urgency(long flags)
{
	return flags | XUrgencyHint;
}

static long clear_urgency(long flags)
{
	return flags & ~XUrgencyHint;
}

static long toggle_urgency(long flags)
{
	if ((flags & XUrgencyHint) == 0) {
		return set_urgency(flags);
	} else {
		return clear_urgency(flags);
	}
}

static int apply(Display *dpy, Window id, FLAGMANIP f)
{
	XWMHints *hints = XGetWMHints(dpy, id);

	if (hints == NULL)
		return 0;

	hints->flags = f(hints->flags);

	return (XSetWMHints(dpy, id, hints) != 0);
}

int main(int argc, char *argv[])
{
	const char helpstr[] = "Usage: urgent {-set|-clear|-toggle} XWINID\n";
	Display *dpy;
	Window id;
	FLAGMANIP action;

	if (argc == 2 && strcmp(argv[1], "-help") == 0) {
		printf("Sets, clears or toggles the \"urgency\" flag "
			"for a specified X window using its WM_HINTS property\n");
		printf(helpstr);
		return EXIT_SUCCESS;
	}

	if (argc != 3) {
		fprintf(stderr, "Wrong number of agruments\n");
		fprintf(stderr, helpstr);
		return EXIT_FAILURE;
	}

	if (strcmp(argv[1], "-set") == 0) {
		action = set_urgency;
	} else if (strcmp(argv[1], "-clear") == 0) {
		action = clear_urgency;
	} else if (strcmp(argv[1], "-toggle") == 0) {
		action = toggle_urgency;
	} else {
		fprintf(stderr, "Invalid action \"%s\", "
			"must be one of -set, -clear or -toggle\n", argv[1]);
		return EXIT_FAILURE;
	}

	errno = 0;
	id = strtol(argv[2], NULL, 16);
	if (errno != 0) {
		fprintf(stderr, "Invalid X window id \"%s\", "
			"must be a hexadecimal integer\n", argv[2]);
		return EXIT_FAILURE;
	}

	dpy = XOpenDisplay(NULL);

	if (dpy == NULL) {
		fprintf(stderr, "Unable to open display.");
		return EXIT_FAILURE;
	}

	apply(dpy, id, action);

	XFlush(dpy);

	XCloseDisplay(dpy);

	return EXIT_SUCCESS;
}