Return nullptr if client is not found
[apps/agl-service-windowmanager-2017.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 "util.hpp"
20
21 using std::shared_ptr;
22 using std::string;
23 using std::vector;
24
25 namespace wm
26 {
27
28 const static int kReserveClientSize = 100;
29 const static int kReserveReqSize    = 10;
30
31 /**
32  * AppList Constructor.
33  *
34  * Reserve the container size to avoid re-allocating memory.
35  *
36  * @note Size should be changed according to the system.
37  *       If the number of applications becomes over the size, re-allocating memory will happen.
38  */
39 AppList::AppList()
40     : req_list(),
41       app2client(),
42       current_req(1)
43 {
44     this->app2client.reserve(kReserveClientSize);
45     this->req_list.reserve(kReserveReqSize);
46 }
47
48 AppList::~AppList() {}
49
50 // =================== Client Date container API ===================
51
52 /**
53  * Add Client to the list
54  *
55  * Add Client to the list.
56  * The Client means application which has role, layer, surface
57  * This function should be called once for the app.
58  * Caller should take care not to be called more than once.
59  *
60  * @param     string[in]   Application id. This will be the key to withdraw the information.
61  * @param     unsigned[in] Layer ID in which the application is
62  * @param     unsigned[in] surface ID which the application has
63  * @param     string[in]   Role which means what behavior the application will do.
64  * @return    None
65  * @attention This function should be called once for the app
66  *            Caller should take care not to be called more than once.
67  */
68 void AppList::addClient(const string &appid, unsigned layer, unsigned surface, const string &role)
69 {
70     std::lock_guard<std::mutex> lock(this->mtx);
71     shared_ptr<WMClient> client = std::make_shared<WMClient>(appid, layer, surface, role);
72     this->app2client[appid] = client;
73     this->clientDump();
74 }
75
76 void AppList::addClient(const string &appid, unsigned layer, const string &role)
77 {
78     std::lock_guard<std::mutex> lock(this->mtx);
79     shared_ptr<WMClient> client = std::make_shared<WMClient>(appid, layer, role);
80     this->app2client[appid] = client;
81     this->clientDump();
82 }
83
84 /**
85  * Remove WMClient from the list
86  *
87  * @param string[in] Application id. This will be the key to withdraw the information.
88  */
89 void AppList::removeClient(const string &appid)
90 {
91     std::lock_guard<std::mutex> lock(this->mtx);
92     this->app2client.erase(appid);
93     HMI_INFO("Remove client %s", appid.c_str());
94 }
95
96 /**
97  * Check this class stores the appid.
98  *
99  * @param     string[in] Application id. This will be the key to withdraw the information.
100  * @return    true if the class has the requested key(appid)
101  */
102 bool AppList::contains(const string &appid) const
103 {
104     auto result = this->app2client.find(appid);
105     return (this->app2client.end() != result) ? true : false;
106 }
107
108 /**
109  * Remove surface from client
110  *
111  * @param     unsigned[in] surface id.
112  * @return    None
113  */
114 void AppList::removeSurface(unsigned surface){
115     // This function may be very slow
116     std::lock_guard<std::mutex> lock(this->mtx);
117     bool ret = false;
118     for (auto &x : this->app2client)
119     {
120         ret = x.second->removeSurfaceIfExist(surface);
121         if(ret){
122             HMI_DEBUG("remove surface %d from Client %s finish",
123                         surface, x.second->appID().c_str());
124             break;
125         }
126     }
127
128 }
129
130 /**
131  * Get WMClient object.
132  *
133  * After get the WMClient object, caller can call the client method.
134  * Before call this function, caller must call "contains"
135  * to check the key is contained, otherwise, you have to take care of std::out_of_range.
136  *
137  * @param     string[in] application id(key)
138  * @return    WMClient object
139  * @attention Must call cantains to check appid is stored before this function.
140  */
141 shared_ptr<WMClient> AppList::lookUpClient(const string &appid)
142 {
143     if(this->app2client.count(appid) != 0)
144     {
145         return this->app2client.at(appid);
146     }
147     else
148     {
149         return nullptr;
150     }
151 }
152
153 /**
154  * Count Client.
155  *
156  * Returns the number of client stored in the list.
157  *
158  * @param  None
159  * @return The number of client
160  */
161 int AppList::countClient() const
162 {
163     return this->app2client.size();
164 }
165
166 /**
167  * Get AppID with surface and role.
168  *
169  * Returns AppID if found.
170  *
171  * @param     unsigned[in] surfaceID
172  * @param     string[in]   role
173  * @param     bool[in,out] AppID is found or not
174  * @return    AppID
175  * @attention If AppID is not found, param found will be false.
176  */
177 /* string AppList::getAppID(unsigned surface, const string& role, bool* found) const
178 {
179     *found = false;
180     for (const auto &x : this->app2client)
181     {
182         if(x.second->surfaceID(role) == surface){
183             *found = true;
184             return x.second->appID();
185         }
186     }
187     return string("");
188 } */
189
190 string AppList::getAppID(unsigned surface, bool* found) const
191 {
192     *found = false;
193     for (const auto &x : this->app2client)
194     {
195         if(x.second->surfaceID() == surface){
196             *found = true;
197             return x.second->appID();
198         }
199     }
200     return string("");
201 }
202
203 WMError AppList::popFloatingSurface(unsigned pid, unsigned *surface)
204 {
205     WMError ret = WMError::NO_ENTRY;
206
207     auto fwd_itr = std::remove_if(this->floating_surfaces.begin(), this->floating_surfaces.end(),
208                                     [pid, surface, &ret](FloatingSurface x) {
209                                         if(pid == x.pid){
210                                             *surface = x.surface_id;
211                                             ret = WMError::SUCCESS;
212                                             return true;
213                                         }
214                                         else{
215                                             return false;
216                                         }
217                                     });
218     if (fwd_itr != this->floating_surfaces.cend())
219     {
220         HMI_INFO("pop floating surface: %d", *surface);
221     }
222     this->floating_surfaces.erase(fwd_itr, this->floating_surfaces.end());
223     return ret;
224 }
225
226 // =================== Floating(Temporary) surface/client API ===================
227
228 WMError AppList::popFloatingSurface(const string &appid, unsigned *surface)
229 {
230     HMI_ERROR("This function is not implemented");
231     return WMError::SUCCESS;
232 }
233
234 void AppList::addFloatingClient(const string &appid, unsigned layer, const string &role)
235 {
236 }
237
238 void AppList::addFloatingSurface(const string &appid, unsigned surface, unsigned pid)
239 {
240     struct FloatingSurface fsurface{appid, surface, pid};
241     this->floating_surfaces.push_back(fsurface);
242     this->dumpFloatingSurfaces();
243 }
244
245 void AppList::removeFloatingSurface(unsigned surface)
246 {
247     this->dumpFloatingSurfaces();
248     auto fwd_itr = std::remove_if(
249         this->floating_surfaces.begin(), this->floating_surfaces.end(),
250         [surface](FloatingSurface x) {
251             return x.surface_id == surface;
252         });
253     if(fwd_itr != this->floating_surfaces.cend()){
254         HMI_INFO("remove floating surface: %d", surface);
255     }
256     this->floating_surfaces.erase(fwd_itr, this->floating_surfaces.end());
257 }
258
259 WMError AppList::appendRole(const string &id, const string &role)
260 {
261     WMError wm_err = WMError::NO_ENTRY;
262     if (this->contains(id))
263     {
264         auto x = this->lookUpClient(id);
265         x->appendRole(role);
266         wm_err = WMError::SUCCESS;
267     }
268     return wm_err;
269 }
270
271
272 // =================== Request Date container API ===================
273
274 /**
275  * Get current request number
276  *
277  * Request number is the numeric ID to designate the request.
278  * But returned request number from this function doesn't mean the request exists.
279  * This number is used as key to withdraw the WMRequest object.
280  *
281  * @param  None
282  * @return current request number.
283  * @note   request number is more than 0.
284  */
285 unsigned AppList::currentRequestNumber() const
286 {
287     return this->current_req;
288 }
289
290 /**
291  * Get request number
292  *
293  * Request number is the numeric ID to designate the request.
294  * But returned request number from this function doesn't mean the request exists.
295  * This number is used as key to withdraw the WMRequest object.
296  *
297  * @param     None
298  * @return    request number.
299  * @attention If returned value is 0, no request exists.
300  */
301 unsigned AppList::getRequestNumber(const string &appid) const
302 {
303     for (const auto &x : this->req_list)
304     {
305         // Since app will not request twice and more, comparing appid is enough?
306         if ((x.trigger.appid == appid))
307         {
308             return x.req_num;
309         }
310     }
311     return 0;
312 }
313
314 /**
315  * Add Request
316  *
317  * Request number is the numeric ID to designate the request.
318  * But returned request number from this function doesn't mean the request exists.
319  * This number is used as key to withdraw the WMRequest object.
320  *
321  * @param     WMRequest[in] WMRequest object caller creates
322  * @return    Request number
323  * @attention If the request number is different with curent request number,
324  *            it means the previous request is not finished.
325  */
326 unsigned AppList::addRequest(WMRequest req)
327 {
328     std::lock_guard<std::mutex> lock(this->mtx);
329     if (this->req_list.size() == 0)
330     {
331         req.req_num = current_req;
332     }
333     else
334     {
335         HMI_SEQ_INFO(this->current_req, "add: %d", this->req_list.back().req_num + 1);
336         req.req_num = this->req_list.back().req_num + 1;
337     }
338     this->req_list.push_back(req);
339     return req.req_num;
340 }
341
342 /**
343  * Get trigger which the application requests
344  *
345  * WMTrigger contains which application requests what role and where to put(area) and task.
346  * This is used for input event to Window Policy Manager(state machine).
347  *
348  * @param     unsigned[in] request number
349  * @param     bool[in,out] Check request number of the parameter is valid.
350  * @return    WMTrigger which associates with the request number
351  * @attention If the request number is not valid, parameter "found" is false
352  *            and return value will be meaningless value.
353  *            Caller can check the request parameter is valid.
354  */
355 struct WMTrigger AppList::getRequest(unsigned req_num, bool *found)
356 {
357     *found = false;
358     for (const auto &x : this->req_list)
359     {
360         if (req_num == x.req_num)
361         {
362             *found = true;
363             return x.trigger;
364         }
365     }
366     HMI_SEQ_ERROR(req_num, "Couldn't get request : %d", req_num);
367     return WMTrigger{"", "", "", Task::TASK_INVALID};
368 }
369
370 /**
371  * Get actions which the application requests
372  *
373  * WMAciton contains the information of state transition.
374  * In other words, it contains actions of Window Manager,
375  * which role should be put to the area.
376  *
377  * @param     unsigned[in] request number
378  * @param     bool[in,out] Check request number of the parameter is valid.
379  * @return    WMTrigger which associates with the request number
380  * @attention If the request number is not valid, parameter "found" is false
381  *            and return value will be no reference pointer.
382  *            Caller must check the request parameter is valid.
383  */
384 const vector<struct WMAction> &AppList::getActions(unsigned req_num, bool* found)
385 {
386     *found = false;
387     for (auto &x : this->req_list)
388     {
389         if (req_num == x.req_num)
390         {
391             *found = true;
392             return x.sync_draw_req;
393         }
394     }
395     HMI_SEQ_ERROR(req_num, "Couldn't get action with the request : %d", req_num);
396 }
397
398 /**
399  * Set actions to the request.
400  *
401  * Add actions to the request.
402  * This function can be called many times, and actions increase.
403  * This function is used for decision of actions of Window Manager
404  * according to the result of Window Policy Manager.
405  *
406  * @param     unsigned[in] request number
407  * @param     WMAction[in] Action of Window Manager.
408  * @return    WMError If request number is not valid, FAIL will be returned.
409  */
410 WMError AppList::setAction(unsigned req_num, const struct WMAction &action)
411 {
412     std::lock_guard<std::mutex> lock(this->mtx);
413     WMError result = WMError::FAIL;
414     for (auto &x : this->req_list)
415     {
416         if (req_num != x.req_num)
417         {
418             continue;
419         }
420         x.sync_draw_req.push_back(action);
421         result = WMError::SUCCESS;
422         break;
423     }
424     return result;
425 }
426
427 /**
428  * Note:
429  * @note This function set action with parameters.
430  *       If visible is true, it means app should be visible, so enddraw_finished parameter should be false.
431  *       otherwise (visible is false) app should be invisible. Then enddraw_finished param is set to true.
432  *       This function doesn't support actions for focus yet.
433  */
434 /**
435  * Set actions to the request.
436  *
437  * This function is overload function.
438  * The feature is same as other one.
439  *
440  * @param     unsigned[in] request number
441  * @param     string[in]   application id
442  * @param     string[in]   role
443  * @param     string[in]   area
444  * @param     Task[in]     the role should be visible or not.
445  * @return    WMError If request number is not valid, FAIL will be returned.
446  * @attention This function set action with parameters, then caller doesn't need to create WMAction object.
447  *            If visible is true, it means app should be visible, so enddraw_finished parameter will be false.
448  *            otherwise (visible is false) app should be invisible. Then enddraw_finished param is set to true.
449  *            This function doesn't support actions for focus yet.
450  */
451 WMError AppList::setAction(unsigned req_num, shared_ptr<WMClient> client, const string &role, const string &area, TaskVisible visible)
452 {
453     std::lock_guard<std::mutex> lock(this->mtx);
454     WMError result = WMError::FAIL;
455     for (auto &x : req_list)
456     {
457         if (req_num != x.req_num)
458         {
459             continue;
460         }
461         // If visible task is not invisible, redraw is required -> true
462         bool edraw_f = (visible != TaskVisible::INVISIBLE) ? false : true;
463         WMAction action{req_num, client, role, area, visible, edraw_f};
464
465         x.sync_draw_req.push_back(action);
466         result = WMError::SUCCESS;
467         break;
468     }
469     return result;
470 }
471
472 /**
473  * Set end_draw_finished param is true
474  *
475  * This function checks
476  *   - req_num is equal to current request number
477  *   - appid and role are equeal to the appid and role stored in action list
478  * If it is valid, set the action is finished.
479  *
480  * @param  unsigned[in] request number
481  * @param  string[in]   application id
482  * @param  string[in]   role
483  * @return If the parameters are not valid in action list, returns false
484  */
485 bool AppList::setEndDrawFinished(unsigned req_num, const string &appid, const string &role)
486 {
487     std::lock_guard<std::mutex> lock(this->mtx);
488     bool result = false;
489     for (auto &x : req_list)
490     {
491         if (req_num < x.req_num)
492         {
493             break;
494         }
495         if (req_num == x.req_num)
496         {
497             for (auto &y : x.sync_draw_req)
498             {
499                 if (y.client->appID() == appid && y.role == role)
500                 {
501                     HMI_SEQ_INFO(req_num, "Role %s finish redraw", y.role.c_str());
502                     y.end_draw_finished = true;
503                     result = true;
504                 }
505             }
506         }
507     }
508     this->reqDump();
509     return result;
510 }
511
512 /**
513  * Check all actions of the requested sequence is finished
514  *
515  * @param  unsigned[in] request_number
516  * @return true if all action is set.
517  */
518 bool AppList::endDrawFullfilled(unsigned req_num)
519 {
520     bool result = false;
521     for (const auto &x : req_list)
522     {
523         if (req_num < x.req_num)
524         {
525             break;
526         }
527         if (req_num == x.req_num)
528         {
529             result = true;
530             for (const auto &y : x.sync_draw_req)
531             {
532                 result &= y.end_draw_finished;
533                 if (!result)
534                 {
535                     break;
536                 }
537             }
538         }
539     }
540     return result;
541 }
542
543 /**
544  * Finish the request, then remove it.
545  *
546  * @param  unsigned[in] request_number
547  * @return None
548  * @note   Please call next after this function to receive or process next request.
549  */
550 void AppList::removeRequest(unsigned req_num)
551 {
552     std::lock_guard<std::mutex> lock(this->mtx);
553     this->req_list.erase(remove_if(this->req_list.begin(), this->req_list.end(),
554                                    [req_num](WMRequest x) {
555                                        return x.req_num == req_num;
556                                    }));
557 }
558
559 /**
560  * Move the current request to next
561  *
562  * @param  None
563  * @return None
564  */
565 void AppList::next()
566 {
567     std::lock_guard<std::mutex> lock(this->mtx);
568     ++this->current_req;
569     if (0 == this->current_req)
570     {
571         this->current_req = 1;
572     }
573 }
574
575 /**
576  * Check the request exists is in request list
577  *
578  * @param  None
579  * @return true if WMRequest exists in the request list
580  */
581 bool AppList::haveRequest() const
582 {
583     return !this->req_list.empty();
584 }
585
586 void AppList::clientDump()
587 {
588     DUMP("======= client dump =====");
589     for (const auto &x : this->app2client)
590     {
591         const auto &y = x.second;
592         y->dumpInfo();
593     }
594     DUMP("======= client dump end=====");
595 }
596
597 void AppList::reqDump()
598 {
599     DUMP("======= req dump =====");
600     DUMP("current request : %d", current_req);
601     for (const auto &x : req_list)
602     {
603         DUMP("requested       : %d", x.req_num);
604         DUMP("Trigger : (APPID :%s, ROLE :%s, AREA :%s, TASK: %d)",
605              x.trigger.appid.c_str(),
606              x.trigger.role.c_str(),
607              x.trigger.area.c_str(),
608              x.trigger.task);
609
610         for (const auto &y : x.sync_draw_req)
611         {
612             DUMP(
613                 "Action  : (APPID :%s, ROLE :%s, AREA :%s, VISIBLE : %s, END_DRAW_FINISHED: %d)",
614                 y.client->appID().c_str(),
615                 y.role.c_str(),
616                 y.area.c_str(),
617                 (y.visible == TaskVisible::INVISIBLE) ? "invisible" : "visible",
618                 y.end_draw_finished);
619         }
620     }
621     DUMP("======= req dump end =====");
622 }
623
624 void AppList::dumpFloatingSurfaces()
625 {
626     DUMP("======= floating surface dump =====");
627     for (const auto &x : this->floating_surfaces)
628     {
629         DUMP("surface : %d, pid : %d", x.surface_id, x.pid);
630     }
631     DUMP("======= floating surface dump end =====\n");
632 }
633
634 } // namespace wm