Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chromeos / display / touchscreen_delegate_x11.cc
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.
4
5 #include "chromeos/display/touchscreen_delegate_x11.h"
6
7 #include <X11/extensions/XInput.h>
8 #include <X11/extensions/XInput2.h>
9
10 #include <cmath>
11 #include <set>
12
13 #include "base/message_loop/message_pump_x11.h"
14
15 namespace chromeos {
16
17 TouchscreenDelegateX11::TouchscreenDelegateX11()
18     : display_(base::MessagePumpX11::GetDefaultXDisplay()) {}
19
20 TouchscreenDelegateX11::~TouchscreenDelegateX11() {}
21
22 void TouchscreenDelegateX11::AssociateTouchscreens(
23     std::vector<OutputConfigurator::OutputSnapshot>* outputs) {
24   int ndevices = 0;
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)
28     return;
29
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
35
36     double width = -1.0;
37     double height = -1.0;
38     bool is_direct_touch = false;
39
40     for (int j = 0; j < info[i].num_classes; j++) {
41       XIAnyClassInfo* class_info = info[i].classes[j];
42
43       if (class_info->type == XIValuatorClass) {
44         XIValuatorClassInfo* valuator_info =
45             reinterpret_cast<XIValuatorClassInfo*>(class_info);
46
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;
52           }
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;
58           }
59         }
60       }
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;
66       }
67 #endif
68     }
69
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) {
73       size_t k = 0;
74       for (; k < outputs->size(); k++) {
75         OutputConfigurator::OutputSnapshot* output = &(*outputs)[k];
76         if (output->native_mode == None || output->touch_device_id != None)
77           continue;
78
79         const OutputConfigurator::ModeInfo* mode_info =
80             OutputConfigurator::GetModeInfo(*output, output->native_mode);
81         if (!mode_info)
82           continue;
83
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
88         // configuration.
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;
92
93           VLOG(2) << "Found touchscreen for output #" << k
94                   << " id " << output->touch_device_id
95                   << " width " << width
96                   << " height " << height;
97           break;
98         }
99       }
100
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;
107       }
108
109     }
110   }
111
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();
118        it++) {
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;
126         break;
127       }
128     }
129   }
130
131   XIFreeDeviceInfo(info);
132 }
133
134 void TouchscreenDelegateX11::ConfigureCTM(
135     int touch_device_id,
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 << ")";
140   int ndevices = 0;
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) {
145     Atom type;
146     int format;
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);
153     if (data)
154       XFree(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 },
159           {         0.0,         0.0,          1.0 }
160       };
161       XIChangeProperty(display_,
162                        info->deviceid,
163                        prop,
164                        type,
165                        format,
166                        PropModeReplace,
167                        reinterpret_cast<unsigned char*>(value),
168                        9);
169     }
170   }
171   XIFreeDeviceInfo(info);
172 }
173
174 }  // namespace chromeos