#!/usr/bin/perl -w

#
# Copyright (c) 2005 Mike O'Connor
# All rights reserved.
# Author Mike O'Connor <stew@vireo.org>
# 
# code was adapeted from rpws that comes from ratpoison containing the follwing copyright:
# Copyright (C) 2003 Shawn Betts
# Author: Shawn Betts
# 

use strict;
use Fcntl qw (:flock);

my $RATPOISON = $ENV{ "RATPOISON" };
$RATPOISON = "ratpoison" unless $RATPOISON;

my $LOCKFILE = $ENV{ "HOME" }."/.ratpoison/rpwspl.lock";

sub help
{
    system("pod2usage", $0);
    print( "for more detailed documentation run \"perldoc $0\"\n" );
}

sub rp_call
{
    my $result = `$RATPOISON -c "@_"`;
    chomp( $result );
    chomp( $result );
    return $result;
}

sub ws_init_ws
{
    
    my $num = shift;
    
    rp_call( "gnew wspl$num" );
    my $fd = fdump();
    rp_call( "setenv fspl$num $fd" )
}

sub fdump
{
    return rp_call( "fdump" );
}

sub ws_init
{
    my $num = shift;

    # Backup the frames
    my $fd = fdump();

    rp_call( "select -" );
    rp_call( "only" );

    my $i;
    for( my $i = 0; $i < $num; $i++ )
    {
	ws_init_ws( $i );
    }
    
    # Workspace 1 uses the 'default' group.
    # Start in workspace 1.
    $fd = fdump();
    rp_call( "gselect default" );
    rp_call( "setenv fspl1 $fd" );
    rp_call( "setenv wspl 1" );

    # restore the frames
    rp_call( "frestore $fd" );

    if( -e "$LOCKFILE" )
    {
	unlink ("$LOCKFILE" );
    }
}

sub ws_save
{
    my $ws = rp_call( "getenv wspl" );
    my $fd = fdump();
    rp_call( "setenv fspl$ws $fd" );
}

sub ws_restore
{
    my $which = shift;
    
    ws_save();
    
    if( $which == 1 )
    {
	rp_call( "gselect default" );
    }
    else
    {
	rp_call( "gselect wspl$which");
    }
    
    rp_call( "echo Workspace $which" );
    my $last = rp_call( "getenv fspl$which" );
    rp_call( "frestore $last" );
    rp_call( "setenv wspl $which" );
}

my $arg = shift @ARGV || 'help';

if( $arg eq "help" )
{
    help();
}
elsif( $arg eq "init" )
{
    my $num = shift;
    ws_init( $num );
}
else
{
   open LOCK, ">>$LOCKFILE" or die "Cannot open lockfile: $LOCKFILE";
   flock(LOCK, LOCK_EX);
   ws_restore( $arg );
}

__END__

=head1 NAME

rpwspl - Implements multiple workspaces in ratpoison

=head1 SYNOPSIS

 rpwspl init n  - setup rpwspl with n workspaces
 rpwspl help    - this documentation
 rpwspl n       - switch to this workspace


=head1 DESCRIPTION

 B<rpwspl> implements multiple workspaces in ratpoison by making calls
 to fdump, freestore.  It was adapted from rpws which comes with
 ratpoison in the contrib directory.

=head1 USAGE
 
 Here's how I use rpwspl.  I have the following lines in ~/.ratpoisonrc
    
     exec /usr/local/share/ratpoison/rpwspl init 6

     alias rpws1 exec /usr/local/share/ratpoison/rpwspl 1
     alias rpws2 exec /usr/local/share/ratpoison/rpwspl 2
     alias rpws3 exec /usr/local/share/ratpoison/rpwspl 3
     alias rpws4 exec /usr/local/share/ratpoison/rpwspl 4
     alias rpws5 exec /usr/local/share/ratpoison/rpwspl 5
     alias rpws6 exec /usr/local/share/ratpoison/rpwspl 6


 And i have the following in my .xbindkeysrc
     
     "/usr/local/share/ratpoison/rpwspl 1"
           Mod1 + F1

     "/usr/local/share/ratpoison/rpwspl 2"
           Mod1 + F2

     "/usr/local/share/ratpoison/rpwspl 3"
           Mod1 + F3

     "/usr/local/share/ratpoison/rpwspl 4"
           Mod1 + F4

     "/usr/local/share/ratpoison/rpwspl 5"
           Mod1 + F5

     "/usr/local/share/ratpoison/rpwspl 6"
           Mod1 + F6


=head1 FILES

 rpwspl requires use of a lockfile.  It defaults to using
 ~/.ratpoison/rpwspl.lock but this can be changed easily at the top of
 the source code.

=head1 AUTHOR

 Mike O'Connor <stew@vireo.org>

=head1 COPYRIGHT

 Copyright (c) 2005 Mike O'Connor
 All rights reserved.
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

