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"
21 #define INVALID_SURFACE_ID 0
29 static const vector<string> kWMEvents = {
30 // Private event for applications
31 "syncDraw", "flushDraw", "visible", "invisible", "active", "inactive", "error"};
32 static const vector<string> kErrorDescription = {
35 static const char kKeyDrawingName[] = "drawing_name";
36 static const char kKeyrole[] = "role";
37 static const char kKeyError[] = "error";
38 static const char kKeyErrorDesc[] = "kErrorDescription";
40 WMClient::WMClient(const string &appid, unsigned layer, unsigned surface, const string &role)
41 : id(appid), layer(layer),
44 role2surface[role] = surface;
45 for (auto x : kWMEvents)
50 afb_event ev = afb_daemon_make_event(x.c_str());
52 evname2afb_event[x] = ev;
56 WMClient::WMClient(const string &appid, const string &role)
62 role2surface[role] = INVALID_SURFACE_ID;
63 for (auto x : kWMEvents)
68 afb_event ev = afb_daemon_make_event(x.c_str());
70 evname2afb_event[x] = ev;
74 WMClient::WMClient(const string &appid, unsigned layer,
75 const string& layer_name, unsigned surface, const string &role)
76 : id(appid), layer(layer), wm_layer_name(layer_name),
79 role2surface[role] = surface;
80 for (auto x : kWMEvents)
85 afb_event ev = afb_daemon_make_event(x.c_str());
87 evname2afb_event[x] = ev;
91 string WMClient::appID() const
96 unsigned WMClient::surfaceID(const string &role) const
98 if (0 == this->role2surface.count(role))
100 return INVALID_SURFACE_ID;
102 return this->role2surface.at(role);
105 std::string WMClient::role(unsigned surface) const
107 for(const auto& x : this->role2surface)
109 if(x.second == surface)
114 return std::string("");
117 unsigned WMClient::layerID() const
122 unsigned WMClient::surfaceID() const
124 return this->surface;
127 const string& WMClient::getWMLayerName()
129 return this->wm_layer_name;
132 void WMClient::setRole(const string& role)
134 this->role_list.clear();
135 this->role_list.push_back(role);
138 void WMClient::appendRole(const string& role)
140 this->role_list.push_back(role);
144 * Add the pair of role and surface to the client
146 * This function set the pair of role and surface to the client.
147 * This function is used for the client which has multi surfaces.
148 * If the model and relationship for role and surface(layer)
149 * is changed, this function will be changed
150 * Current Window Manager doesn't use this function.
152 * @param string[in] role
153 * @param unsigned[in] surface
156 bool WMClient::addSurface(const string &role, unsigned surface)
158 HMI_DEBUG("Add role %s with surface %d", role.c_str(), surface);
159 if (0 != this->role2surface.count(role))
161 HMI_NOTICE("override surfaceID %d with %d", this->role2surface[role], surface);
163 this->role2surface[role] = surface;
167 bool WMClient::removeSurfaceIfExist(unsigned surface)
170 for (auto &x : this->role2surface)
172 if (surface == x.second)
174 HMI_INFO("Remove surface from client %s: role %s, surface: %d",
175 this->id.c_str(), x.first.c_str(), x.second);
176 this->role2surface.erase(x.first);
184 bool WMClient::removeRole(const string &role)
187 if (this->role2surface.count(role) != 0)
189 this->role2surface.erase(role);
196 bool WMClient::subscribe(afb_req req, const string &evname)
198 if(evname != kKeyError){
199 HMI_DEBUG("error is only enabeled for now");
202 int ret = afb_req_subscribe(req, this->evname2afb_event[evname]);
205 HMI_DEBUG("Failed to subscribe %s", evname.c_str());
211 void WMClient::emitError(WM_CLIENT_ERROR_EVENT ev)
213 if (!afb_event_is_valid(this->evname2afb_event[kKeyError])){
214 HMI_ERROR("event err is not valid");
217 json_object *j = json_object_new_object();
218 json_object_object_add(j, kKeyError, json_object_new_int(ev));
219 json_object_object_add(j, kKeyErrorDesc, json_object_new_string(kErrorDescription[ev].c_str()));
220 HMI_DEBUG("error: %d, description:%s", ev, kErrorDescription[ev].c_str());
222 int ret = afb_event_push(this->evname2afb_event[kKeyError], j);
225 HMI_DEBUG("afb_event_push failed: %m");
230 void WMClient::dumpInfo()
232 DUMP("APPID : %s", id.c_str());
233 DUMP(" LAYER : %d", layer);
234 for (const auto &x : this->role2surface)
236 DUMP(" ROLE : %s , SURFACE : %d", x.first.c_str(), x.second);