File: disable_touchpad_event_rewriter.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (181 lines) | stat: -rw-r--r-- 5,678 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/accessibility/disable_touchpad_event_rewriter.h"

#include "ash/accessibility/accessibility_controller.h"
#include "ash/public/cpp/accessibility_controller_enums.h"
#include "ash/public/mojom/input_device_settings.mojom.h"
#include "ash/shell.h"
#include "ash/system/input_device_settings/input_device_settings_controller_impl.h"
#include "ui/events/base_event_utils.h"
#include "ui/events/devices/device_data_manager.h"
#include "ui/events/devices/input_device.h"
#include "ui/events/event.h"
#include "ui/wm/core/cursor_manager.h"

namespace ash {

namespace {

bool IsExternalDevice(const ui::InputDevice& device) {
  return device.type == ui::InputDeviceType::INPUT_DEVICE_BLUETOOTH ||
         device.type == ui::InputDeviceType::INPUT_DEVICE_USB;
}

bool IsExternalMouseOrTouchpadConnected() {
  ui::DeviceDataManager* device_data_manager =
      ui::DeviceDataManager::GetInstance();
  InputDeviceSettingsControllerImpl* input_device_settings_controller =
      Shell::Get()->input_device_settings_controller();

  // Check for external touchpads.
  for (const auto& touchpad : device_data_manager->GetTouchpadDevices()) {
    const mojom::Touchpad* found_device =
        input_device_settings_controller->GetTouchpad(touchpad.id);

    if (found_device != nullptr && IsExternalDevice(touchpad)) {
      return true;
    }
  }

  // Check for external mice.
  for (const auto& mouse : device_data_manager->GetMouseDevices()) {
    const mojom::Mouse* found_device =
        input_device_settings_controller->GetMouse(mouse.id);

    if (found_device != nullptr && IsExternalDevice(mouse)) {
      return true;
    }
  }

  return false;
}

int GetInternalTouchpadDeviceId() {
  ui::DeviceDataManager* device_data_manager =
      ui::DeviceDataManager::GetInstance();
  InputDeviceSettingsControllerImpl* input_device_settings_controller =
      Shell::Get()->input_device_settings_controller();

  for (const auto& touchpad : device_data_manager->GetTouchpadDevices()) {
    const mojom::Touchpad* found_device =
        input_device_settings_controller->GetTouchpad(touchpad.id);

    if (found_device != nullptr &&
        touchpad.type == ui::InputDeviceType::INPUT_DEVICE_INTERNAL) {
      return touchpad.id;
    }
  }

  return ui::InputDevice::kInvalidId;
}

bool IsFromInternalTouchpad(const ui::Event& event) {
  return event.source_device_id() == GetInternalTouchpadDeviceId();
}

constexpr base::TimeDelta kEnableTouchpadKeyPressWindow = base::Seconds(2);
}  // namespace

DisableTouchpadEventRewriter::DisableTouchpadEventRewriter() {
  Shell::Get()->accessibility_controller()->SetDisableTouchpadEventRewriter(
      this);
}

DisableTouchpadEventRewriter::~DisableTouchpadEventRewriter() {
  Shell::Get()->accessibility_controller()->SetDisableTouchpadEventRewriter(
      nullptr);
}

void DisableTouchpadEventRewriter::SetEnabled(bool enabled) {
  enabled_ = enabled;
}

bool DisableTouchpadEventRewriter::IsEnabled() {
  return enabled_;
}

ui::EventDispatchDetails DisableTouchpadEventRewriter::RewriteEvent(
    const ui::Event& event,
    const Continuation continuation) {
  if (!IsEnabled()) {
    return SendEvent(continuation, &event);
  }

  if (event.IsKeyEvent()) {
    HandleKeyEvent(event.AsKeyEvent());
  }

  if (event.IsMouseEvent() || event.IsScrollEvent()) {
    return HandleMouseOrScrollEvent(event, continuation);
  }

  return SendEvent(continuation, &event);
}

ui::EventDispatchDetails DisableTouchpadEventRewriter::HandleMouseOrScrollEvent(
    const ui::Event& event,
    const Continuation continuation) {
  DisableTouchpadMode disable_touchpad_mode =
      Shell::Get()->accessibility_controller()->GetDisableTouchpadMode();
  bool is_internal_touchpad_event = IsFromInternalTouchpad(event);
  bool is_external_device_connected = IsExternalMouseOrTouchpadConnected();

  switch (disable_touchpad_mode) {
    case DisableTouchpadMode::kNever:
      Shell::Get()->cursor_manager()->ShowCursor();
      return SendEvent(continuation, &event);

    case DisableTouchpadMode::kAlways:
      if (is_internal_touchpad_event) {
        Shell::Get()->cursor_manager()->HideCursor();
        return DiscardEvent(continuation);
      }
      return SendEvent(continuation, &event);

    case DisableTouchpadMode::kOnExternalMouseConnected:
      if (is_internal_touchpad_event && is_external_device_connected) {
        Shell::Get()->cursor_manager()->HideCursor();
        return DiscardEvent(continuation);
      }
      return SendEvent(continuation, &event);
  }
}

void DisableTouchpadEventRewriter::HandleKeyEvent(const ui::KeyEvent* event) {
  if (event->type() != ui::EventType::kKeyReleased) {
    return;
  }
  event->key_code() == ui::VKEY_SHIFT ? HandleShiftKeyRelease()
                                      : ResetShiftKeyReleaseTracking();
}

void DisableTouchpadEventRewriter::HandleShiftKeyRelease() {
  if (shift_release_count_ == 0) {
    first_shift_release_time_ = ui::EventTimeForNow();
  }

  ++shift_release_count_;
  base::TimeDelta elapsed_time =
      ui::EventTimeForNow() - first_shift_release_time_;

  if (elapsed_time > kEnableTouchpadKeyPressWindow) {
    ResetShiftKeyReleaseTracking();
    return;
  }

  if (shift_release_count_ >= 5) {
    SetEnabled(false);
    Shell::Get()->accessibility_controller()->EnableInternalTouchpad();
    ResetShiftKeyReleaseTracking();
  }
}

void DisableTouchpadEventRewriter::ResetShiftKeyReleaseTracking() {
  shift_release_count_ = 0;
  first_shift_release_time_ = base::TimeTicks();
}

}  // namespace ash