3 # vidwhacker, for xscreensaver. Copyright (c) 1998, 1999 Jamie Zawinski.
5 # Permission to use, copy, modify, distribute, and sell this software and its
6 # documentation for any purpose is hereby granted without fee, provided that
7 # the above copyright notice appear in all copies and that both that
8 # copyright notice and this permission notice appear in supporting
9 # documentation. No representations are made about the suitability of this
10 # software for any purpose. It is provided "as is" without express or
14 # This script grabs a frame of video, then uses various pbm filters to
15 # munge the image in random nefarious ways, then uses xli or xv to put it
16 # on the root window. This works out really nicely if you just feed some
17 # random TV station into it...
19 # The video grabbing part is SGI-specific -- if you want to use this on
20 # another system, add a new clause to the grab() procedure.
23 # need perl to generate random numbers -- I don't know another way to do
24 # that from a shell script.
35 tmp=${TMPDIR:-/tmp}/vidwhacker.$$
36 tmp_rgb=$tmp-00000.rgb
45 # Figure out whether to use xli or xv.
49 if [ "$p" = "" ]; then p=. ; fi
50 if [ -x $p/xli ]; then
52 displayer_args="-quiet"
54 displayer_root_args="-onroot -center -border black"
56 elif [ -x $p/xv ]; then
58 displayer_args="-quick24"
59 displayer_win_args="-geom +0+0"
60 displayer_root_args="-root -rmode 5 -noresetroot -rfg black -rbg black -viewonly"
67 echo "$0: neither xli nor xv found on \$PATH"
71 # Process command-line args
76 -display | -disp | -dis | -dpy | -d )
101 echo "VidWhacker, Copyright (c) 1999 Jamie Zawinski <jwz@jwz.org>" >&2
102 echo " http://www.jwz.org/xscreensaver/" >&2
104 echo "usage: $0 [-display dpy] [-verbose] [-root | -window]" >&2
105 echo " [-stdin] [-stdout] [-delay secs]" >&2
114 if [ "$onroot" = true ]; then
115 displayer_args="$displayer_args $displayer_root_args"
117 displayer_args="$displayer_args $displayer_win_args"
122 if [ "$use_stdout" = false ]; then
123 screen_width=`xdpyinfo 2>/dev/null |
124 sed -n 's/.* dimensions: *\([0-9]*\).*/\1/p'`
125 if [ "$screen_width" = "" ]; then
133 rm -f $tmp_rgb $tmp_ppm1 $tmp_ppm2 $tmp_ppm3 $tmp_ppm4
138 rm -f $tmp_ppm0 $tmp_ppmS
142 # Grab a frame of video. leaves it in $tmp_ppm1.
146 if [ $uname = IRIX ]; then
148 # SGI's "vidtomem" returns an SGI RGB image of the default video input,
149 # and has stupid non-overridable ouput-file-naming conventions. So, let
150 # it write its file; and then convert it to a pgm.
154 sgitopnm $tmp_rgb > $tmp_ppm1
156 # Cut off the close-captioning blips in the NTSC overscan region. YMMV.
157 # | pnmcut 12 7 695 477
159 elif [ $uname = Linux ]; then
161 # Marcus Herbert says the following works with his Connectix Qcam.
162 # Don't have qcam? Well, do something else then... and send me a patch.
166 # Friedrich Delgado Friedrichs says the following works if you have
169 # streamer -o $tmp_ppm1
173 echo "$0: don't know how to grab video on this OS." >&2
180 # Use perl to pick a random foreground/background color in pbm's syntax.
183 $perl -e 'srand(time ^ $$);
184 printf("#%02x%02x%02x-#%02x%02x%02x",
190 120+int(rand()*135))'
194 $perl -e "srand(time ^ $$); print int(rand() * $1)"
199 # Frobnicate the image in some random way.
203 w_h=`head -2 $tmp_ppm1 | tail -1`
204 width=`echo $w_h | awk '{print $1}'`
205 height=`echo $w_h | awk '{print $2}'`
209 if [ "$verbose" = true ]; then
210 echo "mode $N..." >&2
214 ppmtopgm $tmp_ppm1 | pgmedge | pgmtoppm `randcolor` | ppmnorm
216 elif [ $N = 1 ]; then
221 elif [ $N = 2 ]; then
222 ppmtopgm $tmp_ppm1 | pgmoil | pgmtoppm `randcolor`
224 elif [ $N = 3 ]; then
225 ppmrelief $tmp_ppm1 | ppmtopgm | pgmedge | ppmrelief | ppmtopgm |
226 pgmedge | pnminvert | pgmtoppm `randcolor`
228 elif [ $N = 4 ]; then
229 ppmspread 71 $tmp_ppm1 > $tmp_ppm2
230 pnmarith -add $tmp_ppm1 $tmp_ppm2
232 elif [ $N = 5 ]; then
233 pnmflip -lr $tmp_ppm1 > $tmp_ppm2
234 pnmarith -multiply $tmp_ppm1 $tmp_ppm2 > $tmp_ppm3
235 pnmflip -tb $tmp_ppm3 | ppmnorm > $tmp_ppm2
236 pnmarith -multiply $tmp_ppm1 $tmp_ppm2
238 elif [ $N = 6 ]; then
241 pnmflip -lr $tmp_ppm1 > $tmp_ppm2
242 elif [ $N2 = 1 ]; then
243 pnmflip -tb $tmp_ppm1 > $tmp_ppm2
245 pnmflip -lr $tmp_ppm1 > $tmp_ppm2
246 pnmflip -tb $tmp_ppm2 > $tmp_ppm3
247 cp $tmp_ppm3 $tmp_ppm2
250 pnmarith -difference $tmp_ppm1 $tmp_ppm2
252 elif [ $N = 7 ]; then
255 ppmtopgm $tmp_ppm1 | pgmedge > $tmp_ppm2
256 pnmarith -difference $tmp_ppm1 $tmp_ppm2 > $tmp_ppm3
257 cp $tmp_ppm3 $tmp_ppm1
261 elif [ $N = 8 ]; then
262 pnmflip -lr $tmp_ppm1 > $tmp_ppm2
263 pnmarith -multiply $tmp_ppm1 $tmp_ppm2 | ppmrelief | ppmnorm | pnminvert
265 elif [ $N = 9 ]; then
266 pnmflip -lr $tmp_ppm1 > $tmp_ppm2
267 pnmarith -subtract $tmp_ppm1 $tmp_ppm2 | ppmrelief | ppmtopgm | pgmedge
269 elif [ $N = 10 ]; then
270 ppmtopgm $tmp_ppm1 | pgmbentley | pgmtoppm `randcolor`
272 elif [ $N = 11 ]; then
273 pgmcrater -number 20000 -height $height -width $width | pgmtoppm `randcolor` > $tmp_ppm2
274 pnmarith -difference $tmp_ppm1 $tmp_ppm2 > $tmp_ppm3
275 pnmflip -tb $tmp_ppm3 | ppmnorm > $tmp_ppm2
276 pnmarith -multiply $tmp_ppm1 $tmp_ppm2
278 elif [ $N = 12 ]; then
279 ppmshift 30 $tmp_ppm1 | ppmtopgm | pgmoil | pgmedge | pgmtoppm `randcolor` > $tmp_ppm2
280 pnmarith -difference $tmp_ppm1 $tmp_ppm2
282 elif [ $N = 13 ]; then
283 ppmpat -madras $width $height | pnmdepth 255 > $tmp_ppm2
284 pnmarith -difference $tmp_ppm1 $tmp_ppm2
286 elif [ $N = 14 ]; then
287 ppmpat -tartan $width $height | pnmdepth 255 > $tmp_ppm2
288 pnmarith -difference $tmp_ppm1 $tmp_ppm2
290 elif [ $N = 15 ]; then
291 ppmpat -camo $width $height | pnmdepth 255 | ppmshift 50 > $tmp_ppm2
292 pnmarith -multiply $tmp_ppm1 $tmp_ppm2
294 elif [ $N = 16 ]; then
295 pgmnoise $width $height | pgmedge | pgmtoppm `randcolor` > $tmp_ppm2
296 pnmarith -difference $tmp_ppm1 $tmp_ppm2 | pnmdepth 255 | pnmsmooth
303 # Grab a frame and frob it. leave it in $tmp_ppm3.
308 while [ ! -f $tmp_ppm1 ]; do
309 if [ "$use_stdin" != true ]; then
312 cp $tmp_ppmS $tmp_ppm0
313 cp $tmp_ppm0 $tmp_ppm1
319 if [ "$screen_width" != "" ]; then
320 frob | pnmscale -width $screen_width > $tmp_ppm3
325 rm -f $tmp_ppm1 $tmp_ppm2
329 # Kill off the xli or xv subprocess, if it's running
332 if [ "$pid" != "" ]; then
334 if [ "$verbose" = true ]; then
335 echo "killing pid $pid..." >&2
338 # need to do this to avoid "6898 Terminated" messages!
339 # apparently one can't redirect the output of the builtin `kill' command.
340 # ( sh -c "kill $pid" ) >/dev/null 2>/dev/null </dev/null
342 # wtf? that doesn't work either. Is it writing to /dev/tty??
343 kill $pid >/dev/null 2>&1
349 # called when this process is signalled (for cleanup)
352 if [ "$verbose" = true ]; then
353 echo "trapped signal!" >&2
364 trap my_trap 1 2 3 6 9 13 15
366 if [ "$use_stdin" = true ]; then
372 # Loop grabbing and frobbing images.
374 # If we're running on the root, run xv or xli in the foreground
375 # (with -exit, if xv) and then wait.
377 # If we're running in a window, spawn xv or xli in the background; then
378 # when it's time to put up the new image, kill off the currently-running
381 if [ "$verbose" = true ]; then
384 whack >/dev/null 2>&1
389 if [ ! -s $tmp_ppm3 ]; then
390 echo "$0: no image grabbed" >&2
392 elif [ "$use_stdout" = true ]; then
400 # pnmtosgi < $tmp_ppm3 > $tmp_ppm2
402 mv $tmp_ppm3 $tmp_ppm2
404 if [ -s $tmp_ppm2 ]; then
405 if [ "$verbose" = true ]; then
406 echo "launching $displayer $displayer_args $tmp_ppm2" >&2
410 mv $tmp_ppm2 $tmp_ppm0
411 eval "$displayer $displayer_args $tmp_ppm0 &"
413 # this doesn't work -- leaves xv processes around, instead of stray xset
416 # # cat the file so that we can nuke it without racing against xv.
417 # cat $tmp_ppm2 | $displayer $displayer_args - &
432 # to find stray xv data:
433 # xwininfo -root -children|grep 'xv image comments' | awk '{print "xkill -id ", $1}'