Skip to content

Commit

Permalink
hyperpixel4-rotate save to xorg conf
Browse files Browse the repository at this point in the history
Add the ability to save touch calibration and screen rotation to a file in xorg.conf.d.

This allows hyperpixel4 rotation to persist in cases where the "Screen Configuraton" utility (arandr) is not present, but the system uses an X desktop environment.
  • Loading branch information
Gadgetoid committed Jun 22, 2021
1 parent 327fd87 commit 236fb21
Showing 1 changed file with 113 additions and 25 deletions.
138 changes: 113 additions & 25 deletions dist/hyperpixel4-rotate
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,21 +1,88 @@
#!/bin/bash

ORIENTATION=$1
DO_XORG=$2
DEVICE="pointer:Goodix Capacitive TouchScreen"
UDEV_FILE="/etc/udev/rules.d/98-hyperpixel4-calibration.rules"
DISP_FILE="/usr/share/dispsetup.sh"
XORG_CONF_FILE="/usr/share/X11/xorg.conf.d/88-hyperpixel4.conf"
LIGHTDM_CONF_FILE="/etc/lightdm/lightdm.conf"

function success() {
echo -e "$(tput setaf 2)$1$(tput sgr0)"
}

function inform() {
echo -e "$(tput setaf 6)$1$(tput sgr0)"
}

function warning() {
echo -e "$(tput setaf 1)$1$(tput sgr0)"
}

function compatibility_check {
grep -e "^dtoverlay=vc4-fkms-v3d.*" /boot/config.txt > /dev/null
if [ $? -ne 0 ]; then
warning "Rotation requires hardware support on the Pi 4 or Pi 400"
warning "Ensure \"dtoverlay=vc4-fkms-v3d\" is enabled in /boot/config.txt"
exit 1
fi
}

function set_xorg_conf {
inform "Rotating display $1";
xrandr --output DSI-1 --rotate $1

inform "Setting libinput Calibration Matrix: 1 0 0 0 1 0";
xinput set-prop "pointer:$DEVICE" "libinput Calibration Matrix" 1 0 0 0 1 0 0 0 1

inform "Setting Coordinate Transformation Matrix: $2 $3 $4 $5 $6 $7";
xinput set-prop "pointer:$DEVICE" "Coordinate Transformation Matrix" $2 $3 $4 $5 $6 $7 0 0 1

inform "Saving xorg config to $XORG_CONF_FILE";
sudo tee $XORG_CONF_FILE > /dev/null <<EOF
Section "InputClass"
Identifier "HyperPixel4 ($DEVICE)"
MatchProduct "$DEVICE"
Option "CalibrationMatrix" "1 0 0 0 1 0 0 0 1"
Option "TransformationMatrix" "$2 $3 $4 $5 $6 $7 0 0 1"
EndSection
Section "Monitor"
Identifier "DSI-1"
Option "Rotate" "$1"
EndSection
EOF

grep -e "^display-setup-script=$DISP_FILE*" $LIGHTDM_CONF_FILE > /dev/null
if [ $? -eq 0 ]; then
warning "You might want to comment-out \"display-setup-script\" from $LIGHTDM_CONF_FILE\n"
fi
}

function set_matrix {
printf "Setting matrix: $1 $2 $3 $4 $5 $6\n";
xinput set-prop "$DEVICE" "libinput Calibration Matrix" $1 $2 $3 $4 $5 $6 0 0 1

printf "Saving touch settings to $UDEV_FILE\n";
echo "ATTRS{name}==\"Goodix Capacitive TouchScreen\", ENV{LIBINPUT_CALIBRATION_MATRIX}=\"$1 $2 $3 $4 $5 $6\"" | sudo tee $UDEV_FILE > /dev/null
inform "Setting touch transform matrix: $1 $2 $3 $4 $5 $6";
xinput set-prop "pointer:$DEVICE" "libinput Calibration Matrix" $1 $2 $3 $4 $5 $6 0 0 1

inform "Saving touch settings to $UDEV_FILE";
echo "ATTRS{name}==\"$DEVICE\", ENV{LIBINPUT_CALIBRATION_MATRIX}=\"$1 $2 $3 $4 $5 $6\"" | sudo tee $UDEV_FILE > /dev/null
echo "ATTRS{name}==\"EP0110M09\", ENV{LIBINPUT_CALIBRATION_MATRIX}=\"$1 $2 $3 $4 $5 $6\"" | sudo tee -a $UDEV_FILE > /dev/null
}

