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 this->render_order.push_back(layer);
69 void LayerState::removeLayer(unsigned layer)
71 auto fwd_itr = std::remove_if(
72 this->render_order.begin(), this->render_order.end(),
73 [layer](unsigned elm) {
77 this->render_order.erase(fwd_itr, this->render_order.end());
80 void LayerState::setArea(const string& app, const string& area)
82 this->area2appid[area] = app;
85 WMLayer::WMLayer(json_object* j, unsigned uuid) : tmp_state(), state(), uuid(uuid)
87 this->name = jh::getStringFromJson(j, "name");
88 this->role_list = jh::getStringFromJson(j, "role");
89 const char* type = jh::getStringFromJson(j, "type");
90 this->id_begin = static_cast<unsigned>(jh::getIntFromJson(j, "id_range_begin"));
91 this->id_end = static_cast<unsigned>(jh::getIntFromJson(j, "id_range_end"));
93 if (name.size() == 0 || !type)
95 HMI_ERROR("Parse Error!!");
98 if(this->id_begin > this->id_end)
100 HMI_ERROR("INVALID");
103 string str_type = type;
104 this->type = (str_type == "tile") ? MANAGEMENT_TYPE::TILE : MANAGEMENT_TYPE::STACK;
107 unsigned WMLayer::getNewLayerID(const string& role)
111 // generate new layer id;
112 if(this->hasRole(role))
114 if(this->id_list.size() == 0)
116 ret = this->idBegin();
117 this->id_list.push_back(ret);
121 ret = this->id_list.back() + 1;
123 HMI_INFO("Generate new id: %d", ret);
130 auto id_found = std::find(id_list.begin(), id_list.end(), ret);
131 if( (ret > this->idEnd()) || (id_found != id_list.cend()) )
133 HMI_NOTICE("id %d is not available then generate new id", ret);
135 for(unsigned i = this->idBegin(); i < this->idEnd(); i++)
137 auto ret_found = std::find(id_list.begin(), id_list.end(), i);
138 if(ret_found == id_list.cend())
140 HMI_INFO("set new id: %d", i);
149 id_list.push_back(ret);
153 HMI_ERROR("failed to get New ID");
158 const string& WMLayer::layerName()
163 WMError WMLayer::setLayerState(const LayerState& l)
166 return WMError::SUCCESS;
169 void WMLayer::addLayer(unsigned layer)
171 this->tmp_state.addLayer(layer);
174 void WMLayer::appendArea(const string& area)
176 this->area_list.push_back(area);
179 void WMLayer::removeLayerID(unsigned id)
181 auto fwd_itr = std::remove_if(this->id_list.begin(), this->id_list.end(),
185 this->id_list.erase(fwd_itr, this->id_list.end());
188 bool WMLayer::hasLayerID(unsigned id)
190 bool ret = (id > this->idBegin() && id < this->idEnd());
193 auto itr = std::find(this->id_list.begin(), this->id_list.end(), id);
194 return (itr != this->id_list.end()) ? true : false;
197 bool WMLayer::hasRole(const string& role)
199 // TODO : use virtual to avoid compare
200 if(this->name == BG_LAYER_NAME)
202 auto re = std::regex(this->role_list);
203 if (std::regex_match(role, re))
205 HMI_DEBUG("role %s matches layer %s", role.c_str(), this->name.c_str());
211 WMError WMLayer::commitChange()
213 this->state = this->tmp_state;
214 return WMError::SUCCESS;
217 /* void WMLayer::undo()
219 this->tmp_state = this->state;