docs: add yaml book
[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       role2surface(0),
76       evname2afb_event(0)
77 {
78     role2surface[role] = INVALID_SURFACE_ID;
79     for (auto x : kWMEvents)
80     {
81 #if GTEST_ENABLED
82         string ev = x;
83 #else
84         afb_event_t ev = afb_api_make_event(afbBindingV3root, x.c_str());
85 #endif
86         evname2afb_event[x] = ev;
87     }
88 }
89
90 WMClient::WMClient(const string &appid, unsigned layer, const string &role)
91     : id(appid),
92       layer(layer),
93       main_role(role),
94       role2surface(0),
95       evname2afb_event(0)
96 {
97     role2surface[role] = INVALID_SURFACE_ID;
98     for (auto x : kWMEvents)
99     {
100 #if GTEST_ENABLED
101         string ev = x;
102 #else
103         afb_event_t ev = afb_api_make_event(afbBindingV3root, x.c_str());
104 #endif
105         evname2afb_event[x] = ev;
106     }
107 }
108
109 string WMClient::appID() const
110 {
111     return this->id;
112 }
113
114 string WMClient::role() const
115 {
116     return this->main_role;
117 }
118
119 unsigned WMClient::layerID() const
120 {
121     return this->layer;
122 }
123
124 unsigned WMClient::surfaceID() const
125 {
126     return this->surface;
127 }
128
129 void WMClient::registerSurface(unsigned surface)
130 {
131     this->surface = surface;
132 }
133
134 /**
135  * Add surface to the client
136  *
137  * This function add main surface to the client(ivi_layer).
138  *
139  * @param     string[in] role
140  * @return    WMError
141  */
142 WMError WMClient::addSurface(unsigned surface)
143 {
144     this->surface = surface;
145     ilmErrorTypes err = ilm_layerAddSurface(this->layer, surface);
146
147     if(err == ILM_SUCCESS)
148     {
149         err = ilm_commitChanges();
150     }
151     return (err == ILM_SUCCESS) ? WMError::SUCCESS : WMError::FAIL;
152 }
153
154 void WMClient::setSurfaceSizeCorrectly()
155 {
156     this->is_source_set = true;
157 }
158
159 bool WMClient::isSourceSizeSet()
160 {
161     return this->is_source_set;
162 }
163
164 bool WMClient::removeSurfaceIfExist(unsigned surface)
165 {
166     bool ret = false;
167     if(surface == this->surface)
168     {
169         this->surface = INVALID_SURFACE_ID;
170         ret = true;
171     }
172     return ret;
173 }
174
175 bool WMClient::subscribe(afb_req_t req, const string &evname)
176 {
177     int ret = afb_req_subscribe(req, this->evname2afb_event[evname]);
178     if (ret)
179     {
180         HMI_DEBUG("Failed to subscribe %s", evname.c_str());
181         return false;
182     }
183     return true;
184 }
185
186 void WMClient::emitVisible(bool visible)
187 {
188     // error check
189     bool allow_send = false;
190     if(visible)
191     {
192         allow_send = afb_event_is_valid(this->evname2afb_event[kVisible]);
193     }
194     else
195     {
196         allow_send = afb_event_is_valid(this->evname2afb_event[kInvisible]);
197     }
198     if(allow_send)
199     {
200         this->area = area;
201         json_object* j = json_object_new_object();
202         json_object_object_add(j, kKeyRole, json_object_new_string(this->role().c_str()));
203         json_object_object_add(j, kKeyDrawingName, json_object_new_string(this->role().c_str()));
204
205         if(visible)
206         {
207             afb_event_push(this->evname2afb_event[kVisible], j);
208         }
209         else
210         {
211             afb_event_push(this->evname2afb_event[kInvisible], j);
212         }
213     }
214     else
215     {
216         HMI_ERROR("Failed to send %s", __func__);
217     }
218 }
219
220 void WMClient::emitActive(bool active)
221 {
222     // error check
223     bool allow_send = false;
224     if(active)
225     {
226         allow_send = afb_event_is_valid(this->evname2afb_event[kActive]);
227     }
228     else
229     {
230         allow_send = afb_event_is_valid(this->evname2afb_event[kInactive]);
231     }
232     if(allow_send)
233     {
234         this->area = area;
235         json_object* j = json_object_new_object();
236         json_object_object_add(j, kKeyRole, json_object_new_string(this->role().c_str()));
237         json_object_object_add(j, kKeyDrawingName, json_object_new_string(this->role().c_str()));
238
239         if(active)
240         {
241             afb_event_push(this->evname2afb_event[kActive], j);
242         }
243         else
244         {
245             afb_event_push(this->evname2afb_event[kInactive], j);
246         }
247     }
248     else
249     {
250         HMI_ERROR("Failed to send %s", __func__);
251     }
252 }
253
254 void WMClient::emitSyncDraw(const string& area, struct rect& r)
255 {
256     HMI_NOTICE("trace");
257     if(afb_event_is_valid(this->evname2afb_event[kSyncDraw]))
258     {
259         this->area = area;
260         json_object *j_rect = json_object_new_object();
261         json_object_object_add(j_rect, kKeyX, json_object_new_int(r.x));
262         json_object_object_add(j_rect, kKeyY, json_object_new_int(r.y));
263         json_object_object_add(j_rect, kKeyWidth, json_object_new_int(r.w));
264         json_object_object_add(j_rect, kKeyHeight, json_object_new_int(r.h));
265
266         json_object* j = json_object_new_object();
267         json_object_object_add(j, kKeyRole, json_object_new_string(this->role().c_str()));
268         json_object_object_add(j, kKeyDrawingName, json_object_new_string(this->role().c_str()));
269         json_object_object_add(j, kKeyDrawingArea, json_object_new_string(area.c_str()));
270         json_object_object_add(j, kKeyArea, json_object_new_string(this->role().c_str()));
271
272         json_object_object_add(j, kKeyDrawingRect, j_rect);
273         afb_event_push(this->evname2afb_event[kSyncDraw], j);
274     }
275     else
276     {
277         HMI_ERROR("Failed to send %s", __func__);
278     }
279 }
280
281 void WMClient::emitFlushDraw()
282 {
283     if(afb_event_is_valid(this->evname2afb_event[kFlushDraw]))
284     {
285         json_object* j = json_object_new_object();
286         json_object_object_add(j, kKeyRole, json_object_new_string(this->role().c_str()));
287         json_object_object_add(j, kKeyDrawingName, json_object_new_string(this->role().c_str()));
288         afb_event_push(this->evname2afb_event[kFlushDraw], nullptr);
289     }
290     else
291     {
292         HMI_ERROR("Failed to send %s", __func__);
293     }
294 }
295
296 void WMClient::emitError(WMError error)
297 {
298     if (!afb_event_is_valid(this->evname2afb_event[kKeyError])){
299         HMI_ERROR("event err is not valid");
300         return;
301     }
302     json_object *j = json_object_new_object();
303     json_object_object_add(j, kKeyError, json_object_new_int(error));
304     json_object_object_add(j, kKeyErrorDesc, json_object_new_string(errorDescription(error)));
305     HMI_DEBUG("error: %d, description:%s", error, errorDescription(error));
306     int ret = afb_event_push(this->evname2afb_event[kKeyError], j);
307     if (ret != 0)
308     {
309         HMI_DEBUG("afb_event_push failed: %m");
310     }
311 }
312
313 void WMClient::dumpInfo()
314 {
315     DUMP("APPID : %s", id.c_str());
316     DUMP("  LAYER : %d", layer);
317     for (const auto &x : this->role2surface)
318     {
319         DUMP("  ROLE  : %s , SURFACE : %d", x.first.c_str(), x.second);
320     }
321 }
322
323 } // namespace wm