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