1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
|
const std = @import("std");
const assert = @import("../../quirks.zig").inlineAssert;
const Allocator = std.mem.Allocator;
const ArenaAllocator = std.heap.ArenaAllocator;
const terminal = @import("../main.zig");
const point = @import("../point.zig");
const size = @import("../size.zig");
const animation = @import("graphics_animation.zig");
const pixel = @import("graphics_pixel.zig");
const command = @import("graphics_command.zig");
const PageList = @import("../PageList.zig");
const Screen = @import("../Screen.zig");
const LoadingImage = @import("graphics_image.zig").LoadingImage;
const Image = @import("graphics_image.zig").Image;
const Rect = @import("graphics_image.zig").Rect;
const Command = command.Command;
const log = std.log.scoped(.kitty_gfx);
/// Process-global counter backing all generation stamps (see
/// ImageStorage.generation and Image.generation). This is global rather
/// than per-storage so that stamps are unique across every storage in
/// the process: two mutation events never produce the same value, even
/// across separate screens (main vs. alt), storage resets, or separate
/// terminals. This lets consumers use a generation value alone as a
/// cache key without any ambiguity.
///
/// Thread-safe because separate terminals may mutate their storages
/// from different threads. On single-threaded targets this lowers to
/// plain operations.
var generation_counter: GenerationCounter = .{};
/// Returns the next generation stamp. Stamps are unique and strictly
/// monotonically increasing process-wide, starting at 1 (0 is reserved
/// to mean "never stamped").
pub fn nextGeneration(io: std.Io) u64 {
return generation_counter.next(io);
}
/// Backing implementation for the generation counter. We use a
/// lock-free atomic counter where we can, but not all targets support
/// 64-bit atomic operations (e.g. 32-bit ARM Android), so we fall back
/// to a mutex-protected counter on those. This is a cold path (only
/// invoked on content mutations) so the mutex cost is irrelevant.
///
/// The pointer-width check is a conservative proxy for 64-bit atomic
/// support: every 64-bit target supports 64-bit atomics, while 32-bit
/// targets may not (per the compiler's atomic operand validation).
const GenerationCounter = if (@bitSizeOf(usize) >= 64) struct {
value: std.atomic.Value(u64) = .init(0),
fn next(self: *@This(), io: std.Io) u64 {
_ = io;
return self.value.fetchAdd(1, .monotonic) + 1;
}
} else struct {
mutex: std.Io.Mutex = .init,
value: u64 = 0,
fn next(self: *@This(), io: std.Io) u64 {
self.mutex.lockUncancelable(io);
defer self.mutex.unlock(io);
self.value += 1;
return self.value;
}
};
/// An image storage is associated with a terminal screen (i.e. main
/// screen, alt screen) and contains all the transmitted images and
/// placements.
pub const ImageStorage = struct {
const ImageMap = std.AutoHashMapUnmanaged(u32, Image);
const PlacementMap = std.AutoHashMapUnmanaged(PlacementKey, Placement);
/// Dirty is set to true if placements or images change. This is
/// purely informational for the renderer and doesn't affect the
/// correctness of the program. The renderer must set this to false
/// if it cares about this value.
///
/// Note that dirty is also set by scrolling and resizing (outside
/// of this struct) because those move placement pins, even though
/// the set of images/placements itself is unchanged. See generation
/// for a signal that only tracks content mutations.
///
/// Invariant: dirty is always set when generation changes
/// (markMutated sets both); dirty set without a generation change
/// means a geometry-only event.
dirty: bool = false,
/// Generation stamp of the last content mutation to this storage:
/// any image transmit/replace, placement add, or delete of either.
/// Zero means the storage has never been mutated (and is therefore
/// empty).
///
/// Unlike dirty, this is NOT updated by scrolling/resizing, so an
/// unchanged generation means the placement set and all image data
/// are identical; only placement geometry (pins) may have moved.
/// Values come from a process-global monotonic counter, so a value
/// observed from any storage never recurs for different content,
/// even across screen switches or storage resets.
///
/// This field must only be written via markMutated.
generation: u64 = 0,
/// This is the next automatically assigned image ID for images
/// transmitted without an ID or number. We start mid-way through
/// the u32 range to stay clear of the low IDs client programs
/// typically pick. See nextImageId.
next_image_id: u32 = 2147483647,
/// This is the next automatically assigned placement ID. This is never
/// user-facing so we can start at 0. This is 32-bits because we use
/// the same space for external placement IDs. We can start at zero
/// because any number is valid.
next_internal_placement_id: u32 = 0,
/// The set of images that are currently known.
images: ImageMap = .{},
/// The set of placements for loaded images.
placements: PlacementMap = .{},
/// Non-null if there is an in-progress loading image.
loading: ?*LoadingImage = null,
/// The limits of what medium types are allowed for image loading.
image_limits: LoadingImage.Limits = .direct,
/// The total bytes of image data that have been loaded and the limit.
/// If the limit is reached, the oldest images will be evicted to make
/// space. Unused images take priority.
total_bytes: usize = 0,
total_limit: usize = 320 * 1000 * 1000, // 320MB
/// Identifies one exact pending image transmission. The generation is
/// assigned by this storage when the pending image is inserted, so a
/// later replacement of the same ID cannot consume stale payload bytes.
pub const PendingImage = struct {
id: u32,
generation: u64,
/// Attach owned decoded bytes if this exact pending transmission is
/// still resident. On true, storage owns `data`; on false, the caller
/// retains ownership. Completion preserves the image generation (and
/// therefore eviction age) while marking storage content as mutated.
pub fn complete(
self: PendingImage,
storage: *ImageStorage,
io: std.Io,
data: []const u8,
) bool {
const img = storage.images.getPtr(self.id) orelse return false;
if (img.generation != self.generation) return false;
const expected_len = switch (img.data) {
.complete, .encoded_png, .native_file => return false,
.pending => |len| len,
};
if (data.len != expected_len) return false;
img.data = .{ .complete = data };
storage.markMutated(io);
return true;
}
};
pub fn deinit(
self: *ImageStorage,
alloc: Allocator,
s: *terminal.Screen,
) void {
if (self.loading) |loading| loading.destroy(alloc);
self.clearPlacements(s);
self.placements.deinit(alloc);
var it = self.images.iterator();
while (it.next()) |kv| kv.value_ptr.deinit(alloc);
self.images.deinit(alloc);
}
/// Kitty image protocol is enabled if we have a non-zero limit.
pub fn enabled(self: *const ImageStorage) bool {
return self.total_limit != 0;
}
/// Record a content mutation: marks the storage dirty and assigns a
/// fresh generation stamp. Must be called by anything that changes
/// the set of images or placements (or image contents), including
/// the animation command handlers in graphics_exec.zig.
///
/// Do NOT call this for geometry-only events (scrolling, resizing,
/// screen switches); those must set only the dirty flag directly.
/// Bumping the generation for geometry changes would break the
/// contract that an unchanged generation means unchanged contents.
pub fn markMutated(self: *ImageStorage, io: std.Io) void {
self.dirty = true;
self.generation = nextGeneration(io);
}
/// Sets the limit in bytes for the total amount of image data that
/// can be loaded. If this limit is lower, this will do an eviction
/// if necessary. If the value is zero, then Kitty image protocol will
/// be disabled.
pub fn setLimit(
self: *ImageStorage,
io: std.Io,
alloc: Allocator,
s: *terminal.Screen,
limit: usize,
) void {
// Special case disabling by quickly deleting all
if (limit == 0) {
const image_limits = self.image_limits;
self.deinit(alloc, s);
self.* = .{ .image_limits = image_limits };
self.markMutated(io);
}
// If we re lowering our limit, check if we need to evict.
if (limit < self.total_bytes) {
const req_bytes = self.total_bytes - limit;
log.info("evicting images to lower limit, evicting={}", .{req_bytes});
if (!self.evictImage(io, alloc, s, req_bytes)) {
log.warn("failed to evict enough images for required bytes", .{});
}
}
self.total_limit = limit;
}
/// Returns the next ID to automatically assign to an image that
/// was transmitted without an explicit ID (i=). The result is
/// never zero (zero means "no ID") and never an ID currently in
/// use, so an automatic ID never replaces an existing image.
pub fn nextImageId(
self: *ImageStorage,
mode: enum { explicit, implicit },
) u32 {
// Starting ID depends on mode.
var id: u32 = switch (mode) {
// Yeah, starting from 1 is wasteful. This matches what Kitty
// does. In the future we can probably cache a low-water-mark
// based on delete behavior.
.explicit => 1,
.implicit => self.next_image_id,
};
// Go through all possible images. We have +2 because we can
// exceed the total by 1 (new ID).
const count: usize = self.images.count();
for (0..count + 2) |_| {
if (id != 0 and !self.images.contains(id)) break;
id +%= 1;
}
// Zero is never allowed.
if (id == 0) id = 1;
// Keep track of our next image id for implicit to speed that up.
if (mode == .implicit) self.next_image_id = id +% 1;
return id;
}
/// Add an image to the storage. This will automatically free any existing
/// image with the same ID. Prefer addPendingImage for pending data so the
/// caller receives a completion token.
pub fn addImage(
self: *ImageStorage,
io: std.Io,
alloc: Allocator,
s: *terminal.Screen,
img: Image,
) Allocator.Error!void {
const new_len = img.data.len();
// If the image itself is over the limit, then error immediately
if (new_len > self.total_limit) return error.OutOfMemory;
// Credit an existing image's reservation before calculating the
// replacement size. In particular, a completed live transmission
// replacing pending snapshot metadata must be able to reuse the
// reservation without evicting its own ID.
const old_len = if (self.images.get(img.id)) |old|
old.storageSize()
else
0;
assert(old_len <= self.total_bytes);
// Reserve map capacity before evicting anything so allocation failure
// cannot leave storage partially evicted. Existing keys need no room.
if (!self.images.contains(img.id)) {
try self.images.ensureUnusedCapacity(alloc, 1);
}
// If this would put us over the limit, then evict.
const total_bytes = self.total_bytes - old_len + new_len;
if (total_bytes > self.total_limit) {
const req_bytes = total_bytes - self.total_limit;
log.info("evicting images to make space for {} bytes", .{req_bytes});
if (!self.evictImageExcept(io, alloc, s, req_bytes, img.id)) {
log.warn("failed to evict enough images for required bytes", .{});
return error.OutOfMemory;
}
}
const gop = self.images.getOrPutAssumeCapacity(img.id);
log.debug("addImage image={}", .{img.withoutData()});
if (gop.found_existing) {
// Retransmitting a specific image ID replaces the old image and all
// of its placements, as required by the Kitty graphics protocol.
self.removePlacementsByImageId(s, img.id);
// Relative placements parented to the removed placements go too.
_ = self.removeOrphans(s, null);
// Replacing an image drops its animation frames with it,
// implementing the protocol rule that retransmitting the
// base image resets the animation.
self.total_bytes -= gop.value_ptr.storageSize();
gop.value_ptr.deinit(alloc);
}
gop.value_ptr.* = img;
gop.value_ptr.metadata.placement_count = 0;
self.total_bytes += new_len;
// Stamp the stored image with a fresh generation. This gives
// every add/replace a unique stamp even when the same image ID
// is retransmitted with identical dimensions, so consumers
// (e.g. renderer texture caches) can detect content changes.
self.markMutated(io);
gop.value_ptr.generation = self.generation;
}
/// Add an image whose decoded payload bytes have not arrived yet.
/// Returns the token required to complete this exact transmission.
pub fn addPendingImage(
self: *ImageStorage,
io: std.Io,
alloc: Allocator,
s: *terminal.Screen,
img: Image,
) Allocator.Error!PendingImage {
assert(img.data == .pending);
try self.addImage(io, alloc, s, img);
const stored = self.images.get(img.id).?;
return .{ .id = stored.id, .generation = stored.generation };
}
/// Add a placement for a given image. The caller must verify in advance
/// the image exists to prevent memory corruption. On success, storage
/// owns `p`; on error, the caller retains ownership. The screen must own
/// any tracked pin in `p` and in the placement it replaces.
pub fn addPlacement(
self: *ImageStorage,
io: std.Io,
alloc: Allocator,
s: *terminal.Screen,
image_id: u32,
placement_id: u32,
p: Placement,
) !void {
assert(self.images.get(image_id) != null);
log.debug("placement image_id={} placement_id={} placement={}\n", .{
image_id,
placement_id,
p,
});
// When we add a placement, take this opportunity to reap garbage.
// Garbage are placements that disappeared off scrollback (were
// pruned). We don't clear Kitty state in the hot path so we do
// this opportunistically here.
self.reapGarbagePlacements(io, s);
// The important piece here is that the placement ID needs to
// be marked internal if it is zero. This allows multiple placements
// to be added for the same image. If it is non-zero, then it is
// an external placement ID and we can only have one placement
// per (image id, placement id) pair.
const key: PlacementKey = .{
.image_id = image_id,
.placement_id = if (placement_id == 0) .{
.tag = .internal,
.id = id: {
defer self.next_internal_placement_id +%= 1;
break :id self.next_internal_placement_id;
},
} else .{
.tag = .external,
.id = placement_id,
},
};
const gop = try self.placements.getOrPut(alloc, key);
if (gop.found_existing) {
gop.value_ptr.deinit(s);
} else {
const img = self.images.getPtr(image_id).?;
img.metadata.placement_count = std.math.add(
@TypeOf(img.metadata.placement_count),
img.metadata.placement_count,
1,
) catch {
self.placements.removeByPtr(gop.key_ptr);
return error.OutOfMemory;
};
}
gop.value_ptr.* = p;
// This always mutates
self.markMutated(io);
}
/// How the row operation behind a scrollMarginsBegin/end pair moves
/// tracked pins, which determines how much repositioning work the
/// placements need.
pub const ScrollOp = enum {
/// The operation shifts the entire active window down (it
/// creates scrollback), so every placement anchored in the
/// active area changes active position and must be restored.
window_shift,
/// The operation moves rows in place within the scrolling
/// region: pins anchored outside the region rows keep their
/// active position without help, so only placements anchored
/// inside the region rows need handling. This is the common
/// case (e.g. a status bar image beside a scrolling pane
/// costs almost nothing per scroll).
in_place,
};
/// Adjust placements for a scroll of the terminal's scrolling region
/// by `delta` rows (negative moves content up). This implements the
/// Kitty graphics protocol behavior for scrolls bounded by margins:
/// placements entirely inside the scrolling region move with the content
/// and are clipped when the move would extend them past a margin (deleted
/// when fully clipped), while every other placement (straddling a margin,
/// outside the region, or in the scrollback) must not move at all.
///
/// This is split into two phases around the row operation because
/// the various scroll implementations move tracked pins in
/// inconsistent ways (some shift the active area so every pin moves,
/// some rotate rows in place and adjust only pins inside the
/// region). We record the desired final position of every placement
/// up front in the returned state, whose end() repositions the pins
/// once the rows have settled, making the result independent of the
/// path taken. Every call must be paired with exactly one end().
///
/// Callers must skip this entirely when the scrolling region is the
/// full screen: a full-screen scroll moves placements with their
/// anchored rows (possibly into the scrollback) via pin tracking,
/// which already matches kitty's marginless scroll behavior.
pub fn scrollMarginsBegin(
self: *ImageStorage,
io: std.Io,
t: *terminal.Terminal,
delta: isize,
op: ScrollOp,
) ScrollMargins {
const s: *terminal.Screen = t.screens.active;
assert(self == &s.kitty_images);
var result: ScrollMargins = .{ .screen = s };
const top: i64 = t.scrolling_region.top;
const bottom: i64 = t.scrolling_region.bottom;
var mutated = false;
var it = self.placements.iterator();
while (it.next()) |entry| {
const p: *Placement = entry.value_ptr;
// Virtual placements follow their placeholder cells and
// relative placements follow their parents, so neither is
// ever adjusted by scrolls, matching kitty.
const pin: *PageList.Pin = switch (p.location) {
.pin => |pin| pin,
.virtual, .relative => continue,
};
// Pruned placements are reaped by reapGarbagePlacements.
if (pin.garbage) continue;
// Placements anchored outside the active area (scrollback)
// are never touched by a scroll within the margins.
const coord = (s.pages.pointFromPin(
.active,
pin.*,
) orelse continue).coord();
const y: i64 = coord.y;
// An in-place operation doesn't touch pins anchored outside
// the region rows, and a placement anchored there can never
// be entirely inside the region, so it needs no work at all.
if (op == .in_place and (y < top or y > bottom)) continue;
// The final position defaults to unmoved; restoring the pin
// to this exact spot afterwards is what stops the row
// operations from dragging the placement around.
var final_y: i64 = y;
inside: {
const image = self.images.get(entry.key_ptr.image_id) orelse break :inside;
const grid = p.gridSize(image, t);
// Unknown geometry (e.g. pixel sizes not yet reported)
// means we can't know the extent, so we can't consider
// the placement contained.
if (grid.rows == 0 or grid.cols == 0) break :inside;
// Only placements entirely within the scrolling region
// move. Kitty only has top/bottom margins so it only
// checks rows; the column check is our generalization
// for left/right margins and is trivially true for
// full-width regions.
if (y < top or y + grid.rows - 1 > bottom) break :inside;
const x: i64 = coord.x;
if (x < t.scrolling_region.left or
@min(x + grid.cols - 1, @as(i64, t.cols) - 1) > t.scrolling_region.right) break :inside;
const new_y: i64 = y + delta;
const rows: i64 = grid.rows;
const top_clip: i64 = @max(0, top - new_y);
const bottom_clip: i64 = @max(0, new_y + rows - 1 - bottom);
const visible: bool = clip: {
if (top_clip >= rows or bottom_clip >= rows) break :clip false;
if (top_clip > 0) {
if (!p.clipTop(
image,
t,
@intCast(top_clip),
grid.rows,
)) break :clip false;
mutated = true;
final_y = top;
break :clip true;
}
if (bottom_clip > 0) {
if (!p.clipBottom(
image,
t,
@intCast(bottom_clip),
grid.rows,
)) break :clip false;
mutated = true;
}
final_y = new_y;
break :clip true;
};
// Scrolled or clipped entirely out of the region: the
// placement is deleted, like kitty. The image itself is
// retained for future placements.
if (!visible) {
self.removePlacement(s, entry);
mutated = true;
continue;
}
}
result.restores.append(s.alloc, .{
.pin = pin,
.x = pin.x,
.y = @intCast(final_y),
}) catch {
// OOM: this placement is left to the whims of the
// generic pin tracking. Degraded, but safe.
log.warn("OOM adjusting image placement for scroll", .{});
continue;
};
}
if (mutated) {
// Placements deleted by clipping may orphan relative placements.
// Orphans have no pins so this never touches the restores.
_ = self.removeOrphans(s, null);
self.markMutated(io);
}
return result;
}
/// Reap pin-backed placements whose tracked content has been pruned
/// from history, along with any relative placements orphaned by
/// that, marking the content mutation.
///
/// Virtual and relative placements have no tracked screen location and
/// are never pruned themselves. Kitty removes placements once they
/// scroll out of retained history.
fn reapGarbagePlacements(
self: *ImageStorage,
io: std.Io,
s: *terminal.Screen,
) void {
var removed = false;
var it = self.placements.iterator();
while (it.next()) |entry| {
const pin = switch (entry.value_ptr.location) {
.pin => |pin| pin,
.virtual, .relative => continue,
};
if (!pin.garbage) continue;
self.removePlacement(s, entry);
removed = true;
}
if (!removed) return;
// If we removed a placement, then also remove any orphan
// children if this was a parent.
_ = self.removeOrphans(s, null);
self.markMutated(io);
}
fn clearPlacements(self: *ImageStorage, s: *terminal.Screen) void {
var it = self.placements.iterator();
while (it.next()) |entry| entry.value_ptr.deinit(s);
self.placements.clearRetainingCapacity();
}
fn removePlacementsByImageId(
self: *ImageStorage,
s: *terminal.Screen,
image_id: u32,
) void {
var it = self.placements.iterator();
while (it.next()) |entry| {
if (entry.key_ptr.image_id != image_id) continue;
self.removePlacement(s, entry);
}
}
/// Remove a placement by its map entry, releasing any tracked pin
/// and keeping the image's placement count in sync. The entry must
/// point into the placements map (via iteration or getEntry).
fn removePlacement(
self: *ImageStorage,
s: *terminal.Screen,
entry: PlacementMap.Entry,
) void {
entry.value_ptr.deinit(s);
const img = self.images.getPtr(entry.key_ptr.image_id).?;
assert(img.metadata.placement_count > 0);
img.metadata.placement_count -= 1;
self.placements.removeByPtr(entry.key_ptr);
}
/// Remove relative placements whose parent placement no longer
/// exists, keeping a relative placement's lifetime tied to its
/// parent chain. Chains can be several links deep, so this loops
/// until a pass removes nothing to take out entire orphaned
/// subtrees. Returns true if anything was removed.
///
/// If `delete_unused` is non-null, an image left without placements
/// by the reap is freed using it. This matches uppercase delete
/// semantics; every other removal path retains image data and
/// passes null.
///
/// This must be called, outside of any placements iteration, after
/// every operation that removes placements. Kitty gets the same
/// effect lazily by dropping unresolvable placements while drawing;
/// we do it eagerly because our renderer never mutates terminal
/// state.
fn removeOrphans(
self: *ImageStorage,
s: *terminal.Screen,
delete_unused: ?Allocator,
) bool {
var removed_any = false;
var removed = true;
while (removed) {
removed = false;
var it = self.placements.iterator();
while (it.next()) |entry| {
const rel = switch (entry.value_ptr.location) {
.relative => |rel| rel,
.pin, .virtual => continue,
};
if (self.placements.contains(rel.parent)) continue;
// Parent is gone, remove this placement.
const image_id = entry.key_ptr.image_id;
self.removePlacement(s, entry);
removed = true;
removed_any = true;
if (delete_unused) |alloc| self.deleteIfUnused(
alloc,
image_id,
);
}
}
return removed_any;
}
/// Maximum number of parent links in a relative placement chain,
/// matching the minimum specified in the spec and the actual
/// limit defined by Kitty at the time of authoring.
pub const parent_chain_limit = 8;
pub const ParentError = error{
/// The parent image (P=) does not exist.
ParentImageNotFound,
/// The parent image exists but the requested placement (Q=, or
/// any placement when Q is omitted) does not.
ParentPlacementNotFound,
/// The placement refers to itself as its own parent.
SelfParent,
/// The parent chain loops back to the placement being created.
Cycle,
/// The parent chain exceeds parent_chain_limit links.
TooDeep,
/// An ancestor in the chain no longer exists. This should not
/// happen since removeOrphans keeps chains intact, but we
/// check anyway rather than trusting the invariant.
AncestorNotFound,
};
/// Resolve and validate the parent reference (P=/Q=) of a relative
/// placement, returning the concrete key of the parent placement.
/// `child` is the key of the placement being created when it is
/// addressable (an explicit placement ID); null for placements that
/// will receive a fresh internal ID, which nothing can refer to yet.
///
/// Checks: the parent must exist, the placement must not
/// parent itself, and the resulting ancestor chain must be acyclic
/// and within parent_chain_limit.
pub fn resolveParent(
self: *ImageStorage,
io: std.Io,
s: *terminal.Screen,
child: ?PlacementKey,
parent_image_id: u32,
parent_placement_id: u32,
) ParentError!PlacementKey {
// Reap placements whose content has been pruned from history
// before resolving: a pruned parent must be reported as missing,
// not accepted and then immediately reaped by addPlacement's own
// sweep, which would store an orphan.
self.reapGarbagePlacements(io, s);
// If the parent doesn't exist we're already failed.
if (!self.images.contains(parent_image_id)) {
return error.ParentImageNotFound;
}
// Find the parent
const parent: PlacementKey = parent: {
// An explicit parent placement ID (Q=) selects exactly that
// placement.
if (parent_placement_id > 0) {
const key: PlacementKey = .{
.image_id = parent_image_id,
.placement_id = .{
.tag = .external,
.id = parent_placement_id,
},
};
if (!self.placements.contains(key)) {
return error.ParentPlacementNotFound;
}
break :parent key;
}
// No Q: pick a placement of the parent image. Kitty picks
// the oldest surviving placement; our placement map doesn't
// track creation order so we pick by PlacementId.preferredOver
// instead. This is unspecified so any behavior here is fine.
// In practice the parent image has a single placement, and
// clients use Q when it doesn't.
const best: PlacementId = best: {
var best: ?PlacementId = null;
var it = self.placements.keyIterator();
while (it.next()) |key| {
if (key.image_id != parent_image_id) continue;
const id = key.placement_id;
const b = best orelse {
best = id;
continue;
};
if (id.preferredOver(b)) best = id;
}
break :best best orelse return error.ParentPlacementNotFound;
};
break :parent .{
.image_id = parent_image_id,
.placement_id = best,
};
};
// A placement cannot be its own parent. This must be checked
// before the chain walk so it reports EINVAL, not ECYCLE.
if (child) |c| if (parent.eql(c)) return error.SelfParent;
// Walk the would-be ancestor chain. `depth` counts parent links,
// starting at one for the link we are about to create.
var depth: usize = 1;
var key = parent;
while (true) {
if (child) |c| if (key.eql(c)) return error.Cycle;
const p = self.placements.get(key) orelse return error.AncestorNotFound;
const rel = switch (p.location) {
.relative => |rel| rel,
// A pin or virtual placement is the chain root.
.pin, .virtual => break,
};
if (depth >= parent_chain_limit) return error.TooDeep;
depth += 1;
key = rel.parent;
}
return parent;
}
/// The result of resolving a relative placement's parent chain:
/// the chain's root placement (always pin or virtual, never
/// relative) and the total cell offset from the root's origin.
pub const ResolvedChain = struct {
root_key: PlacementKey,
root: Placement,
horizontal_offset: i32,
vertical_offset: i32,
};
/// Resolve a relative placement's parent chain to its root,
/// accumulating the cell offsets (H=/V=) of every link on the way.
/// The placement's position is the root's origin plus the accumulated
/// offsets.
///
/// Returns null when the chain is broken or too deep. Neither can
/// normally happen for stored placements (resolveParent validates
/// chains and removeOrphans removes broken ones), with one kitty
/// quirk: replacing an ancestor placement can deepen the chains
/// below it beyond parent_chain_limit after the fact.
pub fn resolveChain(
self: *const ImageStorage,
rel: Placement.Relative,
) ?ResolvedChain {
var horizontal = rel.horizontal_offset;
var vertical = rel.vertical_offset;
var key = rel.parent;
var depth: usize = 1;
while (true) {
const p = self.placements.get(key) orelse return null;
switch (p.location) {
.relative => |parent_rel| {
if (depth >= parent_chain_limit) return null;
depth += 1;
horizontal +|= parent_rel.horizontal_offset;
vertical +|= parent_rel.vertical_offset;
key = parent_rel.parent;
},
.pin, .virtual => return .{
.root_key = key,
.root = p,
.horizontal_offset = horizontal,
.vertical_offset = vertical,
},
}
}
}
/// Returns the placement that a unicode placeholder cell referencing
/// this image/placement ID pair targets, or null if there is none. A
/// zero placement ID targets one of the image's virtual placements,
/// chosen by PlacementId.preferredOver so the choice is stable (an
/// image rarely has more than one). This is the shared lookup used
/// both for sizing placeholder runs and for positioning relative
/// placements whose chain roots at a virtual placement.
pub fn placeholderTarget(
self: *const ImageStorage,
image_id: u32,
placement_id: u32,
) ?struct { key: PlacementKey, placement: Placement } {
if (placement_id > 0) {
const key: PlacementKey = .{
.image_id = image_id,
.placement_id = .{ .tag = .external, .id = placement_id },
};
const p = self.placements.get(key) orelse return null;
return .{ .key = key, .placement = p };
}
var best: ?PlacementKey = null;
var it = self.placements.iterator();
while (it.next()) |entry| {
if (entry.key_ptr.image_id != image_id) continue;
if (entry.value_ptr.location != .virtual) continue;
const b = best orelse {
best = entry.key_ptr.*;
continue;
};
if (entry.key_ptr.placement_id.preferredOver(b.placement_id)) {
best = entry.key_ptr.*;
}
}
const key = best orelse return null;
return .{ .key = key, .placement = self.placements.get(key).? };
}
/// Get an image by its ID. If the image doesn't exist, null is returned.
pub fn imageById(self: *const ImageStorage, image_id: u32) ?Image {
return self.images.get(image_id);
}
/// Get an image by its number. If the image doesn't exist, return null.
pub fn imageByNumber(self: *const ImageStorage, image_number: u32) ?Image {
var newest: ?Image = null;
var it = self.images.iterator();
while (it.next()) |kv| {
if (kv.value_ptr.number == image_number) {
if (newest == null or
kv.value_ptr.generation > newest.?.generation)
{
newest = kv.value_ptr.*;
}
}
}
return newest;
}
/// Get a mutable pointer to a stored image, by ID or (newest by)
/// number, following the protocol's id/number addressing. Used by
/// the animation commands, which mutate images in place. The
/// pointer is invalidated by any operation that adds or removes
/// images.
pub fn imagePtrByIdOrNumber(
self: *const ImageStorage,
image_id: u32,
image_number: u32,
) ?*Image {
if (image_id != 0) return self.images.getPtr(image_id);
var newest: ?*Image = null;
var it = self.images.iterator();
while (it.next()) |kv| {
if (kv.value_ptr.number != image_number) continue;
if (newest == null or
kv.value_ptr.generation > newest.?.generation)
{
newest = kv.value_ptr;
}
}
return newest;
}
/// Record that the displayed content of an image changed without
/// the image being re-added: marks the storage mutated and stamps
/// the image with the fresh generation so consumers (e.g. the
/// renderer's texture cache) replace what they hold. Used when an
/// animation changes which frame is current or edits the current
/// frame's pixels.
pub fn markImageContentChanged(
self: *ImageStorage,
io: std.Io,
img: *Image,
) void {
self.markMutated(io);
img.generation = self.generation;
}
pub fn materializeBacking(self: *ImageStorage, io: std.Io, alloc: Allocator, img: *Image) !void {
if (img.data != .encoded_png and img.data != .native_file) return;
const before = img.data.len();
try img.materializeFile(alloc);
try img.materializePng(alloc);
self.total_bytes -= before - img.data.len();
self.markImageContentChanged(io, img);
}
/// Convert a stored image's base data to RGBA in place, adjusting
/// byte accounting. All animation composition happens in RGBA;
/// this is called before the first composition into an image.
///
/// The pixels are unchanged visually but the stored representation
/// changed, so the image is stamped with a fresh generation.
pub fn convertImageToRgba(
self: *ImageStorage,
io: std.Io,
alloc: Allocator,
img: *Image,
) Allocator.Error!void {
if (img.format == .rgba) return;
const old = img.data.bytes() orelse return;
const rgba = try pixel.rgbaFromFormat(alloc, img.format, old);
self.total_bytes -= old.len;
self.total_bytes += rgba.len;
img.data.deinit(alloc);
img.data = .{ .complete = rgba };
img.format = .rgba;
self.markImageContentChanged(io, img);
}
/// Reserve `bytes` of storage for animation frame data belonging
/// to `image_id`, evicting other images if needed, mirroring how
/// image transmission reserves space.
///
/// Errors if the space cannot be made available. On success the caller
/// owns the reservation and must either attach the frame data to the
/// image or call releaseAnimationBytes.
pub fn reserveAnimationBytes(
self: *ImageStorage,
io: std.Io,
alloc: Allocator,
s: *terminal.Screen,
image_id: u32,
bytes: usize,
) Allocator.Error!void {
if (bytes > self.total_limit) return error.OutOfMemory;
const total_bytes = self.total_bytes + bytes;
if (total_bytes > self.total_limit) {
const req_bytes = total_bytes - self.total_limit;
// Excess this large cannot be recovered by evicting other
// images (evictImageExcept also requires it).
if (req_bytes > self.total_limit) return error.OutOfMemory;
log.info("evicting images for animation frame, evicting={}", .{req_bytes});
if (!self.evictImageExcept(
io,
alloc,
s,
req_bytes,
image_id,
)) {
log.warn("failed to evict enough images for animation frame", .{});
return error.OutOfMemory;
}
}
self.total_bytes += bytes;
}
/// Release a reservation made by reserveAnimationBytes, or credit
/// bytes freed by deleting animation frame data.
pub fn releaseAnimationBytes(self: *ImageStorage, bytes: usize) void {
assert(bytes <= self.total_bytes);
self.total_bytes -= bytes;
}
/// Advance every running animation to the frame that should be
/// displayed at `now_ms` and report when the next frame change is
/// due, as a delay in milliseconds relative to `now_ms`. Null
/// means no running animation needs a future tick.
///
/// `now_ms` is a monotonic timestamp on a clock of the caller's
/// choosing. The same clock must be used for every call. The
/// caller is expected to be the renderer, ticking once per frame
/// build and scheduling a wakeup for the returned delay.
pub fn animationTick(self: *ImageStorage, io: std.Io, now_ms: u64) ?u64 {
var min_delay: ?u64 = null;
var it = self.images.iterator();
while (it.next()) |entry| {
const img: *Image = entry.value_ptr;
// The gates below mirror Kitty's image_is_animatable.
// No animation state was ever attached (plain image).
const anim = img.animation orelse continue;
// Stopped is the initial state of every animation: frames
// then only change client-driven (a=a c=N), never by time.
if (anim.state == .stopped) continue;
// Only the root frame exists; there is nothing to advance
// to yet even in the running state.
if (anim.frames.items.len == 0) continue;
// Unplaced images don't animate. This is our simpler
// approximation of Kitty's "is actually drawn" visibility
// gate; it is what stops an image that was transmitted but
// never placed from waking the renderer forever.
if (img.metadata.placement_count == 0) continue;
// The base pixel data hasn't arrived yet (e.g. an image
// restored from a snapshot); nothing can be displayed.
if (img.data.isPending()) continue;
// A zero total duration means every frame is gapless and
// no frame can ever be displayed, so the animation can
// never advance. This also guards the gapless-skip loop
// below from never terminating.
if (anim.durationMs() == 0) continue;
// A finite loop budget (a=a v=N) that ran out on an
// earlier tick froze playback on the last frame for good.
if (anim.max_loops > 0 and anim.current_loop >= anim.max_loops) continue;
const shown_at: u64 = shown_at: {
// First tick since playback started (or since the
// current frame changed through another path, e.g.
// a=a c=N): the frame is considered shown as of now,
// and its gap starts counting from here.
const at = anim.frame_shown_at_ms orelse break :shown_at now_ms;
// A timestamp from the future means the caller's clock
// restarted; re-anchor rather than stalling until the
// old timestamp comes around again.
if (at > now_ms) break :shown_at now_ms;
break :shown_at at;
};
anim.frame_shown_at_ms = shown_at;
// The current frame is replaced once its gap has elapsed.
// We advance at most one displayed frame per tick with no
// catch-up, exactly like Kitty: if ticks lag behind the
// gaps, the animation slows down rather than skipping.
var next_at: u64 = shown_at +| anim.gapAt(anim.current_index);
if (now_ms >= next_at) advance: {
// Walk forward to the next displayable frame. This is
// a loop only because gapless (gap=0) frames are never
// displayed and are stepped over; the durationMs gate
// above guarantees a displayable frame exists.
const count: u32 = anim.frameCount();
var idx = anim.current_index;
while (true) {
const next = (idx + 1) % count;
if (next == 0) {
// Wrapping past the last frame back to the
// root. A loading-state (a=a s=2) animation
// refuses the wrap: it parks on the last frame
// awaiting more frames from the client.
if (anim.state == .loading) break :advance;
// Each wrap completes a loop; a finite budget
// that just ran out parks on the last frame.
anim.current_loop += 1;
if (anim.max_loops > 0 and
anim.current_loop >= anim.max_loops) break :advance;
}
idx = next;
if (anim.gapAt(idx) != 0) break;
}
// Show the chosen frame: restart its gap timer and
// stamp a fresh generation so consumers (the renderer
// texture cache, the C API) pick up the new pixels.
anim.current_index = idx;
anim.frame_shown_at_ms = now_ms;
self.markImageContentChanged(io, img);
next_at = now_ms +| anim.gapAt(idx);
}
// Schedule the next tick. A parked animation left next_at
// in the past and so never schedules one; it is woken by
// its trigger instead (a new frame arriving, or an a=a
// command changing the state).
if (next_at > now_ms) {
const delay = next_at - now_ms;
min_delay = if (min_delay) |m| @min(m, delay) else delay;
}
}
return min_delay;
}
/// Clear placements intersecting the active screen, then reclaim every
/// image with no remaining placement. Unlike protocol d=A, a terminal
/// clear also reclaims images that were already unplaced.
pub fn clearScreen(
self: *ImageStorage,
io: std.Io,
alloc: Allocator,
t: *terminal.Terminal,
) void {
const placements_before = self.placements.count();
const images_before = self.images.count();
// Delete unused placements and images. Orphaned relative
// placements must be reaped before the image sweep so that
// their images are reclaimable too.
self.deleteVisiblePlacements(alloc, t, true);
_ = self.removeOrphans(t.screens.active, null);
var image_it = self.images.iterator();
while (image_it.next()) |entry| {
self.deleteIfUnused(alloc, entry.key_ptr.*);
}
// Mark mutated only if things changed.
if (self.placements.count() != placements_before or
self.images.count() != images_before) self.markMutated(io);
}
/// Delete placements, images.
pub fn delete(
self: *ImageStorage,
io: std.Io,
alloc: Allocator,
t: *terminal.Terminal,
cmd: command.Delete.Action,
) void {
// Deletes only ever remove placements/images, so comparing counts
// before and after tells us whether anything actually changed.
// Only then do we mark a mutation, so a delete that matches nothing
// doesn't dirty the image state or bump the generation.
const placements_before = self.placements.count();
const images_before = self.images.count();
defer if (self.placements.count() != placements_before or
self.images.count() != images_before) self.markMutated(io);
switch (cmd) {
.all => |delete_images| self.deleteVisiblePlacements(
alloc,
t,
delete_images,
),
.id => |v| self.deleteById(
alloc,
t.screens.active,
v.image_id,
v.placement_id,
v.delete,
),
.newest => |v| newest: {
const img = self.imageByNumber(v.image_number) orelse break :newest;
self.deleteById(
alloc,
t.screens.active,
img.id,
v.placement_id,
v.delete,
);
},
.intersect_cursor => |delete_images| {
self.deleteIntersecting(
alloc,
t,
.{ .active = .{
.x = t.screens.active.cursor.x,
.y = t.screens.active.cursor.y,
} },
delete_images,
{},
null,
);
},
.intersect_cell => |v| intersect_cell: {
if (v.x <= 0 or v.y <= 0) {
log.warn("delete intersect cell coords must be at least 1", .{});
break :intersect_cell;
}
self.deleteIntersecting(
alloc,
t,
.{ .active = .{
.x = std.math.cast(size.CellCountInt, v.x - 1) orelse break :intersect_cell,
.y = std.math.cast(size.CellCountInt, v.y - 1) orelse break :intersect_cell,
} },
v.delete,
{},
null,
);
},
.intersect_cell_z => |v| intersect_cell_z: {
if (v.x <= 0 or v.y <= 0) {
log.warn("delete intersect cell coords must be at least 1", .{});
break :intersect_cell_z;
}
self.deleteIntersecting(
alloc,
t,
.{ .active = .{
.x = std.math.cast(size.CellCountInt, v.x - 1) orelse break :intersect_cell_z,
.y = std.math.cast(size.CellCountInt, v.y - 1) orelse break :intersect_cell_z,
} },
v.delete,
v.z,
struct {
fn filter(ctx: i32, p: Placement) bool {
return p.z == ctx;
}
}.filter,
);
},
.column => |v| column: {
if (v.x <= 0) {
log.warn("delete column must be greater than zero", .{});
break :column;
}
const x = v.x - 1;
var it = self.placements.iterator();
while (it.next()) |entry| {
const img = self.imageById(entry.key_ptr.image_id) orelse continue;
const rect = entry.value_ptr.rect(img, t) orelse continue;
if (rect.top_left.x <= x and rect.bottom_right.x >= x) {
self.removePlacement(t.screens.active, entry);
if (v.delete) self.deleteIfUnused(alloc, img.id);
}
}
},
.row => |v| row: {
if (v.y <= 0) {
log.warn("delete row must be greater than zero", .{});
break :row;
}
// v.y is in active coords so we want to convert it to a pin
// so we can compare by page offsets.
const target_pin = t.screens.active.pages.pin(.{ .active = .{
.y = std.math.cast(size.CellCountInt, v.y - 1) orelse break :row,
} }) orelse break :row;
var it = self.placements.iterator();
while (it.next()) |entry| {
const img = self.imageById(entry.key_ptr.image_id) orelse continue;
const rect = entry.value_ptr.rect(img, t) orelse continue;
// We need to copy our pin to ensure we are at least at
// the top-left x.
var target_pin_copy = target_pin;
target_pin_copy.x = rect.top_left.x;
if (target_pin_copy.isBetween(rect.top_left, rect.bottom_right)) {
self.removePlacement(t.screens.active, entry);
if (v.delete) self.deleteIfUnused(alloc, img.id);
}
}
},
.z => |v| {
var it = self.placements.iterator();
while (it.next()) |entry| {
switch (entry.value_ptr.location) {
// Relative placements carry their own z value and
// are matched by z deletes just like kitty does.
.pin, .relative => {},
// Virtual placeholders cannot delete by z according
// to the spec.
.virtual => continue,
}
if (entry.value_ptr.z == v.z) {
const image_id = entry.key_ptr.image_id;
self.removePlacement(t.screens.active, entry);
if (v.delete) self.deleteIfUnused(alloc, image_id);
}
}
},
.range => |v| range: {
// Both bounds default to zero when omitted. An inverted range
// or a zero upper bound is not an error, it just selects
// nothing: image IDs are always non-zero.
if (v.last == 0 or v.first > v.last) break :range;
// Remove matching placements in one pass.
var placement_it = self.placements.iterator();
while (placement_it.next()) |entry| {
if (entry.key_ptr.image_id < v.first or
entry.key_ptr.image_id > v.last) continue;
self.removePlacement(t.screens.active, entry);
}
// Uppercase deletion also frees matching images that are now
// unused, including images that had no placements initially.
if (!v.delete) break :range;
var image_it = self.images.iterator();
while (image_it.next()) |entry| {
const image_id = entry.key_ptr.*;
if (image_id >= v.first and image_id <= v.last) {
self.deleteIfUnused(alloc, image_id);
}
}
},
.animation_frames => |v| self.deleteAnimationFrame(
io,
alloc,
t.screens.active,
v,
),
}
// Deleting placements orphans any relative placements parented
// to them (transitively). Their lifetime is tied to the parent
// so they are removed as well, and an uppercase delete also
// frees any image the cascade leaves without placements (the
// per-branch deleteIfUnused calls above ran while the orphans
// still counted as placements).
const delete_unused: bool = switch (cmd) {
.all, .intersect_cursor => |v| v,
inline else => |v| v.delete,
};
_ = self.removeOrphans(
t.screens.active,
if (delete_unused) alloc else null,
);
}
/// Delete only non-virtual placements that intersect the active screen.
/// The protocol defines d=a/A in terms of visible placements, and an
/// uppercase delete only frees data for images selected by those
/// placements.
fn deleteVisiblePlacements(
self: *ImageStorage,
alloc: Allocator,
t: *terminal.Terminal,
delete_unused: bool,
) void {
var it = self.placements.iterator();
while (it.next()) |entry| {
const pin = switch (entry.value_ptr.location) {
.pin => |pin| pin,
// Virtual placements are never selected by visible
// deletes per the protocol. Relative placements are
// removed with their parents instead (removeOrphans).
.virtual, .relative => continue,
};
if (pin.garbage) continue;
// Placements anchored in the active area are necessarily visible.
// Only compute their extent when the anchor is already in history
// and the bottom edge may still intersect the active area.
if (t.screens.active.pages.pointFromPin(
.active,
pin.*,
) == null) {
const image = self.imageById(entry.key_ptr.image_id) orelse continue;
const rect = entry.value_ptr.rect(image, t) orelse continue;
if (t.screens.active.pages.pointFromPin(.active, rect.bottom_right) == null) continue;
}
const image_id = entry.key_ptr.image_id;
self.removePlacement(t.screens.active, entry);
if (delete_unused) self.deleteIfUnused(alloc, image_id);
}
}
fn deleteById(
self: *ImageStorage,
alloc: Allocator,
s: *terminal.Screen,
image_id: u32,
placement_id: u32,
delete_unused: bool,
) void {
var matched = placement_id == 0;
// If no placement, we delete all placements with the ID
if (placement_id == 0) self.removePlacementsByImageId(
s,
image_id,
) else if (self.placements.getEntry(.{
.image_id = image_id,
.placement_id = .{
.tag = .external,
.id = placement_id,
},
})) |entry| {
self.removePlacement(s, entry);
matched = true;
}
// A placement ID narrows the selection to that exact placement, so an
// unmatched selector must not free otherwise-unreferenced image data.
// https://sw.kovidgoyal.net/kitty/graphics-protocol/#deleting-images
if (delete_unused and matched) self.deleteIfUnused(alloc, image_id);
}
/// Delete an animation frame (d=f/F). Deletes never produce
/// responses, so all failures are only logged. Kitty behaviors
/// implemented here: on an image without extra frames a lowercase
/// delete is a no-op while an uppercase delete removes the entire
/// image, placements included; the frame number is clamped to the
/// last frame and zero selects the root frame; deleting the root
/// frame promotes frame 2 to be the new root.
fn deleteAnimationFrame(
self: *ImageStorage,
io: std.Io,
alloc: Allocator,
s: *terminal.Screen,
v: command.Delete.Action.AnimationFrames,
) void {
if (v.image_id == 0 and v.image_number == 0) {
log.warn("delete animation frames requires image id or number", .{});
return;
}
const img = self.imagePtrByIdOrNumber(
v.image_id,
v.image_number,
) orelse {
log.warn(
"delete animation frames for unknown image id={} number={}",
.{ v.image_id, v.image_number },
);
return;
};
const anim: *animation.Animation = anim: {
if (img.animation) |anim| {
if (anim.frames.items.len > 0) break :anim anim;
}
// The image is not (or no longer) an animation. The
// uppercase delete removes the entire image, even when it
// still has placements.
if (!v.delete) return;
self.removePlacementsByImageId(s, img.id);
const entry = self.images.getEntry(img.id).?;
self.total_bytes -= entry.value_ptr.storageSize();
entry.value_ptr.deinit(alloc);
self.images.removeByPtr(entry.key_ptr);
return;
};
// Clamp the frame number: zero selects the root frame and
// values past the end select the last frame.
const count: u32 = anim.frameCount();
var number: u32 = @min(v.frame, count);
if (number == 0) number = 1;
if (number == 1) {
// Deleting the root frame promotes frame 2 to root. The
// promoted frame's bytes stay reserved; only the old root
// data is freed.
self.releaseAnimationBytes(img.data.len());
img.data.deinit(alloc);
const promoted = anim.frames.orderedRemove(0);
img.data = .{ .complete = promoted.data };
anim.root_gap_ms = promoted.gap_ms;
} else {
const removed = anim.frames.orderedRemove(number - 2);
self.releaseAnimationBytes(removed.data.len);
alloc.free(removed.data);
}
// Fix up the current frame.
const removed_idx: u32 = if (number == 1) 0 else number - 2;
const remaining: u32 = @intCast(anim.frames.items.len);
if (anim.current_index > remaining) {
anim.current_index = remaining;
anim.frame_shown_at_ms = null;
self.markImageContentChanged(io, img);
return;
}
if (removed_idx == anim.current_index) {
anim.frame_shown_at_ms = null;
self.markImageContentChanged(io, img);
} else {
if (removed_idx < anim.current_index) anim.current_index -= 1;
self.markMutated(io);
}
}
/// Delete an image if it is unused.
fn deleteIfUnused(self: *ImageStorage, alloc: Allocator, image_id: u32) void {
const entry = self.images.getEntry(image_id) orelse return;
if (entry.value_ptr.metadata.placement_count > 0) return;
self.total_bytes -= entry.value_ptr.storageSize();
entry.value_ptr.deinit(alloc);
self.images.removeByPtr(entry.key_ptr);
}
/// Deletes all placements intersecting a screen point.
fn deleteIntersecting(
self: *ImageStorage,
alloc: Allocator,
t: *terminal.Terminal,
p: point.Point,
delete_unused: bool,
filter_ctx: anytype,
comptime filter: ?fn (@TypeOf(filter_ctx), Placement) bool,
) void {
// Convert our target point to a pin for comparison.
const target_pin = t.screens.active.pages.pin(p) orelse return;
var it = self.placements.iterator();
while (it.next()) |entry| {
const img = self.imageById(entry.key_ptr.image_id) orelse continue;
const rect = entry.value_ptr.rect(img, t) orelse continue;
if (rect.contains(target_pin)) {
if (filter) |f| if (!f(filter_ctx, entry.value_ptr.*)) continue;
self.removePlacement(t.screens.active, entry);
if (delete_unused) self.deleteIfUnused(alloc, img.id);
}
}
}
/// Evict image to make space. This will evict the oldest image,
/// prioritizing transient images and unused images as recommended by the
/// published Kitty spec.
///
/// This will evict as many images as necessary to make space for
/// req bytes.
fn evictImage(
self: *ImageStorage,
io: std.Io,
alloc: Allocator,
s: *terminal.Screen,
req: usize,
) bool {
return self.evictImageExcept(io, alloc, s, req, null);
}
/// Evict images while preserving `exclude_id`, used when replacing an
/// existing image whose old reservation has already been credited.
fn evictImageExcept(
self: *ImageStorage,
io: std.Io,
alloc: Allocator,
s: *terminal.Screen,
req: usize,
exclude_id: ?u32,
) bool {
assert(req <= self.total_limit);
const Candidate = struct {
id: u32,
generation: u64,
// Lower values are evicted first:
// 0: transient, unused
// 1: not transient, unused
// 2: transient, used
// 3: not transient, used
priority: u2,
fn init(img: *const Image) @This() {
return .{
.id = img.id,
.generation = img.generation,
.priority = (if (img.metadata.transient) @as(u2, 0) else @as(u2, 1)) +
(if (img.metadata.placement_count > 0) @as(u2, 2) else @as(u2, 0)),
};
}
fn lessThan(lhs: @This(), rhs: @This()) bool {
if (lhs.priority != rhs.priority)
return lhs.priority < rhs.priority;
if (lhs.generation != rhs.generation)
return lhs.generation < rhs.generation;
return lhs.id < rhs.id;
}
};
// Evicting anything is a content mutation. This matters for the
// setLimit path in particular, which doesn't otherwise mark it.
// Evicted placements can also orphan relative placements of
// other images, which must be reaped along with them.
const images_before = self.images.count();
defer if (self.images.count() != images_before) {
_ = self.removeOrphans(s, null);
self.markMutated(io);
};
var evicted: usize = 0;
while (evicted < req) {
const c = candidate: {
var best: ?Candidate = null;
var it = self.images.iterator();
while (it.next()) |kv| {
const img = kv.value_ptr;
if (exclude_id != null and img.id == exclude_id.?) continue;
const current = Candidate.init(img);
if (best == null or current.lessThan(best.?)) best = current;
}
break :candidate best orelse return false;
};
var p_it = self.placements.iterator();
while (p_it.next()) |entry| {
if (entry.key_ptr.image_id == c.id) {
self.removePlacement(s, entry);
}
}
const entry = self.images.getEntry(c.id).?;
const image_len = entry.value_ptr.storageSize();
log.info("evicting image id={} bytes={}", .{ c.id, image_len });
evicted += image_len;
self.total_bytes -= image_len;
entry.value_ptr.deinit(alloc);
self.images.removeByPtr(entry.key_ptr);
}
return true;
}
/// State returned by scrollMarginsBegin: the final active-area
/// position every surviving placement pin must be restored to once
/// the scrolled rows have settled. Every begin must be paired with
/// exactly one end() call after the scroll's row operations
/// complete.
pub const ScrollMargins = struct {
screen: *terminal.Screen,
restores: std.ArrayListUnmanaged(Restore) = .empty,
const Restore = struct {
pin: *PageList.Pin,
x: size.CellCountInt,
y: u32,
};
/// Reposition every recorded placement pin to its post-scroll
/// position and release the state. Must be called exactly
/// once, after the scroll's row operations complete.
pub fn end(self: *ScrollMargins) void {
for (self.restores.items) |restore| {
restore.pin.* = self.screen.pages.pin(.{ .active = .{
.x = restore.x,
.y = restore.y,
} }) orelse continue;
}
self.restores.deinit(self.screen.alloc);
}
};
/// Every placement is uniquely identified by the image ID and the
/// placement ID. If an image ID isn't specified it is assumed to be 0.
/// Likewise, if a placement ID isn't specified it is assumed to be 0.
pub const PlacementKey = struct {
image_id: u32,
placement_id: PlacementId,
pub fn eql(self: PlacementKey, other: PlacementKey) bool {
return std.meta.eql(self, other);
}
};
/// Internal placement IDs are assigned by us for placements created
/// without an explicit ID (p=0); external IDs are client-specified.
/// The two are separate namespaces, hence the tag.
pub const PlacementId = packed struct {
tag: enum(u1) { internal, external },
id: u32,
/// Deterministic preference order used when an operation must
/// pick a single placement out of several (kitty uses creation
/// order, which our map doesn't track): external IDs win over
/// internal ones, and lower IDs win within a tag.
pub fn preferredOver(self: PlacementId, other: PlacementId) bool {
if (self.tag != other.tag) return self.tag == .external;
return self.id < other.id;
}
};
pub const Placement = struct {
/// The location where this placement should be drawn.
location: Location,
/// Offset of the x/y from the top-left of the cell.
x_offset: u32 = 0,
y_offset: u32 = 0,
/// Source rectangle for the image to pull from
source_x: u32 = 0,
source_y: u32 = 0,
source_width: u32 = 0,
source_height: u32 = 0,
/// The columns/rows this image occupies.
columns: u32 = 0,
rows: u32 = 0,
/// The z-index for this placement.
z: i32 = 0,
pub const Location = union(enum) {
/// Exactly placed on a screen pin.
pin: *PageList.Pin,
/// Virtual placement (U=1) for unicode placeholders.
virtual,
/// Placed relative to a parent placement (P=/Q=). The
/// placement has no screen position of its own; it is
/// positioned at render time by resolving the parent chain
/// to its root (see resolveChain) and therefore follows the
/// parent through scrolls automatically. Its lifetime is
/// tied to the parent: removing any ancestor removes it
/// too (see removeOrphans).
relative: Relative,
};
pub const Relative = struct {
/// The parent placement. Resolved to a concrete key when
/// the placement is created, so the parent is guaranteed
/// to exist at that point.
parent: PlacementKey,
/// Cell offsets from the parent's origin (H=/V=).
horizontal_offset: i32 = 0,
vertical_offset: i32 = 0,
};
pub fn deinit(
self: *const Placement,
s: *terminal.Screen,
) void {
switch (self.location) {
.pin => |p| s.pages.untrackPin(p),
.virtual, .relative => {},
}
}
/// Multiply two protocol-controlled values without allowing them to
/// wrap. Placement geometry is exposed as u32, so values larger than
/// that are represented by the largest possible value.
fn saturatingMul(lhs: u32, rhs: u32) u32 {
return std.math.mul(u32, lhs, rhs) catch std.math.maxInt(u32);
}
/// Scale a dimension by an aspect ratio and round to the nearest
/// integer. The u64 intermediate can hold the product of two u32s as
/// well as the rounding adjustment.
fn scaleDimension(value: u32, numerator: u32, denominator: u32) u32 {
if (denominator == 0) return 0;
const rounded = (@as(u64, value) * @as(u64, numerator) +
@as(u64, denominator) / 2) / @as(u64, denominator);
return std.math.cast(u32, rounded) orelse std.math.maxInt(u32);
}
pub const SourceRect = struct {
x: u32,
y: u32,
width: u32,
height: u32,
};
/// Returns the requested source rectangle intersected with the image.
/// A zero width or height requests the full corresponding image
/// dimension before intersection, as defined by the Kitty protocol.
pub fn sourceRect(self: Placement, image: Image) SourceRect {
const x = @min(self.source_x, image.width);
const y = @min(self.source_y, image.height);
return .{
.x = x,
.y = y,
.width = @min(
if (self.source_width > 0) self.source_width else image.width,
image.width - x,
),
.height = @min(
if (self.source_height > 0) self.source_height else image.height,
image.height - y,
),
};
}
/// Returns the placement offset clamped within the first cell. Pixel
/// geometry may temporarily be unavailable, in which case there is no
/// valid offset within the cell.
pub fn cellOffset(
self: Placement,
t: *const terminal.Terminal,
) struct {
x: u32,
y: u32,
} {
const cell_width: u32 = t.width_px / t.cols;
const cell_height: u32 = t.height_px / t.rows;
return .{
.x = if (cell_width > 0)
@min(self.x_offset, cell_width - 1)
else
0,
.y = if (cell_height > 0)
@min(self.y_offset, cell_height - 1)
else
0,
};
}
/// Returns the size of this placement's image in pixels,
/// taking into account the source rectangle, specified
/// rows/columns, and aspect ratio.
pub fn pixelSize(
self: Placement,
image: Image,
t: *const terminal.Terminal,
) struct {
width: u32,
height: u32,
} {
const source = self.sourceRect(image);
const width = source.width;
const height = source.height;
// If we don't have any specified cols or rows then the placement
// should be the native size of the image, and doesn't need to be
// re-scaled.
if (self.columns == 0 and self.rows == 0) return .{
.width = width,
.height = height,
};
// We calculate the size of a cell so that we can multiply
// it by the specified cols/rows to get the correct px size.
//
// We assume that the width is divided evenly by the column
// count and the height by the row count, because it should be.
const cell_width: u32 = t.width_px / t.cols;
const cell_height: u32 = t.height_px / t.rows;
const cell_offset = self.cellOffset(t);
// If we have a specified cols AND rows then we calculate
// the width and height from them directly, we don't need
// to adjust for aspect ratio.
if (self.columns > 0 and self.rows > 0) {
const calc_width = saturatingMul(cell_width, self.columns);
const calc_height = saturatingMul(cell_height, self.rows);
return .{
.width = calc_width -| cell_offset.x,
.height = calc_height -| cell_offset.y,
};
}
// Either the columns or the rows were specified, but not both,
// so we need to calculate the other one based on the aspect ratio.
// If only the columns were specified, we determine
// the height of the image based on the aspect ratio.
if (self.columns > 0) {
const calc_width = saturatingMul(
cell_width,
self.columns,
) -| cell_offset.x;
const calc_height = scaleDimension(calc_width, height, width);
return .{
.width = calc_width,
.height = calc_height,
};
}
// Otherwise, only the rows were specified, so we
// determine the width based on the aspect ratio.
{
const calc_height = saturatingMul(
cell_height,
self.rows,
) -| cell_offset.y;
const calc_width = scaleDimension(calc_height, width, height);
return .{
.width = calc_width,
.height = calc_height,
};
}
}
/// Returns the size in grid cells that this placement takes up.
pub fn gridSize(
self: Placement,
image: Image,
t: *const terminal.Terminal,
) struct {
cols: u32,
rows: u32,
} {
// If we have a specified columns and rows then this is trivial.
if (self.columns > 0 and self.rows > 0) return .{
.cols = self.columns,
.rows = self.rows,
};
// Otherwise we calculate the pixel size, divide by
// cell size, and round up to the nearest integer.
const calc_size = self.pixelSize(image, t);
const cell_offset = self.cellOffset(t);
return .{
.cols = std.math.divCeil(
u32,
calc_size.width +| cell_offset.x,
t.width_px / t.cols,
) catch 0,
.rows = std.math.divCeil(
u32,
calc_size.height +| cell_offset.y,
t.height_px / t.rows,
) catch 0,
};
// NOTE: Above `divCeil`s can only fail if the cell size is 0,
// in such a case it seems safe to return 0 for this.
}
/// Permanently clip `count` rows off the top of this placement,
/// which currently spans `span` grid rows, by shrinking the
/// source rectangle (the requested source rect is materialized
/// into explicit values in the process). Used when a scroll
/// within margins pushes the placement past the top margin.
/// Requires 0 < count < span. Returns false if no visible
/// source pixels would remain, in which case the placement is
/// unmodified and should be deleted.
fn clipTop(
self: *Placement,
image: Image,
t: *const terminal.Terminal,
count: u32,
span: u32,
) bool {
assert(count > 0 and count < span);
const src = self.sourceRect(image);
const crop: u32 = if (self.rows > 0)
// Scaled placement: each grid row shows an equal
// fraction of the source.
@intCast(@as(u64, src.height) * count / span)
else
// Native size: each grid row shows one cell height of
// source pixels (kitty does the same).
saturatingMul(t.height_px / t.rows, count);
if (crop >= src.height) return false;
self.source_x = src.x;
self.source_y = src.y + crop;
self.source_width = src.width;
self.source_height = src.height - crop;
if (self.rows > 0) self.rows -= count;
return true;
}
/// clipTop, but clipping `count` rows off the bottom.
fn clipBottom(
self: *Placement,
image: Image,
t: *const terminal.Terminal,
count: u32,
span: u32,
) bool {
assert(count > 0 and count < span);
const src = self.sourceRect(image);
// An empty source can never be clipped smaller. This also
// guarantees we never store an explicit zero source height,
// which would be reinterpreted as "full image".
if (src.height == 0) return false;
if (self.rows > 0) {
const crop: u32 = @intCast(@as(u64, src.height) * count / span);
if (crop >= src.height) return false;
self.source_height = src.height - crop;
self.rows -= count;
} else {
// The remaining rows can show this many source pixels,
// with the first cell losing cellOffset pixels to the
// placement's y offset.
const visible = saturatingMul(t.height_px / t.rows, span - count);
const offset = self.cellOffset(t).y;
if (visible <= offset) return false;
self.source_height = @min(src.height, visible - offset);
}
self.source_x = src.x;
self.source_y = src.y;
self.source_width = src.width;
return true;
}
/// Returns a selection of the entire rectangle this placement
/// occupies within the screen. This can return null for a virtual
/// placement or when unavailable pixel geometry makes it empty.
///
/// Relative placements also return null: they have no screen
/// position of their own, so geometric queries (delete by
/// intersection, row, column, etc.) never match them directly.
/// They are instead removed when their parent chain is removed.
pub fn rect(
self: Placement,
image: Image,
t: *const terminal.Terminal,
) ?Rect {
const grid_size = self.gridSize(image, t);
const pin = switch (self.location) {
.pin => |p| p,
.virtual, .relative => return null,
};
if (pin.garbage) return null;
// A zero pixel-sized placement can produce a zero grid size when
// pixel geometry is unavailable. It occupies no rectangle.
if (grid_size.cols == 0 or grid_size.rows == 0) return null;
var br = switch (pin.downOverflow(grid_size.rows - 1)) {
.offset => |v| v,
.overflow => |v| v.end,
};
br.x = @intCast(@min(
// We need to sub one here because the x value is
// one width already. So if the image is width "1"
// then we add zero to X because X itself is width 1.
@as(u32, pin.x) +| (grid_size.cols - 1),
@as(u32, t.cols) - 1,
));
return .{
.top_left = pin.*,
.bottom_right = br,
};
}
};
};
// Our pin for the placement
fn trackPin(
t: *terminal.Terminal,
pt: point.Coordinate,
) !*PageList.Pin {
return try t.screens.active.pages.trackPin(t.screens.active.pages.pin(.{
.active = pt,
}).?);
}
test "storage: add placement with zero placement id" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .cols = 100, .rows = 100 });
defer t.deinit(alloc);
t.width_px = 100;
t.height_px = 100;
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
try s.addImage(io, alloc, t.screens.active, .{ .id = 1, .width = 50, .height = 50 });
try s.addImage(io, alloc, t.screens.active, .{ .id = 2, .width = 25, .height = 25 });
try s.addPlacement(io, alloc, t.screens.active, 1, 0, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 25, .y = 25 }) } });
try s.addPlacement(io, alloc, t.screens.active, 1, 0, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 25, .y = 25 }) } });
try testing.expectEqual(@as(usize, 2), s.placements.count());
try testing.expectEqual(@as(usize, 2), s.images.count());
// verify the placement is what we expect
try testing.expect(s.placements.get(.{
.image_id = 1,
.placement_id = .{ .tag = .internal, .id = 0 },
}) != null);
try testing.expect(s.placements.get(.{
.image_id = 1,
.placement_id = .{ .tag = .internal, .id = 1 },
}) != null);
}
test "storage: replacing placement releases tracked pin" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .cols = 3, .rows = 3 });
defer t.deinit(alloc);
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
try s.addImage(io, alloc, t.screens.active, .{ .id = 1 });
const tracked = t.screens.active.pages.countTrackedPins();
try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{
.location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) },
});
try testing.expectEqual(
tracked + 1,
t.screens.active.pages.countTrackedPins(),
);
const replacement_pin = try trackPin(&t, .{ .x = 2, .y = 2 });
try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{
.location = .{ .pin = replacement_pin },
});
try testing.expectEqual(@as(usize, 1), s.placements.count());
try testing.expectEqual(
tracked + 1,
t.screens.active.pages.countTrackedPins(),
);
try testing.expectEqual(
replacement_pin,
s.placements.get(.{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 1 },
}).?.location.pin,
);
}
test "storage: adding placement reclaims garbage placements" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .cols = 3, .rows = 3 });
defer t.deinit(alloc);
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
try s.addImage(io, alloc, t.screens.active, .{ .id = 1 });
const tracked = t.screens.active.pages.countTrackedPins();
const old_pin = try trackPin(&t, .{ .x = 0, .y = 0 });
try s.addPlacement(io, alloc, t.screens.active, 1, 0, .{
.location = .{ .pin = old_pin },
});
old_pin.garbage = true;
const new_pin = try trackPin(&t, .{ .x = 1, .y = 1 });
try s.addPlacement(io, alloc, t.screens.active, 1, 0, .{
.location = .{ .pin = new_pin },
});
try testing.expectEqual(@as(usize, 1), s.placements.count());
try testing.expectEqual(
tracked + 1,
t.screens.active.pages.countTrackedPins(),
);
try testing.expectEqual(
new_pin,
s.placements.get(.{
.image_id = 1,
.placement_id = .{ .tag = .internal, .id = 1 },
}).?.location.pin,
);
}
test "storage: placement count limit permits replacement" {
const testing = std.testing;
const io = testing.io;
const alloc = testing.allocator;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 });
defer t.deinit(alloc);
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
try s.addImage(io, alloc, t.screens.active, .{ .id = 1 });
try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .virtual = {} } });
const img = s.images.getPtr(1).?;
img.metadata.placement_count = std.math.maxInt(@TypeOf(img.metadata.placement_count));
try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .virtual = {} } });
try testing.expectError(
error.OutOfMemory,
s.addPlacement(io, alloc, t.screens.active, 1, 2, .{ .location = .{ .virtual = {} } }),
);
try testing.expectEqual(@as(usize, 1), s.placements.count());
}
test "storage: delete all visible placements and matching images" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 });
defer t.deinit(alloc);
const tracked = t.screens.active.pages.countTrackedPins();
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
try s.addImage(io, alloc, t.screens.active, .{ .id = 1 });
try s.addImage(io, alloc, t.screens.active, .{ .id = 2 });
try s.addImage(io, alloc, t.screens.active, .{ .id = 3 });
try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } });
try s.addPlacement(io, alloc, t.screens.active, 2, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } });
s.dirty = false;
s.delete(io, alloc, &t, .{ .all = true });
try testing.expect(s.dirty);
try testing.expectEqual(@as(usize, 1), s.images.count());
try testing.expect(s.images.contains(3));
try testing.expectEqual(@as(usize, 0), s.placements.count());
try testing.expectEqual(tracked, t.screens.active.pages.countTrackedPins());
}
test "storage: delete all placements and images preserves limit" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 });
defer t.deinit(alloc);
const tracked = t.screens.active.pages.countTrackedPins();
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
s.total_limit = 5000;
try s.addImage(io, alloc, t.screens.active, .{ .id = 1 });
try s.addImage(io, alloc, t.screens.active, .{ .id = 2 });
try s.addImage(io, alloc, t.screens.active, .{ .id = 3 });
try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } });
try s.addPlacement(io, alloc, t.screens.active, 2, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } });
s.dirty = false;
s.delete(io, alloc, &t, .{ .all = true });
try testing.expect(s.dirty);
try testing.expectEqual(@as(usize, 1), s.images.count());
try testing.expect(s.images.contains(3));
try testing.expectEqual(@as(usize, 0), s.placements.count());
try testing.expectEqual(@as(usize, 5000), s.total_limit);
try testing.expectEqual(tracked, t.screens.active.pages.countTrackedPins());
}
test "storage: delete all visible placements preserves scrollback" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{
.rows = 2,
.cols = 2,
.max_scrollback_bytes = 4096,
});
defer t.deinit(alloc);
t.width_px = 2;
t.height_px = 2;
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
try s.addImage(io, alloc, t.screens.active, .{ .id = 1, .width = 1, .height = 1 });
try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{
.location = .{ .pin = try trackPin(&t, .{ .y = 0 }) },
});
try t.scrollUp(1);
const history_key: ImageStorage.PlacementKey = .{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 1 },
};
try testing.expect(t.screens.active.pages.pointFromPin(
.active,
s.placements.get(history_key).?.location.pin.*,
) == null);
try s.addPlacement(io, alloc, t.screens.active, 1, 2, .{
.location = .{ .pin = try trackPin(&t, .{ .y = 0 }) },
});
s.delete(io, alloc, &t, .{ .all = true });
try testing.expectEqual(@as(usize, 1), s.placements.count());
try testing.expect(s.placements.contains(history_key));
try testing.expectEqual(@as(usize, 1), s.images.count());
try testing.expect(s.images.contains(1));
}
test "storage: delete all includes placements spanning into active area" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{
.rows = 2,
.cols = 2,
.max_scrollback_bytes = 4096,
});
defer t.deinit(alloc);
t.width_px = 2;
t.height_px = 2;
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
try s.addImage(io, alloc, t.screens.active, .{
.id = 1,
.width = 1,
.height = 2,
});
try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{
.location = .{ .pin = try trackPin(&t, .{ .y = 0 }) },
});
try t.scrollUp(1);
const placement = s.placements.get(.{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 1 },
}).?;
try testing.expect(t.screens.active.pages.pointFromPin(
.active,
placement.location.pin.*,
) == null);
const rect = placement.rect(s.imageById(1).?, &t).?;
try testing.expect(t.screens.active.pages.pointFromPin(
.active,
rect.bottom_right,
) != null);
s.delete(io, alloc, &t, .{ .all = true });
try testing.expectEqual(@as(usize, 0), s.placements.count());
try testing.expectEqual(@as(usize, 0), s.images.count());
}
test "storage: delete all placements" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 });
defer t.deinit(alloc);
const tracked = t.screens.active.pages.countTrackedPins();
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
try s.addImage(io, alloc, t.screens.active, .{ .id = 1 });
try s.addImage(io, alloc, t.screens.active, .{ .id = 2 });
try s.addImage(io, alloc, t.screens.active, .{ .id = 3 });
try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } });
try s.addPlacement(io, alloc, t.screens.active, 2, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } });
s.dirty = false;
s.delete(io, alloc, &t, .{ .all = false });
try testing.expect(s.dirty);
try testing.expectEqual(@as(usize, 0), s.placements.count());
try testing.expectEqual(@as(usize, 3), s.images.count());
try testing.expectEqual(tracked, t.screens.active.pages.countTrackedPins());
}
test "storage: delete all placements by image id" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 });
defer t.deinit(alloc);
const tracked = t.screens.active.pages.countTrackedPins();
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
try s.addImage(io, alloc, t.screens.active, .{ .id = 1 });
try s.addImage(io, alloc, t.screens.active, .{ .id = 2 });
try s.addImage(io, alloc, t.screens.active, .{ .id = 3 });
try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } });
try s.addPlacement(io, alloc, t.screens.active, 2, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } });
s.dirty = false;
s.delete(io, alloc, &t, .{ .id = .{ .image_id = 2 } });
try testing.expect(s.dirty);
try testing.expectEqual(@as(usize, 1), s.placements.count());
try testing.expectEqual(@as(usize, 3), s.images.count());
try testing.expectEqual(tracked + 1, t.screens.active.pages.countTrackedPins());
}
test "storage: delete all placements by image id and unused images" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 });
defer t.deinit(alloc);
const tracked = t.screens.active.pages.countTrackedPins();
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
try s.addImage(io, alloc, t.screens.active, .{ .id = 1 });
try s.addImage(io, alloc, t.screens.active, .{ .id = 2 });
try s.addImage(io, alloc, t.screens.active, .{ .id = 3 });
try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } });
try s.addPlacement(io, alloc, t.screens.active, 2, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } });
s.dirty = false;
s.delete(io, alloc, &t, .{ .id = .{ .delete = true, .image_id = 2 } });
try testing.expect(s.dirty);
try testing.expectEqual(@as(usize, 1), s.placements.count());
try testing.expectEqual(@as(usize, 2), s.images.count());
try testing.expectEqual(tracked + 1, t.screens.active.pages.countTrackedPins());
}
test "storage: delete placement by specific id" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 });
defer t.deinit(alloc);
const tracked = t.screens.active.pages.countTrackedPins();
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
try s.addImage(io, alloc, t.screens.active, .{ .id = 1 });
try s.addImage(io, alloc, t.screens.active, .{ .id = 2 });
try s.addImage(io, alloc, t.screens.active, .{ .id = 3 });
try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } });
try s.addPlacement(io, alloc, t.screens.active, 1, 2, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } });
try s.addPlacement(io, alloc, t.screens.active, 2, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } });
s.dirty = false;
s.delete(io, alloc, &t, .{ .id = .{
.delete = true,
.image_id = 1,
.placement_id = 2,
} });
try testing.expect(s.dirty);
try testing.expectEqual(@as(usize, 2), s.placements.count());
try testing.expectEqual(@as(usize, 3), s.images.count());
try testing.expectEqual(tracked + 2, t.screens.active.pages.countTrackedPins());
}
test "storage: uppercase id delete preserves image when placement does not match" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 });
defer t.deinit(alloc);
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
try s.addImage(io, alloc, t.screens.active, .{ .id = 1 });
s.dirty = false;
const generation = s.generation;
s.delete(io, alloc, &t, .{ .id = .{
.delete = true,
.image_id = 1,
.placement_id = 7,
} });
try testing.expect(s.imageById(1) != null);
try testing.expect(!s.dirty);
try testing.expectEqual(generation, s.generation);
}
test "storage: uppercase id delete frees image after placement matches" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 });
defer t.deinit(alloc);
const tracked = t.screens.active.pages.countTrackedPins();
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
try s.addImage(io, alloc, t.screens.active, .{ .id = 1 });
try s.addPlacement(io, alloc, t.screens.active, 1, 9, .{
.location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) },
});
s.dirty = false;
s.delete(io, alloc, &t, .{ .id = .{
.delete = true,
.image_id = 1,
.placement_id = 9,
} });
try testing.expectEqual(@as(usize, 0), s.placements.count());
try testing.expectEqual(@as(usize, 0), s.images.count());
try testing.expect(s.dirty);
try testing.expectEqual(tracked, t.screens.active.pages.countTrackedPins());
}
test "storage: uppercase id delete without placement frees unplaced image" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 });
defer t.deinit(alloc);
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
try s.addImage(io, alloc, t.screens.active, .{ .id = 1 });
s.dirty = false;
s.delete(io, alloc, &t, .{ .id = .{
.delete = true,
.image_id = 1,
} });
try testing.expectEqual(@as(usize, 0), s.images.count());
try testing.expect(s.dirty);
}
test "storage: delete intersecting cursor" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 100, .cols = 100 });
defer t.deinit(alloc);
t.width_px = 100;
t.height_px = 100;
const tracked = t.screens.active.pages.countTrackedPins();
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
try s.addImage(io, alloc, t.screens.active, .{ .id = 1, .width = 50, .height = 50 });
try s.addImage(io, alloc, t.screens.active, .{ .id = 2, .width = 25, .height = 25 });
try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) } });
try s.addPlacement(io, alloc, t.screens.active, 1, 2, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 25, .y = 25 }) } });
t.screens.active.cursorAbsolute(12, 12);
s.dirty = false;
s.delete(io, alloc, &t, .{ .intersect_cursor = false });
try testing.expect(s.dirty);
try testing.expectEqual(@as(usize, 1), s.placements.count());
try testing.expectEqual(@as(usize, 2), s.images.count());
try testing.expectEqual(tracked + 1, t.screens.active.pages.countTrackedPins());
// verify the placement is what we expect
try testing.expect(s.placements.get(.{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 2 },
}) != null);
}
test "storage: delete intersecting cursor checks interior row column" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 100, .cols = 100 });
defer t.deinit(alloc);
t.width_px = 100;
t.height_px = 100;
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
try s.addImage(io, alloc, t.screens.active, .{ .id = 1, .width = 10, .height = 10 });
try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) } });
try s.addPlacement(io, alloc, t.screens.active, 1, 2, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 20, .y = 0 }) } });
// This is inside the right placement and on an interior row shared by
// both placements, but it is outside the left placement's columns.
t.screens.active.cursorAbsolute(21, 5);
s.delete(io, alloc, &t, .{ .intersect_cursor = false });
try testing.expectEqual(@as(usize, 1), s.placements.count());
try testing.expect(s.placements.get(.{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 1 },
}) != null);
}
test "storage: delete intersecting cell checks interior row column" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 100, .cols = 100 });
defer t.deinit(alloc);
t.width_px = 100;
t.height_px = 100;
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
try s.addImage(io, alloc, t.screens.active, .{ .id = 1, .width = 10, .height = 10 });
try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) } });
try s.addPlacement(io, alloc, t.screens.active, 1, 2, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 20, .y = 0 }) } });
// Protocol coordinates are one-based, so this targets grid cell (21, 5).
s.delete(io, alloc, &t, .{ .intersect_cell = .{
.x = 22,
.y = 6,
} });
try testing.expectEqual(@as(usize, 1), s.placements.count());
try testing.expect(s.placements.get(.{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 1 },
}) != null);
}
test "storage: delete intersecting cursor plus unused" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 100, .cols = 100 });
defer t.deinit(alloc);
t.width_px = 100;
t.height_px = 100;
const tracked = t.screens.active.pages.countTrackedPins();
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
try s.addImage(io, alloc, t.screens.active, .{ .id = 1, .width = 50, .height = 50 });
try s.addImage(io, alloc, t.screens.active, .{ .id = 2, .width = 25, .height = 25 });
try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) } });
try s.addPlacement(io, alloc, t.screens.active, 1, 2, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 25, .y = 25 }) } });
t.screens.active.cursorAbsolute(12, 12);
s.dirty = false;
s.delete(io, alloc, &t, .{ .intersect_cursor = true });
try testing.expect(s.dirty);
try testing.expectEqual(@as(usize, 1), s.placements.count());
try testing.expectEqual(@as(usize, 2), s.images.count());
try testing.expectEqual(tracked + 1, t.screens.active.pages.countTrackedPins());
// verify the placement is what we expect
try testing.expect(s.placements.get(.{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 2 },
}) != null);
}
test "storage: delete intersecting cursor hits multiple" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 100, .cols = 100 });
defer t.deinit(alloc);
t.width_px = 100;
t.height_px = 100;
const tracked = t.screens.active.pages.countTrackedPins();
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
try s.addImage(io, alloc, t.screens.active, .{ .id = 1, .width = 50, .height = 50 });
try s.addImage(io, alloc, t.screens.active, .{ .id = 2, .width = 25, .height = 25 });
try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) } });
try s.addPlacement(io, alloc, t.screens.active, 1, 2, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 25, .y = 25 }) } });
t.screens.active.cursorAbsolute(26, 26);
s.dirty = false;
s.delete(io, alloc, &t, .{ .intersect_cursor = true });
try testing.expect(s.dirty);
try testing.expectEqual(@as(usize, 0), s.placements.count());
try testing.expectEqual(@as(usize, 1), s.images.count());
try testing.expectEqual(tracked, t.screens.active.pages.countTrackedPins());
}
test "storage: delete by column" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 100, .cols = 100 });
defer t.deinit(alloc);
t.width_px = 100;
t.height_px = 100;
const tracked = t.screens.active.pages.countTrackedPins();
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
try s.addImage(io, alloc, t.screens.active, .{ .id = 1, .width = 50, .height = 50 });
try s.addImage(io, alloc, t.screens.active, .{ .id = 2, .width = 25, .height = 25 });
try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) } });
try s.addPlacement(io, alloc, t.screens.active, 1, 2, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 25, .y = 25 }) } });
s.dirty = false;
s.delete(io, alloc, &t, .{ .column = .{
.delete = false,
.x = 60,
} });
try testing.expect(s.dirty);
try testing.expectEqual(@as(usize, 1), s.placements.count());
try testing.expectEqual(@as(usize, 2), s.images.count());
try testing.expectEqual(tracked + 1, t.screens.active.pages.countTrackedPins());
// verify the placement is what we expect
try testing.expect(s.placements.get(.{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 1 },
}) != null);
}
test "storage: delete by column 1x1" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 100, .cols = 100 });
defer t.deinit(alloc);
t.width_px = 100;
t.height_px = 100;
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
try s.addImage(io, alloc, t.screens.active, .{ .id = 1, .width = 1, .height = 1 });
try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) } });
try s.addPlacement(io, alloc, t.screens.active, 1, 2, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 0 }) } });
try s.addPlacement(io, alloc, t.screens.active, 1, 3, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 2, .y = 0 }) } });
s.delete(io, alloc, &t, .{ .column = .{
.delete = false,
.x = 2,
} });
try testing.expectEqual(@as(usize, 2), s.placements.count());
try testing.expectEqual(@as(usize, 1), s.images.count());
// verify the placement is what we expect
try testing.expect(s.placements.get(.{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 1 },
}) != null);
try testing.expect(s.placements.get(.{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 3 },
}) != null);
}
test "storage: delete by row" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 100, .cols = 100 });
defer t.deinit(alloc);
t.width_px = 100;
t.height_px = 100;
const tracked = t.screens.active.pages.countTrackedPins();
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
try s.addImage(io, alloc, t.screens.active, .{ .id = 1, .width = 50, .height = 50 });
try s.addImage(io, alloc, t.screens.active, .{ .id = 2, .width = 25, .height = 25 });
try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) } });
try s.addPlacement(io, alloc, t.screens.active, 1, 2, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 25, .y = 25 }) } });
s.dirty = false;
s.delete(io, alloc, &t, .{ .row = .{
.delete = false,
.y = 60,
} });
try testing.expect(s.dirty);
try testing.expectEqual(@as(usize, 1), s.placements.count());
try testing.expectEqual(@as(usize, 2), s.images.count());
try testing.expectEqual(tracked + 1, t.screens.active.pages.countTrackedPins());
// verify the placement is what we expect
try testing.expect(s.placements.get(.{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 1 },
}) != null);
}
test "storage: delete by row 1x1" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 100, .cols = 100 });
defer t.deinit(alloc);
t.width_px = 100;
t.height_px = 100;
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
try s.addImage(io, alloc, t.screens.active, .{ .id = 1, .width = 1, .height = 1 });
try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .y = 0 }) } });
try s.addPlacement(io, alloc, t.screens.active, 1, 2, .{ .location = .{ .pin = try trackPin(&t, .{ .y = 1 }) } });
try s.addPlacement(io, alloc, t.screens.active, 1, 3, .{ .location = .{ .pin = try trackPin(&t, .{ .y = 2 }) } });
s.delete(io, alloc, &t, .{ .row = .{
.delete = false,
.y = 2,
} });
try testing.expectEqual(@as(usize, 2), s.placements.count());
try testing.expectEqual(@as(usize, 1), s.images.count());
// verify the placement is what we expect
try testing.expect(s.placements.get(.{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 1 },
}) != null);
try testing.expect(s.placements.get(.{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 3 },
}) != null);
}
test "storage: delete images by range 1" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 });
defer t.deinit(alloc);
const tracked = t.screens.active.pages.countTrackedPins();
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
try s.addImage(io, alloc, t.screens.active, .{ .id = 1 });
try s.addImage(io, alloc, t.screens.active, .{ .id = 2 });
try s.addImage(io, alloc, t.screens.active, .{ .id = 3 });
try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } });
try s.addPlacement(io, alloc, t.screens.active, 2, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } });
try testing.expectEqual(@as(usize, 3), s.images.count());
try testing.expectEqual(@as(usize, 2), s.placements.count());
s.dirty = false;
s.delete(io, alloc, &t, .{ .range = .{ .delete = false, .first = 1, .last = 2 } });
try testing.expect(s.dirty);
try testing.expectEqual(@as(usize, 3), s.images.count());
try testing.expectEqual(@as(usize, 0), s.placements.count());
try testing.expectEqual(tracked, t.screens.active.pages.countTrackedPins());
}
test "storage: delete images by range 2" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 });
defer t.deinit(alloc);
const tracked = t.screens.active.pages.countTrackedPins();
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
try s.addImage(io, alloc, t.screens.active, .{ .id = 1 });
try s.addImage(io, alloc, t.screens.active, .{ .id = 2 });
try s.addImage(io, alloc, t.screens.active, .{ .id = 3 });
try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } });
try s.addPlacement(io, alloc, t.screens.active, 2, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } });
try testing.expectEqual(@as(usize, 3), s.images.count());
try testing.expectEqual(@as(usize, 2), s.placements.count());
s.dirty = false;
s.delete(io, alloc, &t, .{ .range = .{ .delete = true, .first = 1, .last = 2 } });
try testing.expect(s.dirty);
try testing.expectEqual(@as(usize, 1), s.images.count());
try testing.expectEqual(@as(usize, 0), s.placements.count());
try testing.expectEqual(tracked, t.screens.active.pages.countTrackedPins());
}
test "storage: delete images by range 3" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 });
defer t.deinit(alloc);
const tracked = t.screens.active.pages.countTrackedPins();
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
try s.addImage(io, alloc, t.screens.active, .{ .id = 1 });
try s.addImage(io, alloc, t.screens.active, .{ .id = 2 });
try s.addImage(io, alloc, t.screens.active, .{ .id = 3 });
try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } });
try s.addPlacement(io, alloc, t.screens.active, 2, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } });
try s.addPlacement(io, alloc, t.screens.active, 3, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } });
try testing.expectEqual(@as(usize, 3), s.images.count());
try testing.expectEqual(@as(usize, 3), s.placements.count());
s.dirty = false;
s.delete(io, alloc, &t, .{ .range = .{ .delete = false, .first = 2, .last = 2 } });
try testing.expect(s.dirty);
try testing.expectEqual(@as(usize, 3), s.images.count());
try testing.expectEqual(@as(usize, 2), s.placements.count());
try testing.expect(s.placements.get(.{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 1 },
}) != null);
try testing.expect(s.placements.get(.{
.image_id = 3,
.placement_id = .{ .tag = .external, .id = 1 },
}) != null);
try testing.expectEqual(tracked + 2, t.screens.active.pages.countTrackedPins());
}
test "storage: delete images by range 4" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 });
defer t.deinit(alloc);
const tracked = t.screens.active.pages.countTrackedPins();
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
try s.addImage(io, alloc, t.screens.active, .{ .id = 1 });
try s.addImage(io, alloc, t.screens.active, .{ .id = 2 });
try s.addImage(io, alloc, t.screens.active, .{ .id = 3 });
try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } });
try s.addPlacement(io, alloc, t.screens.active, 2, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } });
try s.addPlacement(io, alloc, t.screens.active, 3, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } });
try testing.expectEqual(@as(usize, 3), s.images.count());
try testing.expectEqual(@as(usize, 3), s.placements.count());
s.dirty = false;
s.delete(io, alloc, &t, .{ .range = .{ .delete = true, .first = 2, .last = 2 } });
try testing.expect(s.dirty);
try testing.expectEqual(@as(usize, 2), s.images.count());
try testing.expectEqual(@as(usize, 2), s.placements.count());
try testing.expect(s.placements.get(.{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 1 },
}) != null);
try testing.expect(s.placements.get(.{
.image_id = 3,
.placement_id = .{ .tag = .external, .id = 1 },
}) != null);
try testing.expectEqual(tracked + 2, t.screens.active.pages.countTrackedPins());
}
test "storage: uppercase range deletes unplaced image data" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 });
defer t.deinit(alloc);
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
try s.addImage(io, alloc, t.screens.active, .{ .id = 1 });
try s.addImage(io, alloc, t.screens.active, .{ .id = 2 });
try s.addImage(io, alloc, t.screens.active, .{ .id = 3 });
s.delete(io, alloc, &t, .{
.range = .{
.delete = true,
// Zero is the default lower bound when x is omitted.
.first = 0,
.last = 2,
},
});
try testing.expectEqual(@as(usize, 1), s.images.count());
try testing.expect(s.images.contains(3));
}
test "storage: delete images by empty range" {
// Ranges that select nothing are a silent no-op, matching kitty, which
// performs no validation and filters with `x <= image_id <= y`.
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 });
defer t.deinit(alloc);
const tracked = t.screens.active.pages.countTrackedPins();
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
try s.addImage(io, alloc, t.screens.active, .{ .id = 1 });
try s.addImage(io, alloc, t.screens.active, .{ .id = 2 });
try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } });
try s.addPlacement(io, alloc, t.screens.active, 2, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } });
// Inverted range.
s.delete(io, alloc, &t, .{ .range = .{ .delete = true, .first = 5, .last = 4 } });
try testing.expectEqual(@as(usize, 2), s.images.count());
try testing.expectEqual(@as(usize, 2), s.placements.count());
// Upper bound omitted, so it defaults to zero.
s.delete(io, alloc, &t, .{ .range = .{ .delete = true, .first = 5, .last = 0 } });
try testing.expectEqual(@as(usize, 2), s.images.count());
try testing.expectEqual(@as(usize, 2), s.placements.count());
// Both bounds omitted. Image IDs are never zero so this matches nothing.
s.delete(io, alloc, &t, .{ .range = .{ .delete = true, .first = 0, .last = 0 } });
try testing.expectEqual(@as(usize, 2), s.images.count());
try testing.expectEqual(@as(usize, 2), s.placements.count());
// Both placements survive, so both of their pins are still tracked.
try testing.expectEqual(tracked + 2, t.screens.active.pages.countTrackedPins());
}
test "storage: erase display preserves scrollback and reclaims unplaced images" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{
.rows = 2,
.cols = 2,
.max_scrollback_bytes = 4096,
});
defer t.deinit(alloc);
t.width_px = 2;
t.height_px = 2;
const s = &t.screens.active.kitty_images;
try s.addImage(io, alloc, t.screens.active, .{ .id = 1, .width = 1, .height = 1 });
try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{
.location = .{ .pin = try trackPin(&t, .{ .y = 0 }) },
});
try t.scrollUp(1);
const history_key: ImageStorage.PlacementKey = .{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 1 },
};
try testing.expect(t.screens.active.pages.pointFromPin(
.active,
s.placements.get(history_key).?.location.pin.*,
) == null);
try s.addImage(io, alloc, t.screens.active, .{ .id = 2, .width = 1, .height = 1 });
try s.addPlacement(io, alloc, t.screens.active, 2, 1, .{
.location = .{ .pin = try trackPin(&t, .{ .y = 0 }) },
});
try s.addImage(io, alloc, t.screens.active, .{ .id = 3, .width = 1, .height = 1 });
t.eraseDisplay(.complete, false);
try testing.expectEqual(@as(usize, 1), s.placements.count());
try testing.expect(s.placements.contains(history_key));
try testing.expectEqual(@as(usize, 1), s.images.count());
try testing.expect(s.images.contains(1));
try testing.expect(!s.images.contains(2));
try testing.expect(!s.images.contains(3));
}
test "storage: cell offsets stay within explicit destination rectangle" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .cols = 5, .rows = 5 });
defer t.deinit(alloc);
t.width_px = 50; // 10 px per col
t.height_px = 100; // 20 px per row
// Explicit columns and rows describe the far cell boundaries. Offsets
// move the near edge inward without moving those far edges.
const placement: ImageStorage.Placement = .{
.location = .{ .virtual = {} },
.x_offset = 3,
.y_offset = 4,
.columns = 2,
.rows = 1,
};
const actual = placement.pixelSize(.{ .width = 4, .height = 3 }, &t);
try testing.expectEqual(@as(u32, 17), actual.width);
try testing.expectEqual(@as(u32, 16), actual.height);
try testing.expectEqual(@as(u32, 2), placement.gridSize(.{ .width = 4, .height = 3 }, &t).cols);
try testing.expectEqual(@as(u32, 1), placement.gridSize(.{ .width = 4, .height = 3 }, &t).rows);
}
test "storage: cell offsets clamp to cell bounds" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .cols = 5, .rows = 5 });
defer t.deinit(alloc);
t.width_px = 50; // 10 px per col
t.height_px = 100; // 20 px per row
const placement: ImageStorage.Placement = .{
.location = .{ .virtual = {} },
.x_offset = std.math.maxInt(u32),
.y_offset = std.math.maxInt(u32),
.columns = 1,
.rows = 1,
};
const offset = placement.cellOffset(&t);
try testing.expectEqual(@as(u32, 9), offset.x);
try testing.expectEqual(@as(u32, 19), offset.y);
// Even hostile offsets leave one pixel inside the requested cell.
const actual = placement.pixelSize(.{ .width = 1, .height = 1 }, &t);
try testing.expectEqual(@as(u32, 1), actual.width);
try testing.expectEqual(@as(u32, 1), actual.height);
}
test "storage: aspect ratio calculation when only columns or rows specified" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .cols = 100, .rows = 100 });
defer t.deinit(alloc);
t.width_px = 1000; // 10 px per col
t.height_px = 2000; // 20 px per row
// Case 1: Only columns specified
{
const image = Image{ .id = 1, .width = 16, .height = 9 };
var placement = ImageStorage.Placement{
.location = .{ .virtual = {} },
.columns = 10,
.rows = 0,
};
// Image is 16x9, set to a width of 10 columns, at 10px per column
// that's 100px width. 100px * (9 / 16) = 56.25, which should round
// to a height of 56px.
const calc_size = placement.pixelSize(image, &t);
try testing.expectEqual(@as(u32, 100), calc_size.width);
try testing.expectEqual(@as(u32, 56), calc_size.height);
}
// Case 2: Only rows specified
{
const image = Image{ .id = 2, .width = 16, .height = 9 };
var placement = ImageStorage.Placement{
.location = .{ .virtual = {} },
.columns = 0,
.rows = 5,
};
// Image is 16x9, set to a height of 5 rows, at 20px per row that's
// 100px height. 100px * (16 / 9) = 177.77..., which should round to
// a width of 178px.
const calc_size = placement.pixelSize(image, &t);
try testing.expectEqual(@as(u32, 178), calc_size.width);
try testing.expectEqual(@as(u32, 100), calc_size.height);
}
}
test "storage: default source rectangle is intersected before sizing" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .cols = 5, .rows = 5 });
defer t.deinit(alloc);
const placement: ImageStorage.Placement = .{
.location = .{ .virtual = {} },
.source_x = 3,
.source_y = 1,
};
const actual = placement.pixelSize(.{ .width = 4, .height = 3 }, &t);
try testing.expectEqual(@as(u32, 1), actual.width);
try testing.expectEqual(@as(u32, 2), actual.height);
}
test "storage: explicit source rectangle is intersected before sizing" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .cols = 10, .rows = 10 });
defer t.deinit(alloc);
t.width_px = 100;
t.height_px = 100;
// The requested 8x8 rectangle intersects this 10x10 image as 2x3.
// With a two-column destination, the clipped 2:3 aspect ratio produces
// a 20x30 pixel destination.
const placement: ImageStorage.Placement = .{
.location = .{ .virtual = {} },
.source_x = 8,
.source_y = 7,
.source_width = 8,
.source_height = 8,
.columns = 2,
};
const actual = placement.pixelSize(.{ .width = 10, .height = 10 }, &t);
try testing.expectEqual(@as(u32, 20), actual.width);
try testing.expectEqual(@as(u32, 30), actual.height);
}
test "storage: placement geometry handles untrusted dimensions" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
const max = std.math.maxInt(u32);
var t = try terminal.Terminal.init(io, alloc, .{ .cols = 2, .rows = 2 });
defer t.deinit(alloc);
t.width_px = max;
t.height_px = max;
// Cell dimensions multiplied by protocol-controlled row and column
// counts saturate instead of panicking or wrapping.
{
const placement: ImageStorage.Placement = .{
.location = .{ .virtual = {} },
.columns = 3,
.rows = 3,
};
const actual = placement.pixelSize(.{ .width = 1, .height = 1 }, &t);
try testing.expectEqual(max, actual.width);
try testing.expectEqual(max, actual.height);
}
// Aspect-ratio scaling also saturates when the derived dimension does
// not fit in the public u32 geometry type.
{
const placement: ImageStorage.Placement = .{
.location = .{ .virtual = {} },
.columns = 3,
.source_height = max,
};
const actual = placement.pixelSize(.{ .width = 1, .height = 1 }, &t);
try testing.expectEqual(max, actual.width);
try testing.expectEqual(max, actual.height);
}
{
const placement: ImageStorage.Placement = .{
.location = .{ .virtual = {} },
.rows = 3,
.source_width = max,
};
const actual = placement.pixelSize(.{ .width = 1, .height = 1 }, &t);
try testing.expectEqual(max, actual.width);
try testing.expectEqual(max, actual.height);
}
// Pixel offsets are protocol-controlled too. Clamp them to the cell
// before including them in grid geometry.
t.width_px = 2;
t.height_px = 2;
{
const placement: ImageStorage.Placement = .{
.location = .{ .virtual = {} },
.x_offset = max,
.y_offset = max,
};
const actual = placement.gridSize(.{ .width = 1, .height = 1 }, &t);
try testing.expectEqual(@as(u32, 1), actual.cols);
try testing.expectEqual(@as(u32, 1), actual.rows);
}
const pin = try trackPin(&t, .{ .x = 0, .y = 0 });
defer t.screens.active.pages.untrackPin(pin);
// Explicit maximum dimensions must clamp the rectangle to the terminal
// without overflowing its horizontal extent.
{
const placement: ImageStorage.Placement = .{
.location = .{ .pin = pin },
.columns = max,
.rows = 1,
};
const rect = placement.rect(.{ .width = 1, .height = 1 }, &t).?;
try testing.expectEqual(@as(size.CellCountInt, 1), rect.bottom_right.x);
}
// A garbage pin represents content that has been pruned from retained
// history. Its fallback location must not make the placement visible.
pin.garbage = true;
{
const placement: ImageStorage.Placement = .{
.location = .{ .pin = pin },
.columns = 1,
.rows = 1,
};
try testing.expect(placement.rect(.{ .width = 1, .height = 1 }, &t) == null);
}
pin.garbage = false;
// Terminals can temporarily have no pixel geometry. A placement whose
// computed grid is empty has no rectangle, so rect must not subtract one
// from a zero row count.
t.width_px = 0;
t.height_px = 0;
{
const placement: ImageStorage.Placement = .{
.location = .{ .pin = pin },
.columns = 1,
};
const actual = placement.gridSize(.{ .width = 1, .height = 1 }, &t);
try testing.expectEqual(@as(u32, 0), actual.cols);
try testing.expectEqual(@as(u32, 0), actual.rows);
try testing.expect(placement.rect(.{ .width = 1, .height = 1 }, &t) == null);
}
}
test "storage: generation stamps on image add and replace" {
const testing = std.testing;
const io = testing.io;
const alloc = testing.allocator;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 });
defer t.deinit(alloc);
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
// Fresh storage has generation zero (never mutated).
try testing.expectEqual(@as(u64, 0), s.generation);
try s.addImage(io, alloc, t.screens.active, .{ .id = 1, .width = 1, .height = 1 });
const gen1 = s.generation;
try testing.expect(gen1 > 0);
const img1 = s.imageById(1).?;
try testing.expectEqual(gen1, img1.generation);
// A second image gets a strictly greater stamp.
try s.addImage(io, alloc, t.screens.active, .{ .id = 2, .width = 1, .height = 1 });
const gen2 = s.generation;
try testing.expect(gen2 > gen1);
try testing.expectEqual(gen2, s.imageById(2).?.generation);
// Retransmitting the same image ID (identical dimensions) gets a
// fresh stamp: this is what makes same-sized retransmissions
// detectable by renderers.
try s.addImage(io, alloc, t.screens.active, .{ .id = 1, .width = 1, .height = 1 });
const gen3 = s.generation;
try testing.expect(gen3 > gen2);
try testing.expectEqual(gen3, s.imageById(1).?.generation);
// Image 2 kept its stamp.
try testing.expectEqual(gen2, s.imageById(2).?.generation);
}
test "storage: generation bumps on placement and delete" {
const testing = std.testing;
const io = testing.io;
const alloc = testing.allocator;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 });
defer t.deinit(alloc);
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
try s.addImage(io, alloc, t.screens.active, .{ .id = 1 });
const gen_add = s.generation;
try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } });
const gen_place = s.generation;
try testing.expect(gen_place > gen_add);
// Reads don't change the generation.
_ = s.imageById(1);
_ = s.imageByNumber(1);
try testing.expectEqual(gen_place, s.generation);
s.delete(io, alloc, &t, .{ .all = true });
try testing.expect(s.generation > gen_place);
}
test "storage: generation bumps when setLimit evicts or disables" {
const testing = std.testing;
const io = testing.io;
const alloc = testing.allocator;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 });
defer t.deinit(alloc);
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
const data = try alloc.dupe(u8, "1234");
try s.addImage(io, alloc, t.screens.active, .{
.id = 1,
.width = 1,
.height = 1,
.data = .{ .complete = data },
});
const gen_add = s.generation;
// Lowering the limit evicts the image and must mark a mutation.
s.dirty = false;
s.setLimit(io, alloc, t.screens.active, 1);
try testing.expect(s.dirty);
try testing.expect(s.generation > gen_add);
try testing.expectEqual(@as(usize, 0), s.images.count());
const gen_evict = s.generation;
// Disabling (limit=0) resets the storage and must mark a mutation.
s.dirty = false;
s.setLimit(io, alloc, t.screens.active, 0);
try testing.expect(s.dirty);
try testing.expect(s.generation > gen_evict);
}
test "storage: imageByNumber returns most recently transmitted" {
const testing = std.testing;
const io = testing.io;
const alloc = testing.allocator;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 });
defer t.deinit(alloc);
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
// Two images sharing a number: the newest transmission wins,
// regardless of insertion order or clock resolution.
try s.addImage(io, alloc, t.screens.active, .{ .id = 1, .number = 7 });
try s.addImage(io, alloc, t.screens.active, .{ .id = 2, .number = 7 });
try testing.expectEqual(@as(u32, 2), s.imageByNumber(7).?.id);
// Retransmit the first: it becomes the newest.
try s.addImage(io, alloc, t.screens.active, .{ .id = 1, .number = 7 });
try testing.expectEqual(@as(u32, 1), s.imageByNumber(7).?.id);
}
test "storage: nextGeneration is unique and monotonic" {
const testing = std.testing;
const a = nextGeneration(testing.io);
const b = nextGeneration(testing.io);
try testing.expect(b > a);
try testing.expect(a > 0);
}
test "storage: no-op delete does not mark a mutation" {
const testing = std.testing;
const io = testing.io;
const alloc = testing.allocator;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 });
defer t.deinit(alloc);
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
// A delete-all on an empty storage (this runs on every screen
// clear) must not dirty the state or bump the generation.
s.delete(io, alloc, &t, .{ .all = true });
try testing.expect(!s.dirty);
try testing.expectEqual(@as(u64, 0), s.generation);
// Same for a delete that matches nothing.
try s.addImage(io, alloc, t.screens.active, .{ .id = 1 });
try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } });
const gen = s.generation;
s.dirty = false;
s.delete(io, alloc, &t, .{ .id = .{ .image_id = 42 } });
try testing.expect(!s.dirty);
try testing.expectEqual(gen, s.generation);
// But a delete that removes something does mark a mutation.
s.delete(io, alloc, &t, .{ .id = .{ .image_id = 1 } });
try testing.expect(s.dirty);
try testing.expect(s.generation > gen);
}
test "storage: evicts images in priority order" {
const testing = std.testing;
const io = testing.io;
const alloc = testing.allocator;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 });
defer t.deinit(alloc);
var s: ImageStorage = .{ .total_limit = 192 };
defer s.deinit(alloc, t.screens.active);
try s.addImage(io, alloc, t.screens.active, .{
.id = 1,
.data = .{ .complete = try alloc.dupe(u8, "*" ** 64) },
.metadata = .{ .transient = false },
});
try s.addImage(io, alloc, t.screens.active, .{
.id = 2,
.data = .{ .complete = try alloc.dupe(u8, "*" ** 64) },
.metadata = .{ .transient = true },
});
try s.addImage(io, alloc, t.screens.active, .{
.id = 3,
.data = .{ .complete = try alloc.dupe(u8, "*" ** 64) },
.metadata = .{ .transient = true },
});
try s.addPlacement(
io,
alloc,
t.screens.active,
2,
1,
.{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } },
);
const gen = s.generation;
const result = s.evictImage(io, alloc, t.screens.active, 96);
try testing.expect(s.dirty);
try testing.expect(s.generation > gen);
try testing.expectEqual(true, result);
try testing.expectEqual(1, s.images.count());
try testing.expect(!s.images.contains(1));
try testing.expect(s.images.contains(2));
try testing.expect(!s.images.contains(3));
}
test "storage: eviction releases placement pins" {
const testing = std.testing;
const io = testing.io;
const alloc = testing.allocator;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 });
defer t.deinit(alloc);
var s: ImageStorage = .{ .total_limit = 128 };
defer s.deinit(alloc, t.screens.active);
const tracked = t.screens.active.pages.countTrackedPins();
try s.addImage(io, alloc, t.screens.active, .{
.id = 1,
.data = .{ .complete = try alloc.dupe(u8, "*" ** 64) },
});
try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{
.location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) },
});
try s.addImage(io, alloc, t.screens.active, .{
.id = 2,
.data = .{ .complete = try alloc.dupe(u8, "*" ** 64) },
});
try s.addPlacement(io, alloc, t.screens.active, 2, 1, .{
.location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) },
});
try testing.expectEqual(tracked + 2, t.screens.active.pages.countTrackedPins());
// Adding a third image evicts the oldest equally-ranked image and its
// placement. The newer image's placement and tracked pin remain intact.
try s.addImage(io, alloc, t.screens.active, .{
.id = 3,
.data = .{ .complete = try alloc.dupe(u8, "*" ** 64) },
});
try testing.expect(!s.images.contains(1));
try testing.expect(s.images.contains(2));
try testing.expect(s.images.contains(3));
try testing.expectEqual(@as(usize, 1), s.placements.count());
try testing.expect(s.placements.contains(.{
.image_id = 2,
.placement_id = .{ .tag = .external, .id = 1 },
}));
try testing.expectEqual(tracked + 1, t.screens.active.pages.countTrackedPins());
}
test "storage: pending image completes once and preserves age" {
const testing = std.testing;
const io = testing.io;
const alloc = testing.allocator;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 });
defer t.deinit(alloc);
var s: ImageStorage = .{ .total_limit = 16 };
defer s.deinit(alloc, t.screens.active);
const pending = try s.addPendingImage(io, alloc, t.screens.active, .{
.id = 1,
.number = 7,
.width = 2,
.height = 2,
.format = .rgba,
.data = .{ .pending = 16 },
});
try testing.expectEqual(@as(usize, 16), s.total_bytes);
try testing.expectEqual(@as(u32, 1), s.imageByNumber(7).?.id);
try testing.expect(s.imageById(1).?.data.isPending());
try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{
.location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) },
});
try testing.expectEqual(@as(usize, 1), s.placements.count());
const wrong = try alloc.dupe(u8, "short");
const wrong_completed = pending.complete(&s, io, wrong);
try testing.expect(!wrong_completed);
defer alloc.free(wrong);
const storage_generation = s.generation;
s.dirty = false;
const pixels = try alloc.dupe(u8, "*" ** 16);
try testing.expect(pending.complete(&s, io, pixels));
try testing.expect(s.dirty);
try testing.expect(s.generation > storage_generation);
try testing.expectEqual(pending.generation, s.imageById(1).?.generation);
try testing.expectEqual(@as(usize, 16), s.total_bytes);
try testing.expectEqualSlices(u8, pixels, s.imageById(1).?.data.bytes().?);
const duplicate = try alloc.dupe(u8, "!" ** 16);
const duplicate_completed = pending.complete(&s, io, duplicate);
try testing.expect(!duplicate_completed);
defer alloc.free(duplicate);
}
test "storage: stale pending completion loses to delete replacement and eviction" {
const testing = std.testing;
const io = testing.io;
const alloc = testing.allocator;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 });
defer t.deinit(alloc);
var s: ImageStorage = .{ .total_limit = 8 };
defer s.deinit(alloc, t.screens.active);
const deleted = try s.addPendingImage(io, alloc, t.screens.active, .{
.id = 1,
.data = .{ .pending = 4 },
});
s.delete(io, alloc, &t, .{ .id = .{ .delete = true, .image_id = 1 } });
const deleted_data = try alloc.dupe(u8, "gone");
const deleted_completed = deleted.complete(&s, io, deleted_data);
try testing.expect(!deleted_completed);
defer alloc.free(deleted_data);
const replaced = try s.addPendingImage(io, alloc, t.screens.active, .{
.id = 2,
.data = .{ .pending = 4 },
});
try s.addImage(io, alloc, t.screens.active, .{
.id = 2,
.data = .{ .complete = try alloc.dupe(u8, "live") },
});
const replaced_data = try alloc.dupe(u8, "late");
const replaced_completed = replaced.complete(&s, io, replaced_data);
try testing.expect(!replaced_completed);
defer alloc.free(replaced_data);
try testing.expectEqualStrings("live", s.imageById(2).?.data.bytes().?);
const evicted = try s.addPendingImage(io, alloc, t.screens.active, .{
.id = 3,
.data = .{ .pending = 4 },
});
try s.addImage(io, alloc, t.screens.active, .{
.id = 4,
.data = .{ .complete = try alloc.dupe(u8, "12345678") },
});
try testing.expect(s.imageById(3) == null);
const evicted_data = try alloc.dupe(u8, "late");
const evicted_completed = evicted.complete(&s, io, evicted_data);
try testing.expect(!evicted_completed);
defer alloc.free(evicted_data);
}
test "storage: replacement reuses pending reservation and removes placements" {
const testing = std.testing;
const io = testing.io;
const alloc = testing.allocator;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 });
defer t.deinit(alloc);
var s: ImageStorage = .{ .total_limit = 12 };
defer s.deinit(alloc, t.screens.active);
const tracked = t.screens.active.pages.countTrackedPins();
const pending = try s.addPendingImage(io, alloc, t.screens.active, .{
.id = 1,
.width = 2,
.height = 1,
.format = .rgba,
.data = .{ .pending = 8 },
});
try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{
.location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) },
});
try s.addImage(io, alloc, t.screens.active, .{
.id = 2,
.data = .{ .complete = try alloc.dupe(u8, "keep") },
});
try s.addImage(io, alloc, t.screens.active, .{
.id = 1,
.width = 2,
.height = 1,
.format = .rgba,
.data = .{ .complete = try alloc.dupe(u8, "12345678") },
});
try testing.expect(s.images.contains(1));
try testing.expect(s.images.contains(2));
try testing.expectEqual(@as(usize, 0), s.placements.count());
try testing.expectEqual(tracked, t.screens.active.pages.countTrackedPins());
try testing.expectEqual(
@as(u30, 0),
s.imageById(1).?.metadata.placement_count,
);
try testing.expectEqual(@as(usize, 12), s.total_bytes);
const stale = try alloc.dupe(u8, "snapshot");
const stale_completed = pending.complete(&s, io, stale);
try testing.expect(!stale_completed);
defer alloc.free(stale);
// Growing the replacement requires eviction, but the replacement ID is
// excluded. The other image supplies the needed bytes.
try s.addImage(io, alloc, t.screens.active, .{
.id = 1,
.data = .{ .complete = try alloc.dupe(u8, "1234567890") },
});
try testing.expect(s.images.contains(1));
try testing.expect(!s.images.contains(2));
try testing.expectEqual(@as(usize, 0), s.placements.count());
try testing.expectEqual(@as(usize, 10), s.total_bytes);
s.delete(io, alloc, &t, .{ .id = .{ .delete = true, .image_id = 1 } });
try testing.expectEqual(@as(usize, 0), s.images.count());
try testing.expectEqual(@as(usize, 0), s.placements.count());
}
test "storage: pending images share exact eviction ordering" {
const testing = std.testing;
const io = testing.io;
const alloc = testing.allocator;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 });
defer t.deinit(alloc);
var s: ImageStorage = .{ .total_limit = 192 };
defer s.deinit(alloc, t.screens.active);
_ = try s.addPendingImage(io, alloc, t.screens.active, .{
.id = 1,
.data = .{ .pending = 64 },
.metadata = .{ .transient = false },
});
_ = try s.addPendingImage(io, alloc, t.screens.active, .{
.id = 2,
.data = .{ .pending = 64 },
.metadata = .{ .transient = true },
});
try s.addImage(io, alloc, t.screens.active, .{
.id = 3,
.data = .{ .complete = try alloc.dupe(u8, "*" ** 64) },
.metadata = .{ .transient = true },
});
try s.addPlacement(io, alloc, t.screens.active, 2, 1, .{
.location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) },
});
// ID 3 is transient and unused, so one exact-size eviction is enough.
try testing.expect(s.evictImage(io, alloc, t.screens.active, 64));
try testing.expect(s.images.contains(1));
try testing.expect(s.images.contains(2));
try testing.expect(!s.images.contains(3));
try testing.expectEqual(@as(usize, 128), s.total_bytes);
}
test "storage: nextImageId number matches Kitty get_free_client_id" {
const testing = std.testing;
const io = testing.io;
const alloc = testing.allocator;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 });
defer t.deinit(alloc);
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
// Empty storage returns 1, as does Kitty.
try testing.expectEqual(@as(u32, 1), s.nextImageId(.explicit));
// Contiguous IDs from 1: the next ID after them.
try s.addImage(io, alloc, t.screens.active, .{ .id = 1 });
try s.addImage(io, alloc, t.screens.active, .{ .id = 2 });
try s.addImage(io, alloc, t.screens.active, .{ .id = 3 });
try testing.expectEqual(@as(u32, 4), s.nextImageId(.explicit));
// The first gap is filled, even when higher IDs exist.
try s.addImage(io, alloc, t.screens.active, .{ .id = 100 });
s.delete(io, alloc, &t, .{ .id = .{ .image_id = 2, .delete = true } });
try testing.expectEqual(@as(u32, 2), s.nextImageId(.explicit));
// 1 is reused as soon as it is free.
s.delete(io, alloc, &t, .{ .id = .{ .image_id = 1, .delete = true } });
try testing.expectEqual(@as(u32, 1), s.nextImageId(.explicit));
}
test "storage: nextImageId implicit skips in-use ids and zero" {
const testing = std.testing;
const io = testing.io;
const alloc = testing.allocator;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 });
defer t.deinit(alloc);
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
// A client image already sits on the counter's first two values.
try s.addImage(io, alloc, t.screens.active, .{ .id = 2147483647 });
try s.addImage(io, alloc, t.screens.active, .{ .id = 2147483648 });
try testing.expectEqual(@as(u32, 2147483649), s.nextImageId(.implicit));
try testing.expectEqual(@as(u32, 2147483650), s.nextImageId(.implicit));
// Wrapping skips zero, which means "no ID" protocol-wide.
s.next_image_id = std.math.maxInt(u32);
try testing.expectEqual(std.math.maxInt(u32), s.nextImageId(.implicit));
try testing.expectEqual(@as(u32, 1), s.nextImageId(.implicit));
}
test "storage: scroll margins placement inside region scrolls and clips at top" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .cols = 10, .rows = 6 });
defer t.deinit(alloc);
t.width_px = 100;
t.height_px = 60;
// 2-row image (10x10px cells) inside the region (rows 1-4).
const storage: *ImageStorage = &t.screens.active.kitty_images;
try storage.addImage(io, alloc, t.screens.active, .{
.id = 1,
.width = 10,
.height = 20,
});
try storage.addPlacement(io, alloc, t.screens.active, 1, 1, .{
.location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 2 }) },
});
t.setTopAndBottomMargin(2, 5);
t.setCursorPos(5, 1);
// Entirely inside: moves up with the content.
try t.index();
{
const p = storage.placements.get(.{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 1 },
}).?;
const pt = t.screens.active.pages.pointFromPin(.active, p.location.pin.*).?;
try testing.expectEqual(point.Coordinate{ .x = 0, .y = 1 }, pt.coord());
}
// Would extend above the top margin: anchored at the top margin
// with the top cell row clipped off the source.
try t.index();
{
const p = storage.placements.get(.{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 1 },
}).?;
const pt = t.screens.active.pages.pointFromPin(.active, p.location.pin.*).?;
try testing.expectEqual(point.Coordinate{ .x = 0, .y = 1 }, pt.coord());
try testing.expectEqual(@as(u32, 10), p.source_y);
try testing.expectEqual(@as(u32, 10), p.source_height);
}
// Fully clipped: deleted.
try t.index();
try testing.expectEqual(@as(usize, 0), storage.placements.count());
// The image itself is retained.
try testing.expectEqual(@as(usize, 1), storage.images.count());
}
test "storage: scroll margins placement straddling region does not move" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .cols = 10, .rows = 6 });
defer t.deinit(alloc);
t.width_px = 100;
t.height_px = 60;
// 3-row image anchored at the top of a 2-row region: not entirely
// within the region, so region scrolls must not move it. This
// exercises the scrollback-creating path (region top at row 0).
const storage: *ImageStorage = &t.screens.active.kitty_images;
try storage.addImage(io, alloc, t.screens.active, .{
.id = 1,
.width = 10,
.height = 30,
});
try storage.addPlacement(io, alloc, t.screens.active, 1, 1, .{
.location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) },
});
t.setTopAndBottomMargin(1, 2);
t.setCursorPos(2, 1);
try t.index();
const p = storage.placements.get(.{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 1 },
}).?;
const pt = t.screens.active.pages.pointFromPin(.active, p.location.pin.*).?;
try testing.expectEqual(point.Coordinate{ .x = 0, .y = 0 }, pt.coord());
try testing.expectEqual(@as(u32, 0), p.source_y);
try testing.expectEqual(@as(u32, 0), p.source_height);
}
test "storage: scroll margins placement below region does not move" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .cols = 10, .rows = 6 });
defer t.deinit(alloc);
t.width_px = 100;
t.height_px = 60;
// A "status bar" image below the bottom margin must stay put while
// the region above it scrolls (scrollback-creating path).
const storage: *ImageStorage = &t.screens.active.kitty_images;
try storage.addImage(io, alloc, t.screens.active, .{
.id = 1,
.width = 10,
.height = 10,
});
try storage.addPlacement(io, alloc, t.screens.active, 1, 1, .{
.location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 5 }) },
});
t.setTopAndBottomMargin(1, 4);
t.setCursorPos(4, 1);
try t.index();
try t.index();
const p = storage.placements.get(.{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 1 },
}).?;
const pt = t.screens.active.pages.pointFromPin(.active, p.location.pin.*).?;
try testing.expectEqual(point.Coordinate{ .x = 0, .y = 5 }, pt.coord());
}
test "storage: scroll margins reverse index clips at bottom" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .cols = 10, .rows = 6 });
defer t.deinit(alloc);
t.width_px = 100;
t.height_px = 60;
// 2-row image at the bottom of the region (rows 1-4).
const storage: *ImageStorage = &t.screens.active.kitty_images;
try storage.addImage(io, alloc, t.screens.active, .{
.id = 1,
.width = 10,
.height = 20,
});
try storage.addPlacement(io, alloc, t.screens.active, 1, 1, .{
.location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 3 }) },
});
t.setTopAndBottomMargin(2, 5);
t.setCursorPos(2, 1);
// Moves down, with the bottom cell row clipped off the source.
t.reverseIndex();
{
const p = storage.placements.get(.{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 1 },
}).?;
const pt = t.screens.active.pages.pointFromPin(.active, p.location.pin.*).?;
try testing.expectEqual(point.Coordinate{ .x = 0, .y = 4 }, pt.coord());
try testing.expectEqual(@as(u32, 0), p.source_y);
try testing.expectEqual(@as(u32, 10), p.source_height);
}
// Fully clipped: deleted.
t.reverseIndex();
try testing.expectEqual(@as(usize, 0), storage.placements.count());
}
test "storage: scroll margins scaled placement clips proportionally" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .cols = 10, .rows = 6 });
defer t.deinit(alloc);
t.width_px = 100;
t.height_px = 60;
// A 40px-tall image scaled into 2 rows, inside a 2-row region.
const storage: *ImageStorage = &t.screens.active.kitty_images;
try storage.addImage(io, alloc, t.screens.active, .{
.id = 1,
.width = 10,
.height = 40,
});
try storage.addPlacement(io, alloc, t.screens.active, 1, 1, .{
.location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 1 }) },
.columns = 1,
.rows = 2,
});
t.setTopAndBottomMargin(2, 3);
t.setCursorPos(3, 1);
try t.index();
const p = storage.placements.get(.{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 1 },
}).?;
const pt = t.screens.active.pages.pointFromPin(.active, p.location.pin.*).?;
try testing.expectEqual(point.Coordinate{ .x = 0, .y = 1 }, pt.coord());
try testing.expectEqual(@as(u32, 20), p.source_y);
try testing.expectEqual(@as(u32, 20), p.source_height);
try testing.expectEqual(@as(u32, 1), p.rows);
try testing.expectEqual(@as(u32, 1), p.columns);
}
test "storage: scroll margins left/right margins respect columns" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .cols = 10, .rows = 6 });
defer t.deinit(alloc);
t.width_px = 100;
t.height_px = 60;
// One placement outside the left/right margin columns, one inside.
const storage: *ImageStorage = &t.screens.active.kitty_images;
try storage.addImage(io, alloc, t.screens.active, .{
.id = 1,
.width = 10,
.height = 10,
});
try storage.addPlacement(io, alloc, t.screens.active, 1, 1, .{
.location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 2 }) },
});
try storage.addPlacement(io, alloc, t.screens.active, 1, 2, .{
.location = .{ .pin = try trackPin(&t, .{ .x = 2, .y = 2 }) },
});
t.modes.set(.enable_left_and_right_margin, true);
t.setLeftAndRightMargin(2, 8);
try t.scrollUp(1);
// Outside the margin columns: stays.
{
const p = storage.placements.get(.{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 1 },
}).?;
const pt = t.screens.active.pages.pointFromPin(.active, p.location.pin.*).?;
try testing.expectEqual(point.Coordinate{ .x = 0, .y = 2 }, pt.coord());
}
// Inside: moves up.
{
const p = storage.placements.get(.{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 2 },
}).?;
const pt = t.screens.active.pages.pointFromPin(.active, p.location.pin.*).?;
try testing.expectEqual(point.Coordinate{ .x = 2, .y = 1 }, pt.coord());
}
}
test "storage: scroll margins insert/delete lines do not move placements" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .cols = 10, .rows = 6 });
defer t.deinit(alloc);
t.width_px = 100;
t.height_px = 60;
// Kitty does not scroll images for IL/DL, only for IND/RI/SU/SD.
const storage: *ImageStorage = &t.screens.active.kitty_images;
try storage.addImage(io, alloc, t.screens.active, .{
.id = 1,
.width = 10,
.height = 10,
});
try storage.addPlacement(io, alloc, t.screens.active, 1, 1, .{
.location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 2 }) },
});
t.setTopAndBottomMargin(2, 5);
t.setCursorPos(2, 1);
t.deleteLines(1);
{
const p = storage.placements.get(.{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 1 },
}).?;
const pt = t.screens.active.pages.pointFromPin(.active, p.location.pin.*).?;
try testing.expectEqual(point.Coordinate{ .x = 0, .y = 2 }, pt.coord());
}
t.insertLines(1);
{
const p = storage.placements.get(.{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 1 },
}).?;
const pt = t.screens.active.pages.pointFromPin(.active, p.location.pin.*).?;
try testing.expectEqual(point.Coordinate{ .x = 0, .y = 2 }, pt.coord());
}
}
test "storage: scroll without margins moves placement into scrollback" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .cols = 10, .rows = 6 });
defer t.deinit(alloc);
t.width_px = 100;
t.height_px = 60;
// No margins: the placement follows its anchored row into the
// scrollback via pin tracking (kitty's marginless behavior).
const storage: *ImageStorage = &t.screens.active.kitty_images;
try storage.addImage(io, alloc, t.screens.active, .{
.id = 1,
.width = 10,
.height = 20,
});
try storage.addPlacement(io, alloc, t.screens.active, 1, 1, .{
.location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) },
});
t.setCursorPos(6, 1);
try t.index();
const p = storage.placements.get(.{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 1 },
}).?;
try testing.expect(t.screens.active.pages.pointFromPin(
.active,
p.location.pin.*,
) == null);
const pt = t.screens.active.pages.pointFromPin(.screen, p.location.pin.*).?;
try testing.expectEqual(point.Coordinate{ .x = 0, .y = 0 }, pt.coord());
}
test "storage: scroll margins large scroll deletes inside placement" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .cols = 10, .rows = 6 });
defer t.deinit(alloc);
t.width_px = 100;
t.height_px = 60;
const storage: *ImageStorage = &t.screens.active.kitty_images;
try storage.addImage(io, alloc, t.screens.active, .{
.id = 1,
.width = 10,
.height = 10,
});
try storage.addPlacement(io, alloc, t.screens.active, 1, 1, .{
.location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 2 }) },
});
t.setTopAndBottomMargin(2, 5);
try t.scrollUp(10);
try testing.expectEqual(@as(usize, 0), storage.placements.count());
}
test "storage: scroll margins straddling placement pin inside region restored" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .cols = 10, .rows = 6 });
defer t.deinit(alloc);
t.width_px = 100;
t.height_px = 60;
// 2-row image anchored on the bottom margin row, extending below
// the region: not entirely inside, so it must not move even though
// the region scroll moves every tracked pin within the region rows.
const storage: *ImageStorage = &t.screens.active.kitty_images;
try storage.addImage(io, alloc, t.screens.active, .{
.id = 1,
.width = 10,
.height = 20,
});
try storage.addPlacement(io, alloc, t.screens.active, 1, 1, .{
.location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 4 }) },
});
t.setTopAndBottomMargin(2, 5);
t.setCursorPos(5, 1);
try t.index();
const p = storage.placements.get(.{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 1 },
}).?;
const pt = t.screens.active.pages.pointFromPin(.active, p.location.pin.*).?;
try testing.expectEqual(point.Coordinate{ .x = 0, .y = 4 }, pt.coord());
try testing.expectEqual(@as(u32, 0), p.source_y);
try testing.expectEqual(@as(u32, 0), p.source_height);
}
test "storage: scroll margins multi-line scroll up with scrollback" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .cols = 10, .rows = 6 });
defer t.deinit(alloc);
t.width_px = 100;
t.height_px = 60;
// 1-row image inside a region whose top is the screen top, so the
// scroll pushes text into the scrollback (window shift) while the
// image moves within the region.
const storage: *ImageStorage = &t.screens.active.kitty_images;
try storage.addImage(io, alloc, t.screens.active, .{
.id = 1,
.width = 10,
.height = 10,
});
try storage.addPlacement(io, alloc, t.screens.active, 1, 1, .{
.location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 2 }) },
});
t.setTopAndBottomMargin(1, 4);
try t.scrollUp(2);
{
const p = storage.placements.get(.{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 1 },
}).?;
const pt = t.screens.active.pages.pointFromPin(.active, p.location.pin.*).?;
try testing.expectEqual(point.Coordinate{ .x = 0, .y = 0 }, pt.coord());
}
// Another two rows scrolls it out of the region: deleted.
try t.scrollUp(2);
try testing.expectEqual(@as(usize, 0), storage.placements.count());
}
test "storage: resolveChain accumulates offsets to the pin root" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 });
defer t.deinit(alloc);
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
try s.addImage(io, alloc, t.screens.active, .{ .id = 1 });
try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{
.location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) },
});
try s.addPlacement(io, alloc, t.screens.active, 1, 2, .{
.location = .{ .relative = .{
.parent = .{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 1 },
},
.horizontal_offset = 2,
.vertical_offset = 1,
} },
});
try s.addPlacement(io, alloc, t.screens.active, 1, 3, .{
.location = .{ .relative = .{
.parent = .{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 2 },
},
.horizontal_offset = -1,
.vertical_offset = 4,
} },
});
const grandchild = s.placements.get(.{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 3 },
}).?;
const chain = s.resolveChain(grandchild.location.relative).?;
try testing.expect(chain.root_key.eql(.{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 1 },
}));
try testing.expect(chain.root.location == .pin);
try testing.expectEqual(@as(i32, 1), chain.horizontal_offset);
try testing.expectEqual(@as(i32, 5), chain.vertical_offset);
}
test "storage: resolveChain finds virtual roots" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 });
defer t.deinit(alloc);
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
try s.addImage(io, alloc, t.screens.active, .{ .id = 1 });
try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{
.location = .{ .virtual = {} },
});
try s.addPlacement(io, alloc, t.screens.active, 1, 2, .{
.location = .{ .relative = .{
.parent = .{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 1 },
},
.horizontal_offset = 1,
} },
});
const child = s.placements.get(.{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 2 },
}).?;
const chain = s.resolveChain(child.location.relative).?;
try testing.expect(chain.root.location == .virtual);
try testing.expect(chain.root_key.eql(.{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 1 },
}));
try testing.expectEqual(@as(i32, 1), chain.horizontal_offset);
try testing.expectEqual(@as(i32, 0), chain.vertical_offset);
}
test "storage: eviction removes orphaned relative placements" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 });
defer t.deinit(alloc);
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
s.total_limit = 8;
// Image 1 holds most of the byte budget and has a pin placement.
try s.addImage(io, alloc, t.screens.active, .{
.id = 1,
.width = 2,
.height = 1,
.data = .{ .complete = try alloc.dupe(u8, &.{ 0, 0, 0, 0, 0, 0 }) },
});
try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{
.location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) },
});
// Image 2's placement is relative to image 1's placement.
try s.addImage(io, alloc, t.screens.active, .{ .id = 2 });
try s.addPlacement(io, alloc, t.screens.active, 2, 1, .{
.location = .{ .relative = .{ .parent = .{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 1 },
} } },
});
// Adding image 3 exceeds the limit and evicts image 1 (oldest),
// removing its placement, which orphans image 2's placement.
try s.addImage(io, alloc, t.screens.active, .{
.id = 3,
.width = 2,
.height = 1,
.data = .{ .complete = try alloc.dupe(u8, &.{ 0, 0, 0, 0, 0, 0 }) },
});
try testing.expect(s.imageById(1) == null);
try testing.expectEqual(@as(usize, 0), s.placements.count());
try testing.expect(s.imageById(2) != null);
}
test "storage: placeholderTarget lookup" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 });
defer t.deinit(alloc);
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
try s.addImage(io, alloc, t.screens.active, .{ .id = 1 });
try s.addPlacement(io, alloc, t.screens.active, 1, 5, .{
.location = .{ .virtual = {} },
});
// Exact external ID match.
{
const target = s.placeholderTarget(1, 5).?;
const expected: ImageStorage.PlacementKey = .{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 5 },
};
try testing.expectEqual(expected, target.key);
try testing.expect(target.placement.location == .virtual);
}
// Zero placement ID falls back to the image's virtual placement.
{
const target = s.placeholderTarget(1, 0).?;
const expected: ImageStorage.PlacementKey = .{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 5 },
};
try testing.expectEqual(expected, target.key);
}
try testing.expect(s.placeholderTarget(1, 9) == null);
try testing.expect(s.placeholderTarget(2, 0) == null);
// With multiple virtual placements, the zero-ID fallback picks
// deterministically by PlacementId.preferredOver: lowest external.
try s.addPlacement(io, alloc, t.screens.active, 1, 3, .{
.location = .{ .virtual = {} },
});
{
const expected: ImageStorage.PlacementKey = .{
.image_id = 1,
.placement_id = .{ .tag = .external, .id = 3 },
};
try testing.expectEqual(expected, s.placeholderTarget(1, 0).?.key);
}
}
test "storage: animation tick advances and schedules" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .cols = 10, .rows = 10 });
defer t.deinit(alloc);
t.width_px = 100;
t.height_px = 100;
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
// A running 1x1 RGBA image whose animation has one extra frame.
try s.addImage(io, alloc, t.screens.active, .{
.id = 1,
.width = 1,
.height = 1,
.format = .rgba,
.data = .{ .complete = try alloc.dupe(u8, &.{ 255, 0, 0, 255 }) },
});
const img = s.images.getPtr(1).?;
const anim = try alloc.create(animation.Animation);
anim.* = .{ .state = .running };
img.animation = anim;
try anim.frames.append(alloc, .{
.data = try alloc.dupe(u8, &.{ 0, 0, 255, 255 }),
.gap_ms = 40,
});
// Without a placement the animation doesn't advance (our
// approximation of Kitty's visibility gate).
try testing.expect(s.animationTick(io, 0) == null);
try testing.expectEqual(@as(u32, 0), anim.current_index);
try s.addPlacement(io, alloc, t.screens.active, 1, 0, .{
.location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) },
});
const gen1 = img.generation;
s.dirty = false;
// First tick: the gapless root frame is due immediately and is
// skipped over to frame 2, which is due again in its 40ms gap.
try testing.expectEqual(@as(?u64, 40), s.animationTick(io, 0));
try testing.expectEqual(@as(u32, 1), anim.current_index);
try testing.expect(img.generation > gen1);
try testing.expect(s.dirty);
// Nothing due yet: no advance, and the delay counts down.
const gen2 = img.generation;
try testing.expectEqual(@as(?u64, 30), s.animationTick(io, 10));
try testing.expectEqual(gen2, img.generation);
// Wrapping is fine with an infinite loop budget: the gapless
// root is skipped and frame 2 is shown again.
try testing.expectEqual(@as(?u64, 40), s.animationTick(io, 40));
try testing.expectEqual(@as(u32, 1), anim.current_index);
try testing.expectEqual(@as(u32, 1), anim.current_loop);
}
test "storage: animation tick loading state parks on last frame" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .cols = 10, .rows = 10 });
defer t.deinit(alloc);
t.width_px = 100;
t.height_px = 100;
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
// A placed 1x1 RGBA image in the loading state (a=a s=2) whose
// animation has one extra frame.
try s.addImage(io, alloc, t.screens.active, .{
.id = 1,
.width = 1,
.height = 1,
.format = .rgba,
.data = .{ .complete = try alloc.dupe(u8, &.{ 255, 0, 0, 255 }) },
});
const anim = try alloc.create(animation.Animation);
anim.* = .{ .state = .loading };
s.images.getPtr(1).?.animation = anim;
try anim.frames.append(alloc, .{
.data = try alloc.dupe(u8, &.{ 0, 0, 255, 255 }),
.gap_ms = 40,
});
try s.addPlacement(io, alloc, t.screens.active, 1, 0, .{
.location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) },
});
// Reach the last frame, then park: no wakeup is scheduled while
// waiting for more frames and the loop counter stays untouched.
try testing.expectEqual(@as(?u64, 40), s.animationTick(io, 0));
try testing.expectEqual(@as(u32, 1), anim.current_index);
try testing.expect(s.animationTick(io, 100) == null);
try testing.expectEqual(@as(u32, 1), anim.current_index);
try testing.expectEqual(@as(u32, 0), anim.current_loop);
// A new frame arriving un-parks playback.
try anim.frames.append(alloc, .{
.data = try alloc.dupe(u8, &.{ 0, 255, 0, 255 }),
.gap_ms = 25,
});
try testing.expectEqual(@as(?u64, 25), s.animationTick(io, 150));
try testing.expectEqual(@as(u32, 2), anim.current_index);
}
test "storage: animation tick exhausts loop budget" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .cols = 10, .rows = 10 });
defer t.deinit(alloc);
t.width_px = 100;
t.height_px = 100;
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
// A placed, running 1x1 RGBA image with a gapped root frame, one
// extra frame, and a one-loop budget (a=a v=2).
try s.addImage(io, alloc, t.screens.active, .{
.id = 1,
.width = 1,
.height = 1,
.format = .rgba,
.data = .{ .complete = try alloc.dupe(u8, &.{ 255, 0, 0, 255 }) },
});
const anim = try alloc.create(animation.Animation);
anim.* = .{
.state = .running,
.root_gap_ms = 10,
.max_loops = 1,
};
s.images.getPtr(1).?.animation = anim;
try anim.frames.append(alloc, .{
.data = try alloc.dupe(u8, &.{ 0, 0, 255, 255 }),
.gap_ms = 40,
});
try s.addPlacement(io, alloc, t.screens.active, 1, 0, .{
.location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) },
});
// Root shows for 10ms, frame 2 for 40ms, then the wrap exhausts
// the budget and playback freezes on the last frame for good.
try testing.expectEqual(@as(?u64, 10), s.animationTick(io, 0));
try testing.expectEqual(@as(u32, 0), anim.current_index);
try testing.expectEqual(@as(?u64, 40), s.animationTick(io, 10));
try testing.expectEqual(@as(u32, 1), anim.current_index);
try testing.expect(s.animationTick(io, 50) == null);
try testing.expectEqual(@as(u32, 1), anim.current_index);
try testing.expect(s.animationTick(io, 500) == null);
}
test "storage: animation tick ignores ineligible animations" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .cols = 10, .rows = 10 });
defer t.deinit(alloc);
t.width_px = 100;
t.height_px = 100;
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
// A placed 1x1 RGBA image with one extra frame, in the default
// stopped state.
try s.addImage(io, alloc, t.screens.active, .{
.id = 1,
.width = 1,
.height = 1,
.format = .rgba,
.data = .{ .complete = try alloc.dupe(u8, &.{ 255, 0, 0, 255 }) },
});
const anim = try alloc.create(animation.Animation);
anim.* = .{};
s.images.getPtr(1).?.animation = anim;
try anim.frames.append(alloc, .{
.data = try alloc.dupe(u8, &.{ 0, 0, 255, 255 }),
.gap_ms = 40,
});
try s.addPlacement(io, alloc, t.screens.active, 1, 0, .{
.location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) },
});
// Stopped (the default) never advances.
try testing.expect(s.animationTick(io, 0) == null);
// An all-gapless animation can never advance either.
anim.state = .running;
anim.frames.items[0].gap_ms = 0;
try testing.expect(s.animationTick(io, 0) == null);
try testing.expectEqual(@as(u32, 0), anim.current_index);
}
test "storage: animation tick re-anchors a restarted clock" {
const testing = std.testing;
const alloc = testing.allocator;
const io = testing.io;
var t = try terminal.Terminal.init(io, alloc, .{ .cols = 10, .rows = 10 });
defer t.deinit(alloc);
t.width_px = 100;
t.height_px = 100;
var s: ImageStorage = .{};
defer s.deinit(alloc, t.screens.active);
// A placed, running 1x1 RGBA image displaying its extra frame,
// with a shown-at timestamp far ahead of the tick clock.
try s.addImage(io, alloc, t.screens.active, .{
.id = 1,
.width = 1,
.height = 1,
.format = .rgba,
.data = .{ .complete = try alloc.dupe(u8, &.{ 255, 0, 0, 255 }) },
});
const anim = try alloc.create(animation.Animation);
anim.* = .{
.state = .running,
.current_index = 1,
.frame_shown_at_ms = 1000,
};
s.images.getPtr(1).?.animation = anim;
try anim.frames.append(alloc, .{
.data = try alloc.dupe(u8, &.{ 0, 0, 255, 255 }),
.gap_ms = 40,
});
try s.addPlacement(io, alloc, t.screens.active, 1, 0, .{
.location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) },
});
// A timestamp in the future relative to now means the caller's
// clock restarted; the animation must not stall until the old
// timestamp comes around again.
try testing.expectEqual(@as(?u64, 40), s.animationTick(io, 5));
try testing.expectEqual(@as(?u64, 5), anim.frame_shown_at_ms);
}
|