Module FvwmSleep for automatic suspend when inactive

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)


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

Created: 2015-02-12



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;


my %initialConfig = (
‘Waittime’ => ‘60’,
‘Pollinterval’ => ‘10’,
‘Use’ => ‘’

module initialization ------------------------------------------------

my $module = new FVWM::Module(
Name => ‘FvwmSleep’,
Debug => $debug,


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);


configuration --------------------------------------------------------

sub readConfig {
my ($module, $event) = @_;
my $modname = $module->name;
return unless $event->_text =~ /^*$modname(.*)$/;

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) = @_;

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");


sub stopIsWanted {

sub stopModule {



=head1 NAME

FvwmSleep - Module to run a fvwm function after a defined X11 idle time
of the user.



FvwmSleep can only be invoked by fvwm. Command line invocation of the
FvwmSleep module will not work.


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).


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.


The following commands are understood by FvwmSleep:

*FvwmSleep: Pollinterval I<>

Sets the poll interval the X11 idle counter is checked. Default is 10 

*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>.


# Module to set system in suspend after a defined time
DestroyModuleConfig FvwmSleep: *
*FvwmSleep: Pollinterval 60
*FvwmSleep: Waittime 15
*FvwmSleep: Use 'FuncSuspend'


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>.


Terminates FvwmSleep.

=head1 BUGS

Bug reports can be sent to Me (see below) ^^.


FvwmSleep stands under the GPL V2. © 2015.

=head1 AUTHOR

Thomas Funk[/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 :slight_smile:

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).

Thanks a lot! This is great, not only as a FvwmSleep module, but as an example on how to build local modules!


You’re welcome :slight_smile:

– Thomas –

I’m running SLackware and is doesn’t have xprintidle. Where is the source available for it, so I can build and install it to use with these scripts? I’ve used FVWM-Themes for a while and miss being able to suspend it and resume, rather than going through a full shutdown and restart each morning and night. I’ve tried the pm-utils of Nightshade and it doesn’t work.

According to this, the xprintidle source is avail from here.