简体   繁体   中英

Changing programatically the wallpaper in Linux independently from the desktop environment or the window manager

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;
}

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM