3

Linux have got lots of desktop environments (GNOME, KDE, Xfce, Cinnamon...) and windowing systems (X11, Wayland, Mir...) and it seems that each one has got its own way to change the wallpaper. Are there some high-level libraries, especially in C++ (and Qt 5), which enables the developer to change programatically the wallpaper in Linux, regardless of the window management or the desktop management? I am looking for something like that:

#include <the_lib>
#include <cstdlib>

int main(int argc, char ** argv) {
    std::string theNewWallpaper = "path/to/my/wallpaper.jpg";
    // Or a file, an image, or something else representing the wallpaper.

    TheLib::changeWallpaper(theNewWallpaper);
    // or a more complicated piece of code which does the same.

    return EXIT_SUCCESS;
}
air-dex
  • 4,130
  • 2
  • 25
  • 25
  • Just a suggestion: Why don't you search for the location where the desktop wallpaper is saved, and replace it then? – Max Sep 20 '15 at 21:57
  • It is a possibility. But how can you find it, **regardless of your window management or your desktop environment**? This is the main difficulty of this question. – air-dex Sep 20 '15 at 22:04
  • Mh... That's why I didn't write it as answer, I don't know where that path is. – Max Sep 20 '15 at 22:05
  • 1
    X11, Wayland, and Mir are not window managers. BTW if your program changes my wallaper it gets uninstalled that very minute. That's despite the fact that my DE changes it every minute so your changes won't last anyway. – n. m. could be an AI Sep 21 '15 at 07:26
  • @n.m. You are right. These are widowing systems, not "window managers". I have edited my question. As for my program, it will change your wallpaper only if you ask for it. I would like to make it run on Linux desktops in general so I do not want to bother with things like "`if (desktop == "GNOME") { /* ... */ } else if (desktop == "KDE") { /* ... */ } /* and so on */`" (same thing with widowing systems). – air-dex Sep 21 '15 at 12:17

1 Answers1

3

Try the solution of "Andrew Y" in the post : Changing wallpaper on Linux programmatically

He claims that his solution is not dependant of the higher layer toolkits, so it's should work for any linux desktop environments.

static void
SetBackgroundToBitmap(Pixmap bitmap, unsigned int width, unsigned int height)
{
    Pixmap pix;
    GC gc;
    XGCValues gc_init;

    gc_init.foreground = NameToPixel(fore_color, BlackPixel(dpy, screen));
    gc_init.background = NameToPixel(back_color, WhitePixel(dpy, screen));
    if (reverse) {
        unsigned long temp=gc_init.foreground;
        gc_init.foreground=gc_init.background;
        gc_init.background=temp;
    }
    gc = XCreateGC(dpy, root, GCForeground|GCBackground, &gc_init);
    pix = XCreatePixmap(dpy, root, width, height,
                        (unsigned int)DefaultDepth(dpy, screen));
    XCopyPlane(dpy, bitmap, pix, gc, 0, 0, width, height, 0, 0, (unsigned long)1);
    XSetWindowBackgroundPixmap(dpy, root, pix);
    XFreeGC(dpy, gc);
    XFreePixmap(dpy, bitmap);
    if (save_colors)
        save_pixmap = pix;
    else
        XFreePixmap(dpy, pix);
    XClearWindow(dpy, root);
    unsave_past = 1;
}
Community
  • 1
  • 1
Tibox
  • 827
  • 6
  • 7
  • I saw the SO question but it does not answer to mine. The code depends on X11. So what about this piece of code with Mir or Wayland? – air-dex Sep 21 '15 at 12:19
  • There is no universal API. Mir and Wayland are still niche products. If you care about them you will have to find solutions for them separately. – n. m. could be an AI Sep 21 '15 at 12:55