-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup-x-resize.sh
96 lines (79 loc) · 2.62 KB
/
setup-x-resize.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/bash
# Check if the script is run as root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root."
exit 1
fi
# Get the current user and their home directory
CURRENT_USER=$(logname)
USER_HOME=$(eval echo "~$CURRENT_USER")
# Paths for the service and script
SERVICE_FILE="/etc/systemd/system/x-resize.service"
SCRIPT_FILE="/usr/local/bin/x-resize"
echo "Configuring for user: $CURRENT_USER"
# Create the systemd service file
cat <<EOF > "$SERVICE_FILE"
[Unit]
Description=Auto resize X screen on resolution change
After=lightdm.service
Requires=lightdm.service
[Service]
ExecStart=$SCRIPT_FILE
ExecStartPre=/bin/sleep 5
User=$CURRENT_USER
Environment=DISPLAY=:0
Environment=XAUTHORITY=$USER_HOME/.Xauthority
Type=simple
Restart=always
[Install]
WantedBy=graphical.target
EOF
echo "Created systemd service file: $SERVICE_FILE"
# Create the executable script
cat <<'EOF' > "$SCRIPT_FILE"
#!/bin/bash
# Script to listen for RANDR events and automatically adjust the resolution
function x_resize() {
declare -A disps usrs
usrs=()
disps=()
# Get active users from the 'w' command, skipping 'root'
for i in $(w -h | awk '{print $1}' | sort | uniq); do
[[ $i = root ]] && continue # Skip the root user
usrs[$i]=1
echo "Found user: $i"
done
# Iterate through users to find active X sessions
for u in "${!usrs[@]}"; do
# Check environment variables and X processes
session_display=$(sudo -u "$u" printenv DISPLAY 2>/dev/null)
if [[ -n "$session_display" ]]; then
disps[$session_display]=$u
fi
done
# Listen for resolution changes
xev -root -event randr | \
grep --line-buffered 'subtype XRROutputChangeNotifyEvent' | \
while read foo ; do
for d in "${!disps[@]}"; do
session_user="${disps[$d]}"
session_display="$d"
session_output=$(sudo -u "$session_user" PATH=/usr/bin DISPLAY="$session_display" xrandr | awk '/ connected/{print $1; exit; }')
echo "Session User: $session_user"
echo "Session Display: $session_display"
echo "Session Output: $session_output"
sudo -u "$session_user" PATH=/usr/bin DISPLAY="$session_display" xrandr --output "$session_output" --auto
done
done
}
# Run the function
x_resize
EOF
# Set appropriate permissions for the script
chmod +x "$SCRIPT_FILE"
echo "Created script: $SCRIPT_FILE"
# Reload systemd configuration, enable the service at startup, and start it
systemctl daemon-reload
systemctl enable x-resize.service
systemctl start x-resize.service
echo "Service has been configured and started."