c6eaa0cc90b0894dd5674980851943c17d8997d4
[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] AppID is found or not
158  * @return    AppID
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::addRequest(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     Task[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, TaskVisible 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         // If visible task is not invisible, redraw is required -> true
364         bool edraw_f = (visible != TaskVisible::INVISIBLE) ? false : true;
365         WMAction action{appid, role, area, visible, edraw_f};
366
367         x.sync_draw_req.push_back(action);
368         result = WMError::SUCCESS;
369         break;
370     }
371     return result;
372 }
373
374 /**
375  * Set end_draw_finished param is true
376  *
377  * This function checks
378  *   - req_num is equal to current request number
379  *   - appid and role are equeal to the appid and role stored in action list
380  * If it is valid, set the action is finished.
381  *
382  * @param  unsigned[in] request number
383  * @param  string[in]   application id
384  * @param  string[in]   role
385  * @return If the parameters are not valid in action list, returns false
386  */
387 bool AppList::setEndDrawFinished(unsigned req_num, const string &appid, const string &role)
388 {
389     std::lock_guard<std::mutex> lock(this->mtx);
390     bool result = false;
391     for (auto &x : req_list)
392     {
393         if (req_num < x.req_num)
394         {
395             break;
396         }
397         if (req_num == x.req_num)
398         {
399             for (auto &y : x.sync_draw_req)
400             {
401                 if (y.appid == appid && y.role == role)
402                 {
403                     HMI_SEQ_INFO(req_num, "Role %s finish redraw", y.role.c_str());
404                     y.end_draw_finished = true;
405                     result = true;
406                 }
407             }
408         }
409     }
410     this->reqDump();
411     return result;
412 }
413
414 /**
415  * Check all actions of the requested sequence is finished
416  *
417  * @param  unsigned[in] request_number
418  * @return true if all action is set.
419  */
420 bool AppList::endDrawFullfilled(unsigned req_num)
421 {
422     bool result = false;
423     for (const auto &x : req_list)
424     {
425         if (req_num < x.req_num)
426         {
427             break;
428         }
429         if (req_num == x.req_num)
430         {
431             result = true;
432             for (const auto &y : x.sync_draw_req)
433             {
434                 result &= y.end_draw_finished;
435                 if (!result)
436                 {
437                     break;
438                 }
439             }
440         }
441     }
442     return result;
443 }
444
445 /**
446  * Finish the request, then remove it.
447  *
448  * @param  unsigned[in] request_number
449  * @return None
450  * @note   Please call next after this function to receive or process next request.
451  */
452 void AppList::removeRequest(unsigned req_num)
453 {
454     std::lock_guard<std::mutex> lock(this->mtx);
455     this->req_list.erase(remove_if(this->req_list.begin(), this->req_list.end(),
456                                    [req_num](WMRequest x) {
457                                        return x.req_num == req_num;
458                                    }));
459 }
460
461 /**
462  * Move the current request to next
463  *
464  * @param  None
465  * @return None
466  */
467 void AppList::next()
468 {
469     std::lock_guard<std::mutex> lock(this->mtx);
470     ++this->current_req;
471     if (0 == this->current_req)
472     {
473         this->current_req = 1;
474     }
475 }
476
477 /**
478  * Check the request exists is in request list
479  *
480  * @param  None
481  * @return true if WMRequest exists in the request list
482  */
483 bool AppList::haveRequest() const
484 {
485     return !this->req_list.empty();
486 }
487
488 void AppList::clientDump()
489 {
490     DUMP("======= client dump =====");
491     for (const auto &x : this->app2client)
492     {
493         const auto &y = x.second;
494         y->dumpInfo();
495     }
496     DUMP("======= client dump end=====");
497 }
498
499 void AppList::reqDump()
500 {
501     DUMP("======= req dump =====");
502     DUMP("current request : %d", current_req);
503     for (const auto &x : req_list)
504     {
505         DUMP("requested       : %d", x.req_num);
506         DUMP("Trigger : (APPID :%s, ROLE :%s, AREA :%s, TASK: %d)",
507              x.trigger.appid.c_str(),
508              x.trigger.role.c_str(),
509              x.trigger.area.c_str(),
510              x.trigger.task);
511
512         for (const auto &y : x.sync_draw_req)
513         {
514             DUMP(
515                 "Action  : (APPID :%s, ROLE :%s, AREA :%s, VISIBLE : %s, END_DRAW_FINISHED: %d)",
516                 y.appid.c_str(),
517                 y.role.c_str(),
518                 y.area.c_str(),
519                 (y.visible == TaskVisible::INVISIBLE) ? "invisible" : "visible",
520                 y.end_draw_finished);
521         }
522     }
523     DUMP("======= req dump end =====");
524 }
525 } // namespace wm