#!/bin/bash
# Get the current orientation from iio-sensor-proxy via dbus-monitor or use a tool like `monitor-sensor`
ORIENTATION=$(monitor-sensor | grep "Orientation changed" | tail -n1 | awk '{print $4}')
# Or use a simple loop with monitor-sensor (see below for example)
case "$1" in
normal)
swaymsg output "*" transform normal
;;
left)
swaymsg output "*" transform 90
;;
right)
swaymsg output "*" transform 270
;;
inverted)
swaymsg output "*" transform 180
;;
*)
swaymsg output "*" transform normal
;;
esac
Make it executable:
bash
chmod +x ~/bin/rotate-screen.sh
3. Use monitor-sensor to detect orientation changes and call the script
You can run a small daemon script that listens for orientation changes:
bash
#!/bin/bash
monitor-sensor | while read -r line; do
if echo "$line" | grep -q "Orientation changed"; then
ORIENTATION=$(echo "$line" | awk '{print $4}')
case "$ORIENTATION" in
normal)
~/bin/rotate-screen.sh normal
;;
left-up)
~/bin/rotate-screen.sh left
;;
right-up)
~/bin/rotate-screen.sh right
;;
bottom-up)
~/bin/rotate-screen.sh inverted
;;
esac
fi
done
Save this as ~/bin/auto-rotate.sh and make executable:
bash
chmod +x ~/bin/auto-rotate.sh
Run this script in the background when you start your Sway session, e.g., add to your ~/.config/sway/config:
bash
exec_always --no-startup-id ~/bin/auto-rotate.sh
Summary
Use iio-sensor-proxy to get orientation events.
Use a script to call swaymsg output "*" transform <rotation>.
Run a background script on sway startup to listen for orientation changes and rotate the screen.
If you want, I can help you write a more complete, ready-to-use script or systemd user service for this. Just ask!
User
is it easier to just define a shortcut in the config to do it manually?
Assistant
Yes! Defining a manual shortcut in your Sway config to rotate the screen is definitely easier and quicker to set up than automatic rotation via sensors.
How to add manual rotation shortcuts in Sway config
Open your Sway config file (usually ~/.config/sway/config) and add keybindings like this:
bash
# Rotate screen to normal orientation
bindsym $mod+Shift+o exec swaymsg output "*" transform normal
# Rotate screen 90 degrees left
bindsym $mod+Shift+l exec swaymsg output "*" transform 90
# Rotate screen 90 degrees right
bindsym $mod+Shift+r exec swaymsg output "*" transform 270
# Rotate screen upside down
bindsym $mod+Shift+i exec swaymsg output "*" transform 180
Replace $mod with your modifier key (usually Mod4 = Super/Windows key).
You can customize the key combos as you like.
How to use
Press Mod+Shift+o to set normal orientation.
Press Mod+Shift+l to rotate left.
Press Mod+Shift+r to rotate right.
Press Mod+Shift+i to invert.
Summary
Manual shortcuts are simple and fast to implement.
No need to install or configure sensor daemons.
You control rotation on demand.
If you want, I can help you with a script or more advanced setup later, but this is the easiest way to get started!