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