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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "components/input/web_input_event_builders_ios.h"
#import <UIKit/UIKit.h>
#include "base/apple/foundation_util.h"
#include "base/notimplemented.h"
#include "base/notreached.h"
#include "third_party/blink/public/common/input/web_pointer_event.h"
#include "third_party/blink/public/common/input/web_touch_point.h"
#include "ui/events/base_event_utils.h"
#include "ui/events/blink/blink_event_util.h"
#include "ui/events/event_utils.h"
#include "ui/events/keycodes/keyboard_code_conversion.h"
#include "ui/events/keycodes/keyboard_code_conversion_ios.h"
#if !BUILDFLAG(IS_IOS_TVOS)
#import <BrowserEngineKit/BrowserEngineKit.h>
#endif
namespace input {
namespace {
constexpr size_t MAX_POINTERS = blink::WebTouchEvent::kTouchesLengthCap;
static UITouch* g_active_touches[MAX_POINTERS] = {};
size_t GetTouchPointerId(UITouch* touch) {
for (size_t i = 0; i < MAX_POINTERS; ++i) {
if (g_active_touches[i] == touch) {
return i + 1;
}
}
return 0;
}
void AddUITouch(UITouch* touch) {
CHECK(GetTouchPointerId(touch) == 0);
for (size_t i = 0; i < MAX_POINTERS; ++i) {
if (!g_active_touches[i]) {
g_active_touches[i] = touch;
return;
}
}
}
void RemoveUITouch(UITouch* touch) {
for (size_t i = 0; i < MAX_POINTERS; ++i) {
if (g_active_touches[i] == touch) {
g_active_touches[i] = nil;
return;
}
}
NOTREACHED();
}
int ModifiersFromEvent(UIKeyModifierFlags modifier_flags) {
int modifiers = 0;
if (modifier_flags & UIKeyModifierControl) {
modifiers |= blink::WebInputEvent::kControlKey;
}
if (modifier_flags & UIKeyModifierShift) {
modifiers |= blink::WebInputEvent::kShiftKey;
}
if (modifier_flags & UIKeyModifierAlternate) {
modifiers |= blink::WebInputEvent::kAltKey;
}
if (modifier_flags & UIKeyModifierCommand) {
modifiers |= blink::WebInputEvent::kMetaKey;
}
if (modifier_flags & UIKeyModifierAlphaShift) {
modifiers |= blink::WebInputEvent::kCapsLockOn;
}
return modifiers;
}
blink::WebTouchPoint::State ToWebTouchPointState(UITouch* event,
bool was_changed) {
// We will get an event for each actual changed phase.
if (!was_changed) {
return blink::WebTouchPoint::State::kStateStationary;
}
switch ([event phase]) {
case UITouchPhaseBegan:
case UITouchPhaseRegionEntered:
return blink::WebTouchPoint::State::kStatePressed;
case UITouchPhaseMoved:
case UITouchPhaseRegionMoved:
return blink::WebTouchPoint::State::kStateMoved;
case UITouchPhaseEnded:
case UITouchPhaseRegionExited:
return blink::WebTouchPoint::State::kStateReleased;
case UITouchPhaseCancelled:
return blink::WebTouchPoint::State::kStateCancelled;
case UITouchPhaseStationary:
return blink::WebTouchPoint::State::kStateStationary;
}
NOTREACHED() << "Invalid MotionEvent::Action.";
}
void SetWebPointerPropertiesFromMotionEventData(
blink::WebPointerProperties& webPointerProperties,
int pointer_id,
float pressure) {
webPointerProperties.id = pointer_id;
webPointerProperties.force = pressure;
webPointerProperties.tilt_x = webPointerProperties.tilt_y = 0;
webPointerProperties.twist = 0;
webPointerProperties.tangential_pressure = 0;
webPointerProperties.button = blink::WebPointerProperties::Button::kNoButton;
webPointerProperties.pointer_type =
blink::WebPointerProperties::PointerType::kTouch;
// TODO(dtapuska): Support stylus.
}
blink::WebTouchPoint CreateWebTouchPoint(
UIView* view,
UITouch* event,
bool was_changed,
const std::optional<gfx::Vector2dF>& view_offset) {
blink::WebTouchPoint touch;
size_t pointer_index = GetTouchPointerId(event);
CHECK(pointer_index != 0);
SetWebPointerPropertiesFromMotionEventData(touch, pointer_index,
[event force]);
touch.state = ToWebTouchPointState(event, was_changed);
gfx::PointF window_location = gfx::PointF([event locationInView:nil]);
touch.SetPositionInScreen(window_location);
gfx::PointF view_location;
if (view_offset) {
view_location = gfx::PointF(window_location);
view_location += view_offset.value();
} else {
view_location = gfx::PointF([event locationInView:view]);
}
touch.SetPositionInWidget(view_location);
float major_radius = event.majorRadius;
float minor_radius = event.majorRadius;
float orientation_deg = 0;
DCHECK_GE(major_radius, 0);
DCHECK_GE(minor_radius, 0);
DCHECK_GE(major_radius, minor_radius);
// Orientation lies in [-180, 180] for a stylus, and [-90, 90] for other
// touchscreen inputs. There are exceptions on Android when a device is
// rotated, yielding touch orientations in the range of [-180, 180].
// Regardless, normalise to [-90, 90), allowing a small tolerance to account
// for floating point conversion.
// TODO(e_hakkinen): Also pass unaltered stylus orientation, avoiding loss of
// quadrant information, see crbug.com/493728.
DCHECK_GT(orientation_deg, -180.01f);
DCHECK_LT(orientation_deg, 180.01f);
if (orientation_deg >= 90.f) {
orientation_deg -= 180.f;
} else if (orientation_deg < -90.f) {
orientation_deg += 180.f;
}
if (orientation_deg >= 0) {
// The case orientation_deg == 0 is handled here on purpose: although the
// 'else' block is equivalent in this case, we want to pass the 0 value
// unchanged (and 0 is the default value for many devices that don't
// report elliptical touches).
touch.radius_x = minor_radius;
touch.radius_y = major_radius;
touch.rotation_angle = orientation_deg;
} else {
touch.radius_x = major_radius;
touch.radius_y = minor_radius;
touch.rotation_angle = orientation_deg + 90;
}
return touch;
}
#if !BUILDFLAG(IS_IOS_TVOS)
NSString* FilterSpecialCharacter(NSString* str) {
if ([str length] != 1) {
return str;
}
unichar c = [str characterAtIndex:0];
NSString* result = str;
if (c == 0x7F) {
// Backspace should be 8
result = @"\x8";
} else if (c >= 0xF700 && c <= 0xF7FF) {
// Mac private use characters should be @"\0" (@"" won't work)
// NSDeleteFunctionKey will also go into here
// Use the range 0xF700~0xF7FF to match
// http://www.opensource.apple.com/source/WebCore/WebCore-7601.1.55/platform/mac/KeyEventMac.mm
result = @"\0";
}
return result;
}
bool IsSystemKeyEvent(const blink::WebKeyboardEvent& event) {
// Windows and Linux set |isSystemKey| if alt is down. Blink looks at this
// flag to decide if it should handle a key or not. E.g. alt-left/right
// shouldn't be used by Blink to scroll the current page, because we want
// to get that key back for it to do history navigation. Hence, the
// corresponding situation on OS X is to set this for cmd key presses.
// cmd-b and and cmd-i are system wide key bindings that OS X doesn't
// handle for us, so the editor handles them.
int modifiers = event.GetModifiers() & blink::WebInputEvent::kInputModifiers;
if (modifiers == blink::WebInputEvent::kMetaKey &&
event.windows_key_code == ui::VKEY_B) {
return false;
}
if (modifiers == blink::WebInputEvent::kMetaKey &&
event.windows_key_code == ui::VKEY_I) {
return false;
}
return event.GetModifiers() & blink::WebInputEvent::kMetaKey;
}
#endif // !BUILDFLAG(IS_IOS_TVOS)
} // namespace
blink::WebKeyboardEvent WebKeyboardEventBuilder::Build(gfx::NativeEvent event) {
#if !BUILDFLAG(IS_IOS_TVOS)
BEKeyEntry* entry = std::get<base::apple::OwnedBEKeyEntry>(event).Get();
CHECK(entry);
ui::DomCode dom_code = ui::DomCodeFromBEKeyEntry(entry);
int modifiers = ModifiersFromEvent(entry.key.modifierFlags) |
ui::DomCodeToWebInputEventModifiers(dom_code);
blink::WebKeyboardEvent result(
entry.state == BEKeyPressStateUp ? blink::WebInputEvent::Type::kKeyUp
: blink::WebInputEvent::Type::kKeyDown,
modifiers, ui::EventTimeStampFromSeconds(entry.timestamp));
// the keyCode is the keyboard code in BEKeyEntry
auto key_code = static_cast<ui::KeyboardCode>(entry.key.keyCode);
bool is_numeric_keypad_keycode =
key_code >= ui::VKEY_NUMPAD0 && key_code <= ui::VKEY_NUMPAD9;
result.windows_key_code = is_numeric_keypad_keycode
? key_code
: ui::LocatedToNonLocatedKeyboardCode(key_code);
result.native_key_code = key_code;
result.dom_code = static_cast<int>(dom_code);
result.dom_key = ui::DomKeyFromBEKeyEntry(entry);
NSString* text_str = FilterSpecialCharacter(entry.key.characters);
NSString* unmodified_str = FilterSpecialCharacter(entry.key.characters);
// Always use 13 for Enter/Return -- we don't want to use AppKit's
// different character for Enter.
if (result.windows_key_code == '\r') {
text_str = @"\r";
unmodified_str = @"\r";
}
// Always use 9 for tab -- we don't want to use AppKit's different character
// for shift-tab.
if (result.windows_key_code == 9) {
text_str = @"\x9";
unmodified_str = @"\x9";
}
if ([text_str length] < blink::WebKeyboardEvent::kTextLengthCap &&
[unmodified_str length] < blink::WebKeyboardEvent::kTextLengthCap) {
[text_str getCharacters:reinterpret_cast<UniChar*>(&result.text[0])];
[unmodified_str
getCharacters:reinterpret_cast<UniChar*>(&result.unmodified_text[0])];
} else {
NOTIMPLEMENTED();
}
result.is_system_key = IsSystemKeyEvent(result);
return result;
#else
TVOS_NOT_YET_IMPLEMENTED();
return blink::WebKeyboardEvent();
#endif // !BUILDFLAG(IS_IOS_TVOS)
}
blink::WebGestureEvent WebGestureEventBuilder::Build(UIEvent*, UIView*) {
return blink::WebGestureEvent();
}
blink::WebTouchEvent WebTouchEventBuilder::Build(
blink::WebInputEvent::Type type,
UITouch* touch,
UIEvent* event,
UIView* view,
const std::optional<gfx::Vector2dF>& view_offset) {
blink::WebTouchEvent result(type, ModifiersFromEvent(event.modifierFlags),
ui::EventTimeStampFromSeconds([event timestamp]));
// TODO(dtapuska): Enable
// ui::ComputeEventLatencyOS(event);
result.dispatch_type =
result.GetType() == blink::WebInputEvent::Type::kTouchCancel
? blink::WebInputEvent::DispatchType::kEventNonBlocking
: blink::WebInputEvent::DispatchType::kBlocking;
result.hovering = type == blink::WebInputEvent::Type::kTouchEnd;
result.unique_touch_event_id = ui::GetNextTouchEventId();
size_t touch_index = 0;
if (type == blink::WebInputEvent::Type::kTouchStart) {
AddUITouch(touch);
}
result.touches[touch_index] =
CreateWebTouchPoint(view, touch, /*was_changed=*/true, view_offset);
++touch_index;
if (type == blink::WebInputEvent::Type::kTouchCancel ||
type == blink::WebInputEvent::Type::kTouchEnd) {
RemoveUITouch(touch);
}
// We can't use `event.allTouches` here, because we need to generate a
// WebTouchEvent for each touch point changing. But event.allTouches will have
// it all already.
for (size_t i = 0; i < MAX_POINTERS; ++i) {
if (!g_active_touches[i] || g_active_touches[i] == touch) {
continue;
}
result.touches[touch_index] = CreateWebTouchPoint(
view, g_active_touches[i], /*was_changed=*/false, view_offset);
++touch_index;
}
result.touches_length = touch_index;
DCHECK_GT(result.touches_length, 0U);
return result;
}
} // namespace input
|