#!/afs/athena/contrib/perl/p
# Idle display program   Seth Finkelstein (sethf@athena.mit.edu)
# When run, it retrieves a filename from a specified list, then calls
# an X display program to write the image to a window. If run as
# root, daemon, or nobody, it assumes it's being used as an idle display,
# and attempts to write to an Athena idle screensaver window. Otherwise,
# the root window is used.
#
# Version of Apr 26 1992

$idlefile = "/afs/athena/contrib/graphics/images/idle/esg/display.list";
# List of images. Format: Full path names, one per line. # is a comment char.
$credit = "/etc/athena/cred.xwd";
# Additional image to be added to all pictures

open(ERRLOG,">>/usr/tmp/idle.errs") || die "Can't open errlog";
$fatal = 1; $warn = 0;

&init;
if (!defined(@files)) { &logerr("No files to show",$fatal); }

$hname = `hostname`;
chop($hname);

$display = "$hname:0";


$errs = 0;
getafile:

srand;
$which = int(0.5 + rand($#files - 1));

if (!(-e $files[$which])) {
  $errs++;
  if($errs < 10) {
    goto getafile;
  }
  else {
    &logerr("Couldn't get a file to show",$fatal);
  }
}

$saved = 0; $root = 0;
@wins = `/usr/athena/bin/xlswins -l -d $display 2>&1`;
for (@wins) {
  if (/(0x\d+).*X Root Window.*(\d+)x(\d+)/) { $winid = $1; $screenx = $2; $screeny = $3; $root = 1;}
  if (/2000x2000/) { $saved = 1; split(' '); $winid = $_[1]; last; }
}

if ($> == 0 || $> == 1 || $> == 32767) {
  if ($saved == 0) { &logerr("No screensaver window",$fatal); }
}
else {
  if ($root == 0) { &logerr("No root window id",$fatal); }
}
# DWIM

$command = "/afs/athena/contrib/graphics/@sys/xloadimage -display $display -border black -center -fullscreen -quiet -windowid $winid $files[$which]";

if (defined($credit) && (-e $credit)) {
  $xpoint = int(0.5 + rand(400));
  $ypoint = 500 + int(0.5 + rand(200));
  $command .= " -merge -at $xpoint,$ypoint -invert $credit";
}

$x = `echo Showing $files[$which] 2>&1 > /dev/tty`; 
if ($x !~ /^\s*$/) { &logerr($x,$warn); }
$x = `$command 2>&1`;
if ($x !~ /^\s*$/) { &logerr($x,$warn); }

close(ERRLOG);

sub init {
  if (defined($idlefile) && (-e $idlefile)) {
    open(IFILE,"< $idlefile") || die "Can't open idle display list $idlefile\n";
    while (<IFILE>) {
      s/\s+//g;
      s/#.*//g;
      next if (/^#/); # Comments
      next if (/^$/); # Skip blank lines
      next if (/[]["'`|;?*!&<>(){}]/); # Disallow anything with shell chars
      push (@files,$_);
    }
  }
  else {
#   @files = ("an","array","of","your","favorites","here");
  }
}

sub logerr {
  local($errmess) = $_[0];
  local($isfatal) = $_[1];
  print ERRLOG join(' ',localtime(time)),"  ";
  print ERRLOG "Error: ",$errmess,"\n";
  if ($isfatal == $fatal) { exit; }
}
