-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathi3wall
More file actions
executable file
·285 lines (251 loc) · 10.1 KB
/
i3wall
File metadata and controls
executable file
·285 lines (251 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/env bash
########################################################################
# File Name: i3wall
# Author: Awkee
# mail: next4nextjob@gmail.com
# Created Time: 2023年 08月 08日 星期二 20:15:18 CST
#
# 非强制性约束: 网络下载使用curl命令而不用wget,没有任何歧视含义,仅处于统一要求
#
# 功能简述: 个人Linux桌面环境配置工具-动态桌面背景配置工具
# 依赖命令:
# - curl 下载依赖代码或文件工具
# - fzf 提供多选择菜单
# - xrandr 分辨率检测
# - mpv 视频文件播放
########################################################################
# set -e # 命令执行失败就中止继续执行
I3WALL_VERSION="v0.0.1" # i3wall版本信息
# Define Colors
RED='\e[41m'
BRB='\e[1;7;31;47m' # Blink Red bold
NC='\e[0m' # No color
BG='\e[7m' # Highlighting Background color
TC='\e[1m' # Highlighting Text color
########### 文字显示颜色输出函数 ######
function echo_white() { printf "\033[0;37m$@\033[0m" ; }
function echo_whiter() { printf "\033[0;37;7m$@\033[0m" ; }
function echo_red() { printf "\033[0;31m$@\033[0m" ; }
function echo_redr() { printf "\033[0;31;7m$@\033[0m" ; }
function echo_green() { printf "\033[0;32m$@\033[0m" ; }
function echo_greenr() { printf "\033[0;32;7m$@\033[0m" ; }
function white_line() { printf "\033[0;37m$@\033[0m\n" ; }
function whiter_line() { printf "\033[0;37;7m$@\033[0m\n" ; }
function red_line() { printf "\033[0;31;1m$@\033[0m\n" ; }
function redr_line() { printf "\033[0;31;7m$@\033[0m\n" ; }
function green_line() { printf "\033[0;32;1m$@\033[0m\n" ; }
function greenr_line() { printf "\033[0;32;7m$@\033[0m\n" ; }
line_feed="+--------------------------------------------------------------------------------+"
PMT=">>>"
# 临时存储代码、下载文件目录
tmp_dir=/tmp
item_index=0 # 记录菜单选项序号
item_line_count=2 # 每行显示菜单数量
ILEN=40 # 单个选项长度
MLEN=$((${ILEN} * ${item_line_count})) # 单行最大长度
# 欢迎和再见提示信息
WELCOME="^_^你笑起来真好看!像春天的花一样!"
SEE_YOU="^_^出去晒晒太阳吧!多运动才更健康!"
READ_TIMEOUT=30 # read timeout seconds
function menu_line() { let rlen="$item_line_count * $ILEN + 1" ; echo -en "|${BRB} $@ $NC" ; tput hpa $rlen ; echo "|" ; }
function menu_head() { echo $line_feed ; menu_line "$@" ; echo $line_feed ; }
# 一行可以有 item_line_count 个菜单选项
function menu_item() { let item_index=$item_index+1 ; n=$1 ; shift ; let rlen="$item_index * $ILEN + 1" ; echo -en "| $BG ${n} $NC $@" ; tput hpa $rlen ; [[ "$item_index" == "$item_line_count" ]] && echo "|" && item_index=0 ; }
# 输出单行长菜单选项,长度有限制
function menu_iteml() { let rlen="$item_line_count * $ILEN + 1" ; n=$1 ; shift ; echo -en "| $BG ${n} $NC $@" ; tput hpa $rlen ; echo "|" ; }
# 用于输入长信息(非菜单选项),不限制结尾长度
function menu_info() { n=$1 ; shift ; echo -e "| $BG ${n} $NC $@" ; }
function menu_tail() { [[ "$item_index" != "0" ]] && echo "|" ; echo $line_feed ; item_index=0 ; }
function check_cmd() {
cmd="$1"
! command -v $cmd >/dev/null 2>&1 && echo "${RED}缺失 $cmd 命令!${NC}" && exit 1
# echo -e "命令${NC}${cmd}${NC}已安装."
}
function check_deps() {
# 检查必备依赖工具
check_cmd curl
check_cmd fzf
check_cmd xrandr
}
# 透明度默认值
default_opacity=0.9
# 背景视频存放路径
video_dir="$HOME/.wallpapers_4k"
# 背景视频的当前使用文件名
video_file="anime_agatsumazenitsu_1080p.webm"
# xscreensaver 路径
xsaver_dir=/usr/libexec/xscreensaver
# 动态背景类型 video/xsaver
bg_type="video"
# config file for your parameters
CFG_FILE="$HOME/.i3wallrc"
function generate_config() {
# 用于生成 $CFG_FILE 配置参数信息
if [[ -f "$CFG_FILE" ]] ; then
menu_head "当前配置信息如下"
cat $CFG_FILE
menu_tail
fi
# 开始生成配置信息
# 设置 透明度(default_opacity, 默认 0.9)
read -t $READ_TIMEOUT -r -e -p "请设置动态背景透明度(0-1之间,默认:${default_opacity}) ${PMT} " str_answer
# check str_answer variable is float value
decimal_regex="^[0-9]+(\.[0-9]+)?$"
if [[ "$str_answer" == "" ]] ; then
echo "使用默认的 $default_opacity 数值"
echo "opacity=$default_opacity" > $CFG_FILE
elif awk -v num="$str_answer" 'BEGIN { exit !(num >0 && num <= 1) }'; then
echo "opacity=$str_answer" > $CFG_FILE
else
echo "无效的透明度数值[$str_answer],使用默认的 $default_opacity 数值"
echo "opacity=$default_opacity" > $CFG_FILE
fi
# 设置视频背景路径 video_background_files_path
read -t $READ_TIMEOUT -r -e -p "请设置视频背景文件的路径(默认:${video_dir}) ${PMT} " str_answer
if [[ -d "$str_answer" ]] ; then
echo "video_dir=$str_answer" >> $CFG_FILE
else
echo "无效的视频路径 [$str_answer],使用默认的目录 $video_dir "
echo video_dir="$video_dir" >> $CFG_FILE
fi
# 设置 xsaver_dir
echo "安装 xscreensaver后的 xsaver_dir 路径通常为如下:"
echo " /usr/libexec/xscreensaver"
echo " /usr/lib/xscreensaver"
echo
for _dir in /usr/libexec/xscreensaver /usr/lib/xscreensaver
do
[[ -d "$_dir" ]] && echo "$_dir 路径存在,可以使用"
done
read -t $READ_TIMEOUT -r -e -p "请设置xscreensaver动态效果程序存放路径(默认:${xsaver_dir}) ${PMT} " str_answer
if [[ -d "$str_answer" ]] ; then
echo "xsaver_dir=$str_answer" >> $CFG_FILE
else
echo "无效的视频路径 [$str_answer],使用默认的目录 $xsaver_dir "
echo xsaver_dir="$xsaver_dir" >> $CFG_FILE
echo "xsaver_file=goop" >> $CFG_FILE
fi
echo "bg_type=video" >> $CFG_FILE
}
function stop() {
#ps -ef | grep xwinwrap |grep -v grep | awk '{ print $2 }' | xargs kill
pkill xwinwrap
sleep 1 # 等待进程关闭
}
function start_video() {
stop
# mpv 视频播放参数信息
wall_cmd="mpv -wid WID --loop --no-cache --no-audio --really-quiet --framedrop=vo --panscan=1.0 ${video_dir}/${video_file}"
# 获取显示器及分辨率信息(自动适应屏幕布局分辨率)
for resolution in `xrandr | awk '/ connected/{ if($3~/primary/) print $4 ; else print $3 }'`
do
xwinwrap -d -ni -b -s -st -sp -nf -ov -g $resolution -o ${opacity} -- $wall_cmd
done
}
# 切换视频背景
function select_video() {
video_file=`ls ${video_dir} | fzf`
if grep "^video_file=" $CFG_FILE >/dev/null 2>&1 ; then
sed -i "s/^video_file=.*/video_file=$video_file/" $CFG_FILE
else
sed -i "$ a\video_file=$video_file" $CFG_FILE
fi
[[ ! -f ${video_dir}/${video_file} ]] && echo "选择的视频文件 [$video_file] 不存在!" && return 0
bg_type=video
sed -i 's/^bg_type=.*/bg_type=video/' $CFG_FILE
start_video
}
# 启动xsaver命令: start_xsaver $xsaver_dir/goop
function start_xsaver() {
stop
# xsaver程序作为输入参数
wall_cmd="$1 --window-id WID"
# 获取显示器及分辨率信息
for resolution in `xrandr | awk '/ connected/{ if($3~/primary/) print $4 ; else print $3 }'`
do
xwinwrap -d -ni -b -s -st -sp -nf -ov -g $resolution -o ${opacity} -- $wall_cmd
done
}
# 切换 xscreensaver 动态背景
function select_xsaver() {
# bumps cubicgrid
# 彩色: epicycle(线环) goop(多彩的形状) kumppa(多彩旋转线条) surfaces vermiculate xrayswarm
# 游戏: pacman maze pipes scooter wormhole
# 系统: phosphor
# 风景: atlantis(鲨鱼群) glschool(小鱼群) fiberlamp(纤维束灯) fireworks(烟花) flurry(极光)
xsaver_file=`ls ${xsaver_dir} | fzf`
[[ ! -f ${xsaver_dir}/${xsaver_file} ]] && echo "选择的屏保程序文件 [$xsaver_file] 不存在!" && return 0
bg_type=xsaver
sed -i 's/^bg_type=.*/bg_type=xsaver/' $CFG_FILE
start_xsaver ${xsaver_dir}/${xsaver_file}
}
start() {
case "$bg_type" in
video)
start_video
;;
xsaver)
start_xsaver ${xsaver_dir}/${xsaver_file}
;;
esac
}
usage() {
echo "Usage:"
echo " `basename $0` [OPTIONS]"
echo
echo "OPTIONS:"
echo " start: 启动动态背景"
echo " stop : 停止动态背景"
echo " status: 查看进程状态"
echo
}
function update_i3wall() {
# 更新 i3config 到最新版本
tmp_path="$tmp_dir/i3wall"
install_path="$0"
curl -L -o $tmp_path https://raw.githubusercontent.com/switchToLinux/dotfiles/main/i3wall
[[ "$?" == "0" ]] && mv ${install_path} ${install_path}.old && mv $tmp_path ${install_path}
chmod +x ${install_path}
echo "已更新 i3wall 到最新版本 ${I3WALL_VERSION}"
}
## main function
function show_menu_install() {
menu_head "i3wall ${I3WALL_VERSION} 安装选项菜单"
menu_item 1 启动动态背景
menu_item 2 停止动态背景
menu_item 3 切换视频动态背景
menu_item 4 切换特效动态背景
menu_item c 设置配置参数
menu_tail
menu_item u update_i3wall
menu_item q 返回上级菜单
menu_tail
}
function start_main() { # 安装菜单选择
while true
do
# source $CFG_FILE # 每次都重新加载一次环境变量
show_menu_install
read -t $READ_TIMEOUT -r -n 1 -e -p "`echo_greenr 请选择:` ${PMT} " str_answer
case "$str_answer" in
1) start ;;
2) stop ;;
3) select_video ;;
4) select_xsaver ;;
c) generate_config ;;
u) update_i3wall ;;
q|"") return 0 ;; # 返回上级菜单
*) redr_line "没这个选择[$str_answer],搞错了再来." ;;
esac
done
}
####### Main process #################################
menu_head "$WELCOME"
check_deps # 基础依赖命令检测与安装
if [[ ! -f $CFG_FILE ]] ; then
# 文件不存在,开始生成配置
generate_config
fi
source $CFG_FILE
start_main
menu_head "${SEE_YOU}"