Introduce changeAreaSize and getAreaList
[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         json_object* j = json_object_new_object();
201         json_object_object_add(j, kKeyRole, json_object_new_string(this->role().c_str()));
202         json_object_object_add(j, kKeyDrawingName, json_object_new_string(this->role().c_str()));
203
204         if(visible)
205         {
206             afb_event_push(this->evname2afb_event[kVisible], j);
207         }
208         else
209         {
210             afb_event_push(this->evname2afb_event[kInvisible], j);
211         }
212     }
213     else
214     {
215         HMI_ERROR("Failed to send %s", __func__);
216     }
217 }
218
219 void WMClient::emitActive(bool active)
220 {
221     // error check
222     bool allow_send = false;
223     if(active)
224     {
225         allow_send = afb_event_is_valid(this->evname2afb_event[kActive]);
226     }
227     else
228     {
229         allow_send = afb_event_is_valid(this->evname2afb_event[kInactive]);
230     }
231     if(allow_send)
232     {
233         json_object* j = json_object_new_object();
234         json_object_object_add(j, kKeyRole, json_object_new_string(this->role().c_str()));
235         json_object_object_add(j, kKeyDrawingName, json_object_new_string(this->role().c_str()));
236
237         if(active)
238         {
239             afb_event_push(this->evname2afb_event[kActive], j);
240         }
241         else
242         {
243             afb_event_push(this->evname2afb_event[kInactive], j);
244         }
245     }
246     else
247     {
248         HMI_ERROR("Failed to send %s", __func__);
249     }
250 }
251
252 void WMClient::emitSyncDraw(const string& area, struct rect& r)
253 {
254     HMI_NOTICE("trace");
255     if(afb_event_is_valid(this->evname2afb_event[kSyncDraw]))
256     {
257         json_object *j_rect = json_object_new_object();
258         json_object_object_add(j_rect, kKeyX, json_object_new_int(r.x));
259         json_object_object_add(j_rect, kKeyY, json_object_new_int(r.y));
260         json_object_object_add(j_rect, kKeyWidth, json_object_new_int(r.w));
261         json_object_object_add(j_rect, kKeyHeight, json_object_new_int(r.h));
262
263         json_object* j = json_object_new_object();
264         json_object_object_add(j, kKeyRole, json_object_new_string(this->role().c_str()));
265         json_object_object_add(j, kKeyDrawingName, json_object_new_string(this->role().c_str()));
266         json_object_object_add(j, kKeyDrawingArea, json_object_new_string(area.c_str()));
267         json_object_object_add(j, kKeyArea, json_object_new_string(this->role().c_str()));
268
269         json_object_object_add(j, kKeyDrawingRect, j_rect);
270         afb_event_push(this->evname2afb_event[kSyncDraw], j);
271     }
272     else
273     {
274         HMI_ERROR("Failed to send %s", __func__);
275     }
276 }
277
278 void WMClient::emitFlushDraw()
279 {
280     if(afb_event_is_valid(this->evname2afb_event[kFlushDraw]))
281     {
282         json_object* j = json_object_new_object();
283         json_object_object_add(j, kKeyRole, json_object_new_string(this->role().c_str()));
284         json_object_object_add(j, kKeyDrawingName, json_object_new_string(this->role().c_str()));
285         afb_event_push(this->evname2afb_event[kFlushDraw], nullptr);
286     }
287     else
288     {
289         HMI_ERROR("Failed to send %s", __func__);
290     }
291 }
292
293 void WMClient::emitError(WMError error)
294 {
295     if (!afb_event_is_valid(this->evname2afb_event[kKeyError])){
296         HMI_ERROR("event err is not valid");
297         return;
298     }
299     json_object *j = json_object_new_object();
300     json_object_object_add(j, kKeyError, json_object_new_int(error));
301     json_object_object_add(j, kKeyErrorDesc, json_object_new_string(errorDescription(error)));
302     HMI_DEBUG("error: %d, description:%s", error, errorDescription(error));
303     int ret = afb_event_push(this->evname2afb_event[kKeyError], j);
304     if (ret != 0)
305     {
306         HMI_DEBUG("afb_event_push failed: %m");
307     }
308 }
309
310 void WMClient::dumpInfo()
311 {
312     DUMP("APPID : %s", id.c_str());
313     DUMP("  LAYER : %d", layer);
314     for (const auto &x : this->role2surface)
315     {
316         DUMP("  ROLE  : %s , SURFACE : %d", x.first.c_str(), x.second);
317     }
318 }
319
320 } // namespace wm