resize.pl revision 01037d57
1#!/usr/bin/env perl
2# $XTermId: resize.pl,v 1.5 2014/10/07 21:10:15 tom Exp $
3# -----------------------------------------------------------------------------
4# this file is part of xterm
5#
6# Copyright 2004,2014 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} else {
83	die "Cannot get current terminal size via escape sequence\n";
84}
85#
86our $maximize=get_reply("\x1b[19t");
87our $maxhigh;
88our $maxwide;
89
90if ( defined($maximize) and ( $maximize =~ /^\x1b\[9;\d+;\d+t/ ) ) {
91	$maxhigh=csi_field($maximize,2);
92	$maxwide=csi_field($maximize,3);
93	$maxhigh != 0 or $maxhigh = $high * 2;
94	$maxwide != 0 or $maxwide = $wide * 2;
95	printf "parsed terminal maxsize $maxhigh,$maxwide\n";
96} else {
97	die "Cannot get maximum terminal size via escape sequence\n";
98}
99
100our $zapped;
101our ( $w, $h, $a );
102
103sub catch_zap {
104	$zapped++;
105}
106$SIG{INT} = \&catch_zap;
107$SIG{QUIT} = \&catch_zap;
108$SIG{KILL} = \&catch_zap;
109$SIG{HUP} = \&catch_zap;
110$SIG{TERM} = \&catch_zap;
111
112$w=$wide;
113$h=$high;
114$a=1;
115$zapped=0;
116while ( $zapped == 0 )
117{
118#	sleep 1
119	printf "resizing to $h by $w\n";
120	write_tty("\x1b[8;$h;$w" . "t");
121	if ( $a == 1 ) {
122		if ( $w == $maxwide ) {
123			$h += $a;
124			if ( $h = $maxhigh ) {
125				$a = -1;
126			}
127		} else {
128			$w += $a;
129		}
130	} else {
131		if ( $w == $wide ) {
132			$h += $a;
133			if ( $h = $high ) {
134				$a=1;
135			}
136		} else {
137			$w += $a;
138		}
139	}
140}
141write_tty($original);
142