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.
This commit is contained in:
Vincent Bernat 2022-03-11 09:20:15 +01:00
parent c56649ca95
commit 9cc40b8755

View file

@ -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