actions.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. try:
  2. import Xlib.X
  3. import Xlib.protocol.event
  4. except ImportError:
  5. pass
  6. class TargetEngine:
  7. Primary = -1
  8. All = -3
  9. class EngineAction:
  10. def __init__(self, target_engine_index):
  11. self._target_engine_index = target_engine_index
  12. def execute_on_window(self, extended_controls, xkeyevent, engine_window):
  13. raise Exception('abstract method')
  14. def execute(self, extended_controls, xkeyevent):
  15. if self._target_engine_index == TargetEngine.Primary:
  16. target_windows = [extended_controls.primary_engine_window]
  17. elif self._target_engine_index == TargetEngine.All:
  18. target_windows = extended_controls.engine_windows
  19. else:
  20. target_windows = extended_controls.engine_windows_by_target_index.get(
  21. self._target_engine_index,
  22. [],
  23. )
  24. for target_window in target_windows:
  25. self.execute_on_window(extended_controls, xkeyevent, target_window)
  26. class SelectGagAction(EngineAction):
  27. def __init__(self, target_engine_index, factor_x, factor_y):
  28. super().__init__(
  29. target_engine_index=target_engine_index,
  30. )
  31. self._button = Xlib.X.Button1
  32. self._factor_x = factor_x
  33. self._factor_y = factor_y
  34. def execute_on_window(self, extended_controls, xkeyevent, engine_window):
  35. engine_geometry = engine_window.get_geometry()
  36. smaller_dimension = min(engine_geometry.width, engine_geometry.height)
  37. attr = dict(
  38. window=engine_window,
  39. detail=self._button,
  40. state=xkeyevent.state,
  41. event_x=int(engine_geometry.width / 2
  42. + smaller_dimension * self._factor_x),
  43. event_y=int(engine_geometry.height / 2
  44. + smaller_dimension * self._factor_y),
  45. # apparently root_x & root_y do not need to correspond with event_x/y.
  46. # attributes are still required to be set.
  47. root_x=0, # xkeyevent.root_x,
  48. root_y=0, # xkeyevent.root_y,
  49. child=xkeyevent.child,
  50. root=xkeyevent.root,
  51. time=xkeyevent.time, # X.CurrentTime
  52. same_screen=xkeyevent.same_screen,
  53. )
  54. if isinstance(xkeyevent, Xlib.protocol.event.KeyPress):
  55. e = Xlib.protocol.event.ButtonPress(**attr)
  56. else:
  57. e = Xlib.protocol.event.ButtonRelease(**attr)
  58. engine_window.send_event(e)
  59. class RewriteKeyEventAction(EngineAction):
  60. def __init__(self, target_engine_index, keysym=None):
  61. super().__init__(
  62. target_engine_index=target_engine_index,
  63. )
  64. self._keysym = keysym
  65. def execute_on_window(self, extended_controls, xkeyevent, engine_window):
  66. engine_window.send_event(type(xkeyevent)(
  67. window=engine_window,
  68. detail=extended_controls.xdisplay.keysym_to_keycode(self._keysym)
  69. if self._keysym else xkeyevent.detail,
  70. state=xkeyevent.state,
  71. root_x=xkeyevent.root_x,
  72. root_y=xkeyevent.root_y,
  73. event_x=xkeyevent.event_x,
  74. event_y=xkeyevent.event_y,
  75. child=xkeyevent.child,
  76. root=xkeyevent.root,
  77. time=xkeyevent.time, # X.CurrentTime
  78. same_screen=xkeyevent.same_screen,
  79. ))
  80. class ForwardKeyEventAction(RewriteKeyEventAction):
  81. def __init__(self):
  82. super().__init__(
  83. keysym=None,
  84. target_engine_index=TargetEngine.Primary,
  85. )
  86. class ToggleExtendedControlsAction:
  87. def execute(self, extended_controls, xkeyevent):
  88. if isinstance(xkeyevent, Xlib.protocol.event.KeyPress):
  89. extended_controls.toggle()