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_daemon_make_event(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_daemon_make_event(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_daemon_make_event(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]);
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()));
206 afb_event_push(this->evname2afb_event[kVisible], j);
210 afb_event_push(this->evname2afb_event[kInvisible], j);
215 HMI_ERROR("Failed to send %s", __func__);
219 void WMClient::emitActive(bool active)
222 bool allow_send = false;
225 allow_send = afb_event_is_valid(this->evname2afb_event[kActive]);
229 allow_send = afb_event_is_valid(this->evname2afb_event[kInactive]);
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()));
239 afb_event_push(this->evname2afb_event[kActive], j);
243 afb_event_push(this->evname2afb_event[kInactive], j);
248 HMI_ERROR("Failed to send %s", __func__);
252 void WMClient::emitSyncDraw(const string& area, struct rect& r)
255 if(afb_event_is_valid(this->evname2afb_event[kSyncDraw]))
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));
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()));
269 json_object_object_add(j, kKeyDrawingRect, j_rect);
270 afb_event_push(this->evname2afb_event[kSyncDraw], j);
274 HMI_ERROR("Failed to send %s", __func__);
278 void WMClient::emitFlushDraw()
280 if(afb_event_is_valid(this->evname2afb_event[kFlushDraw]))
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);
289 HMI_ERROR("Failed to send %s", __func__);
293 void WMClient::emitError(WMError error)
295 if (!afb_event_is_valid(this->evname2afb_event[kKeyError])){
296 HMI_ERROR("event err is not valid");
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);
306 HMI_DEBUG("afb_event_push failed: %m");
310 void WMClient::dumpInfo()
312 DUMP("APPID : %s", id.c_str());
313 DUMP(" LAYER : %d", layer);
314 for (const auto &x : this->role2surface)
316 DUMP(" ROLE : %s , SURFACE : %d", x.first.c_str(), x.second);