Skip to content

Instantly share code, notes, and snippets.

@nuvious
Created June 30, 2024 02:21
Show Gist options
  • Save nuvious/0f19ab07cc3af9fb1c90c83c1ab6f50f to your computer and use it in GitHub Desktop.
Save nuvious/0f19ab07cc3af9fb1c90c83c1ab6f50f to your computer and use it in GitHub Desktop.
Auto rotate 2 in 1 Laptop screens like the Lenovo Flex or Yoga
#!/bin/bash
# Template Configuration:
# ~/.config/autostart/auto-rotate.desktop
#
# [Desktop Entry]
# Type=Application
# Exec=sh -c "export DEVICE=eDP && /usr/bin/auto-rotate" # Change device identifier here
# Hidden=false
# NoDisplay=false
# X-GNOME-Autostart-enabled=true
# Name=Auto Rotate Screen
# Comment=Automatically rotates the screen based on sensor data
if [ -z "$DEVICE" ]; then
echo "DEVICE environment variable is not set."
echo "Set DEVICE variable in ~/.config/autostart/auto-rotate.desktop"
exit 1
fi
if ! which monitor-sensor; then
echo "Could not find monitor-sensor. Please inatll iio-sensor-proxy."
exit 1
fi
while IFS= read -r line; do
if [[ $line == *"Accelerometer orientation changed:"* ]]; then
orientation=$(echo $line | awk '{print $4}')
case $orientation in
normal)
xrandr --output $DEVICE --rotate normal
;;
left-up)
xrandr --output $DEVICE --rotate left
;;
right-up)
xrandr --output $DEVICE --rotate right
;;
bottom-up)
xrandr --output $DEVICE --rotate inverted
;;
esac
fi
done < <(monitor-sensor)
[Desktop Entry]
Type=Application
Exec=sh -c "export DEVICE=eDP && /usr/bin/auto-rotate" # Change device identifier here
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name=Auto Rotate Screen
Comment=Automatically rotates the screen based on sensor data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment