Show [Current] and [To be] in WMLayer log
[apps/agl-service-windowmanager.git] / src / wm_client.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
17 #include <json-c/json.h>
18 #include "wm_client.hpp"
19 #include <ilm/ilm_control.h>
20
21 #define INVALID_SURFACE_ID 0
22
23 using std::string;
24 using std::vector;
25
26 namespace wm
27 {
28
29 static const char kActive[] = "active";
30 static const char kInactive[] = "inactive";
31 static const char kVisible[] = "visible";
32 static const char kInvisible[] = "invisible";
33 static const char kSyncDraw[] = "syncDraw";
34 static const char kFlushDraw[] = "flushDraw";
35 static const char kKeyDrawingName[] = "drawing_name";
36 static const char kKeyDrawingArea[] = "drawing_area";
37 static const char kKeyRole[] = "role";
38 static const char kKeyArea[] = "area";
39 static const char kKeyrole[] = "role";
40 static const char kKeyError[] = "error";
41 static const char kKeyErrorDesc[] = "errorDescription";
42 static const char kKeyX[] = "x";
43 static const char kKeyY[] = "y";
44 static const char kKeyWidth[] = "width";
45 static const char kKeyHeight[] = "height";
46 static const char kKeyDrawingRect[] = "drawing-rect";
47
48 static const vector<string> kWMEvents = {
49     // Private event for applications
50     kActive, kInactive,
51     kVisible, kInvisible,
52     kSyncDraw, kFlushDraw,
53     kKeyError};
54
55 WMClient::WMClient(const string &appid, unsigned layer, unsigned surface, const string &role)
56     : id(appid), layer(layer), is_source_set(false),
57       role2surface(0)
58 {
59     role2surface[role] = surface;
60     for (auto x : kWMEvents)
61     {
62 #if GTEST_ENABLED
63         string ev = x;
64 #else
65         afb_event_t ev = afb_api_make_event(afbBindingV3root, x.c_str());
66 #endif
67         evname2afb_event[x] = ev;
68     }
69 }
70
71 WMClient::WMClient(const string &appid, const string &role)
72     : id(appid),
73       layer(0),
74       is_source_set(false),
75       is_active(false),
76       main_role(role),
77       role2surface(0),
78       evname2afb_event(0)
79 {
80     role2surface[role] = INVALID_SURFACE_ID;
81     for (auto x : kWMEvents)
82     {
83 #if GTEST_ENABLED
84         string ev = x;
85 #else
86         afb_event_t ev = afb_api_make_event(afbBindingV3root, x.c_str());
87 #endif
88         evname2afb_event[x] = ev;
89     }
90 }
91
92 WMClient::WMClient(const string &appid, unsigned layer, const string &role)
93     : id(appid),
94       layer(layer),
95       is_source_set(false),
96       is_active(false),
97       main_role(role),
98       role2surface(0),
99       evname2afb_event(0)
100 {
101     role2surface[role] = INVALID_SURFACE_ID;
102     for (auto x : kWMEvents)
103     {
104 #if GTEST_ENABLED
105         string ev = x;
106 #else
107         afb_event_t ev = afb_api_make_event(afbBindingV3root, x.c_str());
108 #endif
109         evname2afb_event[x] = ev;
110     }
111 }
112
113 string WMClient::appID() const
114 {
115     return this->id;
116 }
117
118 string WMClient::role() const
119 {
120     return this->main_role;
121 }
122
123 unsigned WMClient::layerID() const
124 {
125     return this->layer;
126 }
127
128 unsigned WMClient::surfaceID() const
129 {
130     return this->surface;
131 }
132
133 void WMClient::registerSurface(unsigned surface)
134 {
135     this->surface = surface;
136 }
137
138 void WMClient::activate()
139 {
140     if(!this->isActive())
141         this->emitActive(true); // emit when state is changed
142     this->is_active = true;
143 }
144
145 void WMClient::deactivate()
146 {
147     if(this->isActive())
148         this->emitActive(false); // emit when state is changed
149     this->is_active = false;
150 }
151
152 /**
153  * Add surface to the client
154  *
155  * This function add main surface to the client(ivi_layer).
156  *
157  * @param     string[in] role
158  * @return    WMError
159  */
160 WMError WMClient::addSurface(unsigned surface)
161 {
162     this->surface = surface;
163     ilmErrorTypes err = ilm_layerAddSurface(this->layer, surface);
164
165     if(err == ILM_SUCCESS)
166     {
167         err = ilm_commitChanges();
168     }
169     return (err == ILM_SUCCESS) ? WMError::SUCCESS : WMError::FAIL;
170 }
171
172 void WMClient::setSurfaceSizeCorrectly()
173 {
174     this->is_source_set = true;
175 }
176
177 bool WMClient::isSourceSizeSet()
178 {
179     return this->is_source_set;
180 }
181
182 bool WMClient::removeSurfaceIfExist(unsigned surface)
183 {
184     bool ret = false;
185     if(surface == this->surface)
186     {
187         this->surface = INVALID_SURFACE_ID;
188         ret = true;
189     }
190     return ret;
191 }
192
193 bool WMClient::subscribe(afb_req_t req, const string &evname)
194 {
195     int ret = afb_req_subscribe(req, this->evname2afb_event[evname]);
196     if (ret)
197     {
198         HMI_DEBUG("Failed to subscribe %s", evname.c_str());
199         return false;
200     }
201     return true;
202 }
203
204 void WMClient::emitVisible(bool visible)
205 {
206     // error check
207     bool allow_send = false;
208     if(visible)
209     {
210         allow_send = afb_event_is_valid(this->evname2afb_event[kVisible]);
211     }
212     else
213     {
214         allow_send = afb_event_is_valid(this->evname2afb_event[kInvisible]);
215     }
216     if(allow_send)
217     {
218         json_object* j = json_object_new_object();
219         json_object_object_add(j, kKeyRole, json_object_new_string(this->role().c_str()));
220         json_object_object_add(j, kKeyDrawingName, json_object_new_string(this->role().c_str()));
221
222         if(visible)
223         {
224             afb_event_push(this->evname2afb_event[kVisible], j);
225         }
226         else
227         {
228             afb_event_push(this->evname2afb_event[kInvisible], j);
229         }
230     }
231     else
232     {
233         HMI_ERROR("Failed to send %s", __func__);
234     }
235 }
236
237 void WMClient::emitActive(bool active)
238 {
239     // error check
240     bool allow_send = false;
241     if(active)
242     {
243         allow_send = afb_event_is_valid(this->evname2afb_event[kActive]);
244     }
245     else
246     {
247         allow_send = afb_event_is_valid(this->evname2afb_event[kInactive]);
248     }
249     if(allow_send)
250     {
251         json_object* j = json_object_new_object();
252         json_object_object_add(j, kKeyRole, json_object_new_string(this->role().c_str()));
253         json_object_object_add(j, kKeyDrawingName, json_object_new_string(this->role().c_str()));
254
255         if(active)
256         {
257             afb_event_push(this->evname2afb_event[kActive], j);
258         }
259         else
260         {
261             afb_event_push(this->evname2afb_event[kInactive], j);
262         }
263     }
264     else
265     {
266         HMI_ERROR("Failed to send %s", __func__);
267     }
268 }
269
270 void WMClient::emitSyncDraw(const string& area, struct rect& r)
271 {
272     HMI_NOTICE("trace");
273     if(afb_event_is_valid(this->evname2afb_event[kSyncDraw]))
274     {
275         json_object *j_rect = json_object_new_object();
276         json_object_object_add(j_rect, kKeyX, json_object_new_int(r.x));
277         json_object_object_add(j_rect, kKeyY, json_object_new_int(r.y));
278         json_object_object_add(j_rect, kKeyWidth, json_object_new_int(r.w));
279         json_object_object_add(j_rect, kKeyHeight, json_object_new_int(r.h));
280
281         json_object* j = json_object_new_object();
282         json_object_object_add(j, kKeyRole, json_object_new_string(this->role().c_str()));
283         json_object_object_add(j, kKeyDrawingName, json_object_new_string(this->role().c_str()));
284         json_object_object_add(j, kKeyDrawingArea, json_object_new_string(area.c_str()));
285         json_object_object_add(j, kKeyArea, json_object_new_string(this->role().c_str()));
286
287         json_object_object_add(j, kKeyDrawingRect, j_rect);
288         afb_event_push(this->evname2afb_event[kSyncDraw], j);
289     }
290     else
291     {
292         HMI_ERROR("Failed to send %s", __func__);
293     }
294 }
295
296 void WMClient::emitFlushDraw()
297 {
298     if(afb_event_is_valid(this->evname2afb_event[kFlushDraw]))
299     {
300         json_object* j = json_object_new_object();
301         json_object_object_add(j, kKeyRole, json_object_new_string(this->role().c_str()));
302         json_object_object_add(j, kKeyDrawingName, json_object_new_string(this->role().c_str()));
303         afb_event_push(this->evname2afb_event[kFlushDraw], nullptr);
304     }
305     else
306     {
307         HMI_ERROR("Failed to send %s", __func__);
308     }
309 }
310
311 void WMClient::emitError(WMError error)
312 {
313     if (!afb_event_is_valid(this->evname2afb_event[kKeyError])){
314         HMI_ERROR("event err is not valid");
315         return;
316     }
317     json_object *j = json_object_new_object();
318     json_object_object_add(j, kKeyError, json_object_new_int(error));
319     json_object_object_add(j, kKeyErrorDesc, json_object_new_string(errorDescription(error)));
320     HMI_DEBUG("error: %d, description:%s", error, errorDescription(error));
321     int ret = afb_event_push(this->evname2afb_event[kKeyError], j);
322     if (ret != 0)
323     {
324         HMI_DEBUG("afb_event_push failed: %m");
325     }
326 }
327
328 void WMClient::dumpInfo()
329 {
330     DUMP("APPID : %s", id.c_str());
331     DUMP("  LAYER : %d", layer);
332     for (const auto &x : this->role2surface)
333     {
334         DUMP("  ROLE  : %s , SURFACE : %d", x.first.c_str(), x.second);
335     }
336 }
337
338 } // namespace wm