summaryrefslogtreecommitdiff
path: root/vttests/resize.pl
blob: 29d0d9b8e2fe9019b3ac58e1c6ca842830cc4f86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/perl
# $XTermId: resize.pl,v 1.3 2004/03/04 02:21:58 tom Exp $
# -----------------------------------------------------------------------------
# this file is part of xterm
#
# Copyright 2004 by Thomas E. Dickey
# 
#                         All Rights Reserved
# 
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
# 
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# 
# Except as contained in this notice, the name(s) of the above copyright
# holders shall not be used in advertising or otherwise to promote the
# sale, use or other dealings in this Software without prior written
# authorization.
# -----------------------------------------------------------------------------
# resize.sh rewritten into Perl for comparison.
# See also Term::ReadKey.

use IO::Handle;

sub write_tty {
	open TTY, "+</dev/tty" or die("Cannot open /dev/tty\n");
	autoflush TTY 1;
	print TTY @_;
	close TTY;
}

sub get_reply {
	open TTY, "+</dev/tty" or die("Cannot open /dev/tty\n");
	autoflush TTY 1;
	$old=`stty -g`;
	system "stty raw -echo min 0 time 5";

	print TTY @_;
	my $reply=<TTY>;
	close TTY;
	system "stty $old";
	return $reply;
}

sub csi_field {
	my $first = @_[0];
	my $second = @_[1];
	$first =~ s/^[^0-9]+//;
	while ( --$second > 0 ) {
		$first =~ s/^[\d]+//;
		$first =~ s/^[^\d]+//;
	}
	$first =~ s/[^\d]+.*$//;
	return $first;
}

$original=get_reply("\x1b[18t");
if ( $original =~ /\x1b\[8;\d+;\d+t/ ) {
	$high=csi_field($original,2);
	$wide=csi_field($original,3);
	printf "parsed terminal size $high,$wide\n";
} else {
	die "Cannot get terminal size via escape sequence\n";
}
#
$maximize=get_reply("\x1b[19t");
if ( $maximize =~ /\x1b\[9;\d+;\d+t/ ) {
	$maxhigh=csi_field($maximize,2);
	$maxwide=csi_field($maximize,3);
	$maxhigh != 0 or $maxhigh = $high * 2;
	$maxwide != 0 or $maxwide = $wide * 2;
	printf "parsed terminal maxsize $maxhigh,$maxwide\n";
} else {
	die "Cannot get terminal size via escape sequence\n";
}

sub catch_zap {
	$zapped++;
}
$SIG{INT} = \&catch_zap;
$SIG{QUIT} = \&catch_zap;
$SIG{KILL} = \&catch_zap;
$SIG{HUP} = \&catch_zap;
$SIG{TERM} = \&catch_zap;

$w=$wide;
$h=$high;
$a=1;
$zapped=0;
while ( $zapped == 0 )
{
#	sleep 1
	printf "resizing to $h by $w\n";
	write_tty("\x1b[8;$h;$w" . "t");
	if ( $a == 1 ) {
		if ( $w == $maxwide ) {
			$h += $a;
			if ( $h = $maxhigh ) {
				$a = -1;
			}
		} else {
			$w += $a;
		}
	} else {
		if ( $w == $wide ) {
			$h += $a;
			if ( $h = $high ) {
				$a=1;
			}
		} else {
			$w += $a;
		}
	}
}
write_tty($original);