Skip to content
Snippets Groups Projects
Commit e6379d8f authored by James Vasile's avatar James Vasile
Browse files

Add script to switch between monitors in sway

parent 4d922181
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python3
"""This is part of my sway setup.
It cycles through different combinations of turning two monitors on/off.
"""
import json
import re
import subprocess
import pprint
import sys
import time
from typing import Any, Dict
pp = pprint.PrettyPrinter(indent=4, width=120).pprint
pf = pprint.PrettyPrinter(indent=4, width=120).pformat
import logging
def setup_logging(logfile:str) -> logging.Logger:
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
fh = logging.FileHandler(logfile)
fh.setLevel(logging.DEBUG)
fh.setFormatter(logging.Formatter(datefmt='%Y-%d-%m %H:%M:%S', fmt='%(asctime)s %(levelname)s %(message)s'))
log.addHandler(fh)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
ch.setFormatter(logging.Formatter(fmt='%(levelname)s %(message)s'))
log.addHandler(ch)
return log
log = setup_logging('/tmp/floating-tiles.log')
def err(msg):
log.error(pf(msg))
sys.exit(-1)
sys.stderr.write(pf(msg) + "\n")
def run(cmd):
log.debug(cmd)
result = subprocess.run(cmd, capture_output=True, shell=True)
stdout = result.stdout.decode("utf-8").strip()
if result.stderr:
stderr = result.stderr.decode('utf-8').strip()
raise subprocess.CalledProcessError(result.returncode,
cmd,
stderr=stderr,
output=stdout)
return stdout.split("\n")
def enable(screen:str):
run(f'swaymsg output {screen} enable')
def disable(screen:str):
run(f'swaymsg output {screen} disable')
def toggle(screen:str):
run(f'swaymsg output {screen} toggle')
def main():
# We want to cycle from laptop screen to external and back
laptop_on_left = False
screens = json.loads("\n".join(run('swaymsg -t get_outputs')))
laptop = [s for s in screens if s['name'] == 'eDP-1'][0]
externals = [s for s in screens if s['name'] != 'eDP-1']
external = externals[0]
if len(externals) > 1:
sys.stderr.write(f"Multiple external monitors. Ignoring all but {external['name']}")
if laptop['active']:
if external['active']:
# Both on, so turn off external
run(f"swaymsg output {external['name']} disable")
else:
# Laptop on, external off, so swap them
run(f"swaymsg output {laptop['name']} toggle")
run(f"swaymsg output {external['name']} toggle")
else:
# laptop off, let's turn on both displays
run(f"swaymsg output {laptop['name']} enable")
run(f"swaymsg output {external['name']} enable")
# Arrange screens left vs right
if laptop_on_left:
run(f"swaymsg output {laptop['name']} pos 0 0")
run(f"swaymsg output {external['name']} pos {laptop['current_mode']['width']} 0")
else:
run(f"swaymsg output {external['name']} pos 0 0")
run(f"swaymsg output {laptop['name']} pos {external['current_mode']['width']} 0")
log.debug("end main")
if __name__ == "__main__":
log.debug(f"Starting {__file__}")
main()
run("/home/james/bin/arrange_windows.py")
log.debug(f"Exiting {__file__}")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment