rpmsafereduce: revert
[nethome.git] / bin / fup
diff --git a/bin/fup b/bin/fup
index c8e9144..ec166d4 100755 (executable)
--- a/bin/fup
+++ b/bin/fup
@@ -15,10 +15,11 @@ my $USERNAME="Jan Kratochvil";
 my $PASSWORD=&_priv_postget("uzivatel.gtsnovera.cz.pwd");
 my $IFDEV="eth0";
 my $SECS=4;
-my $RX_THRESHOLD=200000;       # 226085
-my $TX_THRESHOLD=200000;       # 291251
+my $RX_THRESHOLD=210000;       # 226085
+my $TX_THRESHOLD=210000;       # 291251
 my $STABILIZE_OFF=1;
-my $STABILIZE_ON=5*60;
+my $STABILIZE_ON=4*60;
+my $INPUT_IDLE=10*60;
 
 $RX_THRESHOLD=$RX_THRESHOLD*$SECS/8;
 $TX_THRESHOLD=$TX_THRESHOLD*$SECS/8;
@@ -116,6 +117,64 @@ my($what,$length)=@_;
        change($what);
 }
 
+use User::Utmp;
+
+# Users respected for the 'idle' state (see $IdleMax):
+my @ValidUsers=qw(root lace jkratoch);
+
+sub useridle()
+{
+       my %valid_users=map(($_=>1),@ValidUsers);
+       my($idlebest,$linebest);
+       for my $utmp (User::Utmp::getut()) {
+               local $_;
+               next if defined($_=$utmp->{"ut_type"}) && $_!=&User::Utmp::USER_PROCESS();
+               next if defined($_=$utmp->{"ut_user"}) && !$valid_users{$_};
+               my $line="/dev/".$utmp->{"ut_line"};
+               my $atime=(stat $line)[8];
+               my $what="user \"".($utmp->{"ut_user"} || "<local>")."\", line \"$line\"";
+               warn "Unable to stat $what" and next if !$atime;
+               my $idle=time()-$atime;
+               warn "atime in future for $what" and next if $idle<0;
+               next if defined $idlebest && $idlebest<=$idle;
+               $idlebest=$idle;
+               $linebest=$line;
+               }
+       return !wantarray() ? $idlebest : ($idlebest,$linebest);
+}
+
+sub input_idle()
+{
+       return useridle() > $INPUT_IDLE;
+}
+
+sub mplayer_running()
+{
+       local *F;
+       open F,"/proc/net/unix" or die;
+       my %unix;
+       <F>;
+       local $_;
+       while (<F>) {
+               /^(?:\S+\s+){6}(\d+)\s/ or die;
+               $unix{$1}=1;
+               }
+       close F or die;
+
+       local $_;
+       while (</proc/*/exe>) {
+               (my $fdname=$_)=~s{/exe$}{/fd};
+               $_=readlink or next;
+               m{/mplayer$} or next;
+               while (<$fdname/*>) {
+                       $_=readlink or next;
+                       my $inode=/^\Qsocket:[\E(\d+)\Q]\E$/ or next;
+                       return 1 if $unix{$1};
+                       }
+               }
+       return;
+}
+
 for (;;) {
        local *F;
        open F,"/proc/net/dev" or die;
@@ -128,11 +187,18 @@ for (;;) {
                $on_now=0 if ($rx-$rx_last) > $RX_THRESHOLD;
                $on_now=0 if ($tx-$tx_last) > $TX_THRESHOLD;
                }
+       my $print=$on_now;
+       my $input_idle=input_idle();
+       my $mplayer_running=mplayer_running() if $input_idle;
+       $on_now=1 if $input_idle && !$mplayer_running;
        $rx_last=$rx;
        $tx_last=$tx;
        push @history,$on_now;
-       print(($ignore_read ? "_" : $on_now));
-       history(1,$STABILIZE_ON);
+       $print=($print ? "." : "v") if $input_idle;
+       $print="M" if $mplayer_running;
+       $print="*" if $ignore_read;
+       print($print);
+       history(1,(($input_idle && !$mplayer_running) ? 0 : $STABILIZE_ON));
        history(0,$STABILIZE_OFF);
        $ignore_read=$ignore_write;
        $ignore_write=0;