/* * Copyright (c) 2017 TOYOTA MOTOR CORPORATION * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include "applist.hpp" #include "../include/hmi-debug.h" using std::shared_ptr; using std::string; using std::vector; namespace wm { AppList::AppList() : req_list(0), app2client(0), current_req(1) { } AppList::~AppList() {} void AppList::addClient(const string &appid, const string &role) { shared_ptr client = std::make_shared(appid, role); this->app2client[appid] = client; this->clientDump(); } void AppList::addClient(const std::string &appid, unsigned layer, unsigned surface, const std::string &role) { shared_ptr client = std::make_shared(appid, layer, surface, role); this->app2client[appid] = client; this->clientDump(); } void AppList::removeClient(const string &appid) { this->app2client.erase(appid); } bool AppList::contains(const string &appid) { auto result = this->app2client.find(appid); return (this->app2client.end() != result) ? true : false; } void AppList::removeSurface(unsigned surface_id){ // This function may be very slow bool ret = false; for (auto &x : this->app2client) { ret = x.second->removeSurfaceIfExist(surface_id); if(ret){ HMI_DEBUG("wm", "remove surface %d from Client %s finish", surface_id, x.second->appID().c_str()); break; } } } /** * @brief get WMClient object. Before call this function, must call "contains" * to check the key is contained, otherwise, you have to take care of std::out_of_range. * @param string[in] application id(key) * @return WMClient object */ shared_ptr AppList::lookUpClient(const string &appid) { return this->app2client.at(appid); } int AppList::countClient() { return this->app2client.size(); } unsigned AppList::currentSequenceNumber() { return this->current_req; } // Is this function necessary ? unsigned AppList::getSequenceNumber(const string &appid) { for (const auto &x : this->req_list) { // Since app will not request twice and more, comparing appid is enough? if ((x.trigger.appid == appid)) { return x.seq_num; } } return 0; } unsigned AppList::addAllocateRequest(WMRequest req) { if (this->req_list.size() == 0) { req.seq_num = current_req; } else { HMI_SEQ_DEBUG(this->current_req, "add: %d", this->req_list.back().seq_num + 1); req.seq_num = this->req_list.back().seq_num + 1; } this->req_list.push_back(req); return req.seq_num; // return 1; if you test time_expire } bool AppList::requestFinished() { return this->req_list.empty(); } struct WMTrigger AppList::getRequest(unsigned req_num) { for (auto &x : this->req_list) { if (req_num == x.seq_num) { return x.trigger; } } } const vector &AppList::getActions(unsigned req_num) { for (auto &x : this->req_list) { if (req_num == x.seq_num) { return x.sync_draw_req; } } } bool AppList::setAction(unsigned req_num, const struct WMAction &action) { bool result = false; for (auto &x : this->req_list) { if (req_num != x.seq_num) { continue; } x.sync_draw_req.push_back(action); result = true; break; } return result; } bool AppList::setAction(unsigned req_num, const string &appid, const string &role, const string &area, bool visible) { bool result = false; for (auto &x : req_list) { if (req_num != x.seq_num) { continue; } bool edraw_f = false; WMAction action{appid, role, area, visible, edraw_f}; x.sync_draw_req.push_back(action); result = true; break; } return result; } bool AppList::setEndDrawFinished(unsigned req_num, const string &appid, const string &role) { bool result = false; for (auto &x : req_list) { if (req_num < x.seq_num) { break; } if (req_num == x.seq_num) { for (auto &y : x.sync_draw_req) { if (y.appid == appid && y.role == role) { y.end_draw_finished = true; result = true; } } } } this->reqDump(); return result; } /** * @brief check all actions of the requested sequence is finished * @param unsigned sequence_num * @return true if all action is set. */ bool AppList::endDrawFullfilled(unsigned req_num) { bool result = false; for (const auto &x : req_list) { if (req_num < x.seq_num) { break; } if (req_num == x.seq_num) { result = true; for (const auto &y : x.sync_draw_req) { result &= y.end_draw_finished; if (!result) { break; } } } } return result; } void AppList::removeRequest(unsigned req_seq) { this->req_list.erase(remove_if(this->req_list.begin(), this->req_list.end(), [req_seq](WMRequest x) { return x.seq_num == req_seq; })); } void AppList::next() { ++this->current_req; if (0 == this->current_req) { this->current_req = 1; } } bool AppList::haveRequest() { return !this->req_list.empty(); } void AppList::clientDump() { DUMP("======= client dump ====="); for (const auto &x : this->app2client) { const auto &y = x.second; y->dumpInfo(); } DUMP("======= client dump end====="); } void AppList::reqDump() { DUMP("======= req dump ====="); DUMP("current request : %d", current_req); for (const auto &x : req_list) { DUMP("requested with : %d", x.seq_num); DUMP("Trigger : (APPID :%s, ROLE :%s, AREA :%s, TASK: %d)", x.trigger.appid.c_str(), x.trigger.role.c_str(), x.trigger.area.c_str(), x.trigger.task); for (const auto &y : x.sync_draw_req) { DUMP( "Action : (APPID :%s, ROLE :%s, AREA :%s, END_DRAW_FINISHED: %d)", y.appid.c_str(), y.role.c_str(), y.area.c_str(), y.end_draw_finished); } } DUMP("======= req dump end =====\n"); } } // namespace wm