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"
20 #include <ilm/ilm_control.h>
21 #include <uuid/uuid.h>
24 #define INVALID_SURFACE_ID 0
32 static const vector<string> kWMEvents = {
33 // Private event for applications
34 "syncDraw", "flushDraw", "visible", "invisible", "active", "inactive", "error"};
35 static const vector<string> kErrorDescription = {
38 static const char kKeyDrawingName[] = "drawing_name";
39 static const char kKeyrole[] = "role";
40 static const char kKeyError[] = "error";
41 static const char kKeyErrorDesc[] = "kErrorDescription";
43 WMClient::WMClient(const string &appid, unsigned layer, unsigned surface, const string &role)
44 : id(appid), layer(layer),
47 role2surface[role] = surface;
48 for (auto x : kWMEvents)
53 afb_event ev = afb_daemon_make_event(x.c_str());
55 evname2afb_event[x] = ev;
59 WMClient::WMClient(const string &appid, const string &role)
65 role2surface[role] = INVALID_SURFACE_ID;
66 for (auto x : kWMEvents)
71 afb_event ev = afb_daemon_make_event(x.c_str());
73 evname2afb_event[x] = ev;
77 WMClient::WMClient(const string &appid, unsigned layer, const string &role)
84 role2surface[role] = INVALID_SURFACE_ID;
85 for (auto x : kWMEvents)
90 afb_event ev = afb_daemon_make_event(x.c_str());
92 evname2afb_event[x] = ev;
96 string WMClient::appID() const
101 string WMClient::role() const
103 return this->main_role;
106 unsigned WMClient::layerID() const
111 unsigned WMClient::surfaceID() const
113 return this->surface;
117 * Add surface to the client
119 * This function add main surface to the client(ivi_layer).
121 * @param string[in] role
124 WMError WMClient::addSurface(unsigned surface)
126 this->surface = surface;
127 ilmErrorTypes err = ilm_layerAddSurface(this->layer, surface);
129 if(err == ILM_SUCCESS)
131 err = ilm_commitChanges();
133 return (err == ILM_SUCCESS) ? WMError::SUCCESS : WMError::FAIL;
136 bool WMClient::removeSurfaceIfExist(unsigned surface)
139 if(surface == this->surface)
141 this->surface = INVALID_SURFACE_ID;
146 for(auto &x : this->service2surfaces)
148 if(x.second = surface)
151 string key = x.first;
152 this->service2surfaces.erase(key);
153 this->service2supplier.erase(key);
160 WMError WMClient::setRenderOrder(const vector<string> &order)
162 WMError ret = WMError::SUCCESS;
163 this->surface_render_order.clear();
164 for(const auto& x : order)
166 unsigned s; // surface
167 if(x == this->role())
169 s = this->surfaceID();
171 else if(this->service2surfaces.count(x) != 0)
173 s = this->service2surfaces[x];
177 ret = WMError::NOT_REGISTERED;
180 this->surface_render_order.push_back(s);
182 if(ret == WMError::SUCCESS)
185 t_ilm_layer* id_array = new t_ilm_surface[this->surface_render_order.size()];
186 if(id_array == nullptr)
188 HMI_WARNING("short memory");
193 for(const auto& i : this->surface_render_order)
198 ilm_layerSetRenderOrder(this->layerID(),
199 id_array, this->surface_render_order.size());
206 string WMClient::attachTmpServiceSurface(const string& supplier, const string& service_surface)
210 char out[37]; // uuid is 36 characters
211 uuid_generate_random(u);
212 uuid_unparse(u, out);
214 this->service2supplier.emplace(service_surface, supplier);
218 WMError WMClient::attachServiceSurface(const string& service_surface, unsigned surface)
220 WMError ret = WMError::NOT_REGISTERED;
221 if(this->service2supplier.count(service_surface) != 0)
223 this->service2surfaces.emplace(service_surface, surface);
224 ret = WMError::SUCCESS;
230 bool WMClient::subscribe(afb_req req, const string &evname)
232 if(evname != kKeyError){
233 HMI_DEBUG("error is only enabeled for now");
236 int ret = afb_req_subscribe(req, this->evname2afb_event[evname]);
239 HMI_DEBUG("Failed to subscribe %s", evname.c_str());
245 void WMClient::emitError(WM_CLIENT_ERROR_EVENT ev)
247 if (!afb_event_is_valid(this->evname2afb_event[kKeyError])){
248 HMI_ERROR("event err is not valid");
251 json_object *j = json_object_new_object();
252 json_object_object_add(j, kKeyError, json_object_new_int(ev));
253 json_object_object_add(j, kKeyErrorDesc, json_object_new_string(kErrorDescription[ev].c_str()));
254 HMI_DEBUG("error: %d, description:%s", ev, kErrorDescription[ev].c_str());
256 int ret = afb_event_push(this->evname2afb_event[kKeyError], j);
259 HMI_DEBUG("afb_event_push failed: %m");
264 void WMClient::dumpInfo()
266 DUMP("APPID : %s", id.c_str());
267 DUMP(" LAYER : %d", layer);
268 DUMP(" ROLE : %s , SURFACE : %d", main_role.c_str(), surface);