Remove client when the app is terminated
[apps/agl-service-windowmanager.git] / src / windowmanager-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 "windowmanager-client.hpp"
18 #include "hmi-debug.h"
19
20 #define INVALID_SURFACE_ID 0
21
22 using std::string;
23 using std::vector;
24
25 namespace wm
26 {
27
28
29 const vector<string> wm_events = {
30     // Private event for applications
31     "syncDraw", "flushDraw", "visible", "invisible", "active", "inactive", "error"};
32
33 static const char key_drawing_name[] = "drawing_name";
34 static const char key_role[] = "role";
35
36 WMClient::WMClient(const string &appid, unsigned layerID, unsigned surfaceID, const string &role)
37     : layer(layerID),
38       id(appid),
39       role2surface(0)
40 {
41     role2surface[role] = surfaceID;
42     for (auto x : wm_events)
43     {
44 #if GTEST_ENABLED
45         string ev = x;
46 #else
47         afb_event ev = afb_daemon_make_event(x.c_str());
48 #endif
49         event_list[x] = ev;
50     }
51 }
52
53 WMClient::WMClient(const string &appid, const string &role)
54     : id(appid),
55       layer(0),
56       role2surface(0),
57       event_list(0)
58 {
59     role2surface[role] = INVALID_SURFACE_ID;
60     for (auto x : wm_events)
61     {
62 #if GTEST_ENABLED
63         string ev = x;
64 #else
65         afb_event ev = afb_daemon_make_event(x.c_str());
66 #endif
67         event_list[x] = ev;
68     }
69 }
70
71 WMClient::~WMClient()
72 {
73 }
74
75 string WMClient::appID()
76 {
77     return this->id;
78 }
79
80 void WMClient::registerLayer(unsigned layerID)
81 {
82     this->layer = layerID;
83 }
84
85 bool WMClient::addSurface(const string &role, unsigned surface)
86 {
87     HMI_DEBUG("wm", "Add role %s with surface %d", role.c_str(), surface);
88     if(0 != role2surface.count(role)){
89         HMI_NOTICE("wm", "override surfaceID %d with %d", role2surface[role], surface);
90     }
91     role2surface[role] = surface;
92     return true;
93 }
94
95 bool WMClient::removeSurfaceIfExist(unsigned surfaceID){
96     bool ret = false;
97     for (auto &x : role2surface)
98     {
99         if(surfaceID == x.second){
100             role2surface.erase(x.first);
101             ret = true;
102             break;
103         }
104     }
105     return ret;
106 }
107
108 bool WMClient::removeRole(const string& role){
109     bool ret = false;
110     if (role2surface.count(role) != 0)
111     {
112         role2surface.erase(role);
113         ret = true;
114     }
115     return ret;
116 }
117
118 void WMClient::dumpInfo(){
119     DUMP("APPID : %s", id.c_str());
120     DUMP("  LAYER : %d", layer);
121     for(const auto& x : role2surface){
122         DUMP("  ROLE  : %s , SURFACE : %d", x.first.c_str(), x.second);
123     }
124 }
125
126 } // namespace wm