Add "Touchscreen" app to properly setup touchscreen calibration
diff --git a/Touchscreen/Android.mk b/Touchscreen/Android.mk
new file mode 100644
index 0000000..3966b30
--- /dev/null
+++ b/Touchscreen/Android.mk
@@ -0,0 +1,14 @@
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE_TAGS := optional
+LOCAL_SRC_FILES := $(call all-subdir-java-files)
+
+LOCAL_CERTIFICATE := platform
+LOCAL_PRIVILEGED_MODULE := true
+LOCAL_PACKAGE_NAME := Touchscreen
+LOCAL_PROGUARD_ENABLED := disabled
+
+LOCAL_DEX_PREOPT := false
+
+include $(BUILD_PACKAGE)
diff --git a/Touchscreen/AndroidManifest.xml b/Touchscreen/AndroidManifest.xml
new file mode 100644
index 0000000..50e3b92
--- /dev/null
+++ b/Touchscreen/AndroidManifest.xml
@@ -0,0 +1,16 @@
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+	package="me.phh.treble.touchscreen"
+	android:sharedUserId="android.uid.system">
+
+	<uses-permission android:name="android.permission.SET_INPUT_CALIBRATION" />
+    <application
+	    android:label="@string/app_label">
+
+	    <receiver android:name=".Starter">
+		    <intent-filter>
+			    <action android:name="android.intent.action.BOOT_COMPLETED"/>
+		    </intent-filter>
+	    </receiver>
+    </application>
+
+</manifest>
diff --git a/Touchscreen/res/values/strings.xml b/Touchscreen/res/values/strings.xml
new file mode 100644
index 0000000..1bd9b00
--- /dev/null
+++ b/Touchscreen/res/values/strings.xml
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+    <string name="app_label">Hardware Overlay Picker</string>
+</resources>
diff --git a/Touchscreen/src/me/phh/treble/touchscreen/Starter.java b/Touchscreen/src/me/phh/treble/touchscreen/Starter.java
new file mode 100644
index 0000000..bc5fcf4
--- /dev/null
+++ b/Touchscreen/src/me/phh/treble/touchscreen/Starter.java
@@ -0,0 +1,91 @@
+package me.phh.treble.touchscreen;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.om.IOverlayManager;
+import android.hardware.input.InputManager;
+import android.view.InputDevice;
+import android.view.MotionEvent;
+import android.hardware.input.TouchCalibration;
+import android.os.RemoteException;
+import android.os.ServiceManager;
+import android.os.SystemProperties;
+import android.util.Log;
+
+public class Starter extends BroadcastReceiver {
+    private final static String TAG = "TouchScreen";
+
+    private int getRotation(Context ctxt, Intent intent) {
+        int rotation = -1;
+        String vndFingerprint = SystemProperties.get("ro.vendor.build.fingerprint", "");
+        int hwrotation = SystemProperties.getInt("ro.sf.hwrotation", -1);
+        if(vndFingerprint.contains("full_x970_t10") && hwrotation == 270) {
+            rotation = 271;
+        }
+        if(intent.hasExtra("rotation")) {
+            rotation = intent.getIntExtra("rotation", rotation);
+        }
+        return rotation;
+    }
+
+    @Override
+    public void onReceive(Context ctxt, Intent intent) {
+        int rotation = getRotation(ctxt, intent);
+        if(rotation == -1) return;
+        android.util.Log.d("PHH", "Applying rotation " + rotation);
+        InputManager im = ctxt.getSystemService(InputManager.class);
+        int[] ids = im.getInputDeviceIds();
+        for(int id: ids) {
+            InputDevice dev = im.getInputDevice(id);
+            String descriptor = dev.getDescriptor();
+            android.util.Log.d("PHH", "Checking device " + descriptor + ":" + dev.getSources());
+            if( (dev.getSources() & InputDevice.SOURCE_TOUCHSCREEN) == InputDevice.SOURCE_TOUCHSCREEN ) {
+                InputDevice.MotionRange rangeX = dev.getMotionRange(MotionEvent.AXIS_X);
+                InputDevice.MotionRange rangeY = dev.getMotionRange(MotionEvent.AXIS_Y);
+
+                float dX = rangeX.getMax();
+                float dY = rangeY.getMax();
+                //TouchCalibration cal = new TouchCalibration(
+                //		0.0f, dX/dY, 0.0f,
+                //		dY/dX, 0.0f, 0.0f);
+                TouchCalibration cal;
+                if(rotation == 0) {
+                    cal = new TouchCalibration(
+                            1.0f, 0.0f, 0.0f,
+                            0.0f, 1.0f, 0.0f);
+                } else if(rotation == 90) {
+                    cal = new TouchCalibration(
+                            0.0f, dX/dY, 0.0f,
+                            dY/dX, 0.0f, 0.0f);
+                } else if(rotation == 91) {
+                    cal = new TouchCalibration(
+                            0.0f, -dX/dY, dX,
+                            dY/dX, 0.0f, 0.0f);
+                } else if(rotation == 180) {
+                    cal = new TouchCalibration(
+                            -1.0f, 0.0f, dX,
+                            0.0f, -1.0f, dY);
+                } else if(rotation == 270) {
+                    cal = new TouchCalibration(
+                            0.0f, -dX/dY, dX,
+                            -dY/dX, 0.0f, dY);
+                } else if(rotation == 271) {
+                    cal = new TouchCalibration(
+                            0.0f, dX/dY, 0.0f,
+                            -dY/dX, 0.0f, dY);
+                } else {
+                    cal = new TouchCalibration(
+                            1.0f, 0.0f, 0.0f,
+                            0.0f, 1.0f, 0.0f);
+                }
+
+                android.util.Log.d("PHH", "Setting touch calibration " + dY/dX  + ":" + dX/dY);
+                im.setTouchCalibration(descriptor, 0, cal);
+                im.setTouchCalibration(descriptor, 1, cal);
+                im.setTouchCalibration(descriptor, 2, cal);
+                im.setTouchCalibration(descriptor, 3, cal);
+            }
+        }
+    }
+}
diff --git a/overlay.mk b/overlay.mk
index 38f1eec..948962d 100644
--- a/overlay.mk
+++ b/overlay.mk
@@ -5,7 +5,8 @@
 	treble-overlay-Telephony-LTE \
 	treble-overlay-SystemUI-FalseLocks \
 	HardwareOverlayPicker \
-	QtiAudio
+	QtiAudio \
+	Touchscreen
 
 
 PRODUCT_PACKAGES += \