actions.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. try:
  2. import Xlib.X
  3. import Xlib.protocol.event
  4. except ImportError:
  5. pass
  6. class SelectGagAction:
  7. def __init__(self, factor_x, factor_y):
  8. self._button = Xlib.X.Button1
  9. self._factor_x = factor_x
  10. self._factor_y = factor_y
  11. def execute(self, extended_controls, xkeyevent):
  12. engine_geometry = extended_controls._engine_window.get_geometry()
  13. smaller_dimension = min(engine_geometry.width, engine_geometry.height)
  14. attr = dict(
  15. window=extended_controls.engine_window,
  16. detail=self._button,
  17. state=xkeyevent.state,
  18. event_x=int(engine_geometry.width/2+smaller_dimension*self._factor_x),
  19. event_y=int(engine_geometry.height/2+smaller_dimension*self._factor_y),
  20. # apparently root_x & root_y do not need to correspond with event_x/y.
  21. # attributes are still required to be set.
  22. root_x=0, # xkeyevent.root_x,
  23. root_y=0, # xkeyevent.root_y,
  24. child=xkeyevent.child,
  25. root=xkeyevent.root,
  26. time=xkeyevent.time, # X.CurrentTime
  27. same_screen=xkeyevent.same_screen,
  28. )
  29. if isinstance(xkeyevent, Xlib.protocol.event.KeyPress):
  30. e = Xlib.protocol.event.ButtonPress(**attr)
  31. else:
  32. e = Xlib.protocol.event.ButtonRelease(**attr)
  33. extended_controls.engine_window.send_event(e)
  34. class RewriteKeyEventAction:
  35. def __init__(self, keysym=None):
  36. self._keysym = keysym
  37. def execute(self, extended_controls, xkeyevent):
  38. attr = dict(
  39. window=extended_controls.engine_window,
  40. detail=xkeyevent.detail,
  41. state=xkeyevent.state,
  42. root_x=xkeyevent.root_x,
  43. root_y=xkeyevent.root_y,
  44. event_x=xkeyevent.event_x,
  45. event_y=xkeyevent.event_y,
  46. child=xkeyevent.child,
  47. root=xkeyevent.root,
  48. time=xkeyevent.time, # X.CurrentTime
  49. same_screen=xkeyevent.same_screen,
  50. )
  51. if self._keysym:
  52. attr['detail'] = extended_controls.xdisplay.keysym_to_keycode(
  53. self._keysym)
  54. extended_controls.engine_window.send_event(
  55. type(xkeyevent)(**attr),
  56. )
  57. class ForwardKeyEventAction(RewriteKeyEventAction):
  58. def __init__(self):
  59. super().__init__(keysym=None)
  60. class ToggleExtendedControlsAction:
  61. def execute(self, extended_controls, xkeyevent):
  62. if isinstance(xkeyevent, Xlib.protocol.event.KeyPress):
  63. extended_controls.toggle()