Skip to content
Snippets Groups Projects
Commit 778ac2c5 authored by ph's avatar ph
Browse files

i3bar stdin polling

parent 34ee2594
No related branches found
No related tags found
No related merge requests found
......@@ -5,6 +5,7 @@ import sys
import json
import datetime
import time
import select
def jout(d):
......@@ -12,12 +13,44 @@ def jout(d):
sys.stdout.write(json.dumps(d))
sys.stdout.flush()
def jin():
j = '['
while j=='[':
j = sys.stdin.readline().strip()
if j.startswith(","):
j = j[1:]
return json.loads(j)
def debug(s):
sys.stderr.write(s)
sys.stderr.write("\n")
sys.stderr.flush()
def main(argv=None):
sys.stdout.write('{"version":1}\n[\n[]\n')
poller = select.poll()
poller.register(sys.stdin, select.POLLIN | select.POLLPRI | select.POLLHUP | select.POLLERR)
sys.stdout.write('{"version":1,"click_events":true}\n[\n[]\n')
sys.stderr = open("/home/ph/i3-switcher.stderr", "w")
debug("Started")
color = 0x00FF00
while True:
jout([{"full_text": str(datetime.datetime.now()), "short_text": "short", "color": "#00ff00"} for i in range(5)])
time.sleep(1)
jout([{"name": "click", "full_text": str(datetime.datetime.now()), "short_text": "short", "color": "%06x" % color}])
fd_to_socket = { sys.stdin.fileno(): sys.stdin }
events = poller.poll(1000)
for fd, flag in events:
sock = fd_to_socket[fd]
debug("descriptor %i" % fd)
if flag & (select.POLLIN | select.POLLPRI):
debug("flag %x" % flag)
click = jin()
debug("click: %s" % str(click))
if click["name"]=="click" and click["button"]==1:
debug("clicked")
color ^= 0xFFFFFF
else:
print >>sys.stderr, "Unexpected poll event %xi on %s" % (flag, str(sock))
if __name__=="__main__":
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment