136 lines
4.7 KiB
Diff
136 lines
4.7 KiB
Diff
|
diff -Nru surface-tools-20180914git.orig/autorotate/autorotate surface-tools-20180914git/autorotate/autorotate
|
||
|
--- surface-tools-20180914git.orig/autorotate/autorotate 2019-08-03 21:13:46.720000000 +0200
|
||
|
+++ surface-tools-20180914git/autorotate/autorotate 2019-08-03 21:14:01.072000000 +0200
|
||
|
@@ -3,12 +3,14 @@
|
||
|
#####CONFIGURATION#####
|
||
|
#sensorname="accel_3d"
|
||
|
|
||
|
-#screenname="eDP1"
|
||
|
+screenname = None
|
||
|
+touchname = None
|
||
|
|
||
|
#####PROGRAM CODE#####
|
||
|
#Do not change unless you know what you are doing!
|
||
|
|
||
|
xrandr="/usr/bin/xrandr"
|
||
|
+xinput="/usr/bin/xinput"
|
||
|
|
||
|
resourcepaths=[
|
||
|
"./",
|
||
|
@@ -36,6 +38,7 @@
|
||
|
|
||
|
hasAppIndicator = True
|
||
|
try:
|
||
|
+ gi.require_version('AppIndicator', '0.1')
|
||
|
from gi.repository import AppIndicator3 as AppIndicator
|
||
|
except:
|
||
|
hasAppIndicator = False
|
||
|
@@ -46,9 +49,15 @@
|
||
|
out = p.stdout.read().strip()
|
||
|
return out #This is the stdout from the shell command
|
||
|
|
||
|
-def twos_comp(val):
|
||
|
- if val & (1 << (16-1)) != 0:
|
||
|
- val = val - (1 << 16)
|
||
|
+def twos_comp(val, scale):
|
||
|
+ if scale:
|
||
|
+ # Chuwi
|
||
|
+ val = val * scale / 9.8
|
||
|
+ else:
|
||
|
+ # Microsoft Surface (unsupported)
|
||
|
+ print 1 << 16
|
||
|
+ if val & (1 << (16-1)) != 0:
|
||
|
+ val = val - (1 << 16)
|
||
|
return val
|
||
|
|
||
|
def getOrientation(accelX, accelY, accelZ):
|
||
|
@@ -56,18 +65,32 @@
|
||
|
absAccelY = abs(accelY)
|
||
|
absAccelZ = abs(accelZ)
|
||
|
|
||
|
- if absAccelZ > 4 * absAccelX and absAccelZ > 4 * absAccelY:
|
||
|
- orientation = "flat"
|
||
|
- elif 3 * absAccelY > 2 * absAccelX:
|
||
|
- orientation = "inverted" if accelY > 0 else "normal"
|
||
|
- else:
|
||
|
- orientation = "left" if accelX > 0 else "right"
|
||
|
+ orientation = "nochange"
|
||
|
+ if absAccelZ > 0.5:
|
||
|
+ orientation = "nochange"
|
||
|
+ elif absAccelX > 3 * absAccelY:
|
||
|
+ orientation = "normal" if accelX > 0 else "inverted"
|
||
|
+ elif absAccelY > 3 * absAccelX:
|
||
|
+ orientation = "right" if accelY > 0 else "left"
|
||
|
|
||
|
return orientation
|
||
|
|
||
|
def rotate(orientation):
|
||
|
- if orientation in orientations:
|
||
|
+ if orientation in orientations and screenname is not None:
|
||
|
subprocess.call([xrandr, "--output", screenname, "--rotate", orientation])
|
||
|
+ if touchname is not None:
|
||
|
+ if orientation == "normal":
|
||
|
+ subprocess.call([xinput, "set-prop", "pointer:" + touchname, "Coordinate Transformation Matrix",
|
||
|
+ "0", "0", "0", "0", "0", "0", "0", "0", "0"])
|
||
|
+ elif orientation == "inverted":
|
||
|
+ subprocess.call([xinput, "set-prop", "pointer:" + touchname, "Coordinate Transformation Matrix",
|
||
|
+ "-1", "0", "1", "0", "-1", "1", "0", "0", "1"])
|
||
|
+ elif orientation == "right":
|
||
|
+ subprocess.call([xinput, "set-prop", "pointer:" + touchname, "Coordinate Transformation Matrix",
|
||
|
+ "0", "1", "0", "-1", "0", "1", "0", "0", "1"])
|
||
|
+ elif orientation == "left":
|
||
|
+ subprocess.call([xinput, "set-prop", "pointer:" + touchname, "Coordinate Transformation Matrix",
|
||
|
+ "0", "-1", "1", "1", "0", "0", "0", "0", "1"])
|
||
|
|
||
|
def toggleRotLock(event):
|
||
|
global rotlock
|
||
|
@@ -84,14 +107,14 @@
|
||
|
icon.set_from_pixbuf(lockrot if rotlock else unlockrot)
|
||
|
|
||
|
def checkRotation():
|
||
|
- if not rotlock:
|
||
|
- device = pyudev.Device.from_path(context, path)
|
||
|
- accelX = twos_comp(device.attributes.asint("in_accel_x_raw"))
|
||
|
- accelY = twos_comp(device.attributes.asint("in_accel_y_raw"))
|
||
|
- accelZ = twos_comp(device.attributes.asint("in_accel_z_raw"))
|
||
|
-
|
||
|
- orientation = getOrientation(accelX, accelY, accelZ)
|
||
|
+ device = pyudev.Device.from_path(context, path)
|
||
|
+ scale = float(device.attributes.__getitem__("in_accel_scale"))
|
||
|
+ accelX = twos_comp(device.attributes.asint("in_accel_x_raw"), scale)
|
||
|
+ accelY = twos_comp(device.attributes.asint("in_accel_y_raw"), scale)
|
||
|
+ accelZ = twos_comp(device.attributes.asint("in_accel_z_raw"), scale)
|
||
|
+ orientation = getOrientation(accelX, accelY, accelZ)
|
||
|
|
||
|
+ if not rotlock and orientation != "nochange":
|
||
|
global prevorientation
|
||
|
if orientation != prevorientation:
|
||
|
prevorientation = orientation
|
||
|
@@ -121,6 +144,20 @@
|
||
|
screenname = out
|
||
|
break
|
||
|
|
||
|
+# Cache the xinput info
|
||
|
+XINPUT_INFO = run_shell_cmd('xinput --list --name-only')
|
||
|
+
|
||
|
+# Find the display we are interested to move (= the laptop panel)
|
||
|
+POSSIBLE_OUTPUT = [ "CHPN0001:00" ]
|
||
|
+for out in POSSIBLE_OUTPUT:
|
||
|
+ match = re.search("^" + out + "$",
|
||
|
+ XINPUT_INFO,
|
||
|
+ re.MULTILINE
|
||
|
+ )
|
||
|
+ if match:
|
||
|
+ touchname = out
|
||
|
+ break
|
||
|
+
|
||
|
path = device.device_path
|
||
|
|
||
|
prevorientation = ""
|
||
|
@@ -169,5 +206,3 @@
|
||
|
Gtk.main()
|
||
|
else:
|
||
|
checkRotation()
|
||
|
-
|
||
|
-
|