cursors.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #!/usr/bin/env python
  2. # License: GPLv3 Copyright: 2023, Kovid Goyal <kovid at kovidgoyal.net>
  3. import os
  4. import subprocess
  5. import sys
  6. if __name__ == '__main__' and not __package__:
  7. import __main__
  8. __main__.__package__ = 'gen'
  9. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  10. from .key_constants import patch_file
  11. # References for these names:
  12. # CSS:choices_for_{option.name} https://developer.mozilla.org/en-US/docs/Web/CSS/cursor
  13. # XCursor: https://tronche.com/gui/x/xlib/appendix/b/ + Absolute chaos
  14. # Wayland: https://wayland.app/protocols/cursor-shape-v1
  15. # Cocoa: https://developer.apple.com/documentation/appkit/nscursor + secret apple selectors + SDL_cocoamouse.m
  16. # kitty_names CSS_name XCursor_names Wayland_name Cocoa_name
  17. cursors = '''\
  18. arrow default default,!left_ptr default arrowCursor
  19. beam,text text text,!xterm,ibeam text IBeamCursor
  20. pointer,hand pointer pointing_hand,pointer,!hand2,hand pointer pointingHandCursor
  21. help help help,!question_arrow,whats_this help help:arrowCursor
  22. wait wait wait,!clock,watch wait busybutclickable:arrowCursor
  23. progress progress progress,half-busy,left_ptr_watch progress busybutclickable:arrowCursor
  24. crosshair crosshair crosshair,!tcross crosshair crosshairCursor
  25. cell cell cell,!plus,!cross cell cell:crosshairCursor
  26. vertical-text vertical-text vertical-text vertical-text IBeamCursorForVerticalLayout
  27. move move move,!fleur,pointer-move move move:openHandCursor
  28. e-resize e-resize e-resize,!right_side e_resize resizeRightCursor
  29. ne-resize ne-resize ne-resize,!top_right_corner ne_resize resizenortheast:_windowResizeNorthEastSouthWestCursor
  30. nw-resize nw-resize nw-resize,!top_left_corner nw_resize resizenorthwest:_windowResizeNorthWestSouthEastCursor
  31. n-resize n-resize n-resize,!top_side n_resize resizeUpCursor
  32. se-resize se-resize se-resize,!bottom_right_corner se_resize resizesoutheast:_windowResizeNorthWestSouthEastCursor
  33. sw-resize sw-resize sw-resize,!bottom_left_corner sw_resize resizesouthwest:_windowResizeNorthEastSouthWestCursor
  34. s-resize s-resize s-resize,!bottom_side s_resize resizeDownCursor
  35. w-resize w-resize w-resize,!left_side w_resize resizeLeftCursor
  36. ew-resize ew-resize ew-resize,!sb_h_double_arrow,split_h ew_resize resizeLeftRightCursor
  37. ns-resize ns-resize ns-resize,!sb_v_double_arrow,split_v ns_resize resizeUpDownCursor
  38. nesw-resize nesw-resize nesw-resize,size_bdiag,size-bdiag nesw_resize _windowResizeNorthEastSouthWestCursor
  39. nwse-resize nwse-resize nwse-resize,size_fdiag,size-fdiag nwse_resize _windowResizeNorthWestSouthEastCursor
  40. zoom-in zoom-in zoom-in,zoom_in zoom_in zoomin:arrowCursor
  41. zoom-out zoom-out zoom-out,zoom_out zoom_out zoomout:arrowCursor
  42. alias alias dnd-link alias dragLinkCursor
  43. copy copy dnd-copy copy dragCopyCursor
  44. not-allowed not-allowed not-allowed,forbidden,crossed_circle not_allowed operationNotAllowedCursor
  45. no-drop no-drop no-drop,dnd-no-drop no_drop operationNotAllowedCursor
  46. grab grab grab,openhand,!hand1 grab openHandCursor
  47. grabbing grabbing grabbing,closedhand,dnd-none grabbing closedHandCursor
  48. '''
  49. def main(args: list[str]=sys.argv) -> None:
  50. glfw_enum = []
  51. css_names = []
  52. glfw_xc_map = {}
  53. glfw_xfont_map = []
  54. kitty_to_enum_map = {}
  55. enum_to_glfw_map = {}
  56. enum_to_css_map = {}
  57. glfw_cocoa_map = {}
  58. glfw_css_map = {}
  59. css_to_enum = {}
  60. xc_to_enum = {}
  61. glfw_wayland = {}
  62. for line in cursors.splitlines():
  63. line = line.strip()
  64. if line:
  65. names_, css, xc_, wayland, cocoa = line.split()
  66. names, xc = names_.split(','), xc_.split(',')
  67. base = css.replace('-', '_').upper()
  68. glfw_name = 'GLFW_' + base + '_CURSOR'
  69. enum_name = base + '_POINTER'
  70. enum_to_glfw_map[enum_name] = glfw_name
  71. enum_to_css_map[enum_name] = css
  72. glfw_css_map[glfw_name] = css
  73. css_to_enum[css] = enum_name
  74. css_names.append(css)
  75. glfw_wayland[glfw_name] = 'WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_' + wayland.replace('-', '_').upper()
  76. for n in names:
  77. kitty_to_enum_map[n] = enum_name
  78. glfw_enum.append(glfw_name)
  79. glfw_xc_map[glfw_name] = ', '.join(f'''"{x.replace('!', '')}"''' for x in xc)
  80. for x in xc:
  81. if x.startswith('!'):
  82. glfw_xfont_map.append(f"case {glfw_name}: return set_cursor_from_font(cursor, {'XC_' + x[1:]});")
  83. break
  84. else:
  85. items = tuple('"' + x.replace('!', '') + '"' for x in xc)
  86. glfw_xfont_map.append(f'case {glfw_name}: return try_cursor_names(cursor, {len(items)}, {", ".join(items)});')
  87. for x in xc:
  88. x = x.lstrip('!')
  89. xc_to_enum[x] = enum_name
  90. parts = cocoa.split(':', 1)
  91. if len(parts) == 1:
  92. if parts[0].startswith('_'):
  93. glfw_cocoa_map[glfw_name] = f'U({glfw_name}, {parts[0]});'
  94. else:
  95. glfw_cocoa_map[glfw_name] = f'C({glfw_name}, {parts[0]});'
  96. else:
  97. glfw_cocoa_map[glfw_name] = f'S({glfw_name}, {parts[0]}, {parts[1]});'
  98. for x, v in xc_to_enum.items():
  99. if x not in css_to_enum:
  100. css_to_enum[x] = v
  101. glfw_enum.append('GLFW_INVALID_CURSOR')
  102. patch_file('glfw/glfw3.h', 'mouse cursor shapes', '\n'.join(f' {x},' for x in glfw_enum))
  103. patch_file('glfw/wl_window.c', 'glfw to wayland mapping', '\n'.join(f' C({g}, {x});' for g, x in glfw_wayland.items()))
  104. patch_file('glfw/wl_window.c', 'glfw to xc mapping', '\n'.join(f' C({g}, {x});' for g, x in glfw_xc_map.items()))
  105. patch_file('glfw/x11_window.c', 'glfw to xc mapping', '\n'.join(f' {x}' for x in glfw_xfont_map))
  106. patch_file('kitty/data-types.h', 'mouse shapes', '\n'.join(f' {x},' for x in enum_to_glfw_map))
  107. patch_file(
  108. 'kitty/options/definition.py', 'pointer shape names', '\n'.join(f' {x!r},' for x in kitty_to_enum_map),
  109. start_marker='# ', end_marker='',
  110. )
  111. patch_file('kitty/options/to-c.h', 'pointer shapes', '\n'.join(
  112. f' else if (strcmp(name, "{k}") == 0) return {v};' for k, v in kitty_to_enum_map.items()))
  113. patch_file('kitty/glfw.c', 'enum to glfw', '\n'.join(
  114. f' case {k}: set_glfw_mouse_cursor(w, {v}); break;' for k, v in enum_to_glfw_map.items()))
  115. patch_file('kitty/glfw.c', 'name to glfw', '\n'.join(
  116. f' if (strcmp(name, "{k}") == 0) return {enum_to_glfw_map[v]};' for k, v in kitty_to_enum_map.items()))
  117. patch_file('kitty/glfw.c', 'glfw to css', '\n'.join(
  118. f' case {g}: return "{c}";' for g, c in glfw_css_map.items()
  119. ))
  120. patch_file('kitty/screen.c', 'enum to css', '\n'.join(
  121. f' case {e}: ans = "{c}"; break;' for e, c in enum_to_css_map.items()))
  122. patch_file('kitty/screen.c', 'css to enum', '\n'.join(
  123. f' else if (strcmp("{c}", css_name) == 0) s = {e};' for c, e in css_to_enum.items()))
  124. patch_file('glfw/cocoa_window.m', 'glfw to cocoa', '\n'.join(f' {x}' for x in glfw_cocoa_map.values()))
  125. patch_file('docs/pointer-shapes.rst', 'list of shape css names', '\n'.join(
  126. f'#. {x}' if x else '' for x in [''] + sorted(css_names) + ['']), start_marker='.. ', end_marker='')
  127. patch_file('tools/tui/loop/mouse.go', 'pointer shape enum', '\n'.join(
  128. f'\t{x} PointerShape = {i}' for i, x in enumerate(enum_to_glfw_map)), start_marker='// ', end_marker='')
  129. patch_file('tools/tui/loop/mouse.go', 'pointer shape tostring', '\n'.join(
  130. f'''\tcase {x}: return "{x.lower().rpartition('_')[0].replace('_', '-')}"''' for x in enum_to_glfw_map), start_marker='// ', end_marker='')
  131. patch_file('tools/cmd/mouse_demo/main.go', 'all pointer shapes', '\n'.join(
  132. f'\tloop.{x},' for x in enum_to_glfw_map), start_marker='// ', end_marker='')
  133. subprocess.check_call(['glfw/glfw.py'])
  134. if __name__ == '__main__':
  135. import runpy
  136. m = runpy.run_path(os.path.dirname(os.path.abspath(__file__)))
  137. m['main']([sys.executable, 'cursors'])