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