4e31c0348c45c8daabe6d8d9f47b0ca8d645ad86
[apps/agl-service-windowmanager.git] / src / applist.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 #include <iostream>
17 #include <algorithm>
18 #include "applist.hpp"
19 #include "../include/hmi-debug.h"
20
21 using std::shared_ptr;
22 using std::string;
23 using std::vector;
24
25 namespace wm
26 {
27
28 AppList::AppList()
29     : req_list(0),
30       app2client(0),
31       current_req(1)
32 {
33 }
34
35 void AppList::addClient(const string &appid, const string &role)
36 {
37     shared_ptr<WMClient> client = std::make_shared<WMClient>(appid, role);
38     this->app2client[appid] = client;
39     this->clientDump();
40 }
41
42 void AppList::addClient(const std::string &appid, unsigned layer, unsigned surface, const std::string &role)
43 {
44     shared_ptr<WMClient> client = std::make_shared<WMClient>(appid, layer, surface, role);
45     this->app2client[appid] = client;
46     this->clientDump();
47 }
48
49 void AppList::removeClient(const string &appid)
50 {
51     this->app2client.erase(appid);
52 }
53
54 bool AppList::contains(const string &appid) const
55 {
56     auto result = this->app2client.find(appid);
57     return (this->app2client.end() != result) ? true : false;
58 }
59
60 void AppList::removeSurface(unsigned surface_id){
61     // This function may be very slow
62     bool ret = false;
63     for (auto &x : this->app2client)
64     {
65         ret = x.second->removeSurfaceIfExist(surface_id);
66         if(ret){
67             HMI_DEBUG("wm", "remove surface %d from Client %s finish", surface_id, x.second->appID().c_str());
68             break;
69         }
70     }
71 }
72
73 /**
74  * @brief  get WMClient object. Before call this function, must call "contains"
75  * to check the key is contained, otherwise, you have to take care of std::out_of_range.
76  * @param string[in] application id(key)
77  * @return WMClient object
78  */
79 shared_ptr<WMClient> AppList::lookUpClient(const string &appid)
80 {
81     return this->app2client.at(appid);
82 }
83
84 int AppList::countClient() const
85 {
86     return this->app2client.size();
87 }
88
89 unsigned AppList::currentRequestNumber() const
90 {
91     return this->current_req;
92 }
93
94 WMError AppList::lookUpFloatingSurface(unsigned pid, unsigned *surface)
95 {
96     WMError ret = WMError::NO_ENTRY;
97
98     for (auto itr = this->floating_surfaces.begin(); itr != this->floating_surfaces.end(); ++itr)
99     {
100         if(pid == itr->pid){
101             *surface = itr->surface_id;
102             itr = this->floating_surfaces.erase(itr);
103             ret = WMError::SUCCESS;
104             HMI_DEBUG("wm", "Erase surface %d", *surface);
105             break;
106         }
107     }
108     return ret;
109 }
110
111 WMError AppList::lookUpFloatingSurface(const std::string &appid, unsigned *surface)
112 {
113     return WMError::SUCCESS;
114 }
115
116 WMError AppList::appendRole(const std::string &id, const std::string &role, unsigned surface)
117 {
118     WMError wm_err = WMError::NO_ENTRY;
119     if (this->contains(id))
120     {
121         auto x = this->lookUpClient(id);
122         x->addSurface(role, surface);
123         wm_err = WMError::SUCCESS;
124     }
125     return wm_err;
126 }
127
128 unsigned AppList::getRequestNumber(const string &appid) const
129 {
130     for (const auto &x : this->req_list)
131     {
132         // Since app will not request twice and more, comparing appid is enough?
133         if ((x.trigger.appid == appid))
134         {
135             return x.req_num;
136         }
137     }
138     return 0;
139 }
140
141 unsigned AppList::addAllocateRequest(WMRequest req)
142 {
143     if (this->req_list.size() == 0)
144     {
145         req.req_num = current_req;
146     }
147     else
148     {
149         HMI_SEQ_DEBUG(this->current_req, "add: %d", this->req_list.back().req_num + 1);
150         req.req_num = this->req_list.back().req_num + 1;
151     }
152     this->req_list.push_back(req);
153     return req.req_num; // return 1; if you test time_expire
154 }
155
156 struct WMTrigger AppList::getRequest(unsigned req_num)
157 {
158     for (const auto &x : this->req_list)
159     {
160         if (req_num == x.req_num)
161         {
162             return x.trigger;
163         }
164     }
165 }
166
167 const vector<struct WMAction> &AppList::getActions(unsigned req_num)
168 {
169     for (auto &x : this->req_list)
170     {
171         if (req_num == x.req_num)
172         {
173             return x.sync_draw_req;
174         }
175     }
176 }
177
178 WMError AppList::setAction(unsigned req_num, const struct WMAction &action)
179 {
180     WMError result = WMError::FAIL;
181     for (auto &x : this->req_list)
182     {
183         if (req_num != x.req_num)
184         {
185             continue;
186         }
187         x.sync_draw_req.push_back(action);
188         result = WMError::SUCCESS;
189         break;
190     }
191
192     return result;
193 }
194
195 /**
196  * Note:
197  * This function set action with parameters.
198  * if visible is true, it means app should be visible, so enddraw_finished parameter should be false.
199  * otherwise (visible is false) app should be invisible. Then enddraw_finished param is set to true.
200  * This function doesn't support actions for focus yet.
201  */
202 WMError AppList::setAction(unsigned req_num, const string &appid, const string &role, const string &area, bool visible)
203 {
204     WMError result = WMError::NOT_REGISTERED;
205     for (auto &x : req_list)
206     {
207         if (req_num != x.req_num)
208         {
209             continue;
210         }
211         bool edraw_f = (visible) ? false : true;
212         WMAction action{appid, role, area, visible, edraw_f};
213
214         x.sync_draw_req.push_back(action);
215         result = WMError::SUCCESS;
216         break;
217     }
218     return result;
219 }
220
221 /**
222  * This function checks
223  *   * req_num is equal to current request number
224  *   * appid and role are equeal to the appid and role stored in action list(sync_draw_req)
225  */
226 bool AppList::setEndDrawFinished(unsigned req_num, const string &appid, const string &role)
227 {
228     bool result = false;
229     for (auto &x : req_list)
230     {
231         if (req_num < x.req_num)
232         {
233             break;
234         }
235         if (req_num == x.req_num)
236         {
237             for (auto &y : x.sync_draw_req)
238             {
239                 if (y.appid == appid && y.role == role)
240                 {
241                     y.end_draw_finished = true;
242                     result = true;
243                 }
244             }
245         }
246     }
247     this->reqDump();
248     return result;
249 }
250
251 /**
252  * @brief  check all actions of the requested sequence is finished
253  * @param  unsigned request_number
254  * @return true if all action is set.
255  */
256 bool AppList::endDrawFullfilled(unsigned req_num)
257 {
258     bool result = false;
259     for (const auto &x : req_list)
260     {
261         if (req_num < x.req_num)
262         {
263             break;
264         }
265         if (req_num == x.req_num)
266         {
267             result = true;
268             for (const auto &y : x.sync_draw_req)
269             {
270                 result &= y.end_draw_finished;
271                 if (!result)
272                 {
273                     break;
274                 }
275             }
276         }
277     }
278     return result;
279 }
280
281 void AppList::removeRequest(unsigned req_num)
282 {
283     this->req_list.erase(remove_if(this->req_list.begin(), this->req_list.end(),
284                                    [req_num](WMRequest x) {
285                                        return x.req_num == req_num;
286                                    }));
287 }
288
289 void AppList::next()
290 {
291     ++this->current_req;
292     if (0 == this->current_req)
293     {
294         this->current_req = 1;
295     }
296 }
297
298 bool AppList::haveRequest() const
299 {
300     return !this->req_list.empty();
301 }
302
303 void AppList::clientDump()
304 {
305     DUMP("======= client dump =====");
306     for (const auto &x : this->app2client)
307     {
308         const auto &y = x.second;
309         y->dumpInfo();
310     }
311     DUMP("======= client dump end=====");
312 }
313
314 void AppList::reqDump()
315 {
316     DUMP("======= req dump =====");
317     DUMP("current request : %d", current_req);
318     for (const auto &x : req_list)
319     {
320         DUMP("requested with  : %d", x.req_num);
321         DUMP("Trigger : (APPID :%s, ROLE :%s, AREA :%s, TASK: %d)",
322              x.trigger.appid.c_str(),
323              x.trigger.role.c_str(),
324              x.trigger.area.c_str(),
325              x.trigger.task);
326
327         for (const auto &y : x.sync_draw_req)
328         {
329             DUMP(
330                 "Action  : (APPID :%s, ROLE :%s, AREA :%s, END_DRAW_FINISHED: %d)",
331                 y.appid.c_str(),
332                 y.role.c_str(),
333                 y.area.c_str(),
334                 y.end_draw_finished);
335         }
336     }
337     DUMP("======= req dump end =====\n");
338 }
339 } // namespace wm