You are not logged in.

#1 2020-11-25 11:22:07

BozanicJosip
Member
Registered: 2020-05-11
Posts: 10

Polybar refresh on monitor hotplugging

Hello,

I've been struggling with this issue for couple of days now. I just can't seem to get my polybar to reload when I hotplug my monitor

First of all to enable hotplugging I've created a 98-monitor-hotplug udev rule in /etc/udev/rules.d:

KERNEL=="card0", SUBSYSTEM=="drm", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/bozanicjosip/.Xauthority", ENV{HOME}="/home/bozanicjosip", RUN+="/usr/local/bin/monitor-hotplug --notify"

my monitor-hotplug script:

#!/bin/bash
if [ -r /home/bozanicjosip/.dbus/Xdbus ]; then
  source /home/bozanicjosip/.dbus/Xdbus
fi

hdmi_status=$(/usr/bin/xrandr | grep HDMI | awk '{print$2}')

main_output=$(/usr/bin/xrandr | grep eDP | awk '{print$1}')
hdmi_output=$(/usr/bin/xrandr | grep HDMI | awk '{print$1}')

function connect(){
  	/usr/bin/xrandr --output "$2" --mode 2560x1440
	/usr/bin/xrandr --output "$2" --auto --right-of "$1"
	notification="HDMI plugged in"
}

function disconnect(){
	/usr/bin/xrandr --output "$1" --off
	notification="HDMI plugged out"
}

function default(){
	/usr/bin/xrandr --auto
}

if [ "${hdmi_status}" = connected ]; then
	connect "$main_output" "$hdmi_output"
elif [ "${hdmi_status}" = disconnected ]; then
	disconnect "$hdmi_output"
else default
fi

if [ "$1" == "--notify" ] && [ -v notification ]; then
	su bozanicjosip -c "/usr/bin/notify-send --urgency low -t 5000 'Graphics Update' '$notification'"
fi

/home/bozanicjosip/.bin/refresh-wallpaper
/home/bozanicjosip/.config/polybar/launch.sh &

my polybar launch script:

#!/usr/bin/env bash
config_file=/home/bozanicjosip/.config/polybar/config

# Terminate already running bar instances
killall -q polybar
# If all your bars have ipc enabled, you can also use
# polybar-msg cmd quit

# Launch bar1 and bar2
if type "xrandr"; then
    IFS=';' read -ra connected_outputs <<<"$(/home/bozanicjosip/.bin/connected-outputs)"

    for outputs in "${connected_outputs[@]}"; do
        IFS=',' read -ra output_pair <<<"$outputs"

        standard_output="${output_pair[0]}"
        monitor_output="${output_pair[1]}"

        echo "$monitor_output" | systemd-cat -p emerg
        echo polybar -m | systemd-cat -p emerg

        MONITOR=$monitor_output polybar -c "$config_file" --reload main-"$standard_output" &
    done
else
    MONITOR=eDP polybar -c "$config_file" --reload main-eDP &
fi

I've already tested this part and connected outputs script is returning the correct outputs. When I run monitor-hotplug with sudo everything works normally, however when I run it on udev the screens are arranged correctly, but polybar is not.

Any ideas on this one?

Last edited by BozanicJosip (2020-11-25 11:22:30)

Offline

#2 2020-11-25 15:32:39

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 77,697

Re: Polybar refresh on monitor hotplugging

You don't seem to switch the user for launch.sh? (or monitor-hotplug)

Offline

#3 2020-11-25 16:06:42

BozanicJosip
Member
Registered: 2020-05-11
Posts: 10

Re: Polybar refresh on monitor hotplugging

Should I be switching the user? I'm running the command from root and it also works hmm I thought that might be a good indication that the things work correctly

Offline

#4 2020-11-25 16:14:15

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 77,697

Re: Polybar refresh on monitor hotplugging

You want to run polybar for the X11 session of bozanicjosip, right?
Run it as that user (and ensure to export $DISPLAY, $XAUTHORITY and maybe $DBUS_SESSION_BUS_ADDRESS

Even if you gain access to the X11 server (either through xhost or the XAUTHORITY export) you don't want to run your desktop as root and polybar might or not bail on that condition.

Offline

#5 2020-11-25 16:53:41

BozanicJosip
Member
Registered: 2020-05-11
Posts: 10

Re: Polybar refresh on monitor hotplugging

Something I've noticed is that

if type "xrandr"; then

doesn't get executed in udev. Is there anything I should do with this part to work? I think this might be the issue as I get no output from then on. I've basically picked that if conditional somewhere so am not sure if that might be the issue. All of the env variables you've stated above are set correctly

Offline

#6 2020-11-25 17:01:50

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 77,697

Re: Polybar refresh on monitor hotplugging

You don't need the quotes, but the check is ok.

What do you mean by "doesn't get executed in udev"?
That the test isn't performed or returns false?

I'd throw a bunch of "touch /tmp/test1", "touch /tmp/test2" … into the script to track the execution from the udev rule.

Offline

#7 2020-11-25 17:11:17

BozanicJosip
Member
Registered: 2020-05-11
Posts: 10

Re: Polybar refresh on monitor hotplugging

Yeah I've basically put touch /tmp/xrandrb before and /tmp/xrandra just after the if statement and the xrandra file is not created :scratching_my_head:

Offline

#8 2020-11-25 17:12:43

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 77,697

Re: Polybar refresh on monitor hotplugging

Bad $PATH?

Offline

#9 2020-11-25 17:26:13

BozanicJosip
Member
Registered: 2020-05-11
Posts: 10

Re: Polybar refresh on monitor hotplugging

This is crazy now, I'm outputting the type xrandr command into xrandrb tmp file and the output is

xrandr is /usr/bin/xrandr

Offline

#10 2020-11-25 17:29:57

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 77,697

Re: Polybar refresh on monitor hotplugging

What's the exit code of "type xrandr"? ("$?")

Offline

#11 2020-11-25 18:00:36

BozanicJosip
Member
Registered: 2020-05-11
Posts: 10

Re: Polybar refresh on monitor hotplugging

I think this had to do with the fact that I was moving the launc-polybar script to the background. When I've removed the & the script would proceed. Actually I'm coming quite close. I just have this weird issue now where polybar is working for 5 seconds then it disappers :S

I can update my scripts here again if needed since I was changing quite some things

Offline

#12 2020-11-25 21:32:54

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 77,697

Re: Polybar refresh on monitor hotplugging

You cannot run long-term processes via udev, https://wiki.archlinux.org/index.php/Ud … _processes

Offline

#13 2020-11-26 09:54:45

BozanicJosip
Member
Registered: 2020-05-11
Posts: 10

Re: Polybar refresh on monitor hotplugging

That definitely could be an issue. Cleanest solution in the end might be to create my own service that would be responsible for that. I'll look into that since this setup is giving me headaches. Thank you smile

Offline

Board footer

Powered by FluxBB