summaryrefslogtreecommitdiffstats
path: root/demo3/common/agl-service-windowmanager/src/applist.cpp
blob: 44865f6d62479dd42655b78b0a64ffa5c61d1196 (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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
/*
 * 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 "util.hpp"

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

namespace wm
{

const static int kReserveClientSize = 100;
const static int kReserveReqSize    = 10;

/**
 * AppList Constructor.
 *
 * Reserve the container size to avoid re-allocating memory.
 *
 * @note Size should be changed according to the system.
 *       If the number of applications becomes over the size, re-allocating memory will happen.
 */
AppList::AppList()
    : req_list(),
      app2client(),
      current_req(1)
{
    this->app2client.reserve(kReserveClientSize);
    this->req_list.reserve(kReserveReqSize);
}

AppList::~AppList() {}

// =================== Client Date container API ===================

/**
 * Add Client to the list
 *
 * Add Client to the list.
 * The Client means application which has role, layer, surface
 * This function should be called once for the app.
 * Caller should take care not to be called more than once.
 *
 * @param     string[in]   Application id. This will be the key to withdraw the information.
 * @param     unsigned[in] Layer ID in which the application is
 * @param     unsigned[in] surface ID which the application has
 * @param     string[in]   Role which means what behavior the application will do.
 * @return    None
 * @attention This function should be called once for the app
 *            Caller should take care not to be called more than once.
 */
void AppList::addClient(const string &appid, unsigned layer, unsigned surface, const string &role)
{
    std::lock_guard<std::mutex> lock(this->mtx);
    shared_ptr<WMClient> client = std::make_shared<WMClient>(appid, layer, surface, role);
    this->app2client[appid] = client;
    this->clientDump();
}

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

/**
 * Remove WMClient from the list
 *
 * @param string[in] Application id. This will be the key to withdraw the information.
 */
void AppList::removeClient(const string &appid)
{
    std::lock_guard<std::mutex> lock(this->mtx);
    this->app2client.erase(appid);
    HMI_INFO("Remove client %s", appid.c_str());
}

/**
 * Check this class stores the appid.
 *
 * @param     string[in] Application id. This will be the key to withdraw the information.
 * @return    true if the class has the requested key(appid)
 */
bool AppList::contains(const string &appid) const
{
    auto result = this->app2client.find(appid);
    return (this->app2client.end() != result) ? true : false;
}

/**
 * Remove surface from client
 *
 * @param     unsigned[in] surface id.
 * @return    None
 */
void AppList::removeSurface(unsigned surface){
    // This function may be very slow
    std::lock_guard<std::mutex> lock(this->mtx);
    bool ret = false;
    for (auto &x : this->app2client)
    {
        ret = x.second->removeSurfaceIfExist(surface);
        if(ret){
            HMI_DEBUG("remove surface %d from Client %s finish",
                        surface, x.second->appID().c_str());
            break;
        }
    }

}

/**
 * Get WMClient object.
 *
 * After get the WMClient object, caller can call the client method.
 * Before call this function, caller 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
 * @attention Must call cantains to check appid is stored before this function.
 */
shared_ptr<WMClient> AppList::lookUpClient(const string &appid)
{
    if(this->app2client.count(appid) != 0)
    {
        return this->app2client.at(appid);
    }
    else
    {
        return nullptr;
    }
}

/**
 * Count Client.
 *
 * Returns the number of client stored in the list.
 *
 * @param  None
 * @return The number of client
 */
int AppList::countClient() const
{
    return this->app2client.size();
}

/**
 * Get AppID with surface and role.
 *
 * Returns AppID if found.
 *
 * @param     unsigned[in] surfaceID
 * @param     string[in]   role
 * @param     bool[in,out] AppID is found or not
 * @return    AppID
 * @attention If AppID is not found, param found will be false.
 */
/* string AppList::getAppID(unsigned surface, const string& role, bool* found) const
{
    *found = false;
    for (const auto &x : this->app2client)
    {
        if(x.second->surfaceID(role) == surface){
            *found = true;
            return x.second->appID();
        }
    }
    return string("");
} */

string AppList::getAppID(unsigned surface, bool* found) const
{
    *found = false;
    for (const auto &x : this->app2client)
    {
        if(x.second->surfaceID() == surface){
            *found = true;
            return x.second->appID();
        }
    }
    return string("");
}

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

    auto fwd_itr = std::remove_if(this->floating_surfaces.begin(), this->floating_surfaces.end(),
                                    [pid, surface, &ret](FloatingSurface x) {
                                        if(pid == x.pid){
                                            *surface = x.surface_id;
                                            ret = WMError::SUCCESS;
                                            return true;
                                        }
                                        else{
                                            return false;
                                        }
                                    });
    if (fwd_itr != this->floating_surfaces.cend())
    {
        HMI_INFO("pop floating surface: %d", *surface);
    }
    this->floating_surfaces.erase(fwd_itr, this->floating_surfaces.end());
    return ret;
}

