#!/usr/bin/perl -w

use strict;
use Tk;
use Tk::DialogBox;
use Tk::LabEntry;

my($VERSION) = q$Revision: 1.1 $ =~ /(\d+\.\d+)/;

# Tk mainwindow
my $mw = new MainWindow;

$mw->title("RSI");

# menu structure
my $menu = $mw->Menu(qw/-type menubar/);
$mw->configure(-menu => $menu);
my $file_menu = $menu->cascade(qw/-label ~File -tearoff 0/);
$file_menu->command(-label => 'Opties', -command => \&opties);
$file_menu->separator;
$file_menu->command(-label => 'Quit', -command => \&quit);
my $help_menu = $menu->cascade(qw/-label ~Help -tearoff 0/);
$help_menu->command(-label => 'About', -command => \&about);

my $pauze = 180; # 3 minuten
my $werktijd = 1800; # half uur
my $laatsterust = time;
my $laatstewerk = time;
my $keyint = get_keyboard();

$mw->repeat(10000, \&check_idle);

MainLoop;

# get keyboard interrupts
sub get_keyboard {
    if ( ! open(INT, "/proc/interrupts") ) {
	warn "Cannot open /proc/interrupts: $!\n";
	return;
    }
    my @ints;
    while ( <INT> ) {
	next unless /^\s*1:\s*((?:\d+\s+)+)/;
	@ints = split ' ', $1;
	last;
    }
    close INT;
    if ( ! @ints ) {
	warn "No INT1 (keyboard) interrupts in /proc/interrupts!\n";
	return;
    }
    my $ints = 0;
    $ints += shift @ints while @ints;
    $ints;
}

sub check_idle {
    my $newkey = get_keyboard();
    if ( $newkey != $keyint ) {
	$keyint = $newkey;
	$laatstewerk = time;
	if ( $laatsterust + $werktijd < time ) {
	    Alertw(10000, 'Pas op voor RSI!',
		"Je hebt al " . leesbaretijd($werktijd) .
		" geen pauze meer gehad!\nAls je nu niet onmiddelijk " .
		leesbaretijd($pauze) . " rust neemt krijg je RSI hoor!"
	    );
	}
    }
    elsif ( $laatstewerk + $pauze < time ) {
	# mooi, een pauze.
	$laatsterust = time;
    }
}

sub Alertw {
    my($time, $title, $msg) = @_;

    my $alertw = $mw->Toplevel(-title => $title);
    $alertw->bell;
    $alertw->Label(-text => $msg)->pack;
    $alertw->Button(-text => 'OK',
		    -command => sub {
			$alertw->destroy if $alertw;
			undef $alertw
		    } 
	)->pack;
    my $w = $alertw->reqwidth();
    my $h = $alertw->reqheight();
    my $x = int(($mw->screenwidth - $w) / 2);
    my $y = int(($mw->screenheight - $h) / 2);
    $alertw->geometry("+$x+$y");
    $mw->after($time, sub { $alertw->destroy if $alertw } );
}

sub quit {
    $mw->destroy;
}

sub about {
    $mw->messageBox(qw/-type OK -title About/,
	-message => "RSI $VERSION (C) JohnPC 2000");
}

sub opties {
    my $lwerk = $werktijd;
    my $lpauze = $pauze;

    my $d = $mw->DialogBox( -title => "RSI opties",
			    -buttons => [qw/OK Cancel/] );
    $d->add(Label => -text => "Configureer RSI (was het maar zo makkelijk)")
	->pack();
    $d->add(LabEntry =>
	-label => "Maximale werktijd zonder pauze (in s)",
	-textvariable => \$lwerk,
	-width => 5,
	-labelPack => [ -side => "left" ])->pack();
    $d->add(LabEntry =>
	-label => "Minimale pauze (in s)",
	-textvariable => \$lpauze,
	-width => 5,
	-labelPack => [ -side => "left" ])->pack();
    
    my $answ = $d->Show;
    return unless $answ eq "OK";
    
    $werktijd = $lwerk if $lwerk =~ /^\d+$/;
    $pauze = $lpauze if $lpauze =~ /^\d+$/;
}

sub leesbaretijd {
    my $sec = shift;

    my $hr = int($sec / 3600);
    $sec -= $hr * 3600;
    my $min = int($sec / 60);
    $sec -= $min * 60;

    my @str;
    push @str, "$hr uur" if $hr;
    push @str, "$min minuten" if $min > 1;
    push @str, "1 minuut" if $min == 1;
    push @str, "$sec seconden" if $sec > 1;
    push @str, "1 seconde" if $sec == 1;
    join(" ", @str);
}
