From 9cc40b8755986e654eb6ca0dcecf16abdd264867 Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Fri, 11 Mar 2022 09:20:15 +0100 Subject: [PATCH] i3-companion: use new protocol format It's simple enough and I don't want to spawn a polybar-msg. Maybe at a later point, we may be smarter. The problem with the old socket is that if you send messages fast enough on it, polybar may believe they are one message since it is not length-encoded on its side and it's a simple FIFO file (so no message boundaries). Dunno why I didn't get a problem until now. --- bin/i3-companion | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/bin/i3-companion b/bin/i3-companion index c28d3d4..b4232da 100755 --- a/bin/i3-companion +++ b/bin/i3-companion @@ -17,6 +17,7 @@ import shlex import subprocess import sys import types +import struct import i3ipc from i3ipc.aio import Connection @@ -263,14 +264,22 @@ def polybar(module): out.write(content) # Send it to polybar - for name in glob.glob("/tmp/polybar_mqueue.*"): + cmd = bytes(f"#{module}.send.{content}", "utf-8") + data = b"polyipc" + struct.pack("=BIB", 0, len(cmd), 2) + cmd + for name in glob.glob(f"{os.getenv('XDG_RUNTIME_DIR')}/polybar/*.sock"): try: - with open(os.open(name, os.O_WRONLY | os.O_NONBLOCK), "w") as out: - cmd = f"action:#{module}.send.{content}" - out.write(cmd) + reader, writer = await asyncio.open_unix_connection(name) except OSError as e: if e.errno != errno.ENXIO: raise + else: + try: + writer.write(data) + await writer.drain() + await reader.read() + finally: + writer.close() + await writer.wait_closed() logger.info(f"polybar/{module}: content updated") cache[module] = content