aboutsummaryrefslogtreecommitdiff
path: root/.local/bin/hypr/hypr_screen_rotate.sh
blob: 5fdf06268fa1c652ba39ed34a6ce55886fb313e1 (plain)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env bash
# Script taken from and modified little bit from:
# https://github.com/dusklinux/dusky/blob/f8a28f425743e478cddefc327e6e554dfcf9521d/user_scripts/hypr/screen_rotate.sh

# 1. Strict Mode & Safety (Bash 5+ Standards)
# ------------------------------------------------------------------------------
set -euo pipefail
IFS=$'\n\t'

# 2. Global Constants (ANSI-C Quoting for "Elite" Color Handling)
# ------------------------------------------------------------------------------
readonly C_RED=$'\e[31m'
readonly C_GREEN=$'\e[32m'
readonly C_YELLOW=$'\e[33m'
readonly C_BLUE=$'\e[34m'
readonly C_BOLD=$'\e[1m'
readonly C_RESET=$'\e[0m'

# cleanup_trap: Ensures clean exit codes are respected.
cleanup_trap() {
    local exit_code=$?
    if [[ $exit_code -ne 0 ]]; then
        printf "%s[ERROR]%s Script aborted unexpectedly (Exit Code: %d).\n" \
            "$C_RED" "$C_RESET" "$exit_code" >&2
    fi
}
trap cleanup_trap EXIT

# 3. Environment & Privilege Checks
# ------------------------------------------------------------------------------
# Dependency Check: We need 'jq' for JSON parsing.
if ! command -v jq &> /dev/null; then
    printf "%s[ERROR]%s 'jq' is missing. Install it with: sudo pacman -S jq\n" \
        "$C_RED" "$C_RESET" >&2
    exit 1
fi

# Root Check: Hyprland IPC fails if executed as root/sudo due to socket ownership.
if [[ $EUID -eq 0 ]]; then
    printf "%s[ERROR]%s Root detected. Please run this as your normal user to access the Hyprland socket.\n" \
        "$C_RED" "$C_RESET" >&2
    exit 1
fi

# 4. Argument Parsing (+90 or -90)
# ------------------------------------------------------------------------------
DIRECTION=0

if [[ $# -ne 1 ]]; then
    printf "%s[INFO]%s Usage: %s [+90|-90]\n" \
        "$C_YELLOW" "$C_RESET" "${0##*/}"
    exit 1
fi

case "$1" in
    "+90") DIRECTION=1 ;;  # Clockwise
    "-90") DIRECTION=-1 ;; # Counter-Clockwise
    *)
        printf "%s[ERROR]%s Invalid flag '%s'. Use +90 or -90.\n" \
            "$C_RED" "$C_RESET" "$1" >&2
        exit 1
        ;;
esac

# 5. Hardware Detection (Smart Query)
# ------------------------------------------------------------------------------
# We fetch the entire JSON blob once to minimize IPC calls (Performance).
# We select the current focused monitor
MON_STATE=$(hyprctl monitors -j | jq -r '.[] | select(.focused == true)')

# Extract precise values using jq
NAME=$(printf "%s" "$MON_STATE" | jq -r '.name')
SCALE=$(printf "%s" "$MON_STATE" | jq -r '.scale')
CURRENT_TRANSFORM=$(printf "%s" "$MON_STATE" | jq -r '.transform')

# Validation: Ensure we actually found a monitor
if [[ -z "$NAME" || "$NAME" == "null" ]]; then
    printf "%s[ERROR]%s No active monitors detected via Hyprland IPC.\n" \
        "$C_RED" "$C_RESET" >&2
    exit 1
fi

# 6. Transformation Logic (Modulo Arithmetic)
# ------------------------------------------------------------------------------
# Hyprland Transforms: 0=Normal, 1=90, 2=180, 3=270
# The '+ 4' ensures we handle negative wraparounds correctly in Bash logic.
NEW_TRANSFORM=$(( (CURRENT_TRANSFORM + DIRECTION + 4) % 4 ))

# 7. Execution (State overwrite)
# ------------------------------------------------------------------------------
# We use 'preferred' and 'auto' to remain robust against resolution changes,
# but we STRICTLY inject the detected $SCALE to prevent UI scaling issues.

printf "%s[INFO]%s Rotating %s%s%s (Scale: %s): %d -> %d\n" \
    "$C_BLUE" "$C_RESET" "$C_BOLD" "$NAME" "$C_RESET" "$SCALE" "$CURRENT_TRANSFORM" "$NEW_TRANSFORM"

# Apply the new configuration immediately via IPC
if hyprctl keyword monitor "${NAME}, preferred, auto, ${SCALE}, transform, ${NEW_TRANSFORM}" > /dev/null; then
    printf "%s[SUCCESS]%s Rotation applied successfully.\n" \
        "$C_GREEN" "$C_RESET"

    # Notify user visually if notify-send is available (optional UX improvement)
    if command -v notify-send &> /dev/null; then
        notify-send -a "System" "Display Rotated" "Monitor: $NAME\nTransform: $NEW_TRANSFORM" -h string:x-canonical-private-synchronous:display-rotate
    fi
else
    printf "%s[ERROR]%s Failed to apply Hyprland keyword.\n" \
        "$C_RED" "$C_RESET" >&2
    exit 1
fi

# Clean exit
trap - EXIT
exit 0