Merge pull request #423 from sphh/rotate-script

Initial commit of contrib/rotate-screen/ directory.
This commit is contained in:
Maximilian Luz 2021-04-20 23:09:23 +02:00 committed by GitHub
commit 29a897878b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 183 additions and 0 deletions

View file

@ -0,0 +1,41 @@
# One-shot automatic screen rotation
There are many scripts around (you can find some [here](https://gist.github.com/mildmojo/48e9025070a2ba40795c, where this script was published first)), which rotate the screen automatically. Many desktop environment have this feature built in, but if you like a one-shot automatic rotation facility, this is for you.
## Motivation
Automatic rotation can be useful, but sometimes you just want to set the screen to an orientation and keep it at that (think: reading in bed). Many desktop environments provide facilities to either continuously adapt the screen to the device's orientation or to select the orientation manually (if it works, e.g. Cinnamon is not able to adapt *all* input devices to the new orientation). Using the manual option you might get confused with the naming of the orientations …
The script provided here takes a different approach: You hold your device in the orientation you would like and call this script. It figures out the current orientation and rotates the screen and *all* input devices accordingly.
## Installation
Copy the script to a place on your computer and make it executable (`chmod +x rotate-screen`).
Since the Typecover keyboard gets disabled, whenever the Surface is rotated, it is advisable to put the script "on your desktop", so that it is just a click away.
Finally, don't forget to switch off automatic screen rotation!
### Cinnamon
For the Cinnamon desktop, one option is to use the [Command Launcher](https://cinnamon-spices.linuxmint.com/applets/view/139) add-on. Install it, add an instance to your panel and set it up as follows:
```
Tooltip: Set screen rotation
Keyboard shortcut: unassigned | unassigned
Show notification on completion: NO
Command: rotate-screen
Run as root: NO
Run in alternate directory: NO
```
You might have to give the full path to the `rotate-screen` script. A possible icon is `rotation-allowed-symbolic` (or use your own).
### Other desktop environments
If you have a good way, how to call this script from other desktop environments, please make a PR, so that others can benefit from your insight. Thanks.
## Invocation
Hold your Surface in the orientation you like and call the script, e.g. by clicking on the icon installed in the previous step.

View file

@ -0,0 +1,142 @@
#!/bin/sh
#
# One-shot automatic screen rotation
#
# If the attached keyboard is switched off, whenever the tablet is
# rotated, it is best to put this command into the panel, because
# hotkeys do not work.
#
# On Cinnamon, the 'Command Launcher' add-on can be used with the
# following settings:
# Panel Icon: rotation-allowed-symbolic
# Tooltip: Set screen rotation
# Keyboard shortcut: unassigned | unassigned
# Show notification on completion: NO
# Command: rotate-screen
# Run as root: NO
# Run in alternate directory: NO
#
# Anchestors:
# mildmojo
# https://gist.github.com/mildmojo/48e9025070a2ba40795c
# mbinnun:
# https://gist.github.com/mildmojo/48e9025070a2ba40795c#gistcomment-2694429
#
# Check for commands needed
GDBUS=`which gdbus`
if test -z $GDBUS; then
echo "Command 'gdbus' not found."
exit 1
fi
XINPUT=`which xinput`
if test -z $XINPUT; then
echo "Command 'xinput' not found."
exit 2
fi
XRANDR=`which xrandr`
if test -z $GDBUS; then
echo "Command 'xrandr' not found."
fi
get_orientation () {
# Get the orientation from the DBus
#
# No options.
# DBus to query to get the current orientation
DBUS="--system --dest net.hadess.SensorProxy --object-path /net/hadess/SensorProxy"
# Check, if DBus is available
ORIENTATION=`$GDBUS call $DBUS \
--method org.freedesktop.DBus.Properties.Get \
net.hadess.SensorProxy HasAccelerometer`
if test "$ORIENTATION" != "(<true>,)"; then
echo "No sensor available!"
exit 10
fi
# Get the orientation from the DBus
ORIENTATION=`$GDBUS call $DBUS \
--method org.freedesktop.DBus.Properties.Get \
net.hadess.SensorProxy AccelerometerOrientation`
# Release the DBus
$GDBUS call --system $DBUS --method net.hadess.SensorProxy.ReleaseAccelerometer > /dev/null
# Normalize the orientation
case $ORIENTATION in
"(<'normal'>,)")
ORIENTATION=normal
;;
"(<'bottom-up'>,)")
ORIENTATION=inverted
;;
"(<'left-up'>,)")
ORIENTATION=left
;;
"(<'right-up'>,)")
ORIENTATION=right
;;
*)
echo "Orientation $ORIENTATION unknown!"
exit 11
esac
# Return the orientation found
echo $ORIENTATION
}
do_rotate () {
# Rotate screen and pointers
#
# $1: The new orientation
# $2: The screen to rotate
# $3-: The pointers to rotate
TRANSFORM='Coordinate Transformation Matrix'
ORIENTATION=$1
shift
# Rotate the screen
$XRANDR --output $1 --rotate $ORIENTATION
shift
# Rotate all pointers
while test $# -gt 0; do
case $ORIENTATION in
normal)
$XINPUT set-prop $1 "$TRANSFORM" 1 0 0 0 1 0 0 0 1
;;
inverted)
$XINPUT set-prop $1 "$TRANSFORM" -1 0 1 0 -1 1 0 0 1
;;
left)
$XINPUT set-prop $1 "$TRANSFORM" 0 -1 1 1 0 0 0 0 1
;;
right)
$XINPUT set-prop $1 "$TRANSFORM" 0 1 0 -1 0 1 0 0 1
;;
esac
shift
done
}
# Get the tablet's orientation
ORIENTATION=`get_orientation`
# Get all pointers
POINTERS=`$XINPUT | grep slave | grep pointer | sed -e 's/^.*id=\([[:digit:]]\+\).*$/\1/'`
# Get the display and its orientation
XDISPLAY=`$XRANDR --current --verbose | grep primary | cut --delimiter=" " -f1`
# Rotate the screen and pointers
echo "Rotate display $XDISPLAY to $ORIENTATION orientation (Pointers: `echo $POINTERS | sed 's/\n/ /g'`)"
do_rotate $ORIENTATION $XDISPLAY $POINTERS