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