2 * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "applist.hpp"
19 #include "../include/hmi-debug.h"
21 using std::shared_ptr;
28 const static int kReserveClientSize = 100;
29 const static int kReserveReqSize = 10;
32 * AppList Constructor.
34 * Reserve the container size to avoid re-allocating memory.
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.
44 app2client.reserve(kReserveClientSize);
45 req_list.reserve(kReserveReqSize);
48 AppList::~AppList() {}
50 // =================== Client Date container API ===================
53 * Add Client to the list
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.
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.
65 * @attention This function should be called once for the app
66 * Caller should take care not to be called more than once.
68 void AppList::addClient(const std::string &appid, unsigned layer, unsigned surface, const std::string &role)
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;
77 * Remove WMClient from the list
79 * @param string[in] Application id. This will be the key to withdraw the information.
81 void AppList::removeClient(const string &appid)
83 std::lock_guard<std::mutex> lock(mtx);
84 this->app2client.erase(appid);
85 HMI_INFO("wm", "Remove client %s", appid.c_str());
89 * Check this class stores the appid.
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)
94 bool AppList::contains(const string &appid) const
96 auto result = this->app2client.find(appid);
97 return (this->app2client.end() != result) ? true : false;
100 void AppList::removeSurface(unsigned surface_id){
101 // This function may be very slow
102 std::lock_guard<std::mutex> lock(mtx);
104 for (auto &x : this->app2client)
106 ret = x.second->removeSurfaceIfExist(surface_id);
108 HMI_DEBUG("wm", "remove surface %d from Client %s finish", surface_id, x.second->appID().c_str());
116 * Get WMClient object.
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.
122 * @param string[in] application id(key)
123 * @return WMClient object
124 * @attention Must call cantains to check appid is stored before this function.
126 shared_ptr<WMClient> AppList::lookUpClient(const string &appid)
128 return this->app2client.at(appid);
134 * Returns the number of client stored in the list.
137 * @return The number of client
139 int AppList::countClient() const
141 return this->app2client.size();
145 * Get AppID with surface and role.
147 * Returns AppID if found.
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.
155 string AppList::getAppID(unsigned surface, const string& role, bool* found) const
158 for (const auto &x : this->app2client)
160 if(x.second->surfaceID(role) == surface){
162 return x.second->appID();
168 // =================== Request Date container API ===================
171 * Get current request number
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.
178 * @return current request number.
179 * @note request number is more than 0.
181 unsigned AppList::currentRequestNumber() const
183 return this->current_req;
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.
194 * @return request number.
195 * @attention If returned value is 0, no request exists.
197 unsigned AppList::getRequestNumber(const string &appid) const
199 for (const auto &x : this->req_list)
201 // Since app will not request twice and more, comparing appid is enough?
202 if ((x.trigger.appid == appid))
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.
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.
222 unsigned AppList::addAllocateRequest(WMRequest req)
224 std::lock_guard<std::mutex> lock(mtx);
225 if (this->req_list.size() == 0)
227 req.req_num = current_req;
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;
234 this->req_list.push_back(req);
239 * Get trigger which the application requests
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).
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.
251 struct WMTrigger AppList::getRequest(unsigned req_num, bool *found)
254 for (const auto &x : this->req_list)
256 if (req_num == x.req_num)
262 HMI_SEQ_ERROR(req_num, "Couldn't get request : %d", req_num);
263 return WMTrigger{"", "", "", Task::TASK_INVALID};
267 * Get actions which the application requests
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.
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.
280 const vector<struct WMAction> &AppList::getActions(unsigned req_num, bool* found)
283 for (auto &x : this->req_list)
285 if (req_num == x.req_num)
288 return x.sync_draw_req;
291 HMI_SEQ_ERROR(req_num, "Couldn't get action with the request : %d", req_num);
295 * Set actions to the request.
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.
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.
306 WMError AppList::setAction(unsigned req_num, const struct WMAction &action)
308 std::lock_guard<std::mutex> lock(mtx);
309 WMError result = WMError::FAIL;
310 for (auto &x : this->req_list)
312 if (req_num != x.req_num)
316 x.sync_draw_req.push_back(action);
317 result = WMError::SUCCESS;
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.
331 * Set actions to the request.
333 * This function is overload function.
334 * The feature is same as other one.
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.
347 WMError AppList::setAction(unsigned req_num, const string &appid, const string &role, const string &area, bool visible)
349 std::lock_guard<std::mutex> lock(mtx);
350 WMError result = WMError::FAIL;
351 for (auto &x : req_list)
353 if (req_num != x.req_num)
357 bool edraw_f = (visible) ? false : true;
358 WMAction action{appid, role, area, visible, edraw_f};
360 x.sync_draw_req.push_back(action);
361 result = WMError::SUCCESS;
368 * Set end_draw_finished param is true
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.
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
380 bool AppList::setEndDrawFinished(unsigned req_num, const string &appid, const string &role)
382 std::lock_guard<std::mutex> lock(mtx);
384 for (auto &x : req_list)
386 if (req_num < x.req_num)
390 if (req_num == x.req_num)
392 for (auto &y : x.sync_draw_req)
394 if (y.appid == appid && y.role == role)
396 y.end_draw_finished = true;
407 * Check all actions of the requested sequence is finished
409 * @param unsigned[in] request_number
410 * @return true if all action is set.
412 bool AppList::endDrawFullfilled(unsigned req_num)
415 for (const auto &x : req_list)
417 if (req_num < x.req_num)
421 if (req_num == x.req_num)
424 for (const auto &y : x.sync_draw_req)
426 result &= y.end_draw_finished;
438 * Finish the request, then remove it.
440 * @param unsigned[in] request_number
442 * @note Please call next after this function to receive or process next request.
444 void AppList::removeRequest(unsigned req_num)
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;
454 * Move the current request to next
461 std::lock_guard<std::mutex> lock(mtx);
463 if (0 == this->current_req)
465 this->current_req = 1;
470 * Check the request exists is in request list
473 * @return true if WMRequest exists in the request list
475 bool AppList::haveRequest() const
477 return !this->req_list.empty();
480 void AppList::clientDump()
482 DUMP("======= client dump =====");
483 for (const auto &x : this->app2client)
485 const auto &y = x.second;
488 DUMP("======= client dump end=====");
491 void AppList::reqDump()
493 DUMP("======= req dump =====");
494 DUMP("current request : %d", current_req);
495 for (const auto &x : req_list)
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(),
504 for (const auto &y : x.sync_draw_req)
507 "Action : (APPID :%s, ROLE :%s, AREA :%s, END_DRAW_FINISHED: %d)",
511 y.end_draw_finished);
514 DUMP("======= req dump end =====\n");