My first fvwm module

I’ve been using FVWM for some time now, but have never tried writing any kind of modules to extend it. Yesterday (Note, a while ago, but this post wasn’t migrated properly…) I tried it out, and I’ve got my first working FVWM module.

I wrote the module in Perl,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/perl -w
 
use lib `fvwm-perllib dir`;
use FVWM::Module;
 
my $last_win = 0;
my %config   = ();
my %class    = ();
my $debug    = 0;
 
open( DEBUG, ">>/tmp/FvwmPeteTrans.log" ) if $debug;
open( CONFIG, "<", "$ENV{HOME}/.fvwm/fvwmPeteTrans.cfg" );
while (<CONFIG>) {
    chomp;
    my @a = split(/:/);
    print DEBUG "$a[0] = $a[1]\n" if $debug;
    $config{ $a[0] } = $a[1];
}
close(CONFIG);
 
sub ChangeWindow {
    my ( $id, $val ) = @_;
    # old way
    #my $result = `/usr/bin/xprop -id $id | /bin/grep ^WM_CLASS | cut -d= -f2`;
 
    foreach my $key ( keys %config ) {
        my @ressplit = split( /,/, $class{$id} );
        foreach my $test (@ressplit) {
            if ( $key eq $test ) {
                my $ex = "";
                $ex
                    = "/usr/bin/transset-df -i "
                    . $id
                    . " $config{$key} &";
                print DEBUG "$ex\n" if $debug;
                system( $ex ) if ! $debug;
            }
        }
    }
}
 
sub SetWindowOpaque {
    my ($id) = @_;
    my $ex = "/usr/bin/transset-df -i $id 1.0 &";
    print DEBUG "$ex\n" if $debug;
    system( $ex ) if ! $debug;
}
sub callback {
    my ( $module, $event ) = @_;
    my $id = $event->_win_id;
 
    # First see if we're tracking the window
    addwindow( $module, $event );
 
    # 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" );
    }
}
 
sub addwindow {
        my ( $module, $event ) = @_;
        my $id = $event->_win_id;
        print DEBUG "addwindow $id\n" if $debug;
        # get the class of the window id
        #
        my $result = "";
        my @results = `xprop -id $id`;
        LINE: foreach my $resline ( @results ) {
                chomp $resline;
                print DEBUG "inspecting $resline\n";
                next LINE unless ( $resline =~ /WM_CLASS.*/ );
                print DEBUG "Found $resline\n" if $debug;
                my @class = split( /=/, $resline );
                $result = @class[1];
                print DEBUG "Assigning $result to result\n" if $debug;
        }
        $result =~ s/\"|\s//g;
        print DEBUG "Assigning $result to \$class{$id}\n" if $debug;
        $class{ $id } = $result;
}
 
sub destroywindow {
        my ( $module, $event );
        my $id = $event->_win_id;
        print DEBUG "removing $id from class\n" if $debug;
        delete $class{$id};
}
 
my $module = new FVWM::Module;
$module->addHandler( M_FOCUS_CHANGE, \&callback );
$module->eventLoop;