2 * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <json-c/json.h>
18 #include "wm_client.hpp"
19 #include <ilm/ilm_control.h>
21 #define INVALID_SURFACE_ID 0
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";
48 static const vector<string> kWMEvents = {
49 // Private event for applications
52 kSyncDraw, kFlushDraw,
55 WMClient::WMClient(const string &appid, unsigned layer, unsigned surface, const string &role)
56 : id(appid), layer(layer), is_source_set(false),
59 role2surface[role] = surface;
60 for (auto x : kWMEvents)
65 afb_event_t ev = afb_api_make_event(afbBindingV3root, x.c_str());
67 evname2afb_event[x] = ev;
71 WMClient::WMClient(const string &appid, const string &role)
78 role2surface[role] = INVALID_SURFACE_ID;
79 for (auto x : kWMEvents)
84 afb_event_t ev = afb_api_make_event(afbBindingV3root, x.c_str());
86 evname2afb_event[x] = ev;
90 WMClient::WMClient(const string &appid, unsigned layer, const string &role)
97 role2surface[role] = INVALID_SURFACE_ID;
98 for (auto x : kWMEvents)
103 afb_event_t ev = afb_api_make_event(afbBindingV3root, x.c_str());
105 evname2afb_event[x] = ev;
109 string WMClient::appID() const
114 string WMClient::role() const
116 return this->main_role;
119 unsigned WMClient::layerID() const
124 unsigned WMClient::surfaceID() const
126 return this->surface;
129 void WMClient::registerSurface(unsigned surface)
131 this->surface = surface;
135 * Add surface to the client
137 * This function add main surface to the client(ivi_layer).
139 * @param string[in] role
142 WMError WMClient::addSurface(unsigned surface)
144 this->surface = surface;
145 ilmErrorTypes err = ilm_layerAddSurface(this->layer, surface);
147 if(err == ILM_SUCCESS)
149 err = ilm_commitChanges();
151 return (err == ILM_SUCCESS) ? WMError::SUCCESS : WMError::FAIL;
154 void WMClient::setSurfaceSizeCorrectly()
156 this->is_source_set = true;
159 bool WMClient::isSourceSizeSet()
161 return this->is_source_set;
164 bool WMClient::removeSurfaceIfExist(unsigned surface)
167 if(surface == this->surface)
169 this->surface = INVALID_SURFACE_ID;
175 bool WMClient::subscribe(afb_req_t req, const string &evname)
177 int ret = afb_req_subscribe(req, this->evname2afb_event[evname]);
180 HMI_DEBUG("Failed to subscribe %s", evname.c_str());
186 void WMClient::emitVisible(bool visible)
189 bool allow_send = false;
192 allow_send = afb_event_is_valid(this->evname2afb_event[kVisible]);
196 allow_send = afb_event_is_valid(this->evname2afb_event[kInvisible]);
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()));
207 afb_event_push(this->evname2afb_event[kVisible], j);
211 afb_event_push(this->evname2afb_event[kInvisible], j);
216 HMI_ERROR("Failed to send %s", __func__);
220 void WMClient::emitActive(bool active)
223 bool allow_send = false;
226 allow_send = afb_event_is_valid(this->evname2afb_event[kActive]);
230 allow_send = afb_event_is_valid(this->evname2afb_event[kInactive]);
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()));
241 afb_event_push(this->evname2afb_event[kActive], j);
245 afb_event_push(this->evname2afb_event[kInactive], j);
250 HMI_ERROR("Failed to send %s", __func__);
254 void WMClient::emitSyncDraw(const string& area, struct rect& r)
257 if(afb_event_is_valid(this->evname2afb_event[kSyncDraw]))
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));
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()));
272 json_object_object_add(j, kKeyDrawingRect, j_rect);
273 afb_event_push(this->evname2afb_event[kSyncDraw], j);
277 HMI_ERROR("Failed to send %s", __func__);
281 void WMClient::emitFlushDraw()
283 if(afb_event_is_valid(this->evname2afb_event[kFlushDraw]))
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);
292 HMI_ERROR("Failed to send %s", __func__);
296 void WMClient::emitError(WMError error)
298 if (!afb_event_is_valid(this->evname2afb_event[kKeyError])){
299 HMI_ERROR("event err is not valid");
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);
309 HMI_DEBUG("afb_event_push failed: %m");
313 void WMClient::dumpInfo()
315 DUMP("APPID : %s", id.c_str());
316 DUMP(" LAYER : %d", layer);
317 for (const auto &x : this->role2surface)
319 DUMP(" ROLE : %s , SURFACE : %d", x.first.c_str(), x.second);