You are not logged in.

#1 2021-04-22 23:53:55

NUIL
Member
Registered: 2021-04-22
Posts: 7

[SLOVED] XSetWindowBackground does not work.

First, I'm sorry that I once  necrobump.
I did some search and found no much help, it just make me hurry somehow.

I have been learning Xlib, and wrote this:

#include <X11/Xlib.h>

int main()
{
  Display *dpy = XOpenDisplay(NULL);
  int screen = DefaultScreen(dpy);
  Window root = RootWindow(dpy, screen);
  XSetWindowBackground(dpy, root, 0);
  XClearWindow(dpy, root);
  return 0;
}

It will just not change background.
How do I fix it?
I'm using i3wm, and

feh --bg-scale some.jpeg

can work properly.
Thanks.

Last edited by NUIL (2021-04-24 07:03:39)

Offline

#2 2021-04-23 02:26:09

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: [SLOVED] XSetWindowBackground does not work.

Your code is missing an "XCloseDisplay(dpy);" which should be right before the return statement.  Failing that, at least an "XFlush(dpy);" would be needed to make this work.

You cued up commands to be sent to the X server, but the program as-written exits before anything is actually sent.  With either the XCloseDisplay or the XFlush line, the code will "work" assuming feh isn't running in the background.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Online

#3 2021-04-23 04:38:15

NUIL
Member
Registered: 2021-04-22
Posts: 7

Re: [SLOVED] XSetWindowBackground does not work.

I have tried this

#include <X11/Xlib.h>

int main()
{
  Display *dpy = XOpenDisplay(NULL);
  int screen = DefaultScreen(dpy);
  Window root = RootWindow(dpy, screen);
  XSetWindowBackground(dpy, root, 0);
  XClearWindow(dpy, root);
  XFlush(dpy);
  XCloseDisplay(dpy);
  return 0;
}

and did

killall -q feh

before.
But still not work.

Offline

#4 2021-04-23 06:49:11

seth
Member
Registered: 2012-09-03
Posts: 49,951

Re: [SLOVED] XSetWindowBackground does not work.

Please explain "does not work".
What do you expect to happen, what happens instead?
Also: are you maybe running a compositor (picom/xcompmgr/…)?

Online

#5 2021-04-23 07:22:21

NUIL
Member
Registered: 2021-04-22
Posts: 7

Re: [SLOVED] XSetWindowBackground does not work.

I want to change wallpaper to, for now, as a test,  pixel value 0.
After I killed xcompmgr, it can work (make the root window pixle value 0).
But how can I do this without xcompmgr killed?

Offline

#6 2021-04-23 07:26:30

seth
Member
Registered: 2012-09-03
Posts: 49,951

Re: [SLOVED] XSetWindowBackground does not work.

xprop -root | grep PMAP

Online

#7 2021-04-23 07:40:57

NUIL
Member
Registered: 2021-04-22
Posts: 7

Re: [SLOVED] XSetWindowBackground does not work.

xprop -root | grep PMAP
ESETROOT_PMAP_ID(PIXMAP): pixmap id # 0x3e00001
_XROOTPMAP_ID(PIXMAP): pixmap id # 0x3e00001

Could you give some explain? I'm not quite familiar with X.

Offline

#8 2021-04-23 07:46:26

seth
Member
Registered: 2012-09-03
Posts: 49,951

Online

#9 2021-04-23 09:16:56

NUIL
Member
Registered: 2021-04-22
Posts: 7

Re: [SLOVED] XSetWindowBackground does not work.

I have come to this

#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <stdbool.h>

int main()
{
  Display *dpy = XOpenDisplay(NULL);
  int screen = DefaultScreen(dpy);
  Screen *scr = ScreenOfDisplay(dpy, screen);
  Window root = RootWindow(dpy, screen);

  Atom prop_root = XInternAtom(dpy, "_XROOTPMAP_ID", false);
  Atom prop_esetroot = XInternAtom(dpy, "ESETROOT_PMAP_ID", false);

  Pixmap pixmap = XCreatePixmap(dpy, root, scr->width, scr->height,
				DefaultDepth(dpy, screen));

  /* GC gc = XCreateGC(dpy, root, GCForeground, 0); */
  /* XPutImage(dpy, pixmap, gc, image, 0, 0, 0, 0, scr->width, scr->height); */

  XChangeProperty(dpy, root, prop_root, XA_PIXMAP, 32, PropModeReplace,
		  (unsigned char *) &pixmap, 1);

  XChangeProperty(dpy, root, prop_esetroot, XA_PIXMAP, 32, PropModeReplace,
		  (unsigned char *) &pixmap, 1);

  XSetWindowBackgroundPixmap(dpy, root, pixmap);
  XClearWindow(dpy, root);
  XFlush(dpy);
  XSetCloseDownMode(dpy, RetainPermanent);
  XCloseDisplay(dpy);
  return 0;
}

Looks like things left is to load an image/gen some color.
Thank you!

Offline

#10 2021-04-23 12:26:37

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: [SLOVED] XSetWindowBackground does not work.

So this is all starting to sound like an X-Y problem.  What are you *really* trying to do?  Why are you writing code explicitly to work around other tools that you are running?  Just stop running those other tools!


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Online

#11 2021-04-23 12:46:28

seth
Member
Registered: 2012-09-03
Posts: 49,951

Re: [SLOVED] XSetWindowBackground does not work.

He's not necessarily working around something, but compositors will more often then never render into the root window and thus get the root window pixmap from those properties instead.

@NUIL, the block above in the linke feh code prevents background pixmaps from leaking memory (because of XKillClient, check https://man.archlinux.org/man/extra/lib … nMode.3.en )

Online

#12 2021-04-24 04:56:34

NUIL
Member
Registered: 2021-04-22
Posts: 7

Re: [SLOVED] XSetWindowBackground does not work.

I see, thank you! I think this can be closed.

Offline

#13 2021-04-24 06:49:32

seth
Member
Registered: 2012-09-03
Posts: 49,951

Re: [SLOVED] XSetWindowBackground does not work.

Edit your inital post and prepend [SOLVED] to the subject line.

Online

Board footer

Powered by FluxBB