在Linux上以编程方式更改壁纸

7

我如何在Linux桌面(使用GNOME)中通过C/C++程序更改壁纸?是否有系统API可以实现?


1
如果你找到了方法,请做一下,我已经想学会那个技能有一段时间了。 - fauxCoder
3个回答

19

您可以使用 gconf 库来实现。下面的示例是一个完整的程序,用于更改背景:

// bkgmanage.c
#include <glib.h>
#include <gconf/gconf-client.h>
#include <stdio.h>

typedef enum {
    WALLPAPER_ALIGN_TILED     = 0,
    WALLPAPER_ALIGN_CENTERED  = 1,
    WALLPAPER_ALIGN_STRETCHED = 2,
    WALLPAPER_ALIGN_SCALED    = 3,
    WALLPAPER_NONE            = 4
} WallpaperAlign;

gboolean set_as_wallpaper( const gchar *image_path, WallpaperAlign align )
{
    GConfClient *client;
    char        *options = "none";

    client = gconf_client_get_default();

    // TODO: check that image_path is a file
    if ( image_path == NULL ) options = "none";
    else {
        gconf_client_set_string( client, 
            "/desktop/gnome/background/picture_filename",
            image_path,
            NULL );
        switch ( align ) {
            case WALLPAPER_ALIGN_TILED: options = "wallpaper"; break;
            case WALLPAPER_ALIGN_CENTERED: options = "centered"; break;
            case WALLPAPER_ALIGN_STRETCHED: options = "stretched"; break;
            case WALLPAPER_ALIGN_SCALED: options = "scaled"; break;
            case WALLPAPER_NONE: options = "none"; break;
        }
    }
    gboolean result = gconf_client_set_string( client, 
        "/desktop/gnome/background/picture_options", 
        options,
        NULL);
    g_object_unref( G_OBJECT(client) );

    return result;
}

int main(int argc, const char* argv[])
{
  if ( argc > 1 ) {
    printf( "Setting %s as wallpaper... ", argv[1] );
    if ( set_as_wallpaper( argv[1], WALLPAPER_ALIGN_STRETCHED ) ) printf( "Ok\n" );
    else printf( "Failed\n" );
  } else printf( "Usage: ./bkgmanage <filename>\n" );

  return 0;
}

以上源代码是基于gthumb项目的。可以使用以下字符串进行编译:

gcc -Wall -g `pkg-config --libs --cflags glib-2.0 gconf-2.0` bkgmanage.c -o bkgmanage

10

尽管问题是针对gnome的,但也有一种方法可以处理与高层工具包无关的壁纸。 通过研究xsetroot.c的源代码,您应该能够处理根窗口(实际上就是壁纸)。以下是我复制粘贴的最有趣的部分:

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

2

2
OP 问的是 GNOME,不是 KDE。 - Kirill V. Lyadvinsky
1
如果您前往帖子#5,将提到一个gconftool-2命令,该命令设置GNOME中的背景图片。 - Amber
2
好的。终于找到了。不过如果你在帖子中粘贴了那一行代码(以及链接),会更有帮助。 - innaM
命令是:gconftool-2 --type string --set /desktop/gnome/background/picture_filename /path/to/image.jpg - lcapra

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接