/* * 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 "wm_client.hpp" #include "wm_layer.hpp" #include "json_helper.hpp" #include "util.hpp" using std::string; using std::vector; using std::unordered_map; namespace wm { LayerState::LayerState() : render_order(), area2appid() {} void LayerState::attachIdToArea(const string& area, const WMClient& client) { this->area2appid[area] = client.appID(); this->render_order.push_back(client.layerID()); } const unordered_map LayerState::popCurrentState() { unordered_map tmp = this->area2appid; this->area2appid.clear(); this->render_order.clear(); return tmp; } const unordered_map LayerState::getCurrentState() { return this->area2appid; } const vector LayerState::getIviIdList() { return this->render_order; } LayerSetting::LayerSetting(const string& name, MANAGEMENT_TYPE type, unsigned begin, unsigned end) : name(name), type(type), role_list(), area_list(), id_list(), id_begin(begin), id_end(end) {} void LayerSetting::setRoleList(const string& role) { this->role_list = role; } void LayerSetting::appendArea(const string& area) { this->area_list.push_back(area); } unsigned LayerSetting::getNewLayerID(const string& role) { unsigned ret = 0; auto re = std::regex(this->role_list); if (std::regex_match(role, re)) { // generate new layer id; ret = this->id_list.back() + 1; HMI_DEBUG("role %s matches layer %d, new layerID %d", role.c_str(), this->name.c_str(), ret); } if(ret == 0) { return ret; } auto id_found = std::find(id_list.begin(), id_list.end(), ret); if( (ret > this->idEnd()) || (id_found != id_list.cend()) ) { HMI_NOTICE("id %d is not available then generate new id", ret); ret = 0; // reset for(unsigned i = this->idBegin(); i < this->idEnd(); i++) { auto ret_found = std::find(id_list.begin(), id_list.end(), i); if(ret_found == id_list.cend()) { HMI_INFO("set new id: %d", i); ret = i; break; } } } if(ret != 0) { id_list.push_back(ret); } else { HMI_ERROR("failed to get New ID"); } return ret; } void LayerSetting::removeLayerID(unsigned id) { auto fwd_itr = std::remove_if(this->id_list.begin(), this->id_list.end(), [id](unsigned elm) { return elm == id; }); this->id_list.erase(fwd_itr, this->id_list.end()); } WMLayer::WMLayer() : before_state(), state(), setting{} { // this->setting = std::make_unique(name, type, begin, end); } WMLayer::WMLayer(json_object* j) : before_state(), state() { LayerSetting::MANAGEMENT_TYPE t; const char* layer_name = jh::getStringFromJson(j, "name"); const char* roles = jh::getStringFromJson(j, "role"); const char* type = jh::getStringFromJson(j, "type"); int begin = jh::getIntFromJson(j, "id_range_begin"); int end = jh::getIntFromJson(j, "id_range_end"); string name = layer_name; if (layer_name || type || begin < 0 || end < 0) { HMI_ERROR("Parse Error!!"); } if(begin > end) { HMI_ERROR("INVALID."); } string str_type = type; t = (str_type == "tile") ? LayerSetting::TILE : LayerSetting::STACK; this->setting = std::make_unique(name, t, begin, end); this->setting->setRoleList(roles); } unsigned WMLayer::getNewLayerID(const std::string& role) { return this->setting->getNewLayerID(role); } WMError WMLayer::setLayerState(const LayerState& l) { this->before_state = l; return WMError::SUCCESS; } bool WMLayer::checkIDBelongTo(unsigned id) { return (id > this->setting->idBegin() && id < this->setting->idEnd()); } /* WMError WMLayer::commitChange() { this->state = this->before_state; } void WMLayer::undo() { this->before_state = this->state; } */ } // namespace wm