lemonbar: Implement callbacks for host specific stuff
This commit is contained in:
@@ -23,20 +23,6 @@ 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'
|
||||
@@ -60,19 +46,25 @@ class State(object):
|
||||
# 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()
|
||||
backgrounds = {}
|
||||
running = True
|
||||
ws_format = '%s%s '
|
||||
end_format = 'WSP%s'
|
||||
state = State()
|
||||
outputs = []
|
||||
focused_window = None
|
||||
running = True
|
||||
fifo = None
|
||||
|
||||
# Callback functions have argument i3ws
|
||||
change_callbacks = []
|
||||
focus_callbacks = []
|
||||
|
||||
fifo = None
|
||||
|
||||
def __init__(self, state=None, fifo_file=None):
|
||||
if state:
|
||||
self.state = state
|
||||
if fifo_file:
|
||||
self.fifo = open(fifo_file, 'w')
|
||||
common.add_callbacks(self)
|
||||
|
||||
def work(self):
|
||||
# While loop to restart connection as long as there is an i3 ipc socket
|
||||
@@ -80,15 +72,15 @@ class i3ws(object):
|
||||
self.resetConn()
|
||||
common.logger.debug('Started i3 workspaces manager')
|
||||
self.enterMain()
|
||||
common.logger.debug('Finished i3 workspaces manager')
|
||||
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.change(self.conn, None)
|
||||
#self.win_focused(self.conn, None)
|
||||
|
||||
self.conn.on('workspace::focus' , self.change)
|
||||
self.conn.on('workspace::init' , self.change)
|
||||
@@ -110,48 +102,30 @@ class i3ws(object):
|
||||
|
||||
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))
|
||||
self.outputs = i3.get_outputs()
|
||||
fmt_outputs = ['DISP']
|
||||
for output in self.outputs:
|
||||
if output.active:
|
||||
fmt_outputs.append(output.name)
|
||||
self.display(':'.join(fmt_outputs))
|
||||
|
||||
workspaces = i3.get_workspaces()
|
||||
text = self.format(workspaces, outputs)
|
||||
text = self.format(workspaces, self.outputs)
|
||||
self.display(text)
|
||||
self.set_bg()
|
||||
|
||||
# Callbacks
|
||||
for cb in self.change_callbacks:
|
||||
cb(self)
|
||||
|
||||
def win_focused(self, i3, e):
|
||||
win = i3.get_tree().find_focused()
|
||||
name = win.name
|
||||
role = win.window_role
|
||||
wclass = win.window_class
|
||||
|
||||
self.focused_window = i3.get_tree().find_focused()
|
||||
name = self.focused_window.name
|
||||
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:
|
||||
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]
|
||||
common.logger.debug('Setting {} as keymap for {}'.format(new_km, wclass))
|
||||
else:
|
||||
new_km = common.def_keymap
|
||||
common.logger.debug('Setting default keymap {} for {}'.format(common.def_keymap, wclass))
|
||||
call(['/home/kuba/.i3/scripts/lang.sh', 'qset', new_km])
|
||||
|
||||
# Callbacks
|
||||
for cb in self.focus_callbacks:
|
||||
cb(self)
|
||||
|
||||
def format(self, workspaces, outputs):
|
||||
# Formats the text according to the workspace data given.
|
||||
@@ -165,8 +139,6 @@ class i3ws(object):
|
||||
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
|
||||
@@ -181,12 +153,6 @@ class i3ws(object):
|
||||
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):
|
||||
common.logger.debug('Quitting i3 workspace script')
|
||||
self.running = False
|
||||
|
||||
Reference in New Issue
Block a user