Update wm_layer
[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<std::string, std::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<std::string, std::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 WMLayer::WMLayer(json_object* j) : before_state(), state()
63 {
64     this->name = jh::getStringFromJson(j, "name");
65     this->role_list = jh::getStringFromJson(j, "role");
66     const char* type = jh::getStringFromJson(j, "type");
67     this->id_begin = static_cast<unsigned>(jh::getIntFromJson(j, "id_range_begin"));
68     this->id_end = static_cast<unsigned>(jh::getIntFromJson(j, "id_range_end"));
69
70     if (name.size() == 0 || type || this->id_begin == 0 || this->id_end == 0)
71     {
72         HMI_ERROR("Parse Error!!");
73         exit(1);
74     }
75     if(this->id_begin > this->id_end)
76     {
77         HMI_ERROR("INVALID");
78         exit(1);
79     }
80     string str_type = type;
81     this->type = (str_type == "tile") ? MANAGEMENT_TYPE::TILE : MANAGEMENT_TYPE::STACK;
82 }
83
84 unsigned WMLayer::getNewLayerID(const string& role)
85 {
86     unsigned ret = 0;
87     auto re = std::regex(this->role_list);
88     if (std::regex_match(role, re))
89     {
90         // generate new layer id;
91         ret = this->id_list.back() + 1;
92         HMI_DEBUG("role %s matches layer %d, new layerID %d", role.c_str(), this->name.c_str(), ret);
93     }
94
95     if(ret == 0)
96     {
97         return ret;
98     }
99
100     auto id_found = std::find(id_list.begin(), id_list.end(), ret);
101     if( (ret > this->idEnd()) || (id_found != id_list.cend()) )
102     {
103         HMI_NOTICE("id %d is not available then generate new id", ret);
104         ret = 0; // reset
105         for(unsigned i = this->idBegin(); i < this->idEnd(); i++)
106         {
107             auto ret_found = std::find(id_list.begin(), id_list.end(), i);
108             if(ret_found == id_list.cend())
109             {
110                 HMI_INFO("set new id: %d", i);
111                 ret = i;
112                 break;
113             }
114         }
115     }
116
117     if(ret != 0)
118     {
119         id_list.push_back(ret);
120     }
121     else
122     {
123         HMI_ERROR("failed to get New ID");
124     }
125     return ret;
126 }
127
128 const string& WMLayer::layerName()
129 {
130     return this->name;
131 }
132
133 WMError WMLayer::setLayerState(const LayerState& l)
134 {
135     this->before_state = l;
136     return WMError::SUCCESS;
137 }
138
139 void WMLayer::appendArea(const string& area)
140 {
141     this->area_list.push_back(area);
142 }
143
144 void WMLayer::removeLayerID(unsigned id)
145 {
146     auto fwd_itr = std::remove_if(this->id_list.begin(), this->id_list.end(),
147         [id](unsigned elm) {
148             return elm == id;
149         });
150     this->id_list.erase(fwd_itr, this->id_list.end());
151 }
152
153 bool WMLayer::checkIDBelongTo(unsigned id)
154 {
155     return (id > this->idBegin() && id < this->idEnd());
156 }
157
158 /* WMError WMLayer::commitChange()
159 {
160     this->state = this->before_state;
161 }
162
163 void WMLayer::undo()
164 {
165     this->before_state = this->state;
166 }
167  */
168 } // namespace wm