diff --git a/.i3/lemonbar/i3_lemonbar_common.py b/.i3/lemonbar/i3_lemonbar_common.py index b481994..e17d699 100755 --- a/.i3/lemonbar/i3_lemonbar_common.py +++ b/.i3/lemonbar/i3_lemonbar_common.py @@ -21,6 +21,8 @@ class bar_mode(Enum): mode = bar_mode.normal +screen_args = ['screen', '-D', '-R', '-U' '-S' 'lemonbar'] + floatterm_args = lambda prog : ['terminator', '-r', 'FLOAT_TERM', '-p' , 'dark', '-e', 'echo kill_unfocus $$ > {}; exec {}'.format( config.fifo_file_executor, prog)] @@ -97,3 +99,57 @@ commands_dict = {'toggle_secs': toggle_secs ,'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]) diff --git a/.i3/lemonbar/i3_lemonbar_launcher.py b/.i3/lemonbar/i3_lemonbar_launcher.py index f72a523..e40ce26 100755 --- a/.i3/lemonbar/i3_lemonbar_launcher.py +++ b/.i3/lemonbar/i3_lemonbar_launcher.py @@ -193,7 +193,7 @@ def strip_ansi_unicode(s): def user_screen(): # 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) for line in common.p_screen.stdout: #try: diff --git a/.i3/lemonbar/i3_workspaces.py b/.i3/lemonbar/i3_workspaces.py index 1c5a505..2e36d4f 100755 --- a/.i3/lemonbar/i3_workspaces.py +++ b/.i3/lemonbar/i3_workspaces.py @@ -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