Skip to content

Instantly share code, notes, and snippets.

@ajmeese7
Last active March 19, 2025 19:12
Show Gist options
  • Save ajmeese7/cc85d5eccc41b23e221666ee6e6f6edc to your computer and use it in GitHub Desktop.
Save ajmeese7/cc85d5eccc41b23e221666ee6e6f6edc to your computer and use it in GitHub Desktop.
Automatically handle screen rotation in X11
#!/bin/bash
# auto-rotate.sh
# This script waits until the graphical session is fully ready, sets the display rotation based on
# a desired rotation variable, and then re-applies the corresponding touchscreen calibration multiple times.
#
# Save this script (for example, in your home directory) and make it executable:
# chmod +x ~/auto-rotate.sh
#
# Deploy it via your systemd user service so that it runs after login.
#
# Valid rotation options: normal, left, right, inverted
# Wait for the X session to fully load.
sleep 10
# Set the desired rotation here:
desired_rotation="right"
# Ensure we're using the correct display environment.
export DISPLAY=:0
export XAUTHORITY=/home/$USER/.Xauthority
# Detect the primary display automatically.
output=$(xrandr | grep " connected primary" | awk '{print $1}')
if [ -z "$output" ]; then
output=$(xrandr | grep " connected" | head -n 1 | awk '{print $1}')
fi
if [ -z "$output" ]; then
echo "No connected display found. Exiting."
exit 1
fi
echo "Setting display $output rotation to $desired_rotation..."
xrandr --output "$output" --rotate "$desired_rotation"
# Determine the corresponding transformation matrix for the touchscreen based on desired rotation.
case "$desired_rotation" in
normal)
matrix="1 0 0 0 1 0 0 0 1"
;;
left)
matrix="0 -1 1 1 0 0 0 0 1"
;;
right)
matrix="0 1 0 -1 0 1 0 0 1"
;;
inverted)
matrix="-1 0 1 0 -1 1 0 0 1"
;;
*)
echo "Unknown rotation: $desired_rotation. Defaulting to normal."
matrix="1 0 0 0 1 0 0 0 1"
;;
esac
# Auto-detect the touchscreen device (first device with "touch" in its name).
touchscreen=$(xinput list --name-only | grep -i "touch" | head -n 1)
if [ -z "$touchscreen" ]; then
echo "No touchscreen device found. Exiting."
exit 1
fi
echo "Detected touchscreen device: $touchscreen"
echo "Target transformation matrix: $matrix"
# Reapply the transformation matrix several times to ensure it sticks.
for i in {1..5}; do
echo "Reapplying transformation matrix (attempt $i)..."
xinput set-prop "$touchscreen" "Coordinate Transformation Matrix" $matrix
sleep 2
done
echo "Display rotation and touchscreen calibration complete."

How to Deploy the Script

NOTE: Change every instance of $USER in the commands and the Bash file to the user you want this to run for!

  1. Save the Script: Save the script above as auto-rotate.sh in your home directory (or another location of your choice).

  2. Make It Executable: Open a terminal (or via SSH) and run:

chmod +x ~/auto-rotate.sh
  1. Test the Script Manually: To verify it works correctly (while logged in locally on the display), run:
~/auto-rotate.sh

You should see output confirming the display rotation and touchscreen calibration. Adjust the desired_rotation variable if needed.

  1. Create a Systemd User Service: Create a systemd service file in your user configuration directory. Run these commands via SSH or a terminal:
mkdir -p ~/.config/systemd/user
nano ~/.config/systemd/user/rotate.service

Paste in the contents of rotate.service. Save and exit the editor (in nano, press Ctrl+O, Enter, then Ctrl+X).

  1. Enable and Start the Service: Reload your user systemd configuration and enable the service:
systemctl --user daemon-reload
systemctl --user enable rotate.service
  1. Reboot and Verify: Reboot your Raspberry Pi:
sudo reboot

When you log in locally, the systemd user service should run your script after the graphical target is reached, ensuring the proper rotation and touchscreen calibration is applied to your desktop.


Notes

  • The script uses a 10‑second delay (sleep 10) to ensure the X server is fully ready before running the commands. If needed, you can adjust this value.
  • The script automatically detects your primary display and the first touchscreen device it finds. If you have more than one touchscreen or special configuration requirements, you might need to adjust the detection logic.
  • Make sure your user’s .Xauthority file is valid and accessible; otherwise, the script may not be able to communicate with the X server.
[Unit]
Description=Auto Rotate Display and Calibrate Touchscreen
After=graphical.target
[Service]
Type=oneshot
Environment=DISPLAY=:0
Environment=XAUTHORITY=/home/$USER/.Xauthority
ExecStart=/home/$USER/auto-rotate.sh
TimeoutSec=0
[Install]
WantedBy=default.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment