aboutsummaryrefslogblamecommitdiffstats
path: root/urgent.c
blob: 42f01ad03ca26426455179ffc27698515dab3543 (plain) (tree)






































































































                                                                                  
/*
 * $Id$
 *
 * Compile with
 *     gcc -L/usr/X11R6/lib -lX11 urgent.c -o urgent
 */

#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;
}