#!/usr/bin/perl -w

use lib `fvwm-perllib dir`;
use FVWM::Module;

my $last_win   = 0;
my %config = ();

open( CONFIG, "<", "$ENV{HOME}/.fvwm/fvwmPeteTrans.cfg" );
while( <CONFIG> )
{
        chomp;
        my @a = split( /:/ );
        $config{ $a[0] } = $a[1];
}
close( CONFIG );

sub ChangeWindow
{
        my ($id, $val) = @_;
        my $result = `/usr/bin/xprop -id $id | /bin/grep ^WM_CLASS | cut -d= -f2`;
        $result =~ s/\"|\s//g;

        chomp( $result );

        foreach my $key ( keys %config ) {
                my @ressplit = split( /,/, $result );
                foreach my $test ( @ressplit ) {
                        if( $key eq $test ){
                                my $ex = "";
                                $ex = "/usr/local/bin/transset-df -i " . $id . " $config{$key}";
                                system( $ex );
                        }
                }
        }
}

sub SetWindowOpaque
{
        my ($id) = @_;
        my $ex = "/usr/local/bin/transset-df -i $id 1.0";
        system( $ex );
}

sub callback
{
        my ($module, $event) = @_;
        my $id = $event->_win_id;

        # Test:
        #       If we're leaving a window focus, then
        #       the _win_id will not equal the $last_win
        if( $last_win != $id )
        {
                # We're leaving
                ChangeWindow( $last_win, undef );
                $last_win = $id;
        }
        if( $last_win == $id )
        {
                # We're entering
                SetWindowOpaque( $id, "1.0" );
        }
}

my $module = new FVWM::Module;
$module->addHandler(M_FOCUS_CHANGE, \&callback);
$module->eventLoop;

