1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chromeos/display/touchscreen_delegate_x11.h"
7 #include <X11/extensions/XInput.h>
8 #include <X11/extensions/XInput2.h>
13 #include "base/message_loop/message_pump_x11.h"
17 TouchscreenDelegateX11::TouchscreenDelegateX11()
18 : display_(base::MessagePumpX11::GetDefaultXDisplay()) {}
20 TouchscreenDelegateX11::~TouchscreenDelegateX11() {}
22 void TouchscreenDelegateX11::AssociateTouchscreens(
23 std::vector<OutputConfigurator::OutputSnapshot>* outputs) {
25 Atom valuator_x = XInternAtom(display_, "Abs MT Position X", False);
26 Atom valuator_y = XInternAtom(display_, "Abs MT Position Y", False);
27 if (valuator_x == None || valuator_y == None)
30 std::set<int> no_match_touchscreen;
31 XIDeviceInfo* info = XIQueryDevice(display_, XIAllDevices, &ndevices);
32 for (int i = 0; i < ndevices; i++) {
33 if (!info[i].enabled || info[i].use != XIFloatingSlave)
34 continue; // Assume all touchscreens are floating slaves
38 bool is_direct_touch = false;
40 for (int j = 0; j < info[i].num_classes; j++) {
41 XIAnyClassInfo* class_info = info[i].classes[j];
43 if (class_info->type == XIValuatorClass) {
44 XIValuatorClassInfo* valuator_info =
45 reinterpret_cast<XIValuatorClassInfo*>(class_info);
47 if (valuator_x == valuator_info->label) {
48 // Ignore X axis valuator with unexpected properties
49 if (valuator_info->number == 0 && valuator_info->mode == Absolute &&
50 valuator_info->min == 0.0) {
51 width = valuator_info->max;
53 } else if (valuator_y == valuator_info->label) {
54 // Ignore Y axis valuator with unexpected properties
55 if (valuator_info->number == 1 && valuator_info->mode == Absolute &&
56 valuator_info->min == 0.0) {
57 height = valuator_info->max;
61 #if defined(USE_XI2_MT)
62 if (class_info->type == XITouchClass) {
63 XITouchClassInfo* touch_info =
64 reinterpret_cast<XITouchClassInfo*>(class_info);
65 is_direct_touch = touch_info->mode == XIDirectTouch;
70 // Touchscreens should have absolute X and Y axes,
71 // and be direct touch devices.
72 if (width > 0.0 && height > 0.0 && is_direct_touch) {
74 for (; k < outputs->size(); k++) {
75 OutputConfigurator::OutputSnapshot* output = &(*outputs)[k];
76 if (output->native_mode == None || output->touch_device_id != None)
79 const OutputConfigurator::ModeInfo* mode_info =
80 OutputConfigurator::GetModeInfo(*output, output->native_mode);
84 // Allow 1 pixel difference between screen and touchscreen
85 // resolutions. Because in some cases for monitor resolution
86 // 1024x768 touchscreen's resolution would be 1024x768, but for
87 // some 1023x767. It really depends on touchscreen's firmware
89 if (std::abs(mode_info->width - width) <= 1.0 &&
90 std::abs(mode_info->height - height) <= 1.0) {
91 output->touch_device_id = info[i].deviceid;
93 VLOG(2) << "Found touchscreen for output #" << k
94 << " id " << output->touch_device_id
96 << " height " << height;
101 if (k == outputs->size()) {
102 no_match_touchscreen.insert(info[i].deviceid);
103 VLOG(2) << "No matching output for touchscreen"
104 << " id " << info[i].deviceid
105 << " width " << width
106 << " height " << height;
112 // Sometimes we can't find a matching screen for the touchscreen, e.g.
113 // due to the touchscreen's reporting range having no correlation with the
114 // screen's resolution. In this case, we arbitrarily assign unmatched
115 // touchscreens to unmatched screens.
116 for (std::set<int>::iterator it = no_match_touchscreen.begin();
117 it != no_match_touchscreen.end();
119 for (size_t i = 0; i < outputs->size(); i++) {
120 if ((*outputs)[i].type != ui::OUTPUT_TYPE_INTERNAL &&
121 (*outputs)[i].native_mode != None &&
122 (*outputs)[i].touch_device_id == None) {
123 (*outputs)[i].touch_device_id = *it;
124 VLOG(2) << "Arbitrarily matching touchscreen "
125 << (*outputs)[i].touch_device_id << " to output #" << i;
131 XIFreeDeviceInfo(info);
134 void TouchscreenDelegateX11::ConfigureCTM(
136 const OutputConfigurator::CoordinateTransformation& ctm) {
137 VLOG(1) << "ConfigureCTM: id=" << touch_device_id
138 << " scale=" << ctm.x_scale << "x" << ctm.y_scale
139 << " offset=(" << ctm.x_offset << ", " << ctm.y_offset << ")";
141 XIDeviceInfo* info = XIQueryDevice(display_, touch_device_id, &ndevices);
142 Atom prop = XInternAtom(display_, "Coordinate Transformation Matrix", False);
143 Atom float_atom = XInternAtom(display_, "FLOAT", False);
144 if (ndevices == 1 && prop != None && float_atom != None) {
147 unsigned long num_items;
148 unsigned long bytes_after;
149 unsigned char* data = NULL;
150 // Verify that the property exists with correct format, type, etc.
151 int status = XIGetProperty(display_, info->deviceid, prop, 0, 0, False,
152 AnyPropertyType, &type, &format, &num_items, &bytes_after, &data);
155 if (status == Success && type == float_atom && format == 32) {
156 float value[3][3] = {
157 { ctm.x_scale, 0.0, ctm.x_offset },
158 { 0.0, ctm.y_scale, ctm.y_offset },
161 XIChangeProperty(display_,
167 reinterpret_cast<unsigned char*>(value),
171 XIFreeDeviceInfo(info);
174 } // namespace chromeos