2 * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef WINDOW_MANAGER_HPP
18 #define WINDOW_MANAGER_HPP
22 #include <unordered_map>
23 #include <experimental/optional>
25 #include "pm_wrapper.hpp"
27 #include "request.hpp"
28 #include "wm_error.hpp"
29 #include "wm_layer_control.hpp"
32 #include <afb/afb-binding.h>
40 using std::experimental::optional;
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[];
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[];
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;
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;
77 // Insert and return a new ID
78 unsigned generate_id(std::string const &name)
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());
87 // Insert a new ID which defined outside
88 void register_name_id(std::string const &name, unsigned sid)
90 this->id2name[sid] = name;
91 this->name2id[name] = sid;
92 HMI_DEBUG("register id %u with name %s", sid, name.c_str());
96 // Lookup by ID or by name
97 optional<unsigned> lookup(std::string const &name) const
99 auto i = this->name2id.find(name);
100 return i == this->name2id.end() ? nullopt : optional<unsigned>(i->second);
103 optional<std::string> lookup(unsigned id) const
105 auto i = this->id2name.find(id);
106 return i == this->id2name.end() ? nullopt
107 : optional<std::string>(i->second);
110 // Remove a surface id and name
111 void remove_id(std::string const &name)
113 auto i = this->name2id.find(name);
114 if (i != this->name2id.end())
116 this->id2name.erase(i->second);
117 this->name2id.erase(i);
121 void remove_id(unsigned id)
123 auto i = this->id2name.find(id);
124 if (i != this->id2name.end())
126 this->name2id.erase(i->second);
127 this->id2name.erase(i);
141 typedef std::unordered_map<uint32_t, struct rect> rect_map;
142 typedef std::function<void(const char *err_msg)> reply_func;
148 Event_Active = Event_Val_Min,
161 Event_Val_Max = Event_Error,
164 explicit WindowManager();
165 ~WindowManager() = default;
167 WindowManager(WindowManager const &) = delete;
168 WindowManager &operator=(WindowManager const &) = delete;
169 WindowManager(WindowManager &&) = delete;
170 WindowManager &operator=(WindowManager &&) = delete;
174 result<int> api_request_surface(char const *appid, char const *role);
175 char const *api_request_surface(char const *appid, char const *role, char const *ivi_id);
176 bool api_set_role(char const *appid, char const *role);
177 void api_activate_surface(char const *appid, char const *role, char const *drawing_area, const reply_func &reply);
178 void api_deactivate_surface(char const *appid, char const *role, const reply_func &reply);
179 void api_enddraw(char const *appid, char const *role);
180 bool api_client_set_render_order(const char *appid, const std::vector<std::string> &render_order);
181 std::string api_client_attach_service_surface(const char* appid, const char* dest, const char* service_surface);
182 result<json_object *> api_get_display_info();
183 result<json_object *> api_get_area_info(char const *role);
184 void send_event(char const *evname, char const *label);
185 void send_event(char const *evname, char const *label, char const *area, int x, int y, int w, int h);
187 // Events from the compositor we are interested in
188 void surface_created(unsigned pid, unsigned surface_id);
189 void surface_removed(unsigned surface_id);
191 void removeClient(const std::string &appid);
192 void exceptionProcessForTransition();
193 const char* convertRoleOldToNew(char const *role);
195 // Do not use this function
197 void startTransitionWrapper(std::vector<WMAction> &actions);
198 void processError(WMError error);
200 const std::vector<const char *> kListEventName{
209 std::map<const char *, struct afb_event> map_afb_event;
212 // WM Events to clients
213 void emit_activated(char const *label);
214 void emit_deactivated(char const *label);
215 void emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h);
216 void emit_syncdraw(const std::string &role, const std::string &area);
217 void emit_flushdraw(char const *label);
218 void emit_visible(char const *label, bool is_visible);
219 void emit_invisible(char const *label);
220 void emit_visible(char const *label);
222 void activate(int id);
223 void deactivate(int id);
224 WMError setRequest(const std::string &appid, const std::string &role, const std::string &area,
225 Task task, unsigned *req_num);
226 WMError checkPolicy(unsigned req_num);
227 WMError startTransition(unsigned req_num);
229 WMError doEndDraw(unsigned req_num);
230 void emitScreenUpdated(unsigned req_num);
234 void processNextRequest();
238 const char *check_surface_exist(const char *role);
241 std::unordered_map<std::string, std::string> roleold2new;
242 std::unordered_map<std::string, std::string> rolenew2old;
243 std::shared_ptr<LayerControl> lc;
246 // ID allocation and proxy methods for lookup
247 struct id_allocator id_alloc;
248 // Surface are info (x, y, w, h)
251 std::unordered_map<unsigned, struct TmpClient> tmp_surface2app;
252 std::vector<struct TmpClient> tmp_apps;
253 static const char* kDefaultOldRoleDb;
258 #endif // WINDOW_MANAGER_HPP