David Padlipsky | 5eaaade | 2023-07-21 22:39:38 | [diff] [blame] | 1 | // Copyright 2023 The Chromium Authors |
| 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/events/peripheral_customization_event_rewriter.h" |
| 6 | |
David Padlipsky | b7896da | 2023-10-25 19:45:46 | [diff] [blame] | 7 | #include <linux/input.h> |
| 8 | |
David Padlipsky | cc7c2de | 2023-08-23 17:16:54 | [diff] [blame] | 9 | #include <memory> |
| 10 | |
David Padlipsky | f873095 | 2023-09-01 19:38:27 | [diff] [blame] | 11 | #include "ash/accelerators/accelerator_controller_impl.h" |
David Padlipsky | 5eaaade | 2023-07-21 22:39:38 | [diff] [blame] | 12 | #include "ash/constants/ash_features.h" |
David Padlipsky | 5b39dc0 | 2024-01-03 20:36:48 | [diff] [blame] | 13 | #include "ash/public/cpp/accelerators_util.h" |
David Padlipsky | 3b55bb1 | 2023-09-05 22:24:57 | [diff] [blame] | 14 | #include "ash/public/mojom/input_device_settings.mojom-forward.h" |
David Padlipsky | b5a52cd1 | 2023-08-08 21:45:19 | [diff] [blame] | 15 | #include "ash/public/mojom/input_device_settings.mojom-shared.h" |
| 16 | #include "ash/public/mojom/input_device_settings.mojom.h" |
David Padlipsky | f873095 | 2023-09-01 19:38:27 | [diff] [blame] | 17 | #include "ash/shell.h" |
David Padlipsky | f9728f27 | 2023-09-06 16:50:36 | [diff] [blame] | 18 | #include "ash/system/input_device_settings/input_device_settings_controller_impl.h" |
David Padlipsky | 5eaaade | 2023-07-21 22:39:38 | [diff] [blame] | 19 | #include "base/check.h" |
David Padlipsky | d664e67 | 2023-10-25 21:32:15 | [diff] [blame] | 20 | #include "base/containers/fixed_flat_map.h" |
David Padlipsky | b5a52cd1 | 2023-08-08 21:45:19 | [diff] [blame] | 21 | #include "base/notreached.h" |
David Padlipsky | cc7c2de | 2023-08-23 17:16:54 | [diff] [blame] | 22 | #include "base/ranges/algorithm.h" |
Jimmy | b36a877 | 2023-11-10 19:41:39 | [diff] [blame] | 23 | #include "ui/base/ui_base_features.h" |
David Padlipsky | d664e67 | 2023-10-25 21:32:15 | [diff] [blame] | 24 | #include "ui/display/screen.h" |
David Padlipsky | b5a52cd1 | 2023-08-08 21:45:19 | [diff] [blame] | 25 | #include "ui/events/event.h" |
| 26 | #include "ui/events/event_constants.h" |
| 27 | #include "ui/events/event_dispatcher.h" |
David Padlipsky | d664e67 | 2023-10-25 21:32:15 | [diff] [blame] | 28 | #include "ui/events/event_utils.h" |
David Padlipsky | cc7c2de | 2023-08-23 17:16:54 | [diff] [blame] | 29 | #include "ui/events/keycodes/dom/dom_code.h" |
| 30 | #include "ui/events/keycodes/dom/dom_key.h" |
| 31 | #include "ui/events/keycodes/keyboard_codes_posix.h" |
David Padlipsky | b7896da | 2023-10-25 19:45:46 | [diff] [blame] | 32 | #include "ui/events/ozone/evdev/mouse_button_property.h" |
David Padlipsky | 5b39dc0 | 2024-01-03 20:36:48 | [diff] [blame] | 33 | #include "ui/events/ozone/layout/keyboard_layout_engine.h" |
| 34 | #include "ui/events/ozone/layout/keyboard_layout_engine_manager.h" |
David Padlipsky | b5a52cd1 | 2023-08-08 21:45:19 | [diff] [blame] | 35 | #include "ui/events/types/event_type.h" |
David Padlipsky | d664e67 | 2023-10-25 21:32:15 | [diff] [blame] | 36 | #include "ui/gfx/geometry/point_f.h" |
David Padlipsky | 5eaaade | 2023-07-21 22:39:38 | [diff] [blame] | 37 | |
| 38 | namespace ash { |
| 39 | |
David Padlipsky | b5a52cd1 | 2023-08-08 21:45:19 | [diff] [blame] | 40 | namespace { |
| 41 | |
| 42 | constexpr int kMouseRemappableFlags = ui::EF_BACK_MOUSE_BUTTON | |
| 43 | ui::EF_FORWARD_MOUSE_BUTTON | |
| 44 | ui::EF_MIDDLE_MOUSE_BUTTON; |
| 45 | |
| 46 | constexpr int kGraphicsTabletRemappableFlags = |
| 47 | ui::EF_RIGHT_MOUSE_BUTTON | ui::EF_BACK_MOUSE_BUTTON | |
| 48 | ui::EF_FORWARD_MOUSE_BUTTON | ui::EF_MIDDLE_MOUSE_BUTTON; |
| 49 | |
David Padlipsky | d664e67 | 2023-10-25 21:32:15 | [diff] [blame] | 50 | constexpr auto kStaticActionToMouseButtonFlag = |
| 51 | base::MakeFixedFlatMap<mojom::StaticShortcutAction, ui::EventFlags>({ |
| 52 | {mojom::StaticShortcutAction::kLeftClick, ui::EF_LEFT_MOUSE_BUTTON}, |
| 53 | {mojom::StaticShortcutAction::kRightClick, ui::EF_RIGHT_MOUSE_BUTTON}, |
| 54 | {mojom::StaticShortcutAction::kMiddleClick, ui::EF_MIDDLE_MOUSE_BUTTON}, |
| 55 | }); |
| 56 | |
David Padlipsky | 74935f59 | 2024-02-02 00:24:41 | [diff] [blame^] | 57 | constexpr std::string_view ToMetricsString( |
YuhanYang | 92cbe159 | 2023-12-05 02:13:29 | [diff] [blame] | 58 | PeripheralCustomizationEventRewriter::PeripheralCustomizationMetricsType |
| 59 | peripheral_kind) { |
| 60 | switch (peripheral_kind) { |
| 61 | case PeripheralCustomizationEventRewriter:: |
| 62 | PeripheralCustomizationMetricsType::kMouse: |
David Padlipsky | 74935f59 | 2024-02-02 00:24:41 | [diff] [blame^] | 63 | return "Mouse"; |
YuhanYang | 92cbe159 | 2023-12-05 02:13:29 | [diff] [blame] | 64 | case PeripheralCustomizationEventRewriter:: |
| 65 | PeripheralCustomizationMetricsType::kGraphicsTablet: |
David Padlipsky | 74935f59 | 2024-02-02 00:24:41 | [diff] [blame^] | 66 | return "GraphicsTablet"; |
YuhanYang | 92cbe159 | 2023-12-05 02:13:29 | [diff] [blame] | 67 | case PeripheralCustomizationEventRewriter:: |
| 68 | PeripheralCustomizationMetricsType::kGraphicsTabletPen: |
David Padlipsky | 74935f59 | 2024-02-02 00:24:41 | [diff] [blame^] | 69 | return "GraphicsTabletPen"; |
YuhanYang | 92cbe159 | 2023-12-05 02:13:29 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
Danny Wang | d388f6b | 2023-09-29 00:06:50 | [diff] [blame] | 73 | mojom::KeyEvent GetStaticShortcutAction(mojom::StaticShortcutAction action) { |
Danny Wang | 6bdb558 | 2023-09-18 21:27:29 | [diff] [blame] | 74 | mojom::KeyEvent key_event; |
| 75 | switch (action) { |
Danny Wang | 75a9a86f | 2023-09-29 21:00:30 | [diff] [blame] | 76 | case mojom::StaticShortcutAction::kDisable: |
David Padlipsky | d664e67 | 2023-10-25 21:32:15 | [diff] [blame] | 77 | case mojom::StaticShortcutAction::kLeftClick: |
| 78 | case mojom::StaticShortcutAction::kRightClick: |
| 79 | case mojom::StaticShortcutAction::kMiddleClick: |
Danny Wang | 75a9a86f | 2023-09-29 21:00:30 | [diff] [blame] | 80 | NOTREACHED_NORETURN(); |
Danny Wang | d388f6b | 2023-09-29 00:06:50 | [diff] [blame] | 81 | case mojom::StaticShortcutAction::kCopy: |
Danny Wang | 6bdb558 | 2023-09-18 21:27:29 | [diff] [blame] | 82 | key_event = mojom::KeyEvent( |
| 83 | ui::VKEY_C, static_cast<int>(ui::DomCode::US_C), |
| 84 | static_cast<int>(ui::DomKey::Constant<'c'>::Character), |
David Padlipsky | fc60da89 | 2023-10-27 00:14:31 | [diff] [blame] | 85 | ui::EF_CONTROL_DOWN, /*key_display=*/""); |
Danny Wang | 6bdb558 | 2023-09-18 21:27:29 | [diff] [blame] | 86 | break; |
Danny Wang | d388f6b | 2023-09-29 00:06:50 | [diff] [blame] | 87 | case mojom::StaticShortcutAction::kPaste: |
Danny Wang | 6bdb558 | 2023-09-18 21:27:29 | [diff] [blame] | 88 | key_event = mojom::KeyEvent( |
| 89 | ui::VKEY_V, static_cast<int>(ui::DomCode::US_V), |
| 90 | static_cast<int>(ui::DomKey::Constant<'v'>::Character), |
David Padlipsky | fc60da89 | 2023-10-27 00:14:31 | [diff] [blame] | 91 | ui::EF_CONTROL_DOWN, /*key_display=*/""); |
Danny Wang | 6bdb558 | 2023-09-18 21:27:29 | [diff] [blame] | 92 | break; |
Danny Wang | 0dfd3b0 | 2023-10-19 01:36:02 | [diff] [blame] | 93 | case mojom::StaticShortcutAction::kUndo: |
| 94 | key_event = mojom::KeyEvent( |
| 95 | ui::VKEY_Z, static_cast<int>(ui::DomCode::US_Z), |
| 96 | static_cast<int>(ui::DomKey::Constant<'z'>::Character), |
David Padlipsky | fc60da89 | 2023-10-27 00:14:31 | [diff] [blame] | 97 | ui::EF_CONTROL_DOWN, /*key_display=*/""); |
Danny Wang | 0dfd3b0 | 2023-10-19 01:36:02 | [diff] [blame] | 98 | break; |
| 99 | case mojom::StaticShortcutAction::kRedo: |
| 100 | key_event = mojom::KeyEvent( |
| 101 | ui::VKEY_Z, static_cast<int>(ui::DomCode::US_Z), |
| 102 | static_cast<int>(ui::DomKey::Constant<'z'>::Character), |
David Padlipsky | fc60da89 | 2023-10-27 00:14:31 | [diff] [blame] | 103 | ui::EF_CONTROL_DOWN | ui::EF_SHIFT_DOWN, /*key_display=*/""); |
Danny Wang | 0dfd3b0 | 2023-10-19 01:36:02 | [diff] [blame] | 104 | break; |
| 105 | case mojom::StaticShortcutAction::kZoomIn: |
| 106 | key_event = mojom::KeyEvent( |
| 107 | ui::VKEY_OEM_PLUS, static_cast<int>(ui::DomCode::EQUAL), |
| 108 | static_cast<int>(ui::DomKey::Constant<'='>::Character), |
David Padlipsky | fc60da89 | 2023-10-27 00:14:31 | [diff] [blame] | 109 | ui::EF_CONTROL_DOWN, /*key_display=*/""); |
Danny Wang | 0dfd3b0 | 2023-10-19 01:36:02 | [diff] [blame] | 110 | break; |
| 111 | case mojom::StaticShortcutAction::kZoomOut: |
| 112 | key_event = mojom::KeyEvent( |
| 113 | ui::VKEY_OEM_MINUS, static_cast<int>(ui::DomCode::MINUS), |
| 114 | static_cast<int>(ui::DomKey::Constant<'-'>::Character), |
David Padlipsky | fc60da89 | 2023-10-27 00:14:31 | [diff] [blame] | 115 | ui::EF_CONTROL_DOWN, /*key_display=*/""); |
Danny Wang | 0dfd3b0 | 2023-10-19 01:36:02 | [diff] [blame] | 116 | break; |
| 117 | case mojom::StaticShortcutAction::kPreviousPage: |
David Padlipsky | fc60da89 | 2023-10-27 00:14:31 | [diff] [blame] | 118 | key_event = mojom::KeyEvent(ui::VKEY_BROWSER_BACK, |
| 119 | static_cast<int>(ui::DomCode::BROWSER_BACK), |
| 120 | static_cast<int>(ui::DomKey::BROWSER_BACK), |
| 121 | ui::EF_NONE, /*key_display=*/""); |
Danny Wang | 0dfd3b0 | 2023-10-19 01:36:02 | [diff] [blame] | 122 | break; |
| 123 | case mojom::StaticShortcutAction::kNextPage: |
David Padlipsky | fc60da89 | 2023-10-27 00:14:31 | [diff] [blame] | 124 | key_event = |
| 125 | mojom::KeyEvent(ui::VKEY_BROWSER_FORWARD, |
| 126 | static_cast<int>(ui::DomCode::BROWSER_FORWARD), |
| 127 | static_cast<int>(ui::DomKey::BROWSER_FORWARD), |
| 128 | ui::EF_NONE, /*key_display=*/""); |
Danny Wang | 0dfd3b0 | 2023-10-19 01:36:02 | [diff] [blame] | 129 | break; |
Danny Wang | 6bdb558 | 2023-09-18 21:27:29 | [diff] [blame] | 130 | } |
| 131 | return key_event; |
| 132 | } |
| 133 | |
David Padlipsky | 3394af9 | 2023-10-27 21:30:50 | [diff] [blame] | 134 | int ConvertKeyCodeToFlags(ui::KeyboardCode key_code) { |
| 135 | switch (key_code) { |
| 136 | case ui::VKEY_LWIN: |
| 137 | case ui::VKEY_RWIN: |
| 138 | return ui::EF_COMMAND_DOWN; |
| 139 | case ui::VKEY_CONTROL: |
David Padlipsky | 5b39dc0 | 2024-01-03 20:36:48 | [diff] [blame] | 140 | case ui::VKEY_RCONTROL: |
David Padlipsky | 3394af9 | 2023-10-27 21:30:50 | [diff] [blame] | 141 | return ui::EF_CONTROL_DOWN; |
| 142 | case ui::VKEY_SHIFT: |
| 143 | case ui::VKEY_LSHIFT: |
| 144 | case ui::VKEY_RSHIFT: |
| 145 | return ui::EF_SHIFT_DOWN; |
| 146 | case ui::VKEY_MENU: |
| 147 | case ui::VKEY_RMENU: |
| 148 | return ui::EF_ALT_DOWN; |
| 149 | default: |
| 150 | return ui::EF_NONE; |
| 151 | } |
| 152 | } |
| 153 | |
Danny Wang | 6bdb558 | 2023-09-18 21:27:29 | [diff] [blame] | 154 | std::unique_ptr<ui::Event> RewriteEventToKeyEvent( |
| 155 | const ui::Event& event, |
| 156 | const mojom::KeyEvent& key_event) { |
| 157 | const ui::EventType event_type = (event.type() == ui::ET_MOUSE_PRESSED || |
| 158 | event.type() == ui::ET_KEY_PRESSED) |
| 159 | ? ui::ET_KEY_PRESSED |
| 160 | : ui::ET_KEY_RELEASED; |
David Padlipsky | 3394af9 | 2023-10-27 21:30:50 | [diff] [blame] | 161 | int flags_to_apply = key_event.modifiers; |
| 162 | // Do not apply modifier flags when the key is a modifier and it is a release |
| 163 | // event. Modifier keys do not apply their flag on release. |
| 164 | if (event_type == ui::ET_KEY_RELEASED && |
| 165 | key_event.modifiers == |
| 166 | static_cast<uint32_t>(ConvertKeyCodeToFlags(key_event.vkey))) { |
| 167 | flags_to_apply = ui::EF_NONE; |
| 168 | } |
Danny Wang | 6bdb558 | 2023-09-18 21:27:29 | [diff] [blame] | 169 | auto rewritten_event = std::make_unique<ui::KeyEvent>( |
| 170 | event_type, key_event.vkey, static_cast<ui::DomCode>(key_event.dom_code), |
David Padlipsky | 3394af9 | 2023-10-27 21:30:50 | [diff] [blame] | 171 | flags_to_apply | event.flags(), |
Danny Wang | 6bdb558 | 2023-09-18 21:27:29 | [diff] [blame] | 172 | static_cast<ui::DomKey>(key_event.dom_key), event.time_stamp()); |
| 173 | rewritten_event->set_source_device_id(event.source_device_id()); |
| 174 | return rewritten_event; |
| 175 | } |
| 176 | |
David Padlipsky | d664e67 | 2023-10-25 21:32:15 | [diff] [blame] | 177 | std::unique_ptr<ui::Event> RewriteEventToMouseButtonEvent( |
| 178 | const ui::Event& event, |
| 179 | mojom::StaticShortcutAction action) { |
| 180 | auto* flag_iter = kStaticActionToMouseButtonFlag.find(action); |
| 181 | CHECK(flag_iter != kStaticActionToMouseButtonFlag.end()); |
| 182 | const int characteristic_flag = flag_iter->second; |
| 183 | |
| 184 | auto* screen = display::Screen::GetScreen(); |
| 185 | CHECK(screen); |
David Padlipsky | 3394af9 | 2023-10-27 21:30:50 | [diff] [blame] | 186 | auto display = screen->GetDisplayNearestPoint(screen->GetCursorScreenPoint()); |
| 187 | const gfx::PointF location = |
| 188 | gfx::ScalePoint(gfx::PointF(screen->GetCursorScreenPoint() - |
| 189 | display.bounds().origin().OffsetFromOrigin()), |
| 190 | display.device_scale_factor()); |
David Padlipsky | d664e67 | 2023-10-25 21:32:15 | [diff] [blame] | 191 | |
| 192 | const ui::EventType type = (event.type() == ui::ET_MOUSE_PRESSED || |
| 193 | event.type() == ui::ET_KEY_PRESSED) |
| 194 | ? ui::ET_MOUSE_PRESSED |
| 195 | : ui::ET_MOUSE_RELEASED; |
| 196 | auto rewritten_event = std::make_unique<ui::MouseEvent>( |
| 197 | type, location, location, event.time_stamp(), |
| 198 | event.flags() | characteristic_flag, characteristic_flag); |
| 199 | rewritten_event->set_source_device_id(event.source_device_id()); |
| 200 | return rewritten_event; |
| 201 | } |
| 202 | |
David Padlipsky | b5a52cd1 | 2023-08-08 21:45:19 | [diff] [blame] | 203 | bool IsMouseButtonEvent(const ui::MouseEvent& mouse_event) { |
| 204 | return mouse_event.type() == ui::ET_MOUSE_PRESSED || |
| 205 | mouse_event.type() == ui::ET_MOUSE_RELEASED; |
| 206 | } |
| 207 | |
| 208 | bool IsMouseRemappableButton(int flags) { |
| 209 | return (flags & kMouseRemappableFlags) != 0; |
| 210 | } |
| 211 | |
| 212 | bool IsGraphicsTabletRemappableButton(int flags) { |
| 213 | return (flags & kGraphicsTabletRemappableFlags) != 0; |
| 214 | } |
| 215 | |
| 216 | int GetRemappableMouseEventFlags( |
| 217 | PeripheralCustomizationEventRewriter::DeviceType device_type) { |
| 218 | switch (device_type) { |
| 219 | case PeripheralCustomizationEventRewriter::DeviceType::kMouse: |
| 220 | return kMouseRemappableFlags; |
| 221 | case PeripheralCustomizationEventRewriter::DeviceType::kGraphicsTablet: |
| 222 | return kGraphicsTabletRemappableFlags; |
| 223 | } |
| 224 | } |
| 225 | |
David Padlipsky | b7896da | 2023-10-25 19:45:46 | [diff] [blame] | 226 | mojom::ButtonPtr GetButtonFromMouseEvent(const ui::MouseEvent& mouse_event) { |
| 227 | switch (mouse_event.changed_button_flags()) { |
David Padlipsky | b5a52cd1 | 2023-08-08 21:45:19 | [diff] [blame] | 228 | case ui::EF_LEFT_MOUSE_BUTTON: |
| 229 | return mojom::Button::NewCustomizableButton( |
| 230 | mojom::CustomizableButton::kLeft); |
| 231 | case ui::EF_RIGHT_MOUSE_BUTTON: |
| 232 | return mojom::Button::NewCustomizableButton( |
| 233 | mojom::CustomizableButton::kRight); |
| 234 | case ui::EF_MIDDLE_MOUSE_BUTTON: |
| 235 | return mojom::Button::NewCustomizableButton( |
| 236 | mojom::CustomizableButton::kMiddle); |
| 237 | case ui::EF_FORWARD_MOUSE_BUTTON: |
David Padlipsky | b7896da | 2023-10-25 19:45:46 | [diff] [blame] | 238 | case ui::EF_BACK_MOUSE_BUTTON: |
| 239 | break; |
| 240 | } |
| 241 | |
| 242 | CHECK(mouse_event.changed_button_flags() == ui::EF_FORWARD_MOUSE_BUTTON || |
| 243 | mouse_event.changed_button_flags() == ui::EF_BACK_MOUSE_BUTTON); |
| 244 | auto linux_key_code = ui::GetForwardBackMouseButtonProperty(mouse_event); |
| 245 | if (!linux_key_code) { |
| 246 | return (mouse_event.changed_button_flags() == ui::EF_FORWARD_MOUSE_BUTTON) |
| 247 | ? mojom::Button::NewCustomizableButton( |
| 248 | mojom::CustomizableButton::kForward) |
| 249 | : mojom::Button::NewCustomizableButton( |
| 250 | mojom::CustomizableButton::kBack); |
| 251 | } |
| 252 | |
| 253 | switch (*linux_key_code) { |
| 254 | case BTN_FORWARD: |
David Padlipsky | b5a52cd1 | 2023-08-08 21:45:19 | [diff] [blame] | 255 | return mojom::Button::NewCustomizableButton( |
| 256 | mojom::CustomizableButton::kForward); |
David Padlipsky | b7896da | 2023-10-25 19:45:46 | [diff] [blame] | 257 | case BTN_BACK: |
David Padlipsky | b5a52cd1 | 2023-08-08 21:45:19 | [diff] [blame] | 258 | return mojom::Button::NewCustomizableButton( |
| 259 | mojom::CustomizableButton::kBack); |
David Padlipsky | b7896da | 2023-10-25 19:45:46 | [diff] [blame] | 260 | case BTN_SIDE: |
| 261 | return mojom::Button::NewCustomizableButton( |
| 262 | mojom::CustomizableButton::kSide); |
| 263 | case BTN_EXTRA: |
| 264 | return mojom::Button::NewCustomizableButton( |
| 265 | mojom::CustomizableButton::kExtra); |
David Padlipsky | b5a52cd1 | 2023-08-08 21:45:19 | [diff] [blame] | 266 | } |
David Padlipsky | b7896da | 2023-10-25 19:45:46 | [diff] [blame] | 267 | |
David Padlipsky | b5a52cd1 | 2023-08-08 21:45:19 | [diff] [blame] | 268 | NOTREACHED_NORETURN(); |
| 269 | } |
| 270 | |
David Padlipsky | cc7c2de | 2023-08-23 17:16:54 | [diff] [blame] | 271 | int ConvertButtonToFlags(const mojom::Button& button) { |
| 272 | if (button.is_customizable_button()) { |
| 273 | switch (button.get_customizable_button()) { |
| 274 | case mojom::CustomizableButton::kLeft: |
| 275 | return ui::EF_LEFT_MOUSE_BUTTON; |
| 276 | case mojom::CustomizableButton::kRight: |
| 277 | return ui::EF_RIGHT_MOUSE_BUTTON; |
| 278 | case mojom::CustomizableButton::kMiddle: |
| 279 | return ui::EF_MIDDLE_MOUSE_BUTTON; |
| 280 | case mojom::CustomizableButton::kForward: |
David Padlipsky | cc7c2de | 2023-08-23 17:16:54 | [diff] [blame] | 281 | case mojom::CustomizableButton::kExtra: |
| 282 | return ui::EF_FORWARD_MOUSE_BUTTON; |
David Padlipsky | b7896da | 2023-10-25 19:45:46 | [diff] [blame] | 283 | case mojom::CustomizableButton::kBack: |
David Padlipsky | cc7c2de | 2023-08-23 17:16:54 | [diff] [blame] | 284 | case mojom::CustomizableButton::kSide: |
| 285 | return ui::EF_BACK_MOUSE_BUTTON; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | if (button.is_vkey()) { |
David Padlipsky | 3b55bb1 | 2023-09-05 22:24:57 | [diff] [blame] | 290 | return ConvertKeyCodeToFlags(button.get_vkey()); |
David Padlipsky | cc7c2de | 2023-08-23 17:16:54 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | return ui::EF_NONE; |
| 294 | } |
| 295 | |
YuhanYang | 92cbe159 | 2023-12-05 02:13:29 | [diff] [blame] | 296 | std::optional<PeripheralCustomizationEventRewriter::RemappingActionResult> |
| 297 | GetRemappingActionFromMouseSettings(const mojom::Button& button, |
| 298 | const mojom::MouseSettings& settings) { |
David Padlipsky | f9728f27 | 2023-09-06 16:50:36 | [diff] [blame] | 299 | const auto button_remapping_iter = base::ranges::find( |
| 300 | settings.button_remappings, button, |
| 301 | [](const mojom::ButtonRemappingPtr& entry) { return *entry->button; }); |
| 302 | if (button_remapping_iter != settings.button_remappings.end()) { |
David Padlipsky | 1eb6f0ae | 2024-01-08 18:49:21 | [diff] [blame] | 303 | const mojom::ButtonRemapping& button_remapping = *(*button_remapping_iter); |
| 304 | if (!button_remapping.remapping_action) { |
| 305 | return std::nullopt; |
| 306 | } |
| 307 | |
YuhanYang | 92cbe159 | 2023-12-05 02:13:29 | [diff] [blame] | 308 | auto result = PeripheralCustomizationEventRewriter::RemappingActionResult( |
David Padlipsky | 1eb6f0ae | 2024-01-08 18:49:21 | [diff] [blame] | 309 | *button_remapping.remapping_action, |
YuhanYang | 92cbe159 | 2023-12-05 02:13:29 | [diff] [blame] | 310 | PeripheralCustomizationEventRewriter:: |
| 311 | PeripheralCustomizationMetricsType::kMouse); |
David Padlipsky | 1eb6f0ae | 2024-01-08 18:49:21 | [diff] [blame] | 312 | return result; |
David Padlipsky | f9728f27 | 2023-09-06 16:50:36 | [diff] [blame] | 313 | } |
| 314 | |
David Padlipsky | 1eb6f0ae | 2024-01-08 18:49:21 | [diff] [blame] | 315 | return std::nullopt; |
David Padlipsky | f9728f27 | 2023-09-06 16:50:36 | [diff] [blame] | 316 | } |
| 317 | |
YuhanYang | 92cbe159 | 2023-12-05 02:13:29 | [diff] [blame] | 318 | std::optional<PeripheralCustomizationEventRewriter::RemappingActionResult> |
| 319 | GetRemappingActionFromGraphicsTabletSettings( |
David Padlipsky | f9728f27 | 2023-09-06 16:50:36 | [diff] [blame] | 320 | const mojom::Button& button, |
| 321 | const mojom::GraphicsTabletSettings& settings) { |
| 322 | const auto pen_button_remapping_iter = base::ranges::find( |
| 323 | settings.pen_button_remappings, button, |
| 324 | [](const mojom::ButtonRemappingPtr& entry) { return *entry->button; }); |
| 325 | if (pen_button_remapping_iter != settings.pen_button_remappings.end()) { |
David Padlipsky | 1eb6f0ae | 2024-01-08 18:49:21 | [diff] [blame] | 326 | const mojom::ButtonRemapping& button_remapping = |
| 327 | *(*pen_button_remapping_iter); |
| 328 | if (!button_remapping.remapping_action) { |
| 329 | return std::nullopt; |
| 330 | } |
| 331 | |
YuhanYang | 92cbe159 | 2023-12-05 02:13:29 | [diff] [blame] | 332 | auto pen_action = |
| 333 | PeripheralCustomizationEventRewriter::RemappingActionResult( |
David Padlipsky | 1eb6f0ae | 2024-01-08 18:49:21 | [diff] [blame] | 334 | *button_remapping.remapping_action, |
YuhanYang | 92cbe159 | 2023-12-05 02:13:29 | [diff] [blame] | 335 | PeripheralCustomizationEventRewriter:: |
| 336 | PeripheralCustomizationMetricsType::kGraphicsTabletPen); |
| 337 | return std::move(pen_action); |
David Padlipsky | f9728f27 | 2023-09-06 16:50:36 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | const auto tablet_button_remapping_iter = base::ranges::find( |
| 341 | settings.tablet_button_remappings, button, |
| 342 | [](const mojom::ButtonRemappingPtr& entry) { return *entry->button; }); |
| 343 | if (tablet_button_remapping_iter != settings.tablet_button_remappings.end()) { |
David Padlipsky | 1eb6f0ae | 2024-01-08 18:49:21 | [diff] [blame] | 344 | const mojom::ButtonRemapping& button_remapping = |
| 345 | *(*tablet_button_remapping_iter); |
| 346 | if (!button_remapping.remapping_action) { |
| 347 | return std::nullopt; |
| 348 | } |
| 349 | |
YuhanYang | 92cbe159 | 2023-12-05 02:13:29 | [diff] [blame] | 350 | auto tablet_action = |
| 351 | PeripheralCustomizationEventRewriter::RemappingActionResult( |
David Padlipsky | 1eb6f0ae | 2024-01-08 18:49:21 | [diff] [blame] | 352 | *button_remapping.remapping_action, |
YuhanYang | 92cbe159 | 2023-12-05 02:13:29 | [diff] [blame] | 353 | PeripheralCustomizationEventRewriter:: |
| 354 | PeripheralCustomizationMetricsType::kGraphicsTablet); |
| 355 | return std::move(tablet_action); |
David Padlipsky | f9728f27 | 2023-09-06 16:50:36 | [diff] [blame] | 356 | } |
| 357 | |
David Padlipsky | 1eb6f0ae | 2024-01-08 18:49:21 | [diff] [blame] | 358 | return std::nullopt; |
David Padlipsky | f9728f27 | 2023-09-06 16:50:36 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | int GetRemappedModifiersFromMouseSettings( |
| 362 | const mojom::MouseSettings& settings) { |
| 363 | int modifiers = 0; |
| 364 | for (const auto& button_remapping : settings.button_remappings) { |
David Padlipsky | d664e67 | 2023-10-25 21:32:15 | [diff] [blame] | 365 | if (button_remapping->remapping_action) { |
| 366 | modifiers |= ConvertButtonToFlags(*button_remapping->button); |
| 367 | } |
David Padlipsky | f9728f27 | 2023-09-06 16:50:36 | [diff] [blame] | 368 | } |
| 369 | return modifiers; |
| 370 | } |
| 371 | |
| 372 | int GetRemappedModifiersFromGraphicsTabletSettings( |
| 373 | const mojom::GraphicsTabletSettings& settings) { |
| 374 | int modifiers = 0; |
| 375 | for (const auto& button_remapping : settings.pen_button_remappings) { |
| 376 | modifiers |= ConvertButtonToFlags(*button_remapping->button); |
| 377 | } |
| 378 | for (const auto& button_remapping : settings.tablet_button_remappings) { |
| 379 | modifiers |= ConvertButtonToFlags(*button_remapping->button); |
| 380 | } |
| 381 | return modifiers; |
| 382 | } |
| 383 | |
Danny Wang | a917f0c | 2023-12-21 00:05:50 | [diff] [blame] | 384 | // Verify if the keyboard code is an alphabet letter. |
| 385 | bool IsAlphaKeyboardCode(ui::KeyboardCode key_code) { |
| 386 | return key_code >= ui::VKEY_A && key_code <= ui::VKEY_Z; |
| 387 | } |
| 388 | |
| 389 | // Verify if the keyboard code is a number. |
| 390 | bool IsNumberKeyboardCode(ui::KeyboardCode key_code) { |
| 391 | return key_code >= ui::VKEY_0 && key_code <= ui::VKEY_9; |
| 392 | } |
| 393 | |
David Padlipsky | b5a52cd1 | 2023-08-08 21:45:19 | [diff] [blame] | 394 | } // namespace |
| 395 | |
David Padlipsky | 3b55bb1 | 2023-09-05 22:24:57 | [diff] [blame] | 396 | // Compares the `DeviceIdButton` struct based on first the device id, and then |
| 397 | // the button stored within the `DeviceIdButton` struct. |
| 398 | bool operator<( |
| 399 | const PeripheralCustomizationEventRewriter::DeviceIdButton& left, |
| 400 | const PeripheralCustomizationEventRewriter::DeviceIdButton& right) { |
| 401 | if (right.device_id != left.device_id) { |
| 402 | return left.device_id < right.device_id; |
| 403 | } |
| 404 | |
| 405 | // If both are VKeys, compare them. |
| 406 | if (left.button->is_vkey() && right.button->is_vkey()) { |
| 407 | return left.button->get_vkey() < right.button->get_vkey(); |
| 408 | } |
| 409 | |
| 410 | // If both are customizable buttons, compare them. |
| 411 | if (left.button->is_customizable_button() && |
| 412 | right.button->is_customizable_button()) { |
| 413 | return left.button->get_customizable_button() < |
| 414 | right.button->get_customizable_button(); |
| 415 | } |
| 416 | |
| 417 | // Otherwise, return true if the lhs is a VKey as they mismatch and VKeys |
| 418 | // should be considered less than customizable buttons. |
| 419 | return left.button->is_vkey(); |
| 420 | } |
| 421 | |
| 422 | PeripheralCustomizationEventRewriter::DeviceIdButton::DeviceIdButton( |
| 423 | int device_id, |
| 424 | mojom::ButtonPtr button) |
| 425 | : device_id(device_id), button(std::move(button)) {} |
| 426 | |
| 427 | PeripheralCustomizationEventRewriter::DeviceIdButton::DeviceIdButton( |
| 428 | DeviceIdButton&& device_id_button) |
| 429 | : device_id(device_id_button.device_id), |
| 430 | button(std::move(device_id_button.button)) {} |
| 431 | |
| 432 | PeripheralCustomizationEventRewriter::DeviceIdButton::~DeviceIdButton() = |
| 433 | default; |
| 434 | |
| 435 | PeripheralCustomizationEventRewriter::DeviceIdButton& |
| 436 | PeripheralCustomizationEventRewriter::DeviceIdButton::operator=( |
| 437 | PeripheralCustomizationEventRewriter::DeviceIdButton&& device_id_button) = |
| 438 | default; |
| 439 | |
YuhanYang | 92cbe159 | 2023-12-05 02:13:29 | [diff] [blame] | 440 | PeripheralCustomizationEventRewriter::RemappingActionResult:: |
| 441 | RemappingActionResult(mojom::RemappingAction& remapping_action, |
| 442 | PeripheralCustomizationMetricsType peripheral_kind) |
| 443 | : remapping_action(remapping_action), peripheral_kind(peripheral_kind) {} |
| 444 | |
| 445 | PeripheralCustomizationEventRewriter::RemappingActionResult:: |
| 446 | RemappingActionResult(RemappingActionResult&& result) |
| 447 | : remapping_action(std::move(result.remapping_action)), |
| 448 | peripheral_kind(result.peripheral_kind) {} |
| 449 | |
| 450 | PeripheralCustomizationEventRewriter::RemappingActionResult:: |
| 451 | ~RemappingActionResult() = default; |
| 452 | |
David Padlipsky | f9728f27 | 2023-09-06 16:50:36 | [diff] [blame] | 453 | PeripheralCustomizationEventRewriter::PeripheralCustomizationEventRewriter( |
| 454 | InputDeviceSettingsController* input_device_settings_controller) |
YuhanYang | 92cbe159 | 2023-12-05 02:13:29 | [diff] [blame] | 455 | : input_device_settings_controller_(input_device_settings_controller) { |
| 456 | metrics_manager_ = std::make_unique<InputDeviceSettingsMetricsManager>(); |
| 457 | } |
| 458 | |
David Padlipsky | 5eaaade | 2023-07-21 22:39:38 | [diff] [blame] | 459 | PeripheralCustomizationEventRewriter::~PeripheralCustomizationEventRewriter() = |
| 460 | default; |
| 461 | |
Arthur Sonzogni | a98e443 | 2023-11-28 18:15:01 | [diff] [blame] | 462 | std::optional<PeripheralCustomizationEventRewriter::DeviceType> |
David Padlipsky | eedcbfe | 2023-08-08 23:58:36 | [diff] [blame] | 463 | PeripheralCustomizationEventRewriter::GetDeviceTypeToObserve(int device_id) { |
| 464 | if (mice_to_observe_.contains(device_id)) { |
| 465 | return DeviceType::kMouse; |
| 466 | } |
| 467 | if (graphics_tablets_to_observe_.contains(device_id)) { |
| 468 | return DeviceType::kGraphicsTablet; |
| 469 | } |
Arthur Sonzogni | a98e443 | 2023-11-28 18:15:01 | [diff] [blame] | 470 | return std::nullopt; |
David Padlipsky | eedcbfe | 2023-08-08 23:58:36 | [diff] [blame] | 471 | } |
| 472 | |
Danny Wang | 50d4518 | 2023-10-05 17:55:45 | [diff] [blame] | 473 | void PeripheralCustomizationEventRewriter::StartObservingMouse( |
| 474 | int device_id, |
Danny Wang | 6bc788e | 2023-12-08 23:08:02 | [diff] [blame] | 475 | mojom::CustomizationRestriction customization_restriction) { |
| 476 | mice_to_observe_.insert_or_assign(device_id, customization_restriction); |
David Padlipsky | b5a52cd1 | 2023-08-08 21:45:19 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | void PeripheralCustomizationEventRewriter::StartObservingGraphicsTablet( |
Danny Wang | f2ecf15 | 2023-12-08 23:13:22 | [diff] [blame] | 480 | int device_id, |
| 481 | mojom::CustomizationRestriction customization_restriction) { |
| 482 | graphics_tablets_to_observe_.insert_or_assign(device_id, |
| 483 | customization_restriction); |
David Padlipsky | b5a52cd1 | 2023-08-08 21:45:19 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | void PeripheralCustomizationEventRewriter::StopObserving() { |
| 487 | graphics_tablets_to_observe_.clear(); |
| 488 | mice_to_observe_.clear(); |
| 489 | } |
| 490 | |
| 491 | bool PeripheralCustomizationEventRewriter::NotifyMouseEventObserving( |
| 492 | const ui::MouseEvent& mouse_event, |
| 493 | DeviceType device_type) { |
| 494 | if (!IsMouseButtonEvent(mouse_event)) { |
| 495 | return false; |
| 496 | } |
| 497 | |
| 498 | // Make sure the button is remappable for the current `device_type`. |
| 499 | switch (device_type) { |
| 500 | case DeviceType::kMouse: |
| 501 | if (!IsMouseRemappableButton(mouse_event.changed_button_flags())) { |
| 502 | return false; |
| 503 | } |
| 504 | break; |
| 505 | case DeviceType::kGraphicsTablet: |
| 506 | if (!IsGraphicsTabletRemappableButton( |
| 507 | mouse_event.changed_button_flags())) { |
| 508 | return false; |
| 509 | } |
| 510 | break; |
| 511 | } |
| 512 | |
| 513 | if (mouse_event.type() != ui::ET_MOUSE_PRESSED) { |
| 514 | return true; |
| 515 | } |
| 516 | |
David Padlipsky | b7896da | 2023-10-25 19:45:46 | [diff] [blame] | 517 | const auto button = GetButtonFromMouseEvent(mouse_event); |
David Padlipsky | c16dd52 | 2023-09-08 21:18:22 | [diff] [blame] | 518 | switch (device_type) { |
| 519 | case DeviceType::kMouse: |
| 520 | input_device_settings_controller_->OnMouseButtonPressed( |
| 521 | mouse_event.source_device_id(), *button); |
| 522 | break; |
| 523 | case DeviceType::kGraphicsTablet: |
| 524 | input_device_settings_controller_->OnGraphicsTabletButtonPressed( |
| 525 | mouse_event.source_device_id(), *button); |
| 526 | break; |
David Padlipsky | b5a52cd1 | 2023-08-08 21:45:19 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | return true; |
| 530 | } |
| 531 | |
Danny Wang | ab5c8a36 | 2023-12-15 22:34:18 | [diff] [blame] | 532 | bool PeripheralCustomizationEventRewriter::IsButtonCustomizable( |
| 533 | const ui::KeyEvent& key_event) { |
| 534 | const auto iter = mice_to_observe_.find(key_event.source_device_id()); |
| 535 | if (iter == mice_to_observe().end()) { |
| 536 | return false; |
| 537 | } |
| 538 | const auto customization_restriction = iter->second; |
| 539 | // There are several cases for the customization restriction: |
| 540 | // 1. If restriction is kAllowCustomizations, mice are allowed to observe |
| 541 | // key events. |
| 542 | // 2. If restriction is kAllowAlphabetKeyEventRewrites, mice are allowed to |
| 543 | // observe only alphabet letters key event. |
Danny Wang | e2a70187 | 2023-12-15 23:13:47 | [diff] [blame] | 544 | // 3. If restriction is kAllowAlphabetOrNumberKeyEventRewrites, mice are |
| 545 | // allowed to observe alphabet letters or number key event. |
| 546 | // 4. Mice are not allowed to observe key event in other cases. |
Danny Wang | ab5c8a36 | 2023-12-15 22:34:18 | [diff] [blame] | 547 | switch (customization_restriction) { |
| 548 | case mojom::CustomizationRestriction::kAllowCustomizations: |
| 549 | return true; |
| 550 | case mojom::CustomizationRestriction::kAllowAlphabetKeyEventRewrites: |
| 551 | return IsAlphaKeyboardCode(key_event.key_code()); |
Danny Wang | e2a70187 | 2023-12-15 23:13:47 | [diff] [blame] | 552 | case mojom::CustomizationRestriction:: |
| 553 | kAllowAlphabetOrNumberKeyEventRewrites: |
| 554 | return IsAlphaKeyboardCode(key_event.key_code()) || |
Danny Wang | a917f0c | 2023-12-21 00:05:50 | [diff] [blame] | 555 | IsNumberKeyboardCode(key_event.key_code()); |
Danny Wang | ab5c8a36 | 2023-12-15 22:34:18 | [diff] [blame] | 556 | case mojom::CustomizationRestriction::kDisallowCustomizations: |
| 557 | case mojom::CustomizationRestriction::kDisableKeyEventRewrites: |
| 558 | return false; |
| 559 | } |
| 560 | } |
| 561 | |
David Padlipsky | eedcbfe | 2023-08-08 23:58:36 | [diff] [blame] | 562 | bool PeripheralCustomizationEventRewriter::NotifyKeyEventObserving( |
| 563 | const ui::KeyEvent& key_event, |
| 564 | DeviceType device_type) { |
Danny Wang | ab5c8a36 | 2023-12-15 22:34:18 | [diff] [blame] | 565 | if (device_type == DeviceType::kMouse && !IsButtonCustomizable(key_event)) { |
| 566 | return false; |
Danny Wang | 50d4518 | 2023-10-05 17:55:45 | [diff] [blame] | 567 | } |
| 568 | |
David Padlipsky | eedcbfe | 2023-08-08 23:58:36 | [diff] [blame] | 569 | // Observers should only be notified on key presses. |
| 570 | if (key_event.type() != ui::ET_KEY_PRESSED) { |
| 571 | return true; |
| 572 | } |
| 573 | |
| 574 | const auto button = mojom::Button::NewVkey(key_event.key_code()); |
David Padlipsky | c16dd52 | 2023-09-08 21:18:22 | [diff] [blame] | 575 | switch (device_type) { |
| 576 | case DeviceType::kMouse: |
| 577 | input_device_settings_controller_->OnMouseButtonPressed( |
| 578 | key_event.source_device_id(), *button); |
| 579 | break; |
| 580 | case DeviceType::kGraphicsTablet: |
| 581 | input_device_settings_controller_->OnGraphicsTabletButtonPressed( |
| 582 | key_event.source_device_id(), *button); |
| 583 | break; |
David Padlipsky | eedcbfe | 2023-08-08 23:58:36 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | return true; |
| 587 | } |
| 588 | |
David Padlipsky | cc7c2de | 2023-08-23 17:16:54 | [diff] [blame] | 589 | bool PeripheralCustomizationEventRewriter::RewriteEventFromButton( |
| 590 | const ui::Event& event, |
| 591 | const mojom::Button& button, |
| 592 | std::unique_ptr<ui::Event>& rewritten_event) { |
YuhanYang | 92cbe159 | 2023-12-05 02:13:29 | [diff] [blame] | 593 | absl::optional<PeripheralCustomizationEventRewriter::RemappingActionResult> |
| 594 | remapping_action_result = |
| 595 | GetRemappingAction(event.source_device_id(), button); |
| 596 | if (!remapping_action_result) { |
David Padlipsky | cc7c2de | 2023-08-23 17:16:54 | [diff] [blame] | 597 | return false; |
| 598 | } |
YuhanYang | 92cbe159 | 2023-12-05 02:13:29 | [diff] [blame] | 599 | auto remapping_action = remapping_action_result->remapping_action; |
| 600 | |
| 601 | metrics_manager_->RecordRemappingActionWhenButtonPressed( |
| 602 | *remapping_action, |
David Padlipsky | 74935f59 | 2024-02-02 00:24:41 | [diff] [blame^] | 603 | ToMetricsString(remapping_action_result->peripheral_kind).data()); |
David Padlipsky | cc7c2de | 2023-08-23 17:16:54 | [diff] [blame] | 604 | |
Danny Wang | d388f6b | 2023-09-29 00:06:50 | [diff] [blame] | 605 | if (remapping_action->is_accelerator_action()) { |
David Padlipsky | f873095 | 2023-09-01 19:38:27 | [diff] [blame] | 606 | if (event.type() == ui::ET_KEY_PRESSED || |
| 607 | event.type() == ui::ET_MOUSE_PRESSED) { |
| 608 | // Every accelerator supported by peripheral customization is not impacted |
| 609 | // by the accelerator passed. Therefore, passing an empty accelerator will |
| 610 | // cause no issues. |
| 611 | Shell::Get()->accelerator_controller()->PerformActionIfEnabled( |
Danny Wang | d388f6b | 2023-09-29 00:06:50 | [diff] [blame] | 612 | remapping_action->get_accelerator_action(), /*accelerator=*/{}); |
David Padlipsky | f873095 | 2023-09-01 19:38:27 | [diff] [blame] | 613 | } |
| 614 | |
David Padlipsky | cc7c2de | 2023-08-23 17:16:54 | [diff] [blame] | 615 | return true; |
| 616 | } |
| 617 | |
| 618 | if (remapping_action->is_key_event()) { |
| 619 | const auto& key_event = remapping_action->get_key_event(); |
David Padlipsky | 5b39dc0 | 2024-01-03 20:36:48 | [diff] [blame] | 620 | auto entry = FindKeyCodeEntry(key_event->vkey); |
| 621 | // If no entry can be found, use the stored key_event struct. |
| 622 | if (!entry) { |
| 623 | rewritten_event = RewriteEventToKeyEvent(event, *key_event); |
| 624 | } else { |
| 625 | rewritten_event = RewriteEventToKeyEvent( |
| 626 | event, mojom::KeyEvent(entry->resulting_key_code, |
| 627 | static_cast<int>(entry->dom_code), |
| 628 | static_cast<int>(entry->dom_key), |
| 629 | key_event->modifiers, "")); |
| 630 | } |
Danny Wang | 6bdb558 | 2023-09-18 21:27:29 | [diff] [blame] | 631 | } |
| 632 | |
Danny Wang | d388f6b | 2023-09-29 00:06:50 | [diff] [blame] | 633 | if (remapping_action->is_static_shortcut_action()) { |
David Padlipsky | d664e67 | 2023-10-25 21:32:15 | [diff] [blame] | 634 | const auto static_action = remapping_action->get_static_shortcut_action(); |
| 635 | if (static_action == mojom::StaticShortcutAction::kDisable) { |
Danny Wang | 75a9a86f | 2023-09-29 21:00:30 | [diff] [blame] | 636 | // Return true to discard the event. |
| 637 | return true; |
| 638 | } |
David Padlipsky | d664e67 | 2023-10-25 21:32:15 | [diff] [blame] | 639 | |
| 640 | if (kStaticActionToMouseButtonFlag.contains(static_action)) { |
| 641 | rewritten_event = RewriteEventToMouseButtonEvent(event, static_action); |
| 642 | } else { |
| 643 | rewritten_event = RewriteEventToKeyEvent( |
| 644 | event, GetStaticShortcutAction( |
| 645 | remapping_action->get_static_shortcut_action())); |
| 646 | } |
David Padlipsky | cc7c2de | 2023-08-23 17:16:54 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | return false; |
| 650 | } |
| 651 | |
David Padlipsky | eedcbfe | 2023-08-08 23:58:36 | [diff] [blame] | 652 | ui::EventDispatchDetails PeripheralCustomizationEventRewriter::RewriteKeyEvent( |
| 653 | const ui::KeyEvent& key_event, |
| 654 | const Continuation continuation) { |
| 655 | auto device_type_to_observe = |
| 656 | GetDeviceTypeToObserve(key_event.source_device_id()); |
| 657 | if (device_type_to_observe) { |
| 658 | if (NotifyKeyEventObserving(key_event, *device_type_to_observe)) { |
| 659 | return DiscardEvent(continuation); |
| 660 | } |
| 661 | } |
| 662 | |
David Padlipsky | cc7c2de | 2023-08-23 17:16:54 | [diff] [blame] | 663 | std::unique_ptr<ui::Event> rewritten_event; |
David Padlipsky | 3b55bb1 | 2023-09-05 22:24:57 | [diff] [blame] | 664 | mojom::ButtonPtr button = mojom::Button::NewVkey(key_event.key_code()); |
David Padlipsky | 3394af9 | 2023-10-27 21:30:50 | [diff] [blame] | 665 | bool updated_button_map = false; |
David Padlipsky | 3b55bb1 | 2023-09-05 22:24:57 | [diff] [blame] | 666 | if (RewriteEventFromButton(key_event, *button, rewritten_event)) { |
David Padlipsky | cc7c2de | 2023-08-23 17:16:54 | [diff] [blame] | 667 | return DiscardEvent(continuation); |
| 668 | } |
David Padlipsky | 3394af9 | 2023-10-27 21:30:50 | [diff] [blame] | 669 | |
| 670 | // Update pressed button map now if either there was no rewrite or if its not |
| 671 | // a mouse release event. |
| 672 | if (!rewritten_event || rewritten_event->type() != ui::ET_MOUSE_RELEASED) { |
| 673 | updated_button_map = true; |
| 674 | UpdatePressedButtonMap(std::move(button), key_event, rewritten_event); |
| 675 | } |
David Padlipsky | cc7c2de | 2023-08-23 17:16:54 | [diff] [blame] | 676 | |
David Padlipsky | 34156418 | 2024-01-11 22:09:32 | [diff] [blame] | 677 | if (!rewritten_event && key_event.key_code() >= ui::VKEY_BUTTON_0 && |
| 678 | key_event.key_code() <= ui::VKEY_BUTTON_Z) { |
| 679 | return DiscardEvent(continuation); |
| 680 | } |
| 681 | |
David Padlipsky | cc7c2de | 2023-08-23 17:16:54 | [diff] [blame] | 682 | if (!rewritten_event) { |
| 683 | rewritten_event = std::make_unique<ui::KeyEvent>(key_event); |
| 684 | } |
| 685 | |
| 686 | RemoveRemappedModifiers(*rewritten_event); |
David Padlipsky | 3b55bb1 | 2023-09-05 22:24:57 | [diff] [blame] | 687 | ApplyRemappedModifiers(*rewritten_event); |
David Padlipsky | 3394af9 | 2023-10-27 21:30:50 | [diff] [blame] | 688 | |
| 689 | if (!updated_button_map) { |
| 690 | UpdatePressedButtonMap(std::move(button), key_event, rewritten_event); |
| 691 | } |
| 692 | |
David Padlipsky | cc7c2de | 2023-08-23 17:16:54 | [diff] [blame] | 693 | return SendEvent(continuation, rewritten_event.get()); |
David Padlipsky | eedcbfe | 2023-08-08 23:58:36 | [diff] [blame] | 694 | } |
| 695 | |
David Padlipsky | 3b55bb1 | 2023-09-05 22:24:57 | [diff] [blame] | 696 | void PeripheralCustomizationEventRewriter::UpdatePressedButtonMap( |
| 697 | mojom::ButtonPtr button, |
| 698 | const ui::Event& original_event, |
| 699 | const std::unique_ptr<ui::Event>& rewritten_event) { |
| 700 | DeviceIdButton device_id_button_key = |
| 701 | DeviceIdButton{original_event.source_device_id(), std::move(button)}; |
David Padlipsky | 3394af9 | 2023-10-27 21:30:50 | [diff] [blame] | 702 | |
| 703 | // If the button is released, the entry must be removed from the map. |
| 704 | if (original_event.type() == ui::ET_MOUSE_RELEASED || |
| 705 | original_event.type() == ui::ET_KEY_RELEASED) { |
| 706 | device_button_to_flags_.erase(std::move(device_id_button_key)); |
| 707 | return; |
| 708 | } |
| 709 | |
David Padlipsky | d664e67 | 2023-10-25 21:32:15 | [diff] [blame] | 710 | const int key_event_flags = |
David Padlipsky | 3b55bb1 | 2023-09-05 22:24:57 | [diff] [blame] | 711 | (rewritten_event && rewritten_event->IsKeyEvent()) |
| 712 | ? ConvertKeyCodeToFlags(rewritten_event->AsKeyEvent()->key_code()) |
| 713 | : 0; |
David Padlipsky | d664e67 | 2023-10-25 21:32:15 | [diff] [blame] | 714 | const int mouse_event_flags = |
| 715 | (rewritten_event && rewritten_event->IsMouseEvent()) |
| 716 | ? rewritten_event->AsMouseEvent()->changed_button_flags() |
| 717 | : 0; |
David Padlipsky | 3b55bb1 | 2023-09-05 22:24:57 | [diff] [blame] | 718 | |
David Padlipsky | 3394af9 | 2023-10-27 21:30:50 | [diff] [blame] | 719 | const int combined_flags = key_event_flags | mouse_event_flags; |
| 720 | if (!combined_flags) { |
David Padlipsky | 3b55bb1 | 2023-09-05 22:24:57 | [diff] [blame] | 721 | return; |
| 722 | } |
| 723 | |
| 724 | // Add the entry to the map with the flags that must be applied to other |
| 725 | // events. |
| 726 | device_button_to_flags_.insert_or_assign(std::move(device_id_button_key), |
David Padlipsky | 3394af9 | 2023-10-27 21:30:50 | [diff] [blame] | 727 | combined_flags); |
David Padlipsky | 3b55bb1 | 2023-09-05 22:24:57 | [diff] [blame] | 728 | } |
| 729 | |
David Padlipsky | b5a52cd1 | 2023-08-08 21:45:19 | [diff] [blame] | 730 | ui::EventDispatchDetails |
| 731 | PeripheralCustomizationEventRewriter::RewriteMouseEvent( |
| 732 | const ui::MouseEvent& mouse_event, |
| 733 | const Continuation continuation) { |
David Padlipsky | eedcbfe | 2023-08-08 23:58:36 | [diff] [blame] | 734 | auto device_type_to_observe = |
| 735 | GetDeviceTypeToObserve(mouse_event.source_device_id()); |
| 736 | if (device_type_to_observe) { |
| 737 | if (NotifyMouseEventObserving(mouse_event, *device_type_to_observe)) { |
David Padlipsky | b5a52cd1 | 2023-08-08 21:45:19 | [diff] [blame] | 738 | return DiscardEvent(continuation); |
| 739 | } |
| 740 | |
| 741 | // Otherwise, the flags must be cleared for the remappable buttons so they |
| 742 | // do not affect the application while the mouse is meant to be observed. |
David Padlipsky | dcd57e1 | 2023-10-25 23:00:18 | [diff] [blame] | 743 | std::unique_ptr<ui::Event> rewritten_event = CloneEvent(mouse_event); |
David Padlipsky | eedcbfe | 2023-08-08 23:58:36 | [diff] [blame] | 744 | const int remappable_flags = |
| 745 | GetRemappableMouseEventFlags(*device_type_to_observe); |
David Padlipsky | 4687d1e1 | 2024-01-04 00:18:45 | [diff] [blame] | 746 | rewritten_event->SetFlags(rewritten_event->flags() & ~remappable_flags); |
David Padlipsky | dcd57e1 | 2023-10-25 23:00:18 | [diff] [blame] | 747 | if (rewritten_event->IsMouseEvent()) { |
| 748 | auto& rewritten_mouse_event = *rewritten_event->AsMouseEvent(); |
| 749 | rewritten_mouse_event.set_changed_button_flags( |
| 750 | rewritten_mouse_event.changed_button_flags() & ~remappable_flags); |
| 751 | } |
| 752 | return SendEvent(continuation, rewritten_event.get()); |
David Padlipsky | b5a52cd1 | 2023-08-08 21:45:19 | [diff] [blame] | 753 | } |
| 754 | |
David Padlipsky | cc7c2de | 2023-08-23 17:16:54 | [diff] [blame] | 755 | std::unique_ptr<ui::Event> rewritten_event; |
David Padlipsky | 3394af9 | 2023-10-27 21:30:50 | [diff] [blame] | 756 | mojom::ButtonPtr button; |
| 757 | bool updated_button_map = false; |
David Padlipsky | 3b55bb1 | 2023-09-05 22:24:57 | [diff] [blame] | 758 | if (IsMouseButtonEvent(mouse_event) && mouse_event.changed_button_flags()) { |
David Padlipsky | 3394af9 | 2023-10-27 21:30:50 | [diff] [blame] | 759 | button = GetButtonFromMouseEvent(mouse_event); |
David Padlipsky | 3b55bb1 | 2023-09-05 22:24:57 | [diff] [blame] | 760 | if (RewriteEventFromButton(mouse_event, *button, rewritten_event)) { |
David Padlipsky | cc7c2de | 2023-08-23 17:16:54 | [diff] [blame] | 761 | return DiscardEvent(continuation); |
| 762 | } |
David Padlipsky | 3394af9 | 2023-10-27 21:30:50 | [diff] [blame] | 763 | // Update pressed button map now if either there was no rewrite or if its |
| 764 | // not a mouse release event. |
| 765 | if (!rewritten_event || rewritten_event->type() != ui::ET_MOUSE_RELEASED) { |
| 766 | updated_button_map = true; |
| 767 | UpdatePressedButtonMap(std::move(button), mouse_event, rewritten_event); |
| 768 | } |
David Padlipsky | cc7c2de | 2023-08-23 17:16:54 | [diff] [blame] | 769 | } |
David Padlipsky | 3394af9 | 2023-10-27 21:30:50 | [diff] [blame] | 770 | |
David Padlipsky | cc7c2de | 2023-08-23 17:16:54 | [diff] [blame] | 771 | if (!rewritten_event) { |
David Padlipsky | dcd57e1 | 2023-10-25 23:00:18 | [diff] [blame] | 772 | rewritten_event = CloneEvent(mouse_event); |
David Padlipsky | cc7c2de | 2023-08-23 17:16:54 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | RemoveRemappedModifiers(*rewritten_event); |
David Padlipsky | 3b55bb1 | 2023-09-05 22:24:57 | [diff] [blame] | 776 | ApplyRemappedModifiers(*rewritten_event); |
David Padlipsky | 3394af9 | 2023-10-27 21:30:50 | [diff] [blame] | 777 | |
| 778 | if (!updated_button_map) { |
| 779 | UpdatePressedButtonMap(std::move(button), mouse_event, rewritten_event); |
| 780 | } |
| 781 | |
David Padlipsky | cc7c2de | 2023-08-23 17:16:54 | [diff] [blame] | 782 | return SendEvent(continuation, rewritten_event.get()); |
David Padlipsky | b5a52cd1 | 2023-08-08 21:45:19 | [diff] [blame] | 783 | } |
| 784 | |
David Padlipsky | 5eaaade | 2023-07-21 22:39:38 | [diff] [blame] | 785 | ui::EventDispatchDetails PeripheralCustomizationEventRewriter::RewriteEvent( |
| 786 | const ui::Event& event, |
| 787 | const Continuation continuation) { |
Jimmy | b36a877 | 2023-11-10 19:41:39 | [diff] [blame] | 788 | DCHECK(features::IsPeripheralCustomizationEnabled() || |
| 789 | ::features::IsShortcutCustomizationEnabled()); |
David Padlipsky | b5a52cd1 | 2023-08-08 21:45:19 | [diff] [blame] | 790 | |
| 791 | if (event.IsMouseEvent()) { |
| 792 | return RewriteMouseEvent(*event.AsMouseEvent(), continuation); |
| 793 | } |
| 794 | |
David Padlipsky | eedcbfe | 2023-08-08 23:58:36 | [diff] [blame] | 795 | if (event.IsKeyEvent()) { |
| 796 | return RewriteKeyEvent(*event.AsKeyEvent(), continuation); |
| 797 | } |
| 798 | |
David Padlipsky | 5eaaade | 2023-07-21 22:39:38 | [diff] [blame] | 799 | return SendEvent(continuation, &event); |
| 800 | } |
| 801 | |
YuhanYang | 92cbe159 | 2023-12-05 02:13:29 | [diff] [blame] | 802 | std::optional<PeripheralCustomizationEventRewriter::RemappingActionResult> |
David Padlipsky | cc7c2de | 2023-08-23 17:16:54 | [diff] [blame] | 803 | PeripheralCustomizationEventRewriter::GetRemappingAction( |
| 804 | int device_id, |
| 805 | const mojom::Button& button) { |
David Padlipsky | f9728f27 | 2023-09-06 16:50:36 | [diff] [blame] | 806 | const auto* mouse_settings = |
| 807 | input_device_settings_controller_->GetMouseSettings(device_id); |
| 808 | if (mouse_settings) { |
| 809 | return GetRemappingActionFromMouseSettings(button, *mouse_settings); |
David Padlipsky | cc7c2de | 2023-08-23 17:16:54 | [diff] [blame] | 810 | } |
| 811 | |
David Padlipsky | f9728f27 | 2023-09-06 16:50:36 | [diff] [blame] | 812 | const auto* graphics_tablet_settings = |
| 813 | input_device_settings_controller_->GetGraphicsTabletSettings(device_id); |
| 814 | if (graphics_tablet_settings) { |
| 815 | return GetRemappingActionFromGraphicsTabletSettings( |
| 816 | button, *graphics_tablet_settings); |
David Padlipsky | cc7c2de | 2023-08-23 17:16:54 | [diff] [blame] | 817 | } |
| 818 | |
YuhanYang | 92cbe159 | 2023-12-05 02:13:29 | [diff] [blame] | 819 | return absl::nullopt; |
David Padlipsky | cc7c2de | 2023-08-23 17:16:54 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | void PeripheralCustomizationEventRewriter::RemoveRemappedModifiers( |
| 823 | ui::Event& event) { |
David Padlipsky | f9728f27 | 2023-09-06 16:50:36 | [diff] [blame] | 824 | int modifier_flags = 0; |
| 825 | if (const auto* mouse_settings = |
| 826 | input_device_settings_controller_->GetMouseSettings( |
| 827 | event.source_device_id()); |
| 828 | mouse_settings) { |
| 829 | modifier_flags = GetRemappedModifiersFromMouseSettings(*mouse_settings); |
| 830 | } else if (const auto* graphics_tablet_settings = |
| 831 | input_device_settings_controller_->GetGraphicsTabletSettings( |
| 832 | event.source_device_id()); |
| 833 | graphics_tablet_settings) { |
| 834 | modifier_flags = GetRemappedModifiersFromGraphicsTabletSettings( |
| 835 | *graphics_tablet_settings); |
David Padlipsky | cc7c2de | 2023-08-23 17:16:54 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | // TODO(dpad): This logic isn't quite correct. If a second devices is holding |
| 839 | // "Ctrl" and the original device has a button that is "Ctrl" that is |
| 840 | // remapped, this will behave incorrectly as it will remove "Ctrl". Instead, |
| 841 | // this needs to track what keys are being pressed by the device that have |
| 842 | // modifiers attached to them. For now, this is close enough to being correct. |
David Padlipsky | 4687d1e1 | 2024-01-04 00:18:45 | [diff] [blame] | 843 | event.SetFlags(event.flags() & ~modifier_flags); |
David Padlipsky | cc7c2de | 2023-08-23 17:16:54 | [diff] [blame] | 844 | } |
| 845 | |
David Padlipsky | 3b55bb1 | 2023-09-05 22:24:57 | [diff] [blame] | 846 | void PeripheralCustomizationEventRewriter::ApplyRemappedModifiers( |
| 847 | ui::Event& event) { |
| 848 | int flags = 0; |
| 849 | for (const auto& [_, flag] : device_button_to_flags_) { |
| 850 | flags |= flag; |
| 851 | } |
David Padlipsky | 4687d1e1 | 2024-01-04 00:18:45 | [diff] [blame] | 852 | event.SetFlags(event.flags() | flags); |
David Padlipsky | 3b55bb1 | 2023-09-05 22:24:57 | [diff] [blame] | 853 | } |
| 854 | |
David Padlipsky | dcd57e1 | 2023-10-25 23:00:18 | [diff] [blame] | 855 | std::unique_ptr<ui::Event> PeripheralCustomizationEventRewriter::CloneEvent( |
| 856 | const ui::Event& event) { |
| 857 | std::unique_ptr<ui::Event> cloned_event = event.Clone(); |
| 858 | // SetNativeEvent must be called explicitly as native events are not copied |
| 859 | // on ChromeOS by default. This is because `PlatformEvent` is a pointer by |
| 860 | // default, so its lifetime can not be guaranteed in general. In this case, |
| 861 | // the lifetime of `rewritten_event` is guaranteed to be less than the |
| 862 | // original `mouse_event`. |
| 863 | SetNativeEvent(*cloned_event, event.native_event()); |
| 864 | return cloned_event; |
| 865 | } |
| 866 | |
David Padlipsky | 5eaaade | 2023-07-21 22:39:38 | [diff] [blame] | 867 | } // namespace ash |