c344ba4bfe212224c497b238a3fa52ba676c0f0c
[platform/framework/web/crosswalk.git] / src / mojo / services / view_manager / window_tree_host_impl.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/services/view_manager/window_tree_host_impl.h"
6
7 #include "mojo/public/c/gles2/gles2.h"
8 #include "mojo/services/public/cpp/geometry/geometry_type_converters.h"
9 #include "mojo/services/view_manager/context_factory_impl.h"
10 #include "ui/aura/env.h"
11 #include "ui/aura/window.h"
12 #include "ui/aura/window_event_dispatcher.h"
13 #include "ui/compositor/compositor.h"
14 #include "ui/events/event.h"
15 #include "ui/events/event_constants.h"
16 #include "ui/gfx/geometry/insets.h"
17 #include "ui/gfx/geometry/rect.h"
18
19 namespace mojo {
20 namespace view_manager {
21 namespace service {
22
23 // TODO(sky): nuke this. It shouldn't be static.
24 // static
25 ContextFactoryImpl* WindowTreeHostImpl::context_factory_ = NULL;
26
27 ////////////////////////////////////////////////////////////////////////////////
28 // WindowTreeHostImpl, public:
29
30 WindowTreeHostImpl::WindowTreeHostImpl(
31     NativeViewportPtr viewport,
32     const gfx::Rect& bounds,
33     const base::Callback<void()>& compositor_created_callback)
34     : native_viewport_(viewport.Pass()),
35       compositor_created_callback_(compositor_created_callback),
36       bounds_(bounds) {
37   native_viewport_.set_client(this);
38   native_viewport_->Create(Rect::From(bounds));
39
40   MessagePipe pipe;
41   native_viewport_->CreateGLES2Context(
42       MakeRequest<CommandBuffer>(pipe.handle0.Pass()));
43
44   // The ContextFactory must exist before any Compositors are created.
45   if (context_factory_) {
46     delete context_factory_;
47     context_factory_ = NULL;
48   }
49   context_factory_ = new ContextFactoryImpl(pipe.handle1.Pass());
50   aura::Env::GetInstance()->set_context_factory(context_factory_);
51   CHECK(context_factory_) << "No GL bindings.";
52 }
53
54 WindowTreeHostImpl::~WindowTreeHostImpl() {
55   DestroyCompositor();
56   DestroyDispatcher();
57 }
58
59 ////////////////////////////////////////////////////////////////////////////////
60 // WindowTreeHostImpl, aura::WindowTreeHost implementation:
61
62 ui::EventSource* WindowTreeHostImpl::GetEventSource() {
63   return this;
64 }
65
66 gfx::AcceleratedWidget WindowTreeHostImpl::GetAcceleratedWidget() {
67   NOTIMPLEMENTED() << "GetAcceleratedWidget";
68   return gfx::kNullAcceleratedWidget;
69 }
70
71 void WindowTreeHostImpl::Show() {
72   window()->Show();
73   native_viewport_->Show();
74 }
75
76 void WindowTreeHostImpl::Hide() {
77   native_viewport_->Hide();
78   window()->Hide();
79 }
80
81 gfx::Rect WindowTreeHostImpl::GetBounds() const {
82   return bounds_;
83 }
84
85 void WindowTreeHostImpl::SetBounds(const gfx::Rect& bounds) {
86   native_viewport_->SetBounds(Rect::From(bounds));
87 }
88
89 gfx::Point WindowTreeHostImpl::GetLocationOnNativeScreen() const {
90   return gfx::Point(0, 0);
91 }
92
93 void WindowTreeHostImpl::SetCapture() {
94   NOTIMPLEMENTED();
95 }
96
97 void WindowTreeHostImpl::ReleaseCapture() {
98   NOTIMPLEMENTED();
99 }
100
101 void WindowTreeHostImpl::PostNativeEvent(
102     const base::NativeEvent& native_event) {
103   NOTIMPLEMENTED();
104 }
105
106 void WindowTreeHostImpl::OnDeviceScaleFactorChanged(float device_scale_factor) {
107   NOTIMPLEMENTED();
108 }
109
110 void WindowTreeHostImpl::SetCursorNative(gfx::NativeCursor cursor) {
111   NOTIMPLEMENTED();
112 }
113
114 void WindowTreeHostImpl::MoveCursorToNative(const gfx::Point& location) {
115   NOTIMPLEMENTED();
116 }
117
118 void WindowTreeHostImpl::OnCursorVisibilityChangedNative(bool show) {
119   NOTIMPLEMENTED();
120 }
121
122 ////////////////////////////////////////////////////////////////////////////////
123 // WindowTreeHostImpl, ui::EventSource implementation:
124
125 ui::EventProcessor* WindowTreeHostImpl::GetEventProcessor() {
126   return dispatcher();
127 }
128
129 ////////////////////////////////////////////////////////////////////////////////
130 // WindowTreeHostImpl, NativeViewportClient implementation:
131
132 void WindowTreeHostImpl::OnCreated() {
133   CreateCompositor(GetAcceleratedWidget());
134   compositor_created_callback_.Run();
135 }
136
137 void WindowTreeHostImpl::OnBoundsChanged(RectPtr bounds) {
138   bounds_ = bounds.To<gfx::Rect>();
139   OnHostResized(bounds_.size());
140 }
141
142 void WindowTreeHostImpl::OnDestroyed() {
143   base::MessageLoop::current()->Quit();
144 }
145
146 void WindowTreeHostImpl::OnEvent(EventPtr event,
147                                  const mojo::Callback<void()>& callback) {
148   switch (event->action) {
149     case ui::ET_MOUSE_PRESSED:
150     case ui::ET_MOUSE_DRAGGED:
151     case ui::ET_MOUSE_RELEASED:
152     case ui::ET_MOUSE_MOVED:
153     case ui::ET_MOUSE_ENTERED:
154     case ui::ET_MOUSE_EXITED: {
155       gfx::Point location(event->location->x, event->location->y);
156       ui::MouseEvent ev(static_cast<ui::EventType>(event->action), location,
157                         location, event->flags, 0);
158       SendEventToProcessor(&ev);
159       break;
160     }
161     case ui::ET_KEY_PRESSED:
162     case ui::ET_KEY_RELEASED: {
163       ui::KeyEvent ev(
164           static_cast<ui::EventType>(event->action),
165           static_cast<ui::KeyboardCode>(event->key_data->key_code),
166           event->flags, event->key_data->is_char);
167       SendEventToProcessor(&ev);
168       break;
169     }
170     // TODO(beng): touch, etc.
171   }
172   callback.Run();
173 };
174
175 }  // namespace service
176 }  // namespace view_manager
177 }  // namespace mojo