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;
25 const static int kReserveReqSize = 10;
26 const static int kReserveCLSize = 100;
34 req_list.reserve(kReserveReqSize);
35 app2client.reserve(kReserveCLSize);
38 void AppList::addClient(const string &appid, const string &role)
40 shared_ptr<WMClient> client = std::make_shared<WMClient>(appid, role);
41 this->app2client[appid] = client;
45 void AppList::addClient(const std::string &appid, unsigned layer, unsigned surface, const std::string &role)
47 shared_ptr<WMClient> client = std::make_shared<WMClient>(appid, layer, surface, role);
48 this->app2client[appid] = client;
52 void AppList::removeClient(const string &appid)
54 this->app2client.erase(appid);
57 bool AppList::contains(const string &appid) const
59 auto result = this->app2client.find(appid);
60 return (this->app2client.end() != result) ? true : false;
63 void AppList::removeSurface(unsigned surface_id){
64 // This function may be very slow
66 for (auto &x : this->app2client)
68 ret = x.second->removeSurfaceIfExist(surface_id);
70 HMI_DEBUG("wm", "remove surface %d from Client %s finish", surface_id, x.second->appID().c_str());
77 * @brief get WMClient object. Before call this function, must call "contains"
78 * to check the key is contained, otherwise, you have to take care of std::out_of_range.
79 * @param string[in] application id(key)
80 * @return WMClient object
82 shared_ptr<WMClient> AppList::lookUpClient(const string &appid)
84 return this->app2client.at(appid);
87 int AppList::countClient() const
89 return this->app2client.size();
92 unsigned AppList::currentRequestNumber() const
94 return this->current_req;
97 WMError AppList::popFloatingSurface(unsigned pid, unsigned *surface)
99 WMError ret = WMError::NO_ENTRY;
101 for (auto itr = this->floating_surfaces.begin(); itr != this->floating_surfaces.end(); ++itr)
104 *surface = itr->surface_id;
105 itr = this->floating_surfaces.erase(itr);
106 ret = WMError::SUCCESS;
107 HMI_DEBUG("wm", "Erase surface %d", *surface);
114 WMError AppList::popFloatingSurface(const std::string &appid, unsigned *surface)
116 HMI_ERROR("wm", "This function is not implemented");
117 return WMError::SUCCESS;
120 void AppList::addFloatingClient(const std::string &appid, unsigned layer, const std::string &role)
124 void AppList::addFloatingSurface(unsigned surface, unsigned pid)
126 struct FloatingSurface fsurface{surface, pid};
127 this->floating_surfaces.push_back(fsurface);
130 void AppList::removeFloatingSurface(unsigned surface)
132 for (auto itr = this->floating_surfaces.begin(); itr != this->floating_surfaces.end(); ++itr)
134 if (surface == itr->surface_id)
136 HMI_DEBUG("wm", "Erase surface %d", itr->surface_id);
137 itr = this->floating_surfaces.erase(itr);
143 WMError AppList::appendRole(const std::string &id, const std::string &role, unsigned surface)
145 WMError wm_err = WMError::NO_ENTRY;
146 if (this->contains(id))
148 auto x = this->lookUpClient(id);
149 x->addSurface(role, surface);
150 wm_err = WMError::SUCCESS;
155 unsigned AppList::getRequestNumber(const string &appid) const
157 for (const auto &x : this->req_list)
159 // Since app will not request twice and more, comparing appid is enough?
160 if ((x.trigger.appid == appid))
168 unsigned AppList::addAllocateRequest(WMRequest req)
170 if (this->req_list.size() == 0)
172 req.req_num = current_req;
176 HMI_SEQ_DEBUG(this->current_req, "add: %d", this->req_list.back().req_num + 1);
177 req.req_num = this->req_list.back().req_num + 1;
179 this->req_list.push_back(req);
180 return req.req_num; // return 1; if you test time_expire
183 struct WMTrigger AppList::getRequest(unsigned req_num, bool *found)
186 for (const auto &x : this->req_list)
188 if (req_num == x.req_num)
194 return WMTrigger{"", "", "", Task::TASK_INVALID};
197 const vector<struct WMAction> &AppList::getActions(unsigned req_num, bool* found)
200 for (auto &x : this->req_list)
202 if (req_num == x.req_num)
205 return x.sync_draw_req;
210 WMError AppList::setAction(unsigned req_num, const struct WMAction &action)
212 WMError result = WMError::FAIL;
213 for (auto &x : this->req_list)
215 if (req_num != x.req_num)
219 x.sync_draw_req.push_back(action);
220 result = WMError::SUCCESS;
229 * This function set action with parameters.
230 * if visible is true, it means app should be visible, so enddraw_finished parameter should be false.
231 * otherwise (visible is false) app should be invisible. Then enddraw_finished param is set to true.
232 * This function doesn't support actions for focus yet.
234 WMError AppList::setAction(unsigned req_num, const string &appid, const string &role, const string &area, bool visible)
236 WMError result = WMError::NOT_REGISTERED;
237 for (auto &x : req_list)
239 if (req_num != x.req_num)
243 bool edraw_f = (visible) ? false : true;
244 WMAction action{appid, role, area, visible, edraw_f};
246 x.sync_draw_req.push_back(action);
247 result = WMError::SUCCESS;
254 * This function checks
255 * * req_num is equal to current request number
256 * * appid and role are equeal to the appid and role stored in action list(sync_draw_req)
258 bool AppList::setEndDrawFinished(unsigned req_num, const string &appid, const string &role)
261 for (auto &x : req_list)
263 if (req_num < x.req_num)
267 if (req_num == x.req_num)
269 for (auto &y : x.sync_draw_req)
271 if (y.appid == appid && y.role == role)
273 y.end_draw_finished = true;
284 * @brief check all actions of the requested sequence is finished
285 * @param unsigned request_number
286 * @return true if all action is set.
288 bool AppList::endDrawFullfilled(unsigned req_num)
291 for (const auto &x : req_list)
293 if (req_num < x.req_num)
297 if (req_num == x.req_num)
300 for (const auto &y : x.sync_draw_req)
302 result &= y.end_draw_finished;
313 void AppList::removeRequest(unsigned req_num)
315 this->req_list.erase(remove_if(this->req_list.begin(), this->req_list.end(),
316 [req_num](WMRequest x) {
317 return x.req_num == req_num;
324 if (0 == this->current_req)
326 this->current_req = 1;
330 bool AppList::haveRequest() const
332 return !this->req_list.empty();
335 void AppList::clientDump()
337 DUMP("======= client dump =====");
338 for (const auto &x : this->app2client)
340 const auto &y = x.second;
343 DUMP("======= client dump end=====");
346 void AppList::reqDump()
348 DUMP("======= req dump =====");
349 DUMP("current request : %d", current_req);
350 for (const auto &x : req_list)
352 DUMP("requested with : %d", x.req_num);
353 DUMP("Trigger : (APPID :%s, ROLE :%s, AREA :%s, TASK: %d)",
354 x.trigger.appid.c_str(),
355 x.trigger.role.c_str(),
356 x.trigger.area.c_str(),
359 for (const auto &y : x.sync_draw_req)
362 "Action : (APPID :%s, ROLE :%s, AREA :%s, END_DRAW_FINISHED: %d)",
366 y.end_draw_finished);
369 DUMP("======= req dump end =====\n");