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"
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 this->app2client.reserve(kReserveClientSize);
45 this->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 string &appid, unsigned layer, unsigned surface, const string &role)
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;
76 void AppList::addClient(const string &appid, unsigned layer, const string &role)
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;
85 * Add Client to the list
87 * This function is overload function.
88 * But this function just register application.
89 * So an application does not have role, surface, layer.
90 * Client need to register role and layer afterward.
92 * @param string[in] Application id. This will be the key to withdraw the information.
94 * @attention This function should be called once for the app
95 * Caller should take care not to be called more than once.
97 void AppList::addClient(const string &appid)
99 std::lock_guard<std::mutex> lock(this->mtx);
100 shared_ptr<WMClient> client = std::make_shared<WMClient>(appid, 0, "");
101 this->app2client[appid] = client;
106 * Remove WMClient from the list
108 * @param string[in] Application id. This will be the key to withdraw the information.
110 void AppList::removeClient(const string &appid)
112 std::lock_guard<std::mutex> lock(this->mtx);
113 this->app2client.erase(appid);
114 HMI_INFO("Remove client %s", appid.c_str());
118 * Check this class stores the appid.
120 * @param string[in] Application id. This will be the key to withdraw the information.
121 * @return true if the class has the requested key(appid)
123 bool AppList::contains(const string &appid) const
125 auto result = this->app2client.find(appid);
126 return (this->app2client.end() != result) ? true : false;
130 * Remove surface from client
132 * @param unsigned[in] surface id.
135 void AppList::removeSurface(unsigned surface){
136 // This function may be very slow
137 std::lock_guard<std::mutex> lock(this->mtx);
139 for (auto &x : this->app2client)
141 ret = x.second->removeSurfaceIfExist(surface);
143 HMI_DEBUG("remove surface %d from Client %s finish",
144 surface, x.second->appID().c_str());
152 * Get WMClient object.
154 * After get the WMClient object, caller can call the client method.
155 * Before call this function, caller must call "contains"
156 * to check the key is contained, otherwise, you have to take care of std::out_of_range.
158 * @param string[in] application id(key)
159 * @return WMClient object
160 * @attention Must call cantains to check appid is stored before this function.
162 shared_ptr<WMClient> AppList::lookUpClient(const string &appid)
164 if(this->app2client.count(appid) != 0)
166 return this->app2client.at(appid);
177 * Returns the number of client stored in the list.
180 * @return The number of client
182 int AppList::countClient() const
184 return this->app2client.size();
188 * Get AppID with surface and role.
190 * Returns AppID if found.
192 * @param unsigned[in] surfaceID
193 * @param bool[in,out] AppID is found or not
195 * @attention If AppID is not found, param found will be false.
197 string AppList::getAppID(unsigned surface, bool* found) const
200 for (const auto &x : this->app2client)
202 if(x.second->surfaceID() == surface){
204 return x.second->appID();
210 // =================== Request Date container API ===================
213 * Get current request number
215 * Request number is the numeric ID to designate the request.
216 * But returned request number from this function doesn't mean the request exists.
217 * This number is used as key to withdraw the WMRequest object.
220 * @return current request number.
221 * @note request number is more than 0.
223 unsigned AppList::currentRequestNumber() const
225 return this->current_req;
231 * Request number is the numeric ID to designate the request.
232 * But returned request number from this function doesn't mean the request exists.
233 * This number is used as key to withdraw the WMRequest object.
236 * @return request number.
237 * @attention If returned value is 0, no request exists.
239 unsigned AppList::getRequestNumber(const string &appid) const
241 for (const auto &x : this->req_list)
243 // Since app will not request twice and more, comparing appid is enough?
244 if ((x.trigger.appid == appid))
255 * Request number is the numeric ID to designate the request.
256 * But returned request number from this function doesn't mean the request exists.
257 * This number is used as key to withdraw the WMRequest object.
259 * @param WMRequest[in] WMRequest object caller creates
260 * @return Request number
261 * @attention If the request number is different with curent request number,
262 * it means the previous request is not finished.
264 unsigned AppList::addRequest(WMRequest req)
266 std::lock_guard<std::mutex> lock(this->mtx);
267 if (this->req_list.size() == 0)
269 req.req_num = current_req;
273 HMI_SEQ_INFO(this->current_req, "add: %d", this->req_list.back().req_num + 1);
274 req.req_num = this->req_list.back().req_num + 1;
276 this->req_list.push_back(req);
281 * Get trigger which the application requests
283 * WMTrigger contains which application requests what role and where to put(area) and task.
284 * This is used for input event to Window Policy Manager(state machine).
286 * @param unsigned[in] request number
287 * @param bool[in,out] Check request number of the parameter is valid.
288 * @return WMTrigger which associates with the request number
289 * @attention If the request number is not valid, parameter "found" is false
290 * and return value will be meaningless value.
291 * Caller can check the request parameter is valid.
293 struct WMTrigger AppList::getRequest(unsigned req_num, bool *found)
296 for (const auto &x : this->req_list)
298 if (req_num == x.req_num)
304 HMI_SEQ_ERROR(req_num, "Couldn't get request : %d", req_num);
305 return WMTrigger{"", "", "", Task::TASK_INVALID};
309 * Get actions which the application requests
311 * WMAciton contains the information of state transition.
312 * In other words, it contains actions of Window Manager,
313 * which role should be put to the area.
315 * @param unsigned[in] request number
316 * @param bool[in,out] Check request number of the parameter is valid.
317 * @return WMTrigger which associates with the request number
318 * @attention If the request number is not valid, parameter "found" is false
319 * and return value will be no reference pointer.
320 * Caller must check the request parameter is valid.
322 const vector<struct WMAction> &AppList::getActions(unsigned req_num, bool* found)
325 for (auto &x : this->req_list)
327 if (req_num == x.req_num)
330 return x.sync_draw_req;
333 HMI_SEQ_ERROR(req_num, "Couldn't get action with the request : %d", req_num);
335 static vector<struct WMAction> empty;
341 * Set actions to the request.
343 * Add actions to the request.
344 * This function can be called many times, and actions increase.
345 * This function is used for decision of actions of Window Manager
346 * according to the result of Window Policy Manager.
348 * @param unsigned[in] request number
349 * @param WMAction[in] Action of Window Manager.
350 * @return WMError If request number is not valid, FAIL will be returned.
352 WMError AppList::setAction(unsigned req_num, const struct WMAction &action)
354 std::lock_guard<std::mutex> lock(this->mtx);
355 WMError result = WMError::FAIL;
356 for (auto &x : this->req_list)
358 if (req_num != x.req_num)
362 x.sync_draw_req.push_back(action);
363 result = WMError::SUCCESS;
371 * @note This function set action with parameters.
372 * If visible is true, it means app should be visible, so enddraw_finished parameter should be false.
373 * otherwise (visible is false) app should be invisible. Then enddraw_finished param is set to true.
374 * This function doesn't support actions for focus yet.
377 * Set actions to the request.
379 * This function is overload function.
380 * The feature is same as other one.
382 * @param unsigned[in] request number
383 * @param string[in] application id
384 * @param string[in] role
385 * @param string[in] area
386 * @param Task[in] the role should be visible or not.
387 * @return WMError If request number is not valid, FAIL will be returned.
388 * @attention This function set action with parameters, then caller doesn't need to create WMAction object.
389 * If visible is true, it means app should be visible, so enddraw_finished parameter will be false.
390 * otherwise (visible is false) app should be invisible. Then enddraw_finished param is set to true.
391 * This function doesn't support actions for focus yet.
393 WMError AppList::setAction(unsigned req_num, shared_ptr<WMClient> client, const string &role, const string &area, TaskVisible visible)
395 std::lock_guard<std::mutex> lock(this->mtx);
396 WMError result = WMError::FAIL;
397 for (auto &x : req_list)
399 if (req_num != x.req_num)
403 // If visible task is not invisible, redraw is required -> true
404 bool edraw_f = (visible != TaskVisible::INVISIBLE) ? false : true;
405 WMAction action{req_num, client, role, area, visible, edraw_f};
407 x.sync_draw_req.push_back(action);
408 result = WMError::SUCCESS;
415 * Set end_draw_finished param is true
417 * This function checks
418 * - req_num is equal to current request number
419 * - appid and role are equeal to the appid and role stored in action list
420 * If it is valid, set the action is finished.
422 * @param unsigned[in] request number
423 * @param string[in] application id
424 * @param string[in] role
425 * @return If the parameters are not valid in action list, returns false
427 bool AppList::setEndDrawFinished(unsigned req_num, const string &appid, const string &role)
429 std::lock_guard<std::mutex> lock(this->mtx);
431 for (auto &x : req_list)
433 if (req_num < x.req_num)
437 if (req_num == x.req_num)
439 for (auto &y : x.sync_draw_req)
441 if (y.client->appID() == appid && y.role == role)
443 HMI_SEQ_INFO(req_num, "Role %s finish redraw", y.role.c_str());
444 y.end_draw_finished = true;
455 * Check all actions of the requested sequence is finished
457 * @param unsigned[in] request_number
458 * @return true if all action is set.
460 bool AppList::endDrawFullfilled(unsigned req_num)
463 for (const auto &x : req_list)
465 if (req_num < x.req_num)
469 if (req_num == x.req_num)
472 for (const auto &y : x.sync_draw_req)
474 result &= y.end_draw_finished;
486 * Finish the request, then remove it.
488 * @param unsigned[in] request_number
490 * @note Please call next after this function to receive or process next request.
492 void AppList::removeRequest(unsigned req_num)
494 std::lock_guard<std::mutex> lock(this->mtx);
495 this->req_list.erase(remove_if(this->req_list.begin(), this->req_list.end(),
496 [req_num](WMRequest x) {
497 return x.req_num == req_num;
502 * Move the current request to next
509 std::lock_guard<std::mutex> lock(this->mtx);
511 if (0 == this->current_req)
513 this->current_req = 1;
518 * Check the request exists is in request list
521 * @return true if WMRequest exists in the request list
523 bool AppList::haveRequest() const
525 return !this->req_list.empty();
528 void AppList::clientDump()
530 DUMP("======= client dump =====");
531 for (const auto &x : this->app2client)
533 const auto &y = x.second;
536 DUMP("======= client dump end=====");
539 void AppList::reqDump()
541 DUMP("======= req dump =====");
542 DUMP("current request : %d", current_req);
543 for (const auto &x : req_list)
545 DUMP("requested : %d", x.req_num);
546 DUMP("Trigger : (APPID :%s, ROLE :%s, AREA :%s, TASK: %d)",
547 x.trigger.appid.c_str(),
548 x.trigger.role.c_str(),
549 x.trigger.area.c_str(),
552 for (const auto &y : x.sync_draw_req)
555 "Action : (APPID :%s, ROLE :%s, AREA :%s, VISIBLE : %s, END_DRAW_FINISHED: %d)",
556 y.client->appID().c_str(),
559 (y.visible == TaskVisible::INVISIBLE) ? "invisible" : "visible",
560 y.end_draw_finished);
563 DUMP("======= req dump end =====");