From 17bc2f507035b9abde2e35e09437040dbf76327e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Thu, 22 Apr 2021 10:56:03 +0200 Subject: [PATCH 1/6] 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(-) 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 From da5a3bae3e9a514983d33ad45ea4a3d3bba443b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Thu, 22 Apr 2021 10:57:27 +0200 Subject: [PATCH 2/6] generate-alt-random: assert that lines/cols/width/height are > 0 --- scripts/generate-alt-random-writes.py | 5 +++++ 1 file changed, 5 insertions(+) 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 From 44b8bd236423d764611bb600739faf8d898ddf1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Thu, 22 Apr 2021 10:57:43 +0200 Subject: [PATCH 3/6] 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(-) 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 From 10e512f14f29bbbe3fb34eb4ce6292b201341060 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Thu, 22 Apr 2021 11:00:31 +0200 Subject: [PATCH 4/6] 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(-) 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 From a9236129f64118b16a76f2aa59c9d9448def2383 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Thu, 22 Apr 2021 11:20:55 +0200 Subject: [PATCH 5/6] =?UTF-8?q?generate-alt-random:=20don=E2=80=99t=20run?= =?UTF-8?q?=20TIOCGWINSZ=20ioctl=20if=20--cols=20and=20--rows=20were=20use?= =?UTF-8?q?d?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/generate-alt-random-writes.py | 49 ++++++++++++++------------- 1 file changed, 25 insertions(+), 24 deletions(-) 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) + 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))) + 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 + 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]) + # 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) + signal.signal(signal.SIGWINCH, signal.SIG_DFL) - except OSError: - lines = None - cols = None - height = None - width = None + except OSError: + lines = None + cols = None + height = None + width = None if opts.rows is not None: lines = opts.rows From 033e1bd6aefc8da25167af8532153d754af8d4fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Thu, 22 Apr 2021 11:26:59 +0200 Subject: [PATCH 6/6] changelog: generate-alt-random-writes.py --sixel fixes --- CHANGELOG.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 728dbf9b..0c614c4b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,9 +29,19 @@ ## Unreleased ### Added ### Changed + +* `generate-alt-random-writes.py --sixel`: width and height of emitted + sixels has been adjusted. + + ### Deprecated ### Removed ### Fixed + +* `generate-alt-random-writes.py --sixel` sometimes crashing, + resulting in PGO build failures. + + ### Security ### Contributors