Index: ui/events/x/device_data_manager.cc |
diff --git a/ui/events/x/device_data_manager.cc b/ui/events/x/device_data_manager.cc |
index cac9dfda0bdbb6f93a7adf451f54070c25041984..fceb6c0c1c59a1b54c9ac6ea1590c3045675c983 100644 |
--- a/ui/events/x/device_data_manager.cc |
+++ b/ui/events/x/device_data_manager.cc |
@@ -8,11 +8,20 @@ |
#include <X11/extensions/XInput2.h> |
#include <X11/Xlib.h> |
+#include "base/command_line.h" |
#include "base/logging.h" |
#include "base/memory/singleton.h" |
+#include "base/strings/string_number_conversions.h" |
+#include "base/strings/string_util.h" |
+#include "base/sys_info.h" |
#include "ui/events/event_constants.h" |
+#include "ui/events/event_switches.h" |
+#include "ui/events/platform/platform_event_observer.h" |
+#include "ui/events/platform/x11/x11_event_source.h" |
#include "ui/events/x/device_list_cache_x.h" |
#include "ui/events/x/touch_factory_x11.h" |
+#include "ui/gfx/display.h" |
+#include "ui/gfx/point3_f.h" |
#include "ui/gfx/x/x11_types.h" |
// XIScrollClass was introduced in XI 2.1 so we need to define it here |
@@ -97,6 +106,137 @@ const int kTouchDataTypeEnd = ui::DeviceDataManager::DT_TOUCH_RAW_TIMESTAMP; |
namespace ui { |
+// Accomplishes 2 tasks concerning touch event calibration: |
+// 1. Being a message-pump observer, |
+// routes all the touch events to the X root window, |
+// where they can be calibrated later. |
+// 2. Has the Calibrate method that does the actual bezel calibration, |
+// when invoked from X root window's event dispatcher. |
+class DeviceDataManager::TouchEventCalibrate |
+ : public ui::PlatformEventObserver { |
+ public: |
+ TouchEventCalibrate() : left_(0), right_(0), top_(0), bottom_(0) { |
+ if (ui::PlatformEventSource::GetInstance()) |
+ ui::PlatformEventSource::GetInstance()->AddPlatformEventObserver(this); |
+#if defined(USE_XI2_MT) |
+ std::vector<std::string> parts; |
+ if (Tokenize(CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
+ switches::kTouchCalibration), |
+ ",", |
+ &parts) >= 4) { |
+ if (!base::StringToInt(parts[0], &left_)) |
+ DLOG(ERROR) << "Incorrect left border calibration value passed."; |
+ if (!base::StringToInt(parts[1], &right_)) |
+ DLOG(ERROR) << "Incorrect right border calibration value passed."; |
+ if (!base::StringToInt(parts[2], &top_)) |
+ DLOG(ERROR) << "Incorrect top border calibration value passed."; |
+ if (!base::StringToInt(parts[3], &bottom_)) |
+ DLOG(ERROR) << "Incorrect bottom border calibration value passed."; |
+ } |
+#endif // defined(USE_XI2_MT) |
+ } |
+ |
+ virtual ~TouchEventCalibrate() { |
+ if (ui::PlatformEventSource::GetInstance()) |
+ ui::PlatformEventSource::GetInstance()->RemovePlatformEventObserver(this); |
+ } |
+ |
+ // Modify the location of the |event|, |
+ // expanding it from |bounds| to (|bounds| + bezels). |
+ // Required when touchscreen is bigger than screen (i.e. has bezels), |
+ // because we receive events in touchscreen coordinates, |
+ // which need to be expanded when converting to screen coordinates, |
+ // so that location on bezels will be outside of screen area. |
+ void Calibrate(ui::TouchEvent* event, const gfx::Rect& bounds) { |
+#if defined(USE_XI2_MT) |
+ int x = event->x(); |
+ int y = event->y(); |
+ |
+ if (!left_ && !right_ && !top_ && !bottom_) |
+ return; |
+ |
+ const int resolution_x = bounds.width(); |
+ const int resolution_y = bounds.height(); |
+ // The "grace area" (10% in this case) is to make it easier for the user to |
+ // navigate to the corner. |
+ const double kGraceAreaFraction = 0.1; |
+ if (left_ || right_) { |
+ // Offset the x position to the real |
+ x -= left_; |
+ // Check if we are in the grace area of the left side. |
+ // Note: We might not want to do this when the gesture is locked? |
+ if (x < 0 && x > -left_ * kGraceAreaFraction) |
+ x = 0; |
+ // Check if we are in the grace area of the right side. |
+ // Note: We might not want to do this when the gesture is locked? |
+ if (x > resolution_x - left_ && |
+ x < resolution_x - left_ + right_ * kGraceAreaFraction) |
+ x = resolution_x - left_; |
+ // Scale the screen area back to the full resolution of the screen. |
+ x = (x * resolution_x) / (resolution_x - (right_ + left_)); |
+ } |
+ if (top_ || bottom_) { |
+ // When there is a top bezel we add our border, |
+ y -= top_; |
+ |
+ // Check if we are in the grace area of the top side. |
+ // Note: We might not want to do this when the gesture is locked? |
+ if (y < 0 && y > -top_ * kGraceAreaFraction) |
+ y = 0; |
+ |
+ // Check if we are in the grace area of the bottom side. |
+ // Note: We might not want to do this when the gesture is locked? |
+ if (y > resolution_y - top_ && |
+ y < resolution_y - top_ + bottom_ * kGraceAreaFraction) |
+ y = resolution_y - top_; |
+ // Scale the screen area back to the full resolution of the screen. |
+ y = (y * resolution_y) / (resolution_y - (bottom_ + top_)); |
+ } |
+ |
+ // Set the modified coordinate back to the event. |
+ if (event->root_location() == event->location()) { |
+ // Usually those will be equal, |
+ // if not, I am not sure what the correct value should be. |
+ event->set_root_location(gfx::Point(x, y)); |
+ } |
+ event->set_location(gfx::Point(x, y)); |
+#endif // defined(USE_XI2_MT) |
+ } |
+ |
+ private: |
+ // ui::PlatformEventObserver: |
+ virtual void WillProcessEvent(const ui::PlatformEvent& event) OVERRIDE { |
+#if defined(USE_XI2_MT) |
+ if (event->type == GenericEvent && |
+ (event->xgeneric.evtype == XI_TouchBegin || |
+ event->xgeneric.evtype == XI_TouchUpdate || |
+ event->xgeneric.evtype == XI_TouchEnd)) { |
+ XIDeviceEvent* xievent = static_cast<XIDeviceEvent*>(event->xcookie.data); |
+ LOG(ERROR) << "Raw " << xievent->event_x << " " << xievent->event_y |
+ << " " << xievent->root_x << " " << xievent->root_y; |
+ xievent->event = xievent->root; |
+ xievent->event_x = xievent->root_x; |
+ xievent->event_y = xievent->root_y; |
+ } |
+#endif // defined(USE_XI2_MT) |
+ } |
+ |
+ virtual void DidProcessEvent(const ui::PlatformEvent& event) OVERRIDE {} |
+ |
+ // The difference in screen's native resolution pixels between |
+ // the border of the touchscreen and the border of the screen, |
+ // aka bezel sizes. |
+ int left_; |
+ int right_; |
+ int top_; |
+ int bottom_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(TouchEventCalibrate); |
+}; |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+// DeviceDataManager |
+ |
bool DeviceDataManager::IsCMTDataType(const int type) { |
return (type >= kCMTDataTypeStart) && (type <= kCMTDataTypeEnd); |
} |
@@ -112,7 +252,8 @@ DeviceDataManager* DeviceDataManager::GetInstance() { |
DeviceDataManager::DeviceDataManager() |
: xi_opcode_(-1), |
atom_cache_(gfx::GetXDisplay(), kCachedAtoms), |
- button_map_count_(0) { |
+ button_map_count_(0), |
+ touch_calibrate_(new TouchEventCalibrate) { |
CHECK(gfx::GetXDisplay()); |
InitializeXInputInternal(); |
@@ -120,6 +261,8 @@ DeviceDataManager::DeviceDataManager() |
CHECK(arraysize(kCachedAtoms) == static_cast<size_t>(DT_LAST_ENTRY) + 1); |
UpdateDeviceList(gfx::GetXDisplay()); |
UpdateButtonMap(); |
+ for (int i = 0; i < kMaxDeviceNum; i++) |
+ touch_device_to_display_map_[i] = gfx::Display::kInvalidDisplayID; |
} |
DeviceDataManager::~DeviceDataManager() { |
@@ -634,4 +777,55 @@ void DeviceDataManager::InitializeValuatorsForTest(int deviceid, |
} |
} |
+void DeviceDataManager::CalibrateTouchEvent(TouchEvent* event, |
+ int touch_device_id, |
+ const gfx::Rect& bounds) { |
+#if defined(OS_CHROMEOS) && defined(USE_XI2_MT) |
+ int64 touch_display_id = GetDisplayForTouchDevice(touch_device_id); |
+ if (base::SysInfo::IsRunningOnChromeOS() && |
+ touch_display_id == gfx::Display::InternalDisplayId()) { |
+ touch_calibrate_->Calibrate(event, bounds); |
+ } |
+#endif // defined(OS_CHROMEOS) && defined(USE_XI2_MT) |
+} |
+ |
+void DeviceDataManager::ClearTouchTransformerRecord() { |
+ for (int i = 0; i < kMaxDeviceNum; i++) { |
+ touch_device_transformer_map_[i] = gfx::Transform(); |
+ touch_device_to_display_map_[i] = gfx::Display::kInvalidDisplayID; |
+ } |
+} |
+ |
+bool DeviceDataManager::IsTouchDeviceIdValid(int touch_device_id) const { |
+ return (touch_device_id > 0 && touch_device_id < kMaxDeviceNum); |
+} |
+ |
+void DeviceDataManager::UpdateTouchInfoForDisplay( |
+ int64 display_id, |
+ int touch_device_id, |
+ const gfx::Transform& touch_transformer) { |
+ if (IsTouchDeviceIdValid(touch_device_id)) { |
+ touch_device_to_display_map_[touch_device_id] = display_id; |
+ touch_device_transformer_map_[touch_device_id] = touch_transformer; |
+ } |
+} |
+ |
+void DeviceDataManager::ApplyTouchTransformer(int touch_device_id, |
+ float* x, float* y) { |
+ if (IsTouchDeviceIdValid(touch_device_id)) { |
+ gfx::Point3F point(*x, *y, 0.0); |
+ const gfx::Transform& trans = |
+ touch_device_transformer_map_[touch_device_id]; |
+ trans.TransformPoint(&point); |
+ *x = point.x(); |
+ *y = point.y(); |
+ } |
+} |
+ |
+int64 DeviceDataManager::GetDisplayForTouchDevice(int touch_device_id) const { |
+ if (IsTouchDeviceIdValid(touch_device_id)) |
+ return touch_device_to_display_map_[touch_device_id]; |
+ return gfx::Display::kInvalidDisplayID; |
+} |
+ |
} // namespace ui |