fehに各モニターのサイズに応じて壁紙を設定させるにはどうすればよいですか?

fehに各モニターのサイズに応じて壁紙を設定させるにはどうすればよいですか?

pngファイルでいっぱいのディレクトリがあります

$ tree ~/wallpaper/
~/wallpaper/
├── foo--1366x768.png
├── foo--1920x1080.png
├── foo--2048x1080.png
├── foo--3440x1440.png
└── foo--3840x2160.png

0 directories, 5 files

モニターを頻繁に交換し、時々複数のモニターを接続することもあります。コンピューターに接続されているモニターのサイズを検出し、~/wallpaperモニターのサイズに応じて壁紙を設定するスクリプトが必要です。

したがって、モニターに解像度があり、接続されている場合は、スクリプト1366x7683440x1440壁紙をそれぞれ~/wallpaper/foo--1366x768.pngに設定したいと思います~/wallpaper/foo--3440x1440.png。この特定の例では、次のコマンドを実行する方法を知っています。

feh --bg-fill ~/wallpaper/foo--1366x768.png ~/wallpaper/foo--3440x1440.png

壁紙が正しく設定されます。

一般的な問題を解決するために発行できることを知っています。

xrandr | grep ' connected' | awk '{print $3}' | cut -f1 -d"+"

接続されているモニターのサイズを取得します。実行中の例では、出力は次のようになります。

1366x768
3440x1440

これで、壁紙を設定するために使用できるコマンドとしてこれをインポートしたいと思いますfeh。どうすればいいですか?

ベストアンサー1

を使用できます。--no-xineramaこの場合、指定した画像がすべての画面に表示されます。もしそうなら、唯一の質問は、モニターの設定方法に従ってすべての画像を組み合わせてデスクトップの背景を作成する方法です。

私が考えた唯一のものはImageMagickを使用することでしたが、それは非常に奇妙で混乱していました。だから何があってもやることに決めましたが…全体の内容は次のとおりです。

#!/bin/sh

AWK_XRANDR_PARSE='/ connected/ {
    split($3,sizePos,"+")
    split(sizePos[1],size,"x")
    print size[1] "," size[2] "," sizePos[2] "," sizePos[3]
}'

# Fetches a wallpaper for the monitor of size $1x$2. $1 is the required width,
# and $2 is the required height.
wallpaper_file() {
    # Use a case or string substitution to pick out any image files you fancy on
    # your system... They must be printf'd.
    printf "$HOME/.desktop"
}

# Writes any line whose $2nth column is equal to that of the first line.
# Columns are split by $1.
matching() {
    SENTINEL="$([ "$#" -gt 2 ] && printf '%s' "$3" || printf 'sentinel')"
    awk -F"$1" -v first="$SENTINEL" \
        "{if (first == \"$SENTINEL\") first = \$$2; if (\$$2 == first) print}"
}

# Writes the value within the variable named "$1".
cat() {
    printf '%s' "${!1}"
}

# Writes the $2nth column. Columns are split by $1.
nth() {
    awk -F"$1" "{print \$$2}"
}

# This one variable assignment takes xrandr's output, parses it via awk, runs
# the wallpaper_file, takes it's output, and combines all that information into
# a list of tuples. Each item in the list represents a monitor, and the tuple
# is roughly equivalent to 'W,H,X,Y,F', where W is width, H is height, X is the
# X position, Y is the Y position, and F is the file of the image.
DISPLAYS="$(while read X Y REST; do
    printf '%s,%s,%s,%s\n' "$X" "$Y" "$REST" "$(wallpaper_file "$X" "$Y")"
done < <(xrandr | awk "$AWK_XRANDR_PARSE" | \
    awk -F',' '{print $1 " " $2 " " $3 "," $4}'))"

# This simply finds the monitor that is the farthest out in the X direction,
# and is the widest. It then combines the X position and the width of the
# monitor to find the absolute width of your desktop.
PLACEMENT_WIDTH="$(cat DISPLAYS | sort -rnt, -k3 | matching , 3)"
SIZE_WIDTH="$(cat PLACEMENT_WIDTH | sort -rnt, -k1 | head -n1)"
WIDTH="$(("$(cat SIZE_WIDTH | nth , 1)" + "$(cat SIZE_WIDTH | nth , 3)"))"

# Same goes on as above, but with the height and Y direction.
PLACEMENT_HEIGHT="$(cat DISPLAYS | sort -rnt, -k4 | matching , 4)"
SIZE_HEIGHT="$(cat PLACEMENT_HEIGHT | sort -rnt, -k2 | head -n1)"
HEIGHT="$(("$(cat SIZE_HEIGHT | nth , 2)" + "$(cat SIZE_HEIGHT | nth , 4)"))"

# Take all that information, and make a wallpaper file from it via imagemagick.
magick -size $WIDTH'x'$HEIGHT canvas:black \
    $(cat DISPLAYS | awk -F',' '{print $5 " -geometry " $1 "x" $2 "+" $3 "+" $4 " -composite"}') \
    /tmp/wallpaper.png

# Then set it as the background.
feh --bg-fill --no-xinerama /tmp/wallpaper.png

スクリプト全体を要旨として公開しています。ここ、あなたが好むなら。

おそらくそうしてはいけないことを知っていますが、マブソサ、とにかく私はそうします。

おすすめ記事