Auto-Rotation on KDE Plasma ( Wayland ) – Lenovo ThinkPad X1 Tablet Gen 3

CONTEXT

--------

Debian 13 (Trixie) + KDE Plasma 5.27 on Wayland.  

Lenovo ThinkPad X1 Tablet Gen 3 (QHD 3000×2000, i7-8550U).  

iio-sensor-proxy detects the accelerometer but doesn’t publish orientation on DBus.  

Solution: use raw accelerometer values and rotate via kscreen-doctor (KDE’s Wayland tool).



REQUIREMENTS

-------------

sudo apt install kscreen  

Accelerometer visible at /sys/bus/iio/devices/iio:device*/in_accel_x_raw



FINAL SCRIPT — ~/.local/bin/autorotate-wayland.sh

-------------------------------------------------

#!/usr/bin/env bash

# Auto-rotate for KDE Plasma (Wayland) – ThinkPad X1 Tablet Gen 3

# Reads raw accelerometer data and rotates screen via kscreen-doctor.

# Touch/pen mapping handled automatically by Wayland.

# Create ~/.config/autorotate.lock to disable rotation temporarily.


OUTPUT_ID="output.1"                                 # from `kscreen-doctor -o`

IIO_DIR="/sys/bus/iio/devices/iio:device3"           # adjust if sensor index differs

INTERVAL=1                                           # seconds

STABILITY_COUNT=3                                    # debounce samples

THRESHOLD=2000                                       # tilt threshold

Z_DOMINANCE=3500                                     # ignore if flat on table


set -euo pipefail

command -v kscreen-doctor >/dev/null || { echo "kscreen missing"; exit 1; }


apply_rotation(){ kscreen-doctor "${OUTPUT_ID}.rotation.$1" >/dev/null 2>&1 || true; }

read_axis(){ cat "$IIO_DIR/in_accel_${1}_raw" 2>/dev/null || echo ""; }


decide_orientation(){

  local x=$1 y=$2 z=$3 ax=${x#-} ay=${y#-} az=${z#-}

  (( az>Z_DOMINANCE && az>ax && az>ay )) && { echo hold; return; }

  (( ax<THRESHOLD && ay<THRESHOLD )) && { echo hold; return; }

  if (( ax>ay )); then

    (( x>0 )) && echo left || echo right          # corrected X-axis mapping

  else

    (( y<0 )) && echo normal || echo inverted     # corrected Y-axis mapping

  fi

}


last_vote=""; vote_count=0; current_applied=""

while :; do

  [[ -f "$HOME/.config/autorotate.lock" ]] && { sleep $INTERVAL; continue; }

  [[ "${XDG_SESSION_TYPE:-}" != "wayland" ]] && { sleep $INTERVAL; continue; }


  X=$(read_axis x); Y=$(read_axis y); Z=$(read_axis z)

  [[ -z $X || -z $Y || -z $Z ]] && { sleep $INTERVAL; continue; }


  vote=$(decide_orientation "$X" "$Y" "$Z")

  [[ $vote == hold ]] && { ((vote_count>0)) && ((vote_count--)); sleep $INTERVAL; continue; }


  if [[ $vote == $last_vote ]]; then ((vote_count++)); else last_vote=$vote; vote_count=1; fi

  if (( vote_count>=STABILITY_COUNT )) && [[ $vote != $current_applied ]]; then

    apply_rotation "$vote"; current_applied=$vote; vote_count=$STABILITY_COUNT

  fi

  sleep $INTERVAL

done



MAKE EXECUTABLE

----------------

chmod +x ~/.local/bin/autorotate-wayland.sh



MANUAL TEST

------------

bash ~/.local/bin/autorotate-wayland.sh  

watch -n1 'kscreen-doctor -o | grep Rotation'  

# Stop with Ctrl + C



AUTOSTART

----------

System Settings → Startup & Shutdown → Autostart → Add Login Script…  

Choose ~/.local/bin/autorotate-wayland.sh  


Rotation lock commands:  

touch ~/.config/autorotate.lock   # freeze orientation  

rm ~/.config/autorotate.lock     # resume auto-rotation



BEHAVIOUR

----------

• Polls accelerometer every 1 s  

• Ignores noise when flat (Z dominant)  

• Applies rotation after 3 consistent readings  

• Runs silently in background – stop with pkill -f autorotate-wayland.sh



PORTABILITY NOTES

-----------------

Works on any device that:  

  1. Runs KDE Plasma on Wayland  

  2. Has a readable accelerometer under /sys/bus/iio/devices/iio:device*/in_accel_*_raw  

  3. Supports rotation via kscreen-doctor  


Adjust as needed:  

  • IIO_DIR → correct device index  

  • OUTPUT_ID → run kscreen-doctor -o to find yours  

  • Axis sign logic → flip x/y tests in decide_orientation() if portrait/landscape are reversed  


Confirmed working on:  

  • Lenovo ThinkPad X1 Tablet Gen 3  

  • X12 Detachable  

  • X1 Yoga Gen 4-6  

  • Other 2-in-1s (Dell XPS 13 2-in-1, HP Spectre x360) with minor axis tweaks  


Not applicable to:  

  • GNOME (uses Mutter’s DBus interface)  

  • X11 sessions (require xrandr/xinput version)



SUMMARY

--------

Lightweight, self-contained auto-rotation daemon for KDE Plasma on Wayland.  

No iio-sensor-proxy, no root privileges, no X11 dependencies.  

Polls raw accelerometer data → stabilises → rotates via kscreen-doctor.  

Safe to autostart, easy to lock, adaptable to any Wayland-based tablet.

 

Comments