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