OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "mojo/services/native_viewport/platform_viewport.h" |
| 6 |
| 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" |
| 20 |
| 21 namespace native_viewport { |
| 22 namespace { |
| 23 |
| 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)))); |
| 31 } |
| 32 } // namespace |
| 33 |
| 34 class PlatformViewportX11 : public PlatformViewport, |
| 35 public ui::PlatformWindowDelegate { |
| 36 public: |
| 37 explicit PlatformViewportX11(Delegate* delegate) : delegate_(delegate) { |
| 38 } |
| 39 |
| 40 ~PlatformViewportX11() override { |
| 41 // Destroy the platform-window while |this| is still alive. |
| 42 platform_window_.reset(); |
| 43 } |
| 44 |
| 45 private: |
| 46 // Overridden from PlatformViewport: |
| 47 void Init(const gfx::Rect& bounds) override { |
| 48 CHECK(!event_source_); |
| 49 CHECK(!platform_window_); |
| 50 |
| 51 event_source_ = ui::PlatformEventSource::CreateDefault(); |
| 52 |
| 53 metrics_ = mojo::ViewportMetrics::New(); |
| 54 metrics_->size = mojo::Size::From(bounds.size()); |
| 55 |
| 56 platform_window_.reset(new ui::X11Window(this)); |
| 57 platform_window_->SetBounds(bounds); |
| 58 } |
| 59 |
| 60 void Show() override { platform_window_->Show(); } |
| 61 |
| 62 void Hide() override { platform_window_->Hide(); } |
| 63 |
| 64 void Close() override { platform_window_->Close(); } |
| 65 |
| 66 gfx::Size GetSize() override { return metrics_->size.To<gfx::Size>(); } |
| 67 |
| 68 void SetBounds(const gfx::Rect& bounds) override { |
| 69 platform_window_->SetBounds(bounds); |
| 70 } |
| 71 |
| 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()); |
| 76 } |
| 77 |
| 78 void OnDamageRect(const gfx::Rect& damaged_region) override {} |
| 79 |
| 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()); |
| 92 } |
| 93 delegate_->OnEvent(mojo_event.Pass()); |
| 94 |
| 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; |
| 106 } |
| 107 |
| 108 // We want to emulate the WM_CHAR generation behaviour of Windows. |
| 109 // |
| 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. |
| 116 // |
| 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()); |
| 124 |
| 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()); |
| 128 |
| 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()); |
| 135 |
| 136 delegate_->OnEvent(mojo::Event::From(char_event)); |
| 137 } |
| 138 } |
| 139 |
| 140 void OnCloseRequest() override { platform_window_->Close(); } |
| 141 |
| 142 void OnClosed() override { delegate_->OnDestroyed(); } |
| 143 |
| 144 void OnWindowStateChanged(ui::PlatformWindowState state) override {} |
| 145 |
| 146 void OnLostCapture() override {} |
| 147 |
| 148 void OnAcceleratedWidgetAvailable(gfx::AcceleratedWidget widget) override { |
| 149 delegate_->OnAcceleratedWidgetAvailable(widget); |
| 150 } |
| 151 |
| 152 void OnActivationChanged(bool active) override {} |
| 153 |
| 154 scoped_ptr<ui::PlatformEventSource> event_source_; |
| 155 scoped_ptr<ui::PlatformWindow> platform_window_; |
| 156 Delegate* delegate_; |
| 157 mojo::ViewportMetricsPtr metrics_; |
| 158 |
| 159 DISALLOW_COPY_AND_ASSIGN(PlatformViewportX11); |
| 160 }; |
| 161 |
| 162 // static |
| 163 scoped_ptr<PlatformViewport> PlatformViewport::Create(Delegate* delegate) { |
| 164 return make_scoped_ptr(new PlatformViewportX11(delegate)); |
| 165 } |
| 166 |
| 167 } // namespace native_viewport |
OLD | NEW |