recovery: calibrate touchscreen

Change-Id: I5a32a59691dea5fa2aa867e6594cec15b1b24ccf
diff --git a/recovery_ui/include/recovery_ui/ui.h b/recovery_ui/include/recovery_ui/ui.h
index f1a6a8e..38a7427 100644
--- a/recovery_ui/include/recovery_ui/ui.h
+++ b/recovery_ui/include/recovery_ui/ui.h
@@ -312,6 +312,7 @@
 
   void OnTouchDeviceDetected(int fd);
   void OnKeyDetected(int key_code);
+  void CalibrateTouch(int fd);
   void OnTouchPress();
   void OnTouchTrack();
   void OnTouchRelease();
@@ -366,6 +367,8 @@
   Point touch_pos_;
   Point touch_start_;
   Point touch_track_;
+  Point touch_max_;
+  Point touch_min_;
   std::vector<vkey_t> virtual_keys_;
   bool is_bootreason_recovery_ui_;
 
diff --git a/recovery_ui/ui.cpp b/recovery_ui/ui.cpp
index a8e1374..6fc2dde 100644
--- a/recovery_ui/ui.cpp
+++ b/recovery_ui/ui.cpp
@@ -239,6 +239,26 @@
   return true;
 }
 
+void RecoveryUI::CalibrateTouch(int fd) {
+  struct input_absinfo info;
+  static bool calibrated = false;
+
+  if (calibrated) return;
+
+  memset(&info, 0, sizeof(info));
+  if (ioctl(fd, EVIOCGABS(ABS_MT_POSITION_X), &info) == 0) {
+    touch_min_.x(info.minimum);
+    touch_max_.x(info.maximum);
+  }
+  memset(&info, 0, sizeof(info));
+  if (ioctl(fd, EVIOCGABS(ABS_MT_POSITION_Y), &info) == 0) {
+    touch_min_.y(info.minimum);
+    touch_max_.y(info.maximum);
+  }
+
+  calibrated = true;
+}
+
 void RecoveryUI::OnTouchPress() {
   touch_start_ = touch_track_ = touch_pos_;
 }
@@ -345,6 +365,7 @@
   }
 
   if (touch_screen_allowed_ && ev.type == EV_ABS) {
+    CalibrateTouch(fd);
     if (ev.code == ABS_MT_SLOT) {
       touch_slot_ = ev.value;
     }
@@ -355,7 +376,7 @@
       case ABS_MT_POSITION_X:
         touch_finger_down_ = true;
         touch_saw_x_ = true;
-        touch_pos_.x(ev.value);
+        touch_pos_.x(ev.value * gr_fb_width() / (touch_max_.x() - touch_min_.x()));
         if (touch_reported_ && touch_saw_y_) {
           OnTouchTrack();
           touch_saw_x_ = touch_saw_y_ = false;
@@ -365,7 +386,7 @@
       case ABS_MT_POSITION_Y:
         touch_finger_down_ = true;
         touch_saw_y_ = true;
-        touch_pos_.y(ev.value);
+        touch_pos_.y(ev.value * gr_fb_height() / (touch_max_.y() - touch_min_.y()));
         if (touch_reported_ && touch_saw_x_) {
           OnTouchTrack();
           touch_saw_x_ = touch_saw_y_ = false;