79922fa7293253a48bcca76ce98f9bf276f81906
[apps/agl-service-windowmanager-2017.git] / src / wm_client.cpp
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <json-c/json.h>
18 #include "wm_client.hpp"
19 #include "hmi-debug.h"
20
21 #define INVALID_SURFACE_ID 0
22
23 using std::string;
24 using std::vector;
25
26 namespace wm
27 {
28
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 = {
33     "unknown-error"};
34
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";
39
40 WMClient::WMClient(const string &appid, unsigned layer, unsigned surface, const string &role)
41     : id(appid), layer(layer),
42       role2surface(0)
43 {
44     role2surface[role] = surface;
45     for (auto x : kWMEvents)
46     {
47 #if GTEST_ENABLED
48         string ev = x;
49 #else
50         afb_event ev = afb_daemon_make_event(x.c_str());
51 #endif
52         event2list[x] = ev;
53     }
54 }
55
56 WMClient::WMClient(const string &appid, const string &role)
57     : id(appid),
58       layer(0),
59       role2surface(0),
60       event2list(0)
61 {
62     role2surface[role] = INVALID_SURFACE_ID;
63     for (auto x : kWMEvents)
64     {
65 #if GTEST_ENABLED
66         string ev = x;
67 #else
68         afb_event ev = afb_daemon_make_event(x.c_str());
69 #endif
70         event2list[x] = ev;
71     }
72 }
73
74 WMClient::~WMClient()
75 {
76 }
77
78 string WMClient::appID() const
79 {
80     return this->id;
81 }
82
83 unsigned WMClient::surfaceID(const string &role) const
84 {
85     if (0 == this->role2surface.count(role))
86     {
87         return INVALID_SURFACE_ID;
88     }
89     return this->role2surface.at(role);
90 }
91
92 std::string WMClient::role(unsigned surface) const
93 {
94     for(const auto& x : this->role2surface)
95     {
96         if(x.second == surface)
97         {
98             return x.first;
99         }
100     }
101     return std::string("");
102 }
103
104 unsigned WMClient::layerID() const
105 {
106     return this->layer;
107 }
108
109 unsigned WMClient::surfaceID() const
110 {
111     return this->surface;
112 }
113
114 const string& WMClient::getWMLayerName()
115 {
116     return this->wm_layer_name;
117 }
118
119 void WMClient::setRole(const string& role)
120 {
121     this->role_list.push_back(role);
122 }
123
124 /**
125  * Set layerID the client belongs to
126  *
127  * This function set layerID the client belongs to.
128  * But this function may not used because the layer should be fixed at constructor.
129  * So this function will be used to change layer by some reasons.
130  *
131  * @param     unsigned[in] layerID
132  * @return    None
133  * @attention WMClient can't have multiple layer
134  */
135 void WMClient::registerLayer(unsigned layer)
136 {
137     this->layer = layer;
138 }
139
140 /**
141  * Add the pair of role and surface to the client
142  *
143  * This function set the pair of role and surface to the client.
144  * This function is used for the client which has multi surfaces.
145  * If the model and relationship for role and surface(layer)
146  * is changed, this function will be changed
147  * Current Window Manager doesn't use this function.
148  *
149  * @param     string[in] role
150  * @param     unsigned[in] surface
151  * @return    true
152  */
153 bool WMClient::addSurface(const string &role, unsigned surface)
154 {
155     HMI_DEBUG("wm", "Add role %s with surface %d", role.c_str(), surface);
156     if (0 != this->role2surface.count(role))
157     {
158         HMI_NOTICE("wm", "override surfaceID %d with %d", this->role2surface[role], surface);
159     }
160     this->role2surface[role] = surface;
161     return true;
162 }
163
164 bool WMClient::removeSurfaceIfExist(unsigned surface)
165 {
166     bool ret = false;
167     for (auto &x : this->role2surface)
168     {
169         if (surface == x.second)
170         {
171             HMI_INFO("wm", "Remove surface from client %s: role %s, surface: %d",
172                                 this->id.c_str(), x.first.c_str(), x.second);
173             this->role2surface.erase(x.first);
174             ret = true;
175             break;
176         }
177     }
178     return ret;
179 }
180
181 bool WMClient::removeRole(const string &role)
182 {
183     bool ret = false;
184     if (this->role2surface.count(role) != 0)
185     {
186         this->role2surface.erase(role);
187         ret = true;
188     }
189     return ret;
190 }
191
192 #if GTEST_ENABLED
193 bool WMClient::subscribe(afb_req req, const string &evname)
194 {
195     if(evname != kKeyError){
196         HMI_DEBUG("wm", "error is only enabeled for now");
197         return false;
198     }
199     int ret = afb_req_subscribe(req, this->event2list[evname]);
200     if (ret)
201     {
202         HMI_DEBUG("wm", "Failed to subscribe %s", evname.c_str());
203         return false;
204     }
205     return true;
206 }
207
208 void WMClient::emitError(WM_CLIENT_ERROR_EVENT ev)
209 {
210     if (!afb_event_is_valid(this->event2list[kKeyError])){
211         HMI_ERROR("wm", "event err is not valid");
212         return;
213     }
214     json_object *j = json_object_new_object();
215     json_object_object_add(j, kKeyError, json_object_new_int(ev));
216     json_object_object_add(j, kKeyErrorDesc, json_object_new_string(kErrorDescription[ev].c_str()));
217     HMI_DEBUG("wm", "error: %d, description:%s", ev, kErrorDescription[ev].c_str());
218
219     int ret = afb_event_push(this->event2list[kKeyError], j);
220     if (ret != 0)
221     {
222         HMI_DEBUG("wm", "afb_event_push failed: %m");
223     }
224 }
225 #endif
226
227 void WMClient::dumpInfo()
228 {
229     DUMP("APPID : %s", id.c_str());
230     DUMP("  LAYER : %d", layer);
231     for (const auto &x : this->role2surface)
232     {
233         DUMP("  ROLE  : %s , SURFACE : %d", x.first.c_str(), x.second);
234     }
235 }
236
237 } // namespace wm