main.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #!/usr/bin/env python
  2. # License: GPL v3 Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net>
  3. import sys
  4. from collections.abc import Callable
  5. from typing import Any
  6. from kitty.cli import parse_args
  7. from kitty.cli_stub import PanelCLIOptions
  8. from kitty.constants import appname, is_macos, is_wayland
  9. from kitty.fast_data_types import (
  10. GLFW_EDGE_BOTTOM,
  11. GLFW_EDGE_LEFT,
  12. GLFW_EDGE_NONE,
  13. GLFW_EDGE_RIGHT,
  14. GLFW_EDGE_TOP,
  15. GLFW_FOCUS_EXCLUSIVE,
  16. GLFW_FOCUS_NOT_ALLOWED,
  17. GLFW_FOCUS_ON_DEMAND,
  18. GLFW_LAYER_SHELL_BACKGROUND,
  19. GLFW_LAYER_SHELL_OVERLAY,
  20. GLFW_LAYER_SHELL_PANEL,
  21. GLFW_LAYER_SHELL_TOP,
  22. glfw_primary_monitor_size,
  23. make_x11_window_a_dock_window,
  24. )
  25. from kitty.os_window_size import WindowSizeData, edge_spacing
  26. from kitty.types import LayerShellConfig
  27. from kitty.typing import EdgeLiteral
  28. OPTIONS = r'''
  29. --lines
  30. type=int
  31. default=1
  32. The number of lines shown in the panel. Ignored for background and vertical panels.
  33. --columns
  34. type=int
  35. default=1
  36. The number of columns shown in the panel. Ignored for background and horizontal panels.
  37. --margin-top
  38. type=int
  39. default=0
  40. Request a given top margin to the compositor.
  41. Only works on a Wayland compositor that supports the wlr layer shell protocol.
  42. --margin-left
  43. type=int
  44. default=0
  45. Request a given left margin to the compositor.
  46. Only works on a Wayland compositor that supports the wlr layer shell protocol.
  47. --margin-bottom
  48. type=int
  49. default=0
  50. Request a given bottom margin to the compositor.
  51. Only works on a Wayland compositor that supports the wlr layer shell protocol.
  52. --margin-right
  53. type=int
  54. default=0
  55. Request a given right margin to the compositor.
  56. Only works on a Wayland compositor that supports the wlr layer shell protocol.
  57. --edge
  58. choices=top,bottom,left,right,background,none
  59. default=top
  60. Which edge of the screen to place the panel on. Note that some window managers
  61. (such as i3) do not support placing docked windows on the left and right edges.
  62. The value :code:`background` means make the panel the "desktop wallpaper". This
  63. is only supported on Wayland, not X11 and note that when using sway if you set
  64. a background in your sway config it will cover the background drawn using this
  65. kitten.
  66. The value :code:`none` anchors the panel to the top left corner by default
  67. and the panel should be placed using margins parameters.
  68. --layer
  69. choices=background,bottom,top,overlay
  70. default=bottom
  71. On a Wayland compositor that supports the wlr layer shell protocol, specifies the layer
  72. on which the panel should be drawn. This parameter is ignored and set to
  73. :code:`background` if :option:`--edge` is set to :code:`background`.
  74. --config -c
  75. type=list
  76. Path to config file to use for kitty when drawing the panel.
  77. --override -o
  78. type=list
  79. Override individual kitty configuration options, can be specified multiple times.
  80. Syntax: :italic:`name=value`. For example: :option:`kitty +kitten panel -o` font_size=20
  81. --output-name
  82. On Wayland, the panel can only be displayed on a single monitor (output) at a time. This allows
  83. you to specify which output is used, by name. If not specified the compositor will choose an
  84. output automatically, typically the last output the user interacted with or the primary monitor.
  85. --class
  86. dest=cls
  87. default={appname}-panel
  88. condition=not is_macos
  89. Set the class part of the :italic:`WM_CLASS` window property. On Wayland, it sets the app id.
  90. --name
  91. condition=not is_macos
  92. Set the name part of the :italic:`WM_CLASS` property (defaults to using the value from :option:`{appname} --class`)
  93. --focus-policy
  94. choices=not-allowed,exclusive,on-demand
  95. default=not-allowed
  96. On a Wayland compositor that supports the wlr layer shell protocol, specify the focus policy for keyboard
  97. interactivity with the panel. Please refer to the wlr layer shell protocol documentation for more details.
  98. --exclusive-zone
  99. type=int
  100. default=-1
  101. On a Wayland compositor that supports the wlr layer shell protocol, request a given exclusive zone for the panel.
  102. Please refer to the wlr layer shell documentation for more details on the meaning of exclusive and its value.
  103. If :option:`--edge` is set to anything else than :code:`none`, this flag will not have any effect unless
  104. the flag :option:`--override-exclusive-zone` is also set.
  105. If :option:`--edge` is set to :code:`background`, this option has no effect.
  106. --override-exclusive-zone
  107. type=bool-set
  108. On a Wayland compositor that supports the wlr layer shell protocol, override the default exclusive zone.
  109. This has effect only if :option:`--edge` is set to :code:`top`, :code:`left`, :code:`bottom` or :code:`right`.
  110. --debug-rendering
  111. type=bool-set
  112. For internal debugging use.
  113. '''.format(appname=appname).format
  114. args = PanelCLIOptions()
  115. help_text = 'Use a command line program to draw a GPU accelerated panel on your X11 desktop'
  116. usage = 'program-to-run'
  117. def parse_panel_args(args: list[str]) -> tuple[PanelCLIOptions, list[str]]:
  118. return parse_args(args, OPTIONS, usage, help_text, 'kitty +kitten panel', result_class=PanelCLIOptions)
  119. Strut = tuple[int, int, int, int, int, int, int, int, int, int, int, int]
  120. def create_strut(
  121. win_id: int,
  122. left: int = 0, right: int = 0, top: int = 0, bottom: int = 0, left_start_y: int = 0, left_end_y: int = 0,
  123. right_start_y: int = 0, right_end_y: int = 0, top_start_x: int = 0, top_end_x: int = 0,
  124. bottom_start_x: int = 0, bottom_end_x: int = 0
  125. ) -> Strut:
  126. return left, right, top, bottom, left_start_y, left_end_y, right_start_y, right_end_y, top_start_x, top_end_x, bottom_start_x, bottom_end_x
  127. def create_top_strut(win_id: int, width: int, height: int) -> Strut:
  128. return create_strut(win_id, top=height, top_end_x=width)
  129. def create_bottom_strut(win_id: int, width: int, height: int) -> Strut:
  130. return create_strut(win_id, bottom=height, bottom_end_x=width)
  131. def create_left_strut(win_id: int, width: int, height: int) -> Strut:
  132. return create_strut(win_id, left=width, left_end_y=height)
  133. def create_right_strut(win_id: int, width: int, height: int) -> Strut:
  134. return create_strut(win_id, right=width, right_end_y=height)
  135. window_width = window_height = 0
  136. def setup_x11_window(win_id: int) -> None:
  137. if is_wayland():
  138. return
  139. func = globals()[f'create_{args.edge}_strut']
  140. strut = func(win_id, window_width, window_height)
  141. make_x11_window_a_dock_window(win_id, strut)
  142. def initial_window_size_func(opts: WindowSizeData, cached_values: dict[str, Any]) -> Callable[[int, int, float, float, float, float], tuple[int, int]]:
  143. def es(which: EdgeLiteral) -> float:
  144. return edge_spacing(which, opts)
  145. def initial_window_size(cell_width: int, cell_height: int, dpi_x: float, dpi_y: float, xscale: float, yscale: float) -> tuple[int, int]:
  146. if not is_macos and not is_wayland():
  147. # Not sure what the deal with scaling on X11 is
  148. xscale = yscale = 1
  149. global window_width, window_height
  150. monitor_width, monitor_height = glfw_primary_monitor_size()
  151. if args.edge in {'top', 'bottom'}:
  152. spacing = es('top') + es('bottom')
  153. window_height = int(cell_height * args.lines / yscale + (dpi_y / 72) * spacing + 1)
  154. window_width = monitor_width
  155. elif args.edge == 'background':
  156. window_width, window_height = monitor_width, monitor_height
  157. else:
  158. spacing = es('left') + es('right')
  159. window_width = int(cell_width * args.lines / xscale + (dpi_x / 72) * spacing + 1)
  160. window_height = monitor_height
  161. return window_width, window_height
  162. return initial_window_size
  163. def layer_shell_config(opts: PanelCLIOptions) -> LayerShellConfig:
  164. ltype = {'background': GLFW_LAYER_SHELL_BACKGROUND,
  165. 'bottom': GLFW_LAYER_SHELL_PANEL,
  166. 'top': GLFW_LAYER_SHELL_TOP,
  167. 'overlay': GLFW_LAYER_SHELL_OVERLAY}.get(opts.layer, GLFW_LAYER_SHELL_PANEL)
  168. ltype = GLFW_LAYER_SHELL_BACKGROUND if opts.edge == 'background' else ltype
  169. edge = {
  170. 'top': GLFW_EDGE_TOP, 'bottom': GLFW_EDGE_BOTTOM, 'left': GLFW_EDGE_LEFT, 'right': GLFW_EDGE_RIGHT, 'none': GLFW_EDGE_NONE
  171. }.get(opts.edge, GLFW_EDGE_TOP)
  172. focus_policy = {
  173. 'not-allowed': GLFW_FOCUS_NOT_ALLOWED, 'exclusive': GLFW_FOCUS_EXCLUSIVE, 'on-demand': GLFW_FOCUS_ON_DEMAND
  174. }.get(opts.focus_policy, GLFW_FOCUS_NOT_ALLOWED)
  175. return LayerShellConfig(type=ltype,
  176. edge=edge,
  177. x_size_in_cells=max(1, opts.columns),
  178. y_size_in_cells=max(1, opts.lines),
  179. requested_top_margin=max(0, opts.margin_top),
  180. requested_left_margin=max(0, opts.margin_left),
  181. requested_bottom_margin=max(0, opts.margin_bottom),
  182. requested_right_margin=max(0, opts.margin_right),
  183. focus_policy=focus_policy,
  184. requested_exclusive_zone=opts.exclusive_zone,
  185. override_exclusive_zone=opts.override_exclusive_zone,
  186. output_name=opts.output_name or '')
  187. def main(sys_args: list[str]) -> None:
  188. global args
  189. if is_macos:
  190. raise SystemExit('Currently the panel kitten is not supported on macOS')
  191. args, items = parse_panel_args(sys_args[1:])
  192. if not items:
  193. raise SystemExit('You must specify the program to run')
  194. sys.argv = ['kitty']
  195. if args.debug_rendering:
  196. sys.argv.append('--debug-rendering')
  197. for config in args.config:
  198. sys.argv.extend(('--config', config))
  199. sys.argv.extend(('--class', args.cls))
  200. if args.name:
  201. sys.argv.extend(('--name', args.name))
  202. for override in args.override:
  203. sys.argv.extend(('--override', override))
  204. sys.argv.append('--override=linux_display_server=auto')
  205. sys.argv.extend(items)
  206. from kitty.main import main as real_main
  207. from kitty.main import run_app
  208. run_app.cached_values_name = 'panel'
  209. run_app.layer_shell_config = layer_shell_config(args)
  210. run_app.first_window_callback = setup_x11_window
  211. run_app.initial_window_size_func = initial_window_size_func
  212. real_main()
  213. if __name__ == '__main__':
  214. main(sys.argv)
  215. elif __name__ == '__doc__':
  216. cd: dict = sys.cli_docs # type: ignore
  217. cd['usage'] = usage
  218. cd['options'] = OPTIONS
  219. cd['help_text'] = help_text
  220. cd['short_desc'] = help_text