| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/platform/x11/x11_hotplug_event_handler.h" | 5 #include "ui/events/platform/x11/x11_hotplug_event_handler.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <X11/extensions/XInput.h> | 8 #include <X11/extensions/XInput.h> |
| 9 #include <X11/extensions/XInput2.h> | 9 #include <X11/extensions/XInput2.h> |
| 10 #include <X11/Xatom.h> | 10 #include <X11/Xatom.h> |
| 11 | 11 |
| 12 #include <algorithm> | 12 #include <algorithm> |
| 13 #include <cmath> | 13 #include <cmath> |
| 14 #include <set> | 14 #include <set> |
| 15 #include <string> | 15 #include <string> |
| 16 #include <vector> | 16 #include <vector> |
| 17 | 17 |
| 18 #include "base/bind.h" | 18 #include "base/bind.h" |
| 19 #include "base/command_line.h" | 19 #include "base/command_line.h" |
| 20 #include "base/location.h" | 20 #include "base/location.h" |
| 21 #include "base/logging.h" | 21 #include "base/logging.h" |
| 22 #include "base/process/launch.h" | 22 #include "base/process/launch.h" |
| 23 #include "base/single_thread_task_runner.h" | 23 #include "base/single_thread_task_runner.h" |
| 24 #include "base/strings/string_util.h" | 24 #include "base/strings/string_util.h" |
| 25 #include "base/sys_info.h" | 25 #include "base/sys_info.h" |
| 26 #include "base/threading/thread_task_runner_handle.h" | 26 #include "base/threading/thread_task_runner_handle.h" |
| 27 #include "base/threading/worker_pool.h" | 27 #include "base/threading/worker_pool.h" |
| 28 #include "ui/base/x/x11_util.h" | |
| 29 #include "ui/events/devices/device_data_manager.h" | 28 #include "ui/events/devices/device_data_manager.h" |
| 30 #include "ui/events/devices/device_hotplug_event_observer.h" | 29 #include "ui/events/devices/device_hotplug_event_observer.h" |
| 31 #include "ui/events/devices/device_util_linux.h" | 30 #include "ui/events/devices/device_util_linux.h" |
| 32 #include "ui/events/devices/input_device.h" | 31 #include "ui/events/devices/input_device.h" |
| 33 #include "ui/events/devices/touchscreen_device.h" | 32 #include "ui/events/devices/touchscreen_device.h" |
| 34 #include "ui/gfx/x/x11_atom_cache.h" | 33 #include "ui/gfx/x/x11_atom_cache.h" |
| 35 #include "ui/gfx/x/x11_types.h" | 34 #include "ui/gfx/x/x11_types.h" |
| 36 | 35 |
| 37 #ifndef XI_PROP_PRODUCT_ID | 36 #ifndef XI_PROP_PRODUCT_ID |
| 38 #define XI_PROP_PRODUCT_ID "Device Product ID" | 37 #define XI_PROP_PRODUCT_ID "Device Product ID" |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 } | 189 } |
| 191 | 190 |
| 192 base::FilePath GetDevicePath(XDisplay* dpy, const XIDeviceInfo& device) { | 191 base::FilePath GetDevicePath(XDisplay* dpy, const XIDeviceInfo& device) { |
| 193 // Skip the main pointer and keyboard since XOpenDevice() generates a | 192 // Skip the main pointer and keyboard since XOpenDevice() generates a |
| 194 // BadDevice error when passed these devices. | 193 // BadDevice error when passed these devices. |
| 195 if (device.use == XIMasterPointer || device.use == XIMasterKeyboard) | 194 if (device.use == XIMasterPointer || device.use == XIMasterKeyboard) |
| 196 return base::FilePath(); | 195 return base::FilePath(); |
| 197 | 196 |
| 198 // Input device has a property "Device Node" pointing to its dev input node, | 197 // Input device has a property "Device Node" pointing to its dev input node, |
| 199 // e.g. Device Node (250): "/dev/input/event8" | 198 // e.g. Device Node (250): "/dev/input/event8" |
| 200 Atom device_node = ui::X11AtomCache::GetInstance()->GetAtom("Device Node"); | 199 Atom device_node = gfx::GetAtom("Device Node"); |
| 201 if (device_node == None) | 200 if (device_node == None) |
| 202 return base::FilePath(); | 201 return base::FilePath(); |
| 203 | 202 |
| 204 Atom actual_type; | 203 Atom actual_type; |
| 205 int actual_format; | 204 int actual_format; |
| 206 unsigned long nitems, bytes_after; | 205 unsigned long nitems, bytes_after; |
| 207 unsigned char* data; | 206 unsigned char* data; |
| 208 XDevice* dev = XOpenDevice(dpy, device.deviceid); | 207 XDevice* dev = XOpenDevice(dpy, device.deviceid); |
| 209 | 208 |
| 210 // Sometimes XOpenDevice() doesn't return null but the contents aren't valid. | 209 // Sometimes XOpenDevice() doesn't return null but the contents aren't valid. |
| (...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 DeviceType device_types[kMaxDeviceNum]; | 415 DeviceType device_types[kMaxDeviceNum]; |
| 417 for (int i = 0; i < kMaxDeviceNum; ++i) | 416 for (int i = 0; i < kMaxDeviceNum; ++i) |
| 418 device_types[i] = DEVICE_TYPE_OTHER; | 417 device_types[i] = DEVICE_TYPE_OTHER; |
| 419 | 418 |
| 420 for (int i = 0; i < device_list_xi.count; ++i) { | 419 for (int i = 0; i < device_list_xi.count; ++i) { |
| 421 int id = device_list_xi[i].id; | 420 int id = device_list_xi[i].id; |
| 422 if (id < 0 || id >= kMaxDeviceNum) | 421 if (id < 0 || id >= kMaxDeviceNum) |
| 423 continue; | 422 continue; |
| 424 | 423 |
| 425 Atom type = device_list_xi[i].type; | 424 Atom type = device_list_xi[i].type; |
| 426 if (type == GetAtom(XI_KEYBOARD)) | 425 if (type == gfx::GetAtom(XI_KEYBOARD)) |
| 427 device_types[id] = DEVICE_TYPE_KEYBOARD; | 426 device_types[id] = DEVICE_TYPE_KEYBOARD; |
| 428 else if (type == GetAtom(XI_MOUSE)) | 427 else if (type == gfx::GetAtom(XI_MOUSE)) |
| 429 device_types[id] = DEVICE_TYPE_MOUSE; | 428 device_types[id] = DEVICE_TYPE_MOUSE; |
| 430 else if (type == GetAtom(XI_TOUCHPAD)) | 429 else if (type == gfx::GetAtom(XI_TOUCHPAD)) |
| 431 device_types[id] = DEVICE_TYPE_TOUCHPAD; | 430 device_types[id] = DEVICE_TYPE_TOUCHPAD; |
| 432 else if (type == GetAtom(XI_TOUCHSCREEN)) | 431 else if (type == gfx::GetAtom(XI_TOUCHSCREEN)) |
| 433 device_types[id] = DEVICE_TYPE_TOUCHSCREEN; | 432 device_types[id] = DEVICE_TYPE_TOUCHSCREEN; |
| 434 } | 433 } |
| 435 | 434 |
| 436 std::vector<DeviceInfo> device_infos; | 435 std::vector<DeviceInfo> device_infos; |
| 437 for (int i = 0; i < device_list_xi2.count; ++i) { | 436 for (int i = 0; i < device_list_xi2.count; ++i) { |
| 438 const XIDeviceInfo& device = device_list_xi2[i]; | 437 const XIDeviceInfo& device = device_list_xi2[i]; |
| 439 if (!device.enabled || IsTestDevice(device.name)) | 438 if (!device.enabled || IsTestDevice(device.name)) |
| 440 continue; | 439 continue; |
| 441 | 440 |
| 442 DeviceType device_type = | 441 DeviceType device_type = |
| 443 (device.deviceid >= 0 && device.deviceid < kMaxDeviceNum) | 442 (device.deviceid >= 0 && device.deviceid < kMaxDeviceNum) |
| 444 ? device_types[device.deviceid] | 443 ? device_types[device.deviceid] |
| 445 : DEVICE_TYPE_OTHER; | 444 : DEVICE_TYPE_OTHER; |
| 446 | 445 |
| 447 // Obtain the USB-style vendor and product identifiers. | 446 // Obtain the USB-style vendor and product identifiers. |
| 448 // (On Linux, XI2 makes this available for all evdev devices. | 447 // (On Linux, XI2 makes this available for all evdev devices. |
| 449 uint32_t* product_info; | 448 uint32_t* product_info; |
| 450 Atom type; | 449 Atom type; |
| 451 int format_return; | 450 int format_return; |
| 452 unsigned long num_items_return; | 451 unsigned long num_items_return; |
| 453 unsigned long bytes_after_return; | 452 unsigned long bytes_after_return; |
| 454 uint16_t vendor = 0; | 453 uint16_t vendor = 0; |
| 455 uint16_t product = 0; | 454 uint16_t product = 0; |
| 456 if (XIGetProperty(gfx::GetXDisplay(), device.deviceid, | 455 if (XIGetProperty(gfx::GetXDisplay(), device.deviceid, |
| 457 GetAtom(XI_PROP_PRODUCT_ID), 0, 2, 0, XA_INTEGER, &type, | 456 gfx::GetAtom(XI_PROP_PRODUCT_ID), 0, 2, 0, XA_INTEGER, |
| 458 &format_return, &num_items_return, &bytes_after_return, | 457 &type, &format_return, &num_items_return, |
| 458 &bytes_after_return, |
| 459 reinterpret_cast<unsigned char**>(&product_info)) == 0 && | 459 reinterpret_cast<unsigned char**>(&product_info)) == 0 && |
| 460 product_info) { | 460 product_info) { |
| 461 if (num_items_return == 2) { | 461 if (num_items_return == 2) { |
| 462 vendor = product_info[0]; | 462 vendor = product_info[0]; |
| 463 product = product_info[1]; | 463 product = product_info[1]; |
| 464 } | 464 } |
| 465 XFree(product_info); | 465 XFree(product_info); |
| 466 } | 466 } |
| 467 | 467 |
| 468 device_infos.push_back(DeviceInfo( | 468 device_infos.push_back(DeviceInfo( |
| 469 device, device_type, GetDevicePath(display, device), vendor, product)); | 469 device, device_type, GetDevicePath(display, device), vendor, product)); |
| 470 } | 470 } |
| 471 | 471 |
| 472 // X11 is not thread safe, so first get all the required state. | 472 // X11 is not thread safe, so first get all the required state. |
| 473 DisplayState display_state; | 473 DisplayState display_state; |
| 474 display_state.mt_position_x = GetAtom("Abs MT Position X"); | 474 display_state.mt_position_x = gfx::GetAtom("Abs MT Position X"); |
| 475 display_state.mt_position_y = GetAtom("Abs MT Position Y"); | 475 display_state.mt_position_y = gfx::GetAtom("Abs MT Position Y"); |
| 476 | 476 |
| 477 UiCallbacks callbacks; | 477 UiCallbacks callbacks; |
| 478 callbacks.keyboard_callback = base::Bind(&OnKeyboardDevices); | 478 callbacks.keyboard_callback = base::Bind(&OnKeyboardDevices); |
| 479 callbacks.touchscreen_callback = base::Bind(&OnTouchscreenDevices); | 479 callbacks.touchscreen_callback = base::Bind(&OnTouchscreenDevices); |
| 480 callbacks.mouse_callback = base::Bind(&OnMouseDevices); | 480 callbacks.mouse_callback = base::Bind(&OnMouseDevices); |
| 481 callbacks.touchpad_callback = base::Bind(&OnTouchpadDevices); | 481 callbacks.touchpad_callback = base::Bind(&OnTouchpadDevices); |
| 482 callbacks.hotplug_finished_callback = base::Bind(&OnHotplugFinished); | 482 callbacks.hotplug_finished_callback = base::Bind(&OnHotplugFinished); |
| 483 | 483 |
| 484 // Parsing the device information may block, so delegate the operation to a | 484 // Parsing the device information may block, so delegate the operation to a |
| 485 // worker thread. Once the device information is extracted the parsed devices | 485 // worker thread. Once the device information is extracted the parsed devices |
| 486 // will be returned via the callbacks. | 486 // will be returned via the callbacks. |
| 487 base::WorkerPool::PostTask( | 487 base::WorkerPool::PostTask( |
| 488 FROM_HERE, | 488 FROM_HERE, |
| 489 base::Bind(&HandleHotplugEventInWorker, device_infos, display_state, | 489 base::Bind(&HandleHotplugEventInWorker, device_infos, display_state, |
| 490 base::ThreadTaskRunnerHandle::Get(), callbacks), | 490 base::ThreadTaskRunnerHandle::Get(), callbacks), |
| 491 true /* task_is_slow */); | 491 true /* task_is_slow */); |
| 492 } | 492 } |
| 493 | 493 |
| 494 } // namespace ui | 494 } // namespace ui |
| OLD | NEW |