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