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 <ilm/ilm_control.h>
20 #include "wm_client.hpp"
21 #include "wm_layer.hpp"
22 #include "json_helper.hpp"
27 using std::unordered_map;
29 #define BG_LAYER_NAME "BackGroundLayer"
34 LayerState::LayerState()
40 void LayerState::attachIdToArea(const string& area, const WMClient& client)
42 this->area2appid[area] = client.appID();
43 this->render_order.push_back(client.layerID());
46 const unordered_map<string, string> LayerState::popCurrentState()
48 unordered_map<string, string> tmp = this->area2appid;
49 this->area2appid.clear();
50 this->render_order.clear();
54 const unordered_map<string, string> LayerState::getCurrentState()
56 return this->area2appid;
59 const vector<unsigned> LayerState::getIviIdList()
61 return this->render_order;
64 void LayerState::addLayer(unsigned layer)
66 auto result = std::find(this->render_order.begin(), this->render_order.end(), layer);
67 if(result == this->render_order.end())
68 this->render_order.push_back(layer);
71 void LayerState::removeLayer(unsigned layer)
73 auto fwd_itr = std::remove_if(
74 this->render_order.begin(), this->render_order.end(),
75 [layer](unsigned elm) {
77 HMI_DEBUG("remove layer %d", elm);
81 this->render_order.erase(fwd_itr, this->render_order.end());
84 void LayerState::attachAppToArea(const string& app, const string& area)
86 this->area2appid[area] = app;
89 void LayerState::dump()
91 std::string ids, apps;
92 for(const auto& ro : this->render_order)
94 ids += std::to_string(ro);
97 for(const auto& area : this->area2appid)
104 DUMP(" render order : %s", ids.c_str());
105 DUMP(" area, app : %s", apps.c_str());
108 WMLayer::WMLayer(json_object* j, unsigned wm_layer_id) : tmp_state(), state(), wm_layer_id(wm_layer_id)
110 this->name = jh::getStringFromJson(j, "name");
111 this->role_list = jh::getStringFromJson(j, "role");
112 this->id_begin = static_cast<unsigned>(jh::getIntFromJson(j, "id_range_begin"));
113 this->id_end = static_cast<unsigned>(jh::getIntFromJson(j, "id_range_end"));
117 HMI_ERROR("Parse Error!!");
120 if(this->id_begin > this->id_end)
122 HMI_ERROR("INVALID");
127 unsigned WMLayer::getNewLayerID(const string& role)
130 if(this->name == BG_LAYER_NAME)
133 // generate new layer id;
134 if(this->hasRole(role))
136 if(this->id_list.size() == 0)
138 ret = this->idBegin();
139 this->id_list.push_back(ret);
143 ret = this->id_list.back() + 1;
145 HMI_INFO("Generate new id: %d", ret);
152 size_t count = std::count(id_list.begin(), id_list.end(), ret);
153 if( (ret > this->idEnd()) || (count > 1))
155 HMI_NOTICE("id %d is not available then generate new id", ret);
157 for(unsigned i = this->idBegin(); i < this->idEnd(); i++)
159 auto ret_found = std::find(id_list.begin(), id_list.end(), i);
160 if(ret_found == id_list.cend())
162 HMI_INFO("set new id: %d", i);
171 id_list.push_back(ret);
175 HMI_ERROR("failed to get New ID");
180 const string& WMLayer::layerName()
185 WMError WMLayer::setLayerState(const LayerState& l)
188 return WMError::SUCCESS;
191 void WMLayer::addLayerToState(unsigned layer)
193 this->tmp_state.addLayer(layer);
196 void WMLayer::removeLayerFromState(unsigned layer)
198 this->tmp_state.removeLayer(layer);
201 void WMLayer::attachAppToArea(const string& app, const string& area)
203 this->tmp_state.attachAppToArea(app, area);
206 void WMLayer::appendArea(const string& area)
208 this->area_list.push_back(area);
211 void WMLayer::appTerminated(unsigned id)
213 auto fwd_itr = std::remove_if(this->id_list.begin(), this->id_list.end(),
217 this->id_list.erase(fwd_itr, this->id_list.end());
218 this->tmp_state.removeLayer(id);
219 this->state.removeLayer(id);
223 bool WMLayer::hasLayerID(unsigned id)
225 bool ret = (id >= this->idBegin() && id <= this->idEnd());
228 auto itr = std::find(this->id_list.begin(), this->id_list.end(), id);
229 return (itr != this->id_list.end()) ? true : false;
232 bool WMLayer::hasRole(const string& role)
234 auto re = std::regex(this->role_list);
235 if (std::regex_match(role, re))
237 HMI_DEBUG("role %s matches layer %s", role.c_str(), this->name.c_str());
243 void WMLayer::update()
245 this->state = this->tmp_state;
250 this->tmp_state = this->state;
255 DUMP("===== wm layer status =====");
256 DUMP("Layer :%s", this->name.c_str());
257 this->tmp_state.dump();
259 DUMP("===== wm layer status end =====");
263 /* void WMLayer::undo()
265 this->tmp_state = this->state;