imagegox11wayland

How to add a background images to several screens for X11/Wayland using Go?


I make a custom wallpaper setter for window managers in Go. Currently I use github.com/xyproto/wallutils project, which in turn depends on feh.

For one screen I can use its dimensions to create the image of exact size, and all works well, but if there are several screens with different dimensions connected to a computer, this approach does not work.

For the exact image I use feh's 'fill' option, which would work correctly only for one of the monitors.

What I want to do is to create images of correct dimensions for each screen and send them as background images to corresponding screens either in X11 or Wayland. How can I achieve this in Go?


Solution

  • Requirements:

    muro and wallutils

    wallutils specifies a WM interface that provides, among other things, the SetWallpaper method. There are corresponding implementations of this interface for a number of different window managers.

    The Go package muro in turn uses wallutils. Based on the flag WithAnyWindowManager it will either use wallutils' SetWallpaperCustom method which selects a concrete SetWallpaper implementation based on the detected window manager or just directly call SetWallpaper of the feh variant.

    wallutils and feh

    The specific display mode depends on how it is called, but SetWallpaper in wallutils feh.go would basically call feh in your case as follows:

        feh --bg-fill <image file name>
    

    Two notes:

    Also, wallutils' readme explicitly states:

    Setting a wallpaper per monitor

    Setting a wallpaper per monitor is not supported, yet. Currently, a wallpaper is set for all monitors.

    see https://github.com/xyproto/wallutils#setting-a-wallpaper-per-monitor

    Possible solution

    Since you can determine monitors and resolution, we focus on sending the predefined images in the correct order as background images under use of feh to the appropriate screens.

    feh itself supports setting different wallpapers per monitor. You just call feh with the different images having different resolutions. The order is guaranteed to be the same as determined by a call to xrandr --listmonitors.

    After determining the order and taking it as a given, the simplest possible GO program would look something like this (see also wallutil's utility function run):

    package main
    
    import (
        "os/exec"
    )
    
    func main() {
        args := []string{"--bg-fill", "1.png", "2.png"}
        cmd := exec.Command("feh", args...)
        if _, err := cmd.CombinedOutput(); err != nil {
            panic(err)
        }
    }
    

    (tested with FluxBox window manager)

    Provided feh works with the appropriate window manager and there are the two prepared images in the go directory, this is the simplest case. Of course, one could also programmatically determine the screens and dynamically adjust the call of feh.

    Since feh does not work in every environment, wallutils provides concrete implementations of its WM interface for a number of window manager environments (Cinnamon, Deepin, Gnome, Mate, Pekwm, Plasma, Sway, Weston, Xfce4). This is of course very cool. However, if you wanted to create an MR for wallutils, you would probably have to do so in all variants, at least those that support it.