OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/events/x/device_data_manager.h" | 5 #include "ui/events/x/device_data_manager.h" |
6 | 6 |
7 #include <X11/extensions/XInput.h> | 7 #include <X11/extensions/XInput.h> |
8 #include <X11/extensions/XInput2.h> | 8 #include <X11/extensions/XInput2.h> |
9 #include <X11/Xlib.h> | 9 #include <X11/Xlib.h> |
10 | 10 |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 bool DeviceDataManager::IsTouchDataType(const int type) { | 105 bool DeviceDataManager::IsTouchDataType(const int type) { |
106 return (type >= kTouchDataTypeStart) && (type <= kTouchDataTypeEnd); | 106 return (type >= kTouchDataTypeStart) && (type <= kTouchDataTypeEnd); |
107 } | 107 } |
108 | 108 |
109 DeviceDataManager* DeviceDataManager::GetInstance() { | 109 DeviceDataManager* DeviceDataManager::GetInstance() { |
110 return Singleton<DeviceDataManager>::get(); | 110 return Singleton<DeviceDataManager>::get(); |
111 } | 111 } |
112 | 112 |
113 DeviceDataManager::DeviceDataManager() | 113 DeviceDataManager::DeviceDataManager() |
114 : natural_scroll_enabled_(false), | 114 : natural_scroll_enabled_(false), |
115 xi_opcode_(-1), | |
116 atom_cache_(gfx::GetXDisplay(), kCachedAtoms), | 115 atom_cache_(gfx::GetXDisplay(), kCachedAtoms), |
117 button_map_count_(0) { | 116 button_map_count_(0) { |
118 CHECK(gfx::GetXDisplay()); | |
119 InitializeXInputInternal(); | 117 InitializeXInputInternal(); |
120 | 118 |
121 // Make sure the sizes of enum and kCachedAtoms are aligned. | 119 // Make sure the sizes of enum and kCachedAtoms are aligned. |
122 CHECK(arraysize(kCachedAtoms) == static_cast<size_t>(DT_LAST_ENTRY) + 1); | 120 CHECK(arraysize(kCachedAtoms) == static_cast<size_t>(DT_LAST_ENTRY) + 1); |
123 UpdateDeviceList(gfx::GetXDisplay()); | 121 UpdateDeviceList(gfx::GetXDisplay()); |
124 UpdateButtonMap(); | 122 UpdateButtonMap(); |
125 } | 123 } |
126 | 124 |
127 DeviceDataManager::~DeviceDataManager() { | 125 DeviceDataManager::~DeviceDataManager() { |
128 } | 126 } |
129 | 127 |
130 bool DeviceDataManager::InitializeXInputInternal() { | 128 bool DeviceDataManager::InitializeXInputInternal() { |
131 // Check if XInput is available on the system. | 129 // Check if XInput is available on the system. |
132 xi_opcode_ = -1; | 130 xi_opcode_ = -1; |
133 int opcode, event, error; | 131 int opcode, event, error; |
134 if (!XQueryExtension( | 132 if (!XQueryExtension( |
135 gfx::GetXDisplay(), "XInputExtension", &opcode, &event, &error)) { | 133 gfx::GetXDisplay(), "XInputExtension", &opcode, &event, &error)) { |
136 VLOG(1) << "X Input extension not available: error=" << error; | 134 VLOG(1) << "X Input extension not available: error=" << error; |
137 return false; | 135 return false; |
138 } | 136 } |
| 137 xi_opcode_ = opcode; |
139 | 138 |
140 // Check the XInput version. | 139 // Check the XInput version. |
141 #if defined(USE_XI2_MT) | 140 #if defined(USE_XI2_MT) |
142 int major = 2, minor = USE_XI2_MT; | 141 int major = 2, minor = USE_XI2_MT; |
143 #else | 142 #else |
144 int major = 2, minor = 0; | 143 int major = 2, minor = 0; |
145 #endif | 144 #endif |
146 if (XIQueryVersion(gfx::GetXDisplay(), &major, &minor) == BadRequest) { | 145 if (XIQueryVersion(gfx::GetXDisplay(), &major, &minor) == BadRequest) { |
147 VLOG(1) << "XInput2 not supported in the server."; | 146 VLOG(1) << "XInput2 not supported in the server."; |
148 return false; | 147 return false; |
149 } | 148 } |
150 #if defined(USE_XI2_MT) | |
151 if (major < 2 || (major == 2 && minor < USE_XI2_MT)) { | |
152 DVLOG(1) << "XI version on server is " << major << "." << minor << ". " | |
153 << "But 2." << USE_XI2_MT << " is required."; | |
154 return false; | |
155 } | |
156 #endif | |
157 | |
158 xi_opcode_ = opcode; | |
159 CHECK_NE(-1, xi_opcode_); | |
160 | 149 |
161 // Possible XI event types for XIDeviceEvent. See the XI2 protocol | 150 // Possible XI event types for XIDeviceEvent. See the XI2 protocol |
162 // specification. | 151 // specification. |
163 xi_device_event_types_[XI_KeyPress] = true; | 152 xi_device_event_types_[XI_KeyPress] = true; |
164 xi_device_event_types_[XI_KeyRelease] = true; | 153 xi_device_event_types_[XI_KeyRelease] = true; |
165 xi_device_event_types_[XI_ButtonPress] = true; | 154 xi_device_event_types_[XI_ButtonPress] = true; |
166 xi_device_event_types_[XI_ButtonRelease] = true; | 155 xi_device_event_types_[XI_ButtonRelease] = true; |
167 xi_device_event_types_[XI_Motion] = true; | 156 xi_device_event_types_[XI_Motion] = true; |
168 // Multi-touch support was introduced in XI 2.2. | 157 // Multi-touch support was introduced in XI 2.2. |
169 if (minor >= 2) { | 158 if (minor >= 2) { |
170 xi_device_event_types_[XI_TouchBegin] = true; | 159 xi_device_event_types_[XI_TouchBegin] = true; |
171 xi_device_event_types_[XI_TouchUpdate] = true; | 160 xi_device_event_types_[XI_TouchUpdate] = true; |
172 xi_device_event_types_[XI_TouchEnd] = true; | 161 xi_device_event_types_[XI_TouchEnd] = true; |
173 } | 162 } |
174 return true; | 163 return true; |
175 } | 164 } |
176 | 165 |
177 bool DeviceDataManager::IsXInput2Available() const { | |
178 return xi_opcode_ != -1; | |
179 } | |
180 | |
181 float DeviceDataManager::GetNaturalScrollFactor(int sourceid) const { | 166 float DeviceDataManager::GetNaturalScrollFactor(int sourceid) const { |
182 // Natural scroll is touchpad-only. | 167 // Natural scroll is touchpad-only. |
183 if (sourceid >= kMaxDeviceNum || !touchpads_[sourceid]) | 168 if (sourceid >= kMaxDeviceNum || !touchpads_[sourceid]) |
184 return -1.0f; | 169 return -1.0f; |
185 | 170 |
186 return natural_scroll_enabled_ ? 1.0f : -1.0f; | 171 return natural_scroll_enabled_ ? 1.0f : -1.0f; |
187 } | 172 } |
188 | 173 |
189 void DeviceDataManager::UpdateDeviceList(Display* display) { | 174 void DeviceDataManager::UpdateDeviceList(Display* display) { |
190 cmt_devices_.reset(); | 175 cmt_devices_.reset(); |
191 touchpads_.reset(); | 176 touchpads_.reset(); |
192 for (int i = 0; i < kMaxDeviceNum; ++i) { | 177 for (int i = 0; i < kMaxDeviceNum; ++i) { |
193 valuator_count_[i] = 0; | 178 valuator_count_[i] = 0; |
194 valuator_lookup_[i].clear(); | 179 valuator_lookup_[i].clear(); |
195 data_type_lookup_[i].clear(); | 180 data_type_lookup_[i].clear(); |
196 valuator_min_[i].clear(); | 181 valuator_min_[i].clear(); |
197 valuator_max_[i].clear(); | 182 valuator_max_[i].clear(); |
198 for (int j = 0; j < kMaxSlotNum; j++) | 183 for (int j = 0; j < kMaxSlotNum; j++) |
199 last_seen_valuator_[i][j].clear(); | 184 last_seen_valuator_[i][j].clear(); |
200 } | 185 } |
201 | 186 |
202 // Find all the touchpad devices. | 187 // Find all the touchpad devices. |
203 XDeviceList dev_list = | 188 XDeviceList dev_list = |
204 ui::DeviceListCacheX::GetInstance()->GetXDeviceList(display); | 189 ui::DeviceListCacheX::GetInstance()->GetXDeviceList(display); |
205 Atom xi_touchpad = XInternAtom(display, XI_TOUCHPAD, false); | 190 Atom xi_touchpad = XInternAtom(display, XI_TOUCHPAD, false); |
206 for (int i = 0; i < dev_list.count; ++i) | 191 for (int i = 0; i < dev_list.count; ++i) |
207 if (dev_list[i].type == xi_touchpad) | 192 if (dev_list[i].type == xi_touchpad) |
208 touchpads_[dev_list[i].id] = true; | 193 touchpads_[dev_list[i].id] = true; |
209 | 194 |
210 if (!IsXInput2Available()) | |
211 return; | |
212 | |
213 // Update the structs with new valuator information | 195 // Update the structs with new valuator information |
214 XIDeviceList info_list = | 196 XIDeviceList info_list = |
215 ui::DeviceListCacheX::GetInstance()->GetXI2DeviceList(display); | 197 ui::DeviceListCacheX::GetInstance()->GetXI2DeviceList(display); |
216 Atom atoms[DT_LAST_ENTRY]; | 198 Atom atoms[DT_LAST_ENTRY]; |
217 for (int data_type = 0; data_type < DT_LAST_ENTRY; ++data_type) | 199 for (int data_type = 0; data_type < DT_LAST_ENTRY; ++data_type) |
218 atoms[data_type] = atom_cache_.GetAtom(kCachedAtoms[data_type]); | 200 atoms[data_type] = atom_cache_.GetAtom(kCachedAtoms[data_type]); |
219 | 201 |
220 for (int i = 0; i < info_list.count; ++i) { | 202 for (int i = 0; i < info_list.count; ++i) { |
221 XIDeviceInfo* info = info_list.devices + i; | 203 XIDeviceInfo* info = info_list.devices + i; |
222 | 204 |
(...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
609 int val_index, | 591 int val_index, |
610 DataType data_type, | 592 DataType data_type, |
611 double min, | 593 double min, |
612 double max) { | 594 double max) { |
613 valuator_lookup_[deviceid][data_type] = val_index; | 595 valuator_lookup_[deviceid][data_type] = val_index; |
614 data_type_lookup_[deviceid][val_index] = data_type; | 596 data_type_lookup_[deviceid][val_index] = data_type; |
615 valuator_min_[deviceid][data_type] = min; | 597 valuator_min_[deviceid][data_type] = min; |
616 valuator_max_[deviceid][data_type] = max; | 598 valuator_max_[deviceid][data_type] = max; |
617 } | 599 } |
618 } // namespace ui | 600 } // namespace ui |
OLD | NEW |