773b5c23857abcb585e812cca68cf9ea11c79476
[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
19 #include "wm_client.hpp"
20 #include "wm_layer.hpp"
21 #include "json_helper.hpp"
22 #include "util.hpp"
23
24 using std::string;
25 using std::vector;
26 using std::unordered_map;
27
28 namespace wm
29 {
30
31 LayerState::LayerState()
32     :  render_order(),
33        area2appid()
34 {}
35
36
37 void LayerState::attachIdToArea(const string& area, const WMClient& client)
38 {
39     this->area2appid[area] = client.appID();
40     this->render_order.push_back(client.layerID());
41 }
42
43 const unordered_map<std::string, std::string> LayerState::popCurrentState()
44 {
45     unordered_map<string, string> tmp = this->area2appid;
46     this->area2appid.clear();
47     this->render_order.clear();
48     return tmp;
49 }
50
51 const unordered_map<std::string, std::string> LayerState::getCurrentState()
52 {
53     return this->area2appid;
54 }
55
56 const vector<unsigned> LayerState::getIviIdList()
57 {
58     return this->render_order;
59 }
60
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)
65 {}
66
67 void LayerSetting::setRoleList(const string& role)
68 {
69     this->role_list = role;
70 }
71
72 void LayerSetting::appendArea(const string& area)
73 {
74     this->area_list.push_back(area);
75 }
76
77 unsigned LayerSetting::getNewLayerID(const string& role)
78 {
79     unsigned ret = 0;
80     auto re = std::regex(this->role_list);
81     if (std::regex_match(role, re))
82     {
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);
86     }
87
88     if(ret == 0)
89     {
90         return ret;
91     }
92
93     auto id_found = std::find(id_list.begin(), id_list.end(), ret);
94     if( (ret > this->idEnd()) || (id_found != id_list.cend()) )
95     {
96         HMI_NOTICE("id %d is not available then generate new id", ret);
97         ret = 0; // reset
98         for(unsigned i = this->idBegin(); i < this->idEnd(); i++)
99         {
100             auto ret_found = std::find(id_list.begin(), id_list.end(), i);
101             if(ret_found == id_list.cend())
102             {
103                 HMI_INFO("set new id: %d", i);
104                 ret = i;
105                 break;
106             }
107         }
108     }
109
110     if(ret != 0)
111     {
112         id_list.push_back(ret);
113     }
114     else
115     {
116         HMI_ERROR("failed to get New ID");
117     }
118     return ret;
119 }
120
121 void LayerSetting::removeLayerID(unsigned id)
122 {
123     auto fwd_itr = std::remove_if(this->id_list.begin(), this->id_list.end(),
124         [id](unsigned elm) {
125             return elm == id;
126         });
127     this->id_list.erase(fwd_itr, this->id_list.end());
128 }
129
130 WMLayer::WMLayer()
131     :  before_state(),
132        state(),
133        setting{}
134 {
135     // this->setting = std::make_unique<LayerSetting>(name, type, begin, end);
136 }
137
138 WMLayer::WMLayer(json_object* j) : before_state(), state()
139 {
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;
147
148     if (layer_name || type || begin < 0 || end < 0)
149     {
150         HMI_ERROR("Parse Error!!");
151     }
152     if(begin > end)
153     {
154         HMI_ERROR("INVALID.");
155     }
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);
160 }
161
162 unsigned WMLayer::getNewLayerID(const std::string& role)
163 {
164     return this->setting->getNewLayerID(role);
165 }
166
167 WMError WMLayer::setLayerState(const LayerState& l)
168 {
169     this->before_state = l;
170     return WMError::SUCCESS;
171 }
172
173 bool WMLayer::checkIDBelongTo(unsigned id)
174 {
175     return (id > this->setting->idBegin() && id < this->setting->idEnd());
176 }
177
178 /* WMError WMLayer::commitChange()
179 {
180     this->state = this->before_state;
181 }
182
183 void WMLayer::undo()
184 {
185     this->before_state = this->state;
186 }
187  */
188 } // namespace wm