[英]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.我在 Go 中为窗口管理器制作了一个自定义墙纸设置器。 Currently I use github.com/xyproto/wallutils
project, which in turn depends on feh
.目前我使用github.com/xyproto/wallutils
项目,它又依赖于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.对于准确的图像,我使用feh 的“填充”选项,该选项仅适用于其中一台显示器。
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.我想要做的是为每个屏幕创建正确尺寸的图像,并将它们作为背景图像发送到 X11 或 Wayland 中的相应屏幕。 How can I achieve this in Go?我怎样才能在围棋中实现这一点?
Requirements:要求:
feh
underneath在下面使用feh
muro and wallutils muro 和 wallutils
wallutils
specifies a WM
interface that provides, among other things, the SetWallpaper
method. wallutils
指定一个WM
接口,该接口提供SetWallpaper
方法等。 There are corresponding implementations of this interface for a number of different window managers.对于许多不同的窗口管理器,这个接口都有相应的实现。
The Go package muro
in turn uses wallutils
. Go 包muro
反过来使用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.基于WithAnyWindowManager
标志,它将使用 wallutils 的SetWallpaperCustom
方法,该方法根据检测到的窗口管理器选择具体的SetWallpaper
实现,或者直接调用feh
变体的SetWallpaper
。
wallutils and feh wallutils 和 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:具体显示模式取决于它的调用方式,但是SetWallpaper
中的 SetWallpaper 基本上会在您的情况下调用feh
,如下所示:
feh --bg-fill <image file name>
Two notes:两个注意事项:
feh
sets the wallpaper on all screens to the very same image这里feh
将所有屏幕上的墙纸设置为完全相同的图像Also, wallutils' readme explicitly states:此外,wallutils 的自述文件明确指出:
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请参阅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
的情况下以正确的顺序将预定义图像作为背景图像发送到适当的屏幕。
feh
itself supports setting different wallpapers per monitor. feh
本身支持为每个显示器设置不同的壁纸。 You just call feh
with the different images having different resolutions.您只需使用具有不同分辨率的不同图像调用feh
。 The order is guaranteed to be the same as determined by a call to xrandr --listmonitors
.该顺序保证与调用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 ):在确定顺序并将其作为给定之后,最简单的 GO 程序可能看起来像这样(另请参阅 wallutil 的实用函数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) (使用FluxBox
窗口管理器测试)
Provided feh
works with the appropriate window manager and there are the two prepared images in the go directory, this is the simplest case.如果feh
与适当的窗口管理器一起工作并且在 go 目录中有两个准备好的图像,这是最简单的情况。 Of course, one could also programmatically determine the screens and dynamically adjust the call of feh
.当然,也可以通过编程方式确定屏幕并动态调整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).由于feh
并不适用于所有环境,wallutils 为许多窗口管理器环境(Cinnamon、Deepin、Gnome、Mate、Pekwm、Plasma、Sway、Weston、Xfce4)提供了其WM
接口的具体实现。 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.但是,如果您想为 wallutils 创建一个 MR,您可能必须在所有变体中这样做,至少是那些支持它的变体。
You might consider vbsw/xlib
, which includes:您可能会考虑vbsw/xlib
,其中包括:
XOpenDisplay()
, which returns a Display ( C.Display
) XOpenDisplay()
,它返回一个 Display ( C.Display
)XScreenCount
, for the number of "screens" XScreenCount
,用于“屏幕”的数量XScreenOfDisplay
which gets a Screen for a given "screen number" XScreenOfDisplay
获取给定“屏幕编号”的屏幕XWidthOfScreen
XHeightOfScreen
That is inspired from the C implementation " Obtaining List of all Xorg Displays ".这是从 C 实现“获取所有 Xorg 显示的列表”中获得灵感的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.