OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "services/native_viewport/platform_viewport.h" | 5 #include "services/native_viewport/platform_viewport.h" |
6 | 6 |
7 #include "base/command_line.h" | |
8 #include "base/message_loop/message_loop.h" | |
9 #include "mojo/converters/geometry/geometry_type_converters.h" | 7 #include "mojo/converters/geometry/geometry_type_converters.h" |
10 #include "mojo/converters/input_events/input_events_type_converters.h" | 8 #include "mojo/converters/input_events/input_events_type_converters.h" |
11 #include "mojo/converters/input_events/mojo_extended_key_event_data.h" | 9 #include "mojo/converters/input_events/mojo_extended_key_event_data.h" |
12 #include "ui/events/event.h" | 10 #include "ui/events/event.h" |
13 #include "ui/events/event_utils.h" | |
14 #include "ui/events/platform/platform_event_dispatcher.h" | 11 #include "ui/events/platform/platform_event_dispatcher.h" |
15 #include "ui/events/platform/platform_event_source.h" | 12 #include "ui/events/platform/platform_event_source.h" |
16 #include "ui/gfx/rect.h" | 13 #include "ui/ozone/public/cursor_factory_ozone.h" |
| 14 #include "ui/ozone/public/ozone_platform.h" |
| 15 #include "ui/ozone/public/surface_factory_ozone.h" |
17 #include "ui/platform_window/platform_window.h" | 16 #include "ui/platform_window/platform_window.h" |
18 #include "ui/platform_window/platform_window_delegate.h" | 17 #include "ui/platform_window/platform_window_delegate.h" |
19 #include "ui/platform_window/x11/x11_window.h" | |
20 | 18 |
21 namespace native_viewport { | 19 namespace native_viewport { |
22 namespace { | 20 namespace { |
23 | 21 |
24 float ConvertUIWheelValueToMojoValue(int offset) { | 22 float ConvertUIWheelValueToMojoValue(int offset) { |
25 // Mojo's event type takes a value between -1 and 1. Normalize by allowing | 23 // 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. | 24 // up to 20 of ui's offset. This is a bit arbitrary. |
27 return std::max( | 25 return std::max( |
28 -1.0f, std::min(1.0f, static_cast<float>(offset) / | 26 -1.0f, std::min(1.0f, static_cast<float>(offset) / |
29 (20 * static_cast<float>( | 27 (20 * static_cast<float>( |
30 ui::MouseWheelEvent::kWheelDelta)))); | 28 ui::MouseWheelEvent::kWheelDelta)))); |
31 } | 29 } |
32 } // namespace | 30 } // namespace |
33 | 31 |
34 class PlatformViewportX11 : public PlatformViewport, | 32 // TODO(spang): Deduplicate with PlatformViewportX11.. but there's a hack |
35 public ui::PlatformWindowDelegate { | 33 // in there that prevents this. |
| 34 class PlatformViewportOzone : public PlatformViewport, |
| 35 public ui::PlatformWindowDelegate { |
36 public: | 36 public: |
37 explicit PlatformViewportX11(Delegate* delegate) : delegate_(delegate) {} | 37 explicit PlatformViewportOzone(Delegate* delegate) : delegate_(delegate) { |
| 38 ui::OzonePlatform::InitializeForUI(); |
| 39 } |
38 | 40 |
39 ~PlatformViewportX11() override { | 41 ~PlatformViewportOzone() override { |
40 // Destroy the platform-window while |this| is still alive. | 42 // Destroy the platform-window while |this| is still alive. |
41 platform_window_.reset(); | 43 platform_window_.reset(); |
42 } | 44 } |
43 | 45 |
44 private: | 46 private: |
45 // Overridden from PlatformViewport: | 47 // Overridden from PlatformViewport: |
46 void Init(const gfx::Rect& bounds) override { | 48 void Init(const gfx::Rect& bounds) override { |
47 CHECK(!event_source_); | 49 platform_window_ = |
48 CHECK(!platform_window_); | 50 ui::OzonePlatform::GetInstance()->CreatePlatformWindow(this, bounds); |
49 | |
50 event_source_ = ui::PlatformEventSource::CreateDefault(); | |
51 | 51 |
52 metrics_ = mojo::ViewportMetrics::New(); | 52 metrics_ = mojo::ViewportMetrics::New(); |
53 metrics_->size = mojo::Size::From(bounds.size()); | 53 metrics_->size = mojo::Size::From(bounds.size()); |
54 | |
55 platform_window_.reset(new ui::X11Window(this)); | |
56 platform_window_->SetBounds(bounds); | |
57 } | 54 } |
58 | 55 |
59 void Show() override { platform_window_->Show(); } | 56 void Show() override { platform_window_->Show(); } |
60 | 57 |
61 void Hide() override { platform_window_->Hide(); } | 58 void Hide() override { platform_window_->Hide(); } |
62 | 59 |
63 void Close() override { platform_window_->Close(); } | 60 void Close() override { platform_window_->Close(); } |
64 | 61 |
65 gfx::Size GetSize() override { return metrics_->size.To<gfx::Size>(); } | 62 gfx::Size GetSize() override { |
| 63 return platform_window_->GetBounds().size(); |
| 64 } |
66 | 65 |
67 void SetBounds(const gfx::Rect& bounds) override { | 66 void SetBounds(const gfx::Rect& bounds) override { |
68 platform_window_->SetBounds(bounds); | 67 platform_window_->SetBounds(bounds); |
69 } | 68 } |
70 | 69 |
71 // ui::PlatformWindowDelegate: | 70 // ui::PlatformWindowDelegate: |
72 void OnBoundsChanged(const gfx::Rect& new_bounds) override { | 71 void OnBoundsChanged(const gfx::Rect& new_bounds) override { |
73 metrics_->size = mojo::Size::From(new_bounds.size()); | 72 metrics_->size = mojo::Size::From(new_bounds.size()); |
74 delegate_->OnMetricsChanged(metrics_.Clone()); | 73 delegate_->OnMetricsChanged(metrics_.Clone()); |
75 } | 74 } |
(...skipping 20 matching lines...) Expand all Loading... |
96 case ui::ET_TOUCH_PRESSED: | 95 case ui::ET_TOUCH_PRESSED: |
97 platform_window_->SetCapture(); | 96 platform_window_->SetCapture(); |
98 break; | 97 break; |
99 case ui::ET_MOUSE_RELEASED: | 98 case ui::ET_MOUSE_RELEASED: |
100 case ui::ET_TOUCH_RELEASED: | 99 case ui::ET_TOUCH_RELEASED: |
101 platform_window_->ReleaseCapture(); | 100 platform_window_->ReleaseCapture(); |
102 break; | 101 break; |
103 default: | 102 default: |
104 break; | 103 break; |
105 } | 104 } |
106 | |
107 // We want to emulate the WM_CHAR generation behaviour of Windows. | |
108 // | |
109 // On Linux, we've previously inserted characters by having | |
110 // InputMethodAuraLinux take all key down events and send a character event | |
111 // to the TextInputClient. This causes a mismatch in code that has to be | |
112 // shared between Windows and Linux, including blink code. Now that we're | |
113 // trying to have one way of doing things, we need to standardize on and | |
114 // emulate Windows character events. | |
115 // | |
116 // This is equivalent to what we're doing in the current Linux port, but | |
117 // done once instead of done multiple times in different places. | |
118 if (event->type() == ui::ET_KEY_PRESSED) { | |
119 ui::KeyEvent* key_press_event = static_cast<ui::KeyEvent*>(event); | |
120 ui::KeyEvent char_event(key_press_event->GetCharacter(), | |
121 key_press_event->key_code(), | |
122 key_press_event->flags()); | |
123 | |
124 DCHECK_EQ(key_press_event->GetCharacter(), char_event.GetCharacter()); | |
125 DCHECK_EQ(key_press_event->key_code(), char_event.key_code()); | |
126 DCHECK_EQ(key_press_event->flags(), char_event.flags()); | |
127 | |
128 char_event.SetExtendedKeyEventData( | |
129 make_scoped_ptr(new mojo::MojoExtendedKeyEventData( | |
130 key_press_event->GetLocatedWindowsKeyboardCode(), | |
131 key_press_event->GetText(), | |
132 key_press_event->GetUnmodifiedText()))); | |
133 char_event.set_platform_keycode(key_press_event->platform_keycode()); | |
134 | |
135 delegate_->OnEvent(mojo::Event::From(char_event)); | |
136 } | |
137 } | 105 } |
138 | 106 |
139 void OnCloseRequest() override { platform_window_->Close(); } | 107 void OnCloseRequest() override { platform_window_->Close(); } |
140 | 108 |
141 void OnClosed() override { delegate_->OnDestroyed(); } | 109 void OnClosed() override { delegate_->OnDestroyed(); } |
142 | 110 |
143 void OnWindowStateChanged(ui::PlatformWindowState state) override {} | 111 void OnWindowStateChanged(ui::PlatformWindowState state) override {} |
144 | 112 |
145 void OnLostCapture() override {} | 113 void OnLostCapture() override {} |
146 | 114 |
147 void OnAcceleratedWidgetAvailable(gfx::AcceleratedWidget widget) override { | 115 void OnAcceleratedWidgetAvailable(gfx::AcceleratedWidget widget, |
| 116 float device_pixel_ratio) override { |
148 delegate_->OnAcceleratedWidgetAvailable(widget); | 117 delegate_->OnAcceleratedWidgetAvailable(widget); |
149 } | 118 } |
150 | 119 |
151 void OnActivationChanged(bool active) override {} | 120 void OnActivationChanged(bool active) override {} |
152 | 121 |
153 scoped_ptr<ui::PlatformEventSource> event_source_; | |
154 scoped_ptr<ui::PlatformWindow> platform_window_; | 122 scoped_ptr<ui::PlatformWindow> platform_window_; |
155 Delegate* delegate_; | 123 Delegate* delegate_; |
156 mojo::ViewportMetricsPtr metrics_; | 124 mojo::ViewportMetricsPtr metrics_; |
157 | 125 |
158 DISALLOW_COPY_AND_ASSIGN(PlatformViewportX11); | 126 DISALLOW_COPY_AND_ASSIGN(PlatformViewportOzone); |
159 }; | 127 }; |
160 | 128 |
161 // static | 129 // static |
162 scoped_ptr<PlatformViewport> PlatformViewport::Create( | 130 scoped_ptr<PlatformViewport> PlatformViewport::Create( |
163 mojo::ApplicationImpl* application_, | 131 mojo::ApplicationImpl* application_, |
164 Delegate* delegate) { | 132 Delegate* delegate) { |
165 return make_scoped_ptr(new PlatformViewportX11(delegate)); | 133 return scoped_ptr<PlatformViewport>( |
| 134 new PlatformViewportOzone(delegate)).Pass(); |
166 } | 135 } |
167 | 136 |
168 } // namespace native_viewport | 137 } // namespace native_viewport |
OLD | NEW |