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.
19 #include "wm_client.hpp"
20 #include "wm_layer.hpp"
21 #include "json_helper.hpp"
26 using std::unordered_map;
31 LayerState::LayerState()
37 void LayerState::attachIdToArea(const string& area, const WMClient& client)
39 this->area2appid[area] = client.appID();
40 this->render_order.push_back(client.layerID());
43 const unordered_map<std::string, std::string> LayerState::popCurrentState()
45 unordered_map<string, string> tmp = this->area2appid;
46 this->area2appid.clear();
47 this->render_order.clear();
51 const unordered_map<std::string, std::string> LayerState::getCurrentState()
53 return this->area2appid;
56 const vector<unsigned> LayerState::getIviIdList()
58 return this->render_order;
61 LayerSetting::LayerSetting(const string& name, MANAGEMENT_TYPE type, unsigned begin, unsigned end)
62 : name(name), type(type),
63 role_list(), area_list(), id_list(),
64 id_begin(begin), id_end(end)
67 void LayerSetting::setRoleList(const string& role)
69 this->role_list = role;
72 void LayerSetting::appendArea(const string& area)
74 this->area_list.push_back(area);
77 unsigned LayerSetting::getNewLayerID(const string& role)
80 auto re = std::regex(this->role_list);
81 if (std::regex_match(role, re))
83 // generate new layer id;
84 ret = this->id_list.back() + 1;
85 HMI_DEBUG("role %s matches layer %d, new layerID %d", role.c_str(), this->name.c_str(), ret);
93 auto id_found = std::find(id_list.begin(), id_list.end(), ret);
94 if( (ret > this->idEnd()) || (id_found != id_list.cend()) )
96 HMI_NOTICE("id %d is not available then generate new id", ret);
98 for(unsigned i = this->idBegin(); i < this->idEnd(); i++)
100 auto ret_found = std::find(id_list.begin(), id_list.end(), i);
101 if(ret_found == id_list.cend())
103 HMI_INFO("set new id: %d", i);
112 id_list.push_back(ret);
116 HMI_ERROR("failed to get New ID");
121 void LayerSetting::removeLayerID(unsigned id)
123 auto fwd_itr = std::remove_if(this->id_list.begin(), this->id_list.end(),
127 this->id_list.erase(fwd_itr, this->id_list.end());
135 // this->setting = std::make_unique<LayerSetting>(name, type, begin, end);
138 WMLayer::WMLayer(json_object* j) : before_state(), state()
140 LayerSetting::MANAGEMENT_TYPE t;
141 const char* layer_name = jh::getStringFromJson(j, "name");
142 const char* roles = jh::getStringFromJson(j, "role");
143 const char* type = jh::getStringFromJson(j, "type");
144 int begin = jh::getIntFromJson(j, "id_range_begin");
145 int end = jh::getIntFromJson(j, "id_range_end");
146 string name = layer_name;
148 if (layer_name || type || begin < 0 || end < 0)
150 HMI_ERROR("Parse Error!!");
154 HMI_ERROR("INVALID.");
156 string str_type = type;
157 t = (str_type == "tile") ? LayerSetting::TILE : LayerSetting::STACK;
158 this->setting = std::make_unique<LayerSetting>(name, t, begin, end);
159 this->setting->setRoleList(roles);
162 unsigned WMLayer::getNewLayerID(const std::string& role)
164 return this->setting->getNewLayerID(role);
167 WMError WMLayer::setLayerState(const LayerState& l)
169 this->before_state = l;
170 return WMError::SUCCESS;
173 bool WMLayer::checkIDBelongTo(unsigned id)
175 return (id > this->setting->idBegin() && id < this->setting->idEnd());
178 /* WMError WMLayer::commitChange()
180 this->state = this->before_state;
185 this->before_state = this->state;