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 "hmi-debug.h"
21 #define INVALID_SURFACE_ID 0
29 const vector<string> wm_events = {
30 // Private event for applications
31 "syncDraw", "flushDraw", "visible", "invisible", "active", "inactive", "error"};
32 const vector<string> error_description = {
35 static const char key_drawing_name[] = "drawing_name";
36 static const char key_role[] = "role";
37 static const char key_err[] = "error";
38 static const char key_err_desc[] = "error_description";
40 WMClient::WMClient(const string &appid, unsigned layerID, unsigned surfaceID, const string &role)
45 role2surface[role] = surfaceID;
46 for (auto x : wm_events)
51 afb_event ev = afb_daemon_make_event(x.c_str());
57 WMClient::WMClient(const string &appid, const string &role)
63 role2surface[role] = INVALID_SURFACE_ID;
64 for (auto x : wm_events)
69 afb_event ev = afb_daemon_make_event(x.c_str());
79 string WMClient::appID()
84 unsigned WMClient::surfaceID(const string &role)
86 if (0 == role2surface.count(role))
88 HMI_WARNING("wm", "invalid role");
89 return INVALID_SURFACE_ID;
91 return role2surface.at(role);
94 unsigned WMClient::layerID()
99 void WMClient::registerLayer(unsigned layerID)
101 this->layer = layerID;
104 bool WMClient::addSurface(const string &role, unsigned surface)
106 HMI_DEBUG("wm", "Add role %s with surface %d", role.c_str(), surface);
107 if (0 != role2surface.count(role))
109 HMI_NOTICE("wm", "override surfaceID %d with %d", role2surface[role], surface);
111 role2surface[role] = surface;
115 bool WMClient::removeSurfaceIfExist(unsigned surfaceID)
118 for (auto &x : role2surface)
120 if (surfaceID == x.second)
122 role2surface.erase(x.first);
130 bool WMClient::removeRole(const string &role)
133 if (role2surface.count(role) != 0)
135 role2surface.erase(role);
141 bool WMClient::subscribe(afb_req req, const string &evname)
143 if(evname != key_err){
144 HMI_DEBUG("wm", "error is only enabeled for now");
147 int ret = afb_req_subscribe(req, event_list[evname]);
150 HMI_DEBUG("wm", "Failed to subscribe %s", evname.c_str());
156 void WMClient::emitError(WM_CLIENT_ERROR_EVENT ev)
158 if (!afb_event_is_valid(this->event_list[key_err])){
159 HMI_ERROR("wm", "event err is not valid");
162 json_object *j = json_object_new_object();
163 json_object_object_add(j, key_err, json_object_new_int(ev));
164 json_object_object_add(j, key_err_desc, json_object_new_string(error_description[ev].c_str()));
165 HMI_DEBUG("wm", "error: %d, description:%s", ev, error_description[ev].c_str());
167 int ret = afb_event_push(this->event_list[key_err], j);
170 HMI_DEBUG("wm", "afb_event_push failed: %m");
174 void WMClient::dumpInfo()
176 DUMP("APPID : %s", id.c_str());
177 DUMP(" LAYER : %d", layer);
178 for (const auto &x : role2surface)
180 DUMP(" ROLE : %s , SURFACE : %d", x.first.c_str(), x.second);