Add gitlab issue/merge request templates
[apps/agl-service-windowmanager.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     // Surfaces that where requested but not yet created
68     std::unordered_map<unsigned, std::string> id2name;
69     std::unordered_map<std::string, unsigned> name2id;
70
71     id_allocator(id_allocator const &) = delete;
72     id_allocator(id_allocator &&) = delete;
73     id_allocator &operator=(id_allocator const &);
74     id_allocator &operator=(id_allocator &&) = delete;
75
76     // Insert and return a new ID
77     unsigned generate_id(std::string const &name)
78     {
79         unsigned sid = this->next++;
80         this->id2name[sid] = name;
81         this->name2id[name] = sid;
82         HMI_DEBUG("allocated new id %u with name %s", sid, name.c_str());
83         return sid;
84     }
85
86     // Insert a new ID which defined outside
87     void register_name_id(std::string const &name, unsigned sid)
88     {
89         this->id2name[sid] = name;
90         this->name2id[name] = sid;
91         HMI_DEBUG("register id %u with name %s", sid, name.c_str());
92         return;
93     }
94
95     // Lookup by ID or by name
96     optional<unsigned> lookup(std::string const &name) const
97     {
98         auto i = this->name2id.find(name);
99         return i == this->name2id.end() ? nullopt : optional<unsigned>(i->second);
100     }
101
102     optional<std::string> lookup(unsigned id) const
103     {
104         auto i = this->id2name.find(id);
105         return i == this->id2name.end() ? nullopt
106                                         : optional<std::string>(i->second);
107     }
108
109     // Remove a surface id and name
110     void remove_id(std::string const &name)
111     {
112         auto i = this->name2id.find(name);
113         if (i != this->name2id.end())
114         {
115             this->id2name.erase(i->second);
116             this->name2id.erase(i);
117         }
118     }
119
120     void remove_id(unsigned id)
121     {
122         auto i = this->id2name.find(id);
123         if (i != this->id2name.end())
124         {
125             this->name2id.erase(i->second);
126             this->id2name.erase(i);
127         }
128     }
129 };
130
131 struct TmpClient
132 {
133     std::string   appid;
134     unsigned layer;
135 };
136
137 class WindowManager
138 {
139   public:
140     typedef std::unordered_map<uint32_t, struct rect> rect_map;
141     typedef std::function<void(const char *err_msg)> reply_func;
142
143     enum EventType
144     {
145         Event_Val_Min = 0,
146
147         Event_Active = Event_Val_Min,
148         Event_Inactive,
149
150         Event_Visible,
151         Event_Invisible,
152
153         Event_SyncDraw,
154         Event_FlushDraw,
155
156         Event_ScreenUpdated,
157
158         Event_Error,
159
160         Event_Val_Max = Event_Error,
161     };
162
163     explicit WindowManager();
164     ~WindowManager() = default;
165
166     WindowManager(WindowManager const &) = delete;
167     WindowManager &operator=(WindowManager const &) = delete;
168     WindowManager(WindowManager &&) = delete;
169     WindowManager &operator=(WindowManager &&) = delete;
170
171     int init();
172
173     result<int> api_request_surface(char const *appid, char const *role);
174     char const *api_request_surface(char const *appid, char const *role, char const *ivi_id);
175     void api_activate_window(char const *appid, char const *role, char const *drawing_area, const reply_func &reply);
176     void api_deactivate_window(char const *appid, char const *role, const reply_func &reply);
177     void api_enddraw(char const *appid, char const *role);
178     json_object* api_get_area_list();
179     void api_change_area_size(ChangeAreaReq &areas);
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
191     // Do not use this function
192     void timerHandler();
193     void startTransitionWrapper(std::vector<WMAction> &actions);
194     void processError(WMError error);
195
196   private:
197     unsigned generateLayerForClient(const std::string &role);
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     PMWrapper pmw;
215     rect_map area_info;
216     struct id_allocator id_alloc;
217
218     // ID allocation and proxy methods for lookup
219     std::unordered_map<unsigned, struct TmpClient> tmp_surface2app;
220 };
221
222 } // namespace wm
223
224 #endif // WINDOW_MANAGER_HPP