| 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 <X11/Xatom.h> | 7 #include <X11/Xatom.h> |
| 8 #include <X11/cursorfont.h> | 8 #include <X11/cursorfont.h> |
| 9 #include <X11/extensions/XInput.h> | 9 #include <X11/extensions/XInput.h> |
| 10 #include <X11/extensions/XInput2.h> | 10 #include <X11/extensions/XInput2.h> |
| 11 #include <X11/extensions/XIproto.h> | 11 #include <X11/extensions/XIproto.h> |
| 12 | 12 |
| 13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 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 "ui/events/base_event_utils.h" |
| 22 #include "ui/events/devices/x11/device_data_manager_x11.h" | 23 #include "ui/events/devices/x11/device_data_manager_x11.h" |
| 23 #include "ui/events/devices/x11/device_list_cache_x11.h" | 24 #include "ui/events/devices/x11/device_list_cache_x11.h" |
| 24 #include "ui/events/event_switches.h" | 25 #include "ui/events/event_switches.h" |
| 25 #include "ui/gfx/x/x11_types.h" | 26 #include "ui/gfx/x/x11_types.h" |
| 26 | 27 |
| 27 namespace ui { | 28 namespace ui { |
| 28 | 29 |
| 29 TouchFactory::TouchFactory() | 30 TouchFactory::TouchFactory() |
| 30 : pointer_device_lookup_(), | 31 : pointer_device_lookup_(), |
| 31 touch_events_disabled_(false), | |
| 32 touch_device_list_(), | 32 touch_device_list_(), |
| 33 virtual_core_keyboard_device_(-1), | 33 virtual_core_keyboard_device_(-1), |
| 34 id_generator_(0) { | 34 id_generator_(0) { |
| 35 if (!DeviceDataManagerX11::GetInstance()->IsXInput2Available()) | 35 if (!DeviceDataManagerX11::GetInstance()->IsXInput2Available()) |
| 36 return; | 36 return; |
| 37 | 37 |
| 38 XDisplay* display = gfx::GetXDisplay(); | 38 XDisplay* display = gfx::GetXDisplay(); |
| 39 UpdateDeviceList(display); | 39 UpdateDeviceList(display); |
| 40 | |
| 41 base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess(); | |
| 42 touch_events_disabled_ = cmdline->HasSwitch(switches::kTouchEvents) && | |
| 43 cmdline->GetSwitchValueASCII(switches::kTouchEvents) == | |
| 44 switches::kTouchEventsDisabled; | |
| 45 } | 40 } |
| 46 | 41 |
| 47 TouchFactory::~TouchFactory() { | 42 TouchFactory::~TouchFactory() { |
| 48 } | 43 } |
| 49 | 44 |
| 50 // static | 45 // static |
| 51 TouchFactory* TouchFactory::GetInstance() { | 46 TouchFactory* TouchFactory::GetInstance() { |
| 52 return base::Singleton<TouchFactory>::get(); | 47 return base::Singleton<TouchFactory>::get(); |
| 53 } | 48 } |
| 54 | 49 |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 // the touchscreen device), and one from the master (deviceid == the | 152 // the touchscreen device), and one from the master (deviceid == the |
| 158 // id of the master pointer device). Instead of processing both | 153 // id of the master pointer device). Instead of processing both |
| 159 // events, discard the event that comes from the slave, and only | 154 // events, discard the event that comes from the slave, and only |
| 160 // allow processing the event coming from the master. | 155 // allow processing the event coming from the master. |
| 161 // For a 'floating' touchscreen device, X11 sends only one event for | 156 // For a 'floating' touchscreen device, X11 sends only one event for |
| 162 // each touch, with both deviceid and sourceid set to the id of the | 157 // each touch, with both deviceid and sourceid set to the id of the |
| 163 // touchscreen device. | 158 // touchscreen device. |
| 164 bool is_from_master_or_float = touch_device_list_[xiev->deviceid]; | 159 bool is_from_master_or_float = touch_device_list_[xiev->deviceid]; |
| 165 bool is_from_slave_device = !is_from_master_or_float | 160 bool is_from_slave_device = !is_from_master_or_float |
| 166 && xiev->sourceid == xiev->deviceid; | 161 && xiev->sourceid == xiev->deviceid; |
| 167 return !touch_events_disabled_ && | 162 return ui::AreTouchEventsEnabled() && |
| 168 IsTouchDevice(xiev->deviceid) && | 163 IsTouchDevice(xiev->deviceid) && |
| 169 !is_from_slave_device; | 164 !is_from_slave_device; |
| 170 } | 165 } |
| 171 | 166 |
| 172 // 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. |
| 173 if (event->evtype == XI_KeyPress || event->evtype == XI_KeyRelease) { | 168 if (event->evtype == XI_KeyPress || event->evtype == XI_KeyRelease) { |
| 174 return (virtual_core_keyboard_device_ < 0) || | 169 return (virtual_core_keyboard_device_ < 0) || |
| 175 (virtual_core_keyboard_device_ == xiev->deviceid); | 170 (virtual_core_keyboard_device_ == xiev->deviceid); |
| 176 } | 171 } |
| 177 | 172 |
| 178 if (event->evtype != XI_ButtonPress && | 173 if (event->evtype != XI_ButtonPress && |
| 179 event->evtype != XI_ButtonRelease && | 174 event->evtype != XI_ButtonRelease && |
| 180 event->evtype != XI_Motion) | 175 event->evtype != XI_Motion) { |
| 181 return true; | 176 return true; |
| 177 } |
| 182 | 178 |
| 183 if (!pointer_device_lookup_[xiev->deviceid]) | 179 if (!pointer_device_lookup_[xiev->deviceid]) |
| 184 return false; | 180 return false; |
| 185 | 181 |
| 186 return IsTouchDevice(xiev->deviceid) ? !touch_events_disabled_ : true; | 182 return IsTouchDevice(xiev->deviceid) ? ui::AreTouchEventsEnabled() : true; |
| 187 } | 183 } |
| 188 | 184 |
| 189 void TouchFactory::SetupXI2ForXWindow(Window window) { | 185 void TouchFactory::SetupXI2ForXWindow(Window window) { |
| 190 // 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 |
| 191 // 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 |
| 192 // 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 |
| 193 // 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 |
| 194 // 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 |
| 195 // simpler. | 191 // simpler. |
| 196 | 192 |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 | 259 |
| 264 int TouchFactory::GetSlotForTrackingID(uint32 tracking_id) { | 260 int TouchFactory::GetSlotForTrackingID(uint32 tracking_id) { |
| 265 return id_generator_.GetGeneratedID(tracking_id); | 261 return id_generator_.GetGeneratedID(tracking_id); |
| 266 } | 262 } |
| 267 | 263 |
| 268 void TouchFactory::ReleaseSlotForTrackingID(uint32 tracking_id) { | 264 void TouchFactory::ReleaseSlotForTrackingID(uint32 tracking_id) { |
| 269 id_generator_.ReleaseNumber(tracking_id); | 265 id_generator_.ReleaseNumber(tracking_id); |
| 270 } | 266 } |
| 271 | 267 |
| 272 bool TouchFactory::IsTouchDevicePresent() { | 268 bool TouchFactory::IsTouchDevicePresent() { |
| 273 return !touch_events_disabled_ && touch_device_lookup_.any(); | 269 return ui::AreTouchEventsEnabled() && touch_device_lookup_.any(); |
| 274 } | 270 } |
| 275 | 271 |
| 276 void TouchFactory::ResetForTest() { | 272 void TouchFactory::ResetForTest() { |
| 277 pointer_device_lookup_.reset(); | 273 pointer_device_lookup_.reset(); |
| 278 touch_device_lookup_.reset(); | 274 touch_device_lookup_.reset(); |
| 279 touch_events_disabled_ = false; | |
| 280 touch_device_list_.clear(); | 275 touch_device_list_.clear(); |
| 281 touchscreen_ids_.clear(); | 276 touchscreen_ids_.clear(); |
| 282 id_generator_.ResetForTest(); | 277 id_generator_.ResetForTest(); |
| 283 } | 278 } |
| 284 | 279 |
| 285 void TouchFactory::SetTouchDeviceForTest( | 280 void TouchFactory::SetTouchDeviceForTest( |
| 286 const std::vector<int>& devices) { | 281 const std::vector<int>& devices) { |
| 287 touch_device_lookup_.reset(); | 282 touch_device_lookup_.reset(); |
| 288 touch_device_list_.clear(); | 283 touch_device_list_.clear(); |
| 289 for (std::vector<int>::const_iterator iter = devices.begin(); | 284 for (std::vector<int>::const_iterator iter = devices.begin(); |
| 290 iter != devices.end(); ++iter) { | 285 iter != devices.end(); ++iter) { |
| 291 DCHECK(IsValidDevice(*iter)); | 286 DCHECK(IsValidDevice(*iter)); |
| 292 touch_device_lookup_[*iter] = true; | 287 touch_device_lookup_[*iter] = true; |
| 293 touch_device_list_[*iter] = true; | 288 touch_device_list_[*iter] = true; |
| 294 } | 289 } |
| 295 touch_events_disabled_ = false; | |
| 296 } | 290 } |
| 297 | 291 |
| 298 void TouchFactory::SetPointerDeviceForTest( | 292 void TouchFactory::SetPointerDeviceForTest( |
| 299 const std::vector<int>& devices) { | 293 const std::vector<int>& devices) { |
| 300 pointer_device_lookup_.reset(); | 294 pointer_device_lookup_.reset(); |
| 301 for (std::vector<int>::const_iterator iter = devices.begin(); | 295 for (std::vector<int>::const_iterator iter = devices.begin(); |
| 302 iter != devices.end(); ++iter) { | 296 iter != devices.end(); ++iter) { |
| 303 pointer_device_lookup_[*iter] = true; | 297 pointer_device_lookup_[*iter] = true; |
| 304 } | 298 } |
| 305 } | 299 } |
| 306 | 300 |
| 307 void TouchFactory::CacheTouchscreenIds(int device_id) { | 301 void TouchFactory::CacheTouchscreenIds(int device_id) { |
| 308 if (!DeviceDataManager::HasInstance()) | 302 if (!DeviceDataManager::HasInstance()) |
| 309 return; | 303 return; |
| 310 std::vector<TouchscreenDevice> touchscreens = | 304 std::vector<TouchscreenDevice> touchscreens = |
| 311 DeviceDataManager::GetInstance()->touchscreen_devices(); | 305 DeviceDataManager::GetInstance()->touchscreen_devices(); |
| 312 const auto it = | 306 const auto it = |
| 313 std::find_if(touchscreens.begin(), touchscreens.end(), | 307 std::find_if(touchscreens.begin(), touchscreens.end(), |
| 314 [device_id](const TouchscreenDevice& touchscreen) { | 308 [device_id](const TouchscreenDevice& touchscreen) { |
| 315 return touchscreen.id == device_id; | 309 return touchscreen.id == device_id; |
| 316 }); | 310 }); |
| 317 // Internal displays will have a vid and pid of 0. Ignore them. | 311 // Internal displays will have a vid and pid of 0. Ignore them. |
| 318 if (it != touchscreens.end() && it->vendor_id && it->product_id) | 312 if (it != touchscreens.end() && it->vendor_id && it->product_id) |
| 319 touchscreen_ids_.insert(std::make_pair(it->vendor_id, it->product_id)); | 313 touchscreen_ids_.insert(std::make_pair(it->vendor_id, it->product_id)); |
| 320 } | 314 } |
| 321 | 315 |
| 322 } // namespace ui | 316 } // namespace ui |
| OLD | NEW |