wmname

sets/prints window manager name similiar to hostname(1)
git clone git://git.suckless.org/wmname
Log | Files | Refs | README | LICENSE

wmname.c (1499B)


      1 /* See LICENSE file for details. */
      2 #include <stdarg.h>
      3 #include <stdlib.h>
      4 #include <stdio.h>
      5 #include <string.h>
      6 #include <X11/Xlib.h>
      7 #include <X11/Xatom.h>
      8 #include <X11/Xutil.h>
      9 
     10 void
     11 eprint(const char *errstr, ...) {
     12 	va_list ap;
     13 
     14 	va_start(ap, errstr);
     15 	vfprintf(stderr, errstr, ap);
     16 	va_end(ap);
     17 	exit(EXIT_FAILURE);
     18 }
     19 
     20 int
     21 main(int argc, char **argv) {
     22 	int status, format;
     23 	unsigned char *data = NULL;
     24 	unsigned long n, extra;
     25 	Display *dpy;
     26 	Window root;
     27 	Atom netwmcheck, netwmname, utf8_string, real;
     28 
     29 	if(argc > 2)
     30 		eprint("usage: wmname [name] [-v]\n");
     31 	else if(argc == 2 && !strncmp(argv[1], "-v", 3))
     32 		eprint("wmname-"VERSION", © 2008-2013 Anselm R Garbe\n", stdout);
     33 
     34 	if(!(dpy = XOpenDisplay(0)))
     35 		eprint("wmname: cannot open display\n");
     36 	root = DefaultRootWindow(dpy);
     37 	netwmcheck = XInternAtom(dpy, "_NET_SUPPORTING_WM_CHECK", False);
     38 	netwmname = XInternAtom(dpy, "_NET_WM_NAME", False);
     39 	utf8_string = XInternAtom(dpy, "UTF8_STRING", False);
     40 	if(argc == 1) {
     41 		status = XGetWindowProperty(dpy, root, netwmname, 0L, 32L, False, utf8_string, &real, &format, &n, &extra, (unsigned char **) &data);
     42 		if(status == Success && data != NULL)
     43 			fprintf(stdout, "%s\n", data);
     44 		XFree(data);
     45 	}
     46 	else {
     47 		XChangeProperty(dpy, root, netwmcheck, XA_WINDOW, 32, PropModeReplace, (unsigned char *)&root, 1);
     48 		XChangeProperty(dpy, root, netwmname, utf8_string, 8, PropModeReplace, (unsigned char *)argv[1], strlen(argv[1]));
     49 	}
     50 	XSync(dpy, False);
     51 	XCloseDisplay(dpy);
     52 	return 0;
     53 }