2023-11-08 00:28:29 +01:00
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""Shows a widget for each connected screen and allows the user to loop through different orientations.
|
|
|
|
|
|
|
|
Requires the following executable:
|
|
|
|
* swaymsg
|
|
|
|
"""
|
|
|
|
|
2023-11-08 21:52:08 +01:00
|
|
|
import core.module
|
|
|
|
import core.input
|
|
|
|
import util.cli
|
|
|
|
|
2023-11-08 00:28:29 +01:00
|
|
|
import json
|
|
|
|
from os import environ, path
|
|
|
|
|
|
|
|
possible_orientations = ["normal", "90", "180", "270"]
|
|
|
|
|
2023-11-08 21:52:08 +01:00
|
|
|
class Module(core.module.Module):
|
|
|
|
def __init__(self, config, theme):
|
|
|
|
super().__init__(config, theme, [])
|
|
|
|
|
2023-11-08 00:28:29 +01:00
|
|
|
self.service = "sway-auto-rotate@%s" % path.basename(environ['SWAYSOCK']).replace('-', '\\\\x2d')
|
2023-11-08 21:52:08 +01:00
|
|
|
# self._widgets = []
|
|
|
|
self.update_widgets()
|
2023-11-08 00:28:29 +01:00
|
|
|
|
2023-11-08 21:52:08 +01:00
|
|
|
def update_widgets(self):
|
|
|
|
for display in json.loads(util.cli.execute("/usr/bin/swaymsg -t get_outputs -r")):
|
2023-11-08 00:28:29 +01:00
|
|
|
name = display['name']
|
|
|
|
widget = self.widget(name)
|
|
|
|
if not widget:
|
2023-11-08 21:52:08 +01:00
|
|
|
widget = self.add_widget(name=name, full_text="若 "+name)
|
|
|
|
widget.set("auto", util.cli.execute("systemctl --user show %s -p ActiveState" % self.service).split("\n")[0].split("=")[1])
|
|
|
|
core.input.register(widget, button=core.input.LEFT_MOUSE, cmd=self._toggle)
|
|
|
|
core.input.register(widget, button=core.input.RIGHT_MOUSE, cmd=self._toggle_auto)
|
2023-11-08 00:28:29 +01:00
|
|
|
widget.set("orientation", display['transform'])
|
|
|
|
|
|
|
|
def state(self, widget):
|
|
|
|
curr = widget.get("auto", "inactive")
|
|
|
|
if curr == "inactive":
|
|
|
|
return ["warning"]
|
|
|
|
return [curr]
|
|
|
|
|
|
|
|
def _toggle_auto(self, event):
|
2023-11-08 21:52:08 +01:00
|
|
|
widget = self.widget(widget_id=event["instance"])
|
2023-11-08 00:28:29 +01:00
|
|
|
if widget.get("auto") == "inactive":
|
2023-11-08 21:52:08 +01:00
|
|
|
util.cli.execute("systemctl --user start %s" % self.service)
|
2023-11-08 00:28:29 +01:00
|
|
|
else:
|
2023-11-08 21:52:08 +01:00
|
|
|
util.cli.execute("systemctl --user stop %s" % self.service)
|
|
|
|
widget.set("auto", util.cli.execute("systemctl --user show %s -p ActiveState" % self.service).split("\n")[0].split("=")[1])
|
2023-11-08 00:28:29 +01:00
|
|
|
|
|
|
|
def _toggle(self, event):
|
2023-11-08 21:52:08 +01:00
|
|
|
widget = self.widget(widget_id=event["instance"])
|
2023-11-08 00:28:29 +01:00
|
|
|
|
|
|
|
# compute new orientation based on current orientation
|
2023-11-08 21:52:08 +01:00
|
|
|
idx = (possible_orientations.index(widget.get("orientation"))+ 1) % len(possible_orientations)
|
2023-11-08 00:28:29 +01:00
|
|
|
new_orientation = possible_orientations[idx]
|
|
|
|
|
|
|
|
widget.set("orientation", new_orientation)
|
|
|
|
|
2023-11-08 21:52:08 +01:00
|
|
|
util.cli.execute("swaymsg 'output {} transform {}'".format(widget.name, new_orientation))
|
2023-11-08 00:28:29 +01:00
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|