Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[XLib] Trasnparent ARGB-window with opaque children

839 views
Skip to first unread message

Heinz-Mario Frühbeis

unread,
Mar 5, 2016, 1:09:04 PM3/5/16
to
Hi,

I'm searching for a way to create a ARGB-window that has its children
opaque.

That is how I create the parent-window:
XVisualInfo vinfo;
XSetWindowAttributes attr;
Window nSection;
XMatchVisualInfo(vDisplay, DefaultScreen(vDisplay), 32, TrueColor, &vinfo);
attr.colormap = XCreateColormap(vDisplay, vSectionRoot, vinfo.visual,
AllocNone);
attr.border_pixel = WhitePixel(dpy, screen);
attr.background_pixel = WhitePixel(dpy, screen);
nSection = XCreateWindow(vDisplay, vSectionRoot
, x, y, w, h, 0, vinfo.depth, InputOutput, vinfo.visual
, CWColormap | CWBorderPixel | CWBackPixel, &attr);

and with this I draw a shape to make the window transparent with an
opaque square:

int Create_XShape(int x, int y, unsigned int w, unsigned int h){
XWindowAttributes attr;
mSec_Values.width = mWidth;
mSec_Values.height = mHeight;
XConfigureWindow(mDisplay, mSectionA, CWWidth | CWHeight, &mSec_Values);
XGetWindowAttributes(mDisplay, mArea, &attr);
Pixmap p = XCreatePixmap(mDisplay, mSectionA, mWidth, mHeight, 1);
GC shape_gc = XCreateGC(mDisplay, p, 0, NULL);
XSetBackground(mDisplay, shape_gc, 0);
XSetForeground(mDisplay, shape_gc, 0);
XFillRectangle(mDisplay, p, shape_gc, x, y, w, h);
XSetForeground(mDisplay, shape_gc, mTransparent);
XFillRectangle(mDisplay, p, shape_gc, attr.x, attr.y, attr.width,
attr.height);
XShapeCombineMask(mDisplay, mSectionA, ShapeBounding, 0, 0, p, ShapeSet);
XFreePixmap(mDisplay, p);
XFreeGC(mDisplay, shape_gc);
XSetInputFocus(mDisplay, mSectionA, RevertToNone, CurrentTime);
return 1;
}

mSectionA is the parent window, child of defaultroot().
mTransparent is 255, 255, 255 aka whitepixel(dpy, screen).

When I do not map the child window I do have an opaque square on the
parent window, but as soon as I map the child window this part has an
opacity transparency, because the child window shall not have whitepixel
as backcolor. (The child window with WhitePixel is opaque...)

So, I'm searching a methode to have a transparent ARGB-window with
opaque children which have e.g. backcolor RGB(10, 100, 40).
Can someone here help me with this?

Greeting
Earlbyite

sp...@potato.field

unread,
Mar 7, 2016, 4:45:29 AM3/7/16
to
On Sat, 5 Mar 2016 19:09:00 +0100
=?UTF-8?Q?Heinz-Mario_Fr=c3=bchbeis?= <D...@Earlybite.individcore.de> wrote:
>Hi,
>
>I'm searching for a way to create a ARGB-window that has its children
>opaque.

I'm afraid I haven't got time to debug your code, but here's a test program
for the shape extension that I wrote some years ago which I think does what
you want. Hope it helps.


#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Intrinsic.h>
#include <X11/extensions/shape.h>

int main(int argc, char **argv)
{
Display *display;
Window rootwin;
Window win;
GC gc;
XGCValues gcvals;
XFontStruct *font;
XEvent event;
XRectangle rect[3];
int screen;
int a,b,c;
u_int black,white;
char *text="This demonstrates the use of shapes";

if (!(display=XOpenDisplay(NULL)))
{
fprintf(stderr,"XOpenDisplay(): Can't connect to: %s\n",
XDisplayName(NULL));
return 1;
}

/* See if the shape extension is available on this server */
if (!XQueryExtension(display,"SHAPE",&a,&b,&c))
{
printf("SHAPE extension not available on this X server.\n");
return 1;
}

screen = DefaultScreen(display);
rootwin = RootWindow(display,screen);
black = BlackPixel(display,screen);
white = WhitePixel(display,screen);

/* Make the window big enough to hold the rectangles. Use unmanaged
window if don't want WM stuff around edges */
win = XCreateSimpleWindow(display,rootwin,0,0,400,400,0,black,white);

font = XLoadQueryFont(display,"*9x15");
gcvals.font = font->fid;
gcvals.foreground = black;
gcvals.background = white;
gc = XCreateGC(display,win,GCForeground | GCBackground | GCFont,&gcvals)
;

/* Set up shape */
rect[0].x = 0;
rect[0].y = 0;
rect[0].width = 100;
rect[0].height = 100;
rect[1].x = 100;
rect[1].y = 90;
rect[1].width = 150;
rect[1].height = 100;
rect[2].x = 300;
rect[2].y = 50;
rect[2].width = 90;
rect[2].height = 100;

/* ShapeSet and ShapeSubtract are the most useful */
XShapeCombineRectangles(display,win,0,0,0,rect,3,ShapeSet,0);

XSelectInput(display,win,ExposureMask | PointerMotionMask);
XMapWindow(display,win);

while(1)
{
XNextEvent(display,&event);
XDrawLine(display,win,gc,0,0,500,400);
XDrawString(display,win,gc,0,105,text,strlen(text));
}
return 0;
}

--
Spud

Heinz-Mario Frühbeis

unread,
Mar 7, 2016, 7:52:05 AM3/7/16
to
Am 07.03.2016 um 10:45 schrieb sp...@potato.field:
> On Sat, 5 Mar 2016 19:09:00 +0100
> =?UTF-8?Q?Heinz-Mario_Fr=c3=bchbeis?= <D...@Earlybite.individcore.de> wrote:

Thank you for answering...

But I think it cannot help really.
Because and AFAIK has the default root window a depth of 24bit (so has
mine). And only a window on default root window created with
XCreateWindow can have 32bit.
A window created with XCreateSimpleWindow has only 32bit if the its
parent window has.

That is the reason I ask explicit for an ARGB-window...

With 24bit-windows there is no problem. The problem is, that every color
"getting more white" will have the more opacity with 32bit.

Why I want ARGB-windows with opaque children:
The reason is, that later I want to use alpha blending. And AFAIK is
alpha blending only possible on an ARGB-window (32bit).