function set_display {
printf "Rotating display\n";
sudo python2 - <<EOF
inform "Rotating display $1";
xrandr --output DSI-1 --rotate $1

grep -e "^display-setup-script=$DISP_FILE*" $LIGHTDM_CONF_FILE > /dev/null
if [ $? -ne 0 ]; then
warning "The display setup script is not configured in $LIGHTDM_CONF_FILE"
warning "Maybe you're not using Raspberry Pi OS."
warning "Add \"display-setup-script=$DISP_FILE\" to $LIGHTDM_CONF_FILE or use the Xorg config method (add --xorg to this command)\n"
fi

inform "Persisting display settings to $DISP_FILE";
sudo chown `whoami` $DISP_FILE
python2 - <<EOF
import os
import stat
from screenlayout import xrandr
Expand All @@ -33,48 +100,69 @@ template = ["#!/bin/sh",
x = xrandr.XRandR()
x.load_from_x()
data = x.save_to_shellscript_string(template=template)
print("Saving display settings to {}".format(file))
open(file, 'w').write(data)
os.chmod(file, stat.S_IRWXU)
EOF
if [ $? -eq 0 ]; then
xrandr --output DSI-1 --rotate $1
return 0
STATUS=$?
sudo chown root $DISP_FILE
sudo chmod 755 $DISP_FILE
if [ $STATUS -eq 1 ]; then
warning "Failed to persist display orientation."
warning "Make sure you're running Rasperry Pi OS desktop on a Pi 4 or Pi 400"
fi

printf "Failed to set display orientation. Make sure you're running Raspberry Pi OS desktop on a Pi 4.\n"
exit 0
if [ -f $XORG_CONF_FILE ]; then
warning "You might want to remove $XORG_CONF_FILE"
fi
}

printf "This rotate utility only works with the Raspberry Pi OS desktop version.\n"
printf "HyperPixel 4: Square - Display/Touch Rotation\n"
warning "This rotate utility only works with the Raspberry Pi OS desktop version."

compatibility_check

if [ "$DISPLAY" == "" ]; then
printf "You need to set a DISPLAY variable.\n";
printf "Try: DISPLAY=:0.0 hyperpixel4-rotate <orientation>\n"
exit 0
warning "No DISPLAY variable set, trying :0.0"
export DISPLAY=:0.0
fi

if [ "$ORIENTATION" == "right" ]; then
set_display $ORIENTATION
set_matrix 0 1 0 -1 0 1
if [ "$DO_XORG" == "--xorg" ]; then
set_xorg_conf $ORIENTATION 0 1 0 -1 0 1
else
set_display $ORIENTATION
set_matrix 0 1 0 -1 0 1
fi
exit 0
fi

if [ "$ORIENTATION" == "left" ]; then
set_display $ORIENTATION
set_matrix 0 -1 1 1 0 0
if [ "$DO_XORG" == "--xorg" ]; then
set_xorg_conf $ORIENTATION 0 -1 1 1 0 0
else
set_display $ORIENTATION
set_matrix 0 -1 1 1 0 0
fi
exit 0
fi

if [ "$ORIENTATION" == "inverted" ]; then
set_display $ORIENTATION
set_matrix -1 0 1 0 -1 1
if [ "$DO_XORG" == "--xorg" ]; then
set_xorg_conf $ORIENTATION -1 0 1 0 -1 1
else
set_display $ORIENTATION
set_matrix -1 0 1 0 -1 1
fi
exit 0
fi

if [ "$ORIENTATION" == "normal" ]; then
set_display $ORIENTATION
set_matrix 1 0 0 0 1 0
if [ "$DO_XORG" == "--xorg" ]; then
set_xorg_conf $ORIENTATION 1 0 0 0 1 0
else
set_display $ORIENTATION
set_matrix 1 0 0 0 1 0
fi
exit 0
fi

Expand Down

0 comments on commit 236fb21

Please sign in to comment.