From 57fe74e4d10ba490ff4562762378170b93dec966 Mon Sep 17 00:00:00 2001 From: generaldenmark Date: Fri, 13 Nov 2020 15:39:31 +0100 Subject: [PATCH] Removes the random regex, and just tries to match with what monitors are availble. (remember to update your config to match available outputs dummy) --- pi3/smart_workspace.py | 58 +++++++++++++++++++++++++++++------------- setup.py | 2 +- 2 files changed, 42 insertions(+), 18 deletions(-) diff --git a/pi3/smart_workspace.py b/pi3/smart_workspace.py index 371c0dc..9dfdf30 100644 --- a/pi3/smart_workspace.py +++ b/pi3/smart_workspace.py @@ -4,6 +4,17 @@ import pynput import re import argparse import pprint +import subprocess + + +def get_active_outputs(): + ps = subprocess.Popen(('xrandr', '--listmonitors'), stdout=subprocess.PIPE) + output = subprocess.check_output(('awk', "{print $4}"), stdin=ps.stdout) + ps.wait() + return [x for x in output.decode('UTF-8').split('\n') if x is not None and x != ''] + + +names_for_outputs = r'(' + '|'.join(get_active_outputs()) + ')' class WorkSpacer: @@ -20,32 +31,42 @@ class WorkSpacer: self.current_output_name = None def _connect(self): + self.print_if_debug('all available outputs on device') + self.print_if_debug(names_for_outputs) try: self.i3 = Connection() self.config = self.i3.get_config().__dict__['config'] config_outputs = {} for matchNo, match in enumerate( - re.finditer(r'set (\$[a-zA-Z]+) ((HDMI|DP|VGA|eDP)(-|)(\d|$))', self.config, re.MULTILINE), start=1 + re.finditer(r'set (\$[a-zA-Z]+) (' + + names_for_outputs + ')', self.config, re.MULTILINE), start=1 ): config_outputs[match.group(1)] = match.group(2) + self.print_if_debug('All outputs listed in the config, matched on available configs') + self.print_if_debug(config_outputs) config_workspace_names = {} for matchNum, match in enumerate( - re.finditer(r'set (\$.*) (\d.*)', self.config, re.MULTILINE) + re.finditer(r'set (\$.*) (\d.*)', self.config, re.MULTILINE) ): config_workspace_names[match.group(1)] = match.group(2) + self.print_if_debug('All config_workspaces_names') + self.print_if_debug(config_workspace_names) + for matchNum, match in enumerate( - re.finditer(r'workspace (\$.*) output (\$.*)', self.config, re.MULTILINE) + re.finditer(r'workspace (\$.*) output (\$.*)', self.config, re.MULTILINE) ): if not self.workspaces_on_outputs.keys().__contains__(config_outputs[match.group(2)]): self.workspaces_on_outputs[config_outputs[match.group(2)]] = [] - self.workspaces_on_outputs[config_outputs[match.group(2)]].append(config_workspace_names[match.group(1)]) + self.workspaces_on_outputs[config_outputs[match.group(2)]].append( + config_workspace_names[match.group(1)]) self.print_if_debug("All workspaces with outputs") self.print_if_debug(self.workspaces_on_outputs) except Exception as exc: - self.print_if_debug(exc) + if self.args.debug: + raise exc sys.exit(1) - self.workspaces = [workspaces for workspaces in self.i3.get_workspaces()] + self.workspaces = [workspaces for workspaces in self.i3.get_workspaces()] outputs = self.i3.get_outputs() self.outputs = [output for output in outputs if output.__dict__["active"] is True] @@ -55,7 +76,8 @@ class WorkSpacer: self.current_output_name = self._get_workspace_from_courser_position() if self.args.shift: - self.i3.command(f'move container to workspace {self.workspaces_on_outputs[self.current_output_name][self.args.index - 1]}') + self.i3.command( + f'move container to workspace {self.workspaces_on_outputs[self.current_output_name][self.args.index - 1]}') if not self.args.keep_with_it: return self.i3.command(f'workspace {self.workspaces_on_outputs[self.current_output_name][self.args.index - 1]}') @@ -68,16 +90,20 @@ class WorkSpacer: y_offset = output.__dict__["rect"].__dict__["y"] if x_offset == 0 and y_offset == 0: - if x_offset <= self.mouse_position[0] <= x_offset + width and y_offset <= self.mouse_position[1] <= y_offset + height: + if x_offset <= self.mouse_position[0] <= x_offset + width and y_offset <=\ + self.mouse_position[1] <= y_offset + height: return output.__dict__["name"] elif x_offset == 0: - if x_offset <= self.mouse_position[0] <= x_offset + width and y_offset < self.mouse_position[1] <= y_offset + height: + if x_offset <= self.mouse_position[0] <= x_offset + width and y_offset <\ + self.mouse_position[1] <= y_offset + height: return output.__dict__["name"] elif y_offset == 0: - if x_offset < self.mouse_position[0] <= x_offset + width and y_offset <= self.mouse_position[1] <= y_offset + height: + if x_offset < self.mouse_position[0] <= x_offset + width and y_offset <=\ + self.mouse_position[1] <= y_offset + height: return output.__dict__["name"] else: - if x_offset < self.mouse_position[0] <= x_offset + width and y_offset < self.mouse_position[1] <= y_offset + height: + if x_offset < self.mouse_position[0] <= x_offset + width and y_offset <\ + self.mouse_position[1] <= y_offset + height: return output.__dict__["name"] def _get_workspaces_for_output(self, output): @@ -90,25 +116,23 @@ class WorkSpacer: def main(): parser = argparse.ArgumentParser( - description="Dynamic changes the workspace, based on what output your cursoer is on." + description="Dynamic changes the workspace, based on what output your cursor is on." ) parser.add_argument('-d', '--debug', action='store_true', help='Turn on debug mode.') required_group = parser.add_argument_group('Required', '') required_group.add_argument("-i", "--index", type=int, required=True, - help="The indexed workspace for the output where the cursor is currently located") + help="The indexed workspace for the output where the cursor is currently located") shift_group = parser.add_argument_group('Shift', 'manipulate the active window') shift_group.add_argument("-s", "--shift", action='store_true', - help="Moves the active window to the index workspace") + help="Moves the active window to the index workspace") shift_group.add_argument('-k', '--keep-with-it', action='store_true', - help='Moves the active window to the index workspace, and moves with it') + help='Moves the active window to the index workspace, and moves with it') # pprint.pprint(parser.parse_args().__dict__) WorkSpacer(parser.parse_args()).run() if __name__ == '__main__': main() - - diff --git a/setup.py b/setup.py index 374839b..e8da543 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ with open('README.rst', 'r') as fh: setup( name='pi3-smart-workspace', - version='0.1.6', + version='0.1.7', packages=['pi3'], url='https://github.com/GeneralDenmark/PyOutputHandler', license='Apache-2.0 License ',