Files
dotfiles/.i3/lemonbar/i3_workspaces.py
2019-07-09 22:06:48 +02:00

187 lines
5.3 KiB
Python
Executable File

#!/usr/bin/env python
#
# Print i3 workspaces on every change.
#
# Format:
# For every workspace (x = workspace name)
# - "FOCx" -> Focused workspace
# - "INAx" -> Inactive workspace
# - "ACTx" -> Ative workspace
# - "URGx" -> Urgent workspace
#
# Based in wsbar.py en examples dir
#
# 16 feb 2015 - Electro7
import sys, os, signal
import logging
import time
from subprocess import call
import i3ipc
import i3_lemonbar_config as config
import i3_lemonbar_common as common
def img_path(num):
dir = '/home/kuba/Obrazy/Wallpapers/'
return dir + {
1: '1_main',
2: '2_web',
3: '3_music',
4: '4_work',
5: '5_terms',
6: '6_stats',
7: '7',
8: '8',
9: '9',
}.get(int(num), 'default')
class State(object):
# workspace states
focused = 'FOC'
active = 'ACT'
inactive = 'INA'
urgent = 'URG'
def get_state(self, workspace, output):
if workspace['focused']:
if output['current_workspace'] == workspace['name']:
return self.focused
else:
return self.active
if workspace['urgent']:
return self.urgent
else:
return self.inactive
class i3ws(object):
ws_format = '%s%s '
end_format = 'WSP%s'
state = State()
backgrounds = {}
fifo = None
def __init__(self, state=None, fifo_file=None):
if state:
self.state = state
if fifo_file:
self.fifo = open(fifo_file, 'w')
# 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')
try:
self.conn.main()
except BrokenPipeError:
logging.debug('Broken pipe in i3 workspaces thread, exiting')
except:
logging.debug('Unknown exception in i3 workspaces thread, exiting')
def change(self, i3, e):
# Receives event and workspace data
outputs = i3.get_outputs()
active = ['DISP']
for output in outputs:
if output.name != 'xroot-0':
active.append(output.name)
self.backgrounds[output.name] = img_path(1)
self.display(':'.join(active))
workspaces = i3.get_workspaces()
text = self.format(workspaces, outputs)
self.display(text)
self.set_bg()
def win_focused(self, i3, e):
win = i3.get_tree().find_focused()
name = win.name
role = win.window_role
wclass = win.window_class
text = 'WIN{}'.format(name)
self.display(text)
common.cur_class = wclass
# Kill floating windows
if role != 'FLOAT_TERM' and wclass != 'FLOAT_PAVU' and wclass != 'YADWINBR':
# Is there a window that the bar has opened?
for pid in common.kill_on_unfocus:
try:
os.kill(pid, signal.SIGTERM)
except ProcessLookupError:
logging.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))
else:
new_km = common.def_keymap
logging.debug('Setting default keymap {} for {}'.format(common.def_keymap, wclass))
call(['/home/kuba/.i3/scripts/lang.sh', 'qset', new_km])
def format(self, workspaces, outputs):
# Formats the text according to the workspace data given.
out = ''
for workspace in workspaces:
output = None
for output_ in outputs:
if output_['name'] == workspace['output']:
output = output_
break
if not output:
continue
st = self.state.get_state(workspace, output)
if st == 'FOC':
self.backgrounds[output['name']] = img_path(workspace['name'].partition(' ')[0])
name = workspace['name'].replace(" ","___")
item= self.ws_format % (st, name)
out += item
return self.end_format % out
def display(self, text):
if self.fifo is not None:
self.fifo.write(text + '\n')
self.fifo.flush()
else:
# Displays the text in stout
print(text)
sys.stdout.flush()
def set_bg(self):
args = ''
for key in self.backgrounds.keys():
args += ' ' + key + ' ' + self.backgrounds[key]
call(['sh', '/home/kuba/.i3/lemonbar/set_bg.sh', args])
def quit(self):
if self.fifo is not None:
try:
self.fifo.close()
except BrokenPipeError:
pass
if __name__ == '__main__':
ws = i3ws()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print('') # force new line
# finally:
# ws.quit()