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 AppList::~AppList() {}
37 void AppList::addClient(const string &appid, const string &role)
39 std::lock_guard<std::mutex> lock(mtx);
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 std::lock_guard<std::mutex> lock(mtx);
48 shared_ptr<WMClient> client = std::make_shared<WMClient>(appid, layer, surface, role);
49 this->app2client[appid] = client;
53 void AppList::removeClient(const string &appid)
55 std::lock_guard<std::mutex> lock(mtx);
56 this->app2client.erase(appid);
57 HMI_INFO("wm", "Remove client %s", appid.c_str());
60 bool AppList::contains(const string &appid) const
62 auto result = this->app2client.find(appid);
63 return (this->app2client.end() != result) ? true : false;
66 void AppList::removeSurface(unsigned surface_id){
67 // This function may be very slow
68 std::lock_guard<std::mutex> lock(mtx);
70 for (auto &x : this->app2client)
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());
82 * @brief get WMClient object. Before call this function, must call "contains"
83 * to check the key is contained, otherwise, you have to take care of std::out_of_range.
84 * @param string[in] application id(key)
85 * @return WMClient object
87 shared_ptr<WMClient> AppList::lookUpClient(const string &appid)
89 return this->app2client.at(appid);
92 int AppList::countClient() const
94 return this->app2client.size();
97 unsigned AppList::currentRequestNumber() const
99 return this->current_req;
102 unsigned AppList::getRequestNumber(const string &appid) const
104 for (const auto &x : this->req_list)
106 // Since app will not request twice and more, comparing appid is enough?
107 if ((x.trigger.appid == appid))
115 unsigned AppList::addAllocateRequest(WMRequest req)
117 std::lock_guard<std::mutex> lock(mtx);
118 if (this->req_list.size() == 0)
120 req.req_num = current_req;
124 HMI_SEQ_INFO(this->current_req, "add: %d", this->req_list.back().req_num + 1);
125 req.req_num = this->req_list.back().req_num + 1;
127 this->req_list.push_back(req);
131 struct WMTrigger AppList::getRequest(unsigned req_num, bool *found)
134 for (const auto &x : this->req_list)
136 if (req_num == x.req_num)
142 HMI_SEQ_ERROR(req_num, "Couldn't get request : %d", req_num);
143 return WMTrigger{"", "", "", Task::TASK_INVALID};
146 const vector<struct WMAction> &AppList::getActions(unsigned req_num, bool* found)
149 for (auto &x : this->req_list)
151 if (req_num == x.req_num)
154 return x.sync_draw_req;
157 HMI_SEQ_ERROR(req_num, "Couldn't get action with the request : %d", req_num);
160 WMError AppList::setAction(unsigned req_num, const struct WMAction &action)
162 std::lock_guard<std::mutex> lock(mtx);
163 WMError result = WMError::FAIL;
164 for (auto &x : this->req_list)
166 if (req_num != x.req_num)
170 x.sync_draw_req.push_back(action);
171 result = WMError::SUCCESS;
179 * This function set action with parameters.
180 * if visible is true, it means app should be visible, so enddraw_finished parameter should be false.
181 * otherwise (visible is false) app should be invisible. Then enddraw_finished param is set to true.
182 * This function doesn't support actions for focus yet.
184 WMError AppList::setAction(unsigned req_num, const string &appid, const string &role, const string &area, bool visible)
186 std::lock_guard<std::mutex> lock(mtx);
187 WMError result = WMError::NOT_REGISTERED;
188 for (auto &x : req_list)
190 if (req_num != x.req_num)
194 bool edraw_f = (visible) ? false : true;
195 WMAction action{appid, role, area, visible, edraw_f};
197 x.sync_draw_req.push_back(action);
198 result = WMError::SUCCESS;
205 * This function checks
206 * * req_num is equal to current request number
207 * * appid and role are equeal to the appid and role stored in action list(sync_draw_req)
209 bool AppList::setEndDrawFinished(unsigned req_num, const string &appid, const string &role)
211 std::lock_guard<std::mutex> lock(mtx);
213 for (auto &x : req_list)
215 if (req_num < x.req_num)
219 if (req_num == x.req_num)
221 for (auto &y : x.sync_draw_req)
223 if (y.appid == appid && y.role == role)
225 y.end_draw_finished = true;
236 * @brief check all actions of the requested sequence is finished
237 * @param unsigned request_number
238 * @return true if all action is set.
240 bool AppList::endDrawFullfilled(unsigned req_num)
243 for (const auto &x : req_list)
245 if (req_num < x.req_num)
249 if (req_num == x.req_num)
252 for (const auto &y : x.sync_draw_req)
254 result &= y.end_draw_finished;
265 void AppList::removeRequest(unsigned req_num)
267 std::lock_guard<std::mutex> lock(mtx);
268 this->req_list.erase(remove_if(this->req_list.begin(), this->req_list.end(),
269 [req_num](WMRequest x) {
270 return x.req_num == req_num;
276 std::lock_guard<std::mutex> lock(mtx);
278 if (0 == this->current_req)
280 this->current_req = 1;
284 bool AppList::haveRequest() const
286 return !this->req_list.empty();
289 void AppList::clientDump()
291 DUMP("======= client dump =====");
292 for (const auto &x : this->app2client)
294 const auto &y = x.second;
297 DUMP("======= client dump end=====");
300 void AppList::reqDump()
302 DUMP("======= req dump =====");
303 DUMP("current request : %d", current_req);
304 for (const auto &x : req_list)
306 DUMP("requested with : %d", x.req_num);
307 DUMP("Trigger : (APPID :%s, ROLE :%s, AREA :%s, TASK: %d)",
308 x.trigger.appid.c_str(),
309 x.trigger.role.c_str(),
310 x.trigger.area.c_str(),
313 for (const auto &y : x.sync_draw_req)
316 "Action : (APPID :%s, ROLE :%s, AREA :%s, END_DRAW_FINISHED: %d)",
320 y.end_draw_finished);
323 DUMP("======= req dump end =====\n");