// =================== Floating(Temporary) surface/client API ===================

// TODO: After testing setRole, remove these API

WMError AppList::popFloatingSurface(const string &appid, unsigned *surface)
{
    HMI_ERROR("This function is not implemented");
    return WMError::SUCCESS;
}

void AppList::addFloatingClient(const string &appid, unsigned layer, const string &role)
{
}

void AppList::addFloatingSurface(const string &appid, unsigned surface, unsigned pid)
{
    struct FloatingSurface fsurface{appid, surface, pid};
    this->floating_surfaces.push_back(fsurface);
    this->dumpFloatingSurfaces();
}

void AppList::removeFloatingSurface(unsigned surface)
{
    this->dumpFloatingSurfaces();
    auto fwd_itr = std::remove_if(
        this->floating_surfaces.begin(), this->floating_surfaces.end(),
        [surface](FloatingSurface x) {
            return x.surface_id == surface;
        });
    if(fwd_itr != this->floating_surfaces.cend()){
        HMI_INFO("remove floating surface: %d", surface);
    }
    this->floating_surfaces.erase(fwd_itr, this->floating_surfaces.end());
}

// =================== Request Date container API ===================

/**
 * Get current request number
 *
 * Request number is the numeric ID to designate the request.
 * But returned request number from this function doesn't mean the request exists.
 * This number is used as key to withdraw the WMRequest object.
 *
 * @param  None
 * @return current request number.
 * @note   request number is more than 0.
 */
unsigned AppList::currentRequestNumber() const
{
    return this->current_req;
}

/**
 * Get request number
 *
 * Request number is the numeric ID to designate the request.
 * But returned request number from this function doesn't mean the request exists.
 * This number is used as key to withdraw the WMRequest object.
 *
 * @param     None
 * @return    request number.
 * @attention If returned value is 0, no request exists.
 */
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;
}

/**
 * Add Request
 *
 * Request number is the numeric ID to designate the request.
 * But returned request number from this function doesn't mean the request exists.
 * This number is used as key to withdraw the WMRequest object.
 *
 * @param     WMRequest[in] WMRequest object caller creates
 * @return    Request number
 * @attention If the request number is different with curent request number,
 *            it means the previous request is not finished.
 */
