#!/usr/bin/perl -w

# This is a simple FVWM module written in perl using the supplied perl library.
# Iconifies new windows, auto-raises windows and terminates itself in 1 minute.
# Either place this module in FVWM_MODULEDIR or run it using the full path.

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

my $newWindowHandler = sub {
	my ($self, $event) = @_;
	#$self->send("Echo M_ADD_WINDOW " . join(", ", %{$event->args}));

	# auto iconify all new windows
	$self->send("Iconify true", $event->_win_id);
};

my $module = new FVWM::Module(
	Name => "FvwmPerlBasedExample",         # used in config and debug
	Mask => M_ADD_WINDOW | M_FOCUS_CHANGE,  # request two these events
);

# do some funny harm
$module->showMessage("started");
$module->send("Beep");
$module->send("All (FvwmConsole) Iconify true");

# undo harm
my $onExit = sub { $_[0]->send("Beep\nAll (FvwmConsole) Iconify false") };

# auto-exit in 60 seconds; a mere die() in ALRM signal handler is ok too
local $SIG{ALRM} = sub { $module->terminate(); };
alarm(60);

# auto-iconify new windows
$module->addHandler(M_ADD_WINDOW, $newWindowHandler);
# auto-raise all windows with delay of 500 msec
$module->addHandler(M_FOCUS_CHANGE, sub {
	my $winId = $_[1]->_win_id;
	$_[0]->send("Deschedule 1234\nSchedule 500 1234 WindowId $winId Raise");
});
# do a clean-up on exit
$module->addHandler(ON_EXIT, $onExit);

# enter the main event loop
$module->eventLoop();

# this is only reached when the mutual connection with fvwm is lost
$module->showMessage("finished");
