resize.pl revision 20d2c4d2
1#!/usr/bin/perl
2# $XTermId: resize.pl,v 1.3 2004/03/04 02:21:58 tom Exp $
3# -----------------------------------------------------------------------------
4# this file is part of xterm
5#
6# Copyright 2004 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 IO::Handle;
38
39sub write_tty {
40	open TTY, "+</dev/tty" or die("Cannot open /dev/tty\n");
41	autoflush TTY 1;
42	print TTY @_;
43	close TTY;
44}
45
46sub get_reply {
47	open TTY, "+</dev/tty" or die("Cannot open /dev/tty\n");
48	autoflush TTY 1;
49	$old=`stty -g`;
50	system "stty raw -echo min 0 time 5";
51
52	print TTY @_;
53	my $reply=<TTY>;
54	close TTY;
55	system "stty $old";
56	return $reply;
57}
58
59sub csi_field {
60	my $first = @_[0];
61	my $second = @_[1];
62	$first =~ s/^[^0-9]+//;
63	while ( --$second > 0 ) {
64		$first =~ s/^[\d]+//;
65		$first =~ s/^[^\d]+//;
66	}
67	$first =~ s/[^\d]+.*$//;
68	return $first;
69}
70
71$original=get_reply("\x1b[18t");
72if ( $original =~ /\x1b\[8;\d+;\d+t/ ) {
73	$high=csi_field($original,2);
74	$wide=csi_field($original,3);
75	printf "parsed terminal size $high,$wide\n";
76} else {
77	die "Cannot get terminal size via escape sequence\n";
78}
79#
80$maximize=get_reply("\x1b[19t");
81if ( $maximize =~ /\x1b\[9;\d+;\d+t/ ) {
82	$maxhigh=csi_field($maximize,2);
83	$maxwide=csi_field($maximize,3);
84	$maxhigh != 0 or $maxhigh = $high * 2;
85	$maxwide != 0 or $maxwide = $wide * 2;
86	printf "parsed terminal maxsize $maxhigh,$maxwide\n";
87} else {
88	die "Cannot get terminal size via escape sequence\n";
89}
90
91sub catch_zap {
92	$zapped++;
93}
94$SIG{INT} = \&catch_zap;
95$SIG{QUIT} = \&catch_zap;
96$SIG{KILL} = \&catch_zap;
97$SIG{HUP} = \&catch_zap;
98$SIG{TERM} = \&catch_zap;
99
100$w=$wide;
101$h=$high;
102$a=1;
103$zapped=0;
104while ( $zapped == 0 )
105{
106#	sleep 1
107	printf "resizing to $h by $w\n";
108	write_tty("\x1b[8;$h;$w" . "t");
109	if ( $a == 1 ) {
110		if ( $w == $maxwide ) {
111			$h += $a;
112			if ( $h = $maxhigh ) {
113				$a = -1;
114			}
115		} else {
116			$w += $a;
117		}
118	} else {
119		if ( $w == $wide ) {
120			$h += $a;
121			if ( $h = $high ) {
122				$a=1;
123			}
124		} else {
125			$w += $a;
126		}
127	}
128}
129write_tty($original);
130