546e771235a572f2b67a9d412600ad4ddfbcb8d2
[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 "result.hpp"
25 #include "pm_wrapper.hpp"
26 #include "util.hpp"
27 #include "request.hpp"
28 #include "wm_error.hpp"
29 #include "wm_layer_control.hpp"
30 extern "C"
31 {
32 #include <afb/afb-binding.h>
33 }
34
35 struct json_object;
36
37 namespace wm
38 {
39
40 using std::experimental::optional;
41
42 /* DrawingArea name used by "{layout}.{area}" */
43 extern const char kNameLayoutNormal[];
44 extern const char kNameLayoutSplit[];
45 extern const char kNameAreaFull[];
46 extern const char kNameAreaMain[];
47 extern const char kNameAreaSub[];
48
49 /* Key for json obejct */
50 extern const char kKeyDrawingName[];
51 extern const char kKeyDrawingArea[];
52 extern const char kKeyDrawingRect[];
53 extern const char kKeyX[];
54 extern const char kKeyY[];
55 extern const char kKeyWidth[];
56 extern const char kKeyHeigh[];
57 extern const char kKeyWidthPixel[];
58 extern const char kKeyHeightPixel[];
59 extern const char kKeyWidthMm[];
60 extern const char kKeyHeightMm[];
61 extern const char kKeyScale[];
62 extern const char kKeyIds[];
63
64 struct id_allocator
65 {
66     unsigned next = 1;
67
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 class WindowManager
133 {
134   public:
135     typedef std::unordered_map<uint32_t, struct rect> rect_map;
136     typedef std::function<void(const char *err_msg)> reply_func;
137
138     enum EventType
139     {
140         Event_Val_Min = 0,
141
142         Event_Active = Event_Val_Min,
143         Event_Inactive,
144
145         Event_Visible,
146         Event_Invisible,
147
148         Event_SyncDraw,
149         Event_FlushDraw,
150
151         Event_ScreenUpdated,
152
153         Event_Error,
154
155         Event_Val_Max = Event_Error,
156     };
157
158     explicit WindowManager();
159     ~WindowManager() = default;
160
161     WindowManager(WindowManager const &) = delete;
162     WindowManager &operator=(WindowManager const &) = delete;
163     WindowManager(WindowManager &&) = delete;
164     WindowManager &operator=(WindowManager &&) = delete;
165
166     int init();
167
168     result<int> api_request_surface(char const *appid, char const *role);
169     char const *api_request_surface(char const *appid, char const *role, char const *ivi_id);
170     bool api_set_role(char const *appid, char const *role, unsigned pid);
171     void api_activate_surface(char const *appid, char const *role, char const *drawing_area, const reply_func &reply);
172     void api_deactivate_surface(char const *appid, char const *role, const reply_func &reply);
173     void api_enddraw(char const *appid, char const *role);
174     result<json_object *> api_get_display_info();
175     result<json_object *> api_get_area_info(char const *role);
176     void send_event(char const *evname, char const *label);
177     void send_event(char const *evname, char const *label, char const *area, int x, int y, int w, int h);
178
179     // Events from the compositor we are interested in
180     void surface_created(uint32_t surface_id);
181     void surface_removed(uint32_t surface_id);
182
183     void removeClient(const std::string &appid);
184     void exceptionProcessForTransition();
185     const char* convertRoleOldToNew(char const *role);
186
187     // Do not use this function
188     void timerHandler();
189     void startTransitionWrapper(std::vector<WMAction> &actions);
190     void processError(WMError error);
191
192     const std::vector<const char *> kListEventName{
193         "active",
194         "inactive",
195         "visible",
196         "invisible",
197         "syncDraw",
198         "flushDraw",
199         "screenUpdated",
200         "error"};
201     std::map<const char *, struct afb_event> map_afb_event;
202
203   private:
204     // WM Events to clients
205     void emit_activated(char const *label);
206     void emit_deactivated(char const *label);
207     void emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h);
208     void emit_syncdraw(const std::string &role, const std::string &area);
209     void emit_flushdraw(char const *label);
210     void emit_visible(char const *label, bool is_visible);
211     void emit_invisible(char const *label);
212     void emit_visible(char const *label);
213
214     void activate(int id);
215     void deactivate(int id);
216     WMError setRequest(const std::string &appid, const std::string &role, const std::string &area,
217                              Task task, unsigned *req_num);
218     WMError checkPolicy(unsigned req_num);
219     WMError startTransition(unsigned req_num);
220
221     WMError doEndDraw(unsigned req_num);
222     void    emitScreenUpdated(unsigned req_num);
223
224     void setTimer();
225     void stopTimer();
226     void processNextRequest();
227
228     int loadOldRoleDb();
229
230     const char *check_surface_exist(const char *role);
231
232   private:
233     std::unordered_map<std::string, std::string> roleold2new;
234     std::unordered_map<std::string, std::string> rolenew2old;
235     std::shared_ptr<LayerControl> lc;
236     PMWrapper pmw;
237
238     // ID allocation and proxy methods for lookup
239     struct id_allocator id_alloc;
240     // Surface are info (x, y, w, h)
241     rect_map area_info;
242     // FOR CES DEMO
243     std::unordered_map<unsigned, std::string> tmp_surface2app;
244
245     static const char* kDefaultOldRoleDb;
246 };
247
248 } // namespace wm
249
250 #endif // WINDOW_MANAGER_HPP