Merge pull request #47 from psikorski/x11_remove
[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 <cstdint>
21
22 #include "common/logger.h"
23
24 namespace runtime {
25
26 namespace {
27   const char* kEdjePath = "/usr/share/edje/xwalk/xwalk_tizen.edj";
28   const char* kWinowRotationEventKey = "wm,rotation,changed";
29 }  // namespace
30
31
32 NativeWindow::NativeWindow()
33     : initialized_(false),
34       window_(NULL),
35       layout_(NULL),
36       content_(NULL),
37       rotation_(0),
38       handler_id_(0) {
39 }
40
41 NativeWindow::~NativeWindow() {
42 }
43
44 void NativeWindow::Initialize() {
45   // window
46   window_ = CreateWindowInternal();
47   elm_win_conformant_set(window_, EINA_TRUE);
48   int w, h;
49   ecore_wl_screen_size_get(&w, &h);
50   evas_object_resize(window_, w, h);
51   elm_win_autodel_set(window_, EINA_TRUE);
52   evas_object_smart_callback_add(window_, "delete,request",
53                                  DidDeleteRequested, this);
54   evas_object_smart_callback_add(window_, "profile,changed",
55                                  DidProfileChanged, this);
56
57   #define EVAS_SIZE_EXPAND_FILL(obj) \
58     evas_object_size_hint_weight_set(obj, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); \
59     evas_object_size_hint_align_set(obj, EVAS_HINT_FILL, EVAS_HINT_FILL);
60
61   // background
62   Evas_Object* bg = evas_object_rectangle_add(evas_object_evas_get(window_));
63   evas_object_color_set(bg, 0, 0, 0, 255);
64   EVAS_SIZE_EXPAND_FILL(bg);
65   elm_win_resize_object_add(window_, bg);
66   evas_object_render_op_set(bg, EVAS_RENDER_BLEND);
67   evas_object_show(bg);
68
69   // conformant
70   Evas_Object* conformant = elm_conformant_add(window_);
71   EVAS_SIZE_EXPAND_FILL(conformant);
72   elm_win_resize_object_add(window_, conformant);
73   evas_object_show(conformant);
74
75   // top layout
76   Evas_Object* top_layout = elm_layout_add(conformant);
77   elm_layout_file_set(top_layout, kEdjePath, "web-application");
78   EVAS_SIZE_EXPAND_FILL(top_layout);
79   elm_object_content_set(conformant, top_layout);
80   evas_object_show(top_layout);
81   layout_ = top_layout;
82
83   // Rotation
84   auto rotation_callback = [](void* user_data,
85                               Evas_Object* obj,
86                               void*) -> void {
87       NativeWindow* window = static_cast<NativeWindow*>(user_data);
88       int degree = elm_win_rotation_get(obj);
89       window->DidRotation(degree);
90   };
91   evas_object_smart_callback_add(window_,
92                                  kWinowRotationEventKey,
93                                  rotation_callback,
94                                  this);
95
96   if (w > h) {
97     natural_orientation_ = ScreenOrientation::LANDSCAPE_PRIMARY;
98   } else {
99     natural_orientation_ = ScreenOrientation::PORTRAIT_PRIMARY;
100   }
101
102   initialized_ = true;
103 }
104
105 void NativeWindow::DidDeleteRequested(void* /*data*/,
106     Evas_Object* /*obj*/, void* /*event_info*/) {
107   LOGGER(DEBUG) << "didDeleteRequested";
108   elm_exit();
109 }
110
111 void NativeWindow::DidProfileChanged(void* /*data*/,
112     Evas_Object* /*obj*/, void* /*event_info*/) {
113   LOGGER(DEBUG) << "didProfileChanged";
114 }
115
116 Evas_Object* NativeWindow::evas_object() const {
117   return window_;
118 }
119
120 void NativeWindow::SetContent(Evas_Object* content) {
121   // Remarks
122   // If any object was already set as a content object in the same part,
123   // the previous object will be deleted automatically with this call.
124   // If the content is NULL, this call will just delete the previous object.
125   // If the If you wish to preserve it,
126   // issue elm_object_part_content_unset() on it first.
127
128   evas_object_show(content);
129   elm_object_part_content_unset(layout_, "elm.swallow.content");
130   elm_object_part_content_set(layout_, "elm.swallow.content", content);
131   content_ = content;
132
133   // attached webview was resized by evas_norender API
134   evas_norender(evas_object_evas_get(window_));
135 }
136
137 void NativeWindow::DidRotation(int degree) {
138   rotation_ = degree;
139   auto it = handler_table_.begin();
140   for ( ; it != handler_table_.end(); ++it) {
141     it->second(degree);
142   }
143 }
144
145 int NativeWindow::AddRotationHandler(RotationHandler handler) {
146   int id = handler_id_++;
147   handler_table_[id] = handler;
148   return id;
149 }
150
151 void NativeWindow::RemoveRotationHandler(int id) {
152   handler_table_.erase(id);
153 }
154
155 void NativeWindow::SetRotationLock(int degree) {
156   if (degree != -1)
157     rotation_ = degree % 360;
158   elm_win_wm_rotation_preferred_rotation_set(window_, rotation_);
159 }
160
161 void NativeWindow::SetRotationLock(ScreenOrientation orientation) {
162   int portrait_natural_angle[] = {
163     0,  // PORTRAIT_PRIMARY
164     180,  // PORTRAIT_SECONDARY
165     270,  // LANDSCAPE_PRIMARY
166     90,  // LANDSCAPE_SECONDARY
167     0,  // NATURAL
168     -1  // ANY
169   };
170   int landscape_natural_angle[] = {
171     270,  // PORTRAIT_PRIMARY
172     90,  // PORTRAIT_SECONDARY
173     0,  // LANDSCAPE_PRIMARY
174     180,  // LANDSCAPE_SECONDARY
175     0,  // NATURAL
176     -1,  // ANY
177   };
178   auto& convert_table =
179       natural_orientation_ == ScreenOrientation::PORTRAIT_PRIMARY ?
180           portrait_natural_angle :
181           landscape_natural_angle;
182   SetRotationLock(convert_table[static_cast<int>(orientation)]);
183 }
184
185
186 void NativeWindow::SetAutoRotation() {
187   elm_win_wm_rotation_preferred_rotation_set(window_, -1);
188   if (elm_win_wm_rotation_supported_get(window_)) {
189     const int rotation[4] = {0, 90, 180, 270};
190     elm_win_wm_rotation_available_rotations_set(window_, rotation, 4);
191   }
192   rotation_ = elm_win_rotation_get(window_);
193 }
194
195 void NativeWindow::Show() {
196   evas_object_show(window_);
197 }
198
199 void NativeWindow::Active() {
200   elm_win_activate(window_);
201 }
202
203 void NativeWindow::InActive() {
204   elm_win_lower(window_);
205 }
206
207 void NativeWindow::FullScreen(bool enable) {
208   elm_win_indicator_opacity_set(window_,
209       enable ? ELM_WIN_INDICATOR_TRANSPARENT : ELM_WIN_INDICATOR_OPAQUE);
210 }
211
212 }  // namespace runtime