Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

focus-last.py: Terminate threads and re-instantiate after a broken pipe #179

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions examples/focus-last.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

class FocusWatcher:
def __init__(self):
self.alive = True
self.i3 = i3ipc.Connection()
self.i3.on('window::focus', self.on_window_focus)
# Make a directory with permissions that restrict access to
Expand Down Expand Up @@ -52,24 +53,33 @@ def read(conn):
data = conn.recv(1024)
if data == b'switch':
with self.window_list_lock:
tree = self.i3.get_tree()
windows = set(w.id for w in tree.leaves())
for window_id in self.window_list[1:]:
if window_id not in windows:
self.window_list.remove(window_id)
else:
self.i3.command('[con_id=%s] focus' % window_id)
break
try:
tree = self.i3.get_tree()
windows = set(w.id for w in tree.leaves())
for window_id in self.window_list[1:]:
if window_id not in windows:
self.window_list.remove(window_id)
else:
self.i3.command('[con_id=%s] focus' % window_id)
break
except BrokenPipeError:
print("Caught broken pipe, restarting...")
self.alive = False
elif not data:
selector.unregister(conn)
conn.close()

selector.register(self.listening_socket, selectors.EVENT_READ, accept)

while True:
while self.alive:
for key, event in selector.select():
callback = key.data
callback(key.fileobj)

self.i3.main_quit()
focus_watcher = FocusWatcher()
focus_watcher.run()
del(self)

def run(self):
t_i3 = threading.Thread(target=self.launch_i3)
Expand Down