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