Here
<http://www.individcore.de/IDA/Pics1.html>
you can see how it shall look like. (Isn't it a beauty!?)

Each section (aka window) are min. 3 windows.
The first has opacity, e.g. 20%.
The second is transparent by a specific color (e.g. RGB(1, 1, 1)).
And the third (as child of the second) will be alphablended...

That is what I want to realize under GNU/Linux too.
And that needs that a child window on a ARGB-window can be opaque.

Greeting
Heinz-Mario Frühbeis


sp...@potato.field

unread,
Mar 7, 2016, 8:25:46 AM3/7/16
to
On Mon, 7 Mar 2016 13:52:02 +0100
=?UTF-8?Q?Heinz-Mario_Fr=c3=bchbeis?= <D...@Earlybite.individcore.de> wrote:
>Am 07.03.2016 um 10:45 schrieb sp...@potato.field:
>> On Sat, 5 Mar 2016 19:09:00 +0100
>> =?UTF-8?Q?Heinz-Mario_Fr=c3=bchbeis?= <D...@Earlybite.individcore.de> wrote:
>
>Thank you for answering...
>
>But I think it cannot help really.
>Because and AFAIK has the default root window a depth of 24bit (so has
>mine). And only a window on default root window created with
>XCreateWindow can have 32bit.
>A window created with XCreateSimpleWindow has only 32bit if the its
>parent window has.
>
>That is the reason I ask explicit for an ARGB-window...
>
>With 24bit-windows there is no problem. The problem is, that every color
>"getting more white" will have the more opacity with 32bit.
>
>Why I want ARGB-windows with opaque children:
>The reason is, that later I want to use alpha blending. And AFAIK is
>alpha blending only possible on an ARGB-window (32bit).

I did wonder what you meany by ARGB, I thought it was a typo. Neither core
Xlib not the Shape extension support alpha blending. You'll either have do it
manually pixel by pixel which is fairly simple but incredibly inefficient for
anything but the smallest bitmaps or use the XRender extension which is what
most (all?) compositors and modern X applications use. Unfortunately its very
complex, works in a completely different way to the rest of X with its own
primitives (for reasons best known to its designers) and has piss poor online
documentation. Good luck!

--
Spud


Heinz-Mario Frühbeis

unread,
Mar 7, 2016, 9:22:03 AM3/7/16
to
Am 07.03.2016 um 13:52 schrieb Heinz-Mario Frühbeis:
> Am 07.03.2016 um 10:45 schrieb sp...@potato.field:
>> On Sat, 5 Mar 2016 19:09:00 +0100
>> =?UTF-8?Q?Heinz-Mario_Fr=c3=bchbeis?= <D...@Earlybite.individcore.de>
>> wrote
This...

void Create_XShape(int x, int y, unsigned int w, unsigned int h){
Pixmap p = XCreatePixmap(mDisplay, mSectionA, mWidth, mHeight, 1);
GC shape_gc = XCreateGC(mDisplay, p, 0, NULL);
XSetBackground(mDisplay, shape_gc, 0);
XSetForeground(mDisplay, shape_gc, 0);
XFillRectangle(mDisplay, p, shape_gc, x, y, w, h);
XSetForeground(mDisplay, shape_gc, mTransparent);
XFillRectangle(mDisplay, p, shape_gc
, mArea1.mLeft, mArea1.mTop, mArea1.mWidth, mArea1.mHeight);
XFillRectangle(mDisplay, p, shape_gc
, mArea2.mLeft, mArea2.mTop, mArea2.mWidth, mArea2.mHeight);
XShapeCombineMask(mDisplay, mSectionA, ShapeBounding, 0, 0, p, ShapeSet);

XFreePixmap(mDisplay, p);
XFreeGC(mDisplay, shape_gc);
Draw_Area();
SetFocus(mDisplay, mSection);
}

...makes a ARGB-window transparent, without (in this case) these two
squares, which are opaque. But even then and as far as I place a child
window (also ARGB-window) the background has opacity. (e.g. #177986 is
mixed to ~ #80D0DA)

One hour later<!>:
So far I've got it! And I think I've really got it!
The trick is the colormap!
To stay by the names above..., mArea1 has to alloc its colors with the
colormap of mSectiionA! That was all! And now all colors in mArea1 are
opaque, white, dark blue, black, light blue, all colors.

Now comes (hopefully <finger crossing!>) the last trick: Alpha blending
so that I can have a opacity background in mArea1 with opaque text.

@spud:
AFAIK is XRenderComposite the thing.
And it really does it, as far as I could recognize in first tests with
PictOpOver.
And, yes, it's a lot of work e.g. to check ten parameters against ten
parameters.

Regards
Heinz-Mario Frühbeis

sp...@potato.field

unread,
Mar 7, 2016, 11:49:34 AM3/7/16
to
On Mon, 7 Mar 2016 15:22:00 +0100
=?UTF-8?Q?Heinz-Mario_Fr=c3=bchbeis?= <D...@Earlybite.individcore.de> wrote:
>Am 07.03.2016 um 13:52 schrieb Heinz-Mario Frühbeis:
>> Am 07.03.2016 um 10:45 schrieb sp...@potato.field:
>>> On Sat, 5 Mar 2016 19:09:00 +0100
>>> =?UTF-8?Q?Heinz-Mario_Fr=c3=bchbeis?= <D...@Earlybite.individcore.de>
>>> wrote
>This...
>
>void Create_XShape(int x, int y, unsigned int w, unsigned int h){
> Pixmap p = XCreatePixmap(mDisplay, mSectionA, mWidth, mHeight, 1);
> GC shape_gc = XCreateGC(mDisplay, p, 0, NULL);
> XSetBackground(mDisplay, shape_gc, 0);
> XSetForeground(mDisplay, shape_gc, 0);
> XFillRectangle(mDisplay, p, shape_gc, x, y, w, h);
> XSetForeground(mDisplay, shape_gc, mTransparent);
> XFillRectangle(mDisplay, p, shape_gc
> , mArea1.mLeft, mArea1.mTop, mArea1.mWidth, mArea1.mHeight);
> XFillRectangle(mDisplay, p, shape_gc
> , mArea2.mLeft, mArea2.mTop, mArea2.mWidth, mArea2.mHeight);
> XShapeCombineMask(mDisplay, mSectionA, ShapeBounding, 0, 0, p, ShapeSet);
>
> XFreePixmap(mDisplay, p);
> XFreeGC(mDisplay, shape_gc);
> Draw_Area();
> SetFocus(mDisplay, mSection);
>}
>
>....makes a ARGB-window transparent, without (in this case) these two
>squares, which are opaque. But even then and as far as I place a child
>window (also ARGB-window) the background has opacity. (e.g. #177986 is
>mixed to ~ #80D0DA)

I'll have to try that. I didn't think Shape could do it. Are you sure its
not just XORing the background?

The problem with X Windows is that a lot of it is poorly documentated or not
documented at all, the Xlib programming manuals haven't been updated in
literally decades and are out of print anyway though I believe they're
available as PDFs from O'Reillys site.

--
Spud

Scott Lurndal

unread,
Mar 7, 2016, 12:39:58 PM3/7/16
to
sp...@potato.field writes:
>On Mon, 7 Mar 2016 15:22:00 +0100

>The problem with X Windows is that a lot of it is poorly documentated or not
>documented at all, the Xlib programming manuals haven't been updated in
>literally decades and are out of print anyway though I believe they're
>available as PDFs from O'Reillys site.

They're distributed with X11R6 in source form.

$ tar tzf /tmp/X11R6.8.1-src7.tar.gz
...
xc/doc/hardcopy/X11/xlib.PS.gz
xc/doc/hardcopy/Xaw/widgets.PS.gz
xc/doc/hardcopy/ICCCM/icccm.PS.gz
xc/doc/hardcopy/Xext/shape.PS.gz
xc/doc/hardcopy/Xt/intrinsics.PS.gz

...

Nicolas George

unread,
Mar 7, 2016, 12:41:41 PM3/7/16
to
sp...@potato.field, dans le message <nbkbeo$1ncc$1...@gioia.aioe.org>, a
écrit :
> the Xlib programming manuals haven't been updated in
> literally decades

That is true, and I do not disagree on your general statement about X11
libraries, but note that neither the core protocol nor the Xlib API have
been updated in decades. There is no point in updating a manual if what it
describes has not changed.

On the other hand, if someone knows where to find the documentation for
libxcb as man pages...

sp...@potato.field

unread,
Mar 8, 2016, 4:32:05 AM3/8/16
to
On Mon, 07 Mar 2016 17:39:54 GMT
sc...@slp53.sl.home (Scott Lurndal) wrote:
>sp...@potato.field writes:
>>On Mon, 7 Mar 2016 15:22:00 +0100
>
>>The problem with X Windows is that a lot of it is poorly documentated or not
>>documented at all, the Xlib programming manuals haven't been updated in
>>literally decades and are out of print anyway though I believe they're
>>available as PDFs from O'Reillys site.
>
>They're distributed with X11R6 in source form.

Didn't know that, very useful info. Thanks.

--
Spud


sp...@potato.field

unread,
Mar 8, 2016, 4:37:46 AM3/8/16
to
On 07 Mar 2016 17:41:39 GMT
Nicolas George <nicolas$geo...@salle-s.org> wrote:
>sp...@potato.field, dans le message <nbkbeo$1ncc$1...@gioia.aioe.org>, a
> écrit :
>> the Xlib programming manuals haven't been updated in
>> literally decades
>
>That is true, and I do not disagree on your general statement about X11
>libraries, but note that neither the core protocol nor the Xlib API have
>been updated in decades. There is no point in updating a manual if what it
>describes has not changed.

There is if a lot of new extensions have come out in the meantime. I would
have loved some proper manuals/documentation about the Shape, Double buffer and
Region extensions when I had to use them instead of a few online examples and
scrubbing around in code trying to figure them out. Unfortunately other peoples
code usually only tells you the how but not the why. As for XRender - it was so
complex I just gave up trying to understand it from code. I still don't know
how to use it properly.

--
Spud


0 new messages