Skip to content

Instantly share code, notes, and snippets.

@rubdos
Created January 18, 2024 22:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rubdos/d34ee20152bf41726ca658525b070035 to your computer and use it in GitHub Desktop.
Save rubdos/d34ee20152bf41726ca658525b070035 to your computer and use it in GitHub Desktop.
sway automatic scaling on dock (for my Framework)
[Unit]
Description=Sway scaling daemon
[Service]
Environment=HOME=/home/rsmet/
ExecStart=/usr/bin/python /home/rsmet/.local/bin/sway-scaling-daemon.py
Restart=always
ExecStartPost=/bin/sleep 1
[Install]
WantedBy=default.target
#!/usr/bin/env python3
import pyudev
import subprocess
def main():
context = pyudev.Context()
monitor = pyudev.Monitor.from_netlink(context)
monitor.filter_by(subsystem='usb')
monitor.start()
devpath = None
for device in iter(monitor.poll, None):
if devpath is None:
if "ID_VENDOR_ID" not in device:
print("Ignoring", device)
continue
if "ID_MODEL_ID" not in device:
print("Ignoring", device.properties["ID_VENDOR_ID"])
continue
if device.properties["ID_VENDOR_ID"] != u'2109' or device.properties["ID_MODEL_ID"] != u'2822':
print("Ignoring", device.properties["ID_VENDOR_ID"], device.properties["ID_MODEL_ID"])
continue
elif device.action != "remove":
continue
elif device.properties["DEVPATH"] != devpath:
continue
if device.action == "add":
devpath = device.properties["DEVPATH"]
scale = "1.5"
elif device.action == "remove":
devpath = None
scale = "1.3"
else:
print(device.action)
continue
print("Setting scale to", scale, "saving dev", devpath)
# I can add more logic here, to run different scripts for different devices.
subprocess.call(["/usr/bin/swaymsg", "output", "eDP-1", "scale", scale])
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment