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