Fix top surface becomes invisible when background surface is crashed.
[apps/agl-service-windowmanager.git] / src / window_manager.hpp
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
3  * Copyright (c) 2018 Konsulko Group
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #ifndef WINDOW_MANAGER_HPP
19 #define WINDOW_MANAGER_HPP
20
21 #include <atomic>
22 #include <memory>
23 #include <unordered_map>
24 #include <experimental/optional>
25 #include "result.hpp"
26 #include "pm_wrapper.hpp"
27 #include "util.hpp"
28 #include "request.hpp"
29 #include "wm_error.hpp"
30 #include "wm_layer_control.hpp"
31 extern "C"
32 {
33 #include <afb/afb-binding.h>
34 }
35
36 struct json_object;
37
38 namespace wm
39 {
40
41 using std::experimental::optional;
42
43 /* DrawingArea name used by "{layout}.{area}" */
44 extern const char kNameLayoutNormal[];
45 extern const char kNameLayoutSplit[];
46 extern const char kNameAreaFull[];
47 extern const char kNameAreaMain[];
48 extern const char kNameAreaSub[];
49
50 /* Key for json obejct */
51 extern const char kKeyDrawingName[];
52 extern const char kKeyDrawingArea[];
53 extern const char kKeyDrawingRect[];
54 extern const char kKeyX[];
55 extern const char kKeyY[];
56 extern const char kKeyWidth[];
57 extern const char kKeyHeigh[];
58 extern const char kKeyWidthPixel[];
59 extern const char kKeyHeightPixel[];
60 extern const char kKeyWidthMm[];
61 extern const char kKeyHeightMm[];
62 extern const char kKeyScale[];
63 extern const char kKeyIds[];
64
65 struct id_allocator
66 {
67     unsigned next = 1;
68     // Surfaces that where requested but not yet created
69     std::unordered_map<unsigned, std::string> id2name;
70     std::unordered_map<std::string, unsigned> name2id;
71
72     id_allocator(id_allocator const &) = delete;
73     id_allocator(id_allocator &&) = delete;
74     id_allocator &operator=(id_allocator const &);
75     id_allocator &operator=(id_allocator &&) = delete;
76
77     // Insert and return a new ID
78     unsigned generate_id(std::string const &name)
79     {
80         unsigned sid = this->next++;
81         this->id2name[sid] = name;
82         this->name2id[name] = sid;
83         HMI_DEBUG("allocated new id %u with name %s", sid, name.c_str());
84         return sid;
85     }
86
87     // Insert a new ID which defined outside
88     void register_name_id(std::string const &name, unsigned sid)
89     {
90         this->id2name[sid] = name;
91         this->name2id[name] = sid;
92         HMI_DEBUG("register id %u with name %s", sid, name.c_str());
93         return;
94     }
95
96     // Lookup by ID or by name
97     optional<unsigned> lookup(std::string const &name) const
98     {
99         auto i = this->name2id.find(name);
100         return i == this->name2id.end() ? nullopt : optional<unsigned>(i->second);
101     }
102
103     optional<std::string> lookup(unsigned id) const
104     {
105         auto i = this->id2name.find(id);
106         return i == this->id2name.end() ? nullopt
107                                         : optional<std::string>(i->second);
108     }
109
110     // Remove a surface id and name
111     void remove_id(std::string const &name)
112     {
113         auto i = this->name2id.find(name);
114         if (i != this->name2id.end())
115         {
116             this->id2name.erase(i->second);
117             this->name2id.erase(i);
118         }
119     }
120
121     void remove_id(unsigned id)
122     {
123         auto i = this->id2name.find(id);
124         if (i != this->id2name.end())
125         {
126             this->name2id.erase(i->second);
127             this->id2name.erase(i);
128         }
129     }
130 };
131
132 struct TmpClient
133 {
134     std::string   appid;
135     unsigned layer;
136 };
137
138
139 class WindowManager
140 {
141   public:
142     typedef std::unordered_map<uint32_t, struct rect> rect_map;
143     typedef std::function<void(const char *err_msg)> reply_func;
144
145     enum EventType
146     {
147         Event_Val_Min = 0,
148
149         Event_Active = Event_Val_Min,
150         Event_Inactive,
151
152         Event_Visible,
153         Event_Invisible,
154
155         Event_SyncDraw,
156         Event_FlushDraw,
157
158         Event_ScreenUpdated,
159
160         Event_Error,
161
162         Event_Val_Max = Event_Error,
163     };
164
165     explicit WindowManager();
166     ~WindowManager() = default;
167
168     WindowManager(WindowManager const &) = delete;
169     WindowManager &operator=(WindowManager const &) = delete;
170     WindowManager(WindowManager &&) = delete;
171     WindowManager &operator=(WindowManager &&) = delete;
172
173     int init();
174
175     result<int> api_request_surface(char const *appid, char const *role);
176     char const *api_request_surface(char const *appid, char const *role, char const *ivi_id);
177     void api_activate_window(char const *appid, char const *role, char const *drawing_area, const reply_func &reply);
178     void api_deactivate_window(char const *appid, char const *role, const reply_func &reply);
179     void api_enddraw(char const *appid, char const *role);
180     bool api_subscribe(afb_req_t req, EventType event_id);
181     result<json_object *> api_get_display_info();
182     result<json_object *> api_get_area_info(char const *role);
183
184     // Events from the compositor we are interested in
185     void surface_created(unsigned surface_id);
186     void surface_removed(unsigned surface_id);
187
188     void removeClient(const std::string &appid);
189     void exceptionProcessForTransition();
190     const char* convertRoleOldToNew(char const *role);
191
192     // Do not use this function
193     void timerHandler();
194     void startTransitionWrapper(std::vector<WMAction> &actions);
195     void processError(WMError error);
196
197   private:
198     WMError setRequest(const std::string &appid, const std::string &role, const std::string &area,
199                              Task task, unsigned *req_num);
200     WMError checkPolicy(unsigned req_num);
201     WMError startTransition(unsigned req_num);
202
203     WMError doEndDraw(unsigned req_num);
204     void    emitScreenUpdated(unsigned req_num);
205
206     void setTimer();
207     void stopTimer();
208     void processNextRequest();
209
210   private:
211     std::map<std::string, afb_event_t> map_afb_event;
212     std::unordered_map<std::string, struct rect> area2size;
213     std::shared_ptr<LayerControl> lc;
214     std::unordered_map<std::string, std::string> roleold2new;
215     std::unordered_map<std::string, std::string> rolenew2old;
216
217     PMWrapper pmw;
218     rect_map area_info;
219     struct id_allocator id_alloc;
220
221     // ID allocation and proxy methods for lookup
222     std::unordered_map<unsigned, struct TmpClient> tmp_surface2app;
223     int loadOldRoleDb();
224     static const char* kDefaultOldRoleDb;
225 };
226
227 } // namespace wm
228
229 #endif // WINDOW_MANAGER_HPP