desktop-notifications.rst 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. .. _desktop_notifications:
  2. Desktop notifications
  3. =======================
  4. |kitty| implements an extensible escape code (OSC 99) to show desktop
  5. notifications. It is easy to use from shell scripts and fully extensible to show
  6. title and body. Clicking on the notification can optionally focus the window it
  7. came from, and/or send an escape code back to the application running in that
  8. window.
  9. The design of the escape code is partially based on the discussion in the
  10. defunct `terminal-wg <https://gitlab.freedesktop.org/terminal-wg/specifications/-/issues/13>`__
  11. The escape code has the form::
  12. <OSC> 99 ; metadata ; payload <terminator>
  13. Here ``<OSC>`` is :code:`<ESC>]` and ``<terminator>`` is
  14. :code:`<ESC><backslash>`. The ``metadata`` is a section of colon separated
  15. :code:`key=value` pairs. Every key must be a single character from the set
  16. :code:`a-zA-Z` and every value must be a word consisting of characters from
  17. the set :code:`a-zA-Z0-9-_/\+.,(){}[]*&^%$#@!`~`. The payload must be
  18. interpreted based on the metadata section. The two semi-colons *must* always be
  19. present even when no metadata is present.
  20. Before going into details, lets see how one can display a simple, single line
  21. notification from a shell script::
  22. printf '\x1b]99;;Hello world\x1b\\'
  23. To show a message with a title and a body::
  24. printf '\x1b]99;i=1:d=0;Hello world\x1b\\'
  25. printf '\x1b]99;i=1:d=1:p=body;This is cool\x1b\\'
  26. The most important key in the metadata is the ``p`` key, it controls how the
  27. payload is interpreted. A value of ``title`` means the payload is setting the
  28. title for the notification. A value of ``body`` means it is setting the body,
  29. and so on, see the table below for full details.
  30. The design of the escape code is fundamentally chunked, this is because
  31. different terminal emulators have different limits on how large a single escape
  32. code can be. Chunking is accomplished by the ``i`` and ``d`` keys. The ``i``
  33. key is the *notification id* which can be any string containing the characters
  34. ``[a-zA-Z0-9_-+.]``. The ``d`` key stands for *done* and can only take the
  35. values ``0`` and ``1``. A value of ``0`` means the notification is not yet done
  36. and the terminal emulator should hold off displaying it. A value of ``1`` means
  37. the notification is done, and should be displayed. You can specify the title or
  38. body multiple times and the terminal emulator will concatenate them, thereby
  39. allowing arbitrarily long text (terminal emulators are free to impose a sensible
  40. limit to avoid Denial-of-Service attacks). The size of the payload must be no
  41. longer than ``2048`` bytes, *before being encoded*.
  42. Both the ``title`` and ``body`` payloads must be either UTF-8 encoded plain
  43. text with no embedded escape codes, or UTF-8 text that is Base64 encoded, in
  44. which case there must be an ``e=1`` key in the metadata to indicate the payload
  45. is Base64 encoded.
  46. When the user clicks the notification, a couple of things can happen, the
  47. terminal emulator can focus the window from which the notification came, and/or
  48. it can send back an escape code to the application indicating the notification
  49. was activated. This is controlled by the ``a`` key which takes a comma separated
  50. set of values, ``report`` and ``focus``. The value ``focus`` means focus the
  51. window from which the notification was issued and is the default. ``report``
  52. means send an escape code back to the application. The format of the returned
  53. escape code is::
  54. <OSC> 99 ; i=identifier ; <terminator>
  55. The value of ``identifier`` comes from the ``i`` key in the escape code sent by
  56. the application. If the application sends no identifier, then the terminal
  57. *must* use ``i=0``. Actions can be preceded by a negative sign to turn them
  58. off, so for example if you do not want any action, turn off the default
  59. ``focus`` action with::
  60. a=-focus
  61. Complete specification of all the metadata keys is in the table below. If a
  62. terminal emulator encounters a key in the metadata it does not understand,
  63. the key *must* be ignored, to allow for future extensibility of this escape
  64. code. Similarly if values for known keys are unknown, the terminal emulator
  65. *should* either ignore the entire escape code or perform a best guess effort
  66. to display it based on what it does understand.
  67. .. note::
  68. It is possible to extend this escape code to allow specifying an icon for
  69. the notification, however, given that some platforms, such as legacy versions
  70. of macOS, don't allow displaying custom images on a notification, it was
  71. decided to leave it out of the spec for the time being.
  72. Similarly, features such as scheduled notifications could be added in future
  73. revisions.
  74. ======= ==================== ========== =================
  75. Key Value Default Description
  76. ======= ==================== ========== =================
  77. ``a`` Comma separated list ``focus`` What action to perform when the
  78. of ``report``, notification is clicked
  79. ``focus``, with
  80. optional leading
  81. ``-``
  82. ``d`` ``0`` or ``1`` ``1`` Indicates if the notification is
  83. complete or not.
  84. ``e`` ``0`` or ``1`` ``0`` If set to ``1`` means the payload is Base64 encoded UTF-8,
  85. otherwise it is plain UTF-8 text with no C0 control codes in it
  86. ``i`` ``[a-zA-Z0-9-_+.]`` ``0`` Identifier for the notification
  87. ``p`` One of ``title`` or ``title`` Whether the payload is the notification title or body. If a
  88. ``body``. notification has no title, the body will be used as title.
  89. ``o`` One of ``always``, ``always`` When to honor the notification request. ``unfocused`` means when the window
  90. ``unfocused`` or the notification is sent on does not have keyboard focus. ``invisible``
  91. ``invisible`` means the window both is unfocused
  92. and not visible to the user, for example, because it is in an inactive tab or
  93. its OS window is not currently active.
  94. ``always`` is the default and always honors the request.
  95. ``u`` ``0, 1 or 2`` ``unset`` The *urgency* of the notification. ``0`` is low, ``1`` is normal and ``2`` is critical.
  96. If not specified normal is used.
  97. ======= ==================== ========== =================
  98. .. versionadded:: 0.35.0
  99. Support for the ``u`` key to specify urgency
  100. .. versionadded:: 0.31.0
  101. Support for the ``o`` key to prevent notifications from focused windows
  102. .. note::
  103. |kitty| also supports the `legacy OSC 9 protocol developed by iTerm2
  104. <https://iterm2.com/documentation-escape-codes.html>`__ for desktop
  105. notifications.