1#!/bin/sh
2# $XTermId: resize.sh,v 1.25 2022/04/25 08:19:38 tom Exp $
3# -----------------------------------------------------------------------------
4# this file is part of xterm
5#
6# Copyright 1999-2021,2022 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# Obtain the current screen size, then resize the terminal to the nominal
35# screen width/height, and restore the size.
36
37ESC=""
38CSI="${ESC}["
39CMD='/bin/echo'
40OPT='-n'
41SUF=''
42: "${TMPDIR=/tmp}"
43TMP=`(mktemp "$TMPDIR/xterm.XXXXXXXX") 2>/dev/null` || TMP="$TMPDIR/xterm$$"
44eval '$CMD $OPT >$TMP || echo fail >$TMP' 2>/dev/null
45{ test ! -f "$TMP" || test -s "$TMP"; } &&
46for verb in "printf" "print" ; do
47    rm -f "$TMP"
48    eval '$verb "\c" >$TMP || echo fail >$TMP' 2>/dev/null
49    if test -f "$TMP" ; then
50	if test ! -s "$TMP" ; then
51	    CMD="$verb"
52	    OPT=
53	    SUF='\c'
54	    break
55	fi
56    fi
57done
58rm -f "$TMP"
59
60exec </dev/tty
61old=`stty -g`
62stty raw -echo min 0  time 5
63
64$CMD $OPT "${CSI}18t${SUF}" > /dev/tty
65IFS=';' read -r junk high wide
66
67$CMD $OPT "${CSI}19t${SUF}" > /dev/tty
68IFS=';' read -r junk maxhigh maxwide
69
70stty $old
71
72wide=`echo "$wide"|sed -e 's/t.*//'`
73maxwide=`echo "$maxwide"|sed -e 's/t.*//'`
74original=${CSI}8\;${high}\;${wide}t${SUF}
75
76test "$maxwide" = 0 && maxwide=`expr "$wide" \* 2`
77test "$maxhigh" = 0 && maxhigh=`expr "$high" \* 2`
78
79trap '$CMD $OPT "$original" >/dev/tty; exit 1' 1 2 3 15
80trap '$CMD $OPT "$original" >/dev/tty' 0
81
82w=$wide
83h=$high
84a=1
85while true
86do
87#	sleep 1
88	echo "resizing to $h by $w"
89	$CMD $OPT "${CSI}8;${h};${w}t" >/dev/tty
90	if test $a = 1 ; then
91		if test "$w" = "$maxwide" ; then
92			h=`expr "$h" + "$a"`
93			if test "$h" = "$maxhigh" ; then
94				a=-1
95			fi
96		else
97			w=`expr "$w" + "$a"`
98		fi
99	else
100		if test "$w" = "$wide" ; then
101			h=`expr "$h" + "$a"`
102			if test "$h" = "$high" ; then
103				a=1
104			fi
105		else
106			w=`expr "$w" + "$a"`
107		fi
108	fi
109done
110