Implements WatchBase
[platform/core/appfw/widget-viewer.git] / watch-holder / src / watch.cc
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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 <aul.h>
18 #include <dlog.h>
19 #include <glib.h>
20 #include <aul_app_com.h>
21 #include <vconf.h>
22 #include <screen_connector_remote_surface_evas/util.h>
23
24 #include "watch.hh"
25
26 #ifdef LOG_TAG
27 #undef LOG_TAG
28 #endif
29
30 #define LOG_TAG "WATCH_HOLDER"
31
32 using namespace std;
33 using namespace tizen_base;
34 using namespace screen_connector;
35 namespace watch_holder {
36
37 Watch::Watch(std::string appid, Evas_Object* viewer_win,
38     tizen_base::Bundle extra, IEvent* listener, bool mock)
39     : WatchBase(appid, screen_connector::util::GetWlSurface(viewer_win),
40         extra, listener, mock),
41       viewer_win_(make_shared<EvasObject>(viewer_win, false)),
42           image_event_listner_(make_shared<ImageEventListener>(this)) {
43 }
44
45 Watch::Watch(int rid, std::string id, std::string appid,
46     Evas_Object* viewer_win, tizen_base::Bundle extra,
47     WatchBase::IEvent* listener, bool mock)
48     : WatchBase(rid, id, appid,
49       screen_connector::util::GetWlSurface(viewer_win), extra, listener, mock),
50       viewer_win_(make_shared<EvasObject>(viewer_win, false)),
51       image_event_listner_(make_shared<ImageEventListener>(this)) {
52 }
53
54 Watch::Watch(std::string appid, Evas_Object* viewer_win,
55     IEvent* listener, bool mock)
56     : WatchBase(appid, screen_connector::util::GetWlSurface(viewer_win),
57         listener, mock),
58       viewer_win_(make_shared<EvasObject>(viewer_win, false)),
59           image_event_listner_(make_shared<ImageEventListener>(this)) {
60 }
61
62 Watch::Watch(int rid, std::string id, std::string appid,
63     Evas_Object* viewer_win, WatchBase::IEvent* listener, bool mock)
64     : WatchBase(rid, id, appid,
65       screen_connector::util::GetWlSurface(viewer_win), listener, mock),
66       viewer_win_(make_shared<EvasObject>(viewer_win, false)),
67       image_event_listner_(make_shared<ImageEventListener>(this)) {
68 }
69
70 Watch::~Watch() = default;
71 void Watch::OnAuxMsg(void *data, Evas_Object *o, void *ev_info) {
72   Watch* wa = (Watch*)data;
73   if (wa->bind_win_ == nullptr) {
74     LOGW("Null bind win");
75     return;
76   }
77
78   Elm_Win_Aux_Message *msg = (Elm_Win_Aux_Message *)ev_info;
79   const char *key = elm_win_aux_msg_key_get(wa->bind_win_, msg);
80   const char *val = elm_win_aux_msg_val_get(wa->bind_win_, msg);
81
82   if (key == nullptr || val == nullptr) {
83     LOGW("Null msg data");
84     return;
85   }
86
87   if (!strcmp(key, "tz_remote_surface_mng") && !strcmp(val, "prebind")) {
88     wa->GetListener()->OnBound(*wa);
89     LOGI("Start bind mode !!");
90     evas_object_smart_callback_del(wa->bind_win_,
91       "aux,msg,received", OnAuxMsg);
92   }
93 }
94
95 void Watch::Bind(Evas_Object* win) {
96   bind_win_ = win;
97   elm_win_aux_hint_add(win, "wm.policy.win.msg.use", "1");
98   evas_object_smart_callback_add(win, "aux,msg,received",
99       OnAuxMsg, this);
100   WatchBase::Bind(screen_connector::util::GetWlSurface(win));
101 }
102
103 void Watch::NoRenderPush(int timeout) {
104   LOGI("No render push start");
105   elm_win_norender_push(evas_object_evas_get(viewer_win_->GetRaw()));
106   no_render_timer_ = g_timeout_add(timeout,
107       [](gpointer user_data)->gboolean {
108     Watch* wa = static_cast<Watch*>(user_data);
109     elm_win_norender_pop(evas_object_evas_get(wa->viewer_win_->GetRaw()));
110     wa->no_render_timer_ = 0;
111     LOGW("No render push timeout!");
112     return G_SOURCE_REMOVE;
113   }, this);
114 }
115
116 void Watch::Resume() {
117   NoRenderPush(200);
118   WatchBase::Resume();
119 }
120
121 Evas_Object* Watch::GetCurrentImageEvas() const {
122   return current_image_->GetRaw();
123 }
124
125 void Watch::OnAdded(const string& appid, const string& inst_id,
126     int pid, shared_ptr<WlBuffer> tbm) {
127   current_image_.reset(new Image(evas_object_image_filled_add(
128             evas_object_evas_get(viewer_win_->GetRaw())),
129             image_event_listner_.get(), inst_id, pid, this));
130   WatchBase::OnAdded(appid, inst_id, pid, tbm);
131 }
132
133 void Watch::OnRemoved(const string& appid, const string& inst_id,
134       int pid, shared_ptr<WlBuffer> tbm) {
135   WatchBase::OnRemoved(appid, inst_id, pid, tbm);
136   current_image_ = nullptr;
137 }
138
139 void Watch::OnChanged(const string& appid, const string& inst_id,
140       int pid, shared_ptr<WlBuffer> tbm) {
141   current_image_->Update(tbm);
142   WatchBase::OnChanged(appid, inst_id, pid, tbm);
143 }
144
145 }