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