Bind surface properties to adding floating surface
[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::popFloatingSurface(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::popFloatingSurface(const std::string &appid, unsigned *surface)
112 {
113     HMI_ERROR("wm", "This function is not implemented");
114     return WMError::SUCCESS;
115 }
116
117 void AppList::addFloatingClient(const std::string &appid, unsigned layer, const std::string &role)
118 {
119 }
120
121 void AppList::addFloatingSurface(unsigned surface, unsigned pid)
122 {
123     struct FloatingSurface fsurface{surface, pid};
124     this->floating_surfaces.push_back(fsurface);
125 }
126
127 WMError AppList::appendRole(const std::string &id, const std::string &role, unsigned surface)
128 {
129     WMError wm_err = WMError::NO_ENTRY;
130     if (this->contains(id))
131     {
132         auto x = this->lookUpClient(id);
133         x->addSurface(role, surface);
134         wm_err = WMError::SUCCESS;
135     }
136     return wm_err;
137 }
138
139 unsigned AppList::getRequestNumber(const string &appid) const
140 {
141     for (const auto &x : this->req_list)
142     {
143         // Since app will not request twice and more, comparing appid is enough?
144         if ((x.trigger.appid == appid))
145         {
146             return x.req_num;
147         }
148     }
149     return 0;
150 }
151
152 unsigned AppList::addAllocateRequest(WMRequest req)
153 {
154     if (this->req_list.size() == 0)
155     {
156         req.req_num = current_req;
157     }
158     else
159     {
160         HMI_SEQ_DEBUG(this->current_req, "add: %d", this->req_list.back().req_num + 1);
161         req.req_num = this->req_list.back().req_num + 1;
162     }
163     this->req_list.push_back(req);
164     return req.req_num; // return 1; if you test time_expire
165 }
166
167 struct WMTrigger AppList::getRequest(unsigned req_num)
168 {
169     for (const auto &x : this->req_list)
170     {
171         if (req_num == x.req_num)
172         {
173             return x.trigger;
174         }
175     }
176 }
177
178 const vector<struct WMAction> &AppList::getActions(unsigned req_num)
179 {
180     for (auto &x : this->req_list)
181     {
182         if (req_num == x.req_num)
183         {
184             return x.sync_draw_req;
185         }
186     }
187 }
188
189 WMError AppList::setAction(unsigned req_num, const struct WMAction &action)
190 {
191     WMError result = WMError::FAIL;
192     for (auto &x : this->req_list)
193     {
194         if (req_num != x.req_num)
195         {
196             continue;
197         }
198         x.sync_draw_req.push_back(action);
199         result = WMError::SUCCESS;
200         break;
201     }
202
203     return result;
204 }
205
206 /**
207  * Note:
208  * This function set action with parameters.
209  * if visible is true, it means app should be visible, so enddraw_finished parameter should be false.
210  * otherwise (visible is false) app should be invisible. Then enddraw_finished param is set to true.
211  * This function doesn't support actions for focus yet.
212  */
213 WMError AppList::setAction(unsigned req_num, const string &appid, const string &role, const string &area, bool visible)
214 {
215     WMError result = WMError::NOT_REGISTERED;
216     for (auto &x : req_list)
217     {
218         if (req_num != x.req_num)
219         {
220             continue;
221         }
222         bool edraw_f = (visible) ? false : true;
223         WMAction action{appid, role, area, visible, edraw_f};
224
225         x.sync_draw_req.push_back(action);
226         result = WMError::SUCCESS;
227         break;
228     }
229     return result;
230 }
231
232 /**
233  * This function checks
234  *   * req_num is equal to current request number
235  *   * appid and role are equeal to the appid and role stored in action list(sync_draw_req)
236  */
237 bool AppList::setEndDrawFinished(unsigned req_num, const string &appid, const string &role)
238 {
239     bool result = false;
240     for (auto &x : req_list)
241     {
242         if (req_num < x.req_num)
243         {
244             break;
245         }
246         if (req_num == x.req_num)
247         {
248             for (auto &y : x.sync_draw_req)
249             {
250                 if (y.appid == appid && y.role == role)
251                 {
252                     y.end_draw_finished = true;
253                     result = true;
254                 }
255             }
256         }
257     }
258     this->reqDump();
259     return result;
260 }
261
262 /**
263  * @brief  check all actions of the requested sequence is finished
264  * @param  unsigned request_number
265  * @return true if all action is set.
266  */
267 bool AppList::endDrawFullfilled(unsigned req_num)
268 {
269     bool result = false;
270     for (const auto &x : req_list)
271     {
272         if (req_num < x.req_num)
273         {
274             break;
275         }
276         if (req_num == x.req_num)
277         {
278             result = true;
279             for (const auto &y : x.sync_draw_req)
280             {
281                 result &= y.end_draw_finished;
282                 if (!result)
283                 {
284                     break;
285                 }
286             }
287         }
288     }
289     return result;
290 }
291
292 void AppList::removeRequest(unsigned req_num)
293 {
294     this->req_list.erase(remove_if(this->req_list.begin(), this->req_list.end(),
295                                    [req_num](WMRequest x) {
296                                        return x.req_num == req_num;
297                                    }));
298 }
299
300 void AppList::next()
301 {
302     ++this->current_req;
303     if (0 == this->current_req)
304     {
305         this->current_req = 1;
306     }
307 }
308
309 bool AppList::haveRequest() const
310 {
311     return !this->req_list.empty();
312 }
313
314 void AppList::clientDump()
315 {
316     DUMP("======= client dump =====");
317     for (const auto &x : this->app2client)
318     {
319         const auto &y = x.second;
320         y->dumpInfo();
321     }
322     DUMP("======= client dump end=====");
323 }
324
325 void AppList::reqDump()
326 {
327     DUMP("======= req dump =====");
328     DUMP("current request : %d", current_req);
329     for (const auto &x : req_list)
330     {
331         DUMP("requested with  : %d", x.req_num);
332         DUMP("Trigger : (APPID :%s, ROLE :%s, AREA :%s, TASK: %d)",
333              x.trigger.appid.c_str(),
334              x.trigger.role.c_str(),
335              x.trigger.area.c_str(),
336              x.trigger.task);
337
338         for (const auto &y : x.sync_draw_req)
339         {
340             DUMP(
341                 "Action  : (APPID :%s, ROLE :%s, AREA :%s, END_DRAW_FINISHED: %d)",
342                 y.appid.c_str(),
343                 y.role.c_str(),
344                 y.area.c_str(),
345                 y.end_draw_finished);
346         }
347     }
348     DUMP("======= req dump end =====\n");
349 }
350 } // namespace wm