| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/devices/x11/touch_factory_x11.h" | 5 #include "ui/events/devices/x11/touch_factory_x11.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <X11/Xatom.h> | 8 #include <X11/Xatom.h> |
| 9 #include <X11/cursorfont.h> | 9 #include <X11/cursorfont.h> |
| 10 #include <X11/extensions/XInput.h> | 10 #include <X11/extensions/XInput.h> |
| 11 #include <X11/extensions/XInput2.h> | 11 #include <X11/extensions/XInput2.h> |
| 12 #include <X11/extensions/XIproto.h> | 12 #include <X11/extensions/XIproto.h> |
| 13 | 13 |
| 14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
| 15 #include "base/compiler_specific.h" | 15 #include "base/compiler_specific.h" |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/memory/singleton.h" | 17 #include "base/memory/singleton.h" |
| 18 #include "base/message_loop/message_loop.h" | 18 #include "base/message_loop/message_loop.h" |
| 19 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
| 20 #include "base/strings/string_split.h" | 20 #include "base/strings/string_split.h" |
| 21 #include "base/sys_info.h" | 21 #include "base/sys_info.h" |
| 22 #include "build/build_config.h" | 22 #include "build/build_config.h" |
| 23 #include "ui/events/base_event_utils.h" | 23 #include "ui/events/base_event_utils.h" |
| 24 #include "ui/events/devices/x11/device_data_manager_x11.h" | 24 #include "ui/events/devices/x11/device_data_manager_x11.h" |
| 25 #include "ui/events/devices/x11/device_list_cache_x11.h" | 25 #include "ui/events/devices/x11/device_list_cache_x11.h" |
| 26 #include "ui/events/event_switches.h" | 26 #include "ui/events/event_switches.h" |
| 27 | 27 |
| 28 namespace ui { | 28 namespace ui { |
| 29 | 29 |
| 30 namespace { | |
| 31 | |
| 32 bool IsTouchEventsFlagDisabled() { | |
| 33 auto* command_line = base::CommandLine::ForCurrentProcess(); | |
| 34 bool touch_flag_status = command_line->HasSwitch(switches::kTouchEvents) && | |
| 35 command_line->GetSwitchValueASCII(switches::kTouchEvents) == | |
| 36 switches::kTouchEventsDisabled; | |
| 37 return touch_flag_status; | |
| 38 } | |
| 39 | |
| 40 } // namespace | |
| 41 | |
| 42 | |
| 43 TouchFactory::TouchFactory() | 30 TouchFactory::TouchFactory() |
| 44 : pointer_device_lookup_(), | 31 : pointer_device_lookup_(), |
| 45 touch_device_list_(), | 32 touch_device_list_(), |
| 46 virtual_core_keyboard_device_(-1), | 33 virtual_core_keyboard_device_(-1), |
| 47 id_generator_(0), | 34 id_generator_(0), |
| 48 touch_events_flag_disabled_(IsTouchEventsFlagDisabled()), | |
| 49 touch_screens_enabled_(true) { | 35 touch_screens_enabled_(true) { |
| 50 if (!DeviceDataManagerX11::GetInstance()->IsXInput2Available()) | 36 if (!DeviceDataManagerX11::GetInstance()->IsXInput2Available()) |
| 51 return; | 37 return; |
| 52 | 38 |
| 53 XDisplay* display = gfx::GetXDisplay(); | 39 XDisplay* display = gfx::GetXDisplay(); |
| 54 UpdateDeviceList(display); | 40 UpdateDeviceList(display); |
| 55 } | 41 } |
| 56 | 42 |
| 57 TouchFactory::~TouchFactory() { | 43 TouchFactory::~TouchFactory() { |
| 58 } | 44 } |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 } | 137 } |
| 152 } | 138 } |
| 153 } | 139 } |
| 154 } | 140 } |
| 155 | 141 |
| 156 bool TouchFactory::ShouldProcessXI2Event(XEvent* xev) { | 142 bool TouchFactory::ShouldProcessXI2Event(XEvent* xev) { |
| 157 DCHECK_EQ(GenericEvent, xev->type); | 143 DCHECK_EQ(GenericEvent, xev->type); |
| 158 XIEvent* event = static_cast<XIEvent*>(xev->xcookie.data); | 144 XIEvent* event = static_cast<XIEvent*>(xev->xcookie.data); |
| 159 XIDeviceEvent* xiev = reinterpret_cast<XIDeviceEvent*>(event); | 145 XIDeviceEvent* xiev = reinterpret_cast<XIDeviceEvent*>(event); |
| 160 | 146 |
| 161 const bool is_touch_disabled = | |
| 162 touch_events_flag_disabled_ && !touch_screens_enabled_; | |
| 163 | |
| 164 if (event->evtype == XI_TouchBegin || | 147 if (event->evtype == XI_TouchBegin || |
| 165 event->evtype == XI_TouchUpdate || | 148 event->evtype == XI_TouchUpdate || |
| 166 event->evtype == XI_TouchEnd) { | 149 event->evtype == XI_TouchEnd) { |
| 167 // Since SetupXI2ForXWindow() selects events from all devices, for a | 150 // Since SetupXI2ForXWindow() selects events from all devices, for a |
| 168 // touchscreen attached to a master pointer device, X11 sends two | 151 // touchscreen attached to a master pointer device, X11 sends two |
| 169 // events for each touch: one from the slave (deviceid == the id of | 152 // events for each touch: one from the slave (deviceid == the id of |
| 170 // the touchscreen device), and one from the master (deviceid == the | 153 // the touchscreen device), and one from the master (deviceid == the |
| 171 // id of the master pointer device). Instead of processing both | 154 // id of the master pointer device). Instead of processing both |
| 172 // events, discard the event that comes from the slave, and only | 155 // events, discard the event that comes from the slave, and only |
| 173 // allow processing the event coming from the master. | 156 // allow processing the event coming from the master. |
| 174 // For a 'floating' touchscreen device, X11 sends only one event for | 157 // For a 'floating' touchscreen device, X11 sends only one event for |
| 175 // each touch, with both deviceid and sourceid set to the id of the | 158 // each touch, with both deviceid and sourceid set to the id of the |
| 176 // touchscreen device. | 159 // touchscreen device. |
| 177 bool is_from_master_or_float = touch_device_list_[xiev->deviceid]; | 160 bool is_from_master_or_float = touch_device_list_[xiev->deviceid]; |
| 178 bool is_from_slave_device = !is_from_master_or_float | 161 bool is_from_slave_device = !is_from_master_or_float |
| 179 && xiev->sourceid == xiev->deviceid; | 162 && xiev->sourceid == xiev->deviceid; |
| 180 return !is_touch_disabled && | 163 return touch_screens_enabled_ && IsTouchDevice(xiev->deviceid) && |
| 181 IsTouchDevice(xiev->deviceid) && | |
| 182 !is_from_slave_device; | 164 !is_from_slave_device; |
| 183 } | 165 } |
| 184 | 166 |
| 185 // Make sure only key-events from the virtual core keyboard are processed. | 167 // Make sure only key-events from the virtual core keyboard are processed. |
| 186 if (event->evtype == XI_KeyPress || event->evtype == XI_KeyRelease) { | 168 if (event->evtype == XI_KeyPress || event->evtype == XI_KeyRelease) { |
| 187 return (virtual_core_keyboard_device_ < 0) || | 169 return (virtual_core_keyboard_device_ < 0) || |
| 188 (virtual_core_keyboard_device_ == xiev->deviceid); | 170 (virtual_core_keyboard_device_ == xiev->deviceid); |
| 189 } | 171 } |
| 190 | 172 |
| 191 if (event->evtype != XI_ButtonPress && | 173 if (event->evtype != XI_ButtonPress && |
| 192 event->evtype != XI_ButtonRelease && | 174 event->evtype != XI_ButtonRelease && |
| 193 event->evtype != XI_Motion) { | 175 event->evtype != XI_Motion) { |
| 194 return true; | 176 return true; |
| 195 } | 177 } |
| 196 | 178 |
| 197 if (!pointer_device_lookup_[xiev->deviceid]) | 179 if (!pointer_device_lookup_[xiev->deviceid]) |
| 198 return false; | 180 return false; |
| 199 | 181 |
| 200 return IsTouchDevice(xiev->deviceid) ? !is_touch_disabled : true; | 182 return IsTouchDevice(xiev->deviceid) ? touch_screens_enabled_ : true; |
| 201 } | 183 } |
| 202 | 184 |
| 203 void TouchFactory::SetupXI2ForXWindow(Window window) { | 185 void TouchFactory::SetupXI2ForXWindow(Window window) { |
| 204 // Setup mask for mouse events. It is possible that a device is loaded/plugged | 186 // Setup mask for mouse events. It is possible that a device is loaded/plugged |
| 205 // in after we have setup XInput2 on a window. In such cases, we need to | 187 // in after we have setup XInput2 on a window. In such cases, we need to |
| 206 // either resetup XInput2 for the window, so that we get events from the new | 188 // either resetup XInput2 for the window, so that we get events from the new |
| 207 // device, or we need to listen to events from all devices, and then filter | 189 // device, or we need to listen to events from all devices, and then filter |
| 208 // the events from uninteresting devices. We do the latter because that's | 190 // the events from uninteresting devices. We do the latter because that's |
| 209 // simpler. | 191 // simpler. |
| 210 | 192 |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 | 268 |
| 287 int TouchFactory::GetSlotForTrackingID(uint32_t tracking_id) { | 269 int TouchFactory::GetSlotForTrackingID(uint32_t tracking_id) { |
| 288 return id_generator_.GetGeneratedID(tracking_id); | 270 return id_generator_.GetGeneratedID(tracking_id); |
| 289 } | 271 } |
| 290 | 272 |
| 291 void TouchFactory::ReleaseSlotForTrackingID(uint32_t tracking_id) { | 273 void TouchFactory::ReleaseSlotForTrackingID(uint32_t tracking_id) { |
| 292 id_generator_.ReleaseNumber(tracking_id); | 274 id_generator_.ReleaseNumber(tracking_id); |
| 293 } | 275 } |
| 294 | 276 |
| 295 bool TouchFactory::IsTouchDevicePresent() { | 277 bool TouchFactory::IsTouchDevicePresent() { |
| 296 return !touch_events_flag_disabled_ && | 278 return touch_screens_enabled_ && touch_device_lookup_.any(); |
| 297 touch_screens_enabled_ && | |
| 298 touch_device_lookup_.any(); | |
| 299 } | 279 } |
| 300 | 280 |
| 301 void TouchFactory::ResetForTest() { | 281 void TouchFactory::ResetForTest() { |
| 302 pointer_device_lookup_.reset(); | 282 pointer_device_lookup_.reset(); |
| 303 touch_device_lookup_.reset(); | 283 touch_device_lookup_.reset(); |
| 304 touch_device_list_.clear(); | 284 touch_device_list_.clear(); |
| 305 touchscreen_ids_.clear(); | 285 touchscreen_ids_.clear(); |
| 306 id_generator_.ResetForTest(); | 286 id_generator_.ResetForTest(); |
| 307 touch_events_flag_disabled_ = false; | |
| 308 SetTouchscreensEnabled(true); | 287 SetTouchscreensEnabled(true); |
| 309 } | 288 } |
| 310 | 289 |
| 311 void TouchFactory::SetTouchDeviceForTest( | 290 void TouchFactory::SetTouchDeviceForTest( |
| 312 const std::vector<int>& devices) { | 291 const std::vector<int>& devices) { |
| 313 touch_device_lookup_.reset(); | 292 touch_device_lookup_.reset(); |
| 314 touch_device_list_.clear(); | 293 touch_device_list_.clear(); |
| 315 for (std::vector<int>::const_iterator iter = devices.begin(); | 294 for (std::vector<int>::const_iterator iter = devices.begin(); |
| 316 iter != devices.end(); ++iter) { | 295 iter != devices.end(); ++iter) { |
| 317 DCHECK(IsValidDevice(*iter)); | 296 DCHECK(IsValidDevice(*iter)); |
| 318 touch_device_lookup_[*iter] = true; | 297 touch_device_lookup_[*iter] = true; |
| 319 touch_device_list_[*iter] = true; | 298 touch_device_list_[*iter] = true; |
| 320 } | 299 } |
| 321 touch_events_flag_disabled_ = false; | |
| 322 SetTouchscreensEnabled(true); | 300 SetTouchscreensEnabled(true); |
| 323 } | 301 } |
| 324 | 302 |
| 325 void TouchFactory::SetPointerDeviceForTest( | 303 void TouchFactory::SetPointerDeviceForTest( |
| 326 const std::vector<int>& devices) { | 304 const std::vector<int>& devices) { |
| 327 pointer_device_lookup_.reset(); | 305 pointer_device_lookup_.reset(); |
| 328 for (std::vector<int>::const_iterator iter = devices.begin(); | 306 for (std::vector<int>::const_iterator iter = devices.begin(); |
| 329 iter != devices.end(); ++iter) { | 307 iter != devices.end(); ++iter) { |
| 330 pointer_device_lookup_[*iter] = true; | 308 pointer_device_lookup_[*iter] = true; |
| 331 } | 309 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 345 std::find_if(touchscreens.begin(), touchscreens.end(), | 323 std::find_if(touchscreens.begin(), touchscreens.end(), |
| 346 [device_id](const TouchscreenDevice& touchscreen) { | 324 [device_id](const TouchscreenDevice& touchscreen) { |
| 347 return touchscreen.id == device_id; | 325 return touchscreen.id == device_id; |
| 348 }); | 326 }); |
| 349 // Internal displays will have a vid and pid of 0. Ignore them. | 327 // Internal displays will have a vid and pid of 0. Ignore them. |
| 350 if (it != touchscreens.end() && it->vendor_id && it->product_id) | 328 if (it != touchscreens.end() && it->vendor_id && it->product_id) |
| 351 touchscreen_ids_.insert(std::make_pair(it->vendor_id, it->product_id)); | 329 touchscreen_ids_.insert(std::make_pair(it->vendor_id, it->product_id)); |
| 352 } | 330 } |
| 353 | 331 |
| 354 } // namespace ui | 332 } // namespace ui |
| OLD | NEW |