unsigned AppList::addRequest(WMRequest req)
{
    std::lock_guard<std::mutex> lock(this->mtx);
    if (this->req_list.size() == 0)
    {
        req.req_num = current_req;
    }
    else
    {
        HMI_SEQ_INFO(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;
}

/**
 * Get trigger which the application requests
 *
 * WMTrigger contains which application requests what role and where to put(area) and task.
 * This is used for input event to Window Policy Manager(state machine).
 *
 * @param     unsigned[in] request number
 * @param     bool[in,out] Check request number of the parameter is valid.
 * @return    WMTrigger which associates with the request number
 * @attention If the request number is not valid, parameter "found" is false
 *            and return value will be meaningless value.
 *            Caller can check the request parameter is valid.
 */
struct WMTrigger AppList::getRequest(unsigned req_num, bool *found)
{
    *found = false;
    for (const auto &x : this->req_list)
    {
        if (req_num == x.req_num)
        {
            *found = true;
            return x.trigger;
        }
    }
    HMI_SEQ_ERROR(req_num, "Couldn't get request : %d", req_num);
    return WMTrigger{"", "", "", Task::TASK_INVALID};
}

/**
 * Get actions which the application requests
 *
 * WMAciton contains the information of state transition.
 * In other words, it contains actions of Window Manager,
 * which role should be put to the area.
 *
 * @param     unsigned[in] request number
 * @param     bool[in,out] Check request number of the parameter is valid.
 * @return    WMTrigger which associates with the request number
 * @attention If the request number is not valid, parameter "found" is false
 *            and return value will be no reference pointer.
 *            Caller must check the request parameter is valid.
 */
const vector<struct WMAction> &AppList::getActions(unsigned req_num, bool* found)
{
    *found = false;
    for (auto &x : this->req_list)
    {
        if (req_num == x.req_num)
        {
            *found = true;
            return x.sync_draw_req;
        }
    }
    HMI_SEQ_ERROR(req_num, "Couldn't get action with the request : %d", req_num);
}

/**
 * Set actions to the request.
 *
 * Add actions to the request.
 * This function can be called many times, and actions increase.
 * This function is used for decision of actions of Window Manager
 * according to the result of Window Policy Manager.
 *
 * @param     unsigned[in] request number
 * @param     WMAction[in] Action of Window Manager.
 * @return    WMError If request number is not valid, FAIL will be returned.
 */
WMError AppList::setAction(unsigned req_num, const struct WMAction &action)
{
    std::lock_guard<std::mutex> lock(this->mtx);
    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:
 * @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.
 */
/**
 * Set actions to the request.
 *
 * This function is overload function.
 * The feature is same as other one.
 *
 * @param     unsigned[in] request number
 * @param     string[in]   application id
 * @param     string[in]   role
 * @param     string[in]   area
 * @param     Task[in]     the role should be visible or not.
 * @return    WMError If request number is not valid, FAIL will be returned.
 * @attention This function set action with parameters, then caller doesn't need to create WMAction object.
 *            If visible is true, it means app should be visible, so enddraw_finished parameter will 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, shared_ptr<WMClient> client, const string &role, const string &area, TaskVisible visible)
{
    std::lock_guard<std::mutex> lock(this->mtx);
    WMError result = WMError::FAIL;
    for (auto &x : req_list)
    {
        if (req_num != x.req_num)
        {
            continue;
        }
        // If visible task is not invisible, redraw is required -> true
        bool edraw_f = (visible != TaskVisible::INVISIBLE) ? false : true;
        WMAction action{req_num, client, role, area, visible, edraw_f, TaskCarState::NO_TASK};

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

/**
 * Set end_draw_finished param is true
 *
 * 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
 * If it is valid, set the action is finished.
 *
 * @param  unsigned[in] request number
 * @param  string[in]   application id
 * @param  string[in]   role
 * @return If the parameters are not valid in action list, returns false
 */
bool AppList::setEndDrawFinished(unsigned req_num, const string &appid, const string &role)
{
    std::lock_guard<std::mutex> lock(this->mtx);
    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 (nullptr != y.client)
                {
                    if (y.client->appID() == appid && y.role == role)
                    {
                        HMI_SEQ_INFO(req_num, "Role %s finish redraw", y.role.c_str());
                        y.end_draw_finished = true;
                        result = true;
                    }
                }
                else
                {
                    if (y.role == role)
                    {
                        HMI_SEQ_INFO(req_num, "Role %s finish redraw", y.role.c_str());
                        y.end_draw_finished = true;
                        result = true;
                    }
                }
            }
        }
    }
    this->reqDump();
    return result;
}

/**
 * Check all actions of the requested sequence is finished
 *
 * @param  unsigned[in] 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;
}

/**
 * Finish the request, then remove it.
 *
 * @param  unsigned[in] request_number
 * @return None
 * @note   Please call next after this function to receive or process next request.
 */
void AppList::removeRequest(unsigned req_num)
{
    std::lock_guard<std::mutex> lock(this->mtx);
    this->req_list.erase(remove_if(this->req_list.begin(), this->req_list.end(),
                                   [req_num](WMRequest x) {
                                       return x.req_num == req_num;
                                   }));
}

/**
 * Move the current request to next
 *
 * @param  None
 * @return None
 */
void AppList::next()
{
    std::lock_guard<std::mutex> lock(this->mtx);
    ++this->current_req;
    if (0 == this->current_req)
    {
        this->current_req = 1;
    }
}

/**
 * Check the request exists is in request list
 *
 * @param  None
 * @return true if WMRequest exists in the request list
 */
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       : %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, VISIBLE : %s, END_DRAW_FINISHED: %d)",
                (y.client) ? y.client->appID().c_str() : "-",
                y.role.c_str(),
                y.area.c_str(),
                (y.visible == TaskVisible::INVISIBLE) ? "invisible" : "visible",
                y.end_draw_finished);
        }
    }
    DUMP("======= req dump end =====");
}

void AppList::dumpFloatingSurfaces()
{
    DUMP("======= floating surface dump =====");
    for (const auto &x : this->floating_surfaces)
    {
        DUMP("surface : %d, pid : %d", x.surface_id, x.pid);
    }
    DUMP("======= floating surface dump end =====\n");
}

} // namespace wm