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;
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("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("remove surface %d from Client %s finish",
115 surface, x.second->appID().c_str());
123 * Get WMClient object.
125 * After get the WMClient object, caller can call the client method.
126 * Before call this function, caller must call "contains"
127 * to check the key is contained, otherwise, you have to take care of std::out_of_range.
129 * @param string[in] application id(key)
130 * @return WMClient object
131 * @attention Must call cantains to check appid is stored before this function.
133 shared_ptr<WMClient> AppList::lookUpClient(const string &appid)
135 return this->app2client.at(appid);
141 * Returns the number of client stored in the list.
144 * @return The number of client
146 int AppList::countClient() const
148 return this->app2client.size();
152 * Get AppID with surface and role.
154 * Returns AppID if found.
156 * @param unsigned[in] surfaceID
157 * @param string[in] role
158 * @param bool[in,out] AppID is found or not
160 * @attention If AppID is not found, param found will be false.
162 string AppList::getAppID(unsigned surface, const string& role, bool* found) const
165 for (const auto &x : this->app2client)
167 if(x.second->surfaceID(role) == surface){
169 return x.second->appID();
175 WMError AppList::popFloatingSurface(unsigned pid, unsigned *surface)
177 WMError ret = WMError::NO_ENTRY;
179 auto fwd_itr = std::remove_if(this->floating_surfaces.begin(), this->floating_surfaces.end(),
180 [pid, surface, &ret](FloatingSurface x) {
182 *surface = x.surface_id;
183 ret = WMError::SUCCESS;
190 if (fwd_itr != this->floating_surfaces.cend())
192 HMI_INFO("pop floating surface: %d", *surface);
194 this->floating_surfaces.erase(fwd_itr, this->floating_surfaces.end());
198 // =================== Floating(Temporary) surface/client API ===================
200 WMError AppList::popFloatingSurface(const string &appid, unsigned *surface)
202 HMI_ERROR("This function is not implemented");
203 return WMError::SUCCESS;
206 void AppList::addFloatingClient(const string &appid, unsigned layer, const string &role)
210 void AppList::addFloatingSurface(const string &appid, unsigned surface, unsigned pid)
212 struct FloatingSurface fsurface{appid, surface, pid};
213 this->floating_surfaces.push_back(fsurface);
214 this->dumpFloatingSurfaces();
217 void AppList::removeFloatingSurface(unsigned surface)
219 this->dumpFloatingSurfaces();
220 auto fwd_itr = std::remove_if(
221 this->floating_surfaces.begin(), this->floating_surfaces.end(),
222 [surface](FloatingSurface x) {
223 return x.surface_id == surface;
225 if(fwd_itr != this->floating_surfaces.cend()){
226 HMI_INFO("remove floating surface: %d", surface);
228 this->floating_surfaces.erase(fwd_itr, this->floating_surfaces.end());
231 WMError AppList::appendRole(const string &id, const string &role, unsigned surface)
233 WMError wm_err = WMError::NO_ENTRY;
234 HMI_ERROR("This function is disabled");
235 /* if (this->contains(id))
237 auto x = this->lookUpClient(id);
238 x->addSurface(role, surface);
239 wm_err = WMError::SUCCESS;
245 // =================== Request Date container API ===================
248 * Get current request number
250 * Request number is the numeric ID to designate the request.
251 * But returned request number from this function doesn't mean the request exists.
252 * This number is used as key to withdraw the WMRequest object.
255 * @return current request number.
256 * @note request number is more than 0.
258 unsigned AppList::currentRequestNumber() const
260 return this->current_req;
266 * Request number is the numeric ID to designate the request.
267 * But returned request number from this function doesn't mean the request exists.
268 * This number is used as key to withdraw the WMRequest object.
271 * @return request number.
272 * @attention If returned value is 0, no request exists.
274 unsigned AppList::getRequestNumber(const string &appid) const
276 for (const auto &x : this->req_list)
278 // Since app will not request twice and more, comparing appid is enough?
279 if ((x.trigger.appid == appid))
290 * Request number is the numeric ID to designate the request.
291 * But returned request number from this function doesn't mean the request exists.
292 * This number is used as key to withdraw the WMRequest object.
294 * @param WMRequest[in] WMRequest object caller creates
295 * @return Request number
296 * @attention If the request number is different with curent request number,
297 * it means the previous request is not finished.
299 unsigned AppList::addRequest(WMRequest req)
301 std::lock_guard<std::mutex> lock(this->mtx);
302 if (this->req_list.size() == 0)
304 req.req_num = current_req;
308 HMI_SEQ_INFO(this->current_req, "add: %d", this->req_list.back().req_num + 1);
309 req.req_num = this->req_list.back().req_num + 1;
311 this->req_list.push_back(req);
316 * Get trigger which the application requests
318 * WMTrigger contains which application requests what role and where to put(area) and task.
319 * This is used for input event to Window Policy Manager(state machine).
321 * @param unsigned[in] request number
322 * @param bool[in,out] Check request number of the parameter is valid.
323 * @return WMTrigger which associates with the request number
324 * @attention If the request number is not valid, parameter "found" is false
325 * and return value will be meaningless value.
326 * Caller can check the request parameter is valid.
328 struct WMTrigger AppList::getRequest(unsigned req_num, bool *found)
331 for (const auto &x : this->req_list)
333 if (req_num == x.req_num)
339 HMI_SEQ_ERROR(req_num, "Couldn't get request : %d", req_num);
340 return WMTrigger{"", "", "", Task::TASK_INVALID};
344 * Get actions which the application requests
346 * WMAciton contains the information of state transition.
347 * In other words, it contains actions of Window Manager,
348 * which role should be put to the area.
350 * @param unsigned[in] request number
351 * @param bool[in,out] Check request number of the parameter is valid.
352 * @return WMTrigger which associates with the request number
353 * @attention If the request number is not valid, parameter "found" is false
354 * and return value will be no reference pointer.
355 * Caller must check the request parameter is valid.
357 const vector<struct WMAction> &AppList::getActions(unsigned req_num, bool* found)
360 for (auto &x : this->req_list)
362 if (req_num == x.req_num)
365 return x.sync_draw_req;
368 HMI_SEQ_ERROR(req_num, "Couldn't get action with the request : %d", req_num);
372 * Set actions to the request.
374 * Add actions to the request.
375 * This function can be called many times, and actions increase.
376 * This function is used for decision of actions of Window Manager
377 * according to the result of Window Policy Manager.
379 * @param unsigned[in] request number
380 * @param WMAction[in] Action of Window Manager.
381 * @return WMError If request number is not valid, FAIL will be returned.
383 WMError AppList::setAction(unsigned req_num, const struct WMAction &action)
385 std::lock_guard<std::mutex> lock(this->mtx);
386 WMError result = WMError::FAIL;
387 for (auto &x : this->req_list)
389 if (req_num != x.req_num)
393 x.sync_draw_req.push_back(action);
394 result = WMError::SUCCESS;
402 * @note This function set action with parameters.
403 * If visible is true, it means app should be visible, so enddraw_finished parameter should be false.
404 * otherwise (visible is false) app should be invisible. Then enddraw_finished param is set to true.
405 * This function doesn't support actions for focus yet.
408 * Set actions to the request.
410 * This function is overload function.
411 * The feature is same as other one.
413 * @param unsigned[in] request number
414 * @param string[in] application id
415 * @param string[in] role
416 * @param string[in] area
417 * @param Task[in] the role should be visible or not.
418 * @return WMError If request number is not valid, FAIL will be returned.
419 * @attention This function set action with parameters, then caller doesn't need to create WMAction object.
420 * If visible is true, it means app should be visible, so enddraw_finished parameter will be false.
421 * otherwise (visible is false) app should be invisible. Then enddraw_finished param is set to true.
422 * This function doesn't support actions for focus yet.
424 WMError AppList::setAction(unsigned req_num, const string &appid, const string &role, const string &area, TaskVisible visible)
426 std::lock_guard<std::mutex> lock(this->mtx);
427 WMError result = WMError::FAIL;
428 for (auto &x : req_list)
430 if (req_num != x.req_num)
434 // If visible task is not invisible, redraw is required -> true
435 bool edraw_f = (visible != TaskVisible::INVISIBLE) ? false : true;
436 WMAction action{appid, role, area, visible, edraw_f};
438 x.sync_draw_req.push_back(action);
439 result = WMError::SUCCESS;
446 * Set end_draw_finished param is true
448 * This function checks
449 * - req_num is equal to current request number
450 * - appid and role are equeal to the appid and role stored in action list
451 * If it is valid, set the action is finished.
453 * @param unsigned[in] request number
454 * @param string[in] application id
455 * @param string[in] role
456 * @return If the parameters are not valid in action list, returns false
458 bool AppList::setEndDrawFinished(unsigned req_num, const string &appid, const string &role)
460 std::lock_guard<std::mutex> lock(this->mtx);
462 for (auto &x : req_list)
464 if (req_num < x.req_num)
468 if (req_num == x.req_num)
470 for (auto &y : x.sync_draw_req)
472 if (y.appid == appid && y.role == role)
474 HMI_SEQ_INFO(req_num, "Role %s finish redraw", y.role.c_str());
475 y.end_draw_finished = true;
486 * Check all actions of the requested sequence is finished
488 * @param unsigned[in] request_number
489 * @return true if all action is set.
491 bool AppList::endDrawFullfilled(unsigned req_num)
494 for (const auto &x : req_list)
496 if (req_num < x.req_num)
500 if (req_num == x.req_num)
503 for (const auto &y : x.sync_draw_req)
505 result &= y.end_draw_finished;
517 * Finish the request, then remove it.
519 * @param unsigned[in] request_number
521 * @note Please call next after this function to receive or process next request.
523 void AppList::removeRequest(unsigned req_num)
525 std::lock_guard<std::mutex> lock(this->mtx);
526 this->req_list.erase(remove_if(this->req_list.begin(), this->req_list.end(),
527 [req_num](WMRequest x) {
528 return x.req_num == req_num;
533 * Move the current request to next
540 std::lock_guard<std::mutex> lock(this->mtx);
542 if (0 == this->current_req)
544 this->current_req = 1;
549 * Check the request exists is in request list
552 * @return true if WMRequest exists in the request list
554 bool AppList::haveRequest() const
556 return !this->req_list.empty();
559 void AppList::clientDump()
561 DUMP("======= client dump =====");
562 for (const auto &x : this->app2client)
564 const auto &y = x.second;
567 DUMP("======= client dump end=====");
570 void AppList::reqDump()
572 DUMP("======= req dump =====");
573 DUMP("current request : %d", current_req);
574 for (const auto &x : req_list)
576 DUMP("requested : %d", x.req_num);
577 DUMP("Trigger : (APPID :%s, ROLE :%s, AREA :%s, TASK: %d)",
578 x.trigger.appid.c_str(),
579 x.trigger.role.c_str(),
580 x.trigger.area.c_str(),
583 for (const auto &y : x.sync_draw_req)
586 "Action : (APPID :%s, ROLE :%s, AREA :%s, VISIBLE : %s, END_DRAW_FINISHED: %d)",
590 (y.visible == TaskVisible::INVISIBLE) ? "invisible" : "visible",
591 y.end_draw_finished);
594 DUMP("======= req dump end =====");
597 void AppList::dumpFloatingSurfaces()
599 DUMP("======= floating surface dump =====");
600 for (const auto &x : this->floating_surfaces)
602 DUMP("surface : %d, pid : %d", x.surface_id, x.pid);
604 DUMP("======= floating surface dump end =====\n");