Question

This C function can be used to disable or enable windows decorations in many window managers. If 'mode' is 'd' the window will hide the decorations, otherwise if the 'mode' is 'D' the window will show them.

void window_tune_decorations(Display *disp, Window win, char mode) {
    long hints[5] = { 2, 0, 0, 0, 0};
    Atom motif_hints = XInternAtom(disp, "_MOTIF_WM_HINTS", False);

    switch (mode) {
        case 'D':
            hints[2] = 1;
            /* fall through */
        case 'd':
            XChangeProperty(disp, win, motif_hints, motif_hints, 32, PropModeReplace, (unsigned char *)hints, 5);
            break;
        default:
            fputs("Invalid mode.\n", stderr);
    }
}

I would like to implement a ``toggle mode''. So my question is, there a way to detect if a windows has the decorations? I tried using XGetWindowProperty with _MOTIF_WM_HINTS, but I am not sure how to interpret the output.

Was it helpful?

Solution

You interpret the data you get from XGetWindowProperty the same way you interpret data sent to XChangeProperty.

In the case of _MOTIF_WM_HINTS it's an array of 5 longs, or perhaps the struct MwmHints (syn. MotifWmHints). It's a struct of 5 long fields, plus several #defined bit flags. It is inherited from the Motif window manager, but we don't usually keep Motif includes and libraries around nowadays, so the struct gets copied to various places (bad practice but everyonee is doing it). You may find its definition in xprops.h of Gnome and several other places. Look it up on the 'net and copy to your code, or find it in an include file you already depend on, or just look at the definition and keep using the array of 5 longs, your choice.

You need to check the right flags in the right fields. For decorations, check if the window is override-redirect first. If it is, it is undecorated (obviously) and you cannot add any decorations. If the window manager is not running, it's undecorated as well, and you cannot add any decorations in this case too.

Otherwise, if the window does not have the property at all (XGetWindowProperty sets type to None), you may assume it's decorated.

If it does have the property, and MWM_HINTS_DECORATIONS bit is set in flags, then it has exactly the decorations specified in the decorations field by the MWM_DECOR_* bit values. If the field is non-zero, there are some decorations present. AFAIK if MWM_HINTS_DECORATIONS is unset, then the window is (surprisingly) decorated. But please test this yourself, I don't remember and don't have an X11 machine around at the moment so I can't check it.

Naturally, some window managers don't use _MOTIF_WM_HINTS (e.g. ones that were around before Motif). If you have one of those, you cannot check or set decorations with this method.

Don't forget to XFree(hints).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top