Change the chromium header from ewk_chromium.h to EWebKit.h/Ewebkit_internal.h
[platform/framework/web/crosswalk-tizen.git] / runtime / browser / native_window.cc
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16
17 #include "runtime/browser/native_window.h"
18
19 #include <Ecore_Wayland.h>
20 #include <EWebKit.h>
21 #include <EWebKit_internal.h>
22 #include <cstdint>
23
24 #include "common/arraysize.h"
25 #include "common/logger.h"
26
27 namespace runtime {
28
29 namespace {
30 const char* kEdjePath = "/usr/share/edje/xwalk/xwalk_tizen.edj";
31 const char* kWinowRotationEventKey = "wm,rotation,changed";
32 const char* kWinowFocusedEventKey = "focused";
33 const char* kWinowUnfocusedEventKey = "unfocused";
34
35 const int kPortraitNaturalAngle[] = {
36   0,  // PORTRAIT_PRIMARY
37   180,  // PORTRAIT_SECONDARY
38   270,  // LANDSCAPE_PRIMARY
39   90,  // LANDSCAPE_SECONDARY
40   0,  // NATURAL
41   -1  // ANY
42 };
43 const int kLandscapeNaturalAngle[] = {
44   270,  // PORTRAIT_PRIMARY
45   90,  // PORTRAIT_SECONDARY
46   0,  // LANDSCAPE_PRIMARY
47   180,  // LANDSCAPE_SECONDARY
48   0,  // NATURAL
49   -1,  // ANY
50 };
51
52 NativeWindow::ScreenOrientation NativeAngleToOrientation(
53     int angle, NativeWindow::ScreenOrientation natural_orientation) {
54   auto& convert_table =
55       natural_orientation == NativeWindow::ScreenOrientation::PORTRAIT_PRIMARY ?
56           kPortraitNaturalAngle :
57           kLandscapeNaturalAngle;
58   unsigned index = ARRAYSIZE(convert_table) - 1;
59   for (unsigned i = 0; i < ARRAYSIZE(convert_table); ++i) {
60     if (convert_table[i] == angle) {
61       index = i;
62       break;
63     }
64   }
65   return static_cast<NativeWindow::ScreenOrientation>(index);
66 }
67
68 }  // namespace
69
70
71 NativeWindow::NativeWindow()
72     : window_(NULL),
73       window_type_(Type::NORMAL),
74       initialized_(false),
75       focus_(NULL),
76       content_(NULL),
77       rotation_(0),
78       handler_id_(0) {
79 }
80
81 NativeWindow::~NativeWindow() {
82   if (window_)
83     evas_object_del(window_);
84 }
85
86 void NativeWindow::Initialize() {
87   if (initialized_) {
88     LOGGER(DEBUG) << "already initialized";
89     return;
90   }
91   // window
92   window_ = CreateWindowInternal();
93   elm_win_conformant_set(window_, EINA_TRUE);
94   int w, h;
95   ecore_wl_screen_size_get(&w, &h);
96   evas_object_resize(window_, w, h);
97   elm_win_size_base_set(window_, w, h);
98   elm_win_autodel_set(window_, EINA_TRUE);
99   evas_object_smart_callback_add(window_, "delete,request",
100                                  DidDeleteRequested, this);
101   evas_object_smart_callback_add(window_, "profile,changed",
102                                  DidProfileChanged, this);
103
104   #define EVAS_SIZE_EXPAND_FILL(obj) \
105     evas_object_size_hint_weight_set(obj, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); \
106     evas_object_size_hint_align_set(obj, EVAS_HINT_FILL, EVAS_HINT_FILL);
107
108   // background
109   Evas_Object* bg = evas_object_rectangle_add(evas_object_evas_get(window_));
110   evas_object_color_set(bg, 0, 0, 0, 255);
111   EVAS_SIZE_EXPAND_FILL(bg);
112   elm_win_resize_object_add(window_, bg);
113   evas_object_render_op_set(bg, EVAS_RENDER_BLEND);
114   evas_object_show(bg);
115
116   // conformant
117   Evas_Object* conformant = elm_conformant_add(window_);
118   EVAS_SIZE_EXPAND_FILL(conformant);
119   elm_win_resize_object_add(window_, conformant);
120   evas_object_show(conformant);
121
122   // top layout
123   Evas_Object* top_layout = elm_layout_add(conformant);
124   elm_layout_file_set(top_layout, kEdjePath, "web-application");
125   EVAS_SIZE_EXPAND_FILL(top_layout);
126   elm_object_content_set(conformant, top_layout);
127   evas_object_show(top_layout);
128
129   // focus
130   Evas_Object* focus = elm_bg_add(top_layout);
131   evas_object_size_hint_align_set(focus, EVAS_HINT_FILL, EVAS_HINT_FILL);
132   evas_object_size_hint_weight_set(focus, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
133   elm_object_focus_allow_set(focus, EINA_TRUE);
134   elm_object_part_content_set(top_layout, "elm.swallow.content", focus);
135   EVAS_SIZE_EXPAND_FILL(focus);
136   elm_access_object_unregister(focus);
137   evas_object_show(focus);
138   focus_ = focus;
139
140   // focus callback
141   auto focus_callback = [](void* user_data,
142                            Evas_Object*,
143                            void*) -> void {
144     NativeWindow* window = static_cast<NativeWindow*>(user_data);
145     window->DidFocusChanged(true);
146   };
147   auto unfocus_callback = [](void* user_data,
148                              Evas_Object*,
149                              void*) -> void {
150     NativeWindow* window = static_cast<NativeWindow*>(user_data);
151     window->DidFocusChanged(false);
152   };
153
154   evas_object_smart_callback_add(focus,
155                                  kWinowFocusedEventKey,
156                                  focus_callback,
157                                  this);
158   evas_object_smart_callback_add(focus,
159                                  kWinowUnfocusedEventKey,
160                                  unfocus_callback,
161                                  this);
162
163   // Rotation
164   auto rotation_callback = [](void* user_data,
165                               Evas_Object* obj,
166                               void*) -> void {
167       NativeWindow* window = static_cast<NativeWindow*>(user_data);
168       int degree = elm_win_rotation_get(obj);
169       window->DidRotation(degree);
170   };
171   evas_object_smart_callback_add(window_,
172                                  kWinowRotationEventKey,
173                                  rotation_callback,
174                                  this);
175
176   if (w > h) {
177     natural_orientation_ = ScreenOrientation::LANDSCAPE_PRIMARY;
178   } else {
179     natural_orientation_ = ScreenOrientation::PORTRAIT_PRIMARY;
180   }
181
182   initialized_ = true;
183 }
184
185 void NativeWindow::DidDeleteRequested(void* /*data*/,
186     Evas_Object* /*obj*/, void* /*event_info*/) {
187   LOGGER(DEBUG) << "didDeleteRequested";
188   elm_exit();
189 }
190
191 void NativeWindow::DidProfileChanged(void* /*data*/,
192     Evas_Object* /*obj*/, void* /*event_info*/) {
193   LOGGER(DEBUG) << "didProfileChanged";
194 }
195
196 Evas_Object* NativeWindow::evas_object() const {
197   return window_;
198 }
199
200 void NativeWindow::SetContent(Evas_Object* content) {
201   // Remarks
202   // If any object was already set as a content object in the same part,
203   // the previous object will be deleted automatically with this call.
204   // If the content is NULL, this call will just delete the previous object.
205   // If the If you wish to preserve it,
206   // issue elm_object_part_content_unset() on it first.
207
208   evas_object_show(content);
209   evas_object_hide(
210     elm_object_part_content_unset(focus_, "elm.swallow.content"));
211   elm_object_part_content_set(focus_, "elm.swallow.content", content);
212   elm_object_focus_set(focus_, EINA_TRUE);
213   content_ = content;
214
215   // attached webview was resized by evas_norender API
216   evas_norender(evas_object_evas_get(window_));
217 }
218
219 void NativeWindow::DidRotation(int degree) {
220   rotation_ = degree;
221   auto it = handler_table_.begin();
222   for ( ; it != handler_table_.end(); ++it) {
223     it->second(degree);
224   }
225 }
226
227 void NativeWindow::DidFocusChanged(bool got) {
228   if (content_ != NULL) {
229     ewk_view_focus_set(content_, got ? EINA_TRUE : EINA_FALSE);
230   }
231 }
232
233 int NativeWindow::AddRotationHandler(RotationHandler handler) {
234   int id = handler_id_++;
235   handler_table_[id] = handler;
236   return id;
237 }
238
239 void NativeWindow::RemoveRotationHandler(int id) {
240   handler_table_.erase(id);
241 }
242
243 NativeWindow::ScreenOrientation NativeWindow::orientation() const {
244   return NativeAngleToOrientation(rotation_, natural_orientation_);
245 }
246
247 void NativeWindow::SetRotationLock(int degree) {
248   if (degree != -1)
249     rotation_ = degree % 360;
250   elm_win_wm_rotation_preferred_rotation_set(window_, rotation_);
251 }
252
253 void NativeWindow::SetRotationLock(ScreenOrientation orientation) {
254   auto& convert_table =
255       natural_orientation_ == ScreenOrientation::PORTRAIT_PRIMARY ?
256           kPortraitNaturalAngle :
257           kLandscapeNaturalAngle;
258   SetRotationLock(convert_table[static_cast<int>(orientation)]);
259 }
260
261
262 void NativeWindow::SetAutoRotation() {
263   elm_win_wm_rotation_preferred_rotation_set(window_, -1);
264   if (elm_win_wm_rotation_supported_get(window_)) {
265     const int rotation[4] = {0, 90, 180, 270};
266     elm_win_wm_rotation_available_rotations_set(window_, rotation, 4);
267   }
268   rotation_ = elm_win_rotation_get(window_);
269 }
270
271 void NativeWindow::Show() {
272   evas_object_show(window_);
273 }
274
275 void NativeWindow::Active() {
276   elm_win_activate(window_);
277 }
278
279 void NativeWindow::InActive() {
280   elm_win_lower(window_);
281 }
282
283 void NativeWindow::FullScreen(bool enable) {
284   elm_win_indicator_opacity_set(window_,
285       enable ? ELM_WIN_INDICATOR_TRANSPARENT : ELM_WIN_INDICATOR_OPAQUE);
286 }
287
288 #ifdef MANUAL_ROTATE_FEATURE_SUPPORT
289 void NativeWindow::EnableManualRotation(bool enable) {
290   LOGGER(DEBUG) << "set manual rotation : " << (enable ? "enabled" : "disabled");
291   elm_win_wm_rotation_manual_rotation_done_set(window_, enable ? EINA_TRUE : EINA_FALSE);
292 }
293
294 void NativeWindow::ManualRotationDone() {
295   if (EINA_TRUE == elm_win_wm_rotation_manual_rotation_done_get(window_)) {
296     elm_win_wm_rotation_manual_rotation_done(window_);
297   }
298 }
299 #endif  // MANUAL_ROTATE_FEATURE_SUPPORT
300
301 }  // namespace runtime