4

I use the following code to get a transparent window, but it returns black.What's wrong with me? And, can anybody give me a simple example to create a window with transparent background?THANKS!

#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;
}
lidedongsn
  • 125
  • 2
  • 12

1 Answers1

1

Your code works fine for me:

kde:

enter image description here

openbox + xcompmgr:

enter image description here

Most likely you are not running composite manager. Try to start xcompmgr command

Also check _NET_WM_CM_S0 selection owner - it should point to a window created by composite manager.

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

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

    Atom cmAtom = XInternAtom(display, "_NET_WM_CM_S0", 0);
    Window cmOwner = XGetSelectionOwner(display, cmAtom);

    printf("Composite manager window: %i\n", cmOwner);

    XCloseDisplay(display);
    return 0;
}

Update:

Try to set override-redirect to prevent WM decorations from obscuring your window.

attr.border_pixel = 0;
attr.background_pixel = 0;
attr.override_redirect = 1; /* this line added */

Window win = XCreateWindow(display, DefaultRootWindow(display), 0, 0, 300, 200, 0, 
vinfo.depth, InputOutput, vinfo.visual, 
CWColormap | CWBorderPixel | CWBackPixel | CWOverrideRedirect /* and this one*/, &attr);
Andrey Sidorov
  • 24,905
  • 4
  • 62
  • 75
  • Thank you.You are so generous to anwser my questions. I run the xcompmgr already. The code you given print "Composite manager window: 0".Is this correct?what should I do next? – lidedongsn Apr 17 '14 at 06:06
  • "Composite manager window: 0" means that you are not running composite manager. Are you sure it is still running when you check selection owner? – Andrey Sidorov Apr 17 '14 at 06:11
  • this is what I have: `laplace@laplace-Mint ~ $ xcompmgr & [1] 6656 laplace@laplace-Mint ~ $ ./cmowner Composite manager window: 29360129` – Andrey Sidorov Apr 17 '14 at 06:12
  • Sorry, I forgot to run xcompmgr.Here is :# xcompmgr & # ./test Composite manager window: 16777217 ....But the window I create is still unsee .[Just as the comment I given](http://stackoverflow.com/questions/23051594/how-to-set-xlib-window-background-transpatrent/23052541?noredirect=1#comment35279989_23052541) – lidedongsn Apr 17 '14 at 06:38
  • which wm are you using? I just tried icewm (black window, no transparency) and fwvm (transparent window). My screenshot is under kde / kwm. If your wm reparents window to add title / borders and new parent window is not transparent you'll not get transparent window as a result – Andrey Sidorov Apr 17 '14 at 06:47
  • The backgroud is black! – lidedongsn Apr 17 '14 at 06:49
  • try to set override-redirect flag (see update in answer) – Andrey Sidorov Apr 17 '14 at 06:55
  • According to your suggest:# ./test Composite manager window: 25165825 vinfo.depth: 32 The background is black still! – lidedongsn Apr 17 '14 at 07:03
  • Is there any software will interference the xcompmgr&openbox?Any extra settings for xserver or something? – lidedongsn Apr 17 '14 at 08:13
  • I just used freshly installed openbox & xcompmgr for screenshot. No idea why it fails for you. What is result of `xdpyinfo | grep -i composite` on your box? – Andrey Sidorov Apr 17 '14 at 08:33
  • _italic_ **bold** `# xdpyinfo | grep -i composite Composite` yes, Composite extention is loaded.-_-" – lidedongsn Apr 17 '14 at 08:40
  • Hey,[I find a page just now](https://wiki.archlinux.org/index.php/xcompmgr)**hsetroot**,I set it.My window is steady.But it shows like a 24 bit window.No special phenomenon happend when I run xcompmgr.I want to say, there should be some extra settings affect my system.According to my thought, do you have any suggestions?Thank you in advance.I think you are an expert for xwindow somehow. – lidedongsn Apr 17 '14 at 10:01
  • I create a new thread for this http://stackoverflow.com/questions/23130581/how-to-use-openboxxcompmgr-correctly – lidedongsn Apr 17 '14 at 10:24
  • what does this mean:'"urxvt allows for true transparent background if you are using a compositing manager that supports that. I use xcompmgr with Openbox (no LXDE) which does true transparency. True, xterm will not do transparent backgrounds."' – lidedongsn Apr 18 '14 at 02:46
  • no idea. Likely means it tries to create 32-bit window and allows you to set background with alpha cjannel – Andrey Sidorov Apr 18 '14 at 03:29