Add loadLayerSetting
[apps/agl-service-windowmanager-2017.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_layer.hpp"
20 #include "json_helper.hpp"
21 #include "util.hpp"
22
23 using std::string;
24 using std::vector;
25
26 namespace wm
27 {
28
29 LayerState::LayerState()
30     :  _ivi_layer_id_list(),
31        area2ivi_layer_id()
32 {}
33
34 LayerSetting::LayerSetting(const string& name, MANAGEMENT_TYPE type, unsigned begin, unsigned end)
35     : name(name), type(type),
36      role_list(), area_list(), id_list(),
37      id_begin(begin), id_end(end)
38 {}
39
40 void LayerSetting::appendRole(const string& role)
41 {
42     this->role_list.push_back(role);
43 }
44
45 void LayerSetting::appendArea(const string& area)
46 {
47     this->area_list.push_back(area);
48 }
49
50 unsigned LayerSetting::getNewLayerID(const string& role)
51 {
52     unsigned ret = 0;
53     auto found = std::find(role_list.cbegin(), role_list.cend(), role);
54     if(found == role_list.cend())
55     {
56         return ret;
57     }
58     // generate new ivi layer id
59     ret = id_list.back() + 1;
60     HMI_INFO("generate ivi_layer_id : %d on the layer: %s", ret, this->name.c_str());
61
62     auto id_found = std::find(id_list.begin(), id_list.end(), ret);
63     if( (ret > this->idEnd()) || (id_found != id_list.cend()) )
64     {
65         HMI_NOTICE("id %d is not available then generate new id", ret);
66         ret = 0;
67         for(unsigned i = this->idBegin(); i < this->idEnd(); i++)
68         {
69             auto ret_found = std::find(id_list.begin(), id_list.end(), i);
70             if(ret_found == id_list.cend())
71             {
72                 HMI_INFO("set new id: %d", i);
73                 ret = i;
74                 break;
75             }
76         }
77     }
78
79     if(ret != 0)
80     {
81         id_list.push_back(ret);
82     }
83     else
84     {
85         HMI_ERROR("failed to get New ID");
86     }
87     return ret;
88 }
89
90 void LayerSetting::removeLayerID(unsigned id)
91 {
92     auto fwd_itr = std::remove_if(this->id_list.begin(), this->id_list.end(),
93         [id](unsigned elm) {
94             return elm == id;
95         });
96     this->id_list.erase(fwd_itr, this->id_list.end());
97 }
98
99 WMLayer::WMLayer()
100     :  before_state(),
101        state(),
102        setting{}
103 {
104     // this->setting = std::make_unique<LayerSetting>(name, type, begin, end);
105 }
106
107 WMLayer::WMLayer(json_object* j) : before_state(), state()
108 {
109     LayerSetting::MANAGEMENT_TYPE t;
110     const char* layer_name = jh::getStringFromJson(j, "name");
111     const char* role = jh::getStringFromJson(j, "role");
112     const char* type = jh::getStringFromJson(j, "type");
113     int begin = jh::getIntFromJson(j, "id_range_begin");
114     int end = jh::getIntFromJson(j, "id_range_end");
115     string name = layer_name;
116
117     if (layer_name || type || begin >=0 || end >=0)
118     {
119         HMI_ERROR("Parse Error!!");
120     }
121     if(begin > end)
122     {
123         HMI_ERROR("INVALID.");
124     }
125     string str_type = type;
126     t = (type == "tile") ? LayerSetting::TILE : LayerSetting::STACK;
127     this->setting = std::make_unique<LayerSetting>(name, t, begin, end);
128 }
129
130 unsigned WMLayer::getNewLayerID(const std::string& role)
131 {
132     return this->setting->getNewLayerID(role);
133 }
134
135 WMError WMLayer::setLayerState(const LayerState& l)
136 {
137     return WMError::SUCCESS;
138 }
139
140 bool WMLayer::checkIDBelongTo(unsigned id)
141 {
142     return (id > this->setting->idBegin() && id < this->setting->idEnd());
143 }
144
145 } // namespace wm