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 return this->app2client.at(appid);
149 * Returns the number of client stored in the list.
152 * @return The number of client
154 int AppList::countClient() const
156 return this->app2client.size();
160 * Get AppID with surface and role.
162 * Returns AppID if found.
164 * @param unsigned[in] surfaceID
165 * @param string[in] role
166 * @param bool[in,out] AppID is found or not
168 * @attention If AppID is not found, param found will be false.
170 /* string AppList::getAppID(unsigned surface, const string& role, bool* found) const
173 for (const auto &x : this->app2client)
175 if(x.second->surfaceID(role) == surface){
177 return x.second->appID();
183 string AppList::getAppID(unsigned surface, bool* found) const
186 for (const auto &x : this->app2client)
188 if(x.second->surfaceID() == surface){
190 return x.second->appID();
196 WMError AppList::popFloatingSurface(unsigned pid, unsigned *surface)
198 WMError ret = WMError::NO_ENTRY;
200 auto fwd_itr = std::remove_if(this->floating_surfaces.begin(), this->floating_surfaces.end(),
201 [pid, surface, &ret](FloatingSurface x) {
203 *surface = x.surface_id;
204 ret = WMError::SUCCESS;
211 if (fwd_itr != this->floating_surfaces.cend())
213 HMI_INFO("pop floating surface: %d", *surface);
215 this->floating_surfaces.erase(fwd_itr, this->floating_surfaces.end());
219 // =================== Floating(Temporary) surface/client API ===================
221 WMError AppList::popFloatingSurface(const string &appid, unsigned *surface)
223 HMI_ERROR("This function is not implemented");
224 return WMError::SUCCESS;
227 void AppList::addFloatingClient(const string &appid, unsigned layer, const string &role)
231 void AppList::addFloatingSurface(const string &appid, unsigned surface, unsigned pid)
233 struct FloatingSurface fsurface{appid, surface, pid};
234 this->floating_surfaces.push_back(fsurface);
235 this->dumpFloatingSurfaces();
238 void AppList::removeFloatingSurface(unsigned surface)
240 this->dumpFloatingSurfaces();
241 auto fwd_itr = std::remove_if(
242 this->floating_surfaces.begin(), this->floating_surfaces.end(),
243 [surface](FloatingSurface x) {
244 return x.surface_id == surface;
246 if(fwd_itr != this->floating_surfaces.cend()){
247 HMI_INFO("remove floating surface: %d", surface);
249 this->floating_surfaces.erase(fwd_itr, this->floating_surfaces.end());
252 WMError AppList::appendRole(const string &id, const string &role)
254 WMError wm_err = WMError::NO_ENTRY;
255 if (this->contains(id))
257 auto x = this->lookUpClient(id);
259 wm_err = WMError::SUCCESS;
265 // =================== Request Date container API ===================
268 * Get current request number
270 * Request number is the numeric ID to designate the request.
271 * But returned request number from this function doesn't mean the request exists.
272 * This number is used as key to withdraw the WMRequest object.
275 * @return current request number.
276 * @note request number is more than 0.
278 unsigned AppList::currentRequestNumber() const
280 return this->current_req;
286 * Request number is the numeric ID to designate the request.
287 * But returned request number from this function doesn't mean the request exists.
288 * This number is used as key to withdraw the WMRequest object.
291 * @return request number.
292 * @attention If returned value is 0, no request exists.
294 unsigned AppList::getRequestNumber(const string &appid) const
296 for (const auto &x : this->req_list)
298 // Since app will not request twice and more, comparing appid is enough?
299 if ((x.trigger.appid == appid))
310 * Request number is the numeric ID to designate the request.
311 * But returned request number from this function doesn't mean the request exists.
312 * This number is used as key to withdraw the WMRequest object.
314 * @param WMRequest[in] WMRequest object caller creates
315 * @return Request number
316 * @attention If the request number is different with curent request number,
317 * it means the previous request is not finished.
319 unsigned AppList::addRequest(WMRequest req)
321 std::lock_guard<std::mutex> lock(this->mtx);
322 if (this->req_list.size() == 0)
324 req.req_num = current_req;
328 HMI_SEQ_INFO(this->current_req, "add: %d", this->req_list.back().req_num + 1);
329 req.req_num = this->req_list.back().req_num + 1;
331 this->req_list.push_back(req);
336 * Get trigger which the application requests
338 * WMTrigger contains which application requests what role and where to put(area) and task.
339 * This is used for input event to Window Policy Manager(state machine).
341 * @param unsigned[in] request number
342 * @param bool[in,out] Check request number of the parameter is valid.
343 * @return WMTrigger which associates with the request number
344 * @attention If the request number is not valid, parameter "found" is false
345 * and return value will be meaningless value.
346 * Caller can check the request parameter is valid.
348 struct WMTrigger AppList::getRequest(unsigned req_num, bool *found)
351 for (const auto &x : this->req_list)
353 if (req_num == x.req_num)
359 HMI_SEQ_ERROR(req_num, "Couldn't get request : %d", req_num);
360 return WMTrigger{"", "", "", Task::TASK_INVALID};
364 * Get actions which the application requests
366 * WMAciton contains the information of state transition.
367 * In other words, it contains actions of Window Manager,
368 * which role should be put to the area.
370 * @param unsigned[in] request number
371 * @param bool[in,out] Check request number of the parameter is valid.
372 * @return WMTrigger which associates with the request number
373 * @attention If the request number is not valid, parameter "found" is false
374 * and return value will be no reference pointer.
375 * Caller must check the request parameter is valid.
377 const vector<struct WMAction> &AppList::getActions(unsigned req_num, bool* found)
380 for (auto &x : this->req_list)
382 if (req_num == x.req_num)
385 return x.sync_draw_req;
388 HMI_SEQ_ERROR(req_num, "Couldn't get action with the request : %d", req_num);
392 * Set actions to the request.
394 * Add actions to the request.
395 * This function can be called many times, and actions increase.
396 * This function is used for decision of actions of Window Manager
397 * according to the result of Window Policy Manager.
399 * @param unsigned[in] request number
400 * @param WMAction[in] Action of Window Manager.
401 * @return WMError If request number is not valid, FAIL will be returned.
403 WMError AppList::setAction(unsigned req_num, const struct WMAction &action)
405 std::lock_guard<std::mutex> lock(this->mtx);
406 WMError result = WMError::FAIL;
407 for (auto &x : this->req_list)
409 if (req_num != x.req_num)
413 x.sync_draw_req.push_back(action);
414 result = WMError::SUCCESS;
422 * @note This function set action with parameters.
423 * If visible is true, it means app should be visible, so enddraw_finished parameter should be false.
424 * otherwise (visible is false) app should be invisible. Then enddraw_finished param is set to true.
425 * This function doesn't support actions for focus yet.
428 * Set actions to the request.
430 * This function is overload function.
431 * The feature is same as other one.
433 * @param unsigned[in] request number
434 * @param string[in] application id
435 * @param string[in] role
436 * @param string[in] area
437 * @param Task[in] the role should be visible or not.
438 * @return WMError If request number is not valid, FAIL will be returned.
439 * @attention This function set action with parameters, then caller doesn't need to create WMAction object.
440 * If visible is true, it means app should be visible, so enddraw_finished parameter will be false.
441 * otherwise (visible is false) app should be invisible. Then enddraw_finished param is set to true.
442 * This function doesn't support actions for focus yet.
444 WMError AppList::setAction(unsigned req_num, shared_ptr<WMClient> client, const string &role, const string &area, TaskVisible visible)
446 std::lock_guard<std::mutex> lock(this->mtx);
447 WMError result = WMError::FAIL;
448 for (auto &x : req_list)
450 if (req_num != x.req_num)
454 // If visible task is not invisible, redraw is required -> true
455 bool edraw_f = (visible != TaskVisible::INVISIBLE) ? false : true;
456 WMAction action{req_num, client, role, area, visible, edraw_f};
458 x.sync_draw_req.push_back(action);
459 result = WMError::SUCCESS;
466 * Set end_draw_finished param is true
468 * This function checks
469 * - req_num is equal to current request number
470 * - appid and role are equeal to the appid and role stored in action list
471 * If it is valid, set the action is finished.
473 * @param unsigned[in] request number
474 * @param string[in] application id
475 * @param string[in] role
476 * @return If the parameters are not valid in action list, returns false
478 bool AppList::setEndDrawFinished(unsigned req_num, const string &appid, const string &role)
480 std::lock_guard<std::mutex> lock(this->mtx);
482 for (auto &x : req_list)
484 if (req_num < x.req_num)
488 if (req_num == x.req_num)
490 for (auto &y : x.sync_draw_req)
492 if (y.client->appID() == appid && y.role == role)
494 HMI_SEQ_INFO(req_num, "Role %s finish redraw", y.role.c_str());
495 y.end_draw_finished = true;
506 * Check all actions of the requested sequence is finished
508 * @param unsigned[in] request_number
509 * @return true if all action is set.
511 bool AppList::endDrawFullfilled(unsigned req_num)
514 for (const auto &x : req_list)
516 if (req_num < x.req_num)
520 if (req_num == x.req_num)
523 for (const auto &y : x.sync_draw_req)
525 result &= y.end_draw_finished;
537 * Finish the request, then remove it.
539 * @param unsigned[in] request_number
541 * @note Please call next after this function to receive or process next request.
543 void AppList::removeRequest(unsigned req_num)
545 std::lock_guard<std::mutex> lock(this->mtx);
546 this->req_list.erase(remove_if(this->req_list.begin(), this->req_list.end(),
547 [req_num](WMRequest x) {
548 return x.req_num == req_num;
553 * Move the current request to next
560 std::lock_guard<std::mutex> lock(this->mtx);
562 if (0 == this->current_req)
564 this->current_req = 1;
569 * Check the request exists is in request list
572 * @return true if WMRequest exists in the request list
574 bool AppList::haveRequest() const
576 return !this->req_list.empty();
579 void AppList::clientDump()
581 DUMP("======= client dump =====");
582 for (const auto &x : this->app2client)
584 const auto &y = x.second;
587 DUMP("======= client dump end=====");
590 void AppList::reqDump()
592 DUMP("======= req dump =====");
593 DUMP("current request : %d", current_req);
594 for (const auto &x : req_list)
596 DUMP("requested : %d", x.req_num);
597 DUMP("Trigger : (APPID :%s, ROLE :%s, AREA :%s, TASK: %d)",
598 x.trigger.appid.c_str(),
599 x.trigger.role.c_str(),
600 x.trigger.area.c_str(),
603 for (const auto &y : x.sync_draw_req)
606 "Action : (APPID :%s, ROLE :%s, AREA :%s, VISIBLE : %s, END_DRAW_FINISHED: %d)",
607 y.client->appID().c_str(),
610 (y.visible == TaskVisible::INVISIBLE) ? "invisible" : "visible",
611 y.end_draw_finished);
614 DUMP("======= req dump end =====");
617 void AppList::dumpFloatingSurfaces()
619 DUMP("======= floating surface dump =====");
620 for (const auto &x : this->floating_surfaces)
622 DUMP("surface : %d, pid : %d", x.surface_id, x.pid);
624 DUMP("======= floating surface dump end =====\n");