Project

General

Profile

Bug #621 » hdmi_hotplug.sh

Linx, 11/14/2024 03:11 PM

 
1
#!/bin/bash
2

    
3
# Default DISPLAY if not set
4
export DISPLAY=${DISPLAY:-:0}
5

    
6
# Function to find and set XAUTHORITY and DISPLAY
7
function prepare_env() {
8
    # Try to find the X server process and extract DISPLAY and XAUTHORITY
9
    for pid in $(pgrep X); do
10
        PROC_DIR="/proc/$pid"
11
        
12
        # Ensure it's an X or Xorg process
13
        if readlink $PROC_DIR/exe | grep -qwE "X$|Xorg$"; then
14
            export XAUTHORITY=$(cat $PROC_DIR/cmdline | tr '\0' '\n' | grep -w "\-auth" -A 1 | tail -1)
15
            export DISPLAY=$(cat $PROC_DIR/cmdline | tr '\0' '\n' | grep -w "^:.*" || echo ":0")
16
            return
17
        fi
18
    done
19
}
20

    
21
# Check if xdpyinfo works, else try to set environment variables
22
if ! xdpyinfo &>/dev/null; then
23
    prepare_env
24
    if ! xdpyinfo &>/dev/null; then
25
        # Try to switch to an authorized user if necessary
26
        for XUSER in root $(users); do
27
            if sudo -u $XUSER xdpyinfo &>/dev/null; then
28
                logger -t $0 "Switching to user: $XUSER"
29
                sudo -u $XUSER $0
30
                exit 0
31
            fi
32
        done
33
        logger -t $0 "Unable to contact Xserver!"
34
        exit 1
35
    fi
36
fi
37

    
38
# Run xrandr for both HDMI and DP outputs
39
for output in $(xrandr | grep -E 'connected' | cut -d' ' -f1); do
40
    # Enable the connected output (HDMI or DP)
41
    xrandr --output $output --auto
42
done
43

    
44
exit 0
45

    
(3-3/3)