Implements WatchBase
[platform/core/appfw/widget-viewer.git] / watch-holder-base / src / watch_base.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 <aul_app_com.h>
20 #include <vconf.h>
21
22 #include "watch_base.hh"
23
24 #ifdef LOG_TAG
25 #undef LOG_TAG
26 #endif
27
28 #define LOG_TAG "WATCH_HOLDER"
29
30 using namespace std;
31 using namespace tizen_base;
32 using namespace screen_connector;
33 namespace watch_holder {
34
35 WatchBase::WatchBase(std::string appid,
36       std::shared_ptr<WlSurface> surface,
37       tizen_base::Bundle extra, IEvent* listener, bool mock)
38       : RemoteSurface(appid, RemoteSurface::WATCH, surface, mock),
39         appid_(appid), listener_(listener), extra_data_(extra) {
40 }
41
42 WatchBase::WatchBase(int rid, std::string id, std::string appid,
43       std::shared_ptr<WlSurface> surface,
44       tizen_base::Bundle extra, IEvent* listener, bool mock)
45       : RemoteSurface(rid, id, RemoteSurface::WATCH, surface, mock),
46         appid_(appid), listener_(listener), extra_data_(extra) {
47 }
48
49 WatchBase::WatchBase(std::string appid,
50       std::shared_ptr<screen_connector::WlSurface> surface,
51       IEvent* listener, bool mock)
52       : RemoteSurface(appid, RemoteSurface::WATCH, surface, mock),
53         appid_(appid), listener_(listener) {
54 }
55
56 WatchBase::WatchBase(int rid, std::string id, std::string appid,
57       std::shared_ptr<screen_connector::WlSurface> surface,
58       IEvent* listener, bool mock)
59       : RemoteSurface(rid, id, RemoteSurface::WATCH, surface, mock),
60         appid_(appid), listener_(listener) {
61 }
62
63 WatchBase::~WatchBase() = default;
64 void WatchBase::Resume() {
65   RemoteSurface::SendVisibility(true);
66 }
67
68 void WatchBase::Pause() {
69   RemoteSurface::SendVisibility(false);
70 }
71
72 int WatchBase::Terminate() {
73   LOGW("Terminate (%d)", pid_);
74   return aul_terminate_pid(pid_);
75 }
76
77 void WatchBase::BlockUpdate(bool enable) {
78   RemoteSurface::SetBlock(enable);
79 }
80
81
82 void WatchBase::CancelTouch() {
83   RemoteSurface::SendTouchCancel();
84 }
85
86 int WatchBase::GetRid() const {
87   return RemoteSurface::GetRid();
88 }
89
90 int WatchBase::GetPid() const {
91   return pid_;
92 }
93
94 bool WatchBase::IsBound() const {
95   return is_bound_;
96 }
97
98 string WatchBase::GetAppId() const {
99   return appid_;
100 }
101
102 bool WatchBase::IsFaulted() const {
103   return is_faulted_;
104 }
105
106 void WatchBase::Bind(std::shared_ptr<WlSurface> surface) {
107   RemoteSurface::Bind(surface);
108   is_bound_ = true;
109 }
110
111 void WatchBase::Unbind() {
112   RemoteSurface::Unbind();
113   is_bound_ = false;
114 }
115
116 std::shared_ptr<WlBuffer> WatchBase::GetCurrentImage() const {
117   return current_buffer_;
118 }
119
120 tizen_base::Bundle WatchBase::GetExtra() const {
121   return extra_data_;
122 }
123
124 void WatchBase::SetFaulted(bool faulted) {
125   is_faulted_ = faulted;
126 }
127
128 void WatchBase::OnBufferAdded(const std::string& appId,
129       const std::string& instId, int pid) {
130   pid_ = pid;
131 }
132
133 void WatchBase::OnBufferRemoved(const std::string& appId,
134       const std::string& instId, int pid) {
135   if (current_buffer_ == nullptr)
136     return;
137
138   OnRemoved(appId, instId, pid, current_buffer_);
139   current_buffer_ = nullptr;
140 }
141
142 void WatchBase::OnBufferChanged(int type,
143     std::shared_ptr<screen_connector::WlBuffer> tbm,
144     int fd, uint32_t size, uint32_t time) {
145   if (type != TIZEN_REMOTE_SURFACE_BUFFER_TYPE_TBM)
146     return;
147
148   if (tbm.get() == nullptr || tbm->GetRaw() == nullptr) {
149       LOGW("Null tbm");
150       return;
151   }
152
153   if (GetType() == RemoteSurface::WATCH && RemoteSurface::IsBound())
154     return;
155
156   if (current_buffer_ == nullptr) {
157     LOGD("first added !!!! %d %s", type, GetAppId().c_str());
158     OnAdded(GetAppId(), GetInstId(), GetPid(), tbm);
159   } else {
160     OnChanged(GetAppId(), GetInstId(), GetPid(), tbm);
161   }
162   current_buffer_ = tbm;
163 }
164
165 void WatchBase::OnAdded(const std::string& appId, const std::string& instId,
166     int pid, std::shared_ptr<screen_connector::WlBuffer> tbm) {
167   pid_ = pid;
168   listener_->OnAdded(*this);
169 }
170
171 void WatchBase::OnRemoved(const std::string& appId, const std::string& instId,
172     int pid, std::shared_ptr<screen_connector::WlBuffer> tbm) {
173   listener_->OnRemoved(*this);
174 }
175
176 void WatchBase::OnChanged(const std::string& appId, const std::string& instId,
177     int pid, std::shared_ptr<screen_connector::WlBuffer> tbm) {
178   listener_->OnUpdated(*this);
179 }
180
181 int WatchBase::NotifyChangedEvent() const {
182   tizen_base::Bundle b;
183   b.Add(NOTIFY_CHANGED_EVENT_APPID_KEY, appid_);
184   b.Add(NOTIFY_CHANGED_EVENT_RID_KEY, to_string(GetRid()));
185   int ret = vconf_set_str(
186       VCONFKEY_WATCH_CURRENT_WATCH_INFO, (const char*)b.ToRaw().first.get());
187   if (ret != 0) {
188     LOGE("Fail to send noti (%d)", ret);
189     return -1;
190   }
191   return 0;
192 }
193
194 WatchBase::IEvent* WatchBase::GetListener() {
195   return listener_;
196 }
197
198 }  // namespace watch_holder