kitty/kitty_tests/graphics.py
Kovid Goyal 512b8d568a
Use the coordinate conventions of Vulkan and Metal for the GPU API
Pixel coordinates of viewports, scissor rectangles and read back pixels
now have their origin at the top left of the render target, rows are
read back top to bottom, NDC y=-1 is the top of the viewport and render
targets have their top row at texture coordinate t=0, the same as
uploaded images. Vulkan and Metal drivers can then render with no flips
at all.

The OpenGL driver is now the special case. Framebuffer objects already
behave this way, so it converts only when rendering to a window: vertex
shaders negate gl_Position.y via a kitty_flip_y uniform that the GLSL
fixup adds, viewport and scissor y coordinates are converted, rows of
read back pixels are reversed and copying to a texture uses a flipping
blit.

kitty's code gets simpler: viewports and scissor rectangles no longer
need the framebuffer height, screenshots no longer need their rows
flipped and textures produced by rendering are sampled the same way as
uploaded ones. The rounded rect shader no longer uses SV_Position, whose
origin differs between backends.

User facing custom shader UV coordinates are unchanged, still with
their origin at the bottom left.

Rendering is pixel identical to before.
2026-11-16 13:43:37 +05:30

2344 lines
100 KiB
Python

#!/usr/bin/env python
# License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
import os
import random
import tempfile
import time
import unittest
import zlib
from contextlib import suppress
from dataclasses import dataclass
from io import BytesIO
from kitty.fast_data_types import base64_decode, base64_encode, load_png_data, shm_unlink, shm_write
from .base import BaseTest, parse_bytes
try:
from PIL import Image
except ImportError:
Image = None
png_data = base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+P+/HgAFhAJ/wlseKgAAAABJRU5ErkJggg==')
def num_open_fds():
with suppress(OSError):
return len(os.listdir('/proc/self/fd'))
return len(os.listdir('/dev/fd'))
def send_command(screen, cmd, payload=b''):
cmd = '\033_G' + cmd
if payload:
if isinstance(payload, str):
payload = payload.encode('utf-8')
payload = base64_encode(payload).decode('ascii')
cmd += ';' + payload
cmd += '\033\\'
c = screen.callbacks
c.clear()
parse_bytes(screen, cmd.encode('ascii'))
return c.wtcbuf
def parse_response(res):
if not res:
return
return res.decode('ascii').partition(';')[2].partition('\033')[0]
def parse_response_with_ids(res):
if not res:
return
a, b = res.decode('ascii').split(';', 1)
code = b.partition('\033')[0].split(':', 1)[0]
a = a.split('G', 1)[1]
return code, a
@dataclass(frozen=True)
class Response:
code: str = 'OK'
msg: str = ''
image_id: int = 0
image_number: int = 0
frame_number: int = 0
def parse_full_response(res):
if not res:
return
a, b = res.decode('ascii').split(';', 1)
code = b.partition('\033')[0].split(':', 1)
if len(code) == 1:
code = code[0]
msg = ''
else:
code, msg = code
a = a.split('G', 1)[1]
ans = {'code': code, 'msg': msg}
for x in a.split(','):
k, _, v = x.partition('=')
ans[{'i': 'image_id', 'I': 'image_number', 'r': 'frame_number'}[k]] = int(v)
return Response(**ans)
all_bytes = bytes(bytearray(range(256)))
def byte_block(sz):
d, m = divmod(sz, len(all_bytes))
return (all_bytes * d) + all_bytes[:m]
def load_helpers(self):
s = self.create_screen()
g = s.grman
def pl(payload, **kw):
kw.setdefault('i', 1)
cmd = ','.join(f'{k}={v}' for k, v in kw.items())
res = send_command(s, cmd, payload)
return parse_response(res)
def sl(payload, **kw):
if isinstance(payload, str):
payload = payload.encode('utf-8')
data = kw.pop('expecting_data', payload)
cid = kw.setdefault('i', 1)
self.ae('OK', pl(payload, **kw))
img = g.image_for_client_id(cid)
self.assertIsNotNone(img, f'No image with id {cid} found')
self.ae(img['client_id'], cid)
self.ae(img['data'], data)
if 's' in kw:
self.ae((kw['s'], kw['v']), (img['width'], img['height']))
self.ae(img['is_4byte_aligned'], kw.get('f') != 24)
return img
return s, g, pl, sl
def put_helpers(self, cw, ch, cols=10, lines=5):
iid = 0
def create_screen():
s = self.create_screen(cols, lines, cell_width=cw, cell_height=ch)
return s, 2 / s.columns, 2 / s.lines
def put_cmd(
z=0,
num_cols=0,
num_lines=0,
x_off=0,
y_off=0,
width=0,
height=0,
cell_x_off=0,
cell_y_off=0,
placement_id=0,
cursor_movement=0,
unicode_placeholder=0,
parent_id=0,
parent_placement_id=0,
offset_from_parent_x=0,
offset_from_parent_y=0,
):
return (
f'z={z},c={num_cols},r={num_lines},x={x_off},y={y_off},w={width},h={height},'
f'X={cell_x_off},Y={cell_y_off},p={placement_id},C={cursor_movement},'
f'U={unicode_placeholder},P={parent_id},Q={parent_placement_id},'
f'H={offset_from_parent_x},V={offset_from_parent_y}'
)
def put_image(screen, w, h, **kw):
nonlocal iid
iid += 1
imgid = kw.pop('id', None) or iid
no_id = kw.pop('no_id', False)
a = kw.pop('a', 'T')
if no_id:
cmd = f'a={a},f=24,s=%d,v=%d,%s' % (w, h, put_cmd(**kw))
else:
cmd = f'a={a},f=24,i=%d,s=%d,v=%d,%s' % (imgid, w, h, put_cmd(**kw))
data = b'x' * w * h * 3
res = send_command(screen, cmd, data)
return imgid, parse_response(res)
def put_ref(screen, **kw):
imgid = kw.pop('id', None) or iid
cmd = 'a=p,i=%d,%s' % (imgid, put_cmd(**kw))
return imgid, parse_response_with_ids(send_command(screen, cmd))
def layers(screen, scrolled_by=0, xstart=-1, ystart=-1):
return screen.grman.update_layers(scrolled_by, xstart, ystart, dx, dy, screen.columns, screen.lines, cw, ch)
def rect_eq(r, left, top, right, bottom):
for side in 'left top right bottom'.split():
a, b = r[side], locals()[side]
if abs(a - b) > 0.0001:
self.ae(a, b, 'the %s side is not equal' % side)
s, dx, dy = create_screen()
return s, dx, dy, put_image, put_ref, layers, rect_eq
def make_send_command(screen):
def li(payload='abcdefghijkl' * 3, s=4, v=3, f=24, a='f', i=1, **kw):
if s:
kw['s'] = s
if v:
kw['v'] = v
if f:
kw['f'] = f
if i:
kw['i'] = i
kw['a'] = a
cmd = ','.join(f'{k}={v}' for k, v in kw.items())
res = send_command(screen, cmd, payload)
return parse_full_response(res)
return li
class TestGraphics(BaseTest):
def test_disk_cache(self):
s = self.create_screen()
dc = s.grman.disk_cache
dc.small_hole_threshold = 0
data = {}
def key_as_bytes(key):
if isinstance(key, int):
key = str(key)
if isinstance(key, str):
key = key.encode('utf-8')
return bytes(key)
def add(key, val):
bkey = key_as_bytes(key)
data[key] = key_as_bytes(val)
dc.add(bkey, data[key])
def remove(key):
bkey = key_as_bytes(key)
data.pop(key, None)
return dc.remove(bkey)
def check_data():
for key, val in data.items():
self.ae(dc.get(key_as_bytes(key)), val)
def reset(small_hole_threshold=0, defrag_factor=2):
nonlocal dc, data, s
s = self.create_screen()
dc = s.grman.disk_cache
dc.small_hole_threshold = small_hole_threshold
dc.defrag_factor = defrag_factor
data = {}
holes_to_create = 2, 4, 6, 8
for i in range(25):
self.assertIsNone(add(i, f'{i}' * i))
if i <= max(holes_to_create):
# We wait here to ensure data is written in order, otherwise the
# holes test below can fail
self.assertTrue(dc.wait_for_write())
self.assertEqual(dc.total_size, sum(map(len, data.values())))
self.assertTrue(dc.wait_for_write())
check_data()
sz = dc.end_of_data_offset()
self.assertEqual(sz, sum(map(len, data.values())))
self.assertFalse(dc.holes())
holes = set()
for x in holes_to_create:
remove(x)
holes.add(x)
check_data()
self.assertRaises(KeyError, dc.get, key_as_bytes(x))
self.assertEqual(sz, dc.end_of_data_offset())
self.assertEqual(holes, {x[1] for x in dc.holes()})
self.assertEqual(sz, dc.end_of_data_offset())
# fill holes largest first to ensure small one doesn't go into large accidentally causing fragmentation
for i, x in enumerate(sorted(holes, reverse=True)):
x = 'ABCDEFGH'[i] * x
add(x, x)
self.assertTrue(dc.wait_for_write())
check_data()
holes.discard(len(x))
self.assertEqual(holes, {x[1] for x in dc.holes()})
self.assertEqual(sz, dc.end_of_data_offset(), f'Disk cache has unexpectedly grown from {sz} to {dc.end_of_data_offset()} with data: {x!r}')
check_data()
dc.clear()
self.assertEqual(dc.end_of_data_offset(), 0)
data.clear()
for i in range(25):
self.assertIsNone(add(i, f'{i}' * i))
dc.wait_for_write()
check_data()
before = dc.end_of_data_offset()
while dc.total_size > before // 3:
key = random.choice(tuple(data))
self.assertTrue(remove(key))
check_data()
add('trigger defrag', 'XXX')
dc.wait_for_write()
self.assertLess(dc.end_of_data_offset(), before)
check_data()
dc.clear()
st = time.monotonic()
while dc.end_of_data_offset() and time.monotonic() - st < 20:
time.sleep(0.01)
self.assertEqual(dc.end_of_data_offset(), 0)
for frame in range(32):
add(f'1:{frame}', f'{frame:02d}' * 8)
dc.wait_for_write()
self.assertEqual(dc.end_of_data_offset(), 32 * 16)
self.assertEqual(dc.num_cached_in_ram(), 0)
num_in_ram = 0
for frame in range(32):
dc.get(key_as_bytes(f'1:{frame}'))
self.assertEqual(dc.num_cached_in_ram(), num_in_ram)
for frame in range(32):
dc.get(key_as_bytes(f'1:{frame}'), True)
num_in_ram += 1
self.assertEqual(dc.num_cached_in_ram(), num_in_ram)
def clear_predicate(key):
return key.startswith(b'1:')
dc.remove_from_ram(clear_predicate)
self.assertEqual(dc.num_cached_in_ram(), 0)
reset(small_hole_threshold=512, defrag_factor=20)
self.assertIsNone(add(1, '1' * 1024))
self.assertIsNone(add(2, '2' * 1024))
dc.wait_for_write()
sz = dc.end_of_data_offset()
remove(1)
self.ae(sz, dc.end_of_data_offset())
self.ae({x[1] for x in dc.holes()}, {1024})
self.assertIsNone(add(3, '3' * 800))
dc.wait_for_write()
self.assertFalse(dc.holes())
self.ae(sz, dc.end_of_data_offset())
self.assertIsNone(add(4, '4' * 100))
sz += 100
dc.wait_for_write()
self.ae(sz, dc.end_of_data_offset())
check_data()
self.assertFalse(dc.holes())
remove(4)
self.assertFalse(dc.holes())
self.assertIsNone(add(5, '5' * 10))
sz += 10
dc.wait_for_write()
self.ae(sz, dc.end_of_data_offset())
# test hole coalescing
reset(defrag_factor=20)
for i in range(1, 6):
self.assertIsNone(add(i, str(i) * i))
dc.wait_for_write()
remove(2)
remove(4)
self.assertEqual(dc.holes(), {(1, 2), (6, 4)})
remove(3)
self.assertEqual(dc.holes(), {(1, 9)})
def test_disk_cache_entry_changed_while_being_written(self):
# The disk cache write thread releases the lock while writing an entry
# to disk, so the entry can be replaced or removed in the meantime. The
# data written for it is then stale and must neither be associated with
# the entry nor be allowed to leak space in the cache file.
s = self.create_screen()
dc = s.grman.disk_cache
dc.small_hole_threshold = 0
# Replaced while being written
dc.pause_writes()
dc.add(b'k1', b'a' * 100)
self.assertTrue(dc.wait_until_writes_paused())
dc.add(b'k1', b'b' * 200)
self.assertTrue(dc.resume_writes())
self.assertTrue(dc.wait_for_write())
# The new data must have been written to disk rather than the entry
# being marked as written at the position of the old data
self.assertEqual(dc.num_cached_in_ram(), 0)
self.assertEqual(dc.get(b'k1'), b'b' * 200)
self.assertEqual(dc.total_size, 200)
# The space used by the stale data must have been reclaimed
self.assertEqual(dc.holes(), {(0, 100)})
self.assertEqual(dc.end_of_data_offset(), 300)
# Removed while being written
dc.pause_writes()
dc.add(b'k2', b'c' * 50)
self.assertTrue(dc.wait_until_writes_paused())
self.assertTrue(dc.remove(b'k2'))
self.assertTrue(dc.resume_writes())
self.assertTrue(dc.wait_for_write())
self.assertRaises(KeyError, dc.get, b'k2')
self.assertEqual(dc.total_size, 200)
# k2 was written into the existing 100 byte hole, both the 50 bytes it
# used and the 50 byte remainder must be reclaimed and coalesced
self.assertEqual(dc.holes(), {(0, 100)})
self.assertEqual(dc.end_of_data_offset(), 300)
# The untouched entry must be unaffected throughout
self.assertEqual(dc.get(b'k1'), b'b' * 200)
def test_disk_cache_entries_changed_during_defrag(self):
# Stale copies become holes
s = self.create_screen()
dc = s.grman.disk_cache
dc.small_hole_threshold = 0
dc.defrag_factor = 1000
data = {}
for i in range(20):
key = f'k{i}'.encode()
data[key] = bytes([65 + i]) * 100
dc.add(key, data[key])
self.assertTrue(dc.wait_for_write())
for i in range(12):
dc.remove(f'k{i}'.encode())
del data[f'k{i}'.encode()]
self.assertEqual(dc.end_of_data_offset(), 2000)
dc.pause_writes()
dc.defrag_factor = 2
data[b'trigger'] = b'T' * 10
dc.add(b'trigger', data[b'trigger'])
self.assertTrue(dc.wait_until_writes_paused())
dc.defrag_factor = 1000
for i in range(12, 16):
dc.remove(f'k{i}'.encode())
del data[f'k{i}'.encode()]
data[b'k16'] = b'r' * 60
dc.add(b'k16', data[b'k16'])
data[b'k17'] = b'm' * 70
dc.add(b'k17', data[b'k17'], True)
self.assertTrue(dc.resume_writes())
self.assertTrue(dc.wait_for_write())
for k, v in data.items():
self.assertEqual(dc.get(k), v)
holes = sorted(dc.holes())
for (p1, s1), (p2, s2) in zip(holes, holes[1:]):
self.assertLessEqual(p1 + s1, p2)
on_disk = sum(len(v) for k, v in data.items() if k != b'k17')
self.assertEqual(sum(x[1] for x in holes) + on_disk, dc.end_of_data_offset())
# Memory only entries own no disk space
dc.remove(b'k17')
del data[b'k17']
self.assertEqual(sorted(dc.holes()), holes)
for k, v in data.items():
self.assertEqual(dc.get(k), v)
def test_disk_cache_empty_entry_replaced(self):
dc = self.create_screen().grman.disk_cache
dc.small_hole_threshold = 0
dc.defrag_factor = 1000
dc.add(b'a', b'A' * 1000)
self.assertTrue(dc.wait_for_write())
dc.add(b'z', b'')
self.assertTrue(dc.wait_for_write())
dc.remove(b'a')
dc.add(b'c', b'C' * 100)
self.assertTrue(dc.wait_for_write())
dc.pause_writes()
dc.defrag_factor = 2
dc.add(b'z', b'Z' * 50)
self.assertTrue(dc.wait_until_writes_paused())
dc.defrag_factor = 1000
self.assertTrue(dc.resume_writes())
self.assertTrue(dc.wait_for_write())
self.assertEqual(dc.end_of_data_offset(), 150)
self.assertEqual(dc.get(b'z'), b'Z' * 50)
self.assertEqual(dc.get(b'c'), b'C' * 100)
def test_disk_cache_encryption_while_being_written(self):
s = self.create_screen()
dc = s.grman.disk_cache
dc.set_needs_encryption(True)
data = bytes(range(256)) * 1000 + b'xyz'
# Reads while writing return plaintext
dc.pause_writes()
dc.add(b'k1', data)
self.assertTrue(dc.wait_until_writes_paused())
self.assertEqual(dc.get(b'k1'), data)
self.assertTrue(dc.resume_writes())
self.assertTrue(dc.wait_for_write())
self.assertEqual(dc.num_cached_in_ram(), 0)
self.assertEqual(dc.get(b'k1'), data)
dc.pause_writes()
dc.add(b'k2', data)
self.assertTrue(dc.wait_until_writes_paused())
dc.add(b'k2', data[::-1])
self.assertTrue(dc.resume_writes())
self.assertTrue(dc.wait_for_write())
self.assertEqual(dc.get(b'k2'), data[::-1])
self.assertEqual(dc.get(b'k1'), data)
def test_suppressing_gr_command_responses(self):
s, g, pl, sl = load_helpers(self)
self.ae(pl('abcd', s=10, v=10, q=1), 'ENODATA:Insufficient image data: 4 < 400')
self.ae(pl('abcd', s=10, v=10, q=2), None)
self.assertIsNone(pl('abcd', s=1, v=1, a='q', q=1))
# Test chunked load
self.assertIsNone(pl('abcd', s=2, v=2, m=1, q=1))
self.assertIsNone(pl('efgh', m=1))
self.assertIsNone(pl('ijkl', m=1))
self.assertIsNone(pl('mnop', m=0))
# errors
self.assertIsNone(pl('abcd', s=2, v=2, m=1, q=1))
self.ae(pl('mnop', m=0), 'ENODATA:Insufficient image data: 8 < 16')
self.assertIsNone(pl('abcd', s=2, v=2, m=1, q=2))
self.assertIsNone(pl('mnop', m=0))
# frames
s = self.create_screen()
li = make_send_command(s)
self.assertEqual(li().code, 'ENOENT')
self.assertIsNone(li(q=2))
self.assertIsNone(li(a='t', q=1))
self.assertIsNone(li(payload='2' * 12, z=77, m=1, q=1))
self.assertIsNone(li(payload='2' * 12, m=1))
self.assertIsNone(li(payload='2' * 12))
self.assertIsNone(li(payload='2' * 12, z=77, m=1, q=1))
self.ae(li(payload='2' * 12).code, 'ENODATA')
self.assertIsNone(li(payload='2' * 12, z=77, m=1, q=2))
self.assertIsNone(li(payload='2' * 12))
def test_transient_graphics_image(self):
s, g, pl, sl = load_helpers(self)
self.assertEqual(g.disk_cache.end_of_data_offset(), 0)
self.ae(pl('abc', s=1, v=1, f=24, N=1), 'OK')
self.assertTrue(g.disk_cache.wait_for_write())
self.assertEqual(g.disk_cache.end_of_data_offset(), 0)
img = g.image_for_client_id(1)
self.assertIsNotNone(img)
self.ae(img['data'], b'abc')
self.ae(pl('def', s=1, v=1, f=24, i=2), 'OK')
self.assertTrue(g.disk_cache.wait_for_write())
self.assertGreater(g.disk_cache.end_of_data_offset(), 0)
def test_load_images(self):
s, g, pl, sl = load_helpers(self)
self.assertEqual(g.disk_cache.total_size, 0)
# Test load query
self.ae(pl('abcd', s=1, v=1, a='q'), 'OK')
self.ae(g.image_count, 0)
# Test feature queries
def q(e, payload='', **kw):
res = send_command(s, ','.join(f'{k}={v}' for k, v in dict(a='q', i=31, e=e, **kw).items()), payload)
return res.decode('ascii') if res else None
def r(keys=''):
return f'\033_Gi=31{keys};OK\033\\'
self.ae(q('T', s=1, v=1, f=24, U=1), r(',e=T,f=24,s=1,v=1,U=1'))
self.ae(q('T', s=1, v=1, f=24, t='d', C=1, z=-3, N=1), r(',e=T,t=d,f=24,s=1,v=1,N=1,C=1,z=-3'))
# no image data is needed or loaded
self.ae(q('t', 'abcd', s=1, v=1, f=8, c=2), r(',e=t,f=8,s=1,v=1'))
self.ae(g.image_count, 0)
# keys are interpreted according to the action being queried
self.ae(q('p', c=2, r=3, U=1, C=1, s=1), r(',e=p,c=2,r=3,C=1,U=1'))
self.ae(q('f', c=2, r=3, z=40, X=1, f=8, s=1), r(',e=f,s=1,c=2,r=3,z=40,X=1'))
self.ae(q('a', s=3, v=1, r=2, c=1, z=-1), r(',e=a,s=3,v=1,r=2,c=1,z=-1'))
self.ae(q('c', r=1, c=2, w=3, X=1, C=1, U=1), r(',e=c,r=1,c=2,w=3,X=1,C=1'))
self.ae(q('d', d='F', p=2, s=1), r(',e=d,d=F,p=2'))
# unsupported values are not reported
self.ae(q('T', s=1, v=1, f=24, C=7, N=6, U=2), r(',e=T,f=24,s=1,v=1'))
self.ae(q('t', f=42, s=100000), r(',e=t'))
self.ae(q('a', s=4), r(',e=a'))
# unknown actions are not reported
self.ae(q('x', s=1, v=1, f=24, U=1), r())
# a feature query does not disturb a chunked load in progress
self.assertIsNone(pl('ab', i=99, s=1, v=1, f=24, m=1))
self.ae(q('T', U=1), r(',e=T,U=1'))
self.ae(pl('c', i=99, m=0), 'OK')
self.ae(g.image_count, 1)
s.reset()
self.ae(g.image_count, 0)
self.assertIsNone(q('T', U=1, q=1))
# Test simple load
for f in 32, 24:
p = 'abc' + ('d' if f == 32 else '')
img = sl(p, s=1, v=1, f=f)
self.ae(bool(img['is_4byte_aligned']), f == 32)
# Test chunked load
self.assertIsNone(pl('abcd', s=2, v=2, m=1))
self.assertIsNone(pl('efgh', m=1))
self.assertIsNone(pl('ijkl', m=1))
self.ae(pl('mnop', m=0), 'OK')
img = g.image_for_client_id(1)
self.ae(img['data'], b'abcdefghijklmnop')
# Test interrupted and retried chunked load
self.assertIsNone(pl('abcd', s=2, v=2, m=1))
self.assertIsNone(pl('efgh', m=1))
send_command(s, 'a=d') # delete command should clear partial transfer
self.assertIsNone(pl('abcd', s=2, v=2, m=1))
self.assertIsNone(pl('efgh', m=1))
self.assertIsNone(pl('ijkl', m=1))
self.ae(pl('1234', m=0), 'OK')
img = g.image_for_client_id(1)
self.ae(img['data'], b'abcdefghijkl1234')
random_data = byte_block(32 * 1024)
sl(random_data, s=1024, v=8, expecting_data=random_data)
# Test compression
compressed_random_data = zlib.compress(random_data)
sl(compressed_random_data, s=1024, v=8, o='z', expecting_data=random_data)
# Test chunked + compressed
b = len(compressed_random_data) // 2
self.assertIsNone(pl(compressed_random_data[:b], s=1024, v=8, o='z', m=1))
self.ae(pl(compressed_random_data[b:], m=0), 'OK')
img = g.image_for_client_id(1)
self.ae(img['data'], random_data)
# Test loading from file
def load_temp(prefix='tty-graphics-protocol-'):
f = tempfile.NamedTemporaryFile(prefix=prefix)
f.write(random_data), f.flush()
sl(f.name, s=1024, v=8, t='f', expecting_data=random_data)
self.assertTrue(os.path.exists(f.name))
f.seek(0), f.truncate(), f.write(compressed_random_data), f.flush()
sl(f.name, s=1024, v=8, t='t', o='z', expecting_data=random_data)
return f
f = load_temp()
self.assertFalse(os.path.exists(f.name), f'Temp file at {f.name} was not deleted')
with suppress(FileNotFoundError):
f.close()
f = load_temp('')
self.assertTrue(os.path.exists(f.name), f'Temp file at {f.name} was deleted')
f.close()
# Test loading from POSIX SHM
name = '/kitty-test-shm'
shm_write(name, random_data)
sl(name, s=1024, v=8, t='s', expecting_data=random_data)
self.assertRaises(FileNotFoundError, shm_unlink, name) # check that file was deleted
s.reset()
self.assertEqual(g.disk_cache.total_size, 0)
def test_load_images_from_file_edge_cases(self):
s, g, pl, sl = load_helpers(self)
random_data = byte_block(32 * 1024)
# Failures to read an image file must all be reported with the same
# response, otherwise a client, which can be a program running on a
# remote machine or in a sandbox, can use the response to probe the
# filesystem for the existence, type and size of files.
generic_error = 'EBADF:Failed to read image file'
with tempfile.NamedTemporaryFile(prefix='tty-graphics-protocol-') as f:
# A window of the file specified with a non page aligned offset
f.write(b'x' * 3 + random_data + b'y' * 5), f.flush()
sl(f.name, s=1024, v=8, t='f', S=len(random_data), O=3, expecting_data=random_data)
# A file that is truncated after the size declared in the command
# must be reported as a generic failure rather than crashing or
# leaking the size of the file
f.seek(0), f.truncate(), f.write(random_data[:128]), f.flush()
self.ae(pl(f.name, s=1024, v=8, t='f', S=len(random_data)), generic_error)
# Ditto when the size is not declared and is read from the file itself
self.ae(pl(f.name, s=1024, v=8, t='f'), generic_error)
# An offset past the end of the file
self.ae(pl(f.name, s=1024, v=8, t='f', O=4096), generic_error)
# Only regular files may be read
with tempfile.TemporaryDirectory(prefix='tty-graphics-protocol-') as tdir:
fifo = os.path.join(tdir, 'fifo')
os.mkfifo(fifo)
self.ae(pl(fifo, s=1024, v=8, t='f'), generic_error, 'Reading from a FIFO was not refused')
# Neither the existence nor the type of a file may be leaked, so a
# non-existent file, a directory and a file that is too small must
# all give byte for byte identical responses
small = os.path.join(tdir, 'small')
with open(small, 'wb') as sf:
sf.write(random_data[:7])
for path in (os.path.join(tdir, 'does-not-exist'), tdir, small):
self.ae(pl(path, s=1024, v=8, t='f'), generic_error, path)
# Failing to read a file must not leak the file descriptor opened
# for it, else a client can exhaust the process wide fd limit
paths = (small, tdir, os.path.join(tdir, 'does-not-exist'), fifo)
for path in paths:
pl(path, s=1024, v=8, t='f') # warm up any lazily opened fds
before = num_open_fds()
for i in range(64):
for path in paths:
pl(path, s=1024, v=8, t='f')
self.ae(before, num_open_fds(), 'File descriptors were leaked when failing to read image files')
# A window of a shared memory object with a non page aligned offset
name = '/kitty-test-shm-offset'
shm_write(name, b'x' * 3 + random_data + b'y' * 5)
sl(name, s=1024, v=8, t='s', S=len(random_data), O=3, expecting_data=random_data)
self.assertRaises(FileNotFoundError, shm_unlink, name) # check that the object was deleted
# A shared memory object truncated to less than the declared size
name = '/kitty-test-shm-truncated'
shm_write(name, random_data[:64])
self.ae(pl(name, s=1024, v=8, t='s', S=len(random_data)), generic_error)
self.assertRaises(FileNotFoundError, shm_unlink, name) # check that the object was deleted
# A shared memory object that could not be opened must also be
# removed, and reported with the same generic error
self.ae(pl('/kitty-test-shm-missing', s=1024, v=8, t='s'), generic_error)
self.ae(pl('kitty-test-shm-missing-no-leading-slash', s=1024, v=8, t='s'), generic_error)
# Names without a leading slash must be accepted, as, for example,
# sandboxed applications on macOS cannot use a leading slash
name = 'kitty-test-shm-no-leading-slash'
shm_write(name, random_data)
sl(name, s=1024, v=8, t='s', expecting_data=random_data)
self.assertRaises(FileNotFoundError, shm_unlink, name) # check that the object was deleted
s.reset()
self.assertEqual(g.disk_cache.total_size, 0)
def test_graphics_file_reading_policy(self):
from kitty.fast_data_types import set_boss
from kitty.utils import is_ok_to_read_image_file, is_ok_to_read_image_path
class Boss:
def __init__(self):
self.path_checks = []
self.fd_checks = []
def is_ok_to_read_image_path(self, path):
self.path_checks.append(path)
return is_ok_to_read_image_path(path)
def is_ok_to_read_image_file(self, path, fd):
self.fd_checks.append(path)
return is_ok_to_read_image_file(path, fd)
def safe_delete_temp_file(self, path):
with suppress(FileNotFoundError):
os.remove(path)
s, g, pl, sl = load_helpers(self)
random_data = byte_block(32 * 1024)
generic_error = 'EBADF:Failed to read image file'
boss = Boss()
set_boss(boss)
try:
# Files in protected locations must be refused based on their path
# alone, without ever being opened, since both opening a file and
# the error from a failed open leak its existence
protected = ('/proc/self/cmdline', '/proc/does-not-exist', '/sys/kernel/does-not-exist', '/dev/null', '/dev/does-not-exist')
for path in protected:
self.ae(pl(path, s=1024, v=8, t='f'), generic_error, path)
self.assertFalse(boss.fd_checks, 'A file in a protected location was opened before the policy was applied')
self.ae(len(boss.path_checks), len(protected))
# Refusing to read a file must not leak the fd opened for it
before = num_open_fds()
for i in range(64):
for path in protected:
pl(path, s=1024, v=8, t='f')
self.ae(before, num_open_fds(), 'File descriptors were leaked when refusing to read image files')
# Allowed files are still readable
with tempfile.NamedTemporaryFile() as f:
f.write(random_data), f.flush()
sl(f.name, s=1024, v=8, t='f', expecting_data=random_data)
self.ae(boss.fd_checks[-1], f.name)
finally:
set_boss(None)
s.reset()
self.assertEqual(g.disk_cache.total_size, 0)
@unittest.skipIf(Image is None, 'PIL not available, skipping PNG tests')
def test_load_png(self):
s, g, pl, sl = load_helpers(self)
w, h = 5, 3
rgba_data = byte_block(w * h * 4)
img = Image.frombytes('RGBA', (w, h), rgba_data)
rgb_data = img.convert('RGB').convert('RGBA').tobytes()
self.assertEqual(g.disk_cache.total_size, 0)
def png(mode='RGBA'):
buf = BytesIO()
i = img
if mode != i.mode:
i = img.convert(mode)
i.save(buf, 'PNG')
return buf.getvalue()
for mode in 'RGBA RGB'.split():
data = png(mode)
sl(data, f=100, expecting_data=rgb_data if mode == 'RGB' else rgba_data)
for m in 'LP':
img = img.convert(m)
rgba_data = img.convert('RGBA').tobytes()
data = png(m)
sl(data, f=100, expecting_data=rgba_data)
self.ae(pl(b'a' * 20, f=100, S=20).partition(':')[0], 'EBADPNG')
s.reset()
self.assertEqual(g.disk_cache.total_size, 0)
def test_load_png_simple(self):
# 1x1 transparent PNG
expected = b'\x00\xff\xff\x7f'
self.ae(load_png_data(png_data), (expected, 1, 1))
s, g, pl, sl = load_helpers(self)
sl(png_data, f=100, expecting_data=expected)
# test error handling for loading bad png data
self.assertRaisesRegex(ValueError, '[EBADPNG]', load_png_data, b'dsfsdfsfsfd')
# Test that a large PNG chunk sent via direct transmission doesn't crash
# when its size exceeds 2 * initial_buf_capacity. Without the fix in
# commit 48ab623, the buffer reallocation used MIN(2*buf_capacity, MAX_DATA_SZ)
# which could leave buf_capacity smaller than the payload size, causing a
# heap buffer overflow when the data was copied in.
# Initial buf_capacity for PNG without an explicit S= is 10, so 2*10=20;
# a 25-byte chunk previously caused a crash.
res = pl(b'x' * 25, f=100)
self.ae(res.partition(':')[0], 'EBADPNG')
if Image is None:
return
# Test that a truncated PNG (valid header + IHDR declaring more rows
# than the IDAT data can produce, with no trailing chunks) is rejected
# cleanly instead of causing libpng to parse uninitialized memory. The
# read callback must abort via png_error() when it runs out of bytes.
w, h = 3, 3
buf = BytesIO()
Image.frombytes('RGBA', (w, h), byte_block(w * h * 4)).save(buf, 'PNG')
full = buf.getvalue()
# Drop the trailing bytes (IDAT tail + IEND) so the stream runs out
# while libpng still expects more compressed data.
truncated = full[:-16]
self.assertRaisesRegex(ValueError, '[EBADPNG]', load_png_data, truncated)
# Same, but with an embedded ICC profile so that lcms2 handles are
# allocated before libpng longjmps out, they must be freed in the
# error path (detected by LeakSanitizer in ASAN builds).
try:
from PIL import ImageCms
except ImportError:
return
icc = ImageCms.ImageCmsProfile(ImageCms.createProfile('sRGB')).tobytes()
buf = BytesIO()
Image.frombytes('RGBA', (w, h), byte_block(w * h * 4)).save(buf, 'PNG', icc_profile=icc)
self.assertRaisesRegex(ValueError, '[EBADPNG]', load_png_data, buf.getvalue()[:-16])
def test_gr_operations_with_numbers(self):
s = self.create_screen()
g = s.grman
self.assertEqual(g.disk_cache.total_size, 0)
def li(payload, **kw):
cmd = ','.join(f'{k}={v}' for k, v in kw.items())
res = send_command(s, cmd, payload)
return parse_response_with_ids(res)
code, ids = li('abc', s=1, v=1, f=24, I=1, i=3)
self.ae(code, 'EINVAL')
code, ids = li('abc', s=1, v=1, f=24, I=1)
self.ae((code, ids), ('OK', 'i=1,I=1'))
img = g.image_for_client_number(1)
self.ae(img['client_number'], 1)
self.ae(img['client_id'], 1)
code, ids = li('abc', s=1, v=1, f=24, I=1)
self.ae((code, ids), ('OK', 'i=2,I=1'))
img = g.image_for_client_number(1)
self.ae(img['client_number'], 1)
self.ae(img['client_id'], 2)
code, ids = li('abc', s=1, v=1, f=24, I=1)
self.ae((code, ids), ('OK', 'i=3,I=1'))
code, ids = li('abc', s=1, v=1, f=24, i=5)
self.ae((code, ids), ('OK', 'i=5'))
code, ids = li('abc', s=1, v=1, f=24, I=3)
self.ae((code, ids), ('OK', 'i=4,I=3'))
# Test chunked load with number
self.assertIsNone(li('abcd', s=2, v=2, m=1, I=93))
self.assertIsNone(li('efgh', m=1))
self.assertIsNone(li('ijkx', m=1))
self.ae(li('mnop', m=0), ('OK', 'i=6,I=93'))
img = g.image_for_client_number(93)
self.ae(img['data'], b'abcdefghijkxmnop')
self.ae(img['client_id'], 6)
# test put with number
def put(**kw):
cmd = ','.join(f'{k}={v}' for k, v in kw.items())
cmd = 'a=p,' + cmd
return parse_response_with_ids(send_command(s, cmd))
code, idstr = put(c=2, r=2, I=93)
self.ae((code, idstr), ('OK', 'i=6,I=93'))
code, idstr = put(c=2, r=2, I=94)
self.ae(code, 'ENOENT')
# test delete with number
def delete(ac='N', **kw):
cmd = 'a=d'
if ac:
cmd += f',d={ac}'
if kw:
cmd += ',' + ','.join(f'{k}={v}' for k, v in kw.items())
send_command(s, cmd)
count = s.grman.image_count
put(i=1), put(i=2), put(i=3), put(i=4), put(i=5)
delete(I=94)
self.ae(s.grman.image_count, count)
delete(I=93)
self.ae(s.grman.image_count, count - 1)
delete(I=1)
self.ae(s.grman.image_count, count - 2)
cn = 1117
li('abc', s=1, v=1, f=24, I=cn)
first_id = g.image_for_client_number(cn)['internal_id']
li('abc', s=1, v=1, f=24, I=cn)
second_id = g.image_for_client_number(cn)['internal_id']
self.assertNotEqual(first_id, second_id)
count = s.grman.image_count
delete(I=cn)
self.ae(g.image_for_client_number(cn)['internal_id'], first_id)
self.ae(s.grman.image_count, count - 1)
s.reset()
self.assertEqual(g.disk_cache.total_size, 0)
def test_image_put(self):
cw, ch = 10, 20
s, dx, dy, put_image, put_ref, layers, rect_eq = put_helpers(self, cw, ch)
self.ae(put_image(s, cw, ch)[1], 'OK')
l0 = layers(s)
self.ae(len(l0), 1)
rect_eq(l0[0]['src_rect'], 0, 0, 1, 1)
rect_eq(l0[0]['dest_rect'], -1, -1, -1 + dx, -1 + dy)
self.ae(l0[0]['group_count'], 1)
self.ae(s.cursor.x, 1), self.ae(s.cursor.y, 0)
src_width, src_height = 3, 5
iid, (code, idstr) = put_ref(
s, num_cols=s.columns, num_lines=1, x_off=2, y_off=1, width=src_width, height=src_height, cell_x_off=3, cell_y_off=1, z=-1, placement_id=17
)
self.ae(idstr, f'i={iid},p=17')
l2 = layers(s)
self.ae(len(l2), 2)
self.ae(l2[1], l0[0])
rect_eq(l2[0]['src_rect'], 2 / 10, 1 / 20, (2 + 3) / 10, (1 + 5) / 20)
self.ae(l2[0]['group_count'], 2)
left, top = -1 + dx + 3 * dx / cw, -1 + 1 * dy / ch
right = -1 + (1 + s.columns) * dx
bottom = -1 + dy
rect_eq(l2[0]['dest_rect'], left, top, right, bottom)
self.ae(s.cursor.x, 0), self.ae(s.cursor.y, 1)
self.ae(put_image(s, 10, 20, cursor_movement=1)[1], 'OK')
self.ae(s.cursor.x, 0), self.ae(s.cursor.y, 1)
s.reset()
self.assertEqual(s.grman.disk_cache.total_size, 0)
self.ae(put_image(s, 2 * cw, 2 * ch, num_cols=3)[1], 'OK')
self.ae((s.cursor.x, s.cursor.y), (3, 2))
rect_eq(layers(s)[0]['dest_rect'], -1, -1, -1 + 3 * dx, -1 + 3 * dy)
def test_graphics_put_with_pixel_offsets(self):
cw, ch = 10, 20
# Image 10x20 placed with 5px X and Y pixel offsets
s, dx, dy, put_image, put_ref, layers, rect_eq = put_helpers(self, cw, ch)
self.ae(put_image(s, 10, 20, cell_x_off=5, cell_y_off=5)[1], 'OK')
self.ae((s.cursor.x, s.cursor.y), (2, 1))
def test_image_layer_grouping(self):
cw, ch = 10, 20
s, dx, dy, put_image, put_ref, layers, rect_eq = put_helpers(self, cw, ch)
def group_counts():
return tuple(x['group_count'] for x in layers(s))
self.ae(put_image(s, 10, 20, id=1)[1], 'OK')
self.ae(group_counts(), (1,))
put_ref(s, id=1, num_cols=2, num_lines=1, placement_id=2)
put_ref(s, id=1, num_cols=2, num_lines=1, placement_id=3, z=-2)
put_ref(s, id=1, num_cols=2, num_lines=1, placement_id=4, z=-2)
self.ae(group_counts(), (4, 3, 2, 1))
self.ae(put_image(s, 8, 16, id=2, z=-1)[1], 'OK')
self.ae(group_counts(), (2, 1, 1, 2, 1))
def test_indexed_color_images(self):
s, g, pl, sl = load_helpers(self)
li = make_send_command(s)
# Loading
self.ae(pl('ab', s=1, v=1, f=8, a='q'), 'OK')
self.ae(g.image_count, 0)
img = sl(bytes(range(8)), s=4, v=2, f=8)
self.ae(img['indexed_px_sz'], 1)
img = sl(bytes(range(8)), s=2, v=2, f=16)
self.ae(img['indexed_px_sz'], 2)
data = bytes(range(256)) * 4
img = sl(zlib.compress(data), s=32, v=16, f=16, o='z', expecting_data=data)
self.ae(img['indexed_px_sz'], 2)
self.ae(pl(bytes(range(3)), s=3, v=1, f=8), 'OK')
img = g.image_for_client_id(1)
self.ae((img['indexed_px_sz'], img['is_4byte_aligned'], img['data']), (1, False, bytes(range(3))))
self.assertTrue(pl(bytes(range(5)), s=3, v=1, f=16).startswith('ENODATA'))
self.assertTrue(pl(b'', s=0, v=1, f=8).startswith('EINVAL'))
# Replacing an indexed image with a normal one
img = sl(b'abcd', s=1, v=1, f=32)
self.ae(img['indexed_px_sz'], 0)
# Animation is not supported
def unsupported(r, what='Animation'):
self.ae((r.code, r.msg), ('EINVAL', f'{what} is not supported for indexed color images'))
self.ae(li(b'abcd', s=2, v=2, f=8, a='t').code, 'OK')
unsupported(li(b'abcd', s=2, v=2, f=8, a='f'))
unsupported(li(b'abcd', s=1, v=1, f=32, a='f'))
unsupported(li(b'', a='c', r=1, c=1, w=1, h=1, x=1, s=0, v=0, f=0), 'Composition')
self.ae(g.image_for_client_id(1)['extra_frames'], ())
self.ae(li(b'abcd', s=1, v=1, f=32, a='t').code, 'OK')
unsupported(li(b'abcd', s=2, v=2, f=8, a='f'))
# Render data
cw, ch = 10, 20
s, dx, dy, put_image, put_ref, layers, rect_eq = put_helpers(self, cw, ch)
self.ae(put_image(s, 10, 20, id=1)[1], 'OK')
self.ae(parse_response(send_command(s, 'a=T,f=16,i=2,s=2,v=2,z=-1', b'\x01\xff' * 4)), 'OK')
self.ae(parse_response(send_command(s, 'a=T,f=8,i=3,s=2,v=2,z=1', b'\xf8' * 4)), 'OK')
self.ae(tuple((x['image_id'], x['indexed_px_sz']) for x in layers(s)), ((2, 2), (1, 0), (3, 1)))
self.ae(s.grman.image_count, 3)
# Refs drawn at natural size use a cheaper program, so must not share
# a group with scaled refs of the same image
s, dx, dy, put_image, put_ref, layers, rect_eq = put_helpers(self, cw, ch)
self.ae(parse_response(send_command(s, 'a=T,f=8,i=1,s=2,v=2', b'\x01' * 4)), 'OK')
self.ae(parse_response(send_command(s, 'a=p,i=1,c=1,r=1')), 'OK')
self.ae(parse_response(send_command(s, 'a=p,i=1')), 'OK')
self.ae(parse_response(send_command(s, 'a=T,f=32,i=2,s=2,v=2', b'abcd' * 4)), 'OK')
self.ae(tuple((x['is_unscaled'], x['group_count']) for x in layers(s)), ((True, 1), (False, 1), (True, 1), (False, 1)))
def test_image_parents(self):
cw, ch = 10, 20
iw, ih = 10, 20
s, dx, dy, put_image, put_ref, layers, rect_eq = put_helpers(self, cw, ch)
def positions():
ans = {}
def x(x):
return round(((x + 1) / 2) * s.columns)
def y(y):
return int(((y + 1) / 2) * s.lines)
for i in layers(s):
d = i['dest_rect']
ans[(i['image_id'], i['ref_id'])] = {'x': x(d['left']), 'y': y(d['top'])}
return ans
def p(x, y=0):
return {'x': x, 'y': y}
self.ae(put_image(s, iw, ih, id=1)[1], 'OK')
self.ae(put_ref(s, id=1, placement_id=1), (1, ('OK', 'i=1,p=1')))
pos = {(1, 1): p(0), (1, 2): p(1)}
self.ae(positions(), pos)
# check that adding a reference to a non-existent parent fails
self.ae(put_ref(s, id=1, placement_id=33, parent_id=1, parent_placement_id=2), (1, ('ENOPARENT', 'i=1,p=33')))
self.ae(put_ref(s, id=1, placement_id=33, parent_id=33), (1, ('ENOPARENT', 'i=1,p=33')))
# check that we cannot add a reference that is its own parent
self.ae(put_ref(s, id=1, placement_id=1, parent_id=1, parent_placement_id=1), (1, ('EINVAL', 'i=1,p=1')))
self.ae(put_image(s, iw, ih, id=2)[1], 'OK')
pos[(2, 1)] = p(2)
self.ae(positions(), pos)
# Add two children to the first placement of img2
before = s.cursor.x, s.cursor.y
self.ae(put_ref(s, id=1, placement_id=2, parent_id=2, offset_from_parent_y=3), (1, ('OK', 'i=1,p=2')))
self.ae(before, (s.cursor.x, s.cursor.y), 'Cursor must not move for child image')
pos[(1, 3)] = p(2, 3)
self.ae(positions(), pos)
self.ae(put_ref(s, id=2, placement_id=3, parent_id=2, offset_from_parent_y=4), (2, ('OK', 'i=2,p=3')))
pos[(2, 2)] = p(2, 4)
self.ae(positions(), pos)
# Add a grand child to the second child of img2
self.ae(put_ref(s, id=2, placement_id=4, parent_id=2, parent_placement_id=3, offset_from_parent_x=-1), (2, ('OK', 'i=2,p=4')))
pos[(2, 3)] = p(pos[(2, 2)]['x'] - 1, pos[(2, 2)]['y'])
self.ae(positions(), pos)
# Check that creating a cycle is prevented
self.ae(put_ref(s, id=2, placement_id=3, parent_id=2, parent_placement_id=4), (2, ('ECYCLE', 'i=2,p=3')))
self.ae(positions(), pos)
# Check that depth is limited
for i in range(5, 12):
q = put_ref(s, id=2, placement_id=i, parent_id=2, parent_placement_id=i - 1, offset_from_parent_x=-1)[1][0]
if q == 'ETOODEEP':
break
self.ae(q, 'OK')
else:
self.assertTrue(False, 'Failed to limit reference chain depth')
# Check that deleting a parent removes all descendants
send_command(s, 'a=d,d=i,i=2,p=3')
pos.pop((2, 3)), pos.pop((2, 2))
self.ae(positions(), pos)
# Check that deleting a parent deletes all descendants and also removes
# images with no remaining placements
self.ae(put_ref(s, id=2, placement_id=3, parent_id=2, offset_from_parent_y=4), (2, ('OK', 'i=2,p=3')))
pos[(2, 11)] = p(2, 4)
self.ae(positions(), pos)
self.ae(put_image(s, iw, ih, id=3, placement_id=97, parent_id=2, parent_placement_id=3)[1], 'OK')
pos[(3, 1)] = p(2, 4)
self.ae(positions(), pos)
send_command(s, 'a=d,d=i,i=2')
pos.pop((3, 1)), pos.pop((2, 11)), pos.pop((2, 1)), pos.pop((1, 3))
self.ae(positions(), pos)
# Check that virtual placements that try to be relative are rejected
self.ae(put_ref(s, id=1, placement_id=11, parent_id=1, unicode_placeholder=1), (1, ('EINVAL', 'i=1,p=11')))
# Check creation of children of a unicode placeholder based image
s, dx, dy, put_image, put_ref, layers, rect_eq = put_helpers(self, cw, ch)
put_image(s, 20, 20, num_cols=4, num_lines=2, unicode_placeholder=1, id=42)
s.update_only_line_graphics_data()
self.assertFalse(positions()) # the reference is virtual
self.ae(put_ref(s, id=42, placement_id=11, parent_id=42, offset_from_parent_y=2, offset_from_parent_x=1), (42, ('OK', 'i=42,p=11')))
self.assertFalse(positions()) # the reference is virtual without any cell images so the child is invisible
s.apply_sgr('38;5;42')
# These two characters will become one 2x1 ref.
s.cursor.x = s.cursor.y = 1
s.draw('\U0010eeee\u0305\u0305\U0010eeee\u0305\u030d')
s.cursor.x = s.cursor.y = 0
s.draw('\U0010eeee\u0305\u0305\U0010eeee\u0305\u030d')
s.update_only_line_graphics_data()
pos = {(1, 2): p(1, 2), (1, 3): p(0), (1, 4): p(1)}
self.ae(positions(), pos)
s.cursor.x = s.cursor.y = 0
s.erase_in_display(0, False)
s.update_only_line_graphics_data()
self.assertFalse(positions()) # the reference is virtual without any cell images so the child is invisible
s.cursor.x = s.cursor.y = 2
s.draw('\U0010eeee\u0305\u0305\U0010eeee\u0305\u030d')
s.update_only_line_graphics_data()
self.ae(positions(), {(1, 5): {'x': 2, 'y': 2}, (1, 2): {'x': 3, 'y': 4}})
def test_unicode_placeholders(self):
# This test tests basic image placement using unicode placeholders
cw, ch = 10, 20
s, dx, dy, put_image, put_ref, layers, rect_eq = put_helpers(self, cw, ch)
# Upload two images.
put_image(s, 20, 20, num_cols=4, num_lines=2, unicode_placeholder=1, id=42)
put_image(s, 10, 20, num_cols=4, num_lines=2, unicode_placeholder=1, id=(42 << 16) + (43 << 8) + 44)
# The references are virtual, so no visible refs yet.
s.update_only_line_graphics_data()
refs = layers(s)
self.ae(len(refs), 0)
# A reminder of row/column diacritics meaning (assuming 0-based):
# \u0305 -> 0
# \u030D -> 1
# \u030E -> 2
# \u0310 -> 3
# Now print the placeholders for the first image.
# Encode the id as an 8-bit color.
s.apply_sgr('38;5;42')
# These two characters will become one 2x1 ref.
s.draw('\U0010eeee\u0305\u0305\U0010eeee\u0305\u030d')
# These two characters will be two separate refs (not contiguous).
s.draw('\U0010eeee\u0305\u0305\U0010eeee\u0305\u030e')
s.cursor_move(4)
s.update_only_line_graphics_data()
refs = layers(s)
self.ae(len(refs), 3)
self.ae(refs[0]['src_rect'], {'left': 0.0, 'top': 0.0, 'right': 0.5, 'bottom': 0.5})
self.ae(refs[1]['src_rect'], {'left': 0.0, 'top': 0.0, 'right': 0.25, 'bottom': 0.5})
self.ae(refs[2]['src_rect'], {'left': 0.5, 'top': 0.0, 'right': 0.75, 'bottom': 0.5})
# Erase the line.
s.erase_in_line(2)
# There must be 0 refs after the line is erased.
s.update_only_line_graphics_data()
refs = layers(s)
self.ae(len(refs), 0)
# Now test encoding IDs with the 24-bit color.
# The first image, 1x1
s.apply_sgr('38;2;0;0;42')
s.draw('\U0010eeee\u0305\u0305')
# The second image, 2x1
s.apply_sgr('38;2;42;43;44')
s.draw('\U0010eeee\u0305\u030d\U0010eeee\u0305\u030e')
s.cursor_move(2)
s.update_only_line_graphics_data()
refs = layers(s)
self.ae(len(refs), 2)
self.ae(refs[0]['src_rect'], {'left': 0.0, 'top': 0.0, 'right': 0.25, 'bottom': 0.5})
# The second ref spans the whole widths of the second image because it's
# fit to height and centered in a 4x2 box (specified in put_image).
self.ae(refs[1]['src_rect'], {'left': 0.0, 'top': 0.0, 'right': 1.0, 'bottom': 0.5})
# Erase the line.
s.erase_in_line(2)
# Now test implicit column numbers.
# We will mix implicit and explicit column/row specifications, but they
# will be combine into just two references.
s.apply_sgr('38;5;42')
# full row 0 of the first image
s.draw('\U0010eeee\u0305\u0305\U0010eeee\u0305\U0010eeee\U0010eeee\u0305')
# full row 1 of the first image
s.draw('\U0010eeee\u030d\U0010eeee\U0010eeee\U0010eeee\u030d\u0310')
s.cursor_move(8)
s.update_only_line_graphics_data()
refs = layers(s)
self.ae(len(refs), 2)
self.ae(refs[0]['src_rect'], {'left': 0.0, 'top': 0.0, 'right': 1.0, 'bottom': 0.5})
self.ae(refs[1]['src_rect'], {'left': 0.0, 'top': 0.5, 'right': 1.0, 'bottom': 1.0})
# Now reset the screen, the images should be erased.
s.reset()
refs = layers(s)
self.ae(len(refs), 0)
def test_unicode_placeholders_3rd_combining_char(self):
# This test tests that we can use the 3rd diacritic for the most
# significant byte
cw, ch = 10, 20
s, dx, dy, put_image, put_ref, layers, rect_eq = put_helpers(self, cw, ch)
# Upload two images.
put_image(s, 20, 20, num_cols=4, num_lines=2, unicode_placeholder=1, id=42)
put_image(s, 20, 10, num_cols=4, num_lines=1, unicode_placeholder=1, id=(42 << 24) + 43)
# This one will have id=43, which does not exist.
s.apply_sgr('38;2;0;0;43')
s.draw('\U0010eeee\u0305\U0010eeee\U0010eeee\U0010eeee')
s.cursor_move(4)
s.update_only_line_graphics_data()
refs = layers(s)
self.ae(len(refs), 0)
s.erase_in_line(2)
# This one will have id=42. We explicitly specify that the most
# significant byte is 0 (third \u305). Specifying the zero byte like
# this is not necessary but is correct.
s.apply_sgr('38;2;0;0;42')
s.draw('\U0010eeee\u0305\u0305\u0305\U0010eeee\u0305\u030d\u0305')
# This is the second image.
# \u059C -> 42
s.apply_sgr('38;2;0;0;43')
s.draw('\U0010eeee\u0305\u0305\u059c\U0010eeee\u0305\u030d\u059c')
# Check that we can continue by using implicit row/column specification.
s.draw('\U0010eeee\u0305\U0010eeee')
s.cursor_move(6)
s.update_only_line_graphics_data()
refs = layers(s)
self.ae(len(refs), 2)
self.ae(refs[0]['src_rect'], {'left': 0.0, 'top': 0.0, 'right': 0.5, 'bottom': 0.5})
self.ae(refs[1]['src_rect'], {'left': 0.0, 'top': 0.0, 'right': 1.0, 'bottom': 1.0})
s.erase_in_line(2)
# Now test the 8-bit color mode. Using the third diacritic, we can
# specify 16 bits: the most significant byte and the least significant
# byte.
s.apply_sgr('38;5;42')
s.draw('\U0010eeee\u0305\u0305\u0305\U0010eeee')
s.apply_sgr('38;5;43')
s.draw('\U0010eeee\u0305\u0305\u059c\U0010eeee\U0010eeee\u0305\U0010eeee')
s.cursor_move(6)
s.update_only_line_graphics_data()
refs = layers(s)
self.ae(len(refs), 2)
self.ae(refs[0]['src_rect'], {'left': 0.0, 'top': 0.0, 'right': 0.5, 'bottom': 0.5})
self.ae(refs[1]['src_rect'], {'left': 0.0, 'top': 0.0, 'right': 1.0, 'bottom': 1.0})
def test_unicode_placeholders_multiple_placements(self):
# Here we test placement specification via underline color.
cw, ch = 10, 20
s, dx, dy, put_image, put_ref, layers, rect_eq = put_helpers(self, cw, ch)
put_image(s, 20, 20, num_cols=1, num_lines=1, placement_id=1, unicode_placeholder=1, id=42)
put_ref(s, id=42, num_cols=2, num_lines=1, placement_id=22, unicode_placeholder=1)
put_ref(s, id=42, num_cols=4, num_lines=2, placement_id=44, unicode_placeholder=1)
# The references are virtual, so no visible refs yet.
s.update_only_line_graphics_data()
refs = layers(s)
self.ae(len(refs), 0)
# Draw the first row of each placement.
s.apply_sgr('38;5;42')
s.apply_sgr('58;5;1')
s.draw('\U0010eeee\u0305')
s.apply_sgr('58;5;22')
s.draw('\U0010eeee\u0305\U0010eeee\u0305')
s.apply_sgr('58;5;44')
s.draw('\U0010eeee\u0305\U0010eeee\u0305\U0010eeee\u0305\U0010eeee\u0305')
s.update_only_line_graphics_data()
refs = layers(s)
self.ae(len(refs), 3)
self.ae(refs[0]['src_rect'], {'left': 0.0, 'top': 0.0, 'right': 1.0, 'bottom': 1.5})
self.ae(refs[1]['src_rect'], {'left': 0.0, 'top': 0.0, 'right': 1.0, 'bottom': 1.0})
self.ae(refs[2]['src_rect'], {'left': 0.0, 'top': 0.0, 'right': 1.0, 'bottom': 0.5})
def test_unicode_placeholders_scroll(self):
# Here we test scrolling of a region. We'll draw an image spanning 8
# rows and then scroll only the middle part of this image. Each
# reference corresponds to one row.
cw, ch = 5, 10
s, dx, dy, put_image, put_ref, layers, rect_eq = put_helpers(self, cw, ch, lines=8)
put_image(s, 5, 80, num_cols=1, num_lines=8, unicode_placeholder=1, id=42)
s.apply_sgr('38;5;42')
s.cursor_position(1, 0)
s.draw('\U0010eeee\u0305\n')
s.cursor_position(2, 0)
s.draw('\U0010eeee\u030d\n')
s.cursor_position(3, 0)
s.draw('\U0010eeee\u030e\n')
s.cursor_position(4, 0)
s.draw('\U0010eeee\u0310\n')
s.cursor_position(5, 0)
s.draw('\U0010eeee\u0312\n')
s.cursor_position(6, 0)
s.draw('\U0010eeee\u033d\n')
s.cursor_position(7, 0)
s.draw('\U0010eeee\u033e\n')
s.cursor_position(8, 0)
s.draw('\U0010eeee\u033f')
# Each line will contain a part of the image.
s.update_only_line_graphics_data()
refs = layers(s)
refs = sorted(refs, key=lambda r: r['src_rect']['top'])
self.ae(len(refs), 8)
for i in range(8):
self.ae(refs[i]['src_rect'], {'left': 0.0, 'top': 0.125 * i, 'right': 1.0, 'bottom': 0.125 * (i + 1)})
self.ae(refs[i]['dest_rect']['top'], -1 + 0.25 * i)
# Now set margins to lines 3 and 6.
s.set_margins(3, 6) # 1-based indexing
# Scroll two lines down (i.e. move lines 3..6 up).
# Lines 3 and 4 will be erased.
s.cursor_position(6, 0)
s.index()
s.index()
s.update_only_line_graphics_data()
refs = layers(s)
refs = sorted(refs, key=lambda r: r['src_rect']['top'])
self.ae(len(refs), 6)
# Lines 1 and 2 are outside of the region, not scrolled.
self.ae(refs[0]['src_rect'], {'left': 0.0, 'top': 0.0, 'right': 1.0, 'bottom': 0.125})
self.ae(refs[0]['dest_rect']['top'], -1.0)
self.ae(refs[1]['src_rect'], {'left': 0.0, 'top': 0.125 * 1, 'right': 1.0, 'bottom': 0.125 * 2})
self.ae(refs[1]['dest_rect']['top'], -1.0 + 0.25 * 1)
# Lines 3 and 4 are erased.
# Lines 5 and 6 are now higher.
self.ae(refs[2]['src_rect'], {'left': 0.0, 'top': 0.125 * 4, 'right': 1.0, 'bottom': 0.125 * 5})
self.ae(refs[2]['dest_rect']['top'], -1.0 + 0.25 * 2)
self.ae(refs[3]['src_rect'], {'left': 0.0, 'top': 0.125 * 5, 'right': 1.0, 'bottom': 0.125 * 6})
self.ae(refs[3]['dest_rect']['top'], -1.0 + 0.25 * 3)
# Lines 7 and 8 are outside of the region.
self.ae(refs[4]['src_rect'], {'left': 0.0, 'top': 0.125 * 6, 'right': 1.0, 'bottom': 0.125 * 7})
self.ae(refs[4]['dest_rect']['top'], -1.0 + 0.25 * 6)
self.ae(refs[5]['src_rect'], {'left': 0.0, 'top': 0.125 * 7, 'right': 1.0, 'bottom': 0.125 * 8})
self.ae(refs[5]['dest_rect']['top'], -1.0 + 0.25 * 7)
# Now scroll three lines up (i.e. move lines 5..6 down).
# Line 6 will be erased.
s.cursor_position(3, 0)
s.reverse_index()
s.reverse_index()
s.reverse_index()
s.update_only_line_graphics_data()
refs = layers(s)
refs = sorted(refs, key=lambda r: r['src_rect']['top'])
self.ae(len(refs), 5)
# Lines 1 and 2 are outside of the region, not scrolled.
self.ae(refs[0]['src_rect'], {'left': 0.0, 'top': 0.0, 'right': 1.0, 'bottom': 0.125})
self.ae(refs[0]['dest_rect']['top'], -1.0)
self.ae(refs[1]['src_rect'], {'left': 0.0, 'top': 0.125 * 1, 'right': 1.0, 'bottom': 0.125 * 2})
self.ae(refs[1]['dest_rect']['top'], -1.0 + 0.25 * 1)
# Lines 3, 4 and 6 are erased.
# Line 5 is now lower.
self.ae(refs[2]['src_rect'], {'left': 0.0, 'top': 0.125 * 4, 'right': 1.0, 'bottom': 0.125 * 5})
self.ae(refs[2]['dest_rect']['top'], -1.0 + 0.25 * 5)
# Lines 7 and 8 are outside of the region.
self.ae(refs[3]['src_rect'], {'left': 0.0, 'top': 0.125 * 6, 'right': 1.0, 'bottom': 0.125 * 7})
self.ae(refs[3]['dest_rect']['top'], -1.0 + 0.25 * 6)
self.ae(refs[4]['src_rect'], {'left': 0.0, 'top': 0.125 * 7, 'right': 1.0, 'bottom': 0.125 * 8})
self.ae(refs[4]['dest_rect']['top'], -1.0 + 0.25 * 7)
def test_unicode_placeholders_reverse_scroll(self):
# A cell image pushed off the bottom of the screen by a reverse scroll
# must not come back when the screen is scrolled forward again.
cw, ch = 5, 10
s, dx, dy, put_image, put_ref, layers, rect_eq = put_helpers(self, cw, ch, lines=4)
put_image(s, 5, 10, num_cols=1, num_lines=1, unicode_placeholder=1, id=42)
s.apply_sgr('38;5;42')
s.cursor_position(4, 1)
s.draw('\U0010eeee̅')
s.update_only_line_graphics_data()
self.ae(len(layers(s)), 1)
s.cursor_position(1, 1)
s.reverse_index()
s.update_only_line_graphics_data()
self.ae(len(layers(s)), 0)
s.cursor_position(4, 1)
s.index()
s.update_only_line_graphics_data()
self.ae(len(layers(s)), 0)
# Same via CSI T
s.reset()
s.apply_sgr('38;5;42')
s.cursor_position(4, 1)
s.draw('\U0010eeee̅')
s.update_only_line_graphics_data()
self.ae(len(layers(s)), 1)
s.reverse_scroll(2)
s.update_only_line_graphics_data()
self.ae(len(layers(s)), 0)
s.cursor_position(4, 1)
s.index(), s.index()
s.update_only_line_graphics_data()
self.ae(len(layers(s)), 0)
def test_unicode_placeholders_history_margins(self):
# Lines scrolled out of a region at the top of the screen (as tmux does for
# a pane above its status line) enter the history, and the cell images
# rendered for them while the view was scrolled back must move with them.
cw, ch = 5, 10
s, dx, dy, put_image, put_ref, layers, rect_eq = put_helpers(self, cw, ch, lines=6)
put_image(s, 5, 20, num_cols=1, num_lines=2, unicode_placeholder=1, id=42)
def draw_placeholders():
s.apply_sgr('38;5;42')
s.cursor_position(3, 1)
s.draw('\U0010eeee\u0305')
s.cursor_position(4, 1)
s.draw('\U0010eeee\u030d')
s.apply_sgr('39')
def index_lines(row, n):
s.cursor_position(row, 1)
for i in range(n):
s.draw('plain')
s.index()
s.carriage_return()
s.update_only_line_graphics_data()
def scroll_view(amt, up):
s.scroll(amt, up)
s.update_only_line_graphics_data()
return layers(s, s.scrolled_by)
for region in (True, False):
s.reset()
if region:
s.set_margins(1, s.lines - 1)
bottom = s.lines - 1 if region else s.lines
draw_placeholders()
index_lines(bottom, 8)
self.ae(len(scroll_view(6, True)), 2)
scroll_view(6, False)
index_lines(bottom, 3)
self.ae(len(scroll_view(9, True)), 0, f'region={region}')
scroll_view(9, False)
# A reverse index must not move the refs of lines in the history
s.reset()
draw_placeholders()
index_lines(s.lines, 5)
def ref_rows(refs):
return sorted(round((r['dest_rect']['top'] + 1) / dy) - s.scrolled_by for r in refs)
before = ref_rows(scroll_view(6, True))
self.ae(before, [-3, -2])
scroll_view(6, False)
s.cursor_position(1, 1)
s.reverse_index()
s.update_only_line_graphics_data()
self.ae(ref_rows(scroll_view(6, True)), before)
def test_image_layers_with_paused_rendering(self):
# While rendering is paused, images are drawn from a frozen copy of the
# graphics manager, so they must be positioned using the scroll state
# captured with the frozen text, not the live one, otherwise output
# received during the pause shifts them relative to the text.
cw, ch = 10, 20
def screen(pixel_scroll=False, lines=5, before=10, after=20):
s = self.create_screen(10, lines, scrollback=50, cell_width=cw, cell_height=ch, options={'pixel_scroll': pixel_scroll})
for i in range(before):
parse_bytes(s, f'row{i:03d}\r\n'.encode())
# The image is placed in the cell just after the text IMG
s.draw('IMG')
send_command(s, 'a=T,f=24,i=1,s=10,v=20,c=1,r=1,C=1,q=2', b'x' * 10 * 20 * 3)
s.carriage_return(), s.linefeed()
for i in range(after):
parse_bytes(s, f'row{i + before:03d}\r\n'.encode())
return s
def dy(s):
return 2 / s.lines
def live_layers(s):
offset = s.pixel_scroll_offset_y / ch if s.pixel_scroll_offset_y else 0
return s.grman.update_layers(s.scrolled_by, -1, -1, 2 / s.columns, dy(s), s.columns, s.lines, cw, ch, offset)
def image_rows(s, layers):
offset = s.rendered_scroll_state()[1] / ch
return [round((r['dest_rect']['top'] + 1) / dy(s) - offset) for r in layers]
def text_row(s):
for y in range(s.lines):
if str(s.visual_line(y)).startswith('IMG'):
return y
def assert_layers_close(a, b):
# Positions computed from different scroll states differ by float rounding
self.ae(len(a), len(b))
for x, y in zip(a, b):
self.ae({k: v for k, v in x.items() if k != 'dest_rect'}, {k: v for k, v in y.items() if k != 'dest_rect'})
for side in ('left', 'top', 'right', 'bottom'):
self.assertAlmostEqual(x['dest_rect'][side], y['dest_rect'][side], places=5)
def assert_aligned(s, layers, msg=''):
row = text_row(s)
self.assertIsNotNone(row, msg)
self.ae(image_rows(s, layers), [row], msg)
# Not paused: matches positioning with the live scroll state
for scrolled_by in (0, 5, 16, 17, 19, 21, 22):
s = screen()
s.scroll(scrolled_by, True)
self.ae(s.scrolled_by, scrolled_by)
layers = s.update_graphics_layers()
self.ae(layers, live_layers(s))
if 17 <= scrolled_by <= 21:
assert_aligned(s, layers, f'not paused, scrolled by {scrolled_by}')
else:
self.ae(layers, ()) # image is not visible
# Output arriving while paused and scrolled back
for num_new in (1, 3, 4, 10):
with self.subTest(num_new=num_new):
s = screen()
s.scroll(19, True)
before = s.update_graphics_layers()
self.ae(len(before), 1)
assert_aligned(s, before, 'before pause')
parse_bytes(s, b'\x1b[?2026h')
for i in range(num_new):
parse_bytes(s, f'new{i}\r\n'.encode())
self.ae(s.scrolled_by, 19 + num_new)
during = s.update_graphics_layers()
self.ae(during, before)
assert_aligned(s, during, 'during pause')
parse_bytes(s, b'\x1b[?2026l')
after = s.update_graphics_layers()
self.ae(after, live_layers(s))
assert_aligned(s, after, 'after pause')
assert_layers_close(after, before) # the view is anchored on the same content
# Several synchronized updates before the end of the pause
s = screen()
s.scroll(19, True)
before = s.update_graphics_layers()
parse_bytes(s, b'\x1b[?2026hnew\r\n\x1b[?2026l\x1b[?2026hmore\r\n')
self.ae(s.scrolled_by, 21)
assert_layers_close(s.update_graphics_layers(), before) # the second pause re-snapshots from a different scroll state
self.assertTrue(s.pause_rendering(False))
assert_aligned(s, s.update_graphics_layers(), 'after consecutive updates')
# Pixel scrolling: the fractional line offset is also frozen
for position in (18.5, 19.25):
with self.subTest(position=position):
s = screen(pixel_scroll=True)
s.scroll_to_absolute(position)
self.assertNotEqual(s.pixel_scroll_offset_y, 0)
before = s.update_graphics_layers()
self.ae(before, live_layers(s))
self.ae(len(before), 1)
parse_bytes(s, b'\x1b[?2026h')
for i in range(3):
parse_bytes(s, f'new{i}\r\n'.encode())
self.ae(s.update_graphics_layers(), before)
parse_bytes(s, b'\x1b[?2026l')
after = s.update_graphics_layers()
self.ae(after, live_layers(s))
assert_layers_close(after, before) # the view is anchored on the same content
# Paused at the bottom: output scrolls the live image up, the frozen one stays
s = screen(before=2, after=1)
self.ae(s.scrolled_by, 0)
before = s.update_graphics_layers()
assert_aligned(s, before, 'at bottom')
parse_bytes(s, b'\x1b[?2026h')
for i in range(2):
parse_bytes(s, f'new{i}\r\n'.encode())
self.ae(s.scrolled_by, 0)
self.ae(s.update_graphics_layers(), before)
self.assertNotEqual(live_layers(s), before)
parse_bytes(s, b'\x1b[?2026l')
after = s.update_graphics_layers()
self.ae(image_rows(s, after), [r - 2 for r in image_rows(s, before)])
assert_aligned(s, after, 'at bottom after pause')
# Filling from the scrollback during the pause moves the live view to the bottom
s = screen(after=4)
s.scroll(1, True)
self.ae(s.scrolled_by, 1)
before = s.update_graphics_layers()
self.ae(len(before), 1)
parse_bytes(s, b'\x1b[?2026h\x1b[+T')
self.ae(s.scrolled_by, 0)
self.ae(s.update_graphics_layers(), before)
self.assertTrue(s.pause_rendering(False))
self.ae(s.update_graphics_layers(), live_layers(s))
# Images deleted during the pause remain in the frozen frame
s = screen()
s.scroll(19, True)
before = s.update_graphics_layers()
parse_bytes(s, b'\x1b[?2026hnew\r\n')
send_command(s, 'a=d,d=I,i=1,q=2')
self.ae(s.update_graphics_layers(), before)
parse_bytes(s, b'\x1b[?2026l')
self.ae(s.update_graphics_layers(), ())
def test_unicode_placeholders_history_fill(self):
# Lines popped back from the history (CSI + T, or enlarging the window with
# scrollback_fill_enlarged_window) move the history down, so cell images
# rendered for lines in the history must move with them.
cw, ch = 5, 10
s, dx, dy, put_image, put_ref, layers, rect_eq = put_helpers(self, cw, ch, lines=6)
put_image(s, 5, 10, num_cols=1, num_lines=1, unicode_placeholder=1, id=42)
def ref_rows(scrolled_by=0):
s.update_only_line_graphics_data()
return sorted(round((r['dest_rect']['top'] + 1) / dy) - scrolled_by for r in layers(s, scrolled_by))
def rows_in_history():
# Render the history lines as when the view is scrolled back
n = s.historybuf.count
s.scroll(n, True)
ans = [r for r in ref_rows(n) if r < 0]
s.scroll(n, False)
s.update_only_line_graphics_data()
return ans
def setup(region):
# Push 'plain' and then a placeholder line into the history
s.reset()
if region:
s.set_margins(1, s.lines - 1)
s.draw('plain')
s.carriage_return(), s.linefeed()
s.apply_sgr('38;5;42')
s.draw('\U0010eeee\u0305')
s.apply_sgr('39')
s.cursor_position(s.lines - 1 if region else s.lines, 1)
s.index(), s.index()
self.ae(str(s.historybuf.line(0)), '\U0010eeee\u0305')
self.ae(rows_in_history(), [-1])
for region in (False, True):
setup(region)
s.reverse_scroll(1, True)
self.ae(str(s.line(0)), '\U0010eeee\u0305')
self.ae(rows_in_history(), [], f'region={region}')
self.ae(ref_rows(), [0], f'region={region}')
def classic_in_history():
nonlocal s, dy, layers
s, _, dy, put_image, _, layers, _ = put_helpers(self, cw, ch, lines=6)
put_image(s, cw, ch) # a one cell image at (0, 0)
s.draw('img')
s.cursor_position(s.lines, 1)
s.index()
self.ae(ref_rows(1), [-1])
# A reverse index that does not pull lines from the history leaves classic
# placements in the history in place
classic_in_history()
s.cursor_position(1, 1)
s.reverse_index()
self.ae(ref_rows(), [])
self.ae(ref_rows(1), [-1])
# Enlarging the window pulls lines back from the history (cell images are
# removed on resize, so only classic placements are left to move)
classic_in_history()
self.set_options({'scrollback_fill_enlarged_window': True})
s.cursor_position(2, 1)
s.resize(s.lines + 1, s.columns)
self.ae(str(s.line(0)), ' img')
self.ae(ref_rows(), [0])
def test_gr_scroll(self):
cw, ch = 10, 20
s, dx, dy, put_image, put_ref, layers, rect_eq = put_helpers(self, cw, ch)
put_image(s, 10, 20, no_id=True) # a one cell image at (0, 0)
self.ae(len(layers(s)), 1)
for i in range(s.lines):
s.index()
self.ae(len(layers(s)), 0), self.ae(s.grman.image_count, 1)
for i in range(s.historybuf.ynum - 1):
s.index()
self.ae(len(layers(s)), 0), self.ae(s.grman.image_count, 1)
s.index()
self.ae(s.grman.image_count, 0)
# Now test with margins
s.reset()
# Test images outside page area untouched
put_image(s, cw, ch) # a one cell image at (0, 0)
for i in range(s.lines - 1):
s.index()
put_image(s, cw, ch) # a one cell image at (0, bottom)
s.set_margins(2, 4) # 1-based indexing
self.ae(s.grman.image_count, 2)
for i in range(s.lines + s.historybuf.ynum):
s.index()
self.ae(s.grman.image_count, 2)
for i in range(s.lines): # ensure cursor is at top margin
s.reverse_index()
# Test clipped scrolling during index
put_image(s, cw, 2 * ch, z=-1, no_id=True) # 1x2 cell image
self.ae(s.grman.image_count, 3)
self.ae(layers(s)[0]['src_rect'], {'left': 0.0, 'top': 0.0, 'right': 1.0, 'bottom': 1.0})
s.index(), s.index()
l0 = layers(s)
self.ae(len(l0), 3)
self.ae(layers(s)[0]['src_rect'], {'left': 0.0, 'top': 0.5, 'right': 1.0, 'bottom': 1.0})
s.index()
self.ae(s.grman.image_count, 2)
# Test clipped scrolling during reverse_index
for i in range(s.lines):
s.reverse_index()
put_image(s, cw, 2 * ch, z=-1, no_id=True) # 1x2 cell image
self.ae(s.grman.image_count, 3)
self.ae(layers(s)[0]['src_rect'], {'left': 0.0, 'top': 0.0, 'right': 1.0, 'bottom': 1.0})
while s.cursor.y != 1:
s.reverse_index()
s.reverse_index(), s.reverse_index()
self.ae(layers(s)[0]['src_rect'], {'left': 0.0, 'top': 0.0, 'right': 1.0, 'bottom': 0.5})
s.reverse_index()
self.ae(s.grman.image_count, 2)
# Test that scaled images (r=/c=) are clipped rather than distorted
# when scrolled against a margin (#10377)
s.reset()
s.set_margins(1, 3) # 1-based indexing
put_image(s, cw, 4 * ch, num_cols=1, num_lines=2, no_id=True) # 10x80 px image scaled into 1x2 cells at (0, 0)
self.ae(s.grman.image_count, 1)
self.ae(layers(s)[0]['src_rect'], {'left': 0.0, 'top': 0.0, 'right': 1.0, 'bottom': 1.0})
rect_eq(layers(s)[0]['dest_rect'], -1, -1, -1 + dx, -1 + 2 * dy)
while s.cursor.y != 2:
s.index()
s.index() # scroll up, the top row of the image is clipped
l0 = layers(s)
self.ae(len(l0), 1)
self.ae(l0[0]['src_rect'], {'left': 0.0, 'top': 0.5, 'right': 1.0, 'bottom': 1.0})
rect_eq(l0[0]['dest_rect'], -1, -1, -1 + dx, -1 + dy)
s.index()
self.ae(s.grman.image_count, 0)
# Now check clipping of a scaled image at the bottom margin
s.reset()
s.set_margins(1, 3)
s.index()
put_image(s, cw, 4 * ch, num_cols=1, num_lines=2, no_id=True) # 1x2 cells at (0, 1)
while s.cursor.y != 0:
s.reverse_index()
s.reverse_index() # scroll down, the bottom row of the image is clipped
l0 = layers(s)
self.ae(len(l0), 1)
self.ae(l0[0]['src_rect'], {'left': 0.0, 'top': 0.0, 'right': 1.0, 'bottom': 0.5})
rect_eq(l0[0]['dest_rect'], -1, -1 + 2 * dy, -1 + dx, -1 + 3 * dy)
s.reverse_index()
self.ae(s.grman.image_count, 0)
# Scaling specified via c= only, with the height derived from the aspect ratio
s.reset()
s.set_margins(1, 3)
put_image(s, 2 * cw, 4 * ch, num_cols=1, no_id=True) # 20x80 px image scaled into 1 col => 10x40 px => 1x2 cells
rect_eq(layers(s)[0]['dest_rect'], -1, -1, -1 + dx, -1 + 2 * dy)
while s.cursor.y != 2:
s.index()
s.index() # scroll up, the top row of the image is clipped
l0 = layers(s)
self.ae(l0[0]['src_rect'], {'left': 0.0, 'top': 0.5, 'right': 1.0, 'bottom': 1.0})
rect_eq(l0[0]['dest_rect'], -1, -1, -1 + dx, -1 + dy)
s.index()
self.ae(s.grman.image_count, 0)
s.reset()
self.assertEqual(s.grman.disk_cache.total_size, 0)
def test_gr_reset(self):
cw, ch = 10, 20
s, dx, dy, put_image, put_ref, layers, rect_eq = put_helpers(self, cw, ch)
put_image(s, cw, ch) # a one cell image at (0, 0)
self.ae(len(layers(s)), 1)
s.reset()
self.ae(s.grman.image_count, 0)
put_image(s, cw, ch) # a one cell image at (0, 0)
self.ae(s.grman.image_count, 1)
for i in range(s.lines):
s.index()
s.reset()
self.ae(s.grman.image_count, 1)
def test_gr_delete(self):
cw, ch = 10, 20
s, dx, dy, put_image, put_ref, layers, rect_eq = put_helpers(self, cw, ch)
def delete(ac=None, **kw):
cmd = 'a=d'
if ac:
cmd += f',d={ac}'
if kw:
cmd += ',' + ','.join(f'{k}={v}' for k, v in kw.items())
send_command(s, cmd)
iid = put_image(s, cw, ch, a='t')[0]
self.ae(s.grman.image_count, 1)
delete('I', i=iid)
self.ae(s.grman.image_count, 0)
iid1 = put_image(s, cw, ch, a='t')[0]
iid2 = put_image(s, cw, ch, a='t')[0]
self.ae(s.grman.image_count, 2)
delete('R', x=iid1, y=iid2)
self.ae(s.grman.image_count, 0)
put_image(s, cw, ch)
delete()
self.ae(s.grman.image_count, 1)
self.ae(len(layers(s)), 0)
delete('A')
self.ae(s.grman.image_count, 1)
s.reset()
self.ae(s.grman.image_count, 0)
put_image(s, cw, ch)
self.ae(s.grman.image_count, 1)
delete('A')
self.ae(s.grman.image_count, 0)
self.assertEqual(s.grman.disk_cache.total_size, 0)
iid = put_image(s, cw, ch)[0]
delete('I', i=iid, p=7)
self.ae(s.grman.image_count, 1)
delete('I', i=iid)
self.ae(s.grman.image_count, 0)
self.assertEqual(s.grman.disk_cache.total_size, 0)
iid = put_image(s, cw, ch, placement_id=9)[0]
delete('I', i=iid, p=9)
self.ae(s.grman.image_count, 0)
self.assertEqual(s.grman.disk_cache.total_size, 0)
s.reset()
put_image(s, cw, ch)
put_image(s, cw, ch)
delete('C')
self.ae(s.grman.image_count, 2)
s.cursor_position(1, 1)
delete('C')
self.ae(s.grman.image_count, 1)
delete('P', x=2, y=1)
self.ae(s.grman.image_count, 0)
self.assertEqual(s.grman.disk_cache.total_size, 0)
put_image(s, cw, ch, z=9)
delete('Z', z=9)
self.ae(s.grman.image_count, 0)
put_image(s, cw, ch, id=1)
put_image(s, cw, ch, id=2)
put_image(s, cw, ch, id=3)
delete('R', y=2)
self.ae(s.grman.image_count, 1)
delete('R', x=3, y=3)
self.ae(s.grman.image_count, 0)
self.assertEqual(s.grman.disk_cache.total_size, 0)
# test put + delete + put
iid = 999999
self.ae(put_image(s, cw, ch, id=iid), (iid, 'OK'))
self.ae(put_ref(s, id=iid), (iid, ('OK', f'i={iid}')))
delete('i', i=iid)
self.ae(s.grman.image_count, 1)
self.ae(put_ref(s, id=iid), (iid, ('OK', f'i={iid}')))
delete('I', i=iid)
self.ae(put_ref(s, id=iid), (iid, ('ENOENT', f'i={iid}')))
self.ae(s.grman.image_count, 0)
self.assertEqual(s.grman.disk_cache.total_size, 0)
# test delete but not free
s.reset()
iid = 9999999
self.ae(put_image(s, cw, ch, id=iid), (iid, 'OK'))
self.ae(put_ref(s, id=iid), (iid, ('OK', f'i={iid}')))
self.ae(put_image(s, cw, ch, id=iid + 1), (iid + 1, 'OK'))
self.ae(put_ref(s, id=iid + 1), (iid + 1, ('OK', f'i={iid + 1}')))
delete('i', i=iid)
self.ae(s.grman.image_count, 2)
delete('I', i=iid + 1)
self.ae(s.grman.image_count, 1)
def test_animation_frame_delete_dependents(self):
s = self.create_screen()
g = s.grman
li = make_send_command(s)
def t(**kw):
self.assertEqual(li(**kw).code, 'OK')
def load_frames():
t(a='t')
for i in '234':
t(payload=i * 36)
# Preserve the successor display
load_frames()
self.assertIsNone(li(a='a', i=1, c=3))
img = g.image_for_client_id(1)
self.assertEqual(img['displayed_frame_id'], img['extra_frames'][1]['id'])
successor = img['extra_frames'][2]['id']
self.assertIsNone(li(a='d', d='f', i=1, r=3))
img = g.image_for_client_id(1)
self.assertEqual(img['current_frame_index'], 2)
self.assertEqual(img['displayed_frame_id'], successor)
self.assertEqual([f['data'] for f in img['extra_frames']], [b'2' * 36, b'4' * 36])
s.reset()
# Preserve the root successor display
load_frames()
successor = g.image_for_client_id(1)['extra_frames'][0]['id']
self.assertIsNone(li(a='a', i=1, c=1))
self.assertIsNone(li(a='d', d='f', i=1, r=1))
img = g.image_for_client_id(1)
self.assertEqual(img['current_frame_index'], 0)
self.assertEqual(img['displayed_frame_id'], successor)
s.reset()
# Preserve the last frame display
load_frames()
self.assertIsNone(li(a='a', i=1, c=4))
self.assertIsNone(li(a='d', d='f', i=1, r=4))
img = g.image_for_client_id(1)
self.assertEqual(img['current_frame_index'], 2)
self.assertEqual(img['displayed_frame_id'], img['extra_frames'][1]['id'])
s.reset()
# Preserve dependent pixels
t(a='t')
t(payload='2' * 36)
t(payload='4' * 12, c=2, s=2, v=2)
expected = b'444444222222444444222222222222222222'
self.assertEqual(g.image_for_client_id(1)['extra_frames'][1]['data'], expected)
self.assertIsNone(li(a='d', d='f', i=1, r=2))
img = g.image_for_client_id(1)
self.assertEqual(len(img['extra_frames']), 1)
self.assertEqual(img['extra_frames'][0]['data'], expected)
s.reset()
# Preserve root dependents
t(a='t')
t(payload='4' * 12, c=1, s=2, v=2)
expected = g.image_for_client_id(1)['extra_frames'][0]['data']
self.assertIsNone(li(a='d', d='f', i=1, r=1))
img = g.image_for_client_id(1)
self.assertEqual(img['data'], expected)
self.assertFalse(img['extra_frames'])
s.reset()
self.assertEqual(g.disk_cache.total_size, 0)
# Respect the cache quota
g.storage_limit = 36 * 2
t(a='t')
for i in range(10):
t(payload='4' * 12, c=1, s=2, v=2)
before = g.image_for_client_id(1)
size = g.disk_cache.total_size
self.assertIsNone(li(a='d', d='f', i=1, r=1))
after = g.image_for_client_id(1)
self.assertEqual(g.disk_cache.total_size, size)
self.assertEqual(after['data'], before['data'])
self.assertEqual([f['data'] for f in after['extra_frames']], [f['data'] for f in before['extra_frames']])
s.reset()
self.assertEqual(g.disk_cache.total_size, 0)
def test_animation_frame_loading(self):
s = self.create_screen()
g = s.grman
li = make_send_command(s)
def t(code='OK', image_id=1, frame_number=2, **kw):
res = li(**kw)
if code is not None:
self.assertEqual(code, res.code, f'{code} != {res.code}: {res.msg}')
if image_id is not None:
self.assertEqual(image_id, res.image_id)
if frame_number is not None:
self.assertEqual(frame_number, res.frame_number)
# test error on send frame for non-existent image
self.assertEqual(li().code, 'ENOENT')
# create image
self.assertEqual(li(a='t').code, 'OK')
self.assertEqual(g.disk_cache.total_size, 36)
# simple new frame (width=4, height=3)
self.assertIsNone(li(payload='2' * 12, z=77, m=1))
self.assertIsNone(li(payload='2' * 12, z=77, m=1))
t(payload='2' * 12, z=77)
img = g.image_for_client_id(1)
self.assertEqual(img['extra_frames'], ({'gap': 77, 'id': 2, 'data': b'2' * 36},))
# test editing a frame
t(payload='3' * 36, r=2)
img = g.image_for_client_id(1)
self.assertEqual(img['extra_frames'], ({'gap': 77, 'id': 2, 'data': b'3' * 36},))
# test editing part of a frame
t(payload='4' * 12, r=2, s=2, v=2)
img = g.image_for_client_id(1)
def expand(*rows):
ans = []
for r in rows:
ans.append(''.join(x * 3 for x in str(r)))
return ''.join(ans).encode('ascii')
self.assertEqual(img['extra_frames'], ({'gap': 77, 'id': 2, 'data': expand(4433, 4433, 3333)},))
t(payload='5' * 12, r=2, s=2, v=2, x=1, y=1)
img = g.image_for_client_id(1)
self.assertEqual(img['extra_frames'], ({'gap': 77, 'id': 2, 'data': expand(4433, 4553, 3553)},))
t(payload='3' * 36, r=2)
img = g.image_for_client_id(1)
self.assertEqual(img['extra_frames'], ({'gap': 77, 'id': 2, 'data': b'3' * 36},))
# the composition mode for frame edits must be controlled by the X key,
# with fully transparent pixels being a no-op unless X=1 (issue #10379)
transparent = b'\x00' * 48
t(payload=transparent, r=2, f=32)
img = g.image_for_client_id(1)
self.assertEqual(img['extra_frames'], ({'gap': 77, 'id': 2, 'data': b'3' * 36},))
t(payload=transparent, r=2, f=32, C=1)
img = g.image_for_client_id(1)
self.assertEqual(img['extra_frames'], ({'gap': 77, 'id': 2, 'data': b'3' * 36},))
t(payload=transparent, r=2, f=32, X=1)
img = g.image_for_client_id(1)
self.assertEqual(img['extra_frames'], ({'gap': 77, 'id': 2, 'data': b'\x00' * 36},))
t(payload='3' * 36, r=2)
img = g.image_for_client_id(1)
self.assertEqual(img['extra_frames'], ({'gap': 77, 'id': 2, 'data': b'3' * 36},))
# test loading from previous frame
t(payload='4' * 12, c=2, s=2, v=2, z=101, frame_number=3)
img = g.image_for_client_id(1)
self.assertEqual(
img['extra_frames'],
(
{'gap': 77, 'id': 2, 'data': b'3' * 36},
{'gap': 101, 'id': 3, 'data': b'444444333333444444333333333333333333'},
),
)
# test changing gaps
img = g.image_for_client_id(1)
self.assertEqual(img['root_frame_gap'], 0)
self.assertIsNone(li(a='a', i=1, r=1, z=13))
img = g.image_for_client_id(1)
self.assertEqual(img['root_frame_gap'], 13)
self.assertIsNone(li(a='a', i=1, r=2, z=43))
img = g.image_for_client_id(1)
self.assertEqual(img['extra_frames'][0]['gap'], 43)
# test changing current frame
img = g.image_for_client_id(1)
self.assertEqual(img['current_frame_index'], 0)
self.assertIsNone(li(a='a', i=1, c=2))
img = g.image_for_client_id(1)
self.assertEqual(img['current_frame_index'], 1)
# test delete of frames
t(payload='5' * 36, frame_number=4)
img = g.image_for_client_id(1)
self.assertEqual(
img['extra_frames'],
(
{'gap': 43, 'id': 2, 'data': b'3' * 36},
{'gap': 101, 'id': 3, 'data': b'444444333333444444333333333333333333'},
{'gap': 40, 'id': 4, 'data': b'5' * 36},
),
)
self.assertEqual(img['current_frame_index'], 1)
self.assertIsNone(li(a='d', d='f', i=1, r=1))
img = g.image_for_client_id(1)
self.assertEqual(img['current_frame_index'], 0)
self.assertEqual(img['data'], b'3' * 36)
self.assertEqual(
img['extra_frames'],
(
{'gap': 101, 'id': 3, 'data': b'444444333333444444333333333333333333'},
{'gap': 40, 'id': 4, 'data': b'5' * 36},
),
)
self.assertIsNone(li(a='a', i=1, c=3))
img = g.image_for_client_id(1)
self.assertEqual(img['current_frame_index'], 2)
self.assertIsNone(li(a='d', d='f', i=1, r=2))
img = g.image_for_client_id(1)
self.assertEqual(img['current_frame_index'], 1)
self.assertEqual(img['data'], b'3' * 36)
self.assertEqual(img['extra_frames'], ({'gap': 40, 'id': 4, 'data': b'5' * 36},))
self.assertIsNone(li(a='d', d='f', i=1))
img = g.image_for_client_id(1)
self.assertEqual(img['current_frame_index'], 0)
self.assertEqual(img['data'], b'5' * 36)
self.assertFalse(img['extra_frames'])
self.assertIsNone(li(a='d', d='f', i=1))
img = g.image_for_client_id(1)
self.assertEqual(img['data'], b'5' * 36)
self.ae(g.image_count, 1)
self.assertIsNone(li(a='d', d='F', i=1))
self.ae(g.image_count, 0)
self.assertEqual(g.disk_cache.total_size, 0)
# test frame composition
self.assertEqual(li(a='t').code, 'OK')
self.assertEqual(g.disk_cache.total_size, 36)
t(payload='2' * 36)
t(payload='3' * 36, frame_number=3)
img = g.image_for_client_id(1)
self.assertEqual(
img['extra_frames'],
(
{'gap': 40, 'id': 2, 'data': b'2' * 36},
{'gap': 40, 'id': 3, 'data': b'3' * 36},
),
)
self.assertEqual(li(a='c', i=11).code, 'ENOENT')
self.assertEqual(li(a='c', i=1, r=1, c=2).code, 'OK')
img = g.image_for_client_id(1)
self.assertEqual(
img['extra_frames'],
(
{'gap': 40, 'id': 2, 'data': b'abcdefghijkl' * 3},
{'gap': 40, 'id': 3, 'data': b'3' * 36},
),
)
self.assertEqual(li(a='c', i=1, r=2, c=3, w=1, h=2, x=1, y=1).code, 'OK')
img = g.image_for_client_id(1)
self.assertEqual(
img['extra_frames'],
(
{'gap': 40, 'id': 2, 'data': b'abcdefghijkl' * 3},
{'gap': 40, 'id': 3, 'data': b'3' * 12 + (b'333abc' + b'3' * 6) * 2},
),
)
# Composing into a frame materializes it as a full frame. Its pixel
# format metadata must be updated to match the coalesced frame data.
rgba_screen = self.create_screen()
rgba_grman = rgba_screen.grman
rgba_li = make_send_command(rgba_screen)
self.assertEqual(rgba_li(payload=b'\0' * 48, a='t', f=32).code, 'OK')
self.assertEqual(rgba_li(payload=b'R' * 12, c=1, s=2, v=2, f=24).code, 'OK')
frame_before_composition = rgba_grman.image_for_client_id(1)['extra_frames'][0]['data']
self.assertEqual(len(frame_before_composition), 48)
self.assertEqual(rgba_li(payload=b'', a='c', f=0, s=0, v=0, r=1, c=2, w=1, h=1, x=3, y=2).code, 'OK')
frame_after_composition = rgba_grman.image_for_client_id(1)['extra_frames'][0]['data']
self.assertEqual(frame_after_composition, frame_before_composition)
# Test that compose commands with offset values that would overflow a 32-bit
# unsigned integer are correctly rejected with EINVAL instead of crashing.
# In the old code, UINT32_MAX + img->width wrapped around as uint32_t to a
# small value that bypassed the bounds check, causing a crash (fix: e9661f0).
for offset_param in ('x', 'y', 'X', 'Y'):
self.assertEqual(
li(payload=b'', a='c', i=1, r=1, c=2, s=0, v=0, f=0, **{offset_param: 0xFFFFFFFF}).code,
'EINVAL',
f'Expected EINVAL for overflow in compose offset parameter {offset_param!r}',
)
def test_graphics_compose_canvas_bounds(self):
from kitty.fast_data_types import create_canvas
# Sanity: a well formed overlay is copied correctly onto the canvas.
overlay = bytes(range(1, 13)) # 2x2 RGB overlay
canvas = create_canvas(overlay, 2, 0, 0, 2, 2, 3)
self.assertEqual(canvas, overlay)
# out of bounds overlay
big_overlay = bytes((i % 251) + 1 for i in range(4 * 4 * 3))
canvas = create_canvas(big_overlay, 4, 0, 0, 2, 2, 3)
self.assertEqual(len(canvas), 2 * 2 * 3)
expected = big_overlay[0:6] + big_overlay[12:18] # first two rows, first two pixels
self.assertEqual(canvas, expected)
# out of bounds offset overlay
canvas = create_canvas(overlay, 2, 1000, 1000, 2, 2, 3)
self.assertEqual(canvas, b'\x00' * (2 * 2 * 3))
# A huge offset (near UINT32_MAX) must not overflow the address math.
canvas = create_canvas(overlay, 2, 0xFFFFFFFF, 0xFFFFFFFF, 2, 2, 3)
self.assertEqual(canvas, b'\x00' * (2 * 2 * 3))
# A partially overlapping overlay: place a 2x2 overlay at x=1,y=1 of a
# 2x2 canvas so only its top-left pixel lands inside the canvas.
canvas = create_canvas(overlay, 2, 1, 1, 2, 2, 3)
expected = bytearray(2 * 2 * 3)
expected[9:12] = overlay[0:3] # bottom-right pixel of canvas
self.assertEqual(canvas, bytes(expected))
# Degenerate/invalid parameters must raise instead of crashing.
self.assertRaises(ValueError, create_canvas, overlay, 0, 0, 0, 2, 2, 3)
self.assertRaises(ValueError, create_canvas, overlay, 2, 0, 0, 2, 2, 0)
self.assertRaises(ValueError, create_canvas, overlay, 2, 0, 0, 2, 2, 5)
def test_animation_frame_long_reference_chain(self):
# A new frame based on another frame with a long/large reference chain is
# stored as a fully coalesced key frame rather than as a delta
s = self.create_screen()
g = s.grman
li = make_send_command(s)
self.assertEqual(li(a='t').code, 'OK')
self.assertEqual(g.disk_cache.total_size, 36)
# frame 2 is a delta on top of the root frame
self.assertEqual(li(payload='2' * 36, c=1).frame_number, 2)
self.assertEqual(g.disk_cache.total_size, 72)
# frame 3 is based on frame 2, whose reference chain is now large enough
# that frame 3 must be coalesced into a full key frame
self.assertEqual(li(payload='4' * 12, c=2, s=2, v=2).frame_number, 3)
img = g.image_for_client_id(1)
self.assertEqual(
img['extra_frames'],
(
{'gap': 40, 'id': 2, 'data': b'2' * 36},
{'gap': 40, 'id': 3, 'data': b'444444222222' * 2 + b'2' * 12},
),
)
# a full 36 byte key frame, not a 12 byte delta
self.assertEqual(g.disk_cache.total_size, 108)
def test_animation_frame_chunked_loading(self):
# continuation chunks of a chunked a=f transmission carry only the m key,
# they must be routed to the frame load handler, not the add handler
s = self.create_screen()
g = s.grman
li = make_send_command(s)
self.assertEqual(li(a='t').code, 'OK')
def chunked(payload, last_payload, cont='', **kw):
self.assertIsNone(li(payload=payload, m=1, **kw))
self.assertFalse(send_command(s, cont + 'm=1', payload))
return parse_full_response(send_command(s, cont + 'm=0', last_payload))
# create a new frame with continuation chunks
res = chunked('2' * 12, '2' * 12, z=77)
self.assertEqual((res.code, res.image_id, res.frame_number), ('OK', 1, 2))
img = g.image_for_client_id(1)
self.assertEqual(img['data'], b'abcdefghijkl' * 3) # root frame must be untouched
self.assertEqual(img['extra_frames'], ({'gap': 77, 'id': 2, 'data': b'2' * 36},))
# edit an existing frame with continuation chunks, r= is only present
# in the start command
res = chunked('3' * 12, '3' * 12, r=2)
self.assertEqual((res.code, res.image_id, res.frame_number), ('OK', 1, 2))
img = g.image_for_client_id(1)
self.assertEqual(img['extra_frames'], ({'gap': 77, 'id': 2, 'data': b'3' * 36},))
# the documented form of continuation chunks for frame data, a=f
# without an image id, must also get a response identifying the image
res = chunked('4' * 12, '4' * 12, cont='a=f,')
self.assertEqual((res.code, res.image_id, res.frame_number), ('OK', 1, 3))
res = chunked('5' * 12, '5' * 12, cont='a=f,', r=3)
self.assertEqual((res.code, res.image_id, res.frame_number), ('OK', 1, 3))
img = g.image_for_client_id(1)
self.assertEqual(img['data'], b'abcdefghijkl' * 3)
self.assertEqual(img['extra_frames'], ({'gap': 77, 'id': 2, 'data': b'3' * 36}, {'gap': 40, 'id': 3, 'data': b'5' * 36}))
# repeating the image id in continuation chunks works as well
res = chunked('6' * 12, '6' * 12, cont='a=f,i=1,', r=3)
self.assertEqual((res.code, res.image_id, res.frame_number), ('OK', 1, 3))
# with q=1 success is not reported but failure is
self.assertIsNone(chunked('7' * 12, '7' * 12, cont='a=f,', q=1))
self.assertIsNone(li(payload='8' * 12, m=1, q=1))
res = parse_full_response(send_command(s, 'a=f,m=0'))
self.assertEqual((res.code, res.image_id), ('ENODATA', 1))
# with q=2 failure is not reported either
self.assertIsNone(li(payload='8' * 12, m=1, q=2))
self.assertIsNone(parse_full_response(send_command(s, 'a=f,m=0')))
def test_graphics_quota_enforcement(self):
s = self.create_screen()
g = s.grman
g.storage_limit = 36 * 2
li = make_send_command(s)
# test quota for simple images
self.assertEqual(li(a='T').code, 'OK')
self.assertEqual(li(a='T', i=2).code, 'OK')
self.assertEqual(g.disk_cache.total_size, g.storage_limit)
self.assertEqual(g.image_count, 2)
self.assertEqual(li(a='T', i=3).code, 'OK')
self.assertEqual(g.disk_cache.total_size, g.storage_limit)
self.assertEqual(g.image_count, 2)
# test quota for frames
for i in range(8):
self.assertEqual(li(payload=f'{i}' * 36, i=2).code, 'OK')
self.assertEqual(li(payload='x' * 36, i=2).code, 'ENOSPC')
# test editing should not trigger quota
self.assertEqual(li(payload='4' * 12, r=2, s=2, v=2, i=2).code, 'OK')
s.reset()
self.ae(g.image_count, 0)
self.assertEqual(g.disk_cache.total_size, 0)
def test_transient_image_preferential_eviction(self):
# Transient images should be evicted before non-transient ones when
# the storage quota is exceeded, regardless of insertion order.
s = self.create_screen()
g = s.grman
g.storage_limit = 36 * 2
li = make_send_command(s)
# Load a non-transient image first (older atime) and a transient image second.
self.assertEqual(li(a='T', i=1).code, 'OK')
self.assertEqual(li(a='T', i=2, N=1).code, 'OK')
self.assertEqual(g.image_count, 2)
# Adding a third image triggers the quota; the transient image (i=2) must be
# evicted first even though the non-transient image (i=1) is older.
self.assertEqual(li(a='T', i=3).code, 'OK')
self.assertEqual(g.image_count, 2)
self.assertIsNone(g.image_for_client_id(2), 'transient image should have been evicted')
self.assertIsNotNone(g.image_for_client_id(1), 'non-transient image should survive')
self.assertIsNotNone(g.image_for_client_id(3), 'newly added image should survive')
@unittest.skipIf(Image is None, 'PIL not available, skipping PNG tests')
def test_cached_rgba_conversion(self):
from kitty.render_cache import ImageRenderCacheForTesting
w, h = 5, 3
rgba_data = byte_block(w * h * 4)
img = Image.frombytes('RGBA', (w, h), rgba_data)
buf = BytesIO()
img.save(buf, 'PNG')
png_data = buf.getvalue()
with tempfile.TemporaryDirectory() as cache_path:
irc = ImageRenderCacheForTesting(cache_path)
srcs, outputs = [], []
for i in range(2 * irc.max_entries):
with open(os.path.join(cache_path, f'{i}.png'), 'wb') as f:
f.write(png_data)
srcs.append(f.name)
outputs.append(irc.render(f.name))
entries = list(irc.entries())
self.assertLessEqual(len(entries), irc.max_entries)
self.ae(irc.num_of_renders, len(outputs))
remaining_outputs = outputs[-irc.max_entries :]
for x in remaining_outputs:
self.assertTrue(os.path.exists(x))
for x in outputs[: -irc.max_entries]:
self.assertFalse(os.path.exists(x))
self.assertLess(os.path.getmtime(remaining_outputs[0]), os.path.getmtime(remaining_outputs[1]))
remaining_srcs = srcs[-irc.max_entries :]
self.ae(irc.render(remaining_srcs[0]), remaining_outputs[0])
self.ae(irc.num_of_renders, len(outputs))
self.assertGreater(os.path.getmtime(remaining_outputs[0]), os.path.getmtime(remaining_outputs[1]))
width, height, fd = irc(remaining_srcs[-1])
with open(fd, 'rb') as f:
self.ae((width, height), (w, h))
f.seek(8)
self.ae(rgba_data, f.read())
# non-regular files must be rejected rather than streamed into the converter
num = irc.num_of_renders
fifo = os.path.join(cache_path, 'fifo')
os.mkfifo(fifo)
for bad in (os.devnull, fifo, cache_path):
with self.assertRaises(ValueError, msg=bad):
irc.render(bad)
with self.assertRaises(ValueError, msg=bad):
irc.render_image(bad, os.path.join(cache_path, 'out'))
self.assertFalse(os.path.exists(os.path.join(cache_path, 'out')))
self.ae(irc.num_of_renders, num)