]> Dogcows Code - chaz/openbox/blob - util/bsetbg
*** empty log message ***
[chaz/openbox] / util / bsetbg
1 #!/bin/sh
2
3 # Copyright (c) 2000-2002 Timothy M. King (tmk@lordzork.com)
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a
6 # copy of this software and associated documentation files (the "Software"),
7 # to deal in the Software without restriction, including without limitation
8 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 # and/or sell copies of the Software, and to permit persons to whom the
10 # Software is furnished to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 # DEALINGS IN THE SOFTWARE.
22
23 PATH=$PATH:/usr/bin:/usr/local/bin:/usr/X11R6/bin
24
25 img_apps="qiv xli xv wmsetbg Esetroot display xsetbg"
26
27 display_full_cmd="display -geometry 800x600 -window root"
28 display_tile_cmd="display -window root"
29 display_center_cmd="display -backdrop -window root"
30 display_default_cmd="$display_center_cmd"
31
32 Esetroot_full_cmd="Esetroot -scale"
33 Esetroot_tile_cmd="Esetroot"
34 Esetroot_center_cmd="Esetroot -c"
35 Esetroot_default_cmd="$Esetroot_center_cmd"
36
37 wmsetbg_full_cmd="wmsetbg -s -S"
38 wmsetbg_tile_cmd="wmsetbg -t"
39 wmsetbg_center_cmd="wmsetbg -e"
40 wmsetbg_default_cmd="$wmsetbg_center_cmd"
41
42 qiv_full_cmd="qiv --root_s"
43 qiv_tile_cmd="qiv --root_t"
44 qiv_center_cmd="qiv --root"
45 qiv_default_cmd="$qiv_center_cmd"
46
47 xv_full_cmd="xv -max -smooth -root -quit"
48 xv_tile_cmd="xv -root -quit"
49 xv_center_cmd="xv -rmode 5 -root -quit"
50 xv_default_cmd="$xv_center_cmd"
51
52 xli_full_cmd="xli -fullscreen -onroot -quiet"
53 xli_tile_cmd="xli -onroot -quiet"
54 xli_center_cmd="xli -center -onroot quiet"
55 xli_default_cmd="$xli_center_cmd"
56
57 xsetbg_full_cmd="xsetbg -fillscreen"
58 xsetbg_tile_cmd="xsetbg"
59 xsetbg_center_cmd="xsetbg -center"
60 xsetbg_default_cmd="$xsetbg_center_cmd"
61
62 ##################################
63
64 me=${0##*/}
65 version=2.1
66 copyright="(c) 2000-$(date +%Y) by Timothy M. King (http://lordzork.com/)"
67 config=$HOME/.bsetbgrc
68 last_cmd_file=$HOME/.bsetbg_last_cmd
69 refresh_cmd=xrefresh
70 p=$me:
71
72 quit()
73 {
74 [ "$1" ] && rc=$1 && shift 1
75 [ "$*" ] && echo -e $*
76 exit ${rc:-0}
77 }
78
79 bool()
80 {
81 case $1 in
82 [yY][eE][sS]|1|[yY]|[tT][rR][uU][eE]|[oO][nN]) : ;;
83 *) return 1 ;;
84 esac
85 }
86
87 check_exe_in_path()
88 {
89 if [ -z "$1" ]; then
90 return 1
91 elif [ -x "$(which $1 2>/dev/null)" ]; then
92 return 0
93 elif [ -x "$(type $1 2>/dev/null)" ]; then
94 return 0
95 else
96 return 1
97 fi
98 }
99
100 help_msg()
101 {
102 cat <<EOF
103 $me $version $copyright
104
105 -center <file> center an image on the desktop
106 -tile <file> tile an image on the desktop
107 -full <file> stretch an image to fill the desktop
108 -exec <args> <file> specify an external command to execute
109
110 -post <string> arguments to be passed to the post-command
111 -debug prints commands without executing them
112 EOF
113
114 # this is extremely lame, but probably more portable than grep -E
115 bsetroot_help=$(bsetroot -help 2>&1| grep -v "^bsetroot" | grep -v "^ -help")
116 case $bsetroot_help in
117 BaseDisplay*) echo ;;
118 *-gradient*) echo "$bsetroot_help"
119 esac
120
121 cat <<EOF
122 -generate <string> generate a config file
123 -help this message
124 -version output version information
125 EOF
126 quit ${1:-0}
127 }
128
129 get_apps()
130 {
131 for a in $img_apps; do
132 if check_exe_in_path $a; then
133 eval center_cmd=\$$a\_center_cmd
134 eval full_cmd=\$$a\_full_cmd
135 eval tile_cmd=\$$a\_tile_cmd
136 eval default_cmd=\$$a\_default_cmd
137 return 0
138 else
139 if [ "$not_found" ]; then
140 not_found="$not_found $a"
141 else
142 not_found=$a
143 fi
144 fi
145 done
146 return 1
147 }
148
149 do_generate()
150 {
151 echo -e "# created by $me $version on $(date)\n#"
152 echo -e "# seting NO_EXEC to a boolean value (eg true/false) will cause $me"
153 echo -e "# to never modify the root window\n#"
154 echo -e "#NO_EXEC=\n#"
155 echo -e "# POST_COMMAND can be set to a command that will be run run every time"
156 echo -e "# $me sets the root image\n#"
157 echo -e "#POST_COMMAND=\n#"
158 echo -e "# if LOG_LAST_CMD is set (boolean), bsetbg will keep a log of the last"
159 echo -e "# two successful commands.\n#"
160 echo -e "#LOG_LAST_CMD=\n#"
161 echo -e "# the LOGFILE specifies the file that bsetbg uses when LOG_LAST_CMD"
162 echo -e "# is defined. this defaults to ~/.bsetbg_last_cmd .\n#"
163 echo -e "#LOGFILE=\n#"
164 echo -e "# the following are default configuration values for the most popular image"
165 echo -e "# programs. See the man page of the respective application for more info.\n"
166
167 [ "$*" ] && img_apps="$*"
168
169 for a in $img_apps; do
170 if check_exe_in_path $a; then
171 if ! bool $have_match; then
172 q='\"'
173 [ "$(eval echo \$$a\_center_cmd)" ] &&
174 eval echo CENTER=$q\$$a\_center_cmd$q &&
175
176 [ "$(eval echo \$$a\_full_cmd)" ] &&
177 eval echo FULL=$q\$$a\_full_cmd$q &&
178
179 [ "$(eval echo \$$a\_tile_cmd)" ] &&
180 eval echo TILE=$q\$$a\_tile_cmd$q &&
181
182 [ "$(eval echo \$$a\_default_cmd)" ] &&
183 eval echo -e DEFAULT=$q\$$a\_default_cmd$q \\\\n &&
184
185 have_match=1
186 else
187 [ "$(eval echo \$$a\_center_cmd)" ] &&
188 eval echo \\#CENTER=$q\$$a\_center_cmd$q
189
190 [ "$(eval echo \$$a\_full_cmd)" ] &&
191 eval echo \\#FULL=$q\$$a\_full_cmd$q
192
193 [ "$(eval echo \$$a\_tile_cmd)" ] &&
194 eval echo \\#TILE=$q\$$a\_tile_cmd$q
195
196 [ "$(eval echo \$$a\_default_cmd)" ] &&
197 eval echo -e \\#DEFAULT=$q\$$a\_default_cmd$q \\\\n
198 fi
199 fi
200 done
201 quit 0
202 }
203
204 do_bsetroot()
205 {
206 if check_exe_in_path bsetroot; then
207 read_config
208
209 $debug bsetroot $* 2>/dev/null; rc=$?
210
211 if [ "$rc" -gt 0 ]; then
212 help_msg $rc
213 else
214 log_cmd bsetroot $*; $refresh_cmd 2>/dev/null
215 fi
216 else
217 quit 1 "couldn't find bsetroot in $PATH"
218 fi
219 }
220
221 do_standard()
222 {
223 [ -z "$1" ] && help_msg 1
224
225 bool $noconfig || read_config
226
227 get_img_command $1
228 check_img_command $do_this
229
230 case $# in
231 1) file="$1" ;;
232 *) file="$2"
233 esac
234
235 if [ -f "$file" ]; then
236 exec_img_command $do_this $file
237 else
238 quit 1 "$file does not exist"
239 fi
240 }
241
242 do_exec()
243 {
244 [ "$#" -lt 3 ] && help_msg 3
245
246 bool $noconfig || read_config
247
248 # check to see if -*c, -*f, or -*t were spcified, if so
249 # assume the last argument is a filename
250 b_arg=$(eval echo \$$(( $# - 1 )) )
251 app=$1
252
253 case $b_arg in
254 -c|*-center|c|-t|*-tile*|t|-f|*-full|f)
255 eval file=\$$#
256 f_args="$b_arg $file"
257 esac
258
259 # put the rest of the arguments into the varialbe $e_args
260 while [ "$*" ]; do
261 for a in "$*"; do
262 case $1 in
263 $b_arg|$file) : ;;
264 *) e_args="$e_args "$1""
265 esac
266 shift 1
267 done
268 done
269
270 # with $f_args defined, check for image and do things normally
271 if [ "$f_args" ]; then
272 [ ! -f "$file" ] && quit 1 "$file does not exist"
273
274 if check_img_command $e_args; then
275 do_this="$e_args"
276 else
277 read_config
278 get_img_command $f_args
279 check_img_command $do_this
280 echo "$p couldn't find '$app' in path, using $type command instead"
281 fi
282 # without $f_args, run the command blindly if it's in $PATH
283 elif check_exe_in_path $e_args; then
284 do_this="$e_args"
285 else
286 quit 1 "$p unable to run the following command: $e_args"
287 fi
288
289 exec_img_command $do_this $file
290 }
291
292 get_img_command()
293 {
294 case $1 in
295 *-full|-f|f) type=full; do_this="$full_cmd" ;;
296 *-tile|-t|t) type=tile; do_this="$tile_cmd" ;;
297 *-center|-c|c) type=center; do_this="$center_cmd" ;;
298 *) type=default; do_this="$default_cmd"
299 esac
300 }
301
302 check_img_command()
303 {
304 if check_exe_in_path $1; then
305 do_this="$*"
306 rc=0
307 elif get_apps; then
308 get_img_command $*
309 rc=1
310 else
311 quit 1 "$p couldn't find a suitable image program. tried the following:\\n
312 $(for a in $not_found; do echo " $a\\n"; done)"
313 fi
314
315 if [ "$rc" -gt 0 -a -z "$e_args" ] && bool $read_config; then
316 echo "$p couldn't find a suitable $type command in $config"
317 fi
318
319 return $rc
320 }
321
322 exec_img_command()
323 {
324 unset rc
325 command=$*
326
327 if [ "$debug" ]; then
328 $debug $command
329 else
330 $command >/dev/null 2>&1; rc=$?
331 fi
332
333 if [ "$rc" -gt 0 ]; then
334 echo "$p '$command' exited with status $rc"
335 get_apps
336 noconfig=1
337 parse_args ${f_args:-$my_args}
338 echo "$p using '$command' as $type"
339 $debug $command >/dev/null 2>&1; rc=$?
340 [ "$rc" = 0 ] && log_cmd $do_this $file && $refresh_cmd 2>/dev/null
341 else
342 log_cmd $do_this $file; xrefresh 2>/dev/null
343 fi
344 return $rc
345 }
346
347 log_cmd()
348 {
349 bool $LOG_LAST_CMD || return 1
350 [ "$debug" ] && return 1
351 echo -e "$prev_cmd\n$*" >$last_cmd_file
352 return $?
353 }
354
355 read_config()
356 {
357 [ -f $config ] || return 1
358
359 if bool $read_config; then
360 unset read_config
361 else
362 read_config=1
363 fi
364
365 . $HOME/.bsetbgrc 2>/dev/null
366 check_no_exec
367
368 full_cmd=$FULL
369 center_cmd=$CENTER
370 tile_cmd=$TILE
371 default_cmd=$CENTER
372 last_cmd_file=${LOGFILE:-$last_cmd_file}
373
374 bool $LOG_LAST_CMD && prev_cmd=$(tail -n 1 $last_cmd_file 2>/dev/null)
375 }
376
377 check_no_exec()
378 {
379 bool $NO_EXEC &&
380 quit 0 "$p no_exec mode. the root window will not be modified."
381 }
382
383 post_command()
384 {
385 if [ -n "$POST_COMMAND" -a "$rc" -le 0 ]; then
386 if [ -n "$debug" ]; then
387 $debug "running post_command: $POST_COMMAND $post_args"
388 else
389 post_command_output=$($POST_COMMAND $post_args 2>&1); erc=$?
390 if [ "$erc" -gt 0 ]; then
391 echo "$p post-command '$POST_COMMAND $post_args' exited with status $erc"
392 [ -n "$post_command_output" ] &&
393 echo "$POST_COMMAND $post_args: $post_command_output"
394 fi
395 fi
396 fi
397 }
398
399 add_arg()
400 {
401 [ "$1" ] || return 1
402 if [ "$args" ]; then
403 args="$args $1"
404 else
405 args=$1
406 fi
407 }
408
409 add_post_arg()
410 {
411 [ -z "$1" ] && return 1
412 if [ "$post_args" ]; then
413 post_args="$post_args $1"
414 else
415 post_args=$1
416 fi
417 }
418
419 check_cmd()
420 {
421 if [ -z "$command" ]; then
422 command=${2:-$1}; eval ${3:-${2:-$1}}=1
423 elif bool $do_post; then
424 add_post_arg ${1}
425 else
426 finished=1
427 fi
428 }
429
430 parse_args()
431 {
432 case $1 in
433 -d|*-debug|d)
434 unset refresh_cmd; debug=echo\ $me\_debug: ;;
435
436 -p|-*-post|p)
437 unset finished do_standard do_exec; do_post=1 ;;
438
439 -c|*-center|c|-t|*-tile*|t|-f|*-full|f)
440 case $command in
441 do_standard|do_generate|do_bsetroot)
442 finished=1 ;;
443 do_exec)
444 if ! bool $got_fcmd; then
445 add_arg $1 args; got_fcmd=1
446 else
447 finished=1
448 fi ;;
449 *)
450 add_arg $1; do_standard=1; command=do_standard
451 esac ;;
452
453 -a|*-app|a|-e|*-exec|e)
454 check_cmd "do_exec" ;;
455
456 -mod|-gradient|-solid|-display)
457 check_cmd "do_bsetroot" && add_arg $1 ;;
458
459 -g|*-generate*|g)
460 check_cmd $1 "do_generate" ;;
461
462 -v|*-version|v)
463 [ -z "$command" ] && quit 0 $me $version $copyright ;;
464
465 -h|*-help|h)
466 [ -z "$command" ] && help_msg ;;
467
468 *)
469 bool $finished && return 1
470
471 case $1 in -*)
472 bool $do_exec || bool $do_bsetroot || bool $do_post || help_msg 1
473 esac
474
475 if bool $do_standard || bool $do_exec || bool $do_bsetroot || bool $do_generate; then
476 add_arg $1
477 elif bool $do_post; then
478 add_post_arg $1
479 else
480 add_arg $1; command=do_standard; finished=1
481 fi
482 esac
483 }
484
485 [ -z "$*" ] && help_msg 1
486
487 my_args=$*
488
489 for arg in "$@"; do
490 parse_args "$arg"
491 shift 1
492 done
493
494 [ "$debug" ] && echo
495
496 $command $args
497 post_command $rc
498
499 quit $rc
This page took 0.064662 seconds and 4 git commands to generate.