1#!/usr/bin/env perl 2# $XTermId: resize.pl,v 1.6 2017/01/22 18:34:06 tom Exp $ 3# ----------------------------------------------------------------------------- 4# this file is part of xterm 5# 6# Copyright 2004-2014,2017 by Thomas E. Dickey 7# 8# All Rights Reserved 9# 10# Permission is hereby granted, free of charge, to any person obtaining a 11# copy of this software and associated documentation files (the 12# "Software"), to deal in the Software without restriction, including 13# without limitation the rights to use, copy, modify, merge, publish, 14# distribute, sublicense, and/or sell copies of the Software, and to 15# permit persons to whom the Software is furnished to do so, subject to 16# the following conditions: 17# 18# The above copyright notice and this permission notice shall be included 19# in all copies or substantial portions of the Software. 20# 21# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 22# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 23# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 24# IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY 25# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 26# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 27# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 28# 29# Except as contained in this notice, the name(s) of the above copyright 30# holders shall not be used in advertising or otherwise to promote the 31# sale, use or other dealings in this Software without prior written 32# authorization. 33# ----------------------------------------------------------------------------- 34# resize.sh rewritten into Perl for comparison. 35# See also Term::ReadKey. 36 37use strict; 38use warnings; 39 40use IO::Handle; 41 42sub write_tty { 43 open TTY, "+</dev/tty" or die("Cannot open /dev/tty\n"); 44 autoflush TTY 1; 45 print TTY @_; 46 close TTY; 47} 48 49sub get_reply { 50 open TTY, "+</dev/tty" or die("Cannot open /dev/tty\n"); 51 autoflush TTY 1; 52 my $old = `stty -g`; 53 system "stty raw -echo min 0 time 5"; 54 55 print TTY @_; 56 my $reply = <TTY>; 57 close TTY; 58 system "stty $old"; 59 return $reply; 60} 61 62sub csi_field { 63 my $first = $_[0]; 64 my $second = $_[1]; 65 $first =~ s/^[^0-9]+//; 66 while ( --$second > 0 ) { 67 $first =~ s/^[\d]+//; 68 $first =~ s/^[^\d]+//; 69 } 70 $first =~ s/[^\d]+.*$//; 71 return $first; 72} 73 74our $original = get_reply("\x1b[18t"); 75our $high; 76our $wide; 77 78if ( defined($original) and ( $original =~ /\x1b\[8;\d+;\d+t/ ) ) { 79 $high = csi_field( $original, 2 ); 80 $wide = csi_field( $original, 3 ); 81 printf "parsed terminal size $high,$wide\n"; 82} 83else { 84 die "Cannot get current terminal size via escape sequence\n"; 85} 86 87# 88our $maximize = get_reply("\x1b[19t"); 89our $maxhigh; 90our $maxwide; 91 92if ( defined($maximize) and ( $maximize =~ /^\x1b\[9;\d+;\d+t/ ) ) { 93 $maxhigh = csi_field( $maximize, 2 ); 94 $maxwide = csi_field( $maximize, 3 ); 95 $maxhigh != 0 or $maxhigh = $high * 2; 96 $maxwide != 0 or $maxwide = $wide * 2; 97 printf "parsed terminal maxsize $maxhigh,$maxwide\n"; 98} 99else { 100 die "Cannot get maximum terminal size via escape sequence\n"; 101} 102 103our $zapped; 104our ( $w, $h, $a ); 105 106sub catch_zap { 107 $zapped++; 108} 109$SIG{INT} = \&catch_zap; 110$SIG{QUIT} = \&catch_zap; 111$SIG{KILL} = \&catch_zap; 112$SIG{HUP} = \&catch_zap; 113$SIG{TERM} = \&catch_zap; 114 115$w = $wide; 116$h = $high; 117$a = 1; 118$zapped = 0; 119while ( $zapped == 0 ) { 120 121 # sleep 1 122 printf "resizing to $h by $w\n"; 123 write_tty( "\x1b[8;$h;$w" . "t" ); 124 if ( $a == 1 ) { 125 if ( $w == $maxwide ) { 126 $h += $a; 127 if ( $h = $maxhigh ) { 128 $a = -1; 129 } 130 } 131 else { 132 $w += $a; 133 } 134 } 135 else { 136 if ( $w == $wide ) { 137 $h += $a; 138 if ( $h = $high ) { 139 $a = 1; 140 } 141 } 142 else { 143 $w += $a; 144 } 145 } 146} 147write_tty($original); 148