Commit c5d14022 authored by Shawn Gallea's avatar Shawn Gallea Committed by Commit Bot

Add WebviewClient touch support

This allows users to experiment with
webviews via touch.

Bug: 1011977
Test: Run client, experiment.
Change-Id: Iffbb528e5858ad8e6fbab122633ee14d25428d02
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2122912Reviewed-by: default avatarDaniel Nicoara <dnicoara@chromium.org>
Commit-Queue: Shawn Gallea <sagallea@google.com>
Cr-Commit-Position: refs/heads/master@{#757645}
parent a6153cfe
......@@ -15,6 +15,8 @@ executable("wayland_webview_client") {
"//components/exo/wayland:client_support",
"//skia",
"//third_party/wayland:wayland_client",
"//ui/events:event_constants",
"//ui/events/types:headers",
"//ui/gl",
]
}
......@@ -13,6 +13,7 @@
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/time/time.h"
#include "chromecast/browser/webview/proto/webview.pb.h"
#include "third_party/grpc/src/include/grpcpp/grpcpp.h"
#include "third_party/skia/include/gpu/GrContext.h"
......@@ -47,6 +48,8 @@ void BufferReleaseCallback(void* data, wl_buffer* /* buffer */) {
} // namespace
using chromecast::webview::InputEvent;
using chromecast::webview::TouchInput;
using chromecast::webview::WebviewRequest;
using chromecast::webview::WebviewResponse;
......@@ -182,6 +185,34 @@ void WebviewClient::DestroyWebview(const std::vector<std::string>& tokens) {
webviews_.erase(id);
}
void WebviewClient::HandleDown(void* data,
struct wl_touch* wl_touch,
uint32_t serial,
uint32_t time,
struct wl_surface* surface,
int32_t id,
wl_fixed_t x,
wl_fixed_t y) {
gfx::Point touch_point(wl_fixed_to_int(x), wl_fixed_to_int(y));
auto iter = std::find_if(
webviews_.begin(), webviews_.end(),
[surface](const std::pair<const int, std::unique_ptr<Webview>>& pair) {
const auto& webview = pair.second;
return webview->surface.get() == surface;
});
if (iter == webviews_.end()) {
focused_webview_ = nullptr;
return;
}
const Webview* webview = iter->second.get();
focused_webview_ = webview;
SendTouchInput(focused_webview_, touch_point.x(), touch_point.y(),
ui::ET_TOUCH_PRESSED, time, id);
points_[id] = touch_point;
}
void WebviewClient::HandleMode(void* data,
struct wl_output* wl_output,
uint32_t flags,
......@@ -220,6 +251,34 @@ void WebviewClient::HandleMode(void* data,
wl_surface_set_input_region(surface_.get(), opaque_region.get());
}
void WebviewClient::HandleMotion(void* data,
struct wl_touch* wl_touch,
uint32_t time,
int32_t id,
wl_fixed_t x,
wl_fixed_t y) {
if (!focused_webview_)
return;
gfx::Point& touch_point = points_[id];
touch_point.set_x(wl_fixed_to_int(x));
touch_point.set_y(wl_fixed_to_int(y));
SendTouchInput(focused_webview_, touch_point.x(), touch_point.y(),
ui::ET_TOUCH_MOVED, time, id);
}
void WebviewClient::HandleUp(void* data,
struct wl_touch* wl_touch,
uint32_t serial,
uint32_t time,
int32_t id) {
if (!focused_webview_)
return;
const gfx::Point& touch_point = points_[id];
SendTouchInput(focused_webview_, touch_point.x(), touch_point.y(),
ui::ET_TOUCH_RELEASED, time, id);
points_.erase(id);
}
void WebviewClient::InputCallback() {
std::string request;
getline(std::cin, request);
......@@ -337,6 +396,32 @@ void WebviewClient::SendResizeRequest(const std::vector<std::string>& tokens) {
CreateBuffer(gfx::Size(width, height), drm_format_, bo_usage_);
}
void WebviewClient::SendTouchInput(const Webview* webview,
int x,
int y,
ui::EventType event_type,
uint32_t time,
int32_t id) {
auto touch_input = std::make_unique<TouchInput>();
touch_input->set_x(x);
touch_input->set_y(y);
touch_input->set_root_x(x);
touch_input->set_root_y(y);
touch_input->set_pointer_type(
static_cast<int>(ui::EventPointerType::POINTER_TYPE_TOUCH));
touch_input->set_pointer_id(id);
auto input_event = std::make_unique<InputEvent>();
input_event->set_event_type(event_type);
input_event->set_timestamp(time * base::Time::kMicrosecondsPerMillisecond);
input_event->set_allocated_touch(touch_input.release());
WebviewRequest input_request;
input_request.set_allocated_input(input_event.release());
if (!webview->client->Write(input_request))
LOG(ERROR) << "Input request failed";
}
void WebviewClient::SetPosition(const std::vector<std::string>& tokens) {
int id, x, y;
if (tokens.size() != 4 || !base::StringToInt(tokens[0], &id) ||
......
......@@ -15,6 +15,9 @@
#include "chromecast/browser/webview/proto/webview.grpc.pb.h"
#include "components/exo/wayland/clients/client_base.h"
#include "third_party/grpc/src/include/grpcpp/grpcpp.h"
#include "ui/events/event_constants.h"
#include "ui/events/types/event_type.h"
#include "ui/gfx/geometry/point.h"
namespace chromecast {
namespace client {
......@@ -50,17 +53,42 @@ class WebviewClient : public exo::wayland::clients::ClientBase {
void AllocateBuffers(const InitParams& params);
void CreateWebview(const std::vector<std::string>& tokens);
void DestroyWebview(const std::vector<std::string>& tokens);
void HandleDown(void* data,
struct wl_touch* wl_touch,
uint32_t serial,
uint32_t time,
struct wl_surface* surface,
int32_t id,
wl_fixed_t x,
wl_fixed_t y) override;
void HandleMode(void* data,
struct wl_output* wl_output,
uint32_t flags,
int32_t width,
int32_t height,
int32_t refresh) override;
void HandleMotion(void* data,
struct wl_touch* wl_touch,
uint32_t time,
int32_t id,
wl_fixed_t x,
wl_fixed_t y) override;
void HandleUp(void* data,
struct wl_touch* wl_touch,
uint32_t serial,
uint32_t time,
int32_t id) override;
void InputCallback();
void ListActiveWebviews();
void Paint();
void SendNavigationRequest(const std::vector<std::string>& tokens);
void SendResizeRequest(const std::vector<std::string>& tokens);
void SendTouchInput(const Webview* webview,
int x,
int y,
ui::EventType event_type,
uint32_t time,
int32_t id);
void SetPosition(const std::vector<std::string>& tokens);
void TakeExclusiveAccess();
void WlDisplayCallback();
......@@ -68,7 +96,9 @@ class WebviewClient : public exo::wayland::clients::ClientBase {
int32_t drm_format_ = 0;
int32_t bo_usage_ = 0;
const Webview* focused_webview_;
std::map<int, std::unique_ptr<Webview>> webviews_;
std::map<int32_t, gfx::Point> points_;
std::unique_ptr<wl_callback> frame_callback_;
std::vector<std::unique_ptr<BufferCallback>> buffer_callbacks_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment