lemonbar: Handle restarts

This commit is contained in:
kuben
2019-07-28 12:02:50 +02:00
parent 162c3a208f
commit 96d944d2ef
3 changed files with 105 additions and 65 deletions

View File

@@ -56,11 +56,15 @@ class State(object):
return self.inactive
# Create the i3ws object, then call locking function work()
# Another implementation would be to create a thread in __init__
# and implement a locking join() function
class i3ws(object):
ws_format = '%s%s '
end_format = 'WSP%s'
state = State()
ws_format = '%s%s '
end_format = 'WSP%s'
state = State()
backgrounds = {}
running = True
fifo = None
@@ -69,25 +73,40 @@ class i3ws(object):
self.state = state
if fifo_file:
self.fifo = open(fifo_file, 'w')
def work(self):
# While loop to restart connection as long as there is an i3 ipc socket
while self.running:
self.resetConn()
common.logger.debug('Started i3 workspaces manager')
self.enterMain()
time.sleep(0.5) # TODO max number of attempts + catch exception instead
def resetConn(self):
# conn
self.conn = i3ipc.Connection()
# Run call backs once
self.change(self.conn, None)
self.win_focused(self.conn, None)
self.conn.on('workspace::focus', self.change)
self.conn.on('workspace::init', self.change)
self.conn.on('workspace::empty', self.change)
self.conn.on('window::focus', self.win_focused)
logging.debug('Started i3 workspaces manager')
self.conn.on('workspace::focus' , self.change)
self.conn.on('workspace::init' , self.change)
self.conn.on('workspace::empty' , self.change)
self.conn.on('window::focus' , self.win_focused)
self.conn.on('shutdown' , self.shutdown)
def enterMain(self):
try:
# Locking call
self.conn.main()
except BrokenPipeError:
logging.debug('Broken pipe in i3 workspaces thread, exiting')
common.logger.debug('Broken pipe in i3 workspaces thread, exiting')
except:
logging.debug('Unknown exception in i3 workspaces thread, exiting')
common.logger.debug('Unknown exception in i3 workspaces thread, exiting')
def shutdown(self, i3, e):
common.logger.debug('Shut down i3ipc')
def change(self, i3, e):
# Receives event and workspace data
@@ -121,16 +140,16 @@ class i3ws(object):
try:
os.kill(pid, signal.SIGTERM)
except ProcessLookupError:
logging.debug('Tried killing process {} but it doesn\'t exist'.format(pid))
common.logger.debug('Tried killing process {} but it doesn\'t exist'.format(pid))
common.kill_on_unfocus = []
# Set correct keymap
if wclass in common.keymaps:
new_km = common.keymaps[wclass]
logging.debug('Setting {} as keymap for {}'.format(new_km, wclass))
common.logger.debug('Setting {} as keymap for {}'.format(new_km, wclass))
else:
new_km = common.def_keymap
logging.debug('Setting default keymap {} for {}'.format(common.def_keymap, wclass))
common.logger.debug('Setting default keymap {} for {}'.format(common.def_keymap, wclass))
call(['/home/kuba/.i3/scripts/lang.sh', 'qset', new_km])
@@ -169,18 +188,34 @@ class i3ws(object):
call(['sh', '/home/kuba/.i3/lemonbar/set_bg.sh', args])
def quit(self):
common.logger.debug('Quitting i3 workspace script')
self.running = False
self.conn.main_quit()
if self.fifo is not None:
try:
self.fifo.close()
except BrokenPipeError:
pass
def handle_exit(signal, frame):
global ws
print("Recieved Keyboard Interrupt from user")
ws.quit()
sys.exit(1)
if __name__ == '__main__':
# Setup logger to stdout with debug log level
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(formatter)
common.logger = logging.getLogger('Normal logger')
common.logger.setLevel(logging.DEBUG)
common.logger.addHandler(handler)
# Capture Keyboard Interrupt (i3ipc captures this internally)
signal.signal(signal.SIGINT, handle_exit)
# Start main
ws = i3ws()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print('') # force new line
# finally:
# ws.quit()
ws.work()