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;
35 void AppList::addClient(const string &appid, const string &role)
37 shared_ptr<WMClient> client = std::make_shared<WMClient>(appid, role);
38 this->app2client[appid] = client;
42 void AppList::addClient(const std::string &appid, unsigned layer, unsigned surface, const std::string &role)
44 shared_ptr<WMClient> client = std::make_shared<WMClient>(appid, layer, surface, role);
45 this->app2client[appid] = client;
49 void AppList::removeClient(const string &appid)
51 this->app2client.erase(appid);
54 bool AppList::contains(const string &appid) const
56 auto result = this->app2client.find(appid);
57 return (this->app2client.end() != result) ? true : false;
60 void AppList::removeSurface(unsigned surface_id){
61 // This function may be very slow
63 for (auto &x : this->app2client)
65 ret = x.second->removeSurfaceIfExist(surface_id);
67 HMI_DEBUG("wm", "remove surface %d from Client %s finish", surface_id, x.second->appID().c_str());
74 * @brief get WMClient object. Before call this function, must call "contains"
75 * to check the key is contained, otherwise, you have to take care of std::out_of_range.
76 * @param string[in] application id(key)
77 * @return WMClient object
79 shared_ptr<WMClient> AppList::lookUpClient(const string &appid)
81 return this->app2client.at(appid);
84 int AppList::countClient() const
86 return this->app2client.size();
89 unsigned AppList::currentRequestNumber() const
91 return this->current_req;
94 WMError AppList::popFloatingSurface(unsigned pid, unsigned *surface)
96 WMError ret = WMError::NO_ENTRY;
98 for (auto itr = this->floating_surfaces.begin(); itr != this->floating_surfaces.end(); ++itr)
101 *surface = itr->surface_id;
102 itr = this->floating_surfaces.erase(itr);
103 ret = WMError::SUCCESS;
104 HMI_DEBUG("wm", "Erase surface %d", *surface);
111 WMError AppList::popFloatingSurface(const std::string &appid, unsigned *surface)
113 HMI_ERROR("wm", "This function is not implemented");
114 return WMError::SUCCESS;
117 void AppList::addFloatingClient(const std::string &appid, unsigned layer, const std::string &role)
121 void AppList::addFloatingSurface(unsigned surface, unsigned pid)
123 struct FloatingSurface fsurface{surface, pid};
124 this->floating_surfaces.push_back(fsurface);
127 void AppList::removeFloatingSurface(unsigned surface)
129 for (auto itr = this->floating_surfaces.begin(); itr != this->floating_surfaces.end(); ++itr)
131 if (surface == itr->surface_id)
133 HMI_DEBUG("wm", "Erase surface %d", itr->surface_id);
134 itr = this->floating_surfaces.erase(itr);
140 WMError AppList::appendRole(const std::string &id, const std::string &role, unsigned surface)
142 WMError wm_err = WMError::NO_ENTRY;
143 if (this->contains(id))
145 auto x = this->lookUpClient(id);
146 x->addSurface(role, surface);
147 wm_err = WMError::SUCCESS;
152 unsigned AppList::getRequestNumber(const string &appid) const
154 for (const auto &x : this->req_list)
156 // Since app will not request twice and more, comparing appid is enough?
157 if ((x.trigger.appid == appid))
165 unsigned AppList::addAllocateRequest(WMRequest req)
167 if (this->req_list.size() == 0)
169 req.req_num = current_req;
173 HMI_SEQ_DEBUG(this->current_req, "add: %d", this->req_list.back().req_num + 1);
174 req.req_num = this->req_list.back().req_num + 1;
176 this->req_list.push_back(req);
177 return req.req_num; // return 1; if you test time_expire
180 struct WMTrigger AppList::getRequest(unsigned req_num, bool *found)
183 for (const auto &x : this->req_list)
185 if (req_num == x.req_num)
191 return WMTrigger{"", "", "", Task::TASK_INVALID};
194 const vector<struct WMAction> &AppList::getActions(unsigned req_num, bool* found)
197 for (auto &x : this->req_list)
199 if (req_num == x.req_num)
202 return x.sync_draw_req;
207 WMError AppList::setAction(unsigned req_num, const struct WMAction &action)
209 WMError result = WMError::FAIL;
210 for (auto &x : this->req_list)
212 if (req_num != x.req_num)
216 x.sync_draw_req.push_back(action);
217 result = WMError::SUCCESS;
226 * This function set action with parameters.
227 * if visible is true, it means app should be visible, so enddraw_finished parameter should be false.
228 * otherwise (visible is false) app should be invisible. Then enddraw_finished param is set to true.
229 * This function doesn't support actions for focus yet.
231 WMError AppList::setAction(unsigned req_num, const string &appid, const string &role, const string &area, bool visible)
233 WMError result = WMError::NOT_REGISTERED;
234 for (auto &x : req_list)
236 if (req_num != x.req_num)
240 bool edraw_f = (visible) ? false : true;
241 WMAction action{appid, role, area, visible, edraw_f};
243 x.sync_draw_req.push_back(action);
244 result = WMError::SUCCESS;
251 * This function checks
252 * * req_num is equal to current request number
253 * * appid and role are equeal to the appid and role stored in action list(sync_draw_req)
255 bool AppList::setEndDrawFinished(unsigned req_num, const string &appid, const string &role)
258 for (auto &x : req_list)
260 if (req_num < x.req_num)
264 if (req_num == x.req_num)
266 for (auto &y : x.sync_draw_req)
268 if (y.appid == appid && y.role == role)
270 y.end_draw_finished = true;
281 * @brief check all actions of the requested sequence is finished
282 * @param unsigned request_number
283 * @return true if all action is set.
285 bool AppList::endDrawFullfilled(unsigned req_num)
288 for (const auto &x : req_list)
290 if (req_num < x.req_num)
294 if (req_num == x.req_num)
297 for (const auto &y : x.sync_draw_req)
299 result &= y.end_draw_finished;
310 void AppList::removeRequest(unsigned req_num)
312 this->req_list.erase(remove_if(this->req_list.begin(), this->req_list.end(),
313 [req_num](WMRequest x) {
314 return x.req_num == req_num;
321 if (0 == this->current_req)
323 this->current_req = 1;
327 bool AppList::haveRequest() const
329 return !this->req_list.empty();
332 void AppList::clientDump()
334 DUMP("======= client dump =====");
335 for (const auto &x : this->app2client)
337 const auto &y = x.second;
340 DUMP("======= client dump end=====");
343 void AppList::reqDump()
345 DUMP("======= req dump =====");
346 DUMP("current request : %d", current_req);
347 for (const auto &x : req_list)
349 DUMP("requested with : %d", x.req_num);
350 DUMP("Trigger : (APPID :%s, ROLE :%s, AREA :%s, TASK: %d)",
351 x.trigger.appid.c_str(),
352 x.trigger.role.c_str(),
353 x.trigger.area.c_str(),
356 for (const auto &y : x.sync_draw_req)
359 "Action : (APPID :%s, ROLE :%s, AREA :%s, END_DRAW_FINISHED: %d)",
363 y.end_draw_finished);
366 DUMP("======= req dump end =====\n");