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 (int i = 0; i < 1000; i++)
69 for (auto &x : this->app2client)
71 HMI_DEBUG("wm", "app: %s", x.second->appID());
72 ret = x.second->removeSurfaceIfExist(surface_id);
74 HMI_DEBUG("wm", "remove surface %d from Client %s finish", surface_id, x.second->appID().c_str());
81 * @brief get WMClient object. Before call this function, must call "contains"
82 * to check the key is contained, otherwise, you have to take care of std::out_of_range.
83 * @param string[in] application id(key)
84 * @return WMClient object
86 shared_ptr<WMClient> AppList::lookUpClient(const string &appid)
88 return this->app2client.at(appid);
91 int AppList::countClient() const
93 return this->app2client.size();
96 unsigned AppList::currentRequestNumber() const
98 return this->current_req;
101 WMError AppList::popFloatingSurface(unsigned pid, unsigned *surface)
103 WMError ret = WMError::NO_ENTRY;
105 auto rmv_begin = std::remove_if(this->floating_surfaces.begin(), this->floating_surfaces.end(),
106 [pid, surface, &ret](FloatingSurface x) {
108 *surface = x.surface_id;
109 ret = WMError::SUCCESS;
116 this->floating_surfaces.erase(rmv_begin, this->floating_surfaces.end());
120 WMError AppList::popFloatingSurface(const std::string &appid, unsigned *surface)
122 HMI_ERROR("wm", "This function is not implemented");
123 return WMError::SUCCESS;
126 void AppList::addFloatingClient(const std::string &appid, unsigned layer, const std::string &role)
130 void AppList::addFloatingSurface(unsigned surface, unsigned pid)
132 struct FloatingSurface fsurface{surface, pid};
133 this->floating_surfaces.push_back(fsurface);
134 this->dumpFloatingSurfaces();
137 void AppList::removeFloatingSurface(unsigned surface)
139 this->dumpFloatingSurfaces();
140 auto rmv_begin = std::remove_if(this->floating_surfaces.begin(), this->floating_surfaces.end(),
141 [surface](FloatingSurface x) {
142 return x.surface_id == surface;
144 this->floating_surfaces.erase(rmv_begin, this->floating_surfaces.end());
147 WMError AppList::appendRole(const std::string &id, const std::string &role, unsigned surface)
149 WMError wm_err = WMError::NO_ENTRY;
150 if (this->contains(id))
152 auto x = this->lookUpClient(id);
153 x->addSurface(role, surface);
154 wm_err = WMError::SUCCESS;
159 unsigned AppList::getRequestNumber(const string &appid) const
161 for (const auto &x : this->req_list)
163 // Since app will not request twice and more, comparing appid is enough?
164 if ((x.trigger.appid == appid))
172 unsigned AppList::addAllocateRequest(WMRequest req)
174 if (this->req_list.size() == 0)
176 req.req_num = current_req;
180 HMI_SEQ_DEBUG(this->current_req, "add: %d", this->req_list.back().req_num + 1);
181 req.req_num = this->req_list.back().req_num + 1;
183 this->req_list.push_back(req);
184 return req.req_num; // return 1; if you test time_expire
187 struct WMTrigger AppList::getRequest(unsigned req_num, bool *found)
190 for (const auto &x : this->req_list)
192 if (req_num == x.req_num)
198 return WMTrigger{"", "", "", Task::TASK_INVALID};
201 const vector<struct WMAction> &AppList::getActions(unsigned req_num, bool* found)
204 for (auto &x : this->req_list)
206 if (req_num == x.req_num)
209 return x.sync_draw_req;
214 WMError AppList::setAction(unsigned req_num, const struct WMAction &action)
216 WMError result = WMError::FAIL;
217 for (auto &x : this->req_list)
219 if (req_num != x.req_num)
223 x.sync_draw_req.push_back(action);
224 result = WMError::SUCCESS;
233 * This function set action with parameters.
234 * if visible is true, it means app should be visible, so enddraw_finished parameter should be false.
235 * otherwise (visible is false) app should be invisible. Then enddraw_finished param is set to true.
236 * This function doesn't support actions for focus yet.
238 WMError AppList::setAction(unsigned req_num, const string &appid, const string &role, const string &area, bool visible)
240 WMError result = WMError::NOT_REGISTERED;
241 for (auto &x : req_list)
243 if (req_num != x.req_num)
247 bool edraw_f = (visible) ? false : true;
248 WMAction action{appid, role, area, visible, edraw_f};
250 x.sync_draw_req.push_back(action);
251 result = WMError::SUCCESS;
258 * This function checks
259 * * req_num is equal to current request number
260 * * appid and role are equeal to the appid and role stored in action list(sync_draw_req)
262 bool AppList::setEndDrawFinished(unsigned req_num, const string &appid, const string &role)
265 for (auto &x : req_list)
267 if (req_num < x.req_num)
271 if (req_num == x.req_num)
273 for (auto &y : x.sync_draw_req)
275 if (y.appid == appid && y.role == role)
277 y.end_draw_finished = true;
288 * @brief check all actions of the requested sequence is finished
289 * @param unsigned request_number
290 * @return true if all action is set.
292 bool AppList::endDrawFullfilled(unsigned req_num)
295 for (const auto &x : req_list)
297 if (req_num < x.req_num)
301 if (req_num == x.req_num)
304 for (const auto &y : x.sync_draw_req)
306 result &= y.end_draw_finished;
317 void AppList::removeRequest(unsigned req_num)
319 this->req_list.erase(remove_if(this->req_list.begin(), this->req_list.end(),
320 [req_num](WMRequest x) {
321 return x.req_num == req_num;
328 if (0 == this->current_req)
330 this->current_req = 1;
334 bool AppList::haveRequest() const
336 return !this->req_list.empty();
339 void AppList::clientDump()
341 DUMP("======= client dump =====");
342 for (const auto &x : this->app2client)
344 const auto &y = x.second;
347 DUMP("======= client dump end=====");
350 void AppList::reqDump()
352 DUMP("======= req dump =====");
353 DUMP("current request : %d", current_req);
354 for (const auto &x : req_list)
356 DUMP("requested with : %d", x.req_num);
357 DUMP("Trigger : (APPID :%s, ROLE :%s, AREA :%s, TASK: %d)",
358 x.trigger.appid.c_str(),
359 x.trigger.role.c_str(),
360 x.trigger.area.c_str(),
363 for (const auto &y : x.sync_draw_req)
366 "Action : (APPID :%s, ROLE :%s, AREA :%s, END_DRAW_FINISHED: %d)",
370 y.end_draw_finished);
373 DUMP("======= req dump end =====\n");
376 void AppList::dumpFloatingSurfaces()
378 DUMP("======= floating surface dump =====");
379 for (const auto &x : this->floating_surfaces)
381 DUMP("surface : %d, pid : %d", x.surface_id, x.pid);
383 DUMP("======= floating surface dump end =====\n");