#!/usr/bin/env python3
import os
import socket
import time
import subprocess

def notify(event_type, data):
    title = f"Evento: {event_type}"
    message = data if data else "Sin datos adicionales."
    subprocess.Popen(["notify-send", title, message])

def handle_event(event):
    if ">>" in event:
        event_type, data = event.split(">>", 1)
    else:
        event_type, data = event, ""
    print(f"📡 Evento recibido: {event}")
    notify(event_type.strip(), data.strip())

def main():
    his = os.environ.get('HYPRLAND_INSTANCE_SIGNATURE')
    if not his:
        print("❌ La variable HYPRLAND_INSTANCE_SIGNATURE no está establecida.")
        return

    socket_path = f"{os.environ.get('XDG_RUNTIME_DIR')}/hypr/{his}/.socket2.sock"

    if not os.path.exists(socket_path):
        print(f"❌ El socket de eventos no existe: {socket_path}")
        return

    while True:
        try:
            with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as client:
                client.connect(socket_path)
                print("✅ Conectado al socket de eventos de Hyprland.")
                while True:
                    data = client.recv(1024)
                    if not data:
                        print("⚠️ Socket cerrado. Reintentando en 2 segundos...")
                        time.sleep(2)
                        break
                    events = data.decode().strip().split('\n')
                    for event in events:
                        handle_event(event)
        except Exception as e:
            print(f"❌ Error de conexión: {e}")
            time.sleep(2)

if __name__ == "__main__":
    main()

