move layout manager task in do_allocate_resource
[apps/agl-service-windowmanager.git] / src / applist.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 #include <iostream>
17 #include <algorithm>
18 #include "applist.hpp"
19 #include "../include/hmi-debug.h"
20
21 using std::shared_ptr;
22 using std::string;
23 using std::unique_ptr;
24 using std::vector;
25
26 namespace wm {
27
28
29 AppList::AppList()
30     : req_list(0),
31       client_list(0),
32       current_seq(1)
33 {}
34
35 AppList::~AppList(){}
36
37 void AppList::addClient(const string &appid, const string &role){
38     shared_ptr<WMClient> client = std::make_shared<WMClient>(appid, role);
39     client_list[appid] = client;
40     client_dump();
41 }
42
43 void AppList::removeClient(const string &appid){
44     client_list.erase(appid);
45 }
46
47 bool AppList::contains(const string &appid){
48     auto result = client_list.find(appid);
49     return (client_list.end() != result) ? true : false;
50 }
51
52 /**
53  * @brief  get WMClient object. Before call this function, must call "contains"
54  * to check the key is contained, otherwise, you have to take care of std::out_of_range.
55  * @param string[in] application id(key)
56  * @return WMClient object
57  */
58 shared_ptr <WMClient> AppList::lookUpClient(const string &appid)
59 {
60     return client_list.at(appid);
61 }
62
63 int AppList::countClient()
64 {
65     return client_list.size();
66 }
67
68 unsigned AppList::currentSequenceNumber(){
69     return current_seq;
70 }
71
72 // Is this function necessary ?
73 unsigned AppList::getSequenceNumber(const string &appid){
74     for(const auto& x : req_list){
75         // Since app will not request twice and more, comparing appid is enough?
76         if( (x.trigger.appid == appid))
77         {
78             return x.seq_num;
79         }
80     }
81     return 0;
82 }
83
84 unsigned AppList::addAllocateRequest(WMRequest req){
85     if(req_list.size() == 0){
86         req.seq_num = current_seq;
87     }
88     else{
89         HMI_SEQ_DEBUG(current_seq, "real: %d", req_list.back().seq_num + 1);
90         req.seq_num = req_list.back().seq_num + 1;
91     }
92     req_list.push_back(req);
93     return req.seq_num; // return 1; if you test time_expire
94 }
95
96 bool AppList::requestFinished(){
97     return req_list.empty();
98 }
99
100 struct WMTrigger AppList::getRequest(unsigned request_seq){
101     for(auto& x : req_list){
102         if (request_seq == x.seq_num)
103         {
104             return x.trigger;
105         }
106     }
107 }
108
109 const vector<struct WMAction>& AppList::getActions(unsigned request_seq){
110     for (auto &x : req_list)
111     {
112         if (request_seq == x.seq_num)
113         {
114             return x.sync_draw_req;
115         }
116     }
117 }
118
119 bool AppList::setAction(unsigned request_seq, const string &appid, const string &role, const string &area){
120     bool result = false;
121     for (auto& x : req_list)
122     {
123         if (request_seq != x.seq_num)
124         {
125             continue;
126         }
127         WMAction action{appid, role, area, false};
128
129         x.sync_draw_req.push_back(action);
130         result = true;
131         break;
132     }
133     return result;
134 }
135
136 bool AppList::setEndDrawFinished(unsigned request_seq, const string &appid, const string &role){
137     bool result = false;
138     for (auto& x : req_list)
139     {
140         if (request_seq < x.seq_num)
141         {
142             break;
143         }
144         if (request_seq == x.seq_num)
145         {
146             for(auto& y : x.sync_draw_req){
147                 if (y.appid == appid && y.role == role)
148                 {
149                     y.end_draw_finished = true;
150                     result = true;
151                 }
152             }
153         }
154     }
155     req_dump();
156     return result;
157 }
158
159 /**
160  * @brief  check all actions of the requested sequence is finished
161  * @param  unsigned sequence_num
162  * @return true if all action is set.
163  */
164 bool AppList::endDrawFullfilled(unsigned request_seq){
165     bool result = false;
166     for (const auto& x : req_list)
167     {
168         if(request_seq < x.seq_num){
169             break;
170         }
171         if(request_seq == x.seq_num){
172             result = true;
173             for(const auto& y : x.sync_draw_req){
174                 result &= y.end_draw_finished;
175                 if(!result){
176                     break;
177                 }
178             }
179         }
180     }
181     return result;
182 }
183
184 void AppList::removeRequest(unsigned req_seq){
185     req_list.erase(remove_if(req_list.begin(), req_list.end(),
186         [req_seq](WMRequest x) {
187             return x.seq_num == req_seq;
188         }));
189 }
190
191 void AppList::next(){
192     ++this->current_seq;
193     if (0 == this->current_seq)
194     {
195         this->current_seq = 1;
196     }
197 }
198
199 bool AppList::haveRequest(){
200     return !req_list.empty();
201 }
202
203 void AppList::client_dump(){
204     DUMP("======= client dump =====");
205     for(const auto& x : client_list){
206         const auto& y = x.second;
207         DUMP("APPID : %s", y->appID().c_str());
208     }
209     DUMP("======= client dump end=====");
210 }
211
212 void AppList::req_dump()
213 {
214     DUMP("======= req dump =====");
215     DUMP("current request : %d", current_seq);
216     for(const auto& x : req_list){
217         DUMP("requested with  : %d", x.seq_num);
218         DUMP("Trigger : (APPID :%s, ROLE :%s, AREA :%s, TASK: %d)",
219              x.trigger.appid.c_str(),
220              x.trigger.role.c_str(),
221              x.trigger.area.c_str(),
222              x.trigger.task);
223
224         for (const auto& y : x.sync_draw_req){
225             DUMP(
226                 "Action  : (APPID :%s, ROLE :%s, AREA :%s, END_DRAW_FINISHED: %d)",
227                 y.appid.c_str(),
228                 y.role.c_str(),
229                 y.area.c_str(),
230                 y.end_draw_finished);
231         }
232     }
233     DUMP("======= req dump end =====\n");
234 }
235 }