aboutsummaryrefslogtreecommitdiffstats
path: root/src/wm_layer.cpp
blob: 01e8950ab686780765ca296446789e56946cae5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <regex>
#include <ilm/ilm_control.h>
#include <stdlib.h>
#include "wm_client.hpp"
#include "wm_layer.hpp"
#include "json_helper.hpp"
#include "util.hpp"

using std::string;
using std::vector;
using std::unordered_map;

#define BG_LAYER_NAME "BackGroundLayer"

namespace wm
{

LayerState::LayerState()
    :  render_order(),
       area2appid()
{}


void LayerState::attachIdToArea(const string& area, const WMClient& client)
{
    this->area2appid[area] = client.appID();
    this->render_order.push_back(client.layerID());
}

const unordered_map<string, string> LayerState::popCurrentState()
{
    unordered_map<string, string> tmp = this->area2appid;
    this->area2appid.clear();
    this->render_order.clear();
    return tmp;
}

const unordered_map<string, string> LayerState::getCurrentState()
{
    return this->area2appid;
}

const vector<unsigned> LayerState::getIviIdList()
{
    return this->render_order;
}

void LayerState::addLayer(unsigned layer)
{
    this->render_order.push_back(layer);
}

void LayerState::removeLayer(unsigned layer)
{
    auto fwd_itr = std::remove_if(
        this->render_order.begin(), this->render_order.end(),
        [layer](unsigned elm) {
            return elm == layer;
        }
    );
    this->render_order.erase(fwd_itr, this->render_order.end());
}

void LayerState::setArea(const string& app, const string& area)
{
    this->area2appid[area] = app;
}

WMLayer::WMLayer(json_object* j, unsigned uuid) : tmp_state(), state(), uuid(uuid)
{
    this->name = jh::getStringFromJson(j, "name");
    this->role_list = jh::getStringFromJson(j, "role");
    const char* type = jh::getStringFromJson(j, "type");
    this->id_begin = static_cast<unsigned>(jh::getIntFromJson(j, "id_range_begin"));
    this->id_end = static_cast<unsigned>(jh::getIntFromJson(j, "id_range_end"));

    if (name.size() == 0 || !type)
    {
        HMI_ERROR("Parse Error!!");
        exit(1);
    }
    if(this->id_begin > this->id_end)
    {
        HMI_ERROR("INVALID");
        exit(1);
    }
    string str_type = type;
    this->type = (str_type == "tile") ? MANAGEMENT_TYPE::TILE : MANAGEMENT_TYPE::STACK;
}

unsigned WMLayer::getNewLayerID(const string& role)
{
    unsigned ret = 0;

    // generate new layer id;
    if(this->hasRole(role))
    {
        if(this->id_list.size() == 0)
        {
            ret = this->idBegin();
            this->id_list.push_back(ret);
        }
        else
        {
            ret = this->id_list.back() + 1;
        }
        HMI_INFO("Generate new id: %d", ret);
    }
    else
    {
        return ret;
    }

    auto id_found = std::find(id_list.begin(), id_list.end(), ret);
    if( (ret > this->idEnd()) || (id_found != id_list.cend()) )
    {
        HMI_NOTICE("id %d is not available then generate new id", ret);
        ret = 0; // reset
        for(unsigned i = this->idBegin(); i < this->idEnd(); i++)
        {
            auto ret_found = std::find(id_list.begin(), id_list.end(), i);
            if(ret_found == id_list.cend())
            {
                HMI_INFO("set new id: %d", i);
                ret = i;
                break;
            }
        }
    }

    if(ret != 0)
    {
        id_list.push_back(ret);
    }
    else
    {
        HMI_ERROR("failed to get New ID");
    }
    return ret;
}

const string& WMLayer::layerName()
{
    return this->name;
}

WMError WMLayer::setLayerState(const LayerState& l)
{
    this->tmp_state = l;
    return WMError::SUCCESS;
}

void WMLayer::addLayer(unsigned layer)
{
    this->tmp_state.addLayer(layer);
}

void WMLayer::appendArea(const string& area)
{
    this->area_list.push_back(area);
}

void WMLayer::removeLayerID(unsigned id)
{
    auto fwd_itr = std::remove_if(this->id_list.begin(), this->id_list.end(),
        [id](unsigned elm) {
            return elm == id;
        });
    this->id_list.erase(fwd_itr, this->id_list.end());
}

bool WMLayer::hasLayerID(unsigned id)
{
    bool ret = (id > this->idBegin() && id < this->idEnd());
    if(!ret)
        return ret;
    auto itr = std::find(this->id_list.begin(), this->id_list.end(), id);
    return (itr != this->id_list.end()) ? true : false;
}

bool WMLayer::hasRole(const string& role)
{
    // TODO : use virtual to avoid compare
    if(this->name == BG_LAYER_NAME)
        return false;
    auto re = std::regex(this->role_list);
    if (std::regex_match(role, re))
    {
        HMI_DEBUG("role %s matches layer %s", role.c_str(), this->name.c_str());
        return true;
    }
    return false;
}

WMError WMLayer::commitChange()
{
    this->state = this->tmp_state;
    return WMError::SUCCESS;
}

/* void WMLayer::undo()
{
    this->tmp_state = this->state;
}
 */
} // namespace wm