Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 "ash/wm/maximize_mode/scoped_disable_internal_mouse_and_keyboard_x11.h" | |
| 6 | |
| 7 #include <set> | |
| 8 #include <X11/extensions/XInput2.h> | |
| 9 #include <X11/Xlib.h> | |
| 10 | |
| 11 #include "ash/display/display_controller.h" | |
| 12 #include "ash/screen_util.h" | |
| 13 #include "ash/shell.h" | |
| 14 #include "base/memory/scoped_ptr.h" | |
| 15 #include "base/strings/string_util.h" | |
| 16 #include "ui/aura/client/screen_position_client.h" | |
| 17 #include "ui/aura/env.h" | |
| 18 #include "ui/aura/window.h" | |
| 19 #include "ui/aura/window_event_dispatcher.h" | |
| 20 #include "ui/aura/window_tree_host.h" | |
| 21 #include "ui/events/event.h" | |
| 22 #include "ui/events/event_utils.h" | |
| 23 #include "ui/events/keycodes/keyboard_codes_posix.h" | |
| 24 #include "ui/events/platform/platform_event_source.h" | |
| 25 #include "ui/events/x/device_data_manager.h" | |
| 26 #include "ui/events/x/device_list_cache_x.h" | |
| 27 #include "ui/gfx/x/x11_types.h" | |
| 28 | |
| 29 namespace ash { | |
| 30 | |
| 31 namespace { | |
| 32 | |
| 33 // The name of the xinput device corresponding to the internal touchpad. | |
| 34 const char kInternalTouchpadName[] = "Elan Touchpad"; | |
| 35 | |
| 36 // The name of the xinput device corresponding to the internal keyboard. | |
| 37 const char kInternalKeyboardName[] = "AT Translated Set 2 keyboard"; | |
| 38 | |
| 39 gfx::Point GetMouseLocationInScreen() { | |
| 40 return aura::Env::GetInstance()->last_mouse_location(); | |
| 41 } | |
| 42 | |
| 43 void SetMouseLocationInScreen(const gfx::Point& screen_location) { | |
| 44 gfx::Display display = ash::ScreenUtil::FindDisplayContainingPoint( | |
| 45 screen_location); | |
| 46 if (!display.is_valid()) | |
| 47 return; | |
| 48 aura::Window* root_window = Shell::GetInstance()->display_controller()-> | |
| 49 GetRootWindowForDisplayId(display.id()); | |
| 50 gfx::Point host_location(screen_location); | |
| 51 aura::client::ScreenPositionClient* client = | |
| 52 aura::client::GetScreenPositionClient(root_window); | |
| 53 if (client) | |
| 54 client->ConvertPointFromScreen(root_window, &host_location); | |
| 55 root_window->GetHost()->MoveCursorTo(host_location); | |
| 56 } | |
| 57 | |
| 58 } // namespace | |
| 59 | |
| 60 ScopedDisableInternalMouseAndKeyboardX11:: | |
| 61 ScopedDisableInternalMouseAndKeyboardX11() | |
| 62 : touchpad_device_id_(0), | |
| 63 keyboard_device_id_(0), | |
|
flackr
2014/06/11 22:16:47
I believe 0 is a valid device id, I will default t
flackr
2014/06/18 05:28:31
Done.
| |
| 64 last_mouse_location_(GetMouseLocationInScreen()) { | |
| 65 | |
| 66 ui::DeviceDataManager* device_data_manager = | |
| 67 ui::DeviceDataManager::GetInstance(); | |
| 68 if (device_data_manager->IsXInput2Available()) { | |
| 69 XIDeviceList xi_dev_list = ui::DeviceListCacheX::GetInstance()-> | |
| 70 GetXI2DeviceList(gfx::GetXDisplay()); | |
| 71 for (int i = 0; i < xi_dev_list.count; ++i) { | |
| 72 std::string device_name(xi_dev_list[i].name); | |
| 73 base::TrimWhitespaceASCII(device_name, base::TRIM_TRAILING, &device_name); | |
| 74 if (device_name == kInternalTouchpadName) { | |
| 75 touchpad_device_id_ = xi_dev_list[i].deviceid; | |
| 76 device_data_manager->DisableDevice(touchpad_device_id_); | |
| 77 } else if (device_name == kInternalKeyboardName) { | |
| 78 keyboard_device_id_ = xi_dev_list[i].deviceid; | |
| 79 device_data_manager->DisableDevice(keyboard_device_id_); | |
| 80 } | |
| 81 } | |
| 82 } | |
| 83 // Allow the accessible keys present on the side of some devices to continue | |
| 84 // working. | |
| 85 scoped_ptr<std::set<ui::KeyboardCode> > excepted_keys( | |
| 86 new std::set<ui::KeyboardCode>); | |
| 87 excepted_keys->insert(ui::VKEY_VOLUME_DOWN); | |
| 88 excepted_keys->insert(ui::VKEY_VOLUME_UP); | |
| 89 excepted_keys->insert(ui::VKEY_POWER); | |
| 90 device_data_manager->DisableKeyboard(excepted_keys.Pass()); | |
| 91 ui::PlatformEventSource::GetInstance()->AddPlatformEventObserver(this); | |
| 92 } | |
| 93 | |
| 94 ScopedDisableInternalMouseAndKeyboardX11:: | |
| 95 ~ScopedDisableInternalMouseAndKeyboardX11() { | |
| 96 ui::DeviceDataManager* device_data_manager = | |
| 97 ui::DeviceDataManager::GetInstance(); | |
| 98 if (touchpad_device_id_) | |
| 99 device_data_manager->EnableDevice(touchpad_device_id_); | |
| 100 if (keyboard_device_id_) | |
| 101 device_data_manager->EnableDevice(keyboard_device_id_); | |
| 102 device_data_manager->EnableKeyboard(); | |
| 103 ui::PlatformEventSource::GetInstance()->RemovePlatformEventObserver(this); | |
| 104 } | |
| 105 | |
| 106 void ScopedDisableInternalMouseAndKeyboardX11::WillProcessEvent( | |
| 107 const ui::PlatformEvent& event) { | |
| 108 } | |
| 109 | |
| 110 void ScopedDisableInternalMouseAndKeyboardX11::DidProcessEvent( | |
| 111 const ui::PlatformEvent& event) { | |
| 112 if (event->type != GenericEvent) | |
| 113 return; | |
| 114 XIDeviceEvent* xievent = | |
| 115 static_cast<XIDeviceEvent*>(event->xcookie.data); | |
| 116 ui::DeviceDataManager* device_data_manager = | |
| 117 ui::DeviceDataManager::GetInstance(); | |
| 118 if (xievent->evtype != XI_Motion || | |
| 119 device_data_manager->IsFlingEvent(event) || | |
| 120 device_data_manager->IsScrollEvent(event) || | |
| 121 device_data_manager->IsCMTMetricsEvent(event)) { | |
| 122 return; | |
| 123 } | |
| 124 if (xievent->sourceid == touchpad_device_id_) { | |
| 125 // The cursor will have already moved even though the move event will be | |
| 126 // blocked. Move the mouse cursor back to its last known location resulting | |
| 127 // from an external mouse to prevent the internal touchpad from moving it. | |
| 128 SetMouseLocationInScreen(last_mouse_location_); | |
| 129 } else { | |
| 130 // Track the last location seen from an external mouse event. | |
| 131 last_mouse_location_ = GetMouseLocationInScreen(); | |
| 132 } | |
| 133 } | |
| 134 | |
| 135 } // namespace ash | |
| OLD | NEW |