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 * Remove WMClient from the list
87 * @param string[in] Application id. This will be the key to withdraw the information.
89 void AppList::removeClient(const string &appid)
91 std::lock_guard<std::mutex> lock(this->mtx);
92 this->app2client.erase(appid);
93 HMI_INFO("Remove client %s", appid.c_str());
97 * Check this class stores the appid.
99 * @param string[in] Application id. This will be the key to withdraw the information.
100 * @return true if the class has the requested key(appid)
102 bool AppList::contains(const string &appid) const
104 auto result = this->app2client.find(appid);
105 return (this->app2client.end() != result) ? true : false;
109 * Remove surface from client
111 * @param unsigned[in] surface id.
114 void AppList::removeSurface(unsigned surface){
115 // This function may be very slow
116 std::lock_guard<std::mutex> lock(this->mtx);
118 for (auto &x : this->app2client)
120 ret = x.second->removeSurfaceIfExist(surface);
122 HMI_DEBUG("remove surface %d from Client %s finish",
123 surface, x.second->appID().c_str());
131 * Get WMClient object.
133 * After get the WMClient object, caller can call the client method.
134 * Before call this function, caller must call "contains"
135 * to check the key is contained, otherwise, you have to take care of std::out_of_range.
137 * @param string[in] application id(key)
138 * @return WMClient object
139 * @attention Must call cantains to check appid is stored before this function.
141 shared_ptr<WMClient> AppList::lookUpClient(const string &appid)
143 if(this->app2client.count(appid) != 0)
145 return this->app2client.at(appid);
156 * Returns the number of client stored in the list.
159 * @return The number of client
161 int AppList::countClient() const
163 return this->app2client.size();
167 * Get AppID with surface and role.
169 * Returns AppID if found.
171 * @param unsigned[in] surfaceID
172 * @param bool[in,out] AppID is found or not
174 * @attention If AppID is not found, param found will be false.
176 string AppList::getAppID(unsigned surface, bool* found) const
179 for (const auto &x : this->app2client)
181 if(x.second->surfaceID() == surface){
183 return x.second->appID();
189 // =================== Request Date container API ===================
192 * Get current request number
194 * Request number is the numeric ID to designate the request.
195 * But returned request number from this function doesn't mean the request exists.
196 * This number is used as key to withdraw the WMRequest object.
199 * @return current request number.
200 * @note request number is more than 0.
202 unsigned AppList::currentRequestNumber() const
204 return this->current_req;
210 * Request number is the numeric ID to designate the request.
211 * But returned request number from this function doesn't mean the request exists.
212 * This number is used as key to withdraw the WMRequest object.
215 * @return request number.
216 * @attention If returned value is 0, no request exists.
218 unsigned AppList::getRequestNumber(const string &appid) const
220 for (const auto &x : this->req_list)
222 // Since app will not request twice and more, comparing appid is enough?
223 if ((x.trigger.appid == appid))
234 * Request number is the numeric ID to designate the request.
235 * But returned request number from this function doesn't mean the request exists.
236 * This number is used as key to withdraw the WMRequest object.
238 * @param WMRequest[in] WMRequest object caller creates
239 * @return Request number
240 * @attention If the request number is different with curent request number,
241 * it means the previous request is not finished.
243 unsigned AppList::addRequest(WMRequest req)
245 std::lock_guard<std::mutex> lock(this->mtx);
246 if (this->req_list.size() == 0)
248 req.req_num = current_req;
252 HMI_SEQ_INFO(this->current_req, "add: %d", this->req_list.back().req_num + 1);
253 req.req_num = this->req_list.back().req_num + 1;
255 this->req_list.push_back(req);
260 * Get trigger which the application requests
262 * WMTrigger contains which application requests what role and where to put(area) and task.
263 * This is used for input event to Window Policy Manager(state machine).
265 * @param unsigned[in] request number
266 * @param bool[in,out] Check request number of the parameter is valid.
267 * @return WMTrigger which associates with the request number
268 * @attention If the request number is not valid, parameter "found" is false
269 * and return value will be meaningless value.
270 * Caller can check the request parameter is valid.
272 struct WMTrigger AppList::getRequest(unsigned req_num, bool *found)
275 for (const auto &x : this->req_list)
277 if (req_num == x.req_num)
283 HMI_SEQ_ERROR(req_num, "Couldn't get request : %d", req_num);
284 return WMTrigger{"", "", "", Task::TASK_INVALID};
288 * Get actions which the application requests
290 * WMAciton contains the information of state transition.
291 * In other words, it contains actions of Window Manager,
292 * which role should be put to the area.
294 * @param unsigned[in] request number
295 * @param bool[in,out] Check request number of the parameter is valid.
296 * @return WMTrigger which associates with the request number
297 * @attention If the request number is not valid, parameter "found" is false
298 * and return value will be no reference pointer.
299 * Caller must check the request parameter is valid.
301 const vector<struct WMAction> &AppList::getActions(unsigned req_num, bool* found)
304 for (auto &x : this->req_list)
306 if (req_num == x.req_num)
309 return x.sync_draw_req;
312 HMI_SEQ_ERROR(req_num, "Couldn't get action with the request : %d", req_num);
314 static vector<struct WMAction> empty;
320 * Set actions to the request.
322 * Add actions to the request.
323 * This function can be called many times, and actions increase.
324 * This function is used for decision of actions of Window Manager
325 * according to the result of Window Policy Manager.
327 * @param unsigned[in] request number
328 * @param WMAction[in] Action of Window Manager.
329 * @return WMError If request number is not valid, FAIL will be returned.
331 WMError AppList::setAction(unsigned req_num, const struct WMAction &action)
333 std::lock_guard<std::mutex> lock(this->mtx);
334 WMError result = WMError::FAIL;
335 for (auto &x : this->req_list)
337 if (req_num != x.req_num)
341 x.sync_draw_req.push_back(action);
342 result = WMError::SUCCESS;
350 * @note This function set action with parameters.
351 * If visible is true, it means app should be visible, so enddraw_finished parameter should be false.
352 * otherwise (visible is false) app should be invisible. Then enddraw_finished param is set to true.
353 * This function doesn't support actions for focus yet.
356 * Set actions to the request.
358 * This function is overload function.
359 * The feature is same as other one.
361 * @param unsigned[in] request number
362 * @param string[in] application id
363 * @param string[in] role
364 * @param string[in] area
365 * @param Task[in] the role should be visible or not.
366 * @return WMError If request number is not valid, FAIL will be returned.
367 * @attention This function set action with parameters, then caller doesn't need to create WMAction object.
368 * If visible is true, it means app should be visible, so enddraw_finished parameter will be false.
369 * otherwise (visible is false) app should be invisible. Then enddraw_finished param is set to true.
370 * This function doesn't support actions for focus yet.
372 WMError AppList::setAction(unsigned req_num, shared_ptr<WMClient> client, const string &role, const string &area, TaskVisible visible)
374 std::lock_guard<std::mutex> lock(this->mtx);
375 WMError result = WMError::FAIL;
376 for (auto &x : req_list)
378 if (req_num != x.req_num)
382 // If visible task is not invisible, redraw is required -> true
383 bool edraw_f = (visible != TaskVisible::INVISIBLE) ? false : true;
384 WMAction action{req_num, client, role, area, visible, edraw_f};
386 x.sync_draw_req.push_back(action);
387 result = WMError::SUCCESS;
394 * Set end_draw_finished param is true
396 * This function checks
397 * - req_num is equal to current request number
398 * - appid and role are equeal to the appid and role stored in action list
399 * If it is valid, set the action is finished.
401 * @param unsigned[in] request number
402 * @param string[in] application id
403 * @param string[in] role
404 * @return If the parameters are not valid in action list, returns false
406 bool AppList::setEndDrawFinished(unsigned req_num, const string &appid, const string &role)
408 std::lock_guard<std::mutex> lock(this->mtx);
410 for (auto &x : req_list)
412 if (req_num < x.req_num)
416 if (req_num == x.req_num)
418 for (auto &y : x.sync_draw_req)
420 if (y.client->appID() == appid && y.role == role)
422 HMI_SEQ_INFO(req_num, "Role %s finish redraw", y.role.c_str());
423 y.end_draw_finished = true;
434 * Check all actions of the requested sequence is finished
436 * @param unsigned[in] request_number
437 * @return true if all action is set.
439 bool AppList::endDrawFullfilled(unsigned req_num)
442 for (const auto &x : req_list)
444 if (req_num < x.req_num)
448 if (req_num == x.req_num)
451 for (const auto &y : x.sync_draw_req)
453 result &= y.end_draw_finished;
465 * Finish the request, then remove it.
467 * @param unsigned[in] request_number
469 * @note Please call next after this function to receive or process next request.
471 void AppList::removeRequest(unsigned req_num)
473 std::lock_guard<std::mutex> lock(this->mtx);
474 this->req_list.erase(remove_if(this->req_list.begin(), this->req_list.end(),
475 [req_num](WMRequest x) {
476 return x.req_num == req_num;
481 * Move the current request to next
488 std::lock_guard<std::mutex> lock(this->mtx);
490 if (0 == this->current_req)
492 this->current_req = 1;
497 * Check the request exists is in request list
500 * @return true if WMRequest exists in the request list
502 bool AppList::haveRequest() const
504 return !this->req_list.empty();
507 void AppList::clientDump()
509 DUMP("======= client dump =====");
510 for (const auto &x : this->app2client)
512 const auto &y = x.second;
515 DUMP("======= client dump end=====");
518 void AppList::reqDump()
520 DUMP("======= req dump =====");
521 DUMP("current request : %d", current_req);
522 for (const auto &x : req_list)
524 DUMP("requested : %d", x.req_num);
525 DUMP("Trigger : (APPID :%s, ROLE :%s, AREA :%s, TASK: %d)",
526 x.trigger.appid.c_str(),
527 x.trigger.role.c_str(),
528 x.trigger.area.c_str(),
531 for (const auto &y : x.sync_draw_req)
534 "Action : (APPID :%s, ROLE :%s, AREA :%s, VISIBLE : %s, END_DRAW_FINISHED: %d)",
535 y.client->appID().c_str(),
538 (y.visible == TaskVisible::INVISIBLE) ? "invisible" : "visible",
539 y.end_draw_finished);
542 DUMP("======= req dump end =====");