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>
19 #include "wm_client.hpp"
20 #include "hmi-debug.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 service2surfaces[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 service2surfaces[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()
79 string WMClient::appID() const
84 vector<unsigned> WMClient::renderOrder() const
86 return this->surface_render_order;
89 const vector<std::string>& WMClient::roles() const
91 return this->role_list;
94 unsigned WMClient::layerID() const
99 unsigned WMClient::surfaceID() const
101 return this->surface;
104 void WMClient::setRole(const string& role)
106 this->role_list.push_back(role);
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 name and surface of 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] name
135 * @param unsigned[in] surface
138 void WMClient::attachServiceSurface(const string &name, unsigned surface)
140 HMI_DEBUG("wm", "Add surface name %s with surface %d", name.c_str(), surface);
141 if (0 != this->service2surfaces.count(name))
143 HMI_NOTICE("wm", "override surfaceID %d with %d", this->service2surfaces[name], surface);
145 this->service2surfaces[name] = surface;
148 void WMClient::removeServiceSurface(const std::string &name)
150 for (auto &x : this->service2surfaces)
154 HMI_INFO("wm", "Remove surface from client %s: service surface name %s, surface: %d",
155 this->id.c_str(), x.first.c_str(), x.second);
156 this->service2surfaces.erase(name);
159 // re_asign render order
160 this->setRenderOrder(this->surface_render_order);
163 void WMClient::setArea(const std::string& area)
166 // TODO: implementation
169 void WMClient::setRenderOrder()
171 // use private surface_render_order
174 void WMClient::setRenderOrder(const vector<string> &render_order)
176 // TODO: implement LM
179 void WMClient::setRenderOrder(const vector<unsigned> &render_order)
184 bool WMClient::removeSurfaceIfExist(unsigned surface)
187 auto fwd_itr = std::remove_if(
188 this->surface_render_order.begin(), this->surface_render_order.end(),
189 [surface, &ret](unsigned x) {
193 this->surface_render_order.erase(fwd_itr, this->surface_render_order.end());
196 for(auto &y : this->service2surfaces)
198 if(y.second == surface)
200 HMI_INFO("wm", "Remove surface from client %s: role %s, surface: %d",
201 this->id.c_str(), y.first.c_str(), y.second);
202 this->service2surfaces.erase(y.first);
205 if(this->surface == surface)
207 this->surface = INVALID_SURFACE_ID;
208 // TODO: If main surface vanishes, how do we treat?
209 // Window Manager doesn't know it's means dead or on purpose.
210 // option1: remove service surface and notify to the service.
211 // option2: waiting the application calls deallocate.
212 HMI_INFO("wm", "Main surface vanishes");
218 void WMClient::removeRole(const string &role)
220 auto fwd_itr = std::remove_if(
221 this->role_list.begin(), this->role_list.end(),
225 this->role_list.erase(fwd_itr, this->role_list.end());
229 bool WMClient::subscribe(afb_req req, const string &evname)
231 if(evname != kKeyError){
232 HMI_DEBUG("wm", "error is only enabeled for now");
235 int ret = afb_req_subscribe(req, this->evname2afb_event[evname]);
238 HMI_DEBUG("wm", "Failed to subscribe %s", evname.c_str());
244 void WMClient::emitError(WM_CLIENT_ERROR_EVENT ev)
246 if (!afb_event_is_valid(this->evname2afb_event[kKeyError])){
247 HMI_ERROR("wm", "event err is not valid");
250 json_object *j = json_object_new_object();
251 json_object_object_add(j, kKeyError, json_object_new_int(ev));
252 json_object_object_add(j, kKeyErrorDesc, json_object_new_string(kErrorDescription[ev].c_str()));
253 HMI_DEBUG("wm", "error: %d, description:%s", ev, kErrorDescription[ev].c_str());
255 int ret = afb_event_push(this->evname2afb_event[kKeyError], j);
258 HMI_DEBUG("wm", "afb_event_push failed: %m");
263 void WMClient::dumpInfo()
265 DUMP("APPID : %s", this->id.c_str());
266 DUMP(" LAYER : %d", this->layer);
267 for (const auto &x : this->role_list)
269 DUMP(" ROLE : %s , SURFACE : %d", x.c_str());
271 DUMP(" MAIN SURFACE : %d", this->surface);
273 for (const auto &x : this->surface_render_order)
278 DUMP(" RENDER ORDER : %s", ro.c_str());