Proposal: New layer settings
[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 const string& WMClient::getWMLayerName()
105 {
106     return this->wm_layer_name;
107 }
108
109 void WMClient::setRole(const string& role)
110 {
111     this->role_list.push_back(role);
112 }
113
114 /**
115  * Set layerID the client belongs to
116  *
117  * This function set layerID the client belongs to.
118  * But this function may not used because the layer should be fixed at constructor.
119  * So this function will be used to change layer by some reasons.
120  *
121  * @param     unsigned[in] layerID
122  * @return    None
123  * @attention WMClient can't have multiple layer
124  */
125 void WMClient::registerLayer(unsigned layer)
126 {
127     this->layer = layer;
128 }
129
130 /**
131  * Add the pair of name and surface of surface to the client
132  *
133  * This function set the pair of role and surface to the client.
134  * This function is used for the client which has multi surfaces.
135  * If the model and relationship for role and surface(layer)
136  * is changed, this function will be changed
137  * Current Window Manager doesn't use this function.
138  *
139  * @param     string[in] name
140  * @param     unsigned[in] surface
141  * @return    true
142  */
143 void WMClient::attachServiceSurface(const string &name, unsigned surface)
144 {
145     HMI_DEBUG("wm", "Add surface name %s with surface %d", name.c_str(), surface);
146     if (0 != this->service2surfaces.count(name))
147     {
148         HMI_NOTICE("wm", "override surfaceID %d with %d", this->service2surfaces[name], surface);
149     }
150     this->service2surfaces[name] = surface;
151 }
152
153 void WMClient::removeServiceSurface(const std::string &name)
154 {
155     for (auto &x : this->service2surfaces)
156     {
157         if (name == x.first)
158         {
159             HMI_INFO("wm", "Remove surface from client %s: service surface name %s, surface: %d",
160                         this->id.c_str(), x.first.c_str(), x.second);
161             this->service2surfaces.erase(name);
162         }
163     }
164     // re_asign render order
165     this->setRenderOrder(this->surface_render_order);
166 }
167
168 void WMClient::setArea(const std::string& area)
169 {
170     this->area = area;
171     // TODO: implementation
172 }
173
174 void WMClient::setRenderOrder()
175 {
176     // use private surface_render_order
177 }
178
179 void WMClient::setRenderOrder(const vector<string> &render_order)
180 {
181     // TODO: implement LM
182 }
183
184 void WMClient::setRenderOrder(const vector<unsigned> &render_order)
185 {
186     // TODO implement LM
187 }
188
189 bool WMClient::removeSurfaceIfExist(unsigned surface)
190 {
191     bool ret = false;
192     auto fwd_itr = std::remove_if(
193         this->surface_render_order.begin(), this->surface_render_order.end(),
194         [surface, &ret](unsigned x) {
195             ret = true;
196             return x == surface;
197         });
198     this->surface_render_order.erase(fwd_itr, this->surface_render_order.end());
199     if(ret)
200     {
201         for(auto &y : this->service2surfaces)
202         {
203             if(y.second == surface)
204             {
205                 HMI_INFO("wm", "Remove surface from client %s: role %s, surface: %d",
206                         this->id.c_str(), y.first.c_str(), y.second);
207                 this->service2surfaces.erase(y.first);
208             }
209         }
210         if(this->surface == surface)
211         {
212             this->surface = INVALID_SURFACE_ID;
213             // TODO: If main surface vanishes, how do we treat?
214             //       Window Manager doesn't know it's means dead or on purpose.
215             // option1: remove service surface and notify to the service.
216             // option2: waiting the application calls deallocate.
217             HMI_INFO("wm", "Main surface vanishes");
218         }
219     }
220     return ret;
221 }
222
223 void WMClient::removeRole(const string &role)
224 {
225     auto fwd_itr = std::remove_if(
226         this->role_list.begin(), this->role_list.end(),
227         [&role](string x) {
228             return x == role;
229         });
230     this->role_list.erase(fwd_itr, this->role_list.end());
231 }
232
233 #if GTEST_ENABLED
234 bool WMClient::subscribe(afb_req req, const string &evname)
235 {
236     if(evname != kKeyError){
237         HMI_DEBUG("wm", "error is only enabeled for now");
238         return false;
239     }
240     int ret = afb_req_subscribe(req, this->evname2afb_event[evname]);
241     if (ret)
242     {
243         HMI_DEBUG("wm", "Failed to subscribe %s", evname.c_str());
244         return false;
245     }
246     return true;
247 }
248
249 void WMClient::emitError(WM_CLIENT_ERROR_EVENT ev)
250 {
251     if (!afb_event_is_valid(this->evname2afb_event[kKeyError])){
252         HMI_ERROR("wm", "event err is not valid");
253         return;
254     }
255     json_object *j = json_object_new_object();
256     json_object_object_add(j, kKeyError, json_object_new_int(ev));
257     json_object_object_add(j, kKeyErrorDesc, json_object_new_string(kErrorDescription[ev].c_str()));
258     HMI_DEBUG("wm", "error: %d, description:%s", ev, kErrorDescription[ev].c_str());
259
260     int ret = afb_event_push(this->evname2afb_event[kKeyError], j);
261     if (ret != 0)
262     {
263         HMI_DEBUG("wm", "afb_event_push failed: %m");
264     }
265 }
266 #endif
267
268 void WMClient::dumpInfo()
269 {
270     DUMP("APPID : %s", this->id.c_str());
271     DUMP("  LAYER : %d", this->layer);
272     for (const auto &x : this->role_list)
273     {
274         DUMP("  ROLE  : %s , SURFACE : %d", x.c_str());
275     }
276     DUMP("  MAIN SURFACE : %d", this->surface);
277     string ro = "";
278     for (const auto &x : this->surface_render_order)
279     {
280         ro += x;
281         ro += " ,";
282     }
283     DUMP("  RENDER ORDER  : %s", ro.c_str());
284 }
285
286 } // namespace wm