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 string[in] role
173 * @param bool[in,out] AppID is found or not
175 * @attention If AppID is not found, param found will be false.
177 /* string AppList::getAppID(unsigned surface, const string& role, bool* found) const
180 for (const auto &x : this->app2client)
182 if(x.second->surfaceID(role) == surface){
184 return x.second->appID();
190 string AppList::getAppID(unsigned surface, bool* found) const
193 for (const auto &x : this->app2client)
195 if(x.second->surfaceID() == surface){
197 return x.second->appID();
203 WMError AppList::popFloatingSurface(unsigned pid, unsigned *surface)
205 WMError ret = WMError::NO_ENTRY;
207 auto fwd_itr = std::remove_if(this->floating_surfaces.begin(), this->floating_surfaces.end(),
208 [pid, surface, &ret](FloatingSurface x) {
210 *surface = x.surface_id;
211 ret = WMError::SUCCESS;
218 if (fwd_itr != this->floating_surfaces.cend())
220 HMI_INFO("pop floating surface: %d", *surface);
222 this->floating_surfaces.erase(fwd_itr, this->floating_surfaces.end());
226 // =================== Floating(Temporary) surface/client API ===================
228 WMError AppList::popFloatingSurface(const string &appid, unsigned *surface)
230 HMI_ERROR("This function is not implemented");
231 return WMError::SUCCESS;
234 void AppList::addFloatingClient(const string &appid, unsigned layer, const string &role)
238 void AppList::addFloatingSurface(const string &appid, unsigned surface, unsigned pid)
240 struct FloatingSurface fsurface{appid, surface, pid};
241 this->floating_surfaces.push_back(fsurface);
242 this->dumpFloatingSurfaces();
245 void AppList::removeFloatingSurface(unsigned surface)
247 this->dumpFloatingSurfaces();
248 auto fwd_itr = std::remove_if(
249 this->floating_surfaces.begin(), this->floating_surfaces.end(),
250 [surface](FloatingSurface x) {
251 return x.surface_id == surface;
253 if(fwd_itr != this->floating_surfaces.cend()){
254 HMI_INFO("remove floating surface: %d", surface);
256 this->floating_surfaces.erase(fwd_itr, this->floating_surfaces.end());
259 WMError AppList::appendRole(const string &id, const string &role)
261 WMError wm_err = WMError::NO_ENTRY;
262 if (this->contains(id))
264 auto x = this->lookUpClient(id);
266 wm_err = WMError::SUCCESS;
272 // =================== Request Date container API ===================
275 * Get current request number
277 * Request number is the numeric ID to designate the request.
278 * But returned request number from this function doesn't mean the request exists.
279 * This number is used as key to withdraw the WMRequest object.
282 * @return current request number.
283 * @note request number is more than 0.
285 unsigned AppList::currentRequestNumber() const
287 return this->current_req;
293 * Request number is the numeric ID to designate the request.
294 * But returned request number from this function doesn't mean the request exists.
295 * This number is used as key to withdraw the WMRequest object.
298 * @return request number.
299 * @attention If returned value is 0, no request exists.
301 unsigned AppList::getRequestNumber(const string &appid) const
303 for (const auto &x : this->req_list)
305 // Since app will not request twice and more, comparing appid is enough?
306 if ((x.trigger.appid == appid))
317 * Request number is the numeric ID to designate the request.
318 * But returned request number from this function doesn't mean the request exists.
319 * This number is used as key to withdraw the WMRequest object.
321 * @param WMRequest[in] WMRequest object caller creates
322 * @return Request number
323 * @attention If the request number is different with curent request number,
324 * it means the previous request is not finished.
326 unsigned AppList::addRequest(WMRequest req)
328 std::lock_guard<std::mutex> lock(this->mtx);
329 if (this->req_list.size() == 0)
331 req.req_num = current_req;
335 HMI_SEQ_INFO(this->current_req, "add: %d", this->req_list.back().req_num + 1);
336 req.req_num = this->req_list.back().req_num + 1;
338 this->req_list.push_back(req);
343 * Get trigger which the application requests
345 * WMTrigger contains which application requests what role and where to put(area) and task.
346 * This is used for input event to Window Policy Manager(state machine).
348 * @param unsigned[in] request number
349 * @param bool[in,out] Check request number of the parameter is valid.
350 * @return WMTrigger which associates with the request number
351 * @attention If the request number is not valid, parameter "found" is false
352 * and return value will be meaningless value.
353 * Caller can check the request parameter is valid.
355 struct WMTrigger AppList::getRequest(unsigned req_num, bool *found)
358 for (const auto &x : this->req_list)
360 if (req_num == x.req_num)
366 HMI_SEQ_ERROR(req_num, "Couldn't get request : %d", req_num);
367 return WMTrigger{"", "", "", Task::TASK_INVALID};
371 * Get actions which the application requests
373 * WMAciton contains the information of state transition.
374 * In other words, it contains actions of Window Manager,
375 * which role should be put to the area.
377 * @param unsigned[in] request number
378 * @param bool[in,out] Check request number of the parameter is valid.
379 * @return WMTrigger which associates with the request number
380 * @attention If the request number is not valid, parameter "found" is false
381 * and return value will be no reference pointer.
382 * Caller must check the request parameter is valid.
384 const vector<struct WMAction> &AppList::getActions(unsigned req_num, bool* found)
387 for (auto &x : this->req_list)
389 if (req_num == x.req_num)
392 return x.sync_draw_req;
395 HMI_SEQ_ERROR(req_num, "Couldn't get action with the request : %d", req_num);
399 * Set actions to the request.
401 * Add actions to the request.
402 * This function can be called many times, and actions increase.
403 * This function is used for decision of actions of Window Manager
404 * according to the result of Window Policy Manager.
406 * @param unsigned[in] request number
407 * @param WMAction[in] Action of Window Manager.
408 * @return WMError If request number is not valid, FAIL will be returned.
410 WMError AppList::setAction(unsigned req_num, const struct WMAction &action)
412 std::lock_guard<std::mutex> lock(this->mtx);
413 WMError result = WMError::FAIL;
414 for (auto &x : this->req_list)
416 if (req_num != x.req_num)
420 x.sync_draw_req.push_back(action);
421 result = WMError::SUCCESS;
429 * @note This function set action with parameters.
430 * If visible is true, it means app should be visible, so enddraw_finished parameter should be false.
431 * otherwise (visible is false) app should be invisible. Then enddraw_finished param is set to true.
432 * This function doesn't support actions for focus yet.
435 * Set actions to the request.
437 * This function is overload function.
438 * The feature is same as other one.
440 * @param unsigned[in] request number
441 * @param string[in] application id
442 * @param string[in] role
443 * @param string[in] area
444 * @param Task[in] the role should be visible or not.
445 * @return WMError If request number is not valid, FAIL will be returned.
446 * @attention This function set action with parameters, then caller doesn't need to create WMAction object.
447 * If visible is true, it means app should be visible, so enddraw_finished parameter will be false.
448 * otherwise (visible is false) app should be invisible. Then enddraw_finished param is set to true.
449 * This function doesn't support actions for focus yet.
451 WMError AppList::setAction(unsigned req_num, shared_ptr<WMClient> client, const string &role, const string &area, TaskVisible visible)
453 std::lock_guard<std::mutex> lock(this->mtx);
454 WMError result = WMError::FAIL;
455 for (auto &x : req_list)
457 if (req_num != x.req_num)
461 // If visible task is not invisible, redraw is required -> true
462 bool edraw_f = (visible != TaskVisible::INVISIBLE) ? false : true;
463 WMAction action{req_num, client, role, area, visible, edraw_f};
465 x.sync_draw_req.push_back(action);
466 result = WMError::SUCCESS;
473 * Set end_draw_finished param is true
475 * This function checks
476 * - req_num is equal to current request number
477 * - appid and role are equeal to the appid and role stored in action list
478 * If it is valid, set the action is finished.
480 * @param unsigned[in] request number
481 * @param string[in] application id
482 * @param string[in] role
483 * @return If the parameters are not valid in action list, returns false
485 bool AppList::setEndDrawFinished(unsigned req_num, const string &appid, const string &role)
487 std::lock_guard<std::mutex> lock(this->mtx);
489 for (auto &x : req_list)
491 if (req_num < x.req_num)
495 if (req_num == x.req_num)
497 for (auto &y : x.sync_draw_req)
499 if (y.client->appID() == appid && y.role == role)
501 HMI_SEQ_INFO(req_num, "Role %s finish redraw", y.role.c_str());
502 y.end_draw_finished = true;
513 * Check all actions of the requested sequence is finished
515 * @param unsigned[in] request_number
516 * @return true if all action is set.
518 bool AppList::endDrawFullfilled(unsigned req_num)
521 for (const auto &x : req_list)
523 if (req_num < x.req_num)
527 if (req_num == x.req_num)
530 for (const auto &y : x.sync_draw_req)
532 result &= y.end_draw_finished;
544 * Finish the request, then remove it.
546 * @param unsigned[in] request_number
548 * @note Please call next after this function to receive or process next request.
550 void AppList::removeRequest(unsigned req_num)
552 std::lock_guard<std::mutex> lock(this->mtx);
553 this->req_list.erase(remove_if(this->req_list.begin(), this->req_list.end(),
554 [req_num](WMRequest x) {
555 return x.req_num == req_num;
560 * Move the current request to next
567 std::lock_guard<std::mutex> lock(this->mtx);
569 if (0 == this->current_req)
571 this->current_req = 1;
576 * Check the request exists is in request list
579 * @return true if WMRequest exists in the request list
581 bool AppList::haveRequest() const
583 return !this->req_list.empty();
586 void AppList::clientDump()
588 DUMP("======= client dump =====");
589 for (const auto &x : this->app2client)
591 const auto &y = x.second;
594 DUMP("======= client dump end=====");
597 void AppList::reqDump()
599 DUMP("======= req dump =====");
600 DUMP("current request : %d", current_req);
601 for (const auto &x : req_list)
603 DUMP("requested : %d", x.req_num);
604 DUMP("Trigger : (APPID :%s, ROLE :%s, AREA :%s, TASK: %d)",
605 x.trigger.appid.c_str(),
606 x.trigger.role.c_str(),
607 x.trigger.area.c_str(),
610 for (const auto &y : x.sync_draw_req)
613 "Action : (APPID :%s, ROLE :%s, AREA :%s, VISIBLE : %s, END_DRAW_FINISHED: %d)",
614 y.client->appID().c_str(),
617 (y.visible == TaskVisible::INVISIBLE) ? "invisible" : "visible",
618 y.end_draw_finished);
621 DUMP("======= req dump end =====");
624 void AppList::dumpFloatingSurfaces()
626 DUMP("======= floating surface dump =====");
627 for (const auto &x : this->floating_surfaces)
629 DUMP("surface : %d, pid : %d", x.surface_id, x.pid);
631 DUMP("======= floating surface dump end =====\n");