graphics-protocol.rst 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. Terminal graphics protocol
  2. =================================
  3. The goal of this specification is to create a flexible and performant protocol
  4. that allows the program running in the terminal, hereafter called the *client*,
  5. to render arbitrary pixel (raster) graphics to the screen of the terminal
  6. emulator. The major design goals are:
  7. * Should not require terminal emulators to understand image formats.
  8. * Should allow specifying graphics to be drawn at individual pixel positions.
  9. * The graphics should integrate with the text, in particular it should be possible to draw graphics
  10. below as well as above the text, with alpha blending. The graphics should also scroll with the text, automatically.
  11. * Should use optimizations when the client is running on the same computer as the terminal emulator.
  12. For some discussion regarding the design choices, see :iss:`33`.
  13. To see a quick demo, inside a |kitty| terminal run::
  14. kitten icat path/to/some/image.png
  15. You can also see a screenshot with more sophisticated features such as
  16. alpha-blending and text over graphics.
  17. .. image:: https://user-images.githubusercontent.com/1308621/31647475-1188ab66-b326-11e7-8d26-24b937f1c3e8.png
  18. :alt: Demo of graphics rendering in kitty
  19. :align: center
  20. Some applications that use the kitty graphics protocol:
  21. * `awrit <https://github.com/chase/awrit>`_ - Chromium-based web browser rendered in Kitty with mouse and keyboard support
  22. * `broot <https://dystroy.org/broot/>`_ - a terminal file explorer and manager, with preview of images, SVG, PDF, etc.
  23. * `chafa <https://github.com/hpjansson/chafa>`_ - a terminal image viewer
  24. * :doc:`kitty-diff <kittens/diff>` - a side-by-side terminal diff program with support for images
  25. * `fzf <https://github.com/junegunn/fzf/commit/d8188fce7b7bea982e7f9050c35e488e49fb8fd0>`_ - A command line fuzzy finder
  26. * `mpv <https://github.com/mpv-player/mpv/commit/874e28f4a41a916bb567a882063dd2589e9234e1>`_ - A video player that can play videos in the terminal
  27. * `neofetch <https://github.com/dylanaraps/neofetch>`_ - A command line system information tool
  28. * `pixcat <https://github.com/mirukana/pixcat>`_ - a third party CLI and python library that wraps the graphics protocol
  29. * `ranger <https://github.com/ranger/ranger>`_ - a terminal file manager, with image previews
  30. * `termpdf.py <https://github.com/dsanson/termpdf.py>`_ - a terminal PDF/DJVU/CBR viewer
  31. * `timg <https://github.com/hzeller/timg>`_ - a terminal image and video viewer
  32. * `tpix <https://github.com/jesvedberg/tpix>`_ - a statically compiled binary that can be used to display images and easily installed on remote servers without root access
  33. * `twitch-tui <https://github.com/Xithrius/twitch-tui>`_ - Twitch chat in the terminal
  34. * `viu <https://github.com/atanunq/viu>`_ - a terminal image viewer
  35. * `Yazi <https://github.com/sxyazi/yazi>`_ - Blazing fast terminal file manager written in Rust, based on async I/O
  36. Libraries:
  37. * `ctx.graphics <https://ctx.graphics/>`_ - Library for drawing graphics
  38. * `notcurses <https://github.com/dankamongmen/notcurses>`_ - C library for terminal graphics with bindings for C++, Rust and Python
  39. * `rasterm <https://github.com/BourgeoisBear/rasterm>`_ - Go library to display images in the terminal
  40. * `hologram.nvim <https://github.com/edluffy/hologram.nvim>`_ - view images inside nvim
  41. * `image.nvim <https://github.com/3rd/image.nvim>`_ - Bringing images to neovim
  42. * `image_preview.nvim <https://github.com/adelarsq/image_preview.nvim/>`_ - Image preview for neovim
  43. * `kui.nvim <https://github.com/romgrk/kui.nvim>`_ - Build sophisticated UIs inside neovim using the kitty graphics protocol
  44. * `term-image <https://github.com/AnonymouX47/term-image>`_ - A Python library, CLI and TUI to display and browse images in the terminal
  45. * `glkitty <https://github.com/michaeljclark/glkitty>`_ - C library to draw OpenGL shaders in the terminal with a glgears demo
  46. Other terminals that have implemented the graphics protocol:
  47. * `Konsole <https://invent.kde.org/utilities/konsole/-/merge_requests/594>`_
  48. * `wayst <https://github.com/91861/wayst>`_
  49. * `WezTerm <https://github.com/wez/wezterm/issues/986>`_
  50. Getting the window size
  51. -------------------------
  52. In order to know what size of images to display and how to position them, the
  53. client must be able to get the window size in pixels and the number of cells
  54. per row and column. The cell width is then simply the window size divided by the
  55. number of rows. This can be done by using the ``TIOCGWINSZ`` ioctl. Some
  56. code to demonstrate its use
  57. .. tab:: C
  58. .. code-block:: c
  59. #include <stdio.h>
  60. #include <sys/ioctl.h>
  61. int main(int argc, char **argv) {
  62. struct winsize sz;
  63. ioctl(0, TIOCGWINSZ, &sz);
  64. printf(
  65. "number of rows: %i, number of columns: %i, screen width: %i, screen height: %i\n",
  66. sz.ws_row, sz.ws_col, sz.ws_xpixel, sz.ws_ypixel);
  67. return 0;
  68. }
  69. .. tab:: Python
  70. .. code-block:: python
  71. import array, fcntl, sys, termios
  72. buf = array.array('H', [0, 0, 0, 0])
  73. fcntl.ioctl(sys.stdout, termios.TIOCGWINSZ, buf)
  74. print((
  75. 'number of rows: {} number of columns: {}'
  76. 'screen width: {} screen height: {}').format(*buf))
  77. .. tab:: Go
  78. .. code-block:: go
  79. package main
  80. import (
  81. "fmt"
  82. "os"
  83. "golang.org/x/sys/unix"
  84. )
  85. func main() {
  86. var err error
  87. var f *os.File
  88. if f, err = os.OpenFile("/dev/tty", unix.O_NOCTTY|unix.O_CLOEXEC|unix.O_NDELAY|unix.O_RDWR, 0666); err == nil {
  89. var sz *unix.Winsize
  90. if sz, err = unix.IoctlGetWinsize(int(f.Fd()), unix.TIOCGWINSZ); err == nil {
  91. fmt.Printf("rows: %v columns: %v width: %v height %v\n", sz.Row, sz.Col, sz.Xpixel, sz.Ypixel)
  92. return
  93. }
  94. }
  95. fmt.Fprintln(os.Stderr, err)
  96. os.Exit(1)
  97. }
  98. .. tab:: Bash
  99. .. code-block:: sh
  100. #!/bin/bash
  101. # This uses the kitten standalone binary from kitty to get the pixel sizes
  102. # since we can't do IOCTLs directly. Fortunately, kitten is a static exe
  103. # pre-built for every Unix like OS under the sun.
  104. builtin read -r rows cols < <(command stty size)
  105. IFS=x builtin read -r width height < <(command kitten icat --print-window-size); builtin unset IFS
  106. builtin echo "number of rows: $rows number of columns: $cols screen width: $width screen height: $height"
  107. Note that some terminals return ``0`` for the width and height values. Such
  108. terminals should be modified to return the correct values. Examples of
  109. terminals that return correct values: ``kitty, xterm``
  110. You can also use the *CSI t* escape code to get the screen size. Send
  111. ``<ESC>[14t`` to ``STDOUT`` and kitty will reply on ``STDIN`` with
  112. ``<ESC>[4;<height>;<width>t`` where ``height`` and ``width`` are the window
  113. size in pixels. This escape code is supported in many terminals, not just
  114. kitty.
  115. A minimal example
  116. ------------------
  117. Some minimal code to display PNG images in kitty, using the most basic
  118. features of the graphics protocol:
  119. .. tab:: Bash
  120. .. code-block:: sh
  121. #!/bin/bash
  122. transmit_png() {
  123. data=$(base64 "$1")
  124. data="${data//[[:space:]]}"
  125. builtin local pos=0
  126. builtin local chunk_size=4096
  127. while [ $pos -lt ${#data} ]; do
  128. builtin printf "\e_G"
  129. [ $pos = "0" ] && printf "a=T,f=100,"
  130. builtin local chunk="${data:$pos:$chunk_size}"
  131. pos=$(($pos+$chunk_size))
  132. [ $pos -lt ${#data} ] && builtin printf "m=1"
  133. [ ${#chunk} -gt 0 ] && builtin printf ";%s" "${chunk}"
  134. builtin printf "\e\\"
  135. done
  136. }
  137. transmit_png "$1"
  138. .. tab:: Python
  139. .. code-block:: python
  140. #!/usr/bin/python
  141. import sys
  142. from base64 import standard_b64encode
  143. def serialize_gr_command(**cmd):
  144. payload = cmd.pop('payload', None)
  145. cmd = ','.join(f'{k}={v}' for k, v in cmd.items())
  146. ans = []
  147. w = ans.append
  148. w(b'\033_G'), w(cmd.encode('ascii'))
  149. if payload:
  150. w(b';')
  151. w(payload)
  152. w(b'\033\\')
  153. return b''.join(ans)
  154. def write_chunked(**cmd):
  155. data = standard_b64encode(cmd.pop('data'))
  156. while data:
  157. chunk, data = data[:4096], data[4096:]
  158. m = 1 if data else 0
  159. sys.stdout.buffer.write(serialize_gr_command(payload=chunk, m=m,
  160. **cmd))
  161. sys.stdout.flush()
  162. cmd.clear()
  163. with open(sys.argv[-1], 'rb') as f:
  164. write_chunked(a='T', f=100, data=f.read())
  165. Save this script as :file:`send-png`, then you can use it to display any PNG
  166. file in kitty as::
  167. chmod +x send-png
  168. ./send-png file.png
  169. The graphics escape code
  170. ---------------------------
  171. All graphics escape codes are of the form::
  172. <ESC>_G<control data>;<payload><ESC>\
  173. This is a so-called *Application Programming Command (APC)*. Most terminal
  174. emulators ignore APC codes, making it safe to use.
  175. The control data is a comma-separated list of ``key=value`` pairs. The payload
  176. is arbitrary binary data, :rfc:`base64 <4648>` encoded to prevent interoperation problems
  177. with legacy terminals that get confused by control codes within an APC code.
  178. The meaning of the payload is interpreted based on the control data.
  179. The first step is to transmit the actual image data.
  180. .. _transferring_pixel_data:
  181. Transferring pixel data
  182. --------------------------
  183. The first consideration when transferring data between the client and the
  184. terminal emulator is the format in which to do so. Since there is a vast and
  185. growing number of image formats in existence, it does not make sense to have
  186. every terminal emulator implement support for them. Instead, the client should
  187. send simple pixel data to the terminal emulator. The obvious downside to this
  188. is performance, especially when the client is running on a remote machine.
  189. Techniques for remedying this limitation are discussed later. The terminal
  190. emulator must understand pixel data in three formats, 24-bit RGB, 32-bit RGBA and
  191. PNG. This is specified using the ``f`` key in the control data. ``f=32`` (which is the
  192. default) indicates 32-bit RGBA data and ``f=24`` indicates 24-bit RGB data and ``f=100``
  193. indicates PNG data. The PNG format is supported both for convenience, and as a compact way
  194. of transmitting paletted images.
  195. RGB and RGBA data
  196. ~~~~~~~~~~~~~~~~~~~
  197. In these formats the pixel data is stored directly as 3 or 4 bytes per pixel,
  198. respectively. The colors in the data **must** be in the *sRGB color space*. When
  199. specifying images in this format, the image dimensions **must** be sent in the
  200. control data. For example::
  201. <ESC>_Gf=24,s=10,v=20;<payload><ESC>\
  202. Here the width and height are specified using the ``s`` and ``v`` keys respectively. Since
  203. ``f=24`` there are three bytes per pixel and therefore the pixel data must be ``3 * 10 * 20 = 600``
  204. bytes.
  205. PNG data
  206. ~~~~~~~~~~~~~~~
  207. In this format any PNG image can be transmitted directly. For example::
  208. <ESC>_Gf=100;<payload><ESC>\
  209. The PNG format is specified using the ``f=100`` key. The width and height of
  210. the image will be read from the PNG data itself. Note that if you use both PNG and
  211. compression, then you must provide the ``S`` key with the size of the PNG data.
  212. Compression
  213. ~~~~~~~~~~~~~
  214. The client can send compressed image data to the terminal emulator, by
  215. specifying the ``o`` key. Currently, only :rfc:`1950` ZLIB based deflate
  216. compression is supported, which is specified using ``o=z``. For example::
  217. <ESC>_Gf=24,s=10,v=20,o=z;<payload><ESC>\
  218. This is the same as the example from the RGB data section, except that the
  219. payload is now compressed using deflate (this occurs prior to
  220. :rfc:`base64 <4648>` encoding).
  221. The terminal emulator will decompress it before rendering. You can specify
  222. compression for any format. The terminal emulator will decompress before
  223. interpreting the pixel data.
  224. The transmission medium
  225. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  226. The transmission medium is specified using the ``t`` key. The ``t`` key defaults to ``d``
  227. and can take the values:
  228. ================== ============
  229. Value of `t` Meaning
  230. ================== ============
  231. ``d`` Direct (the data is transmitted within the escape code itself)
  232. ``f`` A simple file (regular files only, not named pipes, device files, etc.)
  233. ``t`` A temporary file, the terminal emulator will delete the file after reading the pixel data. For security reasons
  234. the terminal emulator should only delete the file if it
  235. is in a known temporary directory, such as :file:`/tmp`,
  236. :file:`/dev/shm`, :file:`TMPDIR env var if present` and any platform
  237. specific temporary directories and the file has the
  238. string :code:`tty-graphics-protocol` in its full file path.
  239. ``s`` A *shared memory object*, which on POSIX systems is a
  240. `POSIX shared memory object <https://pubs.opengroup.org/onlinepubs/9699919799/functions/shm_open.html>`_
  241. and on Windows is a
  242. `Named shared memory object <https://docs.microsoft.com/en-us/windows/win32/memory/creating-named-shared-memory>`_.
  243. The terminal emulator must read the data from the memory
  244. object and then unlink and close it on POSIX and just
  245. close it on Windows.
  246. ================== ============
  247. When opening files, the terminal emulator must follow symlinks. In case of
  248. symlink loops or too many symlinks, it should fail and respond with an error,
  249. similar to reporting any other kind of I/O error. Since the file paths come
  250. from potentially untrusted sources, terminal emulators **must** refuse to read
  251. any device/socket/etc. special files. Only regular files are allowed.
  252. Additionally, terminal emulators may refuse to read files in *sensitive*
  253. parts of the filesystem, such as :file:`/proc`, :file:`/sys`, :file:`/dev/`, etc.
  254. Local client
  255. ^^^^^^^^^^^^^^
  256. First let us consider the local client techniques (files and shared memory). Some examples::
  257. <ESC>_Gf=100,t=f;<encoded /path/to/file.png><ESC>\
  258. Here we tell the terminal emulator to read PNG data from the specified file of
  259. the specified size::
  260. <ESC>_Gs=10,v=2,t=s,o=z;<encoded /some-shared-memory-name><ESC>\
  261. Here we tell the terminal emulator to read compressed image data from
  262. the specified shared memory object.
  263. The client can also specify a size and offset to tell the terminal emulator
  264. to only read a part of the specified file. The is done using the ``S`` and ``O``
  265. keys respectively. For example::
  266. <ESC>_Gs=10,v=2,t=s,S=80,O=10;<encoded /some-shared-memory-name><ESC>\
  267. This tells the terminal emulator to read ``80`` bytes starting from the offset ``10``
  268. inside the specified shared memory buffer.
  269. Remote client
  270. ^^^^^^^^^^^^^^^^
  271. Remote clients, those that are unable to use the filesystem/shared memory to
  272. transmit data, must send the pixel data directly using escape codes. Since
  273. escape codes are of limited maximum length, the data will need to be chunked up
  274. for transfer. This is done using the ``m`` key. The pixel data must first be
  275. :rfc:`base64 <4648>` encoded then chunked up into chunks no larger than ``4096`` bytes. All
  276. chunks, except the last, must have a size that is a multiple of 4. The client
  277. then sends the graphics escape code as usual, with the addition of an ``m`` key
  278. that must have the value ``1`` for all but the last chunk, where it must be
  279. ``0``. For example, if the data is split into three chunks, the client would
  280. send the following sequence of escape codes to the terminal emulator::
  281. <ESC>_Gs=100,v=30,m=1;<encoded pixel data first chunk><ESC>\
  282. <ESC>_Gm=1;<encoded pixel data second chunk><ESC>\
  283. <ESC>_Gm=0;<encoded pixel data last chunk><ESC>\
  284. Note that only the first escape code needs to have the full set of control
  285. codes such as width, height, format, etc. Subsequent chunks **must** have only
  286. the ``m`` and optionally ``q`` keys. When sending animation frame data, subsequent
  287. chunks **must** also specify the ``a=f`` key. The client **must** finish sending
  288. all chunks for a single image before sending any other graphics related escape
  289. codes. Note that the cursor position used to display the image **must** be the
  290. position when the final chunk is received. Finally, terminals must not display
  291. anything, until the entire sequence is received and validated.
  292. Querying support and available transmission mediums
  293. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  294. Since a client has no a-priori knowledge of whether it shares a filesystem/shared memory
  295. with the terminal emulator, it can send an id with the control data, using the ``i`` key
  296. (which can be an arbitrary positive integer up to 4294967295, it must not be zero).
  297. If it does so, the terminal emulator will reply after trying to load the image, saying
  298. whether loading was successful or not. For example::
  299. <ESC>_Gi=31,s=10,v=2,t=s;<encoded /some-shared-memory-name><ESC>\
  300. to which the terminal emulator will reply (after trying to load the data)::
  301. <ESC>_Gi=31;error message or OK<ESC>\
  302. Here the ``i`` value will be the same as was sent by the client in the original
  303. request. The message data will be a ASCII encoded string containing only
  304. printable characters and spaces. The string will be ``OK`` if reading the pixel
  305. data succeeded or an error message.
  306. Sometimes, using an id is not appropriate, for example, if you do not want to
  307. replace a previously sent image with the same id, or if you are sending a dummy
  308. image and do not want it stored by the terminal emulator. In that case, you can
  309. use the *query action*, set ``a=q``. Then the terminal emulator will try to load
  310. the image and respond with either OK or an error, as above, but it will not
  311. replace an existing image with the same id, nor will it store the image.
  312. As of May 2023, kitty has a complete implementation of this protocol and
  313. WezTerm has a mostly complete implementation. Konsole and wayst have partial
  314. support. We intend that any terminal emulator that wishes to support it can do so. To
  315. check if a terminal emulator supports the graphics protocol the best way is to
  316. send the above *query action* followed by a request for the `primary device
  317. attributes <https://vt100.net/docs/vt510-rm/DA1.html>`_. If you get back an
  318. answer for the device attributes without getting back an answer for the *query
  319. action* the terminal emulator does not support the graphics protocol.
  320. This means that terminal emulators that support the graphics protocol, **must**
  321. reply to *query actions* immediately without processing other input. Most
  322. terminal emulators handle input in a FIFO manner, anyway.
  323. So for example, you could send::
  324. <ESC>_Gi=31,s=1,v=1,a=q,t=d,f=24;AAAA<ESC>\<ESC>[c
  325. If you get back a response to the graphics query, the terminal emulator supports
  326. the protocol, if you get back a response to the device attributes query without
  327. a response to the graphics query, it does not.
  328. Display images on screen
  329. -----------------------------
  330. Every transmitted image can be displayed an arbitrary number of times on the
  331. screen, in different locations, using different parts of the source image, as
  332. needed. Each such display of an image is called a *placement*. You can either
  333. simultaneously transmit and display an image using the action ``a=T``, or first
  334. transmit the image with a id, such as ``i=10`` and then display it with
  335. ``a=p,i=10`` which will display the previously transmitted image at the current
  336. cursor position. When specifying an image id, the terminal emulator will reply
  337. to the placement request with an acknowledgement code, which will be either::
  338. <ESC>_Gi=<id>;OK<ESC>\
  339. when the image referred to by id was found, or::
  340. <ESC>_Gi=<id>;ENOENT:<some detailed error msg><ESC>\
  341. when the image with the specified id was not found. This is similar to the
  342. scheme described above for querying available transmission media, except that
  343. here we are querying if the image with the specified id is available or needs to
  344. be re-transmitted.
  345. Since there can be many placements per image, you can also give placements an
  346. id. To do so add the ``p`` key with a number between ``1`` and ``4294967295``.
  347. When you specify a placement id, it will be added to the acknowledgement code
  348. above. Every placement is uniquely identified by the pair of the ``image id``
  349. and the ``placement id``. If you specify a placement id for an image that does
  350. not have an id (i.e. has id=0), it will be ignored. In particular this means
  351. there can exist multiple images with ``image id=0, placement id=0``. Not
  352. specifying a placement id or using ``p=0`` for multiple put commands (``a=p``)
  353. with the same non-zero image id results in multiple placements the image.
  354. An example response::
  355. <ESC>_Gi=<image id>,p=<placement id>;OK<ESC>\
  356. If you send two placements with the same ``image id`` and ``placement id`` the
  357. second one will replace the first. This can be used to resize or move
  358. placements around the screen, without flicker.
  359. .. versionadded:: 0.19.3
  360. Support for specifying placement ids (see :doc:`kittens/query_terminal` to query kitty version)
  361. Controlling displayed image layout
  362. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  363. The image is rendered at the current cursor position, from the upper left corner of
  364. the current cell. You can also specify extra ``X=3`` and ``Y=4`` pixel offsets to display from
  365. a different origin within the cell. Note that the offsets must be smaller than the size of the cell.
  366. By default, the entire image will be displayed (images wider than the available
  367. width will be truncated on the right edge). You can choose a source rectangle (in pixels)
  368. as the part of the image to display. This is done with the keys: ``x, y, w, h`` which specify
  369. the top-left corner, width and height of the source rectangle. The displayed
  370. area is the intersection of the specified rectangle with the source image
  371. rectangle.
  372. You can also ask the terminal emulator to display the image in a specified rectangle
  373. (num of columns / num of lines), using the control codes ``c,r``. ``c`` is the number of columns
  374. and `r` the number of rows. The image will be scaled (enlarged/shrunk) as needed to fit
  375. the specified area. Note that if you specify a start cell offset via the ``X,Y`` keys, it is not
  376. added to the number of rows/columns. If only one of either ``r`` or ``c`` is
  377. specified, the other one is computed based on the source image aspect ratio, so
  378. that the image is displayed without distortion.
  379. Finally, you can specify the image *z-index*, i.e. the vertical stacking order. Images
  380. placed in the same location with different z-index values will be blended if
  381. they are semi-transparent. You can specify z-index values using the ``z`` key.
  382. Negative z-index values mean that the images will be drawn under the text. This
  383. allows rendering of text on top of images. Negative z-index values below
  384. INT32_MIN/2 (-1,073,741,824) will be drawn under cells with non-default background
  385. colors. If two images with the same z-index overlap then the image with the
  386. lower id is considered to have the lower z-index. If the images have the same
  387. z-index and the same id, then the behavior is undefined.
  388. .. note:: After placing an image on the screen the cursor must be moved to the
  389. right by the number of cols in the image placement rectangle and down by the
  390. number of rows in the image placement rectangle. If either of these cause
  391. the cursor to leave either the screen or the scroll area, the exact
  392. positioning of the cursor is undefined, and up to implementations.
  393. The client can ask the terminal emulator to not move the cursor at all
  394. by specifying ``C=1`` in the command, which sets the cursor movement policy
  395. to no movement for placing the current image.
  396. .. versionadded:: 0.20.0
  397. Support for the C=1 cursor movement policy
  398. .. _graphics_unicode_placeholders:
  399. Unicode placeholders
  400. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  401. .. versionadded:: 0.28.0
  402. Support for image display via Unicode placeholders
  403. You can also use a special Unicode character ``U+10EEEE`` as a placeholder for
  404. an image. This approach is less flexible, but it allows using images inside
  405. any host application that supports Unicode, foreground colors (tmux, vim, weechat, etc.),
  406. and a way to pass escape codes through to the underlying terminal.
  407. The central idea is that we use a single *Private Use* Unicode character as a
  408. *placeholder* to indicate to the terminal that an image is supposed to be
  409. displayed at that cell. Since this character is just normal text, Unicode aware
  410. application will move it around as needed when they redraw their screens,
  411. thereby automatically moving the displayed image as well, even though they know
  412. nothing about the graphics protocol. So an image is first created using the
  413. normal graphics protocol escape codes (albeit in quiet mode (``q=2``) so that there are
  414. no responses from the terminal that could confuse the host application). Then,
  415. the actual image is displayed by getting the host application to emit normal
  416. text consisting of ``U+10EEEE`` and various diacritics (Unicode combining
  417. characters) and colors.
  418. To use it, first create an image as you would normally with the graphics
  419. protocol with (``q=2``), but do not create a placement for it, that is, do not
  420. display it. Then, create a *virtual image placement* by specifying ``U=1`` and
  421. the desired number of lines and columns::
  422. <ESC>_Ga=p,U=1,i=<image_id>,c=<columns>,r=<rows><ESC>\
  423. The creation of the placement need not be a separate escape code, it can be
  424. combined with ``a=T`` to both transmit and create the virtual placement with a
  425. single code.
  426. The image will eventually be fit to the specified rectangle, its aspect ratio
  427. preserved. Finally, the image can be actually displayed by using the
  428. placeholder character, encoding the image ID in its foreground color. The row
  429. and column values are specified with diacritics listed in
  430. :download:`rowcolumn-diacritics.txt <../gen/rowcolumn-diacritics.txt>`. For
  431. example, here is how you can print a ``2x2`` placeholder for image ID ``42``:
  432. .. code-block:: sh
  433. printf "\e[38;5;42m\U10EEEE\U0305\U0305\U10EEEE\U0305\U030D\e[39m\n"
  434. printf "\e[38;5;42m\U10EEEE\U030D\U0305\U10EEEE\U030D\U030D\e[39m\n"
  435. Here, ``U+305`` is the diacritic corresponding to the number ``0``
  436. and ``U+30D`` corresponds to ``1``. So these two commands create the following
  437. ``2x2`` placeholder:
  438. ========== ==========
  439. (0, 0) (0, 1)
  440. (1, 0) (1, 1)
  441. ========== ==========
  442. This will cause the image with ID ``42`` to be displayed in a ``2x2`` grid.
  443. Ideally, you would print out as many cells as the number of rows and columns
  444. specified when creating the virtual placement, but in case of a mismatch only
  445. part of the image will be displayed.
  446. By using only the foreground color for image ID you are limited to either 8-bit IDs in 256 color
  447. mode or 24-bit IDs in true color mode. Since IDs are in a global namespace
  448. there can easily be collisions. If you need more bits for the image
  449. ID, you can specify the most significant byte via a third diacritic. For
  450. example, this is the placeholder for the image ID ``33554474 = 42 + (2 << 24)``:
  451. .. code-block:: sh
  452. printf "\e[38;5;42m\U10EEEE\U0305\U0305\U030E\U10EEEE\U0305\U030D\U030E\n"
  453. printf "\e[38;5;42m\U10EEEE\U030D\U0305\U030E\U10EEEE\U030D\U030D\U030E\n"
  454. Here, ``U+30E`` is the diacritic corresponding to the number ``2``.
  455. You can also specify a placement ID using the underline color (if it's omitted
  456. or zero, the terminal may choose any virtual placement of the given image). The
  457. background color is interpreted as the background color, visible if the image is
  458. transparent. Other text attributes are reserved for future use.
  459. Row, column and most significant byte diacritics may also be omitted, in which
  460. case the placeholder cell will inherit the missing values from the placeholder
  461. cell to the left, following the algorithm:
  462. - If no diacritics are present, and the previous placeholder cell has the same
  463. foreground and underline colors, then the row of the current cell will be the
  464. row of the cell to the left, the column will be the column of the cell to the
  465. left plus one, and the most significant image ID byte will be the most
  466. significant image ID byte of the cell to the left.
  467. - If only the row diacritic is present, and the previous placeholder cell has
  468. the same row and the same foreground and underline colors, then the column of
  469. the current cell will be the column of the cell to the left plus one, and the
  470. most significant image ID byte will be the most significant image ID byte of
  471. the cell to the left.
  472. - If only the row and column diacritics are present, and the previous
  473. placeholder cell has the same row, the same foreground and underline colors,
  474. and its column is one less than the current column, then the most significant
  475. image ID byte of the current cell will be the most significant image ID byte
  476. of the cell to the left.
  477. These rules are applied left-to-right, which allows specifying only row
  478. diacritics of the first column, i.e. here is a 2 rows by 3 columns placeholder:
  479. .. code-block:: sh
  480. printf "\e[38;5;42m\U10EEEE\U0305\U10EEEE\U10EEEE\n"
  481. printf "\e[38;5;42m\U10EEEE\U030D\U10EEEE\U10EEEE\n"
  482. This will not work for horizontal scrolling and overlapping images since the two
  483. given rules will fail to guess the missing information. In such cases, the
  484. terminal may apply other heuristics (but it doesn't have to).
  485. It is important to distinguish between virtual image placements and real images
  486. displayed on top of Unicode placeholders. Virtual placements are invisible and only play
  487. the role of prototypes for real images. Virtual placements can be deleted by a
  488. deletion command only when the `d` key is equal to ``i``, ``I``, ``r``, ``R``, ``n`` or ``N``.
  489. The key values ``a``, ``c``, ``p``, ``q``, ``x``, ``y``, ``z`` and their capital
  490. variants never affect virtual placements because they do not have a physical
  491. location on the screen.
  492. Real images displayed on top of Unicode placeholders are not considered
  493. placements from the protocol perspective. They cannot be manipulated using
  494. graphics commands, instead they should be moved, deleted, or modified by
  495. manipulating the underlying Unicode placeholder as normal text.
  496. .. _relative_image_placement:
  497. Relative placements
  498. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  499. .. versionadded:: 0.31.0
  500. Support for positioning images relative to other images
  501. You can specify that a placement is positioned relative to another placement.
  502. This is particularly useful in combination with
  503. :ref:`graphics_unicode_placeholders` above. It can be used to specify a single
  504. transparent pixel image using a Unicode placeholder, which moves around
  505. naturally with the text, the real image(s) can base their position relative to
  506. the placeholder.
  507. To specify that a placement should be relative to another, use the
  508. ``P=<image_id>,Q=<placement_id>`` keys, when creating the relative placement.
  509. For example::
  510. <ESC>_Ga=p,i=<image_id>,p=<placement_id>,P=<parent_img_id>,Q=<parent_placement_id><ESC>\
  511. This will create a *relative placement* that refers to the *parent placement*
  512. specified by the ``P`` and ``Q`` keys. When the parent placement moves, the
  513. relative placement moves along with it. The relative placement can be offset
  514. from the parent's location by a specified number of cells, using the ``H`` and
  515. ``V`` keys for horizontal and vertical displacement. Positive values move right
  516. and down. Negative values move left and up. The origin is the top left cell of
  517. the parent placement.
  518. The lifetime of a relative placement is tied to the lifetime of its parent. If
  519. its parent is deleted, it is deleted as well. If the image that the relative
  520. placement is a placement of, has no more placements, the image is deleted as
  521. well. Thus, a parent and its relative placements form a *group* that is managed
  522. together.
  523. A relative placement can refer to another relative placement as its parent.
  524. Thus the relative placements can form a chain. It is implementation dependent
  525. how long a chain of such placements is allowed, but implementation must allow
  526. a chain of length at least 8. If the implementation max depth is exceeded, the
  527. terminal must respond with the ``ETOODEEP`` error code.
  528. Virtual placements created for Unicode placeholder based images cannot also be
  529. relative placements. However, a relative placement can refer to a virtual
  530. placement as its parent. When a virtual placement is the parent, its position
  531. is derived from all the actual Unicode placeholder images that refer to it.
  532. The x position is the minimum of all the placeholder x positions and the y
  533. position is the minimum of all the placeholder y positions. If a client
  534. attempts to make a virtual placement relative the terminal must respond with
  535. the ``EINVAL`` error code.
  536. Terminals are required to reject the creation of a relative placement
  537. that would create a cycle, such as when A is relative to B and B is relative to
  538. C and C is relative to A. In such cases, the terminal must respond with the
  539. ``ECYCLE`` error code.
  540. If a client attempts to create a reference to a placement that does not exist
  541. the terminal must respond with the ``ENOPARENT`` error code.
  542. .. note::
  543. Since a relative placement gets its position specified based on another
  544. placement, instead of the cursor, the cursor must not move after a relative
  545. position, regardless of the value of the ``C`` key to control cursor
  546. movement.
  547. Deleting images
  548. ---------------------
  549. Images can be deleted by using the delete action ``a=d``. If specified without any
  550. other keys, it will delete all images visible on screen. To delete specific images,
  551. use the `d` key as described in the table below. Note that each value of d has
  552. both a lowercase and an uppercase variant. The lowercase variant only deletes the
  553. images without necessarily freeing up the stored image data, so that the images can be
  554. re-displayed without needing to resend the data. The uppercase variants will delete
  555. the image data as well, provided that the image is not referenced elsewhere, such as in the
  556. scrollback buffer. The values of the ``x`` and ``y`` keys are the same as cursor positions (i.e.
  557. ``x=1, y=1`` is the top left cell).
  558. ================= ============
  559. Value of ``d`` Meaning
  560. ================= ============
  561. ``a`` or ``A`` Delete all placements visible on screen
  562. ``i`` or ``I`` Delete all images with the specified id, specified using the ``i`` key. If you specify a ``p`` key for the placement id as well, then only the placement with the specified image id and placement id will be deleted.
  563. ``n`` or ``N`` Delete newest image with the specified number, specified using the ``I`` key. If you specify a ``p`` key for the
  564. placement id as well, then only the placement with the specified number and placement id will be deleted.
  565. ``c`` or ``C`` Delete all placements that intersect with the current cursor position.
  566. ``f`` or ``F`` Delete animation frames.
  567. ``p`` or ``P`` Delete all placements that intersect a specific cell, the cell is specified using the ``x`` and ``y`` keys
  568. ``q`` or ``Q`` Delete all placements that intersect a specific cell having a specific z-index. The cell and z-index is specified using the ``x``, ``y`` and ``z`` keys.
  569. ``r`` or ``R`` Delete all images whose id is greater than or equal to the value of the ``x`` key and less than or equal to the value of the ``y`` (added in kitty version 0.33.0).
  570. ``x`` or ``X`` Delete all placements that intersect the specified column, specified using the ``x`` key.
  571. ``y`` or ``Y`` Delete all placements that intersect the specified row, specified using the ``y`` key.
  572. ``z`` or ``Z`` Delete all placements that have the specified z-index, specified using the ``z`` key.
  573. ================= ============
  574. Note when all placements for an image have been deleted, the image is also
  575. deleted, if the capital letter form above is specified. Also, when the terminal
  576. is running out of quota space for new images, existing images without
  577. placements will be preferentially deleted.
  578. Some examples::
  579. <ESC>_Ga=d<ESC>\ # delete all visible placements
  580. <ESC>_Ga=d,d=i,i=10<ESC>\ # delete the image with id=10, without freeing data
  581. <ESC>_Ga=d,d=i,i=10,p=7<ESC>\ # delete the image with id=10 and placement id=7, without freeing data
  582. <ESC>_Ga=d,d=Z,z=-1<ESC>\ # delete the placements with z-index -1, also freeing up image data
  583. <ESC>_Ga=d,d=p,x=3,y=4<ESC>\ # delete all placements that intersect the cell at (3, 4), without freeing data
  584. Suppressing responses from the terminal
  585. -------------------------------------------
  586. If you are using the graphics protocol from a limited client, such as a shell
  587. script, it might be useful to avoid having to process responses from the
  588. terminal. For this, you can use the ``q`` key. Set it to ``1`` to suppress
  589. ``OK`` responses and to ``2`` to suppress failure responses.
  590. .. versionadded:: 0.19.3
  591. The ability to suppress responses (see :doc:`kittens/query_terminal` to query kitty version)
  592. Requesting image ids from the terminal
  593. -------------------------------------------
  594. If you are writing a program that is going to share the screen with other
  595. programs and you still want to use image ids, it is not possible to know
  596. what image ids are free to use. In this case, instead of using the ``i``
  597. key to specify an image id use the ``I`` key to specify an image number
  598. instead. These numbers are not unique.
  599. When creating a new image, even if an existing image has the same number a new
  600. one is created. And the terminal will reply with the id of the newly created
  601. image. For example, when creating an image with ``I=13``, the terminal will
  602. send the response::
  603. <ESC>_Gi=99,I=13;OK<ESC>\
  604. Here, the value of ``i`` is the id for the newly created image and the value of
  605. ``I`` is the same as was sent in the creation command.
  606. All future commands that refer to images using the image number, such as
  607. creating placements or deleting images, will act on only the newest image with
  608. that number. This allows the client program to send a bunch of commands dealing
  609. with an image by image number without waiting for a response from the terminal
  610. with the image id. Once such a response is received, the client program should
  611. use the ``i`` key with the image id for all future communication.
  612. .. note:: Specifying both ``i`` and ``I`` keys in any command is an error. The
  613. terminal must reply with an EINVAL error message, unless silenced.
  614. .. versionadded:: 0.19.3
  615. The ability to use image numbers (see :doc:`kittens/query_terminal` to query kitty version)
  616. .. _animation_protocol:
  617. Animation
  618. -------------------------------------------
  619. .. versionadded:: 0.20.0
  620. Animation support (see :doc:`kittens/query_terminal` to query kitty version)
  621. When designing support for animation, the two main considerations were:
  622. #. There should be a way for both client and terminal driven animations.
  623. Since there is unknown and variable latency between client and terminal,
  624. especially over SSH, client driven animations are not sufficient.
  625. #. Animations often consist of small changes from one frame to the next, the
  626. protocol should thus allow transmitting these deltas for efficiency and
  627. performance reasons.
  628. Animation support is added to the protocol by adding two new modes for the
  629. ``a`` (action) key. A ``f`` mode for transmitting frame data and an ``a`` mode
  630. for controlling the animation of an image. Animation proceeds in two steps,
  631. first a normal image is created as described earlier. Then animation frames are
  632. added to the image to make it into an animation. Since every animation is
  633. associated with a single image, all animation escape codes must specify either
  634. the ``i`` or ``I`` keys to identify the image being operated on.
  635. Transferring animation frame data
  636. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  637. Transferring animation frame data is very similar to
  638. :ref:`transferring_pixel_data` above. The main difference is that the image
  639. the frame belongs to must be specified and it is possible to transmit data for
  640. only part of a frame, declaring the rest of the frame to be filled in by data
  641. from a previous frame, or left blank. To transfer frame data the ``a=f``
  642. key must be used in all escape codes.
  643. First, to transfer a simple frame that has data for the full image area, the
  644. escape codes used are exactly the same as for transferring image data, with the
  645. addition of: ``a=f,i=<image id>`` or ``a=f,I=<image number>``.
  646. If the frame has data for only a part of the image, you can specify the
  647. rectangle for it using the ``x, y, s, v`` keys, for example::
  648. x=10,y=5,s=100,v=200 # A 100x200 rectangle with its top left corner at (10, 5)
  649. Frames are created by composing the transmitted data onto a background canvas.
  650. This canvas can be either a single color, or the pixels from a previous frame.
  651. The composition can be of two types, either a simple replacement (``X=1``) key
  652. or a full alpha blend (the default).
  653. To use a background color for the canvas, specify the ``Y`` key as a 32-bit
  654. RGBA color. For example::
  655. Y=4278190335 # 0xff0000ff opaque red
  656. Y=16711816 # 0x00ff0088 translucent green (alpha=0.53)
  657. The default background color when none is specified is ``0`` i.e. a black,
  658. transparent pixel.
  659. To use the data from a previous frame, specify the ``c`` key which is a 1-based
  660. frame number. Thus ``c=1`` refers to the root frame (the base image data),
  661. ``c=2`` refers to the second frame and so on.
  662. If the frame is composed of multiple rectangular blocks, these can be expressed
  663. by using the ``r`` key. When specifying the ``r`` key the data for an existing
  664. frame is edited. The same composition operation as above happens, but now the
  665. background canvas is the existing frame itself. ``r`` is a 1-based index, so
  666. ``r=1`` is the root frame (base image data), ``r=2`` is the second frame and so
  667. on.
  668. Finally, while transferring frame data, the frame *gap* can also be specified
  669. using the ``z`` key. The gap is the number of milliseconds to wait before
  670. displaying the next frame when the animation is running. A value of ``z=0`` is
  671. ignored, ``z=positive number`` sets the gap to the specified number of
  672. milliseconds and ``z=negative number`` creates a *gapless* frame. Gapless
  673. frames are not displayed to the user since they are instantly skipped over,
  674. however they can be useful as the base data for subsequent frames. For example,
  675. for an animation where the background remains the same and a small object or two
  676. move.
  677. Controlling animations
  678. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  679. Clients can control animations by using the ``a=a`` key in the escape code sent
  680. to the terminal.
  681. The simplest is client driven animations, where the client transmits the frame
  682. data and then also instructs the terminal to make a particular frame the current
  683. frame. To change the current frame, use the ``c`` key::
  684. <ESC>_Ga=a,i=3,c=7<ESC>\
  685. This will make the seventh frame in the image with id ``3`` the current frame.
  686. However, client driven animations can be sub-optimal, since the latency between
  687. the client and terminal is unknown and variable especially over the network.
  688. Also they require the client to remain running for the lifetime of the
  689. animation, which is not desirable for cat like utilities.
  690. Terminal driven animations are achieved by the client specifying *gaps* (time
  691. in milliseconds) between frames and instructing the terminal to stop or start
  692. the animation.
  693. The animation state is controlled by the ``s`` key. ``s=1`` stops the
  694. animation. ``s=2`` runs the animation, but in *loading* mode, in this mode when
  695. reaching the last frame, instead of looping, the terminal will wait for the
  696. arrival of more frames. ``s=3`` runs the animation normally, after the last
  697. frame, the terminal loops back to the first frame. The number of loops can be
  698. controlled by the ``v`` key. ``v=0`` is ignored, ``v=1`` is loop infinitely,
  699. and any other positive number is loop ``number - 1`` times. Note that stopping
  700. the animation resets the loop counter.
  701. Finally, the *gap* for frames can be set using the ``z`` key. This can be
  702. specified either when the frame is created as part of the transmit escape code
  703. or separately using the animation control escape code. The *gap* is the time in
  704. milliseconds to wait before displaying the next frame in the animation.
  705. For example::
  706. <ESC>_Ga=a,i=7,r=3,z=48<ESC>\
  707. This sets the gap for the third frame of the image with id ``7`` to ``48``
  708. milliseconds. Note that *gapless* frames are not displayed to the user since
  709. the next frame comes immediately, however they can be useful to store base data
  710. for subsequent frames, such as in an animation with an object moving against a
  711. static background.
  712. In particular, the first frame or *root frame* is created with the base image
  713. data and has no gap, so its gap must be set using this control code.
  714. Composing animation frames
  715. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  716. .. versionadded:: 0.22.0
  717. Support for frame composition
  718. Clients can *compose* animation frames, this means that they can compose pixels
  719. in rectangular regions from one frame onto another frame. This allows for fast
  720. and low band-width modification of frames.
  721. To achieve this use the ``a=c`` key. The source frame is specified with
  722. ``r=frame number`` and the destination frame as ``c=frame number``. The size of
  723. the rectangle is specified as ``w=width,h=height`` pixels. If unspecified, the
  724. full image width and height are used. The offset of the rectangle from the
  725. top-left corner for the source frame is specified by the ``x,y`` keys and the
  726. destination frame by the ``X,Y`` keys. The composition operation is specified
  727. by the ``C`` key with the default being to alpha blend the source rectangle
  728. onto the destination rectangle. With ``C=1`` it will be a simple replacement
  729. of pixels. For example::
  730. <ESC>_Ga=c,i=1,r=7,c=9,w=23,h=27,X=4,Y=8,x=1,y=3<ESC>\
  731. Will compose a ``23x27`` rectangle located at ``(4, 8)`` in the ``7th frame``
  732. onto the rectangle located at ``(1, 3)`` in the ``9th frame``. These will be
  733. in the image with ``id=1``.
  734. If the frames or the image are not found the terminal emulator must
  735. respond with `ENOENT`. If the rectangles go out of bounds of the image
  736. the terminal must respond with `EINVAL`. If the source and destination frames are
  737. the same and the rectangles overlap, the terminal must respond with `EINVAL`.
  738. .. note::
  739. In kitty, doing a composition will cause a frame to be *fully rendered*
  740. potentially increasing its storage requirements, when the frame was previously
  741. stored as a set of operations on other frames. If this happens and there
  742. is not enough storage space, kitty will respond with ENOSPC.
  743. Image persistence and storage quotas
  744. -----------------------------------------
  745. In order to avoid *Denial-of-Service* attacks, terminal emulators should have a
  746. maximum storage quota for image data. It should allow at least a few full
  747. screen images. For example the quota in kitty is 320MB per buffer. When adding
  748. a new image, if the total size exceeds the quota, the terminal emulator should
  749. delete older images to make space for the new one. In kitty, for animations,
  750. the additional frame data is stored on disk and has a separate, larger quota of
  751. five times the base quota.
  752. Control data reference
  753. ---------------------------
  754. The table below shows all the control data keys as well as what values they can
  755. take, and the default value they take when missing. All integers are 32-bit.
  756. ======= ==================== ========= =================
  757. Key Value Default Description
  758. ======= ==================== ========= =================
  759. ``a`` Single character. ``t`` The overall action this graphics command is performing.
  760. ``(a, c, d, f, `` ``t`` - transmit data, ``T`` - transmit data and display image,
  761. ``p, q, t, T)`` ``q`` - query terminal, ``p`` - put (display) previous transmitted image,
  762. ``d`` - delete image, ``f`` - transmit data for animation frames,
  763. ``a`` - control animation, ``c`` - compose animation frames
  764. ``q`` ``0, 1, 2`` ``0`` Suppress responses from the terminal to this graphics command.
  765. **Keys for image transmission**
  766. -----------------------------------------------------------
  767. ``f`` Positive integer. ``32`` The format in which the image data is sent.
  768. ``(24, 32, 100)``.
  769. ``t`` Single character. ``d`` The transmission medium used.
  770. ``(d, f, t, s)``.
  771. ``s`` Positive integer. ``0`` The width of the image being sent.
  772. ``v`` Positive integer. ``0`` The height of the image being sent.
  773. ``S`` Positive integer. ``0`` The size of data to read from a file.
  774. ``O`` Positive integer. ``0`` The offset from which to read data from a file.
  775. ``i`` Positive integer.
  776. ``(0 - 4294967295)`` ``0`` The image id
  777. ``I`` Positive integer.
  778. ``(0 - 4294967295)`` ``0`` The image number
  779. ``p`` Positive integer.
  780. ``(0 - 4294967295)`` ``0`` The placement id
  781. ``o`` Single character. ``null`` The type of data compression.
  782. ``only z``
  783. ``m`` zero or one ``0`` Whether there is more chunked data available.
  784. **Keys for image display**
  785. -----------------------------------------------------------
  786. ``x`` Positive integer ``0`` The left edge (in pixels) of the image area to display
  787. ``y`` Positive integer ``0`` The top edge (in pixels) of the image area to display
  788. ``w`` Positive integer ``0`` The width (in pixels) of the image area to display. By default, the entire width is used
  789. ``h`` Positive integer ``0`` The height (in pixels) of the image area to display. By default, the entire height is used
  790. ``X`` Positive integer ``0`` The x-offset within the first cell at which to start displaying the image
  791. ``Y`` Positive integer ``0`` The y-offset within the first cell at which to start displaying the image
  792. ``c`` Positive integer ``0`` The number of columns to display the image over
  793. ``r`` Positive integer ``0`` The number of rows to display the image over
  794. ``C`` Positive integer ``0`` Cursor movement policy. ``0`` is the default, to move the cursor to after the image.
  795. ``1`` is to not move the cursor at all when placing the image.
  796. ``U`` Positive integer ``0`` Set to ``1`` to create a virtual placement for a Unicode placeholder.
  797. ``z`` 32-bit integer ``0`` The *z-index* vertical stacking order of the image
  798. ``P`` Positive integer ``0`` The id of a parent image for relative placement
  799. ``Q`` Positive integer ``0`` The id of a placement in the parent image for relative placement
  800. ``H`` 32-bit integer ``0`` The offset in cells in the horizontal direction for relative placement
  801. ``V`` 32-bit integer ``0`` The offset in cells in the vertical direction for relative placement
  802. **Keys for animation frame loading**
  803. -----------------------------------------------------------
  804. ``x`` Positive integer ``0`` The left edge (in pixels) of where the frame data should be updated
  805. ``y`` Positive integer ``0`` The top edge (in pixels) of where the frame data should be updated
  806. ``c`` Positive integer ``0`` The 1-based frame number of the frame whose image data serves as the base data
  807. when creating a new frame, by default the base data is black, fully transparent pixels
  808. ``r`` Positive integer ``0`` The 1-based frame number of the frame that is being edited. By default, a new frame is created
  809. ``z`` 32-bit integer ``0`` The gap (in milliseconds) of this frame from the next one. A value of
  810. zero is ignored. Negative values create a *gapless* frame. If not specified,
  811. frames have a default gap of ``40ms``. The root frame defaults to zero gap.
  812. ``X`` Positive integer ``0`` The composition mode for blending pixels when creating a new frame or
  813. editing a frame's data. The default is full alpha blending. ``1`` means a
  814. simple overwrite.
  815. ``Y`` Positive integer ``0`` The background color for pixels not
  816. specified in the frame data. Must be in 32-bit RGBA format
  817. **Keys for animation frame composition**
  818. -----------------------------------------------------------
  819. ``c`` Positive integer ``0`` The 1-based frame number of the frame whose image data serves as the overlaid data
  820. ``r`` Positive integer ``0`` The 1-based frame number of the frame that is being edited.
  821. ``x`` Positive integer ``0`` The left edge (in pixels) of the destination rectangle
  822. ``y`` Positive integer ``0`` The top edge (in pixels) of the destination rectangle
  823. ``w`` Positive integer ``0`` The width (in pixels) of the source and destination rectangles. By default, the entire width is used
  824. ``h`` Positive integer ``0`` The height (in pixels) of the source and destination rectangles. By default, the entire height is used
  825. ``X`` Positive integer ``0`` The left edge (in pixels) of the source rectangle
  826. ``Y`` Positive integer ``0`` The top edge (in pixels) of the source rectangle
  827. ``C`` Positive integer ``0`` The composition mode for blending
  828. pixels. Default is full alpha blending. ``1`` means a simple overwrite.
  829. **Keys for animation control**
  830. -----------------------------------------------------------
  831. ``s`` Positive integer ``0`` ``1`` - stop animation, ``2`` - run animation, but wait for new frames, ``3`` - run animation
  832. ``r`` Positive integer ``0`` The 1-based frame number of the frame that is being affected
  833. ``z`` 32-bit integer ``0`` The gap (in milliseconds) of this frame from the next one. A value of
  834. zero is ignored. Negative values create a *gapless* frame.
  835. ``c`` Positive integer ``0`` The 1-based frame number of the frame that should be made the current frame
  836. ``v`` Positive integer ``0`` The number of loops to play. ``0`` is
  837. ignored, ``1`` is play infinite and is the default and larger number
  838. means play that number ``-1`` loops
  839. **Keys for deleting images**
  840. -----------------------------------------------------------
  841. ``d`` Single character. ``a`` What to delete.
  842. ``(
  843. a, A, c, C, n, N,
  844. i, I, p, P, q, Q, r,
  845. R, x, X, y, Y, z, Z
  846. )``.
  847. ======= ==================== ========= =================
  848. Interaction with other terminal actions
  849. --------------------------------------------
  850. When resetting the terminal, all images that are visible on the screen must be
  851. cleared. When switching from the main screen to the alternate screen buffer
  852. (1049 private mode) all images in the alternate screen must be cleared, just as
  853. all text is cleared. The clear screen escape code (usually ``<ESC>[2J``) should
  854. also clear all images. This is so that the clear command works.
  855. The other commands to erase text must have no effect on graphics.
  856. The dedicated delete graphics commands must be used for those.
  857. When scrolling the screen (such as when using index cursor movement commands,
  858. or scrolling through the history buffer), images must be scrolled along with
  859. text. When page margins are defined and the index commands are used, only
  860. images that are entirely within the page area (between the margins) must be
  861. scrolled. When scrolling them would cause them to extend outside the page area,
  862. they must be clipped.