commit cf0af3737dd466b7f81b3c337212274126edc99e Author: 0m.ax Date: Sat Jul 19 23:33:47 2025 +0200 init diff --git a/main.py b/main.py new file mode 100644 index 0000000..41b61f8 --- /dev/null +++ b/main.py @@ -0,0 +1,102 @@ +#!/usr/bin/env python3 + +import socket +import struct +import sys +import libevdev +#from libevdev import ecodes + +def main(device_path, dest_ip, dest_port,username,password): + """ + Reads absolute X and Y coordinates from an evdev device and sends them + over UDP to a specified address and port. + + Args: + device_path (str): The path to the input event device + (e.g., /dev/input/event0). + dest_ip (str): The destination IP address. + dest_port (int): The destination port. + """ + try: + # Open the evdev device in a non-blocking way + fd = open(device_path, 'rb') + d = libevdev.Device(fd) + print(f"Successfully opened device: {d.name}") + + + print("Listening for ABS_X and ABS_Y events. Press Ctrl+C to exit.") + + # Create a UDP socket + sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + print(f"Sending coordinates to {dest_ip}:{dest_port}") + + # Variables to store the latest coordinates + x_coor = None + y_coor = None + + # Loop indefinitely to read events + while True: + for e in d.events(): + # We are only interested in absolute axis events + if e.type == libevdev.EV_ABS: + if e.code == libevdev.EV_ABS.ABS_X: + x_coor = e.value + elif e.code == libevdev.EV_ABS.ABS_Y: + y_coor = e.value + + # When we have both coordinates, send them + if x_coor is not None and y_coor is not None: + print(f"Read coordinates: X={x_coor}, Y={y_coor}") + + # Pack the two 16-bit unsigned integers into a buffer. + # The format string 'HH' specifies two unsigned short integers (u16). + # The '<' character ensures little-endian byte order. + try: + packet = struct.pack(' ") + print("Example: sudo python3 evdev_udp_sender.py /dev/input/event4 127.0.0.1 12345") + sys.exit(1) + + device = sys.argv[1] + ip = sys.argv[2] + try: + port = int(sys.argv[3]) + if not (0 < port < 65536): + raise ValueError + except ValueError: + print("Error: Port must be an integer between 1 and 65535.") + sys.exit(1) + + main(device, ip, port) + diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..0469240 --- /dev/null +++ b/shell.nix @@ -0,0 +1,13 @@ +let + pkgs = import {}; +in pkgs.mkShell { + packages = [ pkgs.libinput + (pkgs.python3.withPackages (python-pkgs: [ +python-pkgs.paho-mqtt +pkgs.libinput +python-pkgs.libevdev + ])) + ]; +} + +