Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / mojo / examples / aura_demo / root_window_host_mojo.cc
1 // 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/root_window_host_mojo.h"
6
7 #include "mojo/examples/aura_demo/demo_context_factory.h"
8 #include "mojo/examples/compositor_app/gles2_client_impl.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"
13 #include "ui/aura/window_tree_host_delegate.h"
14 #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
20 namespace mojo {
21 namespace examples {
22
23 // static
24 ui::ContextFactory* WindowTreeHostMojo::context_factory_ = NULL;
25
26 ////////////////////////////////////////////////////////////////////////////////
27 // WindowTreeHostMojo, public:
28
29 WindowTreeHostMojo::WindowTreeHostMojo(
30     ScopedMessagePipeHandle 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   AllocationScope scope;
36   native_viewport_->Create(bounds);
37
38   ScopedMessagePipeHandle gles2_handle;
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   gles2_client_.reset(new GLES2ClientImpl(
52       gles2_handle.Pass(),
53       base::Bind(&WindowTreeHostMojo::DidCreateContext,
54                  base::Unretained(this))));
55   native_viewport_->CreateGLES2Context(gles2_client_handle.Pass());
56 }
57
58 WindowTreeHostMojo::~WindowTreeHostMojo() {}
59
60 ////////////////////////////////////////////////////////////////////////////////
61 // WindowTreeHostMojo, aura::WindowTreeHost implementation:
62
63 aura::RootWindow* WindowTreeHostMojo::GetRootWindow() {
64   return delegate_->AsRootWindow();
65 }
66
67 gfx::AcceleratedWidget WindowTreeHostMojo::GetAcceleratedWidget() {
68   NOTIMPLEMENTED() << "GetAcceleratedWidget";
69   return gfx::kNullAcceleratedWidget;
70 }
71
72 void WindowTreeHostMojo::Show() {
73   native_viewport_->Show();
74 }
75
76 void WindowTreeHostMojo::Hide() {
77   NOTIMPLEMENTED();
78 }
79
80 void WindowTreeHostMojo::ToggleFullScreen() {
81   NOTIMPLEMENTED();
82 }
83
84 gfx::Rect WindowTreeHostMojo::GetBounds() const {
85   return bounds_;
86 }
87
88 void WindowTreeHostMojo::SetBounds(const gfx::Rect& bounds) {
89   AllocationScope scope;
90   native_viewport_->SetBounds(bounds);
91 }
92
93 gfx::Insets WindowTreeHostMojo::GetInsets() const {
94   NOTIMPLEMENTED();
95   return gfx::Insets();
96 }
97
98 void WindowTreeHostMojo::SetInsets(const gfx::Insets& insets) {
99   NOTIMPLEMENTED();
100 }
101
102 gfx::Point WindowTreeHostMojo::GetLocationOnNativeScreen() const {
103   return gfx::Point(0, 0);
104 }
105
106 void WindowTreeHostMojo::SetCapture() {
107   NOTIMPLEMENTED();
108 }
109
110 void WindowTreeHostMojo::ReleaseCapture() {
111   NOTIMPLEMENTED();
112 }
113
114 void WindowTreeHostMojo::SetCursor(gfx::NativeCursor cursor) {
115   NOTIMPLEMENTED();
116 }
117
118 bool WindowTreeHostMojo::QueryMouseLocation(gfx::Point* location_return) {
119   NOTIMPLEMENTED() << "QueryMouseLocation";
120   return false;
121 }
122
123 bool WindowTreeHostMojo::ConfineCursorToRootWindow() {
124   NOTIMPLEMENTED();
125   return false;
126 }
127
128 void WindowTreeHostMojo::UnConfineCursor() {
129   NOTIMPLEMENTED();
130 }
131
132 void WindowTreeHostMojo::OnCursorVisibilityChanged(bool show) {
133   NOTIMPLEMENTED();
134 }
135
136 void WindowTreeHostMojo::MoveCursorTo(const gfx::Point& location) {
137   NOTIMPLEMENTED();
138 }
139
140 void WindowTreeHostMojo::PostNativeEvent(
141     const base::NativeEvent& native_event) {
142   NOTIMPLEMENTED();
143 }
144
145 void WindowTreeHostMojo::OnDeviceScaleFactorChanged(float device_scale_factor) {
146   NOTIMPLEMENTED();
147 }
148
149 void WindowTreeHostMojo::PrepareForShutdown() {
150   NOTIMPLEMENTED();
151 }
152
153 ////////////////////////////////////////////////////////////////////////////////
154 // WindowTreeHostMojo, NativeViewportClient implementation:
155
156 void WindowTreeHostMojo::OnCreated() {
157 }
158
159 void WindowTreeHostMojo::OnBoundsChanged(const Rect& bounds) {
160   bounds_ = gfx::Rect(bounds.position().x(), bounds.position().y(),
161                       bounds.size().width(), bounds.size().height());
162   window()->SetBounds(gfx::Rect(bounds_.size()));
163 }
164
165 void WindowTreeHostMojo::OnDestroyed() {
166   base::MessageLoop::current()->Quit();
167 }
168
169 void WindowTreeHostMojo::OnEvent(const Event& event) {
170   if (!event.location().is_null())
171     native_viewport_->AckEvent(event);
172
173   switch (event.action()) {
174     case ui::ET_MOUSE_PRESSED:
175     case ui::ET_MOUSE_DRAGGED:
176     case ui::ET_MOUSE_RELEASED:
177     case ui::ET_MOUSE_MOVED:
178     case ui::ET_MOUSE_ENTERED:
179     case ui::ET_MOUSE_EXITED: {
180       gfx::Point location(event.location().x(), event.location().y());
181       ui::MouseEvent ev(static_cast<ui::EventType>(event.action()), location,
182                         location, event.flags(), 0);
183       delegate_->OnHostMouseEvent(&ev);
184       break;
185     }
186     case ui::ET_KEY_PRESSED:
187     case ui::ET_KEY_RELEASED: {
188       ui::KeyEvent ev(
189           static_cast<ui::EventType>(event.action()),
190           static_cast<ui::KeyboardCode>(event.key_data().key_code()),
191           event.flags(), event.key_data().is_char());
192       delegate_->OnHostKeyEvent(&ev);
193       break;
194     }
195     // TODO(beng): touch, etc.
196   }
197 };
198
199 ////////////////////////////////////////////////////////////////////////////////
200 // WindowTreeHostMojo, private:
201
202 void WindowTreeHostMojo::DidCreateContext(gfx::Size viewport_size) {
203   CreateCompositor(GetAcceleratedWidget());
204   compositor_created_callback_.Run();
205   NotifyHostResized(viewport_size);
206 }
207
208 }  // namespace examples
209 }  // namespace mojo