162 lines
4.7 KiB
Python
162 lines
4.7 KiB
Python
from enum import Enum
|
|
|
|
from abc import ABC, abstractmethod
|
|
from fabric import Connection
|
|
import re
|
|
|
|
from .xdotool import XDoToolController
|
|
from .python import PythonController
|
|
class MouseClick(Enum):
|
|
LEFT = 1
|
|
MIDDLE = 2
|
|
RIGHT = 3
|
|
WHEEL_UP = 4
|
|
WHEEL_DOWN = 5
|
|
|
|
class AbstractMouseController(ABC):
|
|
@abstractmethod
|
|
def get_mouse(self):
|
|
raise NotImplementedError
|
|
|
|
@abstractmethod
|
|
def mouse_move(self, x: int, y: int):
|
|
raise NotImplementedError
|
|
|
|
@abstractmethod
|
|
def left_down(self):
|
|
raise NotImplementedError
|
|
|
|
@abstractmethod
|
|
def left_up(self):
|
|
raise NotImplementedError
|
|
|
|
@abstractmethod
|
|
def left_click(self):
|
|
raise NotImplementedError
|
|
|
|
@abstractmethod
|
|
def middle_down(self):
|
|
raise NotImplementedError
|
|
|
|
@abstractmethod
|
|
def middle_up(self):
|
|
raise NotImplementedError
|
|
|
|
@abstractmethod
|
|
def middle_click(self):
|
|
raise NotImplementedError
|
|
|
|
@abstractmethod
|
|
def right_down(self):
|
|
raise NotImplementedError
|
|
|
|
@abstractmethod
|
|
def right_up(self):
|
|
raise NotImplementedError
|
|
|
|
@abstractmethod
|
|
def right_click(self):
|
|
raise NotImplementedError
|
|
|
|
@abstractmethod
|
|
def scroll_up(self):
|
|
raise NotImplementedError
|
|
|
|
@abstractmethod
|
|
def scroll_down(self):
|
|
raise NotImplementedError
|
|
|
|
|
|
class XDoToolMouseController(AbstractMouseController, XDoToolController):
|
|
def __init__(self, ssh_connection: Connection):
|
|
super().__init__(ssh_connection=ssh_connection)
|
|
|
|
def get_mouse(self):
|
|
output = self._execute_xdotool_command(f"")
|
|
parts = output.split(" ")
|
|
x = int(parts[0].split(":")[1])
|
|
y = int(parts[1].split(":")[1])
|
|
return x, y
|
|
|
|
def mouse_move(self, x: int, y: int):
|
|
self._execute_xdotool_command(f"mousemove {x} {y}")
|
|
|
|
def left_down(self):
|
|
self._execute_xdotool_command(f"mousedown 1")
|
|
|
|
def left_up(self):
|
|
self._execute_xdotool_command(f"mouseup 1")
|
|
|
|
def left_click(self):
|
|
self._execute_xdotool_command(f"click 1")
|
|
|
|
def middle_down(self):
|
|
self._execute_xdotool_command(f"mousedown 2")
|
|
|
|
def middle_up(self):
|
|
self._execute_xdotool_command(f"mouseup 2")
|
|
|
|
def middle_click(self):
|
|
self._execute_xdotool_command(f"click 2")
|
|
|
|
def right_down(self):
|
|
self._execute_xdotool_command(f"mousedown 3")
|
|
|
|
def right_up(self):
|
|
self._execute_xdotool_command(f"mouseup 3")
|
|
|
|
def right_click(self):
|
|
self._execute_xdotool_command(f"click 3")
|
|
|
|
def scroll_up(self):
|
|
self._execute_xdotool_command(f"click 4")
|
|
|
|
def scroll_down(self):
|
|
self._execute_xdotool_command(f"click 5")
|
|
|
|
class PythonMouseController(AbstractMouseController, PythonController):
|
|
def __init__(self, http_server: str):
|
|
super().__init__(http_server=http_server)
|
|
self.command = "py -c \"import mouse; {command}\""
|
|
|
|
def get_mouse(self):
|
|
response = self._execute_python_command(self.command.format(command=f"print(mouse.get_position())"))
|
|
numbers = re.findall(r'-?\d+', response["output"])
|
|
x, y = map(int, numbers)
|
|
return x, y
|
|
|
|
def mouse_move(self, x: int, y: int):
|
|
self._execute_python_command(self.command.format(command=f"mouse.move({x}, {y})"))
|
|
|
|
def left_down(self):
|
|
self._execute_python_command(self.command.format(command="mouse.press(button='left')"))
|
|
|
|
def left_up(self):
|
|
self._execute_python_command(self.command.format(command="mouse.release(button='left')"))
|
|
|
|
def left_click(self):
|
|
self._execute_python_command(self.command.format(command="mouse.click(button='left')"))
|
|
|
|
def middle_down(self):
|
|
self._execute_python_command(self.command.format(command="mouse.press(button='middle')"))
|
|
|
|
def middle_up(self):
|
|
self._execute_python_command(self.command.format(command="mouse.release(button='middle')"))
|
|
|
|
def middle_click(self):
|
|
self._execute_python_command(self.command.format(command="mouse.click(button='middle')"))
|
|
|
|
def right_down(self):
|
|
self._execute_python_command(self.command.format(command="mouse.press(button='right')"))
|
|
|
|
def right_up(self):
|
|
self._execute_python_command(self.command.format(command="mouse.release(button='right')"))
|
|
|
|
def right_click(self):
|
|
self._execute_python_command(self.command.format(command="mouse.click(button='right')"))
|
|
|
|
def scroll_up(self):
|
|
self._execute_python_command(self.command.format(command="mouse.wheel(10)"))
|
|
|
|
def scroll_down(self):
|
|
self._execute_python_command(self.command.format(command="mouse.wheel(-10)")) |