Hide variable into private
[apps/agl-service-windowmanager-2017.git] / src / window_manager.hpp
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 #ifndef WINDOW_MANAGER_HPP
18 #define WINDOW_MANAGER_HPP
19
20 #include <atomic>
21 #include <memory>
22 #include <unordered_map>
23 #include <experimental/optional>
24 #include "controller_hooks.hpp"
25 //#include "layers.hpp"
26 // #include "layout.hpp"
27 //#include "wayland_ivi_wm.hpp"
28 #include "result.hpp"
29 #include "pm_wrapper.hpp"
30 #include "util.hpp"
31 #include "request.hpp"
32 #include "wm_error.hpp"
33 #include "wm_layer_control.hpp"
34 extern "C"
35 {
36 #include <afb/afb-binding.h>
37 }
38
39 struct json_object;
40
41 namespace wl
42 {
43 struct display;
44 }
45
46 namespace compositor
47 {
48 struct controller;
49 }
50
51 namespace wm
52 {
53
54 using std::experimental::optional;
55
56 /* DrawingArea name used by "{layout}.{area}" */
57 extern const char kNameLayoutNormal[];
58 extern const char kNameLayoutSplit[];
59 extern const char kNameAreaFull[];
60 extern const char kNameAreaMain[];
61 extern const char kNameAreaSub[];
62
63 /* Key for json obejct */
64 extern const char kKeyDrawingName[];
65 extern const char kKeyDrawingArea[];
66 extern const char kKeyDrawingRect[];
67 extern const char kKeyX[];
68 extern const char kKeyY[];
69 extern const char kKeyWidth[];
70 extern const char kKeyHeigh[];
71 extern const char kKeyWidthPixel[];
72 extern const char kKeyHeightPixel[];
73 extern const char kKeyWidthMm[];
74 extern const char kKeyHeightMm[];
75 extern const char kKeyScale[];
76 extern const char kKeyIds[];
77
78 struct id_allocator
79 {
80     unsigned next = 1;
81
82     // Surfaces that where requested but not yet created
83     std::unordered_map<unsigned, std::string> id2name;
84     std::unordered_map<std::string, unsigned> name2id;
85
86     id_allocator(id_allocator const &) = delete;
87     id_allocator(id_allocator &&) = delete;
88     id_allocator &operator=(id_allocator const &);
89     id_allocator &operator=(id_allocator &&) = delete;
90
91     // Insert and return a new ID
92     unsigned generate_id(std::string const &name)
93     {
94         unsigned sid = this->next++;
95         this->id2name[sid] = name;
96         this->name2id[name] = sid;
97         HMI_DEBUG("allocated new id %u with name %s", sid, name.c_str());
98         return sid;
99     }
100
101     // Insert a new ID which defined outside
102     void register_name_id(std::string const &name, unsigned sid)
103     {
104         this->id2name[sid] = name;
105         this->name2id[name] = sid;
106         HMI_DEBUG("register id %u with name %s", sid, name.c_str());
107         return;
108     }
109
110     // Lookup by ID or by name
111     optional<unsigned> lookup(std::string const &name) const
112     {
113         auto i = this->name2id.find(name);
114         return i == this->name2id.end() ? nullopt : optional<unsigned>(i->second);
115     }
116
117     optional<std::string> lookup(unsigned id) const
118     {
119         auto i = this->id2name.find(id);
120         return i == this->id2name.end() ? nullopt
121                                         : optional<std::string>(i->second);
122     }
123
124     // Remove a surface id and name
125     void remove_id(std::string const &name)
126     {
127         auto i = this->name2id.find(name);
128         if (i != this->name2id.end())
129         {
130             this->id2name.erase(i->second);
131             this->name2id.erase(i);
132         }
133     }
134
135     void remove_id(unsigned id)
136     {
137         auto i = this->id2name.find(id);
138         if (i != this->id2name.end())
139         {
140             this->name2id.erase(i->second);
141             this->id2name.erase(i);
142         }
143     }
144 };
145
146 class WindowManager
147 {
148   public:
149     typedef std::unordered_map<uint32_t, struct rect> rect_map;
150     typedef std::function<void(const char *err_msg)> reply_func;
151
152     enum EventType
153     {
154         Event_Val_Min = 0,
155
156         Event_Active = Event_Val_Min,
157         Event_Inactive,
158
159         Event_Visible,
160         Event_Invisible,
161
162         Event_SyncDraw,
163         Event_FlushDraw,
164
165         Event_ScreenUpdated,
166
167         Event_Error,
168
169         Event_Val_Max = Event_Error,
170     };
171
172     explicit WindowManager();
173     ~WindowManager() = default;
174
175     WindowManager(WindowManager const &) = delete;
176     WindowManager &operator=(WindowManager const &) = delete;
177     WindowManager(WindowManager &&) = delete;
178     WindowManager &operator=(WindowManager &&) = delete;
179
180     int init();
181
182     result<int> api_request_surface(char const *appid, char const *role);
183     char const *api_request_surface(char const *appid, char const *role, char const *ivi_id);
184     bool api_set_role(char const *appid, char const *role, unsigned pid);
185     void api_activate_surface(char const *appid, char const *role, char const *drawing_area, const reply_func &reply);
186     void api_deactivate_surface(char const *appid, char const *role, const reply_func &reply);
187     void api_enddraw(char const *appid, char const *role);
188     result<json_object *> api_get_display_info();
189     result<json_object *> api_get_area_info(char const *role);
190     void api_ping();
191     void send_event(char const *evname, char const *label);
192     void send_event(char const *evname, char const *label, char const *area, int x, int y, int w, int h);
193
194     // Events from the compositor we are interested in
195     void surface_created(uint32_t surface_id);
196     void surface_removed(uint32_t surface_id);
197     void surface_properties(uint32_t surface_id, uint32_t pid);
198
199     void removeClient(const std::string &appid);
200     void exceptionProcessForTransition();
201     const char* convertRoleOldToNew(char const *role);
202
203     // Do not use this function
204     void timerHandler();
205     void startTransitionWrapper(std::vector<WMAction> &actions);
206     void processError(WMError error);
207
208     const std::vector<const char *> kListEventName{
209         "active",
210         "inactive",
211         "visible",
212         "invisible",
213         "syncDraw",
214         "flushDraw",
215         "screenUpdated",
216         "error"};
217     std::map<const char *, struct afb_event> map_afb_event;
218
219   private:
220     int init_layers();
221     void surface_set_layout(int surface_id, const std::string& area = "");
222     void layout_commit();
223
224     // WM Events to clients
225     void emit_activated(char const *label);
226     void emit_deactivated(char const *label);
227     void emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h);
228     void emit_syncdraw(const std::string &role, const std::string &area);
229     void emit_flushdraw(char const *label);
230     void emit_visible(char const *label, bool is_visible);
231     void emit_invisible(char const *label);
232     void emit_visible(char const *label);
233
234     void activate(int id);
235     void deactivate(int id);
236     WMError setRequest(const std::string &appid, const std::string &role, const std::string &area,
237                              Task task, unsigned *req_num);
238     WMError doTransition(unsigned sequence_number);
239     WMError checkPolicy(unsigned req_num);
240     WMError startTransition(unsigned req_num);
241
242     WMError doEndDraw(unsigned req_num);
243     WMError layoutChange(const WMAction &action);
244     WMError visibilityChange(const WMAction &action);
245     WMError setSurfaceSize(unsigned surface, const std::string& area);
246     void    emitScreenUpdated(unsigned req_num);
247
248     void setTimer();
249     void stopTimer();
250     void processNextRequest();
251
252     int loadOldRoleDb();
253
254     const char *check_surface_exist(const char *role);
255
256   private:
257     std::unordered_map<std::string, struct rect> area2size;
258     std::unordered_map<std::string, std::string> roleold2new;
259     std::unordered_map<std::string, std::string> rolenew2old;
260     std::shared_ptr<LayerControl> lc;
261     PMWrapper pmw;
262
263     struct controller_hooks chooks;
264     // ID allocation and proxy methods for lookup
265     struct id_allocator id_alloc;
266     // Surface are info (x, y, w, h)
267     rect_map area_info;
268     // FOR CES DEMO
269     std::vector<int> surface_bg;
270
271     static const char* kDefaultOldRoleDb;
272 };
273
274 } // namespace wm
275
276 #endif // WINDOW_MANAGER_HPP