11

Is there a way to create a window with Xlib which only display the border lines, title, close button and that you can move with the mouse? The content of the window must be empty (or "totally transparent", although "transparency" sounds more like an effect I don't need). Basically the window should show the background area.

1 Answer 1

20

I'm not sure if it is what you want, but following code creates an X window with transparent background but still using the window decoration of your window manager.

It will only work though if your X11 and graphics hardware configuration supports visuals with a depth of 32 bit.

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

int main(int argc, char* argv[])
{
    Display* display = XOpenDisplay(NULL);

    XVisualInfo vinfo;
    XMatchVisualInfo(display, DefaultScreen(display), 32, TrueColor, &vinfo);

    XSetWindowAttributes attr;
    attr.colormap = XCreateColormap(display, DefaultRootWindow(display), vinfo.visual, AllocNone);
    attr.border_pixel = 0;
    attr.background_pixel = 0;

    Window win = XCreateWindow(display, DefaultRootWindow(display), 0, 0, 300, 200, 0, vinfo.depth, InputOutput, vinfo.visual, CWColormap | CWBorderPixel | CWBackPixel, &attr);
    XSelectInput(display, win, StructureNotifyMask);
    GC gc = XCreateGC(display, win, 0, 0);

    Atom wm_delete_window = XInternAtom(display, "WM_DELETE_WINDOW", 0);
    XSetWMProtocols(display, win, &wm_delete_window, 1);

    XMapWindow(display, win);

    int keep_running = 1;
    XEvent event;

    while (keep_running) {
        XNextEvent(display, &event);

        switch(event.type) {
            case ClientMessage:
                if (event.xclient.message_type == XInternAtom(display, "WM_PROTOCOLS", 1) && (Atom)event.xclient.data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", 1))
                    keep_running = 0;

                break;

            default:
                break;
        }
    }

    XDestroyWindow(display, win);
    XCloseDisplay(display);
    return 0;
}
3
  • I tried this code on Raspberry Pi3.. XMatchVisualInfo returns 1, so no error and visual was found. But no transparency. What can it be? On PC with ubuntu works with transparency fine.
    – nckm
    Mar 17, 2020 at 8:48
  • My system supports transparent windows (using transset from a terminal applies transparency to any window), yet the root depth on my system appears to be only 24. Is setting a depth of 32 really required?
    – matanster
    Jun 2, 2020 at 12:30
  • 3
    Besides hardware and X, it is necessary to have a screen compositor, such as xcompmgr, compton, etc. running in the background. Lubuntu does not run this by default; install one to run upon startup. You can usually tell if your OS is already running a compositor if the edge of your windows has funky dropshadows shading what's underneath, or if your windows fly in from the side. xcompmgr has been recommended by others, as being lightweight.
    – DragonLord
    Nov 19, 2020 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.