recovery: calibrate touchscreen

Change-Id: I5a32a59691dea5fa2aa867e6594cec15b1b24ccf
diff --git a/recovery_ui/ui.cpp b/recovery_ui/ui.cpp
index 45d747b..b8b4f6c 100644
--- a/recovery_ui/ui.cpp
+++ b/recovery_ui/ui.cpp
@@ -227,6 +227,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_;
 }
@@ -342,6 +362,7 @@
   }
 
   if (touch_screen_allowed_ && ev.type == EV_ABS) {
+    CalibrateTouch(fd);
     if (ev.code == ABS_MT_SLOT) {
       touch_slot_ = ev.value;
     }
@@ -352,7 +373,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;
@@ -362,7 +383,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;