Fix bug
[apps/agl-service-windowmanager.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 using std::string;
22 using std::vector;
23
24 namespace wm
25 {
26
27 const vector<string> kWMEvents = {
28     // Private event for applications
29     "syncDraw", "flushDraw", "visible", "invisible", "active", "inactive", "error"};
30 const vector<string> kErrorDescription = {
31     "unknown-error"};
32
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";
37
38 WMClient::WMClient(const string &appid, unsigned layer, unsigned surface, const string &role)
39     : id(appid), layer(layer),
40       role2surface(0)
41 {
42     role2surface[role] = surface;
43     for (auto x : kWMEvents)
44     {
45 #if GTEST_ENABLED
46         string ev = x;
47 #else
48         afb_event ev = afb_daemon_make_event(x.c_str());
49 #endif
50         event2list[x] = ev;
51     }
52 }
53
54 WMClient::WMClient(const string &appid, const string &role)
55     : id(appid),
56       layer(0),
57       role2surface(0),
58       event2list(0)
59 {
60     role2surface[role] = INVALID_SURFACE_ID;
61     for (auto x : kWMEvents)
62     {
63 #if GTEST_ENABLED
64         string ev = x;
65 #else
66         afb_event ev = afb_daemon_make_event(x.c_str());
67 #endif
68         event2list[x] = ev;
69     }
70 }
71
72 WMClient::~WMClient()
73 {
74 }
75
76 string WMClient::appID() const
77 {
78     return this->id;
79 }
80
81 unsigned WMClient::surfaceID(const string &role) const
82 {
83     if (0 == this->role2surface.count(role))
84     {
85         HMI_WARNING("wm", "invalid role");
86         return INVALID_SURFACE_ID;
87     }
88     return this->role2surface.at(role);
89 }
90
91 unsigned WMClient::layerID() const
92 {
93     return this->layer;
94 }
95
96 void WMClient::registerLayer(unsigned layer)
97 {
98     this->layer = layer;
99 }
100
101 bool WMClient::addSurface(const string &role, unsigned surface)
102 {
103     HMI_DEBUG("wm", "Add role %s with surface %d", role.c_str(), surface);
104     if (0 != this->role2surface.count(role))
105     {
106         HMI_NOTICE("wm", "override surfaceID %d with %d", this->role2surface[role], surface);
107     }
108     this->role2surface[role] = surface;
109     return true;
110 }
111
112 bool WMClient::removeSurfaceIfExist(unsigned surface)
113 {
114     bool ret = false;
115     for (auto &x : this->role2surface)
116     {
117         if (surface == x.second)
118         {
119             this->role2surface.erase(x.first);
120             ret = true;
121             break;
122         }
123     }
124     return ret;
125 }
126
127 bool WMClient::removeRole(const string &role)
128 {
129     bool ret = false;
130     if (this->role2surface.count(role) != 0)
131     {
132         this->role2surface.erase(role);
133         ret = true;
134     }
135     return ret;
136 }
137
138 bool WMClient::subscribe(afb_req req, const string &evname)
139 {
140     if(evname != kKeyError){
141         HMI_DEBUG("wm", "error is only enabeled for now");
142         return false;
143     }
144     int ret = afb_req_subscribe(req, this->event2list[evname]);
145     if (ret)
146     {
147         HMI_DEBUG("wm", "Failed to subscribe %s", evname.c_str());
148         return false;
149     }
150     return true;
151 }
152
153 void WMClient::emitError(WM_CLIENT_ERROR_EVENT ev)
154 {
155     if (!afb_event_is_valid(this->event2list[kKeyError])){
156         HMI_ERROR("wm", "event err is not valid");
157         return;
158     }
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());
163
164     int ret = afb_event_push(this->event2list[kKeyError], j);
165     if (ret != 0)
166     {
167         HMI_DEBUG("wm", "afb_event_push failed: %m");
168     }
169 }
170
171 void WMClient::dumpInfo()
172 {
173     DUMP("APPID : %s", id.c_str());
174     DUMP("  LAYER : %d", layer);
175     for (const auto &x : this->role2surface)
176     {
177         DUMP("  ROLE  : %s , SURFACE : %d", x.first.c_str(), x.second);
178     }
179 }
180
181 } // namespace wm