_cli.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. """
  2. rescriptoon
  3. Copyright (C) 2018-2019 Fabian Peter Hammerle <fabian@hammerle.me>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. """
  15. import argparse
  16. import logging
  17. import Xlib.display
  18. from Xlib import XK, X
  19. import rescriptoon
  20. from rescriptoon._keys import USKeyCode, invert_string_to_keysym
  21. _DEFAULT_TOGGLE_KEYCODE: int = USKeyCode.grave.value
  22. _VERBOSITY_LEVELS = [logging.WARNING, logging.INFO, logging.DEBUG]
  23. def main() -> None:
  24. display = Xlib.display.Display()
  25. argparser = argparse.ArgumentParser(
  26. description="Attach rescriptoon to running Toontown Rewritten engines.",
  27. )
  28. default_toggle_keysym = display.keycode_to_keysym(_DEFAULT_TOGGLE_KEYCODE, index=0)
  29. argparser.add_argument(
  30. "--toggle",
  31. "-t",
  32. metavar="KEYSYM_NAME",
  33. dest="toggle_keysym_name",
  34. default=invert_string_to_keysym(default_toggle_keysym),
  35. help="key to turn extended keyboard controls on / off."
  36. + " any keysym name may be used"
  37. + " (see XStringToKeysym & X11/keysymdef.h, "
  38. + " default: %(default)s)",
  39. )
  40. argparser.add_argument(
  41. "--verbose",
  42. "-v",
  43. action="count",
  44. dest="verbosity",
  45. default=0,
  46. help="repeat to further increase verbosity",
  47. )
  48. args = argparser.parse_args()
  49. logging.basicConfig(
  50. format="%(asctime)s %(levelname)s: %(message)s",
  51. datefmt="%H:%I:%S",
  52. level=_VERBOSITY_LEVELS[min(args.verbosity, len(_VERBOSITY_LEVELS) - 1)],
  53. )
  54. toggle_keysym = XK.string_to_keysym(args.toggle_keysym_name)
  55. if toggle_keysym == X.NoSymbol:
  56. raise ValueError(
  57. "controls toggle: unknown keysym name '{}'".format(args.toggle_keysym_name)
  58. )
  59. rescriptoon.Overlay(
  60. display=display, toggle_keycode=display.keysym_to_keycode(toggle_keysym)
  61. ).run()