Created
March 26, 2025 12:54
-
-
Save cit/902c6245b4bbe3c3d1658b935d9fb837 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| import subprocess | |
| import time | |
| import sys | |
| import os | |
| from i3ipc import Connection | |
| search_str_wp = 'Wayland Output Mirror' | |
| search_str_pdfpc = 'pdfpc - presentation' | |
| def get_window_by_attr(i3, attr, search_string): | |
| tree = i3.get_tree() | |
| for element in tree: | |
| if (str(element.name)).startswith(search_string): | |
| return str(getattr(element, attr)) | |
| return None | |
| def get_output(i3, condition): | |
| return next(filter(condition, i3.get_outputs()), None) | |
| def main(): | |
| i3 = Connection() | |
| if (pid := get_window_by_attr(i3, 'pid', search_str_wp)) is not None: | |
| os.kill(int(pid), 2) | |
| pdfpc_win = get_window_by_attr(i3, 'name', search_str_pdfpc) | |
| if (pdfpc_win): | |
| i3.command('[con_id={}] fullscreen on'.format(window.id)) | |
| # wl-mirror is not running, start it on the external | |
| else: | |
| # start wl-present with the focued output | |
| output_mirror = get_output(i3, lambda s: s.focused == True) | |
| subprocess.Popen(["wl-present", "mirror", output_mirror.name]) | |
| # Wait until wl-present is started. | |
| while not get_window_by_attr(i3, 'name', search_str_wp): | |
| time.sleep(0.1) | |
| # Get the destination output | |
| if len(sys.argv) > 1: | |
| condition = lambda s: s.name == sys.argv[1] | |
| else: | |
| condition = lambda s: s.focused != True | |
| if (output_dest := get_output(i3, condition)) is not None: | |
| id = get_window_by_attr(i3, 'id', search_str_wp) | |
| i3.command('[con_id={}] move container to output {}'.format(id, output_dest.name)) | |
| i3.command('[con_id={}] fullscreen'.format(id)) | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment