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 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());
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());
78 string WMClient::appID() const
83 unsigned WMClient::surfaceID(const string &role) const
85 if (0 == this->role2surface.count(role))
87 return INVALID_SURFACE_ID;
89 return this->role2surface.at(role);
92 std::string WMClient::role(unsigned surface) const
94 for(const auto& x : this->role2surface)
96 if(x.second == surface)
101 return std::string("");
104 unsigned WMClient::layerID() const
110 * Set layerID the client belongs to
112 * This function set layerID the client belongs to.
113 * But this function may not used because the layer should be fixed at constructor.
114 * So this function will be used to change layer by some reasons.
116 * @param unsigned[in] layerID
118 * @attention WMClient can't have multiple layer
120 void WMClient::registerLayer(unsigned layer)
126 * Add the pair of role and surface to the client
128 * This function set the pair of role and surface to the client.
129 * This function is used for the client which has multi surfaces.
130 * If the model and relationship for role and surface(layer)
131 * is changed, this function will be changed
132 * Current Window Manager doesn't use this function.
134 * @param string[in] role
135 * @param unsigned[in] surface
138 bool WMClient::addSurface(const string &role, unsigned surface)
140 HMI_DEBUG("wm", "Add role %s with surface %d", role.c_str(), surface);
141 if (0 != this->role2surface.count(role))
143 HMI_NOTICE("wm", "override surfaceID %d with %d", this->role2surface[role], surface);
145 this->role2surface[role] = surface;
149 bool WMClient::removeSurfaceIfExist(unsigned surface)
152 for (auto &x : this->role2surface)
154 if (surface == x.second)
156 HMI_INFO("wm", "Remove surface from client %s: role %s, surface: %d",
157 this->id.c_str(), x.first.c_str(), x.second);
158 this->role2surface.erase(x.first);
166 bool WMClient::removeRole(const string &role)
169 if (this->role2surface.count(role) != 0)
171 this->role2surface.erase(role);
177 #ifndef GTEST_ENABLED
178 bool WMClient::subscribe(afb_req req, const string &evname)
180 if(evname != kKeyError){
181 HMI_DEBUG("wm", "error is only enabeled for now");
184 int ret = afb_req_subscribe(req, this->event2list[evname]);
187 HMI_DEBUG("wm", "Failed to subscribe %s", evname.c_str());
193 void WMClient::emitError(WM_CLIENT_ERROR_EVENT ev)
195 if (!afb_event_is_valid(this->event2list[kKeyError])){
196 HMI_ERROR("wm", "event err is not valid");
199 json_object *j = json_object_new_object();
200 json_object_object_add(j, kKeyError, json_object_new_int(ev));
201 json_object_object_add(j, kKeyErrorDesc, json_object_new_string(kErrorDescription[ev].c_str()));
202 HMI_DEBUG("wm", "error: %d, description:%s", ev, kErrorDescription[ev].c_str());
204 int ret = afb_event_push(this->event2list[kKeyError], j);
207 HMI_DEBUG("wm", "afb_event_push failed: %m");
212 void WMClient::dumpInfo()
214 DUMP("APPID : %s", id.c_str());
215 DUMP(" LAYER : %d", layer);
216 for (const auto &x : this->role2surface)
218 DUMP(" ROLE : %s , SURFACE : %d", x.first.c_str(), x.second);