#!/usr/bin/perl -w

use strict;
use lib `fvwm-perllib dir`;
use FVWM::Module;
use IO::File;

my $runXmessage = 0;
my $module = new FVWM::Module(
	Name => "FvwmPerlBasedWinList",
	Mask => M_WINDOW_NAME | M_END_WINDOWLIST,
	EnableOptions => { "x" => \$runXmessage },
);
my $fh = $runXmessage?
	new IO::File "| xmessage -geometry 240x300 -title 'Window List' -file -":
	\*STDERR;

# Register the event handlers
$module->addHandler(M_WINDOW_NAME, sub {
	my ($self, $event) = @_;
	printf $fh " 0x%07lx | %s\n", $event->_win_id, $event->_name;
});

# This one signals that the module should be terminated
$module->addHandler(M_END_WINDOWLIST, sub {
	print $fh "-----------+--------------------\n";
	close $fh;
	$module->terminate;
});

print $fh "-----------+--------------------\n";
print $fh " Window id | Name\n";
print $fh "-----------+--------------------\n";

# Ask FVWM to send us its list of windows
$module->send("Send_WindowList"); 

# Enter the main loop
$module->eventLoop;
