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"
27 const vector<string> kWMEvents = {
28 // Private event for applications
29 "syncDraw", "flushDraw", "visible", "invisible", "active", "inactive", "error"};
30 const vector<string> kErrorDescription = {
33 static const char kKeyDrawingName[] = "drawing_name";
34 static const char kKeyrole[] = "role";
35 static const char kKeyError[] = "error";
36 static const char kKeyErrorDesc[] = "kErrorDescription";
38 WMClient::WMClient(const string &appid, unsigned layer, unsigned surface, const string &role)
39 : id(appid), layer(layer),
42 role2surface[role] = surface;
43 for (auto x : kWMEvents)
48 afb_event ev = afb_daemon_make_event(x.c_str());
54 WMClient::WMClient(const string &appid, const string &role)
60 role2surface[role] = INVALID_SURFACE_ID;
61 for (auto x : kWMEvents)
66 afb_event ev = afb_daemon_make_event(x.c_str());
76 string WMClient::appID() const
81 unsigned WMClient::surfaceID(const string &role) const
83 if (0 == this->role2surface.count(role))
85 HMI_WARNING("wm", "invalid role");
86 return INVALID_SURFACE_ID;
88 return this->role2surface.at(role);
91 unsigned WMClient::layerID() const
96 void WMClient::registerLayer(unsigned layer)
101 bool WMClient::addSurface(const string &role, unsigned surface)
103 HMI_DEBUG("wm", "Add role %s with surface %d", role.c_str(), surface);
104 if (0 != this->role2surface.count(role))
106 HMI_NOTICE("wm", "override surfaceID %d with %d", this->role2surface[role], surface);
108 this->role2surface[role] = surface;
112 bool WMClient::removeSurfaceIfExist(unsigned surface)
115 for (auto &x : this->role2surface)
117 if (surface == x.second)
119 this->role2surface.erase(x.first);
127 bool WMClient::removeRole(const string &role)
130 if (this->role2surface.count(role) != 0)
132 this->role2surface.erase(role);
138 bool WMClient::subscribe(afb_req req, const string &evname)
140 if(evname != kKeyError){
141 HMI_DEBUG("wm", "error is only enabeled for now");
144 int ret = afb_req_subscribe(req, this->event2list[evname]);
147 HMI_DEBUG("wm", "Failed to subscribe %s", evname.c_str());
153 void WMClient::emitError(WM_CLIENT_ERROR_EVENT ev)
155 if (!afb_event_is_valid(this->event2list[kKeyError])){
156 HMI_ERROR("wm", "event err is not valid");
159 json_object *j = json_object_new_object();
160 json_object_object_add(j, kKeyError, json_object_new_int(ev));
161 json_object_object_add(j, kKeyErrorDesc, json_object_new_string(kErrorDescription[ev].c_str()));
162 HMI_DEBUG("wm", "error: %d, description:%s", ev, kErrorDescription[ev].c_str());
164 int ret = afb_event_push(this->event2list[kKeyError], j);
167 HMI_DEBUG("wm", "afb_event_push failed: %m");
171 void WMClient::dumpInfo()
173 DUMP("APPID : %s", id.c_str());
174 DUMP(" LAYER : %d", layer);
175 for (const auto &x : this->role2surface)
177 DUMP(" ROLE : %s , SURFACE : %d", x.first.c_str(), x.second);