2024-08-15 03:41:41 +01:00
|
|
|
#!/bin/sh -e
|
2024-08-05 20:24:05 +01:00
|
|
|
|
2024-08-21 08:18:34 +01:00
|
|
|
. ../../common-script.sh
|
2024-08-15 06:15:12 +01:00
|
|
|
|
2024-09-12 21:14:50 +01:00
|
|
|
# Function to check xrandr is installed
|
2024-08-15 06:15:12 +01:00
|
|
|
setup_xrandr() {
|
2024-09-19 01:37:59 +01:00
|
|
|
printf "%b\n" "${YELLOW}Installing xrandr...${RC}"
|
2024-08-15 06:15:12 +01:00
|
|
|
if ! command_exists xrandr; then
|
2024-09-17 13:23:23 +01:00
|
|
|
case "$PACKAGER" in
|
2024-09-12 21:14:50 +01:00
|
|
|
pacman)
|
2024-09-19 01:03:32 +01:00
|
|
|
"$ESCALATION_TOOL" "$PACKAGER" -S --noconfirm xorg-xrandr
|
2024-09-12 21:14:50 +01:00
|
|
|
;;
|
2024-09-16 15:04:15 +01:00
|
|
|
apt-get|nala)
|
2024-09-19 01:03:32 +01:00
|
|
|
"$ESCALATION_TOOL" "$PACKAGER" install -y x11-xserver-utils
|
2024-09-12 21:14:50 +01:00
|
|
|
;;
|
2024-11-07 19:03:21 +00:00
|
|
|
apk)
|
|
|
|
"$ESCALATION_TOOL" "$PACKAGER" add xrandr
|
|
|
|
;;
|
2024-09-12 21:14:50 +01:00
|
|
|
*)
|
2024-09-19 01:03:32 +01:00
|
|
|
"$ESCALATION_TOOL" "$PACKAGER" install -y xorg-x11-server-utils
|
2024-09-12 21:14:50 +01:00
|
|
|
;;
|
2024-08-15 06:15:12 +01:00
|
|
|
esac
|
|
|
|
else
|
2024-09-19 01:37:59 +01:00
|
|
|
printf "%b\n" "${GREEN}xrandr is already installed.${RC}"
|
2024-08-15 06:15:12 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2024-08-05 20:24:05 +01:00
|
|
|
# Function to execute xrandr commands and handle errors
|
|
|
|
execute_command() {
|
2024-09-12 21:14:50 +01:00
|
|
|
command="$1"
|
2024-09-19 01:37:59 +01:00
|
|
|
printf "Executing: %s\n" "$command"
|
2024-08-05 20:24:05 +01:00
|
|
|
eval "$command" 2>&1 | tee /tmp/xrandr.log | tail -n 20
|
|
|
|
if [ $? -ne 0 ]; then
|
2024-09-19 01:37:59 +01:00
|
|
|
printf "%b\n" "${RED}An error occurred while executing the command. Check /tmp/xrandr.log for details.${RC}"
|
2024-08-05 20:24:05 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Function to detect connected monitors
|
|
|
|
detect_connected_monitors() {
|
|
|
|
xrandr_output=$(xrandr)
|
2024-09-22 17:01:10 +01:00
|
|
|
printf "%b\n" "$xrandr_output" | grep " connected" | awk '{print $1}'
|
2024-08-05 20:24:05 +01:00
|
|
|
}
|
|
|
|
|
2024-09-18 15:20:52 +01:00
|
|
|
# Function to get the current brightness for a monitor
|
|
|
|
get_current_brightness() {
|
|
|
|
monitor="$1"
|
|
|
|
xrandr --verbose | grep -A 10 "^$monitor connected" | grep "Brightness:" | awk '{print $2}'
|
|
|
|
}
|
|
|
|
|
2024-08-05 20:24:05 +01:00
|
|
|
# Function to get resolutions for a monitor
|
|
|
|
get_unique_resolutions() {
|
2024-09-12 21:14:50 +01:00
|
|
|
monitor="$1"
|
2024-08-05 20:24:05 +01:00
|
|
|
xrandr_output=$(xrandr)
|
2024-09-19 01:37:59 +01:00
|
|
|
available_resolutions=$(printf "%s" "$xrandr_output" | sed -n "/$monitor connected/,/^[^ ]/p" | grep -oP '\d+x\d+' | sort -u)
|
2024-08-29 22:27:36 +01:00
|
|
|
|
|
|
|
standard_resolutions="1920x1080 1280x720 1600x900 2560x1440 3840x2160"
|
|
|
|
|
2024-09-12 21:14:50 +01:00
|
|
|
temp_file=$(mktemp)
|
2024-09-19 01:37:59 +01:00
|
|
|
printf "%s" "$available_resolutions" > "$temp_file"
|
2024-09-12 21:14:50 +01:00
|
|
|
|
2024-09-19 01:37:59 +01:00
|
|
|
filtered_standard_resolutions=$(printf "%s" "$standard_resolutions" | tr ' ' '\n' | grep -xF -f "$temp_file")
|
2024-09-12 21:14:50 +01:00
|
|
|
|
|
|
|
rm "$temp_file"
|
|
|
|
|
|
|
|
available_res_file=$(mktemp)
|
|
|
|
filtered_standard_res_file=$(mktemp)
|
2024-09-19 01:37:59 +01:00
|
|
|
printf "%s" "$available_resolutions" | sort > "$available_res_file"
|
|
|
|
printf "%s" "$filtered_standard_resolutions" | sort > "$filtered_standard_res_file"
|
2024-08-29 22:27:36 +01:00
|
|
|
|
2024-09-12 21:14:50 +01:00
|
|
|
remaining_resolutions=$(comm -23 "$available_res_file" "$filtered_standard_res_file")
|
|
|
|
|
|
|
|
rm "$available_res_file" "$filtered_standard_res_file"
|
2024-08-29 22:27:36 +01:00
|
|
|
|
2024-09-12 21:14:50 +01:00
|
|
|
printf "%b\n" "$filtered_standard_resolutions\n$remaining_resolutions" | head -n 10
|
2024-08-05 20:24:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Function to prompt for confirmation
|
|
|
|
confirm_action() {
|
2024-09-12 21:14:50 +01:00
|
|
|
action="$1"
|
2024-09-18 18:53:47 +01:00
|
|
|
printf "%b\n" "${CYAN}$action${RC}"
|
2024-09-22 17:01:10 +01:00
|
|
|
printf "%b" "${CYAN}Are you sure? (y/N): ${RC}"
|
2024-09-18 18:53:47 +01:00
|
|
|
read -r confirm
|
2024-09-12 21:14:50 +01:00
|
|
|
if echo "$confirm" | grep -qE '^[Yy]$'; then
|
2024-08-05 20:24:05 +01:00
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
2024-08-15 06:15:12 +01:00
|
|
|
|
2024-09-20 03:17:18 +01:00
|
|
|
checkEmpty() {
|
|
|
|
if [ -z "$1" ]; then
|
|
|
|
printf "%b\n" "${RED}Empty value is not allowed${RC}" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
checkGroups() {
|
|
|
|
groups="$1"
|
|
|
|
available_groups="$2"
|
|
|
|
for group in $groups; do
|
|
|
|
if ! echo "$available_groups" | grep -q -w "$group"; then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
confirmAction() {
|
|
|
|
if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then
|
|
|
|
printf "%b\n" "${RED}Cancelled operation...${RC}" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2024-08-15 06:15:12 +01:00
|
|
|
checkEnv
|
2024-09-16 15:04:15 +01:00
|
|
|
checkEscalationTool
|
|
|
|
setup_xrandr
|