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 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 std::string &appid, unsigned layer, unsigned surface, const std::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;
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(this->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;
101 * Remove surface from client
103 * @param unsigned[in] surface id.
106 void AppList::removeSurface(unsigned surface){
107 // This function may be very slow
108 std::lock_guard<std::mutex> lock(this->mtx);
110 for (auto &x : this->app2client)
112 ret = x.second->removeSurfaceIfExist(surface);
114 HMI_DEBUG("wm", "remove surface %d from Client %s finish", surface, x.second->appID().c_str());
122 * Get WMClient object.
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.
128 * @param string[in] application id(key)
129 * @return WMClient object
130 * @attention Must call cantains to check appid is stored before this function.
132 shared_ptr<WMClient> AppList::lookUpClient(const string &appid)
134 return this->app2client.at(appid);
140 * Returns the number of client stored in the list.
143 * @return The number of client
145 int AppList::countClient() const
147 return this->app2client.size();
151 * Get AppID with surface and role.
153 * Returns AppID if found.
155 * @param unsigned[in] surfaceID
156 * @param string[in] role
157 * @param bool[in,out] (key)
158 * @return WMClient object
159 * @attention If AppID is not found, param found will be false.
161 string AppList::getAppID(unsigned surface, const string& role, bool* found) const
164 for (const auto &x : this->app2client)
166 if(x.second->surfaceID(role) == surface){
168 return x.second->appID();
174 // =================== Request Date container API ===================
177 * Get current request number
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.
184 * @return current request number.
185 * @note request number is more than 0.
187 unsigned AppList::currentRequestNumber() const
189 return this->current_req;
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.
200 * @return request number.
201 * @attention If returned value is 0, no request exists.
203 unsigned AppList::getRequestNumber(const string &appid) const
205 for (const auto &x : this->req_list)
207 // Since app will not request twice and more, comparing appid is enough?
208 if ((x.trigger.appid == appid))
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.
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.
228 unsigned AppList::addAllocateRequest(WMRequest req)
230 std::lock_guard<std::mutex> lock(this->mtx);
231 if (this->req_list.size() == 0)
233 req.req_num = current_req;
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;
240 this->req_list.push_back(req);
245 * Get trigger which the application requests
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).
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.
257 struct WMTrigger AppList::getRequest(unsigned req_num, bool *found)
260 for (const auto &x : this->req_list)
262 if (req_num == x.req_num)
268 HMI_SEQ_ERROR(req_num, "Couldn't get request : %d", req_num);
269 return WMTrigger{"", "", "", Task::TASK_INVALID};
273 * Get actions which the application requests
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.
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.
286 const vector<struct WMAction> &AppList::getActions(unsigned req_num, bool* found)
289 for (auto &x : this->req_list)
291 if (req_num == x.req_num)
294 return x.sync_draw_req;
297 HMI_SEQ_ERROR(req_num, "Couldn't get action with the request : %d", req_num);
301 * Set actions to the request.
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.
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.
312 WMError AppList::setAction(unsigned req_num, const struct WMAction &action)
314 std::lock_guard<std::mutex> lock(this->mtx);
315 WMError result = WMError::FAIL;
316 for (auto &x : this->req_list)
318 if (req_num != x.req_num)
322 x.sync_draw_req.push_back(action);
323 result = WMError::SUCCESS;
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.
337 * Set actions to the request.
339 * This function is overload function.
340 * The feature is same as other one.
342 * @param unsigned[in] request number
343 * @param string[in] application id
344 * @param string[in] role
345 * @param string[in] area
346 * @param bool[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.
353 WMError AppList::setAction(unsigned req_num, const string &appid, const string &role, const string &area, bool visible)
355 std::lock_guard<std::mutex> lock(this->mtx);
356 WMError result = WMError::FAIL;
357 for (auto &x : req_list)
359 if (req_num != x.req_num)
363 bool edraw_f = (visible) ? false : true;
364 WMAction action{appid, role, area, visible, edraw_f};
366 x.sync_draw_req.push_back(action);
367 result = WMError::SUCCESS;
374 * Set end_draw_finished param is true
376 * This function checks
377 * - req_num is equal to current request number
378 * - appid and role are equeal to the appid and role stored in action list
379 * If it is valid, set the action is finished.
381 * @param unsigned[in] request number
382 * @param string[in] application id
383 * @param string[in] role
384 * @return If the parameters are not valid in action list, returns false
386 bool AppList::setEndDrawFinished(unsigned req_num, const string &appid, const string &role)
388 std::lock_guard<std::mutex> lock(this->mtx);
390 for (auto &x : req_list)
392 if (req_num < x.req_num)
396 if (req_num == x.req_num)
398 for (auto &y : x.sync_draw_req)
400 if (y.appid == appid && y.role == role)
402 y.end_draw_finished = true;
413 * Check all actions of the requested sequence is finished
415 * @param unsigned[in] request_number
416 * @return true if all action is set.
418 bool AppList::endDrawFullfilled(unsigned req_num)
421 for (const auto &x : req_list)
423 if (req_num < x.req_num)
427 if (req_num == x.req_num)
430 for (const auto &y : x.sync_draw_req)
432 result &= y.end_draw_finished;
444 * Finish the request, then remove it.
446 * @param unsigned[in] request_number
448 * @note Please call next after this function to receive or process next request.
450 void AppList::removeRequest(unsigned req_num)
452 std::lock_guard<std::mutex> lock(this->mtx);
453 this->req_list.erase(remove_if(this->req_list.begin(), this->req_list.end(),
454 [req_num](WMRequest x) {
455 return x.req_num == req_num;
460 * Move the current request to next
467 std::lock_guard<std::mutex> lock(this->mtx);
469 if (0 == this->current_req)
471 this->current_req = 1;
476 * Check the request exists is in request list
479 * @return true if WMRequest exists in the request list
481 bool AppList::haveRequest() const
483 return !this->req_list.empty();
486 void AppList::clientDump()
488 DUMP("======= client dump =====");
489 for (const auto &x : this->app2client)
491 const auto &y = x.second;
494 DUMP("======= client dump end=====");
497 void AppList::reqDump()
499 DUMP("======= req dump =====");
500 DUMP("current request : %d", current_req);
501 for (const auto &x : req_list)
503 DUMP("requested with : %d", x.req_num);
504 DUMP("Trigger : (APPID :%s, ROLE :%s, AREA :%s, TASK: %d)",
505 x.trigger.appid.c_str(),
506 x.trigger.role.c_str(),
507 x.trigger.area.c_str(),
510 for (const auto &y : x.sync_draw_req)
513 "Action : (APPID :%s, ROLE :%s, AREA :%s, END_DRAW_FINISHED: %d)",
517 y.end_draw_finished);
520 DUMP("======= req dump end =====\n");