From dfaca37b85cf14bbe29c2e11a7027b85cb2e774b Mon Sep 17 00:00:00 2001 From: Nmstr Date: Sat, 12 Jul 2025 16:54:02 +0200 Subject: [PATCH 1/2] fixed issue with pyproject.toml --- pyproject.toml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 8a883fc..08e2fd3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,5 +1,6 @@ [project] name = "WayKey" +version = "0.1.0" readme = "README.md" description = "Wayland automation" requires-python = ">=3.13" @@ -16,11 +17,10 @@ classifiers = [ "Intended Audience :: Developers", "Intended Audience :: End Users/Desktop", ] -[project.urls] -Source = "https://github.com/Nmstr/WayKey" -Issues = "https://github.com/Nmstr/WayKey/issues" - dependencies = [ "evdev>=1.9.2", "screeninfo>=0.8.1", ] +[project.urls] +Source = "https://github.com/Nmstr/WayKey" +Issues = "https://github.com/Nmstr/WayKey/issues" From f817194a6016f0f7e2af31e23de84411907191fe Mon Sep 17 00:00:00 2001 From: Nmstr Date: Sat, 12 Jul 2025 17:54:34 +0200 Subject: [PATCH 2/2] added first python library implementation --- .gitignore | 2 + WayKey/__init__.py | 3 + WayKey/cli/main.py | 4 +- WayKey/daemon/main.py | 4 +- WayKey/library/commands.py | 114 +++++++++++++++++++++++++++++++++++++ pyproject.toml | 12 ++++ 6 files changed, 135 insertions(+), 4 deletions(-) create mode 100644 WayKey/__init__.py create mode 100644 WayKey/library/commands.py diff --git a/.gitignore b/.gitignore index 5c5ffff..a76caa3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ .venv .idea __pycache__ +WayKey.egg-info +dist diff --git a/WayKey/__init__.py b/WayKey/__init__.py new file mode 100644 index 0000000..8c63170 --- /dev/null +++ b/WayKey/__init__.py @@ -0,0 +1,3 @@ +from .library.commands import click, press, release, mouse_move, get_devices, load_device, unload_device + +__all__ = ["click", "press", "release", "mouse_move", "get_devices", "load_device", "unload_device"] diff --git a/WayKey/cli/main.py b/WayKey/cli/main.py index 85662ed..2d65a61 100644 --- a/WayKey/cli/main.py +++ b/WayKey/cli/main.py @@ -102,7 +102,7 @@ def load_device(device_id: str) -> None: """ response = send_command({ "type": "load_device", - "id": device_id + "device_id": device_id }) if response.get("status") == "success": print(f"Device {device_id} loaded successfully.") @@ -115,7 +115,7 @@ def unload_device(device_id: str) -> None: """ response = send_command({ "type": "unload_device", - "id": device_id + "device_id": device_id }) if response.get("status") == "success": print(f"Device {device_id} unloaded successfully.") diff --git a/WayKey/daemon/main.py b/WayKey/daemon/main.py index c5b79b1..ea82ee8 100644 --- a/WayKey/daemon/main.py +++ b/WayKey/daemon/main.py @@ -47,7 +47,7 @@ def get_devices() -> dict: return {"status": "success", "devices": devices} def load_device(command: dict) -> dict: - new_id = command.get("id", None) + new_id = command.get("device_id", None) if not new_id: return {"status": "error", "message": "Device ID is required"} device_path = get_path_from_id(new_id) @@ -58,7 +58,7 @@ def load_device(command: dict) -> dict: return {"status": "success", "message": f"Device {device_id} loaded"} def unload_device(command: dict) -> dict: - device_id = command.get("id", None) + device_id = command.get("device_id", None) if not is_id_valid(device_id): return {"status": "error", "message": f"Invalid device ID: {device_id}"} if device_id not in input_devices: diff --git a/WayKey/library/commands.py b/WayKey/library/commands.py new file mode 100644 index 0000000..7f301a5 --- /dev/null +++ b/WayKey/library/commands.py @@ -0,0 +1,114 @@ +from evdev import ecodes as e +import socket +import json + +SOCKET_PATH = "/tmp/waykeyd.sock" + +def _send_command(command: dict) -> dict: + """ + Send a command to the daemon and get the response + """ + client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + try: + client.connect(SOCKET_PATH) + client.sendall(json.dumps(command).encode('utf-8')) + response = client.recv(4096).decode('utf-8') + return json.loads(response) + finally: + client.close() + +def _convert_code(code: str) -> int or None: + """ + Convert a key code from string to integer + """ + for key, value in e.keys.items(): + if isinstance(value, tuple): + for v in value: + if v == code: + return key + else: + if value == code: + return key + return None + +def press(code: str | int, device_id: str = "default_device") -> dict: + """ + Press a key + """ + if not isinstance(code, int): + code = _convert_code(code) + if code is None: + raise ValueError(f"Invalid key code: {code}") + return _send_command({ + "type": "press", + "code": code, + "device_id": device_id + }) + +def release(code: str | int, device_id: str = "default_device") -> dict: + """ + Release a key + """ + if not isinstance(code, int): + code = _convert_code(code) + if code is None: + raise ValueError(f"Invalid key code: {code}") + return _send_command({ + "type": "release", + "code": code, + "device_id": device_id + }) + +def click(code: str | int, device_id: str = "default_device", delay: int = 0) -> dict: + """ + Click a key + """ + if not isinstance(code, int): + code = _convert_code(code) + if code is None: + raise ValueError(f"Invalid key code: {code}") + return _send_command({ + "type": "click", + "code": code, + "device_id": device_id, + "delay": delay + }) + +def mouse_move(x: int, y: int, w: int = 0, absolute: bool = False, device_id: str = "default_device") -> dict: + """ + Move the mouse cursor + """ + return _send_command({ + "type": "mouse_move", + "x": x, + "y": y, + "w": w, + "absolute": absolute, + "device_id": device_id + }) + +def get_devices() -> dict: + """ + Get a list of all devices + """ + return _send_command({ + "type": "get_devices" + }) + +def load_device(device_id: str = "default_device") -> dict: + """ + Load a device by its ID + """ + return _send_command({ + "type": "load_device", + "device_id": device_id + }) + +def unload_device(device_id: str = "default_device") -> dict: + """ + Unload a device by its ID + """ + return _send_command({ + "type": "unload_device", + "device_id": device_id + }) diff --git a/pyproject.toml b/pyproject.toml index 08e2fd3..cc0c232 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -24,3 +24,15 @@ dependencies = [ [project.urls] Source = "https://github.com/Nmstr/WayKey" Issues = "https://github.com/Nmstr/WayKey/issues" + +[build-system] +requires = ["uv_build>=0.7.20,<0.8.0"] +build-backend = "uv_build" + +[tool.uv.build-backend] +module-root = "" +module-name = "WayKey" +source-exclude = [ + "daemon", + "cli" +]