clipboard.rst 6.8 KB

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