aboutsummaryrefslogtreecommitdiffstats
path: root/src/applist.cpp
blob: 64f5f64634a1eaefe9f314d58cba773994252421 (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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
 * 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 <iostream>
#include <algorithm>
#include "applist.hpp"
#include "../include/hmi-debug.h"

using std::shared_ptr;
using std::string;
using std::vector;

namespace wm
{

struct FloatingSurface{
    unsigned surface_id;
    unsigned pid;
};

AppList::AppList()
    : req_list(0),
      app2client(0),
      current_req(1)
{
}

AppList::~AppList() {}

void AppList::addClient(const string &appid, const string &role)
{
    shared_ptr<WMClient> client = std::make_shared<WMClient>(appid, role);
    this->app2client[appid] = client;
    this->clientDump();
}

void AppList::addClient(const std::string &appid, unsigned layer, unsigned surface, const std::string &role)
{
    shared_ptr<WMClient> client = std::make_shared<WMClient>(appid, layer, surface, role);
    this->app2client[appid] = client;
    this->clientDump();
}

void AppList::removeClient(const string &appid)
{
    this->app2client.erase(appid);
}

bool AppList::contains(const string &appid) const
{
    auto result = this->app2client.find(appid);
    return (this->app2client.end() != result) ? true : false;
}

void AppList::removeSurface(unsigned surface_id){
    // This function may be very slow
    bool ret = false;
    for (auto &x : this->app2client)
    {
        ret = x.second->removeSurfaceIfExist(surface_id);
        if(ret){
            HMI_DEBUG("wm", "remove surface %d from Client %s finish", surface_id, x.second->appID().c_str());
            break;
        }
    }
}

/**
 * @brief  get WMClient object. Before call this function, must call "contains"
 * to check the key is contained, otherwise, you have to take care of std::out_of_range.
 * @param string[in] application id(key)
 * @return WMClient object
 */
shared_ptr<WMClient> AppList::lookUpClient(const string &appid)
{
    return this->app2client.at(appid);
}

int AppList::countClient() const
{
    return this->app2client.size();
}

unsigned AppList::currentRequestNumber() const
{
    return this->current_req;
}

WMError AppList::lookUpFloatingSurface(unsigned pid, unsigned *surface)
{
    WMError ret = WMError::NO_ENTRY;

    for (auto itr = this->floating_surfaces.begin(); itr != this->floating_surfaces.end(); ++itr)
    {
        if(pid == itr->pid){
            *surface = itr->surface_id;
            itr = this->floating_surfaces.erase(itr);
            ret = WMError::SUCCESS;
            HMI_DEBUG("wm", "Erase surface %d", *surface);
            break;
        }
    }
    return ret;
}

WMError AppList::lookUpFloatingSurface(const std::string &appid, unsigned *surface)
{
    return WMError::SUCCESS;
}

WMError AppList::appendRole(const std::string &id, const std::string &role, unsigned surface)
{
    WMError wm_err = WMError::NO_ENTRY;
    if (this->contains(id))
    {
        auto x = this->lookUpClient(id);
        x->addSurface(role, surface);
        wm_err = WMError::SUCCESS;
    }
    return wm_err;
}

unsigned AppList::getRequestNumber(const string &appid) const
{
    for (const auto &x : this->req_list)
    {
        // Since app will not request twice and more, comparing appid is enough?
        if ((x.trigger.appid == appid))
        {
            return x.req_num;
        }
    }
    return 0;
}

unsigned AppList::addAllocateRequest(WMRequest req)
{
    if (this->req_list.size() == 0)
    {
        req.req_num = current_req;
    }
    else
    {
        HMI_SEQ_DEBUG(this->current_req, "add: %d", this->req_list.back().req_num + 1);
        req.req_num = this->req_list.back().req_num + 1;
    }
    this->req_list.push_back(req);
    return req.req_num; // return 1; if you test time_expire
}

struct WMTrigger AppList::getRequest(unsigned req_num)
{
    for (const auto &x : this->req_list)
    {
        if (req_num == x.req_num)
        {
            return x.trigger;
        }
    }
}

const vector<struct WMAction> &AppList::getActions(unsigned req_num)
{
    for (auto &x : this->req_list)
    {
        if (req_num == x.req_num)
        {
            return x.sync_draw_req;
        }
    }
}

WMError AppList::setAction(unsigned req_num, const struct WMAction &action)
{
    WMError result = WMError::FAIL;
    for (auto &x : this->req_list)
    {
        if (req_num != x.req_num)
        {
            continue;
        }
        x.sync_draw_req.push_back(action);
        result = WMError::SUCCESS;
        break;
    }

    return result;
}

/**
 * Note:
 * This function set action with parameters.
 * if visible is true, it means app should be visible, so enddraw_finished parameter should be false.
 * otherwise (visible is false) app should be invisible. Then enddraw_finished param is set to true.
 * This function doesn't support actions for focus yet.
 */
WMError AppList::setAction(unsigned req_num, const string &appid, const string &role, const string &area, bool visible)
{
    WMError result = WMError::NOT_REGISTERED;
    for (auto &x : req_list)
    {
        if (req_num != x.req_num)
        {
            continue;
        }
        bool edraw_f = (visible) ? false : true;
        WMAction action{appid, role, area, visible, edraw_f};

        x.sync_draw_req.push_back(action);
        result = WMError::SUCCESS;
        break;
    }
    return result;
}

/**
 * This function checks
 *   * req_num is equal to current request number
 *   * appid and role are equeal to the appid and role stored in action list(sync_draw_req)
 */
bool AppList::setEndDrawFinished(unsigned req_num, const string &appid, const string &role)
{
    bool result = false;
    for (auto &x : req_list)
    {
        if (req_num < x.req_num)
        {
            break;
        }
        if (req_num == x.req_num)
        {
            for (auto &y : x.sync_draw_req)
            {
                if (y.appid == appid && y.role == role)
                {
                    y.end_draw_finished = true;
                    result = true;
                }
            }
        }
    }
    this->reqDump();
    return result;
}

/**
 * @brief  check all actions of the requested sequence is finished
 * @param  unsigned request_number
 * @return true if all action is set.
 */
bool AppList::endDrawFullfilled(unsigned req_num)
{
    bool result = false;
    for (const auto &x : req_list)
    {
        if (req_num < x.req_num)
        {
            break;
        }
        if (req_num == x.req_num)
        {
            result = true;
            for (const auto &y : x.sync_draw_req)
            {
                result &= y.end_draw_finished;
                if (!result)
                {
                    break;
                }
            }
        }
    }
    return result;
}

void AppList::removeRequest(unsigned req_num)
{
    this->req_list.erase(remove_if(this->req_list.begin(), this->req_list.end(),
                                   [req_num](WMRequest x) {
                                       return x.req_num == req_num;
                                   }));
}

void AppList::next()
{
    ++this->current_req;
    if (0 == this->current_req)
    {
        this->current_req = 1;
    }
}

bool AppList::haveRequest() const
{
    return !this->req_list.empty();
}

void AppList::clientDump()
{
    DUMP("======= client dump =====");
    for (const auto &x : this->app2client)
    {
        const auto &y = x.second;
        y->dumpInfo();
    }
    DUMP("======= client dump end=====");
}

void AppList::reqDump()
{
    DUMP("======= req dump =====");
    DUMP("current request : %d", current_req);
    for (const auto &x : req_list)
    {
        DUMP("requested with  : %d", x.req_num);
        DUMP("Trigger : (APPID :%s, ROLE :%s, AREA :%s, TASK: %d)",
             x.trigger.appid.c_str(),
             x.trigger.role.c_str(),
             x.trigger.area.c_str(),
             x.trigger.task);

        for (const auto &y : x.sync_draw_req)
        {
            DUMP(
                "Action  : (APPID :%s, ROLE :%s, AREA :%s, END_DRAW_FINISHED: %d)",
                y.appid.c_str(),
                y.role.c_str(),
                y.area.c_str(),
                y.end_draw_finished);
        }
    }
    DUMP("======= req dump end =====\n");
}
} // namespace wm