lemonbar: Implement callbacks for host specific stuff

This commit is contained in:
kuben
2019-07-28 19:33:23 +02:00
parent 96d944d2ef
commit d959aa232c
3 changed files with 89 additions and 67 deletions

View File

@@ -21,6 +21,8 @@ class bar_mode(Enum):
mode = bar_mode.normal mode = bar_mode.normal
screen_args = ['screen', '-D', '-R', '-U' '-S' 'lemonbar']
floatterm_args = lambda prog : ['terminator', '-r', 'FLOAT_TERM', '-p' floatterm_args = lambda prog : ['terminator', '-r', 'FLOAT_TERM', '-p'
, 'dark', '-e', 'echo kill_unfocus $$ > {}; exec {}'.format( , 'dark', '-e', 'echo kill_unfocus $$ > {}; exec {}'.format(
config.fifo_file_executor, prog)] config.fifo_file_executor, prog)]
@@ -97,3 +99,57 @@ commands_dict = {'toggle_secs': toggle_secs
,'bluetooth': bluetooth ,'bluetooth': bluetooth
} }
# Callbacks for workspaces
def add_callbacks(i3ws):
i3ws.change_callbacks.append(set_bg)
i3ws.focus_callbacks.append(set_keymap)
i3ws.focus_callbacks.append(kill_floating_windows)
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')
def set_bg(i3ws):
cmd_args = ['sh', '/home/kuba/scripts/set_bg.sh']
for output in i3ws.outputs:
if output.active:
bg = img_path(output.current_workspace.partition(' ')[0])
cmd_args.append(bg)
subprocess.call(cmd_args)
def kill_floating_windows(i3ws):
global kill_on_unfocus, logger
role = i3ws.focused_window.window_role
wclass = i3ws.focused_window.window_class
if role != 'FLOAT_TERM' and wclass != 'FLOAT_PAVU' and wclass != 'YADWINBR':
# Is there a window that the bar has opened?
for pid in kill_on_unfocus:
try:
os.kill(pid, signal.SIGTERM)
except ProcessLookupError:
logger.debug('Tried killing process {} but it doesn\'t exist'.format(pid))
kill_on_unfocus = []
def set_keymap(i3ws):
global cur_class, keymaps, logger
wclass = i3ws.focused_window.window_class
cur_class = wclass
if wclass in keymaps:
new_km = keymaps[wclass]
logger.debug('Setting {} as keymap for {}'.format(new_km, wclass))
else:
new_km = def_keymap
logger.debug('Setting default keymap {} for {}'.format(def_keymap, wclass))
subprocess.call(['/home/kuba/.i3/scripts/lang.sh', 'qset', new_km])

View File

@@ -193,7 +193,7 @@ def strip_ansi_unicode(s):
def user_screen(): def user_screen():
# Attach to screen session, creating if needed, in UTF-8 mode # Attach to screen session, creating if needed, in UTF-8 mode
common.p_screen = subprocess.Popen('screen -D -R -U -S lemonbar' # TODO lemonbar_user common.p_screen = subprocess.Popen(common.screen_args # TODO lemonbar_user
,stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True) ,stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True)
for line in common.p_screen.stdout: for line in common.p_screen.stdout:
#try: #try:

View File

@@ -23,20 +23,6 @@ import i3ipc
import i3_lemonbar_config as config import i3_lemonbar_config as config
import i3_lemonbar_common as common 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): class State(object):
# workspace states # workspace states
focused = 'FOC' focused = 'FOC'
@@ -63,16 +49,22 @@ class i3ws(object):
ws_format = '%s%s ' ws_format = '%s%s '
end_format = 'WSP%s' end_format = 'WSP%s'
state = State() state = State()
backgrounds = {} outputs = []
focused_window = None
running = True running = True
fifo = None fifo = None
# Callback functions have argument i3ws
change_callbacks = []
focus_callbacks = []
def __init__(self, state=None, fifo_file=None): def __init__(self, state=None, fifo_file=None):
if state: if state:
self.state = state self.state = state
if fifo_file: if fifo_file:
self.fifo = open(fifo_file, 'w') self.fifo = open(fifo_file, 'w')
common.add_callbacks(self)
def work(self): def work(self):
# While loop to restart connection as long as there is an i3 ipc socket # While loop to restart connection as long as there is an i3 ipc socket
@@ -80,15 +72,15 @@ class i3ws(object):
self.resetConn() self.resetConn()
common.logger.debug('Started i3 workspaces manager') common.logger.debug('Started i3 workspaces manager')
self.enterMain() self.enterMain()
common.logger.debug('Finished i3 workspaces manager')
time.sleep(0.5) # TODO max number of attempts + catch exception instead time.sleep(0.5) # TODO max number of attempts + catch exception instead
def resetConn(self): def resetConn(self):
# conn
self.conn = i3ipc.Connection() self.conn = i3ipc.Connection()
# Run call backs once # Run call backs once
self.change(self.conn, None) #self.change(self.conn, None)
self.win_focused(self.conn, None) #self.win_focused(self.conn, None)
self.conn.on('workspace::focus' , self.change) self.conn.on('workspace::focus' , self.change)
self.conn.on('workspace::init' , self.change) self.conn.on('workspace::init' , self.change)
@@ -110,48 +102,30 @@ class i3ws(object):
def change(self, i3, e): def change(self, i3, e):
# Receives event and workspace data # Receives event and workspace data
outputs = i3.get_outputs() self.outputs = i3.get_outputs()
active = ['DISP'] fmt_outputs = ['DISP']
for output in outputs: for output in self.outputs:
if output.name != 'xroot-0': if output.active:
active.append(output.name) fmt_outputs.append(output.name)
self.backgrounds[output.name] = img_path(1) self.display(':'.join(fmt_outputs))
self.display(':'.join(active))
workspaces = i3.get_workspaces() workspaces = i3.get_workspaces()
text = self.format(workspaces, outputs) text = self.format(workspaces, self.outputs)
self.display(text) self.display(text)
self.set_bg()
# Callbacks
for cb in self.change_callbacks:
cb(self)
def win_focused(self, i3, e): def win_focused(self, i3, e):
win = i3.get_tree().find_focused() self.focused_window = i3.get_tree().find_focused()
name = win.name name = self.focused_window.name
role = win.window_role
wclass = win.window_class
text = 'WIN{}'.format(name) text = 'WIN{}'.format(name)
self.display(text) self.display(text)
common.cur_class = wclass # Callbacks
for cb in self.focus_callbacks:
# Kill floating windows cb(self)
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])
def format(self, workspaces, outputs): def format(self, workspaces, outputs):
# Formats the text according to the workspace data given. # Formats the text according to the workspace data given.
@@ -165,8 +139,6 @@ class i3ws(object):
if not output: if not output:
continue continue
st = self.state.get_state(workspace, output) st = self.state.get_state(workspace, output)
if st == 'FOC':
self.backgrounds[output['name']] = img_path(workspace['name'].partition(' ')[0])
name = workspace['name'].replace(" ","___") name = workspace['name'].replace(" ","___")
item= self.ws_format % (st, name) item= self.ws_format % (st, name)
out += item out += item
@@ -181,12 +153,6 @@ class i3ws(object):
print(text) print(text)
sys.stdout.flush() 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): def quit(self):
common.logger.debug('Quitting i3 workspace script') common.logger.debug('Quitting i3 workspace script')
self.running = False self.running = False