···
+
(* Kitty Terminal Graphics Protocol - Implementation *)
+
type t = Rgba32 | Rgb24 | Png
+
let to_int = function Rgba32 -> 32 | Rgb24 -> 24 | Png -> 100
+
module Transmission = struct
+
type t = Direct | File | Tempfile
+
let to_char = function Direct -> 'd' | File -> 'f' | Tempfile -> 't'
+
module Compression = struct
+
let to_char = function None -> Option.none | Zlib -> Some 'z'
+
type t = Noisy | Errors_only | Silent
+
let to_int = function Noisy -> 0 | Errors_only -> 1 | Silent -> 2
+
let to_int = function Move -> 0 | Static -> 1
+
module Composition = struct
+
type t = Alpha_blend | Overwrite
+
let to_int = function Alpha_blend -> 0 | Overwrite -> 1
+
| By_id of { image_id : int; placement_id : int option }
+
| By_id_and_free of { image_id : int; placement_id : int option }
+
| By_number of { image_number : int; placement_id : int option }
+
| By_number_and_free of { image_number : int; placement_id : int option }
+
| At_cell of { x : int; y : int }
+
| At_cell_and_free of { x : int; y : int }
+
| At_cell_z of { x : int; y : int; z : int }
+
| At_cell_z_and_free of { x : int; y : int; z : int }
+
| By_column_and_free of int
+
| By_row_and_free of int
+
| By_z_index_and_free of int
+
| By_id_range of { min_id : int; max_id : int }
+
| By_id_range_and_free of { min_id : int; max_id : int }
+
module Placement = struct
+
source_width : int option;
+
source_height : int option;
+
cell_x_offset : int option;
+
cell_y_offset : int option;
+
placement_id : int option;
+
cursor : Cursor.t option;
+
unicode_placeholder : bool;
+
unicode_placeholder = false;
+
let make ?source_x ?source_y ?source_width ?source_height ?cell_x_offset
+
?cell_y_offset ?columns ?rows ?z_index ?placement_id ?cursor
+
?(unicode_placeholder = false) () =
+
base_frame : int option;
+
edit_frame : int option;
+
composition : Composition.t option;
+
background_color : int32 option;
+
background_color = None;
+
let make ?x ?y ?base_frame ?edit_frame ?gap_ms ?composition ?background_color
+
{ x; y; base_frame; edit_frame; gap_ms; composition; background_color }
+
module Animation = struct
+
type state = Stop | Loading | Run
+
| Set_state of { state : state; loops : int option }
+
| Set_gap of { frame : int; gap_ms : int }
+
let set_state ?loops state = Set_state { state; loops }
+
let set_gap ~frame ~gap_ms = Set_gap { frame; gap_ms }
+
let set_current_frame frame = Set_current frame
+
module Compose = struct
+
composition : Composition.t option;
+
let make ~source_frame ~dest_frame ?width ?height ?source_x ?source_y ?dest_x
+
?dest_y ?composition () =
+
module Command = struct
+
format : Format.t option;
+
transmission : Transmission.t option;
+
compression : Compression.t option;
+
quiet : Quiet.t option;
+
image_number : int option;
+
placement : Placement.t option;
+
delete : Delete.t option;
+
frame : Frame.t option;
+
animation : Animation.t option;
+
compose : Compose.t option;
+
let transmit ?image_id ?image_number ?format ?transmission ?compression ?width
+
?height ?size ?offset ?quiet () =
+
(make_base Transmit) with
+
let transmit_and_display ?image_id ?image_number ?format ?transmission
+
?compression ?width ?height ?size ?offset ?quiet ?placement () =
+
(make_base Transmit_and_display) with
+
let query ?format ?transmission ?width ?height ?quiet () =
+
{ (make_base Query) with format; transmission; width; height; quiet }
+
let display ?image_id ?image_number ?placement ?quiet () =
+
{ (make_base Display) with image_id; image_number; placement; quiet }
+
let delete ?quiet del =
+
{ (make_base Delete) with quiet; delete = Some del }
+
let frame ?image_id ?image_number ?format ?transmission ?compression ?width
+
?height ?quiet ~frame () =
+
let animate ?image_id ?image_number ?quiet anim =
+
{ (make_base Animate) with image_id; image_number; quiet; animation = Some anim }
+
let compose ?image_id ?image_number ?quiet comp =
+
{ (make_base Compose) with image_id; image_number; quiet; compose = Some comp }
+
(* APC escape sequences *)
+
let apc_start = "\027_G"
+
(* Helper to add key=value pairs *)
+
let add_kv buf key value =
+
Buffer.add_char buf key;
+
Buffer.add_char buf '=';
+
Buffer.add_string buf value
+
let add_kv_int buf key value =
+
Buffer.add_char buf key;
+
Buffer.add_char buf '=';
+
Buffer.add_string buf (string_of_int value)
+
let add_kv_int32 buf key value =
+
Buffer.add_char buf key;
+
Buffer.add_char buf '=';
+
Buffer.add_string buf (Int32.to_string value)
+
let add_comma buf = Buffer.add_char buf ','
+
let action_char = function
+
| Transmit_and_display -> 'T'
+
let delete_char = function
+
| Delete.All_visible -> 'a'
+
| All_visible_and_free -> 'A'
+
| By_id_and_free _ -> 'I'
+
| By_number_and_free _ -> 'N'
+
| At_cursor_and_free -> 'C'
+
| At_cell_and_free _ -> 'P'
+
| At_cell_z_and_free _ -> 'Q'
+
| By_column_and_free _ -> 'X'
+
| By_row_and_free _ -> 'Y'
+
| By_z_index_and_free _ -> 'Z'
+
| By_id_range_and_free _ -> 'R'
+
| Frames_and_free -> 'F'
+
let write_control_data buf cmd =
+
let first = ref true in
+
if !first then first := false else add_comma buf
+
add_kv buf 'a' (String.make 1 (action_char cmd.action));
+
let v = Quiet.to_int q in
+
add_kv_int buf 'f' (Format.to_int f))
+
let c = Transmission.to_char t in
+
add_kv buf 't' (String.make 1 c)))
+
match Compression.to_char c with
+
add_kv buf 'o' (String.make 1 ch)
+
(* Placement options *)
+
(fun (p : Placement.t) ->
+
let v = Cursor.to_int c in
+
if p.unicode_placeholder then (
+
add_kv buf 'd' (String.make 1 (delete_char d));
+
| Delete.By_id { image_id; placement_id }
+
| Delete.By_id_and_free { image_id; placement_id } ->
+
add_kv_int buf 'i' image_id;
+
| Delete.By_number { image_number; placement_id }
+
| Delete.By_number_and_free { image_number; placement_id } ->
+
add_kv_int buf 'I' image_number;
+
| Delete.At_cell { x; y } | Delete.At_cell_and_free { x; y } ->
+
| Delete.At_cell_z { x; y; z }
+
| Delete.At_cell_z_and_free { x; y; z } ->
+
| Delete.By_column c | Delete.By_column_and_free c ->
+
| Delete.By_row r | Delete.By_row_and_free r ->
+
| Delete.By_z_index z | Delete.By_z_index_and_free z ->
+
| Delete.By_id_range { min_id; max_id }
+
| Delete.By_id_range_and_free { min_id; max_id } ->
+
add_kv_int buf 'x' min_id;
+
add_kv_int buf 'y' max_id
+
let v = Composition.to_int c in
+
add_kv_int32 buf 'Y' v)
+
(* Animation options *)
+
| Animation.Set_state { state; loops } ->
+
| Animation.Loading -> 2
+
| Animation.Set_gap { frame; gap_ms } ->
+
add_kv_int buf 'r' frame;
+
add_kv_int buf 'z' gap_ms
+
| Animation.Set_current frame ->
+
add_kv_int buf 'c' frame)
+
(fun (c : Compose.t) ->
+
add_kv_int buf 'r' c.source_frame;
+
add_kv_int buf 'c' c.dest_frame;
+
let v = Composition.to_int comp in
+
let write buf cmd ~data =
+
Buffer.add_string buf apc_start;
+
write_control_data buf cmd;
+
if String.length data > 0 then begin
+
let encoded = Base64.encode_string data in
+
let len = String.length encoded in
+
if len <= chunk_size then (
+
Buffer.add_char buf ';';
+
Buffer.add_string buf encoded;
+
Buffer.add_string buf apc_end)
+
let first = ref true in
+
let remaining = len - !pos in
+
let this_chunk = min chunk_size remaining in
+
let is_last = !pos + this_chunk >= len in
+
Buffer.add_char buf ';';
+
Buffer.add_substring buf encoded !pos this_chunk;
+
Buffer.add_string buf apc_end)
+
(* Continuation chunk *)
+
Buffer.add_string buf apc_start;
+
add_kv_int buf 'm' (if is_last then 0 else 1);
+
Buffer.add_char buf ';';
+
Buffer.add_substring buf encoded !pos this_chunk;
+
Buffer.add_string buf apc_end);
+
pos := !pos + this_chunk
+
else Buffer.add_string buf apc_end
+
let to_string cmd ~data =
+
let buf = Buffer.create 1024 in
+
module Response = struct
+
image_number : int option;
+
placement_id : int option;
+
let is_ok t = t.message = "OK"
+
let message t = t.message
+
match String.index_opt t.message ':' with
+
| Some i -> Some (String.sub t.message 0 i)
+
| None -> Some t.message
+
let image_id t = t.image_id
+
let image_number t = t.image_number
+
let placement_id t = t.placement_id
+
(* Format: <ESC>_G<keys>;message<ESC>\ *)
+
let len = String.length s in
+
else if s.[0] <> esc || s.[1] <> '_' || s.[2] <> 'G' then None
+
(* Find the semicolon and end *)
+
match String.index_from_opt s 3 ';' with
+
(* Find the APC terminator *)
+
if pos + 1 < len && s.[pos] = esc && s.[pos + 1] = '\\' then
+
else if pos + 1 < len then find_end (pos + 1)
+
match find_end (semi_pos + 1) with
+
let keys_str = String.sub s 3 (semi_pos - 3) in
+
String.sub s (semi_pos + 1) (end_pos - semi_pos - 1)
+
let image_id = ref None in
+
let image_number = ref None in
+
let placement_id = ref None in
+
let parts = String.split_on_char ',' keys_str in
+
if String.length part >= 3 && part.[1] = '=' then
+
let value = String.sub part 2 (String.length part - 2) in
+
| 'i' -> image_id := int_of_string_opt value
+
| 'I' -> image_number := int_of_string_opt value
+
| 'p' -> placement_id := int_of_string_opt value
+
image_number = !image_number;
+
placement_id = !placement_id;
+
module Unicode_placeholder = struct
+
let placeholder_char = Uchar.of_int 0x10EEEE
+
(* Row/column diacritics from the protocol spec *)
+
0x0305; 0x030D; 0x030E; 0x0310; 0x0312; 0x033D; 0x033E; 0x033F;
+
0x0346; 0x034A; 0x034B; 0x034C; 0x0350; 0x0351; 0x0352; 0x0357;
+
0x035B; 0x0363; 0x0364; 0x0365; 0x0366; 0x0367; 0x0368; 0x0369;
+
0x036A; 0x036B; 0x036C; 0x036D; 0x036E; 0x036F; 0x0483; 0x0484;
+
0x0485; 0x0486; 0x0487; 0x0592; 0x0593; 0x0594; 0x0595; 0x0597;
+
0x0598; 0x0599; 0x059C; 0x059D; 0x059E; 0x059F; 0x05A0; 0x05A1;
+
0x05A8; 0x05A9; 0x05AB; 0x05AC; 0x05AF; 0x05C4; 0x0610; 0x0611;
+
0x0612; 0x0613; 0x0614; 0x0615; 0x0616; 0x0617; 0x0657; 0x0658;
+
0x0659; 0x065A; 0x065B; 0x065D; 0x065E; 0x06D6; 0x06D7; 0x06D8;
+
0x06D9; 0x06DA; 0x06DB; 0x06DC; 0x06DF; 0x06E0; 0x06E1; 0x06E2;
+
0x06E4; 0x06E7; 0x06E8; 0x06EB; 0x06EC; 0x0730; 0x0732; 0x0733;
+
0x0735; 0x0736; 0x073A; 0x073D; 0x073F; 0x0740; 0x0741; 0x0743;
+
0x0745; 0x0747; 0x0749; 0x074A; 0x07EB; 0x07EC; 0x07ED; 0x07EE;
+
0x07EF; 0x07F0; 0x07F1; 0x07F3; 0x0816; 0x0817; 0x0818; 0x0819;
+
0x081B; 0x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
+
0x10A0F; 0x10A38; 0x1D185; 0x1D186; 0x1D187; 0x1D188; 0x1D189;
+
0x1D1AA; 0x1D1AB; 0x1D1AC; 0x1D1AD; 0x1D242; 0x1D243; 0x1D244;
+
if n >= 0 && n < Array.length diacritics then
+
Uchar.of_int diacritics.(n)
+
else Uchar.of_int diacritics.(0)
+
let column_diacritic = row_diacritic
+
let id_high_byte_diacritic = row_diacritic
+
let b = Bytes.create 4 in
+
let len = Uchar.utf_8_byte_length u in
+
let _ = Uchar.unsafe_to_char u in
+
(* Encode UTF-8 manually *)
+
let code = Uchar.to_int u in
+
Bytes.set b 0 (Char.chr code);
+
Buffer.add_subbytes buf b 0 1)
+
else if code < 0x800 then (
+
Bytes.set b 0 (Char.chr (0xC0 lor (code lsr 6)));
+
Bytes.set b 1 (Char.chr (0x80 lor (code land 0x3F)));
+
Buffer.add_subbytes buf b 0 2)
+
else if code < 0x10000 then (
+
Bytes.set b 0 (Char.chr (0xE0 lor (code lsr 12)));
+
Bytes.set b 1 (Char.chr (0x80 lor ((code lsr 6) land 0x3F)));
+
Bytes.set b 2 (Char.chr (0x80 lor (code land 0x3F)));
+
Buffer.add_subbytes buf b 0 3)
+
Bytes.set b 0 (Char.chr (0xF0 lor (code lsr 18)));
+
Bytes.set b 1 (Char.chr (0x80 lor ((code lsr 12) land 0x3F)));
+
Bytes.set b 2 (Char.chr (0x80 lor ((code lsr 6) land 0x3F)));
+
Bytes.set b 3 (Char.chr (0x80 lor (code land 0x3F)));
+
Buffer.add_subbytes buf b 0 len)
+
let write buf ~image_id ?placement_id ~rows ~cols () =
+
(* Set foreground color using 24-bit mode *)
+
let r = (image_id lsr 16) land 0xFF in
+
let g = (image_id lsr 8) land 0xFF in
+
let b = image_id land 0xFF in
+
Buffer.add_string buf (Printf.sprintf "\027[38;2;%d;%d;%dm" r g b);
+
(* Optionally set underline color for placement ID *)
+
(match placement_id with
+
let pr = (pid lsr 16) land 0xFF in
+
let pg = (pid lsr 8) land 0xFF in
+
let pb = pid land 0xFF in
+
Buffer.add_string buf (Printf.sprintf "\027[58;2;%d;%d;%dm" pr pg pb)
+
(* High byte diacritic if needed *)
+
let high_byte = (image_id lsr 24) land 0xFF in
+
if high_byte > 0 then Some (id_high_byte_diacritic high_byte) else None
+
(* Write placeholder grid *)
+
for row = 0 to rows - 1 do
+
for col = 0 to cols - 1 do
+
add_uchar buf placeholder_char;
+
add_uchar buf (row_diacritic row);
+
add_uchar buf (column_diacritic col);
+
Option.iter (add_uchar buf) high_diac
+
if row < rows - 1 then Buffer.add_string buf "\n\r"
+
Buffer.add_string buf "\027[39m";
+
match placement_id with Some _ -> Buffer.add_string buf "\027[59m" | None -> ()
+
(* Send a 1x1 transparent pixel query *)
+
Command.query ~format:Format.Rgb24 ~transmission:Transmission.Direct
+
let data = "\x00\x00\x00" in
+
let query = Command.to_string cmd ~data in
+
(* Add DA1 query to detect non-supporting terminals *)
+
let supports_graphics response ~da1_received =
+
| Some r -> Response.is_ok r
+
| None -> not da1_received