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>
22 #define INVALID_SURFACE_ID 0
30 static const vector<string> kWMEvents = {
31 // Private event for applications
32 "syncDraw", "flushDraw", "visible", "invisible", "active", "inactive", "error"};
33 static const vector<string> kErrorDescription = {
36 static const char kKeyDrawingName[] = "drawing_name";
37 static const char kKeyrole[] = "role";
38 static const char kKeyError[] = "error";
39 static const char kKeyErrorDesc[] = "kErrorDescription";
41 WMClient::WMClient(const string &appid, unsigned layer, unsigned surface, const string &role)
42 : id(appid), layer(layer),
45 role2surface[role] = surface;
46 for (auto x : kWMEvents)
51 afb_event ev = afb_daemon_make_event(x.c_str());
53 evname2afb_event[x] = ev;
57 WMClient::WMClient(const string &appid, const string &role)
63 role2surface[role] = INVALID_SURFACE_ID;
64 for (auto x : kWMEvents)
69 afb_event ev = afb_daemon_make_event(x.c_str());
71 evname2afb_event[x] = ev;
75 WMClient::WMClient(const string &appid, unsigned layer, const string &role)
82 role2surface[role] = INVALID_SURFACE_ID;
83 for (auto x : kWMEvents)
88 afb_event ev = afb_daemon_make_event(x.c_str());
90 evname2afb_event[x] = ev;
94 string WMClient::appID() const
99 string WMClient::role() const
101 return this->main_role;
104 unsigned WMClient::layerID() const
109 unsigned WMClient::surfaceID() const
111 return this->surface;
115 * Add surface to the client
117 * This function add main surface to the client(ivi_layer).
119 * @param string[in] role
122 WMError WMClient::addSurface(unsigned surface)
124 this->surface = surface;
125 ilmErrorTypes err = ilm_layerAddSurface(this->layer, surface);
127 if(err == ILM_SUCCESS)
129 err = ilm_commitChanges();
131 return (err == ILM_SUCCESS) ? WMError::SUCCESS : WMError::FAIL;
134 bool WMClient::removeSurfaceIfExist(unsigned surface)
137 if(surface == this->surface)
139 this->surface = INVALID_SURFACE_ID;
146 bool WMClient::subscribe(afb_req req, const string &evname)
148 if(evname != kKeyError){
149 HMI_DEBUG("error is only enabeled for now");
152 int ret = afb_req_subscribe(req, this->evname2afb_event[evname]);
155 HMI_DEBUG("Failed to subscribe %s", evname.c_str());
161 void WMClient::emitError(WM_CLIENT_ERROR_EVENT ev)
163 if (!afb_event_is_valid(this->evname2afb_event[kKeyError])){
164 HMI_ERROR("event err is not valid");
167 json_object *j = json_object_new_object();
168 json_object_object_add(j, kKeyError, json_object_new_int(ev));
169 json_object_object_add(j, kKeyErrorDesc, json_object_new_string(kErrorDescription[ev].c_str()));
170 HMI_DEBUG("error: %d, description:%s", ev, kErrorDescription[ev].c_str());
172 int ret = afb_event_push(this->evname2afb_event[kKeyError], j);
175 HMI_DEBUG("afb_event_push failed: %m");
180 void WMClient::dumpInfo()
182 DUMP("APPID : %s", id.c_str());
183 DUMP(" LAYER : %d", layer);
184 DUMP(" ROLE : %s , SURFACE : %d", main_role.c_str(), surface);