d74418aa1f5344a7b20a55c26a2fa46329bd49f1
[apps/agl-service-windowmanager.git] / src / wm_layer.cpp
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <regex>
18 #include <ilm/ilm_control.h>
19 #include <stdlib.h>
20 #include "wm_client.hpp"
21 #include "wm_layer.hpp"
22 #include "json_helper.hpp"
23 #include "util.hpp"
24
25 using std::string;
26 using std::vector;
27 using std::unordered_map;
28
29 namespace wm
30 {
31
32 LayerState::LayerState()
33     :  render_order(),
34        area2appid()
35 {}
36
37
38 void LayerState::attachIdToArea(const string& area, const WMClient& client)
39 {
40     this->area2appid[area] = client.appID();
41     this->render_order.push_back(client.layerID());
42 }
43
44 const unordered_map<string, string> LayerState::popCurrentState()
45 {
46     unordered_map<string, string> tmp = this->area2appid;
47     this->area2appid.clear();
48     this->render_order.clear();
49     return tmp;
50 }
51
52 const unordered_map<string, string> LayerState::getCurrentState()
53 {
54     return this->area2appid;
55 }
56
57 const vector<unsigned> LayerState::getIviIdList()
58 {
59     return this->render_order;
60 }
61
62 void LayerState::addLayer(unsigned layer)
63 {
64     this->render_order.push_back(layer);
65 }
66
67 void LayerState::removeLayer(unsigned layer)
68 {
69     auto fwd_itr = std::remove_if(
70         this->render_order.begin(), this->render_order.end(),
71         [layer](unsigned elm) {
72             return elm == layer;
73         }
74     );
75     this->render_order.erase(fwd_itr, this->render_order.end());
76 }
77
78 void LayerState::setArea(const string& app, const string& area)
79 {
80     this->area2appid[area] = app;
81 }
82
83 WMLayer::WMLayer(json_object* j) : before_state(), state()
84 {
85     this->name = jh::getStringFromJson(j, "name");
86     this->role_list = jh::getStringFromJson(j, "role");
87     const char* type = jh::getStringFromJson(j, "type");
88     this->id_begin = static_cast<unsigned>(jh::getIntFromJson(j, "id_range_begin"));
89     this->id_end = static_cast<unsigned>(jh::getIntFromJson(j, "id_range_end"));
90
91     if (name.size() == 0 || type || this->id_begin == 0 || this->id_end == 0)
92     {
93         HMI_ERROR("Parse Error!!");
94         exit(1);
95     }
96     if(this->id_begin > this->id_end)
97     {
98         HMI_ERROR("INVALID");
99         exit(1);
100     }
101     string str_type = type;
102     this->type = (str_type == "tile") ? MANAGEMENT_TYPE::TILE : MANAGEMENT_TYPE::STACK;
103 }
104
105 unsigned WMLayer::getNewLayerID(const string& role)
106 {
107     unsigned ret = 0;
108
109     // generate new layer id;
110     if(this->hasRole(role))
111     {
112         ret = this->id_list.back() + 1;
113         HMI_INFO("Generate new id: %d", ret);
114     }
115     else
116     {
117         return ret;
118     }
119
120     auto id_found = std::find(id_list.begin(), id_list.end(), ret);
121     if( (ret > this->idEnd()) || (id_found != id_list.cend()) )
122     {
123         HMI_NOTICE("id %d is not available then generate new id", ret);
124         ret = 0; // reset
125         for(unsigned i = this->idBegin(); i < this->idEnd(); i++)
126         {
127             auto ret_found = std::find(id_list.begin(), id_list.end(), i);
128             if(ret_found == id_list.cend())
129             {
130                 HMI_INFO("set new id: %d", i);
131                 ret = i;
132                 break;
133             }
134         }
135     }
136
137     if(ret != 0)
138     {
139         id_list.push_back(ret);
140     }
141     else
142     {
143         HMI_ERROR("failed to get New ID");
144     }
145     return ret;
146 }
147
148 const string& WMLayer::layerName()
149 {
150     return this->name;
151 }
152
153 WMError WMLayer::setLayerState(const LayerState& l)
154 {
155     this->before_state = l;
156     return WMError::SUCCESS;
157 }
158
159 void WMLayer::appendArea(const string& area)
160 {
161     this->area_list.push_back(area);
162 }
163
164 void WMLayer::removeLayerID(unsigned id)
165 {
166     auto fwd_itr = std::remove_if(this->id_list.begin(), this->id_list.end(),
167         [id](unsigned elm) {
168             return elm == id;
169         });
170     this->id_list.erase(fwd_itr, this->id_list.end());
171 }
172
173 bool WMLayer::hasLayerID(unsigned id)
174 {
175     bool ret = (id > this->idBegin() && id < this->idEnd());
176     if(!ret)
177         return ret;
178     auto itr = std::find(this->id_list.begin(), this->id_list.end(), id);
179     return (itr != this->id_list.end()) ? true : false;
180 }
181
182 bool WMLayer::hasRole(const string& role)
183 {
184     auto re = std::regex(this->role_list);
185     if (std::regex_match(role, re))
186     {
187         HMI_DEBUG("role %s matches layer %d", role.c_str(), this->name.c_str());
188         return true;
189     }
190     return false;
191 }
192
193 /* WMError WMLayer::commitChange()
194 {
195     this->state = this->before_state;
196 }
197
198 void WMLayer::undo()
199 {
200     this->before_state = this->state;
201 }
202  */
203 } // namespace wm