Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / ozone / content / event_converter_in_process.cc
1 // Copyright 2013 Intel Corporation. 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 "ozone/content/event_converter_in_process.h"
6
7 #include "base/bind.h"
8 #include "ozone/ui/events/keyboard_code_conversion_ozone.h"
9 #include "ozone/ui/events/output_change_observer.h"
10 #include "ozone/ui/events/window_change_observer.h"
11
12 namespace content {
13
14 EventConverterInProcess::EventConverterInProcess()
15     : EventConverterOzoneWayland(),
16       observer_(NULL),
17       output_observer_(NULL) {
18 }
19
20 EventConverterInProcess::~EventConverterInProcess() {
21 }
22
23 void EventConverterInProcess::MotionNotify(float x, float y) {
24   gfx::Point position(x, y);
25   scoped_ptr<ui::MouseEvent> mouseev(new ui::MouseEvent(ui::ET_MOUSE_MOVED,
26                                                         position,
27                                                         position,
28                                                         0,
29                                                         0));
30
31   ui::EventConverterOzoneWayland::PostTaskOnMainLoop(base::Bind(
32       &EventConverterInProcess::DispatchEventHelper, base::Passed(
33           mouseev.PassAs<ui::Event>())));
34 }
35
36 void EventConverterInProcess::ButtonNotify(unsigned handle,
37                                            ui::EventType type,
38                                            ui::EventFlags flags,
39                                            float x,
40                                            float y) {
41   gfx::Point position(x, y);
42   scoped_ptr<ui::MouseEvent> mouseev(new ui::MouseEvent(type,
43                                                         position,
44                                                         position,
45                                                         flags,
46                                                         1));
47   ui::EventConverterOzoneWayland::PostTaskOnMainLoop(base::Bind(
48       &EventConverterInProcess::DispatchEventHelper, base::Passed(
49           mouseev.PassAs<ui::Event>())));
50
51   if (type == ui::ET_MOUSE_RELEASED)
52     ui::EventConverterOzoneWayland::PostTaskOnMainLoop(base::Bind(
53         &EventConverterInProcess::NotifyButtonPress, this, handle));
54 }
55
56 void EventConverterInProcess::AxisNotify(float x,
57                                          float y,
58                                          int xoffset,
59                                          int yoffset) {
60   gfx::Point position(x, y);
61   ui::MouseEvent mouseev(ui::ET_MOUSEWHEEL, position, position, 0, 0);
62
63   scoped_ptr<ui::MouseWheelEvent> wheelev(new ui::MouseWheelEvent(mouseev,
64                                                                   xoffset,
65                                                                   yoffset));
66
67   ui::EventConverterOzoneWayland::PostTaskOnMainLoop(base::Bind(
68       &EventConverterInProcess::DispatchEventHelper, base::Passed(
69           wheelev.PassAs<ui::Event>())));
70 }
71
72 void EventConverterInProcess::PointerEnter(unsigned handle,
73                                            float x,
74                                            float y) {
75   gfx::Point position(x, y);
76   scoped_ptr<ui::MouseEvent> mouseev(new ui::MouseEvent(ui::ET_MOUSE_ENTERED,
77                                                         position,
78                                                         position,
79                                                         0,
80                                                         0));
81   ui::EventConverterOzoneWayland::PostTaskOnMainLoop(base::Bind(
82       &EventConverterInProcess::NotifyPointerEnter, this, handle));
83
84   ui::EventConverterOzoneWayland::PostTaskOnMainLoop(base::Bind(
85       &EventConverterInProcess::DispatchEventHelper, base::Passed(
86           mouseev.PassAs<ui::Event>())));
87 }
88
89 void EventConverterInProcess::PointerLeave(unsigned handle,
90                                            float x,
91                                            float y) {
92   gfx::Point position(x, y);
93   scoped_ptr<ui::MouseEvent> mouseev(new ui::MouseEvent(ui::ET_MOUSE_EXITED,
94                                                         position,
95                                                         position,
96                                                         0,
97                                                         0));
98
99   ui::EventConverterOzoneWayland::PostTaskOnMainLoop(base::Bind(
100       &EventConverterInProcess::NotifyPointerLeave, this, handle));
101
102   ui::EventConverterOzoneWayland::PostTaskOnMainLoop(base::Bind(
103       &EventConverterInProcess::DispatchEventHelper, base::Passed(
104           mouseev.PassAs<ui::Event>())));
105 }
106
107 void EventConverterInProcess::KeyNotify(ui::EventType type,
108                                         unsigned code,
109                                         unsigned modifiers) {
110   scoped_ptr<ui::KeyEvent> keyev(new ui::KeyEvent(type,
111       ui::KeyboardCodeFromNativeKeysym(code), modifiers, true));
112
113   keyev.get()->set_character(
114       ui::CharacterCodeFromNativeKeySym(code, modifiers));
115
116   ui::EventConverterOzoneWayland::PostTaskOnMainLoop(base::Bind(
117       &EventConverterInProcess::DispatchEventHelper, base::Passed(
118           keyev.PassAs<ui::Event>())));
119 }
120
121 void EventConverterInProcess::TouchNotify(ui::EventType type,
122                                           float x,
123                                           float y,
124                                           int32_t touch_id,
125                                           uint32_t time_stamp) {
126   gfx::Point position(x, y);
127   base::TimeDelta time_delta = base::TimeDelta::FromMilliseconds(time_stamp);
128   scoped_ptr<ui::TouchEvent> touchev(new ui::TouchEvent(type,
129                                                         position,
130                                                         touch_id,
131                                                         time_delta));
132
133   ui::EventConverterOzoneWayland::PostTaskOnMainLoop(base::Bind(
134       &EventConverterInProcess::DispatchEventHelper, base::Passed(
135           touchev.PassAs<ui::Event>())));
136 }
137
138 void EventConverterInProcess::CloseWidget(unsigned handle) {
139   ui::EventConverterOzoneWayland::PostTaskOnMainLoop(base::Bind(
140       &EventConverterInProcess::NotifyCloseWidget, this, handle));
141 }
142
143 void EventConverterInProcess::OutputSizeChanged(unsigned width,
144                                                 unsigned height) {
145   ui::EventConverterOzoneWayland::PostTaskOnMainLoop(base::Bind(
146       &EventConverterInProcess::NotifyOutputSizeChanged, this, width, height));
147 }
148
149 void EventConverterInProcess::WindowResized(unsigned handle,
150                                             unsigned width,
151                                             unsigned height) {
152   ui::EventConverterOzoneWayland::PostTaskOnMainLoop(base::Bind(
153       &EventConverterInProcess::NotifyWindowResized, this, handle, width,
154           height));
155 }
156
157 void EventConverterInProcess::SetWindowChangeObserver(
158     ui::WindowChangeObserver* observer) {
159   observer_ = observer;
160 }
161
162 void EventConverterInProcess::SetOutputChangeObserver(
163     ui::OutputChangeObserver* observer) {
164   output_observer_ = observer;
165 }
166
167 void EventConverterInProcess::NotifyPointerEnter(
168     EventConverterInProcess* data, unsigned handle) {
169   if (data->observer_)
170     data->observer_->OnWindowEnter(handle);
171 }
172
173 void EventConverterInProcess::NotifyPointerLeave(
174     EventConverterInProcess* data, unsigned handle) {
175   if (data->observer_)
176     data->observer_->OnWindowLeave(handle);
177 }
178
179 void EventConverterInProcess::NotifyButtonPress(
180     EventConverterInProcess* data, unsigned handle) {
181   if (data->observer_)
182     data->observer_->OnWindowFocused(handle);
183 }
184
185 void EventConverterInProcess::NotifyCloseWidget(
186     EventConverterInProcess* data, unsigned handle) {
187   if (data->observer_)
188     data->observer_->OnWindowClose(handle);
189 }
190
191 void
192 EventConverterInProcess::NotifyOutputSizeChanged(EventConverterInProcess* data,
193                                                  unsigned width,
194                                                  unsigned height) {
195   if (data->output_observer_)
196     data->output_observer_->OnOutputSizeChanged(width, height);
197 }
198
199 void
200 EventConverterInProcess::NotifyWindowResized(EventConverterInProcess* data,
201                                              unsigned handle,
202                                              unsigned width,
203                                              unsigned height) {
204   if (data->observer_)
205     data->observer_->OnWindowResized(handle, width, height);
206 }
207
208 void EventConverterInProcess::DispatchEventHelper(
209     scoped_ptr<ui::Event> key) {
210   base::MessagePumpOzone::Current()->Dispatch(key.get());
211 }
212
213 }  // namespace content