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;
28 const static int kReserveClientSize = 100;
29 const static int kReserveReqSize = 10;
36 app2client.reserve(kReserveClientSize);
37 req_list.reserve(kReserveReqSize);
40 AppList::~AppList() {}
42 void AppList::addClient(const string &appid, const string &role)
44 std::lock_guard<std::mutex> lock(mtx);
45 shared_ptr<WMClient> client = std::make_shared<WMClient>(appid, role);
46 this->app2client[appid] = client;
50 void AppList::addClient(const std::string &appid, unsigned layer, unsigned surface, const std::string &role)
52 std::lock_guard<std::mutex> lock(mtx);
53 shared_ptr<WMClient> client = std::make_shared<WMClient>(appid, layer, surface, role);
54 this->app2client[appid] = client;
58 void AppList::removeClient(const string &appid)
60 std::lock_guard<std::mutex> lock(mtx);
61 this->app2client.erase(appid);
62 HMI_INFO("wm", "Remove client %s", appid.c_str());
65 bool AppList::contains(const string &appid) const
67 auto result = this->app2client.find(appid);
68 return (this->app2client.end() != result) ? true : false;
71 void AppList::removeSurface(unsigned surface_id){
72 // This function may be very slow
73 std::lock_guard<std::mutex> lock(mtx);
75 for (auto &x : this->app2client)
77 ret = x.second->removeSurfaceIfExist(surface_id);
79 HMI_DEBUG("wm", "remove surface %d from Client %s finish", surface_id, x.second->appID().c_str());
87 * @brief get WMClient object. Before call this function, must call "contains"
88 * to check the key is contained, otherwise, you have to take care of std::out_of_range.
89 * @param string[in] application id(key)
90 * @return WMClient object
92 shared_ptr<WMClient> AppList::lookUpClient(const string &appid)
94 return this->app2client.at(appid);
97 int AppList::countClient() const
99 return this->app2client.size();
102 unsigned AppList::currentRequestNumber() const
104 return this->current_req;
107 unsigned AppList::getRequestNumber(const string &appid) const
109 for (const auto &x : this->req_list)
111 // Since app will not request twice and more, comparing appid is enough?
112 if ((x.trigger.appid == appid))
120 unsigned AppList::addAllocateRequest(WMRequest req)
122 std::lock_guard<std::mutex> lock(mtx);
123 if (this->req_list.size() == 0)
125 req.req_num = current_req;
129 HMI_SEQ_INFO(this->current_req, "add: %d", this->req_list.back().req_num + 1);
130 req.req_num = this->req_list.back().req_num + 1;
132 this->req_list.push_back(req);
136 struct WMTrigger AppList::getRequest(unsigned req_num, bool *found)
139 for (const auto &x : this->req_list)
141 if (req_num == x.req_num)
147 HMI_SEQ_ERROR(req_num, "Couldn't get request : %d", req_num);
148 return WMTrigger{"", "", "", Task::TASK_INVALID};
151 const vector<struct WMAction> &AppList::getActions(unsigned req_num, bool* found)
154 for (auto &x : this->req_list)
156 if (req_num == x.req_num)
159 return x.sync_draw_req;
162 HMI_SEQ_ERROR(req_num, "Couldn't get action with the request : %d", req_num);
165 WMError AppList::setAction(unsigned req_num, const struct WMAction &action)
167 std::lock_guard<std::mutex> lock(mtx);
168 WMError result = WMError::FAIL;
169 for (auto &x : this->req_list)
171 if (req_num != x.req_num)
175 x.sync_draw_req.push_back(action);
176 result = WMError::SUCCESS;
184 * This function set action with parameters.
185 * if visible is true, it means app should be visible, so enddraw_finished parameter should be false.
186 * otherwise (visible is false) app should be invisible. Then enddraw_finished param is set to true.
187 * This function doesn't support actions for focus yet.
189 WMError AppList::setAction(unsigned req_num, const string &appid, const string &role, const string &area, bool visible)
191 std::lock_guard<std::mutex> lock(mtx);
192 WMError result = WMError::NOT_REGISTERED;
193 for (auto &x : req_list)
195 if (req_num != x.req_num)
199 bool edraw_f = (visible) ? false : true;
200 WMAction action{appid, role, area, visible, edraw_f};
202 x.sync_draw_req.push_back(action);
203 result = WMError::SUCCESS;
210 * This function checks
211 * * req_num is equal to current request number
212 * * appid and role are equeal to the appid and role stored in action list(sync_draw_req)
214 bool AppList::setEndDrawFinished(unsigned req_num, const string &appid, const string &role)
216 std::lock_guard<std::mutex> lock(mtx);
218 for (auto &x : req_list)
220 if (req_num < x.req_num)
224 if (req_num == x.req_num)
226 for (auto &y : x.sync_draw_req)
228 if (y.appid == appid && y.role == role)
230 y.end_draw_finished = true;
241 * @brief check all actions of the requested sequence is finished
242 * @param unsigned request_number
243 * @return true if all action is set.
245 bool AppList::endDrawFullfilled(unsigned req_num)
248 for (const auto &x : req_list)
250 if (req_num < x.req_num)
254 if (req_num == x.req_num)
257 for (const auto &y : x.sync_draw_req)
259 result &= y.end_draw_finished;
270 void AppList::removeRequest(unsigned req_num)
272 std::lock_guard<std::mutex> lock(mtx);
273 this->req_list.erase(remove_if(this->req_list.begin(), this->req_list.end(),
274 [req_num](WMRequest x) {
275 return x.req_num == req_num;
281 std::lock_guard<std::mutex> lock(mtx);
283 if (0 == this->current_req)
285 this->current_req = 1;
289 bool AppList::haveRequest() const
291 return !this->req_list.empty();
294 void AppList::clientDump()
296 DUMP("======= client dump =====");
297 for (const auto &x : this->app2client)
299 const auto &y = x.second;
302 DUMP("======= client dump end=====");
305 void AppList::reqDump()
307 DUMP("======= req dump =====");
308 DUMP("current request : %d", current_req);
309 for (const auto &x : req_list)
311 DUMP("requested with : %d", x.req_num);
312 DUMP("Trigger : (APPID :%s, ROLE :%s, AREA :%s, TASK: %d)",
313 x.trigger.appid.c_str(),
314 x.trigger.role.c_str(),
315 x.trigger.area.c_str(),
318 for (const auto &y : x.sync_draw_req)
321 "Action : (APPID :%s, ROLE :%s, AREA :%s, END_DRAW_FINISHED: %d)",
325 y.end_draw_finished);
328 DUMP("======= req dump end =====\n");