Krolyxon
Commit 2f3c197

Branches

Refs →

aurora.localbinhyprhypr_screen_rotate.sh

114 lines · 4.1 KB · Bourne Again Shell (bash)

1#!/usr/bin/env bash
2# Script taken from and modified little bit from:
3# https://github.com/dusklinux/dusky/blob/f8a28f425743e478cddefc327e6e554dfcf9521d/user_scripts/hypr/screen_rotate.sh
4
5# 1. Strict Mode & Safety (Bash 5+ Standards)
6# ------------------------------------------------------------------------------
7set -euo pipefail
8IFS=$'\n\t'
9
10# 2. Global Constants (ANSI-C Quoting for "Elite" Color Handling)
11# ------------------------------------------------------------------------------
12readonly C_RED=$'\e[31m'
13readonly C_GREEN=$'\e[32m'
14readonly C_YELLOW=$'\e[33m'
15readonly C_BLUE=$'\e[34m'
16readonly C_BOLD=$'\e[1m'
17readonly C_RESET=$'\e[0m'
18
19# cleanup_trap: Ensures clean exit codes are respected.
20cleanup_trap() {
21 local exit_code=$?
22 if [[ $exit_code -ne 0 ]]; then
23 printf "%s[ERROR]%s Script aborted unexpectedly (Exit Code: %d).\n" \
24 "$C_RED" "$C_RESET" "$exit_code" >&2
25 fi
26}
27trap cleanup_trap EXIT
28
29# 3. Environment & Privilege Checks
30# ------------------------------------------------------------------------------
31# Dependency Check: We need 'jq' for JSON parsing.
32if ! command -v jq &> /dev/null; then
33 printf "%s[ERROR]%s 'jq' is missing. Install it with: sudo pacman -S jq\n" \
34 "$C_RED" "$C_RESET" >&2
35 exit 1
36fi
37
38# Root Check: Hyprland IPC fails if executed as root/sudo due to socket ownership.
39if [[ $EUID -eq 0 ]]; then
40 printf "%s[ERROR]%s Root detected. Please run this as your normal user to access the Hyprland socket.\n" \
41 "$C_RED" "$C_RESET" >&2
42 exit 1
43fi
44
45# 4. Argument Parsing (+90 or -90)
46# ------------------------------------------------------------------------------
47DIRECTION=0
48
49if [[ $# -ne 1 ]]; then
50 printf "%s[INFO]%s Usage: %s [+90|-90]\n" \
51 "$C_YELLOW" "$C_RESET" "${0##*/}"
52 exit 1
53fi
54
55case "$1" in
56 "+90") DIRECTION=1 ;; # Clockwise
57 "-90") DIRECTION=-1 ;; # Counter-Clockwise
58 *)
59 printf "%s[ERROR]%s Invalid flag '%s'. Use +90 or -90.\n" \
60 "$C_RED" "$C_RESET" "$1" >&2
61 exit 1
62 ;;
63esac
64
65# 5. Hardware Detection (Smart Query)
66# ------------------------------------------------------------------------------
67# We fetch the entire JSON blob once to minimize IPC calls (Performance).
68# We select the current focused monitor
69MON_STATE=$(hyprctl monitors -j | jq -r '.[] | select(.focused == true)')
70
71# Extract precise values using jq
72NAME=$(printf "%s" "$MON_STATE" | jq -r '.name')
73SCALE=$(printf "%s" "$MON_STATE" | jq -r '.scale')
74CURRENT_TRANSFORM=$(printf "%s" "$MON_STATE" | jq -r '.transform')
75
76# Validation: Ensure we actually found a monitor
77if [[ -z "$NAME" || "$NAME" == "null" ]]; then
78 printf "%s[ERROR]%s No active monitors detected via Hyprland IPC.\n" \
79 "$C_RED" "$C_RESET" >&2
80 exit 1
81fi
82
83# 6. Transformation Logic (Modulo Arithmetic)
84# ------------------------------------------------------------------------------
85# Hyprland Transforms: 0=Normal, 1=90, 2=180, 3=270
86# The '+ 4' ensures we handle negative wraparounds correctly in Bash logic.
87NEW_TRANSFORM=$(( (CURRENT_TRANSFORM + DIRECTION + 4) % 4 ))
88
89# 7. Execution (State overwrite)
90# ------------------------------------------------------------------------------
91# We use 'preferred' and 'auto' to remain robust against resolution changes,
92# but we STRICTLY inject the detected $SCALE to prevent UI scaling issues.
93
94printf "%s[INFO]%s Rotating %s%s%s (Scale: %s): %d -> %d\n" \
95 "$C_BLUE" "$C_RESET" "$C_BOLD" "$NAME" "$C_RESET" "$SCALE" "$CURRENT_TRANSFORM" "$NEW_TRANSFORM"
96
97# Apply the new configuration immediately via IPC
98if hyprctl keyword monitor "${NAME}, preferred, auto, ${SCALE}, transform, ${NEW_TRANSFORM}" > /dev/null; then
99 printf "%s[SUCCESS]%s Rotation applied successfully.\n" \
100 "$C_GREEN" "$C_RESET"
101
102 # Notify user visually if notify-send is available (optional UX improvement)
103 if command -v notify-send &> /dev/null; then
104 notify-send -a "System" "Display Rotated" "Monitor: $NAME\nTransform: $NEW_TRANSFORM" -h string:x-canonical-private-synchronous:display-rotate
105 fi
106else
107 printf "%s[ERROR]%s Failed to apply Hyprland keyword.\n" \
108 "$C_RED" "$C_RESET" >&2
109 exit 1
110fi
111
112# Clean exit
113trap - EXIT
114exit 0