clipboard.rst 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. Copying all data types to the clipboard
  2. ==============================================
  3. There already exists an escape code to allow terminal programs to
  4. read/write plain text data from the system clipboard, *OSC 52*.
  5. kitty introduces a more advanced protocol that supports:
  6. * Copy arbitrary data including images, rich text documents, etc.
  7. * Allow terminals to ask the user for permission to access the clipboard and
  8. report permission denied
  9. The escape code is *OSC 5522*, an extension of *OSC 52*. The basic format
  10. of the escape code is::
  11. <OSC>5522;metadata;payload<ST>
  12. Here, *metadata* is a colon separated list of key-value pairs and payload is
  13. base64 encoded data. :code:`OSC` is :code:`<ESC>[`.
  14. :code:`ST` is the string terminator, :code:`<ESC>\\`.
  15. Reading data from the system clipboard
  16. ----------------------------------------
  17. To read data from the system clipboard, the escape code is::
  18. <OSC>5522;type=read;<base 64 encoded space separated list of mime types to read><ST>
  19. For example, to read plain text and PNG data, the payload would be::
  20. text/plain image/png
  21. encoded as base64. To read from the primary selection instead of the
  22. clipboard, add the key ``loc=primary`` to the metadata section.
  23. To get the list of MIME types available on the clipboard the payload must be
  24. just a period (``.``), encoded as base64.
  25. The terminal emulator will reply with a sequence of escape codes of the form::
  26. <OSC>5522;type=read:status=OK<ST>
  27. <OSC>5522;type=read:status=DATA:mime=<base 64 encoded mime type>;<base64 encoded data><ST>
  28. <OSC>5522;type=read:status=DATA:mime=<base 64 encoded mime type>;<base64 encoded data><ST>
  29. .
  30. .
  31. .
  32. <OSC>5522;type=read:status=DONE<ST>
  33. Here, the ``status=DATA`` packets deliver the data (as base64 encoded bytes)
  34. associated with each MIME type. The terminal emulator should chunk up the data
  35. for an individual type, into chunks of size **no more** than 4096 bytes (4096
  36. is the size of a chunk *before* base64 encoding). All
  37. the chunks for a given type must be transmitted sequentially and only once they
  38. are done the chunks for the next type, if any, should be sent. The end of data
  39. is indicated by a ``status=DONE`` packet.
  40. If an error occurs, instead of the opening ``status=OK`` packet the terminal
  41. must send a ``status=ERRORCODE`` packet. The error code must be one of:
  42. ``status=ENOSYS``
  43. Sent if the requested clipboard type is not available. For example, primary
  44. selection is not available on all systems and ``loc=primary`` was used.
  45. ``status=EPERM``
  46. Sent if permission to read from the clipboard was denied by the system or
  47. the user.
  48. ``status=EBUSY``
  49. Sent if there is some temporary problem, such as multiple clients in a
  50. multiplexer trying to access the clipboard simultaneously.
  51. Terminals should ask the user for permission before allowing a read request.
  52. However, if a read request only wishes to list the available data types on the
  53. clipboard, it should be allowed without a permission prompt. This is so that
  54. the user is not presented with a double permission prompt for reading the
  55. available MIME types and then reading the actual data.
  56. Writing data to the system clipboard
  57. ----------------------------------------
  58. To write data to the system clipboard, the terminal programs sends the
  59. following sequence of packets::
  60. <OSC>5522;type=write<ST>
  61. <OSC>5522;type=wdata:mime=<base64 encoded mime type>;<base 64 encoded chunk of data for this type><ST>
  62. <OSC>5522;type=wdata:mime=<base64 encoded mime type>;<base 64 encoded chunk of data for this type><ST>
  63. .
  64. .
  65. .
  66. <OSC>5522;type=wdata<ST>
  67. The final packet with no mime and no data indicates end of transmission. The
  68. data for every MIME type should be split into chunks of no more than 4096
  69. bytes (4096 is the size of the data before base64 encoding).
  70. All the chunks for a given MIME type must be sent sequentially, before
  71. sending chunks for the next MIME type. After the transmission is complete, the
  72. terminal replies with a single packet indicating success::
  73. <OSC>5522;type=write:status=DONE<ST>
  74. If an error occurs the terminal can, at any time, send an error packet of the
  75. form::
  76. <OSC>5522;type=write:status=ERRORCODE<ST>
  77. Here ``ERRORCODE`` must be one of:
  78. ``status=EIO``
  79. An I/O error occurred while processing the data
  80. ``status=EINVAL``
  81. One of the packets was invalid, usually because of invalid base64 encoding.
  82. ``status=ENOSYS``
  83. The client asked to write to the primary selection with (``loc=primary``) and that is not
  84. available on the system
  85. ``status=EPERM``
  86. Sent if permission to write to the clipboard was denied by the system or
  87. the user.
  88. ``status=EBUSY``
  89. Sent if there is some temporary problem, such as multiple clients in a
  90. multiplexer trying to access the clipboard simultaneously.
  91. Once an error occurs, the terminal must ignore all further OSC 5522 write related packets until it
  92. sees the start of a new write with a ``type=write`` packet.
  93. The client can send to the primary selection instead of the clipboard by adding
  94. ``loc=primary`` to the initial ``type=write`` packet.
  95. Finally, clients have the ability to *alias* MIME types when sending data to
  96. the clipboard. To do that, the client must send a ``type=walias`` packet of the
  97. form::
  98. <OSC>5522;type=walias;mime=<base64 encoded target MIME type>;<base64 encoded, space separated list of aliases><ST>
  99. The effect of an alias is that the system clipboard will make available all the
  100. aliased MIME types, with the same data as was transmitted for the target MIME
  101. type. This saves bandwidth, allowing the client to only transmit one copy of
  102. the data, but create multiple references to it in the system clipboard. Alias
  103. packets can be sent anytime after the initial write packet and before the end
  104. of data packet.
  105. Support for terminal multiplexers
  106. ------------------------------------
  107. Since this protocol involves two way communication between the terminal
  108. emulator and the client program, multiplexers need a way to know which window
  109. to send responses from the terminal to. In order to make this possible, the
  110. metadata portion of this escape code includes an optional ``id`` field. If
  111. present the terminal emulator must send it back unchanged with every response.
  112. Valid ids must include only characters from the set: ``[a-zA-Z0-9-_+.]``. Any
  113. other characters must be stripped out from the id by the terminal emulator
  114. before retransmitting it.
  115. Note that when using a terminal multiplexer it is possible for two different
  116. programs to overwrite each others clipboard requests. This is fundamentally
  117. unavoidable since the system clipboard is a single global shared resource.
  118. However, there is an additional complication where responses form this protocol
  119. could get lost if, for instance, multiple write requests are received
  120. simultaneously. It is up to well designed multiplexers to ensure that only a
  121. single request is in flight at a time. The multiplexer can abort requests by
  122. sending back the ``EBUSY`` error code indicating some other window is trying
  123. to access the clipboard.