Gtk+/X11: semitransparent windows with and without

2019-07-25 09:44发布

问题:

I need some code for making my window (and preferably all widgets on it) semitransparent. I know i can play around with gtk_window_set_opacity(), but it works only when composite manager is running, but what if not? I've googled a lot, found lots of code that mostly doesn't even compile, doesn't work or just a proof of concept. No fulfilling solution. I don't want to mess with X11 Xlib awful API (I just don't have time to learn it).

Where to get such library/code snippet?

回答1:

There's no good answer to this (which is a good part of why compositing managers were invented). If you could already do this, people wouldn't have invented the whole compositing manager mechanism.

The only sort-of answer, used in old "transparent terminals" and the like, is based on making screenshots of the stuff underneath the window and then painting the screenshot in your own window. This is an Xlib-involving mess, hard to get mostly right, impossible to get completely correct, and inefficient. Still, you could do it perhaps. Look at old revisions of terminals supporting transparency, I think VTE used to have this code, ZVT widget certainly did. So did the Enlightenment terminal for example.

But really the way to go is to just fall back to no transparency for users without a CM.



回答2:

While modern X11 servers do support RGBA visuals this doesn't mean, they'll do alpha blending. X11 operates on the model, that a window is a mask on a single shared framebuffer. Z ordering may clip parts of a window so these areas are not drawn to at all.

To enable transparency a compositing manager must redirect the windows to off-screen rendering, then compose the final image you see on the screen from those off-screen rendered parts. The XDamage extension is used to keep track of which windows need re-compositing.