Merge "set_role_pid_ver"
[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 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 std::string &appid, unsigned layer, unsigned surface, const std::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 std::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 std::string &appid, unsigned layer, const std::string &role)
207 {
208 }
209
210 void AppList::addFloatingSurface(unsigned surface, unsigned pid)
211 {
212     struct FloatingSurface fsurface{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(this->floating_surfaces.begin(), this->floating_surfaces.end(),
221                                        [surface](FloatingSurface x) {
222                                            return x.surface_id == surface;
223                                        });
224     if(fwd_itr != this->floating_surfaces.cend()){
225         HMI_INFO("wm", "remove floating surface: %d", surface);
226     }
227     this->floating_surfaces.erase(fwd_itr, this->floating_surfaces.end());
228 }
229
230 WMError AppList::appendRole(const std::string &id, const std::string &role, unsigned surface)
231 {
232     WMError wm_err = WMError::NO_ENTRY;
233     if (this->contains(id))
234     {
235         auto x = this->lookUpClient(id);
236         x->addSurface(role, surface);
237         wm_err = WMError::SUCCESS;
238     }
239     return wm_err;
240 }
241
242
243 // =================== Request Date container API ===================
244
245 /**
246  * Get current request number
247  *
248  * Request number is the numeric ID to designate the request.
249  * But returned request number from this function doesn't mean the request exists.
250  * This number is used as key to withdraw the WMRequest object.
251  *
252  * @param  None
253  * @return current request number.
254  * @note   request number is more than 0.
255  */
256 unsigned AppList::currentRequestNumber() const
257 {
258     return this->current_req;
259 }
260
261 /**
262  * Get request number
263  *
264  * Request number is the numeric ID to designate the request.
265  * But returned request number from this function doesn't mean the request exists.
266  * This number is used as key to withdraw the WMRequest object.
267  *
268  * @param     None
269  * @return    request number.
270  * @attention If returned value is 0, no request exists.
271  */
272 unsigned AppList::getRequestNumber(const string &appid) const
273 {
274     for (const auto &x : this->req_list)
275     {
276         // Since app will not request twice and more, comparing appid is enough?
277         if ((x.trigger.appid == appid))
278         {
279             return x.req_num;
280         }
281     }
282     return 0;
283 }
284
285 /**
286  * Add Request
287  *
288  * Request number is the numeric ID to designate the request.
289  * But returned request number from this function doesn't mean the request exists.
290  * This number is used as key to withdraw the WMRequest object.
291  *
292  * @param     WMRequest[in] WMRequest object caller creates
293  * @return    Request number
294  * @attention If the request number is different with curent request number,
295  *            it means the previous request is not finished.
296  */
297 unsigned AppList::addRequest(WMRequest req)
298 {
299     std::lock_guard<std::mutex> lock(this->mtx);
300     if (this->req_list.size() == 0)
301     {
302         req.req_num = current_req;
303     }
304     else
305     {
306         HMI_SEQ_INFO(this->current_req, "add: %d", this->req_list.back().req_num + 1);
307         req.req_num = this->req_list.back().req_num + 1;
308     }
309     this->req_list.push_back(req);
310     return req.req_num;
311 }
312
313 /**
314  * Get trigger which the application requests
315  *
316  * WMTrigger contains which application requests what role and where to put(area) and task.
317  * This is used for input event to Window Policy Manager(state machine).
318  *
319  * @param     unsigned[in] request number
320  * @param     bool[in,out] Check request number of the parameter is valid.
321  * @return    WMTrigger which associates with the request number
322  * @attention If the request number is not valid, parameter "found" is false
323  *            and return value will be meaningless value.
324  *            Caller can check the request parameter is valid.
325  */
326 struct WMTrigger AppList::getRequest(unsigned req_num, bool *found)
327 {
328     *found = false;
329     for (const auto &x : this->req_list)
330     {
331         if (req_num == x.req_num)
332         {
333             *found = true;
334             return x.trigger;
335         }
336     }
337     HMI_SEQ_ERROR(req_num, "Couldn't get request : %d", req_num);
338     return WMTrigger{"", "", "", Task::TASK_INVALID};
339 }
340
341 /**
342  * Get actions which the application requests
343  *
344  * WMAciton contains the information of state transition.
345  * In other words, it contains actions of Window Manager,
346  * which role should be put to the area.
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 no reference pointer.
353  *            Caller must check the request parameter is valid.
354  */
355 const vector<struct WMAction> &AppList::getActions(unsigned req_num, bool* found)
356 {
357     *found = false;
358     for (auto &x : this->req_list)
359     {
360         if (req_num == x.req_num)
361         {
362             *found = true;
363             return x.sync_draw_req;
364         }
365     }
366     HMI_SEQ_ERROR(req_num, "Couldn't get action with the request : %d", req_num);
367 }
368
369 /**
370  * Set actions to the request.
371  *
372  * Add actions to the request.
373  * This function can be called many times, and actions increase.
374  * This function is used for decision of actions of Window Manager
375  * according to the result of Window Policy Manager.
376  *
377  * @param     unsigned[in] request number
378  * @param     WMAction[in] Action of Window Manager.
379  * @return    WMError If request number is not valid, FAIL will be returned.
380  */
381 WMError AppList::setAction(unsigned req_num, const struct WMAction &action)
382 {
383     std::lock_guard<std::mutex> lock(this->mtx);
384     WMError result = WMError::FAIL;
385     for (auto &x : this->req_list)
386     {
387         if (req_num != x.req_num)
388         {
389             continue;
390         }
391         x.sync_draw_req.push_back(action);
392         result = WMError::SUCCESS;
393         break;
394     }
395     return result;
396 }
397
398 /**
399  * Note:
400  * @note This function set action with parameters.
401  *       If visible is true, it means app should be visible, so enddraw_finished parameter should be false.
402  *       otherwise (visible is false) app should be invisible. Then enddraw_finished param is set to true.
403  *       This function doesn't support actions for focus yet.
404  */
405 /**
406  * Set actions to the request.
407  *
408  * This function is overload function.
409  * The feature is same as other one.
410  *
411  * @param     unsigned[in] request number
412  * @param     string[in]   application id
413  * @param     string[in]   role
414  * @param     string[in]   area
415  * @param     Task[in]     the role should be visible or not.
416  * @return    WMError If request number is not valid, FAIL will be returned.
417  * @attention This function set action with parameters, then caller doesn't need to create WMAction object.
418  *            If visible is true, it means app should be visible, so enddraw_finished parameter will be false.
419  *            otherwise (visible is false) app should be invisible. Then enddraw_finished param is set to true.
420  *            This function doesn't support actions for focus yet.
421  */
422 WMError AppList::setAction(unsigned req_num, const string &appid, const string &role, const string &area, TaskVisible visible)
423 {
424     std::lock_guard<std::mutex> lock(this->mtx);
425     WMError result = WMError::FAIL;
426     for (auto &x : req_list)
427     {
428         if (req_num != x.req_num)
429         {
430             continue;
431         }
432         // If visible task is not invisible, redraw is required -> true
433         bool edraw_f = (visible != TaskVisible::INVISIBLE) ? false : true;
434         WMAction action{appid, role, area, visible, edraw_f};
435
436         x.sync_draw_req.push_back(action);
437         result = WMError::SUCCESS;
438         break;
439     }
440     return result;
441 }
442
443 /**
444  * Set end_draw_finished param is true
445  *
446  * This function checks
447  *   - req_num is equal to current request number
448  *   - appid and role are equeal to the appid and role stored in action list
449  * If it is valid, set the action is finished.
450  *
451  * @param  unsigned[in] request number
452  * @param  string[in]   application id
453  * @param  string[in]   role
454  * @return If the parameters are not valid in action list, returns false
455  */
456 bool AppList::setEndDrawFinished(unsigned req_num, const string &appid, const string &role)
457 {
458     std::lock_guard<std::mutex> lock(this->mtx);
459     bool result = false;
460     for (auto &x : req_list)
461     {
462         if (req_num < x.req_num)
463         {
464             break;
465         }
466         if (req_num == x.req_num)
467         {
468             for (auto &y : x.sync_draw_req)
469             {
470                 if (y.appid == appid && y.role == role)
471                 {
472                     HMI_SEQ_INFO(req_num, "Role %s finish redraw", y.role.c_str());
473                     y.end_draw_finished = true;
474                     result = true;
475                 }
476             }
477         }
478     }
479     this->reqDump();
480     return result;
481 }
482
483 /**
484  * Check all actions of the requested sequence is finished
485  *
486  * @param  unsigned[in] request_number
487  * @return true if all action is set.
488  */
489 bool AppList::endDrawFullfilled(unsigned req_num)
490 {
491     bool result = false;
492     for (const auto &x : req_list)
493     {
494         if (req_num < x.req_num)
495         {
496             break;
497         }
498         if (req_num == x.req_num)
499         {
500             result = true;
501             for (const auto &y : x.sync_draw_req)
502             {
503                 result &= y.end_draw_finished;
504                 if (!result)
505                 {
506                     break;
507                 }
508             }
509         }
510     }
511     return result;
512 }
513
514 /**
515  * Finish the request, then remove it.
516  *
517  * @param  unsigned[in] request_number
518  * @return None
519  * @note   Please call next after this function to receive or process next request.
520  */
521 void AppList::removeRequest(unsigned req_num)
522 {
523     std::lock_guard<std::mutex> lock(this->mtx);
524     this->req_list.erase(remove_if(this->req_list.begin(), this->req_list.end(),
525                                    [req_num](WMRequest x) {
526                                        return x.req_num == req_num;
527                                    }));
528 }
529
530 /**
531  * Move the current request to next
532  *
533  * @param  None
534  * @return None
535  */
536 void AppList::next()
537 {
538     std::lock_guard<std::mutex> lock(this->mtx);
539     ++this->current_req;
540     if (0 == this->current_req)
541     {
542         this->current_req = 1;
543     }
544 }
545
546 /**
547  * Check the request exists is in request list
548  *
549  * @param  None
550  * @return true if WMRequest exists in the request list
551  */
552 bool AppList::haveRequest() const
553 {
554     return !this->req_list.empty();
555 }
556
557 void AppList::clientDump()
558 {
559     DUMP("======= client dump =====");
560     for (const auto &x : this->app2client)
561     {
562         const auto &y = x.second;
563         y->dumpInfo();
564     }
565     DUMP("======= client dump end=====");
566 }
567
568 void AppList::reqDump()
569 {
570     DUMP("======= req dump =====");
571     DUMP("current request : %d", current_req);
572     for (const auto &x : req_list)
573     {
574         DUMP("requested       : %d", x.req_num);
575         DUMP("Trigger : (APPID :%s, ROLE :%s, AREA :%s, TASK: %d)",
576              x.trigger.appid.c_str(),
577              x.trigger.role.c_str(),
578              x.trigger.area.c_str(),
579              x.trigger.task);
580
581         for (const auto &y : x.sync_draw_req)
582         {
583             DUMP(
584                 "Action  : (APPID :%s, ROLE :%s, AREA :%s, VISIBLE : %s, END_DRAW_FINISHED: %d)",
585                 y.appid.c_str(),
586                 y.role.c_str(),
587                 y.area.c_str(),
588                 (y.visible == TaskVisible::INVISIBLE) ? "invisible" : "visible",
589                 y.end_draw_finished);
590         }
591     }
592     DUMP("======= req dump end =====");
593 }
594
595 void AppList::dumpFloatingSurfaces()
596 {
597     DUMP("======= floating surface dump =====");
598     for (const auto &x : this->floating_surfaces)
599     {
600         DUMP("surface : %d, pid : %d", x.surface_id, x.pid);
601     }
602     DUMP("======= floating surface dump end =====\n");
603 }
604
605 } // namespace wm