Add in Wayne Scott's window tiling algorithm (reduce number of rows rather

then overall term size)
Remove code to check existance of processes using PID's
Remove code to fork when opening xterm
  - both of these can be removed due to checking if pipes exist instead
  - should also speed code up as not forking as often any more
This commit is contained in:
duncan_ferguson 2004-05-04 15:08:01 +00:00
parent c30dba9af3
commit 1b14c9235b

View file

@ -94,8 +94,7 @@ $SIG{CHLD}='IGNORE';
use Getopt::Std; # command line parsing, incase someone uses -v or -h
my %options;
# NOTE - option x is hidden and should never be called directly
getopts('x:l:hdvncgst:T:', \%options);
getopts('l:hdvncgst:T:', \%options);
my $TIOCSTI = "";
@ -116,83 +115,6 @@ unless ($ENV{TERM}) {
my $ospeed = $termios->getospeed;
my $terminal = Tgetent Term::Cap { TERM => undef, OSPEED => $ospeed };
# This is the process by which we get around requiring setuid while also
# only running from one script
if($options{x})
{
if( ! -p $options{x})
{
die ("cssh called incorrectly\n");
}
my $pid=fork();
if(!defined($pid))
{
die("Could not fork: $!");
}
if($pid==0)
{
# this is the child
exec(@ARGV) || die("Could not exec within x: $!");
} else {
# this is the parent
use IO::Select;
use IO::Handle;
my $READER;
# open pipe for reading from
if(!sysopen($READER, $options{x}, O_RDONLY))
{
unlink($options{x});
die ("Cannot open pipe for reading: $!");
}
# Don't allow the read to stop the prog
$READER->blocking(0);
my $reader=new IO::Select($READER) or die("$!");;
my @ready;
OUTTER:
{
while()
{
@ready = $reader->can_read(0.25);
foreach my $fh (@ready)
{
if($fh == $READER)
{
while(read($READER,my $char,1))
{
last OUTTER if(ord($char) == KILLOFF);
last if(! -p $options{x});
unless(ioctl(STDIN,$TIOCSTI,$char))
{
print "failed to write to client\n";
last OUTTER;
}
}
}
}
# if we can no longer write to client, it is gone
last unless(kill(0,$pid));
}
}
kill(9,$pid) if (kill(0,$pid));
unlink($options{x}) if(-p $options{x});
exit;
}
die("Weird error - should never get here: $!");
}
# Set up some defaults
my %user_config;
$user_config{'default.terminal'}="xterm";
@ -204,7 +126,9 @@ $user_config{'default.key_addhost'}="Control-plus";
$user_config{'default.key_clientname'}="Alt-n";
$user_config{'default.variables'}="no";
$user_config{'default.title_number'}="no";
$user_config{'default.titlebar_size'}=30;
$user_config{'default.term_size'}="80x24";
$user_config{'default.term_font'}="6x13";
$user_config{'default.always_tile'}="never";
# Now read in the system config file
@ -400,7 +324,7 @@ foreach (reverse(@cmdargs))
# hash to contain all required info about windows used
# format of %servers is:
# [0] active
# [1] process id
# [1] process id # no longer required
# [2] pipe file name
# [3] pipe file handle
# [4] window geometry if tiling
@ -416,65 +340,41 @@ my ($term_height, $term_width, $rows, $cols) = (0,0,0,0);
if($options{g})
{
# Now get the screen height for tiling windows later
my $screen_height=$mw->screenheight;
my $screen_height=$mw->screenheight - $user_config{'default.titlebar_size'};
my $screen_width=$mw->screenwidth;
#warn "screen_height=$screen_height\n";
#warn "screen_width=$screen_width\n";
RECALC:
my $finfo = `xlsfonts -ll -fn $user_config{'default.term_font'}`;
my $fwidth = ($finfo =~ /^\s*max\s+(\d+)/m)[0];
my $fheight = ($finfo =~ /^\s*PIXEL_SIZE\s*(\d+)/m)[0];
#warn "font = $fwidth x $fheight\n";
my $term_cols = 80; # make user choice
# Get the term width and height
$term_height=$user_config{'default.term_size'};
$term_height=~ s/.*x//;
$term_height= ( $term_height * 13 ) + 5 ; # convert loosly to pixels
$term_width=$user_config{'default.term_size'};
$term_width=~ s/x.*//;
$term_width= ( $term_width * 6 ) + 20 ; # convert loosly to pixels
# Convert character size to pixels
$rows=int(( $screen_height - ($screen_height % $term_height )) / $term_height);
$cols=int(( $screen_width - ( $screen_width % $term_width )) / $term_width);
$term_width = ( $term_cols * $fwidth ) + 8 ; # convert loosly to pixels
my $total=@cmdargs;
#warn "total=$total\n";
#warn "term_height=$term_height\n";
#warn "term_width=$term_width\n";
#warn "cols=$cols\n";
#warn "rows=$rows\n";
#warn "default.term_size=$user_config{'default.term_size'}\n";
$cols = int($screen_width / $term_width);
$rows = int($total/$cols + 0.99999);
# If we cannot fit them all in, decrease size of windows and rework out
if($total > ($rows * $cols ))
{
my $term_height=$user_config{'default.term_size'};
$term_height=~ s/.*x//;
my $term_width=$user_config{'default.term_size'};
$term_width=~ s/x.*//;
my $term_rows = int(($screen_height - 10 * $rows) / $fheight / $rows);
$term_height = $term_rows * $fheight + 10;
print "term_rows=$term_rows\n" if($options{d});
$term_rows= $term_rows > 24 ? 24 : $term_rows;
if($term_height>10 && $term_width > 40)
{
$term_height-=2;
$term_width-=5;
$user_config{'default.term_size'}="${term_width}x${term_height}";
$user_config{'default.term_size'} = "${term_cols}x${term_rows}";
warn "WARNING: too many too big terminals to fit; reducing in size to ",
$user_config{'default.term_size'}, "\n";
#warn "total=$total\n";
#warn "term_height=$term_height\n";
#warn "term_width=$term_width\n";
#warn "cols=$cols\n";
#warn "rows=$rows\n";
#warn "default.term_size=$user_config{'default.term_size'}\n";
goto RECALC;
} else {
warn "WARNING: cannot tile windows - not attempting to tile\n";
delete($options{g});
}
}
warn "total=$total\n" if($options{d});
warn "term_height=$term_height\n" if($options{d});
warn "term_width=$term_width\n" if($options{d});
warn "cols=$cols\n" if($options{d});
warn "rows=$rows\n" if($options{d});
warn "default.term_size=$user_config{'default.term_size'}\n" if($options{d});
}
@ -520,10 +420,6 @@ sub open_windows
$cur_col=$cols;
}
# If we are tiling, reverse the array we work on, else we get the windows
# out backwards
@_ = reverse(@_);
#warn "start cols:$cols, rows:$rows\n";
#warn "start cur_col=$cur_col\n";
#warn "start cur_row=$cur_row\n";
@ -560,7 +456,7 @@ sub open_windows
# Now we create the fifo pipe file
mkfifo($servers{$serv_name}[2], 0600) or die("Cannot create pipe: $!");
my $size= $options{g} ? "-geometry $user_config{'default.term_size'}" : "";
my $size= $options{g} ? "-font $user_config{'default.term_font'} -geometry $user_config{'default.term_size'}" : "";
my $place=$size;
# If tiling, start at the bottom right and work backwards to ensure the
@ -568,12 +464,16 @@ sub open_windows
if($options{g})
{
#warn "in cur_col=$cur_col\n";
#warn "in cur_row=$cur_row\n";
warn "in cur_col=$cur_col\n" if($options{d});
warn "in cur_row=$cur_row\n" if($options{d});
# Include extra space (+30) required on first line for title bar here
$place=$size."+".$cur_col*$term_width."+".(($cur_row*$term_height)+30);
#print "place=$place\n";
# Also cater for the size of the titlebar if necessary
#$place=$size."+".$cur_col*$term_width."+".(($cur_row*$term_height)+28);
my $x = $cur_col * $term_width;
my $y = ($cur_row*$term_height) + $user_config{'default.titlebar_size'};
$place="$size+$x+$y";
print "place=$place\n" if($options{d});
$cur_col -= 1;
@ -588,14 +488,14 @@ sub open_windows
#warn "out cur_row=$cur_row\n";
}
$servers{$serv_name}[1]=fork();
#$servers{$serv_name}[1]=fork();
if(!defined($servers{$serv_name}[1]))
{
# unset => fork failed for whatever reason
warn "Cannot fork: $!";
exit_prog();
} elsif($servers{$serv_name}[1] == 0) {
#if(!defined($servers{$serv_name}[1]))
#{
# # unset => fork failed for whatever reason
# warn "Cannot fork: $!";
# exit_prog();
#} elsif($servers{$serv_name}[1] == 0) {
# child => fork returned 0
my $KILLOFF=KILLOFF();
@ -606,8 +506,8 @@ sub open_windows
# Start up the terminal via ourselves so the pipes are in place
# NOTE - ssh -x to disable forwarding of X11 sessions due to problem
# on redhat machines
exec("$user_config{'default.terminal'} $user_config{'default.terminal_options'} $place -title '$method:$serv' -e $FindBin::Bin/cchp $debug -x $servers{$serv_name}[2] -y $TIOCSTI -z $KILLOFF $path_method -x $serv") or warn("Could not exec session to $serv: $! ");
} else {
system("$user_config{'default.terminal'} $user_config{'default.terminal_options'} $place -title '$method:$serv' -e $FindBin::Bin/cchp $debug -x $servers{$serv_name}[2] -y $TIOCSTI -z $KILLOFF $path_method -x $serv &") or warn("Could not exec session to $serv: $! ");
#} else {
# parent => fork return process id of child
if(!sysopen($servers{$serv_name}[3], $servers{$serv_name}[2], O_WRONLY))
@ -627,7 +527,7 @@ sub open_windows
syswrite($servers{$serv_name}[3], "setenv CSSH_CLIENT=".$serv.chr(13));
syswrite($servers{$serv_name}[3], "setenv CSSH_SERVER=".hostname.chr(13));
}
}
#}
}
}
@ -797,6 +697,7 @@ $mw->bind('<Key>' => sub {
if (!(keys(%servers)))
{
#no servers within hash array
print "Exit without servers in hash\n" if($options{d});
exit if $Tk::event->k == 39; # <CTRL>-D pressed, so exit
}
@ -863,8 +764,12 @@ sub send_character {
$mw->repeat(500, sub{
foreach (keys(%servers))
{
unless (checkProcID($servers{$_}[1]))
#print "In repeat checking $_\n" if ($options{d});
#unless (checkProcID($servers{$_}[1]))
if(! -p $servers{$_}[2])
{
print "Looking at $servers{$_}[2]\n" if($options{d});
print "Removing $_ from hosts menu\n" if($options{d});
delete_host($_);
}
}
@ -877,6 +782,7 @@ $mw->repeat(500, sub{
# Do this neatly...
sub exit_prog()
{
print "Exiting the program neatly\n" if($options{d});
foreach (keys(%servers))
{
if($servers{$_}[0])
@ -885,6 +791,7 @@ sub exit_prog()
{
# send the children die signal - unused ASCII char
syswrite($servers{$_}[3], chr(KILLOFF));
unlink($servers{$_}[2]);
}
}
}
@ -940,12 +847,17 @@ for (sort(keys(%servers)))
# Remove any user@ if it is the current user
$serv=~s/.*@// if($serv =~ /$ENV{LOGNAME}@/);
if(checkProcID($servers{$_}[1]))
#if(checkProcID($servers{$_}[1]))
print "Checking add in $_ with $servers{$_}[2]\n" if($options{d});
if(-p $servers{$_}[2])
{
print "Adding in $_ to hosts menu\n" if($options{d});
$hosts_menu->checkbutton(
-label=>"$serv",
-variable=>\$servers{$_}[0],
);
} else {
print "pipe file gone: $servers{$_}[2] on $_\n" if($options{d});
}
}
###
@ -1075,10 +987,10 @@ sub setup_OS()
}
# func to check if a process is still around and functioning
sub checkProcID
{
return kill(0,$_[0]);
}
#sub checkProcID
#{
#return kill(0,$_[0]);
#}
__END__
@ -1411,6 +1323,14 @@ L<Config::Simple>
# Moved to sf.net cvs
#
# $Log$
# Revision 2.13 2004/05/04 15:08:01 duncan_ferguson
# Add in Wayne Scott's window tiling algorithm (reduce number of rows rather
# then overall term size)
# Remove code to check existance of processes using PID's
# Remove code to fork when opening xterm
# - both of these can be removed due to checking if pipes exist instead
# - should also speed code up as not forking as often any more
#
# Revision 2.12 2004/05/04 09:32:18 duncan_ferguson
# Use -x in ssh connection string to disabled X forwarding
#