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
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/exo/wayland/zcr_stylus.h"
#include "base/memory/raw_ptr.h"
#include "components/exo/pointer.h"
#include "components/exo/pointer_stylus_delegate.h"
#include "components/exo/touch.h"
#include "components/exo/touch_stylus_delegate.h"
#include "components/exo/wayland/server_util.h"
namespace exo::wayland {
namespace {
////////////////////////////////////////////////////////////////////////////////
// zcr_touch_stylus_v2 interface:
class WaylandTouchStylusDelegate : public TouchStylusDelegate {
public:
WaylandTouchStylusDelegate(wl_resource* resource, Touch* touch)
: resource_(resource), touch_(touch) {
touch_->SetStylusDelegate(this);
}
WaylandTouchStylusDelegate(const WaylandTouchStylusDelegate&) = delete;
WaylandTouchStylusDelegate& operator=(const WaylandTouchStylusDelegate&) =
delete;
~WaylandTouchStylusDelegate() override {
if (touch_ != nullptr)
touch_->SetStylusDelegate(nullptr);
}
void OnTouchDestroying(Touch* touch) override { touch_ = nullptr; }
void OnTouchTool(int touch_id, ui::EventPointerType type) override {
uint wayland_type = ZCR_TOUCH_STYLUS_V2_TOOL_TYPE_TOUCH;
if (type == ui::EventPointerType::kPen)
wayland_type = ZCR_TOUCH_STYLUS_V2_TOOL_TYPE_PEN;
else if (type == ui::EventPointerType::kEraser)
wayland_type = ZCR_TOUCH_STYLUS_V2_TOOL_TYPE_ERASER;
zcr_touch_stylus_v2_send_tool(resource_, touch_id, wayland_type);
}
void OnTouchForce(base::TimeTicks time_stamp,
int touch_id,
float force) override {
zcr_touch_stylus_v2_send_force(resource_,
TimeTicksToMilliseconds(time_stamp),
touch_id, wl_fixed_from_double(force));
}
void OnTouchTilt(base::TimeTicks time_stamp,
int touch_id,
const gfx::Vector2dF& tilt) override {
zcr_touch_stylus_v2_send_tilt(
resource_, TimeTicksToMilliseconds(time_stamp), touch_id,
wl_fixed_from_double(tilt.x()), wl_fixed_from_double(tilt.y()));
}
private:
raw_ptr<wl_resource> resource_;
raw_ptr<Touch> touch_;
};
void touch_stylus_destroy(wl_client* client, wl_resource* resource) {
wl_resource_destroy(resource);
}
const struct zcr_touch_stylus_v2_interface touch_stylus_implementation = {
touch_stylus_destroy};
////////////////////////////////////////////////////////////////////////////////
// zcr_pointer_stylus_v2 interface:
class WaylandPointerStylusDelegate : public PointerStylusDelegate {
public:
WaylandPointerStylusDelegate(wl_resource* resource, Pointer* pointer)
: resource_(resource), pointer_(pointer) {
pointer_->SetStylusDelegate(this);
}
WaylandPointerStylusDelegate(const WaylandPointerStylusDelegate&) = delete;
const WaylandPointerStylusDelegate& operator=(
const WaylandPointerStylusDelegate&) = delete;
~WaylandPointerStylusDelegate() override {
if (pointer_ != nullptr)
pointer_->SetStylusDelegate(nullptr);
}
void OnPointerDestroying(Pointer* pointer) override { pointer_ = nullptr; }
void OnPointerToolChange(ui::EventPointerType type) override {
uint wayland_type = ZCR_POINTER_STYLUS_V2_TOOL_TYPE_NONE;
if (type == ui::EventPointerType::kTouch)
wayland_type = ZCR_POINTER_STYLUS_V2_TOOL_TYPE_TOUCH;
else if (type == ui::EventPointerType::kPen)
wayland_type = ZCR_POINTER_STYLUS_V2_TOOL_TYPE_PEN;
else if (type == ui::EventPointerType::kEraser)
wayland_type = ZCR_POINTER_STYLUS_V2_TOOL_TYPE_ERASER;
zcr_pointer_stylus_v2_send_tool(resource_, wayland_type);
supports_force_ = false;
supports_tilt_ = false;
}
void OnPointerForce(base::TimeTicks time_stamp, float force) override {
// Set the force as 0 if the current tool previously reported a valid
// force, but is now reporting a NaN value indicating that force is not
// supported.
if (std::isnan(force)) {
if (supports_force_)
force = 0;
else
return;
}
supports_force_ = true;
zcr_pointer_stylus_v2_send_force(resource_,
TimeTicksToMilliseconds(time_stamp),
wl_fixed_from_double(force));
}
void OnPointerTilt(base::TimeTicks time_stamp,
const gfx::Vector2dF& tilt) override {
if (!supports_tilt_ && tilt.IsZero())
return;
supports_tilt_ = true;
zcr_pointer_stylus_v2_send_tilt(
resource_, TimeTicksToMilliseconds(time_stamp),
wl_fixed_from_double(tilt.x()), wl_fixed_from_double(tilt.y()));
}
private:
raw_ptr<wl_resource> resource_;
raw_ptr<Pointer> pointer_;
bool supports_force_ = false;
bool supports_tilt_ = false;
};
void pointer_stylus_destroy(wl_client* client, wl_resource* resource) {
wl_resource_destroy(resource);
}
const struct zcr_pointer_stylus_v2_interface pointer_stylus_implementation = {
pointer_stylus_destroy};
////////////////////////////////////////////////////////////////////////////////
// zcr_stylus_v2 interface:
void stylus_get_touch_stylus(wl_client* client,
wl_resource* resource,
uint32_t id,
wl_resource* touch_resource) {
Touch* touch = GetUserDataAs<Touch>(touch_resource);
if (touch->HasStylusDelegate()) {
wl_resource_post_error(
resource, ZCR_STYLUS_V2_ERROR_TOUCH_STYLUS_EXISTS,
"touch has already been associated with a stylus object");
return;
}
wl_resource* stylus_resource =
wl_resource_create(client, &zcr_touch_stylus_v2_interface,
wl_resource_get_version(resource), id);
SetImplementation(
stylus_resource, &touch_stylus_implementation,
std::make_unique<WaylandTouchStylusDelegate>(stylus_resource, touch));
}
void stylus_get_pointer_stylus(wl_client* client,
wl_resource* resource,
uint32_t id,
wl_resource* pointer_resource) {
Pointer* pointer = GetUserDataAs<Pointer>(pointer_resource);
if (pointer->HasStylusDelegate()) {
wl_resource_post_error(
resource, ZCR_STYLUS_V2_ERROR_POINTER_STYLUS_EXISTS,
"pointer has already been associated with a stylus object");
return;
}
wl_resource* stylus_resource =
wl_resource_create(client, &zcr_pointer_stylus_v2_interface,
wl_resource_get_version(resource), id);
SetImplementation(
stylus_resource, &pointer_stylus_implementation,
std::make_unique<WaylandPointerStylusDelegate>(stylus_resource, pointer));
}
const struct zcr_stylus_v2_interface stylus_v2_implementation = {
stylus_get_touch_stylus, stylus_get_pointer_stylus};
} // namespace
void bind_stylus_v2(wl_client* client,
void* data,
uint32_t version,
uint32_t id) {
wl_resource* resource =
wl_resource_create(client, &zcr_stylus_v2_interface, version, id);
wl_resource_set_implementation(resource, &stylus_v2_implementation, data,
nullptr);
}
} // namespace exo::wayland
|