Posted to tcl by patthoyts at Tue Sep 19 22:21:05 GMT 2006view pretty

/* whatwm.c -
 *
 * Print the window manager name.
 *
 * See:
 *   http://standards.freedesktop.org/wm-spec/wm-spec-latest.html#id2510530
 */

#include <X11/Xlib.h>
#include <stdio.h>

int
main(int argc, char *const argv[])
{
    Display *display;
    Atom aWmCheck, aWmName, aType;
    Window *windowPtr = NULL;
    int format = 0;
    unsigned long cReturned = 0, cbReturned = 0;
    char *name = NULL;

    display = XOpenDisplay(NULL);
    if (display == NULL) {
        printf("failed to connect to X display\n");
        return 1;
    }

    aWmCheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
    aWmName = XInternAtom(display, "_NET_WM_NAME", False);
    XGetWindowProperty(display, DefaultRootWindow(display),
                       aWmCheck, 0, 8, False, AnyPropertyType,
                       &aType, &format, &cReturned,
                       &cbReturned, (unsigned char **)&windowPtr);
    if (cReturned < 1) {
        printf("wm name not supported\n");
    } else {
        Window *childPtr = NULL;
        XGetWindowProperty(display, *windowPtr,
                           aWmCheck, 0, 8, False, AnyPropertyType,
                           &aType, &format, &cReturned,
                           &cbReturned, (unsigned char **)&childPtr);
        if (cReturned < 1) {
            printf("no supporting window manager available\n");
        } else {
            XGetWindowProperty(display, *windowPtr,
                               aWmName, 0, 128, False, AnyPropertyType,
                               &aType, &format, &cReturned,
                               &cbReturned, (unsigned char **)&name);
            if (cReturned == 0) {
                printf("no wm name provided\n");
            } else {
                printf("%s\n", name);
                XFree(name);
            }
            XFree(childPtr);
        }
        XFree(windowPtr);
    }
    XCloseDisplay(display);
    return 0;
}