| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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" | 7 #include "base/command_line.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "mojo/converters/geometry/geometry_type_converters.h" | 9 #include "mojo/converters/geometry/geometry_type_converters.h" |
| 10 #include "mojo/converters/input_events/input_events_type_converters.h" |
| 10 #include "mojo/converters/input_events/mojo_extended_key_event_data.h" | 11 #include "mojo/converters/input_events/mojo_extended_key_event_data.h" |
| 11 #include "ui/events/event.h" | 12 #include "ui/events/event.h" |
| 12 #include "ui/events/event_utils.h" | 13 #include "ui/events/event_utils.h" |
| 13 #include "ui/events/platform/platform_event_dispatcher.h" | 14 #include "ui/events/platform/platform_event_dispatcher.h" |
| 14 #include "ui/events/platform/platform_event_source.h" | 15 #include "ui/events/platform/platform_event_source.h" |
| 15 #include "ui/gfx/rect.h" | 16 #include "ui/gfx/rect.h" |
| 16 #include "ui/platform_window/platform_window.h" | 17 #include "ui/platform_window/platform_window.h" |
| 17 #include "ui/platform_window/platform_window_delegate.h" | 18 #include "ui/platform_window/platform_window_delegate.h" |
| 18 #include "ui/platform_window/x11/x11_window.h" | 19 #include "ui/platform_window/x11/x11_window.h" |
| 19 | 20 |
| 20 namespace native_viewport { | 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 |
| 21 | 33 |
| 22 class PlatformViewportX11 : public PlatformViewport, | 34 class PlatformViewportX11 : public PlatformViewport, |
| 23 public ui::PlatformWindowDelegate { | 35 public ui::PlatformWindowDelegate { |
| 24 public: | 36 public: |
| 25 explicit PlatformViewportX11(Delegate* delegate) : delegate_(delegate) { | 37 explicit PlatformViewportX11(Delegate* delegate) : delegate_(delegate) { |
| 26 } | 38 } |
| 27 | 39 |
| 28 ~PlatformViewportX11() override { | 40 ~PlatformViewportX11() override { |
| 29 // Destroy the platform-window while |this| is still alive. | 41 // Destroy the platform-window while |this| is still alive. |
| 30 platform_window_.reset(); | 42 platform_window_.reset(); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 50 void Hide() override { platform_window_->Hide(); } | 62 void Hide() override { platform_window_->Hide(); } |
| 51 | 63 |
| 52 void Close() override { platform_window_->Close(); } | 64 void Close() override { platform_window_->Close(); } |
| 53 | 65 |
| 54 gfx::Size GetSize() override { return metrics_->size.To<gfx::Size>(); } | 66 gfx::Size GetSize() override { return metrics_->size.To<gfx::Size>(); } |
| 55 | 67 |
| 56 void SetBounds(const gfx::Rect& bounds) override { | 68 void SetBounds(const gfx::Rect& bounds) override { |
| 57 platform_window_->SetBounds(bounds); | 69 platform_window_->SetBounds(bounds); |
| 58 } | 70 } |
| 59 | 71 |
| 60 void SetCapture() override { platform_window_->SetCapture(); } | |
| 61 | |
| 62 void ReleaseCapture() override { platform_window_->ReleaseCapture(); } | |
| 63 | |
| 64 // ui::PlatformWindowDelegate: | 72 // ui::PlatformWindowDelegate: |
| 65 void OnBoundsChanged(const gfx::Rect& new_bounds) override { | 73 void OnBoundsChanged(const gfx::Rect& new_bounds) override { |
| 66 metrics_->size = mojo::Size::From(new_bounds.size()); | 74 metrics_->size = mojo::Size::From(new_bounds.size()); |
| 67 delegate_->OnMetricsChanged(metrics_.Clone()); | 75 delegate_->OnMetricsChanged(metrics_.Clone()); |
| 68 } | 76 } |
| 69 | 77 |
| 70 void OnDamageRect(const gfx::Rect& damaged_region) override {} | 78 void OnDamageRect(const gfx::Rect& damaged_region) override {} |
| 71 | 79 |
| 72 void DispatchEvent(ui::Event* event) override { | 80 void DispatchEvent(ui::Event* event) override { |
| 73 delegate_->OnEvent(event); | 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 } |
| 74 | 107 |
| 75 // We want to emulate the WM_CHAR generation behaviour of Windows. | 108 // We want to emulate the WM_CHAR generation behaviour of Windows. |
| 76 // | 109 // |
| 77 // On Linux, we've previously inserted characters by having | 110 // On Linux, we've previously inserted characters by having |
| 78 // InputMethodAuraLinux take all key down events and send a character event | 111 // InputMethodAuraLinux take all key down events and send a character event |
| 79 // to the TextInputClient. This causes a mismatch in code that has to be | 112 // to the TextInputClient. This causes a mismatch in code that has to be |
| 80 // shared between Windows and Linux, including blink code. Now that we're | 113 // shared between Windows and Linux, including blink code. Now that we're |
| 81 // trying to have one way of doing things, we need to standardize on and | 114 // trying to have one way of doing things, we need to standardize on and |
| 82 // emulate Windows character events. | 115 // emulate Windows character events. |
| 83 // | 116 // |
| 84 // This is equivalent to what we're doing in the current Linux port, but | 117 // This is equivalent to what we're doing in the current Linux port, but |
| 85 // done once instead of done multiple times in different places. | 118 // done once instead of done multiple times in different places. |
| 86 if (event->type() == ui::ET_KEY_PRESSED) { | 119 if (event->type() == ui::ET_KEY_PRESSED) { |
| 87 ui::KeyEvent* key_press_event = static_cast<ui::KeyEvent*>(event); | 120 ui::KeyEvent* key_press_event = static_cast<ui::KeyEvent*>(event); |
| 88 ui::KeyEvent char_event(key_press_event->GetCharacter(), | 121 ui::KeyEvent char_event(key_press_event->GetCharacter(), |
| 89 key_press_event->key_code(), | 122 key_press_event->key_code(), |
| 90 key_press_event->flags()); | 123 key_press_event->flags()); |
| 91 | 124 |
| 92 DCHECK_EQ(key_press_event->GetCharacter(), char_event.GetCharacter()); | 125 DCHECK_EQ(key_press_event->GetCharacter(), char_event.GetCharacter()); |
| 93 DCHECK_EQ(key_press_event->key_code(), char_event.key_code()); | 126 DCHECK_EQ(key_press_event->key_code(), char_event.key_code()); |
| 94 DCHECK_EQ(key_press_event->flags(), char_event.flags()); | 127 DCHECK_EQ(key_press_event->flags(), char_event.flags()); |
| 95 | 128 |
| 96 char_event.SetExtendedKeyEventData( | 129 char_event.SetExtendedKeyEventData( |
| 97 make_scoped_ptr(new mojo::MojoExtendedKeyEventData( | 130 make_scoped_ptr(new mojo::MojoExtendedKeyEventData( |
| 98 key_press_event->GetLocatedWindowsKeyboardCode(), | 131 key_press_event->GetLocatedWindowsKeyboardCode(), |
| 99 key_press_event->GetText(), | 132 key_press_event->GetText(), |
| 100 key_press_event->GetUnmodifiedText()))); | 133 key_press_event->GetUnmodifiedText()))); |
| 101 char_event.set_platform_keycode(key_press_event->platform_keycode()); | 134 char_event.set_platform_keycode(key_press_event->platform_keycode()); |
| 102 | 135 |
| 103 delegate_->OnEvent(&char_event); | 136 delegate_->OnEvent(mojo::Event::From(char_event)); |
| 104 } | 137 } |
| 105 } | 138 } |
| 106 | 139 |
| 107 void OnCloseRequest() override { platform_window_->Close(); } | 140 void OnCloseRequest() override { platform_window_->Close(); } |
| 108 | 141 |
| 109 void OnClosed() override { delegate_->OnDestroyed(); } | 142 void OnClosed() override { delegate_->OnDestroyed(); } |
| 110 | 143 |
| 111 void OnWindowStateChanged(ui::PlatformWindowState state) override {} | 144 void OnWindowStateChanged(ui::PlatformWindowState state) override {} |
| 112 | 145 |
| 113 void OnLostCapture() override {} | 146 void OnLostCapture() override {} |
| (...skipping 11 matching lines...) Expand all Loading... |
| 125 | 158 |
| 126 DISALLOW_COPY_AND_ASSIGN(PlatformViewportX11); | 159 DISALLOW_COPY_AND_ASSIGN(PlatformViewportX11); |
| 127 }; | 160 }; |
| 128 | 161 |
| 129 // static | 162 // static |
| 130 scoped_ptr<PlatformViewport> PlatformViewport::Create(Delegate* delegate) { | 163 scoped_ptr<PlatformViewport> PlatformViewport::Create(Delegate* delegate) { |
| 131 return make_scoped_ptr(new PlatformViewportX11(delegate)); | 164 return make_scoped_ptr(new PlatformViewportX11(delegate)); |
| 132 } | 165 } |
| 133 | 166 |
| 134 } // namespace native_viewport | 167 } // namespace native_viewport |
| OLD | NEW |