i3-companion: don't restart everything on spurious RandR events

This commit is contained in:
Vincent Bernat 2021-07-15 15:14:39 +02:00
parent 390597ecd7
commit 84b1aa99cc
2 changed files with 38 additions and 9 deletions

View file

@ -507,11 +507,42 @@ async def workspace_info(i3, event):
workspace_info.last_id = result[0]
@on(I3Event.OUTPUT)
@on(I3Event.OUTPUT, StartEvent)
@static(last_setup={})
@debounce(2)
async def output_update(i3, event):
"""React to a XRandR change."""
logger.info("XRandR change detected")
proc = await asyncio.create_subprocess_exec(
"xrandr", "--current", stdout=asyncio.subprocess.PIPE
)
stdout, _ = await proc.communicate()
if proc.returncode != 0:
logger.warning(f"xrandr exited with {proc.returncode}")
return
stdout = stdout.decode("ascii")
xrandr_re = re.compile(
r"^"
r"(?P<output>\S+) connected"
r"(?: (?P<primary>primary))? "
r"(?P<geometry>\d+x\d+\+\d+\+\d+) "
r"\(.*?\) "
r"\d+mm x \d+mm"
r"$"
)
current_setup = {
mo.groups()
for mo in {xrandr_re.match(line) for line in stdout.split("\n")}
if mo
}
if current_setup == output_update.last_setup:
logger.debug("current xrandr setup unchanged")
return
output_update.last_setup = current_setup
logger.info("xrandr setup: %s", current_setup)
if event is StartEvent:
return
logger.info("xrandr change detected")
cmds = (
"systemctl --user reload --no-block xsettingsd.service",
"systemctl --user start --no-block wallpaper.service",
@ -562,9 +593,7 @@ async def bluetooth_notifications(
signature="sa{sv}as",
)
)
async def dunst_status_update(
i3, event, path, interface, changed, invalid
):
async def dunst_status_update(i3, event, path, interface, changed, invalid):
"""Update notification status in polybar."""
if interface != "org.dunstproject.cmd0":
return

View file

@ -25,10 +25,10 @@ case "$1" in
trap "systemctl --user kill -s CONT redshift@${display}.service" EXIT
trap 'echo notify: user activity; kill %% 2> /dev/null; exit 0' HUP # user activity
trap 'echo notify: locker started; kill %% 2> /dev/null; exit 0' TERM # locker started
# outputs=($(xrandr --current | sed -n 's/\([^ ]*\) connected .*/\1/p'))
# for out in ${outputs[@]}; do
# xrandr --output $out --brightness 0.2
# done
outputs=($(xrandr --current | sed -n 's/\([^ ]*\) connected .*/\1/p'))
for out in ${outputs[@]}; do
xrandr --output $out --brightness 0.2
done
sleep infinity &
wait
echo "notify: end"