在 linux 上设置壁纸有通用的方法吗?

Is there an universal way to set a wallpaper on linux?

我正在构建一个程序,它会定期使用从 reddit 获取的壁纸更换您的壁纸。它目前支持 windows,但我希望它也适用于 Linux。该程序开发于 Java。

问题是每个 DE 都有其独特的更换壁纸的方式:XFCE, Cinnamon, GNOME ecc...

我想找到一种简单通用的方法来做到这一点。到目前为止,这是我的代码(它使用 XFCE 解决方案,因为我的机器在 XFCE 上运行):

public static void setWallpaper(String wallpaperPath) { 
    String s = "xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitorVGA-1/workspace0/last-image -s \"" + wallpaperPath + "\"";
    ProcessBuilder processBuilder = new ProcessBuilder().command("bash", "-c", s);
    Process process = processBuilder.start();
}

我已经用 xwallpaper 试过了,但它不起作用,没有变化,没有输出,没有错误。

这是我找到的最佳解决方案,但我敢打赌它还可以改进。我首先扫描 $XDG_CURRENT_DESKTOP,然后扫描 $GDM_SESSION(显然 xdg 并不总是作为环境变量存在)。然后我根据我找到的环境设置壁纸。

public static void main(String[] args) {
        String wpPath = "/path/to/file";
        String os = System.getProperty("os.name");
        switch (os) {
            case "Windows 10":
                System.out.println("WINODWS DETECTED");
                break;
            case "Linux":
                String de = identifyDE();
                if (de == null) {
                    System.out.println("Couldn't identify your Desktop Environment"); // log Severe
                    break;
                }

                switch (de) {
                    case "xfce":
                        executeProcess("xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitorVGA-1/workspace0/last-image -s \"" + wpPath + "\"");
                        break;
                    case "gnome":
                        executeProcess("gsettings set org.gnome.desktop.background draw-background false && gsettings set org.gnome.desktop.background picture-uri \"file://" + wpPath + "\" && gsettings set org.gnome.desktop.background draw-background true");
                        break;
                    case "kde":
                        executeProcess("qdbus org.kde.plasmashell /PlasmaShell org.kde.PlasmaShell.evaluateScript 'var allDesktops = desktops();print (allDesktops);for (i=0;i<allDesktops.length;i++) {d = allDesktops[i];d.wallpaperPlugin = \"org.kde.image\";d.currentConfigGroup = Array(\"Wallpaper\", \"org.kde.image\", \"General\");d.writeConfig(\"Image\", \"" + wpPath + "\")}'");
                        break;
                    case "unity":
                        executeProcess("gsettings set org.gnome.desktop.background picture-uri \"file://" + wpPath + "\"");
                        break;
                    case "cinnamon":
                        executeProcess("gsettings set org.cinnamon.desktop.background picture-uri  \"file://" + wpPath + "\"");
                        break;
                    default:
                        System.out.println("Can't recognize DE: " + de);
                }


                break;
            default:
                System.out.println("Can't recognize OS: " + os);
        }
    }

public static String identifyDE() {
        String de;
        de = System.getenv("XDG_CURRENT_DESKTOP").toLowerCase();

        if (de.contains("xfce")) {
            return "xfce";
        } else if (de.contains("kde")) {
            return "kde";
        } else if (de.contains("unity")) {
            return "unity";
        } else if (de.contains("gnome")) {
            return "gnome";
        } else if (de.contains("cinnamon")) {
            return "cinnamon";
        } else if (de.contains("mate")) {
            return "mate";
        } else if (de.contains("deepin")) {
            return "deepin";
        } else if (de.contains("budgie")) {
            return "budgie";
        } else if (de.contains("lxqt")) {
            return "lxqt";
        } else {
            System.out.println("Not identifiable with: echo $XDG_CURRENT_DESKTOP: " + de);
        }

        de = System.getenv("GDM_SESSION").toLowerCase();

        if (de.contains("xfce")) {
            return "xfce";
        } else if (de.contains("kde")) {
            return "kde";
        } else if (de.contains("unity")) {
            return "unity";
        } else if (de.contains("gnome")) {
            return "gnome";
        } else if (de.contains("cinnamon")) {
            return "cinnamon";
        }  else if (de.contains("mate")) {
            return "mate";
        } else if (de.contains("deepin")) {
            return "deepin";
        } else if (de.contains("budgie")) {
            return "budgie";
        } else if (de.contains("lxqt")) {
            return "lxqt";
        } else {
            System.out.println("Not identifiable with: echo $GDM_SESSION: " + de);
        }

        return null;
    }

public static String executeProcess(String s) {
        ProcessBuilder pb = new ProcessBuilder("bash", "-c", s);
        pb.redirectErrorStream(true);
        Process p = null;
        try {
            p = pb.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

        StringBuilder res = new StringBuilder();
        String line;

        try {
            while ((line = reader.readLine()) != null) {
                res.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


        return res.toString();
    }

这段代码确实需要进一步测试,因为目前它只在manjaro XFCE和manjaro KDE上测试过,我什至不确定一些名字(deepin的XDG_CURRENT_DESKTOP包含“deepin”或“dde”?)。但是我认为这是一个很好的解决方案(或者 backbone 一个)所以我还是发布了这个。

来源: