Hi all,
after answering a forums post about Best way to suspend computer when inactive? I thought this would be a good thing for a small Fvwm module and here it is 8)
[code]#!/usr/bin/perl
#-----------------------------------------------------------------------
File: FvwmSleep
Version: 1.0.0
Licence: GPL 2
Description: Module to run a fvwm function after a defined X11 idle time
of the user.
Author: Thomas Funk t.funk@web.de
Created: 2015-02-12
Changed:
#-----------------------------------------------------------------------
To get a man page:
pod2man --center=" " --section=1 --release=“1.0.0” FvwmSleep > FvwmSleep.1
To get a Html page:
pod2html --noindex --infile FvwmSleep --outfile FvwmSleep.html
use warnings;
use strict;
use lib fvwm-perllib dir
;
use FVWM::Module;
use Text::ParseWords;
use Scalar::Util qw(looks_like_number);
$SIG{ALRM} = &polling;
my $debug = 0;
my $stopIsWanted = 0;
my $sleepafter;
my $pollinterval;
my $exec_string;
defaults
my %initialConfig = (
‘Waittime’ => ‘60’,
‘Pollinterval’ => ‘10’,
‘Use’ => ‘’
);
module initialization ------------------------------------------------
my $module = new FVWM::Module(
Name => ‘FvwmSleep’,
Mask => M_CONFIG_INFO | M_END_CONFIG_INFO | M_STRING,
Debug => $debug,
);
$module->showMessage(“starting”);
read configuration if available
my $configTracker = $module->track(“ModuleConfig”);
foreach (keys %{$configTracker->data}) {
$initialConfig{$} = $configTracker->data->{$};
}
set signal handlers
$module->addHandler(M_CONFIG_INFO, &readConfig);
$module->addHandler(M_END_CONFIG_INFO, sub {
$module->addHandler(M_STRING, &getMsg);
&polling;
});
$module->send(“Send_ConfigInfo”);
$module->eventLoop;
configuration --------------------------------------------------------
sub readConfig {
my ($module, $event) = @_;
my $modname = $module->name;
return unless $event->_text =~ /^*$modname(.*)$/;
processConfig($1);
}
sub processConfig {
my ($s) = @_;
my ($option, $args)= $s =~/\s*(\w+)\s*(.*)/;
my %opts = (
Waittime => sub {$sleepafter = ($args*60);},
Use => sub {$exec_string = $args;},
Pollinterval => sub {$pollinterval = $args;}
);
if (defined $opts{$option}) {
$opts{$option}() ;
} else {
$module->showMessage("Unknown option \"$option\"");
}
}
commands -------------------------------------------------------------
sub getMsg {
my ($module, $event) = @_;
processCmd($event->_text);
}
sub processCmd {
my ($s) = @_;
my ($command, $args)= $s =~/\s*(\w+)\s*(.*)/;
my %cmd = (
settime => sub {$sleepafter = looks_like_number($args) ? $args : $sleepafter;},
interval => sub {$pollinterval = looks_like_number($args) ? $args : $pollinterval;},
stop => sub {&stopIsWanted;},
);
if (defined $cmd{$command}) {
$cmd{$command}() ;
$module->showMessage("debug: $command");
} else {
$module->showMessage("unknown command \"$command\"");
}
}
sub polling {
my $current_idle = xprintidle
;
# get it in seconds
my $seconds=sprintf("%.0f", $current_idle/1000);
$module->debug(“Idle time: $seconds s”);
unless ($seconds >= $sleepafter) {
$module->debug("Pollinterval: $pollinterval");
alarm($pollinterval);
return;
}
$module->send($exec_string);
#&stopIsWanted;
}
sub stopIsWanted {
alarm(0);
&stopModule;
}
sub stopModule {
$module->showMessage(“stopped”);
$module->terminate;
}
END
----------------------------------------------------------------------
=head1 NAME
FvwmSleep - Module to run a fvwm function after a defined X11 idle time
of the user.
=head1 SYNOPSIS
I
FvwmSleep can only be invoked by fvwm. Command line invocation of the
FvwmSleep module will not work.
=head1 DESCRIPTION
This module polls in periodical intervals the X11 idle time counter and
executes a fvwm function if the counter is equal to the set time in minutes.
It needs I. Most distribution has it (Debian, Ubuntu, Arch, Gentoo).
=head1 INVOCATION
FvwmSleep can be invoked by inserting the line ‘Module FvwmSleep’
in the .fvwm2rc file. This should be placed in the StartFunction if
FvwmSleep is to be spawned during fvwm’s initialization.
=head1 CONFIGURATION OPTIONS
The following commands are understood by FvwmSleep:
*FvwmSleep: Pollinterval I<>
Sets the poll interval the X11 idle counter is checked. Default is 10
seconds.
*FvwmSleep: Waittime I<>
Sets the time how long FvwmSleep should wait before executing the
fvwm command or function.
*FvwmSleep: Use I<>
Sets fvwm command or function which should executing after I<Waittime>.
Example:
# Module to set system in suspend after a defined time
DestroyModuleConfig FvwmSleep: *
*FvwmSleep: Pollinterval 60
*FvwmSleep: Waittime 15
*FvwmSleep: Use 'FuncSuspend'
=head1 DYNAMICAL ACTIONS
A running FvwmSleep instance may receive some dynamical actions.
This is achived using the fvwm command
SendToModule FvwmSleep <action> <params>
Supported actions:
settime I<>
Changes the value of I<Waittime>.
interval I<>
Changes the value of I<Polltime>.
stop
Terminates FvwmSleep.
=head1 BUGS
Bug reports can be sent to Me (see below) ^^.
=head1 COPYRIGHTS
FvwmSleep stands under the GPL V2. © 2015.
=head1 AUTHOR
Thomas Funk t.funk@web.de.[/code]
To use it for suspending add the function ‘FuncSuspend’ from the post above in your .fvwm2rc.
Also the config part described in the help]DestroyModuleConfig FvwmSleep: *
*FvwmSleep: Pollinterval 60
*FvwmSleep: Waittime 15
*FvwmSleep: Use FuncSuspend[/code]
Copy the module into /usr/share/fvwm and make it executable. If you haven’t access create in .fvwm directory a directory e.g. modules and add to your .fvwm2rc
ModulePath $[FVWM_USERDIR]/lib:+
Now ~/.fvwm/lib is in Fvwms module path
Last step add FvwmSleep to your Startfunction]AddToFunc StartFunction
- I Module FvwmSleep[/code]
That’s it. After a restart of Fvwm the module is running in the background and after 15 minutes of inactivity (no mouse move or keyboard use) the system will suspend.
Hope you like it.
– Thomas –
[Edit]: I’ve forgot one important point - the module needs xprintidle. Most distribution has it (Debian, Ubuntu, Arch, Gentoo).