blob: 815cce66fed668e5277c762b666b23b5bd408ff3 [file] [log] [blame]
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +00001// Copyright (c) 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/examples/aura_demo/window_tree_host_mojo.h"
6
7#include "mojo/examples/aura_demo/demo_context_factory.h"
8#include "mojo/public/bindings/allocation_scope.h"
9#include "mojo/public/gles2/gles2.h"
10#include "mojo/services/native_viewport/geometry_conversions.h"
11#include "ui/aura/env.h"
12#include "ui/aura/window.h"
Torne (Richard Coles)a1401312014-03-18 10:20:56 +000013#include "ui/aura/window_event_dispatcher.h"
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +000014#include "ui/compositor/compositor.h"
15#include "ui/events/event.h"
16#include "ui/events/event_constants.h"
17#include "ui/gfx/geometry/insets.h"
18#include "ui/gfx/geometry/rect.h"
19
20namespace mojo {
21namespace examples {
22
23// static
24ui::ContextFactory* WindowTreeHostMojo::context_factory_ = NULL;
25
26////////////////////////////////////////////////////////////////////////////////
27// WindowTreeHostMojo, public:
28
29WindowTreeHostMojo::WindowTreeHostMojo(
30 ScopedNativeViewportHandle viewport_handle,
31 const gfx::Rect& bounds,
32 const base::Callback<void()>& compositor_created_callback)
33 : native_viewport_(viewport_handle.Pass(), this),
34 compositor_created_callback_(compositor_created_callback),
35 bounds_(bounds) {
36 AllocationScope scope;
37 native_viewport_->Create(bounds);
38
39 ScopedMessagePipeHandle gles2_client_handle;
40 CreateMessagePipe(&gles2_handle_, &gles2_client_handle);
41
42 // The ContextFactory must exist before any Compositors are created.
43 if (!context_factory_) {
44 scoped_ptr<DemoContextFactory> cf(new DemoContextFactory(this));
45 if (cf->Initialize())
46 context_factory_ = cf.release();
47 ui::ContextFactory::SetInstance(context_factory_);
48 }
49 CHECK(context_factory_) << "No GL bindings.";
50
51 native_viewport_->CreateGLES2Context(gles2_client_handle.Pass());
52}
53
54WindowTreeHostMojo::~WindowTreeHostMojo() {}
55
56////////////////////////////////////////////////////////////////////////////////
57// WindowTreeHostMojo, aura::WindowTreeHost implementation:
58
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +000059gfx::AcceleratedWidget WindowTreeHostMojo::GetAcceleratedWidget() {
60 NOTIMPLEMENTED() << "GetAcceleratedWidget";
61 return gfx::kNullAcceleratedWidget;
62}
63
64void WindowTreeHostMojo::Show() {
65 window()->Show();
66 native_viewport_->Show();
67}
68
69void WindowTreeHostMojo::Hide() {
70 native_viewport_->Hide();
71 window()->Hide();
72}
73
74void WindowTreeHostMojo::ToggleFullScreen() {
75 NOTIMPLEMENTED();
76}
77
78gfx::Rect WindowTreeHostMojo::GetBounds() const {
79 return bounds_;
80}
81
82void WindowTreeHostMojo::SetBounds(const gfx::Rect& bounds) {
83 AllocationScope scope;
84 native_viewport_->SetBounds(bounds);
85}
86
87gfx::Insets WindowTreeHostMojo::GetInsets() const {
88 NOTIMPLEMENTED();
89 return gfx::Insets();
90}
91
92void WindowTreeHostMojo::SetInsets(const gfx::Insets& insets) {
93 NOTIMPLEMENTED();
94}
95
96gfx::Point WindowTreeHostMojo::GetLocationOnNativeScreen() const {
97 return gfx::Point(0, 0);
98}
99
100void WindowTreeHostMojo::SetCapture() {
101 NOTIMPLEMENTED();
102}
103
104void WindowTreeHostMojo::ReleaseCapture() {
105 NOTIMPLEMENTED();
106}
107
108bool WindowTreeHostMojo::QueryMouseLocation(gfx::Point* location_return) {
109 NOTIMPLEMENTED() << "QueryMouseLocation";
110 return false;
111}
112
113bool WindowTreeHostMojo::ConfineCursorToRootWindow() {
114 NOTIMPLEMENTED();
115 return false;
116}
117
118void WindowTreeHostMojo::UnConfineCursor() {
119 NOTIMPLEMENTED();
120}
121
122void WindowTreeHostMojo::PostNativeEvent(
123 const base::NativeEvent& native_event) {
124 NOTIMPLEMENTED();
125}
126
127void WindowTreeHostMojo::OnDeviceScaleFactorChanged(float device_scale_factor) {
128 NOTIMPLEMENTED();
129}
130
131void WindowTreeHostMojo::PrepareForShutdown() {
132 NOTIMPLEMENTED();
133}
134
135void WindowTreeHostMojo::SetCursorNative(gfx::NativeCursor cursor) {
136 NOTIMPLEMENTED();
137}
138
139void WindowTreeHostMojo::MoveCursorToNative(const gfx::Point& location) {
140 NOTIMPLEMENTED();
141}
142
143void WindowTreeHostMojo::OnCursorVisibilityChangedNative(bool show) {
144 NOTIMPLEMENTED();
145}
146
147////////////////////////////////////////////////////////////////////////////////
148// WindowTreeHostMojo, ui::EventSource implementation:
149
150ui::EventProcessor* WindowTreeHostMojo::GetEventProcessor() {
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000151 return dispatcher();
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000152}
153
154////////////////////////////////////////////////////////////////////////////////
155// WindowTreeHostMojo, NativeViewportClient implementation:
156
157void WindowTreeHostMojo::OnCreated() {
158 CreateCompositor(GetAcceleratedWidget());
159 compositor_created_callback_.Run();
160}
161
162void WindowTreeHostMojo::OnBoundsChanged(const Rect& bounds) {
163 bounds_ = gfx::Rect(bounds.position().x(), bounds.position().y(),
164 bounds.size().width(), bounds.size().height());
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000165 window()->SetBounds(gfx::Rect(bounds_.size()));
166 OnHostResized(bounds_.size());
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000167}
168
169void WindowTreeHostMojo::OnDestroyed() {
170 base::MessageLoop::current()->Quit();
171}
172
173void WindowTreeHostMojo::OnEvent(const Event& event) {
174 if (!event.location().is_null())
175 native_viewport_->AckEvent(event);
176
177 switch (event.action()) {
178 case ui::ET_MOUSE_PRESSED:
179 case ui::ET_MOUSE_DRAGGED:
180 case ui::ET_MOUSE_RELEASED:
181 case ui::ET_MOUSE_MOVED:
182 case ui::ET_MOUSE_ENTERED:
183 case ui::ET_MOUSE_EXITED: {
184 gfx::Point location(event.location().x(), event.location().y());
185 ui::MouseEvent ev(static_cast<ui::EventType>(event.action()), location,
186 location, event.flags(), 0);
187 SendEventToProcessor(&ev);
188 break;
189 }
190 case ui::ET_KEY_PRESSED:
191 case ui::ET_KEY_RELEASED: {
192 ui::KeyEvent ev(
193 static_cast<ui::EventType>(event.action()),
194 static_cast<ui::KeyboardCode>(event.key_data().key_code()),
195 event.flags(), event.key_data().is_char());
196 SendEventToProcessor(&ev);
197 break;
198 }
199 // TODO(beng): touch, etc.
200 }
201};
202
203} // namespace examples
204} // namespace mojo