Add new function addFloatingClient function
[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::vector;
24
25 namespace wm
26 {
27
28 AppList::AppList()
29     : req_list(0),
30       app2client(0),
31       current_req(1)
32 {
33 }
34
35 void AppList::addClient(const string &appid, const string &role)
36 {
37     shared_ptr<WMClient> client = std::make_shared<WMClient>(appid, role);
38     this->app2client[appid] = client;
39     this->clientDump();
40 }
41
42 void AppList::addClient(const std::string &appid, unsigned layer, unsigned surface, const std::string &role)
43 {
44     shared_ptr<WMClient> client = std::make_shared<WMClient>(appid, layer, surface, role);
45     this->app2client[appid] = client;
46     this->clientDump();
47 }
48
49 void AppList::removeClient(const string &appid)
50 {
51     this->app2client.erase(appid);
52 }
53
54 bool AppList::contains(const string &appid) const
55 {
56     auto result = this->app2client.find(appid);
57     return (this->app2client.end() != result) ? true : false;
58 }
59
60 void AppList::removeSurface(unsigned surface_id){
61     // This function may be very slow
62     bool ret = false;
63     for (auto &x : this->app2client)
64     {
65         ret = x.second->removeSurfaceIfExist(surface_id);
66         if(ret){
67             HMI_DEBUG("wm", "remove surface %d from Client %s finish", surface_id, x.second->appID().c_str());
68             break;
69         }
70     }
71 }
72
73 /**
74  * @brief  get WMClient object. Before call this function, must call "contains"
75  * to check the key is contained, otherwise, you have to take care of std::out_of_range.
76  * @param string[in] application id(key)
77  * @return WMClient object
78  */
79 shared_ptr<WMClient> AppList::lookUpClient(const string &appid)
80 {
81     return this->app2client.at(appid);
82 }
83
84 int AppList::countClient() const
85 {
86     return this->app2client.size();
87 }
88
89 unsigned AppList::currentRequestNumber() const
90 {
91     return this->current_req;
92 }
93
94 WMError AppList::lookUpFloatingSurface(unsigned pid, unsigned *surface)
95 {
96     WMError ret = WMError::NO_ENTRY;
97
98     for (auto itr = this->floating_surfaces.begin(); itr != this->floating_surfaces.end(); ++itr)
99     {
100         if(pid == itr->pid){
101             *surface = itr->surface_id;
102             itr = this->floating_surfaces.erase(itr);
103             ret = WMError::SUCCESS;
104             HMI_DEBUG("wm", "Erase surface %d", *surface);
105             break;
106         }
107     }
108     return ret;
109 }
110
111 WMError AppList::lookUpFloatingSurface(const std::string &appid, unsigned *surface)
112 {
113     HMI_ERROR("wm", "This function is not implemented");
114     return WMError::SUCCESS;
115 }
116
117 void addFloatingClient(const std::string &appid, unsigned layer, const std::string &role)
118 {
119 }
120
121 WMError AppList::appendRole(const std::string &id, const std::string &role, unsigned surface)
122 {
123     WMError wm_err = WMError::NO_ENTRY;
124     if (this->contains(id))
125     {
126         auto x = this->lookUpClient(id);
127         x->addSurface(role, surface);
128         wm_err = WMError::SUCCESS;
129     }
130     return wm_err;
131 }
132
133 unsigned AppList::getRequestNumber(const string &appid) const
134 {
135     for (const auto &x : this->req_list)
136     {
137         // Since app will not request twice and more, comparing appid is enough?
138         if ((x.trigger.appid == appid))
139         {
140             return x.req_num;
141         }
142     }
143     return 0;
144 }
145
146 unsigned AppList::addAllocateRequest(WMRequest req)
147 {
148     if (this->req_list.size() == 0)
149     {
150         req.req_num = current_req;
151     }
152     else
153     {
154         HMI_SEQ_DEBUG(this->current_req, "add: %d", this->req_list.back().req_num + 1);
155         req.req_num = this->req_list.back().req_num + 1;
156     }
157     this->req_list.push_back(req);
158     return req.req_num; // return 1; if you test time_expire
159 }
160
161 struct WMTrigger AppList::getRequest(unsigned req_num)
162 {
163     for (const auto &x : this->req_list)
164     {
165         if (req_num == x.req_num)
166         {
167             return x.trigger;
168         }
169     }
170 }
171
172 const vector<struct WMAction> &AppList::getActions(unsigned req_num)
173 {
174     for (auto &x : this->req_list)
175     {
176         if (req_num == x.req_num)
177         {
178             return x.sync_draw_req;
179         }
180     }
181 }
182
183 WMError AppList::setAction(unsigned req_num, const struct WMAction &action)
184 {
185     WMError result = WMError::FAIL;
186     for (auto &x : this->req_list)
187     {
188         if (req_num != x.req_num)
189         {
190             continue;
191         }
192         x.sync_draw_req.push_back(action);
193         result = WMError::SUCCESS;
194         break;
195     }
196
197     return result;
198 }
199
200 /**
201  * Note:
202  * This function set action with parameters.
203  * if visible is true, it means app should be visible, so enddraw_finished parameter should be false.
204  * otherwise (visible is false) app should be invisible. Then enddraw_finished param is set to true.
205  * This function doesn't support actions for focus yet.
206  */
207 WMError AppList::setAction(unsigned req_num, const string &appid, const string &role, const string &area, bool visible)
208 {
209     WMError result = WMError::NOT_REGISTERED;
210     for (auto &x : req_list)
211     {
212         if (req_num != x.req_num)
213         {
214             continue;
215         }
216         bool edraw_f = (visible) ? false : true;
217         WMAction action{appid, role, area, visible, edraw_f};
218
219         x.sync_draw_req.push_back(action);
220         result = WMError::SUCCESS;
221         break;
222     }
223     return result;
224 }
225
226 /**
227  * This function checks
228  *   * req_num is equal to current request number
229  *   * appid and role are equeal to the appid and role stored in action list(sync_draw_req)
230  */
231 bool AppList::setEndDrawFinished(unsigned req_num, const string &appid, const string &role)
232 {
233     bool result = false;
234     for (auto &x : req_list)
235     {
236         if (req_num < x.req_num)
237         {
238             break;
239         }
240         if (req_num == x.req_num)
241         {
242             for (auto &y : x.sync_draw_req)
243             {
244                 if (y.appid == appid && y.role == role)
245                 {
246                     y.end_draw_finished = true;
247                     result = true;
248                 }
249             }
250         }
251     }
252     this->reqDump();
253     return result;
254 }
255
256 /**
257  * @brief  check all actions of the requested sequence is finished
258  * @param  unsigned request_number
259  * @return true if all action is set.
260  */
261 bool AppList::endDrawFullfilled(unsigned req_num)
262 {
263     bool result = false;
264     for (const auto &x : req_list)
265     {
266         if (req_num < x.req_num)
267         {
268             break;
269         }
270         if (req_num == x.req_num)
271         {
272             result = true;
273             for (const auto &y : x.sync_draw_req)
274             {
275                 result &= y.end_draw_finished;
276                 if (!result)
277                 {
278                     break;
279                 }
280             }
281         }
282     }
283     return result;
284 }
285
286 void AppList::removeRequest(unsigned req_num)
287 {
288     this->req_list.erase(remove_if(this->req_list.begin(), this->req_list.end(),
289                                    [req_num](WMRequest x) {
290                                        return x.req_num == req_num;
291                                    }));
292 }
293
294 void AppList::next()
295 {
296     ++this->current_req;
297     if (0 == this->current_req)
298     {
299         this->current_req = 1;
300     }
301 }
302
303 bool AppList::haveRequest() const
304 {
305     return !this->req_list.empty();
306 }
307
308 void AppList::clientDump()
309 {
310     DUMP("======= client dump =====");
311     for (const auto &x : this->app2client)
312     {
313         const auto &y = x.second;
314         y->dumpInfo();
315     }
316     DUMP("======= client dump end=====");
317 }
318
319 void AppList::reqDump()
320 {
321     DUMP("======= req dump =====");
322     DUMP("current request : %d", current_req);
323     for (const auto &x : req_list)
324     {
325         DUMP("requested with  : %d", x.req_num);
326         DUMP("Trigger : (APPID :%s, ROLE :%s, AREA :%s, TASK: %d)",
327              x.trigger.appid.c_str(),
328              x.trigger.role.c_str(),
329              x.trigger.area.c_str(),
330              x.trigger.task);
331
332         for (const auto &y : x.sync_draw_req)
333         {
334             DUMP(
335                 "Action  : (APPID :%s, ROLE :%s, AREA :%s, END_DRAW_FINISHED: %d)",
336                 y.appid.c_str(),
337                 y.role.c_str(),
338                 y.area.c_str(),
339                 y.end_draw_finished);
340         }
341     }
342     DUMP("======= req dump end =====\n");
343 }
344 } // namespace wm