Roll src/third_party/WebKit b3f094a:f697bbd (svn 194310:194313)
[chromium-blink-merge.git] / components / native_viewport / platform_viewport_x11.cc
blob8e02143b0764f662ce26142d2f7bec1ba64139d5
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/native_viewport/platform_viewport.h"
7 #include "base/command_line.h"
8 #include "base/message_loop/message_loop.h"
9 #include "mojo/converters/geometry/geometry_type_converters.h"
10 #include "mojo/converters/input_events/input_events_type_converters.h"
11 #include "mojo/converters/input_events/mojo_extended_key_event_data.h"
12 #include "ui/events/event.h"
13 #include "ui/events/event_utils.h"
14 #include "ui/events/platform/platform_event_dispatcher.h"
15 #include "ui/events/platform/platform_event_source.h"
16 #include "ui/gfx/geometry/rect.h"
17 #include "ui/platform_window/platform_window.h"
18 #include "ui/platform_window/platform_window_delegate.h"
19 #include "ui/platform_window/x11/x11_window.h"
21 namespace native_viewport {
22 namespace {
24 float ConvertUIWheelValueToMojoValue(int offset) {
25 // Mojo's event type takes a value between -1 and 1. Normalize by allowing
26 // up to 20 of ui's offset. This is a bit arbitrary.
27 return std::max(
28 -1.0f, std::min(1.0f, static_cast<float>(offset) /
29 (20 * static_cast<float>(
30 ui::MouseWheelEvent::kWheelDelta))));
32 } // namespace
34 class PlatformViewportX11 : public PlatformViewport,
35 public ui::PlatformWindowDelegate {
36 public:
37 explicit PlatformViewportX11(Delegate* delegate) : delegate_(delegate) {
40 ~PlatformViewportX11() override {
41 // Destroy the platform-window while |this| is still alive.
42 platform_window_.reset();
45 private:
46 // Overridden from PlatformViewport:
47 void Init(const gfx::Rect& bounds) override {
48 CHECK(!event_source_);
49 CHECK(!platform_window_);
51 event_source_ = ui::PlatformEventSource::CreateDefault();
53 metrics_ = mojo::ViewportMetrics::New();
54 metrics_->size = mojo::Size::From(bounds.size());
56 platform_window_.reset(new ui::X11Window(this));
57 platform_window_->SetBounds(bounds);
60 void Show() override { platform_window_->Show(); }
62 void Hide() override { platform_window_->Hide(); }
64 void Close() override { platform_window_->Close(); }
66 gfx::Size GetSize() override { return metrics_->size.To<gfx::Size>(); }
68 void SetBounds(const gfx::Rect& bounds) override {
69 platform_window_->SetBounds(bounds);
72 // ui::PlatformWindowDelegate:
73 void OnBoundsChanged(const gfx::Rect& new_bounds) override {
74 metrics_->size = mojo::Size::From(new_bounds.size());
75 delegate_->OnMetricsChanged(metrics_.Clone());
78 void OnDamageRect(const gfx::Rect& damaged_region) override {}
80 void DispatchEvent(ui::Event* event) override {
81 mojo::EventPtr mojo_event(mojo::Event::From(*event));
82 if (event->IsMouseWheelEvent()) {
83 // Mojo's event type has a different meaning for wheel events. Convert
84 // between the two.
85 ui::MouseWheelEvent* wheel_event =
86 static_cast<ui::MouseWheelEvent*>(event);
87 DCHECK(mojo_event->pointer_data);
88 mojo_event->pointer_data->horizontal_wheel =
89 ConvertUIWheelValueToMojoValue(wheel_event->x_offset());
90 mojo_event->pointer_data->horizontal_wheel =
91 ConvertUIWheelValueToMojoValue(wheel_event->y_offset());
93 delegate_->OnEvent(mojo_event.Pass());
95 switch (event->type()) {
96 case ui::ET_MOUSE_PRESSED:
97 case ui::ET_TOUCH_PRESSED:
98 platform_window_->SetCapture();
99 break;
100 case ui::ET_MOUSE_RELEASED:
101 case ui::ET_TOUCH_RELEASED:
102 platform_window_->ReleaseCapture();
103 break;
104 default:
105 break;
108 // We want to emulate the WM_CHAR generation behaviour of Windows.
110 // On Linux, we've previously inserted characters by having
111 // InputMethodAuraLinux take all key down events and send a character event
112 // to the TextInputClient. This causes a mismatch in code that has to be
113 // shared between Windows and Linux, including blink code. Now that we're
114 // trying to have one way of doing things, we need to standardize on and
115 // emulate Windows character events.
117 // This is equivalent to what we're doing in the current Linux port, but
118 // done once instead of done multiple times in different places.
119 if (event->type() == ui::ET_KEY_PRESSED) {
120 ui::KeyEvent* key_press_event = static_cast<ui::KeyEvent*>(event);
121 ui::KeyEvent char_event(key_press_event->GetCharacter(),
122 key_press_event->key_code(),
123 key_press_event->flags());
125 DCHECK_EQ(key_press_event->GetCharacter(), char_event.GetCharacter());
126 DCHECK_EQ(key_press_event->key_code(), char_event.key_code());
127 DCHECK_EQ(key_press_event->flags(), char_event.flags());
129 char_event.SetExtendedKeyEventData(
130 make_scoped_ptr(new mojo::MojoExtendedKeyEventData(
131 key_press_event->GetLocatedWindowsKeyboardCode(),
132 key_press_event->GetText(),
133 key_press_event->GetUnmodifiedText())));
134 char_event.set_platform_keycode(key_press_event->platform_keycode());
136 delegate_->OnEvent(mojo::Event::From(char_event));
140 void OnCloseRequest() override { platform_window_->Close(); }
142 void OnClosed() override { delegate_->OnDestroyed(); }
144 void OnWindowStateChanged(ui::PlatformWindowState state) override {}
146 void OnLostCapture() override {}
148 void OnAcceleratedWidgetAvailable(gfx::AcceleratedWidget widget) override {
149 delegate_->OnAcceleratedWidgetAvailable(widget);
152 void OnActivationChanged(bool active) override {}
154 scoped_ptr<ui::PlatformEventSource> event_source_;
155 scoped_ptr<ui::PlatformWindow> platform_window_;
156 Delegate* delegate_;
157 mojo::ViewportMetricsPtr metrics_;
159 DISALLOW_COPY_AND_ASSIGN(PlatformViewportX11);
162 // static
163 scoped_ptr<PlatformViewport> PlatformViewport::Create(Delegate* delegate) {
164 return make_scoped_ptr(new PlatformViewportX11(delegate));
167 } // namespace native_viewport