| 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 | # ------------------------------------------------------------------------------ |
| 7 | set -euo pipefail |
| 8 | IFS=$'\n\t' |
| 9 | |
| 10 | # 2. Global Constants (ANSI-C Quoting for "Elite" Color Handling) |
| 11 | # ------------------------------------------------------------------------------ |
| 12 | readonly C_RED=$'\e[31m' |
| 13 | readonly C_GREEN=$'\e[32m' |
| 14 | readonly C_YELLOW=$'\e[33m' |
| 15 | readonly C_BLUE=$'\e[34m' |
| 16 | readonly C_BOLD=$'\e[1m' |
| 17 | readonly C_RESET=$'\e[0m' |
| 18 | |
| 19 | # cleanup_trap: Ensures clean exit codes are respected. |
| 20 | cleanup_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 | } |
| 27 | trap cleanup_trap EXIT |
| 28 | |
| 29 | # 3. Environment & Privilege Checks |
| 30 | # ------------------------------------------------------------------------------ |
| 31 | # Dependency Check: We need 'jq' for JSON parsing. |
| 32 | if ! 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 |
| 36 | fi |
| 37 | |
| 38 | # Root Check: Hyprland IPC fails if executed as root/sudo due to socket ownership. |
| 39 | if [[ $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 |
| 43 | fi |
| 44 | |
| 45 | # 4. Argument Parsing (+90 or -90) |
| 46 | # ------------------------------------------------------------------------------ |
| 47 | DIRECTION=0 |
| 48 | |
| 49 | if [[ $# -ne 1 ]]; then |
| 50 | printf "%s[INFO]%s Usage: %s [+90|-90]\n" \ |
| 51 | "$C_YELLOW" "$C_RESET" "${0##*/}" |
| 52 | exit 1 |
| 53 | fi |
| 54 | |
| 55 | case "$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 | ;; |
| 63 | esac |
| 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 |
| 69 | MON_STATE=$(hyprctl monitors -j | jq -r '.[] | select(.focused == true)') |
| 70 | |
| 71 | # Extract precise values using jq |
| 72 | NAME=$(printf "%s" "$MON_STATE" | jq -r '.name') |
| 73 | SCALE=$(printf "%s" "$MON_STATE" | jq -r '.scale') |
| 74 | CURRENT_TRANSFORM=$(printf "%s" "$MON_STATE" | jq -r '.transform') |
| 75 | |
| 76 | # Validation: Ensure we actually found a monitor |
| 77 | if [[ -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 |
| 81 | fi |
| 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. |
| 87 | NEW_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 | |
| 94 | printf "%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 |
| 98 | if 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 |
| 106 | else |
| 107 | printf "%s[ERROR]%s Failed to apply Hyprland keyword.\n" \ |
| 108 | "$C_RED" "$C_RESET" >&2 |
| 109 | exit 1 |
| 110 | fi |
| 111 | |
| 112 | # Clean exit |
| 113 | trap - EXIT |
| 114 | exit 0 |