resize.pl revision 5307cd1a
17ec681f3Smrg#!/usr/bin/env perl
27ec681f3Smrg# $XTermId: resize.pl,v 1.5 2014/10/07 21:10:15 tom Exp $
37ec681f3Smrg# -----------------------------------------------------------------------------
47ec681f3Smrg# this file is part of xterm
57ec681f3Smrg#
67ec681f3Smrg# Copyright 2004-2014,2017 by Thomas E. Dickey
77ec681f3Smrg#
87ec681f3Smrg#                         All Rights Reserved
97ec681f3Smrg#
107ec681f3Smrg# Permission is hereby granted, free of charge, to any person obtaining a
117ec681f3Smrg# copy of this software and associated documentation files (the
127ec681f3Smrg# "Software"), to deal in the Software without restriction, including
137ec681f3Smrg# without limitation the rights to use, copy, modify, merge, publish,
147ec681f3Smrg# distribute, sublicense, and/or sell copies of the Software, and to
157ec681f3Smrg# permit persons to whom the Software is furnished to do so, subject to
167ec681f3Smrg# the following conditions:
177ec681f3Smrg#
187ec681f3Smrg# The above copyright notice and this permission notice shall be included
197ec681f3Smrg# in all copies or substantial portions of the Software.
207ec681f3Smrg#
217ec681f3Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
227ec681f3Smrg# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
237ec681f3Smrg# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
247ec681f3Smrg# IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
257ec681f3Smrg# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
267ec681f3Smrg# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
277ec681f3Smrg# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
287ec681f3Smrg#
297ec681f3Smrg# Except as contained in this notice, the name(s) of the above copyright
307ec681f3Smrg# holders shall not be used in advertising or otherwise to promote the
317ec681f3Smrg# sale, use or other dealings in this Software without prior written
327ec681f3Smrg# authorization.
337ec681f3Smrg# -----------------------------------------------------------------------------
347ec681f3Smrg# resize.sh rewritten into Perl for comparison.
357ec681f3Smrg# See also Term::ReadKey.
367ec681f3Smrg
377ec681f3Smrguse strict;
387ec681f3Smrguse warnings;
397ec681f3Smrg
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