From 17bc2f507035b9abde2e35e09437040dbf76327e Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 22 Apr 2021 10:56:03 +0200 Subject: generate-alt-random: use {width,height} + 1 in randrange() When randomizing the sixel width and height, use width/height + 1 in the call to randrange(). This ensures the width/height is never 0, and that the maximum width/height is half the window size, not slightly less than that. --- scripts/generate-alt-random-writes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'scripts') diff --git a/scripts/generate-alt-random-writes.py b/scripts/generate-alt-random-writes.py index 63becdcf..8aa4835c 100755 --- a/scripts/generate-alt-random-writes.py +++ b/scripts/generate-alt-random-writes.py @@ -176,7 +176,7 @@ def main(): else: # Random origin in upper left quadrant last_pos = random.randrange(lines // 2) + 1, random.randrange(cols // 2) + 1 - last_size = random.randrange(height // 2), random.randrange(width // 2) + last_size = random.randrange((height + 1) // 2), random.randrange((width + 1) // 2) out.write(f'\033[{last_pos[0]};{last_pos[1]}H') six_height, six_width = last_size -- cgit v1.3 From da5a3bae3e9a514983d33ad45ea4a3d3bba443b8 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 22 Apr 2021 10:57:27 +0200 Subject: generate-alt-random: assert that lines/cols/width/height are > 0 --- scripts/generate-alt-random-writes.py | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'scripts') diff --git a/scripts/generate-alt-random-writes.py b/scripts/generate-alt-random-writes.py index 8aa4835c..33033081 100755 --- a/scripts/generate-alt-random-writes.py +++ b/scripts/generate-alt-random-writes.py @@ -59,6 +59,11 @@ def main(): if lines is None or cols is None or height is None or width is None: raise Exception('could not get terminal width/height; use --rows and --cols') + assert lines > 0, f'{lines}' + assert cols > 0, f'{cols}' + assert width > 0, f'{width}' + assert height > 0, f'{height}' + # Number of characters to write to screen count = 256 * 1024**1 -- cgit v1.3 From 44b8bd236423d764611bb600739faf8d898ddf1f Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 22 Apr 2021 10:57:43 +0200 Subject: generate-alt-random: wait for SIGWINCH if width/height is 0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If width/height (that is, the *pixel* values) are 0, that means we are early (or that foot is slow) - we’ve managed to reach this point before the foot window has been mapped. Or rather, before foot has loaded the primary fonts and calculated the cell geometry. But that, and the initial mapping of the window is tightly coupled. To handle this case, detect when width or height is 0, an then wait for SIGWINCH before trying again. --- scripts/generate-alt-random-writes.py | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) (limited to 'scripts') diff --git a/scripts/generate-alt-random-writes.py b/scripts/generate-alt-random-writes.py index 33033081..8db3401b 100755 --- a/scripts/generate-alt-random-writes.py +++ b/scripts/generate-alt-random-writes.py @@ -2,8 +2,9 @@ import argparse import enum import fcntl -import struct import random +import signal +import struct import sys import termios @@ -38,11 +39,28 @@ def main(): out = opts.out if opts.out is not None else sys.stdout try: - lines, cols, height, width = struct.unpack( - 'HHHH', - fcntl.ioctl(sys.stdout.fileno(), - termios.TIOCGWINSZ, - struct.pack('HHHH', 0, 0, 0, 0))) + def dummy(*args): + """Need a handler installed for sigwait() to trigger.""" + pass + signal.signal(signal.SIGWINCH, dummy) + + while True: + lines, cols, height, width = struct.unpack( + 'HHHH', + fcntl.ioctl(sys.stdout.fileno(), + termios.TIOCGWINSZ, + struct.pack('HHHH', 0, 0, 0, 0))) + + if width > 0 and height > 0: + break + + # We’re early; the foot window hasn’t been mapped yet. Or, + # to be more precise, fonts haven’t yet been loaded, + # meaning it doesn’t have any cell geometry yet. + signal.sigwait([signal.SIGWINCH]) + + signal.signal(signal.SIGWINCH, signal.SIG_DFL) + except OSError: lines = None cols = None -- cgit v1.3 From 10e512f14f29bbbe3fb34eb4ce6292b201341060 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 22 Apr 2021 11:00:31 +0200 Subject: generate-alt-random: do ioctl(TIOCGWINSZ) on /dev/tty, not stdout --- scripts/generate-alt-random-writes.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'scripts') diff --git a/scripts/generate-alt-random-writes.py b/scripts/generate-alt-random-writes.py index 8db3401b..df053844 100755 --- a/scripts/generate-alt-random-writes.py +++ b/scripts/generate-alt-random-writes.py @@ -45,11 +45,12 @@ def main(): signal.signal(signal.SIGWINCH, dummy) while True: - lines, cols, height, width = struct.unpack( - 'HHHH', - fcntl.ioctl(sys.stdout.fileno(), - termios.TIOCGWINSZ, - struct.pack('HHHH', 0, 0, 0, 0))) + with open('/dev/tty', 'rb') as pty: + lines, cols, height, width = struct.unpack( + 'HHHH', + fcntl.ioctl(pty, + termios.TIOCGWINSZ, + struct.pack('HHHH', 0, 0, 0, 0))) if width > 0 and height > 0: break -- cgit v1.3 From a9236129f64118b16a76f2aa59c9d9448def2383 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 22 Apr 2021 11:20:55 +0200 Subject: generate-alt-random: don’t run TIOCGWINSZ ioctl if --cols and --rows were used MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/generate-alt-random-writes.py | 59 ++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 29 deletions(-) (limited to 'scripts') diff --git a/scripts/generate-alt-random-writes.py b/scripts/generate-alt-random-writes.py index df053844..4e50bd8c 100755 --- a/scripts/generate-alt-random-writes.py +++ b/scripts/generate-alt-random-writes.py @@ -38,35 +38,36 @@ def main(): opts = parser.parse_args() out = opts.out if opts.out is not None else sys.stdout - try: - def dummy(*args): - """Need a handler installed for sigwait() to trigger.""" - pass - signal.signal(signal.SIGWINCH, dummy) - - while True: - with open('/dev/tty', 'rb') as pty: - lines, cols, height, width = struct.unpack( - 'HHHH', - fcntl.ioctl(pty, - termios.TIOCGWINSZ, - struct.pack('HHHH', 0, 0, 0, 0))) - - if width > 0 and height > 0: - break - - # We’re early; the foot window hasn’t been mapped yet. Or, - # to be more precise, fonts haven’t yet been loaded, - # meaning it doesn’t have any cell geometry yet. - signal.sigwait([signal.SIGWINCH]) - - signal.signal(signal.SIGWINCH, signal.SIG_DFL) - - except OSError: - lines = None - cols = None - height = None - width = None + if opts.rows is None or opts.cols is None: + try: + def dummy(*args): + """Need a handler installed for sigwait() to trigger.""" + pass + signal.signal(signal.SIGWINCH, dummy) + + while True: + with open('/dev/tty', 'rb') as pty: + lines, cols, height, width = struct.unpack( + 'HHHH', + fcntl.ioctl(pty, + termios.TIOCGWINSZ, + struct.pack('HHHH', 0, 0, 0, 0))) + + if width > 0 and height > 0: + break + + # We’re early; the foot window hasn’t been mapped yet. Or, + # to be more precise, fonts haven’t yet been loaded, + # meaning it doesn’t have any cell geometry yet. + signal.sigwait([signal.SIGWINCH]) + + signal.signal(signal.SIGWINCH, signal.SIG_DFL) + + except OSError: + lines = None + cols = None + height = None + width = None if opts.rows is not None: lines = opts.rows -- cgit v1.3