【问题标题】:Changing wallpaper on Linux programmatically以编程方式更改 Linux 上的墙纸
【发布时间】:2009-08-03 05:58:59
【问题描述】:

如何在 C/C++ 程序中更改 Linux 桌面上的墙纸(使用 GNOME)?是否有系统 API 可以做到这一点?

【问题讨论】:

  • 如果你知道怎么做,请做,我一直希望能够做到这一点。

标签: c++ linux gnome wallpaper desktop-wallpaper


【解决方案1】:

您可以使用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

【讨论】:

    【解决方案2】:

    虽然问题是 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;
    }
    

    【讨论】:

      【解决方案3】:

      如果不出意外,您可以使用system() 来调用此处建议的命令行之一:

      http://www.linuxquestions.org/questions/linux-general-1/change-background-via-command-line-350936/

      【讨论】:

      • OP 询问的是 GNOME,而不是 KDE。
      • 如果你去线程中的#5,会提到一个gconftool-2命令,它可以在GNOME中设置背景图片。
      • 好的。终于找到了。不过,如果您将该行(连同链接)粘贴到您的帖子中,会更有帮助。
      • 命令是gconftool-2 --type string --set /desktop/gnome/background/picture_filename /path/to/image.jpg
      猜你喜欢
      • 2012-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-03
      • 1970-01-01
      • 1970-01-01
      • 2017-12-25
      • 1970-01-01
      相关资源
      最近更新 更多