From 84b1aa99cc4269a62cf634956221d49152ff5173 Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Thu, 15 Jul 2021 15:14:39 +0200 Subject: [PATCH] i3-companion: don't restart everything on spurious RandR events --- bin/i3-companion | 39 ++++++++++++++++++++++++++++++++++----- bin/xss-lock | 8 ++++---- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/bin/i3-companion b/bin/i3-companion index b0f82d1..8d67e7c 100755 --- a/bin/i3-companion +++ b/bin/i3-companion @@ -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\S+) connected" + r"(?: (?Pprimary))? " + r"(?P\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 diff --git a/bin/xss-lock b/bin/xss-lock index 61c8340..8c63547 100755 --- a/bin/xss-lock +++ b/bin/xss-lock @@ -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"