Fix WM attach layers to different screen.
[apps/agl-service-windowmanager-2017.git] / src / wm_client.cpp
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
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 <json-c/json.h>
18 #include <algorithm>
19 #include "wm_client.hpp"
20 #include "hmi-debug.h"
21
22 #define INVALID_SURFACE_ID 0
23
24 using std::string;
25 using std::vector;
26
27 namespace wm
28 {
29
30 static const vector<string> kWMEvents = {
31     // Private event for applications
32     "syncDraw", "flushDraw", "visible", "invisible", "active", "inactive", "error"};
33 static const vector<string> kErrorDescription = {
34     "unknown-error"};
35
36 static const char kKeyDrawingName[] = "drawing_name";
37 static const char kKeyrole[] = "role";
38 static const char kKeyError[] = "error";
39 static const char kKeyErrorDesc[] = "kErrorDescription";
40
41 WMClient::WMClient(const string &appid, unsigned layer, unsigned surface, const string &role)
42     : id(appid), layer(layer),
43       service2surfaces(0)
44 {
45     service2surfaces[role] = surface;
46     for (auto x : kWMEvents)
47     {
48 #if GTEST_ENABLED
49         string ev = x;
50 #else
51         afb_event ev = afb_daemon_make_event(x.c_str());
52 #endif
53         evname2afb_event[x] = ev;
54     }
55 }
56
57 WMClient::WMClient(const string &appid, const string &role)
58     : id(appid),
59       layer(0),
60       service2surfaces(0),
61       evname2afb_event(0)
62 {
63     service2surfaces[role] = INVALID_SURFACE_ID;
64     for (auto x : kWMEvents)
65     {
66 #if GTEST_ENABLED
67         string ev = x;
68 #else
69         afb_event ev = afb_daemon_make_event(x.c_str());
70 #endif
71         evname2afb_event[x] = ev;
72     }
73 }
74
75 // WMClient::~WMClient()
76 // {
77 // }
78
79 string WMClient::appID() const
80 {
81     return this->id;
82 }
83
84 vector<unsigned> WMClient::renderOrder() const
85 {
86     return this->surface_render_order;
87 }
88
89 const vector<std::string>& WMClient::roles() const
90 {
91     return this->role_list;
92 }
93
94 unsigned WMClient::layerID() const
95 {
96     return this->layer;
97 }
98
99 unsigned WMClient::surfaceID() const
100 {
101     return this->surface;
102 }
103
104 void WMClient::setRole(const string& role)
105 {
106     this->role_list.push_back(role);
107 }
108
109 /**
110  * Set layerID the client belongs to
111  *
112  * This function set layerID the client belongs to.
113  * But this function may not used because the layer should be fixed at constructor.
114  * So this function will be used to change layer by some reasons.
115  *
116  * @param     unsigned[in] layerID
117  * @return    None
118  * @attention WMClient can't have multiple layer
119  */
120 void WMClient::registerLayer(unsigned layer)
121 {
122     this->layer = layer;
123 }
124
125 /**
126  * Add the pair of name and surface of surface to the client
127  *
128  * This function set the pair of role and surface to the client.
129  * This function is used for the client which has multi surfaces.
130  * If the model and relationship for role and surface(layer)
131  * is changed, this function will be changed
132  * Current Window Manager doesn't use this function.
133  *
134  * @param     string[in] name
135  * @param     unsigned[in] surface
136  * @return    true
137  */
138 void WMClient::attachServiceSurface(const string &name, unsigned surface)
139 {
140     HMI_DEBUG("wm", "Add surface name %s with surface %d", name.c_str(), surface);
141     if (0 != this->service2surfaces.count(name))
142     {
143         HMI_NOTICE("wm", "override surfaceID %d with %d", this->service2surfaces[name], surface);
144     }
145     this->service2surfaces[name] = surface;
146 }
147
148 void WMClient::removeServiceSurface(const std::string &name)
149 {
150     for (auto &x : this->service2surfaces)
151     {
152         if (name == x.first)
153         {
154             HMI_INFO("wm", "Remove surface from client %s: service surface name %s, surface: %d",
155                         this->id.c_str(), x.first.c_str(), x.second);
156             this->service2surfaces.erase(name);
157         }
158     }
159     // re_asign render order
160     this->setRenderOrder(this->surface_render_order);
161 }
162
163 void WMClient::setArea(const std::string& area)
164 {
165     this->area = area;
166     // TODO: implementation
167 }
168
169 void WMClient::setRenderOrder()
170 {
171     // use private surface_render_order
172 }
173
174 void WMClient::setRenderOrder(const vector<string> &render_order)
175 {
176     // TODO: implement LM
177 }
178
179 void WMClient::setRenderOrder(const vector<unsigned> &render_order)
180 {
181     // TODO implement LM
182 }
183
184 bool WMClient::removeSurfaceIfExist(unsigned surface)
185 {
186     bool ret = false;
187     auto fwd_itr = std::remove_if(
188         this->surface_render_order.begin(), this->surface_render_order.end(),
189         [surface, &ret](unsigned x) {
190             ret = true;
191             return x == surface;
192         });
193     this->surface_render_order.erase(fwd_itr, this->surface_render_order.end());
194     if(ret)
195     {
196         for(auto &y : this->service2surfaces)
197         {
198             if(y.second == surface)
199             {
200                 HMI_INFO("wm", "Remove surface from client %s: role %s, surface: %d",
201                         this->id.c_str(), y.first.c_str(), y.second);
202                 this->service2surfaces.erase(y.first);
203             }
204         }
205         if(this->surface == surface)
206         {
207             this->surface = INVALID_SURFACE_ID;
208             // TODO: If main surface vanishes, how do we treat?
209             //       Window Manager doesn't know it's means dead or on purpose.
210             // option1: remove service surface and notify to the service.
211             // option2: waiting the application calls deallocate.
212             HMI_INFO("wm", "Main surface vanishes");
213         }
214     }
215     return ret;
216 }
217
218 void WMClient::removeRole(const string &role)
219 {
220     auto fwd_itr = std::remove_if(
221         this->role_list.begin(), this->role_list.end(),
222         [&role](string x) {
223             return x == role;
224         });
225     this->role_list.erase(fwd_itr, this->role_list.end());
226 }
227
228 #if GTEST_ENABLED
229 bool WMClient::subscribe(afb_req req, const string &evname)
230 {
231     if(evname != kKeyError){
232         HMI_DEBUG("wm", "error is only enabeled for now");
233         return false;
234     }
235     int ret = afb_req_subscribe(req, this->evname2afb_event[evname]);
236     if (ret)
237     {
238         HMI_DEBUG("wm", "Failed to subscribe %s", evname.c_str());
239         return false;
240     }
241     return true;
242 }
243
244 void WMClient::emitError(WM_CLIENT_ERROR_EVENT ev)
245 {
246     if (!afb_event_is_valid(this->evname2afb_event[kKeyError])){
247         HMI_ERROR("wm", "event err is not valid");
248         return;
249     }
250     json_object *j = json_object_new_object();
251     json_object_object_add(j, kKeyError, json_object_new_int(ev));
252     json_object_object_add(j, kKeyErrorDesc, json_object_new_string(kErrorDescription[ev].c_str()));
253     HMI_DEBUG("wm", "error: %d, description:%s", ev, kErrorDescription[ev].c_str());
254
255     int ret = afb_event_push(this->evname2afb_event[kKeyError], j);
256     if (ret != 0)
257     {
258         HMI_DEBUG("wm", "afb_event_push failed: %m");
259     }
260 }
261 #endif
262
263 void WMClient::dumpInfo()
264 {
265     DUMP("APPID : %s", this->id.c_str());
266     DUMP("  LAYER : %d", this->layer);
267     for (const auto &x : this->role_list)
268     {
269         DUMP("  ROLE  : %s , SURFACE : %d", x.c_str());
270     }
271     DUMP("  MAIN SURFACE : %d", this->surface);
272     string ro = "";
273     for (const auto &x : this->surface_render_order)
274     {
275         ro += x;
276         ro += " ,";
277     }
278     DUMP("  RENDER ORDER  : %s", ro.c_str());
279 }
280
281 } // namespace wm