Skip to content

Instantly share code, notes, and snippets.

@ilyaluk
Last active August 5, 2017 18:30
Show Gist options
  • Save ilyaluk/2f74588975499ce44ca6e164a8c17700 to your computer and use it in GitHub Desktop.
Save ilyaluk/2f74588975499ce44ca6e164a8c17700 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import socket
import os
i3ipc_magic = b'i3-ipc'
def send_packet(client, type, msg):
pac = i3ipc_magic + len(msg).to_bytes(4, 'little')
pac += type.to_bytes(4, 'little') + msg
client.send(pac)
def recv_packet(client):
pac = client.recv(len(i3ipc_magic) + 8)
assert pac[:len(i3ipc_magic)] == i3ipc_magic
pac = pac[len(i3ipc_magic):]
ln = int.from_bytes(pac[:4], 'little')
type = int.from_bytes(pac[4:8], 'little')
msg = b''
while ln > 0:
msg += client.recv(1)
ln -= 1
return type, msg
path = os.popen('sway --get-socketpath').read().strip()
print(path)
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as c:
c.connect(path)
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as c2:
c2.connect(path)
send_packet(c2, 2, b'["workspace"]') # 2 -- subscribe
print(recv_packet(c2))
for _ in range(100):
send_packet(c, 0, b'workspace "next_on_output"') # 0 -- command
print(recv_packet(c))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment